Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6] / 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/usb.h>
24
25 #include "em28xx.h"
26 #include <media/v4l2-common.h>
27 #include <media/videobuf-vmalloc.h>
28
29 #include "lgdt330x.h"
30 #include "zl10353.h"
31 #include "s5h1409.h"
32
33 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
34 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
35 MODULE_LICENSE("GPL");
36
37 static unsigned int debug;
38 module_param(debug, int, 0644);
39 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
40
41 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42
43 #define dprintk(level, fmt, arg...) do {                        \
44 if (debug >= level)                                             \
45         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
46 } while (0)
47
48 #define EM28XX_DVB_NUM_BUFS 5
49 #define EM28XX_DVB_MAX_PACKETSIZE 564
50 #define EM28XX_DVB_MAX_PACKETS 64
51
52 struct em28xx_dvb {
53         struct dvb_frontend        *frontend;
54
55         /* feed count management */
56         struct mutex               lock;
57         int                        nfeeds;
58
59         /* general boilerplate stuff */
60         struct dvb_adapter         adapter;
61         struct dvb_demux           demux;
62         struct dmxdev              dmxdev;
63         struct dmx_frontend        fe_hw;
64         struct dmx_frontend        fe_mem;
65         struct dvb_net             net;
66 };
67
68
69 static inline void print_err_status(struct em28xx *dev,
70                                      int packet, int status)
71 {
72         char *errmsg = "Unknown";
73
74         switch (status) {
75         case -ENOENT:
76                 errmsg = "unlinked synchronuously";
77                 break;
78         case -ECONNRESET:
79                 errmsg = "unlinked asynchronuously";
80                 break;
81         case -ENOSR:
82                 errmsg = "Buffer error (overrun)";
83                 break;
84         case -EPIPE:
85                 errmsg = "Stalled (device not responding)";
86                 break;
87         case -EOVERFLOW:
88                 errmsg = "Babble (bad cable?)";
89                 break;
90         case -EPROTO:
91                 errmsg = "Bit-stuff error (bad cable?)";
92                 break;
93         case -EILSEQ:
94                 errmsg = "CRC/Timeout (could be anything)";
95                 break;
96         case -ETIME:
97                 errmsg = "Device does not respond";
98                 break;
99         }
100         if (packet < 0) {
101                 dprintk(1, "URB status %d [%s].\n", status, errmsg);
102         } else {
103                 dprintk(1, "URB packet %d, status %d [%s].\n",
104                         packet, status, errmsg);
105         }
106 }
107
108 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
109 {
110         int i;
111
112         if (!dev)
113                 return 0;
114
115         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
116                 return 0;
117
118         if (urb->status < 0) {
119                 print_err_status(dev, -1, urb->status);
120                 if (urb->status == -ENOENT)
121                         return 0;
122         }
123
124         for (i = 0; i < urb->number_of_packets; i++) {
125                 int status = urb->iso_frame_desc[i].status;
126
127                 if (status < 0) {
128                         print_err_status(dev, i, status);
129                         if (urb->iso_frame_desc[i].status != -EPROTO)
130                                 continue;
131                 }
132
133                 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
134                                  urb->iso_frame_desc[i].offset,
135                                  urb->iso_frame_desc[i].actual_length);
136         }
137
138         return 0;
139 }
140
141 static int start_streaming(struct em28xx_dvb *dvb)
142 {
143         int rc;
144         struct em28xx *dev = dvb->adapter.priv;
145
146         usb_set_interface(dev->udev, 0, 1);
147         rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
148         if (rc < 0)
149                 return rc;
150
151         return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
152                                 EM28XX_DVB_NUM_BUFS, EM28XX_DVB_MAX_PACKETSIZE,
153                                 dvb_isoc_copy);
154 }
155
156 static int stop_streaming(struct em28xx_dvb *dvb)
157 {
158         struct em28xx *dev = dvb->adapter.priv;
159
160         em28xx_uninit_isoc(dev);
161
162         em28xx_set_mode(dev, EM28XX_SUSPEND);
163
164         return 0;
165 }
166
167 static int start_feed(struct dvb_demux_feed *feed)
168 {
169         struct dvb_demux *demux  = feed->demux;
170         struct em28xx_dvb *dvb = demux->priv;
171         int rc, ret;
172
173         if (!demux->dmx.frontend)
174                 return -EINVAL;
175
176         mutex_lock(&dvb->lock);
177         dvb->nfeeds++;
178         rc = dvb->nfeeds;
179
180         if (dvb->nfeeds == 1) {
181                 ret = start_streaming(dvb);
182                 if (ret < 0)
183                         rc = ret;
184         }
185
186         mutex_unlock(&dvb->lock);
187         return rc;
188 }
189
190 static int stop_feed(struct dvb_demux_feed *feed)
191 {
192         struct dvb_demux *demux  = feed->demux;
193         struct em28xx_dvb *dvb = demux->priv;
194         int err = 0;
195
196         mutex_lock(&dvb->lock);
197         dvb->nfeeds--;
198
199         if (0 == dvb->nfeeds)
200                 err = stop_streaming(dvb);
201
202         mutex_unlock(&dvb->lock);
203         return err;
204 }
205
206
207
208 /* ------------------------------------------------------------------ */
209 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
210 {
211         struct em28xx *dev = fe->dvb->priv;
212
213         if (acquire)
214                 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
215         else
216                 return em28xx_set_mode(dev, EM28XX_SUSPEND);
217 }
218
219 /* ------------------------------------------------------------------ */
220
221 static struct lgdt330x_config em2880_lgdt3303_dev = {
222         .demod_address = 0x0e,
223         .demod_chip = LGDT3303,
224 };
225
226 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
227         .demod_address = (0x1e >> 1),
228         .no_tuner = 1,
229         .parallel_ts = 1,
230         .if2 = 45600,
231 };
232
233 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
234         .demod_address = 0x32 >> 1,
235         .output_mode   = S5H1409_PARALLEL_OUTPUT,
236         .gpio          = S5H1409_GPIO_OFF,
237         .inversion     = S5H1409_INVERSION_OFF,
238         .status_mode   = S5H1409_DEMODLOCKING,
239         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
240 };
241
242 #ifdef EM28XX_DRX397XD_SUPPORT
243 /* [TODO] djh - not sure yet what the device config needs to contain */
244 static struct drx397xD_config em28xx_drx397xD_with_xc3028 = {
245         .demod_address = (0xe0 >> 1),
246 };
247 #endif
248
249 /* ------------------------------------------------------------------ */
250
251 static int attach_xc3028(u8 addr, struct em28xx *dev)
252 {
253         struct dvb_frontend *fe;
254         struct xc2028_config cfg;
255
256         memset(&cfg, 0, sizeof(cfg));
257         cfg.i2c_adap  = &dev->i2c_adap;
258         cfg.i2c_addr  = addr;
259
260         if (!dev->dvb->frontend) {
261                 printk(KERN_ERR "%s/2: dvb frontend not attached. "
262                                 "Can't attach xc3028\n",
263                        dev->name);
264                 return -EINVAL;
265         }
266
267         fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
268         if (!fe) {
269                 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
270                        dev->name);
271                 dvb_frontend_detach(dev->dvb->frontend);
272                 dev->dvb->frontend = NULL;
273                 return -EINVAL;
274         }
275
276         printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
277
278         return 0;
279 }
280
281 /* ------------------------------------------------------------------ */
282
283 static int register_dvb(struct em28xx_dvb *dvb,
284                  struct module *module,
285                  struct em28xx *dev,
286                  struct device *device)
287 {
288         int result;
289
290         mutex_init(&dvb->lock);
291
292         /* register adapter */
293         result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
294                                       adapter_nr);
295         if (result < 0) {
296                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
297                        dev->name, result);
298                 goto fail_adapter;
299         }
300
301         /* Ensure all frontends negotiate bus access */
302         dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
303
304         dvb->adapter.priv = dev;
305
306         /* register frontend */
307         result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
308         if (result < 0) {
309                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
310                        dev->name, result);
311                 goto fail_frontend;
312         }
313
314         /* register demux stuff */
315         dvb->demux.dmx.capabilities =
316                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
317                 DMX_MEMORY_BASED_FILTERING;
318         dvb->demux.priv       = dvb;
319         dvb->demux.filternum  = 256;
320         dvb->demux.feednum    = 256;
321         dvb->demux.start_feed = start_feed;
322         dvb->demux.stop_feed  = stop_feed;
323
324         result = dvb_dmx_init(&dvb->demux);
325         if (result < 0) {
326                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
327                        dev->name, result);
328                 goto fail_dmx;
329         }
330
331         dvb->dmxdev.filternum    = 256;
332         dvb->dmxdev.demux        = &dvb->demux.dmx;
333         dvb->dmxdev.capabilities = 0;
334         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
335         if (result < 0) {
336                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
337                        dev->name, result);
338                 goto fail_dmxdev;
339         }
340
341         dvb->fe_hw.source = DMX_FRONTEND_0;
342         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
343         if (result < 0) {
344                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
345                        dev->name, result);
346                 goto fail_fe_hw;
347         }
348
349         dvb->fe_mem.source = DMX_MEMORY_FE;
350         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
351         if (result < 0) {
352                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
353                        dev->name, result);
354                 goto fail_fe_mem;
355         }
356
357         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
358         if (result < 0) {
359                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
360                        dev->name, result);
361                 goto fail_fe_conn;
362         }
363
364         /* register network adapter */
365         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
366         return 0;
367
368 fail_fe_conn:
369         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
370 fail_fe_mem:
371         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
372 fail_fe_hw:
373         dvb_dmxdev_release(&dvb->dmxdev);
374 fail_dmxdev:
375         dvb_dmx_release(&dvb->demux);
376 fail_dmx:
377         dvb_unregister_frontend(dvb->frontend);
378 fail_frontend:
379         dvb_frontend_detach(dvb->frontend);
380         dvb_unregister_adapter(&dvb->adapter);
381 fail_adapter:
382         return result;
383 }
384
385 static void unregister_dvb(struct em28xx_dvb *dvb)
386 {
387         dvb_net_release(&dvb->net);
388         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
389         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
390         dvb_dmxdev_release(&dvb->dmxdev);
391         dvb_dmx_release(&dvb->demux);
392         dvb_unregister_frontend(dvb->frontend);
393         dvb_frontend_detach(dvb->frontend);
394         dvb_unregister_adapter(&dvb->adapter);
395 }
396
397
398 static int dvb_init(struct em28xx *dev)
399 {
400         int result = 0;
401         struct em28xx_dvb *dvb;
402
403         if (!dev->board.has_dvb) {
404                 /* This device does not support the extension */
405                 return 0;
406         }
407
408         dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
409
410         if (dvb == NULL) {
411                 printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
412                 return -ENOMEM;
413         }
414         dev->dvb = dvb;
415
416         em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
417         /* init frontend */
418         switch (dev->model) {
419         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
420         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
421         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
422         case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
423                 dvb->frontend = dvb_attach(lgdt330x_attach,
424                                            &em2880_lgdt3303_dev,
425                                            &dev->i2c_adap);
426                 if (attach_xc3028(0x61, dev) < 0) {
427                         result = -EINVAL;
428                         goto out_free;
429                 }
430                 break;
431         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
432         case EM2880_BOARD_TERRATEC_HYBRID_XS:
433         case EM2880_BOARD_KWORLD_DVB_310U:
434                 dvb->frontend = dvb_attach(zl10353_attach,
435                                            &em28xx_zl10353_with_xc3028,
436                                            &dev->i2c_adap);
437                 if (attach_xc3028(0x61, dev) < 0) {
438                         result = -EINVAL;
439                         goto out_free;
440                 }
441                 break;
442         case EM2883_BOARD_KWORLD_HYBRID_330U:
443                 dvb->frontend = dvb_attach(s5h1409_attach,
444                                            &em28xx_s5h1409_with_xc3028,
445                                            &dev->i2c_adap);
446                 if (attach_xc3028(0x61, dev) < 0) {
447                         result = -EINVAL;
448                         goto out_free;
449                 }
450                 break;
451         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
452 #ifdef EM28XX_DRX397XD_SUPPORT
453                 /* We don't have the config structure properly populated, so
454                    this is commented out for now */
455                 dvb->frontend = dvb_attach(drx397xD_attach,
456                                            &em28xx_drx397xD_with_xc3028,
457                                            &dev->i2c_adap);
458                 if (attach_xc3028(0x61, dev) < 0) {
459                         result = -EINVAL;
460                         goto out_free;
461                 }
462                 break;
463 #endif
464         default:
465                 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
466                                 " isn't supported yet\n",
467                        dev->name);
468                 break;
469         }
470         if (NULL == dvb->frontend) {
471                 printk(KERN_ERR
472                        "%s/2: frontend initialization failed\n",
473                        dev->name);
474                 result = -EINVAL;
475                 goto out_free;
476         }
477         /* define general-purpose callback pointer */
478         dvb->frontend->callback = em28xx_tuner_callback;
479
480         /* register everything */
481         result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
482
483         if (result < 0)
484                 goto out_free;
485
486         em28xx_set_mode(dev, EM28XX_SUSPEND);
487         printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
488         return 0;
489
490 out_free:
491         em28xx_set_mode(dev, EM28XX_SUSPEND);
492         kfree(dvb);
493         dev->dvb = NULL;
494         return result;
495 }
496
497 static int dvb_fini(struct em28xx *dev)
498 {
499         if (!dev->board.has_dvb) {
500                 /* This device does not support the extension */
501                 return 0;
502         }
503
504         if (dev->dvb) {
505                 unregister_dvb(dev->dvb);
506                 dev->dvb = NULL;
507         }
508
509         return 0;
510 }
511
512 static struct em28xx_ops dvb_ops = {
513         .id   = EM28XX_DVB,
514         .name = "Em28xx dvb Extension",
515         .init = dvb_init,
516         .fini = dvb_fini,
517 };
518
519 static int __init em28xx_dvb_register(void)
520 {
521         return em28xx_register_extension(&dvb_ops);
522 }
523
524 static void __exit em28xx_dvb_unregister(void)
525 {
526         em28xx_unregister_extension(&dvb_ops);
527 }
528
529 module_init(em28xx_dvb_register);
530 module_exit(em28xx_dvb_unregister);