vp702x.c 10 KB

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