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