vp702x.c 8.6 KB

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