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.
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>
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
[] = {
47 static struct au8522_led_config hauppauge_hvr950q_led_cfg
= {
48 .gpio_output
= 0x00e0,
49 .gpio_output_enable
= 0x6006,
50 .gpio_output_disable
= 0x0660,
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
= {
90 static struct xc5000_config hauppauge_xc5000c_config
= {
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
);
124 dprintk(2, "%s: %d\n", __func__
, purb
->actual_length
);
127 dprintk(2, "%s: no dev!\n", __func__
);
131 if (!dev
->urb_streaming
) {
132 dprintk(2, "%s: not streaming!\n", __func__
);
136 if (ptype
!= PIPE_BULK
) {
137 pr_err("%s: Unsupported URB type %d\n",
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
);
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
);
167 usb_submit_urb(purb
, GFP_ATOMIC
);
170 static int stop_urb_transfer(struct au0828_dev
*dev
)
174 dprintk(2, "%s()\n", __func__
);
176 if (!dev
->urb_streaming
)
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
++) {
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
]);
198 static int start_urb_transfer(struct au0828_dev
*dev
)
203 dprintk(2, "%s()\n", __func__
);
205 if (dev
->urb_streaming
) {
206 dprintk(2, "%s: bulk xfer already running!\n", __func__
);
210 for (i
= 0; i
< URB_COUNT
; i
++) {
212 dev
->urbs
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
218 if (preallocate_big_buffers
)
219 purb
->transfer_buffer
= dev
->dig_transfer_buffer
[i
];
221 purb
->transfer_buffer
= kzalloc(URB_BUFSIZE
,
224 if (!purb
->transfer_buffer
) {
228 pr_err("%s: failed big buffer allocation, err = %d\n",
233 purb
->status
= -EINPROGRESS
;
234 usb_fill_bulk_urb(purb
,
236 usb_rcvbulkpipe(dev
->usbdev
,
238 purb
->transfer_buffer
,
245 for (i
= 0; i
< URB_COUNT
; i
++) {
246 ret
= usb_submit_urb(dev
->urbs
[i
], GFP_ATOMIC
);
248 stop_urb_transfer(dev
);
249 pr_err("%s: failed urb submission, err = %d\n",
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;
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
)
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
;
290 dprintk(1, "%s()\n", __func__
);
292 if (!demux
->dmx
.frontend
)
296 mutex_lock(&dvb
->lock
);
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
);
305 au0828_stop_transport(dev
, 0);
306 dvb
->feeding
--; /* We ran out of memory... */
309 mutex_unlock(&dvb
->lock
);
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
;
322 dprintk(1, "%s()\n", __func__
);
325 cancel_work_sync(&dev
->restart_streaming
);
327 mutex_lock(&dvb
->lock
);
329 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__
,
330 dvb
->start_count
, dvb
->stop_count
);
331 if (dvb
->feeding
> 0) {
333 if (dvb
->feeding
== 0) {
335 ret
= stop_urb_transfer(dev
);
336 au0828_stop_transport(dev
, 0);
339 mutex_unlock(&dvb
->lock
);
345 static void au0828_restart_dvb_streaming(struct work_struct
*work
)
347 struct au0828_dev
*dev
= container_of(work
, struct au0828_dev
,
349 struct au0828_dvb
*dvb
= &dev
->dvb
;
351 if (!dev
->urb_streaming
)
354 dprintk(1, "Restarting streaming...!\n");
356 mutex_lock(&dvb
->lock
);
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
;
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
);
395 mutex_lock(&dvb
->lock
);
396 au0828_start_transport(dev
);
397 start_urb_transfer(dev
);
398 mutex_unlock(&dvb
->lock
);
404 static int dvb_register(struct au0828_dev
*dev
)
406 struct au0828_dvb
*dvb
= &dev
->dvb
;
409 dprintk(1, "%s()\n", __func__
);
411 if (preallocate_big_buffers
) {
413 for (i
= 0; i
< URB_COUNT
; i
++) {
414 dev
->dig_transfer_buffer
[i
] = kzalloc(URB_BUFSIZE
,
417 if (!dev
->dig_transfer_buffer
[i
]) {
420 pr_err("failed buffer allocation (errno = %d)\n",
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
);
434 pr_err("dvb_register_adapter failed (errno = %d)\n",
439 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
440 dvb
->adapter
.mdev
= dev
->media_dev
;
443 dvb
->adapter
.priv
= dev
;
445 /* register frontend */
446 result
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
448 pr_err("dvb_register_frontend failed (errno = %d)\n",
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
);
468 pr_err("dvb_dmx_init failed (errno = %d)\n", result
);
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
);
477 pr_err("dvb_dmxdev_init failed (errno = %d)\n", result
);
481 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
482 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
484 pr_err("add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
489 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
490 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
492 pr_err("add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
497 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
499 pr_err("connect_frontend failed (errno = %d)\n", result
);
503 /* register network adapter */
504 dvb_net_init(&dvb
->adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
506 dvb
->start_count
= 0;
509 result
= dvb_create_media_graph(&dvb
->adapter
, false);
511 goto fail_create_graph
;
516 dvb_net_release(&dvb
->net
);
518 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
520 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
522 dvb_dmxdev_release(&dvb
->dmxdev
);
524 dvb_dmx_release(&dvb
->demux
);
526 dvb_unregister_frontend(dvb
->frontend
);
528 dvb_frontend_detach(dvb
->frontend
);
529 dvb_unregister_adapter(&dvb
->adapter
);
532 if (preallocate_big_buffers
) {
534 for (i
= 0; i
< URB_COUNT
; i
++)
535 kfree(dev
->dig_transfer_buffer
[i
]);
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
)
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
) {
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
573 int au0828_dvb_register(struct au0828_dev
*dev
)
575 struct au0828_dvb
*dvb
= &dev
->dvb
;
578 dprintk(1, "%s()\n", __func__
);
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
,
587 if (dvb
->frontend
!= NULL
)
588 switch (dev
->board
.tuner_type
) {
591 dvb_attach(xc5000_attach
, dvb
->frontend
,
593 &hauppauge_xc5000a_config
);
596 dvb_attach(xc5000_attach
, dvb
->frontend
,
598 &hauppauge_xc5000c_config
);
602 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL
:
603 dvb
->frontend
= dvb_attach(au8522_attach
,
604 &hauppauge_hvr950q_config
,
606 if (dvb
->frontend
!= NULL
)
607 dvb_attach(mxl5007t_attach
, dvb
->frontend
,
608 &dev
->i2c_adap
, 0x60,
609 &mxl5007t_hvr950q_config
);
611 case AU0828_BOARD_HAUPPAUGE_WOODBURY
:
612 dvb
->frontend
= dvb_attach(au8522_attach
,
613 &hauppauge_woodbury_config
,
615 if (dvb
->frontend
!= NULL
)
616 dvb_attach(tda18271_attach
, dvb
->frontend
,
617 0x60, &dev
->i2c_adap
,
618 &hauppauge_woodbury_tunerconfig
);
620 case AU0828_BOARD_DVICO_FUSIONHDTV7
:
621 dvb
->frontend
= dvb_attach(au8522_attach
,
622 &fusionhdtv7usb_config
,
624 if (dvb
->frontend
!= NULL
) {
625 dvb_attach(xc5000_attach
, dvb
->frontend
,
627 &hauppauge_xc5000a_config
);
631 pr_warn("The frontend of your DVB/ATSC card isn't supported yet\n");
634 if (NULL
== dvb
->frontend
) {
635 pr_err("%s() Frontend initialization failed\n",
639 /* define general-purpose callback pointer */
640 dvb
->frontend
->callback
= au0828_tuner_callback
;
642 /* register everything */
643 ret
= dvb_register(dev
);
645 if (dvb
->frontend
->ops
.release
)
646 dvb
->frontend
->ops
.release(dvb
->frontend
);
647 dvb
->frontend
= NULL
;
651 timer_setup(&dev
->bulk_timeout
, au0828_bulk_timeout
, 0);
656 void au0828_dvb_suspend(struct au0828_dev
*dev
)
658 struct au0828_dvb
*dvb
= &dev
->dvb
;
662 if (dev
->urb_streaming
) {
663 cancel_work_sync(&dev
->restart_streaming
);
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
;
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
);