treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / usb / tm6000 / tm6000-dvb.c
blob19c90fa9e443d9b472cb2f3b2ea6818aff3942b7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
6 */
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/usb.h>
12 #include "tm6000.h"
13 #include "tm6000-regs.h"
15 #include "zl10353.h"
17 #include <media/tuner.h>
19 #include "tuner-xc2028.h"
20 #include "xc5000.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}");
28 static int debug;
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";
38 switch (status) {
39 case -ENOENT:
40 errmsg = "unlinked synchronously";
41 break;
42 case -ECONNRESET:
43 errmsg = "unlinked asynchronously";
44 break;
45 case -ENOSR:
46 errmsg = "Buffer error (overrun)";
47 break;
48 case -EPIPE:
49 errmsg = "Stalled (device not responding)";
50 break;
51 case -EOVERFLOW:
52 errmsg = "Babble (bad cable?)";
53 break;
54 case -EPROTO:
55 errmsg = "Bit-stuff error (bad cable?)";
56 break;
57 case -EILSEQ:
58 errmsg = "CRC/Timeout (could be anything)";
59 break;
60 case -ETIME:
61 errmsg = "Device does not respond";
62 break;
64 if (packet < 0) {
65 dprintk(dev, 1, "URB status %d [%s].\n",
66 status, errmsg);
67 } else {
68 dprintk(dev, 1, "URB packet %d, status %d [%s].\n",
69 packet, status, errmsg);
73 static void tm6000_urb_received(struct urb *urb)
75 int ret;
76 struct tm6000_core *dev = urb->context;
78 switch (urb->status) {
79 case 0:
80 case -ETIMEDOUT:
81 break;
82 case -ENOENT:
83 case -ECONNRESET:
84 case -ESHUTDOWN:
85 return;
86 default:
87 print_err_status(dev, 0, urb->status);
90 if (urb->actual_length > 0)
91 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
92 urb->actual_length);
94 if (dev->dvb->streams > 0) {
95 ret = usb_submit_urb(urb, GFP_ATOMIC);
96 if (ret < 0) {
97 printk(KERN_ERR "tm6000: error %s\n", __func__);
98 kfree(urb->transfer_buffer);
99 usb_free_urb(urb);
100 dev->dvb->bulk_urb = NULL;
105 static int tm6000_start_stream(struct tm6000_core *dev)
107 int ret;
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);
119 if (!dvb->bulk_urb)
120 return -ENOMEM;
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;
132 return -ENOMEM;
135 usb_fill_bulk_urb(dvb->bulk_urb, dev->udev, pipe,
136 dvb->bulk_urb->transfer_buffer,
137 size,
138 tm6000_urb_received, dev);
140 ret = usb_clear_halt(dev->udev, pipe);
141 if (ret < 0) {
142 printk(KERN_ERR "tm6000: error %i in %s during pipe reset\n",
143 ret, __func__);
144 return ret;
145 } else
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); */
152 if (ret) {
153 printk(KERN_ERR "tm6000: submit of urb failed (error=%i)\n",
154 ret);
156 kfree(dvb->bulk_urb->transfer_buffer);
157 usb_free_urb(dvb->bulk_urb);
158 dvb->bulk_urb = NULL;
159 return ret;
162 return 0;
165 static void tm6000_stop_stream(struct tm6000_core *dev)
167 struct tm6000_dvb *dvb = dev->dvb;
169 if (dvb->bulk_urb) {
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) {
188 dvb->streams = 1;
189 /* mutex_init(&tm6000_dev->streming_mutex); */
190 tm6000_start_stream(dev);
191 } else
192 ++(dvb->streams);
193 mutex_unlock(&dvb->mutex);
195 return 0;
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);
209 --(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); */
218 return 0;
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,
228 .no_tuner = 1,
229 .parallel_ts = 1,
230 .if2 = 45700,
231 .disable_i2c_gate_ctrl = 1,
234 dvb->frontend = dvb_attach(zl10353_attach, &config,
235 &dev->i2c_adap);
236 } else {
237 printk(KERN_ERR "tm6000: no frontend defined for the device!\n");
238 return -1;
241 return (!dvb->frontend) ? -1 : 0;
244 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
246 static int register_dvb(struct tm6000_core *dev)
248 int ret = -1;
249 struct tm6000_dvb *dvb = dev->dvb;
251 mutex_init(&dvb->mutex);
253 dvb->streams = 0;
255 /* attach the frontend */
256 ret = tm6000_dvb_attach_frontend(dev);
257 if (ret < 0) {
258 printk(KERN_ERR "tm6000: couldn't attach the frontend!\n");
259 goto err;
262 ret = dvb_register_adapter(&dvb->adapter, "Trident TVMaster 6000 DVB-T",
263 THIS_MODULE, &dev->udev->dev, adapter_nr);
264 if (ret < 0) {
265 pr_err("tm6000: couldn't register the adapter!\n");
266 goto err;
269 dvb->adapter.priv = dev;
271 if (dvb->frontend) {
272 switch (dev->tuner_type) {
273 case TUNER_XC2028: {
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);
281 if (ret < 0) {
282 printk(KERN_ERR
283 "tm6000: couldn't register frontend\n");
284 goto adapter_err;
287 if (!dvb_attach(xc2028_attach, dvb->frontend, &cfg)) {
288 printk(KERN_ERR "tm6000: couldn't register frontend (xc3028)\n");
289 ret = -EINVAL;
290 goto frontend_err;
292 printk(KERN_INFO "tm6000: XC2028/3028 asked to be attached to frontend!\n");
293 break;
295 case TUNER_XC5000: {
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);
302 if (ret < 0) {
303 printk(KERN_ERR
304 "tm6000: couldn't register frontend\n");
305 goto adapter_err;
308 if (!dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap, &cfg)) {
309 printk(KERN_ERR "tm6000: couldn't register frontend (xc5000)\n");
310 ret = -EINVAL;
311 goto frontend_err;
313 printk(KERN_INFO "tm6000: XC5000 asked to be attached to frontend!\n");
314 break;
317 } else
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);
329 if (ret < 0) {
330 printk(KERN_ERR "tm6000: dvb_dmx_init failed (errno = %d)\n", ret);
331 goto frontend_err;
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);
339 if (ret < 0) {
340 printk(KERN_ERR "tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret);
341 goto dvb_dmx_err;
344 return 0;
346 dvb_dmx_err:
347 dvb_dmx_release(&dvb->demux);
348 frontend_err:
349 if (dvb->frontend) {
350 dvb_unregister_frontend(dvb->frontend);
351 dvb_frontend_detach(dvb->frontend);
353 adapter_err:
354 dvb_unregister_adapter(&dvb->adapter);
355 err:
356 return ret;
359 static void unregister_dvb(struct tm6000_core *dev)
361 struct tm6000_dvb *dvb = dev->dvb;
363 if (dvb->bulk_urb) {
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); */
373 if (dvb->frontend) {
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;
388 int rc;
390 if (!dev)
391 return 0;
393 if (!dev->caps.has_dvb)
394 return 0;
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");
398 return 0;
401 dvb = kzalloc(sizeof(struct tm6000_dvb), GFP_KERNEL);
402 if (!dvb)
403 return -ENOMEM;
405 dev->dvb = dvb;
407 rc = register_dvb(dev);
408 if (rc < 0) {
409 kfree(dvb);
410 dev->dvb = NULL;
411 return 0;
414 return 0;
417 static int dvb_fini(struct tm6000_core *dev)
419 if (!dev)
420 return 0;
422 if (!dev->caps.has_dvb)
423 return 0;
425 if (dev->dvb) {
426 unregister_dvb(dev);
427 kfree(dev->dvb);
428 dev->dvb = NULL;
431 return 0;
434 static struct tm6000_ops dvb_ops = {
435 .type = TM6000_DVB,
436 .name = "TM6000 dvb Extension",
437 .init = dvb_init,
438 .fini = dvb_fini,
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);