vp702x-fe.c 8.0 KB

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