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
10 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
13 (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
14 (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License.
21 #include <linux/kernel.h>
22 #include <linux/usb.h>
25 #include <media/v4l2-common.h>
26 #include <media/videobuf-vmalloc.h>
31 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
32 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
33 MODULE_LICENSE("GPL");
35 static unsigned int debug;
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
39 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
41 #define dprintk(level, fmt, arg...) do { \
43 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
46 #define EM28XX_DVB_NUM_BUFS 5
47 #define EM28XX_DVB_MAX_PACKETSIZE 564
48 #define EM28XX_DVB_MAX_PACKETS 64
51 struct dvb_frontend *frontend;
53 /* feed count management */
57 /* general boilerplate stuff */
58 struct dvb_adapter adapter;
59 struct dvb_demux demux;
61 struct dmx_frontend fe_hw;
62 struct dmx_frontend fe_mem;
67 static inline void print_err_status(struct em28xx *dev,
68 int packet, int status)
70 char *errmsg = "Unknown";
74 errmsg = "unlinked synchronuously";
77 errmsg = "unlinked asynchronuously";
80 errmsg = "Buffer error (overrun)";
83 errmsg = "Stalled (device not responding)";
86 errmsg = "Babble (bad cable?)";
89 errmsg = "Bit-stuff error (bad cable?)";
92 errmsg = "CRC/Timeout (could be anything)";
95 errmsg = "Device does not respond";
99 dprintk(1, "URB status %d [%s].\n", status, errmsg);
101 dprintk(1, "URB packet %d, status %d [%s].\n",
102 packet, status, errmsg);
106 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
113 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
116 if (urb->status < 0) {
117 print_err_status(dev, -1, urb->status);
118 if (urb->status == -ENOENT)
122 for (i = 0; i < urb->number_of_packets; i++) {
123 int status = urb->iso_frame_desc[i].status;
126 print_err_status(dev, i, status);
127 if (urb->iso_frame_desc[i].status != -EPROTO)
131 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
132 urb->iso_frame_desc[i].offset,
133 urb->iso_frame_desc[i].actual_length);
139 static int start_streaming(struct em28xx_dvb *dvb)
142 struct em28xx *dev = dvb->adapter.priv;
144 usb_set_interface(dev->udev, 0, 1);
145 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
149 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
150 EM28XX_DVB_NUM_BUFS, EM28XX_DVB_MAX_PACKETSIZE,
154 static int stop_streaming(struct em28xx_dvb *dvb)
156 struct em28xx *dev = dvb->adapter.priv;
158 em28xx_uninit_isoc(dev);
160 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
165 static int start_feed(struct dvb_demux_feed *feed)
167 struct dvb_demux *demux = feed->demux;
168 struct em28xx_dvb *dvb = demux->priv;
171 if (!demux->dmx.frontend)
174 mutex_lock(&dvb->lock);
178 if (dvb->nfeeds == 1) {
179 ret = start_streaming(dvb);
184 mutex_unlock(&dvb->lock);
188 static int stop_feed(struct dvb_demux_feed *feed)
190 struct dvb_demux *demux = feed->demux;
191 struct em28xx_dvb *dvb = demux->priv;
194 mutex_lock(&dvb->lock);
197 if (0 == dvb->nfeeds)
198 err = stop_streaming(dvb);
200 mutex_unlock(&dvb->lock);
206 /* ------------------------------------------------------------------ */
207 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
209 struct em28xx *dev = fe->dvb->priv;
212 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
214 return em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
217 /* ------------------------------------------------------------------ */
219 static struct lgdt330x_config em2880_lgdt3303_dev = {
220 .demod_address = 0x0e,
221 .demod_chip = LGDT3303,
224 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
225 .demod_address = (0x1e >> 1),
231 /* ------------------------------------------------------------------ */
233 static int attach_xc3028(u8 addr, struct em28xx *dev)
235 struct dvb_frontend *fe;
236 struct xc2028_config cfg;
238 memset(&cfg, 0, sizeof(cfg));
239 cfg.i2c_adap = &dev->i2c_adap;
241 cfg.callback = em28xx_tuner_callback;
243 if (!dev->dvb->frontend) {
244 printk(KERN_ERR "%s/2: dvb frontend not attached. "
245 "Can't attach xc3028\n",
250 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
252 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
254 dvb_frontend_detach(dev->dvb->frontend);
255 dev->dvb->frontend = NULL;
259 printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
264 /* ------------------------------------------------------------------ */
266 int register_dvb(struct em28xx_dvb *dvb,
267 struct module *module,
269 struct device *device)
273 mutex_init(&dvb->lock);
275 /* register adapter */
276 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
279 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
284 /* Ensure all frontends negotiate bus access */
285 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
287 dvb->adapter.priv = dev;
289 /* register frontend */
290 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
292 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
297 /* register demux stuff */
298 dvb->demux.dmx.capabilities =
299 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
300 DMX_MEMORY_BASED_FILTERING;
301 dvb->demux.priv = dvb;
302 dvb->demux.filternum = 256;
303 dvb->demux.feednum = 256;
304 dvb->demux.start_feed = start_feed;
305 dvb->demux.stop_feed = stop_feed;
307 result = dvb_dmx_init(&dvb->demux);
309 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
314 dvb->dmxdev.filternum = 256;
315 dvb->dmxdev.demux = &dvb->demux.dmx;
316 dvb->dmxdev.capabilities = 0;
317 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
319 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
324 dvb->fe_hw.source = DMX_FRONTEND_0;
325 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
327 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
332 dvb->fe_mem.source = DMX_MEMORY_FE;
333 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
335 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
340 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
342 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
347 /* register network adapter */
348 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
352 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
354 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
356 dvb_dmxdev_release(&dvb->dmxdev);
358 dvb_dmx_release(&dvb->demux);
360 dvb_unregister_frontend(dvb->frontend);
362 dvb_frontend_detach(dvb->frontend);
363 dvb_unregister_adapter(&dvb->adapter);
368 static void unregister_dvb(struct em28xx_dvb *dvb)
370 dvb_net_release(&dvb->net);
371 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
372 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
373 dvb_dmxdev_release(&dvb->dmxdev);
374 dvb_dmx_release(&dvb->demux);
375 dvb_unregister_frontend(dvb->frontend);
376 dvb_frontend_detach(dvb->frontend);
377 dvb_unregister_adapter(&dvb->adapter);
381 static int dvb_init(struct em28xx *dev)
384 struct em28xx_dvb *dvb;
387 /* This device does not support the extension */
391 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
394 printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
399 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
401 switch (dev->model) {
402 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
403 case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
404 dvb->frontend = dvb_attach(lgdt330x_attach,
405 &em2880_lgdt3303_dev,
407 if (attach_xc3028(0x61, dev) < 0) {
412 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
413 dvb->frontend = dvb_attach(zl10353_attach,
414 &em28xx_zl10353_with_xc3028,
416 if (attach_xc3028(0x61, dev) < 0) {
422 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
423 " isn't supported yet\n",
427 if (NULL == dvb->frontend) {
429 "%s/2: frontend initialization failed\n",
435 /* register everything */
436 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
441 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
442 printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
446 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
452 static int dvb_fini(struct em28xx *dev)
455 /* This device does not support the extension */
460 unregister_dvb(dev->dvb);
467 static struct em28xx_ops dvb_ops = {
469 .name = "Em28xx dvb Extension",
474 static int __init em28xx_dvb_register(void)
476 return em28xx_register_extension(&dvb_ops);
479 static void __exit em28xx_dvb_unregister(void)
481 em28xx_unregister_extension(&dvb_ops);
484 module_init(em28xx_dvb_register);
485 module_exit(em28xx_dvb_unregister);