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