PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / media / video / em28xx / em28xx-dvb.c
blob8cf4983f0039602ce617fbe929a1edad032baa26
1 /*
2 DVB device driver for em28xx
4 (c) 2008 Mauro Carvalho Chehab <mchehab@infradead.org>
6 (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7 - Fixes for the driver to properly work with HVR-950
9 (c) 2008 Aidan Thornton <makosoft@googlemail.com>
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]
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.
20 #include <linux/kernel.h>
21 #include <linux/usb.h>
23 #include "em28xx.h"
24 #include <media/v4l2-common.h>
25 #include <media/videobuf-vmalloc.h>
27 #include "lgdt330x.h"
28 #include "zl10353.h"
30 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
31 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
32 MODULE_LICENSE("GPL");
34 static unsigned int debug;
35 module_param(debug, int, 0644);
36 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
38 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
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)
45 #define EM28XX_DVB_NUM_BUFS 5
46 #define EM28XX_DVB_MAX_PACKETSIZE 564
47 #define EM28XX_DVB_MAX_PACKETS 64
49 struct em28xx_dvb {
50 struct dvb_frontend *frontend;
52 /* feed count management */
53 struct mutex lock;
54 int nfeeds;
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;
66 static inline void print_err_status(struct em28xx *dev,
67 int packet, int status)
69 char *errmsg = "Unknown";
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;
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);
105 static inline int dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
107 int i;
109 if (!dev)
110 return 0;
112 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
113 return 0;
115 if (urb->status < 0) {
116 print_err_status(dev, -1, urb->status);
117 if (urb->status == -ENOENT)
118 return 0;
121 for (i = 0; i < urb->number_of_packets; i++) {
122 int status = urb->iso_frame_desc[i].status;
124 if (status < 0) {
125 print_err_status(dev, i, status);
126 if (urb->iso_frame_desc[i].status != -EPROTO)
127 continue;
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);
135 return 0;
138 static int start_streaming(struct em28xx_dvb *dvb)
140 int rc;
141 struct em28xx *dev = dvb->adapter.priv;
143 usb_set_interface(dev->udev, 0, 1);
144 rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
145 if (rc < 0)
146 return rc;
148 return em28xx_init_isoc(dev, EM28XX_DVB_MAX_PACKETS,
149 EM28XX_DVB_NUM_BUFS, EM28XX_DVB_MAX_PACKETSIZE,
150 dvb_isoc_copy);
153 static int stop_streaming(struct em28xx_dvb *dvb)
155 struct em28xx *dev = dvb->adapter.priv;
157 em28xx_uninit_isoc(dev);
159 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
161 return 0;
164 static int start_feed(struct dvb_demux_feed *feed)
166 struct dvb_demux *demux = feed->demux;
167 struct em28xx_dvb *dvb = demux->priv;
168 int rc, ret;
170 if (!demux->dmx.frontend)
171 return -EINVAL;
173 mutex_lock(&dvb->lock);
174 dvb->nfeeds++;
175 rc = dvb->nfeeds;
177 if (dvb->nfeeds == 1) {
178 ret = start_streaming(dvb);
179 if (ret < 0)
180 rc = ret;
183 mutex_unlock(&dvb->lock);
184 return rc;
187 static int stop_feed(struct dvb_demux_feed *feed)
189 struct dvb_demux *demux = feed->demux;
190 struct em28xx_dvb *dvb = demux->priv;
191 int err = 0;
193 mutex_lock(&dvb->lock);
194 dvb->nfeeds--;
196 if (0 == dvb->nfeeds)
197 err = stop_streaming(dvb);
199 mutex_unlock(&dvb->lock);
200 return err;
205 /* ------------------------------------------------------------------ */
206 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
208 struct em28xx *dev = fe->dvb->priv;
210 if (acquire)
211 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
212 else
213 return em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
216 /* ------------------------------------------------------------------ */
218 static struct lgdt330x_config em2880_lgdt3303_dev = {
219 .demod_address = 0x0e,
220 .demod_chip = LGDT3303,
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,
230 /* ------------------------------------------------------------------ */
232 static int attach_xc3028(u8 addr, struct em28xx *dev)
234 struct dvb_frontend *fe;
235 struct xc2028_config cfg;
237 memset(&cfg, 0, sizeof(cfg));
238 cfg.i2c_adap = &dev->i2c_adap;
239 cfg.i2c_addr = addr;
240 cfg.callback = em28xx_tuner_callback;
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;
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;
258 printk(KERN_INFO "%s/2: xc3028 attached\n", dev->name);
260 return 0;
263 /* ------------------------------------------------------------------ */
265 int register_dvb(struct em28xx_dvb *dvb,
266 struct module *module,
267 struct em28xx *dev,
268 struct device *device)
270 int result;
272 mutex_init(&dvb->lock);
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;
283 /* Ensure all frontends negotiate bus access */
284 dvb->frontend->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
286 dvb->adapter.priv = dev;
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;
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;
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;
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;
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;
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;
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;
346 /* register network adapter */
347 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
348 return 0;
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;
367 static void unregister_dvb(struct em28xx_dvb *dvb)
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);
380 static int dvb_init(struct em28xx *dev)
382 int result = 0;
383 struct em28xx_dvb *dvb;
385 dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
387 if (dvb == NULL) {
388 printk(KERN_INFO "em28xx_dvb: memory allocation failed\n");
389 return -ENOMEM;
391 dev->dvb = dvb;
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;
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;
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;
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;
428 /* register everything */
429 result = register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
431 if (result < 0)
432 goto out_free;
434 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
435 printk(KERN_INFO "Successfully loaded em28xx-dvb\n");
436 return 0;
438 out_free:
439 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
440 kfree(dvb);
441 dev->dvb = NULL;
442 return result;
445 static int dvb_fini(struct em28xx *dev)
447 if (dev->dvb) {
448 unregister_dvb(dev->dvb);
449 dev->dvb = NULL;
452 return 0;
455 static struct em28xx_ops dvb_ops = {
456 .id = EM28XX_DVB,
457 .name = "Em28xx dvb Extension",
458 .init = dvb_init,
459 .fini = dvb_fini,
462 static int __init em28xx_dvb_register(void)
464 return em28xx_register_extension(&dvb_ops);
467 static void __exit em28xx_dvb_unregister(void)
469 em28xx_unregister_extension(&dvb_ops);
472 module_init(em28xx_dvb_register);
473 module_exit(em28xx_dvb_unregister);