Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/usb-2.6
[linux-2.6] / drivers / media / video / video-buf.c
1 /*
2  *
3  * generic helper functions for video4linux capture buffers, to handle
4  * memory management and PCI DMA.  Right now bttv + saa7134 use it.
5  *
6  * The functions expect the hardware being able to scatter gatter
7  * (i.e. the buffers are not linear in physical memory, but fragmented
8  * into PAGE_SIZE chunks).  They also assume the driver does not need
9  * to touch the video data (thus it is probably not useful for USB 1.1
10  * as data often must be uncompressed by the drivers).
11  *
12  * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  */
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/vmalloc.h>
24 #include <linux/pagemap.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
30
31 #include <media/video-buf.h>
32
33 #define MAGIC_DMABUF 0x19721112
34 #define MAGIC_BUFFER 0x20040302
35 #define MAGIC_CHECK(is,should)  if (unlikely((is) != (should))) \
36         { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
37
38 static int debug = 0;
39 module_param(debug, int, 0644);
40
41 MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers");
42 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
43 MODULE_LICENSE("GPL");
44
45 #define dprintk(level, fmt, arg...)     if (debug >= level) \
46         printk(KERN_DEBUG "vbuf: " fmt , ## arg)
47
48 struct scatterlist*
49 videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
50 {
51         struct scatterlist *sglist;
52         struct page *pg;
53         int i;
54
55         sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
56         if (NULL == sglist)
57                 return NULL;
58         for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
59                 pg = vmalloc_to_page(virt);
60                 if (NULL == pg)
61                         goto err;
62                 BUG_ON(PageHighMem(pg));
63                 sglist[i].page   = pg;
64                 sglist[i].length = PAGE_SIZE;
65         }
66         return sglist;
67
68  err:
69         kfree(sglist);
70         return NULL;
71 }
72
73 struct scatterlist*
74 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
75 {
76         struct scatterlist *sglist;
77         int i = 0;
78
79         if (NULL == pages[0])
80                 return NULL;
81         sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
82         if (NULL == sglist)
83                 return NULL;
84
85         if (NULL == pages[0])
86                 goto nopage;
87         if (PageHighMem(pages[0]))
88                 /* DMA to highmem pages might not work */
89                 goto highmem;
90         sglist[0].page   = pages[0];
91         sglist[0].offset = offset;
92         sglist[0].length = PAGE_SIZE - offset;
93         for (i = 1; i < nr_pages; i++) {
94                 if (NULL == pages[i])
95                         goto nopage;
96                 if (PageHighMem(pages[i]))
97                         goto highmem;
98                 sglist[i].page   = pages[i];
99                 sglist[i].length = PAGE_SIZE;
100         }
101         return sglist;
102
103  nopage:
104         dprintk(2,"sgl: oops - no page\n");
105         kfree(sglist);
106         return NULL;
107
108  highmem:
109         dprintk(2,"sgl: oops - highmem page\n");
110         kfree(sglist);
111         return NULL;
112 }
113
114 /* --------------------------------------------------------------------- */
115
116 void videobuf_dma_init(struct videobuf_dmabuf *dma)
117 {
118         memset(dma,0,sizeof(*dma));
119         dma->magic = MAGIC_DMABUF;
120 }
121
122 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
123                            unsigned long data, unsigned long size)
124 {
125         unsigned long first,last;
126         int err, rw = 0;
127
128         dma->direction = direction;
129         switch (dma->direction) {
130         case PCI_DMA_FROMDEVICE: rw = READ;  break;
131         case PCI_DMA_TODEVICE:   rw = WRITE; break;
132         default:                 BUG();
133         }
134
135         first = (data          & PAGE_MASK) >> PAGE_SHIFT;
136         last  = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
137         dma->offset   = data & ~PAGE_MASK;
138         dma->nr_pages = last-first+1;
139         dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
140                              GFP_KERNEL);
141         if (NULL == dma->pages)
142                 return -ENOMEM;
143         dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
144                 data,size,dma->nr_pages);
145
146         down_read(&current->mm->mmap_sem);
147         err = get_user_pages(current,current->mm,
148                              data & PAGE_MASK, dma->nr_pages,
149                              rw == READ, 1, /* force */
150                              dma->pages, NULL);
151         up_read(&current->mm->mmap_sem);
152         if (err != dma->nr_pages) {
153                 dma->nr_pages = (err >= 0) ? err : 0;
154                 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
155                 return err < 0 ? err : -EINVAL;
156         }
157         return 0;
158 }
159
160 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
161                              int nr_pages)
162 {
163         dprintk(1,"init kernel [%d pages]\n",nr_pages);
164         dma->direction = direction;
165         dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
166         if (NULL == dma->vmalloc) {
167                 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
168                 return -ENOMEM;
169         }
170         memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
171         dma->nr_pages = nr_pages;
172         return 0;
173 }
174
175 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
176                               dma_addr_t addr, int nr_pages)
177 {
178         dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
179                 nr_pages,(unsigned long)addr);
180         dma->direction = direction;
181         if (0 == addr)
182                 return -EINVAL;
183
184         dma->bus_addr = addr;
185         dma->nr_pages = nr_pages;
186         return 0;
187 }
188
189 int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma)
190 {
191         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
192         BUG_ON(0 == dma->nr_pages);
193
194         if (dma->pages) {
195                 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
196                                                    dma->offset);
197         }
198         if (dma->vmalloc) {
199                 dma->sglist = videobuf_vmalloc_to_sg
200                         (dma->vmalloc,dma->nr_pages);
201         }
202         if (dma->bus_addr) {
203                 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
204                 if (NULL != dma->sglist) {
205                         dma->sglen  = 1;
206                         sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
207                         dma->sglist[0].offset           = dma->bus_addr & ~PAGE_MASK;
208                         sg_dma_len(&dma->sglist[0])     = dma->nr_pages * PAGE_SIZE;
209                 }
210         }
211         if (NULL == dma->sglist) {
212                 dprintk(1,"scatterlist is NULL\n");
213                 return -ENOMEM;
214         }
215
216         if (!dma->bus_addr) {
217                 dma->sglen = pci_map_sg(dev,dma->sglist,dma->nr_pages,
218                                         dma->direction);
219                 if (0 == dma->sglen) {
220                         printk(KERN_WARNING
221                                "%s: pci_map_sg failed\n",__FUNCTION__);
222                         kfree(dma->sglist);
223                         dma->sglist = NULL;
224                         dma->sglen = 0;
225                         return -EIO;
226                 }
227         }
228         return 0;
229 }
230
231 int videobuf_dma_pci_sync(struct pci_dev *dev, struct videobuf_dmabuf *dma)
232 {
233         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
234         BUG_ON(!dma->sglen);
235
236         if (!dma->bus_addr)
237                 pci_dma_sync_sg_for_cpu(dev,dma->sglist,dma->nr_pages,dma->direction);
238         return 0;
239 }
240
241 int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma)
242 {
243         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
244         if (!dma->sglen)
245                 return 0;
246
247         if (!dma->bus_addr)
248                 pci_unmap_sg(dev,dma->sglist,dma->nr_pages,dma->direction);
249         kfree(dma->sglist);
250         dma->sglist = NULL;
251         dma->sglen = 0;
252         return 0;
253 }
254
255 int videobuf_dma_free(struct videobuf_dmabuf *dma)
256 {
257         MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
258         BUG_ON(dma->sglen);
259
260         if (dma->pages) {
261                 int i;
262                 for (i=0; i < dma->nr_pages; i++)
263                         page_cache_release(dma->pages[i]);
264                 kfree(dma->pages);
265                 dma->pages = NULL;
266         }
267
268         vfree(dma->vmalloc);
269         dma->vmalloc = NULL;
270
271         if (dma->bus_addr) {
272                 dma->bus_addr = 0;
273         }
274         dma->direction = PCI_DMA_NONE;
275         return 0;
276 }
277
278 /* --------------------------------------------------------------------- */
279
280 void* videobuf_alloc(unsigned int size)
281 {
282         struct videobuf_buffer *vb;
283
284         vb = kzalloc(size,GFP_KERNEL);
285         if (NULL != vb) {
286                 videobuf_dma_init(&vb->dma);
287                 init_waitqueue_head(&vb->done);
288                 vb->magic     = MAGIC_BUFFER;
289         }
290         return vb;
291 }
292
293 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
294 {
295         int retval = 0;
296         DECLARE_WAITQUEUE(wait, current);
297
298         MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
299         add_wait_queue(&vb->done, &wait);
300         while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
301                 if (non_blocking) {
302                         retval = -EAGAIN;
303                         break;
304                 }
305                 set_current_state(intr  ? TASK_INTERRUPTIBLE
306                                         : TASK_UNINTERRUPTIBLE);
307                 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
308                         schedule();
309                 set_current_state(TASK_RUNNING);
310                 if (intr && signal_pending(current)) {
311                         dprintk(1,"buffer waiton: -EINTR\n");
312                         retval = -EINTR;
313                         break;
314                 }
315         }
316         remove_wait_queue(&vb->done, &wait);
317         return retval;
318 }
319
320 int
321 videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
322                 struct v4l2_framebuffer *fbuf)
323 {
324         int err,pages;
325         dma_addr_t bus;
326
327         MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
328         switch (vb->memory) {
329         case V4L2_MEMORY_MMAP:
330         case V4L2_MEMORY_USERPTR:
331                 if (0 == vb->baddr) {
332                         /* no userspace addr -- kernel bounce buffer */
333                         pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
334                         err = videobuf_dma_init_kernel(&vb->dma,PCI_DMA_FROMDEVICE,
335                                                        pages);
336                         if (0 != err)
337                                 return err;
338                 } else {
339                         /* dma directly to userspace */
340                         err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE,
341                                                      vb->baddr,vb->bsize);
342                         if (0 != err)
343                                 return err;
344                 }
345                 break;
346         case V4L2_MEMORY_OVERLAY:
347                 if (NULL == fbuf)
348                         return -EINVAL;
349                 /* FIXME: need sanity checks for vb->boff */
350                 bus   = (dma_addr_t)fbuf->base + vb->boff;
351                 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
352                 err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
353                                                 bus, pages);
354                 if (0 != err)
355                         return err;
356                 break;
357         default:
358                 BUG();
359         }
360         err = videobuf_dma_pci_map(pci,&vb->dma);
361         if (0 != err)
362                 return err;
363
364         return 0;
365 }
366
367 /* --------------------------------------------------------------------- */
368
369 void videobuf_queue_init(struct videobuf_queue* q,
370                          struct videobuf_queue_ops *ops,
371                          struct pci_dev *pci,
372                          spinlock_t *irqlock,
373                          enum v4l2_buf_type type,
374                          enum v4l2_field field,
375                          unsigned int msize,
376                          void *priv)
377 {
378         memset(q,0,sizeof(*q));
379         q->irqlock = irqlock;
380         q->pci     = pci;
381         q->type    = type;
382         q->field   = field;
383         q->msize   = msize;
384         q->ops     = ops;
385         q->priv_data = priv;
386
387         mutex_init(&q->lock);
388         INIT_LIST_HEAD(&q->stream);
389 }
390
391 int
392 videobuf_queue_is_busy(struct videobuf_queue *q)
393 {
394         int i;
395
396         if (q->streaming) {
397                 dprintk(1,"busy: streaming active\n");
398                 return 1;
399         }
400         if (q->reading) {
401                 dprintk(1,"busy: pending read #1\n");
402                 return 1;
403         }
404         if (q->read_buf) {
405                 dprintk(1,"busy: pending read #2\n");
406                 return 1;
407         }
408         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
409                 if (NULL == q->bufs[i])
410                         continue;
411                 if (q->bufs[i]->map) {
412                         dprintk(1,"busy: buffer #%d mapped\n",i);
413                         return 1;
414                 }
415                 if (q->bufs[i]->state == STATE_QUEUED) {
416                         dprintk(1,"busy: buffer #%d queued\n",i);
417                         return 1;
418                 }
419                 if (q->bufs[i]->state == STATE_ACTIVE) {
420                         dprintk(1,"busy: buffer #%d avtive\n",i);
421                         return 1;
422                 }
423         }
424         return 0;
425 }
426
427 void
428 videobuf_queue_cancel(struct videobuf_queue *q)
429 {
430         unsigned long flags=0;
431         int i;
432
433         /* remove queued buffers from list */
434         spin_lock_irqsave(q->irqlock,flags);
435         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
436                 if (NULL == q->bufs[i])
437                         continue;
438                 if (q->bufs[i]->state == STATE_QUEUED) {
439                         list_del(&q->bufs[i]->queue);
440                         q->bufs[i]->state = STATE_ERROR;
441                 }
442         }
443         spin_unlock_irqrestore(q->irqlock,flags);
444
445         /* free all buffers + clear queue */
446         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
447                 if (NULL == q->bufs[i])
448                         continue;
449                 q->ops->buf_release(q,q->bufs[i]);
450         }
451         INIT_LIST_HEAD(&q->stream);
452 }
453
454 /* --------------------------------------------------------------------- */
455
456 enum v4l2_field
457 videobuf_next_field(struct videobuf_queue *q)
458 {
459         enum v4l2_field field = q->field;
460
461         BUG_ON(V4L2_FIELD_ANY == field);
462
463         if (V4L2_FIELD_ALTERNATE == field) {
464                 if (V4L2_FIELD_TOP == q->last) {
465                         field   = V4L2_FIELD_BOTTOM;
466                         q->last = V4L2_FIELD_BOTTOM;
467                 } else {
468                         field   = V4L2_FIELD_TOP;
469                         q->last = V4L2_FIELD_TOP;
470                 }
471         }
472         return field;
473 }
474
475 void
476 videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
477                 enum v4l2_buf_type type)
478 {
479         MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
480
481         b->index    = vb->i;
482         b->type     = type;
483
484         b->memory   = vb->memory;
485         switch (b->memory) {
486         case V4L2_MEMORY_MMAP:
487                 b->m.offset  = vb->boff;
488                 b->length    = vb->bsize;
489                 break;
490         case V4L2_MEMORY_USERPTR:
491                 b->m.userptr = vb->baddr;
492                 b->length    = vb->bsize;
493                 break;
494         case V4L2_MEMORY_OVERLAY:
495                 b->m.offset  = vb->boff;
496                 break;
497         }
498
499         b->flags    = 0;
500         if (vb->map)
501                 b->flags |= V4L2_BUF_FLAG_MAPPED;
502
503         switch (vb->state) {
504         case STATE_PREPARED:
505         case STATE_QUEUED:
506         case STATE_ACTIVE:
507                 b->flags |= V4L2_BUF_FLAG_QUEUED;
508                 break;
509         case STATE_DONE:
510         case STATE_ERROR:
511                 b->flags |= V4L2_BUF_FLAG_DONE;
512                 break;
513         case STATE_NEEDS_INIT:
514         case STATE_IDLE:
515                 /* nothing */
516                 break;
517         }
518
519         if (vb->input != UNSET) {
520                 b->flags |= V4L2_BUF_FLAG_INPUT;
521                 b->input  = vb->input;
522         }
523
524         b->field     = vb->field;
525         b->timestamp = vb->ts;
526         b->bytesused = vb->size;
527         b->sequence  = vb->field_count >> 1;
528 }
529
530 int
531 videobuf_reqbufs(struct videobuf_queue *q,
532                  struct v4l2_requestbuffers *req)
533 {
534         unsigned int size,count;
535         int retval;
536
537         if (req->type != q->type)
538                 return -EINVAL;
539         if (req->count < 1)
540                 return -EINVAL;
541         if (req->memory != V4L2_MEMORY_MMAP     &&
542             req->memory != V4L2_MEMORY_USERPTR  &&
543             req->memory != V4L2_MEMORY_OVERLAY)
544                 return -EINVAL;
545
546         if (q->streaming)
547                 return -EBUSY;
548         if (!list_empty(&q->stream))
549                 return -EBUSY;
550
551         mutex_lock(&q->lock);
552         count = req->count;
553         if (count > VIDEO_MAX_FRAME)
554                 count = VIDEO_MAX_FRAME;
555         size = 0;
556         q->ops->buf_setup(q,&count,&size);
557         size = PAGE_ALIGN(size);
558         dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
559                 count, size, (count*size)>>PAGE_SHIFT);
560
561         retval = videobuf_mmap_setup(q,count,size,req->memory);
562         if (retval < 0)
563                 goto done;
564
565         req->count = count;
566
567  done:
568         mutex_unlock(&q->lock);
569         return retval;
570 }
571
572 int
573 videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
574 {
575         if (unlikely(b->type != q->type))
576                 return -EINVAL;
577         if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME))
578                 return -EINVAL;
579         if (unlikely(NULL == q->bufs[b->index]))
580                 return -EINVAL;
581         videobuf_status(b,q->bufs[b->index],q->type);
582         return 0;
583 }
584
585 int
586 videobuf_qbuf(struct videobuf_queue *q,
587               struct v4l2_buffer *b)
588 {
589         struct videobuf_buffer *buf;
590         enum v4l2_field field;
591         unsigned long flags=0;
592         int retval;
593
594         mutex_lock(&q->lock);
595         retval = -EBUSY;
596         if (q->reading)
597                 goto done;
598         retval = -EINVAL;
599         if (b->type != q->type)
600                 goto done;
601         if (b->index < 0 || b->index >= VIDEO_MAX_FRAME)
602                 goto done;
603         buf = q->bufs[b->index];
604         if (NULL == buf)
605                 goto done;
606         MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
607         if (buf->memory != b->memory)
608                 goto done;
609         if (buf->state == STATE_QUEUED ||
610             buf->state == STATE_ACTIVE)
611                 goto done;
612
613         if (b->flags & V4L2_BUF_FLAG_INPUT) {
614                 if (b->input >= q->inputs)
615                         goto done;
616                 buf->input = b->input;
617         } else {
618                 buf->input = UNSET;
619         }
620
621         switch (b->memory) {
622         case V4L2_MEMORY_MMAP:
623                 if (0 == buf->baddr)
624                         goto done;
625                 break;
626         case V4L2_MEMORY_USERPTR:
627                 if (b->length < buf->bsize)
628                         goto done;
629                 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
630                         q->ops->buf_release(q,buf);
631                 buf->baddr = b->m.userptr;
632                 break;
633         case V4L2_MEMORY_OVERLAY:
634                 buf->boff = b->m.offset;
635                 break;
636         default:
637                 goto done;
638         }
639
640         field = videobuf_next_field(q);
641         retval = q->ops->buf_prepare(q,buf,field);
642         if (0 != retval)
643                 goto done;
644
645         list_add_tail(&buf->stream,&q->stream);
646         if (q->streaming) {
647                 spin_lock_irqsave(q->irqlock,flags);
648                 q->ops->buf_queue(q,buf);
649                 spin_unlock_irqrestore(q->irqlock,flags);
650         }
651         retval = 0;
652
653  done:
654         mutex_unlock(&q->lock);
655         return retval;
656 }
657
658 int
659 videobuf_dqbuf(struct videobuf_queue *q,
660                struct v4l2_buffer *b, int nonblocking)
661 {
662         struct videobuf_buffer *buf;
663         int retval;
664
665         mutex_lock(&q->lock);
666         retval = -EBUSY;
667         if (q->reading)
668                 goto done;
669         retval = -EINVAL;
670         if (b->type != q->type)
671                 goto done;
672         if (list_empty(&q->stream))
673                 goto done;
674         buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
675         retval = videobuf_waiton(buf, nonblocking, 1);
676         if (retval < 0)
677                 goto done;
678         switch (buf->state) {
679         case STATE_ERROR:
680                 retval = -EIO;
681                 /* fall through */
682         case STATE_DONE:
683                 videobuf_dma_pci_sync(q->pci,&buf->dma);
684                 buf->state = STATE_IDLE;
685                 break;
686         default:
687                 retval = -EINVAL;
688                 goto done;
689         }
690         list_del(&buf->stream);
691         memset(b,0,sizeof(*b));
692         videobuf_status(b,buf,q->type);
693
694  done:
695         mutex_unlock(&q->lock);
696         return retval;
697 }
698
699 int videobuf_streamon(struct videobuf_queue *q)
700 {
701         struct videobuf_buffer *buf;
702         struct list_head *list;
703         unsigned long flags=0;
704         int retval;
705
706         mutex_lock(&q->lock);
707         retval = -EBUSY;
708         if (q->reading)
709                 goto done;
710         retval = 0;
711         if (q->streaming)
712                 goto done;
713         q->streaming = 1;
714         spin_lock_irqsave(q->irqlock,flags);
715         list_for_each(list,&q->stream) {
716                 buf = list_entry(list, struct videobuf_buffer, stream);
717                 if (buf->state == STATE_PREPARED)
718                         q->ops->buf_queue(q,buf);
719         }
720         spin_unlock_irqrestore(q->irqlock,flags);
721
722  done:
723         mutex_unlock(&q->lock);
724         return retval;
725 }
726
727 int videobuf_streamoff(struct videobuf_queue *q)
728 {
729         int retval = -EINVAL;
730
731         mutex_lock(&q->lock);
732         if (!q->streaming)
733                 goto done;
734         videobuf_queue_cancel(q);
735         q->streaming = 0;
736         retval = 0;
737
738  done:
739         mutex_unlock(&q->lock);
740         return retval;
741 }
742
743 static ssize_t
744 videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
745                        size_t count, loff_t *ppos)
746 {
747         enum v4l2_field field;
748         unsigned long flags=0;
749         int retval;
750
751         /* setup stuff */
752         q->read_buf = videobuf_alloc(q->msize);
753         if (NULL == q->read_buf)
754                 return -ENOMEM;
755
756         q->read_buf->memory = V4L2_MEMORY_USERPTR;
757         q->read_buf->baddr  = (unsigned long)data;
758         q->read_buf->bsize  = count;
759         field = videobuf_next_field(q);
760         retval = q->ops->buf_prepare(q,q->read_buf,field);
761         if (0 != retval)
762                 goto done;
763
764         /* start capture & wait */
765         spin_lock_irqsave(q->irqlock,flags);
766         q->ops->buf_queue(q,q->read_buf);
767         spin_unlock_irqrestore(q->irqlock,flags);
768         retval = videobuf_waiton(q->read_buf,0,0);
769         if (0 == retval) {
770                 videobuf_dma_pci_sync(q->pci,&q->read_buf->dma);
771                 if (STATE_ERROR == q->read_buf->state)
772                         retval = -EIO;
773                 else
774                         retval = q->read_buf->size;
775         }
776
777  done:
778         /* cleanup */
779         q->ops->buf_release(q,q->read_buf);
780         kfree(q->read_buf);
781         q->read_buf = NULL;
782         return retval;
783 }
784
785 ssize_t videobuf_read_one(struct videobuf_queue *q,
786                           char __user *data, size_t count, loff_t *ppos,
787                           int nonblocking)
788 {
789         enum v4l2_field field;
790         unsigned long flags=0;
791         unsigned size, nbufs, bytes;
792         int retval;
793
794         mutex_lock(&q->lock);
795
796         nbufs = 1; size = 0;
797         q->ops->buf_setup(q,&nbufs,&size);
798         if (NULL == q->read_buf  &&
799             count >= size        &&
800             !nonblocking) {
801                 retval = videobuf_read_zerocopy(q,data,count,ppos);
802                 if (retval >= 0  ||  retval == -EIO)
803                         /* ok, all done */
804                         goto done;
805                 /* fallback to kernel bounce buffer on failures */
806         }
807
808         if (NULL == q->read_buf) {
809                 /* need to capture a new frame */
810                 retval = -ENOMEM;
811                 q->read_buf = videobuf_alloc(q->msize);
812                 if (NULL == q->read_buf)
813                         goto done;
814                 q->read_buf->memory = V4L2_MEMORY_USERPTR;
815                 q->read_buf->bsize = count; /* preferred size */
816                 field = videobuf_next_field(q);
817                 retval = q->ops->buf_prepare(q,q->read_buf,field);
818                 if (0 != retval) {
819                         kfree (q->read_buf);
820                         q->read_buf = NULL;
821                         goto done;
822                 }
823                 spin_lock_irqsave(q->irqlock,flags);
824                 q->ops->buf_queue(q,q->read_buf);
825                 spin_unlock_irqrestore(q->irqlock,flags);
826                 q->read_off = 0;
827         }
828
829         /* wait until capture is done */
830         retval = videobuf_waiton(q->read_buf, nonblocking, 1);
831         if (0 != retval)
832                 goto done;
833         videobuf_dma_pci_sync(q->pci,&q->read_buf->dma);
834
835         if (STATE_ERROR == q->read_buf->state) {
836                 /* catch I/O errors */
837                 q->ops->buf_release(q,q->read_buf);
838                 kfree(q->read_buf);
839                 q->read_buf = NULL;
840                 retval = -EIO;
841                 goto done;
842         }
843
844         /* copy to userspace */
845         bytes = count;
846         if (bytes > q->read_buf->size - q->read_off)
847                 bytes = q->read_buf->size - q->read_off;
848         retval = -EFAULT;
849         if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
850                 goto done;
851
852         retval = bytes;
853         q->read_off += bytes;
854         if (q->read_off == q->read_buf->size) {
855                 /* all data copied, cleanup */
856                 q->ops->buf_release(q,q->read_buf);
857                 kfree(q->read_buf);
858                 q->read_buf = NULL;
859         }
860
861  done:
862         mutex_unlock(&q->lock);
863         return retval;
864 }
865
866 int videobuf_read_start(struct videobuf_queue *q)
867 {
868         enum v4l2_field field;
869         unsigned long flags=0;
870         int count = 0, size = 0;
871         int err, i;
872
873         q->ops->buf_setup(q,&count,&size);
874         if (count < 2)
875                 count = 2;
876         if (count > VIDEO_MAX_FRAME)
877                 count = VIDEO_MAX_FRAME;
878         size = PAGE_ALIGN(size);
879
880         err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
881         if (err)
882                 return err;
883         for (i = 0; i < count; i++) {
884                 field = videobuf_next_field(q);
885                 err = q->ops->buf_prepare(q,q->bufs[i],field);
886                 if (err)
887                         return err;
888                 list_add_tail(&q->bufs[i]->stream, &q->stream);
889         }
890         spin_lock_irqsave(q->irqlock,flags);
891         for (i = 0; i < count; i++)
892                 q->ops->buf_queue(q,q->bufs[i]);
893         spin_unlock_irqrestore(q->irqlock,flags);
894         q->reading = 1;
895         return 0;
896 }
897
898 void videobuf_read_stop(struct videobuf_queue *q)
899 {
900         int i;
901
902         videobuf_queue_cancel(q);
903         videobuf_mmap_free(q);
904         INIT_LIST_HEAD(&q->stream);
905         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
906                 if (NULL == q->bufs[i])
907                         continue;
908                 kfree(q->bufs[i]);
909                 q->bufs[i] = NULL;
910         }
911         q->read_buf = NULL;
912         q->reading  = 0;
913 }
914
915 ssize_t videobuf_read_stream(struct videobuf_queue *q,
916                              char __user *data, size_t count, loff_t *ppos,
917                              int vbihack, int nonblocking)
918 {
919         unsigned int *fc, bytes;
920         int err, retval;
921         unsigned long flags=0;
922
923         dprintk(2,"%s\n",__FUNCTION__);
924         mutex_lock(&q->lock);
925         retval = -EBUSY;
926         if (q->streaming)
927                 goto done;
928         if (!q->reading) {
929                 retval = videobuf_read_start(q);
930                 if (retval < 0)
931                         goto done;
932         }
933
934         retval = 0;
935         while (count > 0) {
936                 /* get / wait for data */
937                 if (NULL == q->read_buf) {
938                         q->read_buf = list_entry(q->stream.next,
939                                                  struct videobuf_buffer,
940                                                  stream);
941                         list_del(&q->read_buf->stream);
942                         q->read_off = 0;
943                 }
944                 err = videobuf_waiton(q->read_buf, nonblocking, 1);
945                 if (err < 0) {
946                         if (0 == retval)
947                                 retval = err;
948                         break;
949                 }
950
951                 if (q->read_buf->state == STATE_DONE) {
952                         if (vbihack) {
953                                 /* dirty, undocumented hack -- pass the frame counter
954                                  * within the last four bytes of each vbi data block.
955                                  * We need that one to maintain backward compatibility
956                                  * to all vbi decoding software out there ... */
957                                 fc  = (unsigned int*)q->read_buf->dma.vmalloc;
958                                 fc += (q->read_buf->size>>2) -1;
959                                 *fc = q->read_buf->field_count >> 1;
960                                 dprintk(1,"vbihack: %d\n",*fc);
961                         }
962
963                         /* copy stuff */
964                         bytes = count;
965                         if (bytes > q->read_buf->size - q->read_off)
966                                 bytes = q->read_buf->size - q->read_off;
967                         if (copy_to_user(data + retval,
968                                          q->read_buf->dma.vmalloc + q->read_off,
969                                          bytes)) {
970                                 if (0 == retval)
971                                         retval = -EFAULT;
972                                 break;
973                         }
974                         count       -= bytes;
975                         retval      += bytes;
976                         q->read_off += bytes;
977                 } else {
978                         /* some error */
979                         q->read_off = q->read_buf->size;
980                         if (0 == retval)
981                                 retval = -EIO;
982                 }
983
984                 /* requeue buffer when done with copying */
985                 if (q->read_off == q->read_buf->size) {
986                         list_add_tail(&q->read_buf->stream,
987                                       &q->stream);
988                         spin_lock_irqsave(q->irqlock,flags);
989                         q->ops->buf_queue(q,q->read_buf);
990                         spin_unlock_irqrestore(q->irqlock,flags);
991                         q->read_buf = NULL;
992                 }
993                 if (retval < 0)
994                         break;
995         }
996
997  done:
998         mutex_unlock(&q->lock);
999         return retval;
1000 }
1001
1002 unsigned int videobuf_poll_stream(struct file *file,
1003                                   struct videobuf_queue *q,
1004                                   poll_table *wait)
1005 {
1006         struct videobuf_buffer *buf = NULL;
1007         unsigned int rc = 0;
1008
1009         mutex_lock(&q->lock);
1010         if (q->streaming) {
1011                 if (!list_empty(&q->stream))
1012                         buf = list_entry(q->stream.next,
1013                                          struct videobuf_buffer, stream);
1014         } else {
1015                 if (!q->reading)
1016                         videobuf_read_start(q);
1017                 if (!q->reading) {
1018                         rc = POLLERR;
1019                 } else if (NULL == q->read_buf) {
1020                         q->read_buf = list_entry(q->stream.next,
1021                                                  struct videobuf_buffer,
1022                                                  stream);
1023                         list_del(&q->read_buf->stream);
1024                         q->read_off = 0;
1025                 }
1026                 buf = q->read_buf;
1027         }
1028         if (!buf)
1029                 rc = POLLERR;
1030
1031         if (0 == rc) {
1032                 poll_wait(file, &buf->done, wait);
1033                 if (buf->state == STATE_DONE ||
1034                     buf->state == STATE_ERROR)
1035                         rc = POLLIN|POLLRDNORM;
1036         }
1037         mutex_unlock(&q->lock);
1038         return rc;
1039 }
1040
1041 /* --------------------------------------------------------------------- */
1042
1043 static void
1044 videobuf_vm_open(struct vm_area_struct *vma)
1045 {
1046         struct videobuf_mapping *map = vma->vm_private_data;
1047
1048         dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1049                 map->count,vma->vm_start,vma->vm_end);
1050         map->count++;
1051 }
1052
1053 static void
1054 videobuf_vm_close(struct vm_area_struct *vma)
1055 {
1056         struct videobuf_mapping *map = vma->vm_private_data;
1057         struct videobuf_queue *q = map->q;
1058         int i;
1059
1060         dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1061                 map->count,vma->vm_start,vma->vm_end);
1062
1063         map->count--;
1064         if (0 == map->count) {
1065                 dprintk(1,"munmap %p q=%p\n",map,q);
1066                 mutex_lock(&q->lock);
1067                 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1068                         if (NULL == q->bufs[i])
1069                                 continue;
1070                         if (q->bufs[i])
1071                                 ;
1072                         if (q->bufs[i]->map != map)
1073                                 continue;
1074                         q->bufs[i]->map   = NULL;
1075                         q->bufs[i]->baddr = 0;
1076                         q->ops->buf_release(q,q->bufs[i]);
1077                 }
1078                 mutex_unlock(&q->lock);
1079                 kfree(map);
1080         }
1081         return;
1082 }
1083
1084 /*
1085  * Get a anonymous page for the mapping.  Make sure we can DMA to that
1086  * memory location with 32bit PCI devices (i.e. don't use highmem for
1087  * now ...).  Bounce buffers don't work very well for the data rates
1088  * video capture has.
1089  */
1090 static struct page*
1091 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1092                    int *type)
1093 {
1094         struct page *page;
1095
1096         dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1097                 vaddr,vma->vm_start,vma->vm_end);
1098         if (vaddr > vma->vm_end)
1099                 return NOPAGE_SIGBUS;
1100         page = alloc_page(GFP_USER);
1101         if (!page)
1102                 return NOPAGE_OOM;
1103         clear_user_page(page_address(page), vaddr, page);
1104         if (type)
1105                 *type = VM_FAULT_MINOR;
1106         return page;
1107 }
1108
1109 static struct vm_operations_struct videobuf_vm_ops =
1110 {
1111         .open     = videobuf_vm_open,
1112         .close    = videobuf_vm_close,
1113         .nopage   = videobuf_vm_nopage,
1114 };
1115
1116 int videobuf_mmap_setup(struct videobuf_queue *q,
1117                         unsigned int bcount, unsigned int bsize,
1118                         enum v4l2_memory memory)
1119 {
1120         unsigned int i;
1121         int err;
1122
1123         err = videobuf_mmap_free(q);
1124         if (0 != err)
1125                 return err;
1126
1127         for (i = 0; i < bcount; i++) {
1128                 q->bufs[i] = videobuf_alloc(q->msize);
1129                 q->bufs[i]->i      = i;
1130                 q->bufs[i]->input  = UNSET;
1131                 q->bufs[i]->memory = memory;
1132                 q->bufs[i]->bsize  = bsize;
1133                 switch (memory) {
1134                 case V4L2_MEMORY_MMAP:
1135                         q->bufs[i]->boff  = bsize * i;
1136                         break;
1137                 case V4L2_MEMORY_USERPTR:
1138                 case V4L2_MEMORY_OVERLAY:
1139                         /* nothing */
1140                         break;
1141                 }
1142         }
1143         dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1144                 bcount,bsize);
1145         return 0;
1146 }
1147
1148 int videobuf_mmap_free(struct videobuf_queue *q)
1149 {
1150         int i;
1151
1152         for (i = 0; i < VIDEO_MAX_FRAME; i++)
1153                 if (q->bufs[i] && q->bufs[i]->map)
1154                         return -EBUSY;
1155         for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1156                 if (NULL == q->bufs[i])
1157                         continue;
1158                 q->ops->buf_release(q,q->bufs[i]);
1159                 kfree(q->bufs[i]);
1160                 q->bufs[i] = NULL;
1161         }
1162         return 0;
1163 }
1164
1165 int videobuf_mmap_mapper(struct videobuf_queue *q,
1166                          struct vm_area_struct *vma)
1167 {
1168         struct videobuf_mapping *map;
1169         unsigned int first,last,size,i;
1170         int retval;
1171
1172         mutex_lock(&q->lock);
1173         retval = -EINVAL;
1174         if (!(vma->vm_flags & VM_WRITE)) {
1175                 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1176                 goto done;
1177         }
1178         if (!(vma->vm_flags & VM_SHARED)) {
1179                 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1180                 goto done;
1181         }
1182
1183         /* look for first buffer to map */
1184         for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1185                 if (NULL == q->bufs[first])
1186                         continue;
1187                 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1188                         continue;
1189                 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1190                         break;
1191         }
1192         if (VIDEO_MAX_FRAME == first) {
1193                 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1194                         (vma->vm_pgoff << PAGE_SHIFT));
1195                 goto done;
1196         }
1197
1198         /* look for last buffer to map */
1199         for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1200                 if (NULL == q->bufs[last])
1201                         continue;
1202                 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1203                         continue;
1204                 if (q->bufs[last]->map) {
1205                         retval = -EBUSY;
1206                         goto done;
1207                 }
1208                 size += q->bufs[last]->bsize;
1209                 if (size == (vma->vm_end - vma->vm_start))
1210                         break;
1211         }
1212         if (VIDEO_MAX_FRAME == last) {
1213                 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1214                         (vma->vm_end - vma->vm_start));
1215                 goto done;
1216         }
1217
1218         /* create mapping + update buffer list */
1219         retval = -ENOMEM;
1220         map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1221         if (NULL == map)
1222                 goto done;
1223         for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1224                 q->bufs[i]->map   = map;
1225                 q->bufs[i]->baddr = vma->vm_start + size;
1226         }
1227         map->count    = 1;
1228         map->start    = vma->vm_start;
1229         map->end      = vma->vm_end;
1230         map->q        = q;
1231         vma->vm_ops   = &videobuf_vm_ops;
1232         vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1233         vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1234         vma->vm_private_data = map;
1235         dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1236                 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1237         retval = 0;
1238
1239  done:
1240         mutex_unlock(&q->lock);
1241         return retval;
1242 }
1243
1244 /* --------------------------------------------------------------------- */
1245
1246 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1247
1248 EXPORT_SYMBOL_GPL(videobuf_dma_init);
1249 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1250 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1251 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1252 EXPORT_SYMBOL_GPL(videobuf_dma_pci_map);
1253 EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync);
1254 EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap);
1255 EXPORT_SYMBOL_GPL(videobuf_dma_free);
1256
1257 EXPORT_SYMBOL_GPL(videobuf_alloc);
1258 EXPORT_SYMBOL_GPL(videobuf_waiton);
1259 EXPORT_SYMBOL_GPL(videobuf_iolock);
1260
1261 EXPORT_SYMBOL_GPL(videobuf_queue_init);
1262 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1263 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1264
1265 EXPORT_SYMBOL_GPL(videobuf_next_field);
1266 EXPORT_SYMBOL_GPL(videobuf_status);
1267 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1268 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1269 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1270 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1271 EXPORT_SYMBOL_GPL(videobuf_streamon);
1272 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1273
1274 EXPORT_SYMBOL_GPL(videobuf_read_start);
1275 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1276 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1277 EXPORT_SYMBOL_GPL(videobuf_read_one);
1278 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1279
1280 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1281 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1282 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1283
1284 /*
1285  * Local variables:
1286  * c-basic-offset: 8
1287  * End:
1288  */