Merge branch 'block-2.6.24' of git://git.kernel.dk/data/git/linux-2.6-block
[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 int videobuf_queue_is_busy(struct videobuf_queue *q)
145 {
146         int i;
147
148         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
149
150         if (q->streaming) {
151                 dprintk(1,"busy: streaming active\n");
152                 return 1;
153         }
154         if (q->reading) {
155                 dprintk(1,"busy: pending read #1\n");
156                 return 1;
157         }
158         if (q->read_buf) {
159                 dprintk(1,"busy: pending read #2\n");
160                 return 1;
161         }
162         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
163                 if (NULL == q->bufs[i])
164                         continue;
165                 if (q->bufs[i]->map) {
166                         dprintk(1,"busy: buffer #%d mapped\n",i);
167                         return 1;
168                 }
169                 if (q->bufs[i]->state == STATE_QUEUED) {
170                         dprintk(1,"busy: buffer #%d queued\n",i);
171                         return 1;
172                 }
173                 if (q->bufs[i]->state == STATE_ACTIVE) {
174                         dprintk(1,"busy: buffer #%d avtive\n",i);
175                         return 1;
176                 }
177         }
178         return 0;
179 }
180
181 void videobuf_queue_cancel(struct videobuf_queue *q)
182 {
183         unsigned long flags=0;
184         int i;
185
186         /* remove queued buffers from list */
187         if (q->irqlock)
188                 spin_lock_irqsave(q->irqlock,flags);
189         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
190                 if (NULL == q->bufs[i])
191                         continue;
192                 if (q->bufs[i]->state == STATE_QUEUED) {
193                         list_del(&q->bufs[i]->queue);
194                         q->bufs[i]->state = STATE_ERROR;
195                 }
196         }
197         if (q->irqlock)
198                 spin_unlock_irqrestore(q->irqlock,flags);
199
200         /* free all buffers + clear queue */
201         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
202                 if (NULL == q->bufs[i])
203                         continue;
204                 q->ops->buf_release(q,q->bufs[i]);
205         }
206         INIT_LIST_HEAD(&q->stream);
207 }
208
209 /* --------------------------------------------------------------------- */
210
211 enum v4l2_field videobuf_next_field(struct videobuf_queue *q)
212 {
213         enum v4l2_field field = q->field;
214
215         BUG_ON(V4L2_FIELD_ANY == field);
216
217         if (V4L2_FIELD_ALTERNATE == field) {
218                 if (V4L2_FIELD_TOP == q->last) {
219                         field   = V4L2_FIELD_BOTTOM;
220                         q->last = V4L2_FIELD_BOTTOM;
221                 } else {
222                         field   = V4L2_FIELD_TOP;
223                         q->last = V4L2_FIELD_TOP;
224                 }
225         }
226         return field;
227 }
228
229 static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
230                             struct videobuf_buffer *vb, enum v4l2_buf_type type)
231 {
232         MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
233         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
234
235         b->index    = vb->i;
236         b->type     = type;
237
238         b->memory   = vb->memory;
239         switch (b->memory) {
240         case V4L2_MEMORY_MMAP:
241                 b->m.offset  = vb->boff;
242                 b->length    = vb->bsize;
243                 break;
244         case V4L2_MEMORY_USERPTR:
245                 b->m.userptr = vb->baddr;
246                 b->length    = vb->bsize;
247                 break;
248         case V4L2_MEMORY_OVERLAY:
249                 b->m.offset  = vb->boff;
250                 break;
251         }
252
253         b->flags    = 0;
254         if (vb->map)
255                 b->flags |= V4L2_BUF_FLAG_MAPPED;
256
257         switch (vb->state) {
258         case STATE_PREPARED:
259         case STATE_QUEUED:
260         case STATE_ACTIVE:
261                 b->flags |= V4L2_BUF_FLAG_QUEUED;
262                 break;
263         case STATE_DONE:
264         case STATE_ERROR:
265                 b->flags |= V4L2_BUF_FLAG_DONE;
266                 break;
267         case STATE_NEEDS_INIT:
268         case STATE_IDLE:
269                 /* nothing */
270                 break;
271         }
272
273         if (vb->input != UNSET) {
274                 b->flags |= V4L2_BUF_FLAG_INPUT;
275                 b->input  = vb->input;
276         }
277
278         b->field     = vb->field;
279         b->timestamp = vb->ts;
280         b->bytesused = vb->size;
281         b->sequence  = vb->field_count >> 1;
282 }
283
284 int videobuf_reqbufs(struct videobuf_queue *q,
285                  struct v4l2_requestbuffers *req)
286 {
287         unsigned int size,count;
288         int retval;
289
290         if (req->type != q->type) {
291                 dprintk(1,"reqbufs: queue type invalid\n");
292                 return -EINVAL;
293         }
294         if (req->count < 1) {
295                 dprintk(1,"reqbufs: count invalid (%d)\n",req->count);
296                 return -EINVAL;
297         }
298         if (req->memory != V4L2_MEMORY_MMAP     &&
299             req->memory != V4L2_MEMORY_USERPTR  &&
300             req->memory != V4L2_MEMORY_OVERLAY) {
301                 dprintk(1,"reqbufs: memory type invalid\n");
302                 return -EINVAL;
303         }
304
305         mutex_lock(&q->lock);
306         if (q->streaming) {
307                 dprintk(1,"reqbufs: streaming already exists\n");
308                 retval = -EBUSY;
309                 goto done;
310         }
311         if (!list_empty(&q->stream)) {
312                 dprintk(1,"reqbufs: stream running\n");
313                 retval = -EBUSY;
314                 goto done;
315         }
316
317         count = req->count;
318         if (count > VIDEO_MAX_FRAME)
319                 count = VIDEO_MAX_FRAME;
320         size = 0;
321         q->ops->buf_setup(q,&count,&size);
322         size = PAGE_ALIGN(size);
323         dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
324                 count, size, (count*size)>>PAGE_SHIFT);
325
326         retval = videobuf_mmap_setup(q,count,size,req->memory);
327         if (retval < 0) {
328                 dprintk(1,"reqbufs: mmap setup returned %d\n",retval);
329                 goto done;
330         }
331
332         req->count = retval;
333
334  done:
335         mutex_unlock(&q->lock);
336         return retval;
337 }
338
339 int videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
340 {
341         if (unlikely(b->type != q->type)) {
342                 dprintk(1,"querybuf: Wrong type.\n");
343                 return -EINVAL;
344         }
345         if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
346                 dprintk(1,"querybuf: index out of range.\n");
347                 return -EINVAL;
348         }
349         if (unlikely(NULL == q->bufs[b->index])) {
350                 dprintk(1,"querybuf: buffer is null.\n");
351                 return -EINVAL;
352         }
353         videobuf_status(q,b,q->bufs[b->index],q->type);
354         return 0;
355 }
356
357 int videobuf_qbuf(struct videobuf_queue *q,
358               struct v4l2_buffer *b)
359 {
360         struct videobuf_buffer *buf;
361         enum v4l2_field field;
362         unsigned long flags=0;
363         int retval;
364
365         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
366
367         if (b->memory == V4L2_MEMORY_MMAP)
368                 down_read(&current->mm->mmap_sem);
369
370         mutex_lock(&q->lock);
371         retval = -EBUSY;
372         if (q->reading) {
373                 dprintk(1,"qbuf: Reading running...\n");
374                 goto done;
375         }
376         retval = -EINVAL;
377         if (b->type != q->type) {
378                 dprintk(1,"qbuf: Wrong type.\n");
379                 goto done;
380         }
381         if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
382                 dprintk(1,"qbuf: index out of range.\n");
383                 goto done;
384         }
385         buf = q->bufs[b->index];
386         if (NULL == buf) {
387                 dprintk(1,"qbuf: buffer is null.\n");
388                 goto done;
389         }
390         MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
391         if (buf->memory != b->memory) {
392                 dprintk(1,"qbuf: memory type is wrong.\n");
393                 goto done;
394         }
395         if (buf->state != STATE_NEEDS_INIT && buf->state != STATE_IDLE) {
396                 dprintk(1,"qbuf: buffer is already queued or active.\n");
397                 goto done;
398         }
399
400         if (b->flags & V4L2_BUF_FLAG_INPUT) {
401                 if (b->input >= q->inputs) {
402                         dprintk(1,"qbuf: wrong input.\n");
403                         goto done;
404                 }
405                 buf->input = b->input;
406         } else {
407                 buf->input = UNSET;
408         }
409
410         switch (b->memory) {
411         case V4L2_MEMORY_MMAP:
412                 if (0 == buf->baddr) {
413                         dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n");
414                         goto done;
415                 }
416                 break;
417         case V4L2_MEMORY_USERPTR:
418                 if (b->length < buf->bsize) {
419                         dprintk(1,"qbuf: buffer length is not enough\n");
420                         goto done;
421                 }
422                 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
423                         q->ops->buf_release(q,buf);
424                 buf->baddr = b->m.userptr;
425                 break;
426         case V4L2_MEMORY_OVERLAY:
427                 buf->boff = b->m.offset;
428                 break;
429         default:
430                 dprintk(1,"qbuf: wrong memory type\n");
431                 goto done;
432         }
433
434         dprintk(1,"qbuf: requesting next field\n");
435         field = videobuf_next_field(q);
436         retval = q->ops->buf_prepare(q,buf,field);
437         if (0 != retval) {
438                 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval);
439                 goto done;
440         }
441
442         list_add_tail(&buf->stream,&q->stream);
443         if (q->streaming) {
444                 if (q->irqlock)
445                         spin_lock_irqsave(q->irqlock,flags);
446                 q->ops->buf_queue(q,buf);
447                 if (q->irqlock)
448                         spin_unlock_irqrestore(q->irqlock,flags);
449         }
450         dprintk(1,"qbuf: succeded\n");
451         retval = 0;
452
453  done:
454         mutex_unlock(&q->lock);
455
456         if (b->memory == V4L2_MEMORY_MMAP)
457                 up_read(&current->mm->mmap_sem);
458
459         return retval;
460 }
461
462 int videobuf_dqbuf(struct videobuf_queue *q,
463                struct v4l2_buffer *b, int nonblocking)
464 {
465         struct videobuf_buffer *buf;
466         int retval;
467
468         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
469
470         mutex_lock(&q->lock);
471         retval = -EBUSY;
472         if (q->reading) {
473                 dprintk(1,"dqbuf: Reading running...\n");
474                 goto done;
475         }
476         retval = -EINVAL;
477         if (b->type != q->type) {
478                 dprintk(1,"dqbuf: Wrong type.\n");
479                 goto done;
480         }
481         if (list_empty(&q->stream)) {
482                 dprintk(1,"dqbuf: stream running\n");
483                 goto done;
484         }
485         buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
486         retval = videobuf_waiton(buf, nonblocking, 1);
487         if (retval < 0) {
488                 dprintk(1,"dqbuf: waiton returned %d\n",retval);
489                 goto done;
490         }
491         switch (buf->state) {
492         case STATE_ERROR:
493                 dprintk(1,"dqbuf: state is error\n");
494                 retval = -EIO;
495                 CALL(q,sync,q, buf);
496                 buf->state = STATE_IDLE;
497                 break;
498         case STATE_DONE:
499                 dprintk(1,"dqbuf: state is done\n");
500                 CALL(q,sync,q, buf);
501                 buf->state = STATE_IDLE;
502                 break;
503         default:
504                 dprintk(1,"dqbuf: state invalid\n");
505                 retval = -EINVAL;
506                 goto done;
507         }
508         list_del(&buf->stream);
509         memset(b,0,sizeof(*b));
510         videobuf_status(q,b,buf,q->type);
511
512  done:
513         mutex_unlock(&q->lock);
514         return retval;
515 }
516
517 int videobuf_streamon(struct videobuf_queue *q)
518 {
519         struct videobuf_buffer *buf;
520         unsigned long flags=0;
521         int retval;
522
523         mutex_lock(&q->lock);
524         retval = -EBUSY;
525         if (q->reading)
526                 goto done;
527         retval = 0;
528         if (q->streaming)
529                 goto done;
530         q->streaming = 1;
531         if (q->irqlock)
532                 spin_lock_irqsave(q->irqlock,flags);
533         list_for_each_entry(buf, &q->stream, stream)
534                 if (buf->state == STATE_PREPARED)
535                         q->ops->buf_queue(q,buf);
536         if (q->irqlock)
537                 spin_unlock_irqrestore(q->irqlock,flags);
538
539  done:
540         mutex_unlock(&q->lock);
541         return retval;
542 }
543
544 int videobuf_streamoff(struct videobuf_queue *q)
545 {
546         int retval = -EINVAL;
547
548         mutex_lock(&q->lock);
549         if (!q->streaming)
550                 goto done;
551         videobuf_queue_cancel(q);
552         q->streaming = 0;
553         retval = 0;
554
555  done:
556         mutex_unlock(&q->lock);
557         return retval;
558 }
559
560 static ssize_t videobuf_read_zerocopy(struct videobuf_queue *q,
561                                       char __user *data,
562                                       size_t count, loff_t *ppos)
563 {
564         enum v4l2_field field;
565         unsigned long flags=0;
566         int retval;
567
568         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
569
570         /* setup stuff */
571         q->read_buf = videobuf_alloc(q);
572         if (NULL == q->read_buf)
573                 return -ENOMEM;
574
575         q->read_buf->memory = V4L2_MEMORY_USERPTR;
576         q->read_buf->baddr  = (unsigned long)data;
577         q->read_buf->bsize  = count;
578
579         field = videobuf_next_field(q);
580         retval = q->ops->buf_prepare(q,q->read_buf,field);
581         if (0 != retval)
582                 goto done;
583
584         /* start capture & wait */
585         if (q->irqlock)
586                 spin_lock_irqsave(q->irqlock,flags);
587         q->ops->buf_queue(q,q->read_buf);
588         if (q->irqlock)
589                 spin_unlock_irqrestore(q->irqlock,flags);
590         retval = videobuf_waiton(q->read_buf,0,0);
591         if (0 == retval) {
592                 CALL(q,sync,q,q->read_buf);
593                 if (STATE_ERROR == q->read_buf->state)
594                         retval = -EIO;
595                 else
596                         retval = q->read_buf->size;
597         }
598
599  done:
600         /* cleanup */
601         q->ops->buf_release(q,q->read_buf);
602         kfree(q->read_buf);
603         q->read_buf = NULL;
604         return retval;
605 }
606
607 ssize_t videobuf_read_one(struct videobuf_queue *q,
608                           char __user *data, size_t count, loff_t *ppos,
609                           int nonblocking)
610 {
611         enum v4l2_field field;
612         unsigned long flags=0;
613         unsigned size, nbufs;
614         int retval;
615
616         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
617
618         mutex_lock(&q->lock);
619
620         nbufs = 1; size = 0;
621         q->ops->buf_setup(q,&nbufs,&size);
622
623         if (NULL == q->read_buf  &&
624             count >= size        &&
625             !nonblocking) {
626                 retval = videobuf_read_zerocopy(q,data,count,ppos);
627                 if (retval >= 0  ||  retval == -EIO)
628                         /* ok, all done */
629                         goto done;
630                 /* fallback to kernel bounce buffer on failures */
631         }
632
633         if (NULL == q->read_buf) {
634                 /* need to capture a new frame */
635                 retval = -ENOMEM;
636                 q->read_buf = videobuf_alloc(q);
637
638                 dprintk(1,"video alloc=0x%p\n", q->read_buf);
639                 if (NULL == q->read_buf)
640                         goto done;
641                 q->read_buf->memory = V4L2_MEMORY_USERPTR;
642                 q->read_buf->bsize = count; /* preferred size */
643                 field = videobuf_next_field(q);
644                 retval = q->ops->buf_prepare(q,q->read_buf,field);
645
646                 if (0 != retval) {
647                         kfree (q->read_buf);
648                         q->read_buf = NULL;
649                         goto done;
650                 }
651                 if (q->irqlock)
652                         spin_lock_irqsave(q->irqlock,flags);
653
654                 q->ops->buf_queue(q,q->read_buf);
655                 if (q->irqlock)
656                         spin_unlock_irqrestore(q->irqlock,flags);
657                 q->read_off = 0;
658         }
659
660         /* wait until capture is done */
661         retval = videobuf_waiton(q->read_buf, nonblocking, 1);
662         if (0 != retval)
663                 goto done;
664
665         CALL(q,sync,q,q->read_buf);
666
667         if (STATE_ERROR == q->read_buf->state) {
668                 /* catch I/O errors */
669                 q->ops->buf_release(q,q->read_buf);
670                 kfree(q->read_buf);
671                 q->read_buf = NULL;
672                 retval = -EIO;
673                 goto done;
674         }
675
676         /* Copy to userspace */
677         retval=CALL(q,copy_to_user,q,data,count,nonblocking);
678         if (retval<0)
679                 goto done;
680
681         q->read_off += retval;
682         if (q->read_off == q->read_buf->size) {
683                 /* all data copied, cleanup */
684                 q->ops->buf_release(q,q->read_buf);
685                 kfree(q->read_buf);
686                 q->read_buf = NULL;
687         }
688
689  done:
690         mutex_unlock(&q->lock);
691         return retval;
692 }
693
694 int videobuf_read_start(struct videobuf_queue *q)
695 {
696         enum v4l2_field field;
697         unsigned long flags=0;
698         unsigned int count = 0, size = 0;
699         int err, i;
700
701         q->ops->buf_setup(q,&count,&size);
702         if (count < 2)
703                 count = 2;
704         if (count > VIDEO_MAX_FRAME)
705                 count = VIDEO_MAX_FRAME;
706         size = PAGE_ALIGN(size);
707
708         err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
709         if (err < 0)
710                 return err;
711
712         count = err;
713
714         for (i = 0; i < count; i++) {
715                 field = videobuf_next_field(q);
716                 err = q->ops->buf_prepare(q,q->bufs[i],field);
717                 if (err)
718                         return err;
719                 list_add_tail(&q->bufs[i]->stream, &q->stream);
720         }
721         if (q->irqlock)
722                 spin_lock_irqsave(q->irqlock,flags);
723         for (i = 0; i < count; i++)
724                 q->ops->buf_queue(q,q->bufs[i]);
725         if (q->irqlock)
726                 spin_unlock_irqrestore(q->irqlock,flags);
727         q->reading = 1;
728         return 0;
729 }
730
731 void videobuf_read_stop(struct videobuf_queue *q)
732 {
733         int i;
734
735         videobuf_queue_cancel(q);
736         videobuf_mmap_free(q);
737         INIT_LIST_HEAD(&q->stream);
738         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
739                 if (NULL == q->bufs[i])
740                         continue;
741                 kfree(q->bufs[i]);
742                 q->bufs[i] = NULL;
743         }
744         q->read_buf = NULL;
745         q->reading  = 0;
746 }
747
748 ssize_t videobuf_read_stream(struct videobuf_queue *q,
749                              char __user *data, size_t count, loff_t *ppos,
750                              int vbihack, int nonblocking)
751 {
752         int rc, retval;
753         unsigned long flags=0;
754
755         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
756
757         dprintk(2,"%s\n",__FUNCTION__);
758         mutex_lock(&q->lock);
759         retval = -EBUSY;
760         if (q->streaming)
761                 goto done;
762         if (!q->reading) {
763                 retval = videobuf_read_start(q);
764                 if (retval < 0)
765                         goto done;
766         }
767
768         retval = 0;
769         while (count > 0) {
770                 /* get / wait for data */
771                 if (NULL == q->read_buf) {
772                         q->read_buf = list_entry(q->stream.next,
773                                                  struct videobuf_buffer,
774                                                  stream);
775                         list_del(&q->read_buf->stream);
776                         q->read_off = 0;
777                 }
778                 rc = videobuf_waiton(q->read_buf, nonblocking, 1);
779                 if (rc < 0) {
780                         if (0 == retval)
781                                 retval = rc;
782                         break;
783                 }
784
785                 if (q->read_buf->state == STATE_DONE) {
786                         rc = CALL (q,copy_stream, q, data + retval, count,
787                                         retval, vbihack, nonblocking);
788                         if (rc < 0) {
789                                 retval = rc;
790                                 break;
791                         }
792                         retval      += rc;
793                         count       -= rc;
794                         q->read_off += rc;
795                 } else {
796                         /* some error */
797                         q->read_off = q->read_buf->size;
798                         if (0 == retval)
799                                 retval = -EIO;
800                 }
801
802                 /* requeue buffer when done with copying */
803                 if (q->read_off == q->read_buf->size) {
804                         list_add_tail(&q->read_buf->stream,
805                                       &q->stream);
806                         if (q->irqlock)
807                                 spin_lock_irqsave(q->irqlock,flags);
808                         q->ops->buf_queue(q,q->read_buf);
809                         if (q->irqlock)
810                                 spin_unlock_irqrestore(q->irqlock,flags);
811                         q->read_buf = NULL;
812                 }
813                 if (retval < 0)
814                         break;
815         }
816
817  done:
818         mutex_unlock(&q->lock);
819         return retval;
820 }
821
822 unsigned int videobuf_poll_stream(struct file *file,
823                                   struct videobuf_queue *q,
824                                   poll_table *wait)
825 {
826         struct videobuf_buffer *buf = NULL;
827         unsigned int rc = 0;
828
829         mutex_lock(&q->lock);
830         if (q->streaming) {
831                 if (!list_empty(&q->stream))
832                         buf = list_entry(q->stream.next,
833                                          struct videobuf_buffer, stream);
834         } else {
835                 if (!q->reading)
836                         videobuf_read_start(q);
837                 if (!q->reading) {
838                         rc = POLLERR;
839                 } else if (NULL == q->read_buf) {
840                         q->read_buf = list_entry(q->stream.next,
841                                                  struct videobuf_buffer,
842                                                  stream);
843                         list_del(&q->read_buf->stream);
844                         q->read_off = 0;
845                 }
846                 buf = q->read_buf;
847         }
848         if (!buf)
849                 rc = POLLERR;
850
851         if (0 == rc) {
852                 poll_wait(file, &buf->done, wait);
853                 if (buf->state == STATE_DONE ||
854                     buf->state == STATE_ERROR)
855                         rc = POLLIN|POLLRDNORM;
856         }
857         mutex_unlock(&q->lock);
858         return rc;
859 }
860
861 int videobuf_mmap_setup(struct videobuf_queue *q,
862                         unsigned int bcount, unsigned int bsize,
863                         enum v4l2_memory memory)
864 {
865         unsigned int i;
866         int err;
867
868         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
869
870         err = videobuf_mmap_free(q);
871         if (0 != err)
872                 return err;
873
874         /* Allocate and initialize buffers */
875         for (i = 0; i < bcount; i++) {
876                 q->bufs[i] = videobuf_alloc(q);
877
878                 if (q->bufs[i] == NULL)
879                         break;
880
881                 q->bufs[i]->i      = i;
882                 q->bufs[i]->input  = UNSET;
883                 q->bufs[i]->memory = memory;
884                 q->bufs[i]->bsize  = bsize;
885                 switch (memory) {
886                 case V4L2_MEMORY_MMAP:
887                         q->bufs[i]->boff  = bsize * i;
888                         break;
889                 case V4L2_MEMORY_USERPTR:
890                 case V4L2_MEMORY_OVERLAY:
891                         /* nothing */
892                         break;
893                 }
894         }
895
896         if (!i)
897                 return -ENOMEM;
898
899         dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
900                 i, bsize);
901
902         return i;
903 }
904
905 int videobuf_mmap_free(struct videobuf_queue *q)
906 {
907         int i;
908         int rc;
909
910         if (!q)
911                 return 0;
912
913         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
914
915         rc  = CALL(q,mmap_free,q);
916         if (rc<0)
917                 return rc;
918
919         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
920                 if (NULL == q->bufs[i])
921                         continue;
922                 q->ops->buf_release(q,q->bufs[i]);
923                 kfree(q->bufs[i]);
924                 q->bufs[i] = NULL;
925         }
926
927         return rc;
928 }
929
930 int videobuf_mmap_mapper(struct videobuf_queue *q,
931                          struct vm_area_struct *vma)
932 {
933         int retval;
934
935         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
936
937         mutex_lock(&q->lock);
938         retval=CALL(q,mmap_mapper,q,vma);
939         mutex_unlock(&q->lock);
940
941         return retval;
942 }
943
944 #ifdef CONFIG_VIDEO_V4L1_COMPAT
945 int videobuf_cgmbuf(struct videobuf_queue *q,
946                     struct video_mbuf *mbuf, int count)
947 {
948         struct v4l2_requestbuffers req;
949         int rc,i;
950
951         MAGIC_CHECK(q->int_ops->magic,MAGIC_QTYPE_OPS);
952
953         memset(&req,0,sizeof(req));
954         req.type   = q->type;
955         req.count  = count;
956         req.memory = V4L2_MEMORY_MMAP;
957         rc = videobuf_reqbufs(q,&req);
958         if (rc < 0)
959                 return rc;
960
961         mbuf->frames = req.count;
962         mbuf->size   = 0;
963         for (i = 0; i < mbuf->frames; i++) {
964                 mbuf->offsets[i]  = q->bufs[i]->boff;
965                 mbuf->size       += q->bufs[i]->bsize;
966         }
967
968         return 0;
969 }
970 #endif
971
972 /* --------------------------------------------------------------------- */
973
974 EXPORT_SYMBOL_GPL(videobuf_waiton);
975 EXPORT_SYMBOL_GPL(videobuf_iolock);
976
977 EXPORT_SYMBOL_GPL(videobuf_alloc);
978
979 EXPORT_SYMBOL_GPL(videobuf_queue_core_init);
980 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
981 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
982
983 EXPORT_SYMBOL_GPL(videobuf_next_field);
984 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
985 EXPORT_SYMBOL_GPL(videobuf_querybuf);
986 EXPORT_SYMBOL_GPL(videobuf_qbuf);
987 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
988 EXPORT_SYMBOL_GPL(videobuf_cgmbuf);
989 EXPORT_SYMBOL_GPL(videobuf_streamon);
990 EXPORT_SYMBOL_GPL(videobuf_streamoff);
991
992 EXPORT_SYMBOL_GPL(videobuf_read_start);
993 EXPORT_SYMBOL_GPL(videobuf_read_stop);
994 EXPORT_SYMBOL_GPL(videobuf_read_stream);
995 EXPORT_SYMBOL_GPL(videobuf_read_one);
996 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
997
998 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
999 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1000 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1001
1002 /*
1003  * Local variables:
1004  * c-basic-offset: 8
1005  * End:
1006  */