cinergyT2-fe.c 7.5 KB

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