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 */
44 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
45 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
46 MODULE_LICENSE("GPL");
48 static unsigned int debug;
49 module_param(debug, int, 0644);
50 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
52 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
54 #define dprintk(level, fmt, arg...) do { \
56 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
59 #define EM28XX_DVB_NUM_BUFS 5
60 #define EM28XX_DVB_MAX_PACKETS 64
63 struct dvb_frontend *fe[2];
65 /* feed count management */
69 /* general boilerplate stuff */
70 struct dvb_adapter adapter;
71 struct dvb_demux demux;
73 struct dmx_frontend fe_hw;
74 struct dmx_frontend fe_mem;
79 static inline void print_err_status(struct em28xx *dev,
80 int packet, int status)
82 char *errmsg = "Unknown";
86 errmsg = "unlinked synchronuously";
89 errmsg = "unlinked asynchronuously";
92 errmsg = "Buffer error (overrun)";
95 errmsg = "Stalled (device not responding)";
98 errmsg = "Babble (bad cable?)";
101 errmsg = "Bit-stuff error (bad cable?)";
104 errmsg = "CRC/Timeout (could be anything)";
107 errmsg = "Device does not respond";
111 dprintk(1, "URB status %d [%s].\n", status, errmsg);
113 dprintk(1, "URB packet %d, status %d [%s].\n",
114 packet, status, errmsg);
118 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
125 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
128 if (urb->status < 0) {
129 print_err_status(dev, -1, urb->status);
130 if (urb->status == -ENOENT)
134 for (i = 0; i < urb->number_of_packets; i++) {
135 int status = urb->iso_frame_desc[i].status;
138 print_err_status(dev, i, status);
139 if (urb->iso_frame_desc[i].status != -EPROTO)
143 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
144 urb->iso_frame_desc[i].offset,
145 urb->iso_frame_desc[i].actual_length);
151 static int start_streaming(struct em28xx_dvb *dvb)
154 struct em28xx *dev = dvb->adapter.priv;
155 int max_dvb_packet_size;
157 usb_set_interface(dev->udev, 0, 1);
158 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
162 max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev);
164 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
165 EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
169 static int stop_streaming(struct em28xx_dvb *dvb)
171 struct em28xx *dev = dvb->adapter.priv;
173 em28xx_uninit_isoc(dev);
175 em28xx_set_mode(dev, EM28XX_SUSPEND);
180 static int start_feed(struct dvb_demux_feed *feed)
182 struct dvb_demux *demux = feed->demux;
183 struct em28xx_dvb *dvb = demux->priv;
186 if (!demux->dmx.frontend)
189 mutex_lock(&dvb->lock);
193 if (dvb->nfeeds == 1) {
194 ret = start_streaming(dvb);
199 mutex_unlock(&dvb->lock);
203 static int stop_feed(struct dvb_demux_feed *feed)
205 struct dvb_demux *demux = feed->demux;
206 struct em28xx_dvb *dvb = demux->priv;
209 mutex_lock(&dvb->lock);
212 if (0 == dvb->nfeeds)
213 err = stop_streaming(dvb);
215 mutex_unlock(&dvb->lock);
221 /* ------------------------------------------------------------------ */
222 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
224 struct em28xx *dev = fe->dvb->priv;
227 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
229 return em28xx_set_mode(dev, EM28XX_SUSPEND);
232 /* ------------------------------------------------------------------ */
234 static struct lgdt330x_config em2880_lgdt3303_dev = {
235 .demod_address = 0x0e,
236 .demod_chip = LGDT3303,
239 static struct lgdt3305_config em2870_lgdt3304_dev = {
241 .demod_chip = LGDT3304,
242 .spectral_inversion = 1,
244 .mpeg_mode = LGDT3305_MPEG_PARALLEL,
245 .tpclk_edge = LGDT3305_TPCLK_FALLING_EDGE,
246 .tpvalid_polarity = LGDT3305_TP_VALID_HIGH,
251 static struct s921_config sharp_isdbt = {
252 .demod_address = 0x30 >> 1
255 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
256 .demod_address = (0x1e >> 1),
262 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
263 .demod_address = 0x32 >> 1,
264 .output_mode = S5H1409_PARALLEL_OUTPUT,
265 .gpio = S5H1409_GPIO_OFF,
266 .inversion = S5H1409_INVERSION_OFF,
267 .status_mode = S5H1409_DEMODLOCKING,
268 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
271 static struct tda18271_std_map kworld_a340_std_map = {
272 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 0,
273 .if_lvl = 1, .rfagc_top = 0x37, },
274 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 1,
275 .if_lvl = 1, .rfagc_top = 0x37, },
278 static struct tda18271_config kworld_a340_config = {
279 .std_map = &kworld_a340_std_map,
282 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
283 .demod_address = (0x1e >> 1),
285 .disable_i2c_gate_ctrl = 1,
290 static struct drxd_config em28xx_drxd = {
291 .index = 0, .demod_address = 0x70, .demod_revision = 0xa2,
292 .demoda_address = 0x00, .pll_address = 0x00,
293 .pll_type = DRXD_PLL_NONE, .clock = 12000, .insert_rs_byte = 1,
294 .pll_set = NULL, .osc_deviation = NULL, .IF = 42800000,
295 .disable_i2c_gate_ctrl = 1,
298 static int mt352_terratec_xs_init(struct dvb_frontend *fe)
300 /* Values extracted from a USB trace of the Terratec Windows driver */
301 static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x2c };
302 static u8 reset[] = { RESET, 0x80 };
303 static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
304 static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0xa0 };
305 static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
306 static u8 rs_err_cfg[] = { RS_ERR_PER_1, 0x00, 0x4d };
307 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
308 static u8 trl_nom_cfg[] = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
309 static u8 tps_given_cfg[] = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
310 static u8 tuner_go[] = { TUNER_GO, 0x01};
312 mt352_write(fe, clock_config, sizeof(clock_config));
314 mt352_write(fe, reset, sizeof(reset));
315 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
316 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
317 mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
318 mt352_write(fe, rs_err_cfg, sizeof(rs_err_cfg));
319 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
320 mt352_write(fe, trl_nom_cfg, sizeof(trl_nom_cfg));
321 mt352_write(fe, tps_given_cfg, sizeof(tps_given_cfg));
322 mt352_write(fe, tuner_go, sizeof(tuner_go));
326 static struct mt352_config terratec_xs_mt352_cfg = {
327 .demod_address = (0x1e >> 1),
330 .demod_init = mt352_terratec_xs_init,
333 static struct tda10023_config em28xx_tda10023_config = {
334 .demod_address = 0x0c,
338 static struct cxd2820r_config em28xx_cxd2820r_config = {
339 .i2c_address = (0xd8 >> 1),
340 .ts_mode = CXD2820R_TS_SERIAL,
349 /* enable LNA for DVB-T2 and DVB-C */
350 .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
351 .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
354 static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
355 .output_opt = TDA18271_OUTPUT_LT_OFF,
358 /* ------------------------------------------------------------------ */
360 static int attach_xc3028(u8 addr, struct em28xx *dev)
362 struct dvb_frontend *fe;
363 struct xc2028_config cfg;
365 memset(&cfg, 0, sizeof(cfg));
366 cfg.i2c_adap = &dev->i2c_adap;
369 if (!dev->dvb->fe[0]) {
370 em28xx_errdev("/2: dvb frontend not attached. "
371 "Can't attach xc3028\n");
375 fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
377 em28xx_errdev("/2: xc3028 attach failed\n");
378 dvb_frontend_detach(dev->dvb->fe[0]);
379 dev->dvb->fe[0] = NULL;
383 em28xx_info("%s/2: xc3028 attached\n", dev->name);
388 /* ------------------------------------------------------------------ */
390 static int register_dvb(struct em28xx_dvb *dvb,
391 struct module *module,
393 struct device *device)
397 mutex_init(&dvb->lock);
399 /* register adapter */
400 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
403 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
408 /* Ensure all frontends negotiate bus access */
409 dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
411 dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
413 dvb->adapter.priv = dev;
415 /* register frontend */
416 result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
418 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
423 /* register 2nd frontend */
425 result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
427 printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
433 /* register demux stuff */
434 dvb->demux.dmx.capabilities =
435 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
436 DMX_MEMORY_BASED_FILTERING;
437 dvb->demux.priv = dvb;
438 dvb->demux.filternum = 256;
439 dvb->demux.feednum = 256;
440 dvb->demux.start_feed = start_feed;
441 dvb->demux.stop_feed = stop_feed;
443 result = dvb_dmx_init(&dvb->demux);
445 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
450 dvb->dmxdev.filternum = 256;
451 dvb->dmxdev.demux = &dvb->demux.dmx;
452 dvb->dmxdev.capabilities = 0;
453 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
455 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
460 dvb->fe_hw.source = DMX_FRONTEND_0;
461 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
463 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
468 dvb->fe_mem.source = DMX_MEMORY_FE;
469 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
471 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
476 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
478 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
483 /* register network adapter */
484 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
488 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
490 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
492 dvb_dmxdev_release(&dvb->dmxdev);
494 dvb_dmx_release(&dvb->demux);
497 dvb_unregister_frontend(dvb->fe[1]);
498 dvb_unregister_frontend(dvb->fe[0]);
501 dvb_frontend_detach(dvb->fe[1]);
503 dvb_frontend_detach(dvb->fe[0]);
504 dvb_unregister_adapter(&dvb->adapter);
509 static void unregister_dvb(struct em28xx_dvb *dvb)
511 dvb_net_release(&dvb->net);
512 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
513 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
514 dvb_dmxdev_release(&dvb->dmxdev);
515 dvb_dmx_release(&dvb->demux);
517 dvb_unregister_frontend(dvb->fe[1]);
518 dvb_unregister_frontend(dvb->fe[0]);
520 dvb_frontend_detach(dvb->fe[1]);
521 dvb_frontend_detach(dvb->fe[0]);
522 dvb_unregister_adapter(&dvb->adapter);
525 static int dvb_init(struct em28xx *dev)
528 struct em28xx_dvb *dvb;
530 if (!dev->board.has_dvb) {
531 /* This device does not support the extension */
532 printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
536 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
539 em28xx_info("em28xx_dvb: memory allocation failed\n");
543 dvb->fe[0] = dvb->fe[1] = NULL;
545 mutex_lock(&dev->lock);
546 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
548 switch (dev->model) {
549 case EM2874_LEADERSHIP_ISDBT:
550 dvb->fe[0] = dvb_attach(s921_attach,
551 &sharp_isdbt, &dev->i2c_adap);
559 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
560 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
561 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
562 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
563 dvb->fe[0] = dvb_attach(lgdt330x_attach,
564 &em2880_lgdt3303_dev,
566 if (attach_xc3028(0x61, dev) < 0) {
571 case EM2880_BOARD_KWORLD_DVB_310U:
572 dvb->fe[0] = dvb_attach(zl10353_attach,
573 &em28xx_zl10353_with_xc3028,
575 if (attach_xc3028(0x61, dev) < 0) {
580 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
581 case EM2882_BOARD_TERRATEC_HYBRID_XS:
582 case EM2880_BOARD_EMPIRE_DUAL_TV:
583 dvb->fe[0] = dvb_attach(zl10353_attach,
584 &em28xx_zl10353_xc3028_no_i2c_gate,
586 if (attach_xc3028(0x61, dev) < 0) {
591 case EM2880_BOARD_TERRATEC_HYBRID_XS:
592 case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
593 case EM2881_BOARD_PINNACLE_HYBRID_PRO:
594 case EM2882_BOARD_DIKOM_DK300:
595 case EM2882_BOARD_KWORLD_VS_DVBT:
596 dvb->fe[0] = dvb_attach(zl10353_attach,
597 &em28xx_zl10353_xc3028_no_i2c_gate,
599 if (dvb->fe[0] == NULL) {
600 /* This board could have either a zl10353 or a mt352.
601 If the chip id isn't for zl10353, try mt352 */
602 dvb->fe[0] = dvb_attach(mt352_attach,
603 &terratec_xs_mt352_cfg,
607 if (attach_xc3028(0x61, dev) < 0) {
612 case EM2883_BOARD_KWORLD_HYBRID_330U:
613 case EM2882_BOARD_EVGA_INDTUBE:
614 dvb->fe[0] = dvb_attach(s5h1409_attach,
615 &em28xx_s5h1409_with_xc3028,
617 if (attach_xc3028(0x61, dev) < 0) {
622 case EM2882_BOARD_KWORLD_ATSC_315U:
623 dvb->fe[0] = dvb_attach(lgdt330x_attach,
624 &em2880_lgdt3303_dev,
626 if (dvb->fe[0] != NULL) {
627 if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
628 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
634 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
635 case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
636 dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
637 &dev->i2c_adap, &dev->udev->dev);
638 if (attach_xc3028(0x61, dev) < 0) {
643 case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
644 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
645 dvb->fe[0] = dvb_attach(tda10023_attach,
646 &em28xx_tda10023_config,
647 &dev->i2c_adap, 0x48);
649 if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
650 &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
656 case EM2870_BOARD_KWORLD_A340:
657 dvb->fe[0] = dvb_attach(lgdt3305_attach,
658 &em2870_lgdt3304_dev,
660 if (dvb->fe[0] != NULL)
661 dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
662 &dev->i2c_adap, &kworld_a340_config);
664 case EM28174_BOARD_PCTV_290E:
666 * FE 0 = DVB-T/T2 + FE 1 = DVB-C, both sharing same tuner. */
668 dvb->fe[0] = dvb_attach(cxd2820r_attach,
669 &em28xx_cxd2820r_config, &dev->i2c_adap, NULL);
671 struct i2c_adapter *i2c_tuner;
672 i2c_tuner = cxd2820r_get_tuner_i2c_adapter(dvb->fe[0]);
673 /* FE 0 attach tuner */
674 if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
675 i2c_tuner, &em28xx_cxd2820r_tda18271_config)) {
676 dvb_frontend_detach(dvb->fe[0]);
680 /* FE 1. This dvb_attach() cannot fail. */
681 dvb->fe[1] = dvb_attach(cxd2820r_attach, NULL, NULL,
684 /* FE 1 attach tuner */
685 if (!dvb_attach(tda18271_attach, dvb->fe[1], 0x60,
686 i2c_tuner, &em28xx_cxd2820r_tda18271_config)) {
687 dvb_frontend_detach(dvb->fe[1]);
688 /* leave FE 0 still active */
693 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
694 " isn't supported yet\n");
697 if (NULL == dvb->fe[0]) {
698 em28xx_errdev("/2: frontend initialization failed\n");
702 /* define general-purpose callback pointer */
703 dvb->fe[0]->callback = em28xx_tuner_callback;
705 /* register everything */
706 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
711 em28xx_info("Successfully loaded em28xx-dvb\n");
713 em28xx_set_mode(dev, EM28XX_SUSPEND);
714 mutex_unlock(&dev->lock);
723 static int dvb_fini(struct em28xx *dev)
725 if (!dev->board.has_dvb) {
726 /* This device does not support the extension */
731 unregister_dvb(dev->dvb);
739 static struct em28xx_ops dvb_ops = {
741 .name = "Em28xx dvb Extension",
746 static int __init em28xx_dvb_register(void)
748 return em28xx_register_extension(&dvb_ops);
751 static void __exit em28xx_dvb_unregister(void)
753 em28xx_unregister_extension(&dvb_ops);
756 module_init(em28xx_dvb_register);
757 module_exit(em28xx_dvb_unregister);