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