dtt200u-fe.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Frontend part of the Linux driver for the WideView/ Yakumo/ Hama/
  2. * Typhoon/ Yuan DVB-T USB2.0 receiver.
  3. *
  4. * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@desy.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation, version 2.
  9. *
  10. * see Documentation/dvb/README.dvb-usb for more information
  11. */
  12. #include "dtt200u.h"
  13. struct dtt200u_fe_state {
  14. struct dvb_usb_device *d;
  15. enum fe_status stat;
  16. struct dtv_frontend_properties fep;
  17. struct dvb_frontend frontend;
  18. };
  19. static int dtt200u_fe_read_status(struct dvb_frontend *fe,
  20. enum fe_status *stat)
  21. {
  22. struct dtt200u_fe_state *state = fe->demodulator_priv;
  23. u8 st = GET_TUNE_STATUS, b[3];
  24. dvb_usb_generic_rw(state->d,&st,1,b,3,0);
  25. switch (b[0]) {
  26. case 0x01:
  27. *stat = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  28. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  29. break;
  30. case 0x00: /* pending */
  31. *stat = FE_TIMEDOUT; /* during set_frontend */
  32. break;
  33. default:
  34. case 0x02: /* failed */
  35. *stat = 0;
  36. break;
  37. }
  38. return 0;
  39. }
  40. static int dtt200u_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  41. {
  42. struct dtt200u_fe_state *state = fe->demodulator_priv;
  43. u8 bw = GET_VIT_ERR_CNT,b[3];
  44. dvb_usb_generic_rw(state->d,&bw,1,b,3,0);
  45. *ber = (b[0] << 16) | (b[1] << 8) | b[2];
  46. return 0;
  47. }
  48. static int dtt200u_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  49. {
  50. struct dtt200u_fe_state *state = fe->demodulator_priv;
  51. u8 bw = GET_RS_UNCOR_BLK_CNT,b[2];
  52. dvb_usb_generic_rw(state->d,&bw,1,b,2,0);
  53. *unc = (b[0] << 8) | b[1];
  54. return 0;
  55. }
  56. static int dtt200u_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  57. {
  58. struct dtt200u_fe_state *state = fe->demodulator_priv;
  59. u8 bw = GET_AGC, b;
  60. dvb_usb_generic_rw(state->d,&bw,1,&b,1,0);
  61. *strength = (b << 8) | b;
  62. return 0;
  63. }
  64. static int dtt200u_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  65. {
  66. struct dtt200u_fe_state *state = fe->demodulator_priv;
  67. u8 bw = GET_SNR,br;
  68. dvb_usb_generic_rw(state->d,&bw,1,&br,1,0);
  69. *snr = ~((br << 8) | br);
  70. return 0;
  71. }
  72. static int dtt200u_fe_init(struct dvb_frontend* fe)
  73. {
  74. struct dtt200u_fe_state *state = fe->demodulator_priv;
  75. u8 b = SET_INIT;
  76. return dvb_usb_generic_write(state->d,&b,1);
  77. }
  78. static int dtt200u_fe_sleep(struct dvb_frontend* fe)
  79. {
  80. return dtt200u_fe_init(fe);
  81. }
  82. static int dtt200u_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  83. {
  84. tune->min_delay_ms = 1500;
  85. tune->step_size = 0;
  86. tune->max_drift = 0;
  87. return 0;
  88. }
  89. static int dtt200u_fe_set_frontend(struct dvb_frontend *fe)
  90. {
  91. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  92. struct dtt200u_fe_state *state = fe->demodulator_priv;
  93. int i;
  94. enum fe_status st;
  95. u16 freq = fep->frequency / 250000;
  96. u8 bwbuf[2] = { SET_BANDWIDTH, 0 },freqbuf[3] = { SET_RF_FREQ, 0, 0 };
  97. switch (fep->bandwidth_hz) {
  98. case 8000000:
  99. bwbuf[1] = 8;
  100. break;
  101. case 7000000:
  102. bwbuf[1] = 7;
  103. break;
  104. case 6000000:
  105. bwbuf[1] = 6;
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. dvb_usb_generic_write(state->d,bwbuf,2);
  111. freqbuf[1] = freq & 0xff;
  112. freqbuf[2] = (freq >> 8) & 0xff;
  113. dvb_usb_generic_write(state->d,freqbuf,3);
  114. for (i = 0; i < 30; i++) {
  115. msleep(20);
  116. dtt200u_fe_read_status(fe, &st);
  117. if (st & FE_TIMEDOUT)
  118. continue;
  119. }
  120. return 0;
  121. }
  122. static int dtt200u_fe_get_frontend(struct dvb_frontend* fe)
  123. {
  124. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  125. struct dtt200u_fe_state *state = fe->demodulator_priv;
  126. memcpy(fep, &state->fep, sizeof(struct dtv_frontend_properties));
  127. return 0;
  128. }
  129. static void dtt200u_fe_release(struct dvb_frontend* fe)
  130. {
  131. struct dtt200u_fe_state *state = (struct dtt200u_fe_state*) fe->demodulator_priv;
  132. kfree(state);
  133. }
  134. static struct dvb_frontend_ops dtt200u_fe_ops;
  135. struct dvb_frontend* dtt200u_fe_attach(struct dvb_usb_device *d)
  136. {
  137. struct dtt200u_fe_state* state = NULL;
  138. /* allocate memory for the internal state */
  139. state = kzalloc(sizeof(struct dtt200u_fe_state), GFP_KERNEL);
  140. if (state == NULL)
  141. goto error;
  142. deb_info("attaching frontend dtt200u\n");
  143. state->d = d;
  144. memcpy(&state->frontend.ops,&dtt200u_fe_ops,sizeof(struct dvb_frontend_ops));
  145. state->frontend.demodulator_priv = state;
  146. return &state->frontend;
  147. error:
  148. return NULL;
  149. }
  150. static struct dvb_frontend_ops dtt200u_fe_ops = {
  151. .delsys = { SYS_DVBT },
  152. .info = {
  153. .name = "WideView USB DVB-T",
  154. .frequency_min = 44250000,
  155. .frequency_max = 867250000,
  156. .frequency_stepsize = 250000,
  157. .caps = FE_CAN_INVERSION_AUTO |
  158. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  159. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  160. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  161. FE_CAN_TRANSMISSION_MODE_AUTO |
  162. FE_CAN_GUARD_INTERVAL_AUTO |
  163. FE_CAN_RECOVER |
  164. FE_CAN_HIERARCHY_AUTO,
  165. },
  166. .release = dtt200u_fe_release,
  167. .init = dtt200u_fe_init,
  168. .sleep = dtt200u_fe_sleep,
  169. .set_frontend = dtt200u_fe_set_frontend,
  170. .get_frontend = dtt200u_fe_get_frontend,
  171. .get_tune_settings = dtt200u_fe_get_tune_settings,
  172. .read_status = dtt200u_fe_read_status,
  173. .read_ber = dtt200u_fe_read_ber,
  174. .read_signal_strength = dtt200u_fe_read_signal_strength,
  175. .read_snr = dtt200u_fe_read_snr,
  176. .read_ucblocks = dtt200u_fe_read_unc_blocks,
  177. };