vp702x-fe.c 8.6 KB

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