cinergyT2-fe.c 7.3 KB

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