cinergyT2-fe.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * TerraTec Cinergy T2/qanu USB2 DVB-T adapter.
  4. *
  5. * Copyright (C) 2007 Tomi Orava ([email protected])
  6. *
  7. * Based on the dvb-usb-framework code and the
  8. * original Terratec Cinergy T2 driver by:
  9. *
  10. * Copyright (C) 2004 Daniel Mack <[email protected]> and
  11. * Holger Waechtler <[email protected]>
  12. *
  13. * Protocol Spec published on http://qanu.de/specs/terratec_cinergyT2.pdf
  14. */
  15. #include "cinergyT2.h"
  16. /*
  17. * convert linux-dvb frontend parameter set into TPS.
  18. * See ETSI ETS-300744, section 4.6.2, table 9 for details.
  19. *
  20. * This function is probably reusable and may better get placed in a support
  21. * library.
  22. *
  23. * We replace erroneous fields by default TPS fields (the ones with value 0).
  24. */
  25. static uint16_t compute_tps(struct dtv_frontend_properties *op)
  26. {
  27. uint16_t tps = 0;
  28. switch (op->code_rate_HP) {
  29. case FEC_2_3:
  30. tps |= (1 << 7);
  31. break;
  32. case FEC_3_4:
  33. tps |= (2 << 7);
  34. break;
  35. case FEC_5_6:
  36. tps |= (3 << 7);
  37. break;
  38. case FEC_7_8:
  39. tps |= (4 << 7);
  40. break;
  41. case FEC_1_2:
  42. case FEC_AUTO:
  43. default:
  44. /* tps |= (0 << 7) */;
  45. }
  46. switch (op->code_rate_LP) {
  47. case FEC_2_3:
  48. tps |= (1 << 4);
  49. break;
  50. case FEC_3_4:
  51. tps |= (2 << 4);
  52. break;
  53. case FEC_5_6:
  54. tps |= (3 << 4);
  55. break;
  56. case FEC_7_8:
  57. tps |= (4 << 4);
  58. break;
  59. case FEC_1_2:
  60. case FEC_AUTO:
  61. default:
  62. /* tps |= (0 << 4) */;
  63. }
  64. switch (op->modulation) {
  65. case QAM_16:
  66. tps |= (1 << 13);
  67. break;
  68. case QAM_64:
  69. tps |= (2 << 13);
  70. break;
  71. case QPSK:
  72. default:
  73. /* tps |= (0 << 13) */;
  74. }
  75. switch (op->transmission_mode) {
  76. case TRANSMISSION_MODE_8K:
  77. tps |= (1 << 0);
  78. break;
  79. case TRANSMISSION_MODE_2K:
  80. default:
  81. /* tps |= (0 << 0) */;
  82. }
  83. switch (op->guard_interval) {
  84. case GUARD_INTERVAL_1_16:
  85. tps |= (1 << 2);
  86. break;
  87. case GUARD_INTERVAL_1_8:
  88. tps |= (2 << 2);
  89. break;
  90. case GUARD_INTERVAL_1_4:
  91. tps |= (3 << 2);
  92. break;
  93. case GUARD_INTERVAL_1_32:
  94. default:
  95. /* tps |= (0 << 2) */;
  96. }
  97. switch (op->hierarchy) {
  98. case HIERARCHY_1:
  99. tps |= (1 << 10);
  100. break;
  101. case HIERARCHY_2:
  102. tps |= (2 << 10);
  103. break;
  104. case HIERARCHY_4:
  105. tps |= (3 << 10);
  106. break;
  107. case HIERARCHY_NONE:
  108. default:
  109. /* tps |= (0 << 10) */;
  110. }
  111. return tps;
  112. }
  113. struct cinergyt2_fe_state {
  114. struct dvb_frontend fe;
  115. struct dvb_usb_device *d;
  116. unsigned char data[64];
  117. struct mutex data_mutex;
  118. struct dvbt_get_status_msg status;
  119. };
  120. static int cinergyt2_fe_read_status(struct dvb_frontend *fe,
  121. enum fe_status *status)
  122. {
  123. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  124. int ret;
  125. mutex_lock(&state->data_mutex);
  126. state->data[0] = CINERGYT2_EP1_GET_TUNER_STATUS;
  127. ret = dvb_usb_generic_rw(state->d, state->data, 1,
  128. state->data, sizeof(state->status), 0);
  129. if (!ret)
  130. memcpy(&state->status, state->data, sizeof(state->status));
  131. mutex_unlock(&state->data_mutex);
  132. if (ret < 0)
  133. return ret;
  134. *status = 0;
  135. if (0xffff - le16_to_cpu(state->status.gain) > 30)
  136. *status |= FE_HAS_SIGNAL;
  137. if (state->status.lock_bits & (1 << 6))
  138. *status |= FE_HAS_LOCK;
  139. if (state->status.lock_bits & (1 << 5))
  140. *status |= FE_HAS_SYNC;
  141. if (state->status.lock_bits & (1 << 4))
  142. *status |= FE_HAS_CARRIER;
  143. if (state->status.lock_bits & (1 << 1))
  144. *status |= FE_HAS_VITERBI;
  145. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  146. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  147. *status &= ~FE_HAS_LOCK;
  148. return 0;
  149. }
  150. static int cinergyt2_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
  151. {
  152. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  153. *ber = le32_to_cpu(state->status.viterbi_error_rate);
  154. return 0;
  155. }
  156. static int cinergyt2_fe_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  157. {
  158. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  159. *unc = le32_to_cpu(state->status.uncorrected_block_count);
  160. return 0;
  161. }
  162. static int cinergyt2_fe_read_signal_strength(struct dvb_frontend *fe,
  163. u16 *strength)
  164. {
  165. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  166. *strength = (0xffff - le16_to_cpu(state->status.gain));
  167. return 0;
  168. }
  169. static int cinergyt2_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
  170. {
  171. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  172. *snr = (state->status.snr << 8) | state->status.snr;
  173. return 0;
  174. }
  175. static int cinergyt2_fe_init(struct dvb_frontend *fe)
  176. {
  177. return 0;
  178. }
  179. static int cinergyt2_fe_sleep(struct dvb_frontend *fe)
  180. {
  181. deb_info("cinergyt2_fe_sleep() Called\n");
  182. return 0;
  183. }
  184. static int cinergyt2_fe_get_tune_settings(struct dvb_frontend *fe,
  185. struct dvb_frontend_tune_settings *tune)
  186. {
  187. tune->min_delay_ms = 800;
  188. return 0;
  189. }
  190. static int cinergyt2_fe_set_frontend(struct dvb_frontend *fe)
  191. {
  192. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  193. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  194. struct dvbt_set_parameters_msg *param;
  195. int err;
  196. mutex_lock(&state->data_mutex);
  197. param = (void *)state->data;
  198. param->cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
  199. param->tps = cpu_to_le16(compute_tps(fep));
  200. param->freq = cpu_to_le32(fep->frequency / 1000);
  201. param->flags = 0;
  202. switch (fep->bandwidth_hz) {
  203. default:
  204. case 8000000:
  205. param->bandwidth = 8;
  206. break;
  207. case 7000000:
  208. param->bandwidth = 7;
  209. break;
  210. case 6000000:
  211. param->bandwidth = 6;
  212. break;
  213. }
  214. err = dvb_usb_generic_rw(state->d, state->data, sizeof(*param),
  215. state->data, 2, 0);
  216. if (err < 0)
  217. err("cinergyt2_fe_set_frontend() Failed! err=%d\n", err);
  218. mutex_unlock(&state->data_mutex);
  219. return (err < 0) ? err : 0;
  220. }
  221. static void cinergyt2_fe_release(struct dvb_frontend *fe)
  222. {
  223. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  224. kfree(state);
  225. }
  226. static const struct dvb_frontend_ops cinergyt2_fe_ops;
  227. struct dvb_frontend *cinergyt2_fe_attach(struct dvb_usb_device *d)
  228. {
  229. struct cinergyt2_fe_state *s = kzalloc(sizeof(
  230. struct cinergyt2_fe_state), GFP_KERNEL);
  231. if (s == NULL)
  232. return NULL;
  233. s->d = d;
  234. memcpy(&s->fe.ops, &cinergyt2_fe_ops, sizeof(struct dvb_frontend_ops));
  235. s->fe.demodulator_priv = s;
  236. mutex_init(&s->data_mutex);
  237. return &s->fe;
  238. }
  239. static const struct dvb_frontend_ops cinergyt2_fe_ops = {
  240. .delsys = { SYS_DVBT },
  241. .info = {
  242. .name = DRIVER_NAME,
  243. .frequency_min_hz = 174 * MHz,
  244. .frequency_max_hz = 862 * MHz,
  245. .frequency_stepsize_hz = 166667,
  246. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_FEC_1_2
  247. | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4
  248. | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8
  249. | FE_CAN_FEC_AUTO | FE_CAN_QPSK
  250. | FE_CAN_QAM_16 | FE_CAN_QAM_64
  251. | FE_CAN_QAM_AUTO
  252. | FE_CAN_TRANSMISSION_MODE_AUTO
  253. | FE_CAN_GUARD_INTERVAL_AUTO
  254. | FE_CAN_HIERARCHY_AUTO
  255. | FE_CAN_RECOVER
  256. | FE_CAN_MUTE_TS
  257. },
  258. .release = cinergyt2_fe_release,
  259. .init = cinergyt2_fe_init,
  260. .sleep = cinergyt2_fe_sleep,
  261. .set_frontend = cinergyt2_fe_set_frontend,
  262. .get_tune_settings = cinergyt2_fe_get_tune_settings,
  263. .read_status = cinergyt2_fe_read_status,
  264. .read_ber = cinergyt2_fe_read_ber,
  265. .read_signal_strength = cinergyt2_fe_read_signal_strength,
  266. .read_snr = cinergyt2_fe_read_snr,
  267. .read_ucblocks = cinergyt2_fe_read_unc_blocks,
  268. };