vp702x.c 7.2 KB

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