gp8psk-fe.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* DVB USB compliant Linux driver for the
  2. * - GENPIX 8pks/qpsk/DCII USB2.0 DVB-S module
  3. *
  4. * Copyright (C) 2006,2007 Alan Nisota (alannisota@gmail.com)
  5. * Copyright (C) 2006,2007 Genpix Electronics (genpix@genpix-electronics.com)
  6. *
  7. * Thanks to GENPIX for the sample code used to implement this module.
  8. *
  9. * This module is based off the vp7045 and vp702x modules
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation, version 2.
  14. *
  15. * see Documentation/dvb/README.dvb-usb for more information
  16. */
  17. #include "gp8psk.h"
  18. struct gp8psk_fe_state {
  19. struct dvb_frontend fe;
  20. struct dvb_usb_device *d;
  21. u8 lock;
  22. u16 snr;
  23. unsigned long next_status_check;
  24. unsigned long status_check_interval;
  25. };
  26. static int gp8psk_tuned_to_DCII(struct dvb_frontend *fe)
  27. {
  28. struct gp8psk_fe_state *st = fe->demodulator_priv;
  29. u8 status;
  30. gp8psk_usb_in_op(st->d, GET_8PSK_CONFIG, 0, 0, &status, 1);
  31. return status & bmDCtuned;
  32. }
  33. static int gp8psk_set_tuner_mode(struct dvb_frontend *fe, int mode)
  34. {
  35. struct gp8psk_fe_state *state = fe->demodulator_priv;
  36. return gp8psk_usb_out_op(state->d, SET_8PSK_CONFIG, mode, 0, NULL, 0);
  37. }
  38. static int gp8psk_fe_update_status(struct gp8psk_fe_state *st)
  39. {
  40. u8 buf[6];
  41. if (time_after(jiffies,st->next_status_check)) {
  42. gp8psk_usb_in_op(st->d, GET_SIGNAL_LOCK, 0,0,&st->lock,1);
  43. gp8psk_usb_in_op(st->d, GET_SIGNAL_STRENGTH, 0,0,buf,6);
  44. st->snr = (buf[1]) << 8 | buf[0];
  45. st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
  46. }
  47. return 0;
  48. }
  49. static int gp8psk_fe_read_status(struct dvb_frontend* fe, fe_status_t *status)
  50. {
  51. struct gp8psk_fe_state *st = fe->demodulator_priv;
  52. gp8psk_fe_update_status(st);
  53. if (st->lock)
  54. *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
  55. else
  56. *status = 0;
  57. if (*status & FE_HAS_LOCK)
  58. st->status_check_interval = 1000;
  59. else
  60. st->status_check_interval = 100;
  61. return 0;
  62. }
  63. /* not supported by this Frontend */
  64. static int gp8psk_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  65. {
  66. (void) fe;
  67. *ber = 0;
  68. return 0;
  69. }
  70. /* not supported by this Frontend */
  71. static int gp8psk_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  72. {
  73. (void) fe;
  74. *unc = 0;
  75. return 0;
  76. }
  77. static int gp8psk_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  78. {
  79. struct gp8psk_fe_state *st = fe->demodulator_priv;
  80. gp8psk_fe_update_status(st);
  81. /* snr is reported in dBu*256 */
  82. *snr = st->snr;
  83. return 0;
  84. }
  85. static int gp8psk_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  86. {
  87. struct gp8psk_fe_state *st = fe->demodulator_priv;
  88. gp8psk_fe_update_status(st);
  89. /* snr is reported in dBu*256 */
  90. /* snr / 38.4 ~= 100% strength */
  91. /* snr * 17 returns 100% strength as 65535 */
  92. if (st->snr > 0xf00)
  93. *strength = 0xffff;
  94. else
  95. *strength = (st->snr << 4) + st->snr; /* snr*17 */
  96. return 0;
  97. }
  98. static int gp8psk_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  99. {
  100. tune->min_delay_ms = 800;
  101. return 0;
  102. }
  103. static int gp8psk_fe_set_property(struct dvb_frontend *fe,
  104. struct dtv_property *tvp)
  105. {
  106. deb_fe("%s(..)\n", __func__);
  107. return 0;
  108. }
  109. static int gp8psk_fe_get_property(struct dvb_frontend *fe,
  110. struct dtv_property *tvp)
  111. {
  112. deb_fe("%s(..)\n", __func__);
  113. return 0;
  114. }
  115. static int gp8psk_fe_set_frontend(struct dvb_frontend* fe,
  116. struct dvb_frontend_parameters *fep)
  117. {
  118. struct gp8psk_fe_state *state = fe->demodulator_priv;
  119. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  120. u8 cmd[10];
  121. u32 freq = fep->frequency * 1000;
  122. int gp_product_id = le16_to_cpu(state->d->udev->descriptor.idProduct);
  123. deb_fe("%s()\n", __func__);
  124. cmd[4] = freq & 0xff;
  125. cmd[5] = (freq >> 8) & 0xff;
  126. cmd[6] = (freq >> 16) & 0xff;
  127. cmd[7] = (freq >> 24) & 0xff;
  128. /* backwards compatibility: DVB-S + 8-PSK were used for Turbo-FEC */
  129. if (c->delivery_system == SYS_DVBS && c->modulation == PSK_8)
  130. c->delivery_system = SYS_TURBO;
  131. switch (c->delivery_system) {
  132. case SYS_DVBS:
  133. if (c->modulation != QPSK) {
  134. deb_fe("%s: unsupported modulation selected (%d)\n",
  135. __func__, c->modulation);
  136. return -EOPNOTSUPP;
  137. }
  138. c->fec_inner = FEC_AUTO;
  139. break;
  140. case SYS_DVBS2: /* kept for backwards compatibility */
  141. deb_fe("%s: DVB-S2 delivery system selected\n", __func__);
  142. break;
  143. case SYS_TURBO:
  144. deb_fe("%s: Turbo-FEC delivery system selected\n", __func__);
  145. break;
  146. default:
  147. deb_fe("%s: unsupported delivery system selected (%d)\n",
  148. __func__, c->delivery_system);
  149. return -EOPNOTSUPP;
  150. }
  151. cmd[0] = c->symbol_rate & 0xff;
  152. cmd[1] = (c->symbol_rate >> 8) & 0xff;
  153. cmd[2] = (c->symbol_rate >> 16) & 0xff;
  154. cmd[3] = (c->symbol_rate >> 24) & 0xff;
  155. switch (c->modulation) {
  156. case QPSK:
  157. if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
  158. if (gp8psk_tuned_to_DCII(fe))
  159. gp8psk_bcm4500_reload(state->d);
  160. switch (c->fec_inner) {
  161. case FEC_1_2:
  162. cmd[9] = 0; break;
  163. case FEC_2_3:
  164. cmd[9] = 1; break;
  165. case FEC_3_4:
  166. cmd[9] = 2; break;
  167. case FEC_5_6:
  168. cmd[9] = 3; break;
  169. case FEC_7_8:
  170. cmd[9] = 4; break;
  171. case FEC_AUTO:
  172. cmd[9] = 5; break;
  173. default:
  174. cmd[9] = 5; break;
  175. }
  176. if (c->delivery_system == SYS_TURBO)
  177. cmd[8] = ADV_MOD_TURBO_QPSK;
  178. else
  179. cmd[8] = ADV_MOD_DVB_QPSK;
  180. break;
  181. case PSK_8: /* PSK_8 is for compatibility with DN */
  182. cmd[8] = ADV_MOD_TURBO_8PSK;
  183. switch (c->fec_inner) {
  184. case FEC_2_3:
  185. cmd[9] = 0; break;
  186. case FEC_3_4:
  187. cmd[9] = 1; break;
  188. case FEC_3_5:
  189. cmd[9] = 2; break;
  190. case FEC_5_6:
  191. cmd[9] = 3; break;
  192. case FEC_8_9:
  193. cmd[9] = 4; break;
  194. default:
  195. cmd[9] = 0; break;
  196. }
  197. break;
  198. case QAM_16: /* QAM_16 is for compatibility with DN */
  199. cmd[8] = ADV_MOD_TURBO_16QAM;
  200. cmd[9] = 0;
  201. break;
  202. default: /* Unknown modulation */
  203. deb_fe("%s: unsupported modulation selected (%d)\n",
  204. __func__, c->modulation);
  205. return -EOPNOTSUPP;
  206. }
  207. if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM)
  208. gp8psk_set_tuner_mode(fe, 0);
  209. gp8psk_usb_out_op(state->d, TUNE_8PSK, 0, 0, cmd, 10);
  210. state->lock = 0;
  211. state->next_status_check = jiffies;
  212. state->status_check_interval = 200;
  213. return 0;
  214. }
  215. static int gp8psk_fe_send_diseqc_msg (struct dvb_frontend* fe,
  216. struct dvb_diseqc_master_cmd *m)
  217. {
  218. struct gp8psk_fe_state *st = fe->demodulator_priv;
  219. deb_fe("%s\n",__func__);
  220. if (gp8psk_usb_out_op(st->d,SEND_DISEQC_COMMAND, m->msg[0], 0,
  221. m->msg, m->msg_len)) {
  222. return -EINVAL;
  223. }
  224. return 0;
  225. }
  226. static int gp8psk_fe_send_diseqc_burst (struct dvb_frontend* fe,
  227. fe_sec_mini_cmd_t burst)
  228. {
  229. struct gp8psk_fe_state *st = fe->demodulator_priv;
  230. u8 cmd;
  231. deb_fe("%s\n",__func__);
  232. /* These commands are certainly wrong */
  233. cmd = (burst == SEC_MINI_A) ? 0x00 : 0x01;
  234. if (gp8psk_usb_out_op(st->d,SEND_DISEQC_COMMAND, cmd, 0,
  235. &cmd, 0)) {
  236. return -EINVAL;
  237. }
  238. return 0;
  239. }
  240. static int gp8psk_fe_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  241. {
  242. struct gp8psk_fe_state* state = fe->demodulator_priv;
  243. if (gp8psk_usb_out_op(state->d,SET_22KHZ_TONE,
  244. (tone == SEC_TONE_ON), 0, NULL, 0)) {
  245. return -EINVAL;
  246. }
  247. return 0;
  248. }
  249. static int gp8psk_fe_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  250. {
  251. struct gp8psk_fe_state* state = fe->demodulator_priv;
  252. if (gp8psk_usb_out_op(state->d,SET_LNB_VOLTAGE,
  253. voltage == SEC_VOLTAGE_18, 0, NULL, 0)) {
  254. return -EINVAL;
  255. }
  256. return 0;
  257. }
  258. static int gp8psk_fe_enable_high_lnb_voltage(struct dvb_frontend* fe, long onoff)
  259. {
  260. struct gp8psk_fe_state* state = fe->demodulator_priv;
  261. return gp8psk_usb_out_op(state->d, USE_EXTRA_VOLT, onoff, 0,NULL,0);
  262. }
  263. static int gp8psk_fe_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long sw_cmd)
  264. {
  265. struct gp8psk_fe_state* state = fe->demodulator_priv;
  266. u8 cmd = sw_cmd & 0x7f;
  267. if (gp8psk_usb_out_op(state->d,SET_DN_SWITCH, cmd, 0,
  268. NULL, 0)) {
  269. return -EINVAL;
  270. }
  271. if (gp8psk_usb_out_op(state->d,SET_LNB_VOLTAGE, !!(sw_cmd & 0x80),
  272. 0, NULL, 0)) {
  273. return -EINVAL;
  274. }
  275. return 0;
  276. }
  277. static void gp8psk_fe_release(struct dvb_frontend* fe)
  278. {
  279. struct gp8psk_fe_state *state = fe->demodulator_priv;
  280. kfree(state);
  281. }
  282. static struct dvb_frontend_ops gp8psk_fe_ops;
  283. struct dvb_frontend * gp8psk_fe_attach(struct dvb_usb_device *d)
  284. {
  285. struct gp8psk_fe_state *s = kzalloc(sizeof(struct gp8psk_fe_state), GFP_KERNEL);
  286. if (s == NULL)
  287. goto error;
  288. s->d = d;
  289. memcpy(&s->fe.ops, &gp8psk_fe_ops, sizeof(struct dvb_frontend_ops));
  290. s->fe.demodulator_priv = s;
  291. goto success;
  292. error:
  293. return NULL;
  294. success:
  295. return &s->fe;
  296. }
  297. static struct dvb_frontend_ops gp8psk_fe_ops = {
  298. .info = {
  299. .name = "Genpix DVB-S",
  300. .type = FE_QPSK,
  301. .frequency_min = 800000,
  302. .frequency_max = 2250000,
  303. .frequency_stepsize = 100,
  304. .symbol_rate_min = 1000000,
  305. .symbol_rate_max = 45000000,
  306. .symbol_rate_tolerance = 500, /* ppm */
  307. .caps = FE_CAN_INVERSION_AUTO |
  308. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  309. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  310. /*
  311. * FE_CAN_QAM_16 is for compatibility
  312. * (Myth incorrectly detects Turbo-QPSK as plain QAM-16)
  313. */
  314. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_TURBO_FEC
  315. },
  316. .release = gp8psk_fe_release,
  317. .init = NULL,
  318. .sleep = NULL,
  319. .set_property = gp8psk_fe_set_property,
  320. .get_property = gp8psk_fe_get_property,
  321. .set_frontend = gp8psk_fe_set_frontend,
  322. .get_tune_settings = gp8psk_fe_get_tune_settings,
  323. .read_status = gp8psk_fe_read_status,
  324. .read_ber = gp8psk_fe_read_ber,
  325. .read_signal_strength = gp8psk_fe_read_signal_strength,
  326. .read_snr = gp8psk_fe_read_snr,
  327. .read_ucblocks = gp8psk_fe_read_unc_blocks,
  328. .diseqc_send_master_cmd = gp8psk_fe_send_diseqc_msg,
  329. .diseqc_send_burst = gp8psk_fe_send_diseqc_burst,
  330. .set_tone = gp8psk_fe_set_tone,
  331. .set_voltage = gp8psk_fe_set_voltage,
  332. .dishnetwork_send_legacy_command = gp8psk_fe_send_legacy_dish_cmd,
  333. .enable_high_lnb_voltage = gp8psk_fe_enable_high_lnb_voltage
  334. };