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>
31 #ifdef EM28XX_DRX397XD_SUPPORT
35 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
36 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
37 MODULE_LICENSE("GPL");
39 static unsigned int debug;
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
43 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
45 #define dprintk(level, fmt, arg...) do { \
47 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
50 #define EM28XX_DVB_NUM_BUFS 5
51 #define EM28XX_DVB_MAX_PACKETSIZE 564
52 #define EM28XX_DVB_MAX_PACKETS 64
55 struct dvb_frontend *frontend;
57 /* feed count management */
61 /* general boilerplate stuff */
62 struct dvb_adapter adapter;
63 struct dvb_demux demux;
65 struct dmx_frontend fe_hw;
66 struct dmx_frontend fe_mem;
71 static inline void print_err_status(struct em28xx *dev,
72 int packet, int status)
74 char *errmsg = "Unknown";
78 errmsg = "unlinked synchronuously";
81 errmsg = "unlinked asynchronuously";
84 errmsg = "Buffer error (overrun)";
87 errmsg = "Stalled (device not responding)";
90 errmsg = "Babble (bad cable?)";
93 errmsg = "Bit-stuff error (bad cable?)";
96 errmsg = "CRC/Timeout (could be anything)";
99 errmsg = "Device does not respond";
103 dprintk(1, "URB status %d [%s].\n", status, errmsg);
105 dprintk(1, "URB packet %d, status %d [%s].\n",
106 packet, status, errmsg);
110 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
117 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
120 if (urb->status < 0) {
121 print_err_status(dev, -1, urb->status);
122 if (urb->status == -ENOENT)
126 for (i = 0; i < urb->number_of_packets; i++) {
127 int status = urb->iso_frame_desc[i].status;
130 print_err_status(dev, i, status);
131 if (urb->iso_frame_desc[i].status != -EPROTO)
135 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
136 urb->iso_frame_desc[i].offset,
137 urb->iso_frame_desc[i].actual_length);
143 static int start_streaming(struct em28xx_dvb *dvb)
146 struct em28xx *dev = dvb->adapter.priv;
148 usb_set_interface(dev->udev, 0, 1);
149 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
153 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
154 EM28XX_DVB_NUM_BUFS, EM28XX_DVB_MAX_PACKETSIZE,
158 static int stop_streaming(struct em28xx_dvb *dvb)
160 struct em28xx *dev = dvb->adapter.priv;
162 em28xx_uninit_isoc(dev);
164 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
169 static int start_feed(struct dvb_demux_feed *feed)
171 struct dvb_demux *demux = feed->demux;
172 struct em28xx_dvb *dvb = demux->priv;
175 if (!demux->dmx.frontend)
178 mutex_lock(&dvb->lock);
182 if (dvb->nfeeds == 1) {
183 ret = start_streaming(dvb);
188 mutex_unlock(&dvb->lock);
192 static int stop_feed(struct dvb_demux_feed *feed)
194 struct dvb_demux *demux = feed->demux;
195 struct em28xx_dvb *dvb = demux->priv;
198 mutex_lock(&dvb->lock);
201 if (0 == dvb->nfeeds)
202 err = stop_streaming(dvb);
204 mutex_unlock(&dvb->lock);
210 /* ------------------------------------------------------------------ */
211 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
213 struct em28xx *dev = fe->dvb->priv;
216 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
218 return em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
221 /* ------------------------------------------------------------------ */
223 static struct lgdt330x_config em2880_lgdt3303_dev = {
224 .demod_address = 0x0e,
225 .demod_chip = LGDT3303,
228 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
229 .demod_address = (0x1e >> 1),
235 #ifdef EM28XX_DRX397XD_SUPPORT
236 /* [TODO] djh - not sure yet what the device config needs to contain */
237 static struct drx397xD_config em28xx_drx397xD_with_xc3028 = {
238 .demod_address = (0xe0 >> 1),
242 /* ------------------------------------------------------------------ */
244 static int attach_xc3028(u8 addr, struct em28xx *dev)
246 struct dvb_frontend *fe;
247 struct xc2028_config cfg;
249 memset(&cfg, 0, sizeof(cfg));
250 cfg.i2c_adap = &dev->i2c_adap;
252 cfg.callback = em28xx_tuner_callback;
254 if (!dev->dvb->frontend) {
255 printk(KERN_ERR "%s/2: dvb frontend not attached. "
256 "Can't attach xc3028\n",
261 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
263 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
265 dvb_frontend_detach(dev->dvb->frontend);
266 dev->dvb->frontend = NULL;
270 printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
275 /* ------------------------------------------------------------------ */
277 static int register_dvb(struct em28xx_dvb *dvb,
278 struct module *module,
280 struct device *device)
284 mutex_init(&dvb->lock);
286 /* register adapter */
287 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
290 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
295 /* Ensure all frontends negotiate bus access */
296 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
298 dvb->adapter.priv = dev;
300 /* register frontend */
301 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
303 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
308 /* register demux stuff */
309 dvb->demux.dmx.capabilities =
310 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
311 DMX_MEMORY_BASED_FILTERING;
312 dvb->demux.priv = dvb;
313 dvb->demux.filternum = 256;
314 dvb->demux.feednum = 256;
315 dvb->demux.start_feed = start_feed;
316 dvb->demux.stop_feed = stop_feed;
318 result = dvb_dmx_init(&dvb->demux);
320 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
325 dvb->dmxdev.filternum = 256;
326 dvb->dmxdev.demux = &dvb->demux.dmx;
327 dvb->dmxdev.capabilities = 0;
328 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
330 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
335 dvb->fe_hw.source = DMX_FRONTEND_0;
336 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
338 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
343 dvb->fe_mem.source = DMX_MEMORY_FE;
344 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
346 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
351 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
353 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
358 /* register network adapter */
359 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
363 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
365 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
367 dvb_dmxdev_release(&dvb->dmxdev);
369 dvb_dmx_release(&dvb->demux);
371 dvb_unregister_frontend(dvb->frontend);
373 dvb_frontend_detach(dvb->frontend);
374 dvb_unregister_adapter(&dvb->adapter);
379 static void unregister_dvb(struct em28xx_dvb *dvb)
381 dvb_net_release(&dvb->net);
382 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
383 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
384 dvb_dmxdev_release(&dvb->dmxdev);
385 dvb_dmx_release(&dvb->demux);
386 dvb_unregister_frontend(dvb->frontend);
387 dvb_frontend_detach(dvb->frontend);
388 dvb_unregister_adapter(&dvb->adapter);
392 static int dvb_init(struct em28xx *dev)
395 struct em28xx_dvb *dvb;
398 /* This device does not support the extension */
402 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
405 printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
410 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
412 switch (dev->model) {
413 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
414 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
415 case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
416 dvb->frontend = dvb_attach(lgdt330x_attach,
417 &em2880_lgdt3303_dev,
419 if (attach_xc3028(0x61, dev) < 0) {
424 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
425 dvb->frontend = dvb_attach(zl10353_attach,
426 &em28xx_zl10353_with_xc3028,
428 if (attach_xc3028(0x61, dev) < 0) {
433 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
434 #ifdef EM28XX_DRX397XD_SUPPORT
435 /* We don't have the config structure properly populated, so
436 this is commented out for now */
437 dvb->frontend = dvb_attach(drx397xD_attach,
438 &em28xx_drx397xD_with_xc3028,
440 if (attach_xc3028(0x61, dev) < 0) {
446 case EM2880_BOARD_TERRATEC_HYBRID_XS:
447 dvb->frontend = dvb_attach(zl10353_attach,
448 &em28xx_zl10353_with_xc3028,
450 if (attach_xc3028(0x61, dev) < 0) {
455 case EM2880_BOARD_KWORLD_DVB_310U:
456 dvb->frontend = dvb_attach(zl10353_attach,
457 &em28xx_zl10353_with_xc3028,
459 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",
478 /* register everything */
479 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
484 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
485 printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
489 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
495 static int dvb_fini(struct em28xx *dev)
498 /* This device does not support the extension */
503 unregister_dvb(dev->dvb);
510 static struct em28xx_ops dvb_ops = {
512 .name = "Em28xx dvb Extension",
517 static int __init em28xx_dvb_register(void)
519 return em28xx_register_extension(&dvb_ops);
522 static void __exit em28xx_dvb_unregister(void)
524 em28xx_unregister_extension(&dvb_ops);
527 module_init(em28xx_dvb_register);
528 module_exit(em28xx_dvb_unregister);