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