vp702x-fe.c 8.1 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, sr, sr);
  128. /* if (fep->inversion == INVERSION_ON)
  129. cmd[6] |= 0x80; */
  130. if (st->voltage == SEC_VOLTAGE_18)
  131. cmd[6] |= 0x40;
  132. /* if (fep->u.qpsk.symbol_rate > 8000000)
  133. cmd[6] |= 0x20;
  134. if (fep->frequency < 1531000)
  135. cmd[6] |= 0x04;
  136. if (st->tone_mode == SEC_TONE_ON)
  137. cmd[6] |= 0x01;*/
  138. cmd[7] = vp702x_chksum(cmd,0,7);
  139. st->status_check_interval = 250;
  140. st->next_status_check = jiffies;
  141. vp702x_usb_in_op(st->d, RESET_TUNER, 0, 0, NULL, 0);
  142. msleep(30);
  143. vp702x_usb_inout_op(st->d,cmd,8,ibuf,10,100);
  144. if (ibuf[2] == 0 && ibuf[3] == 0)
  145. deb_fe("tuning failed.\n");
  146. else
  147. deb_fe("tuning succeeded.\n");
  148. return 0;
  149. }
  150. static int vp702x_fe_get_frontend(struct dvb_frontend* fe,
  151. struct dvb_frontend_parameters *fep)
  152. {
  153. deb_fe("%s\n",__FUNCTION__);
  154. return 0;
  155. }
  156. static int vp702x_fe_send_diseqc_msg (struct dvb_frontend* fe,
  157. struct dvb_diseqc_master_cmd *m)
  158. {
  159. struct vp702x_fe_state *st = fe->demodulator_priv;
  160. u8 cmd[8],ibuf[10];
  161. memset(cmd,0,8);
  162. deb_fe("%s\n",__FUNCTION__);
  163. if (m->msg_len > 4)
  164. return -EINVAL;
  165. cmd[1] = SET_DISEQC_CMD;
  166. cmd[2] = m->msg_len;
  167. memcpy(&cmd[3], m->msg, m->msg_len);
  168. cmd[7] = vp702x_chksum(cmd,0,7);
  169. vp702x_usb_inout_op(st->d,cmd,8,ibuf,10,100);
  170. if (ibuf[2] == 0 && ibuf[3] == 0)
  171. deb_fe("diseqc cmd failed.\n");
  172. else
  173. deb_fe("diseqc cmd succeeded.\n");
  174. return 0;
  175. }
  176. static int vp702x_fe_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  177. {
  178. deb_fe("%s\n",__FUNCTION__);
  179. return 0;
  180. }
  181. static int vp702x_fe_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  182. {
  183. struct vp702x_fe_state *st = fe->demodulator_priv;
  184. u8 ibuf[10];
  185. deb_fe("%s\n",__FUNCTION__);
  186. st->tone_mode = tone;
  187. if (tone == SEC_TONE_ON)
  188. st->lnb_buf[2] = 0x02;
  189. else
  190. st->lnb_buf[2] = 0x00;
  191. st->lnb_buf[7] = vp702x_chksum(st->lnb_buf,0,7);
  192. vp702x_usb_inout_op(st->d,st->lnb_buf,8,ibuf,10,100);
  193. if (ibuf[2] == 0 && ibuf[3] == 0)
  194. deb_fe("set_tone cmd failed.\n");
  195. else
  196. deb_fe("set_tone cmd succeeded.\n");
  197. return 0;
  198. }
  199. static int vp702x_fe_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t
  200. voltage)
  201. {
  202. struct vp702x_fe_state *st = fe->demodulator_priv;
  203. u8 ibuf[10];
  204. deb_fe("%s\n",__FUNCTION__);
  205. st->voltage = voltage;
  206. if (voltage != SEC_VOLTAGE_OFF)
  207. st->lnb_buf[4] = 0x01;
  208. else
  209. st->lnb_buf[4] = 0x00;
  210. st->lnb_buf[7] = vp702x_chksum(st->lnb_buf,0,7);
  211. vp702x_usb_inout_op(st->d,st->lnb_buf,8,ibuf,10,100);
  212. if (ibuf[2] == 0 && ibuf[3] == 0)
  213. deb_fe("set_voltage cmd failed.\n");
  214. else
  215. deb_fe("set_voltage cmd succeeded.\n");
  216. return 0;
  217. }
  218. static void vp702x_fe_release(struct dvb_frontend* fe)
  219. {
  220. struct vp702x_fe_state *st = fe->demodulator_priv;
  221. kfree(st);
  222. }
  223. static struct dvb_frontend_ops vp702x_fe_ops;
  224. struct dvb_frontend * vp702x_fe_attach(struct dvb_usb_device *d)
  225. {
  226. struct vp702x_fe_state *s = kmalloc(sizeof(struct vp702x_fe_state), GFP_KERNEL);
  227. if (s == NULL)
  228. goto error;
  229. memset(s,0,sizeof(struct vp702x_fe_state));
  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. };