vp702x.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* DVB USB compliant Linux driver for the TwinhanDTV StarBox USB2.0 DVB-S
  2. * 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 program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation, version 2.
  14. *
  15. * see Documentation/dvb/README.dvb-usb for more information
  16. */
  17. #include "vp702x.h"
  18. /* debug */
  19. int dvb_usb_vp702x_debug;
  20. module_param_named(debug,dvb_usb_vp702x_debug, int, 0644);
  21. MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
  22. struct vp702x_state {
  23. int pid_filter_count;
  24. int pid_filter_can_bypass;
  25. u8 pid_filter_state;
  26. };
  27. struct vp702x_device_state {
  28. u8 power_state;
  29. };
  30. /* check for mutex FIXME */
  31. int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
  32. {
  33. int ret = -1;
  34. ret = usb_control_msg(d->udev,
  35. usb_rcvctrlpipe(d->udev,0),
  36. req,
  37. USB_TYPE_VENDOR | USB_DIR_IN,
  38. value,index,b,blen,
  39. 2000);
  40. if (ret < 0) {
  41. warn("usb in operation failed. (%d)", ret);
  42. ret = -EIO;
  43. } else
  44. ret = 0;
  45. deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
  46. debug_dump(b,blen,deb_xfer);
  47. return ret;
  48. }
  49. int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
  50. u16 index, u8 *b, int blen)
  51. {
  52. int ret;
  53. deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
  54. debug_dump(b,blen,deb_xfer);
  55. if ((ret = usb_control_msg(d->udev,
  56. usb_sndctrlpipe(d->udev,0),
  57. req,
  58. USB_TYPE_VENDOR | USB_DIR_OUT,
  59. value,index,b,blen,
  60. 2000)) != blen) {
  61. warn("usb out operation failed. (%d)",ret);
  62. return -EIO;
  63. } else
  64. return 0;
  65. }
  66. int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec)
  67. {
  68. int ret;
  69. if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
  70. return ret;
  71. ret = vp702x_usb_out_op(d,REQUEST_OUT,0,0,o,olen);
  72. msleep(msec);
  73. ret = vp702x_usb_in_op(d,REQUEST_IN,0,0,i,ilen);
  74. mutex_unlock(&d->usb_mutex);
  75. return ret;
  76. }
  77. static int vp702x_usb_inout_cmd(struct dvb_usb_device *d, u8 cmd, u8 *o,
  78. int olen, u8 *i, int ilen, int msec)
  79. {
  80. u8 bout[olen+2];
  81. u8 bin[ilen+1];
  82. int ret = 0;
  83. bout[0] = 0x00;
  84. bout[1] = cmd;
  85. memcpy(&bout[2],o,olen);
  86. ret = vp702x_usb_inout_op(d, bout, olen+2, bin, ilen+1,msec);
  87. if (ret == 0)
  88. memcpy(i,&bin[1],ilen);
  89. return ret;
  90. }
  91. static int vp702x_set_pld_mode(struct dvb_usb_adapter *adap, u8 bypass)
  92. {
  93. u8 buf[16] = { 0 };
  94. return vp702x_usb_in_op(adap->dev, 0xe0, (bypass << 8) | 0x0e, 0, buf, 16);
  95. }
  96. static int vp702x_set_pld_state(struct dvb_usb_adapter *adap, u8 state)
  97. {
  98. u8 buf[16] = { 0 };
  99. return vp702x_usb_in_op(adap->dev, 0xe0, (state << 8) | 0x0f, 0, buf, 16);
  100. }
  101. static int vp702x_set_pid(struct dvb_usb_adapter *adap, u16 pid, u8 id, int onoff)
  102. {
  103. struct vp702x_state *st = adap->priv;
  104. u8 buf[16] = { 0 };
  105. if (onoff)
  106. st->pid_filter_state |= (1 << id);
  107. else {
  108. st->pid_filter_state &= ~(1 << id);
  109. pid = 0xffff;
  110. }
  111. id = 0x10 + id*2;
  112. vp702x_set_pld_state(adap, st->pid_filter_state);
  113. vp702x_usb_in_op(adap->dev, 0xe0, (((pid >> 8) & 0xff) << 8) | (id), 0, buf, 16);
  114. vp702x_usb_in_op(adap->dev, 0xe0, (((pid ) & 0xff) << 8) | (id+1), 0, buf, 16);
  115. return 0;
  116. }
  117. static int vp702x_init_pid_filter(struct dvb_usb_adapter *adap)
  118. {
  119. struct vp702x_state *st = adap->priv;
  120. int i;
  121. u8 b[10] = { 0 };
  122. st->pid_filter_count = 8;
  123. st->pid_filter_can_bypass = 1;
  124. st->pid_filter_state = 0x00;
  125. vp702x_set_pld_mode(adap, 1); // bypass
  126. for (i = 0; i < st->pid_filter_count; i++)
  127. vp702x_set_pid(adap, 0xffff, i, 1);
  128. vp702x_usb_in_op(adap->dev, 0xb5, 3, 0, b, 10);
  129. vp702x_usb_in_op(adap->dev, 0xb5, 0, 0, b, 10);
  130. vp702x_usb_in_op(adap->dev, 0xb5, 1, 0, b, 10);
  131. //vp702x_set_pld_mode(d, 0); // filter
  132. return 0;
  133. }
  134. static int vp702x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  135. {
  136. return 0;
  137. }
  138. /* keys for the enclosed remote control */
  139. static struct dvb_usb_rc_key vp702x_rc_keys[] = {
  140. { 0x00, 0x01, KEY_1 },
  141. { 0x00, 0x02, KEY_2 },
  142. };
  143. /* remote control stuff (does not work with my box) */
  144. static int vp702x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  145. {
  146. u8 key[10];
  147. int i;
  148. /* remove the following return to enabled remote querying */
  149. return 0;
  150. vp702x_usb_in_op(d,READ_REMOTE_REQ,0,0,key,10);
  151. deb_rc("remote query key: %x %d\n",key[1],key[1]);
  152. if (key[1] == 0x44) {
  153. *state = REMOTE_NO_KEY_PRESSED;
  154. return 0;
  155. }
  156. for (i = 0; i < ARRAY_SIZE(vp702x_rc_keys); i++)
  157. if (vp702x_rc_keys[i].custom == key[1]) {
  158. *state = REMOTE_KEY_PRESSED;
  159. *event = vp702x_rc_keys[i].event;
  160. break;
  161. }
  162. return 0;
  163. }
  164. int vp702x_power_ctrl(struct dvb_usb_device *d, int onoff)
  165. {
  166. struct vp702x_device_state *st = d->priv;
  167. if (st->power_state == 0 && onoff)
  168. vp702x_usb_out_op(d, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
  169. else if (st->power_state == 1 && onoff == 0)
  170. vp702x_usb_out_op(d, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
  171. st->power_state = onoff;
  172. return 0;
  173. }
  174. static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
  175. {
  176. u8 i;
  177. for (i = 6; i < 12; i++)
  178. vp702x_usb_in_op(d, READ_EEPROM_REQ, i, 1, &mac[i - 6], 1);
  179. return 0;
  180. }
  181. static int vp702x_frontend_attach(struct dvb_usb_adapter *adap)
  182. {
  183. u8 buf[10] = { 0 };
  184. vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
  185. if (vp702x_usb_inout_cmd(adap->dev, GET_SYSTEM_STRING, NULL, 0, buf, 10, 10))
  186. return -EIO;
  187. buf[9] = '\0';
  188. info("system string: %s",&buf[1]);
  189. vp702x_init_pid_filter(adap);
  190. adap->fe = vp702x_fe_attach(adap->dev);
  191. vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
  192. return 0;
  193. }
  194. static struct dvb_usb_device_properties vp702x_properties;
  195. static int vp702x_usb_probe(struct usb_interface *intf,
  196. const struct usb_device_id *id)
  197. {
  198. return dvb_usb_device_init(intf,&vp702x_properties,THIS_MODULE,NULL);
  199. }
  200. static struct usb_device_id vp702x_usb_table [] = {
  201. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_COLD) },
  202. // { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
  203. // { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
  204. { 0 },
  205. };
  206. MODULE_DEVICE_TABLE(usb, vp702x_usb_table);
  207. static struct dvb_usb_device_properties vp702x_properties = {
  208. .usb_ctrl = CYPRESS_FX2,
  209. .firmware = "dvb-usb-vp702x-02.fw",
  210. .no_reconnect = 1,
  211. .size_of_priv = sizeof(struct vp702x_device_state),
  212. .num_adapters = 1,
  213. .adapter = {
  214. {
  215. .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS,
  216. .streaming_ctrl = vp702x_streaming_ctrl,
  217. .frontend_attach = vp702x_frontend_attach,
  218. /* parameter for the MPEG2-data transfer */
  219. .stream = {
  220. .type = USB_BULK,
  221. .count = 10,
  222. .endpoint = 0x02,
  223. .u = {
  224. .bulk = {
  225. .buffersize = 4096,
  226. }
  227. }
  228. },
  229. .size_of_priv = sizeof(struct vp702x_state),
  230. }
  231. },
  232. .read_mac_address = vp702x_read_mac_addr,
  233. .rc_key_map = vp702x_rc_keys,
  234. .rc_key_map_size = ARRAY_SIZE(vp702x_rc_keys),
  235. .rc_interval = 400,
  236. .rc_query = vp702x_rc_query,
  237. .num_device_descs = 1,
  238. .devices = {
  239. { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
  240. .cold_ids = { &vp702x_usb_table[0], NULL },
  241. .warm_ids = { NULL },
  242. },
  243. /* { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
  244. .cold_ids = { &vp702x_usb_table[2], NULL },
  245. .warm_ids = { &vp702x_usb_table[3], NULL },
  246. },
  247. */ { NULL },
  248. }
  249. };
  250. /* usb specific object needed to register this driver with the usb subsystem */
  251. static struct usb_driver vp702x_usb_driver = {
  252. .name = "dvb_usb_vp702x",
  253. .probe = vp702x_usb_probe,
  254. .disconnect = dvb_usb_device_exit,
  255. .id_table = vp702x_usb_table,
  256. };
  257. /* module stuff */
  258. static int __init vp702x_usb_module_init(void)
  259. {
  260. int result;
  261. if ((result = usb_register(&vp702x_usb_driver))) {
  262. err("usb_register failed. (%d)",result);
  263. return result;
  264. }
  265. return 0;
  266. }
  267. static void __exit vp702x_usb_module_exit(void)
  268. {
  269. /* deregister this driver from the USB subsystem */
  270. usb_deregister(&vp702x_usb_driver);
  271. }
  272. module_init(vp702x_usb_module_init);
  273. module_exit(vp702x_usb_module_exit);
  274. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  275. MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
  276. MODULE_VERSION("1.0");
  277. MODULE_LICENSE("GPL");