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);
18 /* USB control message buffer */
21 struct i2c_adapter *demod_sub_i2c;
22 struct i2c_client *i2c_client_demod;
23 struct i2c_client *i2c_client_tuner;
26 #define CMD_WRITE_SHORT 0x01
28 #define CMD_WRITE 0x03
30 static int gl861_ctrl_msg(struct dvb_usb_device *d, u8 request, u16 value,
31 u16 index, void *data, u16 size)
33 struct gl861 *ctx = d_to_priv(d);
34 struct usb_interface *intf = d->intf;
39 mutex_lock(&d->usb_mutex);
43 memcpy(ctx->buf, data, size);
46 pipe = usb_sndctrlpipe(d->udev, 0);
47 requesttype = USB_TYPE_VENDOR | USB_DIR_OUT;
50 pipe = usb_rcvctrlpipe(d->udev, 0);
51 requesttype = USB_TYPE_VENDOR | USB_DIR_IN;
55 goto err_mutex_unlock;
58 ret = usb_control_msg(d->udev, pipe, request, requesttype, value,
59 index, ctx->buf, size, 200);
60 dev_dbg(&intf->dev, "%d | %02x %02x %*ph %*ph %*ph %s %*ph\n",
61 ret, requesttype, request, 2, &value, 2, &index, 2, &size,
62 (requesttype & USB_DIR_IN) ? "<<<" : ">>>", size, ctx->buf);
64 goto err_mutex_unlock;
66 if (request == CMD_READ)
67 memcpy(data, ctx->buf, size);
69 usleep_range(1000, 2000); /* Avoid I2C errors */
71 mutex_unlock(&d->usb_mutex);
76 mutex_unlock(&d->usb_mutex);
77 dev_dbg(&intf->dev, "failed %d\n", ret);
81 static int gl861_short_write(struct dvb_usb_device *d, u8 addr, u8 reg, u8 val)
83 return gl861_ctrl_msg(d, CMD_WRITE_SHORT,
84 (addr << 9) | val, reg, NULL, 0);
87 static int gl861_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
90 struct dvb_usb_device *d = i2c_get_adapdata(adap);
91 struct usb_interface *intf = d->intf;
92 struct gl861 *ctx = d_to_priv(d);
95 u16 value, index, size;
97 /* XXX: I2C adapter maximum data lengths are not tested */
98 if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
100 if (msg[0].len < 2 || msg[0].len > sizeof(ctx->buf)) {
105 value = (msg[0].addr << 1) << 8;
106 index = msg[0].buf[0];
108 if (msg[0].len == 2) {
109 request = CMD_WRITE_SHORT;
110 value |= msg[0].buf[1];
115 size = msg[0].len - 1;
116 data = &msg[0].buf[1];
119 ret = gl861_ctrl_msg(d, request, value, index, data, size);
120 } else if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
121 (msg[1].flags & I2C_M_RD)) {
122 /* I2C write + read */
123 if (msg[0].len > 1 || msg[1].len > sizeof(ctx->buf)) {
128 value = (msg[0].addr << 1) << 8;
129 index = msg[0].buf[0];
132 ret = gl861_ctrl_msg(d, request, value, index,
133 msg[1].buf, msg[1].len);
134 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
136 if (msg[0].len > sizeof(ctx->buf)) {
140 value = (msg[0].addr << 1) << 8;
144 ret = gl861_ctrl_msg(d, request, value, index,
145 msg[0].buf, msg[0].len);
147 /* Unsupported I2C message */
148 dev_dbg(&intf->dev, "unknown i2c msg, num %u\n", num);
156 dev_dbg(&intf->dev, "failed %d\n", ret);
160 static u32 gl861_i2c_functionality(struct i2c_adapter *adapter)
165 static struct i2c_algorithm gl861_i2c_algo = {
166 .master_xfer = gl861_i2c_master_xfer,
167 .functionality = gl861_i2c_functionality,
170 /* Callbacks for DVB USB */
171 static struct zl10353_config gl861_zl10353_config = {
172 .demod_address = 0x0f,
177 static int gl861_frontend_attach(struct dvb_usb_adapter *adap)
180 adap->fe[0] = dvb_attach(zl10353_attach, &gl861_zl10353_config,
181 &adap_to_d(adap)->i2c_adap);
182 if (adap->fe[0] == NULL)
188 static struct qt1010_config gl861_qt1010_config = {
192 static int gl861_tuner_attach(struct dvb_usb_adapter *adap)
194 return dvb_attach(qt1010_attach,
195 adap->fe[0], &adap_to_d(adap)->i2c_adap,
196 &gl861_qt1010_config) == NULL ? -ENODEV : 0;
199 static int gl861_init(struct dvb_usb_device *d)
202 * There is 2 interfaces. Interface 0 is for TV and interface 1 is
203 * for HID remote controller. Interface 0 has 2 alternate settings.
204 * For some reason we need to set interface explicitly, defaulted
205 * as alternate setting 1?
207 return usb_set_interface(d->udev, 0, 0);
210 /* DVB USB Driver stuff */
211 static struct dvb_usb_device_properties gl861_props = {
212 .driver_name = KBUILD_MODNAME,
213 .owner = THIS_MODULE,
214 .adapter_nr = adapter_nr,
216 .size_of_priv = sizeof(struct gl861),
218 .i2c_algo = &gl861_i2c_algo,
219 .frontend_attach = gl861_frontend_attach,
220 .tuner_attach = gl861_tuner_attach,
226 .stream = DVB_USB_STREAM_BULK(0x81, 7, 512),
235 struct friio_config {
236 struct i2c_board_info demod_info;
237 struct tc90522_config demod_cfg;
239 struct i2c_board_info tuner_info;
240 struct dvb_pll_config tuner_cfg;
243 static const struct friio_config friio_config = {
244 .demod_info = { I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x18), },
245 .tuner_info = { I2C_BOARD_INFO("tua6034_friio", 0x60), },
249 /* GPIO control in Friio */
251 #define FRIIO_CTL_LNB (1 << 0)
252 #define FRIIO_CTL_STROBE (1 << 1)
253 #define FRIIO_CTL_CLK (1 << 2)
254 #define FRIIO_CTL_LED (1 << 3)
256 #define FRIIO_LED_RUNNING 0x6400ff64
257 #define FRIIO_LED_STOPPED 0x96ff00ff
259 /* control PIC16F676 attached to Friio */
260 static int friio_ext_ctl(struct dvb_usb_device *d,
261 u32 sat_color, int power_on)
267 u8 power = (power_on) ? FRIIO_CTL_LNB : 0;
269 buf = kmalloc(2, GFP_KERNEL);
279 /* send 2bit header (&B10) */
280 buf[1] = power | FRIIO_CTL_LED | FRIIO_CTL_STROBE;
281 ret = i2c_transfer(&d->i2c_adap, &msg, 1);
282 buf[1] |= FRIIO_CTL_CLK;
283 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
285 buf[1] = power | FRIIO_CTL_STROBE;
286 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
287 buf[1] |= FRIIO_CTL_CLK;
288 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
290 /* send 32bit(satur, R, G, B) data in serial */
292 for (i = 0; i < 32; i++) {
293 buf[1] = power | FRIIO_CTL_STROBE;
294 if (sat_color & mask)
295 buf[1] |= FRIIO_CTL_LED;
296 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
297 buf[1] |= FRIIO_CTL_CLK;
298 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
302 /* set the strobe off */
304 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
305 buf[1] |= FRIIO_CTL_CLK;
306 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
309 return (ret == 70) ? 0 : -EREMOTEIO;
312 /* init/config of gl861 for Friio */
314 * This function cannot be moved to friio_init()/dvb_usbv2_init(),
315 * because the init defined here includes a whole device reset,
316 * it must be run early before any activities like I2C,
317 * but friio_init() is called by dvb-usbv2 after {_frontend, _tuner}_attach(),
318 * where I2C communication is used.
319 * In addition, this reset is required in reset_resume() as well.
320 * Thus this function is set to be called from _power_ctl().
322 * Since it will be called on the early init stage
323 * where the i2c adapter is not initialized yet,
324 * we cannot use i2c_transfer() here.
326 static int friio_reset(struct dvb_usb_device *d)
331 static const u8 friio_init_cmds[][2] = {
332 {0x33, 0x08}, {0x37, 0x40}, {0x3a, 0x1f}, {0x3b, 0xff},
333 {0x3c, 0x1f}, {0x3d, 0xff}, {0x38, 0x00}, {0x35, 0x00},
334 {0x39, 0x00}, {0x36, 0x00},
337 ret = usb_set_interface(d->udev, 0, 0);
341 ret = gl861_short_write(d, 0x00, 0x11, 0x02);
344 usleep_range(2000, 3000);
346 ret = gl861_short_write(d, 0x00, 0x11, 0x00);
351 * Check if the dev is really a Friio White, since it might be
352 * another device, Friio Black, with the same VID/PID.
355 usleep_range(1000, 2000);
357 ret = gl861_ctrl_msg(d, CMD_WRITE, 0x09 << 9, 0x03, wbuf, 1);
361 usleep_range(2000, 3000);
362 ret = gl861_ctrl_msg(d, CMD_READ, 0x09 << 9, 0x0100, rbuf, 2);
365 if (rbuf[0] != 0xff || rbuf[1] != 0xff)
369 usleep_range(1000, 2000);
371 ret = gl861_ctrl_msg(d, CMD_WRITE, 0x48 << 9, 0x03, wbuf, 1);
375 usleep_range(2000, 3000);
376 ret = gl861_ctrl_msg(d, CMD_READ, 0x48 << 9, 0x0100, rbuf, 2);
379 if (rbuf[0] != 0xff || rbuf[1] != 0xff)
382 ret = gl861_short_write(d, 0x00, 0x30, 0x04);
386 ret = gl861_short_write(d, 0x00, 0x00, 0x01);
390 ret = gl861_short_write(d, 0x00, 0x06, 0x0f);
394 for (i = 0; i < ARRAY_SIZE(friio_init_cmds); i++) {
395 ret = gl861_short_write(d, 0x00, friio_init_cmds[i][0],
396 friio_init_cmds[i][1]);
404 * DVB callbacks for Friio
407 static int friio_power_ctrl(struct dvb_usb_device *d, int onoff)
409 return onoff ? friio_reset(d) : 0;
412 static int friio_frontend_attach(struct dvb_usb_adapter *adap)
414 const struct i2c_board_info *info;
415 struct dvb_usb_device *d;
416 struct tc90522_config cfg;
417 struct i2c_client *cl;
420 info = &friio_config.demod_info;
421 cfg = friio_config.demod_cfg;
423 cl = dvb_module_probe("tc90522", info->type,
424 &d->i2c_adap, info->addr, &cfg);
427 adap->fe[0] = cfg.fe;
429 priv = adap_to_priv(adap);
430 priv->i2c_client_demod = cl;
431 priv->demod_sub_i2c = cfg.tuner_i2c;
435 static int friio_frontend_detach(struct dvb_usb_adapter *adap)
439 priv = adap_to_priv(adap);
440 dvb_module_release(priv->i2c_client_demod);
444 static int friio_tuner_attach(struct dvb_usb_adapter *adap)
446 const struct i2c_board_info *info;
447 struct dvb_pll_config cfg;
448 struct i2c_client *cl;
451 priv = adap_to_priv(adap);
452 info = &friio_config.tuner_info;
453 cfg = friio_config.tuner_cfg;
454 cfg.fe = adap->fe[0];
456 cl = dvb_module_probe("dvb_pll", info->type,
457 priv->demod_sub_i2c, info->addr, &cfg);
460 priv->i2c_client_tuner = cl;
464 static int friio_tuner_detach(struct dvb_usb_adapter *adap)
468 priv = adap_to_priv(adap);
469 dvb_module_release(priv->i2c_client_tuner);
473 static int friio_init(struct dvb_usb_device *d)
479 static const u8 demod_init[][2] = {
480 {0x01, 0x40}, {0x04, 0x38}, {0x05, 0x40}, {0x07, 0x40},
481 {0x0f, 0x4f}, {0x11, 0x21}, {0x12, 0x0b}, {0x13, 0x2f},
482 {0x14, 0x31}, {0x16, 0x02}, {0x21, 0xc4}, {0x22, 0x20},
483 {0x2c, 0x79}, {0x2d, 0x34}, {0x2f, 0x00}, {0x30, 0x28},
484 {0x31, 0x31}, {0x32, 0xdf}, {0x38, 0x01}, {0x39, 0x78},
485 {0x3b, 0x33}, {0x3c, 0x33}, {0x48, 0x90}, {0x51, 0x68},
486 {0x5e, 0x38}, {0x71, 0x00}, {0x72, 0x08}, {0x77, 0x00},
487 {0xc0, 0x21}, {0xc1, 0x10}, {0xe4, 0x1a}, {0xea, 0x1f},
488 {0x77, 0x00}, {0x71, 0x00}, {0x71, 0x00}, {0x76, 0x0c},
492 ret = friio_ext_ctl(d, FRIIO_LED_STOPPED, true);
497 /* init/config demod */
499 for (i = 0; i < ARRAY_SIZE(demod_init); i++) {
502 ret = i2c_master_send(priv->i2c_client_demod, demod_init[i], 2);
510 static void friio_exit(struct dvb_usb_device *d)
512 friio_ext_ctl(d, FRIIO_LED_STOPPED, false);
515 static int friio_streaming_ctrl(struct dvb_frontend *fe, int onoff)
519 led_color = onoff ? FRIIO_LED_RUNNING : FRIIO_LED_STOPPED;
520 return friio_ext_ctl(fe_to_d(fe), led_color, true);
524 static struct dvb_usb_device_properties friio_props = {
525 .driver_name = KBUILD_MODNAME,
526 .owner = THIS_MODULE,
527 .adapter_nr = adapter_nr,
529 .size_of_priv = sizeof(struct gl861),
531 .i2c_algo = &gl861_i2c_algo,
532 .power_ctrl = friio_power_ctrl,
533 .frontend_attach = friio_frontend_attach,
534 .frontend_detach = friio_frontend_detach,
535 .tuner_attach = friio_tuner_attach,
536 .tuner_detach = friio_tuner_detach,
539 .streaming_ctrl = friio_streaming_ctrl,
544 .stream = DVB_USB_STREAM_BULK(0x01, 8, 16384),
549 static const struct usb_device_id gl861_id_table[] = {
550 { DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801,
551 &gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) },
552 { DVB_USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU,
553 &gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) },
554 { DVB_USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE,
555 &friio_props, "774 Friio White ISDB-T USB2.0", NULL) },
558 MODULE_DEVICE_TABLE(usb, gl861_id_table);
560 static struct usb_driver gl861_usb_driver = {
561 .name = KBUILD_MODNAME,
562 .id_table = gl861_id_table,
563 .probe = dvb_usbv2_probe,
564 .disconnect = dvb_usbv2_disconnect,
565 .suspend = dvb_usbv2_suspend,
566 .resume = dvb_usbv2_resume,
567 .reset_resume = dvb_usbv2_reset_resume,
572 module_usb_driver(gl861_usb_driver);
574 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
575 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
576 MODULE_VERSION("0.1");
577 MODULE_LICENSE("GPL");