drm/rockchip: Don't change hdmi reference clock rate
[drm/drm-misc.git] / drivers / media / usb / dvb-usb / vp702x.c
blob5b6740cbd1d17c6e4be655beb23583481ea5e434
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB compliant Linux driver for the TwinhanDTV StarBox USB2.0 DVB-S
3 * receiver.
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
14 #include "vp702x.h"
15 #include <linux/mutex.h>
17 /* debug */
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 {
25 int pid_filter_count;
26 int pid_filter_can_bypass;
27 u8 pid_filter_state;
30 static int vp702x_usb_in_op_unlocked(struct dvb_usb_device *d, u8 req,
31 u16 value, u16 index, u8 *b, int blen)
33 int ret;
35 ret = usb_control_msg(d->udev,
36 usb_rcvctrlpipe(d->udev, 0),
37 req,
38 USB_TYPE_VENDOR | USB_DIR_IN,
39 value, index, b, blen,
40 2000);
42 if (ret < 0) {
43 warn("usb in operation failed. (%d)", ret);
44 ret = -EIO;
45 } else
46 ret = 0;
49 deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index);
50 debug_dump(b,blen,deb_xfer);
52 return ret;
55 int vp702x_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value,
56 u16 index, u8 *b, int blen)
58 int ret;
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);
64 return ret;
67 static int vp702x_usb_out_op_unlocked(struct dvb_usb_device *d, u8 req,
68 u16 value, u16 index, u8 *b, int blen)
70 int ret;
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),
76 req,
77 USB_TYPE_VENDOR | USB_DIR_OUT,
78 value,index,b,blen,
79 2000)) != blen) {
80 warn("usb out operation failed. (%d)",ret);
81 return -EIO;
82 } else
83 return 0;
86 static int vp702x_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value,
87 u16 index, u8 *b, int blen)
89 int ret;
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);
95 return ret;
98 int vp702x_usb_inout_op(struct dvb_usb_device *d, u8 *o, int olen, u8 *i, int ilen, int msec)
100 int ret;
102 if ((ret = mutex_lock_interruptible(&d->usb_mutex)))
103 return ret;
105 ret = vp702x_usb_out_op_unlocked(d, REQUEST_OUT, 0, 0, o, olen);
106 msleep(msec);
107 ret = vp702x_usb_in_op_unlocked(d, REQUEST_IN, 0, 0, i, ilen);
109 mutex_unlock(&d->usb_mutex);
110 return ret;
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;
117 int ret = 0;
118 u8 *buf;
119 int buflen = max(olen + 2, ilen + 1);
121 ret = mutex_lock_interruptible(&st->buf_mutex);
122 if (ret < 0)
123 return ret;
125 if (buflen > st->buf_len) {
126 buf = kmalloc(buflen, GFP_KERNEL);
127 if (!buf) {
128 mutex_unlock(&st->buf_mutex);
129 return -ENOMEM;
131 info("successfully reallocated a bigger buffer");
132 kfree(st->buf);
133 st->buf = buf;
134 st->buf_len = buflen;
135 } else {
136 buf = st->buf;
139 buf[0] = 0x00;
140 buf[1] = cmd;
141 memcpy(&buf[2], o, olen);
143 ret = vp702x_usb_inout_op(d, buf, olen+2, buf, ilen+1, msec);
145 if (ret == 0)
146 memcpy(i, &buf[1], ilen);
147 mutex_unlock(&st->buf_mutex);
149 return ret;
152 static int vp702x_set_pld_mode(struct dvb_usb_adapter *adap, u8 bypass)
154 int ret;
155 struct vp702x_device_state *st = adap->dev->priv;
156 u8 *buf;
158 mutex_lock(&st->buf_mutex);
160 buf = st->buf;
161 memset(buf, 0, 16);
163 ret = vp702x_usb_in_op(adap->dev, 0xe0, (bypass << 8) | 0x0e,
164 0, buf, 16);
165 mutex_unlock(&st->buf_mutex);
166 return ret;
169 static int vp702x_set_pld_state(struct dvb_usb_adapter *adap, u8 state)
171 int ret;
172 struct vp702x_device_state *st = adap->dev->priv;
173 u8 *buf;
175 mutex_lock(&st->buf_mutex);
177 buf = st->buf;
178 memset(buf, 0, 16);
179 ret = vp702x_usb_in_op(adap->dev, 0xe0, (state << 8) | 0x0f,
180 0, buf, 16);
182 mutex_unlock(&st->buf_mutex);
184 return ret;
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;
191 u8 *buf;
193 if (onoff)
194 st->pid_filter_state |= (1 << id);
195 else {
196 st->pid_filter_state &= ~(1 << id);
197 pid = 0xffff;
200 id = 0x10 + id*2;
202 vp702x_set_pld_state(adap, st->pid_filter_state);
204 mutex_lock(&dst->buf_mutex);
206 buf = dst->buf;
207 memset(buf, 0, 16);
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);
213 return 0;
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;
221 int i;
222 u8 *b;
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);
234 b = dst->buf;
235 memset(b, 0, 10);
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 */
242 return 0;
245 static int vp702x_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
247 return 0;
250 /* keys for the enclosed remote control */
251 static struct rc_map_table rc_map_vp702x_table[] = {
252 { 0x0001, KEY_1 },
253 { 0x0002, KEY_2 },
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 */
260 #if 0
261 u8 *key;
262 int i;
264 key = kmalloc(10, GFP_KERNEL);
265 if (!key)
266 return -ENOMEM;
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;
274 kfree(key);
275 return 0;
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;
282 break;
284 kfree(key);
285 #endif
287 return 0;
291 static int vp702x_read_mac_addr(struct dvb_usb_device *d,u8 mac[6])
293 u8 i, *buf;
294 int ret;
295 struct vp702x_device_state *st = d->priv;
297 mutex_lock(&st->buf_mutex);
298 buf = st->buf;
299 for (i = 6; i < 12; i++) {
300 ret = vp702x_usb_in_op(d, READ_EEPROM_REQ, i, 1,
301 &buf[i - 6], 1);
302 if (ret < 0)
303 goto err;
306 memcpy(mac, buf, 6);
307 err:
308 mutex_unlock(&st->buf_mutex);
309 return ret;
312 static int vp702x_frontend_attach(struct dvb_usb_adapter *adap)
314 u8 buf[10] = { 0 };
316 vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 0, 7, NULL, 0);
318 if (vp702x_usb_inout_cmd(adap->dev, GET_SYSTEM_STRING, NULL, 0,
319 buf, 10, 10))
320 return -EIO;
322 buf[9] = '\0';
323 info("system string: %s",&buf[1]);
325 vp702x_init_pid_filter(adap);
327 adap->fe_adap[0].fe = vp702x_fe_attach(adap->dev);
328 vp702x_usb_out_op(adap->dev, SET_TUNER_POWER_REQ, 1, 7, NULL, 0);
330 return 0;
333 static struct dvb_usb_device_properties vp702x_properties;
335 static int vp702x_usb_probe(struct usb_interface *intf,
336 const struct usb_device_id *id)
338 struct dvb_usb_device *d;
339 struct vp702x_device_state *st;
340 int ret;
342 ret = dvb_usb_device_init(intf, &vp702x_properties,
343 THIS_MODULE, &d, adapter_nr);
344 if (ret)
345 goto out;
347 st = d->priv;
348 st->buf_len = 16;
349 st->buf = kmalloc(st->buf_len, GFP_KERNEL);
350 if (!st->buf) {
351 ret = -ENOMEM;
352 dvb_usb_device_exit(intf);
353 goto out;
355 mutex_init(&st->buf_mutex);
357 out:
358 return ret;
362 static void vp702x_usb_disconnect(struct usb_interface *intf)
364 struct dvb_usb_device *d = usb_get_intfdata(intf);
365 struct vp702x_device_state *st = d->priv;
366 mutex_lock(&st->buf_mutex);
367 kfree(st->buf);
368 mutex_unlock(&st->buf_mutex);
369 dvb_usb_device_exit(intf);
372 enum {
373 VISIONPLUS_VP7021_COLD,
374 VISIONPLUS_VP7020_COLD,
375 VISIONPLUS_VP7020_WARM,
378 static struct usb_device_id vp702x_usb_table[] = {
379 DVB_USB_DEV(VISIONPLUS, VISIONPLUS_VP7021_COLD),
380 // DVB_USB_DEV(VISIONPLUS, VISIONPLUS_VP7020_COLD),
381 // DVB_USB_DEV(VISIONPLUS, VISIONPLUS_VP7020_WARM),
385 MODULE_DEVICE_TABLE(usb, vp702x_usb_table);
387 static struct dvb_usb_device_properties vp702x_properties = {
388 .usb_ctrl = CYPRESS_FX2,
389 .firmware = "dvb-usb-vp702x-02.fw",
390 .no_reconnect = 1,
392 .size_of_priv = sizeof(struct vp702x_device_state),
394 .num_adapters = 1,
395 .adapter = {
397 .num_frontends = 1,
398 .fe = {{
399 .caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS,
401 .streaming_ctrl = vp702x_streaming_ctrl,
402 .frontend_attach = vp702x_frontend_attach,
404 /* parameter for the MPEG2-data transfer */
405 .stream = {
406 .type = USB_BULK,
407 .count = 10,
408 .endpoint = 0x02,
409 .u = {
410 .bulk = {
411 .buffersize = 4096,
416 .size_of_priv = sizeof(struct vp702x_adapter_state),
419 .read_mac_address = vp702x_read_mac_addr,
421 .rc.legacy = {
422 .rc_map_table = rc_map_vp702x_table,
423 .rc_map_size = ARRAY_SIZE(rc_map_vp702x_table),
424 .rc_interval = 400,
425 .rc_query = vp702x_rc_query,
428 .num_device_descs = 1,
429 .devices = {
430 { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7021)",
431 .cold_ids = { &vp702x_usb_table[VISIONPLUS_VP7021_COLD], NULL },
432 .warm_ids = { NULL },
434 /* { .name = "TwinhanDTV StarBox DVB-S USB2.0 (VP7020)",
435 .cold_ids = { &vp702x_usb_table[VISIONPLUS_VP7020_COLD], NULL },
436 .warm_ids = { &vp702x_usb_table[VISIONPLUS_VP7020_WARM], NULL },
438 */ { NULL },
442 /* usb specific object needed to register this driver with the usb subsystem */
443 static struct usb_driver vp702x_usb_driver = {
444 .name = "dvb_usb_vp702x",
445 .probe = vp702x_usb_probe,
446 .disconnect = vp702x_usb_disconnect,
447 .id_table = vp702x_usb_table,
450 module_usb_driver(vp702x_usb_driver);
452 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
453 MODULE_DESCRIPTION("Driver for Twinhan StarBox DVB-S USB2.0 and clones");
454 MODULE_VERSION("1.0");
455 MODULE_LICENSE("GPL");