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