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>
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
[] = {
51 static struct au8522_led_config hauppauge_hvr950q_led_cfg
= {
52 .gpio_output
= 0x00e0,
53 .gpio_output_enable
= 0x6006,
54 .gpio_output_disable
= 0x0660,
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
= {
93 static struct xc5000_config hauppauge_xc5000c_config
= {
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
);
117 dprintk(2, "%s()\n", __func__
);
122 if (dev
->urb_streaming
== 0)
125 if (ptype
!= PIPE_BULK
) {
126 printk(KERN_ERR
"%s() Unsupported URB type %d\n",
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
);
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
);
149 usb_submit_urb(purb
, GFP_ATOMIC
);
152 static int stop_urb_transfer(struct au0828_dev
*dev
)
156 dprintk(2, "%s()\n", __func__
);
158 dev
->urb_streaming
= 0;
159 for (i
= 0; i
< URB_COUNT
; 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
]);
172 static int start_urb_transfer(struct au0828_dev
*dev
)
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__
);
184 for (i
= 0; i
< URB_COUNT
; i
++) {
186 dev
->urbs
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
192 if (preallocate_big_buffers
)
193 purb
->transfer_buffer
= dev
->dig_transfer_buffer
[i
];
195 purb
->transfer_buffer
= kzalloc(URB_BUFSIZE
,
198 if (!purb
->transfer_buffer
) {
202 "%s: failed big buffer allocation, err = %d\n",
207 purb
->status
= -EINPROGRESS
;
208 usb_fill_bulk_urb(purb
,
210 usb_rcvbulkpipe(dev
->usbdev
,
212 purb
->transfer_buffer
,
219 for (i
= 0; i
< URB_COUNT
; i
++) {
220 ret
= usb_submit_urb(dev
->urbs
[i
], GFP_ATOMIC
);
222 stop_urb_transfer(dev
);
223 printk(KERN_ERR
"%s: failed urb submission, "
224 "err = %d\n", __func__
, ret
);
229 dev
->urb_streaming
= 1;
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
)
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
;
264 dprintk(1, "%s()\n", __func__
);
266 if (!demux
->dmx
.frontend
)
270 mutex_lock(&dvb
->lock
);
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
);
279 au0828_stop_transport(dev
, 0);
280 dvb
->feeding
--; /* We ran out of memory... */
283 mutex_unlock(&dvb
->lock
);
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
;
296 dprintk(1, "%s()\n", __func__
);
299 mutex_lock(&dvb
->lock
);
301 dprintk(1, "%s(), start_count: %d, stop_count: %d\n", __func__
,
302 dvb
->start_count
, dvb
->stop_count
);
303 if (dvb
->feeding
> 0) {
305 if (dvb
->feeding
== 0) {
307 ret
= stop_urb_transfer(dev
);
308 au0828_stop_transport(dev
, 0);
311 mutex_unlock(&dvb
->lock
);
317 static void au0828_restart_dvb_streaming(struct work_struct
*work
)
319 struct au0828_dev
*dev
= container_of(work
, struct au0828_dev
,
321 struct au0828_dvb
*dvb
= &dev
->dvb
;
323 if (dev
->urb_streaming
== 0)
326 dprintk(1, "Restarting streaming...!\n");
328 mutex_lock(&dvb
->lock
);
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
;
346 dprintk(1, "%s()\n", __func__
);
348 if (preallocate_big_buffers
) {
350 for (i
= 0; i
< URB_COUNT
; i
++) {
351 dev
->dig_transfer_buffer
[i
] = kzalloc(URB_BUFSIZE
,
354 if (!dev
->dig_transfer_buffer
[i
]) {
358 "%s: failed buffer allocation (errno = %d)\n",
359 DRIVER_NAME
, result
);
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
);
371 printk(KERN_ERR
"%s: dvb_register_adapter failed "
372 "(errno = %d)\n", DRIVER_NAME
, result
);
375 dvb
->adapter
.priv
= dev
;
377 /* register frontend */
378 result
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
380 printk(KERN_ERR
"%s: dvb_register_frontend failed "
381 "(errno = %d)\n", DRIVER_NAME
, result
);
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
);
396 printk(KERN_ERR
"%s: dvb_dmx_init failed (errno = %d)\n",
397 DRIVER_NAME
, result
);
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
);
406 printk(KERN_ERR
"%s: dvb_dmxdev_init failed (errno = %d)\n",
407 DRIVER_NAME
, result
);
411 dvb
->fe_hw
.source
= DMX_FRONTEND_0
;
412 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
414 printk(KERN_ERR
"%s: add_frontend failed "
415 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME
, result
);
419 dvb
->fe_mem
.source
= DMX_MEMORY_FE
;
420 result
= dvb
->demux
.dmx
.add_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
422 printk(KERN_ERR
"%s: add_frontend failed "
423 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME
, result
);
427 result
= dvb
->demux
.dmx
.connect_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
429 printk(KERN_ERR
"%s: connect_frontend failed (errno = %d)\n",
430 DRIVER_NAME
, result
);
434 /* register network adapter */
435 dvb_net_init(&dvb
->adapter
, &dvb
->net
, &dvb
->demux
.dmx
);
437 dvb
->start_count
= 0;
442 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_mem
);
444 dvb
->demux
.dmx
.remove_frontend(&dvb
->demux
.dmx
, &dvb
->fe_hw
);
446 dvb_dmxdev_release(&dvb
->dmxdev
);
448 dvb_dmx_release(&dvb
->demux
);
450 dvb_unregister_frontend(dvb
->frontend
);
452 dvb_frontend_detach(dvb
->frontend
);
453 dvb_unregister_adapter(&dvb
->adapter
);
456 if (preallocate_big_buffers
) {
458 for (i
= 0; i
< URB_COUNT
; i
++)
459 kfree(dev
->dig_transfer_buffer
[i
]);
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
)
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
) {
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
496 int au0828_dvb_register(struct au0828_dev
*dev
)
498 struct au0828_dvb
*dvb
= &dev
->dvb
;
501 dprintk(1, "%s()\n", __func__
);
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
,
510 if (dvb
->frontend
!= NULL
)
511 switch (dev
->board
.tuner_type
) {
514 dvb_attach(xc5000_attach
, dvb
->frontend
,
516 &hauppauge_xc5000a_config
);
519 dvb_attach(xc5000_attach
, dvb
->frontend
,
521 &hauppauge_xc5000c_config
);
525 case AU0828_BOARD_HAUPPAUGE_HVR950Q_MXL
:
526 dvb
->frontend
= dvb_attach(au8522_attach
,
527 &hauppauge_hvr950q_config
,
529 if (dvb
->frontend
!= NULL
)
530 dvb_attach(mxl5007t_attach
, dvb
->frontend
,
531 &dev
->i2c_adap
, 0x60,
532 &mxl5007t_hvr950q_config
);
534 case AU0828_BOARD_HAUPPAUGE_WOODBURY
:
535 dvb
->frontend
= dvb_attach(au8522_attach
,
536 &hauppauge_woodbury_config
,
538 if (dvb
->frontend
!= NULL
)
539 dvb_attach(tda18271_attach
, dvb
->frontend
,
540 0x60, &dev
->i2c_adap
,
541 &hauppauge_woodbury_tunerconfig
);
543 case AU0828_BOARD_DVICO_FUSIONHDTV7
:
544 dvb
->frontend
= dvb_attach(au8522_attach
,
545 &fusionhdtv7usb_config
,
547 if (dvb
->frontend
!= NULL
) {
548 dvb_attach(xc5000_attach
, dvb
->frontend
,
550 &hauppauge_xc5000a_config
);
554 printk(KERN_WARNING
"The frontend of your DVB/ATSC card "
555 "isn't supported yet\n");
558 if (NULL
== dvb
->frontend
) {
559 printk(KERN_ERR
"%s() Frontend initialization failed\n",
563 /* define general-purpose callback pointer */
564 dvb
->frontend
->callback
= au0828_tuner_callback
;
566 /* register everything */
567 ret
= dvb_register(dev
);
569 if (dvb
->frontend
->ops
.release
)
570 dvb
->frontend
->ops
.release(dvb
->frontend
);