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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/usb.h>
25 #include "tm6000-regs.h"
29 #include <media/tuner.h>
31 #include "tuner-xc2028.h"
34 MODULE_DESCRIPTION("DVB driver extension module for tm5600/6000/6010 based TV cards");
35 MODULE_AUTHOR("Mauro Carvalho Chehab");
36 MODULE_LICENSE("GPL");
38 MODULE_SUPPORTED_DEVICE("{{Trident, tm5600},{{Trident, tm6000},{{Trident, tm6010}");
42 module_param(debug
, int, 0644);
43 MODULE_PARM_DESC(debug
, "enable debug message");
45 static inline void print_err_status(struct tm6000_core
*dev
,
46 int packet
, int status
)
48 char *errmsg
= "Unknown";
52 errmsg
= "unlinked synchronuously";
55 errmsg
= "unlinked asynchronuously";
58 errmsg
= "Buffer error (overrun)";
61 errmsg
= "Stalled (device not responding)";
64 errmsg
= "Babble (bad cable?)";
67 errmsg
= "Bit-stuff error (bad cable?)";
70 errmsg
= "CRC/Timeout (could be anything)";
73 errmsg
= "Device does not respond";
77 dprintk(dev
, 1, "URB status %d [%s].\n",
80 dprintk(dev
, 1, "URB packet %d, status %d [%s].\n",
81 packet
, status
, errmsg
);
85 static void tm6000_urb_received(struct urb
*urb
)
88 struct tm6000_core
*dev
= urb
->context
;
90 switch (urb
->status
) {
99 print_err_status(dev
, 0, urb
->status
);
102 if (urb
->actual_length
> 0)
103 dvb_dmx_swfilter(&dev
->dvb
->demux
, urb
->transfer_buffer
,
106 if (dev
->dvb
->streams
> 0) {
107 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
109 printk(KERN_ERR
"tm6000: error %s\n", __func__
);
110 kfree(urb
->transfer_buffer
);
116 static int tm6000_start_stream(struct tm6000_core
*dev
)
119 unsigned int pipe
, size
;
120 struct tm6000_dvb
*dvb
= dev
->dvb
;
122 printk(KERN_INFO
"tm6000: got start stream request %s\n", __func__
);
124 if (dev
->mode
!= TM6000_MODE_DIGITAL
) {
125 tm6000_init_digital_mode(dev
);
126 dev
->mode
= TM6000_MODE_DIGITAL
;
129 dvb
->bulk_urb
= usb_alloc_urb(0, GFP_KERNEL
);
130 if (dvb
->bulk_urb
== NULL
)
133 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->bulk_in
.endp
->desc
.bEndpointAddress
134 & USB_ENDPOINT_NUMBER_MASK
);
136 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
137 size
= size
* 15; /* 512 x 8 or 12 or 15 */
139 dvb
->bulk_urb
->transfer_buffer
= kzalloc(size
, GFP_KERNEL
);
140 if (dvb
->bulk_urb
->transfer_buffer
== NULL
) {
141 usb_free_urb(dvb
->bulk_urb
);
142 printk(KERN_ERR
"tm6000: couldn't allocate transfer buffer!\n");
146 usb_fill_bulk_urb(dvb
->bulk_urb
, dev
->udev
, pipe
,
147 dvb
->bulk_urb
->transfer_buffer
,
149 tm6000_urb_received
, dev
);
151 ret
= usb_clear_halt(dev
->udev
, pipe
);
153 printk(KERN_ERR
"tm6000: error %i in %s during pipe reset\n",
157 printk(KERN_ERR
"tm6000: pipe resetted\n");
159 /* mutex_lock(&tm6000_driver.open_close_mutex); */
160 ret
= usb_submit_urb(dvb
->bulk_urb
, GFP_ATOMIC
);
162 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
164 printk(KERN_ERR
"tm6000: submit of urb failed (error=%i)\n",
167 kfree(dvb
->bulk_urb
->transfer_buffer
);
168 usb_free_urb(dvb
->bulk_urb
);
175 static void tm6000_stop_stream(struct tm6000_core
*dev
)
177 struct tm6000_dvb
*dvb
= dev
->dvb
;
180 printk(KERN_INFO
"urb killing\n");
181 usb_kill_urb(dvb
->bulk_urb
);
182 printk(KERN_INFO
"urb buffer free\n");
183 kfree(dvb
->bulk_urb
->transfer_buffer
);
184 usb_free_urb(dvb
->bulk_urb
);
185 dvb
->bulk_urb
= NULL
;
189 static int tm6000_start_feed(struct dvb_demux_feed
*feed
)
191 struct dvb_demux
*demux
= feed
->demux
;
192 struct tm6000_core
*dev
= demux
->priv
;
193 struct tm6000_dvb
*dvb
= dev
->dvb
;
194 printk(KERN_INFO
"tm6000: got start feed request %s\n", __func__
);
196 mutex_lock(&dvb
->mutex
);
197 if (dvb
->streams
== 0) {
199 /* mutex_init(&tm6000_dev->streming_mutex); */
200 tm6000_start_stream(dev
);
203 mutex_unlock(&dvb
->mutex
);
208 static int tm6000_stop_feed(struct dvb_demux_feed
*feed
)
210 struct dvb_demux
*demux
= feed
->demux
;
211 struct tm6000_core
*dev
= demux
->priv
;
212 struct tm6000_dvb
*dvb
= dev
->dvb
;
214 printk(KERN_INFO
"tm6000: got stop feed request %s\n", __func__
);
216 mutex_lock(&dvb
->mutex
);
218 printk(KERN_INFO
"stream %#x\n", dvb
->streams
);
220 if (dvb
->streams
== 0) {
221 printk(KERN_INFO
"stop stream\n");
222 tm6000_stop_stream(dev
);
223 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
225 mutex_unlock(&dvb
->mutex
);
226 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
231 static int tm6000_dvb_attach_frontend(struct tm6000_core
*dev
)
233 struct tm6000_dvb
*dvb
= dev
->dvb
;
235 if (dev
->caps
.has_zl10353
) {
236 struct zl10353_config config
= {
237 .demod_address
= dev
->demod_addr
,
241 .disable_i2c_gate_ctrl
= 1,
244 dvb
->frontend
= dvb_attach(zl10353_attach
, &config
,
247 printk(KERN_ERR
"tm6000: no frontend defined for the device!\n");
251 return (!dvb
->frontend
) ? -1 : 0;
254 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
256 static int register_dvb(struct tm6000_core
*dev
)
259 struct tm6000_dvb
*dvb
= dev
->dvb
;
261 mutex_init(&dvb
->mutex
);
265 /* attach the frontend */
266 ret
= tm6000_dvb_attach_frontend(dev
);
268 printk(KERN_ERR
"tm6000: couldn't attach the frontend!\n");
272 ret
= dvb_register_adapter(&dvb
->adapter
, "Trident TVMaster 6000 DVB-T",
273 THIS_MODULE
, &dev
->udev
->dev
, adapter_nr
);
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
;
368 if (dvb
->bulk_urb
!= NULL
) {
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
);
408 printk(KERN_INFO
"Cannot allocate memory\n");
414 rc
= register_dvb(dev
);
424 static int dvb_fini(struct tm6000_core
*dev
)
429 if (!dev
->caps
.has_dvb
)
441 static struct tm6000_ops dvb_ops
= {
443 .name
= "TM6000 dvb Extension",
448 static int __init
tm6000_dvb_register(void)
450 return tm6000_register_extension(&dvb_ops
);
453 static void __exit
tm6000_dvb_unregister(void)
455 tm6000_unregister_extension(&dvb_ops
);
458 module_init(tm6000_dvb_register
);
459 module_exit(tm6000_dvb_unregister
);