blob: 261e66acbe46b7ea061d2c2be4bd3737b1f8cffa [file] [log] [blame]
Michel Ludwig3169c9b2007-08-21 17:37:22 -03001/*
Dmitri Belimove28f49b2010-02-22 06:32:15 -03002 tm6000-dvb.c - dvb-t support for TM5600/TM6000/TM6010 USB video capture devices
Michel Ludwig3169c9b2007-08-21 17:37:22 -03003
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.
18 */
19
Stefan Ringel08e60ba2010-02-05 20:06:52 -030020#include <linux/kernel.h>
Michel Ludwig3169c9b2007-08-21 17:37:22 -030021#include <linux/usb.h>
22
23#include "tm6000.h"
24#include "tm6000-regs.h"
25
Michel Ludwig3169c9b2007-08-21 17:37:22 -030026#include "zl10353.h"
27
28#include <media/tuner.h>
29
Michel Ludwig5b74c2c2007-11-16 13:16:59 -030030#include "tuner-xc2028.h"
Stefan Ringel7e2d9822010-05-23 15:31:44 -030031#include "xc5000.h"
Michel Ludwig5b74c2c2007-11-16 13:16:59 -030032
Stefan Ringel08e60ba2010-02-05 20:06:52 -030033static void inline print_err_status (struct tm6000_core *dev,
34 int packet, int status)
35{
36 char *errmsg = "Unknown";
37
38 switch(status) {
39 case -ENOENT:
40 errmsg = "unlinked synchronuously";
41 break;
42 case -ECONNRESET:
43 errmsg = "unlinked asynchronuously";
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;
63 }
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);
70 }
71}
72
Michel Ludwig3169c9b2007-08-21 17:37:22 -030073static void tm6000_urb_received(struct urb *urb)
74{
75 int ret;
76 struct tm6000_core* dev = urb->context;
77
Stefan Ringel08e60ba2010-02-05 20:06:52 -030078 if(urb->status != 0) {
79 print_err_status (dev,0,urb->status);
Michel Ludwig3169c9b2007-08-21 17:37:22 -030080 }
81 else if(urb->actual_length>0){
82 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
83 urb->actual_length);
84 }
85
86 if(dev->dvb->streams > 0) {
87 ret = usb_submit_urb(urb, GFP_ATOMIC);
88 if(ret < 0) {
89 printk(KERN_ERR "tm6000: error %s\n", __FUNCTION__);
90 kfree(urb->transfer_buffer);
91 usb_free_urb(urb);
92 }
93 }
94}
95
96int tm6000_start_stream(struct tm6000_core *dev)
97{
98 int ret;
Stefan Ringel08e60ba2010-02-05 20:06:52 -030099 unsigned int pipe, size;
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300100 struct tm6000_dvb *dvb = dev->dvb;
101
102 printk(KERN_INFO "tm6000: got start stream request %s\n",__FUNCTION__);
103
Stefan Ringeldcf5d3a2010-05-23 15:29:26 -0300104 if (dev->mode != TM6000_MODE_DIGITAL) {
105 tm6000_init_digital_mode(dev);
106 dev->mode = TM6000_MODE_DIGITAL;
107 }
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300108
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300109 dvb->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
110 if(dvb->bulk_urb == NULL) {
111 printk(KERN_ERR "tm6000: couldn't allocate urb\n");
112 return -ENOMEM;
113 }
114
Mauro Carvalho Chehab6ae635c2010-04-26 11:24:18 -0300115 pipe = usb_rcvbulkpipe(dev->udev, dev->bulk_in.endp->desc.bEndpointAddress
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300116 & USB_ENDPOINT_NUMBER_MASK);
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300117
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300118 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
119 size = size * 15; /* 512 x 8 or 12 or 15 */
120
121 dvb->bulk_urb->transfer_buffer = kzalloc(size, GFP_KERNEL);
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300122 if(dvb->bulk_urb->transfer_buffer == NULL) {
123 usb_free_urb(dvb->bulk_urb);
124 printk(KERN_ERR "tm6000: couldn't allocate transfer buffer!\n");
125 return -ENOMEM;
126 }
127
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300128 usb_fill_bulk_urb(dvb->bulk_urb, dev->udev, pipe,
129 dvb->bulk_urb->transfer_buffer,
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300130 size,
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300131 tm6000_urb_received, dev);
Michel Ludwig43861362007-09-24 17:01:49 -0300132
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300133 ret = usb_clear_halt(dev->udev, pipe);
134 if(ret < 0) {
135 printk(KERN_ERR "tm6000: error %i in %s during pipe reset\n",ret,__FUNCTION__);
Michel Ludwig43861362007-09-24 17:01:49 -0300136 return ret;
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300137 }
138 else {
139 printk(KERN_ERR "tm6000: pipe resetted\n");
140 }
141
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300142/* mutex_lock(&tm6000_driver.open_close_mutex); */
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300143 ret = usb_submit_urb(dvb->bulk_urb, GFP_KERNEL);
144
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300145/* mutex_unlock(&tm6000_driver.open_close_mutex); */
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300146 if (ret) {
147 printk(KERN_ERR "tm6000: submit of urb failed (error=%i)\n",ret);
148
149 kfree(dvb->bulk_urb->transfer_buffer);
150 usb_free_urb(dvb->bulk_urb);
151 return ret;
152 }
153
154 return 0;
155}
156
157void tm6000_stop_stream(struct tm6000_core *dev)
158{
159 struct tm6000_dvb *dvb = dev->dvb;
160
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300161 if(dvb->bulk_urb) {
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300162 printk (KERN_INFO "urb killing\n");
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300163 usb_kill_urb(dvb->bulk_urb);
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300164 printk (KERN_INFO "urb buffer free\n");
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300165 kfree(dvb->bulk_urb->transfer_buffer);
166 usb_free_urb(dvb->bulk_urb);
167 dvb->bulk_urb = NULL;
168 }
169}
170
171int tm6000_start_feed(struct dvb_demux_feed *feed)
172{
173 struct dvb_demux *demux = feed->demux;
174 struct tm6000_core *dev = demux->priv;
175 struct tm6000_dvb *dvb = dev->dvb;
176 printk(KERN_INFO "tm6000: got start feed request %s\n",__FUNCTION__);
177
178 mutex_lock(&dvb->mutex);
179 if(dvb->streams == 0) {
180 dvb->streams = 1;
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300181/* mutex_init(&tm6000_dev->streming_mutex); */
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300182 tm6000_start_stream(dev);
183 }
184 else {
185 ++(dvb->streams);
186 }
187 mutex_unlock(&dvb->mutex);
188
189 return 0;
190}
191
192int tm6000_stop_feed(struct dvb_demux_feed *feed) {
193 struct dvb_demux *demux = feed->demux;
194 struct tm6000_core *dev = demux->priv;
195 struct tm6000_dvb *dvb = dev->dvb;
196
197 printk(KERN_INFO "tm6000: got stop feed request %s\n",__FUNCTION__);
198
199 mutex_lock(&dvb->mutex);
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300200
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300201 printk (KERN_INFO "stream %#x\n", dvb->streams);
202 --(dvb->streams);
203 if(dvb->streams == 0) {
204 printk (KERN_INFO "stop stream\n");
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300205 tm6000_stop_stream(dev);
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300206/* mutex_destroy(&tm6000_dev->streaming_mutex); */
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300207 }
208 mutex_unlock(&dvb->mutex);
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300209/* mutex_destroy(&tm6000_dev->streaming_mutex); */
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300210
211 return 0;
212}
213
214int tm6000_dvb_attach_frontend(struct tm6000_core *dev)
215{
216 struct tm6000_dvb *dvb = dev->dvb;
217
218 if(dev->caps.has_zl10353) {
219 struct zl10353_config config =
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300220 {.demod_address = dev->demod_addr,
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300221 .no_tuner = 1,
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300222 .parallel_ts = 1,
223 .if2 = 45700,
224 .disable_i2c_gate_ctrl = 1,
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300225 };
226
Stefan Ringel89eeda62010-02-15 14:37:23 -0300227 dvb->frontend = dvb_attach(zl10353_attach, &config,
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300228 &dev->i2c_adap);
229 }
230 else {
231 printk(KERN_ERR "tm6000: no frontend defined for the device!\n");
232 return -1;
233 }
234
Michel Ludwig70bfae52007-11-19 06:10:54 -0300235 return (!dvb->frontend) ? -1 : 0;
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300236}
237
Maykel Moya1b4c5b12008-04-28 19:20:26 -0300238DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
239
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300240int tm6000_dvb_register(struct tm6000_core *dev)
241{
242 int ret = -1;
243 struct tm6000_dvb *dvb = dev->dvb;
244
245 mutex_init(&dvb->mutex);
246
247 dvb->streams = 0;
248
249 /* attach the frontend */
250 ret = tm6000_dvb_attach_frontend(dev);
251 if(ret < 0) {
252 printk(KERN_ERR "tm6000: couldn't attach the frontend!\n");
Michel Ludwig70bfae52007-11-19 06:10:54 -0300253 goto err;
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300254 }
255
256 ret = dvb_register_adapter(&dvb->adapter, "Trident TVMaster 6000 DVB-T",
Maykel Moya1b4c5b12008-04-28 19:20:26 -0300257 THIS_MODULE, &dev->udev->dev, adapter_nr);
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300258 dvb->adapter.priv = dev;
259
Michel Ludwig5b74c2c2007-11-16 13:16:59 -0300260 if (dvb->frontend) {
Stefan Ringel7e2d9822010-05-23 15:31:44 -0300261 switch (dev->tuner_type) {
262 case TUNER_XC2028: {
263 struct xc2028_config cfg = {
264 .i2c_adap = &dev->i2c_adap,
265 .i2c_addr = dev->tuner_addr,
266 };
Michel Ludwig5b74c2c2007-11-16 13:16:59 -0300267
Stefan Ringel7e2d9822010-05-23 15:31:44 -0300268 dvb->frontend->callback = tm6000_tuner_callback;
269 ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
270 if (ret < 0) {
271 printk(KERN_ERR
272 "tm6000: couldn't register frontend\n");
273 goto adapter_err;
274 }
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300275
Stefan Ringel7e2d9822010-05-23 15:31:44 -0300276 if (!dvb_attach(xc2028_attach, dvb->frontend, &cfg)) {
277 printk(KERN_ERR "tm6000: couldn't register "
278 "frontend (xc3028)\n");
279 ret = -EINVAL;
280 goto frontend_err;
281 }
282 printk(KERN_INFO "tm6000: XC2028/3028 asked to be "
283 "attached to frontend!\n");
284 break;
285 }
286 case TUNER_XC5000: {
287 struct xc5000_config cfg = {
288 .i2c_address = dev->tuner_addr,
289 };
290
291 dvb->frontend->callback = tm6000_xc5000_callback;
292 ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
293 if (ret < 0) {
294 printk(KERN_ERR
295 "tm6000: couldn't register frontend\n");
296 goto adapter_err;
297 }
298
299 if (!dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap, &cfg)) {
300 printk(KERN_ERR "tm6000: couldn't register "
301 "frontend (xc5000)\n");
302 ret = -EINVAL;
303 goto frontend_err;
304 }
305 printk(KERN_INFO "tm6000: XC5000 asked to be "
306 "attached to frontend!\n");
307 break;
308 }
Michel Ludwig5b74c2c2007-11-16 13:16:59 -0300309 }
Michel Ludwig5b74c2c2007-11-16 13:16:59 -0300310 } else {
311 printk(KERN_ERR "tm6000: no frontend found\n");
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300312 }
313
314 dvb->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING
315 | DMX_MEMORY_BASED_FILTERING;
316 dvb->demux.priv = dev;
Stefan Ringel38d75a72010-02-15 14:37:19 -0300317 dvb->demux.filternum = 8;
318 dvb->demux.feednum = 8;
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300319 dvb->demux.start_feed = tm6000_start_feed;
320 dvb->demux.stop_feed = tm6000_stop_feed;
321 dvb->demux.write_to_decoder = NULL;
322 ret = dvb_dmx_init(&dvb->demux);
323 if(ret < 0) {
324 printk("tm6000: dvb_dmx_init failed (errno = %d)\n", ret);
325 goto frontend_err;
326 }
327
328 dvb->dmxdev.filternum = dev->dvb->demux.filternum;
329 dvb->dmxdev.demux = &dev->dvb->demux.dmx;
330 dvb->dmxdev.capabilities = 0;
331
332 ret = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
333 if(ret < 0) {
334 printk("tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret);
335 goto dvb_dmx_err;
336 }
337
338 return 0;
339
340dvb_dmx_err:
341 dvb_dmx_release(&dvb->demux);
342frontend_err:
343 if(dvb->frontend) {
Michel Ludwig70bfae52007-11-19 06:10:54 -0300344 dvb_frontend_detach(dvb->frontend);
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300345 dvb_unregister_frontend(dvb->frontend);
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300346 }
347adapter_err:
348 dvb_unregister_adapter(&dvb->adapter);
349err:
350 return ret;
351}
352
353void tm6000_dvb_unregister(struct tm6000_core *dev)
354{
355 struct tm6000_dvb *dvb = dev->dvb;
356
357 if(dvb->bulk_urb != NULL) {
358 struct urb *bulk_urb = dvb->bulk_urb;
359
360 kfree(bulk_urb->transfer_buffer);
361 bulk_urb->transfer_buffer = NULL;
362 usb_unlink_urb(bulk_urb);
363 usb_free_urb(bulk_urb);
364 }
365
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300366/* mutex_lock(&tm6000_driver.open_close_mutex); */
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300367 if(dvb->frontend) {
Michel Ludwig70bfae52007-11-19 06:10:54 -0300368 dvb_frontend_detach(dvb->frontend);
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300369 dvb_unregister_frontend(dvb->frontend);
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300370 }
371
372 dvb_dmxdev_release(&dvb->dmxdev);
373 dvb_dmx_release(&dvb->demux);
374 dvb_unregister_adapter(&dvb->adapter);
375 mutex_destroy(&dvb->mutex);
Stefan Ringel08e60ba2010-02-05 20:06:52 -0300376/* mutex_unlock(&tm6000_driver.open_close_mutex); */
Michel Ludwig3169c9b2007-08-21 17:37:22 -0300377
378}