1 /* DVB USB compliant Linux driver for the TwinhanDTV StarBox USB2.0 DVB-S
4 * Copyright (C) 2005 Ralph Metzler <rjkm@metzlerbros.de>
5 * Metzler Brothers Systementwicklung GbR
7 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@posteo.de>
9 * Thanks to Twinhan who kindly provided hardware and information.
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.
15 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
18 #include <linux/mutex.h>
21 int dvb_usb_vp702x_debug
;
22 module_param_named(debug
,dvb_usb_vp702x_debug
, int, 0644);
23 MODULE_PARM_DESC(debug
, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS
);
25 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
27 struct vp702x_adapter_state
{
29 int pid_filter_can_bypass
;
33 static int vp702x_usb_in_op_unlocked(struct dvb_usb_device
*d
, u8 req
,
34 u16 value
, u16 index
, u8
*b
, int blen
)
38 ret
= usb_control_msg(d
->udev
,
39 usb_rcvctrlpipe(d
->udev
, 0),
41 USB_TYPE_VENDOR
| USB_DIR_IN
,
42 value
, index
, b
, blen
,
46 warn("usb in operation failed. (%d)", ret
);
52 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req
,value
,index
);
53 debug_dump(b
,blen
,deb_xfer
);
58 int vp702x_usb_in_op(struct dvb_usb_device
*d
, u8 req
, u16 value
,
59 u16 index
, u8
*b
, int blen
)
63 mutex_lock(&d
->usb_mutex
);
64 ret
= vp702x_usb_in_op_unlocked(d
, req
, value
, index
, b
, blen
);
65 mutex_unlock(&d
->usb_mutex
);
70 static int vp702x_usb_out_op_unlocked(struct dvb_usb_device
*d
, u8 req
,
71 u16 value
, u16 index
, u8
*b
, int blen
)
74 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ",req
,value
,index
);
75 debug_dump(b
,blen
,deb_xfer
);
77 if ((ret
= usb_control_msg(d
->udev
,
78 usb_sndctrlpipe(d
->udev
,0),
80 USB_TYPE_VENDOR
| USB_DIR_OUT
,
83 warn("usb out operation failed. (%d)",ret
);
89 static int vp702x_usb_out_op(struct dvb_usb_device
*d
, u8 req
, u16 value
,
90 u16 index
, u8
*b
, int blen
)
94 mutex_lock(&d
->usb_mutex
);
95 ret
= vp702x_usb_out_op_unlocked(d
, req
, value
, index
, b
, blen
);
96 mutex_unlock(&d
->usb_mutex
);
101 int vp702x_usb_inout_op(struct dvb_usb_device
*d
, u8
*o
, int olen
, u8
*i
, int ilen
, int msec
)
105 if ((ret
= mutex_lock_interruptible(&d
->usb_mutex
)))
108 ret
= vp702x_usb_out_op_unlocked(d
, REQUEST_OUT
, 0, 0, o
, olen
);
110 ret
= vp702x_usb_in_op_unlocked(d
, REQUEST_IN
, 0, 0, i
, ilen
);
112 mutex_unlock(&d
->usb_mutex
);
116 static int vp702x_usb_inout_cmd(struct dvb_usb_device
*d
, u8 cmd
, u8
*o
,
117 int olen
, u8
*i
, int ilen
, int msec
)
119 struct vp702x_device_state
*st
= d
->priv
;
122 int buflen
= max(olen
+ 2, ilen
+ 1);
124 ret
= mutex_lock_interruptible(&st
->buf_mutex
);
128 if (buflen
> st
->buf_len
) {
129 buf
= kmalloc(buflen
, GFP_KERNEL
);
131 mutex_unlock(&st
->buf_mutex
);
134 info("successfully reallocated a bigger buffer");
137 st
->buf_len
= buflen
;
144 memcpy(&buf
[2], o
, olen
);
146 ret
= vp702x_usb_inout_op(d
, buf
, olen
+2, buf
, ilen
+1, msec
);
149 memcpy(i
, &buf
[1], ilen
);
150 mutex_unlock(&st
->buf_mutex
);
155 static int vp702x_set_pld_mode(struct dvb_usb_adapter
*adap
, u8 bypass
)
158 struct vp702x_device_state
*st
= adap
->dev
->priv
;
161 mutex_lock(&st
->buf_mutex
);
166 ret
= vp702x_usb_in_op(adap
->dev
, 0xe0, (bypass
<< 8) | 0x0e,
168 mutex_unlock(&st
->buf_mutex
);
172 static int vp702x_set_pld_state(struct dvb_usb_adapter
*adap
, u8 state
)
175 struct vp702x_device_state
*st
= adap
->dev
->priv
;
178 mutex_lock(&st
->buf_mutex
);
182 ret
= vp702x_usb_in_op(adap
->dev
, 0xe0, (state
<< 8) | 0x0f,
185 mutex_unlock(&st
->buf_mutex
);
190 static int vp702x_set_pid(struct dvb_usb_adapter
*adap
, u16 pid
, u8 id
, int onoff
)
192 struct vp702x_adapter_state
*st
= adap
->priv
;
193 struct vp702x_device_state
*dst
= adap
->dev
->priv
;
197 st
->pid_filter_state
|= (1 << id
);
199 st
->pid_filter_state
&= ~(1 << id
);
205 vp702x_set_pld_state(adap
, st
->pid_filter_state
);
207 mutex_lock(&dst
->buf_mutex
);
211 vp702x_usb_in_op(adap
->dev
, 0xe0, (((pid
>> 8) & 0xff) << 8) | (id
), 0, buf
, 16);
212 vp702x_usb_in_op(adap
->dev
, 0xe0, (((pid
) & 0xff) << 8) | (id
+1), 0, buf
, 16);
214 mutex_unlock(&dst
->buf_mutex
);
220 static int vp702x_init_pid_filter(struct dvb_usb_adapter
*adap
)
222 struct vp702x_adapter_state
*st
= adap
->priv
;
223 struct vp702x_device_state
*dst
= adap
->dev
->priv
;
227 st
->pid_filter_count
= 8;
228 st
->pid_filter_can_bypass
= 1;
229 st
->pid_filter_state
= 0x00;
231 vp702x_set_pld_mode(adap
, 1); /* bypass */
233 for (i
= 0; i
< st
->pid_filter_count
; i
++)
234 vp702x_set_pid(adap
, 0xffff, i
, 1);
236 mutex_lock(&dst
->buf_mutex
);
239 vp702x_usb_in_op(adap
->dev
, 0xb5, 3, 0, b
, 10);
240 vp702x_usb_in_op(adap
->dev
, 0xb5, 0, 0, b
, 10);
241 vp702x_usb_in_op(adap
->dev
, 0xb5, 1, 0, b
, 10);
242 mutex_unlock(&dst
->buf_mutex
);
243 /*vp702x_set_pld_mode(d, 0); // filter */
248 static int vp702x_streaming_ctrl(struct dvb_usb_adapter
*adap
, int onoff
)
253 /* keys for the enclosed remote control */
254 static struct rc_map_table rc_map_vp702x_table
[] = {
259 /* remote control stuff (does not work with my box) */
260 static int vp702x_rc_query(struct dvb_usb_device
*d
, u32
*event
, int *state
)
262 /* remove the following return to enabled remote querying */
267 key
= kmalloc(10, GFP_KERNEL
);
271 vp702x_usb_in_op(d
,READ_REMOTE_REQ
,0,0,key
,10);
273 deb_rc("remote query key: %x %d\n",key
[1],key
[1]);
275 if (key
[1] == 0x44) {
276 *state
= REMOTE_NO_KEY_PRESSED
;
281 for (i
= 0; i
< ARRAY_SIZE(rc_map_vp702x_table
); i
++)
282 if (rc5_custom(&rc_map_vp702x_table
[i
]) == key
[1]) {
283 *state
= REMOTE_KEY_PRESSED
;
284 *event
= rc_map_vp702x_table
[i
].keycode
;
294 static int vp702x_read_mac_addr(struct dvb_usb_device
*d
,u8 mac
[6])
297 struct vp702x_device_state
*st
= d
->priv
;
299 mutex_lock(&st
->buf_mutex
);
301 for (i
= 6; i
< 12; i
++)
302 vp702x_usb_in_op(d
, READ_EEPROM_REQ
, i
, 1, &buf
[i
- 6], 1);
305 mutex_unlock(&st
->buf_mutex
);
309 static int vp702x_frontend_attach(struct dvb_usb_adapter
*adap
)
313 vp702x_usb_out_op(adap
->dev
, SET_TUNER_POWER_REQ
, 0, 7, NULL
, 0);
315 if (vp702x_usb_inout_cmd(adap
->dev
, GET_SYSTEM_STRING
, NULL
, 0,
320 info("system string: %s",&buf
[1]);
322 vp702x_init_pid_filter(adap
);
324 adap
->fe_adap
[0].fe
= vp702x_fe_attach(adap
->dev
);
325 vp702x_usb_out_op(adap
->dev
, SET_TUNER_POWER_REQ
, 1, 7, NULL
, 0);
330 static struct dvb_usb_device_properties vp702x_properties
;
332 static int vp702x_usb_probe(struct usb_interface
*intf
,
333 const struct usb_device_id
*id
)
335 struct dvb_usb_device
*d
;
336 struct vp702x_device_state
*st
;
339 ret
= dvb_usb_device_init(intf
, &vp702x_properties
,
340 THIS_MODULE
, &d
, adapter_nr
);
346 st
->buf
= kmalloc(st
->buf_len
, GFP_KERNEL
);
349 dvb_usb_device_exit(intf
);
352 mutex_init(&st
->buf_mutex
);
359 static void vp702x_usb_disconnect(struct usb_interface
*intf
)
361 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
362 struct vp702x_device_state
*st
= d
->priv
;
363 mutex_lock(&st
->buf_mutex
);
365 mutex_unlock(&st
->buf_mutex
);
366 dvb_usb_device_exit(intf
);
369 static struct usb_device_id vp702x_usb_table
[] = {
370 { USB_DEVICE(USB_VID_VISIONPLUS
, USB_PID_TWINHAN_VP7021_COLD
) },
371 // { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
372 // { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
375 MODULE_DEVICE_TABLE(usb
, vp702x_usb_table
);
377 static struct dvb_usb_device_properties vp702x_properties
= {
378 .usb_ctrl
= CYPRESS_FX2
,
379 .firmware
= "dvb-usb-vp702x-02.fw",
382 .size_of_priv
= sizeof(struct vp702x_device_state
),
389 .caps
= DVB_USB_ADAP_RECEIVES_204_BYTE_TS
,
391 .streaming_ctrl
= vp702x_streaming_ctrl
,
392 .frontend_attach
= vp702x_frontend_attach
,
394 /* parameter for the MPEG2-data transfer */
406 .size_of_priv
= sizeof(struct vp702x_adapter_state
),
409 .read_mac_address
= vp702x_read_mac_addr
,
412 .rc_map_table
= rc_map_vp702x_table
,
413 .rc_map_size
= ARRAY_SIZE(rc_map_vp702x_table
),
415 .rc_query
= vp702x_rc_query
,
418 .num_device_descs
= 1,
420 { .name
= "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
421 .cold_ids
= { &vp702x_usb_table
[0], NULL
},
422 .warm_ids
= { NULL
},
424 /* { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
425 .cold_ids = { &vp702x_usb_table[2], NULL },
426 .warm_ids = { &vp702x_usb_table[3], NULL },
432 /* usb specific object needed to register this driver with the usb subsystem */
433 static struct usb_driver vp702x_usb_driver
= {
434 .name
= "dvb_usb_vp702x",
435 .probe
= vp702x_usb_probe
,
436 .disconnect
= vp702x_usb_disconnect
,
437 .id_table
= vp702x_usb_table
,
440 module_usb_driver(vp702x_usb_driver
);
442 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
443 MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
444 MODULE_VERSION("1.0");
445 MODULE_LICENSE("GPL");