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