aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/video-buf.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/media/video/video-buf.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/media/video/video-buf.c')
-rw-r--r--drivers/media/video/video-buf.c1290
1 files changed, 1290 insertions, 0 deletions
diff --git a/drivers/media/video/video-buf.c b/drivers/media/video/video-buf.c
new file mode 100644
index 000000000000..5afdc7852610
--- /dev/null
+++ b/drivers/media/video/video-buf.c
@@ -0,0 +1,1290 @@
1/*
2 * $Id: video-buf.c,v 1.18 2005/02/24 13:32:30 kraxel Exp $
3 *
4 * generic helper functions for video4linux capture buffers, to handle
5 * memory management and PCI DMA. Right now bttv + saa7134 use it.
6 *
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).
12 *
13 * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
14 *
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.
19 */
20
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>
31
32#include <media/video-buf.h>
33
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(); }
38
39static int debug = 0;
40module_param(debug, int, 0644);
41
42MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers");
43MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
44MODULE_LICENSE("GPL");
45
46#define dprintk(level, fmt, arg...) if (debug >= level) \
47 printk(KERN_DEBUG "vbuf: " fmt , ## arg)
48
49struct scatterlist*
50videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
51{
52 struct scatterlist *sglist;
53 struct page *pg;
54 int i;
55
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;
68 }
69 return sglist;
70
71 err:
72 kfree(sglist);
73 return NULL;
74}
75
76struct scatterlist*
77videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
78{
79 struct scatterlist *sglist;
80 int i = 0;
81
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);
88
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;
104 }
105 return sglist;
106
107 nopage:
108 dprintk(2,"sgl: oops - no page\n");
109 kfree(sglist);
110 return NULL;
111
112 highmem:
113 dprintk(2,"sgl: oops - highmem page\n");
114 kfree(sglist);
115 return NULL;
116}
117
118/* --------------------------------------------------------------------- */
119
120void videobuf_dma_init(struct videobuf_dmabuf *dma)
121{
122 memset(dma,0,sizeof(*dma));
123 dma->magic = MAGIC_DMABUF;
124}
125
126int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
127 unsigned long data, unsigned long size)
128{
129 unsigned long first,last;
130 int err, rw = 0;
131
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();
137 }
138
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);
149
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;
160 }
161 return 0;
162}
163
164int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
165 int nr_pages)
166{
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;
173 }
174 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
175 dma->nr_pages = nr_pages;
176 return 0;
177}
178
179int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
180 dma_addr_t addr, int nr_pages)
181{
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;
187
188 dma->bus_addr = addr;
189 dma->nr_pages = nr_pages;
190 return 0;
191}
192
193int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma)
194{
195 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
196 BUG_ON(0 == dma->nr_pages);
197
198 if (dma->pages) {
199 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
200 dma->offset);
201 }
202 if (dma->vmalloc) {
203 dma->sglist = videobuf_vmalloc_to_sg
204 (dma->vmalloc,dma->nr_pages);
205 }
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;
213 }
214 }
215 if (NULL == dma->sglist) {
216 dprintk(1,"scatterlist is NULL\n");
217 return -ENOMEM;
218 }
219
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;
230 }
231 }
232 return 0;
233}
234
235int videobuf_dma_pci_sync(struct pci_dev *dev, struct videobuf_dmabuf *dma)
236{
237 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
238 BUG_ON(!dma->sglen);
239
240 if (!dma->bus_addr)
241 pci_dma_sync_sg_for_cpu(dev,dma->sglist,dma->nr_pages,dma->direction);
242 return 0;
243}
244
245int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma)
246{
247 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
248 if (!dma->sglen)
249 return 0;
250
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;
257}
258
259int videobuf_dma_free(struct videobuf_dmabuf *dma)
260{
261 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
262 BUG_ON(dma->sglen);
263
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;
270 }
271 if (dma->vmalloc) {
272 vfree(dma->vmalloc);
273 dma->vmalloc = NULL;
274 }
275 if (dma->bus_addr) {
276 dma->bus_addr = 0;
277 }
278 dma->direction = PCI_DMA_NONE;
279 return 0;
280}
281
282/* --------------------------------------------------------------------- */
283
284void* videobuf_alloc(unsigned int size)
285{
286 struct videobuf_buffer *vb;
287
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;
294 }
295 return vb;
296}
297
298int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
299{
300 int retval = 0;
301 DECLARE_WAITQUEUE(wait, current);
302
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;
309 }
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;
319 }
320 }
321 remove_wait_queue(&vb->done, &wait);
322 return retval;
323}
324
325int
326videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
327 struct v4l2_framebuffer *fbuf)
328{
329 int err,pages;
330 dma_addr_t bus;
331
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;
349 }
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();
364 }
365 err = videobuf_dma_pci_map(pci,&vb->dma);
366 if (0 != err)
367 return err;
368
369 return 0;
370}
371
372/* --------------------------------------------------------------------- */
373
374void 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)
382{
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;
391
392 init_MUTEX(&q->lock);
393 INIT_LIST_HEAD(&q->stream);
394}
395
396int
397videobuf_queue_is_busy(struct videobuf_queue *q)
398{
399 int i;
400
401 if (q->streaming) {
402 dprintk(1,"busy: streaming active\n");
403 return 1;
404 }
405 if (q->reading) {
406 dprintk(1,"busy: pending read #1\n");
407 return 1;
408 }
409 if (q->read_buf) {
410 dprintk(1,"busy: pending read #2\n");
411 return 1;
412 }
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;
419 }
420 if (q->bufs[i]->state == STATE_QUEUED) {
421 dprintk(1,"busy: buffer #%d queued\n",i);
422 return 1;
423 }
424 if (q->bufs[i]->state == STATE_ACTIVE) {
425 dprintk(1,"busy: buffer #%d avtive\n",i);
426 return 1;
427 }
428 }
429 return 0;
430}
431
432void
433videobuf_queue_cancel(struct videobuf_queue *q)
434{
435 unsigned long flags;
436 int i;
437
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;
446 }
447 }
448 spin_unlock_irqrestore(q->irqlock,flags);
449
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]);
455 }
456 INIT_LIST_HEAD(&q->stream);
457}
458
459/* --------------------------------------------------------------------- */
460
461enum v4l2_field
462videobuf_next_field(struct videobuf_queue *q)
463{
464 enum v4l2_field field = q->field;
465
466 BUG_ON(V4L2_FIELD_ANY == field);
467
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;
475 }
476 }
477 return field;
478}
479
480void
481videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
482 enum v4l2_buf_type type)
483{
484 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
485
486 b->index = vb->i;
487 b->type = type;
488
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;
502 }
503
504 b->flags = 0;
505 if (vb->map)
506 b->flags |= V4L2_BUF_FLAG_MAPPED;
507
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;
522 }
523
524 if (vb->input != UNSET) {
525 b->flags |= V4L2_BUF_FLAG_INPUT;
526 b->input = vb->input;
527 }
528
529 b->field = vb->field;
530 b->timestamp = vb->ts;
531 b->bytesused = vb->size;
532 b->sequence = vb->field_count >> 1;
533}
534
535int
536videobuf_reqbufs(struct videobuf_queue *q,
537 struct v4l2_requestbuffers *req)
538{
539 unsigned int size,count;
540 int retval;
541
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;
550
551 if (q->streaming)
552 return -EBUSY;
553 if (!list_empty(&q->stream))
554 return -EBUSY;
555
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);
565
566 retval = videobuf_mmap_setup(q,count,size,req->memory);
567 if (retval < 0)
568 goto done;
569
570 req->count = count;
571
572 done:
573 up(&q->lock);
574 return retval;
575}
576
577int
578videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
579{
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;
588}
589
590int
591videobuf_qbuf(struct videobuf_queue *q,
592 struct v4l2_buffer *b)
593{
594 struct videobuf_buffer *buf;
595 enum v4l2_field field;
596 unsigned long flags;
597 int retval;
598
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;
617
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;
624 }
625
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;
643 }
644
645 field = videobuf_next_field(q);
646 retval = q->ops->buf_prepare(q,buf,field);
647 if (0 != retval)
648 goto done;
649
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);
655 }
656 retval = 0;
657
658 done:
659 up(&q->lock);
660 return retval;
661}
662
663int
664videobuf_dqbuf(struct videobuf_queue *q,
665 struct v4l2_buffer *b, int nonblocking)
666{
667 struct videobuf_buffer *buf;
668 int retval;
669
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;
694 }
695 list_del(&buf->stream);
696 memset(b,0,sizeof(*b));
697 videobuf_status(b,buf,q->type);
698
699 done:
700 up(&q->lock);
701 return retval;
702}
703
704int videobuf_streamon(struct videobuf_queue *q)
705{
706 struct videobuf_buffer *buf;
707 struct list_head *list;
708 unsigned long flags;
709 int retval;
710
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);
724 }
725 spin_unlock_irqrestore(q->irqlock,flags);
726
727 done:
728 up(&q->lock);
729 return retval;
730}
731
732int videobuf_streamoff(struct videobuf_queue *q)
733{
734 int retval = -EINVAL;
735
736 down(&q->lock);
737 if (!q->streaming)
738 goto done;
739 videobuf_queue_cancel(q);
740 q->streaming = 0;
741 retval = 0;
742
743 done:
744 up(&q->lock);
745 return retval;
746}
747
748static ssize_t
749videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
750 size_t count, loff_t *ppos)
751{
752 enum v4l2_field field;
753 unsigned long flags;
754 int retval;
755
756 /* setup stuff */
757 retval = -ENOMEM;
758 q->read_buf = videobuf_alloc(q->msize);
759 if (NULL == q->read_buf)
760 goto done;
761
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;
769
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;
781 }
782
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;
789}
790
791ssize_t videobuf_read_one(struct videobuf_queue *q,
792 char __user *data, size_t count, loff_t *ppos,
793 int nonblocking)
794{
795 enum v4l2_field field;
796 unsigned long flags;
797 unsigned size, nbufs, bytes;
798 int retval;
799
800 down(&q->lock);
801
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 */
812 }
813
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;
829 }
830
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);
836
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;
844 }
845
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;
853
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;
861 }
862
863 done:
864 up(&q->lock);
865 return retval;
866}
867
868int videobuf_read_start(struct videobuf_queue *q)
869{
870 enum v4l2_field field;
871 unsigned long flags;
872 int count = 0, size = 0;
873 int err, i;
874
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);
881
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);
891 }
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;
898}
899
900void videobuf_read_stop(struct videobuf_queue *q)
901{
902 int i;
903
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;
912 }
913 q->read_buf = NULL;
914 q->reading = 0;
915}
916
917ssize_t videobuf_read_stream(struct videobuf_queue *q,
918 char __user *data, size_t count, loff_t *ppos,
919 int vbihack, int nonblocking)
920{
921 unsigned int *fc, bytes;
922 int err, retval;
923 unsigned long flags;
924
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;
934 }
935
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;
945 }
946 err = videobuf_waiton(q->read_buf, nonblocking, 1);
947 if (err < 0) {
948 if (0 == retval)
949 retval = err;
950 break;
951 }
952
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);
963 }
964
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;
975 }
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;
984 }
985
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;
994 }
995 if (retval < 0)
996 break;
997 }
998
999 done:
1000 up(&q->lock);
1001 return retval;
1002}
1003
1004unsigned int videobuf_poll_stream(struct file *file,
1005 struct videobuf_queue *q,
1006 poll_table *wait)
1007{
1008 struct videobuf_buffer *buf = NULL;
1009 unsigned int rc = 0;
1010
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;
1027 }
1028 buf = q->read_buf;
1029 }
1030 if (!buf)
1031 rc = POLLERR;
1032
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;
1038 }
1039 up(&q->lock);
1040 return rc;
1041}
1042
1043/* --------------------------------------------------------------------- */
1044
1045static void
1046videobuf_vm_open(struct vm_area_struct *vma)
1047{
1048 struct videobuf_mapping *map = vma->vm_private_data;
1049
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++;
1053}
1054
1055static void
1056videobuf_vm_close(struct vm_area_struct *vma)
1057{
1058 struct videobuf_mapping *map = vma->vm_private_data;
1059 struct videobuf_queue *q = map->q;
1060 int i;
1061
1062 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1063 map->count,vma->vm_start,vma->vm_end);
1064
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])
1073 ;
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]);
1079 }
1080 up(&q->lock);
1081 kfree(map);
1082 }
1083 return;
1084}
1085
1086/*
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.
1091 */
1092static struct page*
1093videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1094 int *type)
1095{
1096 struct page *page;
1097
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;
1109}
1110
1111static struct vm_operations_struct videobuf_vm_ops =
1112{
1113 .open = videobuf_vm_open,
1114 .close = videobuf_vm_close,
1115 .nopage = videobuf_vm_nopage,
1116};
1117
1118int videobuf_mmap_setup(struct videobuf_queue *q,
1119 unsigned int bcount, unsigned int bsize,
1120 enum v4l2_memory memory)
1121{
1122 unsigned int i;
1123 int err;
1124
1125 err = videobuf_mmap_free(q);
1126 if (0 != err)
1127 return err;
1128
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;
1143 }
1144 }
1145 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1146 bcount,bsize);
1147 return 0;
1148}
1149
1150int videobuf_mmap_free(struct videobuf_queue *q)
1151{
1152 int i;
1153
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;
1163 }
1164 return 0;
1165}
1166
1167int videobuf_mmap_mapper(struct videobuf_queue *q,
1168 struct vm_area_struct *vma)
1169{
1170 struct videobuf_mapping *map;
1171 unsigned int first,last,size,i;
1172 int retval;
1173
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;
1179 }
1180 if (!(vma->vm_flags & VM_SHARED)) {
1181 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1182 goto done;
1183 }
1184
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;
1193 }
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;
1198 }
1199
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;
1209 }
1210 size += q->bufs[last]->bsize;
1211 if (size == (vma->vm_end - vma->vm_start))
1212 break;
1213 }
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;
1218 }
1219
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;
1228 }
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;
1240
1241 done:
1242 up(&q->lock);
1243 return retval;
1244}
1245
1246/* --------------------------------------------------------------------- */
1247
1248EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1249
1250EXPORT_SYMBOL_GPL(videobuf_dma_init);
1251EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1252EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1253EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1254EXPORT_SYMBOL_GPL(videobuf_dma_pci_map);
1255EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync);
1256EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap);
1257EXPORT_SYMBOL_GPL(videobuf_dma_free);
1258
1259EXPORT_SYMBOL_GPL(videobuf_alloc);
1260EXPORT_SYMBOL_GPL(videobuf_waiton);
1261EXPORT_SYMBOL_GPL(videobuf_iolock);
1262
1263EXPORT_SYMBOL_GPL(videobuf_queue_init);
1264EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1265EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1266
1267EXPORT_SYMBOL_GPL(videobuf_next_field);
1268EXPORT_SYMBOL_GPL(videobuf_status);
1269EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1270EXPORT_SYMBOL_GPL(videobuf_querybuf);
1271EXPORT_SYMBOL_GPL(videobuf_qbuf);
1272EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1273EXPORT_SYMBOL_GPL(videobuf_streamon);
1274EXPORT_SYMBOL_GPL(videobuf_streamoff);
1275
1276EXPORT_SYMBOL_GPL(videobuf_read_start);
1277EXPORT_SYMBOL_GPL(videobuf_read_stop);
1278EXPORT_SYMBOL_GPL(videobuf_read_stream);
1279EXPORT_SYMBOL_GPL(videobuf_read_one);
1280EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1281
1282EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1283EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1284EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1285
1286/*
1287 * Local variables:
1288 * c-basic-offset: 8
1289 * End:
1290 */