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 dma->varea = (void *) data;
153 down_read(¤t->mm->mmap_sem);
154 err = get_user_pages(current,current->mm,
155 data & PAGE_MASK, dma->nr_pages,
156 rw == READ, 1, /* force */
158 up_read(¤t->mm->mmap_sem);
159 if (err != dma->nr_pages) {
160 dma->nr_pages = (err >= 0) ? err : 0;
161 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
162 return err < 0 ? err : -EINVAL;
167 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
170 dprintk(1,"init kernel [%d pages]\n",nr_pages);
171 dma->direction = direction;
172 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
173 if (NULL == dma->vmalloc) {
174 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
177 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
178 (unsigned long)dma->vmalloc,
179 nr_pages << PAGE_SHIFT);
180 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
181 dma->nr_pages = nr_pages;
185 int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
186 dma_addr_t addr, int nr_pages)
188 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
189 nr_pages,(unsigned long)addr);
190 dma->direction = direction;
194 dma->bus_addr = addr;
195 dma->nr_pages = nr_pages;
199 int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
203 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
204 BUG_ON(0 == dma->nr_pages);
207 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
211 dma->sglist = videobuf_vmalloc_to_sg
212 (dma->vmalloc,dma->nr_pages);
215 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
216 if (NULL != dma->sglist) {
218 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
219 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
220 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
223 if (NULL == dma->sglist) {
224 dprintk(1,"scatterlist is NULL\n");
227 if (!dma->bus_addr) {
228 if (q->ops->vb_map_sg) {
229 dma->sglen = q->ops->vb_map_sg(dev,dma->sglist,
230 dma->nr_pages, dma->direction);
232 if (0 == dma->sglen) {
234 "%s: videobuf_map_sg failed\n",__FUNCTION__);
244 int videobuf_dma_sync(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
248 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
251 if (!dma->bus_addr && q->ops->vb_dma_sync_sg)
252 q->ops->vb_dma_sync_sg(dev,dma->sglist,dma->nr_pages,
258 int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
262 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
266 if (!dma->bus_addr && q->ops->vb_unmap_sg)
267 q->ops->vb_unmap_sg(dev,dma->sglist,dma->nr_pages,
275 int videobuf_dma_free(struct videobuf_dmabuf *dma)
277 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
282 for (i=0; i < dma->nr_pages; i++)
283 page_cache_release(dma->pages[i]);
295 dma->direction = PCI_DMA_NONE;
299 /* --------------------------------------------------------------------- */
301 void* videobuf_alloc(unsigned int size)
303 struct videobuf_buffer *vb;
305 vb = kzalloc(size,GFP_KERNEL);
307 videobuf_dma_init(&vb->dma);
308 init_waitqueue_head(&vb->done);
309 vb->magic = MAGIC_BUFFER;
314 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
317 DECLARE_WAITQUEUE(wait, current);
319 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
320 add_wait_queue(&vb->done, &wait);
321 while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
326 set_current_state(intr ? TASK_INTERRUPTIBLE
327 : TASK_UNINTERRUPTIBLE);
328 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
330 set_current_state(TASK_RUNNING);
331 if (intr && signal_pending(current)) {
332 dprintk(1,"buffer waiton: -EINTR\n");
337 remove_wait_queue(&vb->done, &wait);
342 videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
343 struct v4l2_framebuffer *fbuf)
348 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
349 switch (vb->memory) {
350 case V4L2_MEMORY_MMAP:
351 case V4L2_MEMORY_USERPTR:
352 if (0 == vb->baddr) {
353 /* no userspace addr -- kernel bounce buffer */
354 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
355 err = videobuf_dma_init_kernel(&vb->dma,PCI_DMA_FROMDEVICE,
360 /* dma directly to userspace */
361 err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE,
362 vb->baddr,vb->bsize);
367 case V4L2_MEMORY_OVERLAY:
370 /* FIXME: need sanity checks for vb->boff */
372 * Using a double cast to avoid compiler warnings when
373 * building for PAE. Compiler doesn't like direct casting
374 * of a 32 bit ptr to 64 bit integer.
376 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
377 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
378 err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
386 err = videobuf_dma_map(q,&vb->dma);
393 /* --------------------------------------------------------------------- */
395 void videobuf_queue_pci(struct videobuf_queue* q)
397 /* If not specified, defaults to PCI map sg */
398 if (!q->ops->vb_map_sg)
399 q->ops->vb_map_sg=(vb_map_sg_t *)pci_map_sg;
401 if (!q->ops->vb_dma_sync_sg)
402 q->ops->vb_dma_sync_sg=(vb_map_sg_t *)pci_dma_sync_sg_for_cpu;
403 if (!q->ops->vb_unmap_sg)
404 q->ops->vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
407 int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma)
409 struct videobuf_queue q;
410 struct videobuf_queue_ops qops;
413 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
414 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
417 return (videobuf_dma_map(&q,dma));
420 int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma)
422 struct videobuf_queue q;
423 struct videobuf_queue_ops qops;
426 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
427 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
430 return (videobuf_dma_unmap(&q,dma));
433 void videobuf_queue_init(struct videobuf_queue* q,
434 struct videobuf_queue_ops *ops,
437 enum v4l2_buf_type type,
438 enum v4l2_field field,
442 memset(q,0,sizeof(*q));
443 q->irqlock = irqlock;
451 videobuf_queue_pci(q);
453 mutex_init(&q->lock);
454 INIT_LIST_HEAD(&q->stream);
458 videobuf_queue_is_busy(struct videobuf_queue *q)
463 dprintk(1,"busy: streaming active\n");
467 dprintk(1,"busy: pending read #1\n");
471 dprintk(1,"busy: pending read #2\n");
474 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
475 if (NULL == q->bufs[i])
477 if (q->bufs[i]->map) {
478 dprintk(1,"busy: buffer #%d mapped\n",i);
481 if (q->bufs[i]->state == STATE_QUEUED) {
482 dprintk(1,"busy: buffer #%d queued\n",i);
485 if (q->bufs[i]->state == STATE_ACTIVE) {
486 dprintk(1,"busy: buffer #%d avtive\n",i);
494 videobuf_queue_cancel(struct videobuf_queue *q)
496 unsigned long flags=0;
499 /* remove queued buffers from list */
501 spin_lock_irqsave(q->irqlock,flags);
502 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
503 if (NULL == q->bufs[i])
505 if (q->bufs[i]->state == STATE_QUEUED) {
506 list_del(&q->bufs[i]->queue);
507 q->bufs[i]->state = STATE_ERROR;
511 spin_unlock_irqrestore(q->irqlock,flags);
513 /* free all buffers + clear queue */
514 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
515 if (NULL == q->bufs[i])
517 q->ops->buf_release(q,q->bufs[i]);
519 INIT_LIST_HEAD(&q->stream);
522 /* --------------------------------------------------------------------- */
525 videobuf_next_field(struct videobuf_queue *q)
527 enum v4l2_field field = q->field;
529 BUG_ON(V4L2_FIELD_ANY == field);
531 if (V4L2_FIELD_ALTERNATE == field) {
532 if (V4L2_FIELD_TOP == q->last) {
533 field = V4L2_FIELD_BOTTOM;
534 q->last = V4L2_FIELD_BOTTOM;
536 field = V4L2_FIELD_TOP;
537 q->last = V4L2_FIELD_TOP;
544 videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
545 enum v4l2_buf_type type)
547 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
552 b->memory = vb->memory;
554 case V4L2_MEMORY_MMAP:
555 b->m.offset = vb->boff;
556 b->length = vb->bsize;
558 case V4L2_MEMORY_USERPTR:
559 b->m.userptr = vb->baddr;
560 b->length = vb->bsize;
562 case V4L2_MEMORY_OVERLAY:
563 b->m.offset = vb->boff;
569 b->flags |= V4L2_BUF_FLAG_MAPPED;
575 b->flags |= V4L2_BUF_FLAG_QUEUED;
579 b->flags |= V4L2_BUF_FLAG_DONE;
581 case STATE_NEEDS_INIT:
587 if (vb->input != UNSET) {
588 b->flags |= V4L2_BUF_FLAG_INPUT;
589 b->input = vb->input;
592 b->field = vb->field;
593 b->timestamp = vb->ts;
594 b->bytesused = vb->size;
595 b->sequence = vb->field_count >> 1;
599 videobuf_reqbufs(struct videobuf_queue *q,
600 struct v4l2_requestbuffers *req)
602 unsigned int size,count;
605 if (req->type != q->type) {
606 dprintk(1,"reqbufs: queue type invalid\n");
609 if (req->count < 1) {
610 dprintk(1,"reqbufs: count invalid (%d)\n",req->count);
613 if (req->memory != V4L2_MEMORY_MMAP &&
614 req->memory != V4L2_MEMORY_USERPTR &&
615 req->memory != V4L2_MEMORY_OVERLAY) {
616 dprintk(1,"reqbufs: memory type invalid\n");
621 dprintk(1,"reqbufs: streaming already exists\n");
624 if (!list_empty(&q->stream)) {
625 dprintk(1,"reqbufs: stream running\n");
629 mutex_lock(&q->lock);
631 if (count > VIDEO_MAX_FRAME)
632 count = VIDEO_MAX_FRAME;
634 q->ops->buf_setup(q,&count,&size);
635 size = PAGE_ALIGN(size);
636 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
637 count, size, (count*size)>>PAGE_SHIFT);
639 retval = videobuf_mmap_setup(q,count,size,req->memory);
641 dprintk(1,"reqbufs: mmap setup returned %d\n",retval);
648 mutex_unlock(&q->lock);
653 videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
655 if (unlikely(b->type != q->type)) {
656 dprintk(1,"querybuf: Wrong type.\n");
659 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
660 dprintk(1,"querybuf: index out of range.\n");
663 if (unlikely(NULL == q->bufs[b->index])) {
664 dprintk(1,"querybuf: buffer is null.\n");
667 videobuf_status(b,q->bufs[b->index],q->type);
672 videobuf_qbuf(struct videobuf_queue *q,
673 struct v4l2_buffer *b)
675 struct videobuf_buffer *buf;
676 enum v4l2_field field;
677 unsigned long flags=0;
680 mutex_lock(&q->lock);
683 dprintk(1,"qbuf: Reading running...\n");
687 if (b->type != q->type) {
688 dprintk(1,"qbuf: Wrong type.\n");
691 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
692 dprintk(1,"qbuf: index out of range.\n");
695 buf = q->bufs[b->index];
697 dprintk(1,"qbuf: buffer is null.\n");
700 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
701 if (buf->memory != b->memory) {
702 dprintk(1,"qbuf: memory type is wrong.\n");
705 if (buf->state == STATE_QUEUED ||
706 buf->state == STATE_PREPARED ||
707 buf->state == STATE_ACTIVE) {
708 dprintk(1,"qbuf: buffer is already queued or active.\n");
712 if (b->flags & V4L2_BUF_FLAG_INPUT) {
713 if (b->input >= q->inputs) {
714 dprintk(1,"qbuf: wrong input.\n");
717 buf->input = b->input;
723 case V4L2_MEMORY_MMAP:
724 if (0 == buf->baddr) {
725 dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n");
729 case V4L2_MEMORY_USERPTR:
730 if (b->length < buf->bsize) {
731 dprintk(1,"qbuf: buffer length is not enough\n");
734 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
735 q->ops->buf_release(q,buf);
736 buf->baddr = b->m.userptr;
738 case V4L2_MEMORY_OVERLAY:
739 buf->boff = b->m.offset;
742 dprintk(1,"qbuf: wrong memory type\n");
746 dprintk(1,"qbuf: requesting next field\n");
747 field = videobuf_next_field(q);
748 retval = q->ops->buf_prepare(q,buf,field);
750 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval);
754 list_add_tail(&buf->stream,&q->stream);
757 spin_lock_irqsave(q->irqlock,flags);
758 q->ops->buf_queue(q,buf);
760 spin_unlock_irqrestore(q->irqlock,flags);
762 dprintk(1,"qbuf: succeded\n");
766 mutex_unlock(&q->lock);
771 videobuf_dqbuf(struct videobuf_queue *q,
772 struct v4l2_buffer *b, int nonblocking)
774 struct videobuf_buffer *buf;
777 mutex_lock(&q->lock);
780 dprintk(1,"dqbuf: Reading running...\n");
784 if (b->type != q->type) {
785 dprintk(1,"dqbuf: Wrong type.\n");
788 if (list_empty(&q->stream)) {
789 dprintk(1,"dqbuf: stream running\n");
792 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
793 retval = videobuf_waiton(buf, nonblocking, 1);
795 dprintk(1,"dqbuf: waiton returned %d\n",retval);
798 switch (buf->state) {
800 dprintk(1,"dqbuf: state is error\n");
802 videobuf_dma_sync(q,&buf->dma);
803 buf->state = STATE_IDLE;
806 dprintk(1,"dqbuf: state is done\n");
807 videobuf_dma_sync(q,&buf->dma);
808 buf->state = STATE_IDLE;
811 dprintk(1,"dqbuf: state invalid\n");
815 list_del(&buf->stream);
816 memset(b,0,sizeof(*b));
817 videobuf_status(b,buf,q->type);
820 mutex_unlock(&q->lock);
824 int videobuf_streamon(struct videobuf_queue *q)
826 struct videobuf_buffer *buf;
827 struct list_head *list;
828 unsigned long flags=0;
831 mutex_lock(&q->lock);
840 spin_lock_irqsave(q->irqlock,flags);
841 list_for_each(list,&q->stream) {
842 buf = list_entry(list, struct videobuf_buffer, stream);
843 if (buf->state == STATE_PREPARED)
844 q->ops->buf_queue(q,buf);
847 spin_unlock_irqrestore(q->irqlock,flags);
850 mutex_unlock(&q->lock);
854 int videobuf_streamoff(struct videobuf_queue *q)
856 int retval = -EINVAL;
858 mutex_lock(&q->lock);
861 videobuf_queue_cancel(q);
866 mutex_unlock(&q->lock);
871 videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
872 size_t count, loff_t *ppos)
874 enum v4l2_field field;
875 unsigned long flags=0;
879 q->read_buf = videobuf_alloc(q->msize);
880 if (NULL == q->read_buf)
883 q->read_buf->memory = V4L2_MEMORY_USERPTR;
884 q->read_buf->baddr = (unsigned long)data;
885 q->read_buf->bsize = count;
886 field = videobuf_next_field(q);
887 retval = q->ops->buf_prepare(q,q->read_buf,field);
891 /* start capture & wait */
893 spin_lock_irqsave(q->irqlock,flags);
894 q->ops->buf_queue(q,q->read_buf);
896 spin_unlock_irqrestore(q->irqlock,flags);
897 retval = videobuf_waiton(q->read_buf,0,0);
899 videobuf_dma_sync(q,&q->read_buf->dma);
900 if (STATE_ERROR == q->read_buf->state)
903 retval = q->read_buf->size;
908 q->ops->buf_release(q,q->read_buf);
914 ssize_t videobuf_read_one(struct videobuf_queue *q,
915 char __user *data, size_t count, loff_t *ppos,
918 enum v4l2_field field;
919 unsigned long flags=0;
920 unsigned size, nbufs, bytes;
923 mutex_lock(&q->lock);
926 q->ops->buf_setup(q,&nbufs,&size);
927 if (NULL == q->read_buf &&
930 retval = videobuf_read_zerocopy(q,data,count,ppos);
931 if (retval >= 0 || retval == -EIO)
934 /* fallback to kernel bounce buffer on failures */
937 if (NULL == q->read_buf) {
938 /* need to capture a new frame */
940 q->read_buf = videobuf_alloc(q->msize);
941 dprintk(1,"video alloc=0x%p\n", q->read_buf);
942 if (NULL == q->read_buf)
944 q->read_buf->memory = V4L2_MEMORY_USERPTR;
945 q->read_buf->bsize = count; /* preferred size */
946 field = videobuf_next_field(q);
947 retval = q->ops->buf_prepare(q,q->read_buf,field);
954 spin_lock_irqsave(q->irqlock,flags);
955 q->ops->buf_queue(q,q->read_buf);
957 spin_unlock_irqrestore(q->irqlock,flags);
961 /* wait until capture is done */
962 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
965 videobuf_dma_sync(q,&q->read_buf->dma);
967 if (STATE_ERROR == q->read_buf->state) {
968 /* catch I/O errors */
969 q->ops->buf_release(q,q->read_buf);
976 /* copy to userspace */
978 if (bytes > q->read_buf->size - q->read_off)
979 bytes = q->read_buf->size - q->read_off;
981 if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
985 q->read_off += bytes;
986 if (q->read_off == q->read_buf->size) {
987 /* all data copied, cleanup */
988 q->ops->buf_release(q,q->read_buf);
994 mutex_unlock(&q->lock);
998 int videobuf_read_start(struct videobuf_queue *q)
1000 enum v4l2_field field;
1001 unsigned long flags=0;
1002 int count = 0, size = 0;
1005 q->ops->buf_setup(q,&count,&size);
1008 if (count > VIDEO_MAX_FRAME)
1009 count = VIDEO_MAX_FRAME;
1010 size = PAGE_ALIGN(size);
1012 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
1015 for (i = 0; i < count; i++) {
1016 field = videobuf_next_field(q);
1017 err = q->ops->buf_prepare(q,q->bufs[i],field);
1020 list_add_tail(&q->bufs[i]->stream, &q->stream);
1023 spin_lock_irqsave(q->irqlock,flags);
1024 for (i = 0; i < count; i++)
1025 q->ops->buf_queue(q,q->bufs[i]);
1027 spin_unlock_irqrestore(q->irqlock,flags);
1032 void videobuf_read_stop(struct videobuf_queue *q)
1036 videobuf_queue_cancel(q);
1037 videobuf_mmap_free(q);
1038 INIT_LIST_HEAD(&q->stream);
1039 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1040 if (NULL == q->bufs[i])
1049 ssize_t videobuf_read_stream(struct videobuf_queue *q,
1050 char __user *data, size_t count, loff_t *ppos,
1051 int vbihack, int nonblocking)
1053 unsigned int *fc, bytes;
1055 unsigned long flags=0;
1057 dprintk(2,"%s\n",__FUNCTION__);
1058 mutex_lock(&q->lock);
1063 retval = videobuf_read_start(q);
1070 /* get / wait for data */
1071 if (NULL == q->read_buf) {
1072 q->read_buf = list_entry(q->stream.next,
1073 struct videobuf_buffer,
1075 list_del(&q->read_buf->stream);
1078 err = videobuf_waiton(q->read_buf, nonblocking, 1);
1085 if (q->read_buf->state == STATE_DONE) {
1087 /* dirty, undocumented hack -- pass the frame counter
1088 * within the last four bytes of each vbi data block.
1089 * We need that one to maintain backward compatibility
1090 * to all vbi decoding software out there ... */
1091 fc = (unsigned int*)q->read_buf->dma.vmalloc;
1092 fc += (q->read_buf->size>>2) -1;
1093 *fc = q->read_buf->field_count >> 1;
1094 dprintk(1,"vbihack: %d\n",*fc);
1099 if (bytes > q->read_buf->size - q->read_off)
1100 bytes = q->read_buf->size - q->read_off;
1101 if (copy_to_user(data + retval,
1102 q->read_buf->dma.vmalloc + q->read_off,
1110 q->read_off += bytes;
1113 q->read_off = q->read_buf->size;
1118 /* requeue buffer when done with copying */
1119 if (q->read_off == q->read_buf->size) {
1120 list_add_tail(&q->read_buf->stream,
1123 spin_lock_irqsave(q->irqlock,flags);
1124 q->ops->buf_queue(q,q->read_buf);
1126 spin_unlock_irqrestore(q->irqlock,flags);
1134 mutex_unlock(&q->lock);
1138 unsigned int videobuf_poll_stream(struct file *file,
1139 struct videobuf_queue *q,
1142 struct videobuf_buffer *buf = NULL;
1143 unsigned int rc = 0;
1145 mutex_lock(&q->lock);
1147 if (!list_empty(&q->stream))
1148 buf = list_entry(q->stream.next,
1149 struct videobuf_buffer, stream);
1152 videobuf_read_start(q);
1155 } else if (NULL == q->read_buf) {
1156 q->read_buf = list_entry(q->stream.next,
1157 struct videobuf_buffer,
1159 list_del(&q->read_buf->stream);
1168 poll_wait(file, &buf->done, wait);
1169 if (buf->state == STATE_DONE ||
1170 buf->state == STATE_ERROR)
1171 rc = POLLIN|POLLRDNORM;
1173 mutex_unlock(&q->lock);
1177 /* --------------------------------------------------------------------- */
1180 videobuf_vm_open(struct vm_area_struct *vma)
1182 struct videobuf_mapping *map = vma->vm_private_data;
1184 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1185 map->count,vma->vm_start,vma->vm_end);
1190 videobuf_vm_close(struct vm_area_struct *vma)
1192 struct videobuf_mapping *map = vma->vm_private_data;
1193 struct videobuf_queue *q = map->q;
1196 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1197 map->count,vma->vm_start,vma->vm_end);
1200 if (0 == map->count) {
1201 dprintk(1,"munmap %p q=%p\n",map,q);
1202 mutex_lock(&q->lock);
1203 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1204 if (NULL == q->bufs[i])
1208 if (q->bufs[i]->map != map)
1210 q->bufs[i]->map = NULL;
1211 q->bufs[i]->baddr = 0;
1212 q->ops->buf_release(q,q->bufs[i]);
1214 mutex_unlock(&q->lock);
1221 * Get a anonymous page for the mapping. Make sure we can DMA to that
1222 * memory location with 32bit PCI devices (i.e. don't use highmem for
1223 * now ...). Bounce buffers don't work very well for the data rates
1224 * video capture has.
1227 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1232 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1233 vaddr,vma->vm_start,vma->vm_end);
1234 if (vaddr > vma->vm_end)
1235 return NOPAGE_SIGBUS;
1236 page = alloc_page(GFP_USER | __GFP_DMA32);
1239 clear_user_page(page_address(page), vaddr, page);
1241 *type = VM_FAULT_MINOR;
1245 static struct vm_operations_struct videobuf_vm_ops =
1247 .open = videobuf_vm_open,
1248 .close = videobuf_vm_close,
1249 .nopage = videobuf_vm_nopage,
1252 int videobuf_mmap_setup(struct videobuf_queue *q,
1253 unsigned int bcount, unsigned int bsize,
1254 enum v4l2_memory memory)
1259 err = videobuf_mmap_free(q);
1263 for (i = 0; i < bcount; i++) {
1264 q->bufs[i] = videobuf_alloc(q->msize);
1266 q->bufs[i]->input = UNSET;
1267 q->bufs[i]->memory = memory;
1268 q->bufs[i]->bsize = bsize;
1270 case V4L2_MEMORY_MMAP:
1271 q->bufs[i]->boff = bsize * i;
1273 case V4L2_MEMORY_USERPTR:
1274 case V4L2_MEMORY_OVERLAY:
1279 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1284 int videobuf_mmap_free(struct videobuf_queue *q)
1288 for (i = 0; i < VIDEO_MAX_FRAME; i++)
1289 if (q->bufs[i] && q->bufs[i]->map)
1291 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1292 if (NULL == q->bufs[i])
1294 q->ops->buf_release(q,q->bufs[i]);
1301 int videobuf_mmap_mapper(struct videobuf_queue *q,
1302 struct vm_area_struct *vma)
1304 struct videobuf_mapping *map;
1305 unsigned int first,last,size,i;
1308 mutex_lock(&q->lock);
1310 if (!(vma->vm_flags & VM_WRITE)) {
1311 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1314 if (!(vma->vm_flags & VM_SHARED)) {
1315 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1319 /* look for first buffer to map */
1320 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1321 if (NULL == q->bufs[first])
1323 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1325 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1328 if (VIDEO_MAX_FRAME == first) {
1329 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1330 (vma->vm_pgoff << PAGE_SHIFT));
1334 /* look for last buffer to map */
1335 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1336 if (NULL == q->bufs[last])
1338 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1340 if (q->bufs[last]->map) {
1344 size += q->bufs[last]->bsize;
1345 if (size == (vma->vm_end - vma->vm_start))
1348 if (VIDEO_MAX_FRAME == last) {
1349 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1350 (vma->vm_end - vma->vm_start));
1354 /* create mapping + update buffer list */
1356 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1359 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1360 q->bufs[i]->map = map;
1361 q->bufs[i]->baddr = vma->vm_start + size;
1364 map->start = vma->vm_start;
1365 map->end = vma->vm_end;
1367 vma->vm_ops = &videobuf_vm_ops;
1368 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1369 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1370 vma->vm_private_data = map;
1371 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1372 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1376 mutex_unlock(&q->lock);
1380 /* --------------------------------------------------------------------- */
1382 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1384 EXPORT_SYMBOL_GPL(videobuf_dma_init);
1385 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1386 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1387 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1388 EXPORT_SYMBOL_GPL(videobuf_dma_map);
1389 EXPORT_SYMBOL_GPL(videobuf_dma_sync);
1390 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
1391 EXPORT_SYMBOL_GPL(videobuf_dma_free);
1393 EXPORT_SYMBOL_GPL(videobuf_pci_dma_map);
1394 EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap);
1396 EXPORT_SYMBOL_GPL(videobuf_alloc);
1397 EXPORT_SYMBOL_GPL(videobuf_waiton);
1398 EXPORT_SYMBOL_GPL(videobuf_iolock);
1400 EXPORT_SYMBOL_GPL(videobuf_queue_init);
1401 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1402 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1404 EXPORT_SYMBOL_GPL(videobuf_next_field);
1405 EXPORT_SYMBOL_GPL(videobuf_status);
1406 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1407 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1408 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1409 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1410 EXPORT_SYMBOL_GPL(videobuf_streamon);
1411 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1413 EXPORT_SYMBOL_GPL(videobuf_read_start);
1414 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1415 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1416 EXPORT_SYMBOL_GPL(videobuf_read_one);
1417 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1419 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1420 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1421 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);