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