blob: 4d332451653b1e451513b9e6b7342b22830e8fd7 [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"
18
19/* debug */
20int dvb_usb_vp702x_debug;
21module_param_named(debug,dvb_usb_vp702x_debug, int, 0644);
22MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS);
23
Janne Grunau78e92002008-04-09 19:13:13 -030024DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
25
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070026struct vp702x_state {
Patrick Boettcher0540c492006-09-19 12:51:43 -030027 int pid_filter_count;
28 int pid_filter_can_bypass;
29 u8 pid_filter_state;
30};
31
32struct vp702x_device_state {
33 u8 power_state;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070034};
35
36/* check for mutex FIXME */
37int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
38{
Patrick Boettcher0540c492006-09-19 12:51:43 -030039 int ret = -1;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070040
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070041 ret = usb_control_msg(d->udev,
42 usb_rcvctrlpipe(d->udev,0),
43 req,
44 USB_TYPE_VENDOR | USB_DIR_IN,
45 value,index,b,blen,
46 2000);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070047
Patrick Boettcher0540c492006-09-19 12:51:43 -030048 if (ret < 0) {
49 warn("usb in operation failed. (%d)", ret);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070050 ret = -EIO;
51 } else
52 ret = 0;
53
Patrick Boettcher0540c492006-09-19 12:51:43 -030054
55 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070056 debug_dump(b,blen,deb_xfer);
57
58 return ret;
59}
60
Adrian Bunk5e7361a2007-11-05 14:06:47 -030061static int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
Adrian Bunk703cb2c2006-01-23 17:11:09 -020062 u16 index, u8 *b, int blen)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070063{
Patrick Boettcher0540c492006-09-19 12:51:43 -030064 int ret;
65 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070066 debug_dump(b,blen,deb_xfer);
67
Patrick Boettcher0540c492006-09-19 12:51:43 -030068 if ((ret = usb_control_msg(d->udev,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070069 usb_sndctrlpipe(d->udev,0),
70 req,
71 USB_TYPE_VENDOR | USB_DIR_OUT,
72 value,index,b,blen,
Patrick Boettcher0540c492006-09-19 12:51:43 -030073 2000)) != blen) {
74 warn("usb out operation failed. (%d)",ret);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070075 return -EIO;
76 } else
77 return 0;
78}
79
80int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec)
81{
82 int ret;
83
Ingo Molnar3593cab2006-02-07 06:49:14 -020084 if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070085 return ret;
86
Patrick Boettcher0540c492006-09-19 12:51:43 -030087 ret = vp702x_usb_out_op(d,REQUEST_OUT,0,0,o,olen);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070088 msleep(msec);
89 ret = vp702x_usb_in_op(d,REQUEST_IN,0,0,i,ilen);
90
Ingo Molnar3593cab2006-02-07 06:49:14 -020091 mutex_unlock(&d->usb_mutex);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070092
93 return ret;
94}
95
Adrian Bunk703cb2c2006-01-23 17:11:09 -020096static int vp702x_usb_inout_cmd(struct dvb_usb_device *d, u8 cmd, u8 *o,
97 int olen, u8 *i, int ilen, int msec)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070098{
99 u8 bout[olen+2];
100 u8 bin[ilen+1];
101 int ret = 0;
102
103 bout[0] = 0x00;
104 bout[1] = cmd;
105 memcpy(&bout[2],o,olen);
106
107 ret = vp702x_usb_inout_op(d, bout, olen+2, bin, ilen+1,msec);
108
109 if (ret == 0)
110 memcpy(i,&bin[1],ilen);
111
112 return ret;
113}
114
Patrick Boettcher0540c492006-09-19 12:51:43 -0300115static int vp702x_set_pld_mode(struct dvb_usb_adapter *adap, u8 bypass)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700116{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300117 u8 buf[16] = { 0 };
118 return vp702x_usb_in_op(adap->dev, 0xe0, (bypass << 8) | 0x0e, 0, buf, 16);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700119}
120
Patrick Boettcher0540c492006-09-19 12:51:43 -0300121static int vp702x_set_pld_state(struct dvb_usb_adapter *adap, u8 state)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700122{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300123 u8 buf[16] = { 0 };
124 return vp702x_usb_in_op(adap->dev, 0xe0, (state << 8) | 0x0f, 0, buf, 16);
125}
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700126
Patrick Boettcher0540c492006-09-19 12:51:43 -0300127static int vp702x_set_pid(struct dvb_usb_adapter *adap, u16 pid, u8 id, int onoff)
128{
129 struct vp702x_state *st = adap->priv;
130 u8 buf[16] = { 0 };
131
132 if (onoff)
133 st->pid_filter_state |= (1 << id);
134 else {
135 st->pid_filter_state &= ~(1 << id);
136 pid = 0xffff;
137 }
138
139 id = 0x10 + id*2;
140
141 vp702x_set_pld_state(adap, st->pid_filter_state);
142 vp702x_usb_in_op(adap->dev, 0xe0, (((pid >> 8) & 0xff) << 8) | (id), 0, buf, 16);
143 vp702x_usb_in_op(adap->dev, 0xe0, (((pid ) & 0xff) << 8) | (id+1), 0, buf, 16);
144 return 0;
145}
146
147
148static int vp702x_init_pid_filter(struct dvb_usb_adapter *adap)
149{
150 struct vp702x_state *st = adap->priv;
151 int i;
152 u8 b[10] = { 0 };
153
154 st->pid_filter_count = 8;
155 st->pid_filter_can_bypass = 1;
156 st->pid_filter_state = 0x00;
157
158 vp702x_set_pld_mode(adap, 1); // bypass
159
160 for (i = 0; i < st->pid_filter_count; i++)
161 vp702x_set_pid(adap, 0xffff, i, 1);
162
163 vp702x_usb_in_op(adap->dev, 0xb5, 3, 0, b, 10);
164 vp702x_usb_in_op(adap->dev, 0xb5, 0, 0, b, 10);
165 vp702x_usb_in_op(adap->dev, 0xb5, 1, 0, b, 10);
166
167 //vp702x_set_pld_mode(d, 0); // filter
168 return 0;
169}
170
171static int vp702x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
172{
173 return 0;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700174}
175
176/* keys for the enclosed remote control */
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300177static struct dvb_usb_rc_key ir_codes_vp702x_table[] = {
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300178 { 0x0001, KEY_1 },
179 { 0x0002, KEY_2 },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700180};
181
182/* remote control stuff (does not work with my box) */
183static int vp702x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
184{
185 u8 key[10];
186 int i;
187
188/* remove the following return to enabled remote querying */
189 return 0;
190
191 vp702x_usb_in_op(d,READ_REMOTE_REQ,0,0,key,10);
192
193 deb_rc("remote query key: %x %d\n",key[1],key[1]);
194
195 if (key[1] == 0x44) {
196 *state = REMOTE_NO_KEY_PRESSED;
197 return 0;
198 }
199
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300200 for (i = 0; i < ARRAY_SIZE(ir_codes_vp702x_table); i++)
201 if (rc5_custom(&ir_codes_vp702x_table[i]) == key[1]) {
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700202 *state = REMOTE_KEY_PRESSED;
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300203 *event = ir_codes_vp702x_table[i].event;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700204 break;
205 }
206 return 0;
207}
208
Patrick Boettcher0540c492006-09-19 12:51:43 -0300209
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700210static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
211{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300212 u8 i;
213 for (i = 6; i < 12; i++)
214 vp702x_usb_in_op(d, READ_EEPROM_REQ, i, 1, &mac[i - 6], 1);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700215 return 0;
216}
217
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300218static int vp702x_frontend_attach(struct dvb_usb_adapter *adap)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700219{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300220 u8 buf[10] = { 0 };
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700221
Patrick Boettcher0540c492006-09-19 12:51:43 -0300222 vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
223
224 if (vp702x_usb_inout_cmd(adap->dev, GET_SYSTEM_STRING, NULL, 0, buf, 10, 10))
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700225 return -EIO;
226
Patrick Boettcher0540c492006-09-19 12:51:43 -0300227 buf[9] = '\0';
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700228 info("system string: %s",&buf[1]);
229
Patrick Boettcher0540c492006-09-19 12:51:43 -0300230 vp702x_init_pid_filter(adap);
231
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300232 adap->fe = vp702x_fe_attach(adap->dev);
Patrick Boettcher0540c492006-09-19 12:51:43 -0300233 vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
234
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700235 return 0;
236}
237
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300238static struct dvb_usb_device_properties vp702x_properties;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700239
240static int vp702x_usb_probe(struct usb_interface *intf,
241 const struct usb_device_id *id)
242{
Janne Grunau78e92002008-04-09 19:13:13 -0300243 return dvb_usb_device_init(intf, &vp702x_properties,
244 THIS_MODULE, NULL, adapter_nr);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700245}
246
247static struct usb_device_id vp702x_usb_table [] = {
248 { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_COLD) },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300249// { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
250// { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700251 { 0 },
252};
253MODULE_DEVICE_TABLE(usb, vp702x_usb_table);
254
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300255static struct dvb_usb_device_properties vp702x_properties = {
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700256 .usb_ctrl = CYPRESS_FX2,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300257 .firmware = "dvb-usb-vp702x-02.fw",
258 .no_reconnect = 1,
259
260 .size_of_priv = sizeof(struct vp702x_device_state),
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700261
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300262 .num_adapters = 1,
263 .adapter = {
264 {
Patrick Boettcher0540c492006-09-19 12:51:43 -0300265 .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS,
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300266
Patrick Boettcher0540c492006-09-19 12:51:43 -0300267 .streaming_ctrl = vp702x_streaming_ctrl,
Patrick Boettcher01451e72006-10-13 11:34:46 -0300268 .frontend_attach = vp702x_frontend_attach,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300269
Patrick Boettcher01451e72006-10-13 11:34:46 -0300270 /* parameter for the MPEG2-data transfer */
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300271 .stream = {
272 .type = USB_BULK,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300273 .count = 10,
Patrick Boettcher01451e72006-10-13 11:34:46 -0300274 .endpoint = 0x02,
275 .u = {
276 .bulk = {
277 .buffersize = 4096,
278 }
279 }
280 },
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300281 .size_of_priv = sizeof(struct vp702x_state),
Patrick Boettcher0540c492006-09-19 12:51:43 -0300282 }
Patrick Boettcher01451e72006-10-13 11:34:46 -0300283 },
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300284 .read_mac_address = vp702x_read_mac_addr,
285
Mauro Carvalho Chehabe27e9712010-04-01 21:35:32 -0300286 .rc_key_map = ir_codes_vp702x_table,
287 .rc_key_map_size = ARRAY_SIZE(ir_codes_vp702x_table),
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300288 .rc_interval = 400,
289 .rc_query = vp702x_rc_query,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700290
Patrick Boettcher0540c492006-09-19 12:51:43 -0300291 .num_device_descs = 1,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700292 .devices = {
293 { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
294 .cold_ids = { &vp702x_usb_table[0], NULL },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300295 .warm_ids = { NULL },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700296 },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300297/* { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700298 .cold_ids = { &vp702x_usb_table[2], NULL },
299 .warm_ids = { &vp702x_usb_table[3], NULL },
300 },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300301*/ { NULL },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700302 }
303};
304
305/* usb specific object needed to register this driver with the usb subsystem */
306static struct usb_driver vp702x_usb_driver = {
Patrick Boettcher0540c492006-09-19 12:51:43 -0300307 .name = "dvb_usb_vp702x",
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700308 .probe = vp702x_usb_probe,
309 .disconnect = dvb_usb_device_exit,
310 .id_table = vp702x_usb_table,
311};
312
313/* module stuff */
314static int __init vp702x_usb_module_init(void)
315{
316 int result;
317 if ((result = usb_register(&vp702x_usb_driver))) {
318 err("usb_register failed. (%d)",result);
319 return result;
320 }
321
322 return 0;
323}
324
325static void __exit vp702x_usb_module_exit(void)
326{
327 /* deregister this driver from the USB subsystem */
328 usb_deregister(&vp702x_usb_driver);
329}
330
331module_init(vp702x_usb_module_init);
332module_exit(vp702x_usb_module_exit);
333
334MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
335MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
Patrick Boettcher0540c492006-09-19 12:51:43 -0300336MODULE_VERSION("1.0");
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700337MODULE_LICENSE("GPL");