v4l-dvb: update gfp/slab.h includes
[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/slab.h>
22 #include <linux/usb.h>
23
24 #include "tm6000.h"
25 #include "tm6000-regs.h"
26
27 #include "zl10353.h"
28
29 #include <media/tuner.h>
30
31 #include "tuner-xc2028.h"
32 #include "xc5000.h"
33
34 static void inline print_err_status (struct tm6000_core *dev,
35                                      int packet, int status)
36 {
37         char *errmsg = "Unknown";
38
39         switch(status) {
40         case -ENOENT:
41                 errmsg = "unlinked synchronuously";
42                 break;
43         case -ECONNRESET:
44                 errmsg = "unlinked asynchronuously";
45                 break;
46         case -ENOSR:
47                 errmsg = "Buffer error (overrun)";
48                 break;
49         case -EPIPE:
50                 errmsg = "Stalled (device not responding)";
51                 break;
52         case -EOVERFLOW:
53                 errmsg = "Babble (bad cable?)";
54                 break;
55         case -EPROTO:
56                 errmsg = "Bit-stuff error (bad cable?)";
57                 break;
58         case -EILSEQ:
59                 errmsg = "CRC/Timeout (could be anything)";
60                 break;
61         case -ETIME:
62                 errmsg = "Device does not respond";
63                 break;
64         }
65         if (packet<0) {
66                 dprintk(dev, 1, "URB status %d [%s].\n",
67                         status, errmsg);
68         } else {
69                 dprintk(dev, 1, "URB packet %d, status %d [%s].\n",
70                         packet, status, errmsg);
71         }
72 }
73
74 static void tm6000_urb_received(struct urb *urb)
75 {
76         int ret;
77         struct tm6000_core* dev = urb->context;
78
79         if(urb->status != 0) {
80                 print_err_status (dev,0,urb->status);
81         }
82         else if(urb->actual_length>0){
83                 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer,
84                                                    urb->actual_length);
85         }
86
87         if(dev->dvb->streams > 0) {
88                 ret = usb_submit_urb(urb, GFP_ATOMIC);
89                 if(ret < 0) {
90                         printk(KERN_ERR "tm6000:  error %s\n", __FUNCTION__);
91                         kfree(urb->transfer_buffer);
92                         usb_free_urb(urb);
93                 }
94         }
95 }
96
97 int tm6000_start_stream(struct tm6000_core *dev)
98 {
99         int ret;
100         unsigned int pipe, size;
101         struct tm6000_dvb *dvb = dev->dvb;
102
103         printk(KERN_INFO "tm6000: got start stream request %s\n",__FUNCTION__);
104
105         if (dev->mode != TM6000_MODE_DIGITAL) {
106                 tm6000_init_digital_mode(dev);
107                 dev->mode = TM6000_MODE_DIGITAL;
108         }
109
110         dvb->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
111         if(dvb->bulk_urb == NULL) {
112                 printk(KERN_ERR "tm6000: couldn't allocate urb\n");
113                 return -ENOMEM;
114         }
115
116         pipe = usb_rcvbulkpipe(dev->udev, dev->bulk_in.endp->desc.bEndpointAddress
117                                                           & USB_ENDPOINT_NUMBER_MASK);
118
119         size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
120         size = size * 15; /* 512 x 8 or 12 or 15 */
121
122         dvb->bulk_urb->transfer_buffer = kzalloc(size, GFP_KERNEL);
123         if(dvb->bulk_urb->transfer_buffer == NULL) {
124                 usb_free_urb(dvb->bulk_urb);
125                 printk(KERN_ERR "tm6000: couldn't allocate transfer buffer!\n");
126                 return -ENOMEM;
127         }
128
129         usb_fill_bulk_urb(dvb->bulk_urb, dev->udev, pipe,
130                                                  dvb->bulk_urb->transfer_buffer,
131                                                  size,
132                                                  tm6000_urb_received, dev);
133
134         ret = usb_clear_halt(dev->udev, pipe);
135         if(ret < 0) {
136                 printk(KERN_ERR "tm6000: error %i in %s during pipe reset\n",ret,__FUNCTION__);
137                 return ret;
138         }
139         else {
140                 printk(KERN_ERR "tm6000: pipe resetted\n");
141         }
142
143 /*      mutex_lock(&tm6000_driver.open_close_mutex); */
144         ret = usb_submit_urb(dvb->bulk_urb, GFP_KERNEL);
145
146 /*      mutex_unlock(&tm6000_driver.open_close_mutex); */
147         if (ret) {
148                 printk(KERN_ERR "tm6000: submit of urb failed (error=%i)\n",ret);
149
150                 kfree(dvb->bulk_urb->transfer_buffer);
151                 usb_free_urb(dvb->bulk_urb);
152                 return ret;
153         }
154
155         return 0;
156 }
157
158 void tm6000_stop_stream(struct tm6000_core *dev)
159 {
160         struct tm6000_dvb *dvb = dev->dvb;
161
162         if(dvb->bulk_urb) {
163                 printk (KERN_INFO "urb killing\n");
164                 usb_kill_urb(dvb->bulk_urb);
165                 printk (KERN_INFO "urb buffer free\n");
166                 kfree(dvb->bulk_urb->transfer_buffer);
167                 usb_free_urb(dvb->bulk_urb);
168                 dvb->bulk_urb = NULL;
169         }
170 }
171
172 int tm6000_start_feed(struct dvb_demux_feed *feed)
173 {
174         struct dvb_demux *demux = feed->demux;
175         struct tm6000_core *dev = demux->priv;
176         struct tm6000_dvb *dvb = dev->dvb;
177         printk(KERN_INFO "tm6000: got start feed request %s\n",__FUNCTION__);
178
179         mutex_lock(&dvb->mutex);
180         if(dvb->streams == 0) {
181                 dvb->streams = 1;
182 /*              mutex_init(&tm6000_dev->streming_mutex); */
183                 tm6000_start_stream(dev);
184         }
185         else {
186                 ++(dvb->streams);
187         }
188         mutex_unlock(&dvb->mutex);
189
190         return 0;
191 }
192
193 int tm6000_stop_feed(struct dvb_demux_feed *feed) {
194         struct dvb_demux *demux = feed->demux;
195         struct tm6000_core *dev = demux->priv;
196         struct tm6000_dvb *dvb = dev->dvb;
197
198         printk(KERN_INFO "tm6000: got stop feed request %s\n",__FUNCTION__);
199
200         mutex_lock(&dvb->mutex);
201
202         printk (KERN_INFO "stream %#x\n", dvb->streams);
203         --(dvb->streams);
204         if(dvb->streams == 0) {
205                 printk (KERN_INFO "stop stream\n");
206                 tm6000_stop_stream(dev);
207 /*              mutex_destroy(&tm6000_dev->streaming_mutex); */
208         }
209         mutex_unlock(&dvb->mutex);
210 /*      mutex_destroy(&tm6000_dev->streaming_mutex); */
211
212         return 0;
213 }
214
215 int tm6000_dvb_attach_frontend(struct tm6000_core *dev)
216 {
217         struct tm6000_dvb *dvb = dev->dvb;
218
219         if(dev->caps.has_zl10353) {
220                 struct zl10353_config config =
221                                     {.demod_address = dev->demod_addr,
222                                      .no_tuner = 1,
223                                      .parallel_ts = 1,
224                                      .if2 = 45700,
225                                      .disable_i2c_gate_ctrl = 1,
226                                     };
227
228                 dvb->frontend = dvb_attach(zl10353_attach, &config,
229                                                            &dev->i2c_adap);
230         }
231         else {
232                 printk(KERN_ERR "tm6000: no frontend defined for the device!\n");
233                 return -1;
234         }
235
236         return (!dvb->frontend) ? -1 : 0;
237 }
238
239 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
240
241 int tm6000_dvb_register(struct tm6000_core *dev)
242 {
243         int ret = -1;
244         struct tm6000_dvb *dvb = dev->dvb;
245
246         mutex_init(&dvb->mutex);
247
248         dvb->streams = 0;
249
250         /* attach the frontend */
251         ret = tm6000_dvb_attach_frontend(dev);
252         if(ret < 0) {
253                 printk(KERN_ERR "tm6000: couldn't attach the frontend!\n");
254                 goto err;
255         }
256
257         ret = dvb_register_adapter(&dvb->adapter, "Trident TVMaster 6000 DVB-T",
258                                                           THIS_MODULE, &dev->udev->dev, adapter_nr);
259         dvb->adapter.priv = dev;
260
261         if (dvb->frontend) {
262                 switch (dev->tuner_type) {
263                 case TUNER_XC2028: {
264                         struct xc2028_config cfg = {
265                                 .i2c_adap = &dev->i2c_adap,
266                                 .i2c_addr = dev->tuner_addr,
267                         };
268
269                         dvb->frontend->callback = tm6000_tuner_callback;
270                         ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
271                         if (ret < 0) {
272                                 printk(KERN_ERR
273                                         "tm6000: couldn't register frontend\n");
274                                 goto adapter_err;
275                         }
276
277                         if (!dvb_attach(xc2028_attach, dvb->frontend, &cfg)) {
278                                 printk(KERN_ERR "tm6000: couldn't register "
279                                                 "frontend (xc3028)\n");
280                                 ret = -EINVAL;
281                                 goto frontend_err;
282                         }
283                         printk(KERN_INFO "tm6000: XC2028/3028 asked to be "
284                                          "attached to frontend!\n");
285                         break;
286                         }
287                 case TUNER_XC5000: {
288                         struct xc5000_config cfg = {
289                                 .i2c_address = dev->tuner_addr,
290                         };
291
292                         dvb->frontend->callback = tm6000_xc5000_callback;
293                         ret = dvb_register_frontend(&dvb->adapter, dvb->frontend);
294                         if (ret < 0) {
295                                 printk(KERN_ERR
296                                         "tm6000: couldn't register frontend\n");
297                                 goto adapter_err;
298                         }
299
300                         if (!dvb_attach(xc5000_attach, dvb->frontend, &dev->i2c_adap, &cfg)) {
301                                 printk(KERN_ERR "tm6000: couldn't register "
302                                                 "frontend (xc5000)\n");
303                                 ret = -EINVAL;
304                                 goto frontend_err;
305                         }
306                         printk(KERN_INFO "tm6000: XC5000 asked to be "
307                                          "attached to frontend!\n");
308                         break;
309                         }
310                 }
311         } else {
312                 printk(KERN_ERR "tm6000: no frontend found\n");
313         }
314
315         dvb->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING
316                                                             | DMX_MEMORY_BASED_FILTERING;
317         dvb->demux.priv = dev;
318         dvb->demux.filternum = 8;
319         dvb->demux.feednum = 8;
320         dvb->demux.start_feed = tm6000_start_feed;
321         dvb->demux.stop_feed = tm6000_stop_feed;
322         dvb->demux.write_to_decoder = NULL;
323         ret = dvb_dmx_init(&dvb->demux);
324         if(ret < 0) {
325                 printk("tm6000: dvb_dmx_init failed (errno = %d)\n", ret);
326                 goto frontend_err;
327         }
328
329         dvb->dmxdev.filternum = dev->dvb->demux.filternum;
330         dvb->dmxdev.demux = &dev->dvb->demux.dmx;
331         dvb->dmxdev.capabilities = 0;
332
333         ret =  dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
334         if(ret < 0) {
335                 printk("tm6000: dvb_dmxdev_init failed (errno = %d)\n", ret);
336                 goto dvb_dmx_err;
337         }
338
339         return 0;
340
341 dvb_dmx_err:
342         dvb_dmx_release(&dvb->demux);
343 frontend_err:
344         if(dvb->frontend) {
345                 dvb_frontend_detach(dvb->frontend);
346                 dvb_unregister_frontend(dvb->frontend);
347         }
348 adapter_err:
349         dvb_unregister_adapter(&dvb->adapter);
350 err:
351         return ret;
352 }
353
354 void tm6000_dvb_unregister(struct tm6000_core *dev)
355 {
356         struct tm6000_dvb *dvb = dev->dvb;
357
358         if(dvb->bulk_urb != NULL) {
359                 struct urb *bulk_urb = dvb->bulk_urb;
360
361                 kfree(bulk_urb->transfer_buffer);
362                 bulk_urb->transfer_buffer = NULL;
363                 usb_unlink_urb(bulk_urb);
364                 usb_free_urb(bulk_urb);
365         }
366
367 /*      mutex_lock(&tm6000_driver.open_close_mutex); */
368         if(dvb->frontend) {
369                 dvb_frontend_detach(dvb->frontend);
370                 dvb_unregister_frontend(dvb->frontend);
371         }
372
373         dvb_dmxdev_release(&dvb->dmxdev);
374         dvb_dmx_release(&dvb->demux);
375         dvb_unregister_adapter(&dvb->adapter);
376         mutex_destroy(&dvb->mutex);
377 /*      mutex_unlock(&tm6000_driver.open_close_mutex); */
378
379 }