blob: fdab2562bf5d5bed46bf48171fb7a1a128095e79 [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 {
25 u8 pid_table[17]; /* [16] controls the pid_table state */
26};
27
28/* check for mutex FIXME */
29int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen)
30{
31 int ret = 0,try = 0;
32
33 while (ret >= 0 && ret != blen && try < 3) {
34 ret = usb_control_msg(d->udev,
35 usb_rcvctrlpipe(d->udev,0),
36 req,
37 USB_TYPE_VENDOR | USB_DIR_IN,
38 value,index,b,blen,
39 2000);
40 deb_info("reading number %d (ret: %d)\n",try,ret);
41 try++;
42 }
43
44 if (ret < 0 || ret != blen) {
45 warn("usb in operation failed.");
46 ret = -EIO;
47 } else
48 ret = 0;
49
50 deb_xfer("in: req. %x, val: %x, ind: %x, buffer: ",req,value,index);
51 debug_dump(b,blen,deb_xfer);
52
53 return ret;
54}
55
Adrian Bunk703cb2c2006-01-23 17:11:09 -020056static int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
57 u16 index, u8 *b, int blen)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070058{
59 deb_xfer("out: req. %x, val: %x, ind: %x, buffer: ",req,value,index);
60 debug_dump(b,blen,deb_xfer);
61
62 if (usb_control_msg(d->udev,
63 usb_sndctrlpipe(d->udev,0),
64 req,
65 USB_TYPE_VENDOR | USB_DIR_OUT,
66 value,index,b,blen,
67 2000) != blen) {
68 warn("usb out operation failed.");
69 return -EIO;
70 } else
71 return 0;
72}
73
74int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec)
75{
76 int ret;
77
Ingo Molnar3593cab2006-02-07 06:49:14 -020078 if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070079 return ret;
80
81 if ((ret = vp702x_usb_out_op(d,REQUEST_OUT,0,0,o,olen)) < 0)
82 goto unlock;
83 msleep(msec);
84 ret = vp702x_usb_in_op(d,REQUEST_IN,0,0,i,ilen);
85
86unlock:
Ingo Molnar3593cab2006-02-07 06:49:14 -020087 mutex_unlock(&d->usb_mutex);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070088
89 return ret;
90}
91
Adrian Bunk703cb2c2006-01-23 17:11:09 -020092static int vp702x_usb_inout_cmd(struct dvb_usb_device *d, u8 cmd, u8 *o,
93 int olen, u8 *i, int ilen, int msec)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -070094{
95 u8 bout[olen+2];
96 u8 bin[ilen+1];
97 int ret = 0;
98
99 bout[0] = 0x00;
100 bout[1] = cmd;
101 memcpy(&bout[2],o,olen);
102
103 ret = vp702x_usb_inout_op(d, bout, olen+2, bin, ilen+1,msec);
104
105 if (ret == 0)
106 memcpy(i,&bin[1],ilen);
107
108 return ret;
109}
110
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300111static int vp702x_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700112{
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300113 struct vp702x_state *st = adap->priv;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700114 u8 buf[9];
115
116 if (onoff) {
117 st->pid_table[16] |= 1 << index;
118 st->pid_table[index*2] = (pid >> 8) & 0xff;
119 st->pid_table[index*2+1] = pid & 0xff;
120 } else {
121 st->pid_table[16] &= ~(1 << index);
122 st->pid_table[index*2] = st->pid_table[index*2+1] = 0;
123 }
124
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300125 return vp702x_usb_inout_cmd(adap->dev,SET_PID_FILTER,st->pid_table,17,buf,9,10);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700126}
127
128static int vp702x_power_ctrl(struct dvb_usb_device *d, int onoff)
129{
130 vp702x_usb_in_op(d,RESET_TUNER,0,0,NULL,0);
131
132 vp702x_usb_in_op(d,SET_TUNER_POWER_REQ,0,onoff,NULL,0);
133 return vp702x_usb_in_op(d,SET_TUNER_POWER_REQ,0,onoff,NULL,0);
134}
135
136/* keys for the enclosed remote control */
137static struct dvb_usb_rc_key vp702x_rc_keys[] = {
138 { 0x00, 0x01, KEY_1 },
139 { 0x00, 0x02, KEY_2 },
140};
141
142/* remote control stuff (does not work with my box) */
143static int vp702x_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
144{
145 u8 key[10];
146 int i;
147
148/* remove the following return to enabled remote querying */
149 return 0;
150
151 vp702x_usb_in_op(d,READ_REMOTE_REQ,0,0,key,10);
152
153 deb_rc("remote query key: %x %d\n",key[1],key[1]);
154
155 if (key[1] == 0x44) {
156 *state = REMOTE_NO_KEY_PRESSED;
157 return 0;
158 }
159
160 for (i = 0; i < ARRAY_SIZE(vp702x_rc_keys); i++)
161 if (vp702x_rc_keys[i].custom == key[1]) {
162 *state = REMOTE_KEY_PRESSED;
163 *event = vp702x_rc_keys[i].event;
164 break;
165 }
166 return 0;
167}
168
169static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
170{
171 u8 macb[9];
172 if (vp702x_usb_inout_cmd(d, GET_MAC_ADDRESS, NULL, 0, macb, 9, 10))
173 return -EIO;
174 memcpy(mac,&macb[3],6);
175 return 0;
176}
177
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300178static int vp702x_frontend_attach(struct dvb_usb_adapter *adap)
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700179{
180 u8 buf[9] = { 0 };
181
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300182 if (vp702x_usb_inout_cmd(adap->dev, GET_SYSTEM_STRING, NULL, 0, buf, 9, 10))
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700183 return -EIO;
184
185 buf[8] = '\0';
186 info("system string: %s",&buf[1]);
187
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300188 adap->fe = vp702x_fe_attach(adap->dev);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700189 return 0;
190}
191
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300192static struct dvb_usb_device_properties vp702x_properties;
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700193
194static int vp702x_usb_probe(struct usb_interface *intf,
195 const struct usb_device_id *id)
196{
197 struct usb_device *udev = interface_to_usbdev(intf);
198
199 usb_clear_halt(udev,usb_sndctrlpipe(udev,0));
200 usb_clear_halt(udev,usb_rcvctrlpipe(udev,0));
201
Patrick Boettcher47dc3d62005-09-09 13:02:47 -0700202 return dvb_usb_device_init(intf,&vp702x_properties,THIS_MODULE,NULL);
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700203}
204
205static struct usb_device_id vp702x_usb_table [] = {
206 { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_COLD) },
207 { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7021_WARM) },
208 { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
209 { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
210 { 0 },
211};
212MODULE_DEVICE_TABLE(usb, vp702x_usb_table);
213
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300214static struct dvb_usb_device_properties vp702x_properties = {
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700215 .usb_ctrl = CYPRESS_FX2,
216 .firmware = "dvb-usb-vp702x-01.fw",
217
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300218 .num_adapters = 1,
219 .adapter = {
220 {
221 .caps = DVB_USB_ADAP_HAS_PID_FILTER | DVB_USB_ADAP_NEED_PID_FILTERING,
222 .pid_filter_count = 8, /* !!! */
223
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700224 .pid_filter = vp702x_pid_filter,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700225 .frontend_attach = vp702x_frontend_attach,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700226 /* parameter for the MPEG2-data transfer */
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300227 .stream = {
228 .type = USB_BULK,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700229 .count = 7,
230 .endpoint = 0x02,
231 .u = {
232 .bulk = {
233 .buffersize = 4096,
234 }
235 }
236 },
Patrick Boettcher4d43e132006-09-30 06:53:48 -0300237 .size_of_priv = sizeof(struct vp702x_state),
238 },
239 },
240 .power_ctrl = vp702x_power_ctrl,
241 .read_mac_address = vp702x_read_mac_addr,
242
243 .rc_key_map = vp702x_rc_keys,
244 .rc_key_map_size = ARRAY_SIZE(vp702x_rc_keys),
245 .rc_interval = 400,
246 .rc_query = vp702x_rc_query,
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700247
248 .num_device_descs = 2,
249 .devices = {
250 { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
251 .cold_ids = { &vp702x_usb_table[0], NULL },
252 .warm_ids = { &vp702x_usb_table[1], NULL },
253 },
254 { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
255 .cold_ids = { &vp702x_usb_table[2], NULL },
256 .warm_ids = { &vp702x_usb_table[3], NULL },
257 },
258 { 0 },
259 }
260};
261
262/* usb specific object needed to register this driver with the usb subsystem */
263static struct usb_driver vp702x_usb_driver = {
Patrick Boettcher3706a4d2005-09-09 13:02:41 -0700264 .name = "dvb-usb-vp702x",
265 .probe = vp702x_usb_probe,
266 .disconnect = dvb_usb_device_exit,
267 .id_table = vp702x_usb_table,
268};
269
270/* module stuff */
271static int __init vp702x_usb_module_init(void)
272{
273 int result;
274 if ((result = usb_register(&vp702x_usb_driver))) {
275 err("usb_register failed. (%d)",result);
276 return result;
277 }
278
279 return 0;
280}
281
282static void __exit vp702x_usb_module_exit(void)
283{
284 /* deregister this driver from the USB subsystem */
285 usb_deregister(&vp702x_usb_driver);
286}
287
288module_init(vp702x_usb_module_init);
289module_exit(vp702x_usb_module_exit);
290
291MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
292MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
293MODULE_VERSION("1.0-alpha");
294MODULE_LICENSE("GPL");