]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/dvb/dvb-usb/cxusb.c
V4L/DVB (3327): Remove DViCO specific firmware hacks from the generic code.
[karo-tx-linux.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  * Use the cx25840-driver for the analogue part
15  *
16  * Copyright (C) 2005 Patrick Boettcher (patrick.boettcher@desy.de)
17  * Copyright (C) 2005 Michael Krufky (mkrufky@m1k.net)
18  * Copyright (C) 2006 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
33 /* debug */
34 int dvb_usb_cxusb_debug;
35 module_param_named(debug,dvb_usb_cxusb_debug, int, 0644);
36 MODULE_PARM_DESC(debug, "set debugging level (1=rc (or-able))." DVB_USB_DEBUG_STATUS);
37
38 static int cxusb_ctrl_msg(struct dvb_usb_device *d,
39                 u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen)
40 {
41         int wo = (rbuf == NULL || rlen == 0); /* write-only */
42         u8 sndbuf[1+wlen];
43         memset(sndbuf,0,1+wlen);
44
45         sndbuf[0] = cmd;
46         memcpy(&sndbuf[1],wbuf,wlen);
47         if (wo)
48                 dvb_usb_generic_write(d,sndbuf,1+wlen);
49         else
50                 dvb_usb_generic_rw(d,sndbuf,1+wlen,rbuf,rlen,0);
51
52         return 0;
53 }
54
55 /* GPIO */
56 static void cxusb_gpio_tuner(struct dvb_usb_device *d, int onoff)
57 {
58         struct cxusb_state *st = d->priv;
59         u8 o[2],i;
60
61         if (st->gpio_write_state[GPIO_TUNER] == onoff)
62                 return;
63
64         o[0] = GPIO_TUNER;
65         o[1] = onoff;
66         cxusb_ctrl_msg(d,CMD_GPIO_WRITE,o,2,&i,1);
67
68         if (i != 0x01)
69                 deb_info("gpio_write failed.\n");
70
71         st->gpio_write_state[GPIO_TUNER] = onoff;
72 }
73
74 /* I2C */
75 static int cxusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
76 {
77         struct dvb_usb_device *d = i2c_get_adapdata(adap);
78         int i;
79
80         if (down_interruptible(&d->i2c_sem) < 0)
81                 return -EAGAIN;
82
83         if (num > 2)
84                 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
85
86         for (i = 0; i < num; i++) {
87
88                 switch (msg[i].addr) {
89                         case 0x63:
90                                 cxusb_gpio_tuner(d,0);
91                                 break;
92                         default:
93                                 cxusb_gpio_tuner(d,1);
94                                 break;
95                 }
96
97                 /* read request */
98                 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
99                         u8 obuf[3+msg[i].len], ibuf[1+msg[i+1].len];
100                         obuf[0] = msg[i].len;
101                         obuf[1] = msg[i+1].len;
102                         obuf[2] = msg[i].addr;
103                         memcpy(&obuf[3],msg[i].buf,msg[i].len);
104
105                         if (cxusb_ctrl_msg(d, CMD_I2C_READ,
106                                                 obuf, 3+msg[i].len,
107                                                 ibuf, 1+msg[i+1].len) < 0)
108                                 break;
109
110                         if (ibuf[0] != 0x08)
111                                 deb_info("i2c read could have been failed\n");
112
113                         memcpy(msg[i+1].buf,&ibuf[1],msg[i+1].len);
114
115                         i++;
116                 } else { /* write */
117                         u8 obuf[2+msg[i].len], ibuf;
118                         obuf[0] = msg[i].addr;
119                         obuf[1] = msg[i].len;
120                         memcpy(&obuf[2],msg[i].buf,msg[i].len);
121
122                         if (cxusb_ctrl_msg(d,CMD_I2C_WRITE, obuf, 2+msg[i].len, &ibuf,1) < 0)
123                                 break;
124                         if (ibuf != 0x08)
125                                 deb_info("i2c write could have been failed\n");
126                 }
127         }
128
129         up(&d->i2c_sem);
130         return i;
131 }
132
133 static u32 cxusb_i2c_func(struct i2c_adapter *adapter)
134 {
135         return I2C_FUNC_I2C;
136 }
137
138 static struct i2c_algorithm cxusb_i2c_algo = {
139         .master_xfer   = cxusb_i2c_xfer,
140         .functionality = cxusb_i2c_func,
141 };
142
143 static int cxusb_power_ctrl(struct dvb_usb_device *d, int onoff)
144 {
145         u8 b = 0;
146         if (onoff)
147                 return cxusb_ctrl_msg(d, CMD_POWER_ON, &b, 1, NULL, 0);
148         else
149                 return cxusb_ctrl_msg(d, CMD_POWER_OFF, &b, 1, NULL, 0);
150 }
151
152 static int cxusb_streaming_ctrl(struct dvb_usb_device *d, int onoff)
153 {
154         u8 buf[2] = { 0x03, 0x00 };
155         if (onoff)
156                 cxusb_ctrl_msg(d,CMD_STREAMING_ON, buf, 2, NULL, 0);
157         else
158                 cxusb_ctrl_msg(d,CMD_STREAMING_OFF, NULL, 0, NULL, 0);
159
160         return 0;
161 }
162
163 static int cxusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
164 {
165         struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
166         u8 ircode[4];
167         int i;
168
169         cxusb_ctrl_msg(d, CMD_GET_IR_CODE, NULL, 0, ircode, 4);
170
171         *event = 0;
172         *state = REMOTE_NO_KEY_PRESSED;
173
174         for (i = 0; i < d->props.rc_key_map_size; i++) {
175                 if (keymap[i].custom == ircode[2] &&
176                     keymap[i].data == ircode[3]) {
177                         *event = keymap[i].event;
178                         *state = REMOTE_KEY_PRESSED;
179
180                         return 0;
181                 }
182         }
183
184         return 0;
185 }
186
187 struct dvb_usb_rc_key dvico_mce_rc_keys[] = {
188         { 0xfe, 0x02, KEY_TV },
189         { 0xfe, 0x0e, KEY_MP3 },
190         { 0xfe, 0x1a, KEY_DVD },
191         { 0xfe, 0x1e, KEY_FAVORITES },
192         { 0xfe, 0x16, KEY_SETUP },
193         { 0xfe, 0x46, KEY_POWER2 },
194         { 0xfe, 0x0a, KEY_EPG },
195         { 0xfe, 0x49, KEY_BACK },
196         { 0xfe, 0x4d, KEY_MENU },
197         { 0xfe, 0x51, KEY_UP },
198         { 0xfe, 0x5b, KEY_LEFT },
199         { 0xfe, 0x5f, KEY_RIGHT },
200         { 0xfe, 0x53, KEY_DOWN },
201         { 0xfe, 0x5e, KEY_OK },
202         { 0xfe, 0x59, KEY_INFO },
203         { 0xfe, 0x55, KEY_TAB },
204         { 0xfe, 0x0f, KEY_PREVIOUSSONG },/* Replay */
205         { 0xfe, 0x12, KEY_NEXTSONG },   /* Skip */
206         { 0xfe, 0x42, KEY_ENTER  },     /* Windows/Start */
207         { 0xfe, 0x15, KEY_VOLUMEUP },
208         { 0xfe, 0x05, KEY_VOLUMEDOWN },
209         { 0xfe, 0x11, KEY_CHANNELUP },
210         { 0xfe, 0x09, KEY_CHANNELDOWN },
211         { 0xfe, 0x52, KEY_CAMERA },
212         { 0xfe, 0x5a, KEY_TUNER },      /* Live */
213         { 0xfe, 0x19, KEY_OPEN },
214         { 0xfe, 0x0b, KEY_1 },
215         { 0xfe, 0x17, KEY_2 },
216         { 0xfe, 0x1b, KEY_3 },
217         { 0xfe, 0x07, KEY_4 },
218         { 0xfe, 0x50, KEY_5 },
219         { 0xfe, 0x54, KEY_6 },
220         { 0xfe, 0x48, KEY_7 },
221         { 0xfe, 0x4c, KEY_8 },
222         { 0xfe, 0x58, KEY_9 },
223         { 0xfe, 0x13, KEY_ANGLE },      /* Aspect */
224         { 0xfe, 0x03, KEY_0 },
225         { 0xfe, 0x1f, KEY_ZOOM },
226         { 0xfe, 0x43, KEY_REWIND },
227         { 0xfe, 0x47, KEY_PLAYPAUSE },
228         { 0xfe, 0x4f, KEY_FASTFORWARD },
229         { 0xfe, 0x57, KEY_MUTE },
230         { 0xfe, 0x0d, KEY_STOP },
231         { 0xfe, 0x01, KEY_RECORD },
232         { 0xfe, 0x4e, KEY_POWER },
233 };
234
235 static int cxusb_dee1601_demod_init(struct dvb_frontend* fe)
236 {
237         static u8 clock_config []  = { CLOCK_CTL,  0x38, 0x38 };
238         static u8 reset []         = { RESET,      0x80 };
239         static u8 adc_ctl_1_cfg [] = { ADC_CTL_1,  0x40 };
240         static u8 agc_cfg []       = { AGC_TARGET, 0x28, 0x20 };
241         static u8 gpp_ctl_cfg []   = { GPP_CTL,    0x33 };
242         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
243
244         mt352_write(fe, clock_config,   sizeof(clock_config));
245         udelay(200);
246         mt352_write(fe, reset,          sizeof(reset));
247         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
248
249         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
250         mt352_write(fe, gpp_ctl_cfg,    sizeof(gpp_ctl_cfg));
251         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
252
253         return 0;
254 }
255
256 struct cx22702_config cxusb_cx22702_config = {
257         .demod_address = 0x63,
258
259         .output_mode = CX22702_PARALLEL_OUTPUT,
260
261         .pll_init = dvb_usb_pll_init_i2c,
262         .pll_set  = dvb_usb_pll_set_i2c,
263 };
264
265 struct lgdt330x_config cxusb_lgdt330x_config = {
266         .demod_address = 0x0e,
267         .demod_chip    = LGDT3303,
268         .pll_set       = dvb_usb_pll_set_i2c,
269 };
270
271 struct mt352_config cxusb_dee1601_config = {
272         .demod_address = 0x0f,
273         .demod_init    = cxusb_dee1601_demod_init,
274         .pll_set       = dvb_usb_pll_set,
275 };
276
277 /* Callbacks for DVB USB */
278 static int cxusb_fmd1216me_tuner_attach(struct dvb_usb_device *d)
279 {
280         u8 bpll[4] = { 0x0b, 0xdc, 0x9c, 0xa0 };
281         d->pll_addr = 0x61;
282         memcpy(d->pll_init, bpll, 4);
283         d->pll_desc = &dvb_pll_fmd1216me;
284         return 0;
285 }
286
287 static int cxusb_lgh064f_tuner_attach(struct dvb_usb_device *d)
288 {
289         u8 bpll[4] = { 0x00, 0x00, 0x18, 0x50 };
290         /* bpll[2] : unset bit 3, set bits 4&5
291            bpll[3] : 0x50 - digital, 0x20 - analog */
292         d->pll_addr = 0x61;
293         memcpy(d->pll_init, bpll, 4);
294         d->pll_desc = &dvb_pll_tdvs_tua6034;
295         return 0;
296 }
297
298 static int cxusb_dee1601_tuner_attach(struct dvb_usb_device *d)
299 {
300         d->pll_addr = 0x61;
301         d->pll_desc = &dvb_pll_thomson_dtt7579;
302         return 0;
303 }
304
305 static int cxusb_cx22702_frontend_attach(struct dvb_usb_device *d)
306 {
307         u8 b;
308         if (usb_set_interface(d->udev,0,6) < 0)
309                 err("set interface failed");
310
311         cxusb_ctrl_msg(d,CMD_DIGITAL, NULL, 0, &b, 1);
312
313         if ((d->fe = cx22702_attach(&cxusb_cx22702_config, &d->i2c_adap)) != NULL)
314                 return 0;
315
316         return -EIO;
317 }
318
319 static int cxusb_lgdt330x_frontend_attach(struct dvb_usb_device *d)
320 {
321         if (usb_set_interface(d->udev,0,7) < 0)
322                 err("set interface failed");
323
324         cxusb_ctrl_msg(d,CMD_DIGITAL, NULL, 0, NULL, 0);
325
326         if ((d->fe = lgdt330x_attach(&cxusb_lgdt330x_config, &d->i2c_adap)) != NULL)
327                 return 0;
328
329         return -EIO;
330 }
331
332 static int cxusb_dee1601_frontend_attach(struct dvb_usb_device *d)
333 {
334         if (usb_set_interface(d->udev,0,0) < 0)
335                 err("set interface failed");
336
337         cxusb_ctrl_msg(d,CMD_DIGITAL, NULL, 0, NULL, 0);
338
339         if ((d->fe = mt352_attach(&cxusb_dee1601_config, &d->i2c_adap)) != NULL)
340                 return 0;
341
342         return -EIO;
343 }
344
345 /*
346  * DViCO bluebird firmware needs the "warm" product ID to be patched into the
347  * firmware file before download.
348  */
349
350 #define BLUEBIRD_01_ID_OFFSET 6638
351 static int bluebird_patch_dvico_firmware_download(struct usb_device *udev, const struct firmware *fw)
352 {
353         if (fw->size < BLUEBIRD_01_ID_OFFSET + 4)
354                 return -EINVAL;
355
356         if (fw->data[BLUEBIRD_01_ID_OFFSET] == (USB_VID_DVICO & 0xff) &&
357             fw->data[BLUEBIRD_01_ID_OFFSET + 1] == USB_VID_DVICO >> 8) {
358
359                 /* FIXME: are we allowed to change the fw-data ? */
360                 fw->data[BLUEBIRD_01_ID_OFFSET + 2] = udev->descriptor.idProduct + 1;
361                 fw->data[BLUEBIRD_01_ID_OFFSET + 3] = udev->descriptor.idProduct >> 8;
362
363                 return usb_cypress_load_firmware(udev,fw,CYPRESS_FX2);
364         }
365
366         return -EINVAL;
367 }
368
369 /* DVB USB Driver stuff */
370 static struct dvb_usb_properties cxusb_medion_properties;
371 static struct dvb_usb_properties cxusb_bluebird_lgh064f_properties;
372 static struct dvb_usb_properties cxusb_bluebird_dee1601_properties;
373
374 static int cxusb_probe(struct usb_interface *intf,
375                 const struct usb_device_id *id)
376 {
377         if (dvb_usb_device_init(intf,&cxusb_medion_properties,THIS_MODULE,NULL) == 0 ||
378                 dvb_usb_device_init(intf,&cxusb_bluebird_lgh064f_properties,THIS_MODULE,NULL) == 0 ||
379                 dvb_usb_device_init(intf,&cxusb_bluebird_dee1601_properties,THIS_MODULE,NULL) == 0) {
380                 return 0;
381         }
382
383         return -EINVAL;
384 }
385
386 static struct usb_device_id cxusb_table [] = {
387                 { USB_DEVICE(USB_VID_MEDION, USB_PID_MEDION_MD95700) },
388                 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_COLD) },
389                 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_LG064F_WARM) },
390                 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DEE1601_COLD) },
391                 { USB_DEVICE(USB_VID_DVICO, USB_PID_DVICO_BLUEBIRD_DEE1601_WARM) },
392                 {}              /* Terminating entry */
393 };
394 MODULE_DEVICE_TABLE (usb, cxusb_table);
395
396 static struct dvb_usb_properties cxusb_medion_properties = {
397         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
398
399         .usb_ctrl = CYPRESS_FX2,
400
401         .size_of_priv     = sizeof(struct cxusb_state),
402
403         .streaming_ctrl   = cxusb_streaming_ctrl,
404         .power_ctrl       = cxusb_power_ctrl,
405         .frontend_attach  = cxusb_cx22702_frontend_attach,
406         .tuner_attach     = cxusb_fmd1216me_tuner_attach,
407
408         .i2c_algo         = &cxusb_i2c_algo,
409
410         .generic_bulk_ctrl_endpoint = 0x01,
411         /* parameter for the MPEG2-data transfer */
412         .urb = {
413                 .type = DVB_USB_BULK,
414                 .count = 5,
415                 .endpoint = 0x02,
416                 .u = {
417                         .bulk = {
418                                 .buffersize = 8192,
419                         }
420                 }
421         },
422
423         .num_device_descs = 1,
424         .devices = {
425                 {   "Medion MD95700 (MDUSBTV-HYBRID)",
426                         { NULL },
427                         { &cxusb_table[0], NULL },
428                 },
429         }
430 };
431
432 static struct dvb_usb_properties cxusb_bluebird_lgh064f_properties = {
433         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
434
435         .usb_ctrl          = DEVICE_SPECIFIC,
436         .firmware          = "dvb-usb-bluebird-01.fw",
437         .download_firmware = bluebird_patch_dvico_firmware_download,
438         /* use usb alt setting 0 for EP4 transfer (dvb-t),
439            use usb alt setting 7 for EP2 transfer (atsc) */
440
441         .size_of_priv     = sizeof(struct cxusb_state),
442
443         .streaming_ctrl   = cxusb_streaming_ctrl,
444         .power_ctrl       = cxusb_power_ctrl,
445         .frontend_attach  = cxusb_lgdt330x_frontend_attach,
446         .tuner_attach     = cxusb_lgh064f_tuner_attach,
447
448         .i2c_algo         = &cxusb_i2c_algo,
449
450         .generic_bulk_ctrl_endpoint = 0x01,
451         /* parameter for the MPEG2-data transfer */
452         .urb = {
453                 .type = DVB_USB_BULK,
454                 .count = 5,
455                 .endpoint = 0x02,
456                 .u = {
457                         .bulk = {
458                                 .buffersize = 8192,
459                         }
460                 }
461         },
462
463         .num_device_descs = 1,
464         .devices = {
465                 {   "DViCO FusionHDTV5 USB Gold",
466                         { &cxusb_table[1], NULL },
467                         { &cxusb_table[2], NULL },
468                 },
469         }
470 };
471
472 static struct dvb_usb_properties cxusb_bluebird_dee1601_properties = {
473         .caps = DVB_USB_IS_AN_I2C_ADAPTER,
474
475         .usb_ctrl          = DEVICE_SPECIFIC,
476         .firmware          = "dvb-usb-bluebird-01.fw",
477         .download_firmware = bluebird_patch_dvico_firmware_download,
478         /* use usb alt setting 0 for EP4 transfer (dvb-t),
479            use usb alt setting 7 for EP2 transfer (atsc) */
480
481         .size_of_priv     = sizeof(struct cxusb_state),
482
483         .streaming_ctrl   = cxusb_streaming_ctrl,
484         .power_ctrl       = cxusb_power_ctrl,
485         .frontend_attach  = cxusb_dee1601_frontend_attach,
486         .tuner_attach     = cxusb_dee1601_tuner_attach,
487
488         .i2c_algo         = &cxusb_i2c_algo,
489
490         .rc_interval      = 150,
491         .rc_key_map       = dvico_mce_rc_keys,
492         .rc_key_map_size  = ARRAY_SIZE(dvico_mce_rc_keys),
493         .rc_query         = cxusb_rc_query,
494
495         .generic_bulk_ctrl_endpoint = 0x01,
496         /* parameter for the MPEG2-data transfer */
497         .urb = {
498                 .type = DVB_USB_BULK,
499                 .count = 5,
500                 .endpoint = 0x04,
501                 .u = {
502                         .bulk = {
503                                 .buffersize = 8192,
504                         }
505                 }
506         },
507
508         .num_device_descs = 1,
509         .devices = {
510                 {   "DViCO FusionHDTV DVB-T Dual USB",
511                         { &cxusb_table[3], NULL },
512                         { &cxusb_table[4], NULL },
513                 },
514         }
515 };
516
517 static struct usb_driver cxusb_driver = {
518         .name           = "dvb_usb_cxusb",
519         .probe          = cxusb_probe,
520         .disconnect = dvb_usb_device_exit,
521         .id_table       = cxusb_table,
522 };
523
524 /* module stuff */
525 static int __init cxusb_module_init(void)
526 {
527         int result;
528         if ((result = usb_register(&cxusb_driver))) {
529                 err("usb_register failed. Error number %d",result);
530                 return result;
531         }
532
533         return 0;
534 }
535
536 static void __exit cxusb_module_exit(void)
537 {
538         /* deregister this driver from the USB subsystem */
539         usb_deregister(&cxusb_driver);
540 }
541
542 module_init (cxusb_module_init);
543 module_exit (cxusb_module_exit);
544
545 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@desy.de>");
546 MODULE_DESCRIPTION("Driver for Conexant USB2.0 hybrid reference design");
547 MODULE_VERSION("1.0-alpha");
548 MODULE_LICENSE("GPL");