GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / media / usb / dvb-usb / cxusb.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB compliant linux driver for Conexant USB reference design.
3  *
4  * The Conexant reference design I saw on their website was only for analogue
5  * capturing (using the cx25842). The box I took to write this driver (reverse
6  * engineered) is the one labeled Medion MD95700. In addition to the cx25842
7  * for analogue capturing it also has a cx22702 DVB-T demodulator on the main
8  * board. Besides it has a atiremote (X10) and a USB2.0 hub onboard.
9  *
10  * Maybe it is a little bit premature to call this driver cxusb, but I assume
11  * the USB protocol is identical or at least inherited from the reference
12  * design, so it can be reused for the "analogue-only" device (if it will
13  * appear at all).
14  *
15  *
16  * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@posteo.de)
17  * Copyright (C) 2006 Michael Krufky (mkrufky@linuxtv.org)
18  * Copyright (C) 2006, 2007 Chris Pascoe (c.pascoe@itee.uq.edu.au)
19  * Copyright (C) 2011, 2017 Maciej S. Szmigiero (mail@maciej.szmigiero.name)
20  *
21  * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
22  */
23 #include <media/tuner.h>
24 #include <linux/delay.h>
25 #include <linux/device.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/string.h>
29 #include <linux/vmalloc.h>
30
31 #include "cxusb.h"
32
33 #include "cx22702.h"
34 #include "lgdt330x.h"
35 #include "mt352.h"
36 #include "mt352_priv.h"
37 #include "zl10353.h"
38 #include "xc2028.h"
39 #include "tuner-simple.h"
40 #include "mxl5005s.h"
41 #include "max2165.h"
42 #include "dib7000p.h"
43 #include "dib0070.h"
44 #include "lgs8gxx.h"
45 #include "atbm8830.h"
46 #include "si2168.h"
47 #include "si2157.h"
48
49 /* debug */
50 int dvb_usb_cxusb_debug;
51 module_param_named(debug, dvb_usb_cxusb_debug, int, 0644);
52 MODULE_PARM_DESC(debug, "set debugging level (see cxusb.h)."
53                  DVB_USB_DEBUG_STATUS);
54
55 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
56
57 enum cxusb_table_index {
58         MEDION_MD95700,
59         DVICO_BLUEBIRD_LG064F_COLD,
60         DVICO_BLUEBIRD_LG064F_WARM,
61         DVICO_BLUEBIRD_DUAL_1_COLD,
62         DVICO_BLUEBIRD_DUAL_1_WARM,
63         DVICO_BLUEBIRD_LGZ201_COLD,
64         DVICO_BLUEBIRD_LGZ201_WARM,
65         DVICO_BLUEBIRD_TH7579_COLD,
66         DVICO_BLUEBIRD_TH7579_WARM,
67         DIGITALNOW_BLUEBIRD_DUAL_1_COLD,
68         DIGITALNOW_BLUEBIRD_DUAL_1_WARM,
69         DVICO_BLUEBIRD_DUAL_2_COLD,
70         DVICO_BLUEBIRD_DUAL_2_WARM,
71         DVICO_BLUEBIRD_DUAL_4,
72         DVICO_BLUEBIRD_DVB_T_NANO_2,
73         DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM,
74         AVERMEDIA_VOLAR_A868R,
75         DVICO_BLUEBIRD_DUAL_4_REV_2,
76         CONEXANT_D680_DMB,
77         MYGICA_D689,
78         NR__cxusb_table_index
79 };
80
81 static struct usb_device_id cxusb_table[];
82
83 int cxusb_ctrl_msg(struct dvb_usb_device *d,
84                    u8 cmd, const u8 *wbuf, int wlen, u8 *rbuf, int rlen)
85 {
86         struct cxusb_state *st = d->priv;
87         int ret;
88
89         if (1 + wlen > MAX_XFER_SIZE) {
90                 warn("i2c wr: len=%d is too big!\n", wlen);
91                 return -EOPNOTSUPP;
92         }
93
94         if (rlen > MAX_XFER_SIZE) {
95                 warn("i2c rd: len=%d is too big!\n", rlen);
96                 return -EOPNOTSUPP;
97         }
98
99         mutex_lock(&d->data_mutex);
100         st->data[0] = cmd;
101         memcpy(&st->data[1], wbuf, wlen);
102         ret = dvb_usb_generic_rw(d, st->data, 1 + wlen, st->data, rlen, 0);
103         if (!ret && rbuf && rlen)
104                 memcpy(rbuf, st->data, rlen);
105
106         mutex_unlock(&d->data_mutex);
107         return ret;
108 }
109
110 /* GPIO */
111 static void cxusb_gpio_tuner(struct dvb_usb_device *d, int onoff)
112 {
113         struct cxusb_state *st = d->priv;
114         u8 o[2], i;
115
116         if (st->gpio_write_state[GPIO_TUNER] == onoff &&
117             !st->gpio_write_refresh[GPIO_TUNER])
118                 return;
119
120         o[0] = GPIO_TUNER;
121         o[1] = onoff;
122         cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
123
124         if (i != 0x01)
125                 dev_info(&d->udev->dev, "gpio_write failed.\n");
126
127         st->gpio_write_state[GPIO_TUNER] = onoff;
128         st->gpio_write_refresh[GPIO_TUNER] = false;
129 }
130
131 static int cxusb_bluebird_gpio_rw(struct dvb_usb_device *d, u8 changemask,
132                                   u8 newval)
133 {
134         u8 o[2], gpio_state;
135         int rc;
136
137         o[0] = 0xff & ~changemask;      /* mask of bits to keep */
138         o[1] = newval & changemask;     /* new values for bits  */
139
140         rc = cxusb_ctrl_msg(d, CMD_BLUEBIRD_GPIO_RW, o, 2, &gpio_state, 1);
141         if (rc < 0 || (gpio_state & changemask) != (newval & changemask))
142                 dev_info(&d->udev->dev, "bluebird_gpio_write failed.\n");
143
144         return rc < 0 ? rc : gpio_state;
145 }
146
147 static void cxusb_bluebird_gpio_pulse(struct dvb_usb_device *d, u8 pin, int low)
148 {
149         cxusb_bluebird_gpio_rw(d, pin, low ? 0 : pin);
150         msleep(5);
151         cxusb_bluebird_gpio_rw(d, pin, low ? pin : 0);
152 }
153
154 static void cxusb_nano2_led(struct dvb_usb_device *d, int onoff)
155 {
156         cxusb_bluebird_gpio_rw(d, 0x40, onoff ? 0 : 0x40);
157 }
158
159 static int cxusb_d680_dmb_gpio_tuner(struct dvb_usb_device *d,
160                                      u8 addr, int onoff)
161 {
162         u8  o[2] = {addr, onoff};
163         u8  i;
164         int rc;
165
166         rc = cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
167
168         if (rc < 0)
169                 return rc;
170
171         if (i == 0x01)
172                 return 0;
173
174         dev_info(&d->udev->dev, "gpio_write failed.\n");
175         return -EIO;
176 }
177
178 /* I2C */
179 static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
180                           int num)
181 {
182         struct dvb_usb_device *d = i2c_get_adapdata(adap);
183         int ret;
184         int i;
185
186         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
187                 return -EAGAIN;
188
189         for (i = 0; i < num; i++) {
190                 if (le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_MEDION)
191                         switch (msg[i].addr) {
192                         case 0x63:
193                                 cxusb_gpio_tuner(d, 0);
194                                 break;
195                         default:
196                                 cxusb_gpio_tuner(d, 1);
197                                 break;
198                         }
199
200                 if (msg[i].flags & I2C_M_RD) {
201                         /* read only */
202                         u8 obuf[3], ibuf[MAX_XFER_SIZE];
203
204                         if (1 + msg[i].len > sizeof(ibuf)) {
205                                 warn("i2c rd: len=%d is too big!\n",
206                                      msg[i].len);
207                                 ret = -EOPNOTSUPP;
208                                 goto unlock;
209                         }
210                         obuf[0] = 0;
211                         obuf[1] = msg[i].len;
212                         obuf[2] = msg[i].addr;
213                         if (cxusb_ctrl_msg(d, CMD_I2C_READ,
214                                            obuf, 3,
215                                            ibuf, 1 + msg[i].len) < 0) {
216                                 warn("i2c read failed");
217                                 break;
218                         }
219                         memcpy(msg[i].buf, &ibuf[1], msg[i].len);
220                 } else if (i + 1 < num && (msg[i + 1].flags & I2C_M_RD) &&
221                            msg[i].addr == msg[i + 1].addr) {
222                         /* write to then read from same address */
223                         u8 obuf[MAX_XFER_SIZE], ibuf[MAX_XFER_SIZE];
224
225                         if (3 + msg[i].len > sizeof(obuf)) {
226                                 warn("i2c wr: len=%d is too big!\n",
227                                      msg[i].len);
228                                 ret = -EOPNOTSUPP;
229                                 goto unlock;
230                         }
231                         if (1 + msg[i + 1].len > sizeof(ibuf)) {
232                                 warn("i2c rd: len=%d is too big!\n",
233                                      msg[i + 1].len);
234                                 ret = -EOPNOTSUPP;
235                                 goto unlock;
236                         }
237                         obuf[0] = msg[i].len;
238                         obuf[1] = msg[i + 1].len;
239                         obuf[2] = msg[i].addr;
240                         memcpy(&obuf[3], msg[i].buf, msg[i].len);
241
242                         if (cxusb_ctrl_msg(d, CMD_I2C_READ,
243                                            obuf, 3 + msg[i].len,
244                                            ibuf, 1 + msg[i + 1].len) < 0)
245                                 break;
246
247                         if (ibuf[0] != 0x08)
248                                 dev_info(&d->udev->dev, "i2c read may have failed\n");
249
250                         memcpy(msg[i + 1].buf, &ibuf[1], msg[i + 1].len);
251
252                         i++;
253                 } else {
254                         /* write only */
255                         u8 obuf[MAX_XFER_SIZE], ibuf;
256
257                         if (2 + msg[i].len > sizeof(obuf)) {
258                                 warn("i2c wr: len=%d is too big!\n",
259                                      msg[i].len);
260                                 ret = -EOPNOTSUPP;
261                                 goto unlock;
262                         }
263                         obuf[0] = msg[i].addr;
264                         obuf[1] = msg[i].len;
265                         memcpy(&obuf[2], msg[i].buf, msg[i].len);
266
267                         if (cxusb_ctrl_msg(d, CMD_I2C_WRITE, obuf,
268                                            2 + msg[i].len, &ibuf, 1) < 0)
269                                 break;
270                         if (ibuf != 0x08)
271                                 dev_info(&d->udev->dev, "i2c write may have failed\n");
272                 }
273         }
274
275         if (i == num)
276                 ret = num;
277         else
278                 ret = -EREMOTEIO;
279
280 unlock:
281         mutex_unlock(&d->i2c_mutex);
282         return ret;
283 }
284
285 static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
286 {
287         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
288 }
289
290 static struct i2c_algorithm cxusb_i2c_algo = {
291         .master_xfer   = cxusb_i2c_xfer,
292         .functionality = cxusb_i2c_func,
293 };
294
295 static int _cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
296 {
297         u8 b = 0;
298
299         dev_info(&d->udev->dev, "setting power %s\n", onoff ? "ON" : "OFF");
300
301         if (onoff)
302                 return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
303         else
304                 return cxusb_ctrl_msg(d, CMD_POWER_OFF, &b, 1, NULL, 0);
305 }
306
307 static int cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
308 {
309         bool is_medion = d->props.devices[0].warm_ids[0] == &cxusb_table[MEDION_MD95700];
310         int ret;
311
312         if (is_medion && !onoff) {
313                 struct cxusb_medion_dev *cxdev = d->priv;
314
315                 mutex_lock(&cxdev->open_lock);
316
317                 if (cxdev->open_type == CXUSB_OPEN_ANALOG) {
318                         dev_info(&d->udev->dev, "preventing DVB core from setting power OFF while we are in analog mode\n");
319                         ret = -EBUSY;
320                         goto ret_unlock;
321                 }
322         }
323
324         ret = _cxusb_power_ctrl(d, onoff);
325
326 ret_unlock:
327         if (is_medion && !onoff) {
328                 struct cxusb_medion_dev *cxdev = d->priv;
329
330                 mutex_unlock(&cxdev->open_lock);
331         }
332
333         return ret;
334 }
335
336 static int cxusb_aver_power_ctrl(struct dvb_usb_device *d, int onoff)
337 {
338         int ret;
339
340         if (!onoff)
341                 return cxusb_ctrl_msg(d, CMD_POWER_OFF, NULL, 0, NULL, 0);
342         if (d->state == DVB_USB_STATE_INIT &&
343             usb_set_interface(d->udev, 0, 0) < 0)
344                 err("set interface failed");
345         do {
346                 /* Nothing */
347         } while (!(ret = cxusb_ctrl_msg(d, CMD_POWER_ON, NULL, 0, NULL, 0)) &&
348                  !(ret = cxusb_ctrl_msg(d, 0x15, NULL, 0, NULL, 0)) &&
349                  !(ret = cxusb_ctrl_msg(d, 0x17, NULL, 0, NULL, 0)) && 0);
350
351         if (!ret) {
352                 /*
353                  * FIXME: We don't know why, but we need to configure the
354                  * lgdt3303 with the register settings below on resume
355                  */
356                 int i;
357                 u8 buf;
358                 static const u8 bufs[] = {
359                         0x0e, 0x2, 0x00, 0x7f,
360                         0x0e, 0x2, 0x02, 0xfe,
361                         0x0e, 0x2, 0x02, 0x01,
362                         0x0e, 0x2, 0x00, 0x03,
363                         0x0e, 0x2, 0x0d, 0x40,
364                         0x0e, 0x2, 0x0e, 0x87,
365                         0x0e, 0x2, 0x0f, 0x8e,
366                         0x0e, 0x2, 0x10, 0x01,
367                         0x0e, 0x2, 0x14, 0xd7,
368                         0x0e, 0x2, 0x47, 0x88,
369                 };
370                 msleep(20);
371                 for (i = 0; i < ARRAY_SIZE(bufs); i += 4 / sizeof(u8)) {
372                         ret = cxusb_ctrl_msg(d, CMD_I2C_WRITE,
373                                              bufs + i, 4, &buf, 1);
374                         if (ret)
375                                 break;
376                         if (buf != 0x8)
377                                 return -EREMOTEIO;
378                 }
379         }
380         return ret;
381 }
382
383 static int cxusb_bluebird_power_ctrl(struct dvb_usb_device *d, int onoff)
384 {
385         u8 b = 0;
386
387         if (onoff)
388                 return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
389         else
390                 return 0;
391 }
392
393 static int cxusb_nano2_power_ctrl(struct dvb_usb_device *d, int onoff)
394 {
395         int rc = 0;
396
397         rc = cxusb_power_ctrl(d, onoff);
398         if (!onoff)
399                 cxusb_nano2_led(d, 0);
400
401         return rc;
402 }
403
404 static int cxusb_d680_dmb_power_ctrl(struct dvb_usb_device *d, int onoff)
405 {
406         int ret;
407         u8  b;
408
409         ret = cxusb_power_ctrl(d, onoff);
410         if (!onoff)
411                 return ret;
412
413         msleep(128);
414         cxusb_ctrl_msg(d, CMD_DIGITAL, NULL, 0, &b, 1);
415         msleep(100);
416         return ret;
417 }
418
419 static int cxusb_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
420 {
421         struct dvb_usb_device *dvbdev = adap->dev;
422         bool is_medion = dvbdev->props.devices[0].warm_ids[0] ==
423                 &cxusb_table[MEDION_MD95700];
424         u8 buf[2] = { 0x03, 0x00 };
425
426         if (is_medion && onoff) {
427                 int ret;
428
429                 ret = cxusb_medion_get(dvbdev, CXUSB_OPEN_DIGITAL);
430                 if (ret != 0)
431                         return ret;
432         }
433
434         if (onoff)
435                 cxusb_ctrl_msg(dvbdev, CMD_STREAMING_ON, buf, 2, NULL, 0);
436         else
437                 cxusb_ctrl_msg(dvbdev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
438
439         if (is_medion && !onoff)
440                 cxusb_medion_put(dvbdev);
441
442         return 0;
443 }
444
445 static int cxusb_aver_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
446 {
447         if (onoff)
448                 cxusb_ctrl_msg(adap->dev, CMD_AVER_STREAM_ON, NULL, 0, NULL, 0);
449         else
450                 cxusb_ctrl_msg(adap->dev, CMD_AVER_STREAM_OFF,
451                                NULL, 0, NULL, 0);
452         return 0;
453 }
454
455 static void cxusb_d680_dmb_drain_message(struct dvb_usb_device *d)
456 {
457         int       ep = d->props.generic_bulk_ctrl_endpoint;
458         const int timeout = 100;
459         const int junk_len = 32;
460         u8        *junk;
461         int       rd_count;
462
463         /* Discard remaining data in video pipe */
464         junk = kmalloc(junk_len, GFP_KERNEL);
465         if (!junk)
466                 return;
467         while (1) {
468                 if (usb_bulk_msg(d->udev,
469                                  usb_rcvbulkpipe(d->udev, ep),
470                                  junk, junk_len, &rd_count, timeout) < 0)
471                         break;
472                 if (!rd_count)
473                         break;
474         }
475         kfree(junk);
476 }
477
478 static void cxusb_d680_dmb_drain_video(struct dvb_usb_device *d)
479 {
480         struct usb_data_stream_properties *p = &d->props.adapter[0].fe[0].stream;
481         const int timeout = 100;
482         const int junk_len = p->u.bulk.buffersize;
483         u8        *junk;
484         int       rd_count;
485
486         /* Discard remaining data in video pipe */
487         junk = kmalloc(junk_len, GFP_KERNEL);
488         if (!junk)
489                 return;
490         while (1) {
491                 if (usb_bulk_msg(d->udev,
492                                  usb_rcvbulkpipe(d->udev, p->endpoint),
493                                  junk, junk_len, &rd_count, timeout) < 0)
494                         break;
495                 if (!rd_count)
496                         break;
497         }
498         kfree(junk);
499 }
500
501 static int cxusb_d680_dmb_streaming_ctrl(struct dvb_usb_adapter *adap,
502                                          int onoff)
503 {
504         if (onoff) {
505                 u8 buf[2] = { 0x03, 0x00 };
506
507                 cxusb_d680_dmb_drain_video(adap->dev);
508                 return cxusb_ctrl_msg(adap->dev, CMD_STREAMING_ON,
509                                       buf, sizeof(buf), NULL, 0);
510         } else {
511                 int ret = cxusb_ctrl_msg(adap->dev,
512                                          CMD_STREAMING_OFF, NULL, 0, NULL, 0);
513                 return ret;
514         }
515 }
516
517 static int cxusb_rc_query(struct dvb_usb_device *d)
518 {
519         u8 ircode[4];
520
521         if (cxusb_ctrl_msg(d, CMD_GET_IR_CODE, NULL, 0, ircode, 4) < 0)
522                 return 0;
523
524         if (ircode[2] || ircode[3])
525                 rc_keydown(d->rc_dev, RC_PROTO_NEC,
526                            RC_SCANCODE_NEC(~ircode[2] & 0xff, ircode[3]), 0);
527         return 0;
528 }
529
530 static int cxusb_bluebird2_rc_query(struct dvb_usb_device *d)
531 {
532         u8 ircode[4];
533         struct i2c_msg msg = {
534                 .addr = 0x6b,
535                 .flags = I2C_M_RD,
536                 .buf = ircode,
537                 .len = 4
538         };
539
540         if (cxusb_i2c_xfer(&d->i2c_adap, &msg, 1) != 1)
541                 return 0;
542
543         if (ircode[1] || ircode[2])
544                 rc_keydown(d->rc_dev, RC_PROTO_NEC,
545                            RC_SCANCODE_NEC(~ircode[1] & 0xff, ircode[2]), 0);
546         return 0;
547 }
548
549 static int cxusb_d680_dmb_rc_query(struct dvb_usb_device *d)
550 {
551         u8 ircode[2];
552
553         if (cxusb_ctrl_msg(d, 0x10, NULL, 0, ircode, 2) < 0)
554                 return 0;
555
556         if (ircode[0] || ircode[1])
557                 rc_keydown(d->rc_dev, RC_PROTO_UNKNOWN,
558                            RC_SCANCODE_RC5(ircode[0], ircode[1]), 0);
559         return 0;
560 }
561
562 static int cxusb_dee1601_demod_init(struct dvb_frontend *fe)
563 {
564         static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x28 };
565         static u8 reset[]          = { RESET,      0x80 };
566         static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
567         static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0x20 };
568         static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
569         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
570
571         mt352_write(fe, clock_config,   sizeof(clock_config));
572         udelay(200);
573         mt352_write(fe, reset,          sizeof(reset));
574         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
575
576         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
577         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
578         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
579
580         return 0;
581 }
582
583 static int cxusb_mt352_demod_init(struct dvb_frontend *fe)
584 {
585         /* used in both lgz201 and th7579 */
586         static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x29 };
587         static u8 reset[]          = { RESET,      0x80 };
588         static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
589         static u8 agc_cfg[]        = { AGC_TARGET, 0x24, 0x20 };
590         static u8 gpp_ctl_cfg[]    = { GPP_CTL,    0x33 };
591         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
592
593         mt352_write(fe, clock_config,   sizeof(clock_config));
594         udelay(200);
595         mt352_write(fe, reset,          sizeof(reset));
596         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
597
598         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
599         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
600         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
601         return 0;
602 }
603
604 static struct cx22702_config cxusb_cx22702_config = {
605         .demod_address = 0x63,
606         .output_mode = CX22702_PARALLEL_OUTPUT,
607 };
608
609 static struct lgdt330x_config cxusb_lgdt3303_config = {
610         .demod_chip    = LGDT3303,
611 };
612
613 static struct lgdt330x_config cxusb_aver_lgdt3303_config = {
614         .demod_chip          = LGDT3303,
615         .clock_polarity_flip = 2,
616 };
617
618 static struct mt352_config cxusb_dee1601_config = {
619         .demod_address = 0x0f,
620         .demod_init    = cxusb_dee1601_demod_init,
621 };
622
623 static struct zl10353_config cxusb_zl10353_dee1601_config = {
624         .demod_address = 0x0f,
625         .parallel_ts = 1,
626 };
627
628 static struct mt352_config cxusb_mt352_config = {
629         /* used in both lgz201 and th7579 */
630         .demod_address = 0x0f,
631         .demod_init    = cxusb_mt352_demod_init,
632 };
633
634 static struct zl10353_config cxusb_zl10353_xc3028_config = {
635         .demod_address = 0x0f,
636         .if2 = 45600,
637         .no_tuner = 1,
638         .parallel_ts = 1,
639 };
640
641 static struct zl10353_config cxusb_zl10353_xc3028_config_no_i2c_gate = {
642         .demod_address = 0x0f,
643         .if2 = 45600,
644         .no_tuner = 1,
645         .parallel_ts = 1,
646         .disable_i2c_gate_ctrl = 1,
647 };
648
649 static struct mt352_config cxusb_mt352_xc3028_config = {
650         .demod_address = 0x0f,
651         .if2 = 4560,
652         .no_tuner = 1,
653         .demod_init = cxusb_mt352_demod_init,
654 };
655
656 /* FIXME: needs tweaking */
657 static struct mxl5005s_config aver_a868r_tuner = {
658         .i2c_address     = 0x63,
659         .if_freq         = 6000000UL,
660         .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
661         .agc_mode        = MXL_SINGLE_AGC,
662         .tracking_filter = MXL_TF_C,
663         .rssi_enable     = MXL_RSSI_ENABLE,
664         .cap_select      = MXL_CAP_SEL_ENABLE,
665         .div_out         = MXL_DIV_OUT_4,
666         .clock_out       = MXL_CLOCK_OUT_DISABLE,
667         .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
668         .top             = MXL5005S_TOP_25P2,
669         .mod_mode        = MXL_DIGITAL_MODE,
670         .if_mode         = MXL_ZERO_IF,
671         .AgcMasterByte   = 0x00,
672 };
673
674 /* FIXME: needs tweaking */
675 static struct mxl5005s_config d680_dmb_tuner = {
676         .i2c_address     = 0x63,
677         .if_freq         = 36125000UL,
678         .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
679         .agc_mode        = MXL_SINGLE_AGC,
680         .tracking_filter = MXL_TF_C,
681         .rssi_enable     = MXL_RSSI_ENABLE,
682         .cap_select      = MXL_CAP_SEL_ENABLE,
683         .div_out         = MXL_DIV_OUT_4,
684         .clock_out       = MXL_CLOCK_OUT_DISABLE,
685         .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
686         .top             = MXL5005S_TOP_25P2,
687         .mod_mode        = MXL_DIGITAL_MODE,
688         .if_mode         = MXL_ZERO_IF,
689         .AgcMasterByte   = 0x00,
690 };
691
692 static struct max2165_config mygica_d689_max2165_cfg = {
693         .i2c_address = 0x60,
694         .osc_clk = 20
695 };
696
697 /* Callbacks for DVB USB */
698 static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_adapter *adap)
699 {
700         struct dvb_usb_device *dvbdev = adap->dev;
701         bool is_medion = dvbdev->props.devices[0].warm_ids[0] ==
702                 &cxusb_table[MEDION_MD95700];
703
704         dvb_attach(simple_tuner_attach, adap->fe_adap[0].fe,
705                    &dvbdev->i2c_adap, 0x61,
706                    TUNER_PHILIPS_FMD1216ME_MK3);
707
708         if (is_medion && adap->fe_adap[0].fe)
709                 /*
710                  * make sure that DVB core won't put to sleep (reset, really)
711                  * tuner when we might be open in analog mode
712                  */
713                 adap->fe_adap[0].fe->ops.tuner_ops.sleep = NULL;
714
715         return 0;
716 }
717
718 static int cxusb_dee1601_tuner_attach(struct dvb_usb_adapter *adap)
719 {
720         dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x61,
721                    NULL, DVB_PLL_THOMSON_DTT7579);
722         return 0;
723 }
724
725 static int cxusb_lgz201_tuner_attach(struct dvb_usb_adapter *adap)
726 {
727         dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x61,
728                    NULL, DVB_PLL_LG_Z201);
729         return 0;
730 }
731
732 static int cxusb_dtt7579_tuner_attach(struct dvb_usb_adapter *adap)
733 {
734         dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60,
735                    NULL, DVB_PLL_THOMSON_DTT7579);
736         return 0;
737 }
738
739 static int cxusb_lgh064f_tuner_attach(struct dvb_usb_adapter *adap)
740 {
741         dvb_attach(simple_tuner_attach, adap->fe_adap[0].fe,
742                    &adap->dev->i2c_adap, 0x61, TUNER_LG_TDVS_H06XF);
743         return 0;
744 }
745
746 static int dvico_bluebird_xc2028_callback(void *ptr, int component,
747                                           int command, int arg)
748 {
749         struct dvb_usb_adapter *adap = ptr;
750         struct dvb_usb_device *d = adap->dev;
751
752         switch (command) {
753         case XC2028_TUNER_RESET:
754                 dev_info(&d->udev->dev, "XC2028_TUNER_RESET %d\n", arg);
755                 cxusb_bluebird_gpio_pulse(d, 0x01, 1);
756                 break;
757         case XC2028_RESET_CLK:
758                 dev_info(&d->udev->dev, "XC2028_RESET_CLK %d\n", arg);
759                 break;
760         case XC2028_I2C_FLUSH:
761                 break;
762         default:
763                 dev_info(&d->udev->dev, "unknown command %d, arg %d\n",
764                          command, arg);
765                 return -EINVAL;
766         }
767
768         return 0;
769 }
770
771 static int cxusb_dvico_xc3028_tuner_attach(struct dvb_usb_adapter *adap)
772 {
773         struct dvb_frontend      *fe;
774         struct xc2028_config      cfg = {
775                 .i2c_adap  = &adap->dev->i2c_adap,
776                 .i2c_addr  = 0x61,
777         };
778         static struct xc2028_ctrl ctl = {
779                 .fname       = "/*(DEBLOBBED)*/",
780                 .max_len     = 64,
781                 .demod       = XC3028_FE_ZARLINK456,
782         };
783
784         /* FIXME: generalize & move to common area */
785         adap->fe_adap[0].fe->callback = dvico_bluebird_xc2028_callback;
786
787         fe = dvb_attach(xc2028_attach, adap->fe_adap[0].fe, &cfg);
788         if (!fe || !fe->ops.tuner_ops.set_config)
789                 return -EIO;
790
791         fe->ops.tuner_ops.set_config(fe, &ctl);
792
793         return 0;
794 }
795
796 static int cxusb_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
797 {
798         dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
799                    &adap->dev->i2c_adap, &aver_a868r_tuner);
800         return 0;
801 }
802
803 static int cxusb_d680_dmb_tuner_attach(struct dvb_usb_adapter *adap)
804 {
805         struct dvb_frontend *fe;
806
807         fe = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
808                         &adap->dev->i2c_adap, &d680_dmb_tuner);
809         return (!fe) ? -EIO : 0;
810 }
811
812 static int cxusb_mygica_d689_tuner_attach(struct dvb_usb_adapter *adap)
813 {
814         struct dvb_frontend *fe;
815
816         fe = dvb_attach(max2165_attach, adap->fe_adap[0].fe,
817                         &adap->dev->i2c_adap, &mygica_d689_max2165_cfg);
818         return (!fe) ? -EIO : 0;
819 }
820
821 static int cxusb_medion_fe_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
822 {
823         struct dvb_usb_adapter *adap = fe->dvb->priv;
824         struct dvb_usb_device *dvbdev = adap->dev;
825
826         if (acquire)
827                 return cxusb_medion_get(dvbdev, CXUSB_OPEN_DIGITAL);
828
829         cxusb_medion_put(dvbdev);
830
831         return 0;
832 }
833
834 static int cxusb_medion_set_mode(struct dvb_usb_device *dvbdev, bool digital)
835 {
836         struct cxusb_state *st = dvbdev->priv;
837         int ret;
838         u8 b;
839         unsigned int i;
840
841         /*
842          * switching mode while doing an I2C transaction often causes
843          * the device to crash
844          */
845         mutex_lock(&dvbdev->i2c_mutex);
846
847         if (digital) {
848                 ret = usb_set_interface(dvbdev->udev, 0, 6);
849                 if (ret != 0) {
850                         dev_err(&dvbdev->udev->dev,
851                                 "digital interface selection failed (%d)\n",
852                                 ret);
853                         goto ret_unlock;
854                 }
855         } else {
856                 ret = usb_set_interface(dvbdev->udev, 0, 1);
857                 if (ret != 0) {
858                         dev_err(&dvbdev->udev->dev,
859                                 "analog interface selection failed (%d)\n",
860                                 ret);
861                         goto ret_unlock;
862                 }
863         }
864
865         /* pipes need to be cleared after setting interface */
866         ret = usb_clear_halt(dvbdev->udev, usb_rcvbulkpipe(dvbdev->udev, 1));
867         if (ret != 0)
868                 dev_warn(&dvbdev->udev->dev,
869                          "clear halt on IN pipe failed (%d)\n",
870                          ret);
871
872         ret = usb_clear_halt(dvbdev->udev, usb_sndbulkpipe(dvbdev->udev, 1));
873         if (ret != 0)
874                 dev_warn(&dvbdev->udev->dev,
875                          "clear halt on OUT pipe failed (%d)\n",
876                          ret);
877
878         ret = cxusb_ctrl_msg(dvbdev, digital ? CMD_DIGITAL : CMD_ANALOG,
879                              NULL, 0, &b, 1);
880         if (ret != 0) {
881                 dev_err(&dvbdev->udev->dev, "mode switch failed (%d)\n",
882                         ret);
883                 goto ret_unlock;
884         }
885
886         /* mode switch seems to reset GPIO states */
887         for (i = 0; i < ARRAY_SIZE(st->gpio_write_refresh); i++)
888                 st->gpio_write_refresh[i] = true;
889
890 ret_unlock:
891         mutex_unlock(&dvbdev->i2c_mutex);
892
893         return ret;
894 }
895
896 static int cxusb_cx22702_frontend_attach(struct dvb_usb_adapter *adap)
897 {
898         struct dvb_usb_device *dvbdev = adap->dev;
899         bool is_medion = dvbdev->props.devices[0].warm_ids[0] ==
900                 &cxusb_table[MEDION_MD95700];
901
902         if (is_medion) {
903                 int ret;
904
905                 ret = cxusb_medion_set_mode(dvbdev, true);
906                 if (ret)
907                         return ret;
908         }
909
910         adap->fe_adap[0].fe = dvb_attach(cx22702_attach, &cxusb_cx22702_config,
911                                          &dvbdev->i2c_adap);
912         if (!adap->fe_adap[0].fe)
913                 return -EIO;
914
915         if (is_medion)
916                 adap->fe_adap[0].fe->ops.ts_bus_ctrl =
917                         cxusb_medion_fe_ts_bus_ctrl;
918
919         return 0;
920 }
921
922 static int cxusb_lgdt3303_frontend_attach(struct dvb_usb_adapter *adap)
923 {
924         if (usb_set_interface(adap->dev->udev, 0, 7) < 0)
925                 err("set interface failed");
926
927         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
928
929         adap->fe_adap[0].fe = dvb_attach(lgdt330x_attach,
930                                          &cxusb_lgdt3303_config,
931                                          0x0e,
932                                          &adap->dev->i2c_adap);
933         if (adap->fe_adap[0].fe)
934                 return 0;
935
936         return -EIO;
937 }
938
939 static int cxusb_aver_lgdt3303_frontend_attach(struct dvb_usb_adapter *adap)
940 {
941         adap->fe_adap[0].fe = dvb_attach(lgdt330x_attach,
942                                          &cxusb_aver_lgdt3303_config,
943                                          0x0e,
944                                          &adap->dev->i2c_adap);
945         if (adap->fe_adap[0].fe)
946                 return 0;
947
948         return -EIO;
949 }
950
951 static int cxusb_mt352_frontend_attach(struct dvb_usb_adapter *adap)
952 {
953         /* used in both lgz201 and th7579 */
954         if (usb_set_interface(adap->dev->udev, 0, 0) < 0)
955                 err("set interface failed");
956
957         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
958
959         adap->fe_adap[0].fe = dvb_attach(mt352_attach, &cxusb_mt352_config,
960                                          &adap->dev->i2c_adap);
961         if (adap->fe_adap[0].fe)
962                 return 0;
963
964         return -EIO;
965 }
966
967 static int cxusb_dee1601_frontend_attach(struct dvb_usb_adapter *adap)
968 {
969         if (usb_set_interface(adap->dev->udev, 0, 0) < 0)
970                 err("set interface failed");
971
972         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
973
974         adap->fe_adap[0].fe = dvb_attach(mt352_attach, &cxusb_dee1601_config,
975                                          &adap->dev->i2c_adap);
976         if (adap->fe_adap[0].fe)
977                 return 0;
978
979         adap->fe_adap[0].fe = dvb_attach(zl10353_attach,
980                                          &cxusb_zl10353_dee1601_config,
981                                          &adap->dev->i2c_adap);
982         if (adap->fe_adap[0].fe)
983                 return 0;
984
985         return -EIO;
986 }
987
988 static int cxusb_dualdig4_frontend_attach(struct dvb_usb_adapter *adap)
989 {
990         u8 ircode[4];
991         int i;
992         struct i2c_msg msg = {
993                 .addr = 0x6b,
994                 .flags = I2C_M_RD,
995                 .buf = ircode,
996                 .len = 4
997         };
998
999         if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1000                 err("set interface failed");
1001
1002         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1003
1004         /* reset the tuner and demodulator */
1005         cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
1006         cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
1007         cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1008
1009         adap->fe_adap[0].fe =
1010                 dvb_attach(zl10353_attach,
1011                            &cxusb_zl10353_xc3028_config_no_i2c_gate,
1012                            &adap->dev->i2c_adap);
1013         if (!adap->fe_adap[0].fe)
1014                 return -EIO;
1015
1016         /* try to determine if there is no IR decoder on the I2C bus */
1017         for (i = 0; adap->dev->props.rc.core.rc_codes && i < 5; i++) {
1018                 msleep(20);
1019                 if (cxusb_i2c_xfer(&adap->dev->i2c_adap, &msg, 1) != 1)
1020                         goto no_IR;
1021                 if (ircode[0] == 0 && ircode[1] == 0)
1022                         continue;
1023                 if (ircode[2] + ircode[3] != 0xff) {
1024 no_IR:
1025                         adap->dev->props.rc.core.rc_codes = NULL;
1026                         info("No IR receiver detected on this device.");
1027                         break;
1028                 }
1029         }
1030
1031         return 0;
1032 }
1033
1034 static struct dibx000_agc_config dib7070_agc_config = {
1035         .band_caps = BAND_UHF | BAND_VHF | BAND_LBAND | BAND_SBAND,
1036
1037         /*
1038          * P_agc_use_sd_mod1=0, P_agc_use_sd_mod2=0, P_agc_freq_pwm_div=5,
1039          * P_agc_inv_pwm1=0, P_agc_inv_pwm2=0, P_agc_inh_dc_rv_est=0,
1040          * P_agc_time_est=3, P_agc_freeze=0, P_agc_nb_est=5, P_agc_write=0
1041          */
1042         .setup = (0 << 15) | (0 << 14) | (5 << 11) | (0 << 10) | (0 << 9) |
1043                  (0 << 8) | (3 << 5) | (0 << 4) | (5 << 1) | (0 << 0),
1044         .inv_gain = 600,
1045         .time_stabiliz = 10,
1046         .alpha_level = 0,
1047         .thlock = 118,
1048         .wbd_inv = 0,
1049         .wbd_ref = 3530,
1050         .wbd_sel = 1,
1051         .wbd_alpha = 5,
1052         .agc1_max = 65535,
1053         .agc1_min = 0,
1054         .agc2_max = 65535,
1055         .agc2_min = 0,
1056         .agc1_pt1 = 0,
1057         .agc1_pt2 = 40,
1058         .agc1_pt3 = 183,
1059         .agc1_slope1 = 206,
1060         .agc1_slope2 = 255,
1061         .agc2_pt1 = 72,
1062         .agc2_pt2 = 152,
1063         .agc2_slope1 = 88,
1064         .agc2_slope2 = 90,
1065         .alpha_mant = 17,
1066         .alpha_exp = 27,
1067         .beta_mant = 23,
1068         .beta_exp = 51,
1069         .perform_agc_softsplit = 0,
1070 };
1071
1072 static struct dibx000_bandwidth_config dib7070_bw_config_12_mhz = {
1073         .internal = 60000,
1074         .sampling = 15000,
1075         .pll_prediv = 1,
1076         .pll_ratio = 20,
1077         .pll_range = 3,
1078         .pll_reset = 1,
1079         .pll_bypass = 0,
1080         .enable_refdiv = 0,
1081         .bypclk_div = 0,
1082         .IO_CLK_en_core = 1,
1083         .ADClkSrc = 1,
1084         .modulo = 2,
1085         /* refsel, sel, freq_15k */
1086         .sad_cfg = (3 << 14) | (1 << 12) | (524 << 0),
1087         .ifreq = (0 << 25) | 0,
1088         .timf = 20452225,
1089         .xtal_hz = 12000000,
1090 };
1091
1092 static struct dib7000p_config cxusb_dualdig4_rev2_config = {
1093         .output_mode = OUTMODE_MPEG2_PAR_GATED_CLK,
1094         .output_mpeg2_in_188_bytes = 1,
1095
1096         .agc_config_count = 1,
1097         .agc = &dib7070_agc_config,
1098         .bw  = &dib7070_bw_config_12_mhz,
1099         .tuner_is_baseband = 1,
1100         .spur_protect = 1,
1101
1102         .gpio_dir = 0xfcef,
1103         .gpio_val = 0x0110,
1104
1105         .gpio_pwm_pos = DIB7000P_GPIO_DEFAULT_PWM_POS,
1106
1107         .hostbus_diversity = 1,
1108 };
1109
1110 struct dib0700_adapter_state {
1111         int (*set_param_save)(struct dvb_frontend *fe);
1112         struct dib7000p_ops dib7000p_ops;
1113 };
1114
1115 static int cxusb_dualdig4_rev2_frontend_attach(struct dvb_usb_adapter *adap)
1116 {
1117         struct dib0700_adapter_state *state = adap->priv;
1118
1119         if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1120                 err("set interface failed");
1121
1122         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1123
1124         cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1125
1126         if (!dvb_attach(dib7000p_attach, &state->dib7000p_ops))
1127                 return -ENODEV;
1128
1129         if (state->dib7000p_ops.i2c_enumeration(&adap->dev->i2c_adap, 1, 18,
1130                                                 &cxusb_dualdig4_rev2_config) < 0) {
1131                 pr_warn("Unable to enumerate dib7000p\n");
1132                 return -ENODEV;
1133         }
1134
1135         adap->fe_adap[0].fe = state->dib7000p_ops.init(&adap->dev->i2c_adap,
1136                                                        0x80,
1137                                                        &cxusb_dualdig4_rev2_config);
1138         if (!adap->fe_adap[0].fe)
1139                 return -EIO;
1140
1141         return 0;
1142 }
1143
1144 static int dib7070_tuner_reset(struct dvb_frontend *fe, int onoff)
1145 {
1146         struct dvb_usb_adapter *adap = fe->dvb->priv;
1147         struct dib0700_adapter_state *state = adap->priv;
1148
1149         return state->dib7000p_ops.set_gpio(fe, 8, 0, !onoff);
1150 }
1151
1152 static int dib7070_tuner_sleep(struct dvb_frontend *fe, int onoff)
1153 {
1154         return 0;
1155 }
1156
1157 static struct dib0070_config dib7070p_dib0070_config = {
1158         .i2c_address = DEFAULT_DIB0070_I2C_ADDRESS,
1159         .reset = dib7070_tuner_reset,
1160         .sleep = dib7070_tuner_sleep,
1161         .clock_khz = 12000,
1162 };
1163
1164 static int dib7070_set_param_override(struct dvb_frontend *fe)
1165 {
1166         struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1167         struct dvb_usb_adapter *adap = fe->dvb->priv;
1168         struct dib0700_adapter_state *state = adap->priv;
1169
1170         u16 offset;
1171         u8 band = BAND_OF_FREQUENCY(p->frequency / 1000);
1172
1173         switch (band) {
1174         case BAND_VHF:
1175                 offset = 950;
1176                 break;
1177         default:
1178         case BAND_UHF:
1179                 offset = 550;
1180                 break;
1181         }
1182
1183         state->dib7000p_ops.set_wbd_ref(fe, offset + dib0070_wbd_offset(fe));
1184
1185         return state->set_param_save(fe);
1186 }
1187
1188 static int cxusb_dualdig4_rev2_tuner_attach(struct dvb_usb_adapter *adap)
1189 {
1190         struct dib0700_adapter_state *st = adap->priv;
1191         struct i2c_adapter *tun_i2c;
1192
1193         /*
1194          * No need to call dvb7000p_attach here, as it was called
1195          * already, as frontend_attach method is called first, and
1196          * tuner_attach is only called on success.
1197          */
1198         tun_i2c = st->dib7000p_ops.get_i2c_master(adap->fe_adap[0].fe,
1199                                         DIBX000_I2C_INTERFACE_TUNER, 1);
1200
1201         if (dvb_attach(dib0070_attach, adap->fe_adap[0].fe, tun_i2c,
1202                        &dib7070p_dib0070_config) == NULL)
1203                 return -ENODEV;
1204
1205         st->set_param_save = adap->fe_adap[0].fe->ops.tuner_ops.set_params;
1206         adap->fe_adap[0].fe->ops.tuner_ops.set_params = dib7070_set_param_override;
1207         return 0;
1208 }
1209
1210 static int cxusb_nano2_frontend_attach(struct dvb_usb_adapter *adap)
1211 {
1212         if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
1213                 err("set interface failed");
1214
1215         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
1216
1217         /* reset the tuner and demodulator */
1218         cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
1219         cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
1220         cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
1221
1222         adap->fe_adap[0].fe = dvb_attach(zl10353_attach,
1223                                          &cxusb_zl10353_xc3028_config,
1224                                          &adap->dev->i2c_adap);
1225         if (adap->fe_adap[0].fe)
1226                 return 0;
1227
1228         adap->fe_adap[0].fe = dvb_attach(mt352_attach,
1229                                          &cxusb_mt352_xc3028_config,
1230                                          &adap->dev->i2c_adap);
1231         if (adap->fe_adap[0].fe)
1232                 return 0;
1233
1234         return -EIO;
1235 }
1236
1237 static struct lgs8gxx_config d680_lgs8gl5_cfg = {
1238         .prod = LGS8GXX_PROD_LGS8GL5,
1239         .demod_address = 0x19,
1240         .serial_ts = 0,
1241         .ts_clk_pol = 0,
1242         .ts_clk_gated = 1,
1243         .if_clk_freq = 30400, /* 30.4 MHz */
1244         .if_freq = 5725, /* 5.725 MHz */
1245         .if_neg_center = 0,
1246         .ext_adc = 0,
1247         .adc_signed = 0,
1248         .if_neg_edge = 0,
1249 };
1250
1251 static int cxusb_d680_dmb_frontend_attach(struct dvb_usb_adapter *adap)
1252 {
1253         struct dvb_usb_device *d = adap->dev;
1254         int n;
1255
1256         /* Select required USB configuration */
1257         if (usb_set_interface(d->udev, 0, 0) < 0)
1258                 err("set interface failed");
1259
1260         /* Unblock all USB pipes */
1261         usb_clear_halt(d->udev,
1262                        usb_sndbulkpipe(d->udev,
1263                                        d->props.generic_bulk_ctrl_endpoint));
1264         usb_clear_halt(d->udev,
1265                        usb_rcvbulkpipe(d->udev,
1266                                        d->props.generic_bulk_ctrl_endpoint));
1267         usb_clear_halt(d->udev,
1268                        usb_rcvbulkpipe(d->udev,
1269                                        d->props.adapter[0].fe[0].stream.endpoint));
1270
1271         /* Drain USB pipes to avoid hang after reboot */
1272         for (n = 0;  n < 5;  n++) {
1273                 cxusb_d680_dmb_drain_message(d);
1274                 cxusb_d680_dmb_drain_video(d);
1275                 msleep(200);
1276         }
1277
1278         /* Reset the tuner */
1279         if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 0) < 0) {
1280                 err("clear tuner gpio failed");
1281                 return -EIO;
1282         }
1283         msleep(100);
1284         if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 1) < 0) {
1285                 err("set tuner gpio failed");
1286                 return -EIO;
1287         }
1288         msleep(100);
1289
1290         /* Attach frontend */
1291         adap->fe_adap[0].fe = dvb_attach(lgs8gxx_attach,
1292                                          &d680_lgs8gl5_cfg, &d->i2c_adap);
1293         if (!adap->fe_adap[0].fe)
1294                 return -EIO;
1295
1296         return 0;
1297 }
1298
1299 static struct atbm8830_config mygica_d689_atbm8830_cfg = {
1300         .prod = ATBM8830_PROD_8830,
1301         .demod_address = 0x40,
1302         .serial_ts = 0,
1303         .ts_sampling_edge = 1,
1304         .ts_clk_gated = 0,
1305         .osc_clk_freq = 30400, /* in kHz */
1306         .if_freq = 0, /* zero IF */
1307         .zif_swap_iq = 1,
1308         .agc_min = 0x2E,
1309         .agc_max = 0x90,
1310         .agc_hold_loop = 0,
1311 };
1312
1313 static int cxusb_mygica_d689_frontend_attach(struct dvb_usb_adapter *adap)
1314 {
1315         struct dvb_usb_device *d = adap->dev;
1316
1317         /* Select required USB configuration */
1318         if (usb_set_interface(d->udev, 0, 0) < 0)
1319                 err("set interface failed");
1320
1321         /* Unblock all USB pipes */
1322         usb_clear_halt(d->udev,
1323                        usb_sndbulkpipe(d->udev,
1324                                        d->props.generic_bulk_ctrl_endpoint));
1325         usb_clear_halt(d->udev,
1326                        usb_rcvbulkpipe(d->udev,
1327                                        d->props.generic_bulk_ctrl_endpoint));
1328         usb_clear_halt(d->udev,
1329                        usb_rcvbulkpipe(d->udev,
1330                                        d->props.adapter[0].fe[0].stream.endpoint));
1331
1332         /* Reset the tuner */
1333         if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 0) < 0) {
1334                 err("clear tuner gpio failed");
1335                 return -EIO;
1336         }
1337         msleep(100);
1338         if (cxusb_d680_dmb_gpio_tuner(d, 0x07, 1) < 0) {
1339                 err("set tuner gpio failed");
1340                 return -EIO;
1341         }
1342         msleep(100);
1343
1344         /* Attach frontend */
1345         adap->fe_adap[0].fe = dvb_attach(atbm8830_attach,
1346                                          &mygica_d689_atbm8830_cfg,
1347                                          &d->i2c_adap);
1348         if (!adap->fe_adap[0].fe)
1349                 return -EIO;
1350
1351         return 0;
1352 }
1353
1354 /*
1355  * DViCO has shipped two devices with the same USB ID, but only one of them
1356  * needs a firmware download.  Check the device class details to see if they
1357  * have non-default values to decide whether the device is actually cold or
1358  * not, and forget a match if it turns out we selected the wrong device.
1359  */
1360 static int bluebird_fx2_identify_state(struct usb_device *udev,
1361                                        const struct dvb_usb_device_properties *props,
1362                                        const struct dvb_usb_device_description **desc,
1363                                        int *cold)
1364 {
1365         int wascold = *cold;
1366
1367         *cold = udev->descriptor.bDeviceClass == 0xff &&
1368                 udev->descriptor.bDeviceSubClass == 0xff &&
1369                 udev->descriptor.bDeviceProtocol == 0xff;
1370
1371         if (*cold && !wascold)
1372                 *desc = NULL;
1373
1374         return 0;
1375 }
1376
1377 /*
1378  * DViCO bluebird firmware needs the "warm" product ID to be patched into the
1379  * firmware file before download.
1380  */
1381
1382 static const int dvico_firmware_id_offsets[] = { 6638, 3204 };
1383 static int bluebird_patch_dvico_firmware_download(struct usb_device *udev,
1384                                                   const struct firmware *fw)
1385 {
1386         int pos;
1387
1388         for (pos = 0; pos < ARRAY_SIZE(dvico_firmware_id_offsets); pos++) {
1389                 int idoff = dvico_firmware_id_offsets[pos];
1390
1391                 if (fw->size < idoff + 4)
1392                         continue;
1393
1394                 if (fw->data[idoff] == (USB_VID_DVICO & 0xff) &&
1395                     fw->data[idoff + 1] == USB_VID_DVICO >> 8) {
1396                         struct firmware new_fw;
1397                         u8 *new_fw_data = vmalloc(fw->size);
1398                         int ret;
1399
1400                         if (!new_fw_data)
1401                                 return -ENOMEM;
1402
1403                         memcpy(new_fw_data, fw->data, fw->size);
1404                         new_fw.size = fw->size;
1405                         new_fw.data = new_fw_data;
1406
1407                         new_fw_data[idoff + 2] =
1408                                 le16_to_cpu(udev->descriptor.idProduct) + 1;
1409                         new_fw_data[idoff + 3] =
1410                                 le16_to_cpu(udev->descriptor.idProduct) >> 8;
1411
1412                         ret = usb_cypress_load_firmware(udev, &new_fw,
1413                                                         CYPRESS_FX2);
1414                         vfree(new_fw_data);
1415                         return ret;
1416                 }
1417         }
1418
1419         return -EINVAL;
1420 }
1421
1422 int cxusb_medion_get(struct dvb_usb_device *dvbdev,
1423                      enum cxusb_open_type open_type)
1424 {
1425         struct cxusb_medion_dev *cxdev = dvbdev->priv;
1426         int ret = 0;
1427
1428         mutex_lock(&cxdev->open_lock);
1429
1430         if (WARN_ON((cxdev->open_type == CXUSB_OPEN_INIT ||
1431                      cxdev->open_type == CXUSB_OPEN_NONE) &&
1432                     cxdev->open_ctr != 0)) {
1433                 ret = -EINVAL;
1434                 goto ret_unlock;
1435         }
1436
1437         if (cxdev->open_type == CXUSB_OPEN_INIT) {
1438                 ret = -EAGAIN;
1439                 goto ret_unlock;
1440         }
1441
1442         if (cxdev->open_ctr == 0) {
1443                 if (cxdev->open_type != open_type) {
1444                         dev_info(&dvbdev->udev->dev, "will acquire and switch to %s\n",
1445                                  open_type == CXUSB_OPEN_ANALOG ?
1446                                  "analog" : "digital");
1447
1448                         if (open_type == CXUSB_OPEN_ANALOG) {
1449                                 ret = _cxusb_power_ctrl(dvbdev, 1);
1450                                 if (ret != 0)
1451                                         dev_warn(&dvbdev->udev->dev,
1452                                                  "powerup for analog switch failed (%d)\n",
1453                                                  ret);
1454
1455                                 ret = cxusb_medion_set_mode(dvbdev, false);
1456                                 if (ret != 0)
1457                                         goto ret_unlock;
1458
1459                                 ret = cxusb_medion_analog_init(dvbdev);
1460                                 if (ret != 0)
1461                                         goto ret_unlock;
1462                         } else { /* digital */
1463                                 ret = _cxusb_power_ctrl(dvbdev, 1);
1464                                 if (ret != 0)
1465                                         dev_warn(&dvbdev->udev->dev,
1466                                                  "powerup for digital switch failed (%d)\n",
1467                                                  ret);
1468
1469                                 ret = cxusb_medion_set_mode(dvbdev, true);
1470                                 if (ret != 0)
1471                                         goto ret_unlock;
1472                         }
1473
1474                         cxdev->open_type = open_type;
1475                 } else {
1476                         dev_info(&dvbdev->udev->dev, "reacquired idle %s\n",
1477                                  open_type == CXUSB_OPEN_ANALOG ?
1478                                  "analog" : "digital");
1479                 }
1480
1481                 cxdev->open_ctr = 1;
1482         } else if (cxdev->open_type == open_type) {
1483                 cxdev->open_ctr++;
1484                 dev_info(&dvbdev->udev->dev, "acquired %s\n",
1485                          open_type == CXUSB_OPEN_ANALOG ? "analog" : "digital");
1486         } else {
1487                 ret = -EBUSY;
1488         }
1489
1490 ret_unlock:
1491         mutex_unlock(&cxdev->open_lock);
1492
1493         return ret;
1494 }
1495
1496 void cxusb_medion_put(struct dvb_usb_device *dvbdev)
1497 {
1498         struct cxusb_medion_dev *cxdev = dvbdev->priv;
1499
1500         mutex_lock(&cxdev->open_lock);
1501
1502         if (cxdev->open_type == CXUSB_OPEN_INIT) {
1503                 WARN_ON(cxdev->open_ctr != 0);
1504                 cxdev->open_type = CXUSB_OPEN_NONE;
1505                 goto unlock;
1506         }
1507
1508         if (!WARN_ON(cxdev->open_ctr < 1)) {
1509                 cxdev->open_ctr--;
1510
1511                 dev_info(&dvbdev->udev->dev, "release %s\n",
1512                          cxdev->open_type == CXUSB_OPEN_ANALOG ?
1513                          "analog" : "digital");
1514         }
1515
1516 unlock:
1517         mutex_unlock(&cxdev->open_lock);
1518 }
1519
1520 /* DVB USB Driver stuff */
1521 static struct dvb_usb_device_properties cxusb_medion_properties;
1522 static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties;
1523 static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties;
1524 static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties;
1525 static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties;
1526 static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties;
1527 static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_rev2_properties;
1528 static struct dvb_usb_device_properties cxusb_bluebird_nano2_properties;
1529 static struct dvb_usb_device_properties cxusb_bluebird_nano2_needsfirmware_properties;
1530 static struct dvb_usb_device_properties cxusb_aver_a868r_properties;
1531 static struct dvb_usb_device_properties cxusb_d680_dmb_properties;
1532 static struct dvb_usb_device_properties cxusb_mygica_d689_properties;
1533
1534 static int cxusb_medion_priv_init(struct dvb_usb_device *dvbdev)
1535 {
1536         struct cxusb_medion_dev *cxdev = dvbdev->priv;
1537
1538         cxdev->dvbdev = dvbdev;
1539         cxdev->open_type = CXUSB_OPEN_INIT;
1540         mutex_init(&cxdev->open_lock);
1541
1542         return 0;
1543 }
1544
1545 static void cxusb_medion_priv_destroy(struct dvb_usb_device *dvbdev)
1546 {
1547         struct cxusb_medion_dev *cxdev = dvbdev->priv;
1548
1549         mutex_destroy(&cxdev->open_lock);
1550 }
1551
1552 static bool cxusb_medion_check_altsetting(struct usb_host_interface *as)
1553 {
1554         unsigned int ctr;
1555
1556         for (ctr = 0; ctr < as->desc.bNumEndpoints; ctr++) {
1557                 if ((as->endpoint[ctr].desc.bEndpointAddress &
1558                      USB_ENDPOINT_NUMBER_MASK) != 2)
1559                         continue;
1560
1561                 if (as->endpoint[ctr].desc.bEndpointAddress & USB_DIR_IN &&
1562                     ((as->endpoint[ctr].desc.bmAttributes &
1563                       USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC))
1564                         return true;
1565
1566                 break;
1567         }
1568
1569         return false;
1570 }
1571
1572 static bool cxusb_medion_check_intf(struct usb_interface *intf)
1573 {
1574         unsigned int ctr;
1575
1576         if (intf->num_altsetting < 2) {
1577                 dev_err(intf->usb_dev, "no alternate interface");
1578
1579                 return false;
1580         }
1581
1582         for (ctr = 0; ctr < intf->num_altsetting; ctr++) {
1583                 if (intf->altsetting[ctr].desc.bAlternateSetting != 1)
1584                         continue;
1585
1586                 if (cxusb_medion_check_altsetting(&intf->altsetting[ctr]))
1587                         return true;
1588
1589                 break;
1590         }
1591
1592         dev_err(intf->usb_dev, "no iso interface");
1593
1594         return false;
1595 }
1596
1597 static int cxusb_probe(struct usb_interface *intf,
1598                        const struct usb_device_id *id)
1599 {
1600         struct dvb_usb_device *dvbdev;
1601         int ret;
1602
1603         /* Medion 95700 */
1604         if (!dvb_usb_device_init(intf, &cxusb_medion_properties,
1605                                  THIS_MODULE, &dvbdev, adapter_nr)) {
1606                 if (!cxusb_medion_check_intf(intf)) {
1607                         ret = -ENODEV;
1608                         goto ret_uninit;
1609                 }
1610
1611                 _cxusb_power_ctrl(dvbdev, 1);
1612                 ret = cxusb_medion_set_mode(dvbdev, false);
1613                 if (ret)
1614                         goto ret_uninit;
1615
1616                 ret = cxusb_medion_register_analog(dvbdev);
1617
1618                 cxusb_medion_set_mode(dvbdev, true);
1619                 _cxusb_power_ctrl(dvbdev, 0);
1620
1621                 if (ret != 0)
1622                         goto ret_uninit;
1623
1624                 /* release device from INIT mode to normal operation */
1625                 cxusb_medion_put(dvbdev);
1626
1627                 return 0;
1628         } else if (!dvb_usb_device_init(intf,
1629                                         &cxusb_bluebird_lgh064f_properties,
1630                                         THIS_MODULE, NULL, adapter_nr) ||
1631                    !dvb_usb_device_init(intf,
1632                                         &cxusb_bluebird_dee1601_properties,
1633                                         THIS_MODULE, NULL, adapter_nr) ||
1634                    !dvb_usb_device_init(intf,
1635                                         &cxusb_bluebird_lgz201_properties,
1636                                         THIS_MODULE, NULL, adapter_nr) ||
1637                    !dvb_usb_device_init(intf,
1638                                         &cxusb_bluebird_dtt7579_properties,
1639                                         THIS_MODULE, NULL, adapter_nr) ||
1640                    !dvb_usb_device_init(intf,
1641                                         &cxusb_bluebird_dualdig4_properties,
1642                                         THIS_MODULE, NULL, adapter_nr) ||
1643                    !dvb_usb_device_init(intf,
1644                                         &cxusb_bluebird_nano2_properties,
1645                                         THIS_MODULE, NULL, adapter_nr) ||
1646                    !dvb_usb_device_init(intf,
1647                                         &cxusb_bluebird_nano2_needsfirmware_properties,
1648                                         THIS_MODULE, NULL, adapter_nr) ||
1649                    !dvb_usb_device_init(intf, &cxusb_aver_a868r_properties,
1650                                         THIS_MODULE, NULL, adapter_nr) ||
1651                    !dvb_usb_device_init(intf,
1652                                         &cxusb_bluebird_dualdig4_rev2_properties,
1653                                         THIS_MODULE, NULL, adapter_nr) ||
1654                    !dvb_usb_device_init(intf, &cxusb_d680_dmb_properties,
1655                                         THIS_MODULE, NULL, adapter_nr) ||
1656                    !dvb_usb_device_init(intf, &cxusb_mygica_d689_properties,
1657                                         THIS_MODULE, NULL, adapter_nr) ||
1658                    0)
1659                 return 0;
1660
1661         return -EINVAL;
1662
1663 ret_uninit:
1664         dvb_usb_device_exit(intf);
1665
1666         return ret;
1667 }
1668
1669 static void cxusb_disconnect(struct usb_interface *intf)
1670 {
1671         struct dvb_usb_device *d = usb_get_intfdata(intf);
1672         struct cxusb_state *st = d->priv;
1673         struct i2c_client *client;
1674
1675         if (d->props.devices[0].warm_ids[0] == &cxusb_table[MEDION_MD95700])
1676                 cxusb_medion_unregister_analog(d);
1677
1678         /* remove I2C client for tuner */
1679         client = st->i2c_client_tuner;
1680         if (client) {
1681                 module_put(client->dev.driver->owner);
1682                 i2c_unregister_device(client);
1683         }
1684
1685         /* remove I2C client for demodulator */
1686         client = st->i2c_client_demod;
1687         if (client) {
1688                 module_put(client->dev.driver->owner);
1689                 i2c_unregister_device(client);
1690         }
1691
1692         dvb_usb_device_exit(intf);
1693 }
1694
1695 static struct usb_device_id cxusb_table[] = {
1696         DVB_USB_DEV(MEDION, MEDION_MD95700),
1697         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_LG064F_COLD),
1698         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_LG064F_WARM),
1699         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_1_COLD),
1700         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_1_WARM),
1701         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_LGZ201_COLD),
1702         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_LGZ201_WARM),
1703         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_TH7579_COLD),
1704         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_TH7579_WARM),
1705         DVB_USB_DEV(DVICO, DIGITALNOW_BLUEBIRD_DUAL_1_COLD),
1706         DVB_USB_DEV(DVICO, DIGITALNOW_BLUEBIRD_DUAL_1_WARM),
1707         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_2_COLD),
1708         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_2_WARM),
1709         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_4),
1710         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DVB_T_NANO_2),
1711         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM),
1712         DVB_USB_DEV(AVERMEDIA, AVERMEDIA_VOLAR_A868R),
1713         DVB_USB_DEV(DVICO, DVICO_BLUEBIRD_DUAL_4_REV_2),
1714         DVB_USB_DEV(CONEXANT, CONEXANT_D680_DMB),
1715         DVB_USB_DEV(CONEXANT, MYGICA_D689),
1716         { }
1717 };
1718
1719 MODULE_DEVICE_TABLE(usb, cxusb_table);
1720
1721 static struct dvb_usb_device_properties cxusb_medion_properties = {
1722         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1723
1724         .usb_ctrl = CYPRESS_FX2,
1725
1726         .size_of_priv     = sizeof(struct cxusb_medion_dev),
1727         .priv_init        = cxusb_medion_priv_init,
1728         .priv_destroy     = cxusb_medion_priv_destroy,
1729
1730         .num_adapters = 1,
1731         .adapter = {
1732                 {
1733                 .num_frontends = 1,
1734                 .fe = {{
1735                         .streaming_ctrl   = cxusb_streaming_ctrl,
1736                         .frontend_attach  = cxusb_cx22702_frontend_attach,
1737                         .tuner_attach     = cxusb_fmd1216me_tuner_attach,
1738                         /* parameter for the MPEG2-data transfer */
1739                                         .stream = {
1740                                                 .type = USB_BULK,
1741                                 .count = 5,
1742                                 .endpoint = 0x02,
1743                                 .u = {
1744                                         .bulk = {
1745                                                 .buffersize = 8192,
1746                                         }
1747                                 }
1748                         },
1749                 } },
1750                 },
1751         },
1752         .power_ctrl       = cxusb_power_ctrl,
1753
1754         .i2c_algo         = &cxusb_i2c_algo,
1755
1756         .generic_bulk_ctrl_endpoint = 0x01,
1757
1758         .num_device_descs = 1,
1759         .devices = {
1760                 {
1761                         "Medion MD95700 (MDUSBTV-HYBRID)",
1762                         { NULL },
1763                         { &cxusb_table[MEDION_MD95700], NULL },
1764                 },
1765         }
1766 };
1767
1768 static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties = {
1769         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1770
1771         .usb_ctrl          = DEVICE_SPECIFIC,
1772         .firmware          = "/*(DEBLOBBED)*/",
1773         .download_firmware = bluebird_patch_dvico_firmware_download,
1774         /*
1775          * use usb alt setting 0 for EP4 transfer (dvb-t),
1776          * use usb alt setting 7 for EP2 transfer (atsc)
1777          */
1778
1779         .size_of_priv     = sizeof(struct cxusb_state),
1780
1781         .num_adapters = 1,
1782         .adapter = {
1783                 {
1784                 .num_frontends = 1,
1785                 .fe = {{
1786                         .streaming_ctrl   = cxusb_streaming_ctrl,
1787                         .frontend_attach  = cxusb_lgdt3303_frontend_attach,
1788                         .tuner_attach     = cxusb_lgh064f_tuner_attach,
1789
1790                         /* parameter for the MPEG2-data transfer */
1791                                         .stream = {
1792                                                 .type = USB_BULK,
1793                                 .count = 5,
1794                                 .endpoint = 0x02,
1795                                 .u = {
1796                                         .bulk = {
1797                                                 .buffersize = 8192,
1798                                         }
1799                                 }
1800                         },
1801                 } },
1802                 },
1803         },
1804
1805         .power_ctrl       = cxusb_bluebird_power_ctrl,
1806
1807         .i2c_algo         = &cxusb_i2c_algo,
1808
1809         .rc.core = {
1810                 .rc_interval    = 100,
1811                 .rc_codes       = RC_MAP_DVICO_PORTABLE,
1812                 .module_name    = KBUILD_MODNAME,
1813                 .rc_query       = cxusb_rc_query,
1814                 .allowed_protos = RC_PROTO_BIT_NEC,
1815         },
1816
1817         .generic_bulk_ctrl_endpoint = 0x01,
1818
1819         .num_device_descs = 1,
1820         .devices = {
1821                 {   "DViCO FusionHDTV5 USB Gold",
1822                         { &cxusb_table[DVICO_BLUEBIRD_LG064F_COLD], NULL },
1823                         { &cxusb_table[DVICO_BLUEBIRD_LG064F_WARM], NULL },
1824                 },
1825         }
1826 };
1827
1828 static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties = {
1829         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1830
1831         .usb_ctrl          = DEVICE_SPECIFIC,
1832         .firmware          = "/*(DEBLOBBED)*/",
1833         .download_firmware = bluebird_patch_dvico_firmware_download,
1834         /*
1835          * use usb alt setting 0 for EP4 transfer (dvb-t),
1836          * use usb alt setting 7 for EP2 transfer (atsc)
1837          */
1838
1839         .size_of_priv     = sizeof(struct cxusb_state),
1840
1841         .num_adapters = 1,
1842         .adapter = {
1843                 {
1844                 .num_frontends = 1,
1845                 .fe = {{
1846                         .streaming_ctrl   = cxusb_streaming_ctrl,
1847                         .frontend_attach  = cxusb_dee1601_frontend_attach,
1848                         .tuner_attach     = cxusb_dee1601_tuner_attach,
1849                         /* parameter for the MPEG2-data transfer */
1850                         .stream = {
1851                                 .type = USB_BULK,
1852                                 .count = 5,
1853                                 .endpoint = 0x04,
1854                                 .u = {
1855                                         .bulk = {
1856                                                 .buffersize = 8192,
1857                                         }
1858                                 }
1859                         },
1860                 } },
1861                 },
1862         },
1863
1864         .power_ctrl       = cxusb_bluebird_power_ctrl,
1865
1866         .i2c_algo         = &cxusb_i2c_algo,
1867
1868         .rc.core = {
1869                 .rc_interval    = 100,
1870                 .rc_codes       = RC_MAP_DVICO_MCE,
1871                 .module_name    = KBUILD_MODNAME,
1872                 .rc_query       = cxusb_rc_query,
1873                 .allowed_protos = RC_PROTO_BIT_NEC,
1874         },
1875
1876         .generic_bulk_ctrl_endpoint = 0x01,
1877
1878         .num_device_descs = 3,
1879         .devices = {
1880                 {   "DViCO FusionHDTV DVB-T Dual USB",
1881                         { &cxusb_table[DVICO_BLUEBIRD_DUAL_1_COLD], NULL },
1882                         { &cxusb_table[DVICO_BLUEBIRD_DUAL_1_WARM], NULL },
1883                 },
1884                 {   "DigitalNow DVB-T Dual USB",
1885                         { &cxusb_table[DIGITALNOW_BLUEBIRD_DUAL_1_COLD], NULL },
1886                         { &cxusb_table[DIGITALNOW_BLUEBIRD_DUAL_1_WARM], NULL },
1887                 },
1888                 {   "DViCO FusionHDTV DVB-T Dual Digital 2",
1889                         { &cxusb_table[DVICO_BLUEBIRD_DUAL_2_COLD], NULL },
1890                         { &cxusb_table[DVICO_BLUEBIRD_DUAL_2_WARM], NULL },
1891                 },
1892         }
1893 };
1894
1895 static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties = {
1896         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1897
1898         .usb_ctrl          = DEVICE_SPECIFIC,
1899         .firmware          = "/*(DEBLOBBED)*/",
1900         .download_firmware = bluebird_patch_dvico_firmware_download,
1901         /*
1902          * use usb alt setting 0 for EP4 transfer (dvb-t),
1903          * use usb alt setting 7 for EP2 transfer (atsc)
1904          */
1905
1906         .size_of_priv     = sizeof(struct cxusb_state),
1907
1908         .num_adapters = 1,
1909         .adapter = {
1910                 {
1911                 .num_frontends = 1,
1912                 .fe = {{
1913                         .streaming_ctrl   = cxusb_streaming_ctrl,
1914                         .frontend_attach  = cxusb_mt352_frontend_attach,
1915                         .tuner_attach     = cxusb_lgz201_tuner_attach,
1916
1917                         /* parameter for the MPEG2-data transfer */
1918                         .stream = {
1919                                 .type = USB_BULK,
1920                                 .count = 5,
1921                                 .endpoint = 0x04,
1922                                 .u = {
1923                                         .bulk = {
1924                                                 .buffersize = 8192,
1925                                         }
1926                                 }
1927                         },
1928                 } },
1929                 },
1930         },
1931         .power_ctrl       = cxusb_bluebird_power_ctrl,
1932
1933         .i2c_algo         = &cxusb_i2c_algo,
1934
1935         .rc.core = {
1936                 .rc_interval    = 100,
1937                 .rc_codes       = RC_MAP_DVICO_PORTABLE,
1938                 .module_name    = KBUILD_MODNAME,
1939                 .rc_query       = cxusb_rc_query,
1940                 .allowed_protos = RC_PROTO_BIT_NEC,
1941         },
1942
1943         .generic_bulk_ctrl_endpoint = 0x01,
1944         .num_device_descs = 1,
1945         .devices = {
1946                 {   "DViCO FusionHDTV DVB-T USB (LGZ201)",
1947                         { &cxusb_table[DVICO_BLUEBIRD_LGZ201_COLD], NULL },
1948                         { &cxusb_table[DVICO_BLUEBIRD_LGZ201_WARM], NULL },
1949                 },
1950         }
1951 };
1952
1953 static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties = {
1954         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
1955
1956         .usb_ctrl          = DEVICE_SPECIFIC,
1957         .firmware          = "/*(DEBLOBBED)*/",
1958         .download_firmware = bluebird_patch_dvico_firmware_download,
1959
1960         /*
1961          * use usb alt setting 0 for EP4 transfer (dvb-t),
1962          * use usb alt setting 7 for EP2 transfer (atsc)
1963          */
1964
1965         .size_of_priv     = sizeof(struct cxusb_state),
1966
1967         .num_adapters = 1,
1968         .adapter = {
1969                 {
1970                 .num_frontends = 1,
1971                 .fe = {{
1972                         .streaming_ctrl   = cxusb_streaming_ctrl,
1973                         .frontend_attach  = cxusb_mt352_frontend_attach,
1974                         .tuner_attach     = cxusb_dtt7579_tuner_attach,
1975
1976                         /* parameter for the MPEG2-data transfer */
1977                         .stream = {
1978                                 .type = USB_BULK,
1979                                 .count = 5,
1980                                 .endpoint = 0x04,
1981                                 .u = {
1982                                         .bulk = {
1983                                                 .buffersize = 8192,
1984                                         }
1985                                 }
1986                         },
1987                 } },
1988                 },
1989         },
1990         .power_ctrl       = cxusb_bluebird_power_ctrl,
1991
1992         .i2c_algo         = &cxusb_i2c_algo,
1993
1994         .rc.core = {
1995                 .rc_interval    = 100,
1996                 .rc_codes       = RC_MAP_DVICO_PORTABLE,
1997                 .module_name    = KBUILD_MODNAME,
1998                 .rc_query       = cxusb_rc_query,
1999                 .allowed_protos = RC_PROTO_BIT_NEC,
2000         },
2001
2002         .generic_bulk_ctrl_endpoint = 0x01,
2003
2004         .num_device_descs = 1,
2005         .devices = {
2006                 {   "DViCO FusionHDTV DVB-T USB (TH7579)",
2007                         { &cxusb_table[DVICO_BLUEBIRD_TH7579_COLD], NULL },
2008                         { &cxusb_table[DVICO_BLUEBIRD_TH7579_WARM], NULL },
2009                 },
2010         }
2011 };
2012
2013 static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties = {
2014         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2015
2016         .usb_ctrl         = CYPRESS_FX2,
2017
2018         .size_of_priv     = sizeof(struct cxusb_state),
2019
2020         .num_adapters = 1,
2021         .adapter = {
2022                 {
2023                 .num_frontends = 1,
2024                 .fe = {{
2025                         .streaming_ctrl   = cxusb_streaming_ctrl,
2026                         .frontend_attach  = cxusb_dualdig4_frontend_attach,
2027                         .tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
2028                         /* parameter for the MPEG2-data transfer */
2029                         .stream = {
2030                                 .type = USB_BULK,
2031                                 .count = 5,
2032                                 .endpoint = 0x02,
2033                                 .u = {
2034                                         .bulk = {
2035                                                 .buffersize = 8192,
2036                                         }
2037                                 }
2038                         },
2039                 } },
2040                 },
2041         },
2042
2043         .power_ctrl       = cxusb_power_ctrl,
2044
2045         .i2c_algo         = &cxusb_i2c_algo,
2046
2047         .generic_bulk_ctrl_endpoint = 0x01,
2048
2049         .rc.core = {
2050                 .rc_interval    = 100,
2051                 .rc_codes       = RC_MAP_DVICO_MCE,
2052                 .module_name    = KBUILD_MODNAME,
2053                 .rc_query       = cxusb_bluebird2_rc_query,
2054                 .allowed_protos = RC_PROTO_BIT_NEC,
2055         },
2056
2057         .num_device_descs = 1,
2058         .devices = {
2059                 {   "DViCO FusionHDTV DVB-T Dual Digital 4",
2060                         { NULL },
2061                         { &cxusb_table[DVICO_BLUEBIRD_DUAL_4], NULL },
2062                 },
2063         }
2064 };
2065
2066 static struct dvb_usb_device_properties cxusb_bluebird_nano2_properties = {
2067         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2068
2069         .usb_ctrl         = CYPRESS_FX2,
2070         .identify_state   = bluebird_fx2_identify_state,
2071
2072         .size_of_priv     = sizeof(struct cxusb_state),
2073
2074         .num_adapters = 1,
2075         .adapter = {
2076                 {
2077                 .num_frontends = 1,
2078                 .fe = {{
2079                         .streaming_ctrl   = cxusb_streaming_ctrl,
2080                         .frontend_attach  = cxusb_nano2_frontend_attach,
2081                         .tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
2082                         /* parameter for the MPEG2-data transfer */
2083                         .stream = {
2084                                 .type = USB_BULK,
2085                                 .count = 5,
2086                                 .endpoint = 0x02,
2087                                 .u = {
2088                                         .bulk = {
2089                                                 .buffersize = 8192,
2090                                         }
2091                                 }
2092                         },
2093                 } },
2094                 },
2095         },
2096
2097         .power_ctrl       = cxusb_nano2_power_ctrl,
2098
2099         .i2c_algo         = &cxusb_i2c_algo,
2100
2101         .generic_bulk_ctrl_endpoint = 0x01,
2102
2103         .rc.core = {
2104                 .rc_interval    = 100,
2105                 .rc_codes       = RC_MAP_DVICO_PORTABLE,
2106                 .module_name    = KBUILD_MODNAME,
2107                 .rc_query       = cxusb_bluebird2_rc_query,
2108                 .allowed_protos = RC_PROTO_BIT_NEC,
2109         },
2110
2111         .num_device_descs = 1,
2112         .devices = {
2113                 {   "DViCO FusionHDTV DVB-T NANO2",
2114                         { NULL },
2115                         { &cxusb_table[DVICO_BLUEBIRD_DVB_T_NANO_2], NULL },
2116                 },
2117         }
2118 };
2119
2120 static struct dvb_usb_device_properties
2121 cxusb_bluebird_nano2_needsfirmware_properties = {
2122         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2123
2124         .usb_ctrl          = DEVICE_SPECIFIC,
2125         .firmware          = "/*(DEBLOBBED)*/",
2126         .download_firmware = bluebird_patch_dvico_firmware_download,
2127         .identify_state    = bluebird_fx2_identify_state,
2128
2129         .size_of_priv      = sizeof(struct cxusb_state),
2130
2131         .num_adapters = 1,
2132         .adapter = {
2133                 {
2134                 .num_frontends = 1,
2135                 .fe = {{
2136                         .streaming_ctrl   = cxusb_streaming_ctrl,
2137                         .frontend_attach  = cxusb_nano2_frontend_attach,
2138                         .tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
2139                         /* parameter for the MPEG2-data transfer */
2140                         .stream = {
2141                                 .type = USB_BULK,
2142                                 .count = 5,
2143                                 .endpoint = 0x02,
2144                                 .u = {
2145                                         .bulk = {
2146                                                 .buffersize = 8192,
2147                                         }
2148                                 }
2149                         },
2150                 } },
2151                 },
2152         },
2153
2154         .power_ctrl       = cxusb_nano2_power_ctrl,
2155
2156         .i2c_algo         = &cxusb_i2c_algo,
2157
2158         .generic_bulk_ctrl_endpoint = 0x01,
2159
2160         .rc.core = {
2161                 .rc_interval    = 100,
2162                 .rc_codes       = RC_MAP_DVICO_PORTABLE,
2163                 .module_name    = KBUILD_MODNAME,
2164                 .rc_query       = cxusb_rc_query,
2165                 .allowed_protos = RC_PROTO_BIT_NEC,
2166         },
2167
2168         .num_device_descs = 1,
2169         .devices = { {
2170                         "DViCO FusionHDTV DVB-T NANO2 w/o firmware",
2171                         { &cxusb_table[DVICO_BLUEBIRD_DVB_T_NANO_2], NULL },
2172                         { &cxusb_table[DVICO_BLUEBIRD_DVB_T_NANO_2_NFW_WARM],
2173                           NULL },
2174                 },
2175         }
2176 };
2177
2178 static struct dvb_usb_device_properties cxusb_aver_a868r_properties = {
2179         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2180
2181         .usb_ctrl         = CYPRESS_FX2,
2182
2183         .size_of_priv     = sizeof(struct cxusb_state),
2184
2185         .num_adapters = 1,
2186         .adapter = {
2187                 {
2188                 .num_frontends = 1,
2189                 .fe = {{
2190                         .streaming_ctrl   = cxusb_aver_streaming_ctrl,
2191                         .frontend_attach  = cxusb_aver_lgdt3303_frontend_attach,
2192                         .tuner_attach     = cxusb_mxl5003s_tuner_attach,
2193                         /* parameter for the MPEG2-data transfer */
2194                         .stream = {
2195                                 .type = USB_BULK,
2196                                 .count = 5,
2197                                 .endpoint = 0x04,
2198                                 .u = {
2199                                         .bulk = {
2200                                                 .buffersize = 8192,
2201                                         }
2202                                 }
2203                         },
2204                 } },
2205                 },
2206         },
2207         .power_ctrl       = cxusb_aver_power_ctrl,
2208
2209         .i2c_algo         = &cxusb_i2c_algo,
2210
2211         .generic_bulk_ctrl_endpoint = 0x01,
2212
2213         .num_device_descs = 1,
2214         .devices = {
2215                 {   "AVerMedia AVerTVHD Volar (A868R)",
2216                         { NULL },
2217                         { &cxusb_table[AVERMEDIA_VOLAR_A868R], NULL },
2218                 },
2219         }
2220 };
2221
2222 static
2223 struct dvb_usb_device_properties cxusb_bluebird_dualdig4_rev2_properties = {
2224         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2225
2226         .usb_ctrl         = CYPRESS_FX2,
2227
2228         .size_of_priv     = sizeof(struct cxusb_state),
2229
2230         .num_adapters = 1,
2231         .adapter = {
2232                 {
2233                 .size_of_priv    = sizeof(struct dib0700_adapter_state),
2234                 .num_frontends = 1,
2235                 .fe = {{
2236                         .streaming_ctrl  = cxusb_streaming_ctrl,
2237                         .frontend_attach = cxusb_dualdig4_rev2_frontend_attach,
2238                         .tuner_attach    = cxusb_dualdig4_rev2_tuner_attach,
2239                         /* parameter for the MPEG2-data transfer */
2240                         .stream = {
2241                                 .type = USB_BULK,
2242                                 .count = 7,
2243                                 .endpoint = 0x02,
2244                                 .u = {
2245                                         .bulk = {
2246                                                 .buffersize = 4096,
2247                                         }
2248                                 }
2249                         },
2250                 } },
2251                 },
2252         },
2253
2254         .power_ctrl       = cxusb_bluebird_power_ctrl,
2255
2256         .i2c_algo         = &cxusb_i2c_algo,
2257
2258         .generic_bulk_ctrl_endpoint = 0x01,
2259
2260         .rc.core = {
2261                 .rc_interval    = 100,
2262                 .rc_codes       = RC_MAP_DVICO_MCE,
2263                 .module_name    = KBUILD_MODNAME,
2264                 .rc_query       = cxusb_rc_query,
2265                 .allowed_protos = RC_PROTO_BIT_NEC,
2266         },
2267
2268         .num_device_descs = 1,
2269         .devices = {
2270                 {   "DViCO FusionHDTV DVB-T Dual Digital 4 (rev 2)",
2271                         { NULL },
2272                         { &cxusb_table[DVICO_BLUEBIRD_DUAL_4_REV_2], NULL },
2273                 },
2274         }
2275 };
2276
2277 static struct dvb_usb_device_properties cxusb_d680_dmb_properties = {
2278         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2279
2280         .usb_ctrl         = CYPRESS_FX2,
2281
2282         .size_of_priv     = sizeof(struct cxusb_state),
2283
2284         .num_adapters = 1,
2285         .adapter = {
2286                 {
2287                 .num_frontends = 1,
2288                 .fe = {{
2289                         .streaming_ctrl   = cxusb_d680_dmb_streaming_ctrl,
2290                         .frontend_attach  = cxusb_d680_dmb_frontend_attach,
2291                         .tuner_attach     = cxusb_d680_dmb_tuner_attach,
2292
2293                         /* parameter for the MPEG2-data transfer */
2294                         .stream = {
2295                                 .type = USB_BULK,
2296                                 .count = 5,
2297                                 .endpoint = 0x02,
2298                                 .u = {
2299                                         .bulk = {
2300                                                 .buffersize = 8192,
2301                                         }
2302                                 }
2303                         },
2304                 } },
2305                 },
2306         },
2307
2308         .power_ctrl       = cxusb_d680_dmb_power_ctrl,
2309
2310         .i2c_algo         = &cxusb_i2c_algo,
2311
2312         .generic_bulk_ctrl_endpoint = 0x01,
2313
2314         .rc.core = {
2315                 .rc_interval    = 100,
2316                 .rc_codes       = RC_MAP_TOTAL_MEDIA_IN_HAND_02,
2317                 .module_name    = KBUILD_MODNAME,
2318                 .rc_query       = cxusb_d680_dmb_rc_query,
2319                 .allowed_protos = RC_PROTO_BIT_UNKNOWN,
2320         },
2321
2322         .num_device_descs = 1,
2323         .devices = {
2324                 {
2325                         "Conexant DMB-TH Stick",
2326                         { NULL },
2327                         { &cxusb_table[CONEXANT_D680_DMB], NULL },
2328                 },
2329         }
2330 };
2331
2332 static struct dvb_usb_device_properties cxusb_mygica_d689_properties = {
2333         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
2334
2335         .usb_ctrl         = CYPRESS_FX2,
2336
2337         .size_of_priv     = sizeof(struct cxusb_state),
2338
2339         .num_adapters = 1,
2340         .adapter = {
2341                 {
2342                 .num_frontends = 1,
2343                 .fe = {{
2344                         .streaming_ctrl   = cxusb_d680_dmb_streaming_ctrl,
2345                         .frontend_attach  = cxusb_mygica_d689_frontend_attach,
2346                         .tuner_attach     = cxusb_mygica_d689_tuner_attach,
2347
2348                         /* parameter for the MPEG2-data transfer */
2349                         .stream = {
2350                                 .type = USB_BULK,
2351                                 .count = 5,
2352                                 .endpoint = 0x02,
2353                                 .u = {
2354                                         .bulk = {
2355                                                 .buffersize = 8192,
2356                                         }
2357                                 }
2358                         },
2359                 } },
2360                 },
2361         },
2362
2363         .power_ctrl       = cxusb_d680_dmb_power_ctrl,
2364
2365         .i2c_algo         = &cxusb_i2c_algo,
2366
2367         .generic_bulk_ctrl_endpoint = 0x01,
2368
2369         .rc.core = {
2370                 .rc_interval    = 100,
2371                 .rc_codes       = RC_MAP_D680_DMB,
2372                 .module_name    = KBUILD_MODNAME,
2373                 .rc_query       = cxusb_d680_dmb_rc_query,
2374                 .allowed_protos = RC_PROTO_BIT_UNKNOWN,
2375         },
2376
2377         .num_device_descs = 1,
2378         .devices = {
2379                 {
2380                         "Mygica D689 DMB-TH",
2381                         { NULL },
2382                         { &cxusb_table[MYGICA_D689], NULL },
2383                 },
2384         }
2385 };
2386
2387 static struct usb_driver cxusb_driver = {
2388         .name           = "dvb_usb_cxusb",
2389         .probe          = cxusb_probe,
2390         .disconnect     = cxusb_disconnect,
2391         .id_table       = cxusb_table,
2392 };
2393
2394 module_usb_driver(cxusb_driver);
2395
2396 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
2397 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
2398 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
2399 MODULE_AUTHOR("Maciej S. Szmigiero <mail@maciej.szmigiero.name>");
2400 MODULE_DESCRIPTION("Driver for Conexant USB2.0 hybrid reference design");
2401 MODULE_LICENSE("GPL");