dtt200u-fe.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Frontend part of the Linux driver for the WideView/ Yakumo/ Hama/
  3. * Typhoon/ Yuan DVB-T USB2.0 receiver.
  4. *
  5. * Copyright (C) 2005 Patrick Boettcher <[email protected]>
  6. *
  7. * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
  8. */
  9. #include "dtt200u.h"
  10. struct dtt200u_fe_state {
  11. struct dvb_usb_device *d;
  12. enum fe_status stat;
  13. struct dtv_frontend_properties fep;
  14. struct dvb_frontend frontend;
  15. unsigned char data[80];
  16. struct mutex data_mutex;
  17. };
  18. static int dtt200u_fe_read_status(struct dvb_frontend *fe,
  19. enum fe_status *stat)
  20. {
  21. struct dtt200u_fe_state *state = fe->demodulator_priv;
  22. int ret;
  23. mutex_lock(&state->data_mutex);
  24. state->data[0] = GET_TUNE_STATUS;
  25. ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
  26. if (ret < 0) {
  27. *stat = 0;
  28. mutex_unlock(&state->data_mutex);
  29. return ret;
  30. }
  31. switch (state->data[0]) {
  32. case 0x01:
  33. *stat = FE_HAS_SIGNAL | FE_HAS_CARRIER |
  34. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  35. break;
  36. case 0x00: /* pending */
  37. *stat = FE_TIMEDOUT; /* during set_frontend */
  38. break;
  39. default:
  40. case 0x02: /* failed */
  41. *stat = 0;
  42. break;
  43. }
  44. mutex_unlock(&state->data_mutex);
  45. return 0;
  46. }
  47. static int dtt200u_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  48. {
  49. struct dtt200u_fe_state *state = fe->demodulator_priv;
  50. int ret;
  51. mutex_lock(&state->data_mutex);
  52. state->data[0] = GET_VIT_ERR_CNT;
  53. ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 3, 0);
  54. if (ret >= 0)
  55. *ber = (state->data[0] << 16) | (state->data[1] << 8) | state->data[2];
  56. mutex_unlock(&state->data_mutex);
  57. return ret;
  58. }
  59. static int dtt200u_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  60. {
  61. struct dtt200u_fe_state *state = fe->demodulator_priv;
  62. int ret;
  63. mutex_lock(&state->data_mutex);
  64. state->data[0] = GET_RS_UNCOR_BLK_CNT;
  65. ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 2, 0);
  66. if (ret >= 0)
  67. *unc = (state->data[0] << 8) | state->data[1];
  68. mutex_unlock(&state->data_mutex);
  69. return ret;
  70. }
  71. static int dtt200u_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  72. {
  73. struct dtt200u_fe_state *state = fe->demodulator_priv;
  74. int ret;
  75. mutex_lock(&state->data_mutex);
  76. state->data[0] = GET_AGC;
  77. ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);
  78. if (ret >= 0)
  79. *strength = (state->data[0] << 8) | state->data[0];
  80. mutex_unlock(&state->data_mutex);
  81. return ret;
  82. }
  83. static int dtt200u_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  84. {
  85. struct dtt200u_fe_state *state = fe->demodulator_priv;
  86. int ret;
  87. mutex_lock(&state->data_mutex);
  88. state->data[0] = GET_SNR;
  89. ret = dvb_usb_generic_rw(state->d, state->data, 1, state->data, 1, 0);
  90. if (ret >= 0)
  91. *snr = ~((state->data[0] << 8) | state->data[0]);
  92. mutex_unlock(&state->data_mutex);
  93. return ret;
  94. }
  95. static int dtt200u_fe_init(struct dvb_frontend* fe)
  96. {
  97. struct dtt200u_fe_state *state = fe->demodulator_priv;
  98. int ret;
  99. mutex_lock(&state->data_mutex);
  100. state->data[0] = SET_INIT;
  101. ret = dvb_usb_generic_write(state->d, state->data, 1);
  102. mutex_unlock(&state->data_mutex);
  103. return ret;
  104. }
  105. static int dtt200u_fe_sleep(struct dvb_frontend* fe)
  106. {
  107. return dtt200u_fe_init(fe);
  108. }
  109. static int dtt200u_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  110. {
  111. tune->min_delay_ms = 1500;
  112. tune->step_size = 0;
  113. tune->max_drift = 0;
  114. return 0;
  115. }
  116. static int dtt200u_fe_set_frontend(struct dvb_frontend *fe)
  117. {
  118. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  119. struct dtt200u_fe_state *state = fe->demodulator_priv;
  120. int ret;
  121. u16 freq = fep->frequency / 250000;
  122. mutex_lock(&state->data_mutex);
  123. state->data[0] = SET_BANDWIDTH;
  124. switch (fep->bandwidth_hz) {
  125. case 8000000:
  126. state->data[1] = 8;
  127. break;
  128. case 7000000:
  129. state->data[1] = 7;
  130. break;
  131. case 6000000:
  132. state->data[1] = 6;
  133. break;
  134. default:
  135. ret = -EINVAL;
  136. goto ret;
  137. }
  138. ret = dvb_usb_generic_write(state->d, state->data, 2);
  139. if (ret < 0)
  140. goto ret;
  141. state->data[0] = SET_RF_FREQ;
  142. state->data[1] = freq & 0xff;
  143. state->data[2] = (freq >> 8) & 0xff;
  144. ret = dvb_usb_generic_write(state->d, state->data, 3);
  145. if (ret < 0)
  146. goto ret;
  147. ret:
  148. mutex_unlock(&state->data_mutex);
  149. return ret;
  150. }
  151. static int dtt200u_fe_get_frontend(struct dvb_frontend* fe,
  152. struct dtv_frontend_properties *fep)
  153. {
  154. struct dtt200u_fe_state *state = fe->demodulator_priv;
  155. memcpy(fep, &state->fep, sizeof(struct dtv_frontend_properties));
  156. return 0;
  157. }
  158. static void dtt200u_fe_release(struct dvb_frontend* fe)
  159. {
  160. struct dtt200u_fe_state *state = (struct dtt200u_fe_state*) fe->demodulator_priv;
  161. kfree(state);
  162. }
  163. static const struct dvb_frontend_ops dtt200u_fe_ops;
  164. struct dvb_frontend* dtt200u_fe_attach(struct dvb_usb_device *d)
  165. {
  166. struct dtt200u_fe_state* state = NULL;
  167. /* allocate memory for the internal state */
  168. state = kzalloc(sizeof(struct dtt200u_fe_state), GFP_KERNEL);
  169. if (state == NULL)
  170. goto error;
  171. deb_info("attaching frontend dtt200u\n");
  172. state->d = d;
  173. mutex_init(&state->data_mutex);
  174. memcpy(&state->frontend.ops,&dtt200u_fe_ops,sizeof(struct dvb_frontend_ops));
  175. state->frontend.demodulator_priv = state;
  176. return &state->frontend;
  177. error:
  178. return NULL;
  179. }
  180. static const struct dvb_frontend_ops dtt200u_fe_ops = {
  181. .delsys = { SYS_DVBT },
  182. .info = {
  183. .name = "WideView USB DVB-T",
  184. .frequency_min_hz = 44250 * kHz,
  185. .frequency_max_hz = 867250 * kHz,
  186. .frequency_stepsize_hz = 250 * kHz,
  187. .caps = FE_CAN_INVERSION_AUTO |
  188. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  189. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  190. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  191. FE_CAN_TRANSMISSION_MODE_AUTO |
  192. FE_CAN_GUARD_INTERVAL_AUTO |
  193. FE_CAN_RECOVER |
  194. FE_CAN_HIERARCHY_AUTO,
  195. },
  196. .release = dtt200u_fe_release,
  197. .init = dtt200u_fe_init,
  198. .sleep = dtt200u_fe_sleep,
  199. .set_frontend = dtt200u_fe_set_frontend,
  200. .get_frontend = dtt200u_fe_get_frontend,
  201. .get_tune_settings = dtt200u_fe_get_tune_settings,
  202. .read_status = dtt200u_fe_read_status,
  203. .read_ber = dtt200u_fe_read_ber,
  204. .read_signal_strength = dtt200u_fe_read_signal_strength,
  205. .read_snr = dtt200u_fe_read_snr,
  206. .read_ucblocks = dtt200u_fe_read_unc_blocks,
  207. };