261e66acbe46b7ea061d2c2be4bd3737b1f8cffa
[karo-tx-linux.git] / drivers / staging / tm6000 / tm6000-dvb.c
1 /*
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.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/usb.h>
22
23 #include "tm6000.h"
24 #include "tm6000-regs.h"
25
26 #include "zl10353.h"
27
28 #include <media/tuner.h>
29
30 #include "tuner-xc2028.h"
31 #include "xc5000.h"
32
33 static 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
73 static void tm6000_urb_received(struct urb *urb)
74 {
75         int ret;
76         struct tm6000_core* dev = urb->context;
77
78         if(urb->status != 0) {
79                 print_err_status (dev,0,urb->status);
80         }
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
96 int tm6000_start_stream(struct tm6000_core *dev)
97 {
98         int ret;
99         unsigned int pipe, size;
100         struct tm6000_dvb *dvb = dev->dvb;
101
102         printk(KERN_INFO "tm6000: got start stream request %s\n",__FUNCTION__);
103
104         if (dev->mode != TM6000_MODE_DIGITAL) {
105                 tm6000_init_digital_mode(dev);
106                 dev->mode = TM6000_MODE_DIGITAL;
107         }
108
109         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
115         pipe = usb_rcvbulkpipe(dev->udev, dev->bulk_in.endp->desc.bEndpointAddress
116                                                           & USB_ENDPOINT_NUMBER_MASK);
117
118         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);
122         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
128         usb_fill_bulk_urb(dvb->bulk_urb, dev->udev, pipe,
129                                                  dvb->bulk_urb->transfer_buffer,
130                                                  size,
131                                                  tm6000_urb_received, dev);
132
133         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__);
136                 return ret;
137         }
138         else {
139                 printk(KERN_ERR "tm6000: pipe resetted\n");
140         }
141
142 /*      mutex_lock(&tm6000_driver.open_close_mutex); */
143         ret = usb_submit_urb(dvb->bulk_urb, GFP_KERNEL);
144
145 /*      mutex_unlock(&tm6000_driver.open_close_mutex); */
146         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
157 void tm6000_stop_stream(struct tm6000_core *dev)
158 {
159         struct tm6000_dvb *dvb = dev->dvb;
160
161         if(dvb->bulk_urb) {
162                 printk (KERN_INFO "urb killing\n");
163                 usb_kill_urb(dvb->bulk_urb);
164                 printk (KERN_INFO "urb buffer free\n");
165                 kfree(dvb->bulk_urb->transfer_buffer);
166                 usb_free_urb(dvb->bulk_urb);
167                 dvb->bulk_urb = NULL;
168         }
169 }
170
171 int 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;
181 /*              mutex_init(&tm6000_dev->streming_mutex); */
182                 tm6000_start_stream(dev);
183         }
184         else {
185                 ++(dvb->streams);
186         }
187         mutex_unlock(&dvb->mutex);
188
189         return 0;
190 }
191
192 int 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);
200
201         printk (KERN_INFO "stream %#x\n", dvb->streams);
202         --(dvb->streams);
203         if(dvb->streams == 0) {
204                 printk (KERN_INFO "stop stream\n");
205                 tm6000_stop_stream(dev);
206 /*              mutex_destroy(&tm6000_dev->streaming_mutex); */
207         }
208         mutex_unlock(&dvb->mutex);
209 /*      mutex_destroy(&tm6000_dev->streaming_mutex); */
210
211         return 0;
212 }
213
214 int 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 =
220                                     {.demod_address = dev->demod_addr,
221                                      .no_tuner = 1,
222                                      .parallel_ts = 1,
223                                      .if2 = 45700,
224                                      .disable_i2c_gate_ctrl = 1,
225                                     };
226
227                 dvb->frontend = dvb_attach(zl10353_attach, &config,
228                                                            &dev->i2c_adap);
229         }
230         else {
231                 printk(KERN_ERR "tm6000: no frontend defined for the device!\n");
232                 return -1;
233         }
234
235         return (!dvb->frontend) ? -1 : 0;
236 }
237
238 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
239
240 int 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");
253                 goto err;
254         }
255
256         ret = dvb_register_adapter(&dvb->adapter, "Trident TVMaster 6000 DVB-T",
257                                                           THIS_MODULE, &dev->udev->dev, adapter_nr);
258         dvb->adapter.priv = dev;
259
260         if (dvb->frontend) {
261                 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                         };
267
268                         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                         }
275
276                         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                         }
309                 }
310         } else {
311                 printk(KERN_ERR "tm6000: no frontend found\n");
312         }
313
314         dvb->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING
315                                                             | DMX_MEMORY_BASED_FILTERING;
316         dvb->demux.priv = dev;
317         dvb->demux.filternum = 8;
318         dvb->demux.feednum = 8;
319         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
340 dvb_dmx_err:
341         dvb_dmx_release(&dvb->demux);
342 frontend_err:
343         if(dvb->frontend) {
344                 dvb_frontend_detach(dvb->frontend);
345                 dvb_unregister_frontend(dvb->frontend);
346         }
347 adapter_err:
348         dvb_unregister_adapter(&dvb->adapter);
349 err:
350         return ret;
351 }
352
353 void 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
366 /*      mutex_lock(&tm6000_driver.open_close_mutex); */
367         if(dvb->frontend) {
368                 dvb_frontend_detach(dvb->frontend);
369                 dvb_unregister_frontend(dvb->frontend);
370         }
371
372         dvb_dmxdev_release(&dvb->dmxdev);
373         dvb_dmx_release(&dvb->demux);
374         dvb_unregister_adapter(&dvb->adapter);
375         mutex_destroy(&dvb->mutex);
376 /*      mutex_unlock(&tm6000_driver.open_close_mutex); */
377
378 }