1 // SPDX-License-Identifier: GPL-2.0-only
3 * tm6000-dvb.c - dvb-t support for TM5600/TM6000/TM6010 USB video capture devices
5 * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/usb.h>
13 #include "tm6000-regs.h"
17 #include <media/tuner.h>
19 #include "tuner-xc2028.h"
22 MODULE_DESCRIPTION("DVB driver extension module for tm5600/6000/6010 based TV cards");
23 MODULE_AUTHOR("Mauro Carvalho Chehab");
24 MODULE_LICENSE("GPL");
26 MODULE_SUPPORTED_DEVICE("{{Trident, tm5600},{{Trident, tm6000},{{Trident, tm6010}");
30 module_param(debug
, int, 0644);
31 MODULE_PARM_DESC(debug
, "enable debug message");
33 static inline void print_err_status(struct tm6000_core
*dev
,
34 int packet
, int status
)
36 char *errmsg
= "Unknown";
40 errmsg
= "unlinked synchronously";
43 errmsg
= "unlinked asynchronously";
46 errmsg
= "Buffer error (overrun)";
49 errmsg
= "Stalled (device not responding)";
52 errmsg
= "Babble (bad cable?)";
55 errmsg
= "Bit-stuff error (bad cable?)";
58 errmsg
= "CRC/Timeout (could be anything)";
61 errmsg
= "Device does not respond";
65 dprintk(dev
, 1, "URB status %d [%s].\n",
68 dprintk(dev
, 1, "URB packet %d, status %d [%s].\n",
69 packet
, status
, errmsg
);
73 static void tm6000_urb_received(struct urb
*urb
)
76 struct tm6000_core
*dev
= urb
->context
;
78 switch (urb
->status
) {
87 print_err_status(dev
, 0, urb
->status
);
90 if (urb
->actual_length
> 0)
91 dvb_dmx_swfilter(&dev
->dvb
->demux
, urb
->transfer_buffer
,
94 if (dev
->dvb
->streams
> 0) {
95 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
97 printk(KERN_ERR
"tm6000: error %s\n", __func__
);
98 kfree(urb
->transfer_buffer
);
100 dev
->dvb
->bulk_urb
= NULL
;
105 static int tm6000_start_stream(struct tm6000_core
*dev
)
108 unsigned int pipe
, size
;
109 struct tm6000_dvb
*dvb
= dev
->dvb
;
111 printk(KERN_INFO
"tm6000: got start stream request %s\n", __func__
);
113 if (dev
->mode
!= TM6000_MODE_DIGITAL
) {
114 tm6000_init_digital_mode(dev
);
115 dev
->mode
= TM6000_MODE_DIGITAL
;
118 dvb
->bulk_urb
= usb_alloc_urb(0, GFP_KERNEL
);
122 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->bulk_in
.endp
->desc
.bEndpointAddress
123 & USB_ENDPOINT_NUMBER_MASK
);
125 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
126 size
= size
* 15; /* 512 x 8 or 12 or 15 */
128 dvb
->bulk_urb
->transfer_buffer
= kzalloc(size
, GFP_KERNEL
);
129 if (!dvb
->bulk_urb
->transfer_buffer
) {
130 usb_free_urb(dvb
->bulk_urb
);
131 dvb
->bulk_urb
= NULL
;
135 usb_fill_bulk_urb(dvb
->bulk_urb
, dev
->udev
, pipe
,
136 dvb
->bulk_urb
->transfer_buffer
,
138 tm6000_urb_received
, dev
);
140 ret
= usb_clear_halt(dev
->udev
, pipe
);
142 printk(KERN_ERR
"tm6000: error %i in %s during pipe reset\n",
146 printk(KERN_ERR
"tm6000: pipe reset\n");
148 /* mutex_lock(&tm6000_driver.open_close_mutex); */
149 ret
= usb_submit_urb(dvb
->bulk_urb
, GFP_ATOMIC
);
151 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
153 printk(KERN_ERR
"tm6000: submit of urb failed (error=%i)\n",
156 kfree(dvb
->bulk_urb
->transfer_buffer
);
157 usb_free_urb(dvb
->bulk_urb
);
158 dvb
->bulk_urb
= NULL
;
165 static void tm6000_stop_stream(struct tm6000_core
*dev
)
167 struct tm6000_dvb
*dvb
= dev
->dvb
;
170 printk(KERN_INFO
"urb killing\n");
171 usb_kill_urb(dvb
->bulk_urb
);
172 printk(KERN_INFO
"urb buffer free\n");
173 kfree(dvb
->bulk_urb
->transfer_buffer
);
174 usb_free_urb(dvb
->bulk_urb
);
175 dvb
->bulk_urb
= NULL
;
179 static int tm6000_start_feed(struct dvb_demux_feed
*feed
)
181 struct dvb_demux
*demux
= feed
->demux
;
182 struct tm6000_core
*dev
= demux
->priv
;
183 struct tm6000_dvb
*dvb
= dev
->dvb
;
184 printk(KERN_INFO
"tm6000: got start feed request %s\n", __func__
);
186 mutex_lock(&dvb
->mutex
);
187 if (dvb
->streams
== 0) {
189 /* mutex_init(&tm6000_dev->streming_mutex); */
190 tm6000_start_stream(dev
);
193 mutex_unlock(&dvb
->mutex
);
198 static int tm6000_stop_feed(struct dvb_demux_feed
*feed
)
200 struct dvb_demux
*demux
= feed
->demux
;
201 struct tm6000_core
*dev
= demux
->priv
;
202 struct tm6000_dvb
*dvb
= dev
->dvb
;
204 printk(KERN_INFO
"tm6000: got stop feed request %s\n", __func__
);
206 mutex_lock(&dvb
->mutex
);
208 printk(KERN_INFO
"stream %#x\n", dvb
->streams
);
210 if (dvb
->streams
== 0) {
211 printk(KERN_INFO
"stop stream\n");
212 tm6000_stop_stream(dev
);
213 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
215 mutex_unlock(&dvb
->mutex
);
216 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
221 static int tm6000_dvb_attach_frontend(struct tm6000_core
*dev
)
223 struct tm6000_dvb
*dvb
= dev
->dvb
;
225 if (dev
->caps
.has_zl10353
) {
226 struct zl10353_config config
= {
227 .demod_address
= dev
->demod_addr
,
231 .disable_i2c_gate_ctrl
= 1,
234 dvb
->frontend
= dvb_attach(zl10353_attach
, &config
,
237 printk(KERN_ERR
"tm6000: no frontend defined for the device!\n");
241 return (!dvb
->frontend
) ? -1 : 0;
244 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
246 static int register_dvb(struct tm6000_core
*dev
)
249 struct tm6000_dvb
*dvb
= dev
->dvb
;
251 mutex_init(&dvb
->mutex
);
255 /* attach the frontend */
256 ret
= tm6000_dvb_attach_frontend(dev
);
258 printk(KERN_ERR
"tm6000: couldn't attach the frontend!\n");
262 ret
= dvb_register_adapter(&dvb
->adapter
, "Trident TVMaster 6000 DVB-T",
263 THIS_MODULE
, &dev
->udev
->dev
, adapter_nr
);
265 pr_err("tm6000: couldn't register the adapter!\n");
269 dvb
->adapter
.priv
= dev
;
272 switch (dev
->tuner_type
) {
274 struct xc2028_config cfg
= {
275 .i2c_adap
= &dev
->i2c_adap
,
276 .i2c_addr
= dev
->tuner_addr
,
279 dvb
->frontend
->callback
= tm6000_tuner_callback
;
280 ret
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
283 "tm6000: couldn't register frontend\n");
287 if (!dvb_attach(xc2028_attach
, dvb
->frontend
, &cfg
)) {
288 printk(KERN_ERR
"tm6000: couldn't register frontend (xc3028)\n");
292 printk(KERN_INFO
"tm6000: XC2028/3028 asked to be attached to frontend!\n");
296 struct xc5000_config cfg
= {
297 .i2c_address
= dev
->tuner_addr
,
300 dvb
->frontend
->callback
= tm6000_xc5000_callback
;
301 ret
= dvb_register_frontend(&dvb
->adapter
, dvb
->frontend
);
304 "tm6000: couldn't register frontend\n");
308 if (!dvb_attach(xc5000_attach
, dvb
->frontend
, &dev
->i2c_adap
, &cfg
)) {
309 printk(KERN_ERR
"tm6000: couldn't register frontend (xc5000)\n");
313 printk(KERN_INFO
"tm6000: XC5000 asked to be attached to frontend!\n");
318 printk(KERN_ERR
"tm6000: no frontend found\n");
320 dvb
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
| DMX_SECTION_FILTERING
321 | DMX_MEMORY_BASED_FILTERING
;
322 dvb
->demux
.priv
= dev
;
323 dvb
->demux
.filternum
= 8;
324 dvb
->demux
.feednum
= 8;
325 dvb
->demux
.start_feed
= tm6000_start_feed
;
326 dvb
->demux
.stop_feed
= tm6000_stop_feed
;
327 dvb
->demux
.write_to_decoder
= NULL
;
328 ret
= dvb_dmx_init(&dvb
->demux
);
330 printk(KERN_ERR
"tm6000: dvb_dmx_init failed (errno = %d)\n", ret
);
334 dvb
->dmxdev
.filternum
= dev
->dvb
->demux
.filternum
;
335 dvb
->dmxdev
.demux
= &dev
->dvb
->demux
.dmx
;
336 dvb
->dmxdev
.capabilities
= 0;
338 ret
= dvb_dmxdev_init(&dvb
->dmxdev
, &dvb
->adapter
);
340 printk(KERN_ERR
"tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret
);
347 dvb_dmx_release(&dvb
->demux
);
350 dvb_unregister_frontend(dvb
->frontend
);
351 dvb_frontend_detach(dvb
->frontend
);
354 dvb_unregister_adapter(&dvb
->adapter
);
359 static void unregister_dvb(struct tm6000_core
*dev
)
361 struct tm6000_dvb
*dvb
= dev
->dvb
;
364 struct urb
*bulk_urb
= dvb
->bulk_urb
;
366 kfree(bulk_urb
->transfer_buffer
);
367 bulk_urb
->transfer_buffer
= NULL
;
368 usb_unlink_urb(bulk_urb
);
369 usb_free_urb(bulk_urb
);
372 /* mutex_lock(&tm6000_driver.open_close_mutex); */
374 dvb_unregister_frontend(dvb
->frontend
);
375 dvb_frontend_detach(dvb
->frontend
);
378 dvb_dmxdev_release(&dvb
->dmxdev
);
379 dvb_dmx_release(&dvb
->demux
);
380 dvb_unregister_adapter(&dvb
->adapter
);
381 mutex_destroy(&dvb
->mutex
);
382 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
385 static int dvb_init(struct tm6000_core
*dev
)
387 struct tm6000_dvb
*dvb
;
393 if (!dev
->caps
.has_dvb
)
396 if (dev
->udev
->speed
== USB_SPEED_FULL
) {
397 printk(KERN_INFO
"This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)\n");
401 dvb
= kzalloc(sizeof(struct tm6000_dvb
), GFP_KERNEL
);
407 rc
= register_dvb(dev
);
417 static int dvb_fini(struct tm6000_core
*dev
)
422 if (!dev
->caps
.has_dvb
)
434 static struct tm6000_ops dvb_ops
= {
436 .name
= "TM6000 dvb Extension",
441 static int __init
tm6000_dvb_register(void)
443 return tm6000_register_extension(&dvb_ops
);
446 static void __exit
tm6000_dvb_unregister(void)
448 tm6000_unregister_extension(&dvb_ops
);
451 module_init(tm6000_dvb_register
);
452 module_exit(tm6000_dvb_unregister
);