vp702x.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. int ret = 0;
  80. u8 *buf;
  81. int buflen = max(olen + 2, ilen + 1);
  82. buf = kmalloc(buflen, GFP_KERNEL);
  83. if (!buf)
  84. return -ENOMEM;
  85. buf[0] = 0x00;
  86. buf[1] = cmd;
  87. memcpy(&buf[2], o, olen);
  88. ret = vp702x_usb_inout_op(d, buf, olen+2, buf, ilen+1, msec);
  89. if (ret == 0)
  90. memcpy(i, &buf[1], ilen);
  91. kfree(buf);
  92. return ret;
  93. }
  94. static int vp702x_set_pld_mode(struct dvb_usb_adapter *adap, u8 bypass)
  95. {
  96. int ret;
  97. u8 *buf = kzalloc(16, GFP_KERNEL);
  98. if (!buf)
  99. return -ENOMEM;
  100. ret = vp702x_usb_in_op(adap->dev, 0xe0, (bypass << 8) | 0x0e,
  101. 0, buf, 16);
  102. kfree(buf);
  103. return ret;
  104. }
  105. static int vp702x_set_pld_state(struct dvb_usb_adapter *adap, u8 state)
  106. {
  107. int ret;
  108. u8 *buf = kzalloc(16, GFP_KERNEL);
  109. if (!buf)
  110. return -ENOMEM;
  111. ret = vp702x_usb_in_op(adap->dev, 0xe0, (state << 8) | 0x0f,
  112. 0, buf, 16);
  113. kfree(buf);
  114. return ret;
  115. }
  116. static int vp702x_set_pid(struct dvb_usb_adapter *adap, u16 pid, u8 id, int onoff)
  117. {
  118. struct vp702x_adapter_state *st = adap->priv;
  119. u8 *buf;
  120. buf = kzalloc(16, GFP_KERNEL);
  121. if (!buf)
  122. return -ENOMEM;
  123. if (onoff)
  124. st->pid_filter_state |= (1 << id);
  125. else {
  126. st->pid_filter_state &= ~(1 << id);
  127. pid = 0xffff;
  128. }
  129. id = 0x10 + id*2;
  130. vp702x_set_pld_state(adap, st->pid_filter_state);
  131. vp702x_usb_in_op(adap->dev, 0xe0, (((pid >> 8) & 0xff) << 8) | (id), 0, buf, 16);
  132. vp702x_usb_in_op(adap->dev, 0xe0, (((pid ) & 0xff) << 8) | (id+1), 0, buf, 16);
  133. kfree(buf);
  134. return 0;
  135. }
  136. static int vp702x_init_pid_filter(struct dvb_usb_adapter *adap)
  137. {
  138. struct vp702x_adapter_state *st = adap->priv;
  139. int i;
  140. u8 *b;
  141. b = kzalloc(10, GFP_KERNEL);
  142. if (!b)
  143. return -ENOMEM;
  144. st->pid_filter_count = 8;
  145. st->pid_filter_can_bypass = 1;
  146. st->pid_filter_state = 0x00;
  147. vp702x_set_pld_mode(adap, 1); /* bypass */
  148. for (i = 0; i < st->pid_filter_count; i++)
  149. vp702x_set_pid(adap, 0xffff, i, 1);
  150. vp702x_usb_in_op(adap->dev, 0xb5, 3, 0, b, 10);
  151. vp702x_usb_in_op(adap->dev, 0xb5, 0, 0, b, 10);
  152. vp702x_usb_in_op(adap->dev, 0xb5, 1, 0, b, 10);
  153. //vp702x_set_pld_mode(d, 0); // filter
  154. kfree(b);
  155. return 0;
  156. }
  157. static int vp702x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  158. {
  159. return 0;
  160. }
  161. /* keys for the enclosed remote control */
  162. static struct rc_map_table rc_map_vp702x_table[] = {
  163. { 0x0001, KEY_1 },
  164. { 0x0002, KEY_2 },
  165. };
  166. /* remote control stuff (does not work with my box) */
  167. static int vp702x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  168. {
  169. u8 *key;
  170. int i;
  171. /* remove the following return to enabled remote querying */
  172. return 0;
  173. key = kmalloc(10, GFP_KERNEL);
  174. if (!key)
  175. return -ENOMEM;
  176. vp702x_usb_in_op(d,READ_REMOTE_REQ,0,0,key,10);
  177. deb_rc("remote query key: %x %d\n",key[1],key[1]);
  178. if (key[1] == 0x44) {
  179. *state = REMOTE_NO_KEY_PRESSED;
  180. kfree(key);
  181. return 0;
  182. }
  183. for (i = 0; i < ARRAY_SIZE(rc_map_vp702x_table); i++)
  184. if (rc5_custom(&rc_map_vp702x_table[i]) == key[1]) {
  185. *state = REMOTE_KEY_PRESSED;
  186. *event = rc_map_vp702x_table[i].keycode;
  187. break;
  188. }
  189. kfree(key);
  190. return 0;
  191. }
  192. static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
  193. {
  194. u8 i, *buf;
  195. buf = kmalloc(6, GFP_KERNEL);
  196. if (!buf)
  197. return -ENOMEM;
  198. for (i = 6; i < 12; i++)
  199. vp702x_usb_in_op(d, READ_EEPROM_REQ, i, 1, &buf[i - 6], 1);
  200. memcpy(mac, buf, 6);
  201. kfree(buf);
  202. return 0;
  203. }
  204. static int vp702x_frontend_attach(struct dvb_usb_adapter *adap)
  205. {
  206. u8 buf[10] = { 0 };
  207. vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
  208. if (vp702x_usb_inout_cmd(adap->dev, GET_SYSTEM_STRING, NULL, 0,
  209. buf, 10, 10))
  210. return -EIO;
  211. buf[9] = '\0';
  212. info("system string: %s",&buf[1]);
  213. vp702x_init_pid_filter(adap);
  214. adap->fe = vp702x_fe_attach(adap->dev);
  215. vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
  216. return 0;
  217. }
  218. static struct dvb_usb_device_properties vp702x_properties;
  219. static int vp702x_usb_probe(struct usb_interface *intf,
  220. const struct usb_device_id *id)
  221. {
  222. struct dvb_usb_device *d;
  223. struct vp702x_device_state *st;
  224. int ret;
  225. ret = dvb_usb_device_init(intf, &vp702x_properties,
  226. THIS_MODULE, &d, adapter_nr);
  227. if (ret)
  228. goto out;
  229. st = d->priv;
  230. st->buf_len = 16;
  231. st->buf = kmalloc(st->buf_len, GFP_KERNEL);
  232. if (!st->buf) {
  233. ret = -ENOMEM;
  234. dvb_usb_device_exit(intf);
  235. goto out;
  236. }
  237. mutex_init(&st->buf_mutex);
  238. out:
  239. return ret;
  240. }
  241. static void vp702x_usb_disconnect(struct usb_interface *intf)
  242. {
  243. struct dvb_usb_device *d = usb_get_intfdata(intf);
  244. struct vp702x_device_state *st = d->priv;
  245. mutex_lock(&st->buf_mutex);
  246. kfree(st->buf);
  247. mutex_unlock(&st->buf_mutex);
  248. dvb_usb_device_exit(intf);
  249. }
  250. static struct usb_device_id vp702x_usb_table [] = {
  251. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_COLD) },
  252. // { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
  253. // { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
  254. { 0 },
  255. };
  256. MODULE_DEVICE_TABLE(usb, vp702x_usb_table);
  257. static struct dvb_usb_device_properties vp702x_properties = {
  258. .usb_ctrl = CYPRESS_FX2,
  259. .firmware = "dvb-usb-vp702x-02.fw",
  260. .no_reconnect = 1,
  261. .size_of_priv = sizeof(struct vp702x_device_state),
  262. .num_adapters = 1,
  263. .adapter = {
  264. {
  265. .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS,
  266. .streaming_ctrl = vp702x_streaming_ctrl,
  267. .frontend_attach = vp702x_frontend_attach,
  268. /* parameter for the MPEG2-data transfer */
  269. .stream = {
  270. .type = USB_BULK,
  271. .count = 10,
  272. .endpoint = 0x02,
  273. .u = {
  274. .bulk = {
  275. .buffersize = 4096,
  276. }
  277. }
  278. },
  279. .size_of_priv = sizeof(struct vp702x_adapter_state),
  280. }
  281. },
  282. .read_mac_address = vp702x_read_mac_addr,
  283. .rc.legacy = {
  284. .rc_map_table = rc_map_vp702x_table,
  285. .rc_map_size = ARRAY_SIZE(rc_map_vp702x_table),
  286. .rc_interval = 400,
  287. .rc_query = vp702x_rc_query,
  288. },
  289. .num_device_descs = 1,
  290. .devices = {
  291. { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
  292. .cold_ids = { &vp702x_usb_table[0], NULL },
  293. .warm_ids = { NULL },
  294. },
  295. /* { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
  296. .cold_ids = { &vp702x_usb_table[2], NULL },
  297. .warm_ids = { &vp702x_usb_table[3], NULL },
  298. },
  299. */ { NULL },
  300. }
  301. };
  302. /* usb specific object needed to register this driver with the usb subsystem */
  303. static struct usb_driver vp702x_usb_driver = {
  304. .name = "dvb_usb_vp702x",
  305. .probe = vp702x_usb_probe,
  306. .disconnect = vp702x_usb_disconnect,
  307. .id_table = vp702x_usb_table,
  308. };
  309. /* module stuff */
  310. static int __init vp702x_usb_module_init(void)
  311. {
  312. int result;
  313. if ((result = usb_register(&vp702x_usb_driver))) {
  314. err("usb_register failed. (%d)",result);
  315. return result;
  316. }
  317. return 0;
  318. }
  319. static void __exit vp702x_usb_module_exit(void)
  320. {
  321. /* deregister this driver from the USB subsystem */
  322. usb_deregister(&vp702x_usb_driver);
  323. }
  324. module_init(vp702x_usb_module_init);
  325. module_exit(vp702x_usb_module_exit);
  326. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  327. MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
  328. MODULE_VERSION("1.0");
  329. MODULE_LICENSE("GPL");