vp7045.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* DVB USB compliant Linux driver for the
  2. * - TwinhanDTV Alpha/MagicBoxII USB2.0 DVB-T receiver
  3. * - DigitalNow TinyUSB2 DVB-t receiver
  4. *
  5. * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
  6. *
  7. * Thanks to Twinhan who kindly provided hardware and information.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation, version 2.
  12. *
  13. * see Documentation/dvb/README.dvb-usb for more information
  14. */
  15. #include "vp7045.h"
  16. /* debug */
  17. static int dvb_usb_vp7045_debug;
  18. module_param_named(debug,dvb_usb_vp7045_debug, int, 0644);
  19. MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
  20. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  21. #define deb_info(args...) dprintk(dvb_usb_vp7045_debug,0x01,args)
  22. #define deb_xfer(args...) dprintk(dvb_usb_vp7045_debug,0x02,args)
  23. #define deb_rc(args...) dprintk(dvb_usb_vp7045_debug,0x04,args)
  24. int vp7045_usb_op(struct dvb_usb_device *d, u8 cmd, u8 *out, int outlen, u8 *in, int inlen, int msec)
  25. {
  26. int ret = 0;
  27. u8 inbuf[12] = { 0 }, outbuf[20] = { 0 };
  28. outbuf[0] = cmd;
  29. if (outlen > 19)
  30. outlen = 19;
  31. if (inlen > 11)
  32. inlen = 11;
  33. if (out != NULL && outlen > 0)
  34. memcpy(&outbuf[1], out, outlen);
  35. deb_xfer("out buffer: ");
  36. debug_dump(outbuf,outlen+1,deb_xfer);
  37. if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
  38. return ret;
  39. if (usb_control_msg(d->udev,
  40. usb_sndctrlpipe(d->udev,0),
  41. TH_COMMAND_OUT, USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0,
  42. outbuf, 20, 2000) != 20) {
  43. err("USB control message 'out' went wrong.");
  44. ret = -EIO;
  45. goto unlock;
  46. }
  47. msleep(msec);
  48. if (usb_control_msg(d->udev,
  49. usb_rcvctrlpipe(d->udev,0),
  50. TH_COMMAND_IN, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
  51. inbuf, 12, 2000) != 12) {
  52. err("USB control message 'in' went wrong.");
  53. ret = -EIO;
  54. goto unlock;
  55. }
  56. deb_xfer("in buffer: ");
  57. debug_dump(inbuf,12,deb_xfer);
  58. if (in != NULL && inlen > 0)
  59. memcpy(in,&inbuf[1],inlen);
  60. unlock:
  61. mutex_unlock(&d->usb_mutex);
  62. return ret;
  63. }
  64. u8 vp7045_read_reg(struct dvb_usb_device *d, u8 reg)
  65. {
  66. u8 obuf[2] = { 0 },v;
  67. obuf[1] = reg;
  68. vp7045_usb_op(d,TUNER_REG_READ,obuf,2,&v,1,30);
  69. return v;
  70. }
  71. static int vp7045_power_ctrl(struct dvb_usb_device *d, int onoff)
  72. {
  73. u8 v = onoff;
  74. return vp7045_usb_op(d,SET_TUNER_POWER,&v,1,NULL,0,150);
  75. }
  76. /* remote control stuff */
  77. /* The keymapping struct. Somehow this should be loaded to the driver, but
  78. * currently it is hardcoded. */
  79. static struct dvb_usb_rc_key vp7045_rc_keys[] = {
  80. { 0x0016, KEY_POWER },
  81. { 0x0010, KEY_MUTE },
  82. { 0x0003, KEY_1 },
  83. { 0x0001, KEY_2 },
  84. { 0x0006, KEY_3 },
  85. { 0x0009, KEY_4 },
  86. { 0x001d, KEY_5 },
  87. { 0x001f, KEY_6 },
  88. { 0x000d, KEY_7 },
  89. { 0x0019, KEY_8 },
  90. { 0x001b, KEY_9 },
  91. { 0x0015, KEY_0 },
  92. { 0x0005, KEY_CHANNELUP },
  93. { 0x0002, KEY_CHANNELDOWN },
  94. { 0x001e, KEY_VOLUMEUP },
  95. { 0x000a, KEY_VOLUMEDOWN },
  96. { 0x0011, KEY_RECORD },
  97. { 0x0017, KEY_FAVORITES }, /* Heart symbol - Channel list. */
  98. { 0x0014, KEY_PLAY },
  99. { 0x001a, KEY_STOP },
  100. { 0x0040, KEY_REWIND },
  101. { 0x0012, KEY_FASTFORWARD },
  102. { 0x000e, KEY_PREVIOUS }, /* Recall - Previous channel. */
  103. { 0x004c, KEY_PAUSE },
  104. { 0x004d, KEY_SCREEN }, /* Full screen mode. */
  105. { 0x0054, KEY_AUDIO }, /* MTS - Switch to secondary audio. */
  106. { 0x000c, KEY_CANCEL }, /* Cancel */
  107. { 0x001c, KEY_EPG }, /* EPG */
  108. { 0x0000, KEY_TAB }, /* Tab */
  109. { 0x0048, KEY_INFO }, /* Preview */
  110. { 0x0004, KEY_LIST }, /* RecordList */
  111. { 0x000f, KEY_TEXT }, /* Teletext */
  112. { 0x0041, KEY_PREVIOUSSONG },
  113. { 0x0042, KEY_NEXTSONG },
  114. { 0x004b, KEY_UP },
  115. { 0x0051, KEY_DOWN },
  116. { 0x004e, KEY_LEFT },
  117. { 0x0052, KEY_RIGHT },
  118. { 0x004f, KEY_ENTER },
  119. { 0x0013, KEY_CANCEL },
  120. { 0x004a, KEY_CLEAR },
  121. { 0x0054, KEY_PRINT }, /* Capture */
  122. { 0x0043, KEY_SUBTITLE }, /* Subtitle/CC */
  123. { 0x0008, KEY_VIDEO }, /* A/V */
  124. { 0x0007, KEY_SLEEP }, /* Hibernate */
  125. { 0x0045, KEY_ZOOM }, /* Zoom+ */
  126. { 0x0018, KEY_RED},
  127. { 0x0053, KEY_GREEN},
  128. { 0x005e, KEY_YELLOW},
  129. { 0x005f, KEY_BLUE}
  130. };
  131. static int vp7045_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  132. {
  133. u8 key;
  134. int i;
  135. vp7045_usb_op(d,RC_VAL_READ,NULL,0,&key,1,20);
  136. deb_rc("remote query key: %x %d\n",key,key);
  137. if (key == 0x44) {
  138. *state = REMOTE_NO_KEY_PRESSED;
  139. return 0;
  140. }
  141. for (i = 0; i < ARRAY_SIZE(vp7045_rc_keys); i++)
  142. if (rc5_data(&vp7045_rc_keys[i]) == key) {
  143. *state = REMOTE_KEY_PRESSED;
  144. *event = vp7045_rc_keys[i].event;
  145. break;
  146. }
  147. return 0;
  148. }
  149. static int vp7045_read_eeprom(struct dvb_usb_device *d,u8 *buf, int len, int offset)
  150. {
  151. int i = 0;
  152. u8 v,br[2];
  153. for (i=0; i < len; i++) {
  154. v = offset + i;
  155. vp7045_usb_op(d,GET_EE_VALUE,&v,1,br,2,5);
  156. buf[i] = br[1];
  157. }
  158. deb_info("VP7045 EEPROM read (offs: %d, len: %d) : ",offset, i);
  159. debug_dump(buf,i,deb_info);
  160. return 0;
  161. }
  162. static int vp7045_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
  163. {
  164. return vp7045_read_eeprom(d,mac, 6, MAC_0_ADDR);
  165. }
  166. static int vp7045_frontend_attach(struct dvb_usb_adapter *adap)
  167. {
  168. u8 buf[255] = { 0 };
  169. vp7045_usb_op(adap->dev,VENDOR_STRING_READ,NULL,0,buf,20,0);
  170. buf[10] = '\0';
  171. deb_info("firmware says: %s ",buf);
  172. vp7045_usb_op(adap->dev,PRODUCT_STRING_READ,NULL,0,buf,20,0);
  173. buf[10] = '\0';
  174. deb_info("%s ",buf);
  175. vp7045_usb_op(adap->dev,FW_VERSION_READ,NULL,0,buf,20,0);
  176. buf[10] = '\0';
  177. deb_info("v%s\n",buf);
  178. /* Dump the EEPROM */
  179. /* vp7045_read_eeprom(d,buf, 255, FX2_ID_ADDR); */
  180. adap->fe = vp7045_fe_attach(adap->dev);
  181. return 0;
  182. }
  183. static struct dvb_usb_device_properties vp7045_properties;
  184. static int vp7045_usb_probe(struct usb_interface *intf,
  185. const struct usb_device_id *id)
  186. {
  187. return dvb_usb_device_init(intf, &vp7045_properties,
  188. THIS_MODULE, NULL, adapter_nr);
  189. }
  190. static struct usb_device_id vp7045_usb_table [] = {
  191. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7045_COLD) },
  192. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7045_WARM) },
  193. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_DNTV_TINYUSB2_COLD) },
  194. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_DNTV_TINYUSB2_WARM) },
  195. { 0 },
  196. };
  197. MODULE_DEVICE_TABLE(usb, vp7045_usb_table);
  198. static struct dvb_usb_device_properties vp7045_properties = {
  199. .usb_ctrl = CYPRESS_FX2,
  200. .firmware = "dvb-usb-vp7045-01.fw",
  201. .num_adapters = 1,
  202. .adapter = {
  203. {
  204. .frontend_attach = vp7045_frontend_attach,
  205. /* parameter for the MPEG2-data transfer */
  206. .stream = {
  207. .type = USB_BULK,
  208. .count = 7,
  209. .endpoint = 0x02,
  210. .u = {
  211. .bulk = {
  212. .buffersize = 4096,
  213. }
  214. }
  215. },
  216. }
  217. },
  218. .power_ctrl = vp7045_power_ctrl,
  219. .read_mac_address = vp7045_read_mac_addr,
  220. .rc_interval = 400,
  221. .rc_key_map = vp7045_rc_keys,
  222. .rc_key_map_size = ARRAY_SIZE(vp7045_rc_keys),
  223. .rc_query = vp7045_rc_query,
  224. .num_device_descs = 2,
  225. .devices = {
  226. { .name = "Twinhan USB2.0 DVB-T receiver (TwinhanDTV Alpha/MagicBox II)",
  227. .cold_ids = { &vp7045_usb_table[0], NULL },
  228. .warm_ids = { &vp7045_usb_table[1], NULL },
  229. },
  230. { .name = "DigitalNow TinyUSB 2 DVB-t Receiver",
  231. .cold_ids = { &vp7045_usb_table[2], NULL },
  232. .warm_ids = { &vp7045_usb_table[3], NULL },
  233. },
  234. { NULL },
  235. }
  236. };
  237. /* usb specific object needed to register this driver with the usb subsystem */
  238. static struct usb_driver vp7045_usb_driver = {
  239. .name = "dvb_usb_vp7045",
  240. .probe = vp7045_usb_probe,
  241. .disconnect = dvb_usb_device_exit,
  242. .id_table = vp7045_usb_table,
  243. };
  244. /* module stuff */
  245. static int __init vp7045_usb_module_init(void)
  246. {
  247. int result;
  248. if ((result = usb_register(&vp7045_usb_driver))) {
  249. err("usb_register failed. (%d)",result);
  250. return result;
  251. }
  252. return 0;
  253. }
  254. static void __exit vp7045_usb_module_exit(void)
  255. {
  256. /* deregister this driver from the USB subsystem */
  257. usb_deregister(&vp7045_usb_driver);
  258. }
  259. module_init(vp7045_usb_module_init);
  260. module_exit(vp7045_usb_module_exit);
  261. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  262. MODULE_DESCRIPTION("Driver for Twinhan MagicBox/Alpha and DNTV tinyUSB2 DVB-T USB2.0");
  263. MODULE_VERSION("1.0");
  264. MODULE_LICENSE("GPL");