1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB compliant Linux driver for the TwinhanDTV StarBox USB2.0 DVB-S
5 * Copyright (C) 2005 Ralph Metzler <rjkm@metzlerbros.de>
6 * Metzler Brothers Systementwicklung GbR
8 * Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@posteo.de>
10 * Thanks to Twinhan who kindly provided hardware and information.
12 * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
15 #include <linux/mutex.h>
18 int dvb_usb_vp702x_debug
;
19 module_param_named(debug
,dvb_usb_vp702x_debug
, int, 0644);
20 MODULE_PARM_DESC(debug
, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS
);
22 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
24 struct vp702x_adapter_state
{
26 int pid_filter_can_bypass
;
30 static int vp702x_usb_in_op_unlocked(struct dvb_usb_device
*d
, u8 req
,
31 u16 value
, u16 index
, u8
*b
, int blen
)
35 ret
= usb_control_msg(d
->udev
,
36 usb_rcvctrlpipe(d
->udev
, 0),
38 USB_TYPE_VENDOR
| USB_DIR_IN
,
39 value
, index
, b
, blen
,
43 warn("usb in operation failed. (%d)", ret
);
49 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req
,value
,index
);
50 debug_dump(b
,blen
,deb_xfer
);
55 int vp702x_usb_in_op(struct dvb_usb_device
*d
, u8 req
, u16 value
,
56 u16 index
, u8
*b
, int blen
)
60 mutex_lock(&d
->usb_mutex
);
61 ret
= vp702x_usb_in_op_unlocked(d
, req
, value
, index
, b
, blen
);
62 mutex_unlock(&d
->usb_mutex
);
67 static int vp702x_usb_out_op_unlocked(struct dvb_usb_device
*d
, u8 req
,
68 u16 value
, u16 index
, u8
*b
, int blen
)
71 deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ",req
,value
,index
);
72 debug_dump(b
,blen
,deb_xfer
);
74 if ((ret
= usb_control_msg(d
->udev
,
75 usb_sndctrlpipe(d
->udev
,0),
77 USB_TYPE_VENDOR
| USB_DIR_OUT
,
80 warn("usb out operation failed. (%d)",ret
);
86 static int vp702x_usb_out_op(struct dvb_usb_device
*d
, u8 req
, u16 value
,
87 u16 index
, u8
*b
, int blen
)
91 mutex_lock(&d
->usb_mutex
);
92 ret
= vp702x_usb_out_op_unlocked(d
, req
, value
, index
, b
, blen
);
93 mutex_unlock(&d
->usb_mutex
);
98 int vp702x_usb_inout_op(struct dvb_usb_device
*d
, u8
*o
, int olen
, u8
*i
, int ilen
, int msec
)
102 if ((ret
= mutex_lock_interruptible(&d
->usb_mutex
)))
105 ret
= vp702x_usb_out_op_unlocked(d
, REQUEST_OUT
, 0, 0, o
, olen
);
107 ret
= vp702x_usb_in_op_unlocked(d
, REQUEST_IN
, 0, 0, i
, ilen
);
109 mutex_unlock(&d
->usb_mutex
);
113 static int vp702x_usb_inout_cmd(struct dvb_usb_device
*d
, u8 cmd
, u8
*o
,
114 int olen
, u8
*i
, int ilen
, int msec
)
116 struct vp702x_device_state
*st
= d
->priv
;
119 int buflen
= max(olen
+ 2, ilen
+ 1);
121 ret
= mutex_lock_interruptible(&st
->buf_mutex
);
125 if (buflen
> st
->buf_len
) {
126 buf
= kmalloc(buflen
, GFP_KERNEL
);
128 mutex_unlock(&st
->buf_mutex
);
131 info("successfully reallocated a bigger buffer");
134 st
->buf_len
= buflen
;
141 memcpy(&buf
[2], o
, olen
);
143 ret
= vp702x_usb_inout_op(d
, buf
, olen
+2, buf
, ilen
+1, msec
);
146 memcpy(i
, &buf
[1], ilen
);
147 mutex_unlock(&st
->buf_mutex
);
152 static int vp702x_set_pld_mode(struct dvb_usb_adapter
*adap
, u8 bypass
)
155 struct vp702x_device_state
*st
= adap
->dev
->priv
;
158 mutex_lock(&st
->buf_mutex
);
163 ret
= vp702x_usb_in_op(adap
->dev
, 0xe0, (bypass
<< 8) | 0x0e,
165 mutex_unlock(&st
->buf_mutex
);
169 static int vp702x_set_pld_state(struct dvb_usb_adapter
*adap
, u8 state
)
172 struct vp702x_device_state
*st
= adap
->dev
->priv
;
175 mutex_lock(&st
->buf_mutex
);
179 ret
= vp702x_usb_in_op(adap
->dev
, 0xe0, (state
<< 8) | 0x0f,
182 mutex_unlock(&st
->buf_mutex
);
187 static int vp702x_set_pid(struct dvb_usb_adapter
*adap
, u16 pid
, u8 id
, int onoff
)
189 struct vp702x_adapter_state
*st
= adap
->priv
;
190 struct vp702x_device_state
*dst
= adap
->dev
->priv
;
194 st
->pid_filter_state
|= (1 << id
);
196 st
->pid_filter_state
&= ~(1 << id
);
202 vp702x_set_pld_state(adap
, st
->pid_filter_state
);
204 mutex_lock(&dst
->buf_mutex
);
208 vp702x_usb_in_op(adap
->dev
, 0xe0, (((pid
>> 8) & 0xff) << 8) | (id
), 0, buf
, 16);
209 vp702x_usb_in_op(adap
->dev
, 0xe0, (((pid
) & 0xff) << 8) | (id
+1), 0, buf
, 16);
211 mutex_unlock(&dst
->buf_mutex
);
217 static int vp702x_init_pid_filter(struct dvb_usb_adapter
*adap
)
219 struct vp702x_adapter_state
*st
= adap
->priv
;
220 struct vp702x_device_state
*dst
= adap
->dev
->priv
;
224 st
->pid_filter_count
= 8;
225 st
->pid_filter_can_bypass
= 1;
226 st
->pid_filter_state
= 0x00;
228 vp702x_set_pld_mode(adap
, 1); /* bypass */
230 for (i
= 0; i
< st
->pid_filter_count
; i
++)
231 vp702x_set_pid(adap
, 0xffff, i
, 1);
233 mutex_lock(&dst
->buf_mutex
);
236 vp702x_usb_in_op(adap
->dev
, 0xb5, 3, 0, b
, 10);
237 vp702x_usb_in_op(adap
->dev
, 0xb5, 0, 0, b
, 10);
238 vp702x_usb_in_op(adap
->dev
, 0xb5, 1, 0, b
, 10);
239 mutex_unlock(&dst
->buf_mutex
);
240 /*vp702x_set_pld_mode(d, 0); // filter */
245 static int vp702x_streaming_ctrl(struct dvb_usb_adapter
*adap
, int onoff
)
250 /* keys for the enclosed remote control */
251 static struct rc_map_table rc_map_vp702x_table
[] = {
256 /* remote control stuff (does not work with my box) */
257 static int vp702x_rc_query(struct dvb_usb_device
*d
, u32
*event
, int *state
)
259 /* remove the following return to enabled remote querying */
264 key
= kmalloc(10, GFP_KERNEL
);
268 vp702x_usb_in_op(d
,READ_REMOTE_REQ
,0,0,key
,10);
270 deb_rc("remote query key: %x %d\n",key
[1],key
[1]);
272 if (key
[1] == 0x44) {
273 *state
= REMOTE_NO_KEY_PRESSED
;
278 for (i
= 0; i
< ARRAY_SIZE(rc_map_vp702x_table
); i
++)
279 if (rc5_custom(&rc_map_vp702x_table
[i
]) == key
[1]) {
280 *state
= REMOTE_KEY_PRESSED
;
281 *event
= rc_map_vp702x_table
[i
].keycode
;
291 static int vp702x_read_mac_addr(struct dvb_usb_device
*d
,u8 mac
[6])
294 struct vp702x_device_state
*st
= d
->priv
;
296 mutex_lock(&st
->buf_mutex
);
298 for (i
= 6; i
< 12; i
++)
299 vp702x_usb_in_op(d
, READ_EEPROM_REQ
, i
, 1, &buf
[i
- 6], 1);
302 mutex_unlock(&st
->buf_mutex
);
306 static int vp702x_frontend_attach(struct dvb_usb_adapter
*adap
)
310 vp702x_usb_out_op(adap
->dev
, SET_TUNER_POWER_REQ
, 0, 7, NULL
, 0);
312 if (vp702x_usb_inout_cmd(adap
->dev
, GET_SYSTEM_STRING
, NULL
, 0,
317 info("system string: %s",&buf
[1]);
319 vp702x_init_pid_filter(adap
);
321 adap
->fe_adap
[0].fe
= vp702x_fe_attach(adap
->dev
);
322 vp702x_usb_out_op(adap
->dev
, SET_TUNER_POWER_REQ
, 1, 7, NULL
, 0);
327 static struct dvb_usb_device_properties vp702x_properties
;
329 static int vp702x_usb_probe(struct usb_interface
*intf
,
330 const struct usb_device_id
*id
)
332 struct dvb_usb_device
*d
;
333 struct vp702x_device_state
*st
;
336 ret
= dvb_usb_device_init(intf
, &vp702x_properties
,
337 THIS_MODULE
, &d
, adapter_nr
);
343 st
->buf
= kmalloc(st
->buf_len
, GFP_KERNEL
);
346 dvb_usb_device_exit(intf
);
349 mutex_init(&st
->buf_mutex
);
356 static void vp702x_usb_disconnect(struct usb_interface
*intf
)
358 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
359 struct vp702x_device_state
*st
= d
->priv
;
360 mutex_lock(&st
->buf_mutex
);
362 mutex_unlock(&st
->buf_mutex
);
363 dvb_usb_device_exit(intf
);
366 static struct usb_device_id vp702x_usb_table
[] = {
367 { USB_DEVICE(USB_VID_VISIONPLUS
, USB_PID_TWINHAN_VP7021_COLD
) },
368 // { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_COLD) },
369 // { USB_DEVICE(USB_VID_VISIONPLUS, USB_PID_TWINHAN_VP7020_WARM) },
372 MODULE_DEVICE_TABLE(usb
, vp702x_usb_table
);
374 static struct dvb_usb_device_properties vp702x_properties
= {
375 .usb_ctrl
= CYPRESS_FX2
,
376 .firmware
= "dvb-usb-vp702x-02.fw",
379 .size_of_priv
= sizeof(struct vp702x_device_state
),
386 .caps
= DVB_USB_ADAP_RECEIVES_204_BYTE_TS
,
388 .streaming_ctrl
= vp702x_streaming_ctrl
,
389 .frontend_attach
= vp702x_frontend_attach
,
391 /* parameter for the MPEG2-data transfer */
403 .size_of_priv
= sizeof(struct vp702x_adapter_state
),
406 .read_mac_address
= vp702x_read_mac_addr
,
409 .rc_map_table
= rc_map_vp702x_table
,
410 .rc_map_size
= ARRAY_SIZE(rc_map_vp702x_table
),
412 .rc_query
= vp702x_rc_query
,
415 .num_device_descs
= 1,
417 { .name
= "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
418 .cold_ids
= { &vp702x_usb_table
[0], NULL
},
419 .warm_ids
= { NULL
},
421 /* { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
422 .cold_ids = { &vp702x_usb_table[2], NULL },
423 .warm_ids = { &vp702x_usb_table[3], NULL },
429 /* usb specific object needed to register this driver with the usb subsystem */
430 static struct usb_driver vp702x_usb_driver
= {
431 .name
= "dvb_usb_vp702x",
432 .probe
= vp702x_usb_probe
,
433 .disconnect
= vp702x_usb_disconnect
,
434 .id_table
= vp702x_usb_table
,
437 module_usb_driver(vp702x_usb_driver
);
439 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
440 MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
441 MODULE_VERSION("1.0");
442 MODULE_LICENSE("GPL");