1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB compliant linux driver for GL861 USB2.0 devices.
4 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
6 #include <linux/string.h>
15 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
17 static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
18 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
21 u16 value = addr << (8 + 1);
22 int wo = (rbuf == NULL || rlen == 0); /* write-only */
28 req = GL861_REQ_I2C_WRITE;
30 buf = kmemdup(wbuf, wlen, GFP_KERNEL);
32 req = GL861_REQ_I2C_READ;
34 buf = kmalloc(rlen, GFP_KERNEL);
45 value = value + wbuf[1];
48 dev_err(&d->udev->dev, "%s: wlen=%d, aborting\n",
49 KBUILD_MODNAME, wlen);
54 usleep_range(1000, 2000); /* avoid I2C errors */
56 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type,
57 value, index, buf, rlen, 2000);
60 memcpy(rbuf, buf, rlen);
67 static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
70 struct dvb_usb_device *d = i2c_get_adapdata(adap);
76 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
79 for (i = 0; i < num; i++) {
80 /* write/read request */
81 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
82 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
83 msg[i].len, msg[i+1].buf, msg[i+1].len) < 0)
87 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
88 msg[i].len, NULL, 0) < 0)
92 mutex_unlock(&d->i2c_mutex);
96 static u32 gl861_i2c_func(struct i2c_adapter *adapter)
101 static struct i2c_algorithm gl861_i2c_algo = {
102 .master_xfer = gl861_i2c_xfer,
103 .functionality = gl861_i2c_func,
106 /* Callbacks for DVB USB */
107 static struct zl10353_config gl861_zl10353_config = {
108 .demod_address = 0x0f,
113 static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
116 adap->fe[0] = dvb_attach(zl10353_attach, &gl861_zl10353_config,
117 &adap_to_d(adap)->i2c_adap);
118 if (adap->fe[0] == NULL)
124 static struct qt1010_config gl861_qt1010_config = {
128 static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
130 return dvb_attach(qt1010_attach,
131 adap->fe[0], &adap_to_d(adap)->i2c_adap,
132 &gl861_qt1010_config) == NULL ? -ENODEV : 0;
135 static int gl861_init(struct dvb_usb_device *d)
138 * There is 2 interfaces. Interface 0 is for TV and interface 1 is
139 * for HID remote controller. Interface 0 has 2 alternate settings.
140 * For some reason we need to set interface explicitly, defaulted
141 * as alternate setting 1?
143 return usb_set_interface(d->udev, 0, 0);
146 /* DVB USB Driver stuff */
147 static struct dvb_usb_device_properties gl861_props = {
148 .driver_name = KBUILD_MODNAME,
149 .owner = THIS_MODULE,
150 .adapter_nr = adapter_nr,
152 .i2c_algo = &gl861_i2c_algo,
153 .frontend_attach = gl861_frontend_attach,
154 .tuner_attach = gl861_tuner_attach,
160 .stream = DVB_USB_STREAM_BULK(0x81, 7, 512),
171 struct i2c_adapter *demod_sub_i2c;
172 struct i2c_client *i2c_client_demod;
173 struct i2c_client *i2c_client_tuner;
174 struct i2c_adapter tuner_adap;
177 struct friio_config {
178 struct i2c_board_info demod_info;
179 struct tc90522_config demod_cfg;
181 struct i2c_board_info tuner_info;
182 struct dvb_pll_config tuner_cfg;
185 static const struct friio_config friio_config = {
186 .demod_info = { I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x18), },
187 .tuner_info = { I2C_BOARD_INFO("tua6034_friio", 0x60), },
190 /* For another type of I2C:
191 * message sent by a USB control-read/write transaction with data stage.
192 * Used in init/config of Friio.
195 gl861_i2c_write_ex(struct dvb_usb_device *d, u8 addr, u8 *wbuf, u16 wlen)
200 buf = kmemdup(wbuf, wlen, GFP_KERNEL);
204 ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
205 GL861_REQ_I2C_RAW, GL861_WRITE,
206 addr << (8 + 1), 0x0100, buf, wlen, 2000);
212 gl861_i2c_read_ex(struct dvb_usb_device *d, u8 addr, u8 *rbuf, u16 rlen)
217 buf = kmalloc(rlen, GFP_KERNEL);
221 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
222 GL861_REQ_I2C_READ, GL861_READ,
223 addr << (8 + 1), 0x0100, buf, rlen, 2000);
224 if (ret > 0 && rlen > 0)
225 memcpy(buf, rbuf, rlen);
230 /* For I2C transactions to the tuner of Friio (dvb_pll).
232 * Friio uses irregular USB encapsulation for tuner i2c transactions:
233 * write transacions are encapsulated with a different USB 'request' value.
235 * Although all transactions are sent via the demod(tc90522)
236 * and the demod provides an i2c adapter for them, it cannot be used in Friio
237 * since it assumes using the same parent adapter with the demod,
238 * which does not use the request value and uses same one for both read/write.
239 * So we define a dedicated i2c adapter here.
243 friio_i2c_tuner_read(struct dvb_usb_device *d, struct i2c_msg *msg)
245 struct friio_priv *priv;
249 addr = priv->i2c_client_demod->addr;
250 return gl861_i2c_read_ex(d, addr, msg->buf, msg->len);
254 friio_i2c_tuner_write(struct dvb_usb_device *d, struct i2c_msg *msg)
258 struct friio_priv *priv;
265 buf = kmalloc(msg->len + 1, GFP_KERNEL);
268 buf[0] = msg->addr << 1;
269 memcpy(buf + 1, msg->buf, msg->len);
271 ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
272 GL861_REQ_I2C_RAW, GL861_WRITE,
273 priv->i2c_client_demod->addr << (8 + 1),
274 0xFE, buf, msg->len + 1, 2000);
279 static int friio_tuner_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
282 struct dvb_usb_device *d = i2c_get_adapdata(adap);
288 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
291 for (i = 0; i < num; i++) {
294 if (msg[i].flags & I2C_M_RD)
295 ret = friio_i2c_tuner_read(d, &msg[i]);
297 ret = friio_i2c_tuner_write(d, &msg[i]);
302 usleep_range(1000, 2000); /* avoid I2C errors */
305 mutex_unlock(&d->i2c_mutex);
309 static struct i2c_algorithm friio_tuner_i2c_algo = {
310 .master_xfer = friio_tuner_i2c_xfer,
311 .functionality = gl861_i2c_func,
314 /* GPIO control in Friio */
316 #define FRIIO_CTL_LNB (1 << 0)
317 #define FRIIO_CTL_STROBE (1 << 1)
318 #define FRIIO_CTL_CLK (1 << 2)
319 #define FRIIO_CTL_LED (1 << 3)
321 #define FRIIO_LED_RUNNING 0x6400ff64
322 #define FRIIO_LED_STOPPED 0x96ff00ff
324 /* control PIC16F676 attached to Friio */
325 static int friio_ext_ctl(struct dvb_usb_device *d,
326 u32 sat_color, int power_on)
332 u8 power = (power_on) ? FRIIO_CTL_LNB : 0;
334 buf = kmalloc(2, GFP_KERNEL);
344 /* send 2bit header (&B10) */
345 buf[1] = power | FRIIO_CTL_LED | FRIIO_CTL_STROBE;
346 ret = i2c_transfer(&d->i2c_adap, &msg, 1);
347 buf[1] |= FRIIO_CTL_CLK;
348 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
350 buf[1] = power | FRIIO_CTL_STROBE;
351 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
352 buf[1] |= FRIIO_CTL_CLK;
353 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
355 /* send 32bit(satur, R, G, B) data in serial */
357 for (i = 0; i < 32; i++) {
358 buf[1] = power | FRIIO_CTL_STROBE;
359 if (sat_color & mask)
360 buf[1] |= FRIIO_CTL_LED;
361 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
362 buf[1] |= FRIIO_CTL_CLK;
363 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
367 /* set the strobe off */
369 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
370 buf[1] |= FRIIO_CTL_CLK;
371 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
374 return (ret == 70) ? 0 : -EREMOTEIO;
377 /* init/config of gl861 for Friio */
379 * This function cannot be moved to friio_init()/dvb_usbv2_init(),
380 * because the init defined here must be done before any activities like I2C,
381 * but friio_init() is called by dvb-usbv2 after {_frontend, _tuner}_attach(),
382 * where I2C communication is used.
383 * Thus this function is set to be called from _power_ctl().
385 * Since it will be called on the early init stage
386 * where the i2c adapter is not initialized yet,
387 * we cannot use i2c_transfer() here.
389 static int friio_reset(struct dvb_usb_device *d)
394 static const u8 friio_init_cmds[][2] = {
395 {0x33, 0x08}, {0x37, 0x40}, {0x3a, 0x1f}, {0x3b, 0xff},
396 {0x3c, 0x1f}, {0x3d, 0xff}, {0x38, 0x00}, {0x35, 0x00},
397 {0x39, 0x00}, {0x36, 0x00},
400 ret = usb_set_interface(d->udev, 0, 0);
406 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
409 usleep_range(2000, 3000);
413 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
418 * Check if the dev is really a Friio White, since it might be
419 * another device, Friio Black, with the same VID/PID.
422 usleep_range(1000, 2000);
425 ret = gl861_i2c_write_ex(d, 0x09, wbuf, 2);
429 usleep_range(2000, 3000);
430 ret = gl861_i2c_read_ex(d, 0x09, rbuf, 2);
433 if (rbuf[0] != 0xff || rbuf[1] != 0xff)
437 usleep_range(1000, 2000);
438 ret = gl861_i2c_write_ex(d, 0x48, wbuf, 2);
442 usleep_range(2000, 3000);
443 ret = gl861_i2c_read_ex(d, 0x48, rbuf, 2);
446 if (rbuf[0] != 0xff || rbuf[1] != 0xff)
451 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
457 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
463 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
467 for (i = 0; i < ARRAY_SIZE(friio_init_cmds); i++) {
468 ret = gl861_i2c_msg(d, 0x00, (u8 *)friio_init_cmds[i], 2,
477 * DVB callbacks for Friio
480 static int friio_power_ctrl(struct dvb_usb_device *d, int onoff)
482 return onoff ? friio_reset(d) : 0;
485 static int friio_frontend_attach(struct dvb_usb_adapter *adap)
487 const struct i2c_board_info *info;
488 struct dvb_usb_device *d;
489 struct tc90522_config cfg;
490 struct i2c_client *cl;
491 struct friio_priv *priv;
493 info = &friio_config.demod_info;
495 cl = dvb_module_probe("tc90522", info->type,
496 &d->i2c_adap, info->addr, &cfg);
499 adap->fe[0] = cfg.fe;
501 /* ignore cfg.tuner_i2c and create new one */
502 priv = adap_to_priv(adap);
503 priv->i2c_client_demod = cl;
504 priv->tuner_adap.algo = &friio_tuner_i2c_algo;
505 priv->tuner_adap.dev.parent = &d->udev->dev;
506 strscpy(priv->tuner_adap.name, d->name, sizeof(priv->tuner_adap.name));
507 strlcat(priv->tuner_adap.name, "-tuner", sizeof(priv->tuner_adap.name));
508 priv->demod_sub_i2c = &priv->tuner_adap;
509 i2c_set_adapdata(&priv->tuner_adap, d);
511 return i2c_add_adapter(&priv->tuner_adap);
514 static int friio_frontend_detach(struct dvb_usb_adapter *adap)
516 struct friio_priv *priv;
518 priv = adap_to_priv(adap);
519 i2c_del_adapter(&priv->tuner_adap);
520 dvb_module_release(priv->i2c_client_demod);
524 static int friio_tuner_attach(struct dvb_usb_adapter *adap)
526 const struct i2c_board_info *info;
527 struct dvb_pll_config cfg;
528 struct i2c_client *cl;
529 struct friio_priv *priv;
531 priv = adap_to_priv(adap);
532 info = &friio_config.tuner_info;
533 cfg = friio_config.tuner_cfg;
534 cfg.fe = adap->fe[0];
536 cl = dvb_module_probe("dvb_pll", info->type,
537 priv->demod_sub_i2c, info->addr, &cfg);
540 priv->i2c_client_tuner = cl;
544 static int friio_tuner_detach(struct dvb_usb_adapter *adap)
546 struct friio_priv *priv;
548 priv = adap_to_priv(adap);
549 dvb_module_release(priv->i2c_client_tuner);
553 static int friio_init(struct dvb_usb_device *d)
557 struct friio_priv *priv;
559 static const u8 demod_init[][2] = {
560 {0x01, 0x40}, {0x04, 0x38}, {0x05, 0x40}, {0x07, 0x40},
561 {0x0f, 0x4f}, {0x11, 0x21}, {0x12, 0x0b}, {0x13, 0x2f},
562 {0x14, 0x31}, {0x16, 0x02}, {0x21, 0xc4}, {0x22, 0x20},
563 {0x2c, 0x79}, {0x2d, 0x34}, {0x2f, 0x00}, {0x30, 0x28},
564 {0x31, 0x31}, {0x32, 0xdf}, {0x38, 0x01}, {0x39, 0x78},
565 {0x3b, 0x33}, {0x3c, 0x33}, {0x48, 0x90}, {0x51, 0x68},
566 {0x5e, 0x38}, {0x71, 0x00}, {0x72, 0x08}, {0x77, 0x00},
567 {0xc0, 0x21}, {0xc1, 0x10}, {0xe4, 0x1a}, {0xea, 0x1f},
568 {0x77, 0x00}, {0x71, 0x00}, {0x71, 0x00}, {0x76, 0x0c},
572 ret = friio_ext_ctl(d, FRIIO_LED_STOPPED, true);
577 /* init/config demod */
579 for (i = 0; i < ARRAY_SIZE(demod_init); i++) {
582 ret = i2c_master_send(priv->i2c_client_demod, demod_init[i], 2);
590 static void friio_exit(struct dvb_usb_device *d)
592 friio_ext_ctl(d, FRIIO_LED_STOPPED, false);
595 static int friio_streaming_ctrl(struct dvb_frontend *fe, int onoff)
599 led_color = onoff ? FRIIO_LED_RUNNING : FRIIO_LED_STOPPED;
600 return friio_ext_ctl(fe_to_d(fe), led_color, true);
604 static struct dvb_usb_device_properties friio_props = {
605 .driver_name = KBUILD_MODNAME,
606 .owner = THIS_MODULE,
607 .adapter_nr = adapter_nr,
609 .size_of_priv = sizeof(struct friio_priv),
611 .i2c_algo = &gl861_i2c_algo,
612 .power_ctrl = friio_power_ctrl,
613 .frontend_attach = friio_frontend_attach,
614 .frontend_detach = friio_frontend_detach,
615 .tuner_attach = friio_tuner_attach,
616 .tuner_detach = friio_tuner_detach,
619 .streaming_ctrl = friio_streaming_ctrl,
624 .stream = DVB_USB_STREAM_BULK(0x01, 8, 16384),
629 static const struct usb_device_id gl861_id_table[] = {
630 { DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801,
631 &gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) },
632 { DVB_USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU,
633 &gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) },
634 { DVB_USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE,
635 &friio_props, "774 Friio White ISDB-T USB2.0", NULL) },
638 MODULE_DEVICE_TABLE(usb, gl861_id_table);
640 static struct usb_driver gl861_usb_driver = {
641 .name = KBUILD_MODNAME,
642 .id_table = gl861_id_table,
643 .probe = dvb_usbv2_probe,
644 .disconnect = dvb_usbv2_disconnect,
645 .suspend = dvb_usbv2_suspend,
646 .resume = dvb_usbv2_resume,
647 .reset_resume = dvb_usbv2_reset_resume,
652 module_usb_driver(gl861_usb_driver);
654 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
655 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
656 MODULE_VERSION("0.1");
657 MODULE_LICENSE("GPL");