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},"
40 "{{Trident, tm6010}");
44 module_param(debug
, int, 0644);
45 MODULE_PARM_DESC(debug
, "enable debug message");
47 static inline void print_err_status(struct tm6000_core
*dev
,
48 int packet
, int status
)
50 char *errmsg
= "Unknown";
54 errmsg
= "unlinked synchronuously";
57 errmsg
= "unlinked asynchronuously";
60 errmsg
= "Buffer error (overrun)";
63 errmsg
= "Stalled (device not responding)";
66 errmsg
= "Babble (bad cable?)";
69 errmsg
= "Bit-stuff error (bad cable?)";
72 errmsg
= "CRC/Timeout (could be anything)";
75 errmsg
= "Device does not respond";
79 dprintk(dev
, 1, "URB status %d [%s].\n",
82 dprintk(dev
, 1, "URB packet %d, status %d [%s].\n",
83 packet
, status
, errmsg
);
87 static void tm6000_urb_received(struct urb
*urb
)
90 struct tm6000_core
*dev
= urb
->context
;
92 switch (urb
->status
) {
101 print_err_status(dev
, 0, urb
->status
);
104 if (urb
->actual_length
> 0)
105 dvb_dmx_swfilter(&dev
->dvb
->demux
, urb
->transfer_buffer
,
108 if (dev
->dvb
->streams
> 0) {
109 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
111 printk(KERN_ERR
"tm6000: error %s\n", __func__
);
112 kfree(urb
->transfer_buffer
);
118 static int tm6000_start_stream(struct tm6000_core
*dev
)
121 unsigned int pipe
, size
;
122 struct tm6000_dvb
*dvb
= dev
->dvb
;
124 printk(KERN_INFO
"tm6000: got start stream request %s\n", __func__
);
126 if (dev
->mode
!= TM6000_MODE_DIGITAL
) {
127 tm6000_init_digital_mode(dev
);
128 dev
->mode
= TM6000_MODE_DIGITAL
;
131 dvb
->bulk_urb
= usb_alloc_urb(0, GFP_KERNEL
);
132 if (dvb
->bulk_urb
== NULL
) {
133 printk(KERN_ERR
"tm6000: couldn't allocate urb\n");
137 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->bulk_in
.endp
->desc
.bEndpointAddress
138 & USB_ENDPOINT_NUMBER_MASK
);
140 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
141 size
= size
* 15; /* 512 x 8 or 12 or 15 */
143 dvb
->bulk_urb
->transfer_buffer
= kzalloc(size
, GFP_KERNEL
);
144 if (dvb
->bulk_urb
->transfer_buffer
== NULL
) {
145 usb_free_urb(dvb
->bulk_urb
);
146 printk(KERN_ERR
"tm6000: couldn't allocate transfer buffer!\n");
150 usb_fill_bulk_urb(dvb
->bulk_urb
, dev
->udev
, pipe
,
151 dvb
->bulk_urb
->transfer_buffer
,
153 tm6000_urb_received
, dev
);
155 ret
= usb_clear_halt(dev
->udev
, pipe
);
157 printk(KERN_ERR
"tm6000: error %i in %s during pipe reset\n",
161 printk(KERN_ERR
"tm6000: pipe resetted\n");
163 /* mutex_lock(&tm6000_driver.open_close_mutex); */
164 ret
= usb_submit_urb(dvb
->bulk_urb
, GFP_ATOMIC
);
166 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
168 printk(KERN_ERR
"tm6000: submit of urb failed (error=%i)\n",
171 kfree(dvb
->bulk_urb
->transfer_buffer
);
172 usb_free_urb(dvb
->bulk_urb
);
179 static void tm6000_stop_stream(struct tm6000_core
*dev
)
181 struct tm6000_dvb
*dvb
= dev
->dvb
;
184 printk(KERN_INFO
"urb killing\n");
185 usb_kill_urb(dvb
->bulk_urb
);
186 printk(KERN_INFO
"urb buffer free\n");
187 kfree(dvb
->bulk_urb
->transfer_buffer
);
188 usb_free_urb(dvb
->bulk_urb
);
189 dvb
->bulk_urb
= NULL
;
193 static int tm6000_start_feed(struct dvb_demux_feed
*feed
)
195 struct dvb_demux
*demux
= feed
->demux
;
196 struct tm6000_core
*dev
= demux
->priv
;
197 struct tm6000_dvb
*dvb
= dev
->dvb
;
198 printk(KERN_INFO
"tm6000: got start feed request %s\n", __func__
);
200 mutex_lock(&dvb
->mutex
);
201 if (dvb
->streams
== 0) {
203 /* mutex_init(&tm6000_dev->streming_mutex); */
204 tm6000_start_stream(dev
);
207 mutex_unlock(&dvb
->mutex
);
212 static int tm6000_stop_feed(struct dvb_demux_feed
*feed
)
214 struct dvb_demux
*demux
= feed
->demux
;
215 struct tm6000_core
*dev
= demux
->priv
;
216 struct tm6000_dvb
*dvb
= dev
->dvb
;
218 printk(KERN_INFO
"tm6000: got stop feed request %s\n", __func__
);
220 mutex_lock(&dvb
->mutex
);
222 printk(KERN_INFO
"stream %#x\n", dvb
->streams
);
224 if (dvb
->streams
== 0) {
225 printk(KERN_INFO
"stop stream\n");
226 tm6000_stop_stream(dev
);
227 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
229 mutex_unlock(&dvb
->mutex
);
230 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
235 static int tm6000_dvb_attach_frontend(struct tm6000_core
*dev
)
237 struct tm6000_dvb
*dvb
= dev
->dvb
;
239 if (dev
->caps
.has_zl10353
) {
240 struct zl10353_config config
= {
241 .demod_address
= dev
->demod_addr
,
245 .disable_i2c_gate_ctrl
= 1,
248 dvb
->frontend
= dvb_attach(zl10353_attach
, &config
,
251 printk(KERN_ERR
"tm6000: no frontend defined for the device!\n");
255 return (!dvb
->frontend
) ? -1 : 0;
258 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
260 static int register_dvb(struct tm6000_core
*dev
)
263 struct tm6000_dvb
*dvb
= dev
->dvb
;
265 mutex_init(&dvb
->mutex
);
269 /* attach the frontend */
270 ret
= tm6000_dvb_attach_frontend(dev
);
272 printk(KERN_ERR
"tm6000: couldn't attach the frontend!\n");
276 ret
= dvb_register_adapter(&dvb
->adapter
, "Trident TVMaster 6000 DVB-T",
277 THIS_MODULE
, &dev
->udev
->dev
, adapter_nr
);
278 dvb
->adapter
.priv
= dev
;
281 switch (dev
->tuner_type
) {
283 struct xc2028_config cfg
= {
284 .i2c_adap
= &dev
->i2c_adap
,
285 .i2c_addr
= dev
->tuner_addr
,
288 dvb
->frontend
->callback
= tm6000_tuner_callback
;
289 ret
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
292 "tm6000: couldn't register frontend\n");
296 if (!dvb_attach(xc2028_attach
, dvb
->frontend
, &cfg
)) {
297 printk(KERN_ERR
"tm6000: couldn't register "
298 "frontend (xc3028)\n");
302 printk(KERN_INFO
"tm6000: XC2028/3028 asked to be "
303 "attached to frontend!\n");
307 struct xc5000_config cfg
= {
308 .i2c_address
= dev
->tuner_addr
,
311 dvb
->frontend
->callback
= tm6000_xc5000_callback
;
312 ret
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
315 "tm6000: couldn't register frontend\n");
319 if (!dvb_attach(xc5000_attach
, dvb
->frontend
, &dev
->i2c_adap
, &cfg
)) {
320 printk(KERN_ERR
"tm6000: couldn't register "
321 "frontend (xc5000)\n");
325 printk(KERN_INFO
"tm6000: XC5000 asked to be "
326 "attached to frontend!\n");
331 printk(KERN_ERR
"tm6000: no frontend found\n");
333 dvb
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
| DMX_SECTION_FILTERING
334 | DMX_MEMORY_BASED_FILTERING
;
335 dvb
->demux
.priv
= dev
;
336 dvb
->demux
.filternum
= 8;
337 dvb
->demux
.feednum
= 8;
338 dvb
->demux
.start_feed
= tm6000_start_feed
;
339 dvb
->demux
.stop_feed
= tm6000_stop_feed
;
340 dvb
->demux
.write_to_decoder
= NULL
;
341 ret
= dvb_dmx_init(&dvb
->demux
);
343 printk(KERN_ERR
"tm6000: dvb_dmx_init failed (errno = %d)\n", ret
);
347 dvb
->dmxdev
.filternum
= dev
->dvb
->demux
.filternum
;
348 dvb
->dmxdev
.demux
= &dev
->dvb
->demux
.dmx
;
349 dvb
->dmxdev
.capabilities
= 0;
351 ret
= dvb_dmxdev_init(&dvb
->dmxdev
, &dvb
->adapter
);
353 printk(KERN_ERR
"tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret
);
360 dvb_dmx_release(&dvb
->demux
);
363 dvb_unregister_frontend(dvb
->frontend
);
364 dvb_frontend_detach(dvb
->frontend
);
367 dvb_unregister_adapter(&dvb
->adapter
);
372 static void unregister_dvb(struct tm6000_core
*dev
)
374 struct tm6000_dvb
*dvb
= dev
->dvb
;
376 if (dvb
->bulk_urb
!= NULL
) {
377 struct urb
*bulk_urb
= dvb
->bulk_urb
;
379 kfree(bulk_urb
->transfer_buffer
);
380 bulk_urb
->transfer_buffer
= NULL
;
381 usb_unlink_urb(bulk_urb
);
382 usb_free_urb(bulk_urb
);
385 /* mutex_lock(&tm6000_driver.open_close_mutex); */
387 dvb_unregister_frontend(dvb
->frontend
);
388 dvb_frontend_detach(dvb
->frontend
);
391 dvb_dmxdev_release(&dvb
->dmxdev
);
392 dvb_dmx_release(&dvb
->demux
);
393 dvb_unregister_adapter(&dvb
->adapter
);
394 mutex_destroy(&dvb
->mutex
);
395 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
398 static int dvb_init(struct tm6000_core
*dev
)
400 struct tm6000_dvb
*dvb
;
406 if (!dev
->caps
.has_dvb
)
409 if (dev
->udev
->speed
== USB_SPEED_FULL
) {
410 printk(KERN_INFO
"This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)\n");
414 dvb
= kzalloc(sizeof(struct tm6000_dvb
), GFP_KERNEL
);
416 printk(KERN_INFO
"Cannot allocate memory\n");
422 rc
= register_dvb(dev
);
432 static int dvb_fini(struct tm6000_core
*dev
)
437 if (!dev
->caps
.has_dvb
)
449 static struct tm6000_ops dvb_ops
= {
451 .name
= "TM6000 dvb Extension",
456 static int __init
tm6000_dvb_register(void)
458 return tm6000_register_extension(&dvb_ops
);
461 static void __exit
tm6000_dvb_unregister(void)
463 tm6000_unregister_extension(&dvb_ops
);
466 module_init(tm6000_dvb_register
);
467 module_exit(tm6000_dvb_unregister
);