vp702x-fe.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. 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, fe_status_t *status)
  62. {
  63. struct vp702x_fe_state *st = fe->demodulator_priv;
  64. vp702x_fe_refresh_state(st);
  65. deb_fe("%s\n",__func__);
  66. if (st->lock == 0)
  67. *status = FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_SIGNAL | FE_HAS_CARRIER;
  68. else
  69. *status = 0;
  70. if (*status & FE_HAS_LOCK)
  71. st->status_check_interval = 1000;
  72. else
  73. st->status_check_interval = 250;
  74. return 0;
  75. }
  76. /* not supported by this Frontend */
  77. static int vp702x_fe_read_ber(struct dvb_frontend* fe, u32 *ber)
  78. {
  79. struct vp702x_fe_state *st = fe->demodulator_priv;
  80. vp702x_fe_refresh_state(st);
  81. *ber = 0;
  82. return 0;
  83. }
  84. /* not supported by this Frontend */
  85. static int vp702x_fe_read_unc_blocks(struct dvb_frontend* fe, u32 *unc)
  86. {
  87. struct vp702x_fe_state *st = fe->demodulator_priv;
  88. vp702x_fe_refresh_state(st);
  89. *unc = 0;
  90. return 0;
  91. }
  92. static int vp702x_fe_read_signal_strength(struct dvb_frontend* fe, u16 *strength)
  93. {
  94. struct vp702x_fe_state *st = fe->demodulator_priv;
  95. vp702x_fe_refresh_state(st);
  96. *strength = (st->sig << 8) | st->sig;
  97. return 0;
  98. }
  99. static int vp702x_fe_read_snr(struct dvb_frontend* fe, u16 *snr)
  100. {
  101. u8 _snr;
  102. struct vp702x_fe_state *st = fe->demodulator_priv;
  103. vp702x_fe_refresh_state(st);
  104. _snr = (st->snr & 0x1f) * 0xff / 0x1f;
  105. *snr = (_snr << 8) | _snr;
  106. return 0;
  107. }
  108. static int vp702x_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
  109. {
  110. deb_fe("%s\n",__func__);
  111. tune->min_delay_ms = 2000;
  112. return 0;
  113. }
  114. static int vp702x_fe_set_frontend(struct dvb_frontend *fe)
  115. {
  116. struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
  117. struct vp702x_fe_state *st = fe->demodulator_priv;
  118. struct vp702x_device_state *dst = st->d->priv;
  119. u32 freq = fep->frequency/1000;
  120. /*CalFrequency*/
  121. /* u16 frequencyRef[16] = { 2, 4, 8, 16, 32, 64, 128, 256, 24, 5, 10, 20, 40, 80, 160, 320 }; */
  122. u64 sr;
  123. u8 *cmd;
  124. mutex_lock(&dst->buf_mutex);
  125. cmd = dst->buf;
  126. memset(cmd, 0, 10);
  127. cmd[0] = (freq >> 8) & 0x7f;
  128. cmd[1] = freq & 0xff;
  129. cmd[2] = 1; /* divrate == 4 -> frequencyRef[1] -> 1 here */
  130. sr = (u64) (fep->symbol_rate/1000) << 20;
  131. do_div(sr,88000);
  132. cmd[3] = (sr >> 12) & 0xff;
  133. cmd[4] = (sr >> 4) & 0xff;
  134. cmd[5] = (sr << 4) & 0xf0;
  135. deb_fe("setting frontend to: %u -> %u (%x) LNB-based GHz, symbolrate: %d -> %lu (%lx)\n",
  136. fep->frequency, freq, freq, fep->symbol_rate,
  137. (unsigned long) sr, (unsigned long) sr);
  138. /* if (fep->inversion == INVERSION_ON)
  139. cmd[6] |= 0x80; */
  140. if (st->voltage == SEC_VOLTAGE_18)
  141. cmd[6] |= 0x40;
  142. /* if (fep->symbol_rate > 8000000)
  143. cmd[6] |= 0x20;
  144. if (fep->frequency < 1531000)
  145. cmd[6] |= 0x04;
  146. if (st->tone_mode == SEC_TONE_ON)
  147. cmd[6] |= 0x01;*/
  148. cmd[7] = vp702x_chksum(cmd,0,7);
  149. st->status_check_interval = 250;
  150. st->next_status_check = jiffies;
  151. vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
  152. if (cmd[2] == 0 && cmd[3] == 0)
  153. deb_fe("tuning failed.\n");
  154. else
  155. deb_fe("tuning succeeded.\n");
  156. mutex_unlock(&dst->buf_mutex);
  157. return 0;
  158. }
  159. static int vp702x_fe_init(struct dvb_frontend *fe)
  160. {
  161. struct vp702x_fe_state *st = fe->demodulator_priv;
  162. deb_fe("%s\n",__func__);
  163. vp702x_usb_in_op(st->d, RESET_TUNER, 0, 0, NULL, 0);
  164. return 0;
  165. }
  166. static int vp702x_fe_sleep(struct dvb_frontend *fe)
  167. {
  168. deb_fe("%s\n",__func__);
  169. return 0;
  170. }
  171. static int vp702x_fe_send_diseqc_msg (struct dvb_frontend* fe,
  172. struct dvb_diseqc_master_cmd *m)
  173. {
  174. u8 *cmd;
  175. struct vp702x_fe_state *st = fe->demodulator_priv;
  176. struct vp702x_device_state *dst = st->d->priv;
  177. deb_fe("%s\n",__func__);
  178. if (m->msg_len > 4)
  179. return -EINVAL;
  180. mutex_lock(&dst->buf_mutex);
  181. cmd = dst->buf;
  182. cmd[1] = SET_DISEQC_CMD;
  183. cmd[2] = m->msg_len;
  184. memcpy(&cmd[3], m->msg, m->msg_len);
  185. cmd[7] = vp702x_chksum(cmd, 0, 7);
  186. vp702x_usb_inout_op(st->d, cmd, 8, cmd, 10, 100);
  187. if (cmd[2] == 0 && cmd[3] == 0)
  188. deb_fe("diseqc cmd failed.\n");
  189. else
  190. deb_fe("diseqc cmd succeeded.\n");
  191. mutex_unlock(&dst->buf_mutex);
  192. return 0;
  193. }
  194. static int vp702x_fe_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  195. {
  196. deb_fe("%s\n",__func__);
  197. return 0;
  198. }
  199. static int vp702x_fe_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  200. {
  201. struct vp702x_fe_state *st = fe->demodulator_priv;
  202. struct vp702x_device_state *dst = st->d->priv;
  203. u8 *buf;
  204. deb_fe("%s\n",__func__);
  205. st->tone_mode = tone;
  206. if (tone == SEC_TONE_ON)
  207. st->lnb_buf[2] = 0x02;
  208. else
  209. st->lnb_buf[2] = 0x00;
  210. st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
  211. mutex_lock(&dst->buf_mutex);
  212. buf = dst->buf;
  213. memcpy(buf, st->lnb_buf, 8);
  214. vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
  215. if (buf[2] == 0 && buf[3] == 0)
  216. deb_fe("set_tone cmd failed.\n");
  217. else
  218. deb_fe("set_tone cmd succeeded.\n");
  219. mutex_unlock(&dst->buf_mutex);
  220. return 0;
  221. }
  222. static int vp702x_fe_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t
  223. voltage)
  224. {
  225. struct vp702x_fe_state *st = fe->demodulator_priv;
  226. struct vp702x_device_state *dst = st->d->priv;
  227. u8 *buf;
  228. deb_fe("%s\n",__func__);
  229. st->voltage = voltage;
  230. if (voltage != SEC_VOLTAGE_OFF)
  231. st->lnb_buf[4] = 0x01;
  232. else
  233. st->lnb_buf[4] = 0x00;
  234. st->lnb_buf[7] = vp702x_chksum(st->lnb_buf, 0, 7);
  235. mutex_lock(&dst->buf_mutex);
  236. buf = dst->buf;
  237. memcpy(buf, st->lnb_buf, 8);
  238. vp702x_usb_inout_op(st->d, buf, 8, buf, 10, 100);
  239. if (buf[2] == 0 && buf[3] == 0)
  240. deb_fe("set_voltage cmd failed.\n");
  241. else
  242. deb_fe("set_voltage cmd succeeded.\n");
  243. mutex_unlock(&dst->buf_mutex);
  244. return 0;
  245. }
  246. static void vp702x_fe_release(struct dvb_frontend* fe)
  247. {
  248. struct vp702x_fe_state *st = fe->demodulator_priv;
  249. kfree(st);
  250. }
  251. static struct dvb_frontend_ops vp702x_fe_ops;
  252. struct dvb_frontend * vp702x_fe_attach(struct dvb_usb_device *d)
  253. {
  254. struct vp702x_fe_state *s = kzalloc(sizeof(struct vp702x_fe_state), GFP_KERNEL);
  255. if (s == NULL)
  256. goto error;
  257. s->d = d;
  258. memcpy(&s->fe.ops,&vp702x_fe_ops,sizeof(struct dvb_frontend_ops));
  259. s->fe.demodulator_priv = s;
  260. s->lnb_buf[1] = SET_LNB_POWER;
  261. s->lnb_buf[3] = 0xff; /* 0=tone burst, 2=data burst, ff=off */
  262. return &s->fe;
  263. error:
  264. return NULL;
  265. }
  266. static struct dvb_frontend_ops vp702x_fe_ops = {
  267. .delsys = { SYS_DVBS },
  268. .info = {
  269. .name = "Twinhan DST-like frontend (VP7021/VP7020) DVB-S",
  270. .frequency_min = 950000,
  271. .frequency_max = 2150000,
  272. .frequency_stepsize = 1000, /* kHz for QPSK frontends */
  273. .frequency_tolerance = 0,
  274. .symbol_rate_min = 1000000,
  275. .symbol_rate_max = 45000000,
  276. .symbol_rate_tolerance = 500, /* ppm */
  277. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  278. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  279. FE_CAN_QPSK |
  280. FE_CAN_FEC_AUTO
  281. },
  282. .release = vp702x_fe_release,
  283. .init = vp702x_fe_init,
  284. .sleep = vp702x_fe_sleep,
  285. .set_frontend = vp702x_fe_set_frontend,
  286. .get_tune_settings = vp702x_fe_get_tune_settings,
  287. .read_status = vp702x_fe_read_status,
  288. .read_ber = vp702x_fe_read_ber,
  289. .read_signal_strength = vp702x_fe_read_signal_strength,
  290. .read_snr = vp702x_fe_read_snr,
  291. .read_ucblocks = vp702x_fe_read_unc_blocks,
  292. .diseqc_send_master_cmd = vp702x_fe_send_diseqc_msg,
  293. .diseqc_send_burst = vp702x_fe_send_diseqc_burst,
  294. .set_tone = vp702x_fe_set_tone,
  295. .set_voltage = vp702x_fe_set_voltage,
  296. };