V4L/DVB: v4l videobuf: remove mmap_free callback
[safe/jmp/linux-2.6] / drivers / media / video / videobuf-core.c
1 /*
2  * generic helper functions for handling video4linux capture buffers
3  *
4  * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org>
5  *
6  * Highly based on video-buf written originally by:
7  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
8  * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org>
9  * (c) 2006 Ted Walther and John Sokol
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2
14  */
15
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/mm.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23
24 #include <media/videobuf-core.h>
25
26 #define MAGIC_BUFFER 0x20070728
27 #define MAGIC_CHECK(is, should)                                         \
28         do {                                                            \
29                 if (unlikely((is) != (should))) {                       \
30                         printk(KERN_ERR                                 \
31                                 "magic mismatch: %x (expected %x)\n",   \
32                                         is, should);                    \
33                         BUG();                                          \
34                 }                                                       \
35         } while (0)
36
37 static int debug;
38 module_param(debug, int, 0644);
39
40 MODULE_DESCRIPTION("helper module to manage video4linux buffers");
41 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
42 MODULE_LICENSE("GPL");
43
44 #define dprintk(level, fmt, arg...)                                     \
45         do {                                                            \
46                 if (debug >= level)                                     \
47                         printk(KERN_DEBUG "vbuf: " fmt, ## arg);        \
48         } while (0)
49
50 /* --------------------------------------------------------------------- */
51
52 #define CALL(q, f, arg...)                                              \
53         ((q->int_ops->f) ? q->int_ops->f(arg) : 0)
54
55 void *videobuf_alloc(struct videobuf_queue *q)
56 {
57         struct videobuf_buffer *vb;
58
59         BUG_ON(q->msize < sizeof(*vb));
60
61         if (!q->int_ops || !q->int_ops->alloc) {
62                 printk(KERN_ERR "No specific ops defined!\n");
63                 BUG();
64         }
65
66         vb = q->int_ops->alloc(q->msize);
67         if (NULL != vb) {
68                 init_waitqueue_head(&vb->done);
69                 vb->magic = MAGIC_BUFFER;
70         }
71
72         return vb;
73 }
74 EXPORT_SYMBOL_GPL(videobuf_alloc);
75
76 #define WAITON_CONDITION (vb->state != VIDEOBUF_ACTIVE &&\
77                                 vb->state != VIDEOBUF_QUEUED)
78 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
79 {
80         MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
81
82         if (non_blocking) {
83                 if (WAITON_CONDITION)
84                         return 0;
85                 else
86                         return -EAGAIN;
87         }
88
89         if (intr)
90                 return wait_event_interruptible(vb->done, WAITON_CONDITION);
91         else
92                 wait_event(vb->done, WAITON_CONDITION);
93
94         return 0;
95 }
96 EXPORT_SYMBOL_GPL(videobuf_waiton);
97
98 int videobuf_iolock(struct videobuf_queue *q, struct videobuf_buffer *vb,
99                     struct v4l2_framebuffer *fbuf)
100 {
101         MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
102         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
103
104         return CALL(q, iolock, q, vb, fbuf);
105 }
106 EXPORT_SYMBOL_GPL(videobuf_iolock);
107
108 void *videobuf_queue_to_vmalloc(struct videobuf_queue *q,
109                                 struct videobuf_buffer *buf)
110 {
111         if (q->int_ops->vmalloc)
112                 return q->int_ops->vmalloc(buf);
113         else
114                 return NULL;
115 }
116 EXPORT_SYMBOL_GPL(videobuf_queue_to_vmalloc);
117
118 /* --------------------------------------------------------------------- */
119
120
121 void videobuf_queue_core_init(struct videobuf_queue *q,
122                          const struct videobuf_queue_ops *ops,
123                          struct device *dev,
124                          spinlock_t *irqlock,
125                          enum v4l2_buf_type type,
126                          enum v4l2_field field,
127                          unsigned int msize,
128                          void *priv,
129                          struct videobuf_qtype_ops *int_ops)
130 {
131         BUG_ON(!q);
132         memset(q, 0, sizeof(*q));
133         q->irqlock   = irqlock;
134         q->dev       = dev;
135         q->type      = type;
136         q->field     = field;
137         q->msize     = msize;
138         q->ops       = ops;
139         q->priv_data = priv;
140         q->int_ops   = int_ops;
141
142         /* All buffer operations are mandatory */
143         BUG_ON(!q->ops->buf_setup);
144         BUG_ON(!q->ops->buf_prepare);
145         BUG_ON(!q->ops->buf_queue);
146         BUG_ON(!q->ops->buf_release);
147
148         /* Lock is mandatory for queue_cancel to work */
149         BUG_ON(!irqlock);
150
151         /* Having implementations for abstract methods are mandatory */
152         BUG_ON(!q->int_ops);
153
154         mutex_init(&q->vb_lock);
155         init_waitqueue_head(&q->wait);
156         INIT_LIST_HEAD(&q->stream);
157 }
158 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
159
160 /* Locking: Only usage in bttv unsafe find way to remove */
161 int videobuf_queue_is_busy(struct videobuf_queue *q)
162 {
163         int i;
164
165         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
166
167         if (q->streaming) {
168                 dprintk(1, "busy: streaming active\n");
169                 return 1;
170         }
171         if (q->reading) {
172                 dprintk(1, "busy: pending read #1\n");
173                 return 1;
174         }
175         if (q->read_buf) {
176                 dprintk(1, "busy: pending read #2\n");
177                 return 1;
178         }
179         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
180                 if (NULL == q->bufs[i])
181                         continue;
182                 if (q->bufs[i]->map) {
183                         dprintk(1, "busy: buffer #%d mapped\n", i);
184                         return 1;
185                 }
186                 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
187                         dprintk(1, "busy: buffer #%d queued\n", i);
188                         return 1;
189                 }
190                 if (q->bufs[i]->state == VIDEOBUF_ACTIVE) {
191                         dprintk(1, "busy: buffer #%d avtive\n", i);
192                         return 1;
193                 }
194         }
195         return 0;
196 }
197 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
198
199 /* Locking: Caller holds q->vb_lock */
200 void videobuf_queue_cancel(struct videobuf_queue *q)
201 {
202         unsigned long flags = 0;
203         int i;
204
205         q->streaming = 0;
206         q->reading  = 0;
207         wake_up_interruptible_sync(&q->wait);
208
209         /* remove queued buffers from list */
210         spin_lock_irqsave(q->irqlock, flags);
211         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
212                 if (NULL == q->bufs[i])
213                         continue;
214                 if (q->bufs[i]->state == VIDEOBUF_QUEUED) {
215                         list_del(&q->bufs[i]->queue);
216                         q->bufs[i]->state = VIDEOBUF_ERROR;
217                         wake_up_all(&q->bufs[i]->done);
218                 }
219         }
220         spin_unlock_irqrestore(q->irqlock, flags);
221
222         /* free all buffers + clear queue */
223         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
224                 if (NULL == q->bufs[i])
225                         continue;
226                 q->ops->buf_release(q, q->bufs[i]);
227         }
228         INIT_LIST_HEAD(&q->stream);
229 }
230 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
231
232 /* --------------------------------------------------------------------- */
233
234 /* Locking: Caller holds q->vb_lock */
235 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
236 {
237         enum v4l2_field field = q->field;
238
239         BUG_ON(V4L2_FIELD_ANY == field);
240
241         if (V4L2_FIELD_ALTERNATE == field) {
242                 if (V4L2_FIELD_TOP == q->last) {
243                         field   = V4L2_FIELD_BOTTOM;
244                         q->last = V4L2_FIELD_BOTTOM;
245                 } else {
246                         field   = V4L2_FIELD_TOP;
247                         q->last = V4L2_FIELD_TOP;
248                 }
249         }
250         return field;
251 }
252 EXPORT_SYMBOL_GPL(videobuf_next_field);
253
254 /* Locking: Caller holds q->vb_lock */
255 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
256                             struct videobuf_buffer *vb, enum v4l2_buf_type type)
257 {
258         MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
259         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
260
261         b->index    = vb->i;
262         b->type     = type;
263
264         b->memory   = vb->memory;
265         switch (b->memory) {
266         case V4L2_MEMORY_MMAP:
267                 b->m.offset  = vb->boff;
268                 b->length    = vb->bsize;
269                 break;
270         case V4L2_MEMORY_USERPTR:
271                 b->m.userptr = vb->baddr;
272                 b->length    = vb->bsize;
273                 break;
274         case V4L2_MEMORY_OVERLAY:
275                 b->m.offset  = vb->boff;
276                 break;
277         }
278
279         b->flags    = 0;
280         if (vb->map)
281                 b->flags |= V4L2_BUF_FLAG_MAPPED;
282
283         switch (vb->state) {
284         case VIDEOBUF_PREPARED:
285         case VIDEOBUF_QUEUED:
286         case VIDEOBUF_ACTIVE:
287                 b->flags |= V4L2_BUF_FLAG_QUEUED;
288                 break;
289         case VIDEOBUF_DONE:
290         case VIDEOBUF_ERROR:
291                 b->flags |= V4L2_BUF_FLAG_DONE;
292                 break;
293         case VIDEOBUF_NEEDS_INIT:
294         case VIDEOBUF_IDLE:
295                 /* nothing */
296                 break;
297         }
298
299         if (vb->input != UNSET) {
300                 b->flags |= V4L2_BUF_FLAG_INPUT;
301                 b->input  = vb->input;
302         }
303
304         b->field     = vb->field;
305         b->timestamp = vb->ts;
306         b->bytesused = vb->size;
307         b->sequence  = vb->field_count >> 1;
308 }
309
310 /* Locking: Caller holds q->vb_lock */
311 static int __videobuf_mmap_free(struct videobuf_queue *q)
312 {
313         int i;
314
315         if (!q)
316                 return 0;
317
318         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
319
320         for (i = 0; i < VIDEO_MAX_FRAME; i++)
321                 if (q->bufs[i] && q->bufs[i]->map)
322                         return -EBUSY;
323
324         q->is_mmapped = 0;
325
326         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
327                 if (NULL == q->bufs[i])
328                         continue;
329                 q->ops->buf_release(q, q->bufs[i]);
330                 kfree(q->bufs[i]);
331                 q->bufs[i] = NULL;
332         }
333
334         return 0;
335 }
336
337 int videobuf_mmap_free(struct videobuf_queue *q)
338 {
339         int ret;
340         mutex_lock(&q->vb_lock);
341         ret = __videobuf_mmap_free(q);
342         mutex_unlock(&q->vb_lock);
343         return ret;
344 }
345 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
346
347 /* Locking: Caller holds q->vb_lock */
348 int __videobuf_mmap_setup(struct videobuf_queue *q,
349                         unsigned int bcount, unsigned int bsize,
350                         enum v4l2_memory memory)
351 {
352         unsigned int i;
353         int err;
354
355         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
356
357         err = __videobuf_mmap_free(q);
358         if (0 != err)
359                 return err;
360
361         /* Allocate and initialize buffers */
362         for (i = 0; i < bcount; i++) {
363                 q->bufs[i] = videobuf_alloc(q);
364
365                 if (NULL == q->bufs[i])
366                         break;
367
368                 q->bufs[i]->i      = i;
369                 q->bufs[i]->input  = UNSET;
370                 q->bufs[i]->memory = memory;
371                 q->bufs[i]->bsize  = bsize;
372                 switch (memory) {
373                 case V4L2_MEMORY_MMAP:
374                         q->bufs[i]->boff = PAGE_ALIGN(bsize) * i;
375                         break;
376                 case V4L2_MEMORY_USERPTR:
377                 case V4L2_MEMORY_OVERLAY:
378                         /* nothing */
379                         break;
380                 }
381         }
382
383         if (!i)
384                 return -ENOMEM;
385
386         dprintk(1, "mmap setup: %d buffers, %d bytes each\n", i, bsize);
387
388         return i;
389 }
390 EXPORT_SYMBOL_GPL(__videobuf_mmap_setup);
391
392 int videobuf_mmap_setup(struct videobuf_queue *q,
393                         unsigned int bcount, unsigned int bsize,
394                         enum v4l2_memory memory)
395 {
396         int ret;
397         mutex_lock(&q->vb_lock);
398         ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
399         mutex_unlock(&q->vb_lock);
400         return ret;
401 }
402 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
403
404 int videobuf_reqbufs(struct videobuf_queue *q,
405                  struct v4l2_requestbuffers *req)
406 {
407         unsigned int size, count;
408         int retval;
409
410         if (req->count < 1) {
411                 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
412                 return -EINVAL;
413         }
414
415         if (req->memory != V4L2_MEMORY_MMAP     &&
416             req->memory != V4L2_MEMORY_USERPTR  &&
417             req->memory != V4L2_MEMORY_OVERLAY) {
418                 dprintk(1, "reqbufs: memory type invalid\n");
419                 return -EINVAL;
420         }
421
422         mutex_lock(&q->vb_lock);
423         if (req->type != q->type) {
424                 dprintk(1, "reqbufs: queue type invalid\n");
425                 retval = -EINVAL;
426                 goto done;
427         }
428
429         if (q->streaming) {
430                 dprintk(1, "reqbufs: streaming already exists\n");
431                 retval = -EBUSY;
432                 goto done;
433         }
434         if (!list_empty(&q->stream)) {
435                 dprintk(1, "reqbufs: stream running\n");
436                 retval = -EBUSY;
437                 goto done;
438         }
439
440         count = req->count;
441         if (count > VIDEO_MAX_FRAME)
442                 count = VIDEO_MAX_FRAME;
443         size = 0;
444         q->ops->buf_setup(q, &count, &size);
445         dprintk(1, "reqbufs: bufs=%d, size=0x%x [%u pages total]\n",
446                 count, size,
447                 (unsigned int)((count * PAGE_ALIGN(size)) >> PAGE_SHIFT));
448
449         retval = __videobuf_mmap_setup(q, count, size, req->memory);
450         if (retval < 0) {
451                 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
452                 goto done;
453         }
454
455         req->count = retval;
456         retval = 0;
457
458  done:
459         mutex_unlock(&q->vb_lock);
460         return retval;
461 }
462 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
463
464 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
465 {
466         int ret = -EINVAL;
467
468         mutex_lock(&q->vb_lock);
469         if (unlikely(b->type != q->type)) {
470                 dprintk(1, "querybuf: Wrong type.\n");
471                 goto done;
472         }
473         if (unlikely(b->index >= VIDEO_MAX_FRAME)) {
474                 dprintk(1, "querybuf: index out of range.\n");
475                 goto done;
476         }
477         if (unlikely(NULL == q->bufs[b->index])) {
478                 dprintk(1, "querybuf: buffer is null.\n");
479                 goto done;
480         }
481
482         videobuf_status(q, b, q->bufs[b->index], q->type);
483
484         ret = 0;
485 done:
486         mutex_unlock(&q->vb_lock);
487         return ret;
488 }
489 EXPORT_SYMBOL_GPL(videobuf_querybuf);
490
491 int videobuf_qbuf(struct videobuf_queue *q, struct v4l2_buffer *b)
492 {
493         struct videobuf_buffer *buf;
494         enum v4l2_field field;
495         unsigned long flags = 0;
496         int retval;
497
498         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
499
500         if (b->memory == V4L2_MEMORY_MMAP)
501                 down_read(&current->mm->mmap_sem);
502
503         mutex_lock(&q->vb_lock);
504         retval = -EBUSY;
505         if (q->reading) {
506                 dprintk(1, "qbuf: Reading running...\n");
507                 goto done;
508         }
509         retval = -EINVAL;
510         if (b->type != q->type) {
511                 dprintk(1, "qbuf: Wrong type.\n");
512                 goto done;
513         }
514         if (b->index >= VIDEO_MAX_FRAME) {
515                 dprintk(1, "qbuf: index out of range.\n");
516                 goto done;
517         }
518         buf = q->bufs[b->index];
519         if (NULL == buf) {
520                 dprintk(1, "qbuf: buffer is null.\n");
521                 goto done;
522         }
523         MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
524         if (buf->memory != b->memory) {
525                 dprintk(1, "qbuf: memory type is wrong.\n");
526                 goto done;
527         }
528         if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
529                 dprintk(1, "qbuf: buffer is already queued or active.\n");
530                 goto done;
531         }
532
533         if (b->flags & V4L2_BUF_FLAG_INPUT) {
534                 if (b->input >= q->inputs) {
535                         dprintk(1, "qbuf: wrong input.\n");
536                         goto done;
537                 }
538                 buf->input = b->input;
539         } else {
540                 buf->input = UNSET;
541         }
542
543         switch (b->memory) {
544         case V4L2_MEMORY_MMAP:
545                 if (0 == buf->baddr) {
546                         dprintk(1, "qbuf: mmap requested "
547                                    "but buffer addr is zero!\n");
548                         goto done;
549                 }
550                 break;
551         case V4L2_MEMORY_USERPTR:
552                 if (b->length < buf->bsize) {
553                         dprintk(1, "qbuf: buffer length is not enough\n");
554                         goto done;
555                 }
556                 if (VIDEOBUF_NEEDS_INIT != buf->state &&
557                     buf->baddr != b->m.userptr)
558                         q->ops->buf_release(q, buf);
559                 buf->baddr = b->m.userptr;
560                 break;
561         case V4L2_MEMORY_OVERLAY:
562                 buf->boff = b->m.offset;
563                 break;
564         default:
565                 dprintk(1, "qbuf: wrong memory type\n");
566                 goto done;
567         }
568
569         dprintk(1, "qbuf: requesting next field\n");
570         field = videobuf_next_field(q);
571         retval = q->ops->buf_prepare(q, buf, field);
572         if (0 != retval) {
573                 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
574                 goto done;
575         }
576
577         list_add_tail(&buf->stream, &q->stream);
578         if (q->streaming) {
579                 spin_lock_irqsave(q->irqlock, flags);
580                 q->ops->buf_queue(q, buf);
581                 spin_unlock_irqrestore(q->irqlock, flags);
582         }
583         dprintk(1, "qbuf: succeeded\n");
584         retval = 0;
585         wake_up_interruptible_sync(&q->wait);
586
587 done:
588         mutex_unlock(&q->vb_lock);
589
590         if (b->memory == V4L2_MEMORY_MMAP)
591                 up_read(&current->mm->mmap_sem);
592
593         return retval;
594 }
595 EXPORT_SYMBOL_GPL(videobuf_qbuf);
596
597 /* Locking: Caller holds q->vb_lock */
598 static int stream_next_buffer_check_queue(struct videobuf_queue *q, int noblock)
599 {
600         int retval;
601
602 checks:
603         if (!q->streaming) {
604                 dprintk(1, "next_buffer: Not streaming\n");
605                 retval = -EINVAL;
606                 goto done;
607         }
608
609         if (list_empty(&q->stream)) {
610                 if (noblock) {
611                         retval = -EAGAIN;
612                         dprintk(2, "next_buffer: no buffers to dequeue\n");
613                         goto done;
614                 } else {
615                         dprintk(2, "next_buffer: waiting on buffer\n");
616
617                         /* Drop lock to avoid deadlock with qbuf */
618                         mutex_unlock(&q->vb_lock);
619
620                         /* Checking list_empty and streaming is safe without
621                          * locks because we goto checks to validate while
622                          * holding locks before proceeding */
623                         retval = wait_event_interruptible(q->wait,
624                                 !list_empty(&q->stream) || !q->streaming);
625                         mutex_lock(&q->vb_lock);
626
627                         if (retval)
628                                 goto done;
629
630                         goto checks;
631                 }
632         }
633
634         retval = 0;
635
636 done:
637         return retval;
638 }
639
640 /* Locking: Caller holds q->vb_lock */
641 static int stream_next_buffer(struct videobuf_queue *q,
642                         struct videobuf_buffer **vb, int nonblocking)
643 {
644         int retval;
645         struct videobuf_buffer *buf = NULL;
646
647         retval = stream_next_buffer_check_queue(q, nonblocking);
648         if (retval)
649                 goto done;
650
651         buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
652         retval = videobuf_waiton(buf, nonblocking, 1);
653         if (retval < 0)
654                 goto done;
655
656         *vb = buf;
657 done:
658         return retval;
659 }
660
661 int videobuf_dqbuf(struct videobuf_queue *q,
662                    struct v4l2_buffer *b, int nonblocking)
663 {
664         struct videobuf_buffer *buf = NULL;
665         int retval;
666
667         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
668
669         mutex_lock(&q->vb_lock);
670
671         retval = stream_next_buffer(q, &buf, nonblocking);
672         if (retval < 0) {
673                 dprintk(1, "dqbuf: next_buffer error: %i\n", retval);
674                 goto done;
675         }
676
677         switch (buf->state) {
678         case VIDEOBUF_ERROR:
679                 dprintk(1, "dqbuf: state is error\n");
680                 retval = -EIO;
681                 CALL(q, sync, q, buf);
682                 buf->state = VIDEOBUF_IDLE;
683                 break;
684         case VIDEOBUF_DONE:
685                 dprintk(1, "dqbuf: state is done\n");
686                 CALL(q, sync, q, buf);
687                 buf->state = VIDEOBUF_IDLE;
688                 break;
689         default:
690                 dprintk(1, "dqbuf: state invalid\n");
691                 retval = -EINVAL;
692                 goto done;
693         }
694         list_del(&buf->stream);
695         memset(b, 0, sizeof(*b));
696         videobuf_status(q, b, buf, q->type);
697 done:
698         mutex_unlock(&q->vb_lock);
699         return retval;
700 }
701 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
702
703 int videobuf_streamon(struct videobuf_queue *q)
704 {
705         struct videobuf_buffer *buf;
706         unsigned long flags = 0;
707         int retval;
708
709         mutex_lock(&q->vb_lock);
710         retval = -EBUSY;
711         if (q->reading)
712                 goto done;
713         retval = 0;
714         if (q->streaming)
715                 goto done;
716         q->streaming = 1;
717         spin_lock_irqsave(q->irqlock, flags);
718         list_for_each_entry(buf, &q->stream, stream)
719                 if (buf->state == VIDEOBUF_PREPARED)
720                         q->ops->buf_queue(q, buf);
721         spin_unlock_irqrestore(q->irqlock, flags);
722
723         wake_up_interruptible_sync(&q->wait);
724 done:
725         mutex_unlock(&q->vb_lock);
726         return retval;
727 }
728 EXPORT_SYMBOL_GPL(videobuf_streamon);
729
730 /* Locking: Caller holds q->vb_lock */
731 static int __videobuf_streamoff(struct videobuf_queue *q)
732 {
733         if (!q->streaming)
734                 return -EINVAL;
735
736         videobuf_queue_cancel(q);
737
738         return 0;
739 }
740
741 int videobuf_streamoff(struct videobuf_queue *q)
742 {
743         int retval;
744
745         mutex_lock(&q->vb_lock);
746         retval = __videobuf_streamoff(q);
747         mutex_unlock(&q->vb_lock);
748
749         return retval;
750 }
751 EXPORT_SYMBOL_GPL(videobuf_streamoff);
752
753 /* Locking: Caller holds q->vb_lock */
754 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
755                                       char __user *data,
756                                       size_t count, loff_t *ppos)
757 {
758         enum v4l2_field field;
759         unsigned long flags = 0;
760         int retval;
761
762         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
763
764         /* setup stuff */
765         q->read_buf = videobuf_alloc(q);
766         if (NULL == q->read_buf)
767                 return -ENOMEM;
768
769         q->read_buf->memory = V4L2_MEMORY_USERPTR;
770         q->read_buf->baddr  = (unsigned long)data;
771         q->read_buf->bsize  = count;
772
773         field = videobuf_next_field(q);
774         retval = q->ops->buf_prepare(q, q->read_buf, field);
775         if (0 != retval)
776                 goto done;
777
778         /* start capture & wait */
779         spin_lock_irqsave(q->irqlock, flags);
780         q->ops->buf_queue(q, q->read_buf);
781         spin_unlock_irqrestore(q->irqlock, flags);
782         retval = videobuf_waiton(q->read_buf, 0, 0);
783         if (0 == retval) {
784                 CALL(q, sync, q, q->read_buf);
785                 if (VIDEOBUF_ERROR == q->read_buf->state)
786                         retval = -EIO;
787                 else
788                         retval = q->read_buf->size;
789         }
790
791 done:
792         /* cleanup */
793         q->ops->buf_release(q, q->read_buf);
794         kfree(q->read_buf);
795         q->read_buf = NULL;
796         return retval;
797 }
798
799 ssize_t videobuf_read_one(struct videobuf_queue *q,
800                           char __user *data, size_t count, loff_t *ppos,
801                           int nonblocking)
802 {
803         enum v4l2_field field;
804         unsigned long flags = 0;
805         unsigned size = 0, nbufs = 1;
806         int retval;
807
808         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
809
810         mutex_lock(&q->vb_lock);
811
812         q->ops->buf_setup(q, &nbufs, &size);
813
814         if (NULL == q->read_buf  &&
815             count >= size        &&
816             !nonblocking) {
817                 retval = videobuf_read_zerocopy(q, data, count, ppos);
818                 if (retval >= 0  ||  retval == -EIO)
819                         /* ok, all done */
820                         goto done;
821                 /* fallback to kernel bounce buffer on failures */
822         }
823
824         if (NULL == q->read_buf) {
825                 /* need to capture a new frame */
826                 retval = -ENOMEM;
827                 q->read_buf = videobuf_alloc(q);
828
829                 dprintk(1, "video alloc=0x%p\n", q->read_buf);
830                 if (NULL == q->read_buf)
831                         goto done;
832                 q->read_buf->memory = V4L2_MEMORY_USERPTR;
833                 q->read_buf->bsize = count; /* preferred size */
834                 field = videobuf_next_field(q);
835                 retval = q->ops->buf_prepare(q, q->read_buf, field);
836
837                 if (0 != retval) {
838                         kfree(q->read_buf);
839                         q->read_buf = NULL;
840                         goto done;
841                 }
842
843                 spin_lock_irqsave(q->irqlock, flags);
844                 q->ops->buf_queue(q, q->read_buf);
845                 spin_unlock_irqrestore(q->irqlock, flags);
846
847                 q->read_off = 0;
848         }
849
850         /* wait until capture is done */
851         retval = videobuf_waiton(q->read_buf, nonblocking, 1);
852         if (0 != retval)
853                 goto done;
854
855         CALL(q, sync, q, q->read_buf);
856
857         if (VIDEOBUF_ERROR == q->read_buf->state) {
858                 /* catch I/O errors */
859                 q->ops->buf_release(q, q->read_buf);
860                 kfree(q->read_buf);
861                 q->read_buf = NULL;
862                 retval = -EIO;
863                 goto done;
864         }
865
866         /* Copy to userspace */
867         retval = CALL(q, video_copy_to_user, q, data, count, nonblocking);
868         if (retval < 0)
869                 goto done;
870
871         q->read_off += retval;
872         if (q->read_off == q->read_buf->size) {
873                 /* all data copied, cleanup */
874                 q->ops->buf_release(q, q->read_buf);
875                 kfree(q->read_buf);
876                 q->read_buf = NULL;
877         }
878
879 done:
880         mutex_unlock(&q->vb_lock);
881         return retval;
882 }
883 EXPORT_SYMBOL_GPL(videobuf_read_one);
884
885 /* Locking: Caller holds q->vb_lock */
886 static int __videobuf_read_start(struct videobuf_queue *q)
887 {
888         enum v4l2_field field;
889         unsigned long flags = 0;
890         unsigned int count = 0, size = 0;
891         int err, i;
892
893         q->ops->buf_setup(q, &count, &size);
894         if (count < 2)
895                 count = 2;
896         if (count > VIDEO_MAX_FRAME)
897                 count = VIDEO_MAX_FRAME;
898         size = PAGE_ALIGN(size);
899
900         err = __videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
901         if (err < 0)
902                 return err;
903
904         count = err;
905
906         for (i = 0; i < count; i++) {
907                 field = videobuf_next_field(q);
908                 err = q->ops->buf_prepare(q, q->bufs[i], field);
909                 if (err)
910                         return err;
911                 list_add_tail(&q->bufs[i]->stream, &q->stream);
912         }
913         spin_lock_irqsave(q->irqlock, flags);
914         for (i = 0; i < count; i++)
915                 q->ops->buf_queue(q, q->bufs[i]);
916         spin_unlock_irqrestore(q->irqlock, flags);
917         q->reading = 1;
918         return 0;
919 }
920
921 static void __videobuf_read_stop(struct videobuf_queue *q)
922 {
923         int i;
924
925         videobuf_queue_cancel(q);
926         __videobuf_mmap_free(q);
927         INIT_LIST_HEAD(&q->stream);
928         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
929                 if (NULL == q->bufs[i])
930                         continue;
931                 kfree(q->bufs[i]);
932                 q->bufs[i] = NULL;
933         }
934         q->read_buf = NULL;
935 }
936
937 int videobuf_read_start(struct videobuf_queue *q)
938 {
939         int rc;
940
941         mutex_lock(&q->vb_lock);
942         rc = __videobuf_read_start(q);
943         mutex_unlock(&q->vb_lock);
944
945         return rc;
946 }
947 EXPORT_SYMBOL_GPL(videobuf_read_start);
948
949 void videobuf_read_stop(struct videobuf_queue *q)
950 {
951         mutex_lock(&q->vb_lock);
952         __videobuf_read_stop(q);
953         mutex_unlock(&q->vb_lock);
954 }
955 EXPORT_SYMBOL_GPL(videobuf_read_stop);
956
957 void videobuf_stop(struct videobuf_queue *q)
958 {
959         mutex_lock(&q->vb_lock);
960
961         if (q->streaming)
962                 __videobuf_streamoff(q);
963
964         if (q->reading)
965                 __videobuf_read_stop(q);
966
967         mutex_unlock(&q->vb_lock);
968 }
969 EXPORT_SYMBOL_GPL(videobuf_stop);
970
971 ssize_t videobuf_read_stream(struct videobuf_queue *q,
972                              char __user *data, size_t count, loff_t *ppos,
973                              int vbihack, int nonblocking)
974 {
975         int rc, retval;
976         unsigned long flags = 0;
977
978         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
979
980         dprintk(2, "%s\n", __func__);
981         mutex_lock(&q->vb_lock);
982         retval = -EBUSY;
983         if (q->streaming)
984                 goto done;
985         if (!q->reading) {
986                 retval = __videobuf_read_start(q);
987                 if (retval < 0)
988                         goto done;
989         }
990
991         retval = 0;
992         while (count > 0) {
993                 /* get / wait for data */
994                 if (NULL == q->read_buf) {
995                         q->read_buf = list_entry(q->stream.next,
996                                                  struct videobuf_buffer,
997                                                  stream);
998                         list_del(&q->read_buf->stream);
999                         q->read_off = 0;
1000                 }
1001                 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
1002                 if (rc < 0) {
1003                         if (0 == retval)
1004                                 retval = rc;
1005                         break;
1006                 }
1007
1008                 if (q->read_buf->state == VIDEOBUF_DONE) {
1009                         rc = CALL(q, copy_stream, q, data + retval, count,
1010                                         retval, vbihack, nonblocking);
1011                         if (rc < 0) {
1012                                 retval = rc;
1013                                 break;
1014                         }
1015                         retval      += rc;
1016                         count       -= rc;
1017                         q->read_off += rc;
1018                 } else {
1019                         /* some error */
1020                         q->read_off = q->read_buf->size;
1021                         if (0 == retval)
1022                                 retval = -EIO;
1023                 }
1024
1025                 /* requeue buffer when done with copying */
1026                 if (q->read_off == q->read_buf->size) {
1027                         list_add_tail(&q->read_buf->stream,
1028                                       &q->stream);
1029                         spin_lock_irqsave(q->irqlock, flags);
1030                         q->ops->buf_queue(q, q->read_buf);
1031                         spin_unlock_irqrestore(q->irqlock, flags);
1032                         q->read_buf = NULL;
1033                 }
1034                 if (retval < 0)
1035                         break;
1036         }
1037
1038 done:
1039         mutex_unlock(&q->vb_lock);
1040         return retval;
1041 }
1042 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1043
1044 unsigned int videobuf_poll_stream(struct file *file,
1045                                   struct videobuf_queue *q,
1046                                   poll_table *wait)
1047 {
1048         struct videobuf_buffer *buf = NULL;
1049         unsigned int rc = 0;
1050
1051         mutex_lock(&q->vb_lock);
1052         if (q->streaming) {
1053                 if (!list_empty(&q->stream))
1054                         buf = list_entry(q->stream.next,
1055                                          struct videobuf_buffer, stream);
1056         } else {
1057                 if (!q->reading)
1058                         __videobuf_read_start(q);
1059                 if (!q->reading) {
1060                         rc = POLLERR;
1061                 } else if (NULL == q->read_buf) {
1062                         q->read_buf = list_entry(q->stream.next,
1063                                                  struct videobuf_buffer,
1064                                                  stream);
1065                         list_del(&q->read_buf->stream);
1066                         q->read_off = 0;
1067                 }
1068                 buf = q->read_buf;
1069         }
1070         if (!buf)
1071                 rc = POLLERR;
1072
1073         if (0 == rc) {
1074                 poll_wait(file, &buf->done, wait);
1075                 if (buf->state == VIDEOBUF_DONE ||
1076                     buf->state == VIDEOBUF_ERROR)
1077                         rc = POLLIN|POLLRDNORM;
1078         }
1079         mutex_unlock(&q->vb_lock);
1080         return rc;
1081 }
1082 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1083
1084 int videobuf_mmap_mapper(struct videobuf_queue *q, struct vm_area_struct *vma)
1085 {
1086         int retval;
1087
1088         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1089
1090         mutex_lock(&q->vb_lock);
1091         retval = CALL(q, mmap_mapper, q, vma);
1092         q->is_mmapped = 1;
1093         mutex_unlock(&q->vb_lock);
1094
1095         return retval;
1096 }
1097 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1098
1099 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1100 int videobuf_cgmbuf(struct videobuf_queue *q,
1101                     struct video_mbuf *mbuf, int count)
1102 {
1103         struct v4l2_requestbuffers req;
1104         int rc, i;
1105
1106         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
1107
1108         memset(&req, 0, sizeof(req));
1109         req.type   = q->type;
1110         req.count  = count;
1111         req.memory = V4L2_MEMORY_MMAP;
1112         rc = videobuf_reqbufs(q, &req);
1113         if (rc < 0)
1114                 return rc;
1115
1116         mbuf->frames = req.count;
1117         mbuf->size   = 0;
1118         for (i = 0; i < mbuf->frames; i++) {
1119                 mbuf->offsets[i]  = q->bufs[i]->boff;
1120                 mbuf->size       += PAGE_ALIGN(q->bufs[i]->bsize);
1121         }
1122
1123         return 0;
1124 }
1125 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
1126 #endif
1127