dtt200u-fe.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. fe_status_t stat;
  16. struct dvb_frontend_parameters fep;
  17. struct dvb_frontend frontend;
  18. };
  19. static int dtt200u_fe_read_status(struct dvb_frontend* fe, fe_status_t *stat)
  20. {
  21. struct dtt200u_fe_state *state = fe->demodulator_priv;
  22. u8 st = GET_TUNE_STATUS, b[3];
  23. dvb_usb_generic_rw(state->d,&st,1,b,3,0);
  24. switch (b[0]) {
  25. case 0x01:
  26. *stat = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  27. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  28. break;
  29. case 0x00: /* pending */
  30. *stat = FE_TIMEDOUT; /* during set_frontend */
  31. break;
  32. default:
  33. case 0x02: /* failed */
  34. *stat = 0;
  35. break;
  36. }
  37. return 0;
  38. }
  39. static int dtt200u_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  40. {
  41. struct dtt200u_fe_state *state = fe->demodulator_priv;
  42. u8 bw = GET_VIT_ERR_CNT,b[3];
  43. dvb_usb_generic_rw(state->d,&bw,1,b,3,0);
  44. *ber = (b[0] << 16) | (b[1] << 8) | b[2];
  45. return 0;
  46. }
  47. static int dtt200u_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  48. {
  49. struct dtt200u_fe_state *state = fe->demodulator_priv;
  50. u8 bw = GET_RS_UNCOR_BLK_CNT,b[2];
  51. dvb_usb_generic_rw(state->d,&bw,1,b,2,0);
  52. *unc = (b[0] << 8) | b[1];
  53. return 0;
  54. }
  55. static int dtt200u_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  56. {
  57. struct dtt200u_fe_state *state = fe->demodulator_priv;
  58. u8 bw = GET_AGC, b;
  59. dvb_usb_generic_rw(state->d,&bw,1,&b,1,0);
  60. *strength = (b << 8) | b;
  61. return 0;
  62. }
  63. static int dtt200u_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  64. {
  65. struct dtt200u_fe_state *state = fe->demodulator_priv;
  66. u8 bw = GET_SNR,br;
  67. dvb_usb_generic_rw(state->d,&bw,1,&br,1,0);
  68. *snr = ~((br << 8) | br);
  69. return 0;
  70. }
  71. static int dtt200u_fe_init(struct dvb_frontend* fe)
  72. {
  73. struct dtt200u_fe_state *state = fe->demodulator_priv;
  74. u8 b = SET_INIT;
  75. return dvb_usb_generic_write(state->d,&b,1);
  76. }
  77. static int dtt200u_fe_sleep(struct dvb_frontend* fe)
  78. {
  79. return dtt200u_fe_init(fe);
  80. }
  81. static int dtt200u_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  82. {
  83. tune->min_delay_ms = 1500;
  84. tune->step_size = 0;
  85. tune->max_drift = 0;
  86. return 0;
  87. }
  88. static int dtt200u_fe_set_frontend(struct dvb_frontend* fe,
  89. struct dvb_frontend_parameters *fep)
  90. {
  91. struct dtt200u_fe_state *state = fe->demodulator_priv;
  92. int i;
  93. fe_status_t st;
  94. u16 freq = fep->frequency / 250000;
  95. u8 bwbuf[2] = { SET_BANDWIDTH, 0 },freqbuf[3] = { SET_RF_FREQ, 0, 0 };
  96. switch (fep->u.ofdm.bandwidth) {
  97. case BANDWIDTH_8_MHZ: bwbuf[1] = 8; break;
  98. case BANDWIDTH_7_MHZ: bwbuf[1] = 7; break;
  99. case BANDWIDTH_6_MHZ: bwbuf[1] = 6; break;
  100. case BANDWIDTH_AUTO: return -EOPNOTSUPP;
  101. default:
  102. return -EINVAL;
  103. }
  104. dvb_usb_generic_write(state->d,bwbuf,2);
  105. freqbuf[1] = freq & 0xff;
  106. freqbuf[2] = (freq >> 8) & 0xff;
  107. dvb_usb_generic_write(state->d,freqbuf,3);
  108. for (i = 0; i < 30; i++) {
  109. msleep(20);
  110. dtt200u_fe_read_status(fe, &st);
  111. if (st & FE_TIMEDOUT)
  112. continue;
  113. }
  114. return 0;
  115. }
  116. static int dtt200u_fe_get_frontend(struct dvb_frontend* fe,
  117. struct dvb_frontend_parameters *fep)
  118. {
  119. struct dtt200u_fe_state *state = fe->demodulator_priv;
  120. memcpy(fep,&state->fep,sizeof(struct dvb_frontend_parameters));
  121. return 0;
  122. }
  123. static void dtt200u_fe_release(struct dvb_frontend* fe)
  124. {
  125. struct dtt200u_fe_state *state = (struct dtt200u_fe_state*) fe->demodulator_priv;
  126. kfree(state);
  127. }
  128. static struct dvb_frontend_ops dtt200u_fe_ops;
  129. struct dvb_frontend* dtt200u_fe_attach(struct dvb_usb_device *d)
  130. {
  131. struct dtt200u_fe_state* state = NULL;
  132. /* allocate memory for the internal state */
  133. state = kzalloc(sizeof(struct dtt200u_fe_state), GFP_KERNEL);
  134. if (state == NULL)
  135. goto error;
  136. deb_info("attaching frontend dtt200u\n");
  137. state->d = d;
  138. memcpy(&state->frontend.ops,&dtt200u_fe_ops,sizeof(struct dvb_frontend_ops));
  139. state->frontend.demodulator_priv = state;
  140. return &state->frontend;
  141. error:
  142. return NULL;
  143. }
  144. static struct dvb_frontend_ops dtt200u_fe_ops = {
  145. .info = {
  146. .name = "WideView USB DVB-T",
  147. .type = FE_OFDM,
  148. .frequency_min = 44250000,
  149. .frequency_max = 867250000,
  150. .frequency_stepsize = 250000,
  151. .caps = FE_CAN_INVERSION_AUTO |
  152. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  153. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  154. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  155. FE_CAN_TRANSMISSION_MODE_AUTO |
  156. FE_CAN_GUARD_INTERVAL_AUTO |
  157. FE_CAN_RECOVER |
  158. FE_CAN_HIERARCHY_AUTO,
  159. },
  160. .release = dtt200u_fe_release,
  161. .init = dtt200u_fe_init,
  162. .sleep = dtt200u_fe_sleep,
  163. .set_frontend = dtt200u_fe_set_frontend,
  164. .get_frontend = dtt200u_fe_get_frontend,
  165. .get_tune_settings = dtt200u_fe_get_tune_settings,
  166. .read_status = dtt200u_fe_read_status,
  167. .read_ber = dtt200u_fe_read_ber,
  168. .read_signal_strength = dtt200u_fe_read_signal_strength,
  169. .read_snr = dtt200u_fe_read_snr,
  170. .read_ucblocks = dtt200u_fe_read_unc_blocks,
  171. };