sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / usb / tm6000 / tm6000-dvb.c
blob70dbaec1219e11e739b08467e575b5d6f6846153
1 /*
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>
24 #include "tm6000.h"
25 #include "tm6000-regs.h"
27 #include "zl10353.h"
29 #include <media/tuner.h>
31 #include "tuner-xc2028.h"
32 #include "xc5000.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},{{Trident, tm6000},{{Trident, tm6010}");
40 static int debug;
42 module_param(debug, int, 0644);
43 MODULE_PARM_DESC(debug, "enable debug message");
45 static inline void print_err_status(struct tm6000_core *dev,
46 int packet, int status)
48 char *errmsg = "Unknown";
50 switch (status) {
51 case -ENOENT:
52 errmsg = "unlinked synchronuously";
53 break;
54 case -ECONNRESET:
55 errmsg = "unlinked asynchronuously";
56 break;
57 case -ENOSR:
58 errmsg = "Buffer error (overrun)";
59 break;
60 case -EPIPE:
61 errmsg = "Stalled (device not responding)";
62 break;
63 case -EOVERFLOW:
64 errmsg = "Babble (bad cable?)";
65 break;
66 case -EPROTO:
67 errmsg = "Bit-stuff error (bad cable?)";
68 break;
69 case -EILSEQ:
70 errmsg = "CRC/Timeout (could be anything)";
71 break;
72 case -ETIME:
73 errmsg = "Device does not respond";
74 break;
76 if (packet < 0) {
77 dprintk(dev, 1, "URB status %d [%s].\n",
78 status, errmsg);
79 } else {
80 dprintk(dev, 1, "URB packet %d, status %d [%s].\n",
81 packet, status, errmsg);
85 static void tm6000_urb_received(struct urb *urb)
87 int ret;
88 struct tm6000_core *dev = urb->context;
90 switch (urb->status) {
91 case 0:
92 case -ETIMEDOUT:
93 break;
94 case -ENOENT:
95 case -ECONNRESET:
96 case -ESHUTDOWN:
97 return;
98 default:
99 print_err_status(dev, 0, urb->status);
102 if (urb->actual_length > 0)
103 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
104 urb->actual_length);
106 if (dev->dvb->streams > 0) {
107 ret = usb_submit_urb(urb, GFP_ATOMIC);
108 if (ret < 0) {
109 printk(KERN_ERR "tm6000: error %s\n", __func__);
110 kfree(urb->transfer_buffer);
111 usb_free_urb(urb);
116 static int tm6000_start_stream(struct tm6000_core *dev)
118 int ret;
119 unsigned int pipe, size;
120 struct tm6000_dvb *dvb = dev->dvb;
122 printk(KERN_INFO "tm6000: got start stream request %s\n", __func__);
124 if (dev->mode != TM6000_MODE_DIGITAL) {
125 tm6000_init_digital_mode(dev);
126 dev->mode = TM6000_MODE_DIGITAL;
129 dvb->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
130 if (dvb->bulk_urb == NULL)
131 return -ENOMEM;
133 pipe = usb_rcvbulkpipe(dev->udev, dev->bulk_in.endp->desc.bEndpointAddress
134 & USB_ENDPOINT_NUMBER_MASK);
136 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
137 size = size * 15; /* 512 x 8 or 12 or 15 */
139 dvb->bulk_urb->transfer_buffer = kzalloc(size, GFP_KERNEL);
140 if (dvb->bulk_urb->transfer_buffer == NULL) {
141 usb_free_urb(dvb->bulk_urb);
142 printk(KERN_ERR "tm6000: couldn't allocate transfer buffer!\n");
143 return -ENOMEM;
146 usb_fill_bulk_urb(dvb->bulk_urb, dev->udev, pipe,
147 dvb->bulk_urb->transfer_buffer,
148 size,
149 tm6000_urb_received, dev);
151 ret = usb_clear_halt(dev->udev, pipe);
152 if (ret < 0) {
153 printk(KERN_ERR "tm6000: error %i in %s during pipe reset\n",
154 ret, __func__);
155 return ret;
156 } else
157 printk(KERN_ERR "tm6000: pipe resetted\n");
159 /* mutex_lock(&tm6000_driver.open_close_mutex); */
160 ret = usb_submit_urb(dvb->bulk_urb, GFP_ATOMIC);
162 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
163 if (ret) {
164 printk(KERN_ERR "tm6000: submit of urb failed (error=%i)\n",
165 ret);
167 kfree(dvb->bulk_urb->transfer_buffer);
168 usb_free_urb(dvb->bulk_urb);
169 return ret;
172 return 0;
175 static void tm6000_stop_stream(struct tm6000_core *dev)
177 struct tm6000_dvb *dvb = dev->dvb;
179 if (dvb->bulk_urb) {
180 printk(KERN_INFO "urb killing\n");
181 usb_kill_urb(dvb->bulk_urb);
182 printk(KERN_INFO "urb buffer free\n");
183 kfree(dvb->bulk_urb->transfer_buffer);
184 usb_free_urb(dvb->bulk_urb);
185 dvb->bulk_urb = NULL;
189 static int tm6000_start_feed(struct dvb_demux_feed *feed)
191 struct dvb_demux *demux = feed->demux;
192 struct tm6000_core *dev = demux->priv;
193 struct tm6000_dvb *dvb = dev->dvb;
194 printk(KERN_INFO "tm6000: got start feed request %s\n", __func__);
196 mutex_lock(&dvb->mutex);
197 if (dvb->streams == 0) {
198 dvb->streams = 1;
199 /* mutex_init(&tm6000_dev->streming_mutex); */
200 tm6000_start_stream(dev);
201 } else
202 ++(dvb->streams);
203 mutex_unlock(&dvb->mutex);
205 return 0;
208 static int tm6000_stop_feed(struct dvb_demux_feed *feed)
210 struct dvb_demux *demux = feed->demux;
211 struct tm6000_core *dev = demux->priv;
212 struct tm6000_dvb *dvb = dev->dvb;
214 printk(KERN_INFO "tm6000: got stop feed request %s\n", __func__);
216 mutex_lock(&dvb->mutex);
218 printk(KERN_INFO "stream %#x\n", dvb->streams);
219 --(dvb->streams);
220 if (dvb->streams == 0) {
221 printk(KERN_INFO "stop stream\n");
222 tm6000_stop_stream(dev);
223 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
225 mutex_unlock(&dvb->mutex);
226 /* mutex_destroy(&tm6000_dev->streaming_mutex); */
228 return 0;
231 static int tm6000_dvb_attach_frontend(struct tm6000_core *dev)
233 struct tm6000_dvb *dvb = dev->dvb;
235 if (dev->caps.has_zl10353) {
236 struct zl10353_config config = {
237 .demod_address = dev->demod_addr,
238 .no_tuner = 1,
239 .parallel_ts = 1,
240 .if2 = 45700,
241 .disable_i2c_gate_ctrl = 1,
244 dvb->frontend = dvb_attach(zl10353_attach, &config,
245 &dev->i2c_adap);
246 } else {
247 printk(KERN_ERR "tm6000: no frontend defined for the device!\n");
248 return -1;
251 return (!dvb->frontend) ? -1 : 0;
254 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
256 static int register_dvb(struct tm6000_core *dev)
258 int ret = -1;
259 struct tm6000_dvb *dvb = dev->dvb;
261 mutex_init(&dvb->mutex);
263 dvb->streams = 0;
265 /* attach the frontend */
266 ret = tm6000_dvb_attach_frontend(dev);
267 if (ret < 0) {
268 printk(KERN_ERR "tm6000: couldn't attach the frontend!\n");
269 goto err;
272 ret = dvb_register_adapter(&dvb->adapter, "Trident TVMaster 6000 DVB-T",
273 THIS_MODULE, &dev->udev->dev, adapter_nr);
274 dvb->adapter.priv = dev;
276 if (dvb->frontend) {
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,
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;
292 if (!dvb_attach(xc2028_attach, dvb->frontend, &cfg)) {
293 printk(KERN_ERR "tm6000: couldn't register frontend (xc3028)\n");
294 ret = -EINVAL;
295 goto frontend_err;
297 printk(KERN_INFO "tm6000: XC2028/3028 asked to be attached to frontend!\n");
298 break;
300 case TUNER_XC5000: {
301 struct xc5000_config cfg = {
302 .i2c_address = dev->tuner_addr,
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;
313 if (!dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap, &cfg)) {
314 printk(KERN_ERR "tm6000: couldn't register frontend (xc5000)\n");
315 ret = -EINVAL;
316 goto frontend_err;
318 printk(KERN_INFO "tm6000: XC5000 asked to be attached to frontend!\n");
319 break;
322 } else
323 printk(KERN_ERR "tm6000: no frontend found\n");
325 dvb->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING
326 | DMX_MEMORY_BASED_FILTERING;
327 dvb->demux.priv = dev;
328 dvb->demux.filternum = 8;
329 dvb->demux.feednum = 8;
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);
334 if (ret < 0) {
335 printk(KERN_ERR "tm6000: dvb_dmx_init failed (errno = %d)\n", ret);
336 goto frontend_err;
339 dvb->dmxdev.filternum = dev->dvb->demux.filternum;
340 dvb->dmxdev.demux = &dev->dvb->demux.dmx;
341 dvb->dmxdev.capabilities = 0;
343 ret = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
344 if (ret < 0) {
345 printk(KERN_ERR "tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret);
346 goto dvb_dmx_err;
349 return 0;
351 dvb_dmx_err:
352 dvb_dmx_release(&dvb->demux);
353 frontend_err:
354 if (dvb->frontend) {
355 dvb_unregister_frontend(dvb->frontend);
356 dvb_frontend_detach(dvb->frontend);
358 adapter_err:
359 dvb_unregister_adapter(&dvb->adapter);
360 err:
361 return ret;
364 static void unregister_dvb(struct tm6000_core *dev)
366 struct tm6000_dvb *dvb = dev->dvb;
368 if (dvb->bulk_urb != NULL) {
369 struct urb *bulk_urb = dvb->bulk_urb;
371 kfree(bulk_urb->transfer_buffer);
372 bulk_urb->transfer_buffer = NULL;
373 usb_unlink_urb(bulk_urb);
374 usb_free_urb(bulk_urb);
377 /* mutex_lock(&tm6000_driver.open_close_mutex); */
378 if (dvb->frontend) {
379 dvb_unregister_frontend(dvb->frontend);
380 dvb_frontend_detach(dvb->frontend);
383 dvb_dmxdev_release(&dvb->dmxdev);
384 dvb_dmx_release(&dvb->demux);
385 dvb_unregister_adapter(&dvb->adapter);
386 mutex_destroy(&dvb->mutex);
387 /* mutex_unlock(&tm6000_driver.open_close_mutex); */
390 static int dvb_init(struct tm6000_core *dev)
392 struct tm6000_dvb *dvb;
393 int rc;
395 if (!dev)
396 return 0;
398 if (!dev->caps.has_dvb)
399 return 0;
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;
406 dvb = kzalloc(sizeof(struct tm6000_dvb), GFP_KERNEL);
407 if (!dvb) {
408 printk(KERN_INFO "Cannot allocate memory\n");
409 return -ENOMEM;
412 dev->dvb = dvb;
414 rc = register_dvb(dev);
415 if (rc < 0) {
416 kfree(dvb);
417 dev->dvb = NULL;
418 return 0;
421 return 0;
424 static int dvb_fini(struct tm6000_core *dev)
426 if (!dev)
427 return 0;
429 if (!dev->caps.has_dvb)
430 return 0;
432 if (dev->dvb) {
433 unregister_dvb(dev);
434 kfree(dev->dvb);
435 dev->dvb = NULL;
438 return 0;
441 static struct tm6000_ops dvb_ops = {
442 .type = TM6000_DVB,
443 .name = "TM6000 dvb Extension",
444 .init = dvb_init,
445 .fini = dvb_fini,
448 static int __init tm6000_dvb_register(void)
450 return tm6000_register_extension(&dvb_ops);
453 static void __exit tm6000_dvb_unregister(void)
455 tm6000_unregister_extension(&dvb_ops);
458 module_init(tm6000_dvb_register);
459 module_exit(tm6000_dvb_unregister);