vp702x.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. u8 pid_table[17]; /* [16] controls the pid_table state */
  24. };
  25. /* check for mutex FIXME */
  26. int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
  27. {
  28. int ret = 0,try = 0;
  29. while (ret >= 0 && ret != blen && try < 3) {
  30. ret = usb_control_msg(d->udev,
  31. usb_rcvctrlpipe(d->udev,0),
  32. req,
  33. USB_TYPE_VENDOR | USB_DIR_IN,
  34. value,index,b,blen,
  35. 2000);
  36. deb_info("reading number %d (ret: %d)\n",try,ret);
  37. try++;
  38. }
  39. if (ret < 0 || ret != blen) {
  40. warn("usb in operation failed.");
  41. ret = -EIO;
  42. } else
  43. ret = 0;
  44. deb_xfer("in: req. %x, val: %x, ind: %x, buffer: ",req,value,index);
  45. debug_dump(b,blen,deb_xfer);
  46. return ret;
  47. }
  48. static int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
  49. u16 index, u8 *b, int blen)
  50. {
  51. deb_xfer("out: req. %x, val: %x, ind: %x, buffer: ",req,value,index);
  52. debug_dump(b,blen,deb_xfer);
  53. if (usb_control_msg(d->udev,
  54. usb_sndctrlpipe(d->udev,0),
  55. req,
  56. USB_TYPE_VENDOR | USB_DIR_OUT,
  57. value,index,b,blen,
  58. 2000) != blen) {
  59. warn("usb out operation failed.");
  60. return -EIO;
  61. } else
  62. return 0;
  63. }
  64. int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec)
  65. {
  66. int ret;
  67. if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
  68. return ret;
  69. if ((ret = vp702x_usb_out_op(d,REQUEST_OUT,0,0,o,olen)) < 0)
  70. goto unlock;
  71. msleep(msec);
  72. ret = vp702x_usb_in_op(d,REQUEST_IN,0,0,i,ilen);
  73. unlock:
  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_pid_filter(struct dvb_usb_device *d, int index, u16 pid, int onoff)
  92. {
  93. struct vp702x_state *st = d->priv;
  94. u8 buf[9];
  95. if (onoff) {
  96. st->pid_table[16] |= 1 << index;
  97. st->pid_table[index*2] = (pid >> 8) & 0xff;
  98. st->pid_table[index*2+1] = pid & 0xff;
  99. } else {
  100. st->pid_table[16] &= ~(1 << index);
  101. st->pid_table[index*2] = st->pid_table[index*2+1] = 0;
  102. }
  103. return vp702x_usb_inout_cmd(d,SET_PID_FILTER,st->pid_table,17,buf,9,10);
  104. }
  105. static int vp702x_power_ctrl(struct dvb_usb_device *d, int onoff)
  106. {
  107. vp702x_usb_in_op(d,RESET_TUNER,0,0,NULL,0);
  108. vp702x_usb_in_op(d,SET_TUNER_POWER_REQ,0,onoff,NULL,0);
  109. return vp702x_usb_in_op(d,SET_TUNER_POWER_REQ,0,onoff,NULL,0);
  110. }
  111. /* keys for the enclosed remote control */
  112. static struct dvb_usb_rc_key vp702x_rc_keys[] = {
  113. { 0x00, 0x01, KEY_1 },
  114. { 0x00, 0x02, KEY_2 },
  115. };
  116. /* remote control stuff (does not work with my box) */
  117. static int vp702x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  118. {
  119. u8 key[10];
  120. int i;
  121. /* remove the following return to enabled remote querying */
  122. return 0;
  123. vp702x_usb_in_op(d,READ_REMOTE_REQ,0,0,key,10);
  124. deb_rc("remote query key: %x %d\n",key[1],key[1]);
  125. if (key[1] == 0x44) {
  126. *state = REMOTE_NO_KEY_PRESSED;
  127. return 0;
  128. }
  129. for (i = 0; i < ARRAY_SIZE(vp702x_rc_keys); i++)
  130. if (vp702x_rc_keys[i].custom == key[1]) {
  131. *state = REMOTE_KEY_PRESSED;
  132. *event = vp702x_rc_keys[i].event;
  133. break;
  134. }
  135. return 0;
  136. }
  137. static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
  138. {
  139. u8 macb[9];
  140. if (vp702x_usb_inout_cmd(d, GET_MAC_ADDRESS, NULL, 0, macb, 9, 10))
  141. return -EIO;
  142. memcpy(mac,&macb[3],6);
  143. return 0;
  144. }
  145. static int vp702x_frontend_attach(struct dvb_usb_device *d)
  146. {
  147. u8 buf[9] = { 0 };
  148. if (vp702x_usb_inout_cmd(d, GET_SYSTEM_STRING, NULL, 0, buf, 9, 10))
  149. return -EIO;
  150. buf[8] = '\0';
  151. info("system string: %s",&buf[1]);
  152. d->fe = vp702x_fe_attach(d);
  153. return 0;
  154. }
  155. static struct dvb_usb_properties vp702x_properties;
  156. static int vp702x_usb_probe(struct usb_interface *intf,
  157. const struct usb_device_id *id)
  158. {
  159. struct usb_device *udev = interface_to_usbdev(intf);
  160. usb_clear_halt(udev,usb_sndctrlpipe(udev,0));
  161. usb_clear_halt(udev,usb_rcvctrlpipe(udev,0));
  162. return dvb_usb_device_init(intf,&vp702x_properties,THIS_MODULE,NULL);
  163. }
  164. static struct usb_device_id vp702x_usb_table [] = {
  165. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_COLD) },
  166. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_WARM) },
  167. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
  168. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
  169. { 0 },
  170. };
  171. MODULE_DEVICE_TABLE(usb, vp702x_usb_table);
  172. static struct dvb_usb_properties vp702x_properties = {
  173. .caps = DVB_USB_HAS_PID_FILTER | DVB_USB_NEED_PID_FILTERING,
  174. .pid_filter_count = 8, /* !!! */
  175. .usb_ctrl = CYPRESS_FX2,
  176. .firmware = "dvb-usb-vp702x-01.fw",
  177. .pid_filter = vp702x_pid_filter,
  178. .power_ctrl = vp702x_power_ctrl,
  179. .frontend_attach = vp702x_frontend_attach,
  180. .read_mac_address = vp702x_read_mac_addr,
  181. .rc_key_map = vp702x_rc_keys,
  182. .rc_key_map_size = ARRAY_SIZE(vp702x_rc_keys),
  183. .rc_interval = 400,
  184. .rc_query = vp702x_rc_query,
  185. .size_of_priv = sizeof(struct vp702x_state),
  186. /* parameter for the MPEG2-data transfer */
  187. .urb = {
  188. .type = DVB_USB_BULK,
  189. .count = 7,
  190. .endpoint = 0x02,
  191. .u = {
  192. .bulk = {
  193. .buffersize = 4096,
  194. }
  195. }
  196. },
  197. .num_device_descs = 2,
  198. .devices = {
  199. { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
  200. .cold_ids = { &vp702x_usb_table[0], NULL },
  201. .warm_ids = { &vp702x_usb_table[1], NULL },
  202. },
  203. { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
  204. .cold_ids = { &vp702x_usb_table[2], NULL },
  205. .warm_ids = { &vp702x_usb_table[3], NULL },
  206. },
  207. { 0 },
  208. }
  209. };
  210. /* usb specific object needed to register this driver with the usb subsystem */
  211. static struct usb_driver vp702x_usb_driver = {
  212. .name = "dvb-usb-vp702x",
  213. .probe = vp702x_usb_probe,
  214. .disconnect = dvb_usb_device_exit,
  215. .id_table = vp702x_usb_table,
  216. };
  217. /* module stuff */
  218. static int __init vp702x_usb_module_init(void)
  219. {
  220. int result;
  221. if ((result = usb_register(&vp702x_usb_driver))) {
  222. err("usb_register failed. (%d)",result);
  223. return result;
  224. }
  225. return 0;
  226. }
  227. static void __exit vp702x_usb_module_exit(void)
  228. {
  229. /* deregister this driver from the USB subsystem */
  230. usb_deregister(&vp702x_usb_driver);
  231. }
  232. module_init(vp702x_usb_module_init);
  233. module_exit(vp702x_usb_module_exit);
  234. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  235. MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
  236. MODULE_VERSION("1.0-alpha");
  237. MODULE_LICENSE("GPL");