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>
26 #include <media/v4l2-common.h>
27 #include <media/videobuf-vmalloc.h>
28 #include <media/tuner.h>
29 #include "tuner-simple.h"
35 #include "mt352_priv.h" /* FIXME */
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 { \
50 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
53 #define EM28XX_DVB_NUM_BUFS 5
54 #define EM28XX_DVB_MAX_PACKETS 64
57 struct dvb_frontend
*frontend
;
59 /* feed count management */
63 /* general boilerplate stuff */
64 struct dvb_adapter adapter
;
65 struct dvb_demux demux
;
67 struct dmx_frontend fe_hw
;
68 struct dmx_frontend fe_mem
;
73 static inline void print_err_status(struct em28xx
*dev
,
74 int packet
, int status
)
76 char *errmsg
= "Unknown";
80 errmsg
= "unlinked synchronuously";
83 errmsg
= "unlinked asynchronuously";
86 errmsg
= "Buffer error (overrun)";
89 errmsg
= "Stalled (device not responding)";
92 errmsg
= "Babble (bad cable?)";
95 errmsg
= "Bit-stuff error (bad cable?)";
98 errmsg
= "CRC/Timeout (could be anything)";
101 errmsg
= "Device does not respond";
105 dprintk(1, "URB status %d [%s].\n", status
, errmsg
);
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
)
119 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
122 if (urb
->status
< 0) {
123 print_err_status(dev
, -1, urb
->status
);
124 if (urb
->status
== -ENOENT
)
128 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
129 int status
= urb
->iso_frame_desc
[i
].status
;
132 print_err_status(dev
, i
, status
);
133 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
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
);
145 static int start_streaming(struct em28xx_dvb
*dvb
)
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
);
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
,
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
);
174 static int start_feed(struct dvb_demux_feed
*feed
)
176 struct dvb_demux
*demux
= feed
->demux
;
177 struct em28xx_dvb
*dvb
= demux
->priv
;
180 if (!demux
->dmx
.frontend
)
183 mutex_lock(&dvb
->lock
);
187 if (dvb
->nfeeds
== 1) {
188 ret
= start_streaming(dvb
);
193 mutex_unlock(&dvb
->lock
);
197 static int stop_feed(struct dvb_demux_feed
*feed
)
199 struct dvb_demux
*demux
= feed
->demux
;
200 struct em28xx_dvb
*dvb
= demux
->priv
;
203 mutex_lock(&dvb
->lock
);
206 if (0 == dvb
->nfeeds
)
207 err
= stop_streaming(dvb
);
209 mutex_unlock(&dvb
->lock
);
215 /* ------------------------------------------------------------------ */
216 static int em28xx_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
218 struct em28xx
*dev
= fe
->dvb
->priv
;
221 return em28xx_set_mode(dev
, EM28XX_DIGITAL_MODE
);
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),
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),
252 .disable_i2c_gate_ctrl
= 1,
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),
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
));
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
));
292 static struct mt352_config terratec_xs_mt352_cfg
= {
293 .demod_address
= (0x1e >> 1),
296 .demod_init
= mt352_terratec_xs_init
,
299 static struct tda10023_config em28xx_tda10023_config
= {
300 .demod_address
= 0x0c,
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
;
315 if (!dev
->dvb
->frontend
) {
316 em28xx_errdev("/2: dvb frontend not attached. "
317 "Can't attach xc3028\n");
321 fe
= dvb_attach(xc2028_attach
, dev
->dvb
->frontend
, &cfg
);
323 em28xx_errdev("/2: xc3028 attach failed\n");
324 dvb_frontend_detach(dev
->dvb
->frontend
);
325 dev
->dvb
->frontend
= NULL
;
329 em28xx_info("%s/2: xc3028 attached\n", dev
->name
);
334 /* ------------------------------------------------------------------ */
336 static int register_dvb(struct em28xx_dvb
*dvb
,
337 struct module
*module
,
339 struct device
*device
)
343 mutex_init(&dvb
->lock
);
345 /* register adapter */
346 result
= dvb_register_adapter(&dvb
->adapter
, dev
->name
, module
, device
,
349 printk(KERN_WARNING
"%s: dvb_register_adapter failed (errno = %d)\n",
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
);
362 printk(KERN_WARNING
"%s: dvb_register_frontend failed (errno = %d)\n",
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
);
379 printk(KERN_WARNING
"%s: dvb_dmx_init failed (errno = %d)\n",
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
);
389 printk(KERN_WARNING
"%s: dvb_dmxdev_init failed (errno = %d)\n",
394 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
395 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
397 printk(KERN_WARNING
"%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
402 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
403 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
405 printk(KERN_WARNING
"%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
410 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
412 printk(KERN_WARNING
"%s: connect_frontend failed (errno = %d)\n",
417 /* register network adapter */
418 dvb_net_init(&dvb
->adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
422 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
424 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
426 dvb_dmxdev_release(&dvb
->dmxdev
);
428 dvb_dmx_release(&dvb
->demux
);
430 dvb_unregister_frontend(dvb
->frontend
);
432 dvb_frontend_detach(dvb
->frontend
);
433 dvb_unregister_adapter(&dvb
->adapter
);
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
)
454 struct em28xx_dvb
*dvb
;
456 if (!dev
->board
.has_dvb
) {
457 /* This device does not support the extension */
461 dvb
= kzalloc(sizeof(struct em28xx_dvb
), GFP_KERNEL
);
464 em28xx_info("em28xx_dvb: memory allocation failed\n");
469 em28xx_set_mode(dev
, EM28XX_DIGITAL_MODE
);
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
,
479 if (attach_xc3028(0x61, dev
) < 0) {
484 case EM2880_BOARD_KWORLD_DVB_310U
:
485 dvb
->frontend
= dvb_attach(zl10353_attach
,
486 &em28xx_zl10353_with_xc3028
,
488 if (attach_xc3028(0x61, dev
) < 0) {
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
,
499 if (attach_xc3028(0x61, dev
) < 0) {
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
,
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
,
519 if (attach_xc3028(0x61, dev
) < 0) {
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
,
529 if (attach_xc3028(0x61, dev
) < 0) {
534 case EM2882_BOARD_KWORLD_ATSC_315U
:
535 dvb
->frontend
= dvb_attach(lgdt330x_attach
,
536 &em2880_lgdt3303_dev
,
538 if (dvb
->frontend
!= NULL
) {
539 if (!dvb_attach(simple_tuner_attach
, dvb
->frontend
,
540 &dev
->i2c_adap
, 0x61, TUNER_THOMSON_DTT761X
)) {
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
,
553 if (attach_xc3028(0x61, dev
) < 0) {
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);
565 if (!dvb_attach(simple_tuner_attach
, dvb
->frontend
,
566 &dev
->i2c_adap
, 0x60, TUNER_PHILIPS_CU1216L
)) {
573 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
574 " isn't supported yet\n");
577 if (NULL
== dvb
->frontend
) {
578 em28xx_errdev("/2: frontend initialization failed\n");
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
);
591 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
592 em28xx_info("Successfully loaded em28xx-dvb\n");
596 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
602 static int dvb_fini(struct em28xx
*dev
)
604 if (!dev
->board
.has_dvb
) {
605 /* This device does not support the extension */
610 unregister_dvb(dev
->dvb
);
618 static struct em28xx_ops dvb_ops
= {
620 .name
= "Em28xx dvb Extension",
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
);