blob: 9a6bf287e26ae629497aa0e9c3c206281e817a41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * generic helper functions for video4linux capture buffers, to handle
4 * memory management and PCI DMA. Right now bttv + saa7134 use it.
5 *
6 * The functions expect the hardware being able to scatter gatter
7 * (i.e. the buffers are not linear in physical memory, but fragmented
8 * into PAGE_SIZE chunks). They also assume the driver does not need
9 * to touch the video data (thus it is probably not useful for USB 1.1
10 * as data often must be uncompressed by the drivers).
11 *
12 * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/vmalloc.h>
24#include <linux/pagemap.h>
25#include <linux/slab.h>
26#include <linux/pci.h>
27#include <linux/interrupt.h>
28#include <asm/page.h>
29#include <asm/pgtable.h>
30
31#include <media/video-buf.h>
32
33#define MAGIC_DMABUF 0x19721112
34#define MAGIC_BUFFER 0x20040302
35#define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
36 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
37
38static int debug = 0;
39module_param(debug, int, 0644);
40
41MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers");
42MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
43MODULE_LICENSE("GPL");
44
45#define dprintk(level, fmt, arg...) if (debug >= level) \
46 printk(KERN_DEBUG "vbuf: " fmt , ## arg)
47
48struct scatterlist*
49videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
50{
51 struct scatterlist *sglist;
52 struct page *pg;
53 int i;
54
55 sglist = kmalloc(sizeof(struct scatterlist)*nr_pages, GFP_KERNEL);
56 if (NULL == sglist)
57 return NULL;
58 memset(sglist,0,sizeof(struct scatterlist)*nr_pages);
59 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
60 pg = vmalloc_to_page(virt);
61 if (NULL == pg)
62 goto err;
63 if (PageHighMem(pg))
64 BUG();
65 sglist[i].page = pg;
66 sglist[i].length = PAGE_SIZE;
67 }
68 return sglist;
69
70 err:
71 kfree(sglist);
72 return NULL;
73}
74
75struct scatterlist*
76videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
77{
78 struct scatterlist *sglist;
79 int i = 0;
80
81 if (NULL == pages[0])
82 return NULL;
83 sglist = kmalloc(sizeof(*sglist) * nr_pages, GFP_KERNEL);
84 if (NULL == sglist)
85 return NULL;
86 memset(sglist, 0, sizeof(*sglist) * nr_pages);
87
88 if (NULL == pages[0])
89 goto nopage;
90 if (PageHighMem(pages[0]))
91 /* DMA to highmem pages might not work */
92 goto highmem;
93 sglist[0].page = pages[0];
94 sglist[0].offset = offset;
95 sglist[0].length = PAGE_SIZE - offset;
96 for (i = 1; i < nr_pages; i++) {
97 if (NULL == pages[i])
98 goto nopage;
99 if (PageHighMem(pages[i]))
100 goto highmem;
101 sglist[i].page = pages[i];
102 sglist[i].length = PAGE_SIZE;
103 }
104 return sglist;
105
106 nopage:
107 dprintk(2,"sgl: oops - no page\n");
108 kfree(sglist);
109 return NULL;
110
111 highmem:
112 dprintk(2,"sgl: oops - highmem page\n");
113 kfree(sglist);
114 return NULL;
115}
116
117/* --------------------------------------------------------------------- */
118
119void videobuf_dma_init(struct videobuf_dmabuf *dma)
120{
121 memset(dma,0,sizeof(*dma));
122 dma->magic = MAGIC_DMABUF;
123}
124
125int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
126 unsigned long data, unsigned long size)
127{
128 unsigned long first,last;
129 int err, rw = 0;
130
131 dma->direction = direction;
132 switch (dma->direction) {
133 case PCI_DMA_FROMDEVICE: rw = READ; break;
134 case PCI_DMA_TODEVICE: rw = WRITE; break;
135 default: BUG();
136 }
137
138 first = (data & PAGE_MASK) >> PAGE_SHIFT;
139 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
140 dma->offset = data & ~PAGE_MASK;
141 dma->nr_pages = last-first+1;
142 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
143 GFP_KERNEL);
144 if (NULL == dma->pages)
145 return -ENOMEM;
146 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
147 data,size,dma->nr_pages);
148
149 down_read(&current->mm->mmap_sem);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800150 err = get_user_pages(current,current->mm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 data & PAGE_MASK, dma->nr_pages,
152 rw == READ, 1, /* force */
153 dma->pages, NULL);
154 up_read(&current->mm->mmap_sem);
155 if (err != dma->nr_pages) {
156 dma->nr_pages = (err >= 0) ? err : 0;
157 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
158 return err < 0 ? err : -EINVAL;
159 }
160 return 0;
161}
162
163int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
164 int nr_pages)
165{
166 dprintk(1,"init kernel [%d pages]\n",nr_pages);
167 dma->direction = direction;
168 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
169 if (NULL == dma->vmalloc) {
170 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
171 return -ENOMEM;
172 }
173 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
174 dma->nr_pages = nr_pages;
175 return 0;
176}
177
178int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
179 dma_addr_t addr, int nr_pages)
180{
181 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
182 nr_pages,(unsigned long)addr);
183 dma->direction = direction;
184 if (0 == addr)
185 return -EINVAL;
186
187 dma->bus_addr = addr;
188 dma->nr_pages = nr_pages;
189 return 0;
190}
191
192int videobuf_dma_pci_map(struct pci_dev *dev, struct videobuf_dmabuf *dma)
193{
194 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
195 BUG_ON(0 == dma->nr_pages);
196
197 if (dma->pages) {
198 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
199 dma->offset);
200 }
201 if (dma->vmalloc) {
202 dma->sglist = videobuf_vmalloc_to_sg
203 (dma->vmalloc,dma->nr_pages);
204 }
205 if (dma->bus_addr) {
206 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
207 if (NULL != dma->sglist) {
208 dma->sglen = 1;
209 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
210 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
211 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
212 }
213 }
214 if (NULL == dma->sglist) {
215 dprintk(1,"scatterlist is NULL\n");
216 return -ENOMEM;
217 }
218
219 if (!dma->bus_addr) {
220 dma->sglen = pci_map_sg(dev,dma->sglist,dma->nr_pages,
221 dma->direction);
222 if (0 == dma->sglen) {
223 printk(KERN_WARNING
224 "%s: pci_map_sg failed\n",__FUNCTION__);
225 kfree(dma->sglist);
226 dma->sglist = NULL;
227 dma->sglen = 0;
228 return -EIO;
229 }
230 }
231 return 0;
232}
233
234int videobuf_dma_pci_sync(struct pci_dev *dev, struct videobuf_dmabuf *dma)
235{
236 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
237 BUG_ON(!dma->sglen);
238
239 if (!dma->bus_addr)
240 pci_dma_sync_sg_for_cpu(dev,dma->sglist,dma->nr_pages,dma->direction);
241 return 0;
242}
243
244int videobuf_dma_pci_unmap(struct pci_dev *dev, struct videobuf_dmabuf *dma)
245{
246 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
247 if (!dma->sglen)
248 return 0;
249
250 if (!dma->bus_addr)
251 pci_unmap_sg(dev,dma->sglist,dma->nr_pages,dma->direction);
252 kfree(dma->sglist);
253 dma->sglist = NULL;
254 dma->sglen = 0;
255 return 0;
256}
257
258int videobuf_dma_free(struct videobuf_dmabuf *dma)
259{
260 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
261 BUG_ON(dma->sglen);
262
263 if (dma->pages) {
264 int i;
265 for (i=0; i < dma->nr_pages; i++)
266 page_cache_release(dma->pages[i]);
267 kfree(dma->pages);
268 dma->pages = NULL;
269 }
Jesper Juhlf91012102005-09-10 00:26:54 -0700270
271 vfree(dma->vmalloc);
272 dma->vmalloc = NULL;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (dma->bus_addr) {
275 dma->bus_addr = 0;
276 }
277 dma->direction = PCI_DMA_NONE;
278 return 0;
279}
280
281/* --------------------------------------------------------------------- */
282
283void* videobuf_alloc(unsigned int size)
284{
285 struct videobuf_buffer *vb;
286
287 vb = kmalloc(size,GFP_KERNEL);
288 if (NULL != vb) {
289 memset(vb,0,size);
290 videobuf_dma_init(&vb->dma);
291 init_waitqueue_head(&vb->done);
292 vb->magic = MAGIC_BUFFER;
293 }
294 return vb;
295}
296
297int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
298{
299 int retval = 0;
300 DECLARE_WAITQUEUE(wait, current);
301
302 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
303 add_wait_queue(&vb->done, &wait);
304 while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
305 if (non_blocking) {
306 retval = -EAGAIN;
307 break;
308 }
309 set_current_state(intr ? TASK_INTERRUPTIBLE
310 : TASK_UNINTERRUPTIBLE);
311 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
312 schedule();
313 set_current_state(TASK_RUNNING);
314 if (intr && signal_pending(current)) {
315 dprintk(1,"buffer waiton: -EINTR\n");
316 retval = -EINTR;
317 break;
318 }
319 }
320 remove_wait_queue(&vb->done, &wait);
321 return retval;
322}
323
324int
325videobuf_iolock(struct pci_dev *pci, struct videobuf_buffer *vb,
326 struct v4l2_framebuffer *fbuf)
327{
328 int err,pages;
329 dma_addr_t bus;
330
331 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
332 switch (vb->memory) {
333 case V4L2_MEMORY_MMAP:
334 case V4L2_MEMORY_USERPTR:
335 if (0 == vb->baddr) {
336 /* no userspace addr -- kernel bounce buffer */
337 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
338 err = videobuf_dma_init_kernel(&vb->dma,PCI_DMA_FROMDEVICE,
339 pages);
340 if (0 != err)
341 return err;
342 } else {
343 /* dma directly to userspace */
344 err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE,
345 vb->baddr,vb->bsize);
346 if (0 != err)
347 return err;
348 }
349 break;
350 case V4L2_MEMORY_OVERLAY:
351 if (NULL == fbuf)
352 return -EINVAL;
353 /* FIXME: need sanity checks for vb->boff */
354 bus = (dma_addr_t)fbuf->base + vb->boff;
355 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
356 err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
357 bus, pages);
358 if (0 != err)
359 return err;
360 break;
361 default:
362 BUG();
363 }
364 err = videobuf_dma_pci_map(pci,&vb->dma);
365 if (0 != err)
366 return err;
367
368 return 0;
369}
370
371/* --------------------------------------------------------------------- */
372
373void videobuf_queue_init(struct videobuf_queue* q,
374 struct videobuf_queue_ops *ops,
375 struct pci_dev *pci,
376 spinlock_t *irqlock,
377 enum v4l2_buf_type type,
378 enum v4l2_field field,
379 unsigned int msize,
380 void *priv)
381{
382 memset(q,0,sizeof(*q));
383 q->irqlock = irqlock;
384 q->pci = pci;
385 q->type = type;
386 q->field = field;
387 q->msize = msize;
388 q->ops = ops;
389 q->priv_data = priv;
390
391 init_MUTEX(&q->lock);
392 INIT_LIST_HEAD(&q->stream);
393}
394
395int
396videobuf_queue_is_busy(struct videobuf_queue *q)
397{
398 int i;
399
400 if (q->streaming) {
401 dprintk(1,"busy: streaming active\n");
402 return 1;
403 }
404 if (q->reading) {
405 dprintk(1,"busy: pending read #1\n");
406 return 1;
407 }
408 if (q->read_buf) {
409 dprintk(1,"busy: pending read #2\n");
410 return 1;
411 }
412 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
413 if (NULL == q->bufs[i])
414 continue;
415 if (q->bufs[i]->map) {
416 dprintk(1,"busy: buffer #%d mapped\n",i);
417 return 1;
418 }
419 if (q->bufs[i]->state == STATE_QUEUED) {
420 dprintk(1,"busy: buffer #%d queued\n",i);
421 return 1;
422 }
423 if (q->bufs[i]->state == STATE_ACTIVE) {
424 dprintk(1,"busy: buffer #%d avtive\n",i);
425 return 1;
426 }
427 }
428 return 0;
429}
430
431void
432videobuf_queue_cancel(struct videobuf_queue *q)
433{
434 unsigned long flags;
435 int i;
436
437 /* remove queued buffers from list */
438 spin_lock_irqsave(q->irqlock,flags);
439 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
440 if (NULL == q->bufs[i])
441 continue;
442 if (q->bufs[i]->state == STATE_QUEUED) {
443 list_del(&q->bufs[i]->queue);
444 q->bufs[i]->state = STATE_ERROR;
445 }
446 }
447 spin_unlock_irqrestore(q->irqlock,flags);
448
449 /* free all buffers + clear queue */
450 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
451 if (NULL == q->bufs[i])
452 continue;
453 q->ops->buf_release(q,q->bufs[i]);
454 }
455 INIT_LIST_HEAD(&q->stream);
456}
457
458/* --------------------------------------------------------------------- */
459
460enum v4l2_field
461videobuf_next_field(struct videobuf_queue *q)
462{
463 enum v4l2_field field = q->field;
464
465 BUG_ON(V4L2_FIELD_ANY == field);
466
467 if (V4L2_FIELD_ALTERNATE == field) {
468 if (V4L2_FIELD_TOP == q->last) {
469 field = V4L2_FIELD_BOTTOM;
470 q->last = V4L2_FIELD_BOTTOM;
471 } else {
472 field = V4L2_FIELD_TOP;
473 q->last = V4L2_FIELD_TOP;
474 }
475 }
476 return field;
477}
478
479void
480videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
481 enum v4l2_buf_type type)
482{
483 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
484
485 b->index = vb->i;
486 b->type = type;
487
488 b->memory = vb->memory;
489 switch (b->memory) {
490 case V4L2_MEMORY_MMAP:
491 b->m.offset = vb->boff;
492 b->length = vb->bsize;
493 break;
494 case V4L2_MEMORY_USERPTR:
495 b->m.userptr = vb->baddr;
496 b->length = vb->bsize;
497 break;
498 case V4L2_MEMORY_OVERLAY:
499 b->m.offset = vb->boff;
500 break;
501 }
502
503 b->flags = 0;
504 if (vb->map)
505 b->flags |= V4L2_BUF_FLAG_MAPPED;
506
507 switch (vb->state) {
508 case STATE_PREPARED:
509 case STATE_QUEUED:
510 case STATE_ACTIVE:
511 b->flags |= V4L2_BUF_FLAG_QUEUED;
512 break;
513 case STATE_DONE:
514 case STATE_ERROR:
515 b->flags |= V4L2_BUF_FLAG_DONE;
516 break;
517 case STATE_NEEDS_INIT:
518 case STATE_IDLE:
519 /* nothing */
520 break;
521 }
522
523 if (vb->input != UNSET) {
524 b->flags |= V4L2_BUF_FLAG_INPUT;
525 b->input = vb->input;
526 }
527
528 b->field = vb->field;
529 b->timestamp = vb->ts;
530 b->bytesused = vb->size;
531 b->sequence = vb->field_count >> 1;
532}
533
534int
535videobuf_reqbufs(struct videobuf_queue *q,
536 struct v4l2_requestbuffers *req)
537{
538 unsigned int size,count;
539 int retval;
540
541 if (req->type != q->type)
542 return -EINVAL;
543 if (req->count < 1)
544 return -EINVAL;
545 if (req->memory != V4L2_MEMORY_MMAP &&
546 req->memory != V4L2_MEMORY_USERPTR &&
547 req->memory != V4L2_MEMORY_OVERLAY)
548 return -EINVAL;
549
550 if (q->streaming)
551 return -EBUSY;
552 if (!list_empty(&q->stream))
553 return -EBUSY;
554
555 down(&q->lock);
556 count = req->count;
557 if (count > VIDEO_MAX_FRAME)
558 count = VIDEO_MAX_FRAME;
559 size = 0;
560 q->ops->buf_setup(q,&count,&size);
561 size = PAGE_ALIGN(size);
562 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
563 count, size, (count*size)>>PAGE_SHIFT);
564
565 retval = videobuf_mmap_setup(q,count,size,req->memory);
566 if (retval < 0)
567 goto done;
568
569 req->count = count;
570
571 done:
572 up(&q->lock);
573 return retval;
574}
575
576int
577videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
578{
579 if (unlikely(b->type != q->type))
580 return -EINVAL;
581 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME))
582 return -EINVAL;
583 if (unlikely(NULL == q->bufs[b->index]))
584 return -EINVAL;
585 videobuf_status(b,q->bufs[b->index],q->type);
586 return 0;
587}
588
589int
590videobuf_qbuf(struct videobuf_queue *q,
591 struct v4l2_buffer *b)
592{
593 struct videobuf_buffer *buf;
594 enum v4l2_field field;
595 unsigned long flags;
596 int retval;
597
598 down(&q->lock);
599 retval = -EBUSY;
600 if (q->reading)
601 goto done;
602 retval = -EINVAL;
603 if (b->type != q->type)
604 goto done;
605 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME)
606 goto done;
607 buf = q->bufs[b->index];
608 if (NULL == buf)
609 goto done;
610 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
611 if (buf->memory != b->memory)
612 goto done;
613 if (buf->state == STATE_QUEUED ||
614 buf->state == STATE_ACTIVE)
615 goto done;
616
617 if (b->flags & V4L2_BUF_FLAG_INPUT) {
618 if (b->input >= q->inputs)
619 goto done;
620 buf->input = b->input;
621 } else {
622 buf->input = UNSET;
623 }
624
625 switch (b->memory) {
626 case V4L2_MEMORY_MMAP:
627 if (0 == buf->baddr)
628 goto done;
629 break;
630 case V4L2_MEMORY_USERPTR:
631 if (b->length < buf->bsize)
632 goto done;
633 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
634 q->ops->buf_release(q,buf);
635 buf->baddr = b->m.userptr;
636 break;
637 case V4L2_MEMORY_OVERLAY:
638 buf->boff = b->m.offset;
639 break;
640 default:
641 goto done;
642 }
643
644 field = videobuf_next_field(q);
645 retval = q->ops->buf_prepare(q,buf,field);
646 if (0 != retval)
647 goto done;
648
649 list_add_tail(&buf->stream,&q->stream);
650 if (q->streaming) {
651 spin_lock_irqsave(q->irqlock,flags);
652 q->ops->buf_queue(q,buf);
653 spin_unlock_irqrestore(q->irqlock,flags);
654 }
655 retval = 0;
656
657 done:
658 up(&q->lock);
659 return retval;
660}
661
662int
663videobuf_dqbuf(struct videobuf_queue *q,
664 struct v4l2_buffer *b, int nonblocking)
665{
666 struct videobuf_buffer *buf;
667 int retval;
668
669 down(&q->lock);
670 retval = -EBUSY;
671 if (q->reading)
672 goto done;
673 retval = -EINVAL;
674 if (b->type != q->type)
675 goto done;
676 if (list_empty(&q->stream))
677 goto done;
678 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
679 retval = videobuf_waiton(buf, nonblocking, 1);
680 if (retval < 0)
681 goto done;
682 switch (buf->state) {
683 case STATE_ERROR:
684 retval = -EIO;
685 /* fall through */
686 case STATE_DONE:
687 videobuf_dma_pci_sync(q->pci,&buf->dma);
688 buf->state = STATE_IDLE;
689 break;
690 default:
691 retval = -EINVAL;
692 goto done;
693 }
694 list_del(&buf->stream);
695 memset(b,0,sizeof(*b));
696 videobuf_status(b,buf,q->type);
697
698 done:
699 up(&q->lock);
700 return retval;
701}
702
703int videobuf_streamon(struct videobuf_queue *q)
704{
705 struct videobuf_buffer *buf;
706 struct list_head *list;
707 unsigned long flags;
708 int retval;
709
710 down(&q->lock);
711 retval = -EBUSY;
712 if (q->reading)
713 goto done;
714 retval = 0;
715 if (q->streaming)
716 goto done;
717 q->streaming = 1;
718 spin_lock_irqsave(q->irqlock,flags);
719 list_for_each(list,&q->stream) {
720 buf = list_entry(list, struct videobuf_buffer, stream);
721 if (buf->state == STATE_PREPARED)
722 q->ops->buf_queue(q,buf);
723 }
724 spin_unlock_irqrestore(q->irqlock,flags);
725
726 done:
727 up(&q->lock);
728 return retval;
729}
730
731int videobuf_streamoff(struct videobuf_queue *q)
732{
733 int retval = -EINVAL;
734
735 down(&q->lock);
736 if (!q->streaming)
737 goto done;
738 videobuf_queue_cancel(q);
739 q->streaming = 0;
740 retval = 0;
741
742 done:
743 up(&q->lock);
744 return retval;
745}
746
747static ssize_t
748videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
749 size_t count, loff_t *ppos)
750{
751 enum v4l2_field field;
752 unsigned long flags;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800753 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800755 /* setup stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 q->read_buf = videobuf_alloc(q->msize);
757 if (NULL == q->read_buf)
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -0800758 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 q->read_buf->memory = V4L2_MEMORY_USERPTR;
761 q->read_buf->baddr = (unsigned long)data;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800762 q->read_buf->bsize = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 field = videobuf_next_field(q);
764 retval = q->ops->buf_prepare(q,q->read_buf,field);
765 if (0 != retval)
766 goto done;
767
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800768 /* start capture & wait */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 spin_lock_irqsave(q->irqlock,flags);
770 q->ops->buf_queue(q,q->read_buf);
771 spin_unlock_irqrestore(q->irqlock,flags);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800772 retval = videobuf_waiton(q->read_buf,0,0);
773 if (0 == retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 videobuf_dma_pci_sync(q->pci,&q->read_buf->dma);
775 if (STATE_ERROR == q->read_buf->state)
776 retval = -EIO;
777 else
778 retval = q->read_buf->size;
779 }
780
781 done:
782 /* cleanup */
783 q->ops->buf_release(q,q->read_buf);
784 kfree(q->read_buf);
785 q->read_buf = NULL;
786 return retval;
787}
788
789ssize_t videobuf_read_one(struct videobuf_queue *q,
790 char __user *data, size_t count, loff_t *ppos,
791 int nonblocking)
792{
793 enum v4l2_field field;
794 unsigned long flags;
795 unsigned size, nbufs, bytes;
796 int retval;
797
798 down(&q->lock);
799
800 nbufs = 1; size = 0;
801 q->ops->buf_setup(q,&nbufs,&size);
802 if (NULL == q->read_buf &&
803 count >= size &&
804 !nonblocking) {
805 retval = videobuf_read_zerocopy(q,data,count,ppos);
806 if (retval >= 0 || retval == -EIO)
807 /* ok, all done */
808 goto done;
809 /* fallback to kernel bounce buffer on failures */
810 }
811
812 if (NULL == q->read_buf) {
813 /* need to capture a new frame */
814 retval = -ENOMEM;
815 q->read_buf = videobuf_alloc(q->msize);
816 if (NULL == q->read_buf)
817 goto done;
818 q->read_buf->memory = V4L2_MEMORY_USERPTR;
Michael H. Schimek13c72802005-12-01 00:51:37 -0800819 q->read_buf->bsize = count; /* preferred size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 field = videobuf_next_field(q);
821 retval = q->ops->buf_prepare(q,q->read_buf,field);
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -0800822 if (0 != retval) {
823 kfree (q->read_buf);
824 q->read_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 goto done;
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -0800826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 spin_lock_irqsave(q->irqlock,flags);
828 q->ops->buf_queue(q,q->read_buf);
829 spin_unlock_irqrestore(q->irqlock,flags);
830 q->read_off = 0;
831 }
832
833 /* wait until capture is done */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800834 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (0 != retval)
836 goto done;
837 videobuf_dma_pci_sync(q->pci,&q->read_buf->dma);
838
839 if (STATE_ERROR == q->read_buf->state) {
840 /* catch I/O errors */
841 q->ops->buf_release(q,q->read_buf);
842 kfree(q->read_buf);
843 q->read_buf = NULL;
844 retval = -EIO;
845 goto done;
846 }
847
848 /* copy to userspace */
849 bytes = count;
850 if (bytes > q->read_buf->size - q->read_off)
851 bytes = q->read_buf->size - q->read_off;
852 retval = -EFAULT;
853 if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
854 goto done;
855
856 retval = bytes;
857 q->read_off += bytes;
858 if (q->read_off == q->read_buf->size) {
859 /* all data copied, cleanup */
860 q->ops->buf_release(q,q->read_buf);
861 kfree(q->read_buf);
862 q->read_buf = NULL;
863 }
864
865 done:
866 up(&q->lock);
867 return retval;
868}
869
870int videobuf_read_start(struct videobuf_queue *q)
871{
872 enum v4l2_field field;
873 unsigned long flags;
874 int count = 0, size = 0;
875 int err, i;
876
877 q->ops->buf_setup(q,&count,&size);
878 if (count < 2)
879 count = 2;
880 if (count > VIDEO_MAX_FRAME)
881 count = VIDEO_MAX_FRAME;
882 size = PAGE_ALIGN(size);
883
884 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
885 if (err)
886 return err;
887 for (i = 0; i < count; i++) {
888 field = videobuf_next_field(q);
889 err = q->ops->buf_prepare(q,q->bufs[i],field);
890 if (err)
891 return err;
892 list_add_tail(&q->bufs[i]->stream, &q->stream);
893 }
894 spin_lock_irqsave(q->irqlock,flags);
895 for (i = 0; i < count; i++)
896 q->ops->buf_queue(q,q->bufs[i]);
897 spin_unlock_irqrestore(q->irqlock,flags);
898 q->reading = 1;
899 return 0;
900}
901
902void videobuf_read_stop(struct videobuf_queue *q)
903{
904 int i;
905
906 videobuf_queue_cancel(q);
907 videobuf_mmap_free(q);
908 INIT_LIST_HEAD(&q->stream);
909 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
910 if (NULL == q->bufs[i])
911 continue;
912 kfree(q->bufs[i]);
913 q->bufs[i] = NULL;
914 }
915 q->read_buf = NULL;
916 q->reading = 0;
917}
918
919ssize_t videobuf_read_stream(struct videobuf_queue *q,
920 char __user *data, size_t count, loff_t *ppos,
921 int vbihack, int nonblocking)
922{
923 unsigned int *fc, bytes;
924 int err, retval;
925 unsigned long flags;
926
927 dprintk(2,"%s\n",__FUNCTION__);
928 down(&q->lock);
929 retval = -EBUSY;
930 if (q->streaming)
931 goto done;
932 if (!q->reading) {
933 retval = videobuf_read_start(q);
934 if (retval < 0)
935 goto done;
936 }
937
938 retval = 0;
939 while (count > 0) {
940 /* get / wait for data */
941 if (NULL == q->read_buf) {
942 q->read_buf = list_entry(q->stream.next,
943 struct videobuf_buffer,
944 stream);
945 list_del(&q->read_buf->stream);
946 q->read_off = 0;
947 }
948 err = videobuf_waiton(q->read_buf, nonblocking, 1);
949 if (err < 0) {
950 if (0 == retval)
951 retval = err;
952 break;
953 }
954
955 if (q->read_buf->state == STATE_DONE) {
956 if (vbihack) {
957 /* dirty, undocumented hack -- pass the frame counter
958 * within the last four bytes of each vbi data block.
959 * We need that one to maintain backward compatibility
960 * to all vbi decoding software out there ... */
961 fc = (unsigned int*)q->read_buf->dma.vmalloc;
962 fc += (q->read_buf->size>>2) -1;
963 *fc = q->read_buf->field_count >> 1;
964 dprintk(1,"vbihack: %d\n",*fc);
965 }
966
967 /* copy stuff */
968 bytes = count;
969 if (bytes > q->read_buf->size - q->read_off)
970 bytes = q->read_buf->size - q->read_off;
971 if (copy_to_user(data + retval,
972 q->read_buf->dma.vmalloc + q->read_off,
973 bytes)) {
974 if (0 == retval)
975 retval = -EFAULT;
976 break;
977 }
978 count -= bytes;
979 retval += bytes;
980 q->read_off += bytes;
981 } else {
982 /* some error */
983 q->read_off = q->read_buf->size;
984 if (0 == retval)
985 retval = -EIO;
986 }
987
988 /* requeue buffer when done with copying */
989 if (q->read_off == q->read_buf->size) {
990 list_add_tail(&q->read_buf->stream,
991 &q->stream);
992 spin_lock_irqsave(q->irqlock,flags);
993 q->ops->buf_queue(q,q->read_buf);
994 spin_unlock_irqrestore(q->irqlock,flags);
995 q->read_buf = NULL;
996 }
997 if (retval < 0)
998 break;
999 }
1000
1001 done:
1002 up(&q->lock);
1003 return retval;
1004}
1005
1006unsigned int videobuf_poll_stream(struct file *file,
1007 struct videobuf_queue *q,
1008 poll_table *wait)
1009{
1010 struct videobuf_buffer *buf = NULL;
1011 unsigned int rc = 0;
1012
1013 down(&q->lock);
1014 if (q->streaming) {
1015 if (!list_empty(&q->stream))
1016 buf = list_entry(q->stream.next,
1017 struct videobuf_buffer, stream);
1018 } else {
1019 if (!q->reading)
1020 videobuf_read_start(q);
1021 if (!q->reading) {
1022 rc = POLLERR;
1023 } else if (NULL == q->read_buf) {
1024 q->read_buf = list_entry(q->stream.next,
1025 struct videobuf_buffer,
1026 stream);
1027 list_del(&q->read_buf->stream);
1028 q->read_off = 0;
1029 }
1030 buf = q->read_buf;
1031 }
1032 if (!buf)
1033 rc = POLLERR;
1034
1035 if (0 == rc) {
1036 poll_wait(file, &buf->done, wait);
1037 if (buf->state == STATE_DONE ||
1038 buf->state == STATE_ERROR)
1039 rc = POLLIN|POLLRDNORM;
1040 }
1041 up(&q->lock);
1042 return rc;
1043}
1044
1045/* --------------------------------------------------------------------- */
1046
1047static void
1048videobuf_vm_open(struct vm_area_struct *vma)
1049{
1050 struct videobuf_mapping *map = vma->vm_private_data;
1051
1052 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1053 map->count,vma->vm_start,vma->vm_end);
1054 map->count++;
1055}
1056
1057static void
1058videobuf_vm_close(struct vm_area_struct *vma)
1059{
1060 struct videobuf_mapping *map = vma->vm_private_data;
1061 struct videobuf_queue *q = map->q;
1062 int i;
1063
1064 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1065 map->count,vma->vm_start,vma->vm_end);
1066
1067 map->count--;
1068 if (0 == map->count) {
1069 dprintk(1,"munmap %p q=%p\n",map,q);
1070 down(&q->lock);
1071 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1072 if (NULL == q->bufs[i])
1073 continue;
1074 if (q->bufs[i])
1075 ;
1076 if (q->bufs[i]->map != map)
1077 continue;
1078 q->bufs[i]->map = NULL;
1079 q->bufs[i]->baddr = 0;
1080 q->ops->buf_release(q,q->bufs[i]);
1081 }
1082 up(&q->lock);
1083 kfree(map);
1084 }
1085 return;
1086}
1087
1088/*
1089 * Get a anonymous page for the mapping. Make sure we can DMA to that
1090 * memory location with 32bit PCI devices (i.e. don't use highmem for
1091 * now ...). Bounce buffers don't work very well for the data rates
1092 * video capture has.
1093 */
1094static struct page*
1095videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1096 int *type)
1097{
1098 struct page *page;
1099
1100 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1101 vaddr,vma->vm_start,vma->vm_end);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001102 if (vaddr > vma->vm_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return NOPAGE_SIGBUS;
1104 page = alloc_page(GFP_USER);
1105 if (!page)
1106 return NOPAGE_OOM;
1107 clear_user_page(page_address(page), vaddr, page);
1108 if (type)
1109 *type = VM_FAULT_MINOR;
1110 return page;
1111}
1112
1113static struct vm_operations_struct videobuf_vm_ops =
1114{
1115 .open = videobuf_vm_open,
1116 .close = videobuf_vm_close,
1117 .nopage = videobuf_vm_nopage,
1118};
1119
1120int videobuf_mmap_setup(struct videobuf_queue *q,
1121 unsigned int bcount, unsigned int bsize,
1122 enum v4l2_memory memory)
1123{
1124 unsigned int i;
1125 int err;
1126
1127 err = videobuf_mmap_free(q);
1128 if (0 != err)
1129 return err;
1130
1131 for (i = 0; i < bcount; i++) {
1132 q->bufs[i] = videobuf_alloc(q->msize);
1133 q->bufs[i]->i = i;
1134 q->bufs[i]->input = UNSET;
1135 q->bufs[i]->memory = memory;
1136 q->bufs[i]->bsize = bsize;
1137 switch (memory) {
1138 case V4L2_MEMORY_MMAP:
1139 q->bufs[i]->boff = bsize * i;
1140 break;
1141 case V4L2_MEMORY_USERPTR:
1142 case V4L2_MEMORY_OVERLAY:
1143 /* nothing */
1144 break;
1145 }
1146 }
1147 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1148 bcount,bsize);
1149 return 0;
1150}
1151
1152int videobuf_mmap_free(struct videobuf_queue *q)
1153{
1154 int i;
1155
1156 for (i = 0; i < VIDEO_MAX_FRAME; i++)
1157 if (q->bufs[i] && q->bufs[i]->map)
1158 return -EBUSY;
1159 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1160 if (NULL == q->bufs[i])
1161 continue;
1162 q->ops->buf_release(q,q->bufs[i]);
1163 kfree(q->bufs[i]);
1164 q->bufs[i] = NULL;
1165 }
1166 return 0;
1167}
1168
1169int videobuf_mmap_mapper(struct videobuf_queue *q,
1170 struct vm_area_struct *vma)
1171{
1172 struct videobuf_mapping *map;
1173 unsigned int first,last,size,i;
1174 int retval;
1175
1176 down(&q->lock);
1177 retval = -EINVAL;
1178 if (!(vma->vm_flags & VM_WRITE)) {
1179 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1180 goto done;
1181 }
1182 if (!(vma->vm_flags & VM_SHARED)) {
1183 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1184 goto done;
1185 }
1186
1187 /* look for first buffer to map */
1188 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1189 if (NULL == q->bufs[first])
1190 continue;
1191 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1192 continue;
1193 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1194 break;
1195 }
1196 if (VIDEO_MAX_FRAME == first) {
1197 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1198 (vma->vm_pgoff << PAGE_SHIFT));
1199 goto done;
1200 }
1201
1202 /* look for last buffer to map */
1203 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1204 if (NULL == q->bufs[last])
1205 continue;
1206 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1207 continue;
1208 if (q->bufs[last]->map) {
1209 retval = -EBUSY;
1210 goto done;
1211 }
1212 size += q->bufs[last]->bsize;
1213 if (size == (vma->vm_end - vma->vm_start))
1214 break;
1215 }
1216 if (VIDEO_MAX_FRAME == last) {
1217 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1218 (vma->vm_end - vma->vm_start));
1219 goto done;
1220 }
1221
1222 /* create mapping + update buffer list */
1223 retval = -ENOMEM;
1224 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1225 if (NULL == map)
1226 goto done;
1227 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1228 q->bufs[i]->map = map;
1229 q->bufs[i]->baddr = vma->vm_start + size;
1230 }
1231 map->count = 1;
1232 map->start = vma->vm_start;
1233 map->end = vma->vm_end;
1234 map->q = q;
1235 vma->vm_ops = &videobuf_vm_ops;
1236 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1237 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1238 vma->vm_private_data = map;
1239 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1240 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1241 retval = 0;
1242
1243 done:
1244 up(&q->lock);
1245 return retval;
1246}
1247
1248/* --------------------------------------------------------------------- */
1249
1250EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1251
1252EXPORT_SYMBOL_GPL(videobuf_dma_init);
1253EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1254EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1255EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
1256EXPORT_SYMBOL_GPL(videobuf_dma_pci_map);
1257EXPORT_SYMBOL_GPL(videobuf_dma_pci_sync);
1258EXPORT_SYMBOL_GPL(videobuf_dma_pci_unmap);
1259EXPORT_SYMBOL_GPL(videobuf_dma_free);
1260
1261EXPORT_SYMBOL_GPL(videobuf_alloc);
1262EXPORT_SYMBOL_GPL(videobuf_waiton);
1263EXPORT_SYMBOL_GPL(videobuf_iolock);
1264
1265EXPORT_SYMBOL_GPL(videobuf_queue_init);
1266EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1267EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1268
1269EXPORT_SYMBOL_GPL(videobuf_next_field);
1270EXPORT_SYMBOL_GPL(videobuf_status);
1271EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1272EXPORT_SYMBOL_GPL(videobuf_querybuf);
1273EXPORT_SYMBOL_GPL(videobuf_qbuf);
1274EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1275EXPORT_SYMBOL_GPL(videobuf_streamon);
1276EXPORT_SYMBOL_GPL(videobuf_streamoff);
1277
1278EXPORT_SYMBOL_GPL(videobuf_read_start);
1279EXPORT_SYMBOL_GPL(videobuf_read_stop);
1280EXPORT_SYMBOL_GPL(videobuf_read_stream);
1281EXPORT_SYMBOL_GPL(videobuf_read_one);
1282EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1283
1284EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1285EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1286EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1287
1288/*
1289 * Local variables:
1290 * c-basic-offset: 8
1291 * End:
1292 */