blob: 02bd61aaac666b3c6d1b5156ccc1d597dbda2f0b [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
24struct vp702x_state {
Patrick Boettcher0540c492006-09-19 12:51:43 -030025 int pid_filter_count;
26 int pid_filter_can_bypass;
27 u8 pid_filter_state;
28};
29
30struct vp702x_device_state {
31 u8 power_state;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070032};
33
34/* check for mutex FIXME */
35int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
36{
Patrick Boettcher0540c492006-09-19 12:51:43 -030037 int ret = -1;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070038
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070039 ret = usb_control_msg(d->udev,
40 usb_rcvctrlpipe(d->udev,0),
41 req,
42 USB_TYPE_VENDOR | USB_DIR_IN,
43 value,index,b,blen,
44 2000);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070045
Patrick Boettcher0540c492006-09-19 12:51:43 -030046 if (ret < 0) {
47 warn("usb in operation failed. (%d)", ret);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070048 ret = -EIO;
49 } else
50 ret = 0;
51
Patrick Boettcher0540c492006-09-19 12:51:43 -030052
53 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070054 debug_dump(b,blen,deb_xfer);
55
56 return ret;
57}
58
Patrick Boettcher0540c492006-09-19 12:51:43 -030059int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
Adrian Bunk703cb2c2006-01-23 17:11:09 -020060 u16 index, u8 *b, int blen)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070061{
Patrick Boettcher0540c492006-09-19 12:51:43 -030062 int ret;
63 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070064 debug_dump(b,blen,deb_xfer);
65
Patrick Boettcher0540c492006-09-19 12:51:43 -030066 if ((ret = usb_control_msg(d->udev,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070067 usb_sndctrlpipe(d->udev,0),
68 req,
69 USB_TYPE_VENDOR | USB_DIR_OUT,
70 value,index,b,blen,
Patrick Boettcher0540c492006-09-19 12:51:43 -030071 2000)) != blen) {
72 warn("usb out operation failed. (%d)",ret);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070073 return -EIO;
74 } else
75 return 0;
76}
77
78int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec)
79{
80 int ret;
81
Ingo Molnar3593cab2006-02-07 06:49:14 -020082 if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070083 return ret;
84
Patrick Boettcher0540c492006-09-19 12:51:43 -030085 ret = vp702x_usb_out_op(d,REQUEST_OUT,0,0,o,olen);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070086 msleep(msec);
87 ret = vp702x_usb_in_op(d,REQUEST_IN,0,0,i,ilen);
88
Ingo Molnar3593cab2006-02-07 06:49:14 -020089 mutex_unlock(&d->usb_mutex);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070090
91 return ret;
92}
93
Adrian Bunk703cb2c2006-01-23 17:11:09 -020094static int vp702x_usb_inout_cmd(struct dvb_usb_device *d, u8 cmd, u8 *o,
95 int olen, u8 *i, int ilen, int msec)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070096{
97 u8 bout[olen+2];
98 u8 bin[ilen+1];
99 int ret = 0;
100
101 bout[0] = 0x00;
102 bout[1] = cmd;
103 memcpy(&bout[2],o,olen);
104
105 ret = vp702x_usb_inout_op(d, bout, olen+2, bin, ilen+1,msec);
106
107 if (ret == 0)
108 memcpy(i,&bin[1],ilen);
109
110 return ret;
111}
112
Patrick Boettcher0540c492006-09-19 12:51:43 -0300113static int vp702x_set_pld_mode(struct dvb_usb_adapter *adap, u8 bypass)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700114{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300115 u8 buf[16] = { 0 };
116 return vp702x_usb_in_op(adap->dev, 0xe0, (bypass << 8) | 0x0e, 0, buf, 16);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700117}
118
Patrick Boettcher0540c492006-09-19 12:51:43 -0300119static int vp702x_set_pld_state(struct dvb_usb_adapter *adap, u8 state)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700120{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300121 u8 buf[16] = { 0 };
122 return vp702x_usb_in_op(adap->dev, 0xe0, (state << 8) | 0x0f, 0, buf, 16);
123}
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700124
Patrick Boettcher0540c492006-09-19 12:51:43 -0300125static int vp702x_set_pid(struct dvb_usb_adapter *adap, u16 pid, u8 id, int onoff)
126{
127 struct vp702x_state *st = adap->priv;
128 u8 buf[16] = { 0 };
129
130 if (onoff)
131 st->pid_filter_state |= (1 << id);
132 else {
133 st->pid_filter_state &= ~(1 << id);
134 pid = 0xffff;
135 }
136
137 id = 0x10 + id*2;
138
139 vp702x_set_pld_state(adap, st->pid_filter_state);
140 vp702x_usb_in_op(adap->dev, 0xe0, (((pid >> 8) & 0xff) << 8) | (id), 0, buf, 16);
141 vp702x_usb_in_op(adap->dev, 0xe0, (((pid ) & 0xff) << 8) | (id+1), 0, buf, 16);
142 return 0;
143}
144
145
146static int vp702x_init_pid_filter(struct dvb_usb_adapter *adap)
147{
148 struct vp702x_state *st = adap->priv;
149 int i;
150 u8 b[10] = { 0 };
151
152 st->pid_filter_count = 8;
153 st->pid_filter_can_bypass = 1;
154 st->pid_filter_state = 0x00;
155
156 vp702x_set_pld_mode(adap, 1); // bypass
157
158 for (i = 0; i < st->pid_filter_count; i++)
159 vp702x_set_pid(adap, 0xffff, i, 1);
160
161 vp702x_usb_in_op(adap->dev, 0xb5, 3, 0, b, 10);
162 vp702x_usb_in_op(adap->dev, 0xb5, 0, 0, b, 10);
163 vp702x_usb_in_op(adap->dev, 0xb5, 1, 0, b, 10);
164
165 //vp702x_set_pld_mode(d, 0); // filter
166 return 0;
167}
168
169static int vp702x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
170{
171 return 0;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700172}
173
174/* keys for the enclosed remote control */
175static struct dvb_usb_rc_key vp702x_rc_keys[] = {
176 { 0x00, 0x01, KEY_1 },
177 { 0x00, 0x02, KEY_2 },
178};
179
180/* remote control stuff (does not work with my box) */
181static int vp702x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
182{
183 u8 key[10];
184 int i;
185
186/* remove the following return to enabled remote querying */
187 return 0;
188
189 vp702x_usb_in_op(d,READ_REMOTE_REQ,0,0,key,10);
190
191 deb_rc("remote query key: %x %d\n",key[1],key[1]);
192
193 if (key[1] == 0x44) {
194 *state = REMOTE_NO_KEY_PRESSED;
195 return 0;
196 }
197
198 for (i = 0; i < ARRAY_SIZE(vp702x_rc_keys); i++)
199 if (vp702x_rc_keys[i].custom == key[1]) {
200 *state = REMOTE_KEY_PRESSED;
201 *event = vp702x_rc_keys[i].event;
202 break;
203 }
204 return 0;
205}
206
Patrick Boettcher0540c492006-09-19 12:51:43 -0300207int vp702x_power_ctrl(struct dvb_usb_device *d, int onoff)
208{
209 struct vp702x_device_state *st = d->priv;
210
211 if (st->power_state == 0 && onoff)
212 vp702x_usb_out_op(d, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
213 else if (st->power_state == 1 && onoff == 0)
214 vp702x_usb_out_op(d, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
215
216 st->power_state = onoff;
217
218 return 0;
219}
220
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700221static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
222{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300223 u8 i;
224 for (i = 6; i < 12; i++)
225 vp702x_usb_in_op(d, READ_EEPROM_REQ, i, 1, &mac[i - 6], 1);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700226 return 0;
227}
228
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300229static int vp702x_frontend_attach(struct dvb_usb_adapter *adap)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700230{
Patrick Boettcher0540c492006-09-19 12:51:43 -0300231 u8 buf[10] = { 0 };
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700232
Patrick Boettcher0540c492006-09-19 12:51:43 -0300233 vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
234
235 if (vp702x_usb_inout_cmd(adap->dev, GET_SYSTEM_STRING, NULL, 0, buf, 10, 10))
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700236 return -EIO;
237
Patrick Boettcher0540c492006-09-19 12:51:43 -0300238 buf[9] = '\0';
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700239 info("system string: %s",&buf[1]);
240
Patrick Boettcher0540c492006-09-19 12:51:43 -0300241 vp702x_init_pid_filter(adap);
242
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300243 adap->fe = vp702x_fe_attach(adap->dev);
Patrick Boettcher0540c492006-09-19 12:51:43 -0300244 vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
245
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700246 return 0;
247}
248
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300249static struct dvb_usb_device_properties vp702x_properties;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700250
251static int vp702x_usb_probe(struct usb_interface *intf,
252 const struct usb_device_id *id)
253{
Patrick Boettcher47dc3d62005-09-09 13:02:47 -0700254 return dvb_usb_device_init(intf,&vp702x_properties,THIS_MODULE,NULL);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700255}
256
257static struct usb_device_id vp702x_usb_table [] = {
258 { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_COLD) },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300259// { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
260// { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700261 { 0 },
262};
263MODULE_DEVICE_TABLE(usb, vp702x_usb_table);
264
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300265static struct dvb_usb_device_properties vp702x_properties = {
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700266 .usb_ctrl = CYPRESS_FX2,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300267 .firmware = "dvb-usb-vp702x-02.fw",
268 .no_reconnect = 1,
269
270 .size_of_priv = sizeof(struct vp702x_device_state),
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700271
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300272 .num_adapters = 1,
273 .adapter = {
274 {
Patrick Boettcher0540c492006-09-19 12:51:43 -0300275 .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS,
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300276
Patrick Boettcher0540c492006-09-19 12:51:43 -0300277 .streaming_ctrl = vp702x_streaming_ctrl,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700278 .frontend_attach = vp702x_frontend_attach,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300279
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700280 /* parameter for the MPEG2-data transfer */
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300281 .stream = {
282 .type = USB_BULK,
Patrick Boettcher0540c492006-09-19 12:51:43 -0300283 .count = 10,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700284 .endpoint = 0x02,
285 .u = {
286 .bulk = {
287 .buffersize = 4096,
288 }
289 }
290 },
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300291 .size_of_priv = sizeof(struct vp702x_state),
Patrick Boettcher0540c492006-09-19 12:51:43 -0300292 }
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300293 },
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300294 .read_mac_address = vp702x_read_mac_addr,
295
296 .rc_key_map = vp702x_rc_keys,
297 .rc_key_map_size = ARRAY_SIZE(vp702x_rc_keys),
298 .rc_interval = 400,
299 .rc_query = vp702x_rc_query,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700300
Patrick Boettcher0540c492006-09-19 12:51:43 -0300301 .num_device_descs = 1,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700302 .devices = {
303 { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
304 .cold_ids = { &vp702x_usb_table[0], NULL },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300305 .warm_ids = { NULL },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700306 },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300307/* { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700308 .cold_ids = { &vp702x_usb_table[2], NULL },
309 .warm_ids = { &vp702x_usb_table[3], NULL },
310 },
Patrick Boettcher0540c492006-09-19 12:51:43 -0300311*/ { NULL },
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700312 }
313};
314
315/* usb specific object needed to register this driver with the usb subsystem */
316static struct usb_driver vp702x_usb_driver = {
Patrick Boettcher0540c492006-09-19 12:51:43 -0300317 .name = "dvb_usb_vp702x",
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700318 .probe = vp702x_usb_probe,
319 .disconnect = dvb_usb_device_exit,
320 .id_table = vp702x_usb_table,
321};
322
323/* module stuff */
324static int __init vp702x_usb_module_init(void)
325{
326 int result;
327 if ((result = usb_register(&vp702x_usb_driver))) {
328 err("usb_register failed. (%d)",result);
329 return result;
330 }
331
332 return 0;
333}
334
335static void __exit vp702x_usb_module_exit(void)
336{
337 /* deregister this driver from the USB subsystem */
338 usb_deregister(&vp702x_usb_driver);
339}
340
341module_init(vp702x_usb_module_init);
342module_exit(vp702x_usb_module_exit);
343
344MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
345MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
Patrick Boettcher0540c492006-09-19 12:51:43 -0300346MODULE_VERSION("1.0");
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700347MODULE_LICENSE("GPL");