blob: 35fe778993c199e1e69114670edfcdf52ce9d4cc [file] [log] [blame]
Patrick Boettcher3706a4d2005-09-09 13:02:41 -07001/* 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"
Florian Mickler1c6410f2011-03-21 07:19:08 -030018#include <linux/mutex.h>
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070019
20/* debug */
21int dvb_usb_vp702x_debug;
22module_param_named(debug,dvb_usb_vp702x_debug, int, 0644);
23MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
24
Janne Grunau78e92002008-04-09 19:13:13 -030025DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
26
Florian Mickler36f773e2011-03-21 07:19:07 -030027struct vp702x_adapter_state {
Patrick Boettcher0540c492006-09-19 12:51:43 -030028 int pid_filter_count;
29 int pid_filter_can_bypass;
30 u8 pid_filter_state;
31};
32
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070033/* check for mutex FIXME */
34int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
35{
Florian Micklerd30615d2011-03-21 07:19:06 -030036 int ret;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070037
Florian Micklerd30615d2011-03-21 07:19:06 -030038 ret = usb_control_msg(d->udev,
39 usb_rcvctrlpipe(d->udev, 0),
40 req,
41 USB_TYPE_VENDOR | USB_DIR_IN,
42 value, index, b, blen,
43 2000);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070044
Patrick Boettcher0540c492006-09-19 12:51:43 -030045 if (ret < 0) {
46 warn("usb in operation failed. (%d)", ret);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070047 ret = -EIO;
48 } else
49 ret = 0;
50
Patrick Boettcher0540c492006-09-19 12:51:43 -030051
52 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070053 debug_dump(b,blen,deb_xfer);
54
55 return ret;
56}
57
Adrian Bunk5e7361a2007-11-05 14:06:47 -030058static int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
Adrian Bunk703cb2c2006-01-23 17:11:09 -020059 u16 index, u8 *b, int blen)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070060{
Patrick Boettcher0540c492006-09-19 12:51:43 -030061 int ret;
62 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070063 debug_dump(b,blen,deb_xfer);
64
Patrick Boettcher0540c492006-09-19 12:51:43 -030065 if ((ret = usb_control_msg(d->udev,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070066 usb_sndctrlpipe(d->udev,0),
67 req,
68 USB_TYPE_VENDOR | USB_DIR_OUT,
69 value,index,b,blen,
Patrick Boettcher0540c492006-09-19 12:51:43 -030070 2000)) != blen) {
71 warn("usb out operation failed. (%d)",ret);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070072 return -EIO;
73 } else
74 return 0;
75}
76
77int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec)
78{
79 int ret;
80
Ingo Molnar3593cab2006-02-07 06:49:14 -020081 if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070082 return ret;
83
Patrick Boettcher0540c492006-09-19 12:51:43 -030084 ret = vp702x_usb_out_op(d,REQUEST_OUT,0,0,o,olen);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070085 msleep(msec);
86 ret = vp702x_usb_in_op(d,REQUEST_IN,0,0,i,ilen);
87
Ingo Molnar3593cab2006-02-07 06:49:14 -020088 mutex_unlock(&d->usb_mutex);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070089
90 return ret;
91}
92
Adrian Bunk703cb2c2006-01-23 17:11:09 -020093static int vp702x_usb_inout_cmd(struct dvb_usb_device *d, u8 cmd, u8 *o,
94 int olen, u8 *i, int ilen, int msec)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070095{
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070096 int ret = 0;
Florian Mickler57873c72011-03-21 07:19:10 -030097 u8 *buf;
98 int buflen = max(olen + 2, ilen + 1);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070099
Florian Mickler57873c72011-03-21 07:19:10 -0300100 buf = kmalloc(buflen, GFP_KERNEL);
101 if (!buf)
102 return -ENOMEM;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700103
Florian Mickler57873c72011-03-21 07:19:10 -0300104 buf[0] = 0x00;
105 buf[1] = cmd;
106 memcpy(&buf[2], o, olen);
107
108 ret = vp702x_usb_inout_op(d, buf, olen+2, buf, ilen+1, msec);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700109
110 if (ret == 0)
Florian Mickler57873c72011-03-21 07:19:10 -0300111 memcpy(i, &buf[1], ilen);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700112
Florian Mickler57873c72011-03-21 07:19:10 -0300113 kfree(buf);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700114 return ret;
115}
116
Patrick Boettcher0540c492006-09-19 12:51:43 -0300117static int vp702x_set_pld_mode(struct dvb_usb_adapter *adap, u8 bypass)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700118{
Florian Mickler57873c72011-03-21 07:19:10 -0300119 int ret;
120 u8 *buf = kzalloc(16, GFP_KERNEL);
121 if (!buf)
122 return -ENOMEM;
123
124 ret = vp702x_usb_in_op(adap->dev, 0xe0, (bypass << 8) | 0x0e,
125 0, buf, 16);
126 kfree(buf);
127 return ret;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700128}
129
Patrick Boettcher0540c492006-09-19 12:51:43 -0300130static int vp702x_set_pld_state(struct dvb_usb_adapter *adap, u8 state)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700131{
Florian Mickler57873c72011-03-21 07:19:10 -0300132 int ret;
133 u8 *buf = kzalloc(16, GFP_KERNEL);
134 if (!buf)
135 return -ENOMEM;
136
137 ret = vp702x_usb_in_op(adap->dev, 0xe0, (state << 8) | 0x0f,
138 0, buf, 16);
139 kfree(buf);
140 return ret;
Patrick Boettcher0540c492006-09-19 12:51:43 -0300141}
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700142
Patrick Boettcher0540c492006-09-19 12:51:43 -0300143static int vp702x_set_pid(struct dvb_usb_adapter *adap, u16 pid, u8 id, int onoff)
144{
Florian Mickler36f773e2011-03-21 07:19:07 -0300145 struct vp702x_adapter_state *st = adap->priv;
Florian Mickler57873c72011-03-21 07:19:10 -0300146 u8 *buf;
147
148 buf = kzalloc(16, GFP_KERNEL);
149 if (!buf)
150 return -ENOMEM;
Patrick Boettcher0540c492006-09-19 12:51:43 -0300151
152 if (onoff)
153 st->pid_filter_state |= (1 << id);
154 else {
155 st->pid_filter_state &= ~(1 << id);
156 pid = 0xffff;
157 }
158
159 id = 0x10 + id*2;
160
161 vp702x_set_pld_state(adap, st->pid_filter_state);
162 vp702x_usb_in_op(adap->dev, 0xe0, (((pid >> 8) & 0xff) << 8) | (id), 0, buf, 16);
163 vp702x_usb_in_op(adap->dev, 0xe0, (((pid ) & 0xff) << 8) | (id+1), 0, buf, 16);
Florian Mickler57873c72011-03-21 07:19:10 -0300164
165 kfree(buf);
Patrick Boettcher0540c492006-09-19 12:51:43 -0300166 return 0;
167}
168
169
170static int vp702x_init_pid_filter(struct dvb_usb_adapter *adap)
171{
Florian Mickler36f773e2011-03-21 07:19:07 -0300172 struct vp702x_adapter_state *st = adap->priv;
Patrick Boettcher0540c492006-09-19 12:51:43 -0300173 int i;
Florian Mickler57873c72011-03-21 07:19:10 -0300174 u8 *b;
175
176 b = kzalloc(10, GFP_KERNEL);
177 if (!b)
178 return -ENOMEM;
Patrick Boettcher0540c492006-09-19 12:51:43 -0300179
180 st->pid_filter_count = 8;
181 st->pid_filter_can_bypass = 1;
182 st->pid_filter_state = 0x00;
183
Florian Mickler57873c72011-03-21 07:19:10 -0300184 vp702x_set_pld_mode(adap, 1); /* bypass */
Patrick Boettcher0540c492006-09-19 12:51:43 -0300185
186 for (i = 0; i < st->pid_filter_count; i++)
187 vp702x_set_pid(adap, 0xffff, i, 1);
188
189 vp702x_usb_in_op(adap->dev, 0xb5, 3, 0, b, 10);
190 vp702x_usb_in_op(adap->dev, 0xb5, 0, 0, b, 10);
191 vp702x_usb_in_op(adap->dev, 0xb5, 1, 0, b, 10);
192
193 //vp702x_set_pld_mode(d, 0); // filter
Florian Mickler57873c72011-03-21 07:19:10 -0300194 kfree(b);
Patrick Boettcher0540c492006-09-19 12:51:43 -0300195 return 0;
196}
197
198static int vp702x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
199{
200 return 0;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700201}
202
203/* keys for the enclosed remote control */
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300204static struct rc_map_table rc_map_vp702x_table[] = {
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300205 { 0x0001, KEY_1 },
206 { 0x0002, KEY_2 },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700207};
208
209/* remote control stuff (does not work with my box) */
210static int vp702x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
211{
Florian Mickler57873c72011-03-21 07:19:10 -0300212 u8 *key;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700213 int i;
214
215/* remove the following return to enabled remote querying */
216 return 0;
217
Florian Mickler57873c72011-03-21 07:19:10 -0300218 key = kmalloc(10, GFP_KERNEL);
219 if (!key)
220 return -ENOMEM;
221
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700222 vp702x_usb_in_op(d,READ_REMOTE_REQ,0,0,key,10);
223
224 deb_rc("remote query key: %x %d\n",key[1],key[1]);
225
226 if (key[1] == 0x44) {
227 *state = REMOTE_NO_KEY_PRESSED;
Florian Mickler57873c72011-03-21 07:19:10 -0300228 kfree(key);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700229 return 0;
230 }
231
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300232 for (i = 0; i < ARRAY_SIZE(rc_map_vp702x_table); i++)
233 if (rc5_custom(&rc_map_vp702x_table[i]) == key[1]) {
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700234 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300235 *event = rc_map_vp702x_table[i].keycode;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700236 break;
237 }
Florian Mickler57873c72011-03-21 07:19:10 -0300238 kfree(key);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700239 return 0;
240}
241
Patrick Boettcher0540c492006-09-19 12:51:43 -0300242
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700243static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
244{
Florian Mickler57873c72011-03-21 07:19:10 -0300245 u8 i, *buf;
246
247 buf = kmalloc(6, GFP_KERNEL);
248 if (!buf)
249 return -ENOMEM;
250
Patrick Boettcher0540c492006-09-19 12:51:43 -0300251 for (i = 6; i < 12; i++)
Florian Mickler57873c72011-03-21 07:19:10 -0300252 vp702x_usb_in_op(d, READ_EEPROM_REQ, i, 1, &buf[i - 6], 1);
253
254 memcpy(mac, buf, 6);
255 kfree(buf);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700256 return 0;
257}
258
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300259static int vp702x_frontend_attach(struct dvb_usb_adapter *adap)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700260{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300261 u8 buf[10] = { 0 };
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700262
Patrick Boettcher0540c492006-09-19 12:51:43 -0300263 vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
264
Florian Micklerd30615d2011-03-21 07:19:06 -0300265 if (vp702x_usb_inout_cmd(adap->dev, GET_SYSTEM_STRING, NULL, 0,
266 buf, 10, 10))
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700267 return -EIO;
268
Patrick Boettcher0540c492006-09-19 12:51:43 -0300269 buf[9] = '\0';
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700270 info("system string: %s",&buf[1]);
271
Patrick Boettcher0540c492006-09-19 12:51:43 -0300272 vp702x_init_pid_filter(adap);
273
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300274 adap->fe = vp702x_fe_attach(adap->dev);
Patrick Boettcher0540c492006-09-19 12:51:43 -0300275 vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
276
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700277 return 0;
278}
279
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300280static struct dvb_usb_device_properties vp702x_properties;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700281
282static int vp702x_usb_probe(struct usb_interface *intf,
283 const struct usb_device_id *id)
284{
Florian Mickler1c6410f2011-03-21 07:19:08 -0300285 struct dvb_usb_device *d;
286 struct vp702x_device_state *st;
287 int ret;
288
289 ret = dvb_usb_device_init(intf, &vp702x_properties,
290 THIS_MODULE, &d, adapter_nr);
291 if (ret)
292 goto out;
293
294 st = d->priv;
295 st->buf_len = 16;
296 st->buf = kmalloc(st->buf_len, GFP_KERNEL);
297 if (!st->buf) {
298 ret = -ENOMEM;
299 dvb_usb_device_exit(intf);
300 goto out;
301 }
302 mutex_init(&st->buf_mutex);
303
304out:
305 return ret;
306
307}
308
309static void vp702x_usb_disconnect(struct usb_interface *intf)
310{
311 struct dvb_usb_device *d = usb_get_intfdata(intf);
312 struct vp702x_device_state *st = d->priv;
313 mutex_lock(&st->buf_mutex);
314 kfree(st->buf);
315 mutex_unlock(&st->buf_mutex);
316 dvb_usb_device_exit(intf);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700317}
318
319static struct usb_device_id vp702x_usb_table [] = {
320 { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_COLD) },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300321// { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
322// { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700323 { 0 },
324};
325MODULE_DEVICE_TABLE(usb, vp702x_usb_table);
326
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300327static struct dvb_usb_device_properties vp702x_properties = {
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700328 .usb_ctrl = CYPRESS_FX2,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300329 .firmware = "dvb-usb-vp702x-02.fw",
330 .no_reconnect = 1,
331
332 .size_of_priv = sizeof(struct vp702x_device_state),
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700333
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300334 .num_adapters = 1,
335 .adapter = {
336 {
Patrick Boettcher0540c492006-09-19 12:51:43 -0300337 .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS,
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300338
Patrick Boettcher0540c492006-09-19 12:51:43 -0300339 .streaming_ctrl = vp702x_streaming_ctrl,
Patrick Boettcher01451e72006-10-13 11:34:46 -0300340 .frontend_attach = vp702x_frontend_attach,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300341
Patrick Boettcher01451e72006-10-13 11:34:46 -0300342 /* parameter for the MPEG2-data transfer */
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300343 .stream = {
344 .type = USB_BULK,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300345 .count = 10,
Patrick Boettcher01451e72006-10-13 11:34:46 -0300346 .endpoint = 0x02,
347 .u = {
348 .bulk = {
349 .buffersize = 4096,
350 }
351 }
352 },
Florian Mickler36f773e2011-03-21 07:19:07 -0300353 .size_of_priv = sizeof(struct vp702x_adapter_state),
Patrick Boettcher0540c492006-09-19 12:51:43 -0300354 }
Patrick Boettcher01451e72006-10-13 11:34:46 -0300355 },
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300356 .read_mac_address = vp702x_read_mac_addr,
357
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300358 .rc.legacy = {
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300359 .rc_map_table = rc_map_vp702x_table,
360 .rc_map_size = ARRAY_SIZE(rc_map_vp702x_table),
Mauro Carvalho Chehabf72a27b2010-07-31 18:04:09 -0300361 .rc_interval = 400,
362 .rc_query = vp702x_rc_query,
363 },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700364
Patrick Boettcher0540c492006-09-19 12:51:43 -0300365 .num_device_descs = 1,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700366 .devices = {
367 { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
368 .cold_ids = { &vp702x_usb_table[0], NULL },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300369 .warm_ids = { NULL },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700370 },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300371/* { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700372 .cold_ids = { &vp702x_usb_table[2], NULL },
373 .warm_ids = { &vp702x_usb_table[3], NULL },
374 },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300375*/ { NULL },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700376 }
377};
378
379/* usb specific object needed to register this driver with the usb subsystem */
380static struct usb_driver vp702x_usb_driver = {
Patrick Boettcher0540c492006-09-19 12:51:43 -0300381 .name = "dvb_usb_vp702x",
Florian Micklerd30615d2011-03-21 07:19:06 -0300382 .probe = vp702x_usb_probe,
Florian Mickler1c6410f2011-03-21 07:19:08 -0300383 .disconnect = vp702x_usb_disconnect,
Florian Micklerd30615d2011-03-21 07:19:06 -0300384 .id_table = vp702x_usb_table,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700385};
386
387/* module stuff */
388static int __init vp702x_usb_module_init(void)
389{
390 int result;
391 if ((result = usb_register(&vp702x_usb_driver))) {
392 err("usb_register failed. (%d)",result);
393 return result;
394 }
395
396 return 0;
397}
398
399static void __exit vp702x_usb_module_exit(void)
400{
401 /* deregister this driver from the USB subsystem */
402 usb_deregister(&vp702x_usb_driver);
403}
404
405module_init(vp702x_usb_module_init);
406module_exit(vp702x_usb_module_exit);
407
408MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
409MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
Patrick Boettcher0540c492006-09-19 12:51:43 -0300410MODULE_VERSION("1.0");
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700411MODULE_LICENSE("GPL");