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>
33 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
34 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
35 MODULE_LICENSE("GPL");
37 static unsigned int debug;
38 module_param(debug, int, 0644);
39 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
41 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
43 #define dprintk(level, fmt, arg...) do { \
45 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
48 #define EM28XX_DVB_NUM_BUFS 5
49 #define EM28XX_DVB_MAX_PACKETSIZE 564
50 #define EM28XX_DVB_MAX_PACKETS 64
53 struct dvb_frontend *frontend;
55 /* feed count management */
59 /* general boilerplate stuff */
60 struct dvb_adapter adapter;
61 struct dvb_demux demux;
63 struct dmx_frontend fe_hw;
64 struct dmx_frontend fe_mem;
69 static inline void print_err_status(struct em28xx *dev,
70 int packet, int status)
72 char *errmsg = "Unknown";
76 errmsg = "unlinked synchronuously";
79 errmsg = "unlinked asynchronuously";
82 errmsg = "Buffer error (overrun)";
85 errmsg = "Stalled (device not responding)";
88 errmsg = "Babble (bad cable?)";
91 errmsg = "Bit-stuff error (bad cable?)";
94 errmsg = "CRC/Timeout (could be anything)";
97 errmsg = "Device does not respond";
101 dprintk(1, "URB status %d [%s].\n", status, errmsg);
103 dprintk(1, "URB packet %d, status %d [%s].\n",
104 packet, status, errmsg);
108 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
115 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
118 if (urb->status < 0) {
119 print_err_status(dev, -1, urb->status);
120 if (urb->status == -ENOENT)
124 for (i = 0; i < urb->number_of_packets; i++) {
125 int status = urb->iso_frame_desc[i].status;
128 print_err_status(dev, i, status);
129 if (urb->iso_frame_desc[i].status != -EPROTO)
133 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
134 urb->iso_frame_desc[i].offset,
135 urb->iso_frame_desc[i].actual_length);
141 static int start_streaming(struct em28xx_dvb *dvb)
144 struct em28xx *dev = dvb->adapter.priv;
146 usb_set_interface(dev->udev, 0, 1);
147 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
151 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
152 EM28XX_DVB_NUM_BUFS, EM28XX_DVB_MAX_PACKETSIZE,
156 static int stop_streaming(struct em28xx_dvb *dvb)
158 struct em28xx *dev = dvb->adapter.priv;
160 em28xx_uninit_isoc(dev);
162 em28xx_set_mode(dev, EM28XX_SUSPEND);
167 static int start_feed(struct dvb_demux_feed *feed)
169 struct dvb_demux *demux = feed->demux;
170 struct em28xx_dvb *dvb = demux->priv;
173 if (!demux->dmx.frontend)
176 mutex_lock(&dvb->lock);
180 if (dvb->nfeeds == 1) {
181 ret = start_streaming(dvb);
186 mutex_unlock(&dvb->lock);
190 static int stop_feed(struct dvb_demux_feed *feed)
192 struct dvb_demux *demux = feed->demux;
193 struct em28xx_dvb *dvb = demux->priv;
196 mutex_lock(&dvb->lock);
199 if (0 == dvb->nfeeds)
200 err = stop_streaming(dvb);
202 mutex_unlock(&dvb->lock);
208 /* ------------------------------------------------------------------ */
209 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
211 struct em28xx *dev = fe->dvb->priv;
214 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
216 return em28xx_set_mode(dev, EM28XX_SUSPEND);
219 /* ------------------------------------------------------------------ */
221 static struct lgdt330x_config em2880_lgdt3303_dev = {
222 .demod_address = 0x0e,
223 .demod_chip = LGDT3303,
226 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
227 .demod_address = (0x1e >> 1),
233 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
234 .demod_address = 0x32 >> 1,
235 .output_mode = S5H1409_PARALLEL_OUTPUT,
236 .gpio = S5H1409_GPIO_OFF,
237 .inversion = S5H1409_INVERSION_OFF,
238 .status_mode = S5H1409_DEMODLOCKING,
239 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
242 #ifdef EM28XX_DRX397XD_SUPPORT
243 /* [TODO] djh - not sure yet what the device config needs to contain */
244 static struct drx397xD_config em28xx_drx397xD_with_xc3028 = {
245 .demod_address = (0xe0 >> 1),
249 /* ------------------------------------------------------------------ */
251 static int attach_xc3028(u8 addr, struct em28xx *dev)
253 struct dvb_frontend *fe;
254 struct xc2028_config cfg;
256 memset(&cfg, 0, sizeof(cfg));
257 cfg.i2c_adap = &dev->i2c_adap;
260 if (!dev->dvb->frontend) {
261 printk(KERN_ERR "%s/2: dvb frontend not attached. "
262 "Can't attach xc3028\n",
267 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
269 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
271 dvb_frontend_detach(dev->dvb->frontend);
272 dev->dvb->frontend = NULL;
276 printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
281 /* ------------------------------------------------------------------ */
283 static int register_dvb(struct em28xx_dvb *dvb,
284 struct module *module,
286 struct device *device)
290 mutex_init(&dvb->lock);
292 /* register adapter */
293 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
296 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
301 /* Ensure all frontends negotiate bus access */
302 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
304 dvb->adapter.priv = dev;
306 /* register frontend */
307 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
309 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
314 /* register demux stuff */
315 dvb->demux.dmx.capabilities =
316 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
317 DMX_MEMORY_BASED_FILTERING;
318 dvb->demux.priv = dvb;
319 dvb->demux.filternum = 256;
320 dvb->demux.feednum = 256;
321 dvb->demux.start_feed = start_feed;
322 dvb->demux.stop_feed = stop_feed;
324 result = dvb_dmx_init(&dvb->demux);
326 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
331 dvb->dmxdev.filternum = 256;
332 dvb->dmxdev.demux = &dvb->demux.dmx;
333 dvb->dmxdev.capabilities = 0;
334 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
336 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
341 dvb->fe_hw.source = DMX_FRONTEND_0;
342 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
344 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
349 dvb->fe_mem.source = DMX_MEMORY_FE;
350 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
352 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
357 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
359 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
364 /* register network adapter */
365 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
369 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
371 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
373 dvb_dmxdev_release(&dvb->dmxdev);
375 dvb_dmx_release(&dvb->demux);
377 dvb_unregister_frontend(dvb->frontend);
379 dvb_frontend_detach(dvb->frontend);
380 dvb_unregister_adapter(&dvb->adapter);
385 static void unregister_dvb(struct em28xx_dvb *dvb)
387 dvb_net_release(&dvb->net);
388 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
389 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
390 dvb_dmxdev_release(&dvb->dmxdev);
391 dvb_dmx_release(&dvb->demux);
392 dvb_unregister_frontend(dvb->frontend);
393 dvb_frontend_detach(dvb->frontend);
394 dvb_unregister_adapter(&dvb->adapter);
398 static int dvb_init(struct em28xx *dev)
401 struct em28xx_dvb *dvb;
403 if (!dev->board.has_dvb) {
404 /* This device does not support the extension */
408 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
411 printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
416 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
418 switch (dev->model) {
419 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
420 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
421 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
422 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
423 dvb->frontend = dvb_attach(lgdt330x_attach,
424 &em2880_lgdt3303_dev,
426 if (attach_xc3028(0x61, dev) < 0) {
431 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
432 case EM2880_BOARD_TERRATEC_HYBRID_XS:
433 case EM2880_BOARD_KWORLD_DVB_310U:
434 dvb->frontend = dvb_attach(zl10353_attach,
435 &em28xx_zl10353_with_xc3028,
437 if (attach_xc3028(0x61, dev) < 0) {
442 case EM2883_BOARD_KWORLD_HYBRID_330U:
443 dvb->frontend = dvb_attach(s5h1409_attach,
444 &em28xx_s5h1409_with_xc3028,
446 if (attach_xc3028(0x61, dev) < 0) {
451 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
452 #ifdef EM28XX_DRX397XD_SUPPORT
453 /* We don't have the config structure properly populated, so
454 this is commented out for now */
455 dvb->frontend = dvb_attach(drx397xD_attach,
456 &em28xx_drx397xD_with_xc3028,
458 if (attach_xc3028(0x61, dev) < 0) {
465 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
466 " isn't supported yet\n",
470 if (NULL == dvb->frontend) {
472 "%s/2: frontend initialization failed\n",
477 /* define general-purpose callback pointer */
478 dvb->frontend->callback = em28xx_tuner_callback;
480 /* register everything */
481 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
486 em28xx_set_mode(dev, EM28XX_SUSPEND);
487 printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
491 em28xx_set_mode(dev, EM28XX_SUSPEND);
497 static int dvb_fini(struct em28xx *dev)
499 if (!dev->board.has_dvb) {
500 /* This device does not support the extension */
505 unregister_dvb(dev->dvb);
512 static struct em28xx_ops dvb_ops = {
514 .name = "Em28xx dvb Extension",
519 static int __init em28xx_dvb_register(void)
521 return em28xx_register_extension(&dvb_ops);
524 static void __exit em28xx_dvb_unregister(void)
526 em28xx_unregister_extension(&dvb_ops);
529 module_init(em28xx_dvb_register);
530 module_exit(em28xx_dvb_unregister);