cinergyT2-fe.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. };
  131. static int cinergyt2_fe_read_status(struct dvb_frontend *fe,
  132. enum fe_status *status)
  133. {
  134. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  135. struct dvbt_get_status_msg result;
  136. u8 cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  137. int ret;
  138. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (u8 *)&result,
  139. sizeof(result), 0);
  140. if (ret < 0)
  141. return ret;
  142. *status = 0;
  143. if (0xffff - le16_to_cpu(result.gain) > 30)
  144. *status |= FE_HAS_SIGNAL;
  145. if (result.lock_bits & (1 << 6))
  146. *status |= FE_HAS_LOCK;
  147. if (result.lock_bits & (1 << 5))
  148. *status |= FE_HAS_SYNC;
  149. if (result.lock_bits & (1 << 4))
  150. *status |= FE_HAS_CARRIER;
  151. if (result.lock_bits & (1 << 1))
  152. *status |= FE_HAS_VITERBI;
  153. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  154. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  155. *status &= ~FE_HAS_LOCK;
  156. return 0;
  157. }
  158. static int cinergyt2_fe_read_ber(struct dvb_frontend *fe, u32 *ber)
  159. {
  160. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  161. struct dvbt_get_status_msg status;
  162. char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  163. int ret;
  164. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
  165. sizeof(status), 0);
  166. if (ret < 0)
  167. return ret;
  168. *ber = le32_to_cpu(status.viterbi_error_rate);
  169. return 0;
  170. }
  171. static int cinergyt2_fe_read_unc_blocks(struct dvb_frontend *fe, u32 *unc)
  172. {
  173. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  174. struct dvbt_get_status_msg status;
  175. u8 cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  176. int ret;
  177. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (u8 *)&status,
  178. sizeof(status), 0);
  179. if (ret < 0) {
  180. err("cinergyt2_fe_read_unc_blocks() Failed! (Error=%d)\n",
  181. ret);
  182. return ret;
  183. }
  184. *unc = le32_to_cpu(status.uncorrected_block_count);
  185. return 0;
  186. }
  187. static int cinergyt2_fe_read_signal_strength(struct dvb_frontend *fe,
  188. u16 *strength)
  189. {
  190. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  191. struct dvbt_get_status_msg status;
  192. char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  193. int ret;
  194. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
  195. sizeof(status), 0);
  196. if (ret < 0) {
  197. err("cinergyt2_fe_read_signal_strength() Failed!"
  198. " (Error=%d)\n", ret);
  199. return ret;
  200. }
  201. *strength = (0xffff - le16_to_cpu(status.gain));
  202. return 0;
  203. }
  204. static int cinergyt2_fe_read_snr(struct dvb_frontend *fe, u16 *snr)
  205. {
  206. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  207. struct dvbt_get_status_msg status;
  208. char cmd[] = { CINERGYT2_EP1_GET_TUNER_STATUS };
  209. int ret;
  210. ret = dvb_usb_generic_rw(state->d, cmd, sizeof(cmd), (char *)&status,
  211. sizeof(status), 0);
  212. if (ret < 0) {
  213. err("cinergyt2_fe_read_snr() Failed! (Error=%d)\n", ret);
  214. return ret;
  215. }
  216. *snr = (status.snr << 8) | status.snr;
  217. return 0;
  218. }
  219. static int cinergyt2_fe_init(struct dvb_frontend *fe)
  220. {
  221. return 0;
  222. }
  223. static int cinergyt2_fe_sleep(struct dvb_frontend *fe)
  224. {
  225. deb_info("cinergyt2_fe_sleep() Called\n");
  226. return 0;
  227. }
  228. static int cinergyt2_fe_get_tune_settings(struct dvb_frontend *fe,
  229. struct dvb_frontend_tune_settings *tune)
  230. {
  231. tune->min_delay_ms = 800;
  232. return 0;
  233. }
  234. static int cinergyt2_fe_set_frontend(struct dvb_frontend *fe)
  235. {
  236. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  237. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  238. struct dvbt_set_parameters_msg param;
  239. char result[2];
  240. int err;
  241. param.cmd = CINERGYT2_EP1_SET_TUNER_PARAMETERS;
  242. param.tps = cpu_to_le16(compute_tps(fep));
  243. param.freq = cpu_to_le32(fep->frequency / 1000);
  244. param.flags = 0;
  245. switch (fep->bandwidth_hz) {
  246. default:
  247. case 8000000:
  248. param.bandwidth = 8;
  249. break;
  250. case 7000000:
  251. param.bandwidth = 7;
  252. break;
  253. case 6000000:
  254. param.bandwidth = 6;
  255. break;
  256. }
  257. err = dvb_usb_generic_rw(state->d,
  258. (char *)&param, sizeof(param),
  259. result, sizeof(result), 0);
  260. if (err < 0)
  261. err("cinergyt2_fe_set_frontend() Failed! err=%d\n", err);
  262. return (err < 0) ? err : 0;
  263. }
  264. static void cinergyt2_fe_release(struct dvb_frontend *fe)
  265. {
  266. struct cinergyt2_fe_state *state = fe->demodulator_priv;
  267. kfree(state);
  268. }
  269. static struct dvb_frontend_ops cinergyt2_fe_ops;
  270. struct dvb_frontend *cinergyt2_fe_attach(struct dvb_usb_device *d)
  271. {
  272. struct cinergyt2_fe_state *s = kzalloc(sizeof(
  273. struct cinergyt2_fe_state), GFP_KERNEL);
  274. if (s == NULL)
  275. return NULL;
  276. s->d = d;
  277. memcpy(&s->fe.ops, &cinergyt2_fe_ops, sizeof(struct dvb_frontend_ops));
  278. s->fe.demodulator_priv = s;
  279. return &s->fe;
  280. }
  281. static struct dvb_frontend_ops cinergyt2_fe_ops = {
  282. .delsys = { SYS_DVBT },
  283. .info = {
  284. .name = DRIVER_NAME,
  285. .frequency_min = 174000000,
  286. .frequency_max = 862000000,
  287. .frequency_stepsize = 166667,
  288. .caps = FE_CAN_INVERSION_AUTO | FE_CAN_FEC_1_2
  289. | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4
  290. | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8
  291. | FE_CAN_FEC_AUTO | FE_CAN_QPSK
  292. | FE_CAN_QAM_16 | FE_CAN_QAM_64
  293. | FE_CAN_QAM_AUTO
  294. | FE_CAN_TRANSMISSION_MODE_AUTO
  295. | FE_CAN_GUARD_INTERVAL_AUTO
  296. | FE_CAN_HIERARCHY_AUTO
  297. | FE_CAN_RECOVER
  298. | FE_CAN_MUTE_TS
  299. },
  300. .release = cinergyt2_fe_release,
  301. .init = cinergyt2_fe_init,
  302. .sleep = cinergyt2_fe_sleep,
  303. .set_frontend = cinergyt2_fe_set_frontend,
  304. .get_tune_settings = cinergyt2_fe_get_tune_settings,
  305. .read_status = cinergyt2_fe_read_status,
  306. .read_ber = cinergyt2_fe_read_ber,
  307. .read_signal_strength = cinergyt2_fe_read_signal_strength,
  308. .read_snr = cinergyt2_fe_read_snr,
  309. .read_ucblocks = cinergyt2_fe_read_unc_blocks,
  310. };