V4L/DVB (6649): Add support for the DViCO FusionHDTV Dual Digital 4
[pandora-kernel.git] / drivers / media / dvb / 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 "cxusb.h"
27
28 #include "cx22702.h"
29 #include "lgdt330x.h"
30 #include "mt352.h"
31 #include "mt352_priv.h"
32 #include "zl10353.h"
33 #include "tuner-xc2028.h"
34 #include "tuner-xc2028-types.h"
35
36 /* debug */
37 static int dvb_usb_cxusb_debug;
38 module_param_named(debug, dvb_usb_cxusb_debug, int, 0644);
39 MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
40 #define deb_info(args...)   dprintk(dvb_usb_cxusb_debug,0x01,args)
41 #define deb_i2c(args...)    if (d->udev->descriptor.idVendor == USB_VID_MEDION) \
42                                 dprintk(dvb_usb_cxusb_debug,0x01,args)
43
44 static int cxusb_ctrl_msg(struct dvb_usb_device *d,
45                           u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
46 {
47         int wo = (rbuf == NULL || rlen == 0); /* write-only */
48         u8 sndbuf[1+wlen];
49         memset(sndbuf, 0, 1+wlen);
50
51         sndbuf[0] = cmd;
52         memcpy(&sndbuf[1], wbuf, wlen);
53         if (wo)
54                 return dvb_usb_generic_write(d, sndbuf, 1+wlen);
55         else
56                 return dvb_usb_generic_rw(d, sndbuf, 1+wlen, rbuf, rlen, 0);
57 }
58
59 /* GPIO */
60 static void cxusb_gpio_tuner(struct dvb_usb_device *d, int onoff)
61 {
62         struct cxusb_state *st = d->priv;
63         u8 o[2], i;
64
65         if (st->gpio_write_state[GPIO_TUNER] == onoff)
66                 return;
67
68         o[0] = GPIO_TUNER;
69         o[1] = onoff;
70         cxusb_ctrl_msg(d, CMD_GPIO_WRITE, o, 2, &i, 1);
71
72         if (i != 0x01)
73                 deb_info("gpio_write failed.\n");
74
75         st->gpio_write_state[GPIO_TUNER] = onoff;
76 }
77
78 static int cxusb_bluebird_gpio_rw(struct dvb_usb_device *d, u8 changemask,
79                                  u8 newval)
80 {
81         u8 o[2], gpio_state;
82         int rc;
83
84         o[0] = 0xff & ~changemask;      /* mask of bits to keep */
85         o[1] = newval & changemask;     /* new values for bits  */
86
87         rc = cxusb_ctrl_msg(d, CMD_BLUEBIRD_GPIO_RW, o, 2, &gpio_state, 1);
88         if (rc < 0 || (gpio_state & changemask) != (newval & changemask))
89                 deb_info("bluebird_gpio_write failed.\n");
90
91         return rc < 0 ? rc : gpio_state;
92 }
93
94 static void cxusb_bluebird_gpio_pulse(struct dvb_usb_device *d, u8 pin, int low)
95 {
96         cxusb_bluebird_gpio_rw(d, pin, low ? 0 : pin);
97         msleep(5);
98         cxusb_bluebird_gpio_rw(d, pin, low ? pin : 0);
99 }
100
101 /* I2C */
102 static int cxusb_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
103                           int num)
104 {
105         struct dvb_usb_device *d = i2c_get_adapdata(adap);
106         int i;
107
108         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
109                 return -EAGAIN;
110
111         for (i = 0; i < num; i++) {
112
113                 if (d->udev->descriptor.idVendor == USB_VID_MEDION)
114                         switch (msg[i].addr) {
115                         case 0x63:
116                                 cxusb_gpio_tuner(d, 0);
117                                 break;
118                         default:
119                                 cxusb_gpio_tuner(d, 1);
120                                 break;
121                         }
122
123                 if (msg[i].flags & I2C_M_RD) {
124                         /* read only */
125                         u8 obuf[3], ibuf[1+msg[i].len];
126                         obuf[0] = 0;
127                         obuf[1] = msg[i].len;
128                         obuf[2] = msg[i].addr;
129                         if (cxusb_ctrl_msg(d, CMD_I2C_READ,
130                                            obuf, 3,
131                                            ibuf, 1+msg[i].len) < 0) {
132                                 warn("i2c read failed");
133                                 break;
134                         }
135                         memcpy(msg[i].buf, &ibuf[1], msg[i].len);
136                 } else if (i+1 < num && (msg[i+1].flags & I2C_M_RD) &&
137                            msg[i].addr == msg[i+1].addr) {
138                         /* write to then read from same address */
139                         u8 obuf[3+msg[i].len], ibuf[1+msg[i+1].len];
140                         obuf[0] = msg[i].len;
141                         obuf[1] = msg[i+1].len;
142                         obuf[2] = msg[i].addr;
143                         memcpy(&obuf[3], msg[i].buf, msg[i].len);
144
145                         if (cxusb_ctrl_msg(d, CMD_I2C_READ,
146                                            obuf, 3+msg[i].len,
147                                            ibuf, 1+msg[i+1].len) < 0)
148                                 break;
149
150                         if (ibuf[0] != 0x08)
151                                 deb_i2c("i2c read may have failed\n");
152
153                         memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
154
155                         i++;
156                 } else {
157                         /* write only */
158                         u8 obuf[2+msg[i].len], ibuf;
159                         obuf[0] = msg[i].addr;
160                         obuf[1] = msg[i].len;
161                         memcpy(&obuf[2], msg[i].buf, msg[i].len);
162
163                         if (cxusb_ctrl_msg(d, CMD_I2C_WRITE, obuf,
164                                            2+msg[i].len, &ibuf,1) < 0)
165                                 break;
166                         if (ibuf != 0x08)
167                                 deb_i2c("i2c write may have failed\n");
168                 }
169         }
170
171         mutex_unlock(&d->i2c_mutex);
172         return i == num ? num : -EREMOTEIO;
173 }
174
175 static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
176 {
177         return I2C_FUNC_I2C;
178 }
179
180 static struct i2c_algorithm cxusb_i2c_algo = {
181         .master_xfer   = cxusb_i2c_xfer,
182         .functionality = cxusb_i2c_func,
183 };
184
185 static int cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
186 {
187         u8 b = 0;
188         if (onoff)
189                 return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
190         else
191                 return cxusb_ctrl_msg(d, CMD_POWER_OFF, &b, 1, NULL, 0);
192 }
193
194 static int cxusb_bluebird_power_ctrl(struct dvb_usb_device *d, int onoff)
195 {
196         u8 b = 0;
197         if (onoff)
198                 return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
199         else
200                 return 0;
201 }
202
203 static int cxusb_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
204 {
205         u8 buf[2] = { 0x03, 0x00 };
206         if (onoff)
207                 cxusb_ctrl_msg(adap->dev, CMD_STREAMING_ON, buf, 2, NULL, 0);
208         else
209                 cxusb_ctrl_msg(adap->dev, CMD_STREAMING_OFF, NULL, 0, NULL, 0);
210
211         return 0;
212 }
213
214 static int cxusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
215 {
216         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
217         u8 ircode[4];
218         int i;
219
220         cxusb_ctrl_msg(d, CMD_GET_IR_CODE, NULL, 0, ircode, 4);
221
222         *event = 0;
223         *state = REMOTE_NO_KEY_PRESSED;
224
225         for (i = 0; i < d->props.rc_key_map_size; i++) {
226                 if (keymap[i].custom == ircode[2] &&
227                     keymap[i].data == ircode[3]) {
228                         *event = keymap[i].event;
229                         *state = REMOTE_KEY_PRESSED;
230
231                         return 0;
232                 }
233         }
234
235         return 0;
236 }
237
238 static int cxusb_bluebird2_rc_query(struct dvb_usb_device *d, u32 *event,
239                                     int *state)
240 {
241         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
242         u8 ircode[4];
243         int i;
244         struct i2c_msg msg = { .addr = 0x6b, .flags = I2C_M_RD,
245                                .buf = ircode, .len = 4 };
246
247         *event = 0;
248         *state = REMOTE_NO_KEY_PRESSED;
249
250         if (cxusb_i2c_xfer(&d->i2c_adap, &msg, 1) != 1)
251                 return 0;
252
253         for (i = 0; i < d->props.rc_key_map_size; i++) {
254                 if (keymap[i].custom == ircode[1] &&
255                     keymap[i].data == ircode[2]) {
256                         *event = keymap[i].event;
257                         *state = REMOTE_KEY_PRESSED;
258
259                         return 0;
260                 }
261         }
262
263         return 0;
264 }
265
266 static struct dvb_usb_rc_key dvico_mce_rc_keys[] = {
267         { 0xfe, 0x02, KEY_TV },
268         { 0xfe, 0x0e, KEY_MP3 },
269         { 0xfe, 0x1a, KEY_DVD },
270         { 0xfe, 0x1e, KEY_FAVORITES },
271         { 0xfe, 0x16, KEY_SETUP },
272         { 0xfe, 0x46, KEY_POWER2 },
273         { 0xfe, 0x0a, KEY_EPG },
274         { 0xfe, 0x49, KEY_BACK },
275         { 0xfe, 0x4d, KEY_MENU },
276         { 0xfe, 0x51, KEY_UP },
277         { 0xfe, 0x5b, KEY_LEFT },
278         { 0xfe, 0x5f, KEY_RIGHT },
279         { 0xfe, 0x53, KEY_DOWN },
280         { 0xfe, 0x5e, KEY_OK },
281         { 0xfe, 0x59, KEY_INFO },
282         { 0xfe, 0x55, KEY_TAB },
283         { 0xfe, 0x0f, KEY_PREVIOUSSONG },/* Replay */
284         { 0xfe, 0x12, KEY_NEXTSONG },   /* Skip */
285         { 0xfe, 0x42, KEY_ENTER  },     /* Windows/Start */
286         { 0xfe, 0x15, KEY_VOLUMEUP },
287         { 0xfe, 0x05, KEY_VOLUMEDOWN },
288         { 0xfe, 0x11, KEY_CHANNELUP },
289         { 0xfe, 0x09, KEY_CHANNELDOWN },
290         { 0xfe, 0x52, KEY_CAMERA },
291         { 0xfe, 0x5a, KEY_TUNER },      /* Live */
292         { 0xfe, 0x19, KEY_OPEN },
293         { 0xfe, 0x0b, KEY_1 },
294         { 0xfe, 0x17, KEY_2 },
295         { 0xfe, 0x1b, KEY_3 },
296         { 0xfe, 0x07, KEY_4 },
297         { 0xfe, 0x50, KEY_5 },
298         { 0xfe, 0x54, KEY_6 },
299         { 0xfe, 0x48, KEY_7 },
300         { 0xfe, 0x4c, KEY_8 },
301         { 0xfe, 0x58, KEY_9 },
302         { 0xfe, 0x13, KEY_ANGLE },      /* Aspect */
303         { 0xfe, 0x03, KEY_0 },
304         { 0xfe, 0x1f, KEY_ZOOM },
305         { 0xfe, 0x43, KEY_REWIND },
306         { 0xfe, 0x47, KEY_PLAYPAUSE },
307         { 0xfe, 0x4f, KEY_FASTFORWARD },
308         { 0xfe, 0x57, KEY_MUTE },
309         { 0xfe, 0x0d, KEY_STOP },
310         { 0xfe, 0x01, KEY_RECORD },
311         { 0xfe, 0x4e, KEY_POWER },
312 };
313
314 static struct dvb_usb_rc_key dvico_portable_rc_keys[] = {
315         { 0xfc, 0x02, KEY_SETUP },       /* Profile */
316         { 0xfc, 0x43, KEY_POWER2 },
317         { 0xfc, 0x06, KEY_EPG },
318         { 0xfc, 0x5a, KEY_BACK },
319         { 0xfc, 0x05, KEY_MENU },
320         { 0xfc, 0x47, KEY_INFO },
321         { 0xfc, 0x01, KEY_TAB },
322         { 0xfc, 0x42, KEY_PREVIOUSSONG },/* Replay */
323         { 0xfc, 0x49, KEY_VOLUMEUP },
324         { 0xfc, 0x09, KEY_VOLUMEDOWN },
325         { 0xfc, 0x54, KEY_CHANNELUP },
326         { 0xfc, 0x0b, KEY_CHANNELDOWN },
327         { 0xfc, 0x16, KEY_CAMERA },
328         { 0xfc, 0x40, KEY_TUNER },      /* ATV/DTV */
329         { 0xfc, 0x45, KEY_OPEN },
330         { 0xfc, 0x19, KEY_1 },
331         { 0xfc, 0x18, KEY_2 },
332         { 0xfc, 0x1b, KEY_3 },
333         { 0xfc, 0x1a, KEY_4 },
334         { 0xfc, 0x58, KEY_5 },
335         { 0xfc, 0x59, KEY_6 },
336         { 0xfc, 0x15, KEY_7 },
337         { 0xfc, 0x14, KEY_8 },
338         { 0xfc, 0x17, KEY_9 },
339         { 0xfc, 0x44, KEY_ANGLE },      /* Aspect */
340         { 0xfc, 0x55, KEY_0 },
341         { 0xfc, 0x07, KEY_ZOOM },
342         { 0xfc, 0x0a, KEY_REWIND },
343         { 0xfc, 0x08, KEY_PLAYPAUSE },
344         { 0xfc, 0x4b, KEY_FASTFORWARD },
345         { 0xfc, 0x5b, KEY_MUTE },
346         { 0xfc, 0x04, KEY_STOP },
347         { 0xfc, 0x56, KEY_RECORD },
348         { 0xfc, 0x57, KEY_POWER },
349         { 0xfc, 0x41, KEY_UNKNOWN },    /* INPUT */
350         { 0xfc, 0x00, KEY_UNKNOWN },    /* HD */
351 };
352
353 static int cxusb_dee1601_demod_init(struct dvb_frontend* fe)
354 {
355         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x28 };
356         static u8 reset []         = { RESET,      0x80 };
357         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
358         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0x20 };
359         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
360         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
361
362         mt352_write(fe, clock_config,   sizeof(clock_config));
363         udelay(200);
364         mt352_write(fe, reset,          sizeof(reset));
365         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
366
367         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
368         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
369         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
370
371         return 0;
372 }
373
374 static int cxusb_mt352_demod_init(struct dvb_frontend* fe)
375 {       /* used in both lgz201 and th7579 */
376         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x29 };
377         static u8 reset []         = { RESET,      0x80 };
378         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
379         static u8 agc_cfg []       = { AGC_TARGET, 0x24, 0x20 };
380         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
381         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
382
383         mt352_write(fe, clock_config,   sizeof(clock_config));
384         udelay(200);
385         mt352_write(fe, reset,          sizeof(reset));
386         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
387
388         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
389         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
390         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
391         return 0;
392 }
393
394 static struct cx22702_config cxusb_cx22702_config = {
395         .demod_address = 0x63,
396         .output_mode = CX22702_PARALLEL_OUTPUT,
397 };
398
399 static struct lgdt330x_config cxusb_lgdt3303_config = {
400         .demod_address = 0x0e,
401         .demod_chip    = LGDT3303,
402 };
403
404 static struct mt352_config cxusb_dee1601_config = {
405         .demod_address = 0x0f,
406         .demod_init    = cxusb_dee1601_demod_init,
407 };
408
409 static struct zl10353_config cxusb_zl10353_dee1601_config = {
410         .demod_address = 0x0f,
411         .parallel_ts = 1,
412 };
413
414 static struct mt352_config cxusb_mt352_config = {
415         /* used in both lgz201 and th7579 */
416         .demod_address = 0x0f,
417         .demod_init    = cxusb_mt352_demod_init,
418 };
419
420 static struct zl10353_config cxusb_zl10353_xc3028_config = {
421         .demod_address = 0x0f,
422         .if2 = 4560,
423         .no_tuner = 1,
424         .parallel_ts = 1,
425 };
426
427 /* Callbacks for DVB USB */
428 static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_adapter *adap)
429 {
430         dvb_attach(dvb_pll_attach, adap->fe, 0x61, &adap->dev->i2c_adap,
431                    DVB_PLL_FMD1216ME);
432         return 0;
433 }
434
435 static int cxusb_dee1601_tuner_attach(struct dvb_usb_adapter *adap)
436 {
437         dvb_attach(dvb_pll_attach, adap->fe, 0x61,
438                    NULL, DVB_PLL_THOMSON_DTT7579);
439         return 0;
440 }
441
442 static int cxusb_lgz201_tuner_attach(struct dvb_usb_adapter *adap)
443 {
444         dvb_attach(dvb_pll_attach, adap->fe, 0x61, NULL, DVB_PLL_LG_Z201);
445         return 0;
446 }
447
448 static int cxusb_dtt7579_tuner_attach(struct dvb_usb_adapter *adap)
449 {
450         dvb_attach(dvb_pll_attach, adap->fe, 0x60,
451                    NULL, DVB_PLL_THOMSON_DTT7579);
452         return 0;
453 }
454
455 static int cxusb_lgh064f_tuner_attach(struct dvb_usb_adapter *adap)
456 {
457         dvb_attach(dvb_pll_attach, adap->fe, 0x61, &adap->dev->i2c_adap,
458                    DVB_PLL_LG_TDVS_H06XF);
459         return 0;
460 }
461
462 static int dvico_bluebird_xc2028_callback(void *ptr, int command, int arg)
463 {
464         struct dvb_usb_device *d = ptr;
465
466         switch (command) {
467         case XC2028_TUNER_RESET:
468                 deb_info("%s: XC2028_TUNER_RESET %d\n", __FUNCTION__, arg);
469                 cxusb_bluebird_gpio_pulse(d, 0x01, 1);
470                 break;
471         case XC2028_RESET_CLK:
472                 deb_info("%s: XC2028_RESET_CLK %d\n", __FUNCTION__, arg);
473                 break;
474         default:
475                 deb_info("%s: unknown command %d, arg %d\n", __FUNCTION__,
476                          command, arg);
477                 return -EINVAL;
478         }
479
480         return 0;
481 }
482
483 static int cxusb_dvico_xc3028_tuner_attach(struct dvb_usb_adapter *adap)
484 {
485         struct dvb_frontend      *fe;
486         struct xc2028_config      cfg = {
487                 .i2c_adap  = &adap->dev->i2c_adap,
488                 .i2c_addr  = 0x61,
489                 .video_dev = adap->dev,
490                 .callback  = dvico_bluebird_xc2028_callback,
491         };
492         static struct xc2028_ctrl ctl = {
493                 .type        = XC2028_FIRM_NORMAL,
494                 .fname       = "xc3028-dvico-au-01.fw",
495                 .max_len     = 64,
496                 .scode_table = ZARLINK456,
497         };
498
499         fe = dvb_attach(xc2028_attach, adap->fe, &cfg);
500         if (fe == NULL || fe->ops.tuner_ops.set_config == NULL)
501                 return -EIO;
502
503         fe->ops.tuner_ops.set_config(fe, &ctl);
504
505         return 0;
506 }
507
508 static int cxusb_cx22702_frontend_attach(struct dvb_usb_adapter *adap)
509 {
510         u8 b;
511         if (usb_set_interface(adap->dev->udev, 0, 6) < 0)
512                 err("set interface failed");
513
514         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, &b, 1);
515
516         if ((adap->fe = dvb_attach(cx22702_attach, &cxusb_cx22702_config,
517                                    &adap->dev->i2c_adap)) != NULL)
518                 return 0;
519
520         return -EIO;
521 }
522
523 static int cxusb_lgdt3303_frontend_attach(struct dvb_usb_adapter *adap)
524 {
525         if (usb_set_interface(adap->dev->udev, 0, 7) < 0)
526                 err("set interface failed");
527
528         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
529
530         if ((adap->fe = dvb_attach(lgdt330x_attach, &cxusb_lgdt3303_config,
531                                    &adap->dev->i2c_adap)) != NULL)
532                 return 0;
533
534         return -EIO;
535 }
536
537 static int cxusb_mt352_frontend_attach(struct dvb_usb_adapter *adap)
538 {
539         /* used in both lgz201 and th7579 */
540         if (usb_set_interface(adap->dev->udev, 0, 0) < 0)
541                 err("set interface failed");
542
543         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
544
545         if ((adap->fe = dvb_attach(mt352_attach, &cxusb_mt352_config,
546                                    &adap->dev->i2c_adap)) != NULL)
547                 return 0;
548
549         return -EIO;
550 }
551
552 static int cxusb_dee1601_frontend_attach(struct dvb_usb_adapter *adap)
553 {
554         if (usb_set_interface(adap->dev->udev, 0, 0) < 0)
555                 err("set interface failed");
556
557         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
558
559         if (((adap->fe = dvb_attach(mt352_attach, &cxusb_dee1601_config,
560                                     &adap->dev->i2c_adap)) != NULL) ||
561                 ((adap->fe = dvb_attach(zl10353_attach,
562                                         &cxusb_zl10353_dee1601_config,
563                                         &adap->dev->i2c_adap)) != NULL))
564                 return 0;
565
566         return -EIO;
567 }
568
569 static int cxusb_dualdig4_frontend_attach(struct dvb_usb_adapter *adap)
570 {
571         u8 ircode[4];
572         int i;
573         struct i2c_msg msg = { .addr = 0x6b, .flags = I2C_M_RD,
574                                .buf = ircode, .len = 4 };
575
576         if (usb_set_interface(adap->dev->udev, 0, 1) < 0)
577                 err("set interface failed");
578
579         cxusb_ctrl_msg(adap->dev, CMD_DIGITAL, NULL, 0, NULL, 0);
580
581         /* reset the tuner and demodulator */
582         cxusb_bluebird_gpio_rw(adap->dev, 0x04, 0);
583         cxusb_bluebird_gpio_pulse(adap->dev, 0x01, 1);
584         cxusb_bluebird_gpio_pulse(adap->dev, 0x02, 1);
585
586         if ((adap->fe = dvb_attach(zl10353_attach,
587                                    &cxusb_zl10353_xc3028_config,
588                                    &adap->dev->i2c_adap)) == NULL)
589                 return -EIO;
590
591         /* try to determine if there is no IR decoder on the I2C bus */
592         for (i = 0; adap->dev->props.rc_key_map != NULL && i < 5; i++) {
593                 msleep(20);
594                 if (cxusb_i2c_xfer(&adap->dev->i2c_adap, &msg, 1) != 1)
595                         goto no_IR;
596                 if (ircode[0] == 0 && ircode[1] == 0)
597                         continue;
598                 if (ircode[2] + ircode[3] != 0xff) {
599 no_IR:
600                         adap->dev->props.rc_key_map = NULL;
601                         info("No IR receiver detected on this device.");
602                         break;
603                 }
604         }
605
606         return 0;
607 }
608
609 /*
610  * DViCO bluebird firmware needs the "warm" product ID to be patched into the
611  * firmware file before download.
612  */
613
614 #define BLUEBIRD_01_ID_OFFSET 6638
615 static int bluebird_patch_dvico_firmware_download(struct usb_device *udev,
616                                                   const struct firmware *fw)
617 {
618         if (fw->size < BLUEBIRD_01_ID_OFFSET + 4)
619                 return -EINVAL;
620
621         if (fw->data[BLUEBIRD_01_ID_OFFSET] == (USB_VID_DVICO & 0xff) &&
622             fw->data[BLUEBIRD_01_ID_OFFSET + 1] == USB_VID_DVICO >> 8) {
623
624                 fw->data[BLUEBIRD_01_ID_OFFSET + 2] =
625                         le16_to_cpu(udev->descriptor.idProduct) + 1;
626                 fw->data[BLUEBIRD_01_ID_OFFSET + 3] =
627                         le16_to_cpu(udev->descriptor.idProduct) >> 8;
628
629                 return usb_cypress_load_firmware(udev, fw, CYPRESS_FX2);
630         }
631
632         return -EINVAL;
633 }
634
635 /* DVB USB Driver stuff */
636 static struct dvb_usb_device_properties cxusb_medion_properties;
637 static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties;
638 static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties;
639 static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties;
640 static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties;
641 static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties;
642
643 static int cxusb_probe(struct usb_interface *intf,
644                        const struct usb_device_id *id)
645 {
646         if (dvb_usb_device_init(intf,&cxusb_medion_properties,THIS_MODULE,NULL) == 0 ||
647                 dvb_usb_device_init(intf,&cxusb_bluebird_lgh064f_properties,THIS_MODULE,NULL) == 0 ||
648                 dvb_usb_device_init(intf,&cxusb_bluebird_dee1601_properties,THIS_MODULE,NULL) == 0 ||
649                 dvb_usb_device_init(intf,&cxusb_bluebird_lgz201_properties,THIS_MODULE,NULL) == 0 ||
650                 dvb_usb_device_init(intf,&cxusb_bluebird_dtt7579_properties,THIS_MODULE,NULL) == 0 ||
651                 dvb_usb_device_init(intf,&cxusb_bluebird_dualdig4_properties,THIS_MODULE,NULL) == 0) {
652                 return 0;
653         }
654
655         return -EINVAL;
656 }
657
658 static struct usb_device_id cxusb_table [] = {
659         { USB_DEVICE(USB_VID_MEDION, USB_PID_MEDION_MD95700) },
660         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_COLD) },
661         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_WARM) },
662         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_1_COLD) },
663         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_1_WARM) },
664         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LGZ201_COLD) },
665         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LGZ201_WARM) },
666         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_TH7579_COLD) },
667         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_TH7579_WARM) },
668         { USB_DEVICE(USB_VID_DVICO, USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_COLD) },
669         { USB_DEVICE(USB_VID_DVICO, USB_PID_DIGITALNOW_BLUEBIRD_DUAL_1_WARM) },
670         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_2_COLD) },
671         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_2_WARM) },
672         { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DUAL_4) },
673         {}              /* Terminating entry */
674 };
675 MODULE_DEVICE_TABLE (usb, cxusb_table);
676
677 static struct dvb_usb_device_properties cxusb_medion_properties = {
678         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
679
680         .usb_ctrl = CYPRESS_FX2,
681
682         .size_of_priv     = sizeof(struct cxusb_state),
683
684         .num_adapters = 1,
685         .adapter = {
686                 {
687                         .streaming_ctrl   = cxusb_streaming_ctrl,
688                         .frontend_attach  = cxusb_cx22702_frontend_attach,
689                         .tuner_attach     = cxusb_fmd1216me_tuner_attach,
690                         /* parameter for the MPEG2-data transfer */
691                                         .stream = {
692                                                 .type = USB_BULK,
693                                 .count = 5,
694                                 .endpoint = 0x02,
695                                 .u = {
696                                         .bulk = {
697                                                 .buffersize = 8192,
698                                         }
699                                 }
700                         },
701
702                 },
703         },
704         .power_ctrl       = cxusb_power_ctrl,
705
706         .i2c_algo         = &cxusb_i2c_algo,
707
708         .generic_bulk_ctrl_endpoint = 0x01,
709
710         .num_device_descs = 1,
711         .devices = {
712                 {   "Medion MD95700 (MDUSBTV-HYBRID)",
713                         { NULL },
714                         { &cxusb_table[0], NULL },
715                 },
716         }
717 };
718
719 static struct dvb_usb_device_properties cxusb_bluebird_lgh064f_properties = {
720         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
721
722         .usb_ctrl          = DEVICE_SPECIFIC,
723         .firmware          = "dvb-usb-bluebird-01.fw",
724         .download_firmware = bluebird_patch_dvico_firmware_download,
725         /* use usb alt setting 0 for EP4 transfer (dvb-t),
726            use usb alt setting 7 for EP2 transfer (atsc) */
727
728         .size_of_priv     = sizeof(struct cxusb_state),
729
730         .num_adapters = 1,
731         .adapter = {
732                 {
733                         .streaming_ctrl   = cxusb_streaming_ctrl,
734                         .frontend_attach  = cxusb_lgdt3303_frontend_attach,
735                         .tuner_attach     = cxusb_lgh064f_tuner_attach,
736
737                         /* parameter for the MPEG2-data transfer */
738                                         .stream = {
739                                                 .type = USB_BULK,
740                                 .count = 5,
741                                 .endpoint = 0x02,
742                                 .u = {
743                                         .bulk = {
744                                                 .buffersize = 8192,
745                                         }
746                                 }
747                         },
748                 },
749         },
750
751         .power_ctrl       = cxusb_bluebird_power_ctrl,
752
753         .i2c_algo         = &cxusb_i2c_algo,
754
755         .rc_interval      = 100,
756         .rc_key_map       = dvico_portable_rc_keys,
757         .rc_key_map_size  = ARRAY_SIZE(dvico_portable_rc_keys),
758         .rc_query         = cxusb_rc_query,
759
760         .generic_bulk_ctrl_endpoint = 0x01,
761
762         .num_device_descs = 1,
763         .devices = {
764                 {   "DViCO FusionHDTV5 USB Gold",
765                         { &cxusb_table[1], NULL },
766                         { &cxusb_table[2], NULL },
767                 },
768         }
769 };
770
771 static struct dvb_usb_device_properties cxusb_bluebird_dee1601_properties = {
772         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
773
774         .usb_ctrl          = DEVICE_SPECIFIC,
775         .firmware          = "dvb-usb-bluebird-01.fw",
776         .download_firmware = bluebird_patch_dvico_firmware_download,
777         /* use usb alt setting 0 for EP4 transfer (dvb-t),
778            use usb alt setting 7 for EP2 transfer (atsc) */
779
780         .size_of_priv     = sizeof(struct cxusb_state),
781
782         .num_adapters = 1,
783         .adapter = {
784                 {
785                         .streaming_ctrl   = cxusb_streaming_ctrl,
786                         .frontend_attach  = cxusb_dee1601_frontend_attach,
787                         .tuner_attach     = cxusb_dee1601_tuner_attach,
788                         /* parameter for the MPEG2-data transfer */
789                         .stream = {
790                                 .type = USB_BULK,
791                                 .count = 5,
792                                 .endpoint = 0x04,
793                                 .u = {
794                                         .bulk = {
795                                                 .buffersize = 8192,
796                                         }
797                                 }
798                         },
799                 },
800         },
801
802         .power_ctrl       = cxusb_bluebird_power_ctrl,
803
804         .i2c_algo         = &cxusb_i2c_algo,
805
806         .rc_interval      = 150,
807         .rc_key_map       = dvico_mce_rc_keys,
808         .rc_key_map_size  = ARRAY_SIZE(dvico_mce_rc_keys),
809         .rc_query         = cxusb_rc_query,
810
811         .generic_bulk_ctrl_endpoint = 0x01,
812
813         .num_device_descs = 3,
814         .devices = {
815                 {   "DViCO FusionHDTV DVB-T Dual USB",
816                         { &cxusb_table[3], NULL },
817                         { &cxusb_table[4], NULL },
818                 },
819                 {   "DigitalNow DVB-T Dual USB",
820                         { &cxusb_table[9],  NULL },
821                         { &cxusb_table[10], NULL },
822                 },
823                 {   "DViCO FusionHDTV DVB-T Dual Digital 2",
824                         { &cxusb_table[11], NULL },
825                         { &cxusb_table[12], NULL },
826                 },
827         }
828 };
829
830 static struct dvb_usb_device_properties cxusb_bluebird_lgz201_properties = {
831         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
832
833         .usb_ctrl          = DEVICE_SPECIFIC,
834         .firmware          = "dvb-usb-bluebird-01.fw",
835         .download_firmware = bluebird_patch_dvico_firmware_download,
836         /* use usb alt setting 0 for EP4 transfer (dvb-t),
837            use usb alt setting 7 for EP2 transfer (atsc) */
838
839         .size_of_priv     = sizeof(struct cxusb_state),
840
841         .num_adapters = 2,
842         .adapter = {
843                 {
844                         .streaming_ctrl   = cxusb_streaming_ctrl,
845                         .frontend_attach  = cxusb_mt352_frontend_attach,
846                         .tuner_attach     = cxusb_lgz201_tuner_attach,
847
848                         /* parameter for the MPEG2-data transfer */
849                         .stream = {
850                                 .type = USB_BULK,
851                                 .count = 5,
852                                 .endpoint = 0x04,
853                                 .u = {
854                                         .bulk = {
855                                                 .buffersize = 8192,
856                                         }
857                                 }
858                         },
859                 },
860         },
861         .power_ctrl       = cxusb_bluebird_power_ctrl,
862
863         .i2c_algo         = &cxusb_i2c_algo,
864
865         .rc_interval      = 100,
866         .rc_key_map       = dvico_portable_rc_keys,
867         .rc_key_map_size  = ARRAY_SIZE(dvico_portable_rc_keys),
868         .rc_query         = cxusb_rc_query,
869
870         .generic_bulk_ctrl_endpoint = 0x01,
871         .num_device_descs = 1,
872         .devices = {
873                 {   "DViCO FusionHDTV DVB-T USB (LGZ201)",
874                         { &cxusb_table[5], NULL },
875                         { &cxusb_table[6], NULL },
876                 },
877         }
878 };
879
880 static struct dvb_usb_device_properties cxusb_bluebird_dtt7579_properties = {
881         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
882
883         .usb_ctrl          = DEVICE_SPECIFIC,
884         .firmware          = "dvb-usb-bluebird-01.fw",
885         .download_firmware = bluebird_patch_dvico_firmware_download,
886         /* use usb alt setting 0 for EP4 transfer (dvb-t),
887            use usb alt setting 7 for EP2 transfer (atsc) */
888
889         .size_of_priv     = sizeof(struct cxusb_state),
890
891         .num_adapters = 1,
892         .adapter = {
893                 {
894                         .streaming_ctrl   = cxusb_streaming_ctrl,
895                         .frontend_attach  = cxusb_mt352_frontend_attach,
896                         .tuner_attach     = cxusb_dtt7579_tuner_attach,
897
898                         /* parameter for the MPEG2-data transfer */
899                         .stream = {
900                                 .type = USB_BULK,
901                                 .count = 5,
902                                 .endpoint = 0x04,
903                                 .u = {
904                                         .bulk = {
905                                                 .buffersize = 8192,
906                                         }
907                                 }
908                         },
909                 },
910         },
911         .power_ctrl       = cxusb_bluebird_power_ctrl,
912
913         .i2c_algo         = &cxusb_i2c_algo,
914
915         .rc_interval      = 100,
916         .rc_key_map       = dvico_portable_rc_keys,
917         .rc_key_map_size  = ARRAY_SIZE(dvico_portable_rc_keys),
918         .rc_query         = cxusb_rc_query,
919
920         .generic_bulk_ctrl_endpoint = 0x01,
921
922         .num_device_descs = 1,
923         .devices = {
924                 {   "DViCO FusionHDTV DVB-T USB (TH7579)",
925                         { &cxusb_table[7], NULL },
926                         { &cxusb_table[8], NULL },
927                 },
928         }
929 };
930
931 static struct dvb_usb_device_properties cxusb_bluebird_dualdig4_properties = {
932         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
933
934         .usb_ctrl         = CYPRESS_FX2,
935
936         .size_of_priv     = sizeof(struct cxusb_state),
937
938         .num_adapters = 1,
939         .adapter = {
940                 {
941                         .streaming_ctrl   = cxusb_streaming_ctrl,
942                         .frontend_attach  = cxusb_dualdig4_frontend_attach,
943                         .tuner_attach     = cxusb_dvico_xc3028_tuner_attach,
944                         /* parameter for the MPEG2-data transfer */
945                         .stream = {
946                                 .type = USB_BULK,
947                                 .count = 5,
948                                 .endpoint = 0x02,
949                                 .u = {
950                                         .bulk = {
951                                                 .buffersize = 8192,
952                                         }
953                                 }
954                         },
955                 },
956         },
957
958         .power_ctrl       = cxusb_power_ctrl,
959
960         .i2c_algo         = &cxusb_i2c_algo,
961
962         .generic_bulk_ctrl_endpoint = 0x01,
963
964         .rc_interval      = 100,
965         .rc_key_map       = dvico_mce_rc_keys,
966         .rc_key_map_size  = ARRAY_SIZE(dvico_mce_rc_keys),
967         .rc_query         = cxusb_bluebird2_rc_query,
968
969         .num_device_descs = 1,
970         .devices = {
971                 {   "DViCO FusionHDTV DVB-T Dual Digital 4",
972                         { NULL },
973                         { &cxusb_table[13], NULL },
974                 },
975         }
976 };
977
978 static struct usb_driver cxusb_driver = {
979         .name           = "dvb_usb_cxusb",
980         .probe          = cxusb_probe,
981         .disconnect     = dvb_usb_device_exit,
982         .id_table       = cxusb_table,
983 };
984
985 /* module stuff */
986 static int __init cxusb_module_init(void)
987 {
988         int result;
989         if ((result = usb_register(&cxusb_driver))) {
990                 err("usb_register failed. Error number %d",result);
991                 return result;
992         }
993
994         return 0;
995 }
996
997 static void __exit cxusb_module_exit(void)
998 {
999         /* deregister this driver from the USB subsystem */
1000         usb_deregister(&cxusb_driver);
1001 }
1002
1003 module_init (cxusb_module_init);
1004 module_exit (cxusb_module_exit);
1005
1006 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
1007 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1008 MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
1009 MODULE_DESCRIPTION("Driver for Conexant USB2.0 hybrid reference design");
1010 MODULE_VERSION("1.0-alpha");
1011 MODULE_LICENSE("GPL");