1 /* DVB USB compliant linux driver for GL861 USB2.0 devices.
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the
5 * Free Software Foundation, version 2.
7 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
9 #include <linux/string.h>
18 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
20 static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
21 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
24 u16 value = addr << (8 + 1);
25 int wo = (rbuf == NULL || rlen == 0); /* write-only */
31 req = GL861_REQ_I2C_WRITE;
33 buf = kmemdup(wbuf, wlen, GFP_KERNEL);
35 req = GL861_REQ_I2C_READ;
37 buf = kmalloc(rlen, GFP_KERNEL);
48 value = value + wbuf[1];
51 dev_err(&d->udev->dev, "%s: wlen=%d, aborting\n",
52 KBUILD_MODNAME, wlen);
57 usleep_range(1000, 2000); /* avoid I2C errors */
59 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0), req, type,
60 value, index, buf, rlen, 2000);
63 memcpy(rbuf, buf, rlen);
70 static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
73 struct dvb_usb_device *d = i2c_get_adapdata(adap);
79 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
82 for (i = 0; i < num; i++) {
83 /* write/read request */
84 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
85 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
86 msg[i].len, msg[i+1].buf, msg[i+1].len) < 0)
90 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
91 msg[i].len, NULL, 0) < 0)
95 mutex_unlock(&d->i2c_mutex);
99 static u32 gl861_i2c_func(struct i2c_adapter *adapter)
104 static struct i2c_algorithm gl861_i2c_algo = {
105 .master_xfer = gl861_i2c_xfer,
106 .functionality = gl861_i2c_func,
109 /* Callbacks for DVB USB */
110 static struct zl10353_config gl861_zl10353_config = {
111 .demod_address = 0x0f,
116 static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
119 adap->fe[0] = dvb_attach(zl10353_attach, &gl861_zl10353_config,
120 &adap_to_d(adap)->i2c_adap);
121 if (adap->fe[0] == NULL)
127 static struct qt1010_config gl861_qt1010_config = {
131 static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
133 return dvb_attach(qt1010_attach,
134 adap->fe[0], &adap_to_d(adap)->i2c_adap,
135 &gl861_qt1010_config) == NULL ? -ENODEV : 0;
138 static int gl861_init(struct dvb_usb_device *d)
141 * There is 2 interfaces. Interface 0 is for TV and interface 1 is
142 * for HID remote controller. Interface 0 has 2 alternate settings.
143 * For some reason we need to set interface explicitly, defaulted
144 * as alternate setting 1?
146 return usb_set_interface(d->udev, 0, 0);
149 /* DVB USB Driver stuff */
150 static struct dvb_usb_device_properties gl861_props = {
151 .driver_name = KBUILD_MODNAME,
152 .owner = THIS_MODULE,
153 .adapter_nr = adapter_nr,
155 .i2c_algo = &gl861_i2c_algo,
156 .frontend_attach = gl861_frontend_attach,
157 .tuner_attach = gl861_tuner_attach,
163 .stream = DVB_USB_STREAM_BULK(0x81, 7, 512),
174 struct i2c_adapter *demod_sub_i2c;
175 struct i2c_client *i2c_client_demod;
176 struct i2c_client *i2c_client_tuner;
177 struct i2c_adapter tuner_adap;
180 struct friio_config {
181 struct i2c_board_info demod_info;
182 struct tc90522_config demod_cfg;
184 struct i2c_board_info tuner_info;
185 struct dvb_pll_config tuner_cfg;
188 static const struct friio_config friio_config = {
189 .demod_info = { I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x18), },
190 .tuner_info = { I2C_BOARD_INFO("tua6034_friio", 0x60), },
193 /* For another type of I2C:
194 * message sent by a USB control-read/write transaction with data stage.
195 * Used in init/config of Friio.
198 gl861_i2c_write_ex(struct dvb_usb_device *d, u8 addr, u8 *wbuf, u16 wlen)
203 buf = kmalloc(wlen, GFP_KERNEL);
207 memcpy(buf, wbuf, wlen);
208 ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
209 GL861_REQ_I2C_RAW, GL861_WRITE,
210 addr << (8 + 1), 0x0100, buf, wlen, 2000);
216 gl861_i2c_read_ex(struct dvb_usb_device *d, u8 addr, u8 *rbuf, u16 rlen)
221 buf = kmalloc(rlen, GFP_KERNEL);
225 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
226 GL861_REQ_I2C_READ, GL861_READ,
227 addr << (8 + 1), 0x0100, buf, rlen, 2000);
228 if (ret > 0 && rlen > 0)
229 memcpy(buf, rbuf, rlen);
234 /* For I2C transactions to the tuner of Friio (dvb_pll).
236 * Friio uses irregular USB encapsulation for tuner i2c transactions:
237 * write transacions are encapsulated with a different USB 'request' value.
239 * Although all transactions are sent via the demod(tc90522)
240 * and the demod provides an i2c adapter for them, it cannot be used in Friio
241 * since it assumes using the same parent adapter with the demod,
242 * which does not use the request value and uses same one for both read/write.
243 * So we define a dedicated i2c adapter here.
247 friio_i2c_tuner_read(struct dvb_usb_device *d, struct i2c_msg *msg)
249 struct friio_priv *priv;
253 addr = priv->i2c_client_demod->addr;
254 return gl861_i2c_read_ex(d, addr, msg->buf, msg->len);
258 friio_i2c_tuner_write(struct dvb_usb_device *d, struct i2c_msg *msg)
262 struct friio_priv *priv;
269 buf = kmalloc(msg->len + 1, GFP_KERNEL);
272 buf[0] = msg->addr << 1;
273 memcpy(buf + 1, msg->buf, msg->len);
275 ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
276 GL861_REQ_I2C_RAW, GL861_WRITE,
277 priv->i2c_client_demod->addr << (8 + 1),
278 0xFE, buf, msg->len + 1, 2000);
283 static int friio_tuner_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
286 struct dvb_usb_device *d = i2c_get_adapdata(adap);
292 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
295 for (i = 0; i < num; i++) {
298 if (msg[i].flags & I2C_M_RD)
299 ret = friio_i2c_tuner_read(d, &msg[i]);
301 ret = friio_i2c_tuner_write(d, &msg[i]);
306 usleep_range(1000, 2000); /* avoid I2C errors */
309 mutex_unlock(&d->i2c_mutex);
313 static struct i2c_algorithm friio_tuner_i2c_algo = {
314 .master_xfer = friio_tuner_i2c_xfer,
315 .functionality = gl861_i2c_func,
318 /* GPIO control in Friio */
320 #define FRIIO_CTL_LNB (1 << 0)
321 #define FRIIO_CTL_STROBE (1 << 1)
322 #define FRIIO_CTL_CLK (1 << 2)
323 #define FRIIO_CTL_LED (1 << 3)
325 #define FRIIO_LED_RUNNING 0x6400ff64
326 #define FRIIO_LED_STOPPED 0x96ff00ff
328 /* control PIC16F676 attached to Friio */
329 static int friio_ext_ctl(struct dvb_usb_device *d,
330 u32 sat_color, int power_on)
336 u8 power = (power_on) ? FRIIO_CTL_LNB : 0;
338 buf = kmalloc(2, GFP_KERNEL);
348 /* send 2bit header (&B10) */
349 buf[1] = power | FRIIO_CTL_LED | FRIIO_CTL_STROBE;
350 ret = i2c_transfer(&d->i2c_adap, &msg, 1);
351 buf[1] |= FRIIO_CTL_CLK;
352 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
354 buf[1] = power | FRIIO_CTL_STROBE;
355 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
356 buf[1] |= FRIIO_CTL_CLK;
357 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
359 /* send 32bit(satur, R, G, B) data in serial */
361 for (i = 0; i < 32; i++) {
362 buf[1] = power | FRIIO_CTL_STROBE;
363 if (sat_color & mask)
364 buf[1] |= FRIIO_CTL_LED;
365 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
366 buf[1] |= FRIIO_CTL_CLK;
367 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
371 /* set the strobe off */
373 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
374 buf[1] |= FRIIO_CTL_CLK;
375 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
378 return (ret == 70) ? 0 : -EREMOTEIO;
381 /* init/config of gl861 for Friio */
383 * This function cannot be moved to friio_init()/dvb_usbv2_init(),
384 * because the init defined here must be done before any activities like I2C,
385 * but friio_init() is called by dvb-usbv2 after {_frontend, _tuner}_attach(),
386 * where I2C communication is used.
387 * Thus this function is set to be called from _power_ctl().
389 * Since it will be called on the early init stage
390 * where the i2c adapter is not initialized yet,
391 * we cannot use i2c_transfer() here.
393 static int friio_reset(struct dvb_usb_device *d)
398 static const u8 friio_init_cmds[][2] = {
399 {0x33, 0x08}, {0x37, 0x40}, {0x3a, 0x1f}, {0x3b, 0xff},
400 {0x3c, 0x1f}, {0x3d, 0xff}, {0x38, 0x00}, {0x35, 0x00},
401 {0x39, 0x00}, {0x36, 0x00},
404 ret = usb_set_interface(d->udev, 0, 0);
410 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
413 usleep_range(2000, 3000);
417 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
422 * Check if the dev is really a Friio White, since it might be
423 * another device, Friio Black, with the same VID/PID.
426 usleep_range(1000, 2000);
429 ret = gl861_i2c_write_ex(d, 0x09, wbuf, 2);
433 usleep_range(2000, 3000);
434 ret = gl861_i2c_read_ex(d, 0x09, rbuf, 2);
437 if (rbuf[0] != 0xff || rbuf[1] != 0xff)
441 usleep_range(1000, 2000);
442 ret = gl861_i2c_write_ex(d, 0x48, wbuf, 2);
446 usleep_range(2000, 3000);
447 ret = gl861_i2c_read_ex(d, 0x48, rbuf, 2);
450 if (rbuf[0] != 0xff || rbuf[1] != 0xff)
455 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
461 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
467 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
471 for (i = 0; i < ARRAY_SIZE(friio_init_cmds); i++) {
472 ret = gl861_i2c_msg(d, 0x00, (u8 *)friio_init_cmds[i], 2,
481 * DVB callbacks for Friio
484 static int friio_power_ctrl(struct dvb_usb_device *d, int onoff)
486 return onoff ? friio_reset(d) : 0;
489 static int friio_frontend_attach(struct dvb_usb_adapter *adap)
491 const struct i2c_board_info *info;
492 struct dvb_usb_device *d;
493 struct tc90522_config cfg;
494 struct i2c_client *cl;
495 struct friio_priv *priv;
497 info = &friio_config.demod_info;
499 cl = dvb_module_probe("tc90522", info->type,
500 &d->i2c_adap, info->addr, &cfg);
503 adap->fe[0] = cfg.fe;
505 /* ignore cfg.tuner_i2c and create new one */
506 priv = adap_to_priv(adap);
507 priv->i2c_client_demod = cl;
508 priv->tuner_adap.algo = &friio_tuner_i2c_algo;
509 priv->tuner_adap.dev.parent = &d->udev->dev;
510 strlcpy(priv->tuner_adap.name, d->name, sizeof(priv->tuner_adap.name));
511 strlcat(priv->tuner_adap.name, "-tuner", sizeof(priv->tuner_adap.name));
512 priv->demod_sub_i2c = &priv->tuner_adap;
513 i2c_set_adapdata(&priv->tuner_adap, d);
515 return i2c_add_adapter(&priv->tuner_adap);
518 static int friio_frontend_detach(struct dvb_usb_adapter *adap)
520 struct friio_priv *priv;
522 priv = adap_to_priv(adap);
523 i2c_del_adapter(&priv->tuner_adap);
524 dvb_module_release(priv->i2c_client_demod);
528 static int friio_tuner_attach(struct dvb_usb_adapter *adap)
530 const struct i2c_board_info *info;
531 struct dvb_pll_config cfg;
532 struct i2c_client *cl;
533 struct friio_priv *priv;
535 priv = adap_to_priv(adap);
536 info = &friio_config.tuner_info;
537 cfg = friio_config.tuner_cfg;
538 cfg.fe = adap->fe[0];
540 cl = dvb_module_probe("dvb_pll", info->type,
541 priv->demod_sub_i2c, info->addr, &cfg);
544 priv->i2c_client_tuner = cl;
548 static int friio_tuner_detach(struct dvb_usb_adapter *adap)
550 struct friio_priv *priv;
552 priv = adap_to_priv(adap);
553 dvb_module_release(priv->i2c_client_tuner);
557 static int friio_init(struct dvb_usb_device *d)
561 struct friio_priv *priv;
563 static const u8 demod_init[][2] = {
564 {0x01, 0x40}, {0x04, 0x38}, {0x05, 0x40}, {0x07, 0x40},
565 {0x0f, 0x4f}, {0x11, 0x21}, {0x12, 0x0b}, {0x13, 0x2f},
566 {0x14, 0x31}, {0x16, 0x02}, {0x21, 0xc4}, {0x22, 0x20},
567 {0x2c, 0x79}, {0x2d, 0x34}, {0x2f, 0x00}, {0x30, 0x28},
568 {0x31, 0x31}, {0x32, 0xdf}, {0x38, 0x01}, {0x39, 0x78},
569 {0x3b, 0x33}, {0x3c, 0x33}, {0x48, 0x90}, {0x51, 0x68},
570 {0x5e, 0x38}, {0x71, 0x00}, {0x72, 0x08}, {0x77, 0x00},
571 {0xc0, 0x21}, {0xc1, 0x10}, {0xe4, 0x1a}, {0xea, 0x1f},
572 {0x77, 0x00}, {0x71, 0x00}, {0x71, 0x00}, {0x76, 0x0c},
576 ret = friio_ext_ctl(d, FRIIO_LED_STOPPED, true);
581 /* init/config demod */
583 for (i = 0; i < ARRAY_SIZE(demod_init); i++) {
586 ret = i2c_master_send(priv->i2c_client_demod, demod_init[i], 2);
594 static void friio_exit(struct dvb_usb_device *d)
596 friio_ext_ctl(d, FRIIO_LED_STOPPED, false);
599 static int friio_streaming_ctrl(struct dvb_frontend *fe, int onoff)
603 led_color = onoff ? FRIIO_LED_RUNNING : FRIIO_LED_STOPPED;
604 return friio_ext_ctl(fe_to_d(fe), led_color, true);
608 static struct dvb_usb_device_properties friio_props = {
609 .driver_name = KBUILD_MODNAME,
610 .owner = THIS_MODULE,
611 .adapter_nr = adapter_nr,
613 .size_of_priv = sizeof(struct friio_priv),
615 .i2c_algo = &gl861_i2c_algo,
616 .power_ctrl = friio_power_ctrl,
617 .frontend_attach = friio_frontend_attach,
618 .frontend_detach = friio_frontend_detach,
619 .tuner_attach = friio_tuner_attach,
620 .tuner_detach = friio_tuner_detach,
623 .streaming_ctrl = friio_streaming_ctrl,
628 .stream = DVB_USB_STREAM_BULK(0x01, 8, 16384),
633 static const struct usb_device_id gl861_id_table[] = {
634 { DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801,
635 &gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) },
636 { DVB_USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU,
637 &gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) },
638 { DVB_USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE,
639 &friio_props, "774 Friio White ISDB-T USB2.0", NULL) },
642 MODULE_DEVICE_TABLE(usb, gl861_id_table);
644 static struct usb_driver gl861_usb_driver = {
645 .name = KBUILD_MODNAME,
646 .id_table = gl861_id_table,
647 .probe = dvb_usbv2_probe,
648 .disconnect = dvb_usbv2_disconnect,
649 .suspend = dvb_usbv2_suspend,
650 .resume = dvb_usbv2_resume,
651 .reset_resume = dvb_usbv2_reset_resume,
656 module_usb_driver(gl861_usb_driver);
658 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
659 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
660 MODULE_VERSION("0.1");
661 MODULE_LICENSE("GPL");