vp7045.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #define deb_info(args...) dprintk(dvb_usb_vp7045_debug,0x01,args)
  21. #define deb_xfer(args...) dprintk(dvb_usb_vp7045_debug,0x02,args)
  22. #define deb_rc(args...) dprintk(dvb_usb_vp7045_debug,0x04,args)
  23. int vp7045_usb_op(struct dvb_usb_device *d, u8 cmd, u8 *out, int outlen, u8 *in, int inlen, int msec)
  24. {
  25. int ret = 0;
  26. u8 inbuf[12] = { 0 }, outbuf[20] = { 0 };
  27. outbuf[0] = cmd;
  28. if (outlen > 19)
  29. outlen = 19;
  30. if (inlen > 11)
  31. inlen = 11;
  32. if (out != NULL && outlen > 0)
  33. memcpy(&outbuf[1], out, outlen);
  34. deb_xfer("out buffer: ");
  35. debug_dump(outbuf,outlen+1,deb_xfer);
  36. if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
  37. return ret;
  38. if (usb_control_msg(d->udev,
  39. usb_sndctrlpipe(d->udev,0),
  40. TH_COMMAND_OUT, USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0,
  41. outbuf, 20, 2000) != 20) {
  42. err("USB control message 'out' went wrong.");
  43. ret = -EIO;
  44. goto unlock;
  45. }
  46. msleep(msec);
  47. if (usb_control_msg(d->udev,
  48. usb_rcvctrlpipe(d->udev,0),
  49. TH_COMMAND_IN, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
  50. inbuf, 12, 2000) != 12) {
  51. err("USB control message 'in' went wrong.");
  52. ret = -EIO;
  53. goto unlock;
  54. }
  55. deb_xfer("in buffer: ");
  56. debug_dump(inbuf,12,deb_xfer);
  57. if (in != NULL && inlen > 0)
  58. memcpy(in,&inbuf[1],inlen);
  59. unlock:
  60. mutex_unlock(&d->usb_mutex);
  61. return ret;
  62. }
  63. u8 vp7045_read_reg(struct dvb_usb_device *d, u8 reg)
  64. {
  65. u8 obuf[2] = { 0 },v;
  66. obuf[1] = reg;
  67. vp7045_usb_op(d,TUNER_REG_READ,obuf,2,&v,1,30);
  68. return v;
  69. }
  70. static int vp7045_power_ctrl(struct dvb_usb_device *d, int onoff)
  71. {
  72. u8 v = onoff;
  73. return vp7045_usb_op(d,SET_TUNER_POWER,&v,1,NULL,0,150);
  74. }
  75. /* remote control stuff */
  76. /* The keymapping struct. Somehow this should be loaded to the driver, but
  77. * currently it is hardcoded. */
  78. static struct dvb_usb_rc_key vp7045_rc_keys[] = {
  79. { 0x00, 0x16, KEY_POWER },
  80. { 0x00, 0x10, KEY_MUTE },
  81. { 0x00, 0x03, KEY_1 },
  82. { 0x00, 0x01, KEY_2 },
  83. { 0x00, 0x06, KEY_3 },
  84. { 0x00, 0x09, KEY_4 },
  85. { 0x00, 0x1d, KEY_5 },
  86. { 0x00, 0x1f, KEY_6 },
  87. { 0x00, 0x0d, KEY_7 },
  88. { 0x00, 0x19, KEY_8 },
  89. { 0x00, 0x1b, KEY_9 },
  90. { 0x00, 0x15, KEY_0 },
  91. { 0x00, 0x05, KEY_CHANNELUP },
  92. { 0x00, 0x02, KEY_CHANNELDOWN },
  93. { 0x00, 0x1e, KEY_VOLUMEUP },
  94. { 0x00, 0x0a, KEY_VOLUMEDOWN },
  95. { 0x00, 0x11, KEY_RECORD },
  96. { 0x00, 0x17, KEY_FAVORITES }, /* Heart symbol - Channel list. */
  97. { 0x00, 0x14, KEY_PLAY },
  98. { 0x00, 0x1a, KEY_STOP },
  99. { 0x00, 0x40, KEY_REWIND },
  100. { 0x00, 0x12, KEY_FASTFORWARD },
  101. { 0x00, 0x0e, KEY_PREVIOUS }, /* Recall - Previous channel. */
  102. { 0x00, 0x4c, KEY_PAUSE },
  103. { 0x00, 0x4d, KEY_SCREEN }, /* Full screen mode. */
  104. { 0x00, 0x54, KEY_AUDIO }, /* MTS - Switch to secondary audio. */
  105. { 0x00, 0x0c, KEY_CANCEL }, /* Cancel */
  106. { 0x00, 0x1c, KEY_EPG }, /* EPG */
  107. { 0x00, 0x00, KEY_TAB }, /* Tab */
  108. { 0x00, 0x48, KEY_INFO }, /* Preview */
  109. { 0x00, 0x04, KEY_LIST }, /* RecordList */
  110. { 0x00, 0x0f, KEY_TEXT }, /* Teletext */
  111. { 0x00, 0x41, KEY_PREVIOUSSONG },
  112. { 0x00, 0x42, KEY_NEXTSONG },
  113. { 0x00, 0x4b, KEY_UP },
  114. { 0x00, 0x51, KEY_DOWN },
  115. { 0x00, 0x4e, KEY_LEFT },
  116. { 0x00, 0x52, KEY_RIGHT },
  117. { 0x00, 0x4f, KEY_ENTER },
  118. { 0x00, 0x13, KEY_CANCEL },
  119. { 0x00, 0x4a, KEY_CLEAR },
  120. { 0x00, 0x54, KEY_PRINT }, /* Capture */
  121. { 0x00, 0x43, KEY_SUBTITLE }, /* Subtitle/CC */
  122. { 0x00, 0x08, KEY_VIDEO }, /* A/V */
  123. { 0x00, 0x07, KEY_SLEEP }, /* Hibernate */
  124. { 0x00, 0x45, KEY_ZOOM }, /* Zoom+ */
  125. { 0x00, 0x18, KEY_RED},
  126. { 0x00, 0x53, KEY_GREEN},
  127. { 0x00, 0x5e, KEY_YELLOW},
  128. { 0x00, 0x5f, KEY_BLUE}
  129. };
  130. static int vp7045_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  131. {
  132. u8 key;
  133. int i;
  134. vp7045_usb_op(d,RC_VAL_READ,NULL,0,&key,1,20);
  135. deb_rc("remote query key: %x %d\n",key,key);
  136. if (key == 0x44) {
  137. *state = REMOTE_NO_KEY_PRESSED;
  138. return 0;
  139. }
  140. for (i = 0; i < ARRAY_SIZE(vp7045_rc_keys); i++)
  141. if (vp7045_rc_keys[i].data == key) {
  142. *state = REMOTE_KEY_PRESSED;
  143. *event = vp7045_rc_keys[i].event;
  144. break;
  145. }
  146. return 0;
  147. }
  148. static int vp7045_read_eeprom(struct dvb_usb_device *d,u8 *buf, int len, int offset)
  149. {
  150. int i = 0;
  151. u8 v,br[2];
  152. for (i=0; i < len; i++) {
  153. v = offset + i;
  154. vp7045_usb_op(d,GET_EE_VALUE,&v,1,br,2,5);
  155. buf[i] = br[1];
  156. }
  157. deb_info("VP7045 EEPROM read (offs: %d, len: %d) : ",offset, i);
  158. debug_dump(buf,i,deb_info);
  159. return 0;
  160. }
  161. static int vp7045_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
  162. {
  163. return vp7045_read_eeprom(d,mac, 6, MAC_0_ADDR);
  164. }
  165. static int vp7045_frontend_attach(struct dvb_usb_adapter *adap)
  166. {
  167. u8 buf[255] = { 0 };
  168. vp7045_usb_op(adap->dev,VENDOR_STRING_READ,NULL,0,buf,20,0);
  169. buf[10] = '\0';
  170. deb_info("firmware says: %s ",buf);
  171. vp7045_usb_op(adap->dev,PRODUCT_STRING_READ,NULL,0,buf,20,0);
  172. buf[10] = '\0';
  173. deb_info("%s ",buf);
  174. vp7045_usb_op(adap->dev,FW_VERSION_READ,NULL,0,buf,20,0);
  175. buf[10] = '\0';
  176. deb_info("v%s\n",buf);
  177. /* Dump the EEPROM */
  178. /* vp7045_read_eeprom(d,buf, 255, FX2_ID_ADDR); */
  179. adap->fe = vp7045_fe_attach(adap->dev);
  180. return 0;
  181. }
  182. static struct dvb_usb_device_properties vp7045_properties;
  183. static int vp7045_usb_probe(struct usb_interface *intf,
  184. const struct usb_device_id *id)
  185. {
  186. return dvb_usb_device_init(intf,&vp7045_properties,THIS_MODULE,NULL);
  187. }
  188. static struct usb_device_id vp7045_usb_table [] = {
  189. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7045_COLD) },
  190. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7045_WARM) },
  191. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_DNTV_TINYUSB2_COLD) },
  192. { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_DNTV_TINYUSB2_WARM) },
  193. { 0 },
  194. };
  195. MODULE_DEVICE_TABLE(usb, vp7045_usb_table);
  196. static struct dvb_usb_device_properties vp7045_properties = {
  197. .usb_ctrl = CYPRESS_FX2,
  198. .firmware = "dvb-usb-vp7045-01.fw",
  199. .num_adapters = 1,
  200. .adapter = {
  201. {
  202. .frontend_attach = vp7045_frontend_attach,
  203. /* parameter for the MPEG2-data transfer */
  204. .stream = {
  205. .type = USB_BULK,
  206. .count = 7,
  207. .endpoint = 0x02,
  208. .u = {
  209. .bulk = {
  210. .buffersize = 4096,
  211. }
  212. }
  213. },
  214. }
  215. },
  216. .power_ctrl = vp7045_power_ctrl,
  217. .read_mac_address = vp7045_read_mac_addr,
  218. .rc_interval = 400,
  219. .rc_key_map = vp7045_rc_keys,
  220. .rc_key_map_size = ARRAY_SIZE(vp7045_rc_keys),
  221. .rc_query = vp7045_rc_query,
  222. .num_device_descs = 2,
  223. .devices = {
  224. { .name = "Twinhan USB2.0 DVB-T receiver (TwinhanDTV Alpha/MagicBox II)",
  225. .cold_ids = { &vp7045_usb_table[0], NULL },
  226. .warm_ids = { &vp7045_usb_table[1], NULL },
  227. },
  228. { .name = "DigitalNow TinyUSB 2 DVB-t Receiver",
  229. .cold_ids = { &vp7045_usb_table[2], NULL },
  230. .warm_ids = { &vp7045_usb_table[3], NULL },
  231. },
  232. { NULL },
  233. }
  234. };
  235. /* usb specific object needed to register this driver with the usb subsystem */
  236. static struct usb_driver vp7045_usb_driver = {
  237. .name = "dvb_usb_vp7045",
  238. .probe = vp7045_usb_probe,
  239. .disconnect = dvb_usb_device_exit,
  240. .id_table = vp7045_usb_table,
  241. };
  242. /* module stuff */
  243. static int __init vp7045_usb_module_init(void)
  244. {
  245. int result;
  246. if ((result = usb_register(&vp7045_usb_driver))) {
  247. err("usb_register failed. (%d)",result);
  248. return result;
  249. }
  250. return 0;
  251. }
  252. static void __exit vp7045_usb_module_exit(void)
  253. {
  254. /* deregister this driver from the USB subsystem */
  255. usb_deregister(&vp7045_usb_driver);
  256. }
  257. module_init(vp7045_usb_module_init);
  258. module_exit(vp7045_usb_module_exit);
  259. MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
  260. MODULE_DESCRIPTION("Driver for Twinhan MagicBox/Alpha and DNTV tinyUSB2 DVB-T USB2.0");
  261. MODULE_VERSION("1.0");
  262. MODULE_LICENSE("GPL");