1 /* DVB USB compliant Linux driver for the Friio USB2.0 ISDB-T receiver.
3 * Copyright (C) 2009 Akihiro Tsukada <tskd2@yahoo.co.jp>
5 * This module is based off the the gl861 and vp702x modules.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation, version 2.
11 * see Documentation/dvb/README.dvb-usb for more information
16 int dvb_usb_friio_debug;
17 module_param_named(debug, dvb_usb_friio_debug, int, 0644);
18 MODULE_PARM_DESC(debug,
19 "set debugging level (1=info,2=xfer,4=rc,8=fe (or-able))."
20 DVB_USB_DEBUG_STATUS);
22 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
25 * Indirect I2C access to the PLL via FE.
26 * whole I2C protocol data to the PLL is sent via the FE's I2C register.
27 * This is done by a control msg to the FE with the I2C data accompanied, and
28 * a specific USB request number is assigned for that purpose.
30 * this func sends wbuf[1..] to the I2C register wbuf[0] at addr (= at FE).
31 * TODO: refoctored, smarter i2c functions.
33 static int gl861_i2c_ctrlmsg_data(struct dvb_usb_device *d, u8 addr,
34 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
36 u16 index = wbuf[0]; /* must be JDVBT90502_2ND_I2C_REG(=0xFE) */
37 u16 value = addr << (8 + 1);
38 int wo = (rbuf == NULL || rlen == 0); /* write only */
41 deb_xfer("write to PLL:0x%02x via FE reg:0x%02x, len:%d\n",
42 wbuf[1], wbuf[0], wlen - 1);
44 if (wo && wlen >= 2) {
45 req = GL861_REQ_I2C_DATA_CTRL_WRITE;
48 return usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
49 req, type, value, index,
50 &wbuf[1], wlen - 1, 2000);
53 deb_xfer("not supported ctrl-msg, aborting.");
57 /* normal I2C access (without extra data arguments).
58 * write to the register wbuf[0] at I2C address addr with the value wbuf[1],
59 * or read from the register wbuf[0].
60 * register address can be 16bit (wbuf[2]<<8 | wbuf[0]) if wlen==3
62 static int gl861_i2c_msg(struct dvb_usb_device *d, u8 addr,
63 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
66 u16 value = addr << (8 + 1);
67 int wo = (rbuf == NULL || rlen == 0); /* write-only */
71 /* special case for the indirect I2C access to the PLL via FE, */
72 if (addr == friio_fe_config.demod_address &&
73 wbuf[0] == JDVBT90502_2ND_I2C_REG)
74 return gl861_i2c_ctrlmsg_data(d, addr, wbuf, wlen, rbuf, rlen);
77 req = GL861_REQ_I2C_WRITE;
79 pipe = usb_sndctrlpipe(d->udev, 0);
81 req = GL861_REQ_I2C_READ;
83 pipe = usb_rcvctrlpipe(d->udev, 0);
92 value = value + wbuf[1];
95 /* special case for 16bit register-address */
96 index = (wbuf[2] << 8) | wbuf[0];
97 value = value + wbuf[1];
100 deb_xfer("wlen = %x, aborting.", wlen);
104 return usb_control_msg(d->udev, pipe, req, type,
105 value, index, rbuf, rlen, 2000);
109 static int gl861_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
112 struct dvb_usb_device *d = i2c_get_adapdata(adap);
119 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
122 for (i = 0; i < num; i++) {
123 /* write/read request */
124 if (i + 1 < num && (msg[i + 1].flags & I2C_M_RD)) {
125 if (gl861_i2c_msg(d, msg[i].addr,
126 msg[i].buf, msg[i].len,
127 msg[i + 1].buf, msg[i + 1].len) < 0)
131 if (gl861_i2c_msg(d, msg[i].addr, msg[i].buf,
132 msg[i].len, NULL, 0) < 0)
136 mutex_unlock(&d->i2c_mutex);
140 static u32 gl861_i2c_func(struct i2c_adapter *adapter)
146 static int friio_ext_ctl(struct dvb_usb_adapter *adap,
147 u32 sat_color, int lnb_on)
154 u8 lnb = (lnb_on) ? FRIIO_CTL_LNB : 0;
163 /* send 2bit header (&B10) */
164 buf[1] = lnb | FRIIO_CTL_LED | FRIIO_CTL_STROBE;
165 ret = gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
166 buf[1] |= FRIIO_CTL_CLK;
167 ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
169 buf[1] = lnb | FRIIO_CTL_STROBE;
170 ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
171 buf[1] |= FRIIO_CTL_CLK;
172 ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
174 /* send 32bit(satur, R, G, B) data in serial */
176 for (i = 0; i < 32; i++) {
177 buf[1] = lnb | FRIIO_CTL_STROBE;
178 if (sat_color & mask)
179 buf[1] |= FRIIO_CTL_LED;
180 ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
181 buf[1] |= FRIIO_CTL_CLK;
182 ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
186 /* set the strobe off */
188 ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
189 buf[1] |= FRIIO_CTL_CLK;
190 ret += gl861_i2c_xfer(&adap->dev->i2c_adap, &msg, 1);
196 static int friio_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff);
198 /* TODO: move these init cmds to the FE's init routine? */
199 static u8 streaming_init_cmds[][2] = {
211 static int cmdlen = sizeof(streaming_init_cmds) / 2;
214 * Command sequence in this init function is a replay
215 * of the captured USB commands from the Windows proprietary driver.
217 static int friio_initialize(struct dvb_usb_device *d)
225 deb_info("%s called.\n", __func__);
227 /* use gl861_i2c_msg instead of gl861_i2c_xfer(), */
228 /* because the i2c device is not set up yet. */
231 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
238 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
243 /* following msgs should be in the FE's init code? */
244 /* cmd sequence to identify the device type? (friio black/white) */
247 /* can't use gl861_i2c_cmd, as the register-addr is 16bit(0x0100) */
248 ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
249 GL861_REQ_I2C_DATA_CTRL_WRITE, GL861_WRITE,
250 0x1200, 0x0100, wbuf, 2, 2000);
256 wbuf[2] = 0x01; /* reg.0x0100 */
258 ret = gl861_i2c_msg(d, 0x12 >> 1, wbuf, 3, rbuf, 2);
259 /* my Friio White returns 0xffff. */
260 if (ret < 0 || rbuf[0] != 0xff || rbuf[1] != 0xff)
266 ret = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev, 0),
267 GL861_REQ_I2C_DATA_CTRL_WRITE, GL861_WRITE,
268 0x9000, 0x0100, wbuf, 2, 2000);
274 wbuf[2] = 0x01; /* reg.0x0100 */
276 ret = gl861_i2c_msg(d, 0x90 >> 1, wbuf, 3, rbuf, 2);
277 /* my Friio White returns 0xffff again. */
278 if (ret < 0 || rbuf[0] != 0xff || rbuf[1] != 0xff)
284 /* ============ start DEMOD init cmds ================== */
285 /* read PLL status to clear the POR bit */
286 wbuf[0] = JDVBT90502_2ND_I2C_REG;
287 wbuf[1] = (FRIIO_PLL_ADDR << 1) + 1; /* +1 for reading */
288 ret = gl861_i2c_msg(d, FRIIO_DEMOD_ADDR, wbuf, 2, NULL, 0);
293 /* note: DEMODULATOR has 16bit register-address. */
295 wbuf[2] = 0x01; /* reg addr: 0x0100 */
296 wbuf[1] = 0x00; /* val: not used */
297 ret = gl861_i2c_msg(d, FRIIO_DEMOD_ADDR, wbuf, 3, rbuf, 1);
304 ret = gl861_i2c_msg(d, FRIIO_DEMOD_ADDR, wbuf, 2, rbuf, 1);
308 if (rbuf[0] & 0x80) { /* still in PowerOnReset state? */
310 deb_info("failed to get the correct"
311 " FE demod status:0x%02x\n", rbuf[0]);
318 /* TODO: check return value in rbuf */
319 /* =========== end DEMOD init cmds ===================== */
324 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
329 /* following 2 cmds unnecessary? */
332 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
338 ret = gl861_i2c_msg(d, 0x00, wbuf, 2, NULL, 0);
342 /* some streaming ctl cmds (maybe) */
344 for (i = 0; i < cmdlen; i++) {
345 ret = gl861_i2c_msg(d, 0x00, streaming_init_cmds[i], 2,
353 /* change the LED color etc. */
354 ret = friio_streaming_ctrl(&d->adapter[0], 0);
361 deb_info("%s:ret == %d\n", __func__, ret);
365 /* Callbacks for DVB USB */
367 static int friio_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
371 deb_info("%s called.(%d)\n", __func__, onoff);
373 /* set the LED color and saturation (and LNB on) */
375 ret = friio_ext_ctl(adap, 0x6400ff64, 1);
377 ret = friio_ext_ctl(adap, 0x96ff00ff, 1);
380 deb_info("%s failed to send cmdx. ret==%d\n", __func__, ret);
386 static int friio_frontend_attach(struct dvb_usb_adapter *adap)
388 if (friio_initialize(adap->dev) < 0)
391 adap->fe = jdvbt90502_attach(adap->dev);
392 if (adap->fe == NULL)
398 /* DVB USB Driver stuff */
399 static struct dvb_usb_device_properties friio_properties;
401 static int friio_probe(struct usb_interface *intf,
402 const struct usb_device_id *id)
404 struct dvb_usb_device *d;
405 struct usb_host_interface *alt;
408 if (intf->num_altsetting < GL861_ALTSETTING_COUNT)
411 alt = usb_altnum_to_altsetting(intf, FRIIO_BULK_ALTSETTING);
413 deb_rc("not alt found!\n");
416 ret = usb_set_interface(interface_to_usbdev(intf),
417 alt->desc.bInterfaceNumber,
418 alt->desc.bAlternateSetting);
420 deb_rc("failed to set alt-setting!\n");
424 ret = dvb_usb_device_init(intf, &friio_properties,
425 THIS_MODULE, &d, adapter_nr);
427 friio_streaming_ctrl(&d->adapter[0], 1);
433 struct jdvbt90502_config friio_fe_config = {
434 .demod_address = FRIIO_DEMOD_ADDR,
435 .pll_address = FRIIO_PLL_ADDR,
438 static struct i2c_algorithm gl861_i2c_algo = {
439 .master_xfer = gl861_i2c_xfer,
440 .functionality = gl861_i2c_func,
443 static struct usb_device_id friio_table[] = {
444 { USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE) },
445 { } /* Terminating entry */
447 MODULE_DEVICE_TABLE(usb, friio_table);
450 static struct dvb_usb_device_properties friio_properties = {
451 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
452 .usb_ctrl = DEVICE_SPECIFIC,
458 /* caps:0 => no pid filter, 188B TS packet */
459 /* GL861 has a HW pid filter, but no info available. */
463 .frontend_attach = friio_frontend_attach,
464 .streaming_ctrl = friio_streaming_ctrl,
468 /* count <= MAX_NO_URBS_FOR_DATA_STREAM(10) */
472 /* GL861 has 6KB buf inside */
480 .i2c_algo = &gl861_i2c_algo,
482 .num_device_descs = 1,
485 .name = "774 Friio ISDB-T USB2.0",
486 .cold_ids = { NULL },
487 .warm_ids = { &friio_table[0], NULL },
492 static struct usb_driver friio_driver = {
493 .name = "dvb_usb_friio",
494 .probe = friio_probe,
495 .disconnect = dvb_usb_device_exit,
496 .id_table = friio_table,
501 static int __init friio_module_init(void)
505 ret = usb_register(&friio_driver);
507 err("usb_register failed. Error number %d", ret);
513 static void __exit friio_module_exit(void)
515 /* deregister this driver from the USB subsystem */
516 usb_deregister(&friio_driver);
519 module_init(friio_module_init);
520 module_exit(friio_module_exit);
522 MODULE_AUTHOR("Akihiro Tsukada <tskd2@yahoo.co.jp>");
523 MODULE_DESCRIPTION("Driver for Friio ISDB-T USB2.0 Receiver");
524 MODULE_VERSION("0.2");
525 MODULE_LICENSE("GPL");