Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / media / video / video-buf.c
blobacc5ea936687a02ed7b52b049b2c894a828daa74
1 /*
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>
33 #include <asm/page.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(); }
43 static int debug = 0;
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)
53 struct scatterlist*
54 videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
56 struct scatterlist *sglist;
57 struct page *pg;
58 int i;
60 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
61 if (NULL == sglist)
62 return NULL;
63 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
64 pg = vmalloc_to_page(virt);
65 if (NULL == pg)
66 goto err;
67 BUG_ON(PageHighMem(pg));
68 sglist[i].page = pg;
69 sglist[i].length = PAGE_SIZE;
71 return sglist;
73 err:
74 kfree(sglist);
75 return NULL;
78 struct scatterlist*
79 videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
81 struct scatterlist *sglist;
82 int i = 0;
84 if (NULL == pages[0])
85 return NULL;
86 sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
87 if (NULL == sglist)
88 return NULL;
90 if (NULL == pages[0])
91 goto nopage;
92 if (PageHighMem(pages[0]))
93 /* DMA to highmem pages might not work */
94 goto highmem;
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++) {
99 if (NULL == pages[i])
100 goto nopage;
101 if (PageHighMem(pages[i]))
102 goto highmem;
103 sglist[i].page = pages[i];
104 sglist[i].length = PAGE_SIZE;
106 return sglist;
108 nopage:
109 dprintk(2,"sgl: oops - no page\n");
110 kfree(sglist);
111 return NULL;
113 highmem:
114 dprintk(2,"sgl: oops - highmem page\n");
115 kfree(sglist);
116 return NULL;
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;
131 int err, rw = 0;
133 dma->direction = direction;
134 switch (dma->direction) {
135 case PCI_DMA_FROMDEVICE: rw = READ; break;
136 case PCI_DMA_TODEVICE: rw = WRITE; break;
137 default: BUG();
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*),
145 GFP_KERNEL);
146 if (NULL == dma->pages)
147 return -ENOMEM;
148 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
149 data,size,dma->nr_pages);
151 down_read(&current->mm->mmap_sem);
152 err = get_user_pages(current,current->mm,
153 data & PAGE_MASK, dma->nr_pages,
154 rw == READ, 1, /* force */
155 dma->pages, NULL);
156 up_read(&current->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;
162 return 0;
165 int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
166 int nr_pages)
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);
173 return -ENOMEM;
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;
180 return 0;
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;
189 if (0 == addr)
190 return -EINVAL;
192 dma->bus_addr = addr;
193 dma->nr_pages = nr_pages;
194 return 0;
197 int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
199 void *dev=q->dev;
201 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
202 BUG_ON(0 == dma->nr_pages);
204 if (dma->pages) {
205 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
206 dma->offset);
208 if (dma->vmalloc) {
209 dma->sglist = videobuf_vmalloc_to_sg
210 (dma->vmalloc,dma->nr_pages);
212 if (dma->bus_addr) {
213 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
214 if (NULL != dma->sglist) {
215 dma->sglen = 1;
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");
223 return -ENOMEM;
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) {
231 printk(KERN_WARNING
232 "%s: videobuf_map_sg failed\n",__FUNCTION__);
233 kfree(dma->sglist);
234 dma->sglist = NULL;
235 dma->sglen = 0;
236 return -EIO;
239 return 0;
242 int videobuf_dma_sync(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
244 void *dev=q->dev;
246 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
247 BUG_ON(!dma->sglen);
249 if (!dma->bus_addr && q->ops->vb_dma_sync_sg)
250 q->ops->vb_dma_sync_sg(dev,dma->sglist,dma->nr_pages,
251 dma->direction);
253 return 0;
256 int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
258 void *dev=q->dev;
260 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
261 if (!dma->sglen)
262 return 0;
264 if (!dma->bus_addr && q->ops->vb_unmap_sg)
265 q->ops->vb_unmap_sg(dev,dma->sglist,dma->nr_pages,
266 dma->direction);
267 kfree(dma->sglist);
268 dma->sglist = NULL;
269 dma->sglen = 0;
270 return 0;
273 int videobuf_dma_free(struct videobuf_dmabuf *dma)
275 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
276 BUG_ON(dma->sglen);
278 if (dma->pages) {
279 int i;
280 for (i=0; i < dma->nr_pages; i++)
281 page_cache_release(dma->pages[i]);
282 kfree(dma->pages);
283 dma->pages = NULL;
286 vfree(dma->vmalloc);
287 dma->vmalloc = NULL;
289 if (dma->bus_addr) {
290 dma->bus_addr = 0;
292 dma->direction = PCI_DMA_NONE;
293 return 0;
296 /* --------------------------------------------------------------------- */
298 void* videobuf_alloc(unsigned int size)
300 struct videobuf_buffer *vb;
302 vb = kzalloc(size,GFP_KERNEL);
303 if (NULL != vb) {
304 videobuf_dma_init(&vb->dma);
305 init_waitqueue_head(&vb->done);
306 vb->magic = MAGIC_BUFFER;
308 return vb;
311 int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
313 int retval = 0;
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) {
319 if (non_blocking) {
320 retval = -EAGAIN;
321 break;
323 set_current_state(intr ? TASK_INTERRUPTIBLE
324 : TASK_UNINTERRUPTIBLE);
325 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
326 schedule();
327 set_current_state(TASK_RUNNING);
328 if (intr && signal_pending(current)) {
329 dprintk(1,"buffer waiton: -EINTR\n");
330 retval = -EINTR;
331 break;
334 remove_wait_queue(&vb->done, &wait);
335 return retval;
339 videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
340 struct v4l2_framebuffer *fbuf)
342 int err,pages;
343 dma_addr_t bus;
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,
353 pages);
354 if (0 != err)
355 return err;
356 } else {
357 /* dma directly to userspace */
358 err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE,
359 vb->baddr,vb->bsize);
360 if (0 != err)
361 return err;
363 break;
364 case V4L2_MEMORY_OVERLAY:
365 if (NULL == fbuf)
366 return -EINVAL;
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,
371 bus, pages);
372 if (0 != err)
373 return err;
374 break;
375 default:
376 BUG();
378 err = videobuf_dma_map(q,&vb->dma);
379 if (0 != err)
380 return err;
382 return 0;
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;
402 struct videobuf_queue_ops qops;
404 q.dev=pci;
405 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
406 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
407 q.ops = &qops;
409 return (videobuf_dma_map(&q,dma));
412 int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma)
414 struct videobuf_queue q;
415 struct videobuf_queue_ops qops;
417 q.dev=pci;
418 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
419 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
420 q.ops = &qops;
422 return (videobuf_dma_unmap(&q,dma));
425 void videobuf_queue_init(struct videobuf_queue* q,
426 struct videobuf_queue_ops *ops,
427 void *dev,
428 spinlock_t *irqlock,
429 enum v4l2_buf_type type,
430 enum v4l2_field field,
431 unsigned int msize,
432 void *priv)
434 memset(q,0,sizeof(*q));
435 q->irqlock = irqlock;
436 q->dev = dev;
437 q->type = type;
438 q->field = field;
439 q->msize = msize;
440 q->ops = ops;
441 q->priv_data = priv;
443 videobuf_queue_pci(q);
445 mutex_init(&q->lock);
446 INIT_LIST_HEAD(&q->stream);
450 videobuf_queue_is_busy(struct videobuf_queue *q)
452 int i;
454 if (q->streaming) {
455 dprintk(1,"busy: streaming active\n");
456 return 1;
458 if (q->reading) {
459 dprintk(1,"busy: pending read #1\n");
460 return 1;
462 if (q->read_buf) {
463 dprintk(1,"busy: pending read #2\n");
464 return 1;
466 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
467 if (NULL == q->bufs[i])
468 continue;
469 if (q->bufs[i]->map) {
470 dprintk(1,"busy: buffer #%d mapped\n",i);
471 return 1;
473 if (q->bufs[i]->state == STATE_QUEUED) {
474 dprintk(1,"busy: buffer #%d queued\n",i);
475 return 1;
477 if (q->bufs[i]->state == STATE_ACTIVE) {
478 dprintk(1,"busy: buffer #%d avtive\n",i);
479 return 1;
482 return 0;
485 void
486 videobuf_queue_cancel(struct videobuf_queue *q)
488 unsigned long flags=0;
489 int i;
491 /* remove queued buffers from list */
492 if (q->irqlock)
493 spin_lock_irqsave(q->irqlock,flags);
494 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
495 if (NULL == q->bufs[i])
496 continue;
497 if (q->bufs[i]->state == STATE_QUEUED) {
498 list_del(&q->bufs[i]->queue);
499 q->bufs[i]->state = STATE_ERROR;
502 if (q->irqlock)
503 spin_unlock_irqrestore(q->irqlock,flags);
505 /* free all buffers + clear queue */
506 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
507 if (NULL == q->bufs[i])
508 continue;
509 q->ops->buf_release(q,q->bufs[i]);
511 INIT_LIST_HEAD(&q->stream);
514 /* --------------------------------------------------------------------- */
516 enum v4l2_field
517 videobuf_next_field(struct videobuf_queue *q)
519 enum v4l2_field field = q->field;
521 BUG_ON(V4L2_FIELD_ANY == field);
523 if (V4L2_FIELD_ALTERNATE == field) {
524 if (V4L2_FIELD_TOP == q->last) {
525 field = V4L2_FIELD_BOTTOM;
526 q->last = V4L2_FIELD_BOTTOM;
527 } else {
528 field = V4L2_FIELD_TOP;
529 q->last = V4L2_FIELD_TOP;
532 return field;
535 void
536 videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
537 enum v4l2_buf_type type)
539 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
541 b->index = vb->i;
542 b->type = type;
544 b->memory = vb->memory;
545 switch (b->memory) {
546 case V4L2_MEMORY_MMAP:
547 b->m.offset = vb->boff;
548 b->length = vb->bsize;
549 break;
550 case V4L2_MEMORY_USERPTR:
551 b->m.userptr = vb->baddr;
552 b->length = vb->bsize;
553 break;
554 case V4L2_MEMORY_OVERLAY:
555 b->m.offset = vb->boff;
556 break;
559 b->flags = 0;
560 if (vb->map)
561 b->flags |= V4L2_BUF_FLAG_MAPPED;
563 switch (vb->state) {
564 case STATE_PREPARED:
565 case STATE_QUEUED:
566 case STATE_ACTIVE:
567 b->flags |= V4L2_BUF_FLAG_QUEUED;
568 break;
569 case STATE_DONE:
570 case STATE_ERROR:
571 b->flags |= V4L2_BUF_FLAG_DONE;
572 break;
573 case STATE_NEEDS_INIT:
574 case STATE_IDLE:
575 /* nothing */
576 break;
579 if (vb->input != UNSET) {
580 b->flags |= V4L2_BUF_FLAG_INPUT;
581 b->input = vb->input;
584 b->field = vb->field;
585 b->timestamp = vb->ts;
586 b->bytesused = vb->size;
587 b->sequence = vb->field_count >> 1;
591 videobuf_reqbufs(struct videobuf_queue *q,
592 struct v4l2_requestbuffers *req)
594 unsigned int size,count;
595 int retval;
597 if (req->type != q->type) {
598 dprintk(1,"reqbufs: queue type invalid\n");
599 return -EINVAL;
601 if (req->count < 1) {
602 dprintk(1,"reqbufs: count invalid (%d)\n",req->count);
603 return -EINVAL;
605 if (req->memory != V4L2_MEMORY_MMAP &&
606 req->memory != V4L2_MEMORY_USERPTR &&
607 req->memory != V4L2_MEMORY_OVERLAY) {
608 dprintk(1,"reqbufs: memory type invalid\n");
609 return -EINVAL;
612 if (q->streaming) {
613 dprintk(1,"reqbufs: streaming already exists\n");
614 return -EBUSY;
616 if (!list_empty(&q->stream)) {
617 dprintk(1,"reqbufs: stream running\n");
618 return -EBUSY;
621 mutex_lock(&q->lock);
622 count = req->count;
623 if (count > VIDEO_MAX_FRAME)
624 count = VIDEO_MAX_FRAME;
625 size = 0;
626 q->ops->buf_setup(q,&count,&size);
627 size = PAGE_ALIGN(size);
628 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
629 count, size, (count*size)>>PAGE_SHIFT);
631 retval = videobuf_mmap_setup(q,count,size,req->memory);
632 if (retval < 0) {
633 dprintk(1,"reqbufs: mmap setup returned %d\n",retval);
634 goto done;
637 req->count = count;
639 done:
640 mutex_unlock(&q->lock);
641 return retval;
645 videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
647 if (unlikely(b->type != q->type)) {
648 dprintk(1,"querybuf: Wrong type.\n");
649 return -EINVAL;
651 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
652 dprintk(1,"querybuf: index out of range.\n");
653 return -EINVAL;
655 if (unlikely(NULL == q->bufs[b->index])) {
656 dprintk(1,"querybuf: buffer is null.\n");
657 return -EINVAL;
659 videobuf_status(b,q->bufs[b->index],q->type);
660 return 0;
664 videobuf_qbuf(struct videobuf_queue *q,
665 struct v4l2_buffer *b)
667 struct videobuf_buffer *buf;
668 enum v4l2_field field;
669 unsigned long flags=0;
670 int retval;
672 mutex_lock(&q->lock);
673 retval = -EBUSY;
674 if (q->reading) {
675 dprintk(1,"qbuf: Reading running...\n");
676 goto done;
678 retval = -EINVAL;
679 if (b->type != q->type) {
680 dprintk(1,"qbuf: Wrong type.\n");
681 goto done;
683 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
684 dprintk(1,"qbuf: index out of range.\n");
685 goto done;
687 buf = q->bufs[b->index];
688 if (NULL == buf) {
689 dprintk(1,"qbuf: buffer is null.\n");
690 goto done;
692 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
693 if (buf->memory != b->memory) {
694 dprintk(1,"qbuf: memory type is wrong.\n");
695 goto done;
697 if (buf->state == STATE_QUEUED ||
698 buf->state == STATE_ACTIVE) {
699 dprintk(1,"qbuf: buffer is already queued or active.\n");
700 goto done;
703 if (b->flags & V4L2_BUF_FLAG_INPUT) {
704 if (b->input >= q->inputs) {
705 dprintk(1,"qbuf: wrong input.\n");
706 goto done;
708 buf->input = b->input;
709 } else {
710 buf->input = UNSET;
713 switch (b->memory) {
714 case V4L2_MEMORY_MMAP:
715 if (0 == buf->baddr) {
716 dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n");
717 goto done;
719 break;
720 case V4L2_MEMORY_USERPTR:
721 if (b->length < buf->bsize) {
722 dprintk(1,"qbuf: buffer length is not enough\n");
723 goto done;
725 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
726 q->ops->buf_release(q,buf);
727 buf->baddr = b->m.userptr;
728 break;
729 case V4L2_MEMORY_OVERLAY:
730 buf->boff = b->m.offset;
731 break;
732 default:
733 dprintk(1,"qbuf: wrong memory type\n");
734 goto done;
737 dprintk(1,"qbuf: requesting next field\n");
738 field = videobuf_next_field(q);
739 retval = q->ops->buf_prepare(q,buf,field);
740 if (0 != retval) {
741 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval);
742 goto done;
745 list_add_tail(&buf->stream,&q->stream);
746 if (q->streaming) {
747 if (q->irqlock)
748 spin_lock_irqsave(q->irqlock,flags);
749 q->ops->buf_queue(q,buf);
750 if (q->irqlock)
751 spin_unlock_irqrestore(q->irqlock,flags);
753 dprintk(1,"qbuf: succeded\n");
754 retval = 0;
756 done:
757 mutex_unlock(&q->lock);
758 return retval;
762 videobuf_dqbuf(struct videobuf_queue *q,
763 struct v4l2_buffer *b, int nonblocking)
765 struct videobuf_buffer *buf;
766 int retval;
768 mutex_lock(&q->lock);
769 retval = -EBUSY;
770 if (q->reading) {
771 dprintk(1,"dqbuf: Reading running...\n");
772 goto done;
774 retval = -EINVAL;
775 if (b->type != q->type) {
776 dprintk(1,"dqbuf: Wrong type.\n");
777 goto done;
779 if (list_empty(&q->stream)) {
780 dprintk(1,"dqbuf: stream running\n");
781 goto done;
783 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
784 retval = videobuf_waiton(buf, nonblocking, 1);
785 if (retval < 0) {
786 dprintk(1,"dqbuf: waiton returned %d\n",retval);
787 goto done;
789 switch (buf->state) {
790 case STATE_ERROR:
791 dprintk(1,"dqbuf: state is error\n");
792 retval = -EIO;
793 videobuf_dma_sync(q,&buf->dma);
794 buf->state = STATE_IDLE;
795 break;
796 case STATE_DONE:
797 dprintk(1,"dqbuf: state is done\n");
798 videobuf_dma_sync(q,&buf->dma);
799 buf->state = STATE_IDLE;
800 break;
801 default:
802 dprintk(1,"dqbuf: state invalid\n");
803 retval = -EINVAL;
804 goto done;
806 list_del(&buf->stream);
807 memset(b,0,sizeof(*b));
808 videobuf_status(b,buf,q->type);
810 done:
811 mutex_unlock(&q->lock);
812 return retval;
815 int videobuf_streamon(struct videobuf_queue *q)
817 struct videobuf_buffer *buf;
818 struct list_head *list;
819 unsigned long flags=0;
820 int retval;
822 mutex_lock(&q->lock);
823 retval = -EBUSY;
824 if (q->reading)
825 goto done;
826 retval = 0;
827 if (q->streaming)
828 goto done;
829 q->streaming = 1;
830 if (q->irqlock)
831 spin_lock_irqsave(q->irqlock,flags);
832 list_for_each(list,&q->stream) {
833 buf = list_entry(list, struct videobuf_buffer, stream);
834 if (buf->state == STATE_PREPARED)
835 q->ops->buf_queue(q,buf);
837 if (q->irqlock)
838 spin_unlock_irqrestore(q->irqlock,flags);
840 done:
841 mutex_unlock(&q->lock);
842 return retval;
845 int videobuf_streamoff(struct videobuf_queue *q)
847 int retval = -EINVAL;
849 mutex_lock(&q->lock);
850 if (!q->streaming)
851 goto done;
852 videobuf_queue_cancel(q);
853 q->streaming = 0;
854 retval = 0;
856 done:
857 mutex_unlock(&q->lock);
858 return retval;
861 static ssize_t
862 videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
863 size_t count, loff_t *ppos)
865 enum v4l2_field field;
866 unsigned long flags=0;
867 int retval;
869 /* setup stuff */
870 q->read_buf = videobuf_alloc(q->msize);
871 if (NULL == q->read_buf)
872 return -ENOMEM;
874 q->read_buf->memory = V4L2_MEMORY_USERPTR;
875 q->read_buf->baddr = (unsigned long)data;
876 q->read_buf->bsize = count;
877 field = videobuf_next_field(q);
878 retval = q->ops->buf_prepare(q,q->read_buf,field);
879 if (0 != retval)
880 goto done;
882 /* start capture & wait */
883 if (q->irqlock)
884 spin_lock_irqsave(q->irqlock,flags);
885 q->ops->buf_queue(q,q->read_buf);
886 if (q->irqlock)
887 spin_unlock_irqrestore(q->irqlock,flags);
888 retval = videobuf_waiton(q->read_buf,0,0);
889 if (0 == retval) {
890 videobuf_dma_sync(q,&q->read_buf->dma);
891 if (STATE_ERROR == q->read_buf->state)
892 retval = -EIO;
893 else
894 retval = q->read_buf->size;
897 done:
898 /* cleanup */
899 q->ops->buf_release(q,q->read_buf);
900 kfree(q->read_buf);
901 q->read_buf = NULL;
902 return retval;
905 ssize_t videobuf_read_one(struct videobuf_queue *q,
906 char __user *data, size_t count, loff_t *ppos,
907 int nonblocking)
909 enum v4l2_field field;
910 unsigned long flags=0;
911 unsigned size, nbufs, bytes;
912 int retval;
914 mutex_lock(&q->lock);
916 nbufs = 1; size = 0;
917 q->ops->buf_setup(q,&nbufs,&size);
918 if (NULL == q->read_buf &&
919 count >= size &&
920 !nonblocking) {
921 retval = videobuf_read_zerocopy(q,data,count,ppos);
922 if (retval >= 0 || retval == -EIO)
923 /* ok, all done */
924 goto done;
925 /* fallback to kernel bounce buffer on failures */
928 if (NULL == q->read_buf) {
929 /* need to capture a new frame */
930 retval = -ENOMEM;
931 q->read_buf = videobuf_alloc(q->msize);
932 dprintk(1,"video alloc=0x%p\n", q->read_buf);
933 if (NULL == q->read_buf)
934 goto done;
935 q->read_buf->memory = V4L2_MEMORY_USERPTR;
936 q->read_buf->bsize = count; /* preferred size */
937 field = videobuf_next_field(q);
938 retval = q->ops->buf_prepare(q,q->read_buf,field);
939 if (0 != retval) {
940 kfree (q->read_buf);
941 q->read_buf = NULL;
942 goto done;
944 if (q->irqlock)
945 spin_lock_irqsave(q->irqlock,flags);
946 q->ops->buf_queue(q,q->read_buf);
947 if (q->irqlock)
948 spin_unlock_irqrestore(q->irqlock,flags);
949 q->read_off = 0;
952 /* wait until capture is done */
953 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
954 if (0 != retval)
955 goto done;
956 videobuf_dma_sync(q,&q->read_buf->dma);
958 if (STATE_ERROR == q->read_buf->state) {
959 /* catch I/O errors */
960 q->ops->buf_release(q,q->read_buf);
961 kfree(q->read_buf);
962 q->read_buf = NULL;
963 retval = -EIO;
964 goto done;
967 /* copy to userspace */
968 bytes = count;
969 if (bytes > q->read_buf->size - q->read_off)
970 bytes = q->read_buf->size - q->read_off;
971 retval = -EFAULT;
972 if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
973 goto done;
975 retval = bytes;
976 q->read_off += bytes;
977 if (q->read_off == q->read_buf->size) {
978 /* all data copied, cleanup */
979 q->ops->buf_release(q,q->read_buf);
980 kfree(q->read_buf);
981 q->read_buf = NULL;
984 done:
985 mutex_unlock(&q->lock);
986 return retval;
989 int videobuf_read_start(struct videobuf_queue *q)
991 enum v4l2_field field;
992 unsigned long flags=0;
993 int count = 0, size = 0;
994 int err, i;
996 q->ops->buf_setup(q,&count,&size);
997 if (count < 2)
998 count = 2;
999 if (count > VIDEO_MAX_FRAME)
1000 count = VIDEO_MAX_FRAME;
1001 size = PAGE_ALIGN(size);
1003 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
1004 if (err)
1005 return err;
1006 for (i = 0; i < count; i++) {
1007 field = videobuf_next_field(q);
1008 err = q->ops->buf_prepare(q,q->bufs[i],field);
1009 if (err)
1010 return err;
1011 list_add_tail(&q->bufs[i]->stream, &q->stream);
1013 if (q->irqlock)
1014 spin_lock_irqsave(q->irqlock,flags);
1015 for (i = 0; i < count; i++)
1016 q->ops->buf_queue(q,q->bufs[i]);
1017 if (q->irqlock)
1018 spin_unlock_irqrestore(q->irqlock,flags);
1019 q->reading = 1;
1020 return 0;
1023 void videobuf_read_stop(struct videobuf_queue *q)
1025 int i;
1027 videobuf_queue_cancel(q);
1028 videobuf_mmap_free(q);
1029 INIT_LIST_HEAD(&q->stream);
1030 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1031 if (NULL == q->bufs[i])
1032 continue;
1033 kfree(q->bufs[i]);
1034 q->bufs[i] = NULL;
1036 q->read_buf = NULL;
1037 q->reading = 0;
1040 ssize_t videobuf_read_stream(struct videobuf_queue *q,
1041 char __user *data, size_t count, loff_t *ppos,
1042 int vbihack, int nonblocking)
1044 unsigned int *fc, bytes;
1045 int err, retval;
1046 unsigned long flags=0;
1048 dprintk(2,"%s\n",__FUNCTION__);
1049 mutex_lock(&q->lock);
1050 retval = -EBUSY;
1051 if (q->streaming)
1052 goto done;
1053 if (!q->reading) {
1054 retval = videobuf_read_start(q);
1055 if (retval < 0)
1056 goto done;
1059 retval = 0;
1060 while (count > 0) {
1061 /* get / wait for data */
1062 if (NULL == q->read_buf) {
1063 q->read_buf = list_entry(q->stream.next,
1064 struct videobuf_buffer,
1065 stream);
1066 list_del(&q->read_buf->stream);
1067 q->read_off = 0;
1069 err = videobuf_waiton(q->read_buf, nonblocking, 1);
1070 if (err < 0) {
1071 if (0 == retval)
1072 retval = err;
1073 break;
1076 if (q->read_buf->state == STATE_DONE) {
1077 if (vbihack) {
1078 /* dirty, undocumented hack -- pass the frame counter
1079 * within the last four bytes of each vbi data block.
1080 * We need that one to maintain backward compatibility
1081 * to all vbi decoding software out there ... */
1082 fc = (unsigned int*)q->read_buf->dma.vmalloc;
1083 fc += (q->read_buf->size>>2) -1;
1084 *fc = q->read_buf->field_count >> 1;
1085 dprintk(1,"vbihack: %d\n",*fc);
1088 /* copy stuff */
1089 bytes = count;
1090 if (bytes > q->read_buf->size - q->read_off)
1091 bytes = q->read_buf->size - q->read_off;
1092 if (copy_to_user(data + retval,
1093 q->read_buf->dma.vmalloc + q->read_off,
1094 bytes)) {
1095 if (0 == retval)
1096 retval = -EFAULT;
1097 break;
1099 count -= bytes;
1100 retval += bytes;
1101 q->read_off += bytes;
1102 } else {
1103 /* some error */
1104 q->read_off = q->read_buf->size;
1105 if (0 == retval)
1106 retval = -EIO;
1109 /* requeue buffer when done with copying */
1110 if (q->read_off == q->read_buf->size) {
1111 list_add_tail(&q->read_buf->stream,
1112 &q->stream);
1113 if (q->irqlock)
1114 spin_lock_irqsave(q->irqlock,flags);
1115 q->ops->buf_queue(q,q->read_buf);
1116 if (q->irqlock)
1117 spin_unlock_irqrestore(q->irqlock,flags);
1118 q->read_buf = NULL;
1120 if (retval < 0)
1121 break;
1124 done:
1125 mutex_unlock(&q->lock);
1126 return retval;
1129 unsigned int videobuf_poll_stream(struct file *file,
1130 struct videobuf_queue *q,
1131 poll_table *wait)
1133 struct videobuf_buffer *buf = NULL;
1134 unsigned int rc = 0;
1136 mutex_lock(&q->lock);
1137 if (q->streaming) {
1138 if (!list_empty(&q->stream))
1139 buf = list_entry(q->stream.next,
1140 struct videobuf_buffer, stream);
1141 } else {
1142 if (!q->reading)
1143 videobuf_read_start(q);
1144 if (!q->reading) {
1145 rc = POLLERR;
1146 } else if (NULL == q->read_buf) {
1147 q->read_buf = list_entry(q->stream.next,
1148 struct videobuf_buffer,
1149 stream);
1150 list_del(&q->read_buf->stream);
1151 q->read_off = 0;
1153 buf = q->read_buf;
1155 if (!buf)
1156 rc = POLLERR;
1158 if (0 == rc) {
1159 poll_wait(file, &buf->done, wait);
1160 if (buf->state == STATE_DONE ||
1161 buf->state == STATE_ERROR)
1162 rc = POLLIN|POLLRDNORM;
1164 mutex_unlock(&q->lock);
1165 return rc;
1168 /* --------------------------------------------------------------------- */
1170 static void
1171 videobuf_vm_open(struct vm_area_struct *vma)
1173 struct videobuf_mapping *map = vma->vm_private_data;
1175 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1176 map->count,vma->vm_start,vma->vm_end);
1177 map->count++;
1180 static void
1181 videobuf_vm_close(struct vm_area_struct *vma)
1183 struct videobuf_mapping *map = vma->vm_private_data;
1184 struct videobuf_queue *q = map->q;
1185 int i;
1187 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1188 map->count,vma->vm_start,vma->vm_end);
1190 map->count--;
1191 if (0 == map->count) {
1192 dprintk(1,"munmap %p q=%p\n",map,q);
1193 mutex_lock(&q->lock);
1194 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1195 if (NULL == q->bufs[i])
1196 continue;
1197 if (q->bufs[i])
1199 if (q->bufs[i]->map != map)
1200 continue;
1201 q->bufs[i]->map = NULL;
1202 q->bufs[i]->baddr = 0;
1203 q->ops->buf_release(q,q->bufs[i]);
1205 mutex_unlock(&q->lock);
1206 kfree(map);
1208 return;
1212 * Get a anonymous page for the mapping. Make sure we can DMA to that
1213 * memory location with 32bit PCI devices (i.e. don't use highmem for
1214 * now ...). Bounce buffers don't work very well for the data rates
1215 * video capture has.
1217 static struct page*
1218 videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1219 int *type)
1221 struct page *page;
1223 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1224 vaddr,vma->vm_start,vma->vm_end);
1225 if (vaddr > vma->vm_end)
1226 return NOPAGE_SIGBUS;
1227 page = alloc_page(GFP_USER);
1228 if (!page)
1229 return NOPAGE_OOM;
1230 clear_user_page(page_address(page), vaddr, page);
1231 if (type)
1232 *type = VM_FAULT_MINOR;
1233 return page;
1236 static struct vm_operations_struct videobuf_vm_ops =
1238 .open = videobuf_vm_open,
1239 .close = videobuf_vm_close,
1240 .nopage = videobuf_vm_nopage,
1243 int videobuf_mmap_setup(struct videobuf_queue *q,
1244 unsigned int bcount, unsigned int bsize,
1245 enum v4l2_memory memory)
1247 unsigned int i;
1248 int err;
1250 err = videobuf_mmap_free(q);
1251 if (0 != err)
1252 return err;
1254 for (i = 0; i < bcount; i++) {
1255 q->bufs[i] = videobuf_alloc(q->msize);
1256 q->bufs[i]->i = i;
1257 q->bufs[i]->input = UNSET;
1258 q->bufs[i]->memory = memory;
1259 q->bufs[i]->bsize = bsize;
1260 switch (memory) {
1261 case V4L2_MEMORY_MMAP:
1262 q->bufs[i]->boff = bsize * i;
1263 break;
1264 case V4L2_MEMORY_USERPTR:
1265 case V4L2_MEMORY_OVERLAY:
1266 /* nothing */
1267 break;
1270 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1271 bcount,bsize);
1272 return 0;
1275 int videobuf_mmap_free(struct videobuf_queue *q)
1277 int i;
1279 for (i = 0; i < VIDEO_MAX_FRAME; i++)
1280 if (q->bufs[i] && q->bufs[i]->map)
1281 return -EBUSY;
1282 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1283 if (NULL == q->bufs[i])
1284 continue;
1285 q->ops->buf_release(q,q->bufs[i]);
1286 kfree(q->bufs[i]);
1287 q->bufs[i] = NULL;
1289 return 0;
1292 int videobuf_mmap_mapper(struct videobuf_queue *q,
1293 struct vm_area_struct *vma)
1295 struct videobuf_mapping *map;
1296 unsigned int first,last,size,i;
1297 int retval;
1299 mutex_lock(&q->lock);
1300 retval = -EINVAL;
1301 if (!(vma->vm_flags & VM_WRITE)) {
1302 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1303 goto done;
1305 if (!(vma->vm_flags & VM_SHARED)) {
1306 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1307 goto done;
1310 /* look for first buffer to map */
1311 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1312 if (NULL == q->bufs[first])
1313 continue;
1314 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1315 continue;
1316 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1317 break;
1319 if (VIDEO_MAX_FRAME == first) {
1320 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1321 (vma->vm_pgoff << PAGE_SHIFT));
1322 goto done;
1325 /* look for last buffer to map */
1326 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1327 if (NULL == q->bufs[last])
1328 continue;
1329 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1330 continue;
1331 if (q->bufs[last]->map) {
1332 retval = -EBUSY;
1333 goto done;
1335 size += q->bufs[last]->bsize;
1336 if (size == (vma->vm_end - vma->vm_start))
1337 break;
1339 if (VIDEO_MAX_FRAME == last) {
1340 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1341 (vma->vm_end - vma->vm_start));
1342 goto done;
1345 /* create mapping + update buffer list */
1346 retval = -ENOMEM;
1347 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1348 if (NULL == map)
1349 goto done;
1350 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1351 q->bufs[i]->map = map;
1352 q->bufs[i]->baddr = vma->vm_start + size;
1354 map->count = 1;
1355 map->start = vma->vm_start;
1356 map->end = vma->vm_end;
1357 map->q = q;
1358 vma->vm_ops = &videobuf_vm_ops;
1359 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1360 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1361 vma->vm_private_data = map;
1362 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1363 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1364 retval = 0;
1366 done:
1367 mutex_unlock(&q->lock);
1368 return retval;
1371 /* --------------------------------------------------------------------- */
1373 EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1375 EXPORT_SYMBOL_GPL(videobuf_dma_init);
1376 EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1377 EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1378 EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1379 EXPORT_SYMBOL_GPL(videobuf_dma_map);
1380 EXPORT_SYMBOL_GPL(videobuf_dma_sync);
1381 EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
1382 EXPORT_SYMBOL_GPL(videobuf_dma_free);
1384 EXPORT_SYMBOL_GPL(videobuf_pci_dma_map);
1385 EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap);
1387 EXPORT_SYMBOL_GPL(videobuf_alloc);
1388 EXPORT_SYMBOL_GPL(videobuf_waiton);
1389 EXPORT_SYMBOL_GPL(videobuf_iolock);
1391 EXPORT_SYMBOL_GPL(videobuf_queue_init);
1392 EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1393 EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1395 EXPORT_SYMBOL_GPL(videobuf_next_field);
1396 EXPORT_SYMBOL_GPL(videobuf_status);
1397 EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1398 EXPORT_SYMBOL_GPL(videobuf_querybuf);
1399 EXPORT_SYMBOL_GPL(videobuf_qbuf);
1400 EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1401 EXPORT_SYMBOL_GPL(videobuf_streamon);
1402 EXPORT_SYMBOL_GPL(videobuf_streamoff);
1404 EXPORT_SYMBOL_GPL(videobuf_read_start);
1405 EXPORT_SYMBOL_GPL(videobuf_read_stop);
1406 EXPORT_SYMBOL_GPL(videobuf_read_stream);
1407 EXPORT_SYMBOL_GPL(videobuf_read_one);
1408 EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1410 EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1411 EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1412 EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1415 * Local variables:
1416 * c-basic-offset: 8
1417 * End: