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