Merge remote-tracking branch 'mailbox/mailbox-for-next'
[deliverable/linux.git] / drivers / media / usb / tm6000 / tm6000-dvb.c
CommitLineData
3169c9b2 1/*
d0058645
RP
2 * tm6000-dvb.c - dvb-t support for TM5600/TM6000/TM6010 USB video capture devices
3 *
4 * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
5 *
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
9 *
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.
14 *
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.
3169c9b2
ML
18 */
19
08e60ba0 20#include <linux/kernel.h>
4ef09889 21#include <linux/slab.h>
3169c9b2
ML
22#include <linux/usb.h>
23
24#include "tm6000.h"
25#include "tm6000-regs.h"
26
3169c9b2
ML
27#include "zl10353.h"
28
29#include <media/tuner.h>
30
5b74c2c7 31#include "tuner-xc2028.h"
7e2d9820 32#include "xc5000.h"
5b74c2c7 33
cee3926f 34MODULE_DESCRIPTION("DVB driver extension module for tm5600/6000/6010 based TV cards");
37e59f87 35MODULE_AUTHOR("Mauro Carvalho Chehab");
cee3926f
SR
36MODULE_LICENSE("GPL");
37
38MODULE_SUPPORTED_DEVICE("{{Trident, tm5600},"
39 "{{Trident, tm6000},"
40 "{{Trident, tm6010}");
41
42static int debug;
43
44module_param(debug, int, 0644);
45MODULE_PARM_DESC(debug, "enable debug message");
46
52e0a72a
TT
47static inline void print_err_status(struct tm6000_core *dev,
48 int packet, int status)
08e60ba0
SR
49{
50 char *errmsg = "Unknown";
51
52e0a72a 52 switch (status) {
08e60ba0
SR
53 case -ENOENT:
54 errmsg = "unlinked synchronuously";
55 break;
56 case -ECONNRESET:
57 errmsg = "unlinked asynchronuously";
58 break;
59 case -ENOSR:
60 errmsg = "Buffer error (overrun)";
61 break;
62 case -EPIPE:
63 errmsg = "Stalled (device not responding)";
64 break;
65 case -EOVERFLOW:
66 errmsg = "Babble (bad cable?)";
67 break;
68 case -EPROTO:
69 errmsg = "Bit-stuff error (bad cable?)";
70 break;
71 case -EILSEQ:
72 errmsg = "CRC/Timeout (could be anything)";
73 break;
74 case -ETIME:
75 errmsg = "Device does not respond";
76 break;
77 }
52e0a72a 78 if (packet < 0) {
08e60ba0
SR
79 dprintk(dev, 1, "URB status %d [%s].\n",
80 status, errmsg);
81 } else {
82 dprintk(dev, 1, "URB packet %d, status %d [%s].\n",
83 packet, status, errmsg);
84 }
85}
86
3169c9b2
ML
87static void tm6000_urb_received(struct urb *urb)
88{
89 int ret;
52e0a72a 90 struct tm6000_core *dev = urb->context;
3169c9b2 91
de2a20ba
SR
92 switch (urb->status) {
93 case 0:
94 case -ETIMEDOUT:
95 break;
96 case -ENOENT:
97 case -ECONNRESET:
98 case -ESHUTDOWN:
99 return;
100 default:
52e0a72a 101 print_err_status(dev, 0, urb->status);
de2a20ba
SR
102 }
103
104 if (urb->actual_length > 0)
3169c9b2
ML
105 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
106 urb->actual_length);
3169c9b2 107
52e0a72a 108 if (dev->dvb->streams > 0) {
3169c9b2 109 ret = usb_submit_urb(urb, GFP_ATOMIC);
52e0a72a 110 if (ret < 0) {
94b27661 111 printk(KERN_ERR "tm6000: error %s\n", __func__);
3169c9b2
ML
112 kfree(urb->transfer_buffer);
113 usb_free_urb(urb);
114 }
115 }
116}
117
3d1a51db 118static int tm6000_start_stream(struct tm6000_core *dev)
3169c9b2
ML
119{
120 int ret;
08e60ba0 121 unsigned int pipe, size;
3169c9b2
ML
122 struct tm6000_dvb *dvb = dev->dvb;
123
94b27661 124 printk(KERN_INFO "tm6000: got start stream request %s\n", __func__);
3169c9b2 125
dcf5d3aa
SR
126 if (dev->mode != TM6000_MODE_DIGITAL) {
127 tm6000_init_digital_mode(dev);
128 dev->mode = TM6000_MODE_DIGITAL;
129 }
3169c9b2 130
3169c9b2 131 dvb->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
b7eca8d4 132 if (dvb->bulk_urb == NULL)
3169c9b2 133 return -ENOMEM;
3169c9b2 134
6ae635c4 135 pipe = usb_rcvbulkpipe(dev->udev, dev->bulk_in.endp->desc.bEndpointAddress
08e60ba0
SR
136 & USB_ENDPOINT_NUMBER_MASK);
137
138 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
139 size = size * 15; /* 512 x 8 or 12 or 15 */
3169c9b2 140
08e60ba0 141 dvb->bulk_urb->transfer_buffer = kzalloc(size, GFP_KERNEL);
52e0a72a 142 if (dvb->bulk_urb->transfer_buffer == NULL) {
3169c9b2
ML
143 usb_free_urb(dvb->bulk_urb);
144 printk(KERN_ERR "tm6000: couldn't allocate transfer buffer!\n");
145 return -ENOMEM;
146 }
147
3169c9b2
ML
148 usb_fill_bulk_urb(dvb->bulk_urb, dev->udev, pipe,
149 dvb->bulk_urb->transfer_buffer,
08e60ba0 150 size,
3169c9b2 151 tm6000_urb_received, dev);
4386136d 152
3169c9b2 153 ret = usb_clear_halt(dev->udev, pipe);
52e0a72a
TT
154 if (ret < 0) {
155 printk(KERN_ERR "tm6000: error %i in %s during pipe reset\n",
94b27661 156 ret, __func__);
4386136d 157 return ret;
52e0a72a 158 } else
3169c9b2 159 printk(KERN_ERR "tm6000: pipe resetted\n");
3169c9b2 160
08e60ba0 161/* mutex_lock(&tm6000_driver.open_close_mutex); */
de2a20ba 162 ret = usb_submit_urb(dvb->bulk_urb, GFP_ATOMIC);
3169c9b2 163
08e60ba0 164/* mutex_unlock(&tm6000_driver.open_close_mutex); */
3169c9b2 165 if (ret) {
52e0a72a
TT
166 printk(KERN_ERR "tm6000: submit of urb failed (error=%i)\n",
167 ret);
3169c9b2
ML
168
169 kfree(dvb->bulk_urb->transfer_buffer);
170 usb_free_urb(dvb->bulk_urb);
171 return ret;
172 }
173
174 return 0;
175}
176
3d1a51db 177static void tm6000_stop_stream(struct tm6000_core *dev)
3169c9b2
ML
178{
179 struct tm6000_dvb *dvb = dev->dvb;
180
52e0a72a
TT
181 if (dvb->bulk_urb) {
182 printk(KERN_INFO "urb killing\n");
3169c9b2 183 usb_kill_urb(dvb->bulk_urb);
52e0a72a 184 printk(KERN_INFO "urb buffer free\n");
3169c9b2
ML
185 kfree(dvb->bulk_urb->transfer_buffer);
186 usb_free_urb(dvb->bulk_urb);
187 dvb->bulk_urb = NULL;
188 }
189}
190
3d1a51db 191static int tm6000_start_feed(struct dvb_demux_feed *feed)
3169c9b2
ML
192{
193 struct dvb_demux *demux = feed->demux;
194 struct tm6000_core *dev = demux->priv;
195 struct tm6000_dvb *dvb = dev->dvb;
94b27661 196 printk(KERN_INFO "tm6000: got start feed request %s\n", __func__);
3169c9b2
ML
197
198 mutex_lock(&dvb->mutex);
52e0a72a 199 if (dvb->streams == 0) {
3169c9b2 200 dvb->streams = 1;
08e60ba0 201/* mutex_init(&tm6000_dev->streming_mutex); */
3169c9b2 202 tm6000_start_stream(dev);
52e0a72a 203 } else
3169c9b2 204 ++(dvb->streams);
3169c9b2
ML
205 mutex_unlock(&dvb->mutex);
206
207 return 0;
208}
209
3d1a51db 210static int tm6000_stop_feed(struct dvb_demux_feed *feed)
52e0a72a 211{
3169c9b2
ML
212 struct dvb_demux *demux = feed->demux;
213 struct tm6000_core *dev = demux->priv;
214 struct tm6000_dvb *dvb = dev->dvb;
215
94b27661 216 printk(KERN_INFO "tm6000: got stop feed request %s\n", __func__);
3169c9b2
ML
217
218 mutex_lock(&dvb->mutex);
3169c9b2 219
52e0a72a 220 printk(KERN_INFO "stream %#x\n", dvb->streams);
08e60ba0 221 --(dvb->streams);
52e0a72a
TT
222 if (dvb->streams == 0) {
223 printk(KERN_INFO "stop stream\n");
3169c9b2 224 tm6000_stop_stream(dev);
08e60ba0 225/* mutex_destroy(&tm6000_dev->streaming_mutex); */
3169c9b2
ML
226 }
227 mutex_unlock(&dvb->mutex);
08e60ba0 228/* mutex_destroy(&tm6000_dev->streaming_mutex); */
3169c9b2
ML
229
230 return 0;
231}
232
3d1a51db 233static int tm6000_dvb_attach_frontend(struct tm6000_core *dev)
3169c9b2
ML
234{
235 struct tm6000_dvb *dvb = dev->dvb;
236
52e0a72a
TT
237 if (dev->caps.has_zl10353) {
238 struct zl10353_config config = {
239 .demod_address = dev->demod_addr,
3169c9b2 240 .no_tuner = 1,
08e60ba0
SR
241 .parallel_ts = 1,
242 .if2 = 45700,
243 .disable_i2c_gate_ctrl = 1,
3169c9b2
ML
244 };
245
89eeda67 246 dvb->frontend = dvb_attach(zl10353_attach, &config,
3169c9b2 247 &dev->i2c_adap);
52e0a72a 248 } else {
3169c9b2
ML
249 printk(KERN_ERR "tm6000: no frontend defined for the device!\n");
250 return -1;
251 }
252
70bfae5a 253 return (!dvb->frontend) ? -1 : 0;
3169c9b2
ML
254}
255
1b4c5b1f
MM
256DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
257
3d1a51db 258static int register_dvb(struct tm6000_core *dev)
3169c9b2
ML
259{
260 int ret = -1;
261 struct tm6000_dvb *dvb = dev->dvb;
262
263 mutex_init(&dvb->mutex);
264
265 dvb->streams = 0;
266
267 /* attach the frontend */
268 ret = tm6000_dvb_attach_frontend(dev);
52e0a72a 269 if (ret < 0) {
3169c9b2 270 printk(KERN_ERR "tm6000: couldn't attach the frontend!\n");
70bfae5a 271 goto err;
3169c9b2
ML
272 }
273
274 ret = dvb_register_adapter(&dvb->adapter, "Trident TVMaster 6000 DVB-T",
52e0a72a 275 THIS_MODULE, &dev->udev->dev, adapter_nr);
3169c9b2
ML
276 dvb->adapter.priv = dev;
277
5b74c2c7 278 if (dvb->frontend) {
7e2d9820
SR
279 switch (dev->tuner_type) {
280 case TUNER_XC2028: {
281 struct xc2028_config cfg = {
282 .i2c_adap = &dev->i2c_adap,
283 .i2c_addr = dev->tuner_addr,
284 };
285
286 dvb->frontend->callback = tm6000_tuner_callback;
287 ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
288 if (ret < 0) {
289 printk(KERN_ERR
290 "tm6000: couldn't register frontend\n");
291 goto adapter_err;
292 }
293
294 if (!dvb_attach(xc2028_attach, dvb->frontend, &cfg)) {
295 printk(KERN_ERR "tm6000: couldn't register "
296 "frontend (xc3028)\n");
297 ret = -EINVAL;
298 goto frontend_err;
299 }
300 printk(KERN_INFO "tm6000: XC2028/3028 asked to be "
301 "attached to frontend!\n");
302 break;
303 }
304 case TUNER_XC5000: {
305 struct xc5000_config cfg = {
306 .i2c_address = dev->tuner_addr,
307 };
308
309 dvb->frontend->callback = tm6000_xc5000_callback;
310 ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
311 if (ret < 0) {
312 printk(KERN_ERR
313 "tm6000: couldn't register frontend\n");
314 goto adapter_err;
315 }
316
317 if (!dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap, &cfg)) {
318 printk(KERN_ERR "tm6000: couldn't register "
319 "frontend (xc5000)\n");
320 ret = -EINVAL;
321 goto frontend_err;
322 }
323 printk(KERN_INFO "tm6000: XC5000 asked to be "
324 "attached to frontend!\n");
325 break;
326 }
3169c9b2 327 }
52e0a72a 328 } else
5b74c2c7 329 printk(KERN_ERR "tm6000: no frontend found\n");
3169c9b2
ML
330
331 dvb->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING
332 | DMX_MEMORY_BASED_FILTERING;
333 dvb->demux.priv = dev;
38d75a79
SR
334 dvb->demux.filternum = 8;
335 dvb->demux.feednum = 8;
3169c9b2
ML
336 dvb->demux.start_feed = tm6000_start_feed;
337 dvb->demux.stop_feed = tm6000_stop_feed;
338 dvb->demux.write_to_decoder = NULL;
339 ret = dvb_dmx_init(&dvb->demux);
52e0a72a 340 if (ret < 0) {
45dbf0db 341 printk(KERN_ERR "tm6000: dvb_dmx_init failed (errno = %d)\n", ret);
3169c9b2
ML
342 goto frontend_err;
343 }
344
345 dvb->dmxdev.filternum = dev->dvb->demux.filternum;
346 dvb->dmxdev.demux = &dev->dvb->demux.dmx;
347 dvb->dmxdev.capabilities = 0;
348
349 ret = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
52e0a72a 350 if (ret < 0) {
45dbf0db 351 printk(KERN_ERR "tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret);
3169c9b2
ML
352 goto dvb_dmx_err;
353 }
354
355 return 0;
356
357dvb_dmx_err:
358 dvb_dmx_release(&dvb->demux);
359frontend_err:
52e0a72a 360 if (dvb->frontend) {
3169c9b2 361 dvb_unregister_frontend(dvb->frontend);
afca99a2 362 dvb_frontend_detach(dvb->frontend);
3169c9b2
ML
363 }
364adapter_err:
365 dvb_unregister_adapter(&dvb->adapter);
366err:
367 return ret;
368}
369
3d1a51db 370static void unregister_dvb(struct tm6000_core *dev)
3169c9b2
ML
371{
372 struct tm6000_dvb *dvb = dev->dvb;
373
52e0a72a 374 if (dvb->bulk_urb != NULL) {
3169c9b2
ML
375 struct urb *bulk_urb = dvb->bulk_urb;
376
377 kfree(bulk_urb->transfer_buffer);
378 bulk_urb->transfer_buffer = NULL;
379 usb_unlink_urb(bulk_urb);
380 usb_free_urb(bulk_urb);
381 }
382
08e60ba0 383/* mutex_lock(&tm6000_driver.open_close_mutex); */
52e0a72a 384 if (dvb->frontend) {
3169c9b2 385 dvb_unregister_frontend(dvb->frontend);
afca99a2 386 dvb_frontend_detach(dvb->frontend);
3169c9b2
ML
387 }
388
389 dvb_dmxdev_release(&dvb->dmxdev);
390 dvb_dmx_release(&dvb->demux);
391 dvb_unregister_adapter(&dvb->adapter);
392 mutex_destroy(&dvb->mutex);
08e60ba0 393/* mutex_unlock(&tm6000_driver.open_close_mutex); */
3169c9b2 394}
cee3926f
SR
395
396static int dvb_init(struct tm6000_core *dev)
397{
398 struct tm6000_dvb *dvb;
399 int rc;
400
401 if (!dev)
402 return 0;
403
404 if (!dev->caps.has_dvb)
405 return 0;
406
c81c0060 407 if (dev->udev->speed == USB_SPEED_FULL) {
408 printk(KERN_INFO "This USB2.0 device cannot be run on a USB1.1 port. (it lacks a hardware PID filter)\n");
409 return 0;
410 }
411
cee3926f
SR
412 dvb = kzalloc(sizeof(struct tm6000_dvb), GFP_KERNEL);
413 if (!dvb) {
414 printk(KERN_INFO "Cannot allocate memory\n");
415 return -ENOMEM;
416 }
417
418 dev->dvb = dvb;
419
420 rc = register_dvb(dev);
421 if (rc < 0) {
422 kfree(dvb);
423 dev->dvb = NULL;
424 return 0;
425 }
426
427 return 0;
428}
429
430static int dvb_fini(struct tm6000_core *dev)
431{
432 if (!dev)
433 return 0;
434
435 if (!dev->caps.has_dvb)
436 return 0;
437
438 if (dev->dvb) {
439 unregister_dvb(dev);
440 kfree(dev->dvb);
441 dev->dvb = NULL;
442 }
443
444 return 0;
445}
446
447static struct tm6000_ops dvb_ops = {
39e1256b 448 .type = TM6000_DVB,
cee3926f
SR
449 .name = "TM6000 dvb Extension",
450 .init = dvb_init,
451 .fini = dvb_fini,
452};
453
454static int __init tm6000_dvb_register(void)
455{
456 return tm6000_register_extension(&dvb_ops);
457}
458
459static void __exit tm6000_dvb_unregister(void)
460{
461 tm6000_unregister_extension(&dvb_ops);
462}
463
464module_init(tm6000_dvb_register);
465module_exit(tm6000_dvb_unregister);
This page took 0.528299 seconds and 5 git commands to generate.