vp702x.c 9.7 KB

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