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