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 */
369 * Using a double cast to avoid compiler warnings when
370 * building for PAE. Compiler doesn't like direct casting
371 * of a 32 bit ptr to 64 bit integer.
373 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
374 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
375 err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
383 err = videobuf_dma_map(q,&vb->dma);
390 /* --------------------------------------------------------------------- */
392 void videobuf_queue_pci(struct videobuf_queue* q)
394 /* If not specified, defaults to PCI map sg */
395 if (!q->ops->vb_map_sg)
396 q->ops->vb_map_sg=(vb_map_sg_t *)pci_map_sg;
398 if (!q->ops->vb_dma_sync_sg)
399 q->ops->vb_dma_sync_sg=(vb_map_sg_t *)pci_dma_sync_sg_for_cpu;
400 if (!q->ops->vb_unmap_sg)
401 q->ops->vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
404 int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma)
406 struct videobuf_queue q;
407 struct videobuf_queue_ops qops;
410 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
411 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
414 return (videobuf_dma_map(&q,dma));
417 int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma)
419 struct videobuf_queue q;
420 struct videobuf_queue_ops qops;
423 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
424 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
427 return (videobuf_dma_unmap(&q,dma));
430 void videobuf_queue_init(struct videobuf_queue* q,
431 struct videobuf_queue_ops *ops,
434 enum v4l2_buf_type type,
435 enum v4l2_field field,
439 memset(q,0,sizeof(*q));
440 q->irqlock = irqlock;
448 videobuf_queue_pci(q);
450 mutex_init(&q->lock);
451 INIT_LIST_HEAD(&q->stream);
455 videobuf_queue_is_busy(struct videobuf_queue *q)
460 dprintk(1,"busy: streaming active\n");
464 dprintk(1,"busy: pending read #1\n");
468 dprintk(1,"busy: pending read #2\n");
471 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
472 if (NULL == q->bufs[i])
474 if (q->bufs[i]->map) {
475 dprintk(1,"busy: buffer #%d mapped\n",i);
478 if (q->bufs[i]->state == STATE_QUEUED) {
479 dprintk(1,"busy: buffer #%d queued\n",i);
482 if (q->bufs[i]->state == STATE_ACTIVE) {
483 dprintk(1,"busy: buffer #%d avtive\n",i);
491 videobuf_queue_cancel(struct videobuf_queue *q)
493 unsigned long flags=0;
496 /* remove queued buffers from list */
498 spin_lock_irqsave(q->irqlock,flags);
499 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
500 if (NULL == q->bufs[i])
502 if (q->bufs[i]->state == STATE_QUEUED) {
503 list_del(&q->bufs[i]->queue);
504 q->bufs[i]->state = STATE_ERROR;
508 spin_unlock_irqrestore(q->irqlock,flags);
510 /* free all buffers + clear queue */
511 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
512 if (NULL == q->bufs[i])
514 q->ops->buf_release(q,q->bufs[i]);
516 INIT_LIST_HEAD(&q->stream);
519 /* --------------------------------------------------------------------- */
522 videobuf_next_field(struct videobuf_queue *q)
524 enum v4l2_field field = q->field;
526 BUG_ON(V4L2_FIELD_ANY == field);
528 if (V4L2_FIELD_ALTERNATE == field) {
529 if (V4L2_FIELD_TOP == q->last) {
530 field = V4L2_FIELD_BOTTOM;
531 q->last = V4L2_FIELD_BOTTOM;
533 field = V4L2_FIELD_TOP;
534 q->last = V4L2_FIELD_TOP;
541 videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
542 enum v4l2_buf_type type)
544 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
549 b->memory = vb->memory;
551 case V4L2_MEMORY_MMAP:
552 b->m.offset = vb->boff;
553 b->length = vb->bsize;
555 case V4L2_MEMORY_USERPTR:
556 b->m.userptr = vb->baddr;
557 b->length = vb->bsize;
559 case V4L2_MEMORY_OVERLAY:
560 b->m.offset = vb->boff;
566 b->flags |= V4L2_BUF_FLAG_MAPPED;
572 b->flags |= V4L2_BUF_FLAG_QUEUED;
576 b->flags |= V4L2_BUF_FLAG_DONE;
578 case STATE_NEEDS_INIT:
584 if (vb->input != UNSET) {
585 b->flags |= V4L2_BUF_FLAG_INPUT;
586 b->input = vb->input;
589 b->field = vb->field;
590 b->timestamp = vb->ts;
591 b->bytesused = vb->size;
592 b->sequence = vb->field_count >> 1;
596 videobuf_reqbufs(struct videobuf_queue *q,
597 struct v4l2_requestbuffers *req)
599 unsigned int size,count;
602 if (req->type != q->type) {
603 dprintk(1,"reqbufs: queue type invalid\n");
606 if (req->count < 1) {
607 dprintk(1,"reqbufs: count invalid (%d)\n",req->count);
610 if (req->memory != V4L2_MEMORY_MMAP &&
611 req->memory != V4L2_MEMORY_USERPTR &&
612 req->memory != V4L2_MEMORY_OVERLAY) {
613 dprintk(1,"reqbufs: memory type invalid\n");
618 dprintk(1,"reqbufs: streaming already exists\n");
621 if (!list_empty(&q->stream)) {
622 dprintk(1,"reqbufs: stream running\n");
626 mutex_lock(&q->lock);
628 if (count > VIDEO_MAX_FRAME)
629 count = VIDEO_MAX_FRAME;
631 q->ops->buf_setup(q,&count,&size);
632 size = PAGE_ALIGN(size);
633 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
634 count, size, (count*size)>>PAGE_SHIFT);
636 retval = videobuf_mmap_setup(q,count,size,req->memory);
638 dprintk(1,"reqbufs: mmap setup returned %d\n",retval);
645 mutex_unlock(&q->lock);
650 videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
652 if (unlikely(b->type != q->type)) {
653 dprintk(1,"querybuf: Wrong type.\n");
656 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
657 dprintk(1,"querybuf: index out of range.\n");
660 if (unlikely(NULL == q->bufs[b->index])) {
661 dprintk(1,"querybuf: buffer is null.\n");
664 videobuf_status(b,q->bufs[b->index],q->type);
669 videobuf_qbuf(struct videobuf_queue *q,
670 struct v4l2_buffer *b)
672 struct videobuf_buffer *buf;
673 enum v4l2_field field;
674 unsigned long flags=0;
677 mutex_lock(&q->lock);
680 dprintk(1,"qbuf: Reading running...\n");
684 if (b->type != q->type) {
685 dprintk(1,"qbuf: Wrong type.\n");
688 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
689 dprintk(1,"qbuf: index out of range.\n");
692 buf = q->bufs[b->index];
694 dprintk(1,"qbuf: buffer is null.\n");
697 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
698 if (buf->memory != b->memory) {
699 dprintk(1,"qbuf: memory type is wrong.\n");
702 if (buf->state == STATE_QUEUED ||
703 buf->state == STATE_ACTIVE) {
704 dprintk(1,"qbuf: buffer is already queued or active.\n");
708 if (b->flags & V4L2_BUF_FLAG_INPUT) {
709 if (b->input >= q->inputs) {
710 dprintk(1,"qbuf: wrong input.\n");
713 buf->input = b->input;
719 case V4L2_MEMORY_MMAP:
720 if (0 == buf->baddr) {
721 dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n");
725 case V4L2_MEMORY_USERPTR:
726 if (b->length < buf->bsize) {
727 dprintk(1,"qbuf: buffer length is not enough\n");
730 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
731 q->ops->buf_release(q,buf);
732 buf->baddr = b->m.userptr;
734 case V4L2_MEMORY_OVERLAY:
735 buf->boff = b->m.offset;
738 dprintk(1,"qbuf: wrong memory type\n");
742 dprintk(1,"qbuf: requesting next field\n");
743 field = videobuf_next_field(q);
744 retval = q->ops->buf_prepare(q,buf,field);
746 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval);
750 list_add_tail(&buf->stream,&q->stream);
753 spin_lock_irqsave(q->irqlock,flags);
754 q->ops->buf_queue(q,buf);
756 spin_unlock_irqrestore(q->irqlock,flags);
758 dprintk(1,"qbuf: succeded\n");
762 mutex_unlock(&q->lock);
767 videobuf_dqbuf(struct videobuf_queue *q,
768 struct v4l2_buffer *b, int nonblocking)
770 struct videobuf_buffer *buf;
773 mutex_lock(&q->lock);
776 dprintk(1,"dqbuf: Reading running...\n");
780 if (b->type != q->type) {
781 dprintk(1,"dqbuf: Wrong type.\n");
784 if (list_empty(&q->stream)) {
785 dprintk(1,"dqbuf: stream running\n");
788 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
789 retval = videobuf_waiton(buf, nonblocking, 1);
791 dprintk(1,"dqbuf: waiton returned %d\n",retval);
794 switch (buf->state) {
796 dprintk(1,"dqbuf: state is error\n");
798 videobuf_dma_sync(q,&buf->dma);
799 buf->state = STATE_IDLE;
802 dprintk(1,"dqbuf: state is done\n");
803 videobuf_dma_sync(q,&buf->dma);
804 buf->state = STATE_IDLE;
807 dprintk(1,"dqbuf: state invalid\n");
811 list_del(&buf->stream);
812 memset(b,0,sizeof(*b));
813 videobuf_status(b,buf,q->type);
816 mutex_unlock(&q->lock);
820 int videobuf_streamon(struct videobuf_queue *q)
822 struct videobuf_buffer *buf;
823 struct list_head *list;
824 unsigned long flags=0;
827 mutex_lock(&q->lock);
836 spin_lock_irqsave(q->irqlock,flags);
837 list_for_each(list,&q->stream) {
838 buf = list_entry(list, struct videobuf_buffer, stream);
839 if (buf->state == STATE_PREPARED)
840 q->ops->buf_queue(q,buf);
843 spin_unlock_irqrestore(q->irqlock,flags);
846 mutex_unlock(&q->lock);
850 int videobuf_streamoff(struct videobuf_queue *q)
852 int retval = -EINVAL;
854 mutex_lock(&q->lock);
857 videobuf_queue_cancel(q);
862 mutex_unlock(&q->lock);
867 videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
868 size_t count, loff_t *ppos)
870 enum v4l2_field field;
871 unsigned long flags=0;
875 q->read_buf = videobuf_alloc(q->msize);
876 if (NULL == q->read_buf)
879 q->read_buf->memory = V4L2_MEMORY_USERPTR;
880 q->read_buf->baddr = (unsigned long)data;
881 q->read_buf->bsize = count;
882 field = videobuf_next_field(q);
883 retval = q->ops->buf_prepare(q,q->read_buf,field);
887 /* start capture & wait */
889 spin_lock_irqsave(q->irqlock,flags);
890 q->ops->buf_queue(q,q->read_buf);
892 spin_unlock_irqrestore(q->irqlock,flags);
893 retval = videobuf_waiton(q->read_buf,0,0);
895 videobuf_dma_sync(q,&q->read_buf->dma);
896 if (STATE_ERROR == q->read_buf->state)
899 retval = q->read_buf->size;
904 q->ops->buf_release(q,q->read_buf);
910 ssize_t videobuf_read_one(struct videobuf_queue *q,
911 char __user *data, size_t count, loff_t *ppos,
914 enum v4l2_field field;
915 unsigned long flags=0;
916 unsigned size, nbufs, bytes;
919 mutex_lock(&q->lock);
922 q->ops->buf_setup(q,&nbufs,&size);
923 if (NULL == q->read_buf &&
926 retval = videobuf_read_zerocopy(q,data,count,ppos);
927 if (retval >= 0 || retval == -EIO)
930 /* fallback to kernel bounce buffer on failures */
933 if (NULL == q->read_buf) {
934 /* need to capture a new frame */
936 q->read_buf = videobuf_alloc(q->msize);
937 dprintk(1,"video alloc=0x%p\n", q->read_buf);
938 if (NULL == q->read_buf)
940 q->read_buf->memory = V4L2_MEMORY_USERPTR;
941 q->read_buf->bsize = count; /* preferred size */
942 field = videobuf_next_field(q);
943 retval = q->ops->buf_prepare(q,q->read_buf,field);
950 spin_lock_irqsave(q->irqlock,flags);
951 q->ops->buf_queue(q,q->read_buf);
953 spin_unlock_irqrestore(q->irqlock,flags);
957 /* wait until capture is done */
958 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
961 videobuf_dma_sync(q,&q->read_buf->dma);
963 if (STATE_ERROR == q->read_buf->state) {
964 /* catch I/O errors */
965 q->ops->buf_release(q,q->read_buf);
972 /* copy to userspace */
974 if (bytes > q->read_buf->size - q->read_off)
975 bytes = q->read_buf->size - q->read_off;
977 if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
981 q->read_off += bytes;
982 if (q->read_off == q->read_buf->size) {
983 /* all data copied, cleanup */
984 q->ops->buf_release(q,q->read_buf);
990 mutex_unlock(&q->lock);
994 int videobuf_read_start(struct videobuf_queue *q)
996 enum v4l2_field field;
997 unsigned long flags=0;
998 int count = 0, size = 0;
1001 q->ops->buf_setup(q,&count,&size);
1004 if (count > VIDEO_MAX_FRAME)
1005 count = VIDEO_MAX_FRAME;
1006 size = PAGE_ALIGN(size);
1008 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
1011 for (i = 0; i < count; i++) {
1012 field = videobuf_next_field(q);
1013 err = q->ops->buf_prepare(q,q->bufs[i],field);
1016 list_add_tail(&q->bufs[i]->stream, &q->stream);
1019 spin_lock_irqsave(q->irqlock,flags);
1020 for (i = 0; i < count; i++)
1021 q->ops->buf_queue(q,q->bufs[i]);
1023 spin_unlock_irqrestore(q->irqlock,flags);
1028 void videobuf_read_stop(struct videobuf_queue *q)
1032 videobuf_queue_cancel(q);
1033 videobuf_mmap_free(q);
1034 INIT_LIST_HEAD(&q->stream);
1035 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1036 if (NULL == q->bufs[i])
1045 ssize_t videobuf_read_stream(struct videobuf_queue *q,
1046 char __user *data, size_t count, loff_t *ppos,
1047 int vbihack, int nonblocking)
1049 unsigned int *fc, bytes;
1051 unsigned long flags=0;
1053 dprintk(2,"%s\n",__FUNCTION__);
1054 mutex_lock(&q->lock);
1059 retval = videobuf_read_start(q);
1066 /* get / wait for data */
1067 if (NULL == q->read_buf) {
1068 q->read_buf = list_entry(q->stream.next,
1069 struct videobuf_buffer,
1071 list_del(&q->read_buf->stream);
1074 err = videobuf_waiton(q->read_buf, nonblocking, 1);
1081 if (q->read_buf->state == STATE_DONE) {
1083 /* dirty, undocumented hack -- pass the frame counter
1084 * within the last four bytes of each vbi data block.
1085 * We need that one to maintain backward compatibility
1086 * to all vbi decoding software out there ... */
1087 fc = (unsigned int*)q->read_buf->dma.vmalloc;
1088 fc += (q->read_buf->size>>2) -1;
1089 *fc = q->read_buf->field_count >> 1;
1090 dprintk(1,"vbihack: %d\n",*fc);
1095 if (bytes > q->read_buf->size - q->read_off)
1096 bytes = q->read_buf->size - q->read_off;
1097 if (copy_to_user(data + retval,
1098 q->read_buf->dma.vmalloc + q->read_off,
1106 q->read_off += bytes;
1109 q->read_off = q->read_buf->size;
1114 /* requeue buffer when done with copying */
1115 if (q->read_off == q->read_buf->size) {
1116 list_add_tail(&q->read_buf->stream,
1119 spin_lock_irqsave(q->irqlock,flags);
1120 q->ops->buf_queue(q,q->read_buf);
1122 spin_unlock_irqrestore(q->irqlock,flags);
1130 mutex_unlock(&q->lock);
1134 unsigned int videobuf_poll_stream(struct file *file,
1135 struct videobuf_queue *q,
1138 struct videobuf_buffer *buf = NULL;
1139 unsigned int rc = 0;
1141 mutex_lock(&q->lock);
1143 if (!list_empty(&q->stream))
1144 buf = list_entry(q->stream.next,
1145 struct videobuf_buffer, stream);
1148 videobuf_read_start(q);
1151 } else if (NULL == q->read_buf) {
1152 q->read_buf = list_entry(q->stream.next,
1153 struct videobuf_buffer,
1155 list_del(&q->read_buf->stream);
1164 poll_wait(file, &buf->done, wait);
1165 if (buf->state == STATE_DONE ||
1166 buf->state == STATE_ERROR)
1167 rc = POLLIN|POLLRDNORM;
1169 mutex_unlock(&q->lock);
1173 /* --------------------------------------------------------------------- */
1176 videobuf_vm_open(struct vm_area_struct *vma)
1178 struct videobuf_mapping *map = vma->vm_private_data;
1180 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1181 map->count,vma->vm_start,vma->vm_end);
1186 videobuf_vm_close(struct vm_area_struct *vma)
1188 struct videobuf_mapping *map = vma->vm_private_data;
1189 struct videobuf_queue *q = map->q;
1192 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1193 map->count,vma->vm_start,vma->vm_end);
1196 if (0 == map->count) {
1197 dprintk(1,"munmap %p q=%p\n",map,q);
1198 mutex_lock(&q->lock);
1199 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1200 if (NULL == q->bufs[i])
1204 if (q->bufs[i]->map != map)
1206 q->bufs[i]->map = NULL;
1207 q->bufs[i]->baddr = 0;
1208 q->ops->buf_release(q,q->bufs[i]);
1210 mutex_unlock(&q->lock);
1217 * Get a anonymous page for the mapping. Make sure we can DMA to that
1218 * memory location with 32bit PCI devices (i.e. don't use highmem for
1219 * now ...). Bounce buffers don't work very well for the data rates
1220 * video capture has.
1223 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1228 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1229 vaddr,vma->vm_start,vma->vm_end);
1230 if (vaddr > vma->vm_end)
1231 return NOPAGE_SIGBUS;
1232 page = alloc_page(GFP_USER | __GFP_DMA32);
1235 clear_user_page(page_address(page), vaddr, page);
1237 *type = VM_FAULT_MINOR;
1241 static struct vm_operations_struct videobuf_vm_ops =
1243 .open = videobuf_vm_open,
1244 .close = videobuf_vm_close,
1245 .nopage = videobuf_vm_nopage,
1248 int videobuf_mmap_setup(struct videobuf_queue *q,
1249 unsigned int bcount, unsigned int bsize,
1250 enum v4l2_memory memory)
1255 err = videobuf_mmap_free(q);
1259 for (i = 0; i < bcount; i++) {
1260 q->bufs[i] = videobuf_alloc(q->msize);
1262 q->bufs[i]->input = UNSET;
1263 q->bufs[i]->memory = memory;
1264 q->bufs[i]->bsize = bsize;
1266 case V4L2_MEMORY_MMAP:
1267 q->bufs[i]->boff = bsize * i;
1269 case V4L2_MEMORY_USERPTR:
1270 case V4L2_MEMORY_OVERLAY:
1275 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1280 int videobuf_mmap_free(struct videobuf_queue *q)
1284 for (i = 0; i < VIDEO_MAX_FRAME; i++)
1285 if (q->bufs[i] && q->bufs[i]->map)
1287 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1288 if (NULL == q->bufs[i])
1290 q->ops->buf_release(q,q->bufs[i]);
1297 int videobuf_mmap_mapper(struct videobuf_queue *q,
1298 struct vm_area_struct *vma)
1300 struct videobuf_mapping *map;
1301 unsigned int first,last,size,i;
1304 mutex_lock(&q->lock);
1306 if (!(vma->vm_flags & VM_WRITE)) {
1307 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1310 if (!(vma->vm_flags & VM_SHARED)) {
1311 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1315 /* look for first buffer to map */
1316 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1317 if (NULL == q->bufs[first])
1319 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1321 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1324 if (VIDEO_MAX_FRAME == first) {
1325 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1326 (vma->vm_pgoff << PAGE_SHIFT));
1330 /* look for last buffer to map */
1331 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1332 if (NULL == q->bufs[last])
1334 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1336 if (q->bufs[last]->map) {
1340 size += q->bufs[last]->bsize;
1341 if (size == (vma->vm_end - vma->vm_start))
1344 if (VIDEO_MAX_FRAME == last) {
1345 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1346 (vma->vm_end - vma->vm_start));
1350 /* create mapping + update buffer list */
1352 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1355 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1356 q->bufs[i]->map = map;
1357 q->bufs[i]->baddr = vma->vm_start + size;
1360 map->start = vma->vm_start;
1361 map->end = vma->vm_end;
1363 vma->vm_ops = &videobuf_vm_ops;
1364 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1365 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1366 vma->vm_private_data = map;
1367 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1368 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1372 mutex_unlock(&q->lock);
1376 /* --------------------------------------------------------------------- */
1378 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1380 EXPORT_SYMBOL_GPL(videobuf_dma_init);
1381 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1382 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1383 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1384 EXPORT_SYMBOL_GPL(videobuf_dma_map);
1385 EXPORT_SYMBOL_GPL(videobuf_dma_sync);
1386 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
1387 EXPORT_SYMBOL_GPL(videobuf_dma_free);
1389 EXPORT_SYMBOL_GPL(videobuf_pci_dma_map);
1390 EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap);
1392 EXPORT_SYMBOL_GPL(videobuf_alloc);
1393 EXPORT_SYMBOL_GPL(videobuf_waiton);
1394 EXPORT_SYMBOL_GPL(videobuf_iolock);
1396 EXPORT_SYMBOL_GPL(videobuf_queue_init);
1397 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1398 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1400 EXPORT_SYMBOL_GPL(videobuf_next_field);
1401 EXPORT_SYMBOL_GPL(videobuf_status);
1402 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1403 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1404 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1405 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1406 EXPORT_SYMBOL_GPL(videobuf_streamon);
1407 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1409 EXPORT_SYMBOL_GPL(videobuf_read_start);
1410 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1411 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1412 EXPORT_SYMBOL_GPL(videobuf_read_one);
1413 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1415 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1416 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1417 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);