Merge branch 'linus' into sched/core
[linux-3.10.git] / drivers / media / video / em28xx / em28xx-dvb.c
1 /*
2  DVB device driver for em28xx
3
4  (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org>
5
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
10
11  (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12
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]
16
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.
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/usb.h>
25
26 #include "em28xx.h"
27 #include <media/v4l2-common.h>
28 #include <media/videobuf-vmalloc.h>
29 #include <media/tuner.h>
30 #include "tuner-simple.h"
31
32 #include "lgdt330x.h"
33 #include "zl10353.h"
34 #include "s5h1409.h"
35 #include "mt352.h"
36 #include "mt352_priv.h" /* FIXME */
37 #include "tda1002x.h"
38
39 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
40 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
41 MODULE_LICENSE("GPL");
42
43 static unsigned int debug;
44 module_param(debug, int, 0644);
45 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
46
47 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
48
49 #define dprintk(level, fmt, arg...) do {                        \
50 if (debug >= level)                                             \
51         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
52 } while (0)
53
54 #define EM28XX_DVB_NUM_BUFS 5
55 #define EM28XX_DVB_MAX_PACKETS 64
56
57 struct em28xx_dvb {
58         struct dvb_frontend        *frontend;
59
60         /* feed count management */
61         struct mutex               lock;
62         int                        nfeeds;
63
64         /* general boilerplate stuff */
65         struct dvb_adapter         adapter;
66         struct dvb_demux           demux;
67         struct dmxdev              dmxdev;
68         struct dmx_frontend        fe_hw;
69         struct dmx_frontend        fe_mem;
70         struct dvb_net             net;
71 };
72
73
74 static inline void print_err_status(struct em28xx *dev,
75                                      int packet, int status)
76 {
77         char *errmsg = "Unknown";
78
79         switch (status) {
80         case -ENOENT:
81                 errmsg = "unlinked synchronuously";
82                 break;
83         case -ECONNRESET:
84                 errmsg = "unlinked asynchronuously";
85                 break;
86         case -ENOSR:
87                 errmsg = "Buffer error (overrun)";
88                 break;
89         case -EPIPE:
90                 errmsg = "Stalled (device not responding)";
91                 break;
92         case -EOVERFLOW:
93                 errmsg = "Babble (bad cable?)";
94                 break;
95         case -EPROTO:
96                 errmsg = "Bit-stuff error (bad cable?)";
97                 break;
98         case -EILSEQ:
99                 errmsg = "CRC/Timeout (could be anything)";
100                 break;
101         case -ETIME:
102                 errmsg = "Device does not respond";
103                 break;
104         }
105         if (packet < 0) {
106                 dprintk(1, "URB status %d [%s].\n", status, errmsg);
107         } else {
108                 dprintk(1, "URB packet %d, status %d [%s].\n",
109                         packet, status, errmsg);
110         }
111 }
112
113 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
114 {
115         int i;
116
117         if (!dev)
118                 return 0;
119
120         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
121                 return 0;
122
123         if (urb->status < 0) {
124                 print_err_status(dev, -1, urb->status);
125                 if (urb->status == -ENOENT)
126                         return 0;
127         }
128
129         for (i = 0; i < urb->number_of_packets; i++) {
130                 int status = urb->iso_frame_desc[i].status;
131
132                 if (status < 0) {
133                         print_err_status(dev, i, status);
134                         if (urb->iso_frame_desc[i].status != -EPROTO)
135                                 continue;
136                 }
137
138                 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
139                                  urb->iso_frame_desc[i].offset,
140                                  urb->iso_frame_desc[i].actual_length);
141         }
142
143         return 0;
144 }
145
146 static int start_streaming(struct em28xx_dvb *dvb)
147 {
148         int rc;
149         struct em28xx *dev = dvb->adapter.priv;
150         int max_dvb_packet_size;
151
152         usb_set_interface(dev->udev, 0, 1);
153         rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
154         if (rc < 0)
155                 return rc;
156
157         max_dvb_packet_size = em28xx_isoc_dvb_max_packetsize(dev);
158
159         return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
160                                 EM28XX_DVB_NUM_BUFS, max_dvb_packet_size,
161                                 dvb_isoc_copy);
162 }
163
164 static int stop_streaming(struct em28xx_dvb *dvb)
165 {
166         struct em28xx *dev = dvb->adapter.priv;
167
168         em28xx_uninit_isoc(dev);
169
170         em28xx_set_mode(dev, EM28XX_SUSPEND);
171
172         return 0;
173 }
174
175 static int start_feed(struct dvb_demux_feed *feed)
176 {
177         struct dvb_demux *demux  = feed->demux;
178         struct em28xx_dvb *dvb = demux->priv;
179         int rc, ret;
180
181         if (!demux->dmx.frontend)
182                 return -EINVAL;
183
184         mutex_lock(&dvb->lock);
185         dvb->nfeeds++;
186         rc = dvb->nfeeds;
187
188         if (dvb->nfeeds == 1) {
189                 ret = start_streaming(dvb);
190                 if (ret < 0)
191                         rc = ret;
192         }
193
194         mutex_unlock(&dvb->lock);
195         return rc;
196 }
197
198 static int stop_feed(struct dvb_demux_feed *feed)
199 {
200         struct dvb_demux *demux  = feed->demux;
201         struct em28xx_dvb *dvb = demux->priv;
202         int err = 0;
203
204         mutex_lock(&dvb->lock);
205         dvb->nfeeds--;
206
207         if (0 == dvb->nfeeds)
208                 err = stop_streaming(dvb);
209
210         mutex_unlock(&dvb->lock);
211         return err;
212 }
213
214
215
216 /* ------------------------------------------------------------------ */
217 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
218 {
219         struct em28xx *dev = fe->dvb->priv;
220
221         if (acquire)
222                 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
223         else
224                 return em28xx_set_mode(dev, EM28XX_SUSPEND);
225 }
226
227 /* ------------------------------------------------------------------ */
228
229 static struct lgdt330x_config em2880_lgdt3303_dev = {
230         .demod_address = 0x0e,
231         .demod_chip = LGDT3303,
232 };
233
234 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
235         .demod_address = (0x1e >> 1),
236         .no_tuner = 1,
237         .parallel_ts = 1,
238         .if2 = 45600,
239 };
240
241 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
242         .demod_address = 0x32 >> 1,
243         .output_mode   = S5H1409_PARALLEL_OUTPUT,
244         .gpio          = S5H1409_GPIO_OFF,
245         .inversion     = S5H1409_INVERSION_OFF,
246         .status_mode   = S5H1409_DEMODLOCKING,
247         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
248 };
249
250 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
251         .demod_address = (0x1e >> 1),
252         .no_tuner = 1,
253         .disable_i2c_gate_ctrl = 1,
254         .parallel_ts = 1,
255         .if2 = 45600,
256 };
257
258 #ifdef EM28XX_DRX397XD_SUPPORT
259 /* [TODO] djh - not sure yet what the device config needs to contain */
260 static struct drx397xD_config em28xx_drx397xD_with_xc3028 = {
261         .demod_address = (0xe0 >> 1),
262 };
263 #endif
264
265 static int mt352_terratec_xs_init(struct dvb_frontend *fe)
266 {
267         /* Values extracted from a USB trace of the Terratec Windows driver */
268         static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
269         static u8 reset[]          = { RESET,      0x80 };
270         static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
271         static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
272         static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
273         static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
274         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
275         static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
276         static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
277         static u8 tuner_go[]       = { TUNER_GO, 0x01};
278
279         mt352_write(fe, clock_config,   sizeof(clock_config));
280         udelay(200);
281         mt352_write(fe, reset,          sizeof(reset));
282         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
283         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
284         mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
285         mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
286         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
287         mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
288         mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
289         mt352_write(fe, tuner_go,       sizeof(tuner_go));
290         return 0;
291 }
292
293 static struct mt352_config terratec_xs_mt352_cfg = {
294         .demod_address = (0x1e >> 1),
295         .no_tuner = 1,
296         .if2 = 45600,
297         .demod_init = mt352_terratec_xs_init,
298 };
299
300 static struct tda10023_config em28xx_tda10023_config = {
301         .demod_address = 0x0c,
302         .invert = 1,
303 };
304
305 /* ------------------------------------------------------------------ */
306
307 static int attach_xc3028(u8 addr, struct em28xx *dev)
308 {
309         struct dvb_frontend *fe;
310         struct xc2028_config cfg;
311
312         memset(&cfg, 0, sizeof(cfg));
313         cfg.i2c_adap  = &dev->i2c_adap;
314         cfg.i2c_addr  = addr;
315
316         if (!dev->dvb->frontend) {
317                 em28xx_errdev("/2: dvb frontend not attached. "
318                                 "Can't attach xc3028\n");
319                 return -EINVAL;
320         }
321
322         fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
323         if (!fe) {
324                 em28xx_errdev("/2: xc3028 attach failed\n");
325                 dvb_frontend_detach(dev->dvb->frontend);
326                 dev->dvb->frontend = NULL;
327                 return -EINVAL;
328         }
329
330         em28xx_info("%s/2: xc3028 attached\n", dev->name);
331
332         return 0;
333 }
334
335 /* ------------------------------------------------------------------ */
336
337 static int register_dvb(struct em28xx_dvb *dvb,
338                  struct module *module,
339                  struct em28xx *dev,
340                  struct device *device)
341 {
342         int result;
343
344         mutex_init(&dvb->lock);
345
346         /* register adapter */
347         result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
348                                       adapter_nr);
349         if (result < 0) {
350                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
351                        dev->name, result);
352                 goto fail_adapter;
353         }
354
355         /* Ensure all frontends negotiate bus access */
356         dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
357
358         dvb->adapter.priv = dev;
359
360         /* register frontend */
361         result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
362         if (result < 0) {
363                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
364                        dev->name, result);
365                 goto fail_frontend;
366         }
367
368         /* register demux stuff */
369         dvb->demux.dmx.capabilities =
370                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
371                 DMX_MEMORY_BASED_FILTERING;
372         dvb->demux.priv       = dvb;
373         dvb->demux.filternum  = 256;
374         dvb->demux.feednum    = 256;
375         dvb->demux.start_feed = start_feed;
376         dvb->demux.stop_feed  = stop_feed;
377
378         result = dvb_dmx_init(&dvb->demux);
379         if (result < 0) {
380                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
381                        dev->name, result);
382                 goto fail_dmx;
383         }
384
385         dvb->dmxdev.filternum    = 256;
386         dvb->dmxdev.demux        = &dvb->demux.dmx;
387         dvb->dmxdev.capabilities = 0;
388         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
389         if (result < 0) {
390                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
391                        dev->name, result);
392                 goto fail_dmxdev;
393         }
394
395         dvb->fe_hw.source = DMX_FRONTEND_0;
396         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
397         if (result < 0) {
398                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
399                        dev->name, result);
400                 goto fail_fe_hw;
401         }
402
403         dvb->fe_mem.source = DMX_MEMORY_FE;
404         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
405         if (result < 0) {
406                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
407                        dev->name, result);
408                 goto fail_fe_mem;
409         }
410
411         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
412         if (result < 0) {
413                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
414                        dev->name, result);
415                 goto fail_fe_conn;
416         }
417
418         /* register network adapter */
419         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
420         return 0;
421
422 fail_fe_conn:
423         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
424 fail_fe_mem:
425         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
426 fail_fe_hw:
427         dvb_dmxdev_release(&dvb->dmxdev);
428 fail_dmxdev:
429         dvb_dmx_release(&dvb->demux);
430 fail_dmx:
431         dvb_unregister_frontend(dvb->frontend);
432 fail_frontend:
433         dvb_frontend_detach(dvb->frontend);
434         dvb_unregister_adapter(&dvb->adapter);
435 fail_adapter:
436         return result;
437 }
438
439 static void unregister_dvb(struct em28xx_dvb *dvb)
440 {
441         dvb_net_release(&dvb->net);
442         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
443         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
444         dvb_dmxdev_release(&dvb->dmxdev);
445         dvb_dmx_release(&dvb->demux);
446         dvb_unregister_frontend(dvb->frontend);
447         dvb_frontend_detach(dvb->frontend);
448         dvb_unregister_adapter(&dvb->adapter);
449 }
450
451
452 static int dvb_init(struct em28xx *dev)
453 {
454         int result = 0;
455         struct em28xx_dvb *dvb;
456
457         if (!dev->board.has_dvb) {
458                 /* This device does not support the extension */
459                 return 0;
460         }
461
462         dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
463
464         if (dvb == NULL) {
465                 em28xx_info("em28xx_dvb: memory allocation failed\n");
466                 return -ENOMEM;
467         }
468         dev->dvb = dvb;
469
470         em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
471         /* init frontend */
472         switch (dev->model) {
473         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
474         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
475         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
476         case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
477                 dvb->frontend = dvb_attach(lgdt330x_attach,
478                                            &em2880_lgdt3303_dev,
479                                            &dev->i2c_adap);
480                 if (attach_xc3028(0x61, dev) < 0) {
481                         result = -EINVAL;
482                         goto out_free;
483                 }
484                 break;
485         case EM2880_BOARD_KWORLD_DVB_310U:
486                 dvb->frontend = dvb_attach(zl10353_attach,
487                                            &em28xx_zl10353_with_xc3028,
488                                            &dev->i2c_adap);
489                 if (attach_xc3028(0x61, dev) < 0) {
490                         result = -EINVAL;
491                         goto out_free;
492                 }
493                 break;
494         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
495         case EM2882_BOARD_TERRATEC_HYBRID_XS:
496         case EM2880_BOARD_EMPIRE_DUAL_TV:
497                 dvb->frontend = dvb_attach(zl10353_attach,
498                                            &em28xx_zl10353_xc3028_no_i2c_gate,
499                                            &dev->i2c_adap);
500                 if (attach_xc3028(0x61, dev) < 0) {
501                         result = -EINVAL;
502                         goto out_free;
503                 }
504                 break;
505         case EM2880_BOARD_TERRATEC_HYBRID_XS:
506         case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
507         case EM2881_BOARD_PINNACLE_HYBRID_PRO:
508         case EM2882_BOARD_DIKOM_DK300:
509                 dvb->frontend = dvb_attach(zl10353_attach,
510                                            &em28xx_zl10353_xc3028_no_i2c_gate,
511                                            &dev->i2c_adap);
512                 if (dvb->frontend == NULL) {
513                         /* This board could have either a zl10353 or a mt352.
514                            If the chip id isn't for zl10353, try mt352 */
515                         dvb->frontend = dvb_attach(mt352_attach,
516                                                    &terratec_xs_mt352_cfg,
517                                                    &dev->i2c_adap);
518                 }
519
520                 if (attach_xc3028(0x61, dev) < 0) {
521                         result = -EINVAL;
522                         goto out_free;
523                 }
524                 break;
525         case EM2883_BOARD_KWORLD_HYBRID_330U:
526         case EM2882_BOARD_EVGA_INDTUBE:
527                 dvb->frontend = dvb_attach(s5h1409_attach,
528                                            &em28xx_s5h1409_with_xc3028,
529                                            &dev->i2c_adap);
530                 if (attach_xc3028(0x61, dev) < 0) {
531                         result = -EINVAL;
532                         goto out_free;
533                 }
534                 break;
535         case EM2882_BOARD_KWORLD_ATSC_315U:
536                 dvb->frontend = dvb_attach(lgdt330x_attach,
537                                            &em2880_lgdt3303_dev,
538                                            &dev->i2c_adap);
539                 if (dvb->frontend != NULL) {
540                         if (!dvb_attach(simple_tuner_attach, dvb->frontend,
541                                 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
542                                 result = -EINVAL;
543                                 goto out_free;
544                         }
545                 }
546                 break;
547         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
548 #ifdef EM28XX_DRX397XD_SUPPORT
549                 /* We don't have the config structure properly populated, so
550                    this is commented out for now */
551                 dvb->frontend = dvb_attach(drx397xD_attach,
552                                            &em28xx_drx397xD_with_xc3028,
553                                            &dev->i2c_adap);
554                 if (attach_xc3028(0x61, dev) < 0) {
555                         result = -EINVAL;
556                         goto out_free;
557                 }
558                 break;
559 #endif
560         case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
561                 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
562                 dvb->frontend = dvb_attach(tda10023_attach,
563                         &em28xx_tda10023_config,
564                         &dev->i2c_adap, 0x48);
565                 if (dvb->frontend) {
566                         if (!dvb_attach(simple_tuner_attach, dvb->frontend,
567                                 &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
568                                 result = -EINVAL;
569                                 goto out_free;
570                         }
571                 }
572                 break;
573         default:
574                 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
575                                 " isn't supported yet\n");
576                 break;
577         }
578         if (NULL == dvb->frontend) {
579                 em28xx_errdev("/2: frontend initialization failed\n");
580                 result = -EINVAL;
581                 goto out_free;
582         }
583         /* define general-purpose callback pointer */
584         dvb->frontend->callback = em28xx_tuner_callback;
585
586         /* register everything */
587         result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
588
589         if (result < 0)
590                 goto out_free;
591
592         em28xx_set_mode(dev, EM28XX_SUSPEND);
593         em28xx_info("Successfully loaded em28xx-dvb\n");
594         return 0;
595
596 out_free:
597         em28xx_set_mode(dev, EM28XX_SUSPEND);
598         kfree(dvb);
599         dev->dvb = NULL;
600         return result;
601 }
602
603 static int dvb_fini(struct em28xx *dev)
604 {
605         if (!dev->board.has_dvb) {
606                 /* This device does not support the extension */
607                 return 0;
608         }
609
610         if (dev->dvb) {
611                 unregister_dvb(dev->dvb);
612                 kfree(dev->dvb);
613                 dev->dvb = NULL;
614         }
615
616         return 0;
617 }
618
619 static struct em28xx_ops dvb_ops = {
620         .id   = EM28XX_DVB,
621         .name = "Em28xx dvb Extension",
622         .init = dvb_init,
623         .fini = dvb_fini,
624 };
625
626 static int __init em28xx_dvb_register(void)
627 {
628         return em28xx_register_extension(&dvb_ops);
629 }
630
631 static void __exit em28xx_dvb_unregister(void)
632 {
633         em28xx_unregister_extension(&dvb_ops);
634 }
635
636 module_init(em28xx_dvb_register);
637 module_exit(em28xx_dvb_unregister);