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