net: qmi_wwan: add Olivetti Olicard 500
[linux/fpc-iii.git] / drivers / media / usb / au0828 / au0828-dvb.c
blob4ae8b10746496e00d4e0396c896583b6bf0cf084
1 /*
2 * Driver for the Auvitek USB bridge
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/device.h>
26 #include <linux/suspend.h>
27 #include <media/v4l2-common.h>
28 #include <media/tuner.h>
30 #include "au0828.h"
31 #include "au8522.h"
32 #include "xc5000.h"
33 #include "mxl5007t.h"
34 #include "tda18271.h"
36 static int preallocate_big_buffers;
37 module_param_named(preallocate_big_buffers, preallocate_big_buffers, int, 0644);
38 MODULE_PARM_DESC(preallocate_big_buffers, "Preallocate the larger transfer buffers at module load time");
40 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
42 #define _AU0828_BULKPIPE 0x83
43 #define _BULKPIPESIZE 0xe522
45 static u8 hauppauge_hvr950q_led_states[] = {
46 0x00, /* off */
47 0x02, /* yellow */
48 0x04, /* green */
51 static struct au8522_led_config hauppauge_hvr950q_led_cfg = {
52 .gpio_output = 0x00e0,
53 .gpio_output_enable = 0x6006,
54 .gpio_output_disable = 0x0660,
56 .gpio_leds = 0x00e2,
57 .led_states = hauppauge_hvr950q_led_states,
58 .num_led_states = sizeof(hauppauge_hvr950q_led_states),
60 .vsb8_strong = 20 /* dB */ * 10,
61 .qam64_strong = 25 /* dB */ * 10,
62 .qam256_strong = 32 /* dB */ * 10,
65 static struct au8522_config hauppauge_hvr950q_config = {
66 .demod_address = 0x8e >> 1,
67 .status_mode = AU8522_DEMODLOCKING,
68 .qam_if = AU8522_IF_6MHZ,
69 .vsb_if = AU8522_IF_6MHZ,
70 .led_cfg = &hauppauge_hvr950q_led_cfg,
73 static struct au8522_config fusionhdtv7usb_config = {
74 .demod_address = 0x8e >> 1,
75 .status_mode = AU8522_DEMODLOCKING,
76 .qam_if = AU8522_IF_6MHZ,
77 .vsb_if = AU8522_IF_6MHZ,
80 static struct au8522_config hauppauge_woodbury_config = {
81 .demod_address = 0x8e >> 1,
82 .status_mode = AU8522_DEMODLOCKING,
83 .qam_if = AU8522_IF_4MHZ,
84 .vsb_if = AU8522_IF_3_25MHZ,
87 static struct xc5000_config hauppauge_xc5000a_config = {
88 .i2c_address = 0x61,
89 .if_khz = 6000,
90 .chip_id = XC5000A,
93 static struct xc5000_config hauppauge_xc5000c_config = {
94 .i2c_address = 0x61,
95 .if_khz = 6000,
96 .chip_id = XC5000C,
99 static struct mxl5007t_config mxl5007t_hvr950q_config = {
100 .xtal_freq_hz = MxL_XTAL_24_MHZ,
101 .if_freq_hz = MxL_IF_6_MHZ,
104 static struct tda18271_config hauppauge_woodbury_tunerconfig = {
105 .gate = TDA18271_GATE_DIGITAL,
108 static void au0828_restart_dvb_streaming(struct work_struct *work);
110 /*-------------------------------------------------------------------*/
111 static void urb_completion(struct urb *purb)
113 struct au0828_dev *dev = purb->context;
114 int ptype = usb_pipetype(purb->pipe);
115 unsigned char *ptr;
117 dprintk(2, "%s()\n", __func__);
119 if (!dev)
120 return;
122 if (dev->urb_streaming == 0)
123 return;
125 if (ptype != PIPE_BULK) {
126 printk(KERN_ERR "%s() Unsupported URB type %d\n",
127 __func__, ptype);
128 return;
131 /* See if the stream is corrupted (to work around a hardware
132 bug where the stream gets misaligned */
133 ptr = purb->transfer_buffer;
134 if (purb->actual_length > 0 && ptr[0] != 0x47) {
135 dprintk(1, "Need to restart streaming %02x len=%d!\n",
136 ptr[0], purb->actual_length);
137 schedule_work(&dev->restart_streaming);
138 return;
141 /* Feed the transport payload into the kernel demux */
142 dvb_dmx_swfilter_packets(&dev->dvb.demux,
143 purb->transfer_buffer, purb->actual_length / 188);
145 /* Clean the buffer before we requeue */
146 memset(purb->transfer_buffer, 0, URB_BUFSIZE);
148 /* Requeue URB */
149 usb_submit_urb(purb, GFP_ATOMIC);
152 static int stop_urb_transfer(struct au0828_dev *dev)
154 int i;
156 dprintk(2, "%s()\n", __func__);
158 dev->urb_streaming = 0;
159 for (i = 0; i < URB_COUNT; i++) {
160 if (dev->urbs[i]) {
161 usb_kill_urb(dev->urbs[i]);
162 if (!preallocate_big_buffers)
163 kfree(dev->urbs[i]->transfer_buffer);
165 usb_free_urb(dev->urbs[i]);
169 return 0;
172 static int start_urb_transfer(struct au0828_dev *dev)
174 struct urb *purb;
175 int i, ret = -ENOMEM;
177 dprintk(2, "%s()\n", __func__);
179 if (dev->urb_streaming) {
180 dprintk(2, "%s: bulk xfer already running!\n", __func__);
181 return 0;
184 for (i = 0; i < URB_COUNT; i++) {
186 dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
187 if (!dev->urbs[i])
188 goto err;
190 purb = dev->urbs[i];
192 if (preallocate_big_buffers)
193 purb->transfer_buffer = dev->dig_transfer_buffer[i];
194 else
195 purb->transfer_buffer = kzalloc(URB_BUFSIZE,
196 GFP_KERNEL);
198 if (!purb->transfer_buffer) {
199 usb_free_urb(purb);
200 dev->urbs[i] = NULL;
201 printk(KERN_ERR
202 "%s: failed big buffer allocation, err = %d\n",
203 __func__, ret);
204 goto err;
207 purb->status = -EINPROGRESS;
208 usb_fill_bulk_urb(purb,
209 dev->usbdev,
210 usb_rcvbulkpipe(dev->usbdev,
211 _AU0828_BULKPIPE),
212 purb->transfer_buffer,
213 URB_BUFSIZE,
214 urb_completion,
215 dev);
219 for (i = 0; i < URB_COUNT; i++) {
220 ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
221 if (ret != 0) {
222 stop_urb_transfer(dev);
223 printk(KERN_ERR "%s: failed urb submission, "
224 "err = %d\n", __func__, ret);
225 return ret;
229 dev->urb_streaming = 1;
230 ret = 0;
232 err:
233 return ret;
236 static void au0828_start_transport(struct au0828_dev *dev)
238 au0828_write(dev, 0x608, 0x90);
239 au0828_write(dev, 0x609, 0x72);
240 au0828_write(dev, 0x60a, 0x71);
241 au0828_write(dev, 0x60b, 0x01);
245 static void au0828_stop_transport(struct au0828_dev *dev, int full_stop)
247 if (full_stop) {
248 au0828_write(dev, 0x608, 0x00);
249 au0828_write(dev, 0x609, 0x00);
250 au0828_write(dev, 0x60a, 0x00);
252 au0828_write(dev, 0x60b, 0x00);
257 static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
259 struct dvb_demux *demux = feed->demux;
260 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
261 struct au0828_dvb *dvb = &dev->dvb;
262 int ret = 0;
264 dprintk(1, "%s()\n", __func__);
266 if (!demux->dmx.frontend)
267 return -EINVAL;
269 if (dvb) {
270 mutex_lock(&dvb->lock);
271 dvb->start_count++;
272 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__,
273 dvb->start_count, dvb->stop_count);
274 if (dvb->feeding++ == 0) {
275 /* Start transport */
276 au0828_start_transport(dev);
277 ret = start_urb_transfer(dev);
278 if (ret < 0) {
279 au0828_stop_transport(dev, 0);
280 dvb->feeding--; /* We ran out of memory... */
283 mutex_unlock(&dvb->lock);
286 return ret;
289 static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
291 struct dvb_demux *demux = feed->demux;
292 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
293 struct au0828_dvb *dvb = &dev->dvb;
294 int ret = 0;
296 dprintk(1, "%s()\n", __func__);
298 if (dvb) {
299 mutex_lock(&dvb->lock);
300 dvb->stop_count++;
301 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__,
302 dvb->start_count, dvb->stop_count);
303 if (dvb->feeding > 0) {
304 dvb->feeding--;
305 if (dvb->feeding == 0) {
306 /* Stop transport */
307 ret = stop_urb_transfer(dev);
308 au0828_stop_transport(dev, 0);
311 mutex_unlock(&dvb->lock);
314 return ret;
317 static void au0828_restart_dvb_streaming(struct work_struct *work)
319 struct au0828_dev *dev = container_of(work, struct au0828_dev,
320 restart_streaming);
321 struct au0828_dvb *dvb = &dev->dvb;
323 if (dev->urb_streaming == 0)
324 return;
326 dprintk(1, "Restarting streaming...!\n");
328 mutex_lock(&dvb->lock);
330 /* Stop transport */
331 stop_urb_transfer(dev);
332 au0828_stop_transport(dev, 1);
334 /* Start transport */
335 au0828_start_transport(dev);
336 start_urb_transfer(dev);
338 mutex_unlock(&dvb->lock);
341 static int dvb_register(struct au0828_dev *dev)
343 struct au0828_dvb *dvb = &dev->dvb;
344 int result;
346 dprintk(1, "%s()\n", __func__);
348 if (preallocate_big_buffers) {
349 int i;
350 for (i = 0; i < URB_COUNT; i++) {
351 dev->dig_transfer_buffer[i] = kzalloc(URB_BUFSIZE,
352 GFP_KERNEL);
354 if (!dev->dig_transfer_buffer[i]) {
355 result = -ENOMEM;
357 printk(KERN_ERR
358 "%s: failed buffer allocation (errno = %d)\n",
359 DRIVER_NAME, result);
360 goto fail_adapter;
365 INIT_WORK(&dev->restart_streaming, au0828_restart_dvb_streaming);
367 /* register adapter */
368 result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
369 &dev->usbdev->dev, adapter_nr);
370 if (result < 0) {
371 printk(KERN_ERR "%s: dvb_register_adapter failed "
372 "(errno = %d)\n", DRIVER_NAME, result);
373 goto fail_adapter;
375 dvb->adapter.priv = dev;
377 /* register frontend */
378 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
379 if (result < 0) {
380 printk(KERN_ERR "%s: dvb_register_frontend failed "
381 "(errno = %d)\n", DRIVER_NAME, result);
382 goto fail_frontend;
385 /* register demux stuff */
386 dvb->demux.dmx.capabilities =
387 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
388 DMX_MEMORY_BASED_FILTERING;
389 dvb->demux.priv = dev;
390 dvb->demux.filternum = 256;
391 dvb->demux.feednum = 256;
392 dvb->demux.start_feed = au0828_dvb_start_feed;
393 dvb->demux.stop_feed = au0828_dvb_stop_feed;
394 result = dvb_dmx_init(&dvb->demux);
395 if (result < 0) {
396 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
397 DRIVER_NAME, result);
398 goto fail_dmx;
401 dvb->dmxdev.filternum = 256;
402 dvb->dmxdev.demux = &dvb->demux.dmx;
403 dvb->dmxdev.capabilities = 0;
404 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
405 if (result < 0) {
406 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
407 DRIVER_NAME, result);
408 goto fail_dmxdev;
411 dvb->fe_hw.source = DMX_FRONTEND_0;
412 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
413 if (result < 0) {
414 printk(KERN_ERR "%s: add_frontend failed "
415 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
416 goto fail_fe_hw;
419 dvb->fe_mem.source = DMX_MEMORY_FE;
420 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
421 if (result < 0) {
422 printk(KERN_ERR "%s: add_frontend failed "
423 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
424 goto fail_fe_mem;
427 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
428 if (result < 0) {
429 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
430 DRIVER_NAME, result);
431 goto fail_fe_conn;
434 /* register network adapter */
435 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
437 dvb->start_count = 0;
438 dvb->stop_count = 0;
439 return 0;
441 fail_fe_conn:
442 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
443 fail_fe_mem:
444 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
445 fail_fe_hw:
446 dvb_dmxdev_release(&dvb->dmxdev);
447 fail_dmxdev:
448 dvb_dmx_release(&dvb->demux);
449 fail_dmx:
450 dvb_unregister_frontend(dvb->frontend);
451 fail_frontend:
452 dvb_frontend_detach(dvb->frontend);
453 dvb_unregister_adapter(&dvb->adapter);
454 fail_adapter:
456 if (preallocate_big_buffers) {
457 int i;
458 for (i = 0; i < URB_COUNT; i++)
459 kfree(dev->dig_transfer_buffer[i]);
462 return result;
465 void au0828_dvb_unregister(struct au0828_dev *dev)
467 struct au0828_dvb *dvb = &dev->dvb;
469 dprintk(1, "%s()\n", __func__);
471 if (dvb->frontend == NULL)
472 return;
474 dvb_net_release(&dvb->net);
475 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
476 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
477 dvb_dmxdev_release(&dvb->dmxdev);
478 dvb_dmx_release(&dvb->demux);
479 dvb_unregister_frontend(dvb->frontend);
480 dvb_frontend_detach(dvb->frontend);
481 dvb_unregister_adapter(&dvb->adapter);
483 if (preallocate_big_buffers) {
484 int i;
485 for (i = 0; i < URB_COUNT; i++)
486 kfree(dev->dig_transfer_buffer[i]);
492 /* All the DVB attach calls go here, this function get's modified
493 * for each new card. No other function in this file needs
494 * to change.
496 int au0828_dvb_register(struct au0828_dev *dev)
498 struct au0828_dvb *dvb = &dev->dvb;
499 int ret;
501 dprintk(1, "%s()\n", __func__);
503 /* init frontend */
504 switch (dev->boardnr) {
505 case AU0828_BOARD_HAUPPAUGE_HVR850:
506 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
507 dvb->frontend = dvb_attach(au8522_attach,
508 &hauppauge_hvr950q_config,
509 &dev->i2c_adap);
510 if (dvb->frontend != NULL)
511 switch (dev->board.tuner_type) {
512 default:
513 case TUNER_XC5000:
514 dvb_attach(xc5000_attach, dvb->frontend,
515 &dev->i2c_adap,
516 &hauppauge_xc5000a_config);
517 break;
518 case TUNER_XC5000C:
519 dvb_attach(xc5000_attach, dvb->frontend,
520 &dev->i2c_adap,
521 &hauppauge_xc5000c_config);
522 break;
524 break;
525 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
526 dvb->frontend = dvb_attach(au8522_attach,
527 &hauppauge_hvr950q_config,
528 &dev->i2c_adap);
529 if (dvb->frontend != NULL)
530 dvb_attach(mxl5007t_attach, dvb->frontend,
531 &dev->i2c_adap, 0x60,
532 &mxl5007t_hvr950q_config);
533 break;
534 case AU0828_BOARD_HAUPPAUGE_WOODBURY:
535 dvb->frontend = dvb_attach(au8522_attach,
536 &hauppauge_woodbury_config,
537 &dev->i2c_adap);
538 if (dvb->frontend != NULL)
539 dvb_attach(tda18271_attach, dvb->frontend,
540 0x60, &dev->i2c_adap,
541 &hauppauge_woodbury_tunerconfig);
542 break;
543 case AU0828_BOARD_DVICO_FUSIONHDTV7:
544 dvb->frontend = dvb_attach(au8522_attach,
545 &fusionhdtv7usb_config,
546 &dev->i2c_adap);
547 if (dvb->frontend != NULL) {
548 dvb_attach(xc5000_attach, dvb->frontend,
549 &dev->i2c_adap,
550 &hauppauge_xc5000a_config);
552 break;
553 default:
554 printk(KERN_WARNING "The frontend of your DVB/ATSC card "
555 "isn't supported yet\n");
556 break;
558 if (NULL == dvb->frontend) {
559 printk(KERN_ERR "%s() Frontend initialization failed\n",
560 __func__);
561 return -1;
563 /* define general-purpose callback pointer */
564 dvb->frontend->callback = au0828_tuner_callback;
566 /* register everything */
567 ret = dvb_register(dev);
568 if (ret < 0) {
569 if (dvb->frontend->ops.release)
570 dvb->frontend->ops.release(dvb->frontend);
571 return ret;
574 return 0;