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 */
37 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
38 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
39 MODULE_LICENSE("GPL");
41 static unsigned int debug
;
42 module_param(debug
, int, 0644);
43 MODULE_PARM_DESC(debug
, "enable debug messages [dvb]");
45 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
47 #define dprintk(level, fmt, arg...) do { \
49 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
52 #define EM28XX_DVB_NUM_BUFS 5
53 #define EM28XX_DVB_MAX_PACKETS 64
56 struct dvb_frontend
*frontend
;
58 /* feed count management */
62 /* general boilerplate stuff */
63 struct dvb_adapter adapter
;
64 struct dvb_demux demux
;
66 struct dmx_frontend fe_hw
;
67 struct dmx_frontend fe_mem
;
72 static inline void print_err_status(struct em28xx
*dev
,
73 int packet
, int status
)
75 char *errmsg
= "Unknown";
79 errmsg
= "unlinked synchronuously";
82 errmsg
= "unlinked asynchronuously";
85 errmsg
= "Buffer error (overrun)";
88 errmsg
= "Stalled (device not responding)";
91 errmsg
= "Babble (bad cable?)";
94 errmsg
= "Bit-stuff error (bad cable?)";
97 errmsg
= "CRC/Timeout (could be anything)";
100 errmsg
= "Device does not respond";
104 dprintk(1, "URB status %d [%s].\n", status
, errmsg
);
106 dprintk(1, "URB packet %d, status %d [%s].\n",
107 packet
, status
, errmsg
);
111 static inline int dvb_isoc_copy(struct em28xx
*dev
, struct urb
*urb
)
118 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
121 if (urb
->status
< 0) {
122 print_err_status(dev
, -1, urb
->status
);
123 if (urb
->status
== -ENOENT
)
127 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
128 int status
= urb
->iso_frame_desc
[i
].status
;
131 print_err_status(dev
, i
, status
);
132 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
136 dvb_dmx_swfilter(&dev
->dvb
->demux
, urb
->transfer_buffer
+
137 urb
->iso_frame_desc
[i
].offset
,
138 urb
->iso_frame_desc
[i
].actual_length
);
144 static int start_streaming(struct em28xx_dvb
*dvb
)
147 struct em28xx
*dev
= dvb
->adapter
.priv
;
148 int max_dvb_packet_size
;
150 usb_set_interface(dev
->udev
, 0, 1);
151 rc
= em28xx_set_mode(dev
, EM28XX_DIGITAL_MODE
);
155 max_dvb_packet_size
= em28xx_isoc_dvb_max_packetsize(dev
);
157 return em28xx_init_isoc(dev
, EM28XX_DVB_MAX_PACKETS
,
158 EM28XX_DVB_NUM_BUFS
, max_dvb_packet_size
,
162 static int stop_streaming(struct em28xx_dvb
*dvb
)
164 struct em28xx
*dev
= dvb
->adapter
.priv
;
166 em28xx_uninit_isoc(dev
);
168 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
173 static int start_feed(struct dvb_demux_feed
*feed
)
175 struct dvb_demux
*demux
= feed
->demux
;
176 struct em28xx_dvb
*dvb
= demux
->priv
;
179 if (!demux
->dmx
.frontend
)
182 mutex_lock(&dvb
->lock
);
186 if (dvb
->nfeeds
== 1) {
187 ret
= start_streaming(dvb
);
192 mutex_unlock(&dvb
->lock
);
196 static int stop_feed(struct dvb_demux_feed
*feed
)
198 struct dvb_demux
*demux
= feed
->demux
;
199 struct em28xx_dvb
*dvb
= demux
->priv
;
202 mutex_lock(&dvb
->lock
);
205 if (0 == dvb
->nfeeds
)
206 err
= stop_streaming(dvb
);
208 mutex_unlock(&dvb
->lock
);
214 /* ------------------------------------------------------------------ */
215 static int em28xx_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
217 struct em28xx
*dev
= fe
->dvb
->priv
;
220 return em28xx_set_mode(dev
, EM28XX_DIGITAL_MODE
);
222 return em28xx_set_mode(dev
, EM28XX_SUSPEND
);
225 /* ------------------------------------------------------------------ */
227 static struct lgdt330x_config em2880_lgdt3303_dev
= {
228 .demod_address
= 0x0e,
229 .demod_chip
= LGDT3303
,
232 static struct zl10353_config em28xx_zl10353_with_xc3028
= {
233 .demod_address
= (0x1e >> 1),
239 static struct s5h1409_config em28xx_s5h1409_with_xc3028
= {
240 .demod_address
= 0x32 >> 1,
241 .output_mode
= S5H1409_PARALLEL_OUTPUT
,
242 .gpio
= S5H1409_GPIO_OFF
,
243 .inversion
= S5H1409_INVERSION_OFF
,
244 .status_mode
= S5H1409_DEMODLOCKING
,
245 .mpeg_timing
= S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
248 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate
= {
249 .demod_address
= (0x1e >> 1),
251 .disable_i2c_gate_ctrl
= 1,
256 #ifdef EM28XX_DRX397XD_SUPPORT
257 /* [TODO] djh - not sure yet what the device config needs to contain */
258 static struct drx397xD_config em28xx_drx397xD_with_xc3028
= {
259 .demod_address
= (0xe0 >> 1),
263 static int mt352_terratec_xs_init(struct dvb_frontend
*fe
)
265 /* Values extracted from a USB trace of the Terratec Windows driver */
266 static u8 clock_config
[] = { CLOCK_CTL
, 0x38, 0x2c };
267 static u8 reset
[] = { RESET
, 0x80 };
268 static u8 adc_ctl_1_cfg
[] = { ADC_CTL_1
, 0x40 };
269 static u8 agc_cfg
[] = { AGC_TARGET
, 0x28, 0xa0 };
270 static u8 input_freq_cfg
[] = { INPUT_FREQ_1
, 0x31, 0xb8 };
271 static u8 rs_err_cfg
[] = { RS_ERR_PER_1
, 0x00, 0x4d };
272 static u8 capt_range_cfg
[] = { CAPT_RANGE
, 0x32 };
273 static u8 trl_nom_cfg
[] = { TRL_NOMINAL_RATE_1
, 0x64, 0x00 };
274 static u8 tps_given_cfg
[] = { TPS_GIVEN_1
, 0x40, 0x80, 0x50 };
275 static u8 tuner_go
[] = { TUNER_GO
, 0x01};
277 mt352_write(fe
, clock_config
, sizeof(clock_config
));
279 mt352_write(fe
, reset
, sizeof(reset
));
280 mt352_write(fe
, adc_ctl_1_cfg
, sizeof(adc_ctl_1_cfg
));
281 mt352_write(fe
, agc_cfg
, sizeof(agc_cfg
));
282 mt352_write(fe
, input_freq_cfg
, sizeof(input_freq_cfg
));
283 mt352_write(fe
, rs_err_cfg
, sizeof(rs_err_cfg
));
284 mt352_write(fe
, capt_range_cfg
, sizeof(capt_range_cfg
));
285 mt352_write(fe
, trl_nom_cfg
, sizeof(trl_nom_cfg
));
286 mt352_write(fe
, tps_given_cfg
, sizeof(tps_given_cfg
));
287 mt352_write(fe
, tuner_go
, sizeof(tuner_go
));
291 static struct mt352_config terratec_xs_mt352_cfg
= {
292 .demod_address
= (0x1e >> 1),
295 .demod_init
= mt352_terratec_xs_init
,
298 /* ------------------------------------------------------------------ */
300 static int attach_xc3028(u8 addr
, struct em28xx
*dev
)
302 struct dvb_frontend
*fe
;
303 struct xc2028_config cfg
;
305 memset(&cfg
, 0, sizeof(cfg
));
306 cfg
.i2c_adap
= &dev
->i2c_adap
;
309 if (!dev
->dvb
->frontend
) {
310 printk(KERN_ERR
"%s/2: dvb frontend not attached. "
311 "Can't attach xc3028\n",
316 fe
= dvb_attach(xc2028_attach
, dev
->dvb
->frontend
, &cfg
);
318 printk(KERN_ERR
"%s/2: xc3028 attach failed\n",
320 dvb_frontend_detach(dev
->dvb
->frontend
);
321 dev
->dvb
->frontend
= NULL
;
325 printk(KERN_INFO
"%s/2: xc3028 attached\n", dev
->name
);
330 /* ------------------------------------------------------------------ */
332 static int register_dvb(struct em28xx_dvb
*dvb
,
333 struct module
*module
,
335 struct device
*device
)
339 mutex_init(&dvb
->lock
);
341 /* register adapter */
342 result
= dvb_register_adapter(&dvb
->adapter
, dev
->name
, module
, device
,
345 printk(KERN_WARNING
"%s: dvb_register_adapter failed (errno = %d)\n",
350 /* Ensure all frontends negotiate bus access */
351 dvb
->frontend
->ops
.ts_bus_ctrl
= em28xx_dvb_bus_ctrl
;
353 dvb
->adapter
.priv
= dev
;
355 /* register frontend */
356 result
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
358 printk(KERN_WARNING
"%s: dvb_register_frontend failed (errno = %d)\n",
363 /* register demux stuff */
364 dvb
->demux
.dmx
.capabilities
=
365 DMX_TS_FILTERING
| DMX_SECTION_FILTERING
|
366 DMX_MEMORY_BASED_FILTERING
;
367 dvb
->demux
.priv
= dvb
;
368 dvb
->demux
.filternum
= 256;
369 dvb
->demux
.feednum
= 256;
370 dvb
->demux
.start_feed
= start_feed
;
371 dvb
->demux
.stop_feed
= stop_feed
;
373 result
= dvb_dmx_init(&dvb
->demux
);
375 printk(KERN_WARNING
"%s: dvb_dmx_init failed (errno = %d)\n",
380 dvb
->dmxdev
.filternum
= 256;
381 dvb
->dmxdev
.demux
= &dvb
->demux
.dmx
;
382 dvb
->dmxdev
.capabilities
= 0;
383 result
= dvb_dmxdev_init(&dvb
->dmxdev
, &dvb
->adapter
);
385 printk(KERN_WARNING
"%s: dvb_dmxdev_init failed (errno = %d)\n",
390 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
391 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
393 printk(KERN_WARNING
"%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
398 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
399 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
401 printk(KERN_WARNING
"%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
406 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
408 printk(KERN_WARNING
"%s: connect_frontend failed (errno = %d)\n",
413 /* register network adapter */
414 dvb_net_init(&dvb
->adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
418 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
420 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
422 dvb_dmxdev_release(&dvb
->dmxdev
);
424 dvb_dmx_release(&dvb
->demux
);
426 dvb_unregister_frontend(dvb
->frontend
);
428 dvb_frontend_detach(dvb
->frontend
);
429 dvb_unregister_adapter(&dvb
->adapter
);
434 static void unregister_dvb(struct em28xx_dvb
*dvb
)
436 dvb_net_release(&dvb
->net
);
437 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
438 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
439 dvb_dmxdev_release(&dvb
->dmxdev
);
440 dvb_dmx_release(&dvb
->demux
);
441 dvb_unregister_frontend(dvb
->frontend
);
442 dvb_frontend_detach(dvb
->frontend
);
443 dvb_unregister_adapter(&dvb
->adapter
);
447 static int dvb_init(struct em28xx
*dev
)
450 struct em28xx_dvb
*dvb
;
452 if (!dev
->board
.has_dvb
) {
453 /* This device does not support the extension */
457 dvb
= kzalloc(sizeof(struct em28xx_dvb
), GFP_KERNEL
);
460 printk(KERN_INFO
"em28xx_dvb: memory allocation failed\n");
465 em28xx_set_mode(dev
, EM28XX_DIGITAL_MODE
);
467 switch (dev
->model
) {
468 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850
:
469 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950
:
470 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO
:
471 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600
:
472 dvb
->frontend
= dvb_attach(lgdt330x_attach
,
473 &em2880_lgdt3303_dev
,
475 if (attach_xc3028(0x61, dev
) < 0) {
480 case EM2880_BOARD_KWORLD_DVB_310U
:
481 dvb
->frontend
= dvb_attach(zl10353_attach
,
482 &em28xx_zl10353_with_xc3028
,
484 if (attach_xc3028(0x61, dev
) < 0) {
489 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900
:
490 case EM2880_BOARD_EMPIRE_DUAL_TV
:
491 dvb
->frontend
= dvb_attach(zl10353_attach
,
492 &em28xx_zl10353_xc3028_no_i2c_gate
,
494 if (attach_xc3028(0x61, dev
) < 0) {
499 case EM2880_BOARD_TERRATEC_HYBRID_XS
:
500 case EM2881_BOARD_PINNACLE_HYBRID_PRO
:
501 dvb
->frontend
= dvb_attach(zl10353_attach
,
502 &em28xx_zl10353_xc3028_no_i2c_gate
,
504 if (dvb
->frontend
== NULL
) {
505 /* This board could have either a zl10353 or a mt352.
506 If the chip id isn't for zl10353, try mt352 */
507 dvb
->frontend
= dvb_attach(mt352_attach
,
508 &terratec_xs_mt352_cfg
,
512 if (attach_xc3028(0x61, dev
) < 0) {
517 case EM2883_BOARD_KWORLD_HYBRID_330U
:
518 case EM2882_BOARD_EVGA_INDTUBE
:
519 dvb
->frontend
= dvb_attach(s5h1409_attach
,
520 &em28xx_s5h1409_with_xc3028
,
522 if (attach_xc3028(0x61, dev
) < 0) {
527 case EM2882_BOARD_KWORLD_ATSC_315U
:
528 dvb
->frontend
= dvb_attach(lgdt330x_attach
,
529 &em2880_lgdt3303_dev
,
531 if (dvb
->frontend
!= NULL
) {
532 if (!dvb_attach(simple_tuner_attach
, dvb
->frontend
,
533 &dev
->i2c_adap
, 0x61, TUNER_THOMSON_DTT761X
)) {
539 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2
:
540 #ifdef EM28XX_DRX397XD_SUPPORT
541 /* We don't have the config structure properly populated, so
542 this is commented out for now */
543 dvb
->frontend
= dvb_attach(drx397xD_attach
,
544 &em28xx_drx397xD_with_xc3028
,
546 if (attach_xc3028(0x61, dev
) < 0) {
553 printk(KERN_ERR
"%s/2: The frontend of your DVB/ATSC card"
554 " isn't supported yet\n",
558 if (NULL
== dvb
->frontend
) {
560 "%s/2: frontend initialization failed\n",
565 /* define general-purpose callback pointer */
566 dvb
->frontend
->callback
= em28xx_tuner_callback
;
568 /* register everything */
569 result
= register_dvb(dvb
, THIS_MODULE
, dev
, &dev
->udev
->dev
);
574 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
575 printk(KERN_INFO
"Successfully loaded em28xx-dvb\n");
579 em28xx_set_mode(dev
, EM28XX_SUSPEND
);
585 static int dvb_fini(struct em28xx
*dev
)
587 if (!dev
->board
.has_dvb
) {
588 /* This device does not support the extension */
593 unregister_dvb(dev
->dvb
);
600 static struct em28xx_ops dvb_ops
= {
602 .name
= "Em28xx dvb Extension",
607 static int __init
em28xx_dvb_register(void)
609 return em28xx_register_extension(&dvb_ops
);
612 static void __exit
em28xx_dvb_unregister(void)
614 em28xx_unregister_extension(&dvb_ops
);
617 module_init(em28xx_dvb_register
);
618 module_exit(em28xx_dvb_unregister
);