3 * generic helper functions for video4linux capture buffers, to handle
4 * memory management and PCI DMA.
5 * Right now, bttv, saa7134, saa7146 and cx88 use it.
7 * The functions expect the hardware being able to scatter gatter
8 * (i.e. the buffers are not linear in physical memory, but fragmented
9 * into PAGE_SIZE chunks). They also assume the driver does not need
10 * to touch the video data.
12 * device specific map/unmap/sync stuff now are mapped as operations
13 * to allow its usage by USB and virtual devices.
15 * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
16 * (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
17 * (c) 2006 Ted Walther and John Sokol
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/vmalloc.h>
29 #include <linux/pagemap.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <linux/interrupt.h>
34 #include <asm/pgtable.h>
36 #include <media/video-buf.h>
38 #define MAGIC_DMABUF 0x19721112
39 #define MAGIC_BUFFER 0x20040302
40 #define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
41 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
44 module_param(debug, int, 0644);
46 MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers");
47 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
48 MODULE_LICENSE("GPL");
50 #define dprintk(level, fmt, arg...) if (debug >= level) \
51 printk(KERN_DEBUG "vbuf: " fmt , ## arg)
54 videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
56 struct scatterlist *sglist;
60 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
63 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
64 pg = vmalloc_to_page(virt);
67 BUG_ON(PageHighMem(pg));
69 sglist[i].length = PAGE_SIZE;
79 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
81 struct scatterlist *sglist;
86 sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
92 if (PageHighMem(pages[0]))
93 /* DMA to highmem pages might not work */
95 sglist[0].page = pages[0];
96 sglist[0].offset = offset;
97 sglist[0].length = PAGE_SIZE - offset;
98 for (i = 1; i < nr_pages; i++) {
101 if (PageHighMem(pages[i]))
103 sglist[i].page = pages[i];
104 sglist[i].length = PAGE_SIZE;
109 dprintk(2,"sgl: oops - no page\n");
114 dprintk(2,"sgl: oops - highmem page\n");
119 /* --------------------------------------------------------------------- */
121 void videobuf_dma_init(struct videobuf_dmabuf *dma)
123 memset(dma,0,sizeof(*dma));
124 dma->magic = MAGIC_DMABUF;
127 int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
128 unsigned long data, unsigned long size)
130 unsigned long first,last;
133 dma->direction = direction;
134 switch (dma->direction) {
135 case PCI_DMA_FROMDEVICE: rw = READ; break;
136 case PCI_DMA_TODEVICE: rw = WRITE; break;
140 first = (data & PAGE_MASK) >> PAGE_SHIFT;
141 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
142 dma->offset = data & ~PAGE_MASK;
143 dma->nr_pages = last-first+1;
144 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
146 if (NULL == dma->pages)
148 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
149 data,size,dma->nr_pages);
151 down_read(¤t->mm->mmap_sem);
152 err = get_user_pages(current,current->mm,
153 data & PAGE_MASK, dma->nr_pages,
154 rw == READ, 1, /* force */
156 up_read(¤t->mm->mmap_sem);
157 if (err != dma->nr_pages) {
158 dma->nr_pages = (err >= 0) ? err : 0;
159 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
160 return err < 0 ? err : -EINVAL;
165 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
168 dprintk(1,"init kernel [%d pages]\n",nr_pages);
169 dma->direction = direction;
170 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
171 if (NULL == dma->vmalloc) {
172 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
175 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
176 (unsigned long)dma->vmalloc,
177 nr_pages << PAGE_SHIFT);
178 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
179 dma->nr_pages = nr_pages;
183 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
184 dma_addr_t addr, int nr_pages)
186 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
187 nr_pages,(unsigned long)addr);
188 dma->direction = direction;
192 dma->bus_addr = addr;
193 dma->nr_pages = nr_pages;
197 int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
201 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
202 BUG_ON(0 == dma->nr_pages);
205 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
209 dma->sglist = videobuf_vmalloc_to_sg
210 (dma->vmalloc,dma->nr_pages);
213 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
214 if (NULL != dma->sglist) {
216 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
217 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
218 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
221 if (NULL == dma->sglist) {
222 dprintk(1,"scatterlist is NULL\n");
225 if (!dma->bus_addr) {
226 if (q->ops->vb_map_sg) {
227 dma->sglen = q->ops->vb_map_sg(dev,dma->sglist,
228 dma->nr_pages, dma->direction);
230 if (0 == dma->sglen) {
232 "%s: videobuf_map_sg failed\n",__FUNCTION__);
242 int videobuf_dma_sync(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
246 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
249 if (!dma->bus_addr && q->ops->vb_dma_sync_sg)
250 q->ops->vb_dma_sync_sg(dev,dma->sglist,dma->nr_pages,
256 int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
260 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
264 if (!dma->bus_addr && q->ops->vb_unmap_sg)
265 q->ops->vb_unmap_sg(dev,dma->sglist,dma->nr_pages,
273 int videobuf_dma_free(struct videobuf_dmabuf *dma)
275 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
280 for (i=0; i < dma->nr_pages; i++)
281 page_cache_release(dma->pages[i]);
292 dma->direction = PCI_DMA_NONE;
296 /* --------------------------------------------------------------------- */
298 void* videobuf_alloc(unsigned int size)
300 struct videobuf_buffer *vb;
302 vb = kzalloc(size,GFP_KERNEL);
304 videobuf_dma_init(&vb->dma);
305 init_waitqueue_head(&vb->done);
306 vb->magic = MAGIC_BUFFER;
311 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
314 DECLARE_WAITQUEUE(wait, current);
316 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
317 add_wait_queue(&vb->done, &wait);
318 while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
323 set_current_state(intr ? TASK_INTERRUPTIBLE
324 : TASK_UNINTERRUPTIBLE);
325 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
327 set_current_state(TASK_RUNNING);
328 if (intr && signal_pending(current)) {
329 dprintk(1,"buffer waiton: -EINTR\n");
334 remove_wait_queue(&vb->done, &wait);
339 videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
340 struct v4l2_framebuffer *fbuf)
345 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
346 switch (vb->memory) {
347 case V4L2_MEMORY_MMAP:
348 case V4L2_MEMORY_USERPTR:
349 if (0 == vb->baddr) {
350 /* no userspace addr -- kernel bounce buffer */
351 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
352 err = videobuf_dma_init_kernel(&vb->dma,PCI_DMA_FROMDEVICE,
357 /* dma directly to userspace */
358 err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE,
359 vb->baddr,vb->bsize);
364 case V4L2_MEMORY_OVERLAY:
367 /* FIXME: need sanity checks for vb->boff */
368 bus = (dma_addr_t)fbuf->base + vb->boff;
369 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
370 err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
378 err = videobuf_dma_map(q,&vb->dma);
385 /* --------------------------------------------------------------------- */
387 void videobuf_queue_pci(struct videobuf_queue* q)
389 /* If not specified, defaults to PCI map sg */
390 if (!q->ops->vb_map_sg)
391 q->ops->vb_map_sg=(vb_map_sg_t *)pci_map_sg;
393 if (!q->ops->vb_dma_sync_sg)
394 q->ops->vb_dma_sync_sg=(vb_map_sg_t *)pci_dma_sync_sg_for_cpu;
395 if (!q->ops->vb_unmap_sg)
396 q->ops->vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
399 int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma)
401 struct videobuf_queue q;
404 q.ops->vb_map_sg=(vb_map_sg_t *)pci_unmap_sg;
406 return (videobuf_dma_unmap(&q,dma));
409 int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma)
411 struct videobuf_queue q;
414 q.ops->vb_map_sg=(vb_map_sg_t *)pci_unmap_sg;
416 return (videobuf_dma_unmap(&q,dma));
419 void videobuf_queue_init(struct videobuf_queue* q,
420 struct videobuf_queue_ops *ops,
423 enum v4l2_buf_type type,
424 enum v4l2_field field,
428 memset(q,0,sizeof(*q));
429 q->irqlock = irqlock;
437 videobuf_queue_pci(q);
439 mutex_init(&q->lock);
440 INIT_LIST_HEAD(&q->stream);
444 videobuf_queue_is_busy(struct videobuf_queue *q)
449 dprintk(1,"busy: streaming active\n");
453 dprintk(1,"busy: pending read #1\n");
457 dprintk(1,"busy: pending read #2\n");
460 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
461 if (NULL == q->bufs[i])
463 if (q->bufs[i]->map) {
464 dprintk(1,"busy: buffer #%d mapped\n",i);
467 if (q->bufs[i]->state == STATE_QUEUED) {
468 dprintk(1,"busy: buffer #%d queued\n",i);
471 if (q->bufs[i]->state == STATE_ACTIVE) {
472 dprintk(1,"busy: buffer #%d avtive\n",i);
480 videobuf_queue_cancel(struct videobuf_queue *q)
482 unsigned long flags=0;
485 /* remove queued buffers from list */
487 spin_lock_irqsave(q->irqlock,flags);
488 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
489 if (NULL == q->bufs[i])
491 if (q->bufs[i]->state == STATE_QUEUED) {
492 list_del(&q->bufs[i]->queue);
493 q->bufs[i]->state = STATE_ERROR;
497 spin_unlock_irqrestore(q->irqlock,flags);
499 /* free all buffers + clear queue */
500 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
501 if (NULL == q->bufs[i])
503 q->ops->buf_release(q,q->bufs[i]);
505 INIT_LIST_HEAD(&q->stream);
508 /* --------------------------------------------------------------------- */
511 videobuf_next_field(struct videobuf_queue *q)
513 enum v4l2_field field = q->field;
515 BUG_ON(V4L2_FIELD_ANY == field);
517 if (V4L2_FIELD_ALTERNATE == field) {
518 if (V4L2_FIELD_TOP == q->last) {
519 field = V4L2_FIELD_BOTTOM;
520 q->last = V4L2_FIELD_BOTTOM;
522 field = V4L2_FIELD_TOP;
523 q->last = V4L2_FIELD_TOP;
530 videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
531 enum v4l2_buf_type type)
533 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
538 b->memory = vb->memory;
540 case V4L2_MEMORY_MMAP:
541 b->m.offset = vb->boff;
542 b->length = vb->bsize;
544 case V4L2_MEMORY_USERPTR:
545 b->m.userptr = vb->baddr;
546 b->length = vb->bsize;
548 case V4L2_MEMORY_OVERLAY:
549 b->m.offset = vb->boff;
555 b->flags |= V4L2_BUF_FLAG_MAPPED;
561 b->flags |= V4L2_BUF_FLAG_QUEUED;
565 b->flags |= V4L2_BUF_FLAG_DONE;
567 case STATE_NEEDS_INIT:
573 if (vb->input != UNSET) {
574 b->flags |= V4L2_BUF_FLAG_INPUT;
575 b->input = vb->input;
578 b->field = vb->field;
579 b->timestamp = vb->ts;
580 b->bytesused = vb->size;
581 b->sequence = vb->field_count >> 1;
585 videobuf_reqbufs(struct videobuf_queue *q,
586 struct v4l2_requestbuffers *req)
588 unsigned int size,count;
591 if (req->type != q->type) {
592 dprintk(1,"reqbufs: queue type invalid\n");
595 if (req->count < 1) {
596 dprintk(1,"reqbufs: count invalid (%d)\n",req->count);
599 if (req->memory != V4L2_MEMORY_MMAP &&
600 req->memory != V4L2_MEMORY_USERPTR &&
601 req->memory != V4L2_MEMORY_OVERLAY) {
602 dprintk(1,"reqbufs: memory type invalid\n");
607 dprintk(1,"reqbufs: streaming already exists\n");
610 if (!list_empty(&q->stream)) {
611 dprintk(1,"reqbufs: stream running\n");
615 mutex_lock(&q->lock);
617 if (count > VIDEO_MAX_FRAME)
618 count = VIDEO_MAX_FRAME;
620 q->ops->buf_setup(q,&count,&size);
621 size = PAGE_ALIGN(size);
622 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
623 count, size, (count*size)>>PAGE_SHIFT);
625 retval = videobuf_mmap_setup(q,count,size,req->memory);
627 dprintk(1,"reqbufs: mmap setup returned %d\n",retval);
634 mutex_unlock(&q->lock);
639 videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
641 if (unlikely(b->type != q->type)) {
642 dprintk(1,"querybuf: Wrong type.\n");
645 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
646 dprintk(1,"querybuf: index out of range.\n");
649 if (unlikely(NULL == q->bufs[b->index])) {
650 dprintk(1,"querybuf: buffer is null.\n");
653 videobuf_status(b,q->bufs[b->index],q->type);
658 videobuf_qbuf(struct videobuf_queue *q,
659 struct v4l2_buffer *b)
661 struct videobuf_buffer *buf;
662 enum v4l2_field field;
663 unsigned long flags=0;
666 mutex_lock(&q->lock);
669 dprintk(1,"qbuf: Reading running...\n");
673 if (b->type != q->type) {
674 dprintk(1,"qbuf: Wrong type.\n");
677 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
678 dprintk(1,"qbuf: index out of range.\n");
681 buf = q->bufs[b->index];
683 dprintk(1,"qbuf: buffer is null.\n");
686 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
687 if (buf->memory != b->memory) {
688 dprintk(1,"qbuf: memory type is wrong.\n");
691 if (buf->state == STATE_QUEUED ||
692 buf->state == STATE_ACTIVE) {
693 dprintk(1,"qbuf: buffer is already queued or active.\n");
697 if (b->flags & V4L2_BUF_FLAG_INPUT) {
698 if (b->input >= q->inputs) {
699 dprintk(1,"qbuf: wrong input.\n");
702 buf->input = b->input;
708 case V4L2_MEMORY_MMAP:
709 if (0 == buf->baddr) {
710 dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n");
714 case V4L2_MEMORY_USERPTR:
715 if (b->length < buf->bsize) {
716 dprintk(1,"qbuf: buffer length is not enough\n");
719 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
720 q->ops->buf_release(q,buf);
721 buf->baddr = b->m.userptr;
723 case V4L2_MEMORY_OVERLAY:
724 buf->boff = b->m.offset;
727 dprintk(1,"qbuf: wrong memory type\n");
731 dprintk(1,"qbuf: requesting next field\n");
732 field = videobuf_next_field(q);
733 retval = q->ops->buf_prepare(q,buf,field);
735 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval);
739 list_add_tail(&buf->stream,&q->stream);
742 spin_lock_irqsave(q->irqlock,flags);
743 q->ops->buf_queue(q,buf);
745 spin_unlock_irqrestore(q->irqlock,flags);
747 dprintk(1,"qbuf: succeded\n");
751 mutex_unlock(&q->lock);
756 videobuf_dqbuf(struct videobuf_queue *q,
757 struct v4l2_buffer *b, int nonblocking)
759 struct videobuf_buffer *buf;
762 mutex_lock(&q->lock);
765 dprintk(1,"dqbuf: Reading running...\n");
769 if (b->type != q->type) {
770 dprintk(1,"dqbuf: Wrong type.\n");
773 if (list_empty(&q->stream)) {
774 dprintk(1,"dqbuf: stream running\n");
777 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
778 retval = videobuf_waiton(buf, nonblocking, 1);
780 dprintk(1,"dqbuf: waiton returned %d\n",retval);
783 switch (buf->state) {
785 dprintk(1,"dqbuf: state is error\n");
787 videobuf_dma_sync(q,&buf->dma);
788 buf->state = STATE_IDLE;
791 dprintk(1,"dqbuf: state is done\n");
792 videobuf_dma_sync(q,&buf->dma);
793 buf->state = STATE_IDLE;
796 dprintk(1,"dqbuf: state invalid\n");
800 list_del(&buf->stream);
801 memset(b,0,sizeof(*b));
802 videobuf_status(b,buf,q->type);
805 mutex_unlock(&q->lock);
809 int videobuf_streamon(struct videobuf_queue *q)
811 struct videobuf_buffer *buf;
812 struct list_head *list;
813 unsigned long flags=0;
816 mutex_lock(&q->lock);
825 spin_lock_irqsave(q->irqlock,flags);
826 list_for_each(list,&q->stream) {
827 buf = list_entry(list, struct videobuf_buffer, stream);
828 if (buf->state == STATE_PREPARED)
829 q->ops->buf_queue(q,buf);
832 spin_unlock_irqrestore(q->irqlock,flags);
835 mutex_unlock(&q->lock);
839 int videobuf_streamoff(struct videobuf_queue *q)
841 int retval = -EINVAL;
843 mutex_lock(&q->lock);
846 videobuf_queue_cancel(q);
851 mutex_unlock(&q->lock);
856 videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
857 size_t count, loff_t *ppos)
859 enum v4l2_field field;
860 unsigned long flags=0;
864 q->read_buf = videobuf_alloc(q->msize);
865 if (NULL == q->read_buf)
868 q->read_buf->memory = V4L2_MEMORY_USERPTR;
869 q->read_buf->baddr = (unsigned long)data;
870 q->read_buf->bsize = count;
871 field = videobuf_next_field(q);
872 retval = q->ops->buf_prepare(q,q->read_buf,field);
876 /* start capture & wait */
878 spin_lock_irqsave(q->irqlock,flags);
879 q->ops->buf_queue(q,q->read_buf);
881 spin_unlock_irqrestore(q->irqlock,flags);
882 retval = videobuf_waiton(q->read_buf,0,0);
884 videobuf_dma_sync(q,&q->read_buf->dma);
885 if (STATE_ERROR == q->read_buf->state)
888 retval = q->read_buf->size;
893 q->ops->buf_release(q,q->read_buf);
899 ssize_t videobuf_read_one(struct videobuf_queue *q,
900 char __user *data, size_t count, loff_t *ppos,
903 enum v4l2_field field;
904 unsigned long flags=0;
905 unsigned size, nbufs, bytes;
908 mutex_lock(&q->lock);
911 q->ops->buf_setup(q,&nbufs,&size);
912 if (NULL == q->read_buf &&
915 retval = videobuf_read_zerocopy(q,data,count,ppos);
916 if (retval >= 0 || retval == -EIO)
919 /* fallback to kernel bounce buffer on failures */
922 if (NULL == q->read_buf) {
923 /* need to capture a new frame */
925 q->read_buf = videobuf_alloc(q->msize);
926 dprintk(1,"video alloc=0x%08x\n",(unsigned int) q->read_buf);
927 if (NULL == q->read_buf)
929 q->read_buf->memory = V4L2_MEMORY_USERPTR;
930 q->read_buf->bsize = count; /* preferred size */
931 field = videobuf_next_field(q);
932 retval = q->ops->buf_prepare(q,q->read_buf,field);
939 spin_lock_irqsave(q->irqlock,flags);
940 q->ops->buf_queue(q,q->read_buf);
942 spin_unlock_irqrestore(q->irqlock,flags);
946 /* wait until capture is done */
947 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
950 videobuf_dma_sync(q,&q->read_buf->dma);
952 if (STATE_ERROR == q->read_buf->state) {
953 /* catch I/O errors */
954 q->ops->buf_release(q,q->read_buf);
961 /* copy to userspace */
963 if (bytes > q->read_buf->size - q->read_off)
964 bytes = q->read_buf->size - q->read_off;
966 if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
970 q->read_off += bytes;
971 if (q->read_off == q->read_buf->size) {
972 /* all data copied, cleanup */
973 q->ops->buf_release(q,q->read_buf);
979 mutex_unlock(&q->lock);
983 int videobuf_read_start(struct videobuf_queue *q)
985 enum v4l2_field field;
986 unsigned long flags=0;
987 int count = 0, size = 0;
990 q->ops->buf_setup(q,&count,&size);
993 if (count > VIDEO_MAX_FRAME)
994 count = VIDEO_MAX_FRAME;
995 size = PAGE_ALIGN(size);
997 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
1000 for (i = 0; i < count; i++) {
1001 field = videobuf_next_field(q);
1002 err = q->ops->buf_prepare(q,q->bufs[i],field);
1005 list_add_tail(&q->bufs[i]->stream, &q->stream);
1008 spin_lock_irqsave(q->irqlock,flags);
1009 for (i = 0; i < count; i++)
1010 q->ops->buf_queue(q,q->bufs[i]);
1012 spin_unlock_irqrestore(q->irqlock,flags);
1017 void videobuf_read_stop(struct videobuf_queue *q)
1021 videobuf_queue_cancel(q);
1022 videobuf_mmap_free(q);
1023 INIT_LIST_HEAD(&q->stream);
1024 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1025 if (NULL == q->bufs[i])
1034 ssize_t videobuf_read_stream(struct videobuf_queue *q,
1035 char __user *data, size_t count, loff_t *ppos,
1036 int vbihack, int nonblocking)
1038 unsigned int *fc, bytes;
1040 unsigned long flags=0;
1042 dprintk(2,"%s\n",__FUNCTION__);
1043 mutex_lock(&q->lock);
1048 retval = videobuf_read_start(q);
1055 /* get / wait for data */
1056 if (NULL == q->read_buf) {
1057 q->read_buf = list_entry(q->stream.next,
1058 struct videobuf_buffer,
1060 list_del(&q->read_buf->stream);
1063 err = videobuf_waiton(q->read_buf, nonblocking, 1);
1070 if (q->read_buf->state == STATE_DONE) {
1072 /* dirty, undocumented hack -- pass the frame counter
1073 * within the last four bytes of each vbi data block.
1074 * We need that one to maintain backward compatibility
1075 * to all vbi decoding software out there ... */
1076 fc = (unsigned int*)q->read_buf->dma.vmalloc;
1077 fc += (q->read_buf->size>>2) -1;
1078 *fc = q->read_buf->field_count >> 1;
1079 dprintk(1,"vbihack: %d\n",*fc);
1084 if (bytes > q->read_buf->size - q->read_off)
1085 bytes = q->read_buf->size - q->read_off;
1086 if (copy_to_user(data + retval,
1087 q->read_buf->dma.vmalloc + q->read_off,
1095 q->read_off += bytes;
1098 q->read_off = q->read_buf->size;
1103 /* requeue buffer when done with copying */
1104 if (q->read_off == q->read_buf->size) {
1105 list_add_tail(&q->read_buf->stream,
1108 spin_lock_irqsave(q->irqlock,flags);
1109 q->ops->buf_queue(q,q->read_buf);
1111 spin_unlock_irqrestore(q->irqlock,flags);
1119 mutex_unlock(&q->lock);
1123 unsigned int videobuf_poll_stream(struct file *file,
1124 struct videobuf_queue *q,
1127 struct videobuf_buffer *buf = NULL;
1128 unsigned int rc = 0;
1130 mutex_lock(&q->lock);
1132 if (!list_empty(&q->stream))
1133 buf = list_entry(q->stream.next,
1134 struct videobuf_buffer, stream);
1137 videobuf_read_start(q);
1140 } else if (NULL == q->read_buf) {
1141 q->read_buf = list_entry(q->stream.next,
1142 struct videobuf_buffer,
1144 list_del(&q->read_buf->stream);
1153 poll_wait(file, &buf->done, wait);
1154 if (buf->state == STATE_DONE ||
1155 buf->state == STATE_ERROR)
1156 rc = POLLIN|POLLRDNORM;
1158 mutex_unlock(&q->lock);
1162 /* --------------------------------------------------------------------- */
1165 videobuf_vm_open(struct vm_area_struct *vma)
1167 struct videobuf_mapping *map = vma->vm_private_data;
1169 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1170 map->count,vma->vm_start,vma->vm_end);
1175 videobuf_vm_close(struct vm_area_struct *vma)
1177 struct videobuf_mapping *map = vma->vm_private_data;
1178 struct videobuf_queue *q = map->q;
1181 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1182 map->count,vma->vm_start,vma->vm_end);
1185 if (0 == map->count) {
1186 dprintk(1,"munmap %p q=%p\n",map,q);
1187 mutex_lock(&q->lock);
1188 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1189 if (NULL == q->bufs[i])
1193 if (q->bufs[i]->map != map)
1195 q->bufs[i]->map = NULL;
1196 q->bufs[i]->baddr = 0;
1197 q->ops->buf_release(q,q->bufs[i]);
1199 mutex_unlock(&q->lock);
1206 * Get a anonymous page for the mapping. Make sure we can DMA to that
1207 * memory location with 32bit PCI devices (i.e. don't use highmem for
1208 * now ...). Bounce buffers don't work very well for the data rates
1209 * video capture has.
1212 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1217 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1218 vaddr,vma->vm_start,vma->vm_end);
1219 if (vaddr > vma->vm_end)
1220 return NOPAGE_SIGBUS;
1221 page = alloc_page(GFP_USER);
1224 clear_user_page(page_address(page), vaddr, page);
1226 *type = VM_FAULT_MINOR;
1230 static struct vm_operations_struct videobuf_vm_ops =
1232 .open = videobuf_vm_open,
1233 .close = videobuf_vm_close,
1234 .nopage = videobuf_vm_nopage,
1237 int videobuf_mmap_setup(struct videobuf_queue *q,
1238 unsigned int bcount, unsigned int bsize,
1239 enum v4l2_memory memory)
1244 err = videobuf_mmap_free(q);
1248 for (i = 0; i < bcount; i++) {
1249 q->bufs[i] = videobuf_alloc(q->msize);
1251 q->bufs[i]->input = UNSET;
1252 q->bufs[i]->memory = memory;
1253 q->bufs[i]->bsize = bsize;
1255 case V4L2_MEMORY_MMAP:
1256 q->bufs[i]->boff = bsize * i;
1258 case V4L2_MEMORY_USERPTR:
1259 case V4L2_MEMORY_OVERLAY:
1264 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1269 int videobuf_mmap_free(struct videobuf_queue *q)
1273 for (i = 0; i < VIDEO_MAX_FRAME; i++)
1274 if (q->bufs[i] && q->bufs[i]->map)
1276 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1277 if (NULL == q->bufs[i])
1279 q->ops->buf_release(q,q->bufs[i]);
1286 int videobuf_mmap_mapper(struct videobuf_queue *q,
1287 struct vm_area_struct *vma)
1289 struct videobuf_mapping *map;
1290 unsigned int first,last,size,i;
1293 mutex_lock(&q->lock);
1295 if (!(vma->vm_flags & VM_WRITE)) {
1296 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1299 if (!(vma->vm_flags & VM_SHARED)) {
1300 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1304 /* look for first buffer to map */
1305 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1306 if (NULL == q->bufs[first])
1308 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1310 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1313 if (VIDEO_MAX_FRAME == first) {
1314 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1315 (vma->vm_pgoff << PAGE_SHIFT));
1319 /* look for last buffer to map */
1320 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1321 if (NULL == q->bufs[last])
1323 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1325 if (q->bufs[last]->map) {
1329 size += q->bufs[last]->bsize;
1330 if (size == (vma->vm_end - vma->vm_start))
1333 if (VIDEO_MAX_FRAME == last) {
1334 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1335 (vma->vm_end - vma->vm_start));
1339 /* create mapping + update buffer list */
1341 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1344 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1345 q->bufs[i]->map = map;
1346 q->bufs[i]->baddr = vma->vm_start + size;
1349 map->start = vma->vm_start;
1350 map->end = vma->vm_end;
1352 vma->vm_ops = &videobuf_vm_ops;
1353 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1354 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1355 vma->vm_private_data = map;
1356 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1357 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1361 mutex_unlock(&q->lock);
1365 /* --------------------------------------------------------------------- */
1367 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1369 EXPORT_SYMBOL_GPL(videobuf_dma_init);
1370 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1371 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1372 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1373 EXPORT_SYMBOL_GPL(videobuf_dma_map);
1374 EXPORT_SYMBOL_GPL(videobuf_dma_sync);
1375 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
1376 EXPORT_SYMBOL_GPL(videobuf_dma_free);
1378 EXPORT_SYMBOL_GPL(videobuf_pci_dma_map);
1379 EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap);
1381 EXPORT_SYMBOL_GPL(videobuf_alloc);
1382 EXPORT_SYMBOL_GPL(videobuf_waiton);
1383 EXPORT_SYMBOL_GPL(videobuf_iolock);
1385 EXPORT_SYMBOL_GPL(videobuf_queue_init);
1386 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1387 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1389 EXPORT_SYMBOL_GPL(videobuf_next_field);
1390 EXPORT_SYMBOL_GPL(videobuf_status);
1391 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1392 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1393 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1394 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1395 EXPORT_SYMBOL_GPL(videobuf_streamon);
1396 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1398 EXPORT_SYMBOL_GPL(videobuf_read_start);
1399 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1400 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1401 EXPORT_SYMBOL_GPL(videobuf_read_one);
1402 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1404 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1405 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1406 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);