Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 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 | |
| 17 | /* debug */ |
Adrian Bunk | 53133af | 2007-11-05 14:07:06 -0300 | [diff] [blame] | 18 | static int dvb_usb_vp7045_debug; |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 19 | module_param_named(debug,dvb_usb_vp7045_debug, int, 0644); |
| 20 | MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS); |
Janne Grunau | 78e9200 | 2008-04-09 19:13:13 -0300 | [diff] [blame] | 21 | |
| 22 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 23 | |
Adrian Bunk | 53133af | 2007-11-05 14:07:06 -0300 | [diff] [blame] | 24 | #define deb_info(args...) dprintk(dvb_usb_vp7045_debug,0x01,args) |
| 25 | #define deb_xfer(args...) dprintk(dvb_usb_vp7045_debug,0x02,args) |
| 26 | #define deb_rc(args...) dprintk(dvb_usb_vp7045_debug,0x04,args) |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 27 | |
| 28 | int vp7045_usb_op(struct dvb_usb_device *d, u8 cmd, u8 *out, int outlen, u8 *in, int inlen, int msec) |
| 29 | { |
| 30 | int ret = 0; |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 31 | u8 *buf = d->priv; |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 32 | |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 33 | buf[0] = cmd; |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 34 | |
| 35 | if (outlen > 19) |
| 36 | outlen = 19; |
| 37 | |
| 38 | if (inlen > 11) |
| 39 | inlen = 11; |
| 40 | |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 41 | ret = mutex_lock_interruptible(&d->usb_mutex); |
| 42 | if (ret) |
| 43 | return ret; |
| 44 | |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 45 | if (out != NULL && outlen > 0) |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 46 | memcpy(&buf[1], out, outlen); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 47 | |
| 48 | deb_xfer("out buffer: "); |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 49 | debug_dump(buf, outlen+1, deb_xfer); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 50 | |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 51 | |
| 52 | if (usb_control_msg(d->udev, |
| 53 | usb_sndctrlpipe(d->udev,0), |
| 54 | TH_COMMAND_OUT, USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 55 | buf, 20, 2000) != 20) { |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 56 | err("USB control message 'out' went wrong."); |
| 57 | ret = -EIO; |
| 58 | goto unlock; |
| 59 | } |
| 60 | |
| 61 | msleep(msec); |
| 62 | |
| 63 | if (usb_control_msg(d->udev, |
| 64 | usb_rcvctrlpipe(d->udev,0), |
| 65 | TH_COMMAND_IN, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 66 | buf, 12, 2000) != 12) { |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 67 | err("USB control message 'in' went wrong."); |
| 68 | ret = -EIO; |
| 69 | goto unlock; |
| 70 | } |
| 71 | |
| 72 | deb_xfer("in buffer: "); |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 73 | debug_dump(buf, 12, deb_xfer); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 74 | |
| 75 | if (in != NULL && inlen > 0) |
Florian Mickler | ab22cbd | 2011-03-21 15:33:42 -0300 | [diff] [blame] | 76 | memcpy(in, &buf[1], inlen); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 77 | |
| 78 | unlock: |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 79 | mutex_unlock(&d->usb_mutex); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 80 | |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | u8 vp7045_read_reg(struct dvb_usb_device *d, u8 reg) |
| 85 | { |
| 86 | u8 obuf[2] = { 0 },v; |
| 87 | obuf[1] = reg; |
| 88 | |
| 89 | vp7045_usb_op(d,TUNER_REG_READ,obuf,2,&v,1,30); |
| 90 | |
| 91 | return v; |
| 92 | } |
| 93 | |
| 94 | static int vp7045_power_ctrl(struct dvb_usb_device *d, int onoff) |
| 95 | { |
| 96 | u8 v = onoff; |
| 97 | return vp7045_usb_op(d,SET_TUNER_POWER,&v,1,NULL,0,150); |
| 98 | } |
| 99 | |
| 100 | /* remote control stuff */ |
| 101 | |
| 102 | /* The keymapping struct. Somehow this should be loaded to the driver, but |
| 103 | * currently it is hardcoded. */ |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 104 | static struct rc_map_table rc_map_vp7045_table[] = { |
Mauro Carvalho Chehab | 2e36588 | 2009-08-29 15:19:31 -0300 | [diff] [blame] | 105 | { 0x0016, KEY_POWER }, |
| 106 | { 0x0010, KEY_MUTE }, |
| 107 | { 0x0003, KEY_1 }, |
| 108 | { 0x0001, KEY_2 }, |
| 109 | { 0x0006, KEY_3 }, |
| 110 | { 0x0009, KEY_4 }, |
| 111 | { 0x001d, KEY_5 }, |
| 112 | { 0x001f, KEY_6 }, |
| 113 | { 0x000d, KEY_7 }, |
| 114 | { 0x0019, KEY_8 }, |
| 115 | { 0x001b, KEY_9 }, |
| 116 | { 0x0015, KEY_0 }, |
| 117 | { 0x0005, KEY_CHANNELUP }, |
| 118 | { 0x0002, KEY_CHANNELDOWN }, |
| 119 | { 0x001e, KEY_VOLUMEUP }, |
| 120 | { 0x000a, KEY_VOLUMEDOWN }, |
| 121 | { 0x0011, KEY_RECORD }, |
| 122 | { 0x0017, KEY_FAVORITES }, /* Heart symbol - Channel list. */ |
| 123 | { 0x0014, KEY_PLAY }, |
| 124 | { 0x001a, KEY_STOP }, |
| 125 | { 0x0040, KEY_REWIND }, |
| 126 | { 0x0012, KEY_FASTFORWARD }, |
| 127 | { 0x000e, KEY_PREVIOUS }, /* Recall - Previous channel. */ |
| 128 | { 0x004c, KEY_PAUSE }, |
| 129 | { 0x004d, KEY_SCREEN }, /* Full screen mode. */ |
| 130 | { 0x0054, KEY_AUDIO }, /* MTS - Switch to secondary audio. */ |
| 131 | { 0x000c, KEY_CANCEL }, /* Cancel */ |
| 132 | { 0x001c, KEY_EPG }, /* EPG */ |
| 133 | { 0x0000, KEY_TAB }, /* Tab */ |
| 134 | { 0x0048, KEY_INFO }, /* Preview */ |
| 135 | { 0x0004, KEY_LIST }, /* RecordList */ |
| 136 | { 0x000f, KEY_TEXT }, /* Teletext */ |
| 137 | { 0x0041, KEY_PREVIOUSSONG }, |
| 138 | { 0x0042, KEY_NEXTSONG }, |
| 139 | { 0x004b, KEY_UP }, |
| 140 | { 0x0051, KEY_DOWN }, |
| 141 | { 0x004e, KEY_LEFT }, |
| 142 | { 0x0052, KEY_RIGHT }, |
| 143 | { 0x004f, KEY_ENTER }, |
| 144 | { 0x0013, KEY_CANCEL }, |
| 145 | { 0x004a, KEY_CLEAR }, |
| 146 | { 0x0054, KEY_PRINT }, /* Capture */ |
| 147 | { 0x0043, KEY_SUBTITLE }, /* Subtitle/CC */ |
| 148 | { 0x0008, KEY_VIDEO }, /* A/V */ |
| 149 | { 0x0007, KEY_SLEEP }, /* Hibernate */ |
| 150 | { 0x0045, KEY_ZOOM }, /* Zoom+ */ |
| 151 | { 0x0018, KEY_RED}, |
| 152 | { 0x0053, KEY_GREEN}, |
| 153 | { 0x005e, KEY_YELLOW}, |
| 154 | { 0x005f, KEY_BLUE} |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Patrick Boettcher | 8df3d46 | 2005-07-12 13:58:38 -0700 | [diff] [blame] | 157 | static int vp7045_rc_query(struct dvb_usb_device *d, u32 *event, int *state) |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 158 | { |
| 159 | u8 key; |
| 160 | int i; |
| 161 | vp7045_usb_op(d,RC_VAL_READ,NULL,0,&key,1,20); |
| 162 | |
| 163 | deb_rc("remote query key: %x %d\n",key,key); |
| 164 | |
| 165 | if (key == 0x44) { |
| 166 | *state = REMOTE_NO_KEY_PRESSED; |
| 167 | return 0; |
| 168 | } |
| 169 | |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 170 | for (i = 0; i < ARRAY_SIZE(rc_map_vp7045_table); i++) |
| 171 | if (rc5_data(&rc_map_vp7045_table[i]) == key) { |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 172 | *state = REMOTE_KEY_PRESSED; |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 173 | *event = rc_map_vp7045_table[i].keycode; |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int vp7045_read_eeprom(struct dvb_usb_device *d,u8 *buf, int len, int offset) |
| 180 | { |
| 181 | int i = 0; |
| 182 | u8 v,br[2]; |
| 183 | for (i=0; i < len; i++) { |
| 184 | v = offset + i; |
| 185 | vp7045_usb_op(d,GET_EE_VALUE,&v,1,br,2,5); |
| 186 | buf[i] = br[1]; |
| 187 | } |
| 188 | deb_info("VP7045 EEPROM read (offs: %d, len: %d) : ",offset, i); |
| 189 | debug_dump(buf,i,deb_info); |
| 190 | return 0; |
| 191 | } |
| 192 | |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 193 | static int vp7045_read_mac_addr(struct dvb_usb_device *d,u8 mac[6]) |
| 194 | { |
| 195 | return vp7045_read_eeprom(d,mac, 6, MAC_0_ADDR); |
| 196 | } |
| 197 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 198 | static int vp7045_frontend_attach(struct dvb_usb_adapter *adap) |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 199 | { |
| 200 | u8 buf[255] = { 0 }; |
| 201 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 202 | vp7045_usb_op(adap->dev,VENDOR_STRING_READ,NULL,0,buf,20,0); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 203 | buf[10] = '\0'; |
| 204 | deb_info("firmware says: %s ",buf); |
| 205 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 206 | vp7045_usb_op(adap->dev,PRODUCT_STRING_READ,NULL,0,buf,20,0); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 207 | buf[10] = '\0'; |
| 208 | deb_info("%s ",buf); |
| 209 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 210 | vp7045_usb_op(adap->dev,FW_VERSION_READ,NULL,0,buf,20,0); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 211 | buf[10] = '\0'; |
| 212 | deb_info("v%s\n",buf); |
| 213 | |
| 214 | /* Dump the EEPROM */ |
| 215 | /* vp7045_read_eeprom(d,buf, 255, FX2_ID_ADDR); */ |
| 216 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 217 | adap->fe = vp7045_fe_attach(adap->dev); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 222 | static struct dvb_usb_device_properties vp7045_properties; |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 223 | |
| 224 | static int vp7045_usb_probe(struct usb_interface *intf, |
| 225 | const struct usb_device_id *id) |
| 226 | { |
Florian Mickler | fc61ccd | 2011-08-10 07:05:20 -0300 | [diff] [blame] | 227 | return dvb_usb_device_init(intf, &vp7045_properties, |
| 228 | THIS_MODULE, NULL, adapter_nr); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static struct usb_device_id vp7045_usb_table [] = { |
| 232 | { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7045_COLD) }, |
| 233 | { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7045_WARM) }, |
| 234 | { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_DNTV_TINYUSB2_COLD) }, |
| 235 | { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_DNTV_TINYUSB2_WARM) }, |
| 236 | { 0 }, |
| 237 | }; |
| 238 | MODULE_DEVICE_TABLE(usb, vp7045_usb_table); |
| 239 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 240 | static struct dvb_usb_device_properties vp7045_properties = { |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 241 | .usb_ctrl = CYPRESS_FX2, |
| 242 | .firmware = "dvb-usb-vp7045-01.fw", |
Florian Mickler | fc61ccd | 2011-08-10 07:05:20 -0300 | [diff] [blame] | 243 | .size_of_priv = 20, |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 244 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 245 | .num_adapters = 1, |
| 246 | .adapter = { |
| 247 | { |
Patrick Boettcher | 01451e7 | 2006-10-13 11:34:46 -0300 | [diff] [blame] | 248 | .frontend_attach = vp7045_frontend_attach, |
| 249 | /* parameter for the MPEG2-data transfer */ |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 250 | .stream = { |
| 251 | .type = USB_BULK, |
Patrick Boettcher | 01451e7 | 2006-10-13 11:34:46 -0300 | [diff] [blame] | 252 | .count = 7, |
| 253 | .endpoint = 0x02, |
| 254 | .u = { |
| 255 | .bulk = { |
| 256 | .buffersize = 4096, |
| 257 | } |
| 258 | } |
| 259 | }, |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 260 | } |
| 261 | }, |
| 262 | .power_ctrl = vp7045_power_ctrl, |
| 263 | .read_mac_address = vp7045_read_mac_addr, |
| 264 | |
Mauro Carvalho Chehab | f72a27b | 2010-07-31 18:04:09 -0300 | [diff] [blame] | 265 | .rc.legacy = { |
| 266 | .rc_interval = 400, |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 267 | .rc_map_table = rc_map_vp7045_table, |
| 268 | .rc_map_size = ARRAY_SIZE(rc_map_vp7045_table), |
Mauro Carvalho Chehab | f72a27b | 2010-07-31 18:04:09 -0300 | [diff] [blame] | 269 | .rc_query = vp7045_rc_query, |
| 270 | }, |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 271 | |
| 272 | .num_device_descs = 2, |
| 273 | .devices = { |
| 274 | { .name = "Twinhan USB2.0 DVB-T receiver (TwinhanDTV Alpha/MagicBox II)", |
| 275 | .cold_ids = { &vp7045_usb_table[0], NULL }, |
| 276 | .warm_ids = { &vp7045_usb_table[1], NULL }, |
| 277 | }, |
| 278 | { .name = "DigitalNow TinyUSB 2 DVB-t Receiver", |
| 279 | .cold_ids = { &vp7045_usb_table[2], NULL }, |
| 280 | .warm_ids = { &vp7045_usb_table[3], NULL }, |
| 281 | }, |
Al Viro | dad08df | 2006-02-01 06:02:50 -0500 | [diff] [blame] | 282 | { NULL }, |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 283 | } |
| 284 | }; |
| 285 | |
| 286 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 287 | static struct usb_driver vp7045_usb_driver = { |
Patrick Boettcher | 63b5c1c | 2005-07-07 17:58:30 -0700 | [diff] [blame] | 288 | .name = "dvb_usb_vp7045", |
Patrick Boettcher | 3beab78 | 2005-09-09 13:02:50 -0700 | [diff] [blame] | 289 | .probe = vp7045_usb_probe, |
Florian Mickler | fc61ccd | 2011-08-10 07:05:20 -0300 | [diff] [blame] | 290 | .disconnect = dvb_usb_device_exit, |
Patrick Boettcher | 3beab78 | 2005-09-09 13:02:50 -0700 | [diff] [blame] | 291 | .id_table = vp7045_usb_table, |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | /* module stuff */ |
| 295 | static int __init vp7045_usb_module_init(void) |
| 296 | { |
| 297 | int result; |
| 298 | if ((result = usb_register(&vp7045_usb_driver))) { |
| 299 | err("usb_register failed. (%d)",result); |
| 300 | return result; |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static void __exit vp7045_usb_module_exit(void) |
| 307 | { |
| 308 | /* deregister this driver from the USB subsystem */ |
| 309 | usb_deregister(&vp7045_usb_driver); |
| 310 | } |
| 311 | |
| 312 | module_init(vp7045_usb_module_init); |
| 313 | module_exit(vp7045_usb_module_exit); |
| 314 | |
| 315 | MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>"); |
| 316 | MODULE_DESCRIPTION("Driver for Twinhan MagicBox/Alpha and DNTV tinyUSB2 DVB-T USB2.0"); |
| 317 | MODULE_VERSION("1.0"); |
| 318 | MODULE_LICENSE("GPL"); |