perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / usb / au0828 / au0828-dvb.c
blobd9093a3c57c5b275ad4e0956c17832f31e023e64
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.
18 #include "au0828.h"
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/device.h>
24 #include <media/v4l2-common.h>
25 #include <media/tuner.h>
27 #include "au8522.h"
28 #include "xc5000.h"
29 #include "mxl5007t.h"
30 #include "tda18271.h"
32 static int preallocate_big_buffers;
33 module_param_named(preallocate_big_buffers, preallocate_big_buffers, int, 0644);
34 MODULE_PARM_DESC(preallocate_big_buffers, "Preallocate the larger transfer buffers at module load time");
36 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
38 #define _AU0828_BULKPIPE 0x83
39 #define _BULKPIPESIZE 0xe522
41 static u8 hauppauge_hvr950q_led_states[] = {
42 0x00, /* off */
43 0x02, /* yellow */
44 0x04, /* green */
47 static struct au8522_led_config hauppauge_hvr950q_led_cfg = {
48 .gpio_output = 0x00e0,
49 .gpio_output_enable = 0x6006,
50 .gpio_output_disable = 0x0660,
52 .gpio_leds = 0x00e2,
53 .led_states = hauppauge_hvr950q_led_states,
54 .num_led_states = sizeof(hauppauge_hvr950q_led_states),
56 .vsb8_strong = 20 /* dB */ * 10,
57 .qam64_strong = 25 /* dB */ * 10,
58 .qam256_strong = 32 /* dB */ * 10,
61 static struct au8522_config hauppauge_hvr950q_config = {
62 .demod_address = 0x8e >> 1,
63 .status_mode = AU8522_DEMODLOCKING,
64 .qam_if = AU8522_IF_6MHZ,
65 .vsb_if = AU8522_IF_6MHZ,
66 .led_cfg = &hauppauge_hvr950q_led_cfg,
69 static struct au8522_config fusionhdtv7usb_config = {
70 .demod_address = 0x8e >> 1,
71 .status_mode = AU8522_DEMODLOCKING,
72 .qam_if = AU8522_IF_6MHZ,
73 .vsb_if = AU8522_IF_6MHZ,
76 static struct au8522_config hauppauge_woodbury_config = {
77 .demod_address = 0x8e >> 1,
78 .status_mode = AU8522_DEMODLOCKING,
79 .qam_if = AU8522_IF_4MHZ,
80 .vsb_if = AU8522_IF_3_25MHZ,
83 static struct xc5000_config hauppauge_xc5000a_config = {
84 .i2c_address = 0x61,
85 .if_khz = 6000,
86 .chip_id = XC5000A,
87 .output_amp = 0x8f,
90 static struct xc5000_config hauppauge_xc5000c_config = {
91 .i2c_address = 0x61,
92 .if_khz = 6000,
93 .chip_id = XC5000C,
94 .output_amp = 0x8f,
97 static struct mxl5007t_config mxl5007t_hvr950q_config = {
98 .xtal_freq_hz = MxL_XTAL_24_MHZ,
99 .if_freq_hz = MxL_IF_6_MHZ,
102 static struct tda18271_config hauppauge_woodbury_tunerconfig = {
103 .gate = TDA18271_GATE_DIGITAL,
106 static void au0828_restart_dvb_streaming(struct work_struct *work);
108 static void au0828_bulk_timeout(struct timer_list *t)
110 struct au0828_dev *dev = from_timer(dev, t, bulk_timeout);
112 dprintk(1, "%s called\n", __func__);
113 dev->bulk_timeout_running = 0;
114 schedule_work(&dev->restart_streaming);
117 /*-------------------------------------------------------------------*/
118 static void urb_completion(struct urb *purb)
120 struct au0828_dev *dev = purb->context;
121 int ptype = usb_pipetype(purb->pipe);
122 unsigned char *ptr;
124 dprintk(2, "%s: %d\n", __func__, purb->actual_length);
126 if (!dev) {
127 dprintk(2, "%s: no dev!\n", __func__);
128 return;
131 if (!dev->urb_streaming) {
132 dprintk(2, "%s: not streaming!\n", __func__);
133 return;
136 if (ptype != PIPE_BULK) {
137 pr_err("%s: Unsupported URB type %d\n",
138 __func__, ptype);
139 return;
142 /* See if the stream is corrupted (to work around a hardware
143 bug where the stream gets misaligned */
144 ptr = purb->transfer_buffer;
145 if (purb->actual_length > 0 && ptr[0] != 0x47) {
146 dprintk(1, "Need to restart streaming %02x len=%d!\n",
147 ptr[0], purb->actual_length);
148 schedule_work(&dev->restart_streaming);
149 return;
150 } else if (dev->bulk_timeout_running == 1) {
151 /* The URB handler has fired, so cancel timer which would
152 * restart endpoint if we hadn't
154 dprintk(1, "%s cancelling bulk timeout\n", __func__);
155 dev->bulk_timeout_running = 0;
156 del_timer(&dev->bulk_timeout);
159 /* Feed the transport payload into the kernel demux */
160 dvb_dmx_swfilter_packets(&dev->dvb.demux,
161 purb->transfer_buffer, purb->actual_length / 188);
163 /* Clean the buffer before we requeue */
164 memset(purb->transfer_buffer, 0, URB_BUFSIZE);
166 /* Requeue URB */
167 usb_submit_urb(purb, GFP_ATOMIC);
170 static int stop_urb_transfer(struct au0828_dev *dev)
172 int i;
174 dprintk(2, "%s()\n", __func__);
176 if (!dev->urb_streaming)
177 return 0;
179 if (dev->bulk_timeout_running == 1) {
180 dev->bulk_timeout_running = 0;
181 del_timer(&dev->bulk_timeout);
184 dev->urb_streaming = false;
185 for (i = 0; i < URB_COUNT; i++) {
186 if (dev->urbs[i]) {
187 usb_kill_urb(dev->urbs[i]);
188 if (!preallocate_big_buffers)
189 kfree(dev->urbs[i]->transfer_buffer);
191 usb_free_urb(dev->urbs[i]);
195 return 0;
198 static int start_urb_transfer(struct au0828_dev *dev)
200 struct urb *purb;
201 int i, ret;
203 dprintk(2, "%s()\n", __func__);
205 if (dev->urb_streaming) {
206 dprintk(2, "%s: bulk xfer already running!\n", __func__);
207 return 0;
210 for (i = 0; i < URB_COUNT; i++) {
212 dev->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
213 if (!dev->urbs[i])
214 return -ENOMEM;
216 purb = dev->urbs[i];
218 if (preallocate_big_buffers)
219 purb->transfer_buffer = dev->dig_transfer_buffer[i];
220 else
221 purb->transfer_buffer = kzalloc(URB_BUFSIZE,
222 GFP_KERNEL);
224 if (!purb->transfer_buffer) {
225 usb_free_urb(purb);
226 dev->urbs[i] = NULL;
227 ret = -ENOMEM;
228 pr_err("%s: failed big buffer allocation, err = %d\n",
229 __func__, ret);
230 return ret;
233 purb->status = -EINPROGRESS;
234 usb_fill_bulk_urb(purb,
235 dev->usbdev,
236 usb_rcvbulkpipe(dev->usbdev,
237 _AU0828_BULKPIPE),
238 purb->transfer_buffer,
239 URB_BUFSIZE,
240 urb_completion,
241 dev);
245 for (i = 0; i < URB_COUNT; i++) {
246 ret = usb_submit_urb(dev->urbs[i], GFP_ATOMIC);
247 if (ret != 0) {
248 stop_urb_transfer(dev);
249 pr_err("%s: failed urb submission, err = %d\n",
250 __func__, ret);
251 return ret;
255 dev->urb_streaming = true;
257 /* If we don't valid data within 1 second, restart stream */
258 mod_timer(&dev->bulk_timeout, jiffies + (HZ));
259 dev->bulk_timeout_running = 1;
261 return 0;
264 static void au0828_start_transport(struct au0828_dev *dev)
266 au0828_write(dev, 0x608, 0x90);
267 au0828_write(dev, 0x609, 0x72);
268 au0828_write(dev, 0x60a, 0x71);
269 au0828_write(dev, 0x60b, 0x01);
273 static void au0828_stop_transport(struct au0828_dev *dev, int full_stop)
275 if (full_stop) {
276 au0828_write(dev, 0x608, 0x00);
277 au0828_write(dev, 0x609, 0x00);
278 au0828_write(dev, 0x60a, 0x00);
280 au0828_write(dev, 0x60b, 0x00);
283 static int au0828_dvb_start_feed(struct dvb_demux_feed *feed)
285 struct dvb_demux *demux = feed->demux;
286 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
287 struct au0828_dvb *dvb = &dev->dvb;
288 int ret = 0;
290 dprintk(1, "%s()\n", __func__);
292 if (!demux->dmx.frontend)
293 return -EINVAL;
295 if (dvb->frontend) {
296 mutex_lock(&dvb->lock);
297 dvb->start_count++;
298 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__,
299 dvb->start_count, dvb->stop_count);
300 if (dvb->feeding++ == 0) {
301 /* Start transport */
302 au0828_start_transport(dev);
303 ret = start_urb_transfer(dev);
304 if (ret < 0) {
305 au0828_stop_transport(dev, 0);
306 dvb->feeding--; /* We ran out of memory... */
309 mutex_unlock(&dvb->lock);
312 return ret;
315 static int au0828_dvb_stop_feed(struct dvb_demux_feed *feed)
317 struct dvb_demux *demux = feed->demux;
318 struct au0828_dev *dev = (struct au0828_dev *) demux->priv;
319 struct au0828_dvb *dvb = &dev->dvb;
320 int ret = 0;
322 dprintk(1, "%s()\n", __func__);
324 if (dvb->frontend) {
325 cancel_work_sync(&dev->restart_streaming);
327 mutex_lock(&dvb->lock);
328 dvb->stop_count++;
329 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__,
330 dvb->start_count, dvb->stop_count);
331 if (dvb->feeding > 0) {
332 dvb->feeding--;
333 if (dvb->feeding == 0) {
334 /* Stop transport */
335 ret = stop_urb_transfer(dev);
336 au0828_stop_transport(dev, 0);
339 mutex_unlock(&dvb->lock);
342 return ret;
345 static void au0828_restart_dvb_streaming(struct work_struct *work)
347 struct au0828_dev *dev = container_of(work, struct au0828_dev,
348 restart_streaming);
349 struct au0828_dvb *dvb = &dev->dvb;
351 if (!dev->urb_streaming)
352 return;
354 dprintk(1, "Restarting streaming...!\n");
356 mutex_lock(&dvb->lock);
358 /* Stop transport */
359 stop_urb_transfer(dev);
360 au0828_stop_transport(dev, 1);
362 /* Start transport */
363 au0828_start_transport(dev);
364 start_urb_transfer(dev);
366 mutex_unlock(&dvb->lock);
369 static int au0828_set_frontend(struct dvb_frontend *fe)
371 struct au0828_dev *dev = fe->dvb->priv;
372 struct au0828_dvb *dvb = &dev->dvb;
373 int ret, was_streaming;
375 mutex_lock(&dvb->lock);
376 was_streaming = dev->urb_streaming;
377 if (was_streaming) {
378 au0828_stop_transport(dev, 1);
381 * We can't hold a mutex here, as the restart_streaming
382 * kthread may also hold it.
384 mutex_unlock(&dvb->lock);
385 cancel_work_sync(&dev->restart_streaming);
386 mutex_lock(&dvb->lock);
388 stop_urb_transfer(dev);
390 mutex_unlock(&dvb->lock);
392 ret = dvb->set_frontend(fe);
394 if (was_streaming) {
395 mutex_lock(&dvb->lock);
396 au0828_start_transport(dev);
397 start_urb_transfer(dev);
398 mutex_unlock(&dvb->lock);
401 return ret;
404 static int dvb_register(struct au0828_dev *dev)
406 struct au0828_dvb *dvb = &dev->dvb;
407 int result;
409 dprintk(1, "%s()\n", __func__);
411 if (preallocate_big_buffers) {
412 int i;
413 for (i = 0; i < URB_COUNT; i++) {
414 dev->dig_transfer_buffer[i] = kzalloc(URB_BUFSIZE,
415 GFP_KERNEL);
417 if (!dev->dig_transfer_buffer[i]) {
418 result = -ENOMEM;
420 pr_err("failed buffer allocation (errno = %d)\n",
421 result);
422 goto fail_adapter;
427 INIT_WORK(&dev->restart_streaming, au0828_restart_dvb_streaming);
429 /* register adapter */
430 result = dvb_register_adapter(&dvb->adapter,
431 KBUILD_MODNAME, THIS_MODULE,
432 &dev->usbdev->dev, adapter_nr);
433 if (result < 0) {
434 pr_err("dvb_register_adapter failed (errno = %d)\n",
435 result);
436 goto fail_adapter;
439 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
440 dvb->adapter.mdev = dev->media_dev;
441 #endif
443 dvb->adapter.priv = dev;
445 /* register frontend */
446 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
447 if (result < 0) {
448 pr_err("dvb_register_frontend failed (errno = %d)\n",
449 result);
450 goto fail_frontend;
453 /* Hook dvb frontend */
454 dvb->set_frontend = dvb->frontend->ops.set_frontend;
455 dvb->frontend->ops.set_frontend = au0828_set_frontend;
457 /* register demux stuff */
458 dvb->demux.dmx.capabilities =
459 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
460 DMX_MEMORY_BASED_FILTERING;
461 dvb->demux.priv = dev;
462 dvb->demux.filternum = 256;
463 dvb->demux.feednum = 256;
464 dvb->demux.start_feed = au0828_dvb_start_feed;
465 dvb->demux.stop_feed = au0828_dvb_stop_feed;
466 result = dvb_dmx_init(&dvb->demux);
467 if (result < 0) {
468 pr_err("dvb_dmx_init failed (errno = %d)\n", result);
469 goto fail_dmx;
472 dvb->dmxdev.filternum = 256;
473 dvb->dmxdev.demux = &dvb->demux.dmx;
474 dvb->dmxdev.capabilities = 0;
475 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
476 if (result < 0) {
477 pr_err("dvb_dmxdev_init failed (errno = %d)\n", result);
478 goto fail_dmxdev;
481 dvb->fe_hw.source = DMX_FRONTEND_0;
482 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
483 if (result < 0) {
484 pr_err("add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
485 result);
486 goto fail_fe_hw;
489 dvb->fe_mem.source = DMX_MEMORY_FE;
490 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
491 if (result < 0) {
492 pr_err("add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
493 result);
494 goto fail_fe_mem;
497 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
498 if (result < 0) {
499 pr_err("connect_frontend failed (errno = %d)\n", result);
500 goto fail_fe_conn;
503 /* register network adapter */
504 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
506 dvb->start_count = 0;
507 dvb->stop_count = 0;
509 result = dvb_create_media_graph(&dvb->adapter, false);
510 if (result < 0)
511 goto fail_create_graph;
513 return 0;
515 fail_create_graph:
516 dvb_net_release(&dvb->net);
517 fail_fe_conn:
518 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
519 fail_fe_mem:
520 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
521 fail_fe_hw:
522 dvb_dmxdev_release(&dvb->dmxdev);
523 fail_dmxdev:
524 dvb_dmx_release(&dvb->demux);
525 fail_dmx:
526 dvb_unregister_frontend(dvb->frontend);
527 fail_frontend:
528 dvb_frontend_detach(dvb->frontend);
529 dvb_unregister_adapter(&dvb->adapter);
530 fail_adapter:
532 if (preallocate_big_buffers) {
533 int i;
534 for (i = 0; i < URB_COUNT; i++)
535 kfree(dev->dig_transfer_buffer[i]);
538 return result;
541 void au0828_dvb_unregister(struct au0828_dev *dev)
543 struct au0828_dvb *dvb = &dev->dvb;
545 dprintk(1, "%s()\n", __func__);
547 if (dvb->frontend == NULL)
548 return;
550 cancel_work_sync(&dev->restart_streaming);
552 dvb_net_release(&dvb->net);
553 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
554 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
555 dvb_dmxdev_release(&dvb->dmxdev);
556 dvb_dmx_release(&dvb->demux);
557 dvb_unregister_frontend(dvb->frontend);
558 dvb_frontend_detach(dvb->frontend);
559 dvb_unregister_adapter(&dvb->adapter);
561 if (preallocate_big_buffers) {
562 int i;
563 for (i = 0; i < URB_COUNT; i++)
564 kfree(dev->dig_transfer_buffer[i]);
566 dvb->frontend = NULL;
569 /* All the DVB attach calls go here, this function get's modified
570 * for each new card. No other function in this file needs
571 * to change.
573 int au0828_dvb_register(struct au0828_dev *dev)
575 struct au0828_dvb *dvb = &dev->dvb;
576 int ret;
578 dprintk(1, "%s()\n", __func__);
580 /* init frontend */
581 switch (dev->boardnr) {
582 case AU0828_BOARD_HAUPPAUGE_HVR850:
583 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
584 dvb->frontend = dvb_attach(au8522_attach,
585 &hauppauge_hvr950q_config,
586 &dev->i2c_adap);
587 if (dvb->frontend != NULL)
588 switch (dev->board.tuner_type) {
589 default:
590 case TUNER_XC5000:
591 dvb_attach(xc5000_attach, dvb->frontend,
592 &dev->i2c_adap,
593 &hauppauge_xc5000a_config);
594 break;
595 case TUNER_XC5000C:
596 dvb_attach(xc5000_attach, dvb->frontend,
597 &dev->i2c_adap,
598 &hauppauge_xc5000c_config);
599 break;
601 break;
602 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL:
603 dvb->frontend = dvb_attach(au8522_attach,
604 &hauppauge_hvr950q_config,
605 &dev->i2c_adap);
606 if (dvb->frontend != NULL)
607 dvb_attach(mxl5007t_attach, dvb->frontend,
608 &dev->i2c_adap, 0x60,
609 &mxl5007t_hvr950q_config);
610 break;
611 case AU0828_BOARD_HAUPPAUGE_WOODBURY:
612 dvb->frontend = dvb_attach(au8522_attach,
613 &hauppauge_woodbury_config,
614 &dev->i2c_adap);
615 if (dvb->frontend != NULL)
616 dvb_attach(tda18271_attach, dvb->frontend,
617 0x60, &dev->i2c_adap,
618 &hauppauge_woodbury_tunerconfig);
619 break;
620 case AU0828_BOARD_DVICO_FUSIONHDTV7:
621 dvb->frontend = dvb_attach(au8522_attach,
622 &fusionhdtv7usb_config,
623 &dev->i2c_adap);
624 if (dvb->frontend != NULL) {
625 dvb_attach(xc5000_attach, dvb->frontend,
626 &dev->i2c_adap,
627 &hauppauge_xc5000a_config);
629 break;
630 default:
631 pr_warn("The frontend of your DVB/ATSC card isn't supported yet\n");
632 break;
634 if (NULL == dvb->frontend) {
635 pr_err("%s() Frontend initialization failed\n",
636 __func__);
637 return -1;
639 /* define general-purpose callback pointer */
640 dvb->frontend->callback = au0828_tuner_callback;
642 /* register everything */
643 ret = dvb_register(dev);
644 if (ret < 0) {
645 if (dvb->frontend->ops.release)
646 dvb->frontend->ops.release(dvb->frontend);
647 dvb->frontend = NULL;
648 return ret;
651 timer_setup(&dev->bulk_timeout, au0828_bulk_timeout, 0);
653 return 0;
656 void au0828_dvb_suspend(struct au0828_dev *dev)
658 struct au0828_dvb *dvb = &dev->dvb;
659 int rc;
661 if (dvb->frontend) {
662 if (dev->urb_streaming) {
663 cancel_work_sync(&dev->restart_streaming);
664 /* Stop transport */
665 mutex_lock(&dvb->lock);
666 stop_urb_transfer(dev);
667 au0828_stop_transport(dev, 1);
668 mutex_unlock(&dvb->lock);
669 dev->need_urb_start = true;
671 /* suspend frontend - does tuner and fe to sleep */
672 rc = dvb_frontend_suspend(dvb->frontend);
673 pr_info("au0828_dvb_suspend(): Suspending DVB fe %d\n", rc);
677 void au0828_dvb_resume(struct au0828_dev *dev)
679 struct au0828_dvb *dvb = &dev->dvb;
680 int rc;
682 if (dvb->frontend) {
683 /* resume frontend - does fe and tuner init */
684 rc = dvb_frontend_resume(dvb->frontend);
685 pr_info("au0828_dvb_resume(): Resuming DVB fe %d\n", rc);
686 if (dev->need_urb_start) {
687 /* Start transport */
688 mutex_lock(&dvb->lock);
689 au0828_start_transport(dev);
690 start_urb_transfer(dev);
691 mutex_unlock(&dvb->lock);