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