cinergyT2-fe.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 dvb_frontend_parameters *p)
  40. {
  41. struct dvb_ofdm_parameters *op = &p->u.ofdm;
  42. uint16_t tps = 0;
  43. switch (op->code_rate_HP) {
  44. case FEC_2_3:
  45. tps |= (1 << 7);
  46. break;
  47. case FEC_3_4:
  48. tps |= (2 << 7);
  49. break;
  50. case FEC_5_6:
  51. tps |= (3 << 7);
  52. break;
  53. case FEC_7_8:
  54. tps |= (4 << 7);
  55. break;
  56. case FEC_1_2:
  57. case FEC_AUTO:
  58. default:
  59. /* tps |= (0 << 7) */;
  60. }
  61. switch (op->code_rate_LP) {
  62. case FEC_2_3:
  63. tps |= (1 << 4);
  64. break;
  65. case FEC_3_4:
  66. tps |= (2 << 4);
  67. break;
  68. case FEC_5_6:
  69. tps |= (3 << 4);
  70. break;
  71. case FEC_7_8:
  72. tps |= (4 << 4);
  73. break;
  74. case FEC_1_2:
  75. case FEC_AUTO:
  76. default:
  77. /* tps |= (0 << 4) */;
  78. }
  79. switch (op->constellation) {
  80. case QAM_16:
  81. tps |= (1 << 13);
  82. break;
  83. case QAM_64:
  84. tps |= (2 << 13);
  85. break;
  86. case QPSK:
  87. default:
  88. /* tps |= (0 << 13) */;
  89. }
  90. switch (op->transmission_mode) {
  91. case TRANSMISSION_MODE_8K:
  92. tps |= (1 << 0);
  93. break;
  94. case TRANSMISSION_MODE_2K:
  95. default:
  96. /* tps |= (0 << 0) */;
  97. }
  98. switch (op->guard_interval) {
  99. case GUARD_INTERVAL_1_16:
  100. tps |= (1 << 2);
  101. break;
  102. case GUARD_INTERVAL_1_8:
  103. tps |= (2 << 2);
  104. break;
  105. case GUARD_INTERVAL_1_4:
  106. tps |= (3 << 2);
  107. break;
  108. case GUARD_INTERVAL_1_32:
  109. default:
  110. /* tps |= (0 << 2) */;
  111. }
  112. switch (op->hierarchy_information) {
  113. case HIERARCHY_1:
  114. tps |= (1 << 10);
  115. break;
  116. case HIERARCHY_2:
  117. tps |= (2 << 10);
  118. break;
  119. case HIERARCHY_4:
  120. tps |= (3 << 10);
  121. break;
  122. case HIERARCHY_NONE:
  123. default:
  124. /* tps |= (0 << 10) */;
  125. }
  126. return tps;
  127. }
  128. struct cinergyt2_fe_state {
  129. struct dvb_frontend fe;
  130. struct dvb_usb_device *d;
  131. };
  132. static int cinergyt2_fe_read_status(struct dvb_frontend *fe,
  133. fe_status_t *status)
  134. {
  135. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  136. struct dvbt_get_status_msg result;
  137. u8 cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  138. int ret;
  139. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (u8 *)&result,
  140. sizeof(result), 0);
  141. if (ret < 0)
  142. return ret;
  143. *status = 0;
  144. if (0xffff - le16_to_cpu(result.gain) > 30)
  145. *status |= FE_HAS_SIGNAL;
  146. if (result.lock_bits & (1 << 6))
  147. *status |= FE_HAS_LOCK;
  148. if (result.lock_bits & (1 << 5))
  149. *status |= FE_HAS_SYNC;
  150. if (result.lock_bits & (1 << 4))
  151. *status |= FE_HAS_CARRIER;
  152. if (result.lock_bits & (1 << 1))
  153. *status |= FE_HAS_VITERBI;
  154. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  155. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  156. *status &= ~FE_HAS_LOCK;
  157. return 0;
  158. }
  159. static int cinergyt2_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
  160. {
  161. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  162. struct dvbt_get_status_msg status;
  163. char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  164. int ret;
  165. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
  166. sizeof(status), 0);
  167. if (ret < 0)
  168. return ret;
  169. *ber = le32_to_cpu(status.viterbi_error_rate);
  170. return 0;
  171. }
  172. static int cinergyt2_fe_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  173. {
  174. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  175. struct dvbt_get_status_msg status;
  176. u8 cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  177. int ret;
  178. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (u8 *)&status,
  179. sizeof(status), 0);
  180. if (ret < 0) {
  181. err("cinergyt2_fe_read_unc_blocks() Failed! (Error=%d)\n",
  182. ret);
  183. return ret;
  184. }
  185. *unc = le32_to_cpu(status.uncorrected_block_count);
  186. return 0;
  187. }
  188. static int cinergyt2_fe_read_signal_strength(struct dvb_frontend *fe,
  189. u16 *strength)
  190. {
  191. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  192. struct dvbt_get_status_msg status;
  193. char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  194. int ret;
  195. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
  196. sizeof(status), 0);
  197. if (ret < 0) {
  198. err("cinergyt2_fe_read_signal_strength() Failed!"
  199. " (Error=%d)\n", ret);
  200. return ret;
  201. }
  202. *strength = (0xffff - le16_to_cpu(status.gain));
  203. return 0;
  204. }
  205. static int cinergyt2_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
  206. {
  207. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  208. struct dvbt_get_status_msg status;
  209. char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  210. int ret;
  211. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
  212. sizeof(status), 0);
  213. if (ret < 0) {
  214. err("cinergyt2_fe_read_snr() Failed! (Error=%d)\n", ret);
  215. return ret;
  216. }
  217. *snr = (status.snr << 8) | status.snr;
  218. return 0;
  219. }
  220. static int cinergyt2_fe_init(struct dvb_frontend *fe)
  221. {
  222. return 0;
  223. }
  224. static int cinergyt2_fe_sleep(struct dvb_frontend *fe)
  225. {
  226. deb_info("cinergyt2_fe_sleep() Called\n");
  227. return 0;
  228. }
  229. static int cinergyt2_fe_get_tune_settings(struct dvb_frontend *fe,
  230. struct dvb_frontend_tune_settings *tune)
  231. {
  232. tune->min_delay_ms = 800;
  233. return 0;
  234. }
  235. static int cinergyt2_fe_set_frontend(struct dvb_frontend *fe,
  236. struct dvb_frontend_parameters *fep)
  237. {
  238. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  239. struct dvbt_set_parameters_msg param;
  240. char result[2];
  241. int err;
  242. param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
  243. param.tps = cpu_to_le16(compute_tps(fep));
  244. param.freq = cpu_to_le32(fep->frequency / 1000);
  245. param.bandwidth = 8 - fep->u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
  246. param.flags = 0;
  247. err = dvb_usb_generic_rw(state->d,
  248. (char *)&param, sizeof(param),
  249. result, sizeof(result), 0);
  250. if (err < 0)
  251. err("cinergyt2_fe_set_frontend() Failed! err=%d\n", err);
  252. return (err < 0) ? err : 0;
  253. }
  254. static int cinergyt2_fe_get_frontend(struct dvb_frontend *fe,
  255. struct dvb_frontend_parameters *fep)
  256. {
  257. return 0;
  258. }
  259. static void cinergyt2_fe_release(struct dvb_frontend *fe)
  260. {
  261. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  262. if (state != NULL)
  263. kfree(state);
  264. }
  265. static struct dvb_frontend_ops cinergyt2_fe_ops;
  266. struct dvb_frontend *cinergyt2_fe_attach(struct dvb_usb_device *d)
  267. {
  268. struct cinergyt2_fe_state *s = kzalloc(sizeof(
  269. struct cinergyt2_fe_state), GFP_KERNEL);
  270. if (s == NULL)
  271. return NULL;
  272. s->d = d;
  273. memcpy(&s->fe.ops, &cinergyt2_fe_ops, sizeof(struct dvb_frontend_ops));
  274. s->fe.demodulator_priv = s;
  275. return &s->fe;
  276. }
  277. static struct dvb_frontend_ops cinergyt2_fe_ops = {
  278. .info = {
  279. .name = DRIVER_NAME,
  280. .type = FE_OFDM,
  281. .frequency_min = 174000000,
  282. .frequency_max = 862000000,
  283. .frequency_stepsize = 166667,
  284. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_FEC_1_2
  285. | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4
  286. | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8
  287. | FE_CAN_FEC_AUTO | FE_CAN_QPSK
  288. | FE_CAN_QAM_16 | FE_CAN_QAM_64
  289. | FE_CAN_QAM_AUTO
  290. | FE_CAN_TRANSMISSION_MODE_AUTO
  291. | FE_CAN_GUARD_INTERVAL_AUTO
  292. | FE_CAN_HIERARCHY_AUTO
  293. | FE_CAN_RECOVER
  294. | FE_CAN_MUTE_TS
  295. },
  296. .release = cinergyt2_fe_release,
  297. .init = cinergyt2_fe_init,
  298. .sleep = cinergyt2_fe_sleep,
  299. .set_frontend = cinergyt2_fe_set_frontend,
  300. .get_frontend = cinergyt2_fe_get_frontend,
  301. .get_tune_settings = cinergyt2_fe_get_tune_settings,
  302. .read_status = cinergyt2_fe_read_status,
  303. .read_ber = cinergyt2_fe_read_ber,
  304. .read_signal_strength = cinergyt2_fe_read_signal_strength,
  305. .read_snr = cinergyt2_fe_read_snr,
  306. .read_ucblocks = cinergyt2_fe_read_unc_blocks,
  307. };