2 * tm6000-dvb.c - dvb-t support for TM5600/TM6000/TM6010 USB video capture devices
4 * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
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 version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/usb.h>
21 #include "tm6000-regs.h"
25 #include <media/tuner.h>
27 #include "tuner-xc2028.h"
30 MODULE_DESCRIPTION("DVB driver extension module for tm5600/6000/6010 based TV cards");
31 MODULE_AUTHOR("Mauro Carvalho Chehab");
32 MODULE_LICENSE("GPL");
34 MODULE_SUPPORTED_DEVICE("{{Trident, tm5600},{{Trident, tm6000},{{Trident, tm6010}");
38 module_param(debug
, int, 0644);
39 MODULE_PARM_DESC(debug
, "enable debug message");
41 static inline void print_err_status(struct tm6000_core
*dev
,
42 int packet
, int status
)
44 char *errmsg
= "Unknown";
48 errmsg
= "unlinked synchronously";
51 errmsg
= "unlinked asynchronously";
54 errmsg
= "Buffer error (overrun)";
57 errmsg
= "Stalled (device not responding)";
60 errmsg
= "Babble (bad cable?)";
63 errmsg
= "Bit-stuff error (bad cable?)";
66 errmsg
= "CRC/Timeout (could be anything)";
69 errmsg
= "Device does not respond";
73 dprintk(dev
, 1, "URB status %d [%s].\n",
76 dprintk(dev
, 1, "URB packet %d, status %d [%s].\n",
77 packet
, status
, errmsg
);
81 static void tm6000_urb_received(struct urb
*urb
)
84 struct tm6000_core
*dev
= urb
->context
;
86 switch (urb
->status
) {
95 print_err_status(dev
, 0, urb
->status
);
98 if (urb
->actual_length
> 0)
99 dvb_dmx_swfilter(&dev
->dvb
->demux
, urb
->transfer_buffer
,
102 if (dev
->dvb
->streams
> 0) {
103 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
105 printk(KERN_ERR
"tm6000: error %s\n", __func__
);
106 kfree(urb
->transfer_buffer
);
112 static int tm6000_start_stream(struct tm6000_core
*dev
)
115 unsigned int pipe
, size
;
116 struct tm6000_dvb
*dvb
= dev
->dvb
;
118 printk(KERN_INFO
"tm6000: got start stream request %s\n", __func__
);
120 if (dev
->mode
!= TM6000_MODE_DIGITAL
) {
121 tm6000_init_digital_mode(dev
);
122 dev
->mode
= TM6000_MODE_DIGITAL
;
125 dvb
->bulk_urb
= usb_alloc_urb(0, GFP_KERNEL
);
129 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->bulk_in
.endp
->desc
.bEndpointAddress
130 & USB_ENDPOINT_NUMBER_MASK
);
132 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
133 size
= size
* 15; /* 512 x 8 or 12 or 15 */
135 dvb
->bulk_urb
->transfer_buffer
= kzalloc(size
, GFP_KERNEL
);
136 if (!dvb
->bulk_urb
->transfer_buffer
) {
137 usb_free_urb(dvb
->bulk_urb
);
141 usb_fill_bulk_urb(dvb
->bulk_urb
, dev
->udev
, pipe
,
142 dvb
->bulk_urb
->transfer_buffer
,
144 tm6000_urb_received
, dev
);
146 ret
= usb_clear_halt(dev
->udev
, pipe
);
148 printk(KERN_ERR
"tm6000: error %i in %s during pipe reset\n",
152 printk(KERN_ERR
"tm6000: pipe resetted\n");
154 /* mutex_lock(&tm6000_driver.open_close_mutex); */
155 ret
= usb_submit_urb(dvb
->bulk_urb
, GFP_ATOMIC
);
157 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
159 printk(KERN_ERR
"tm6000: submit of urb failed (error=%i)\n",
162 kfree(dvb
->bulk_urb
->transfer_buffer
);
163 usb_free_urb(dvb
->bulk_urb
);
170 static void tm6000_stop_stream(struct tm6000_core
*dev
)
172 struct tm6000_dvb
*dvb
= dev
->dvb
;
175 printk(KERN_INFO
"urb killing\n");
176 usb_kill_urb(dvb
->bulk_urb
);
177 printk(KERN_INFO
"urb buffer free\n");
178 kfree(dvb
->bulk_urb
->transfer_buffer
);
179 usb_free_urb(dvb
->bulk_urb
);
180 dvb
->bulk_urb
= NULL
;
184 static int tm6000_start_feed(struct dvb_demux_feed
*feed
)
186 struct dvb_demux
*demux
= feed
->demux
;
187 struct tm6000_core
*dev
= demux
->priv
;
188 struct tm6000_dvb
*dvb
= dev
->dvb
;
189 printk(KERN_INFO
"tm6000: got start feed request %s\n", __func__
);
191 mutex_lock(&dvb
->mutex
);
192 if (dvb
->streams
== 0) {
194 /* mutex_init(&tm6000_dev->streming_mutex); */
195 tm6000_start_stream(dev
);
198 mutex_unlock(&dvb
->mutex
);
203 static int tm6000_stop_feed(struct dvb_demux_feed
*feed
)
205 struct dvb_demux
*demux
= feed
->demux
;
206 struct tm6000_core
*dev
= demux
->priv
;
207 struct tm6000_dvb
*dvb
= dev
->dvb
;
209 printk(KERN_INFO
"tm6000: got stop feed request %s\n", __func__
);
211 mutex_lock(&dvb
->mutex
);
213 printk(KERN_INFO
"stream %#x\n", dvb
->streams
);
215 if (dvb
->streams
== 0) {
216 printk(KERN_INFO
"stop stream\n");
217 tm6000_stop_stream(dev
);
218 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
220 mutex_unlock(&dvb
->mutex
);
221 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
226 static int tm6000_dvb_attach_frontend(struct tm6000_core
*dev
)
228 struct tm6000_dvb
*dvb
= dev
->dvb
;
230 if (dev
->caps
.has_zl10353
) {
231 struct zl10353_config config
= {
232 .demod_address
= dev
->demod_addr
,
236 .disable_i2c_gate_ctrl
= 1,
239 dvb
->frontend
= dvb_attach(zl10353_attach
, &config
,
242 printk(KERN_ERR
"tm6000: no frontend defined for the device!\n");
246 return (!dvb
->frontend
) ? -1 : 0;
249 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
251 static int register_dvb(struct tm6000_core
*dev
)
254 struct tm6000_dvb
*dvb
= dev
->dvb
;
256 mutex_init(&dvb
->mutex
);
260 /* attach the frontend */
261 ret
= tm6000_dvb_attach_frontend(dev
);
263 printk(KERN_ERR
"tm6000: couldn't attach the frontend!\n");
267 ret
= dvb_register_adapter(&dvb
->adapter
, "Trident TVMaster 6000 DVB-T",
268 THIS_MODULE
, &dev
->udev
->dev
, adapter_nr
);
270 pr_err("tm6000: couldn't register the adapter!\n");
274 dvb
->adapter
.priv
= dev
;
277 switch (dev
->tuner_type
) {
279 struct xc2028_config cfg
= {
280 .i2c_adap
= &dev
->i2c_adap
,
281 .i2c_addr
= dev
->tuner_addr
,
284 dvb
->frontend
->callback
= tm6000_tuner_callback
;
285 ret
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
288 "tm6000: couldn't register frontend\n");
292 if (!dvb_attach(xc2028_attach
, dvb
->frontend
, &cfg
)) {
293 printk(KERN_ERR
"tm6000: couldn't register frontend (xc3028)\n");
297 printk(KERN_INFO
"tm6000: XC2028/3028 asked to be attached to frontend!\n");
301 struct xc5000_config cfg
= {
302 .i2c_address
= dev
->tuner_addr
,
305 dvb
->frontend
->callback
= tm6000_xc5000_callback
;
306 ret
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
309 "tm6000: couldn't register frontend\n");
313 if (!dvb_attach(xc5000_attach
, dvb
->frontend
, &dev
->i2c_adap
, &cfg
)) {
314 printk(KERN_ERR
"tm6000: couldn't register frontend (xc5000)\n");
318 printk(KERN_INFO
"tm6000: XC5000 asked to be attached to frontend!\n");
323 printk(KERN_ERR
"tm6000: no frontend found\n");
325 dvb
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
| DMX_SECTION_FILTERING
326 | DMX_MEMORY_BASED_FILTERING
;
327 dvb
->demux
.priv
= dev
;
328 dvb
->demux
.filternum
= 8;
329 dvb
->demux
.feednum
= 8;
330 dvb
->demux
.start_feed
= tm6000_start_feed
;
331 dvb
->demux
.stop_feed
= tm6000_stop_feed
;
332 dvb
->demux
.write_to_decoder
= NULL
;
333 ret
= dvb_dmx_init(&dvb
->demux
);
335 printk(KERN_ERR
"tm6000: dvb_dmx_init failed (errno = %d)\n", ret
);
339 dvb
->dmxdev
.filternum
= dev
->dvb
->demux
.filternum
;
340 dvb
->dmxdev
.demux
= &dev
->dvb
->demux
.dmx
;
341 dvb
->dmxdev
.capabilities
= 0;
343 ret
= dvb_dmxdev_init(&dvb
->dmxdev
, &dvb
->adapter
);
345 printk(KERN_ERR
"tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret
);
352 dvb_dmx_release(&dvb
->demux
);
355 dvb_unregister_frontend(dvb
->frontend
);
356 dvb_frontend_detach(dvb
->frontend
);
359 dvb_unregister_adapter(&dvb
->adapter
);
364 static void unregister_dvb(struct tm6000_core
*dev
)
366 struct tm6000_dvb
*dvb
= dev
->dvb
;
369 struct urb
*bulk_urb
= dvb
->bulk_urb
;
371 kfree(bulk_urb
->transfer_buffer
);
372 bulk_urb
->transfer_buffer
= NULL
;
373 usb_unlink_urb(bulk_urb
);
374 usb_free_urb(bulk_urb
);
377 /* mutex_lock(&tm6000_driver.open_close_mutex); */
379 dvb_unregister_frontend(dvb
->frontend
);
380 dvb_frontend_detach(dvb
->frontend
);
383 dvb_dmxdev_release(&dvb
->dmxdev
);
384 dvb_dmx_release(&dvb
->demux
);
385 dvb_unregister_adapter(&dvb
->adapter
);
386 mutex_destroy(&dvb
->mutex
);
387 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
390 static int dvb_init(struct tm6000_core
*dev
)
392 struct tm6000_dvb
*dvb
;
398 if (!dev
->caps
.has_dvb
)
401 if (dev
->udev
->speed
== USB_SPEED_FULL
) {
402 printk(KERN_INFO
"This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)\n");
406 dvb
= kzalloc(sizeof(struct tm6000_dvb
), GFP_KERNEL
);
412 rc
= register_dvb(dev
);
422 static int dvb_fini(struct tm6000_core
*dev
)
427 if (!dev
->caps
.has_dvb
)
439 static struct tm6000_ops dvb_ops
= {
441 .name
= "TM6000 dvb Extension",
446 static int __init
tm6000_dvb_register(void)
448 return tm6000_register_extension(&dvb_ops
);
451 static void __exit
tm6000_dvb_unregister(void)
453 tm6000_unregister_extension(&dvb_ops
);
456 module_init(tm6000_dvb_register
);
457 module_exit(tm6000_dvb_unregister
);