Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / em28xx / em28xx-dvb.c
blob1b96356b3ab270c2e195c520b3d455c16092a24d
1 /*
2 DVB device driver for em28xx
4 (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org>
6 (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7 - Fixes for the driver to properly work with HVR-950
8 - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
9 - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
11 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
13 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
14 (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
15 (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License.
22 #include <linux/kernel.h>
23 #include <linux/usb.h>
25 #include "em28xx.h"
26 #include <media/v4l2-common.h>
27 #include <media/videobuf-vmalloc.h>
28 #include <media/tuner.h>
29 #include "tuner-simple.h"
31 #include "lgdt330x.h"
32 #include "zl10353.h"
33 #include "s5h1409.h"
34 #include "mt352.h"
35 #include "mt352_priv.h" /* FIXME */
36 #include "tda1002x.h"
38 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
39 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
40 MODULE_LICENSE("GPL");
42 static unsigned int debug;
43 module_param(debug, int, 0644);
44 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
46 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
48 #define dprintk(level, fmt, arg...) do { \
49 if (debug >= level) \
50 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
51 } while (0)
53 #define EM28XX_DVB_NUM_BUFS 5
54 #define EM28XX_DVB_MAX_PACKETS 64
56 struct em28xx_dvb {
57 struct dvb_frontend *frontend;
59 /* feed count management */
60 struct mutex lock;
61 int nfeeds;
63 /* general boilerplate stuff */
64 struct dvb_adapter adapter;
65 struct dvb_demux demux;
66 struct dmxdev dmxdev;
67 struct dmx_frontend fe_hw;
68 struct dmx_frontend fe_mem;
69 struct dvb_net net;
73 static inline void print_err_status(struct em28xx *dev,
74 int packet, int status)
76 char *errmsg = "Unknown";
78 switch (status) {
79 case -ENOENT:
80 errmsg = "unlinked synchronuously";
81 break;
82 case -ECONNRESET:
83 errmsg = "unlinked asynchronuously";
84 break;
85 case -ENOSR:
86 errmsg = "Buffer error (overrun)";
87 break;
88 case -EPIPE:
89 errmsg = "Stalled (device not responding)";
90 break;
91 case -EOVERFLOW:
92 errmsg = "Babble (bad cable?)";
93 break;
94 case -EPROTO:
95 errmsg = "Bit-stuff error (bad cable?)";
96 break;
97 case -EILSEQ:
98 errmsg = "CRC/Timeout (could be anything)";
99 break;
100 case -ETIME:
101 errmsg = "Device does not respond";
102 break;
104 if (packet < 0) {
105 dprintk(1, "URB status %d [%s].\n", status, errmsg);
106 } else {
107 dprintk(1, "URB packet %d, status %d [%s].\n",
108 packet, status, errmsg);
112 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
114 int i;
116 if (!dev)
117 return 0;
119 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
120 return 0;
122 if (urb->status < 0) {
123 print_err_status(dev, -1, urb->status);
124 if (urb->status == -ENOENT)
125 return 0;
128 for (i = 0; i < urb->number_of_packets; i++) {
129 int status = urb->iso_frame_desc[i].status;
131 if (status < 0) {
132 print_err_status(dev, i, status);
133 if (urb->iso_frame_desc[i].status != -EPROTO)
134 continue;
137 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
138 urb->iso_frame_desc[i].offset,
139 urb->iso_frame_desc[i].actual_length);
142 return 0;
145 static int start_streaming(struct em28xx_dvb *dvb)
147 int rc;
148 struct em28xx *dev = dvb->adapter.priv;
149 int max_dvb_packet_size;
151 usb_set_interface(dev->udev, 0, 1);
152 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
153 if (rc < 0)
154 return rc;
156 max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev);
158 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
159 EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
160 dvb_isoc_copy);
163 static int stop_streaming(struct em28xx_dvb *dvb)
165 struct em28xx *dev = dvb->adapter.priv;
167 em28xx_uninit_isoc(dev);
169 em28xx_set_mode(dev, EM28XX_SUSPEND);
171 return 0;
174 static int start_feed(struct dvb_demux_feed *feed)
176 struct dvb_demux *demux = feed->demux;
177 struct em28xx_dvb *dvb = demux->priv;
178 int rc, ret;
180 if (!demux->dmx.frontend)
181 return -EINVAL;
183 mutex_lock(&dvb->lock);
184 dvb->nfeeds++;
185 rc = dvb->nfeeds;
187 if (dvb->nfeeds == 1) {
188 ret = start_streaming(dvb);
189 if (ret < 0)
190 rc = ret;
193 mutex_unlock(&dvb->lock);
194 return rc;
197 static int stop_feed(struct dvb_demux_feed *feed)
199 struct dvb_demux *demux = feed->demux;
200 struct em28xx_dvb *dvb = demux->priv;
201 int err = 0;
203 mutex_lock(&dvb->lock);
204 dvb->nfeeds--;
206 if (0 == dvb->nfeeds)
207 err = stop_streaming(dvb);
209 mutex_unlock(&dvb->lock);
210 return err;
215 /* ------------------------------------------------------------------ */
216 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
218 struct em28xx *dev = fe->dvb->priv;
220 if (acquire)
221 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
222 else
223 return em28xx_set_mode(dev, EM28XX_SUSPEND);
226 /* ------------------------------------------------------------------ */
228 static struct lgdt330x_config em2880_lgdt3303_dev = {
229 .demod_address = 0x0e,
230 .demod_chip = LGDT3303,
233 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
234 .demod_address = (0x1e >> 1),
235 .no_tuner = 1,
236 .parallel_ts = 1,
237 .if2 = 45600,
240 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
241 .demod_address = 0x32 >> 1,
242 .output_mode = S5H1409_PARALLEL_OUTPUT,
243 .gpio = S5H1409_GPIO_OFF,
244 .inversion = S5H1409_INVERSION_OFF,
245 .status_mode = S5H1409_DEMODLOCKING,
246 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
249 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
250 .demod_address = (0x1e >> 1),
251 .no_tuner = 1,
252 .disable_i2c_gate_ctrl = 1,
253 .parallel_ts = 1,
254 .if2 = 45600,
257 #ifdef EM28XX_DRX397XD_SUPPORT
258 /* [TODO] djh - not sure yet what the device config needs to contain */
259 static struct drx397xD_config em28xx_drx397xD_with_xc3028 = {
260 .demod_address = (0xe0 >> 1),
262 #endif
264 static int mt352_terratec_xs_init(struct dvb_frontend *fe)
266 /* Values extracted from a USB trace of the Terratec Windows driver */
267 static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
268 static u8 reset[] = { RESET, 0x80 };
269 static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
270 static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
271 static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
272 static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
273 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
274 static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
275 static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
276 static u8 tuner_go[] = { TUNER_GO, 0x01};
278 mt352_write(fe, clock_config, sizeof(clock_config));
279 udelay(200);
280 mt352_write(fe, reset, sizeof(reset));
281 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
282 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
283 mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
284 mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
285 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
286 mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
287 mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
288 mt352_write(fe, tuner_go, sizeof(tuner_go));
289 return 0;
292 static struct mt352_config terratec_xs_mt352_cfg = {
293 .demod_address = (0x1e >> 1),
294 .no_tuner = 1,
295 .if2 = 45600,
296 .demod_init = mt352_terratec_xs_init,
299 static struct tda10023_config em28xx_tda10023_config = {
300 .demod_address = 0x0c,
301 .invert = 1,
304 /* ------------------------------------------------------------------ */
306 static int attach_xc3028(u8 addr, struct em28xx *dev)
308 struct dvb_frontend *fe;
309 struct xc2028_config cfg;
311 memset(&cfg, 0, sizeof(cfg));
312 cfg.i2c_adap = &dev->i2c_adap;
313 cfg.i2c_addr = addr;
315 if (!dev->dvb->frontend) {
316 em28xx_errdev("/2: dvb frontend not attached. "
317 "Can't attach xc3028\n");
318 return -EINVAL;
321 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
322 if (!fe) {
323 em28xx_errdev("/2: xc3028 attach failed\n");
324 dvb_frontend_detach(dev->dvb->frontend);
325 dev->dvb->frontend = NULL;
326 return -EINVAL;
329 em28xx_info("%s/2: xc3028 attached\n", dev->name);
331 return 0;
334 /* ------------------------------------------------------------------ */
336 static int register_dvb(struct em28xx_dvb *dvb,
337 struct module *module,
338 struct em28xx *dev,
339 struct device *device)
341 int result;
343 mutex_init(&dvb->lock);
345 /* register adapter */
346 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
347 adapter_nr);
348 if (result < 0) {
349 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
350 dev->name, result);
351 goto fail_adapter;
354 /* Ensure all frontends negotiate bus access */
355 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
357 dvb->adapter.priv = dev;
359 /* register frontend */
360 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
361 if (result < 0) {
362 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
363 dev->name, result);
364 goto fail_frontend;
367 /* register demux stuff */
368 dvb->demux.dmx.capabilities =
369 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
370 DMX_MEMORY_BASED_FILTERING;
371 dvb->demux.priv = dvb;
372 dvb->demux.filternum = 256;
373 dvb->demux.feednum = 256;
374 dvb->demux.start_feed = start_feed;
375 dvb->demux.stop_feed = stop_feed;
377 result = dvb_dmx_init(&dvb->demux);
378 if (result < 0) {
379 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
380 dev->name, result);
381 goto fail_dmx;
384 dvb->dmxdev.filternum = 256;
385 dvb->dmxdev.demux = &dvb->demux.dmx;
386 dvb->dmxdev.capabilities = 0;
387 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
388 if (result < 0) {
389 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
390 dev->name, result);
391 goto fail_dmxdev;
394 dvb->fe_hw.source = DMX_FRONTEND_0;
395 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
396 if (result < 0) {
397 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
398 dev->name, result);
399 goto fail_fe_hw;
402 dvb->fe_mem.source = DMX_MEMORY_FE;
403 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
404 if (result < 0) {
405 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
406 dev->name, result);
407 goto fail_fe_mem;
410 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
411 if (result < 0) {
412 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
413 dev->name, result);
414 goto fail_fe_conn;
417 /* register network adapter */
418 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
419 return 0;
421 fail_fe_conn:
422 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
423 fail_fe_mem:
424 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
425 fail_fe_hw:
426 dvb_dmxdev_release(&dvb->dmxdev);
427 fail_dmxdev:
428 dvb_dmx_release(&dvb->demux);
429 fail_dmx:
430 dvb_unregister_frontend(dvb->frontend);
431 fail_frontend:
432 dvb_frontend_detach(dvb->frontend);
433 dvb_unregister_adapter(&dvb->adapter);
434 fail_adapter:
435 return result;
438 static void unregister_dvb(struct em28xx_dvb *dvb)
440 dvb_net_release(&dvb->net);
441 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
442 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
443 dvb_dmxdev_release(&dvb->dmxdev);
444 dvb_dmx_release(&dvb->demux);
445 dvb_unregister_frontend(dvb->frontend);
446 dvb_frontend_detach(dvb->frontend);
447 dvb_unregister_adapter(&dvb->adapter);
451 static int dvb_init(struct em28xx *dev)
453 int result = 0;
454 struct em28xx_dvb *dvb;
456 if (!dev->board.has_dvb) {
457 /* This device does not support the extension */
458 return 0;
461 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
463 if (dvb == NULL) {
464 em28xx_info("em28xx_dvb: memory allocation failed\n");
465 return -ENOMEM;
467 dev->dvb = dvb;
469 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
470 /* init frontend */
471 switch (dev->model) {
472 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
473 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
474 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
475 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
476 dvb->frontend = dvb_attach(lgdt330x_attach,
477 &em2880_lgdt3303_dev,
478 &dev->i2c_adap);
479 if (attach_xc3028(0x61, dev) < 0) {
480 result = -EINVAL;
481 goto out_free;
483 break;
484 case EM2880_BOARD_KWORLD_DVB_310U:
485 dvb->frontend = dvb_attach(zl10353_attach,
486 &em28xx_zl10353_with_xc3028,
487 &dev->i2c_adap);
488 if (attach_xc3028(0x61, dev) < 0) {
489 result = -EINVAL;
490 goto out_free;
492 break;
493 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
494 case EM2882_BOARD_TERRATEC_HYBRID_XS:
495 case EM2880_BOARD_EMPIRE_DUAL_TV:
496 dvb->frontend = dvb_attach(zl10353_attach,
497 &em28xx_zl10353_xc3028_no_i2c_gate,
498 &dev->i2c_adap);
499 if (attach_xc3028(0x61, dev) < 0) {
500 result = -EINVAL;
501 goto out_free;
503 break;
504 case EM2880_BOARD_TERRATEC_HYBRID_XS:
505 case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
506 case EM2881_BOARD_PINNACLE_HYBRID_PRO:
507 case EM2882_BOARD_DIKOM_DK300:
508 dvb->frontend = dvb_attach(zl10353_attach,
509 &em28xx_zl10353_xc3028_no_i2c_gate,
510 &dev->i2c_adap);
511 if (dvb->frontend == NULL) {
512 /* This board could have either a zl10353 or a mt352.
513 If the chip id isn't for zl10353, try mt352 */
514 dvb->frontend = dvb_attach(mt352_attach,
515 &terratec_xs_mt352_cfg,
516 &dev->i2c_adap);
519 if (attach_xc3028(0x61, dev) < 0) {
520 result = -EINVAL;
521 goto out_free;
523 break;
524 case EM2883_BOARD_KWORLD_HYBRID_330U:
525 case EM2882_BOARD_EVGA_INDTUBE:
526 dvb->frontend = dvb_attach(s5h1409_attach,
527 &em28xx_s5h1409_with_xc3028,
528 &dev->i2c_adap);
529 if (attach_xc3028(0x61, dev) < 0) {
530 result = -EINVAL;
531 goto out_free;
533 break;
534 case EM2882_BOARD_KWORLD_ATSC_315U:
535 dvb->frontend = dvb_attach(lgdt330x_attach,
536 &em2880_lgdt3303_dev,
537 &dev->i2c_adap);
538 if (dvb->frontend != NULL) {
539 if (!dvb_attach(simple_tuner_attach, dvb->frontend,
540 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
541 result = -EINVAL;
542 goto out_free;
545 break;
546 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
547 #ifdef EM28XX_DRX397XD_SUPPORT
548 /* We don't have the config structure properly populated, so
549 this is commented out for now */
550 dvb->frontend = dvb_attach(drx397xD_attach,
551 &em28xx_drx397xD_with_xc3028,
552 &dev->i2c_adap);
553 if (attach_xc3028(0x61, dev) < 0) {
554 result = -EINVAL;
555 goto out_free;
557 break;
558 #endif
559 case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
560 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
561 dvb->frontend = dvb_attach(tda10023_attach,
562 &em28xx_tda10023_config,
563 &dev->i2c_adap, 0x48);
564 if (dvb->frontend) {
565 if (!dvb_attach(simple_tuner_attach, dvb->frontend,
566 &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
567 result = -EINVAL;
568 goto out_free;
571 break;
572 default:
573 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
574 " isn't supported yet\n");
575 break;
577 if (NULL == dvb->frontend) {
578 em28xx_errdev("/2: frontend initialization failed\n");
579 result = -EINVAL;
580 goto out_free;
582 /* define general-purpose callback pointer */
583 dvb->frontend->callback = em28xx_tuner_callback;
585 /* register everything */
586 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
588 if (result < 0)
589 goto out_free;
591 em28xx_set_mode(dev, EM28XX_SUSPEND);
592 em28xx_info("Successfully loaded em28xx-dvb\n");
593 return 0;
595 out_free:
596 em28xx_set_mode(dev, EM28XX_SUSPEND);
597 kfree(dvb);
598 dev->dvb = NULL;
599 return result;
602 static int dvb_fini(struct em28xx *dev)
604 if (!dev->board.has_dvb) {
605 /* This device does not support the extension */
606 return 0;
609 if (dev->dvb) {
610 unregister_dvb(dev->dvb);
611 kfree(dev->dvb);
612 dev->dvb = NULL;
615 return 0;
618 static struct em28xx_ops dvb_ops = {
619 .id = EM28XX_DVB,
620 .name = "Em28xx dvb Extension",
621 .init = dvb_init,
622 .fini = dvb_fini,
625 static int __init em28xx_dvb_register(void)
627 return em28xx_register_extension(&dvb_ops);
630 static void __exit em28xx_dvb_unregister(void)
632 em28xx_unregister_extension(&dvb_ops);
635 module_init(em28xx_dvb_register);
636 module_exit(em28xx_dvb_unregister);