]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/em28xx/em28xx-dvb.c
Merge branches 'at91', 'dyntick', 'ep93xx', 'iop', 'ixp', 'misc', 'orion', 'omap...
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / em28xx / em28xx-dvb.c
CommitLineData
3aefb79a
MCC
1/*
2 DVB device driver for em28xx
3
4 (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org>
5
bdfbf952
DH
6 (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7 - Fixes for the driver to properly work with HVR-950
8
3421b778
AT
9 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
10
11 Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
3aefb79a
MCC
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"
7e6388a1 28#include "zl10353.h"
3aefb79a
MCC
29
30MODULE_DESCRIPTION("driver for em28xx based DVB cards");
31MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
32MODULE_LICENSE("GPL");
33
34static unsigned int debug;
35module_param(debug, int, 0644);
36MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
37
38DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
39
40#define dprintk(level, fmt, arg...) do { \
41if (debug >= level) \
3421b778 42 printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
3aefb79a
MCC
43} while (0)
44
3421b778
AT
45#define EM28XX_DVB_NUM_BUFS 5
46#define EM28XX_DVB_MAX_PACKETSIZE 564
47#define EM28XX_DVB_MAX_PACKETS 64
48
49struct 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
66static inline void print_err_status(struct em28xx *dev,
67 int packet, int status)
3aefb79a 68{
3421b778 69 char *errmsg = "Unknown";
3aefb79a 70
3421b778
AT
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 {
6ea54d93
DSL
100 dprintk(1, "URB packet %d, status %d [%s].\n",
101 packet, status, errmsg);
3421b778
AT
102 }
103}
3aefb79a 104
3421b778
AT
105static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
106{
107 int i;
3aefb79a 108
3421b778
AT
109 if (!dev)
110 return 0;
3aefb79a 111
3421b778
AT
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
6ea54d93
DSL
138static int start_streaming(struct em28xx_dvb *dvb)
139{
c67ec53f 140 int rc;
3421b778
AT
141 struct em28xx *dev = dvb->adapter.priv;
142
143 usb_set_interface(dev->udev, 0, 1);
c67ec53f
MCC
144 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
145 if (rc < 0)
146 return rc;
3421b778
AT
147
148 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
149 EM28XX_DVB_NUM_BUFS, EM28XX_DVB_MAX_PACKETSIZE,
c67ec53f 150 dvb_isoc_copy);
3421b778
AT
151}
152
6ea54d93
DSL
153static int stop_streaming(struct em28xx_dvb *dvb)
154{
3421b778
AT
155 struct em28xx *dev = dvb->adapter.priv;
156
157 em28xx_uninit_isoc(dev);
c67ec53f
MCC
158
159 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
160
3aefb79a
MCC
161 return 0;
162}
163
3421b778
AT
164static 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);
6ea54d93
DSL
179 if (ret < 0)
180 rc = ret;
3421b778
AT
181 }
182
183 mutex_unlock(&dvb->lock);
184 return rc;
185}
186
187static 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--;
6ea54d93
DSL
195
196 if (0 == dvb->nfeeds)
3421b778 197 err = stop_streaming(dvb);
6ea54d93 198
3421b778
AT
199 mutex_unlock(&dvb->lock);
200 return err;
201}
202
203
e3569abc
MCC
204
205/* ------------------------------------------------------------------ */
206static 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
3aefb79a
MCC
216/* ------------------------------------------------------------------ */
217
227ad4ab
MCC
218static struct lgdt330x_config em2880_lgdt3303_dev = {
219 .demod_address = 0x0e,
220 .demod_chip = LGDT3303,
221};
3aefb79a 222
7e6388a1
AT
223static 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
3aefb79a
MCC
230/* ------------------------------------------------------------------ */
231
232static int attach_xc3028(u8 addr, struct em28xx *dev)
233{
234 struct dvb_frontend *fe;
3ca9c093
MCC
235 struct xc2028_config cfg;
236
6ea54d93 237 memset(&cfg, 0, sizeof(cfg));
3ca9c093
MCC
238 cfg.i2c_adap = &dev->i2c_adap;
239 cfg.i2c_addr = addr;
3ca9c093
MCC
240 cfg.callback = em28xx_tuner_callback;
241
3421b778 242 if (!dev->dvb->frontend) {
3aefb79a
MCC
243 printk(KERN_ERR "%s/2: dvb frontend not attached. "
244 "Can't attach xc3028\n",
245 dev->name);
246 return -EINVAL;
247 }
248
3421b778 249 fe = dvb_attach(xc2028_attach, dev->dvb->frontend, &cfg);
3aefb79a
MCC
250 if (!fe) {
251 printk(KERN_ERR "%s/2: xc3028 attach failed\n",
252 dev->name);
3421b778 253 dvb_frontend_detach(dev->dvb->frontend);
3421b778 254 dev->dvb->frontend = NULL;
3aefb79a
MCC
255 return -EINVAL;
256 }
257
258 printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
259
260 return 0;
261}
262
3421b778
AT
263/* ------------------------------------------------------------------ */
264
265int register_dvb(struct em28xx_dvb *dvb,
266 struct module *module,
267 struct em28xx *dev,
268 struct device *device)
3aefb79a 269{
3421b778 270 int result;
3aefb79a 271
3421b778 272 mutex_init(&dvb->lock);
3aefb79a 273
3421b778
AT
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 }
e3569abc
MCC
282
283 /* Ensure all frontends negotiate bus access */
284 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
285
3421b778
AT
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;
e3569abc 305
3421b778
AT
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 }
52284c3e 322
3421b778
AT
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
350fail_fe_conn:
351 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
352fail_fe_mem:
353 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
354fail_fe_hw:
355 dvb_dmxdev_release(&dvb->dmxdev);
356fail_dmxdev:
357 dvb_dmx_release(&dvb->demux);
358fail_dmx:
359 dvb_unregister_frontend(dvb->frontend);
360fail_frontend:
361 dvb_frontend_detach(dvb->frontend);
362 dvb_unregister_adapter(&dvb->adapter);
363fail_adapter:
364 return result;
365}
366
367static 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
380static int dvb_init(struct em28xx *dev)
381{
382 int result = 0;
383 struct em28xx_dvb *dvb;
384
df619181
DH
385 if (!dev->has_dvb) {
386 /* This device does not support the extension */
387 return 0;
388 }
389
3421b778 390 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
6ea54d93
DSL
391
392 if (dvb == NULL) {
393 printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
3421b778
AT
394 return -ENOMEM;
395 }
396 dev->dvb = dvb;
3aefb79a 397
c67ec53f 398 em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
3aefb79a
MCC
399 /* init frontend */
400 switch (dev->model) {
227ad4ab 401 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_950:
3421b778
AT
402 dvb->frontend = dvb_attach(lgdt330x_attach,
403 &em2880_lgdt3303_dev,
404 &dev->i2c_adap);
405 if (attach_xc3028(0x61, dev) < 0) {
406 result = -EINVAL;
407 goto out_free;
408 }
227ad4ab 409 break;
7e6388a1 410 case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
3421b778
AT
411 dvb->frontend = dvb_attach(zl10353_attach,
412 &em28xx_zl10353_with_xc3028,
413 &dev->i2c_adap);
414 if (attach_xc3028(0x61, dev) < 0) {
415 result = -EINVAL;
416 goto out_free;
417 }
7e6388a1 418 break;
3aefb79a
MCC
419 default:
420 printk(KERN_ERR "%s/2: The frontend of your DVB/ATSC card"
421 " isn't supported yet\n",
422 dev->name);
423 break;
424 }
3421b778 425 if (NULL == dvb->frontend) {
3aefb79a
MCC
426 printk(KERN_ERR
427 "%s/2: frontend initialization failed\n",
428 dev->name);
3421b778
AT
429 result = -EINVAL;
430 goto out_free;
3aefb79a
MCC
431 }
432
433 /* register everything */
3421b778
AT
434 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
435
6ea54d93 436 if (result < 0)
3421b778 437 goto out_free;
3421b778 438
c67ec53f 439 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
102a0b08 440 printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
3421b778
AT
441 return 0;
442
443out_free:
c67ec53f 444 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
3421b778
AT
445 kfree(dvb);
446 dev->dvb = NULL;
447 return result;
3aefb79a
MCC
448}
449
450static int dvb_fini(struct em28xx *dev)
451{
df619181
DH
452 if (!dev->has_dvb) {
453 /* This device does not support the extension */
454 return 0;
455 }
456
3421b778
AT
457 if (dev->dvb) {
458 unregister_dvb(dev->dvb);
459 dev->dvb = NULL;
460 }
3aefb79a
MCC
461
462 return 0;
463}
464
465static struct em28xx_ops dvb_ops = {
466 .id = EM28XX_DVB,
467 .name = "Em28xx dvb Extension",
468 .init = dvb_init,
469 .fini = dvb_fini,
470};
471
472static int __init em28xx_dvb_register(void)
473{
474 return em28xx_register_extension(&dvb_ops);
475}
476
477static void __exit em28xx_dvb_unregister(void)
478{
479 em28xx_unregister_extension(&dvb_ops);
480}
481
482module_init(em28xx_dvb_register);
483module_exit(em28xx_dvb_unregister);