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