V4L/DVB (9271): videobuf: data storage optimisation (2)
[linux-2.6] / drivers / media / video / videobuf-dvb.c
1 /*
2  *
3  * some helper function for simple DVB cards which simply DMA the
4  * complete transport stream and let the computer sort everything else
5  * (i.e. we are using the software demux, ...).  Also uses the
6  * video-buf to manage DMA buffers.
7  *
8  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/fs.h>
20 #include <linux/kthread.h>
21 #include <linux/file.h>
22
23 #include <linux/freezer.h>
24
25 #include <media/videobuf-core.h>
26 #include <media/videobuf-dvb.h>
27
28 /* ------------------------------------------------------------------ */
29
30 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
31 MODULE_LICENSE("GPL");
32
33 static unsigned int debug;
34 module_param(debug, int, 0644);
35 MODULE_PARM_DESC(debug,"enable debug messages");
36
37 #define dprintk(fmt, arg...)    if (debug)                      \
38         printk(KERN_DEBUG "%s/dvb: " fmt, dvb->name , ## arg)
39
40 /* ------------------------------------------------------------------ */
41
42 static int videobuf_dvb_thread(void *data)
43 {
44         struct videobuf_dvb *dvb = data;
45         struct videobuf_buffer *buf;
46         unsigned long flags;
47         int err;
48         void *outp;
49
50         dprintk("dvb thread started\n");
51         set_freezable();
52         videobuf_read_start(&dvb->dvbq);
53
54         for (;;) {
55                 /* fetch next buffer */
56                 buf = list_entry(dvb->dvbq.stream.next,
57                                  struct videobuf_buffer, stream);
58                 list_del(&buf->stream);
59                 err = videobuf_waiton(buf,0,1);
60
61                 /* no more feeds left or stop_feed() asked us to quit */
62                 if (0 == dvb->nfeeds)
63                         break;
64                 if (kthread_should_stop())
65                         break;
66                 try_to_freeze();
67
68                 /* feed buffer data to demux */
69                 outp = videobuf_queue_to_vmalloc (&dvb->dvbq, buf);
70
71                 if (buf->state == VIDEOBUF_DONE)
72                         dvb_dmx_swfilter(&dvb->demux, outp,
73                                          buf->size);
74
75                 /* requeue buffer */
76                 list_add_tail(&buf->stream,&dvb->dvbq.stream);
77                 spin_lock_irqsave(dvb->dvbq.irqlock,flags);
78                 dvb->dvbq.ops->buf_queue(&dvb->dvbq,buf);
79                 spin_unlock_irqrestore(dvb->dvbq.irqlock,flags);
80         }
81
82         videobuf_read_stop(&dvb->dvbq);
83         dprintk("dvb thread stopped\n");
84
85         /* Hmm, linux becomes *very* unhappy without this ... */
86         while (!kthread_should_stop()) {
87                 set_current_state(TASK_INTERRUPTIBLE);
88                 schedule();
89         }
90         return 0;
91 }
92
93 static int videobuf_dvb_start_feed(struct dvb_demux_feed *feed)
94 {
95         struct dvb_demux *demux  = feed->demux;
96         struct videobuf_dvb *dvb = demux->priv;
97         int rc;
98
99         if (!demux->dmx.frontend)
100                 return -EINVAL;
101
102         mutex_lock(&dvb->lock);
103         dvb->nfeeds++;
104         rc = dvb->nfeeds;
105
106         if (NULL != dvb->thread)
107                 goto out;
108         dvb->thread = kthread_run(videobuf_dvb_thread,
109                                   dvb, "%s dvb", dvb->name);
110         if (IS_ERR(dvb->thread)) {
111                 rc = PTR_ERR(dvb->thread);
112                 dvb->thread = NULL;
113         }
114
115 out:
116         mutex_unlock(&dvb->lock);
117         return rc;
118 }
119
120 static int videobuf_dvb_stop_feed(struct dvb_demux_feed *feed)
121 {
122         struct dvb_demux *demux  = feed->demux;
123         struct videobuf_dvb *dvb = demux->priv;
124         int err = 0;
125
126         mutex_lock(&dvb->lock);
127         dvb->nfeeds--;
128         if (0 == dvb->nfeeds  &&  NULL != dvb->thread) {
129                 // FIXME: cx8802_cancel_buffers(dev);
130                 err = kthread_stop(dvb->thread);
131                 dvb->thread = NULL;
132         }
133         mutex_unlock(&dvb->lock);
134         return err;
135 }
136
137 /* ------------------------------------------------------------------ */
138 /* Register a single adapter and one or more frontends */
139 int videobuf_dvb_register_bus(struct videobuf_dvb_frontends *f,
140                           struct module *module,
141                           void *adapter_priv,
142                           struct device *device,
143                           short *adapter_nr,
144                           int mfe_shared)
145 {
146         struct list_head *list, *q;
147         struct videobuf_dvb_frontend *fe;
148         int res;
149
150         fe = videobuf_dvb_get_frontend(f, 1);
151         if (!fe) {
152                 printk(KERN_WARNING "Unable to register the adapter which has no frontends\n");
153                 return -EINVAL;
154         }
155
156         /* Bring up the adapter */
157         res = videobuf_dvb_register_adapter(f, module, adapter_priv, device, fe->dvb.name, adapter_nr, mfe_shared);
158         if (res < 0) {
159                 printk(KERN_WARNING "videobuf_dvb_register_adapter failed (errno = %d)\n", res);
160                 return res;
161         }
162
163         /* Attach all of the frontends to the adapter */
164         mutex_lock(&f->lock);
165         list_for_each_safe(list, q, &f->felist) {
166                 fe = list_entry(list, struct videobuf_dvb_frontend, felist);
167                 res = videobuf_dvb_register_frontend(&f->adapter, &fe->dvb);
168                 if (res < 0) {
169                         printk(KERN_WARNING "%s: videobuf_dvb_register_frontend failed (errno = %d)\n",
170                                 fe->dvb.name, res);
171                         goto err;
172                 }
173         }
174         mutex_unlock(&f->lock);
175         return 0;
176
177 err:
178         mutex_unlock(&f->lock);
179         videobuf_dvb_unregister_bus(f);
180         return res;
181 }
182
183 int videobuf_dvb_register_adapter(struct videobuf_dvb_frontends *fe,
184                           struct module *module,
185                           void *adapter_priv,
186                           struct device *device,
187                           char *adapter_name,
188                           short *adapter_nr,
189                           int mfe_shared)
190 {
191         int result;
192
193         mutex_init(&fe->lock);
194
195         /* register adapter */
196         result = dvb_register_adapter(&fe->adapter, adapter_name, module, device, adapter_nr);
197         if (result < 0) {
198                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
199                        adapter_name, result);
200         }
201         fe->adapter.priv = adapter_priv;
202         fe->adapter.mfe_shared = mfe_shared;
203
204         return result;
205 }
206
207 int videobuf_dvb_register_frontend(struct dvb_adapter *adapter, struct videobuf_dvb *dvb)
208 {
209         int result;
210
211         /* register frontend */
212         result = dvb_register_frontend(adapter, dvb->frontend);
213         if (result < 0) {
214                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
215                        dvb->name, result);
216                 goto fail_frontend;
217         }
218
219         /* register demux stuff */
220         dvb->demux.dmx.capabilities =
221                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
222                 DMX_MEMORY_BASED_FILTERING;
223         dvb->demux.priv       = dvb;
224         dvb->demux.filternum  = 256;
225         dvb->demux.feednum    = 256;
226         dvb->demux.start_feed = videobuf_dvb_start_feed;
227         dvb->demux.stop_feed  = videobuf_dvb_stop_feed;
228         result = dvb_dmx_init(&dvb->demux);
229         if (result < 0) {
230                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
231                        dvb->name, result);
232                 goto fail_dmx;
233         }
234
235         dvb->dmxdev.filternum    = 256;
236         dvb->dmxdev.demux        = &dvb->demux.dmx;
237         dvb->dmxdev.capabilities = 0;
238         result = dvb_dmxdev_init(&dvb->dmxdev, adapter);
239
240         if (result < 0) {
241                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
242                        dvb->name, result);
243                 goto fail_dmxdev;
244         }
245
246         dvb->fe_hw.source = DMX_FRONTEND_0;
247         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
248         if (result < 0) {
249                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
250                        dvb->name, result);
251                 goto fail_fe_hw;
252         }
253
254         dvb->fe_mem.source = DMX_MEMORY_FE;
255         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
256         if (result < 0) {
257                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
258                        dvb->name, result);
259                 goto fail_fe_mem;
260         }
261
262         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
263         if (result < 0) {
264                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
265                        dvb->name, result);
266                 goto fail_fe_conn;
267         }
268
269         /* register network adapter */
270         dvb_net_init(adapter, &dvb->net, &dvb->demux.dmx);
271         if (dvb->net.dvbdev == NULL) {
272                 result = -ENOMEM;
273                 goto fail_fe_conn;
274         }
275         return 0;
276
277 fail_fe_conn:
278         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
279 fail_fe_mem:
280         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
281 fail_fe_hw:
282         dvb_dmxdev_release(&dvb->dmxdev);
283 fail_dmxdev:
284         dvb_dmx_release(&dvb->demux);
285 fail_dmx:
286         dvb_unregister_frontend(dvb->frontend);
287 fail_frontend:
288         dvb_frontend_detach(dvb->frontend);
289         dvb->frontend = NULL;
290
291         return result;
292 }
293
294 void videobuf_dvb_unregister_bus(struct videobuf_dvb_frontends *f)
295 {
296         struct list_head *list, *q;
297         struct videobuf_dvb_frontend *fe;
298
299         mutex_lock(&f->lock);
300         list_for_each_safe(list, q, &f->felist) {
301                 fe = list_entry(list, struct videobuf_dvb_frontend, felist);
302                 if(fe->dvb.net.dvbdev) {
303                         dvb_net_release(&fe->dvb.net);
304                         fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx, &fe->dvb.fe_mem);
305                         fe->dvb.demux.dmx.remove_frontend(&fe->dvb.demux.dmx, &fe->dvb.fe_hw);
306                         dvb_dmxdev_release(&fe->dvb.dmxdev);
307                         dvb_dmx_release(&fe->dvb.demux);
308                         dvb_unregister_frontend(fe->dvb.frontend);
309                 }
310                 if(fe->dvb.frontend) /* always allocated, may have been reset */
311                         dvb_frontend_detach(fe->dvb.frontend);
312                 list_del(list);
313                 kfree(fe);
314         }
315         mutex_unlock(&f->lock);
316
317         dvb_unregister_adapter(&f->adapter);
318 }
319
320 struct videobuf_dvb_frontend * videobuf_dvb_get_frontend(struct videobuf_dvb_frontends *f, int id)
321 {
322         struct list_head *list, *q;
323         struct videobuf_dvb_frontend *fe, *ret = NULL;
324
325         mutex_lock(&f->lock);
326
327         list_for_each_safe(list, q, &f->felist) {
328                 fe = list_entry(list, struct videobuf_dvb_frontend, felist);
329                 if (fe->id == id) {
330                         ret = fe;
331                         break;
332                 }
333         }
334
335         mutex_unlock(&f->lock);
336
337         return ret;
338 }
339
340 int videobuf_dvb_find_frontend(struct videobuf_dvb_frontends *f, struct dvb_frontend *p)
341 {
342         struct list_head *list, *q;
343         struct videobuf_dvb_frontend *fe = NULL;
344         int ret = 0;
345
346         mutex_lock(&f->lock);
347
348         list_for_each_safe(list, q, &f->felist) {
349                 fe = list_entry(list, struct videobuf_dvb_frontend, felist);
350                 if (fe->dvb.frontend == p) {
351                         ret = fe->id;
352                         break;
353                 }
354         }
355
356         mutex_unlock(&f->lock);
357
358         return ret;
359 }
360
361 struct videobuf_dvb_frontend * videobuf_dvb_alloc_frontend(struct videobuf_dvb_frontends *f, int id)
362 {
363         struct videobuf_dvb_frontend *fe;
364
365         fe = kzalloc(sizeof(struct videobuf_dvb_frontend),GFP_KERNEL);
366         if (fe == NULL)
367                 goto fail_alloc;
368
369         fe->id = id;
370         mutex_init(&fe->dvb.lock);
371
372         mutex_lock(&f->lock);
373         list_add_tail(&fe->felist,&f->felist);
374         mutex_unlock(&f->lock);
375
376 fail_alloc:
377         return fe;
378 }
379
380 EXPORT_SYMBOL(videobuf_dvb_register_bus);
381 EXPORT_SYMBOL(videobuf_dvb_unregister_bus);
382 EXPORT_SYMBOL(videobuf_dvb_alloc_frontend);
383 EXPORT_SYMBOL(videobuf_dvb_get_frontend);
384 EXPORT_SYMBOL(videobuf_dvb_find_frontend);
385
386 /* ------------------------------------------------------------------ */
387 /*
388  * Local variables:
389  * c-basic-offset: 8
390  * compile-command: "make DVB=1"
391  * End:
392  */