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
9 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
11 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
12 (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
13 (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License.
20 #include <linux/kernel.h>
21 #include <linux/usb.h>
24 #include <media/v4l2-common.h>
25 #include <media/videobuf-vmalloc.h>
30 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
31 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
32 MODULE_LICENSE("GPL");
34 static unsigned int debug;
35 module_param(debug, int, 0644);
36 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
38 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
40 #define dprintk(level, fmt, arg...) do { \
42 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
45 #define EM28XX_DVB_NUM_BUFS 5
46 #define EM28XX_DVB_MAX_PACKETSIZE 564
47 #define EM28XX_DVB_MAX_PACKETS 64
50 struct dvb_frontend *frontend;
52 /* feed count management */
56 /* general boilerplate stuff */
57 struct dvb_adapter adapter;
58 struct dvb_demux demux;
60 struct dmx_frontend fe_hw;
61 struct dmx_frontend fe_mem;
66 static inline void print_err_status(struct em28xx *dev,
67 int packet, int status)
69 char *errmsg = "Unknown";
73 errmsg = "unlinked synchronuously";
76 errmsg = "unlinked asynchronuously";
79 errmsg = "Buffer error (overrun)";
82 errmsg = "Stalled (device not responding)";
85 errmsg = "Babble (bad cable?)";
88 errmsg = "Bit-stuff error (bad cable?)";
91 errmsg = "CRC/Timeout (could be anything)";
94 errmsg = "Device does not respond";
98 dprintk(1, "URB status %d [%s].\n", status, errmsg);
100 dprintk(1, "URB packet %d, status %d [%s].\n",
101 packet, status, errmsg);
105 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
112 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
115 if (urb->status < 0) {
116 print_err_status(dev, -1, urb->status);
117 if (urb->status == -ENOENT)
121 for (i = 0; i < urb->number_of_packets; i++) {
122 int status = urb->iso_frame_desc[i].status;
125 print_err_status(dev, i, status);
126 if (urb->iso_frame_desc[i].status != -EPROTO)
130 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
131 urb->iso_frame_desc[i].offset,
132 urb->iso_frame_desc[i].actual_length);
138 static int start_streaming(struct em28xx_dvb *dvb)
141 struct em28xx *dev = dvb->adapter.priv;
143 usb_set_interface(dev->udev, 0, 1);
144 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
148 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
149 EM28XX_DVB_NUM_BUFS, EM28XX_DVB_MAX_PACKETSIZE,
153 static int stop_streaming(struct em28xx_dvb *dvb)
155 struct em28xx *dev = dvb->adapter.priv;
157 em28xx_uninit_isoc(dev);
159 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
164 static int start_feed(struct dvb_demux_feed *feed)
166 struct dvb_demux *demux = feed->demux;
167 struct em28xx_dvb *dvb = demux->priv;
170 if (!demux->dmx.frontend)
173 mutex_lock(&dvb->lock);
177 if (dvb->nfeeds == 1) {
178 ret = start_streaming(dvb);
183 mutex_unlock(&dvb->lock);
187 static int stop_feed(struct dvb_demux_feed *feed)
189 struct dvb_demux *demux = feed->demux;
190 struct em28xx_dvb *dvb = demux->priv;
193 mutex_lock(&dvb->lock);
196 if (0 == dvb->nfeeds)
197 err = stop_streaming(dvb);
199 mutex_unlock(&dvb->lock);
205 /* ------------------------------------------------------------------ */
206 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
208 struct em28xx *dev = fe->dvb->priv;
211 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
213 return em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
216 /* ------------------------------------------------------------------ */
218 static struct lgdt330x_config em2880_lgdt3303_dev = {
219 .demod_address = 0x0e,
220 .demod_chip = LGDT3303,
223 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
224 .demod_address = (0x1e >> 1),
230 /* ------------------------------------------------------------------ */
232 static int attach_xc3028(u8 addr, struct em28xx *dev)
234 struct dvb_frontend *fe;
235 struct xc2028_config cfg;
237 memset(&cfg, 0, sizeof(cfg));
238 cfg.i2c_adap = &dev->i2c_adap;
240 cfg.callback = em28xx_tuner_callback;
242 if (!dev->dvb->frontend) {
243 printk(KERN_ERR "%s/2: dvb frontend not attached. "
244 "Can't attach xc3028\n",
249 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
251 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
253 dvb_frontend_detach(dev->dvb->frontend);
254 dev->dvb->frontend = NULL;
258 printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
263 /* ------------------------------------------------------------------ */
265 int register_dvb(struct em28xx_dvb *dvb,
266 struct module *module,
268 struct device *device)
272 mutex_init(&dvb->lock);
274 /* register adapter */
275 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
278 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
283 /* Ensure all frontends negotiate bus access */
284 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
286 dvb->adapter.priv = dev;
288 /* register frontend */
289 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
291 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
296 /* register demux stuff */
297 dvb->demux.dmx.capabilities =
298 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
299 DMX_MEMORY_BASED_FILTERING;
300 dvb->demux.priv = dvb;
301 dvb->demux.filternum = 256;
302 dvb->demux.feednum = 256;
303 dvb->demux.start_feed = start_feed;
304 dvb->demux.stop_feed = stop_feed;
306 result = dvb_dmx_init(&dvb->demux);
308 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
313 dvb->dmxdev.filternum = 256;
314 dvb->dmxdev.demux = &dvb->demux.dmx;
315 dvb->dmxdev.capabilities = 0;
316 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
318 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
323 dvb->fe_hw.source = DMX_FRONTEND_0;
324 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
326 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
331 dvb->fe_mem.source = DMX_MEMORY_FE;
332 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
334 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
339 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
341 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
346 /* register network adapter */
347 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
351 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
353 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
355 dvb_dmxdev_release(&dvb->dmxdev);
357 dvb_dmx_release(&dvb->demux);
359 dvb_unregister_frontend(dvb->frontend);
361 dvb_frontend_detach(dvb->frontend);
362 dvb_unregister_adapter(&dvb->adapter);
367 static void unregister_dvb(struct em28xx_dvb *dvb)
369 dvb_net_release(&dvb->net);
370 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
371 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
372 dvb_dmxdev_release(&dvb->dmxdev);
373 dvb_dmx_release(&dvb->demux);
374 dvb_unregister_frontend(dvb->frontend);
375 dvb_frontend_detach(dvb->frontend);
376 dvb_unregister_adapter(&dvb->adapter);
380 static int dvb_init(struct em28xx *dev)
383 struct em28xx_dvb *dvb;
385 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
388 printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
393 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
395 switch (dev->model) {
396 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
397 dvb->frontend = dvb_attach(lgdt330x_attach,
398 &em2880_lgdt3303_dev,
400 if (attach_xc3028(0x61, dev) < 0) {
405 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
406 dvb->frontend = dvb_attach(zl10353_attach,
407 &em28xx_zl10353_with_xc3028,
409 if (attach_xc3028(0x61, dev) < 0) {
415 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
416 " isn't supported yet\n",
420 if (NULL == dvb->frontend) {
422 "%s/2: frontend initialization failed\n",
428 /* register everything */
429 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
434 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
435 printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
439 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
445 static int dvb_fini(struct em28xx *dev)
448 unregister_dvb(dev->dvb);
455 static struct em28xx_ops dvb_ops = {
457 .name = "Em28xx dvb Extension",
462 static int __init em28xx_dvb_register(void)
464 return em28xx_register_extension(&dvb_ops);
467 static void __exit em28xx_dvb_unregister(void)
469 em28xx_unregister_extension(&dvb_ops);
472 module_init(em28xx_dvb_register);
473 module_exit(em28xx_dvb_unregister);