V4L/DVB (7281): v4l: Deadlock in videobuf-core for DQBUF waiting on QBUF
[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                          void *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                 }
208         }
209         if (q->irqlock)
210                 spin_unlock_irqrestore(q->irqlock, flags);
211
212         /* free all buffers + clear queue */
213         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
214                 if (NULL == q->bufs[i])
215                         continue;
216                 q->ops->buf_release(q, q->bufs[i]);
217         }
218         INIT_LIST_HEAD(&q->stream);
219 }
220
221 /* --------------------------------------------------------------------- */
222
223 /* Locking: Caller holds q->vb_lock */
224 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
225 {
226         enum v4l2_field field = q->field;
227
228         BUG_ON(V4L2_FIELD_ANY == field);
229
230         if (V4L2_FIELD_ALTERNATE == field) {
231                 if (V4L2_FIELD_TOP == q->last) {
232                         field   = V4L2_FIELD_BOTTOM;
233                         q->last = V4L2_FIELD_BOTTOM;
234                 } else {
235                         field   = V4L2_FIELD_TOP;
236                         q->last = V4L2_FIELD_TOP;
237                 }
238         }
239         return field;
240 }
241
242 /* Locking: Caller holds q->vb_lock */
243 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
244                             struct videobuf_buffer *vb, enum v4l2_buf_type type)
245 {
246         MAGIC_CHECK(vb->magic, MAGIC_BUFFER);
247         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
248
249         b->index    = vb->i;
250         b->type     = type;
251
252         b->memory   = vb->memory;
253         switch (b->memory) {
254         case V4L2_MEMORY_MMAP:
255                 b->m.offset  = vb->boff;
256                 b->length    = vb->bsize;
257                 break;
258         case V4L2_MEMORY_USERPTR:
259                 b->m.userptr = vb->baddr;
260                 b->length    = vb->bsize;
261                 break;
262         case V4L2_MEMORY_OVERLAY:
263                 b->m.offset  = vb->boff;
264                 break;
265         }
266
267         b->flags    = 0;
268         if (vb->map)
269                 b->flags |= V4L2_BUF_FLAG_MAPPED;
270
271         switch (vb->state) {
272         case VIDEOBUF_PREPARED:
273         case VIDEOBUF_QUEUED:
274         case VIDEOBUF_ACTIVE:
275                 b->flags |= V4L2_BUF_FLAG_QUEUED;
276                 break;
277         case VIDEOBUF_DONE:
278         case VIDEOBUF_ERROR:
279                 b->flags |= V4L2_BUF_FLAG_DONE;
280                 break;
281         case VIDEOBUF_NEEDS_INIT:
282         case VIDEOBUF_IDLE:
283                 /* nothing */
284                 break;
285         }
286
287         if (vb->input != UNSET) {
288                 b->flags |= V4L2_BUF_FLAG_INPUT;
289                 b->input  = vb->input;
290         }
291
292         b->field     = vb->field;
293         b->timestamp = vb->ts;
294         b->bytesused = vb->size;
295         b->sequence  = vb->field_count >> 1;
296 }
297
298 /* Locking: Caller holds q->vb_lock */
299 static int __videobuf_mmap_free(struct videobuf_queue *q)
300 {
301         int i;
302         int rc;
303
304         if (!q)
305                 return 0;
306
307         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
308
309
310         rc  = CALL(q, mmap_free, q);
311
312         q->is_mmapped = 0;
313
314         if (rc < 0)
315                 return rc;
316
317         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
318                 if (NULL == q->bufs[i])
319                         continue;
320                 q->ops->buf_release(q, q->bufs[i]);
321                 kfree(q->bufs[i]);
322                 q->bufs[i] = NULL;
323         }
324
325         return rc;
326 }
327
328 int videobuf_mmap_free(struct videobuf_queue *q)
329 {
330         int ret;
331         mutex_lock(&q->vb_lock);
332         ret = __videobuf_mmap_free(q);
333         mutex_unlock(&q->vb_lock);
334         return ret;
335 }
336
337 /* Locking: Caller holds q->vb_lock */
338 static int __videobuf_mmap_setup(struct videobuf_queue *q,
339                         unsigned int bcount, unsigned int bsize,
340                         enum v4l2_memory memory)
341 {
342         unsigned int i;
343         int err;
344
345         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
346
347         err = __videobuf_mmap_free(q);
348         if (0 != err)
349                 return err;
350
351         /* Allocate and initialize buffers */
352         for (i = 0; i < bcount; i++) {
353                 q->bufs[i] = videobuf_alloc(q);
354
355                 if (q->bufs[i] == NULL)
356                         break;
357
358                 q->bufs[i]->i      = i;
359                 q->bufs[i]->input  = UNSET;
360                 q->bufs[i]->memory = memory;
361                 q->bufs[i]->bsize  = bsize;
362                 switch (memory) {
363                 case V4L2_MEMORY_MMAP:
364                         q->bufs[i]->boff  = bsize * i;
365                         break;
366                 case V4L2_MEMORY_USERPTR:
367                 case V4L2_MEMORY_OVERLAY:
368                         /* nothing */
369                         break;
370                 }
371         }
372
373         if (!i)
374                 return -ENOMEM;
375
376         dprintk(1, "mmap setup: %d buffers, %d bytes each\n",
377                 i, bsize);
378
379         return i;
380 }
381
382 int videobuf_mmap_setup(struct videobuf_queue *q,
383                         unsigned int bcount, unsigned int bsize,
384                         enum v4l2_memory memory)
385 {
386         int ret;
387         mutex_lock(&q->vb_lock);
388         ret = __videobuf_mmap_setup(q, bcount, bsize, memory);
389         mutex_unlock(&q->vb_lock);
390         return ret;
391 }
392
393 int videobuf_reqbufs(struct videobuf_queue *q,
394                  struct v4l2_requestbuffers *req)
395 {
396         unsigned int size, count;
397         int retval;
398
399         if (req->count < 1) {
400                 dprintk(1, "reqbufs: count invalid (%d)\n", req->count);
401                 return -EINVAL;
402         }
403
404         if (req->memory != V4L2_MEMORY_MMAP     &&
405             req->memory != V4L2_MEMORY_USERPTR  &&
406             req->memory != V4L2_MEMORY_OVERLAY) {
407                 dprintk(1, "reqbufs: memory type invalid\n");
408                 return -EINVAL;
409         }
410
411         mutex_lock(&q->vb_lock);
412         if (req->type != q->type) {
413                 dprintk(1, "reqbufs: queue type invalid\n");
414                 retval = -EINVAL;
415                 goto done;
416         }
417
418         if (q->streaming) {
419                 dprintk(1, "reqbufs: streaming already exists\n");
420                 retval = -EBUSY;
421                 goto done;
422         }
423         if (!list_empty(&q->stream)) {
424                 dprintk(1, "reqbufs: stream running\n");
425                 retval = -EBUSY;
426                 goto done;
427         }
428
429         count = req->count;
430         if (count > VIDEO_MAX_FRAME)
431                 count = VIDEO_MAX_FRAME;
432         size = 0;
433         q->ops->buf_setup(q, &count, &size);
434         size = PAGE_ALIGN(size);
435         dprintk(1, "reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
436                 count, size, (count*size)>>PAGE_SHIFT);
437
438         retval = __videobuf_mmap_setup(q, count, size, req->memory);
439         if (retval < 0) {
440                 dprintk(1, "reqbufs: mmap setup returned %d\n", retval);
441                 goto done;
442         }
443
444         req->count = retval;
445
446  done:
447         mutex_unlock(&q->vb_lock);
448         return retval;
449 }
450
451 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
452 {
453         int ret = -EINVAL;
454
455         mutex_lock(&q->vb_lock);
456         if (unlikely(b->type != q->type)) {
457                 dprintk(1, "querybuf: Wrong type.\n");
458                 goto done;
459         }
460         if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
461                 dprintk(1, "querybuf: index out of range.\n");
462                 goto done;
463         }
464         if (unlikely(NULL == q->bufs[b->index])) {
465                 dprintk(1, "querybuf: buffer is null.\n");
466                 goto done;
467         }
468
469         videobuf_status(q, b, q->bufs[b->index], q->type);
470
471         ret = 0;
472 done:
473         mutex_unlock(&q->vb_lock);
474         return ret;
475 }
476
477 int videobuf_qbuf(struct videobuf_queue *q,
478               struct v4l2_buffer *b)
479 {
480         struct videobuf_buffer *buf;
481         enum v4l2_field field;
482         unsigned long flags = 0;
483         int retval;
484
485         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
486
487         if (b->memory == V4L2_MEMORY_MMAP)
488                 down_read(&current->mm->mmap_sem);
489
490         mutex_lock(&q->vb_lock);
491         retval = -EBUSY;
492         if (q->reading) {
493                 dprintk(1, "qbuf: Reading running...\n");
494                 goto done;
495         }
496         retval = -EINVAL;
497         if (b->type != q->type) {
498                 dprintk(1, "qbuf: Wrong type.\n");
499                 goto done;
500         }
501         if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
502                 dprintk(1, "qbuf: index out of range.\n");
503                 goto done;
504         }
505         buf = q->bufs[b->index];
506         if (NULL == buf) {
507                 dprintk(1, "qbuf: buffer is null.\n");
508                 goto done;
509         }
510         MAGIC_CHECK(buf->magic, MAGIC_BUFFER);
511         if (buf->memory != b->memory) {
512                 dprintk(1, "qbuf: memory type is wrong.\n");
513                 goto done;
514         }
515         if (buf->state != VIDEOBUF_NEEDS_INIT && buf->state != VIDEOBUF_IDLE) {
516                 dprintk(1, "qbuf: buffer is already queued or active.\n");
517                 goto done;
518         }
519
520         if (b->flags & V4L2_BUF_FLAG_INPUT) {
521                 if (b->input >= q->inputs) {
522                         dprintk(1, "qbuf: wrong input.\n");
523                         goto done;
524                 }
525                 buf->input = b->input;
526         } else {
527                 buf->input = UNSET;
528         }
529
530         switch (b->memory) {
531         case V4L2_MEMORY_MMAP:
532                 if (0 == buf->baddr) {
533                         dprintk(1, "qbuf: mmap requested "
534                                    "but buffer addr is zero!\n");
535                         goto done;
536                 }
537                 break;
538         case V4L2_MEMORY_USERPTR:
539                 if (b->length < buf->bsize) {
540                         dprintk(1, "qbuf: buffer length is not enough\n");
541                         goto done;
542                 }
543                 if (VIDEOBUF_NEEDS_INIT != buf->state &&
544                     buf->baddr != b->m.userptr)
545                         q->ops->buf_release(q, buf);
546                 buf->baddr = b->m.userptr;
547                 break;
548         case V4L2_MEMORY_OVERLAY:
549                 buf->boff = b->m.offset;
550                 break;
551         default:
552                 dprintk(1, "qbuf: wrong memory type\n");
553                 goto done;
554         }
555
556         dprintk(1, "qbuf: requesting next field\n");
557         field = videobuf_next_field(q);
558         retval = q->ops->buf_prepare(q, buf, field);
559         if (0 != retval) {
560                 dprintk(1, "qbuf: buffer_prepare returned %d\n", retval);
561                 goto done;
562         }
563
564         list_add_tail(&buf->stream, &q->stream);
565         if (q->streaming) {
566                 if (q->irqlock)
567                         spin_lock_irqsave(q->irqlock, flags);
568                 q->ops->buf_queue(q, buf);
569                 if (q->irqlock)
570                         spin_unlock_irqrestore(q->irqlock, flags);
571         }
572         dprintk(1, "qbuf: succeded\n");
573         retval = 0;
574
575  done:
576         mutex_unlock(&q->vb_lock);
577
578         if (b->memory == V4L2_MEMORY_MMAP)
579                 up_read(&current->mm->mmap_sem);
580
581         return retval;
582 }
583
584 int videobuf_dqbuf(struct videobuf_queue *q,
585                struct v4l2_buffer *b, int nonblocking)
586 {
587         struct videobuf_buffer *buf;
588         int retval;
589
590         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
591
592         mutex_lock(&q->vb_lock);
593         retval = -EBUSY;
594         if (q->reading) {
595                 dprintk(1, "dqbuf: Reading running...\n");
596                 goto done;
597         }
598         retval = -EINVAL;
599         if (b->type != q->type) {
600                 dprintk(1, "dqbuf: Wrong type.\n");
601                 goto done;
602         }
603         if (list_empty(&q->stream)) {
604                 dprintk(1, "dqbuf: stream running\n");
605                 goto done;
606         }
607         buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
608         mutex_unlock(&q->vb_lock);
609         retval = videobuf_waiton(buf, nonblocking, 1);
610         mutex_lock(&q->vb_lock);
611         if (retval < 0) {
612                 dprintk(1, "dqbuf: waiton returned %d\n", retval);
613                 goto done;
614         }
615         switch (buf->state) {
616         case VIDEOBUF_ERROR:
617                 dprintk(1, "dqbuf: state is error\n");
618                 retval = -EIO;
619                 CALL(q, sync, q, buf);
620                 buf->state = VIDEOBUF_IDLE;
621                 break;
622         case VIDEOBUF_DONE:
623                 dprintk(1, "dqbuf: state is done\n");
624                 CALL(q, sync, q, buf);
625                 buf->state = VIDEOBUF_IDLE;
626                 break;
627         default:
628                 dprintk(1, "dqbuf: state invalid\n");
629                 retval = -EINVAL;
630                 goto done;
631         }
632         list_del(&buf->stream);
633         memset(b, 0, sizeof(*b));
634         videobuf_status(q, b, buf, q->type);
635
636  done:
637         mutex_unlock(&q->vb_lock);
638         return retval;
639 }
640
641 int videobuf_streamon(struct videobuf_queue *q)
642 {
643         struct videobuf_buffer *buf;
644         unsigned long flags = 0;
645         int retval;
646
647         mutex_lock(&q->vb_lock);
648         retval = -EBUSY;
649         if (q->reading)
650                 goto done;
651         retval = 0;
652         if (q->streaming)
653                 goto done;
654         q->streaming = 1;
655         if (q->irqlock)
656                 spin_lock_irqsave(q->irqlock, flags);
657         list_for_each_entry(buf, &q->stream, stream)
658                 if (buf->state == VIDEOBUF_PREPARED)
659                         q->ops->buf_queue(q, buf);
660         if (q->irqlock)
661                 spin_unlock_irqrestore(q->irqlock, flags);
662
663  done:
664         mutex_unlock(&q->vb_lock);
665         return retval;
666 }
667
668 /* Locking: Caller holds q->vb_lock */
669 static int __videobuf_streamoff(struct videobuf_queue *q)
670 {
671         if (!q->streaming)
672                 return -EINVAL;
673
674         videobuf_queue_cancel(q);
675         q->streaming = 0;
676
677         return 0;
678 }
679
680 int videobuf_streamoff(struct videobuf_queue *q)
681 {
682         int retval;
683
684         mutex_lock(&q->vb_lock);
685         retval = __videobuf_streamoff(q);
686         mutex_unlock(&q->vb_lock);
687
688         return retval;
689 }
690
691 /* Locking: Caller holds q->vb_lock */
692 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
693                                       char __user *data,
694                                       size_t count, loff_t *ppos)
695 {
696         enum v4l2_field field;
697         unsigned long flags = 0;
698         int retval;
699
700         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
701
702         /* setup stuff */
703         q->read_buf = videobuf_alloc(q);
704         if (NULL == q->read_buf)
705                 return -ENOMEM;
706
707         q->read_buf->memory = V4L2_MEMORY_USERPTR;
708         q->read_buf->baddr  = (unsigned long)data;
709         q->read_buf->bsize  = count;
710
711         field = videobuf_next_field(q);
712         retval = q->ops->buf_prepare(q, q->read_buf, field);
713         if (0 != retval)
714                 goto done;
715
716         /* start capture & wait */
717         if (q->irqlock)
718                 spin_lock_irqsave(q->irqlock, flags);
719         q->ops->buf_queue(q, q->read_buf);
720         if (q->irqlock)
721                 spin_unlock_irqrestore(q->irqlock, flags);
722         retval = videobuf_waiton(q->read_buf, 0, 0);
723         if (0 == retval) {
724                 CALL(q, sync, q, q->read_buf);
725                 if (VIDEOBUF_ERROR == q->read_buf->state)
726                         retval = -EIO;
727                 else
728                         retval = q->read_buf->size;
729         }
730
731  done:
732         /* cleanup */
733         q->ops->buf_release(q, q->read_buf);
734         kfree(q->read_buf);
735         q->read_buf = NULL;
736         return retval;
737 }
738
739 ssize_t videobuf_read_one(struct videobuf_queue *q,
740                           char __user *data, size_t count, loff_t *ppos,
741                           int nonblocking)
742 {
743         enum v4l2_field field;
744         unsigned long flags = 0;
745         unsigned size, nbufs;
746         int retval;
747
748         MAGIC_CHECK(q->int_ops->magic, MAGIC_QTYPE_OPS);
749
750         mutex_lock(&q->vb_lock);
751
752         nbufs = 1; size = 0;
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);