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/slab.h>
24 #include <linux/usb.h>
27 #include <media/v4l2-common.h>
28 #include <media/videobuf-vmalloc.h>
29 #include <media/tuner.h>
30 #include "tuner-simple.h"
37 #include "mt352_priv.h" /* FIXME */
41 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
42 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
43 MODULE_LICENSE("GPL");
45 static unsigned int debug;
46 module_param(debug, int, 0644);
47 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
49 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
51 #define dprintk(level, fmt, arg...) do { \
53 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
56 #define EM28XX_DVB_NUM_BUFS 5
57 #define EM28XX_DVB_MAX_PACKETS 64
60 struct dvb_frontend *frontend;
62 /* feed count management */
66 /* general boilerplate stuff */
67 struct dvb_adapter adapter;
68 struct dvb_demux demux;
70 struct dmx_frontend fe_hw;
71 struct dmx_frontend fe_mem;
76 static inline void print_err_status(struct em28xx *dev,
77 int packet, int status)
79 char *errmsg = "Unknown";
83 errmsg = "unlinked synchronuously";
86 errmsg = "unlinked asynchronuously";
89 errmsg = "Buffer error (overrun)";
92 errmsg = "Stalled (device not responding)";
95 errmsg = "Babble (bad cable?)";
98 errmsg = "Bit-stuff error (bad cable?)";
101 errmsg = "CRC/Timeout (could be anything)";
104 errmsg = "Device does not respond";
108 dprintk(1, "URB status %d [%s].\n", status, errmsg);
110 dprintk(1, "URB packet %d, status %d [%s].\n",
111 packet, status, errmsg);
115 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
122 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
125 if (urb->status < 0) {
126 print_err_status(dev, -1, urb->status);
127 if (urb->status == -ENOENT)
131 for (i = 0; i < urb->number_of_packets; i++) {
132 int status = urb->iso_frame_desc[i].status;
135 print_err_status(dev, i, status);
136 if (urb->iso_frame_desc[i].status != -EPROTO)
140 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
141 urb->iso_frame_desc[i].offset,
142 urb->iso_frame_desc[i].actual_length);
148 static int start_streaming(struct em28xx_dvb *dvb)
151 struct em28xx *dev = dvb->adapter.priv;
152 int max_dvb_packet_size;
154 usb_set_interface(dev->udev, 0, 1);
155 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
159 max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev);
161 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
162 EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
166 static int stop_streaming(struct em28xx_dvb *dvb)
168 struct em28xx *dev = dvb->adapter.priv;
170 em28xx_uninit_isoc(dev);
172 em28xx_set_mode(dev, EM28XX_SUSPEND);
177 static int start_feed(struct dvb_demux_feed *feed)
179 struct dvb_demux *demux = feed->demux;
180 struct em28xx_dvb *dvb = demux->priv;
183 if (!demux->dmx.frontend)
186 mutex_lock(&dvb->lock);
190 if (dvb->nfeeds == 1) {
191 ret = start_streaming(dvb);
196 mutex_unlock(&dvb->lock);
200 static int stop_feed(struct dvb_demux_feed *feed)
202 struct dvb_demux *demux = feed->demux;
203 struct em28xx_dvb *dvb = demux->priv;
206 mutex_lock(&dvb->lock);
209 if (0 == dvb->nfeeds)
210 err = stop_streaming(dvb);
212 mutex_unlock(&dvb->lock);
218 /* ------------------------------------------------------------------ */
219 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
221 struct em28xx *dev = fe->dvb->priv;
224 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
226 return em28xx_set_mode(dev, EM28XX_SUSPEND);
229 /* ------------------------------------------------------------------ */
231 static struct lgdt330x_config em2880_lgdt3303_dev = {
232 .demod_address = 0x0e,
233 .demod_chip = LGDT3303,
236 static struct lgdt3305_config em2870_lgdt3304_dev = {
238 .demod_chip = LGDT3304,
239 .spectral_inversion = 1,
241 .mpeg_mode = LGDT3305_MPEG_PARALLEL,
242 .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
243 .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
248 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
249 .demod_address = (0x1e >> 1),
255 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
256 .demod_address = 0x32 >> 1,
257 .output_mode = S5H1409_PARALLEL_OUTPUT,
258 .gpio = S5H1409_GPIO_OFF,
259 .inversion = S5H1409_INVERSION_OFF,
260 .status_mode = S5H1409_DEMODLOCKING,
261 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
264 static struct tda18271_std_map kworld_a340_std_map = {
265 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
266 .if_lvl = 1, .rfagc_top = 0x37, },
267 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
268 .if_lvl = 1, .rfagc_top = 0x37, },
271 static struct tda18271_config kworld_a340_config = {
272 .std_map = &kworld_a340_std_map,
275 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
276 .demod_address = (0x1e >> 1),
278 .disable_i2c_gate_ctrl = 1,
283 #ifdef EM28XX_DRX397XD_SUPPORT
284 /* [TODO] djh - not sure yet what the device config needs to contain */
285 static struct drx397xD_config em28xx_drx397xD_with_xc3028 = {
286 .demod_address = (0xe0 >> 1),
290 static int mt352_terratec_xs_init(struct dvb_frontend *fe)
292 /* Values extracted from a USB trace of the Terratec Windows driver */
293 static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
294 static u8 reset[] = { RESET, 0x80 };
295 static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
296 static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
297 static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
298 static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
299 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
300 static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
301 static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
302 static u8 tuner_go[] = { TUNER_GO, 0x01};
304 mt352_write(fe, clock_config, sizeof(clock_config));
306 mt352_write(fe, reset, sizeof(reset));
307 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
308 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
309 mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
310 mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
311 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
312 mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
313 mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
314 mt352_write(fe, tuner_go, sizeof(tuner_go));
318 static struct mt352_config terratec_xs_mt352_cfg = {
319 .demod_address = (0x1e >> 1),
322 .demod_init = mt352_terratec_xs_init,
325 static struct tda10023_config em28xx_tda10023_config = {
326 .demod_address = 0x0c,
330 /* ------------------------------------------------------------------ */
332 static int attach_xc3028(u8 addr, struct em28xx *dev)
334 struct dvb_frontend *fe;
335 struct xc2028_config cfg;
337 memset(&cfg, 0, sizeof(cfg));
338 cfg.i2c_adap = &dev->i2c_adap;
341 if (!dev->dvb->frontend) {
342 em28xx_errdev("/2: dvb frontend not attached. "
343 "Can't attach xc3028\n");
347 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
349 em28xx_errdev("/2: xc3028 attach failed\n");
350 dvb_frontend_detach(dev->dvb->frontend);
351 dev->dvb->frontend = NULL;
355 em28xx_info("%s/2: xc3028 attached\n", dev->name);
360 /* ------------------------------------------------------------------ */
362 static int register_dvb(struct em28xx_dvb *dvb,
363 struct module *module,
365 struct device *device)
369 mutex_init(&dvb->lock);
371 /* register adapter */
372 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
375 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
380 /* Ensure all frontends negotiate bus access */
381 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
383 dvb->adapter.priv = dev;
385 /* register frontend */
386 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
388 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
393 /* register demux stuff */
394 dvb->demux.dmx.capabilities =
395 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
396 DMX_MEMORY_BASED_FILTERING;
397 dvb->demux.priv = dvb;
398 dvb->demux.filternum = 256;
399 dvb->demux.feednum = 256;
400 dvb->demux.start_feed = start_feed;
401 dvb->demux.stop_feed = stop_feed;
403 result = dvb_dmx_init(&dvb->demux);
405 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
410 dvb->dmxdev.filternum = 256;
411 dvb->dmxdev.demux = &dvb->demux.dmx;
412 dvb->dmxdev.capabilities = 0;
413 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
415 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
420 dvb->fe_hw.source = DMX_FRONTEND_0;
421 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
423 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
428 dvb->fe_mem.source = DMX_MEMORY_FE;
429 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
431 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
436 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
438 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
443 /* register network adapter */
444 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
448 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
450 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
452 dvb_dmxdev_release(&dvb->dmxdev);
454 dvb_dmx_release(&dvb->demux);
456 dvb_unregister_frontend(dvb->frontend);
458 dvb_frontend_detach(dvb->frontend);
459 dvb_unregister_adapter(&dvb->adapter);
464 static void unregister_dvb(struct em28xx_dvb *dvb)
466 dvb_net_release(&dvb->net);
467 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
468 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
469 dvb_dmxdev_release(&dvb->dmxdev);
470 dvb_dmx_release(&dvb->demux);
471 dvb_unregister_frontend(dvb->frontend);
472 dvb_frontend_detach(dvb->frontend);
473 dvb_unregister_adapter(&dvb->adapter);
477 static int dvb_init(struct em28xx *dev)
480 struct em28xx_dvb *dvb;
482 if (!dev->board.has_dvb) {
483 /* This device does not support the extension */
487 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
490 em28xx_info("em28xx_dvb: memory allocation failed\n");
495 mutex_lock(&dev->lock);
496 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
498 switch (dev->model) {
499 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
500 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
501 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
502 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
503 dvb->frontend = dvb_attach(lgdt330x_attach,
504 &em2880_lgdt3303_dev,
506 if (attach_xc3028(0x61, dev) < 0) {
511 case EM2880_BOARD_KWORLD_DVB_310U:
512 dvb->frontend = dvb_attach(zl10353_attach,
513 &em28xx_zl10353_with_xc3028,
515 if (attach_xc3028(0x61, dev) < 0) {
520 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
521 case EM2882_BOARD_TERRATEC_HYBRID_XS:
522 case EM2880_BOARD_EMPIRE_DUAL_TV:
523 dvb->frontend = dvb_attach(zl10353_attach,
524 &em28xx_zl10353_xc3028_no_i2c_gate,
526 if (attach_xc3028(0x61, dev) < 0) {
531 case EM2880_BOARD_TERRATEC_HYBRID_XS:
532 case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
533 case EM2881_BOARD_PINNACLE_HYBRID_PRO:
534 case EM2882_BOARD_DIKOM_DK300:
535 case EM2882_BOARD_KWORLD_VS_DVBT:
536 dvb->frontend = dvb_attach(zl10353_attach,
537 &em28xx_zl10353_xc3028_no_i2c_gate,
539 if (dvb->frontend == NULL) {
540 /* This board could have either a zl10353 or a mt352.
541 If the chip id isn't for zl10353, try mt352 */
542 dvb->frontend = dvb_attach(mt352_attach,
543 &terratec_xs_mt352_cfg,
547 if (attach_xc3028(0x61, dev) < 0) {
552 case EM2883_BOARD_KWORLD_HYBRID_330U:
553 case EM2882_BOARD_EVGA_INDTUBE:
554 dvb->frontend = dvb_attach(s5h1409_attach,
555 &em28xx_s5h1409_with_xc3028,
557 if (attach_xc3028(0x61, dev) < 0) {
562 case EM2882_BOARD_KWORLD_ATSC_315U:
563 dvb->frontend = dvb_attach(lgdt330x_attach,
564 &em2880_lgdt3303_dev,
566 if (dvb->frontend != NULL) {
567 if (!dvb_attach(simple_tuner_attach, dvb->frontend,
568 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
574 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
575 #ifdef EM28XX_DRX397XD_SUPPORT
576 /* We don't have the config structure properly populated, so
577 this is commented out for now */
578 dvb->frontend = dvb_attach(drx397xD_attach,
579 &em28xx_drx397xD_with_xc3028,
581 if (attach_xc3028(0x61, dev) < 0) {
587 case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
588 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
589 dvb->frontend = dvb_attach(tda10023_attach,
590 &em28xx_tda10023_config,
591 &dev->i2c_adap, 0x48);
593 if (!dvb_attach(simple_tuner_attach, dvb->frontend,
594 &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
600 case EM2870_BOARD_KWORLD_A340:
601 dvb->frontend = dvb_attach(lgdt3305_attach,
602 &em2870_lgdt3304_dev,
604 if (dvb->frontend != NULL)
605 dvb_attach(tda18271_attach, dvb->frontend, 0x60,
606 &dev->i2c_adap, &kworld_a340_config);
609 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
610 " isn't supported yet\n");
613 if (NULL == dvb->frontend) {
614 em28xx_errdev("/2: frontend initialization failed\n");
618 /* define general-purpose callback pointer */
619 dvb->frontend->callback = em28xx_tuner_callback;
621 /* register everything */
622 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
627 em28xx_info("Successfully loaded em28xx-dvb\n");
629 em28xx_set_mode(dev, EM28XX_SUSPEND);
630 mutex_unlock(&dev->lock);
639 static int dvb_fini(struct em28xx *dev)
641 if (!dev->board.has_dvb) {
642 /* This device does not support the extension */
647 unregister_dvb(dev->dvb);
655 static struct em28xx_ops dvb_ops = {
657 .name = "Em28xx dvb Extension",
662 static int __init em28xx_dvb_register(void)
664 return em28xx_register_extension(&dvb_ops);
667 static void __exit em28xx_dvb_unregister(void)
669 em28xx_unregister_extension(&dvb_ops);
672 module_init(em28xx_dvb_register);
673 module_exit(em28xx_dvb_unregister);