ext3: Update MAINTAINERS for ext3 and JBD
[linux/fpc-iii.git] / drivers / media / video / em28xx / em28xx-dvb.c
blobd603575431b44b71d59288ac5d860f6d7e4ae254
1 /*
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>
25 #include "em28xx.h"
26 #include <media/v4l2-common.h>
27 #include <media/videobuf-vmalloc.h>
28 #include <media/tuner.h>
29 #include "tuner-simple.h"
31 #include "lgdt330x.h"
32 #include "zl10353.h"
33 #include "s5h1409.h"
34 #include "mt352.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 { \
48 if (debug >= level) \
49 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
50 } while (0)
52 #define EM28XX_DVB_NUM_BUFS 5
53 #define EM28XX_DVB_MAX_PACKETS 64
55 struct em28xx_dvb {
56 struct dvb_frontend *frontend;
58 /* feed count management */
59 struct mutex lock;
60 int nfeeds;
62 /* general boilerplate stuff */
63 struct dvb_adapter adapter;
64 struct dvb_demux demux;
65 struct dmxdev dmxdev;
66 struct dmx_frontend fe_hw;
67 struct dmx_frontend fe_mem;
68 struct dvb_net net;
72 static inline void print_err_status(struct em28xx *dev,
73 int packet, int status)
75 char *errmsg = "Unknown";
77 switch (status) {
78 case -ENOENT:
79 errmsg = "unlinked synchronuously";
80 break;
81 case -ECONNRESET:
82 errmsg = "unlinked asynchronuously";
83 break;
84 case -ENOSR:
85 errmsg = "Buffer error (overrun)";
86 break;
87 case -EPIPE:
88 errmsg = "Stalled (device not responding)";
89 break;
90 case -EOVERFLOW:
91 errmsg = "Babble (bad cable?)";
92 break;
93 case -EPROTO:
94 errmsg = "Bit-stuff error (bad cable?)";
95 break;
96 case -EILSEQ:
97 errmsg = "CRC/Timeout (could be anything)";
98 break;
99 case -ETIME:
100 errmsg = "Device does not respond";
101 break;
103 if (packet < 0) {
104 dprintk(1, "URB status %d [%s].\n", status, errmsg);
105 } else {
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)
113 int i;
115 if (!dev)
116 return 0;
118 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
119 return 0;
121 if (urb->status < 0) {
122 print_err_status(dev, -1, urb->status);
123 if (urb->status == -ENOENT)
124 return 0;
127 for (i = 0; i < urb->number_of_packets; i++) {
128 int status = urb->iso_frame_desc[i].status;
130 if (status < 0) {
131 print_err_status(dev, i, status);
132 if (urb->iso_frame_desc[i].status != -EPROTO)
133 continue;
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);
141 return 0;
144 static int start_streaming(struct em28xx_dvb *dvb)
146 int rc;
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);
152 if (rc < 0)
153 return rc;
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,
159 dvb_isoc_copy);
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);
170 return 0;
173 static int start_feed(struct dvb_demux_feed *feed)
175 struct dvb_demux *demux = feed->demux;
176 struct em28xx_dvb *dvb = demux->priv;
177 int rc, ret;
179 if (!demux->dmx.frontend)
180 return -EINVAL;
182 mutex_lock(&dvb->lock);
183 dvb->nfeeds++;
184 rc = dvb->nfeeds;
186 if (dvb->nfeeds == 1) {
187 ret = start_streaming(dvb);
188 if (ret < 0)
189 rc = ret;
192 mutex_unlock(&dvb->lock);
193 return rc;
196 static int stop_feed(struct dvb_demux_feed *feed)
198 struct dvb_demux *demux = feed->demux;
199 struct em28xx_dvb *dvb = demux->priv;
200 int err = 0;
202 mutex_lock(&dvb->lock);
203 dvb->nfeeds--;
205 if (0 == dvb->nfeeds)
206 err = stop_streaming(dvb);
208 mutex_unlock(&dvb->lock);
209 return err;
214 /* ------------------------------------------------------------------ */
215 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
217 struct em28xx *dev = fe->dvb->priv;
219 if (acquire)
220 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
221 else
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),
234 .no_tuner = 1,
235 .parallel_ts = 1,
236 .if2 = 45600,
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),
250 .no_tuner = 1,
251 .disable_i2c_gate_ctrl = 1,
252 .parallel_ts = 1,
253 .if2 = 45600,
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),
261 #endif
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));
278 udelay(200);
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));
288 return 0;
291 static struct mt352_config terratec_xs_mt352_cfg = {
292 .demod_address = (0x1e >> 1),
293 .no_tuner = 1,
294 .if2 = 45600,
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;
307 cfg.i2c_addr = addr;
309 if (!dev->dvb->frontend) {
310 printk(KERN_ERR "%s/2: dvb frontend not attached. "
311 "Can't attach xc3028\n",
312 dev->name);
313 return -EINVAL;
316 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
317 if (!fe) {
318 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
319 dev->name);
320 dvb_frontend_detach(dev->dvb->frontend);
321 dev->dvb->frontend = NULL;
322 return -EINVAL;
325 printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
327 return 0;
330 /* ------------------------------------------------------------------ */
332 static int register_dvb(struct em28xx_dvb *dvb,
333 struct module *module,
334 struct em28xx *dev,
335 struct device *device)
337 int result;
339 mutex_init(&dvb->lock);
341 /* register adapter */
342 result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
343 adapter_nr);
344 if (result < 0) {
345 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
346 dev->name, result);
347 goto fail_adapter;
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);
357 if (result < 0) {
358 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
359 dev->name, result);
360 goto fail_frontend;
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);
374 if (result < 0) {
375 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
376 dev->name, result);
377 goto fail_dmx;
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);
384 if (result < 0) {
385 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
386 dev->name, result);
387 goto fail_dmxdev;
390 dvb->fe_hw.source = DMX_FRONTEND_0;
391 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
392 if (result < 0) {
393 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
394 dev->name, result);
395 goto fail_fe_hw;
398 dvb->fe_mem.source = DMX_MEMORY_FE;
399 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
400 if (result < 0) {
401 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
402 dev->name, result);
403 goto fail_fe_mem;
406 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
407 if (result < 0) {
408 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
409 dev->name, result);
410 goto fail_fe_conn;
413 /* register network adapter */
414 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
415 return 0;
417 fail_fe_conn:
418 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
419 fail_fe_mem:
420 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
421 fail_fe_hw:
422 dvb_dmxdev_release(&dvb->dmxdev);
423 fail_dmxdev:
424 dvb_dmx_release(&dvb->demux);
425 fail_dmx:
426 dvb_unregister_frontend(dvb->frontend);
427 fail_frontend:
428 dvb_frontend_detach(dvb->frontend);
429 dvb_unregister_adapter(&dvb->adapter);
430 fail_adapter:
431 return result;
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)
449 int result = 0;
450 struct em28xx_dvb *dvb;
452 if (!dev->board.has_dvb) {
453 /* This device does not support the extension */
454 return 0;
457 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
459 if (dvb == NULL) {
460 printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
461 return -ENOMEM;
463 dev->dvb = dvb;
465 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
466 /* init frontend */
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,
474 &dev->i2c_adap);
475 if (attach_xc3028(0x61, dev) < 0) {
476 result = -EINVAL;
477 goto out_free;
479 break;
480 case EM2880_BOARD_KWORLD_DVB_310U:
481 dvb->frontend = dvb_attach(zl10353_attach,
482 &em28xx_zl10353_with_xc3028,
483 &dev->i2c_adap);
484 if (attach_xc3028(0x61, dev) < 0) {
485 result = -EINVAL;
486 goto out_free;
488 break;
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,
493 &dev->i2c_adap);
494 if (attach_xc3028(0x61, dev) < 0) {
495 result = -EINVAL;
496 goto out_free;
498 break;
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,
503 &dev->i2c_adap);
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,
509 &dev->i2c_adap);
512 if (attach_xc3028(0x61, dev) < 0) {
513 result = -EINVAL;
514 goto out_free;
516 break;
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,
521 &dev->i2c_adap);
522 if (attach_xc3028(0x61, dev) < 0) {
523 result = -EINVAL;
524 goto out_free;
526 break;
527 case EM2882_BOARD_KWORLD_ATSC_315U:
528 dvb->frontend = dvb_attach(lgdt330x_attach,
529 &em2880_lgdt3303_dev,
530 &dev->i2c_adap);
531 if (dvb->frontend != NULL) {
532 if (!dvb_attach(simple_tuner_attach, dvb->frontend,
533 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
534 result = -EINVAL;
535 goto out_free;
538 break;
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,
545 &dev->i2c_adap);
546 if (attach_xc3028(0x61, dev) < 0) {
547 result = -EINVAL;
548 goto out_free;
550 break;
551 #endif
552 default:
553 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
554 " isn't supported yet\n",
555 dev->name);
556 break;
558 if (NULL == dvb->frontend) {
559 printk(KERN_ERR
560 "%s/2: frontend initialization failed\n",
561 dev->name);
562 result = -EINVAL;
563 goto out_free;
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);
571 if (result < 0)
572 goto out_free;
574 em28xx_set_mode(dev, EM28XX_SUSPEND);
575 printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
576 return 0;
578 out_free:
579 em28xx_set_mode(dev, EM28XX_SUSPEND);
580 kfree(dvb);
581 dev->dvb = NULL;
582 return result;
585 static int dvb_fini(struct em28xx *dev)
587 if (!dev->board.has_dvb) {
588 /* This device does not support the extension */
589 return 0;
592 if (dev->dvb) {
593 unregister_dvb(dev->dvb);
594 dev->dvb = NULL;
597 return 0;
600 static struct em28xx_ops dvb_ops = {
601 .id = EM28XX_DVB,
602 .name = "Em28xx dvb Extension",
603 .init = dvb_init,
604 .fini = dvb_fini,
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);