vp702x-fe.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. enum fe_sec_voltage voltage;
  27. enum fe_sec_tone_mode 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. struct vp702x_device_state *dst = st->d->priv;
  38. u8 *buf;
  39. if (time_after(jiffies, st->next_status_check)) {
  40. mutex_lock(&dst->buf_mutex);
  41. buf = dst->buf;
  42. vp702x_usb_in_op(st->d, READ_STATUS, 0, 0, buf, 10);
  43. st->lock = buf[4];
  44. vp702x_usb_in_op(st->d, READ_TUNER_REG_REQ, 0x11, 0, buf, 1);
  45. st->snr = buf[0];
  46. vp702x_usb_in_op(st->d, READ_TUNER_REG_REQ, 0x15, 0, buf, 1);
  47. st->sig = buf[0];
  48. mutex_unlock(&dst->buf_mutex);
  49. st->next_status_check = jiffies + (st->status_check_interval*HZ)/1000;
  50. }
  51. return 0;
  52. }
  53. static u8 vp702x_chksum(u8 *buf,int f, int count)
  54. {
  55. u8 s = 0;
  56. int i;
  57. for (i = f; i < f+count; i++)
  58. s += buf[i];
  59. return ~s+1;
  60. }
  61. static int vp702x_fe_read_status(struct dvb_frontend *fe,
  62. enum fe_status *status)
  63. {
  64. struct vp702x_fe_state *st = fe->demodulator_priv;
  65. vp702x_fe_refresh_state(st);
  66. deb_fe("%s\n",__func__);
  67. if (st->lock == 0)
  68. *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
  69. else
  70. *status = 0;
  71. if (*status & FE_HAS_LOCK)
  72. st->status_check_interval = 1000;
  73. else
  74. st->status_check_interval = 250;
  75. return 0;
  76. }
  77. /* not supported by this Frontend */
  78. static int vp702x_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  79. {
  80. struct vp702x_fe_state *st = fe->demodulator_priv;
  81. vp702x_fe_refresh_state(st);
  82. *ber = 0;
  83. return 0;
  84. }
  85. /* not supported by this Frontend */
  86. static int vp702x_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  87. {
  88. struct vp702x_fe_state *st = fe->demodulator_priv;
  89. vp702x_fe_refresh_state(st);
  90. *unc = 0;
  91. return 0;
  92. }
  93. static int vp702x_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  94. {
  95. struct vp702x_fe_state *st = fe->demodulator_priv;
  96. vp702x_fe_refresh_state(st);
  97. *strength = (st->sig << 8) | st->sig;
  98. return 0;
  99. }
  100. static int vp702x_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  101. {
  102. u8 _snr;
  103. struct vp702x_fe_state *st = fe->demodulator_priv;
  104. vp702x_fe_refresh_state(st);
  105. _snr = (st->snr & 0x1f) * 0xff / 0x1f;
  106. *snr = (_snr << 8) | _snr;
  107. return 0;
  108. }
  109. static int vp702x_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  110. {
  111. deb_fe("%s\n",__func__);
  112. tune->min_delay_ms = 2000;
  113. return 0;
  114. }
  115. static int vp702x_fe_set_frontend(struct dvb_frontend *fe)
  116. {
  117. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  118. struct vp702x_fe_state *st = fe->demodulator_priv;
  119. struct vp702x_device_state *dst = st->d->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. mutex_lock(&dst->buf_mutex);
  126. cmd = dst->buf;
  127. memset(cmd, 0, 10);
  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->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->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->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. mutex_unlock(&dst->buf_mutex);
  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_send_diseqc_msg (struct dvb_frontend* fe,
  173. struct dvb_diseqc_master_cmd *m)
  174. {
  175. u8 *cmd;
  176. struct vp702x_fe_state *st = fe->demodulator_priv;
  177. struct vp702x_device_state *dst = st->d->priv;
  178. deb_fe("%s\n",__func__);
  179. if (m->msg_len > 4)
  180. return -EINVAL;
  181. mutex_lock(&dst->buf_mutex);
  182. cmd = dst->buf;
  183. cmd[1] = SET_DISEQC_CMD;
  184. cmd[2] = m->msg_len;
  185. memcpy(&cmd[3], m->msg, m->msg_len);
  186. cmd[7] = vp702x_chksum(cmd, 0, 7);
  187. vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
  188. if (cmd[2] == 0 && cmd[3] == 0)
  189. deb_fe("diseqc cmd failed.\n");
  190. else
  191. deb_fe("diseqc cmd succeeded.\n");
  192. mutex_unlock(&dst->buf_mutex);
  193. return 0;
  194. }
  195. static int vp702x_fe_send_diseqc_burst(struct dvb_frontend *fe,
  196. enum fe_sec_mini_cmd burst)
  197. {
  198. deb_fe("%s\n",__func__);
  199. return 0;
  200. }
  201. static int vp702x_fe_set_tone(struct dvb_frontend *fe,
  202. enum fe_sec_tone_mode tone)
  203. {
  204. struct vp702x_fe_state *st = fe->demodulator_priv;
  205. struct vp702x_device_state *dst = st->d->priv;
  206. u8 *buf;
  207. deb_fe("%s\n",__func__);
  208. st->tone_mode = tone;
  209. if (tone == SEC_TONE_ON)
  210. st->lnb_buf[2] = 0x02;
  211. else
  212. st->lnb_buf[2] = 0x00;
  213. st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
  214. mutex_lock(&dst->buf_mutex);
  215. buf = dst->buf;
  216. memcpy(buf, st->lnb_buf, 8);
  217. vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
  218. if (buf[2] == 0 && buf[3] == 0)
  219. deb_fe("set_tone cmd failed.\n");
  220. else
  221. deb_fe("set_tone cmd succeeded.\n");
  222. mutex_unlock(&dst->buf_mutex);
  223. return 0;
  224. }
  225. static int vp702x_fe_set_voltage(struct dvb_frontend *fe,
  226. enum fe_sec_voltage voltage)
  227. {
  228. struct vp702x_fe_state *st = fe->demodulator_priv;
  229. struct vp702x_device_state *dst = st->d->priv;
  230. u8 *buf;
  231. deb_fe("%s\n",__func__);
  232. st->voltage = voltage;
  233. if (voltage != SEC_VOLTAGE_OFF)
  234. st->lnb_buf[4] = 0x01;
  235. else
  236. st->lnb_buf[4] = 0x00;
  237. st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
  238. mutex_lock(&dst->buf_mutex);
  239. buf = dst->buf;
  240. memcpy(buf, st->lnb_buf, 8);
  241. vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
  242. if (buf[2] == 0 && buf[3] == 0)
  243. deb_fe("set_voltage cmd failed.\n");
  244. else
  245. deb_fe("set_voltage cmd succeeded.\n");
  246. mutex_unlock(&dst->buf_mutex);
  247. return 0;
  248. }
  249. static void vp702x_fe_release(struct dvb_frontend* fe)
  250. {
  251. struct vp702x_fe_state *st = fe->demodulator_priv;
  252. kfree(st);
  253. }
  254. static struct dvb_frontend_ops vp702x_fe_ops;
  255. struct dvb_frontend * vp702x_fe_attach(struct dvb_usb_device *d)
  256. {
  257. struct vp702x_fe_state *s = kzalloc(sizeof(struct vp702x_fe_state), GFP_KERNEL);
  258. if (s == NULL)
  259. goto error;
  260. s->d = d;
  261. memcpy(&s->fe.ops,&vp702x_fe_ops,sizeof(struct dvb_frontend_ops));
  262. s->fe.demodulator_priv = s;
  263. s->lnb_buf[1] = SET_LNB_POWER;
  264. s->lnb_buf[3] = 0xff; /* 0=tone burst, 2=data burst, ff=off */
  265. return &s->fe;
  266. error:
  267. return NULL;
  268. }
  269. static struct dvb_frontend_ops vp702x_fe_ops = {
  270. .delsys = { SYS_DVBS },
  271. .info = {
  272. .name = "Twinhan DST-like frontend (VP7021/VP7020) DVB-S",
  273. .frequency_min = 950000,
  274. .frequency_max = 2150000,
  275. .frequency_stepsize = 1000, /* kHz for QPSK frontends */
  276. .frequency_tolerance = 0,
  277. .symbol_rate_min = 1000000,
  278. .symbol_rate_max = 45000000,
  279. .symbol_rate_tolerance = 500, /* ppm */
  280. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  281. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  282. FE_CAN_QPSK |
  283. FE_CAN_FEC_AUTO
  284. },
  285. .release = vp702x_fe_release,
  286. .init = vp702x_fe_init,
  287. .sleep = vp702x_fe_sleep,
  288. .set_frontend = vp702x_fe_set_frontend,
  289. .get_tune_settings = vp702x_fe_get_tune_settings,
  290. .read_status = vp702x_fe_read_status,
  291. .read_ber = vp702x_fe_read_ber,
  292. .read_signal_strength = vp702x_fe_read_signal_strength,
  293. .read_snr = vp702x_fe_read_snr,
  294. .read_ucblocks = vp702x_fe_read_unc_blocks,
  295. .diseqc_send_master_cmd = vp702x_fe_send_diseqc_msg,
  296. .diseqc_send_burst = vp702x_fe_send_diseqc_burst,
  297. .set_tone = vp702x_fe_set_tone,
  298. .set_voltage = vp702x_fe_set_voltage,
  299. };