1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Auvitek USB bridge
5 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <media/v4l2-common.h>
15 #include <media/tuner.h>
22 static int preallocate_big_buffers
;
23 module_param_named(preallocate_big_buffers
, preallocate_big_buffers
, int, 0644);
24 MODULE_PARM_DESC(preallocate_big_buffers
, "Preallocate the larger transfer buffers at module load time");
26 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
28 #define _AU0828_BULKPIPE 0x83
29 #define _BULKPIPESIZE 0xe522
31 static u8 hauppauge_hvr950q_led_states
[] = {
37 static struct au8522_led_config hauppauge_hvr950q_led_cfg
= {
38 .gpio_output
= 0x00e0,
39 .gpio_output_enable
= 0x6006,
40 .gpio_output_disable
= 0x0660,
43 .led_states
= hauppauge_hvr950q_led_states
,
44 .num_led_states
= sizeof(hauppauge_hvr950q_led_states
),
46 .vsb8_strong
= 20 /* dB */ * 10,
47 .qam64_strong
= 25 /* dB */ * 10,
48 .qam256_strong
= 32 /* dB */ * 10,
51 static struct au8522_config hauppauge_hvr950q_config
= {
52 .demod_address
= 0x8e >> 1,
53 .status_mode
= AU8522_DEMODLOCKING
,
54 .qam_if
= AU8522_IF_6MHZ
,
55 .vsb_if
= AU8522_IF_6MHZ
,
56 .led_cfg
= &hauppauge_hvr950q_led_cfg
,
59 static struct au8522_config fusionhdtv7usb_config
= {
60 .demod_address
= 0x8e >> 1,
61 .status_mode
= AU8522_DEMODLOCKING
,
62 .qam_if
= AU8522_IF_6MHZ
,
63 .vsb_if
= AU8522_IF_6MHZ
,
66 static struct au8522_config hauppauge_woodbury_config
= {
67 .demod_address
= 0x8e >> 1,
68 .status_mode
= AU8522_DEMODLOCKING
,
69 .qam_if
= AU8522_IF_4MHZ
,
70 .vsb_if
= AU8522_IF_3_25MHZ
,
73 static struct xc5000_config hauppauge_xc5000a_config
= {
80 static struct xc5000_config hauppauge_xc5000c_config
= {
87 static struct mxl5007t_config mxl5007t_hvr950q_config
= {
88 .xtal_freq_hz
= MxL_XTAL_24_MHZ
,
89 .if_freq_hz
= MxL_IF_6_MHZ
,
92 static struct tda18271_config hauppauge_woodbury_tunerconfig
= {
93 .gate
= TDA18271_GATE_DIGITAL
,
96 static void au0828_restart_dvb_streaming(struct work_struct
*work
);
98 static void au0828_bulk_timeout(struct timer_list
*t
)
100 struct au0828_dev
*dev
= from_timer(dev
, t
, bulk_timeout
);
102 dprintk(1, "%s called\n", __func__
);
103 dev
->bulk_timeout_running
= 0;
104 schedule_work(&dev
->restart_streaming
);
107 /*-------------------------------------------------------------------*/
108 static void urb_completion(struct urb
*purb
)
110 struct au0828_dev
*dev
= purb
->context
;
111 int ptype
= usb_pipetype(purb
->pipe
);
114 dprintk(2, "%s: %d\n", __func__
, purb
->actual_length
);
117 dprintk(2, "%s: no dev!\n", __func__
);
121 if (!dev
->urb_streaming
) {
122 dprintk(2, "%s: not streaming!\n", __func__
);
126 if (ptype
!= PIPE_BULK
) {
127 pr_err("%s: Unsupported URB type %d\n",
132 /* See if the stream is corrupted (to work around a hardware
133 bug where the stream gets misaligned */
134 ptr
= purb
->transfer_buffer
;
135 if (purb
->actual_length
> 0 && ptr
[0] != 0x47) {
136 dprintk(1, "Need to restart streaming %02x len=%d!\n",
137 ptr
[0], purb
->actual_length
);
138 schedule_work(&dev
->restart_streaming
);
140 } else if (dev
->bulk_timeout_running
== 1) {
141 /* The URB handler has fired, so cancel timer which would
142 * restart endpoint if we hadn't
144 dprintk(1, "%s cancelling bulk timeout\n", __func__
);
145 dev
->bulk_timeout_running
= 0;
146 del_timer(&dev
->bulk_timeout
);
149 /* Feed the transport payload into the kernel demux */
150 dvb_dmx_swfilter_packets(&dev
->dvb
.demux
,
151 purb
->transfer_buffer
, purb
->actual_length
/ 188);
153 /* Clean the buffer before we requeue */
154 memset(purb
->transfer_buffer
, 0, URB_BUFSIZE
);
157 usb_submit_urb(purb
, GFP_ATOMIC
);
160 static int stop_urb_transfer(struct au0828_dev
*dev
)
164 dprintk(2, "%s()\n", __func__
);
166 if (!dev
->urb_streaming
)
169 if (dev
->bulk_timeout_running
== 1) {
170 dev
->bulk_timeout_running
= 0;
171 del_timer(&dev
->bulk_timeout
);
174 dev
->urb_streaming
= false;
175 for (i
= 0; i
< URB_COUNT
; i
++) {
177 usb_kill_urb(dev
->urbs
[i
]);
178 if (!preallocate_big_buffers
)
179 kfree(dev
->urbs
[i
]->transfer_buffer
);
181 usb_free_urb(dev
->urbs
[i
]);
188 static int start_urb_transfer(struct au0828_dev
*dev
)
193 dprintk(2, "%s()\n", __func__
);
195 if (dev
->urb_streaming
) {
196 dprintk(2, "%s: bulk xfer already running!\n", __func__
);
200 for (i
= 0; i
< URB_COUNT
; i
++) {
202 dev
->urbs
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
208 if (preallocate_big_buffers
)
209 purb
->transfer_buffer
= dev
->dig_transfer_buffer
[i
];
211 purb
->transfer_buffer
= kzalloc(URB_BUFSIZE
,
214 if (!purb
->transfer_buffer
) {
218 pr_err("%s: failed big buffer allocation, err = %d\n",
223 purb
->status
= -EINPROGRESS
;
224 usb_fill_bulk_urb(purb
,
226 usb_rcvbulkpipe(dev
->usbdev
,
228 purb
->transfer_buffer
,
235 for (i
= 0; i
< URB_COUNT
; i
++) {
236 ret
= usb_submit_urb(dev
->urbs
[i
], GFP_ATOMIC
);
238 stop_urb_transfer(dev
);
239 pr_err("%s: failed urb submission, err = %d\n",
245 dev
->urb_streaming
= true;
247 /* If we don't valid data within 1 second, restart stream */
248 mod_timer(&dev
->bulk_timeout
, jiffies
+ (HZ
));
249 dev
->bulk_timeout_running
= 1;
254 static void au0828_start_transport(struct au0828_dev
*dev
)
256 au0828_write(dev
, 0x608, 0x90);
257 au0828_write(dev
, 0x609, 0x72);
258 au0828_write(dev
, 0x60a, 0x71);
259 au0828_write(dev
, 0x60b, 0x01);
263 static void au0828_stop_transport(struct au0828_dev
*dev
, int full_stop
)
266 au0828_write(dev
, 0x608, 0x00);
267 au0828_write(dev
, 0x609, 0x00);
268 au0828_write(dev
, 0x60a, 0x00);
270 au0828_write(dev
, 0x60b, 0x00);
273 static int au0828_dvb_start_feed(struct dvb_demux_feed
*feed
)
275 struct dvb_demux
*demux
= feed
->demux
;
276 struct au0828_dev
*dev
= (struct au0828_dev
*) demux
->priv
;
277 struct au0828_dvb
*dvb
= &dev
->dvb
;
280 dprintk(1, "%s()\n", __func__
);
282 if (!demux
->dmx
.frontend
)
286 mutex_lock(&dvb
->lock
);
288 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__
,
289 dvb
->start_count
, dvb
->stop_count
);
290 if (dvb
->feeding
++ == 0) {
291 /* Start transport */
292 au0828_start_transport(dev
);
293 ret
= start_urb_transfer(dev
);
295 au0828_stop_transport(dev
, 0);
296 dvb
->feeding
--; /* We ran out of memory... */
299 mutex_unlock(&dvb
->lock
);
305 static int au0828_dvb_stop_feed(struct dvb_demux_feed
*feed
)
307 struct dvb_demux
*demux
= feed
->demux
;
308 struct au0828_dev
*dev
= (struct au0828_dev
*) demux
->priv
;
309 struct au0828_dvb
*dvb
= &dev
->dvb
;
312 dprintk(1, "%s()\n", __func__
);
315 cancel_work_sync(&dev
->restart_streaming
);
317 mutex_lock(&dvb
->lock
);
319 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__
,
320 dvb
->start_count
, dvb
->stop_count
);
321 if (dvb
->feeding
> 0) {
323 if (dvb
->feeding
== 0) {
325 ret
= stop_urb_transfer(dev
);
326 au0828_stop_transport(dev
, 0);
329 mutex_unlock(&dvb
->lock
);
335 static void au0828_restart_dvb_streaming(struct work_struct
*work
)
337 struct au0828_dev
*dev
= container_of(work
, struct au0828_dev
,
339 struct au0828_dvb
*dvb
= &dev
->dvb
;
341 if (!dev
->urb_streaming
)
344 dprintk(1, "Restarting streaming...!\n");
346 mutex_lock(&dvb
->lock
);
349 stop_urb_transfer(dev
);
350 au0828_stop_transport(dev
, 1);
352 /* Start transport */
353 au0828_start_transport(dev
);
354 start_urb_transfer(dev
);
356 mutex_unlock(&dvb
->lock
);
359 static int au0828_set_frontend(struct dvb_frontend
*fe
)
361 struct au0828_dev
*dev
= fe
->dvb
->priv
;
362 struct au0828_dvb
*dvb
= &dev
->dvb
;
363 int ret
, was_streaming
;
365 mutex_lock(&dvb
->lock
);
366 was_streaming
= dev
->urb_streaming
;
368 au0828_stop_transport(dev
, 1);
371 * We can't hold a mutex here, as the restart_streaming
372 * kthread may also hold it.
374 mutex_unlock(&dvb
->lock
);
375 cancel_work_sync(&dev
->restart_streaming
);
376 mutex_lock(&dvb
->lock
);
378 stop_urb_transfer(dev
);
380 mutex_unlock(&dvb
->lock
);
382 ret
= dvb
->set_frontend(fe
);
385 mutex_lock(&dvb
->lock
);
386 au0828_start_transport(dev
);
387 start_urb_transfer(dev
);
388 mutex_unlock(&dvb
->lock
);
394 static int dvb_register(struct au0828_dev
*dev
)
396 struct au0828_dvb
*dvb
= &dev
->dvb
;
399 dprintk(1, "%s()\n", __func__
);
401 if (preallocate_big_buffers
) {
403 for (i
= 0; i
< URB_COUNT
; i
++) {
404 dev
->dig_transfer_buffer
[i
] = kzalloc(URB_BUFSIZE
,
407 if (!dev
->dig_transfer_buffer
[i
]) {
410 pr_err("failed buffer allocation (errno = %d)\n",
417 INIT_WORK(&dev
->restart_streaming
, au0828_restart_dvb_streaming
);
419 /* register adapter */
420 result
= dvb_register_adapter(&dvb
->adapter
,
421 KBUILD_MODNAME
, THIS_MODULE
,
422 &dev
->usbdev
->dev
, adapter_nr
);
424 pr_err("dvb_register_adapter failed (errno = %d)\n",
429 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
430 dvb
->adapter
.mdev
= dev
->media_dev
;
433 dvb
->adapter
.priv
= dev
;
435 /* register frontend */
436 result
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
438 pr_err("dvb_register_frontend failed (errno = %d)\n",
443 /* Hook dvb frontend */
444 dvb
->set_frontend
= dvb
->frontend
->ops
.set_frontend
;
445 dvb
->frontend
->ops
.set_frontend
= au0828_set_frontend
;
447 /* register demux stuff */
448 dvb
->demux
.dmx
.capabilities
=
449 DMX_TS_FILTERING
| DMX_SECTION_FILTERING
|
450 DMX_MEMORY_BASED_FILTERING
;
451 dvb
->demux
.priv
= dev
;
452 dvb
->demux
.filternum
= 256;
453 dvb
->demux
.feednum
= 256;
454 dvb
->demux
.start_feed
= au0828_dvb_start_feed
;
455 dvb
->demux
.stop_feed
= au0828_dvb_stop_feed
;
456 result
= dvb_dmx_init(&dvb
->demux
);
458 pr_err("dvb_dmx_init failed (errno = %d)\n", result
);
462 dvb
->dmxdev
.filternum
= 256;
463 dvb
->dmxdev
.demux
= &dvb
->demux
.dmx
;
464 dvb
->dmxdev
.capabilities
= 0;
465 result
= dvb_dmxdev_init(&dvb
->dmxdev
, &dvb
->adapter
);
467 pr_err("dvb_dmxdev_init failed (errno = %d)\n", result
);
471 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
472 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
474 pr_err("add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
479 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
480 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
482 pr_err("add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
487 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
489 pr_err("connect_frontend failed (errno = %d)\n", result
);
493 /* register network adapter */
494 dvb_net_init(&dvb
->adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
496 dvb
->start_count
= 0;
499 result
= dvb_create_media_graph(&dvb
->adapter
, false);
501 goto fail_create_graph
;
506 dvb_net_release(&dvb
->net
);
508 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
510 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
512 dvb_dmxdev_release(&dvb
->dmxdev
);
514 dvb_dmx_release(&dvb
->demux
);
516 dvb_unregister_frontend(dvb
->frontend
);
518 dvb_frontend_detach(dvb
->frontend
);
519 dvb_unregister_adapter(&dvb
->adapter
);
522 if (preallocate_big_buffers
) {
524 for (i
= 0; i
< URB_COUNT
; i
++)
525 kfree(dev
->dig_transfer_buffer
[i
]);
531 void au0828_dvb_unregister(struct au0828_dev
*dev
)
533 struct au0828_dvb
*dvb
= &dev
->dvb
;
535 dprintk(1, "%s()\n", __func__
);
537 if (dvb
->frontend
== NULL
)
540 cancel_work_sync(&dev
->restart_streaming
);
542 dvb_net_release(&dvb
->net
);
543 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
544 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
545 dvb_dmxdev_release(&dvb
->dmxdev
);
546 dvb_dmx_release(&dvb
->demux
);
547 dvb_unregister_frontend(dvb
->frontend
);
548 dvb_frontend_detach(dvb
->frontend
);
549 dvb_unregister_adapter(&dvb
->adapter
);
551 if (preallocate_big_buffers
) {
553 for (i
= 0; i
< URB_COUNT
; i
++)
554 kfree(dev
->dig_transfer_buffer
[i
]);
556 dvb
->frontend
= NULL
;
559 /* All the DVB attach calls go here, this function gets modified
560 * for each new card. No other function in this file needs
563 int au0828_dvb_register(struct au0828_dev
*dev
)
565 struct au0828_dvb
*dvb
= &dev
->dvb
;
568 dprintk(1, "%s()\n", __func__
);
571 switch (dev
->boardnr
) {
572 case AU0828_BOARD_HAUPPAUGE_HVR850
:
573 case AU0828_BOARD_HAUPPAUGE_HVR950Q
:
574 dvb
->frontend
= dvb_attach(au8522_attach
,
575 &hauppauge_hvr950q_config
,
577 if (dvb
->frontend
!= NULL
)
578 switch (dev
->board
.tuner_type
) {
581 dvb_attach(xc5000_attach
, dvb
->frontend
,
583 &hauppauge_xc5000a_config
);
586 dvb_attach(xc5000_attach
, dvb
->frontend
,
588 &hauppauge_xc5000c_config
);
592 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL
:
593 dvb
->frontend
= dvb_attach(au8522_attach
,
594 &hauppauge_hvr950q_config
,
596 if (dvb
->frontend
!= NULL
)
597 dvb_attach(mxl5007t_attach
, dvb
->frontend
,
598 &dev
->i2c_adap
, 0x60,
599 &mxl5007t_hvr950q_config
);
601 case AU0828_BOARD_HAUPPAUGE_WOODBURY
:
602 dvb
->frontend
= dvb_attach(au8522_attach
,
603 &hauppauge_woodbury_config
,
605 if (dvb
->frontend
!= NULL
)
606 dvb_attach(tda18271_attach
, dvb
->frontend
,
607 0x60, &dev
->i2c_adap
,
608 &hauppauge_woodbury_tunerconfig
);
610 case AU0828_BOARD_DVICO_FUSIONHDTV7
:
611 dvb
->frontend
= dvb_attach(au8522_attach
,
612 &fusionhdtv7usb_config
,
614 if (dvb
->frontend
!= NULL
) {
615 dvb_attach(xc5000_attach
, dvb
->frontend
,
617 &hauppauge_xc5000a_config
);
621 pr_warn("The frontend of your DVB/ATSC card isn't supported yet\n");
624 if (NULL
== dvb
->frontend
) {
625 pr_err("%s() Frontend initialization failed\n",
629 /* define general-purpose callback pointer */
630 dvb
->frontend
->callback
= au0828_tuner_callback
;
632 /* register everything */
633 ret
= dvb_register(dev
);
635 if (dvb
->frontend
->ops
.release
)
636 dvb
->frontend
->ops
.release(dvb
->frontend
);
637 dvb
->frontend
= NULL
;
641 timer_setup(&dev
->bulk_timeout
, au0828_bulk_timeout
, 0);
646 void au0828_dvb_suspend(struct au0828_dev
*dev
)
648 struct au0828_dvb
*dvb
= &dev
->dvb
;
652 if (dev
->urb_streaming
) {
653 cancel_work_sync(&dev
->restart_streaming
);
655 mutex_lock(&dvb
->lock
);
656 stop_urb_transfer(dev
);
657 au0828_stop_transport(dev
, 1);
658 mutex_unlock(&dvb
->lock
);
659 dev
->need_urb_start
= true;
661 /* suspend frontend - does tuner and fe to sleep */
662 rc
= dvb_frontend_suspend(dvb
->frontend
);
663 pr_info("au0828_dvb_suspend(): Suspending DVB fe %d\n", rc
);
667 void au0828_dvb_resume(struct au0828_dev
*dev
)
669 struct au0828_dvb
*dvb
= &dev
->dvb
;
673 /* resume frontend - does fe and tuner init */
674 rc
= dvb_frontend_resume(dvb
->frontend
);
675 pr_info("au0828_dvb_resume(): Resuming DVB fe %d\n", rc
);
676 if (dev
->need_urb_start
) {
677 /* Start transport */
678 mutex_lock(&dvb
->lock
);
679 au0828_start_transport(dev
);
680 start_urb_transfer(dev
);
681 mutex_unlock(&dvb
->lock
);