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