vp702x-fe.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* DVB frontend part of the Linux driver for the TwinhanDTV StarBox USB2.0
  2. * DVB-S receiver.
  3. *
  4. * Copyright (C) 2005 Ralph Metzler <rjkm@metzlerbros.de>
  5. * Metzler Brothers Systementwicklung GbR
  6. *
  7. * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@desy.de>
  8. *
  9. * Thanks to Twinhan who kindly provided hardware and information.
  10. *
  11. * This file can be removed soon, after the DST-driver is rewritten to provice
  12. * the frontend-controlling separately.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation, version 2.
  17. *
  18. * see Documentation/dvb/README.dvb-usb for more information
  19. *
  20. */
  21. #include "vp702x.h"
  22. struct vp702x_fe_state {
  23. struct dvb_frontend fe;
  24. struct dvb_usb_device *d;
  25. struct dvb_frontend_ops ops;
  26. fe_sec_voltage_t voltage;
  27. fe_sec_tone_mode_t tone_mode;
  28. u8 lnb_buf[8];
  29. u8 lock;
  30. u8 sig;
  31. u8 snr;
  32. unsigned long next_status_check;
  33. unsigned long status_check_interval;
  34. };
  35. static int vp702x_fe_refresh_state(struct vp702x_fe_state *st)
  36. {
  37. u8 *buf;
  38. if (time_after(jiffies, st->next_status_check)) {
  39. buf = kmalloc(10, GFP_KERNEL);
  40. if (!buf) {
  41. deb_fe("%s: buffer alloc failed\n", __func__);
  42. return -ENOMEM;
  43. }
  44. vp702x_usb_in_op(st->d, READ_STATUS, 0, 0, buf, 10);
  45. st->lock = buf[4];
  46. vp702x_usb_in_op(st->d, READ_TUNER_REG_REQ, 0x11, 0, buf, 1);
  47. st->snr = buf[0];
  48. vp702x_usb_in_op(st->d, READ_TUNER_REG_REQ, 0x15, 0, buf, 1);
  49. st->sig = buf[0];
  50. st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
  51. kfree(buf);
  52. }
  53. return 0;
  54. }
  55. static u8 vp702x_chksum(u8 *buf,int f, int count)
  56. {
  57. u8 s = 0;
  58. int i;
  59. for (i = f; i < f+count; i++)
  60. s += buf[i];
  61. return ~s+1;
  62. }
  63. static int vp702x_fe_read_status(struct dvb_frontend* fe, fe_status_t *status)
  64. {
  65. struct vp702x_fe_state *st = fe->demodulator_priv;
  66. vp702x_fe_refresh_state(st);
  67. deb_fe("%s\n",__func__);
  68. if (st->lock == 0)
  69. *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
  70. else
  71. *status = 0;
  72. if (*status & FE_HAS_LOCK)
  73. st->status_check_interval = 1000;
  74. else
  75. st->status_check_interval = 250;
  76. return 0;
  77. }
  78. /* not supported by this Frontend */
  79. static int vp702x_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  80. {
  81. struct vp702x_fe_state *st = fe->demodulator_priv;
  82. vp702x_fe_refresh_state(st);
  83. *ber = 0;
  84. return 0;
  85. }
  86. /* not supported by this Frontend */
  87. static int vp702x_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  88. {
  89. struct vp702x_fe_state *st = fe->demodulator_priv;
  90. vp702x_fe_refresh_state(st);
  91. *unc = 0;
  92. return 0;
  93. }
  94. static int vp702x_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  95. {
  96. struct vp702x_fe_state *st = fe->demodulator_priv;
  97. vp702x_fe_refresh_state(st);
  98. *strength = (st->sig << 8) | st->sig;
  99. return 0;
  100. }
  101. static int vp702x_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  102. {
  103. u8 _snr;
  104. struct vp702x_fe_state *st = fe->demodulator_priv;
  105. vp702x_fe_refresh_state(st);
  106. _snr = (st->snr & 0x1f) * 0xff / 0x1f;
  107. *snr = (_snr << 8) | _snr;
  108. return 0;
  109. }
  110. static int vp702x_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  111. {
  112. deb_fe("%s\n",__func__);
  113. tune->min_delay_ms = 2000;
  114. return 0;
  115. }
  116. static int vp702x_fe_set_frontend(struct dvb_frontend* fe,
  117. struct dvb_frontend_parameters *fep)
  118. {
  119. struct vp702x_fe_state *st = fe->demodulator_priv;
  120. u32 freq = fep->frequency/1000;
  121. /*CalFrequency*/
  122. /* u16 frequencyRef[16] = { 2, 4, 8, 16, 32, 64, 128, 256, 24, 5, 10, 20, 40, 80, 160, 320 }; */
  123. u64 sr;
  124. u8 *cmd;
  125. cmd = kzalloc(10, GFP_KERNEL);
  126. if (!cmd)
  127. return -ENOMEM;
  128. cmd[0] = (freq >> 8) & 0x7f;
  129. cmd[1] = freq & 0xff;
  130. cmd[2] = 1; /* divrate == 4 -> frequencyRef[1] -> 1 here */
  131. sr = (u64) (fep->u.qpsk.symbol_rate/1000) << 20;
  132. do_div(sr,88000);
  133. cmd[3] = (sr >> 12) & 0xff;
  134. cmd[4] = (sr >> 4) & 0xff;
  135. cmd[5] = (sr << 4) & 0xf0;
  136. deb_fe("setting frontend to: %u -> %u (%x) LNB-based GHz, symbolrate: %d -> %lu (%lx)\n",
  137. fep->frequency,freq,freq, fep->u.qpsk.symbol_rate,
  138. (unsigned long) sr, (unsigned long) sr);
  139. /* if (fep->inversion == INVERSION_ON)
  140. cmd[6] |= 0x80; */
  141. if (st->voltage == SEC_VOLTAGE_18)
  142. cmd[6] |= 0x40;
  143. /* if (fep->u.qpsk.symbol_rate > 8000000)
  144. cmd[6] |= 0x20;
  145. if (fep->frequency < 1531000)
  146. cmd[6] |= 0x04;
  147. if (st->tone_mode == SEC_TONE_ON)
  148. cmd[6] |= 0x01;*/
  149. cmd[7] = vp702x_chksum(cmd,0,7);
  150. st->status_check_interval = 250;
  151. st->next_status_check = jiffies;
  152. vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
  153. if (cmd[2] == 0 && cmd[3] == 0)
  154. deb_fe("tuning failed.\n");
  155. else
  156. deb_fe("tuning succeeded.\n");
  157. kfree(cmd);
  158. return 0;
  159. }
  160. static int vp702x_fe_init(struct dvb_frontend *fe)
  161. {
  162. struct vp702x_fe_state *st = fe->demodulator_priv;
  163. deb_fe("%s\n",__func__);
  164. vp702x_usb_in_op(st->d, RESET_TUNER, 0, 0, NULL, 0);
  165. return 0;
  166. }
  167. static int vp702x_fe_sleep(struct dvb_frontend *fe)
  168. {
  169. deb_fe("%s\n",__func__);
  170. return 0;
  171. }
  172. static int vp702x_fe_get_frontend(struct dvb_frontend* fe,
  173. struct dvb_frontend_parameters *fep)
  174. {
  175. deb_fe("%s\n",__func__);
  176. return 0;
  177. }
  178. static int vp702x_fe_send_diseqc_msg (struct dvb_frontend* fe,
  179. struct dvb_diseqc_master_cmd *m)
  180. {
  181. int ret;
  182. u8 *cmd;
  183. struct vp702x_fe_state *st = fe->demodulator_priv;
  184. cmd = kzalloc(10, GFP_KERNEL);
  185. if (!cmd)
  186. return -ENOMEM;
  187. deb_fe("%s\n",__func__);
  188. if (m->msg_len > 4) {
  189. ret = -EINVAL;
  190. goto out;
  191. }
  192. cmd[1] = SET_DISEQC_CMD;
  193. cmd[2] = m->msg_len;
  194. memcpy(&cmd[3], m->msg, m->msg_len);
  195. cmd[7] = vp702x_chksum(cmd, 0, 7);
  196. vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
  197. if (cmd[2] == 0 && cmd[3] == 0)
  198. deb_fe("diseqc cmd failed.\n");
  199. else
  200. deb_fe("diseqc cmd succeeded.\n");
  201. ret = 0;
  202. out:
  203. kfree(cmd);
  204. return ret;
  205. }
  206. static int vp702x_fe_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  207. {
  208. deb_fe("%s\n",__func__);
  209. return 0;
  210. }
  211. static int vp702x_fe_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  212. {
  213. struct vp702x_fe_state *st = fe->demodulator_priv;
  214. u8 *buf;
  215. deb_fe("%s\n",__func__);
  216. buf = kmalloc(10, GFP_KERNEL);
  217. if (!buf)
  218. return -ENOMEM;
  219. st->tone_mode = tone;
  220. if (tone == SEC_TONE_ON)
  221. st->lnb_buf[2] = 0x02;
  222. else
  223. st->lnb_buf[2] = 0x00;
  224. st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
  225. memcpy(buf, st->lnb_buf, 8);
  226. vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
  227. if (buf[2] == 0 && buf[3] == 0)
  228. deb_fe("set_tone cmd failed.\n");
  229. else
  230. deb_fe("set_tone cmd succeeded.\n");
  231. kfree(buf);
  232. return 0;
  233. }
  234. static int vp702x_fe_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t
  235. voltage)
  236. {
  237. struct vp702x_fe_state *st = fe->demodulator_priv;
  238. u8 *buf;
  239. deb_fe("%s\n",__func__);
  240. buf = kmalloc(10, GFP_KERNEL);
  241. if (!buf)
  242. return -ENOMEM;
  243. st->voltage = voltage;
  244. if (voltage != SEC_VOLTAGE_OFF)
  245. st->lnb_buf[4] = 0x01;
  246. else
  247. st->lnb_buf[4] = 0x00;
  248. st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
  249. memcpy(buf, st->lnb_buf, 8);
  250. vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
  251. if (buf[2] == 0 && buf[3] == 0)
  252. deb_fe("set_voltage cmd failed.\n");
  253. else
  254. deb_fe("set_voltage cmd succeeded.\n");
  255. kfree(buf);
  256. return 0;
  257. }
  258. static void vp702x_fe_release(struct dvb_frontend* fe)
  259. {
  260. struct vp702x_fe_state *st = fe->demodulator_priv;
  261. kfree(st);
  262. }
  263. static struct dvb_frontend_ops vp702x_fe_ops;
  264. struct dvb_frontend * vp702x_fe_attach(struct dvb_usb_device *d)
  265. {
  266. struct vp702x_fe_state *s = kzalloc(sizeof(struct vp702x_fe_state), GFP_KERNEL);
  267. if (s == NULL)
  268. goto error;
  269. s->d = d;
  270. memcpy(&s->fe.ops,&vp702x_fe_ops,sizeof(struct dvb_frontend_ops));
  271. s->fe.demodulator_priv = s;
  272. s->lnb_buf[1] = SET_LNB_POWER;
  273. s->lnb_buf[3] = 0xff; /* 0=tone burst, 2=data burst, ff=off */
  274. return &s->fe;
  275. error:
  276. return NULL;
  277. }
  278. static struct dvb_frontend_ops vp702x_fe_ops = {
  279. .info = {
  280. .name = "Twinhan DST-like frontend (VP7021/VP7020) DVB-S",
  281. .type = FE_QPSK,
  282. .frequency_min = 950000,
  283. .frequency_max = 2150000,
  284. .frequency_stepsize = 1000, /* kHz for QPSK frontends */
  285. .frequency_tolerance = 0,
  286. .symbol_rate_min = 1000000,
  287. .symbol_rate_max = 45000000,
  288. .symbol_rate_tolerance = 500, /* ppm */
  289. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  290. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  291. FE_CAN_QPSK |
  292. FE_CAN_FEC_AUTO
  293. },
  294. .release = vp702x_fe_release,
  295. .init = vp702x_fe_init,
  296. .sleep = vp702x_fe_sleep,
  297. .set_frontend = vp702x_fe_set_frontend,
  298. .get_frontend = vp702x_fe_get_frontend,
  299. .get_tune_settings = vp702x_fe_get_tune_settings,
  300. .read_status = vp702x_fe_read_status,
  301. .read_ber = vp702x_fe_read_ber,
  302. .read_signal_strength = vp702x_fe_read_signal_strength,
  303. .read_snr = vp702x_fe_read_snr,
  304. .read_ucblocks = vp702x_fe_read_unc_blocks,
  305. .diseqc_send_master_cmd = vp702x_fe_send_diseqc_msg,
  306. .diseqc_send_burst = vp702x_fe_send_diseqc_burst,
  307. .set_tone = vp702x_fe_set_tone,
  308. .set_voltage = vp702x_fe_set_voltage,
  309. };