blob: 635d102c86f0e5945ec6d19afadf5743dcc5a037 [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
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03004 * memory management and PCI DMA.
5 * Right now, bttv, saa7134, saa7146 and cx88 use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030010 * to touch the video data.
11 *
12 * device specific map/unmap/sync stuff now are mapped as operations
13 * to allow its usage by USB and virtual devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * (c) 2001-2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030016 * (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
17 * (c) 2006 Ted Walther and John Sokol
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 */
24
25#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/vmalloc.h>
29#include <linux/pagemap.h>
30#include <linux/slab.h>
31#include <linux/pci.h>
32#include <linux/interrupt.h>
33#include <asm/page.h>
34#include <asm/pgtable.h>
35
36#include <media/video-buf.h>
37
38#define MAGIC_DMABUF 0x19721112
39#define MAGIC_BUFFER 0x20040302
40#define MAGIC_CHECK(is,should) if (unlikely((is) != (should))) \
41 { printk(KERN_ERR "magic mismatch: %x (expected %x)\n",is,should); BUG(); }
42
43static int debug = 0;
44module_param(debug, int, 0644);
45
46MODULE_DESCRIPTION("helper module to manage video4linux pci dma buffers");
47MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
48MODULE_LICENSE("GPL");
49
50#define dprintk(level, fmt, arg...) if (debug >= level) \
51 printk(KERN_DEBUG "vbuf: " fmt , ## arg)
52
53struct scatterlist*
54videobuf_vmalloc_to_sg(unsigned char *virt, int nr_pages)
55{
56 struct scatterlist *sglist;
57 struct page *pg;
58 int i;
59
Panagiotis Issaris74081872006-01-11 19:40:56 -020060 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (NULL == sglist)
62 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
64 pg = vmalloc_to_page(virt);
65 if (NULL == pg)
66 goto err;
Eric Sesterhennae246012006-03-13 13:17:11 -030067 BUG_ON(PageHighMem(pg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 sglist[i].page = pg;
69 sglist[i].length = PAGE_SIZE;
70 }
71 return sglist;
72
73 err:
74 kfree(sglist);
75 return NULL;
76}
77
78struct scatterlist*
79videobuf_pages_to_sg(struct page **pages, int nr_pages, int offset)
80{
81 struct scatterlist *sglist;
82 int i = 0;
83
84 if (NULL == pages[0])
85 return NULL;
Panagiotis Issaris74081872006-01-11 19:40:56 -020086 sglist = kcalloc(nr_pages, sizeof(*sglist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (NULL == sglist)
88 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (NULL == pages[0])
91 goto nopage;
92 if (PageHighMem(pages[0]))
93 /* DMA to highmem pages might not work */
94 goto highmem;
95 sglist[0].page = pages[0];
96 sglist[0].offset = offset;
97 sglist[0].length = PAGE_SIZE - offset;
98 for (i = 1; i < nr_pages; i++) {
99 if (NULL == pages[i])
100 goto nopage;
101 if (PageHighMem(pages[i]))
102 goto highmem;
103 sglist[i].page = pages[i];
104 sglist[i].length = PAGE_SIZE;
105 }
106 return sglist;
107
108 nopage:
109 dprintk(2,"sgl: oops - no page\n");
110 kfree(sglist);
111 return NULL;
112
113 highmem:
114 dprintk(2,"sgl: oops - highmem page\n");
115 kfree(sglist);
116 return NULL;
117}
118
119/* --------------------------------------------------------------------- */
120
121void videobuf_dma_init(struct videobuf_dmabuf *dma)
122{
123 memset(dma,0,sizeof(*dma));
124 dma->magic = MAGIC_DMABUF;
125}
126
127int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction,
128 unsigned long data, unsigned long size)
129{
130 unsigned long first,last;
131 int err, rw = 0;
132
133 dma->direction = direction;
134 switch (dma->direction) {
135 case PCI_DMA_FROMDEVICE: rw = READ; break;
136 case PCI_DMA_TODEVICE: rw = WRITE; break;
137 default: BUG();
138 }
139
140 first = (data & PAGE_MASK) >> PAGE_SHIFT;
141 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT;
142 dma->offset = data & ~PAGE_MASK;
143 dma->nr_pages = last-first+1;
144 dma->pages = kmalloc(dma->nr_pages * sizeof(struct page*),
145 GFP_KERNEL);
146 if (NULL == dma->pages)
147 return -ENOMEM;
148 dprintk(1,"init user [0x%lx+0x%lx => %d pages]\n",
149 data,size,dma->nr_pages);
150
151 down_read(&current->mm->mmap_sem);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800152 err = get_user_pages(current,current->mm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 data & PAGE_MASK, dma->nr_pages,
154 rw == READ, 1, /* force */
155 dma->pages, NULL);
156 up_read(&current->mm->mmap_sem);
157 if (err != dma->nr_pages) {
158 dma->nr_pages = (err >= 0) ? err : 0;
159 dprintk(1,"get_user_pages: err=%d [%d]\n",err,dma->nr_pages);
160 return err < 0 ? err : -EINVAL;
161 }
162 return 0;
163}
164
165int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction,
166 int nr_pages)
167{
168 dprintk(1,"init kernel [%d pages]\n",nr_pages);
169 dma->direction = direction;
170 dma->vmalloc = vmalloc_32(nr_pages << PAGE_SHIFT);
171 if (NULL == dma->vmalloc) {
172 dprintk(1,"vmalloc_32(%d pages) failed\n",nr_pages);
173 return -ENOMEM;
174 }
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300175 dprintk(1,"vmalloc is at addr 0x%08lx, size=%d\n",
176 (unsigned long)dma->vmalloc,
177 nr_pages << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 memset(dma->vmalloc,0,nr_pages << PAGE_SHIFT);
179 dma->nr_pages = nr_pages;
180 return 0;
181}
182
183int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction,
184 dma_addr_t addr, int nr_pages)
185{
186 dprintk(1,"init overlay [%d pages @ bus 0x%lx]\n",
187 nr_pages,(unsigned long)addr);
188 dma->direction = direction;
189 if (0 == addr)
190 return -EINVAL;
191
192 dma->bus_addr = addr;
193 dma->nr_pages = nr_pages;
194 return 0;
195}
196
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300197int videobuf_dma_map(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300199 void *dev=q->dev;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
202 BUG_ON(0 == dma->nr_pages);
203
204 if (dma->pages) {
205 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages,
206 dma->offset);
207 }
208 if (dma->vmalloc) {
209 dma->sglist = videobuf_vmalloc_to_sg
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300210 (dma->vmalloc,dma->nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 if (dma->bus_addr) {
213 dma->sglist = kmalloc(sizeof(struct scatterlist), GFP_KERNEL);
214 if (NULL != dma->sglist) {
215 dma->sglen = 1;
216 sg_dma_address(&dma->sglist[0]) = dma->bus_addr & PAGE_MASK;
217 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK;
218 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE;
219 }
220 }
221 if (NULL == dma->sglist) {
222 dprintk(1,"scatterlist is NULL\n");
223 return -ENOMEM;
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!dma->bus_addr) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300226 if (q->ops->vb_map_sg) {
227 dma->sglen = q->ops->vb_map_sg(dev,dma->sglist,
228 dma->nr_pages, dma->direction);
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (0 == dma->sglen) {
231 printk(KERN_WARNING
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300232 "%s: videobuf_map_sg failed\n",__FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 kfree(dma->sglist);
234 dma->sglist = NULL;
235 dma->sglen = 0;
236 return -EIO;
237 }
238 }
239 return 0;
240}
241
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300242int videobuf_dma_sync(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300244 void *dev=q->dev;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
247 BUG_ON(!dma->sglen);
248
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300249 if (!dma->bus_addr && q->ops->vb_dma_sync_sg)
250 q->ops->vb_dma_sync_sg(dev,dma->sglist,dma->nr_pages,
251 dma->direction);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
255
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300256int videobuf_dma_unmap(struct videobuf_queue* q,struct videobuf_dmabuf *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300258 void *dev=q->dev;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
261 if (!dma->sglen)
262 return 0;
263
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300264 if (!dma->bus_addr && q->ops->vb_unmap_sg)
265 q->ops->vb_unmap_sg(dev,dma->sglist,dma->nr_pages,
266 dma->direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 kfree(dma->sglist);
268 dma->sglist = NULL;
269 dma->sglen = 0;
270 return 0;
271}
272
273int videobuf_dma_free(struct videobuf_dmabuf *dma)
274{
275 MAGIC_CHECK(dma->magic,MAGIC_DMABUF);
276 BUG_ON(dma->sglen);
277
278 if (dma->pages) {
279 int i;
280 for (i=0; i < dma->nr_pages; i++)
281 page_cache_release(dma->pages[i]);
282 kfree(dma->pages);
283 dma->pages = NULL;
284 }
Jesper Juhlf91012102005-09-10 00:26:54 -0700285
286 vfree(dma->vmalloc);
287 dma->vmalloc = NULL;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (dma->bus_addr) {
290 dma->bus_addr = 0;
291 }
292 dma->direction = PCI_DMA_NONE;
293 return 0;
294}
295
296/* --------------------------------------------------------------------- */
297
298void* videobuf_alloc(unsigned int size)
299{
300 struct videobuf_buffer *vb;
301
Panagiotis Issaris74081872006-01-11 19:40:56 -0200302 vb = kzalloc(size,GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (NULL != vb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 videobuf_dma_init(&vb->dma);
305 init_waitqueue_head(&vb->done);
306 vb->magic = MAGIC_BUFFER;
307 }
308 return vb;
309}
310
311int videobuf_waiton(struct videobuf_buffer *vb, int non_blocking, int intr)
312{
313 int retval = 0;
314 DECLARE_WAITQUEUE(wait, current);
315
316 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
317 add_wait_queue(&vb->done, &wait);
318 while (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED) {
319 if (non_blocking) {
320 retval = -EAGAIN;
321 break;
322 }
323 set_current_state(intr ? TASK_INTERRUPTIBLE
324 : TASK_UNINTERRUPTIBLE);
325 if (vb->state == STATE_ACTIVE || vb->state == STATE_QUEUED)
326 schedule();
327 set_current_state(TASK_RUNNING);
328 if (intr && signal_pending(current)) {
329 dprintk(1,"buffer waiton: -EINTR\n");
330 retval = -EINTR;
331 break;
332 }
333 }
334 remove_wait_queue(&vb->done, &wait);
335 return retval;
336}
337
338int
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300339videobuf_iolock(struct videobuf_queue* q, struct videobuf_buffer *vb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct v4l2_framebuffer *fbuf)
341{
342 int err,pages;
343 dma_addr_t bus;
344
345 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
346 switch (vb->memory) {
347 case V4L2_MEMORY_MMAP:
348 case V4L2_MEMORY_USERPTR:
349 if (0 == vb->baddr) {
350 /* no userspace addr -- kernel bounce buffer */
351 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
352 err = videobuf_dma_init_kernel(&vb->dma,PCI_DMA_FROMDEVICE,
353 pages);
354 if (0 != err)
355 return err;
356 } else {
357 /* dma directly to userspace */
358 err = videobuf_dma_init_user(&vb->dma,PCI_DMA_FROMDEVICE,
359 vb->baddr,vb->bsize);
360 if (0 != err)
361 return err;
362 }
363 break;
364 case V4L2_MEMORY_OVERLAY:
365 if (NULL == fbuf)
366 return -EINVAL;
367 /* FIXME: need sanity checks for vb->boff */
Sujoy Gupta6eb32a792006-09-30 06:55:29 -0300368 /*
369 * Using a double cast to avoid compiler warnings when
370 * building for PAE. Compiler doesn't like direct casting
371 * of a 32 bit ptr to 64 bit integer.
372 */
373 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT;
375 err = videobuf_dma_init_overlay(&vb->dma,PCI_DMA_FROMDEVICE,
376 bus, pages);
377 if (0 != err)
378 return err;
379 break;
380 default:
381 BUG();
382 }
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300383 err = videobuf_dma_map(q,&vb->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (0 != err)
385 return err;
386
387 return 0;
388}
389
390/* --------------------------------------------------------------------- */
391
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300392void videobuf_queue_pci(struct videobuf_queue* q)
393{
394 /* If not specified, defaults to PCI map sg */
395 if (!q->ops->vb_map_sg)
396 q->ops->vb_map_sg=(vb_map_sg_t *)pci_map_sg;
397
398 if (!q->ops->vb_dma_sync_sg)
399 q->ops->vb_dma_sync_sg=(vb_map_sg_t *)pci_dma_sync_sg_for_cpu;
400 if (!q->ops->vb_unmap_sg)
401 q->ops->vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
402}
403
404int videobuf_pci_dma_map(struct pci_dev *pci,struct videobuf_dmabuf *dma)
405{
406 struct videobuf_queue q;
Ricardo Cerqueira731f9022006-03-27 09:16:31 -0300407 struct videobuf_queue_ops qops;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300408
409 q.dev=pci;
Ricardo Cerqueira731f9022006-03-27 09:16:31 -0300410 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
411 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
412 q.ops = &qops;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300413
Ricardo Cerqueira731f9022006-03-27 09:16:31 -0300414 return (videobuf_dma_map(&q,dma));
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300415}
416
417int videobuf_pci_dma_unmap(struct pci_dev *pci,struct videobuf_dmabuf *dma)
418{
419 struct videobuf_queue q;
Ricardo Cerqueira731f9022006-03-27 09:16:31 -0300420 struct videobuf_queue_ops qops;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300421
422 q.dev=pci;
Ricardo Cerqueira731f9022006-03-27 09:16:31 -0300423 qops.vb_map_sg=(vb_map_sg_t *)pci_map_sg;
424 qops.vb_unmap_sg=(vb_map_sg_t *)pci_unmap_sg;
425 q.ops = &qops;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300426
427 return (videobuf_dma_unmap(&q,dma));
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430void videobuf_queue_init(struct videobuf_queue* q,
431 struct videobuf_queue_ops *ops,
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300432 void *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spinlock_t *irqlock,
434 enum v4l2_buf_type type,
435 enum v4l2_field field,
436 unsigned int msize,
437 void *priv)
438{
439 memset(q,0,sizeof(*q));
440 q->irqlock = irqlock;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300441 q->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 q->type = type;
443 q->field = field;
444 q->msize = msize;
445 q->ops = ops;
446 q->priv_data = priv;
447
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300448 videobuf_queue_pci(q);
449
Ingo Molnar3593cab2006-02-07 06:49:14 -0200450 mutex_init(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 INIT_LIST_HEAD(&q->stream);
452}
453
454int
455videobuf_queue_is_busy(struct videobuf_queue *q)
456{
457 int i;
458
459 if (q->streaming) {
460 dprintk(1,"busy: streaming active\n");
461 return 1;
462 }
463 if (q->reading) {
464 dprintk(1,"busy: pending read #1\n");
465 return 1;
466 }
467 if (q->read_buf) {
468 dprintk(1,"busy: pending read #2\n");
469 return 1;
470 }
471 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
472 if (NULL == q->bufs[i])
473 continue;
474 if (q->bufs[i]->map) {
475 dprintk(1,"busy: buffer #%d mapped\n",i);
476 return 1;
477 }
478 if (q->bufs[i]->state == STATE_QUEUED) {
479 dprintk(1,"busy: buffer #%d queued\n",i);
480 return 1;
481 }
482 if (q->bufs[i]->state == STATE_ACTIVE) {
483 dprintk(1,"busy: buffer #%d avtive\n",i);
484 return 1;
485 }
486 }
487 return 0;
488}
489
490void
491videobuf_queue_cancel(struct videobuf_queue *q)
492{
Mauro Carvalho Chehab9b89f2d2006-03-15 17:52:14 -0300493 unsigned long flags=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int i;
495
496 /* remove queued buffers from list */
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300497 if (q->irqlock)
498 spin_lock_irqsave(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
500 if (NULL == q->bufs[i])
501 continue;
502 if (q->bufs[i]->state == STATE_QUEUED) {
503 list_del(&q->bufs[i]->queue);
504 q->bufs[i]->state = STATE_ERROR;
505 }
506 }
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300507 if (q->irqlock)
508 spin_unlock_irqrestore(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* free all buffers + clear queue */
511 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
512 if (NULL == q->bufs[i])
513 continue;
514 q->ops->buf_release(q,q->bufs[i]);
515 }
516 INIT_LIST_HEAD(&q->stream);
517}
518
519/* --------------------------------------------------------------------- */
520
521enum v4l2_field
522videobuf_next_field(struct videobuf_queue *q)
523{
524 enum v4l2_field field = q->field;
525
526 BUG_ON(V4L2_FIELD_ANY == field);
527
528 if (V4L2_FIELD_ALTERNATE == field) {
529 if (V4L2_FIELD_TOP == q->last) {
530 field = V4L2_FIELD_BOTTOM;
531 q->last = V4L2_FIELD_BOTTOM;
532 } else {
533 field = V4L2_FIELD_TOP;
534 q->last = V4L2_FIELD_TOP;
535 }
536 }
537 return field;
538}
539
540void
541videobuf_status(struct v4l2_buffer *b, struct videobuf_buffer *vb,
542 enum v4l2_buf_type type)
543{
544 MAGIC_CHECK(vb->magic,MAGIC_BUFFER);
545
546 b->index = vb->i;
547 b->type = type;
548
549 b->memory = vb->memory;
550 switch (b->memory) {
551 case V4L2_MEMORY_MMAP:
552 b->m.offset = vb->boff;
553 b->length = vb->bsize;
554 break;
555 case V4L2_MEMORY_USERPTR:
556 b->m.userptr = vb->baddr;
557 b->length = vb->bsize;
558 break;
559 case V4L2_MEMORY_OVERLAY:
560 b->m.offset = vb->boff;
561 break;
562 }
563
564 b->flags = 0;
565 if (vb->map)
566 b->flags |= V4L2_BUF_FLAG_MAPPED;
567
568 switch (vb->state) {
569 case STATE_PREPARED:
570 case STATE_QUEUED:
571 case STATE_ACTIVE:
572 b->flags |= V4L2_BUF_FLAG_QUEUED;
573 break;
574 case STATE_DONE:
575 case STATE_ERROR:
576 b->flags |= V4L2_BUF_FLAG_DONE;
577 break;
578 case STATE_NEEDS_INIT:
579 case STATE_IDLE:
580 /* nothing */
581 break;
582 }
583
584 if (vb->input != UNSET) {
585 b->flags |= V4L2_BUF_FLAG_INPUT;
586 b->input = vb->input;
587 }
588
589 b->field = vb->field;
590 b->timestamp = vb->ts;
591 b->bytesused = vb->size;
592 b->sequence = vb->field_count >> 1;
593}
594
595int
596videobuf_reqbufs(struct videobuf_queue *q,
597 struct v4l2_requestbuffers *req)
598{
599 unsigned int size,count;
600 int retval;
601
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300602 if (req->type != q->type) {
603 dprintk(1,"reqbufs: queue type invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return -EINVAL;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300605 }
606 if (req->count < 1) {
607 dprintk(1,"reqbufs: count invalid (%d)\n",req->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -EINVAL;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (req->memory != V4L2_MEMORY_MMAP &&
611 req->memory != V4L2_MEMORY_USERPTR &&
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300612 req->memory != V4L2_MEMORY_OVERLAY) {
613 dprintk(1,"reqbufs: memory type invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return -EINVAL;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300617 if (q->streaming) {
618 dprintk(1,"reqbufs: streaming already exists\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return -EBUSY;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300620 }
621 if (!list_empty(&q->stream)) {
622 dprintk(1,"reqbufs: stream running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EBUSY;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Ingo Molnar3593cab2006-02-07 06:49:14 -0200626 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 count = req->count;
628 if (count > VIDEO_MAX_FRAME)
629 count = VIDEO_MAX_FRAME;
630 size = 0;
631 q->ops->buf_setup(q,&count,&size);
632 size = PAGE_ALIGN(size);
633 dprintk(1,"reqbufs: bufs=%d, size=0x%x [%d pages total]\n",
634 count, size, (count*size)>>PAGE_SHIFT);
635
636 retval = videobuf_mmap_setup(q,count,size,req->memory);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300637 if (retval < 0) {
638 dprintk(1,"reqbufs: mmap setup returned %d\n",retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 req->count = count;
643
644 done:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200645 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return retval;
647}
648
649int
650videobuf_querybuf(struct videobuf_queue *q, struct v4l2_buffer *b)
651{
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300652 if (unlikely(b->type != q->type)) {
653 dprintk(1,"querybuf: Wrong type.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300655 }
656 if (unlikely(b->index < 0 || b->index >= VIDEO_MAX_FRAME)) {
657 dprintk(1,"querybuf: index out of range.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EINVAL;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300659 }
660 if (unlikely(NULL == q->bufs[b->index])) {
661 dprintk(1,"querybuf: buffer is null.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -EINVAL;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 videobuf_status(b,q->bufs[b->index],q->type);
665 return 0;
666}
667
668int
669videobuf_qbuf(struct videobuf_queue *q,
670 struct v4l2_buffer *b)
671{
672 struct videobuf_buffer *buf;
673 enum v4l2_field field;
Mauro Carvalho Chehab9b89f2d2006-03-15 17:52:14 -0300674 unsigned long flags=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int retval;
676
Ingo Molnar3593cab2006-02-07 06:49:14 -0200677 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 retval = -EBUSY;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300679 if (q->reading) {
680 dprintk(1,"qbuf: Reading running...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 retval = -EINVAL;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300684 if (b->type != q->type) {
685 dprintk(1,"qbuf: Wrong type.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300687 }
688 if (b->index < 0 || b->index >= VIDEO_MAX_FRAME) {
689 dprintk(1,"qbuf: index out of range.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 buf = q->bufs[b->index];
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300693 if (NULL == buf) {
694 dprintk(1,"qbuf: buffer is null.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 MAGIC_CHECK(buf->magic,MAGIC_BUFFER);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300698 if (buf->memory != b->memory) {
699 dprintk(1,"qbuf: memory type is wrong.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (buf->state == STATE_QUEUED ||
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300703 buf->state == STATE_ACTIVE) {
704 dprintk(1,"qbuf: buffer is already queued or active.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (b->flags & V4L2_BUF_FLAG_INPUT) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300709 if (b->input >= q->inputs) {
710 dprintk(1,"qbuf: wrong input.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 buf->input = b->input;
714 } else {
715 buf->input = UNSET;
716 }
717
718 switch (b->memory) {
719 case V4L2_MEMORY_MMAP:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300720 if (0 == buf->baddr) {
721 dprintk(1,"qbuf: mmap requested but buffer addr is zero!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
725 case V4L2_MEMORY_USERPTR:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300726 if (b->length < buf->bsize) {
727 dprintk(1,"qbuf: buffer length is not enough\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (STATE_NEEDS_INIT != buf->state && buf->baddr != b->m.userptr)
731 q->ops->buf_release(q,buf);
732 buf->baddr = b->m.userptr;
733 break;
734 case V4L2_MEMORY_OVERLAY:
735 buf->boff = b->m.offset;
736 break;
737 default:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300738 dprintk(1,"qbuf: wrong memory type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto done;
740 }
741
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300742 dprintk(1,"qbuf: requesting next field\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 field = videobuf_next_field(q);
744 retval = q->ops->buf_prepare(q,buf,field);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300745 if (0 != retval) {
746 dprintk(1,"qbuf: buffer_prepare returned %d\n",retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 list_add_tail(&buf->stream,&q->stream);
751 if (q->streaming) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300752 if (q->irqlock)
753 spin_lock_irqsave(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 q->ops->buf_queue(q,buf);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300755 if (q->irqlock)
756 spin_unlock_irqrestore(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300758 dprintk(1,"qbuf: succeded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 retval = 0;
760
761 done:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200762 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return retval;
764}
765
766int
767videobuf_dqbuf(struct videobuf_queue *q,
768 struct v4l2_buffer *b, int nonblocking)
769{
770 struct videobuf_buffer *buf;
771 int retval;
772
Ingo Molnar3593cab2006-02-07 06:49:14 -0200773 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 retval = -EBUSY;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300775 if (q->reading) {
776 dprintk(1,"dqbuf: Reading running...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 retval = -EINVAL;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300780 if (b->type != q->type) {
781 dprintk(1,"dqbuf: Wrong type.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300783 }
784 if (list_empty(&q->stream)) {
785 dprintk(1,"dqbuf: stream running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
789 retval = videobuf_waiton(buf, nonblocking, 1);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300790 if (retval < 0) {
791 dprintk(1,"dqbuf: waiton returned %d\n",retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 switch (buf->state) {
795 case STATE_ERROR:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300796 dprintk(1,"dqbuf: state is error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 retval = -EIO;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300798 videobuf_dma_sync(q,&buf->dma);
799 buf->state = STATE_IDLE;
800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 case STATE_DONE:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300802 dprintk(1,"dqbuf: state is done\n");
803 videobuf_dma_sync(q,&buf->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 buf->state = STATE_IDLE;
805 break;
806 default:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300807 dprintk(1,"dqbuf: state invalid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 retval = -EINVAL;
809 goto done;
810 }
811 list_del(&buf->stream);
812 memset(b,0,sizeof(*b));
813 videobuf_status(b,buf,q->type);
814
815 done:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200816 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return retval;
818}
819
820int videobuf_streamon(struct videobuf_queue *q)
821{
822 struct videobuf_buffer *buf;
823 struct list_head *list;
Mauro Carvalho Chehab9b89f2d2006-03-15 17:52:14 -0300824 unsigned long flags=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int retval;
826
Ingo Molnar3593cab2006-02-07 06:49:14 -0200827 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 retval = -EBUSY;
829 if (q->reading)
830 goto done;
831 retval = 0;
832 if (q->streaming)
833 goto done;
834 q->streaming = 1;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300835 if (q->irqlock)
836 spin_lock_irqsave(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 list_for_each(list,&q->stream) {
838 buf = list_entry(list, struct videobuf_buffer, stream);
839 if (buf->state == STATE_PREPARED)
840 q->ops->buf_queue(q,buf);
841 }
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300842 if (q->irqlock)
843 spin_unlock_irqrestore(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 done:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200846 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return retval;
848}
849
850int videobuf_streamoff(struct videobuf_queue *q)
851{
852 int retval = -EINVAL;
853
Ingo Molnar3593cab2006-02-07 06:49:14 -0200854 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!q->streaming)
856 goto done;
857 videobuf_queue_cancel(q);
858 q->streaming = 0;
859 retval = 0;
860
861 done:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200862 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return retval;
864}
865
866static ssize_t
867videobuf_read_zerocopy(struct videobuf_queue *q, char __user *data,
868 size_t count, loff_t *ppos)
869{
870 enum v4l2_field field;
Mauro Carvalho Chehab9b89f2d2006-03-15 17:52:14 -0300871 unsigned long flags=0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800872 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800874 /* setup stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 q->read_buf = videobuf_alloc(q->msize);
876 if (NULL == q->read_buf)
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -0800877 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 q->read_buf->memory = V4L2_MEMORY_USERPTR;
880 q->read_buf->baddr = (unsigned long)data;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800881 q->read_buf->bsize = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 field = videobuf_next_field(q);
883 retval = q->ops->buf_prepare(q,q->read_buf,field);
884 if (0 != retval)
885 goto done;
886
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800887 /* start capture & wait */
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300888 if (q->irqlock)
889 spin_lock_irqsave(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 q->ops->buf_queue(q,q->read_buf);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300891 if (q->irqlock)
892 spin_unlock_irqrestore(q->irqlock,flags);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800893 retval = videobuf_waiton(q->read_buf,0,0);
894 if (0 == retval) {
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300895 videobuf_dma_sync(q,&q->read_buf->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (STATE_ERROR == q->read_buf->state)
897 retval = -EIO;
898 else
899 retval = q->read_buf->size;
900 }
901
902 done:
903 /* cleanup */
904 q->ops->buf_release(q,q->read_buf);
905 kfree(q->read_buf);
906 q->read_buf = NULL;
907 return retval;
908}
909
910ssize_t videobuf_read_one(struct videobuf_queue *q,
911 char __user *data, size_t count, loff_t *ppos,
912 int nonblocking)
913{
914 enum v4l2_field field;
Mauro Carvalho Chehab9b89f2d2006-03-15 17:52:14 -0300915 unsigned long flags=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 unsigned size, nbufs, bytes;
917 int retval;
918
Ingo Molnar3593cab2006-02-07 06:49:14 -0200919 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 nbufs = 1; size = 0;
922 q->ops->buf_setup(q,&nbufs,&size);
923 if (NULL == q->read_buf &&
924 count >= size &&
925 !nonblocking) {
926 retval = videobuf_read_zerocopy(q,data,count,ppos);
927 if (retval >= 0 || retval == -EIO)
928 /* ok, all done */
929 goto done;
930 /* fallback to kernel bounce buffer on failures */
931 }
932
933 if (NULL == q->read_buf) {
934 /* need to capture a new frame */
935 retval = -ENOMEM;
936 q->read_buf = videobuf_alloc(q->msize);
Mauro Carvalho Chehab4ae5c2e2006-03-25 15:53:38 -0300937 dprintk(1,"video alloc=0x%p\n", q->read_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (NULL == q->read_buf)
939 goto done;
940 q->read_buf->memory = V4L2_MEMORY_USERPTR;
Michael H. Schimek13c72802005-12-01 00:51:37 -0800941 q->read_buf->bsize = count; /* preferred size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 field = videobuf_next_field(q);
943 retval = q->ops->buf_prepare(q,q->read_buf,field);
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -0800944 if (0 != retval) {
945 kfree (q->read_buf);
946 q->read_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto done;
Nickolay V. Shmyrev50ab5ed2005-12-01 00:51:32 -0800948 }
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300949 if (q->irqlock)
950 spin_lock_irqsave(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 q->ops->buf_queue(q,q->read_buf);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300952 if (q->irqlock)
953 spin_unlock_irqrestore(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 q->read_off = 0;
955 }
956
957 /* wait until capture is done */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800958 retval = videobuf_waiton(q->read_buf, nonblocking, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (0 != retval)
960 goto done;
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300961 videobuf_dma_sync(q,&q->read_buf->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (STATE_ERROR == q->read_buf->state) {
964 /* catch I/O errors */
965 q->ops->buf_release(q,q->read_buf);
966 kfree(q->read_buf);
967 q->read_buf = NULL;
968 retval = -EIO;
969 goto done;
970 }
971
972 /* copy to userspace */
973 bytes = count;
974 if (bytes > q->read_buf->size - q->read_off)
975 bytes = q->read_buf->size - q->read_off;
976 retval = -EFAULT;
977 if (copy_to_user(data, q->read_buf->dma.vmalloc+q->read_off, bytes))
978 goto done;
979
980 retval = bytes;
981 q->read_off += bytes;
982 if (q->read_off == q->read_buf->size) {
983 /* all data copied, cleanup */
984 q->ops->buf_release(q,q->read_buf);
985 kfree(q->read_buf);
986 q->read_buf = NULL;
987 }
988
989 done:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200990 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return retval;
992}
993
994int videobuf_read_start(struct videobuf_queue *q)
995{
996 enum v4l2_field field;
Mauro Carvalho Chehab9b89f2d2006-03-15 17:52:14 -0300997 unsigned long flags=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 int count = 0, size = 0;
999 int err, i;
1000
1001 q->ops->buf_setup(q,&count,&size);
1002 if (count < 2)
1003 count = 2;
1004 if (count > VIDEO_MAX_FRAME)
1005 count = VIDEO_MAX_FRAME;
1006 size = PAGE_ALIGN(size);
1007
1008 err = videobuf_mmap_setup(q, count, size, V4L2_MEMORY_USERPTR);
1009 if (err)
1010 return err;
1011 for (i = 0; i < count; i++) {
1012 field = videobuf_next_field(q);
1013 err = q->ops->buf_prepare(q,q->bufs[i],field);
1014 if (err)
1015 return err;
1016 list_add_tail(&q->bufs[i]->stream, &q->stream);
1017 }
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001018 if (q->irqlock)
1019 spin_lock_irqsave(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 for (i = 0; i < count; i++)
1021 q->ops->buf_queue(q,q->bufs[i]);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001022 if (q->irqlock)
1023 spin_unlock_irqrestore(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 q->reading = 1;
1025 return 0;
1026}
1027
1028void videobuf_read_stop(struct videobuf_queue *q)
1029{
1030 int i;
1031
1032 videobuf_queue_cancel(q);
1033 videobuf_mmap_free(q);
1034 INIT_LIST_HEAD(&q->stream);
1035 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1036 if (NULL == q->bufs[i])
1037 continue;
1038 kfree(q->bufs[i]);
1039 q->bufs[i] = NULL;
1040 }
1041 q->read_buf = NULL;
1042 q->reading = 0;
1043}
1044
1045ssize_t videobuf_read_stream(struct videobuf_queue *q,
1046 char __user *data, size_t count, loff_t *ppos,
1047 int vbihack, int nonblocking)
1048{
1049 unsigned int *fc, bytes;
1050 int err, retval;
Mauro Carvalho Chehab9b89f2d2006-03-15 17:52:14 -03001051 unsigned long flags=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 dprintk(2,"%s\n",__FUNCTION__);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001054 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 retval = -EBUSY;
1056 if (q->streaming)
1057 goto done;
1058 if (!q->reading) {
1059 retval = videobuf_read_start(q);
1060 if (retval < 0)
1061 goto done;
1062 }
1063
1064 retval = 0;
1065 while (count > 0) {
1066 /* get / wait for data */
1067 if (NULL == q->read_buf) {
1068 q->read_buf = list_entry(q->stream.next,
1069 struct videobuf_buffer,
1070 stream);
1071 list_del(&q->read_buf->stream);
1072 q->read_off = 0;
1073 }
1074 err = videobuf_waiton(q->read_buf, nonblocking, 1);
1075 if (err < 0) {
1076 if (0 == retval)
1077 retval = err;
1078 break;
1079 }
1080
1081 if (q->read_buf->state == STATE_DONE) {
1082 if (vbihack) {
1083 /* dirty, undocumented hack -- pass the frame counter
1084 * within the last four bytes of each vbi data block.
1085 * We need that one to maintain backward compatibility
1086 * to all vbi decoding software out there ... */
1087 fc = (unsigned int*)q->read_buf->dma.vmalloc;
1088 fc += (q->read_buf->size>>2) -1;
1089 *fc = q->read_buf->field_count >> 1;
1090 dprintk(1,"vbihack: %d\n",*fc);
1091 }
1092
1093 /* copy stuff */
1094 bytes = count;
1095 if (bytes > q->read_buf->size - q->read_off)
1096 bytes = q->read_buf->size - q->read_off;
1097 if (copy_to_user(data + retval,
1098 q->read_buf->dma.vmalloc + q->read_off,
1099 bytes)) {
1100 if (0 == retval)
1101 retval = -EFAULT;
1102 break;
1103 }
1104 count -= bytes;
1105 retval += bytes;
1106 q->read_off += bytes;
1107 } else {
1108 /* some error */
1109 q->read_off = q->read_buf->size;
1110 if (0 == retval)
1111 retval = -EIO;
1112 }
1113
1114 /* requeue buffer when done with copying */
1115 if (q->read_off == q->read_buf->size) {
1116 list_add_tail(&q->read_buf->stream,
1117 &q->stream);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001118 if (q->irqlock)
1119 spin_lock_irqsave(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 q->ops->buf_queue(q,q->read_buf);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001121 if (q->irqlock)
1122 spin_unlock_irqrestore(q->irqlock,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 q->read_buf = NULL;
1124 }
1125 if (retval < 0)
1126 break;
1127 }
1128
1129 done:
Ingo Molnar3593cab2006-02-07 06:49:14 -02001130 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return retval;
1132}
1133
1134unsigned int videobuf_poll_stream(struct file *file,
1135 struct videobuf_queue *q,
1136 poll_table *wait)
1137{
1138 struct videobuf_buffer *buf = NULL;
1139 unsigned int rc = 0;
1140
Ingo Molnar3593cab2006-02-07 06:49:14 -02001141 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (q->streaming) {
1143 if (!list_empty(&q->stream))
1144 buf = list_entry(q->stream.next,
1145 struct videobuf_buffer, stream);
1146 } else {
1147 if (!q->reading)
1148 videobuf_read_start(q);
1149 if (!q->reading) {
1150 rc = POLLERR;
1151 } else if (NULL == q->read_buf) {
1152 q->read_buf = list_entry(q->stream.next,
1153 struct videobuf_buffer,
1154 stream);
1155 list_del(&q->read_buf->stream);
1156 q->read_off = 0;
1157 }
1158 buf = q->read_buf;
1159 }
1160 if (!buf)
1161 rc = POLLERR;
1162
1163 if (0 == rc) {
1164 poll_wait(file, &buf->done, wait);
1165 if (buf->state == STATE_DONE ||
1166 buf->state == STATE_ERROR)
1167 rc = POLLIN|POLLRDNORM;
1168 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001169 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return rc;
1171}
1172
1173/* --------------------------------------------------------------------- */
1174
1175static void
1176videobuf_vm_open(struct vm_area_struct *vma)
1177{
1178 struct videobuf_mapping *map = vma->vm_private_data;
1179
1180 dprintk(2,"vm_open %p [count=%d,vma=%08lx-%08lx]\n",map,
1181 map->count,vma->vm_start,vma->vm_end);
1182 map->count++;
1183}
1184
1185static void
1186videobuf_vm_close(struct vm_area_struct *vma)
1187{
1188 struct videobuf_mapping *map = vma->vm_private_data;
1189 struct videobuf_queue *q = map->q;
1190 int i;
1191
1192 dprintk(2,"vm_close %p [count=%d,vma=%08lx-%08lx]\n",map,
1193 map->count,vma->vm_start,vma->vm_end);
1194
1195 map->count--;
1196 if (0 == map->count) {
1197 dprintk(1,"munmap %p q=%p\n",map,q);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001198 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1200 if (NULL == q->bufs[i])
1201 continue;
1202 if (q->bufs[i])
1203 ;
1204 if (q->bufs[i]->map != map)
1205 continue;
1206 q->bufs[i]->map = NULL;
1207 q->bufs[i]->baddr = 0;
1208 q->ops->buf_release(q,q->bufs[i]);
1209 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02001210 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 kfree(map);
1212 }
1213 return;
1214}
1215
1216/*
1217 * Get a anonymous page for the mapping. Make sure we can DMA to that
1218 * memory location with 32bit PCI devices (i.e. don't use highmem for
1219 * now ...). Bounce buffers don't work very well for the data rates
1220 * video capture has.
1221 */
1222static struct page*
1223videobuf_vm_nopage(struct vm_area_struct *vma, unsigned long vaddr,
1224 int *type)
1225{
1226 struct page *page;
1227
1228 dprintk(3,"nopage: fault @ %08lx [vma %08lx-%08lx]\n",
1229 vaddr,vma->vm_start,vma->vm_end);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001230 if (vaddr > vma->vm_end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return NOPAGE_SIGBUS;
Gerd Hoffmann10329b92007-01-12 21:26:32 -03001232 page = alloc_page(GFP_USER | __GFP_DMA32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (!page)
1234 return NOPAGE_OOM;
1235 clear_user_page(page_address(page), vaddr, page);
1236 if (type)
1237 *type = VM_FAULT_MINOR;
1238 return page;
1239}
1240
1241static struct vm_operations_struct videobuf_vm_ops =
1242{
1243 .open = videobuf_vm_open,
1244 .close = videobuf_vm_close,
1245 .nopage = videobuf_vm_nopage,
1246};
1247
1248int videobuf_mmap_setup(struct videobuf_queue *q,
1249 unsigned int bcount, unsigned int bsize,
1250 enum v4l2_memory memory)
1251{
1252 unsigned int i;
1253 int err;
1254
1255 err = videobuf_mmap_free(q);
1256 if (0 != err)
1257 return err;
1258
1259 for (i = 0; i < bcount; i++) {
1260 q->bufs[i] = videobuf_alloc(q->msize);
1261 q->bufs[i]->i = i;
1262 q->bufs[i]->input = UNSET;
1263 q->bufs[i]->memory = memory;
1264 q->bufs[i]->bsize = bsize;
1265 switch (memory) {
1266 case V4L2_MEMORY_MMAP:
1267 q->bufs[i]->boff = bsize * i;
1268 break;
1269 case V4L2_MEMORY_USERPTR:
1270 case V4L2_MEMORY_OVERLAY:
1271 /* nothing */
1272 break;
1273 }
1274 }
1275 dprintk(1,"mmap setup: %d buffers, %d bytes each\n",
1276 bcount,bsize);
1277 return 0;
1278}
1279
1280int videobuf_mmap_free(struct videobuf_queue *q)
1281{
1282 int i;
1283
1284 for (i = 0; i < VIDEO_MAX_FRAME; i++)
1285 if (q->bufs[i] && q->bufs[i]->map)
1286 return -EBUSY;
1287 for (i = 0; i < VIDEO_MAX_FRAME; i++) {
1288 if (NULL == q->bufs[i])
1289 continue;
1290 q->ops->buf_release(q,q->bufs[i]);
1291 kfree(q->bufs[i]);
1292 q->bufs[i] = NULL;
1293 }
1294 return 0;
1295}
1296
1297int videobuf_mmap_mapper(struct videobuf_queue *q,
1298 struct vm_area_struct *vma)
1299{
1300 struct videobuf_mapping *map;
1301 unsigned int first,last,size,i;
1302 int retval;
1303
Ingo Molnar3593cab2006-02-07 06:49:14 -02001304 mutex_lock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 retval = -EINVAL;
1306 if (!(vma->vm_flags & VM_WRITE)) {
1307 dprintk(1,"mmap app bug: PROT_WRITE please\n");
1308 goto done;
1309 }
1310 if (!(vma->vm_flags & VM_SHARED)) {
1311 dprintk(1,"mmap app bug: MAP_SHARED please\n");
1312 goto done;
1313 }
1314
1315 /* look for first buffer to map */
1316 for (first = 0; first < VIDEO_MAX_FRAME; first++) {
1317 if (NULL == q->bufs[first])
1318 continue;
1319 if (V4L2_MEMORY_MMAP != q->bufs[first]->memory)
1320 continue;
1321 if (q->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
1322 break;
1323 }
1324 if (VIDEO_MAX_FRAME == first) {
1325 dprintk(1,"mmap app bug: offset invalid [offset=0x%lx]\n",
1326 (vma->vm_pgoff << PAGE_SHIFT));
1327 goto done;
1328 }
1329
1330 /* look for last buffer to map */
1331 for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
1332 if (NULL == q->bufs[last])
1333 continue;
1334 if (V4L2_MEMORY_MMAP != q->bufs[last]->memory)
1335 continue;
1336 if (q->bufs[last]->map) {
1337 retval = -EBUSY;
1338 goto done;
1339 }
1340 size += q->bufs[last]->bsize;
1341 if (size == (vma->vm_end - vma->vm_start))
1342 break;
1343 }
1344 if (VIDEO_MAX_FRAME == last) {
1345 dprintk(1,"mmap app bug: size invalid [size=0x%lx]\n",
1346 (vma->vm_end - vma->vm_start));
1347 goto done;
1348 }
1349
1350 /* create mapping + update buffer list */
1351 retval = -ENOMEM;
1352 map = kmalloc(sizeof(struct videobuf_mapping),GFP_KERNEL);
1353 if (NULL == map)
1354 goto done;
1355 for (size = 0, i = first; i <= last; size += q->bufs[i++]->bsize) {
1356 q->bufs[i]->map = map;
1357 q->bufs[i]->baddr = vma->vm_start + size;
1358 }
1359 map->count = 1;
1360 map->start = vma->vm_start;
1361 map->end = vma->vm_end;
1362 map->q = q;
1363 vma->vm_ops = &videobuf_vm_ops;
1364 vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED;
1365 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */
1366 vma->vm_private_data = map;
1367 dprintk(1,"mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n",
1368 map,q,vma->vm_start,vma->vm_end,vma->vm_pgoff,first,last);
1369 retval = 0;
1370
1371 done:
Ingo Molnar3593cab2006-02-07 06:49:14 -02001372 mutex_unlock(&q->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return retval;
1374}
1375
1376/* --------------------------------------------------------------------- */
1377
1378EXPORT_SYMBOL_GPL(videobuf_vmalloc_to_sg);
1379
1380EXPORT_SYMBOL_GPL(videobuf_dma_init);
1381EXPORT_SYMBOL_GPL(videobuf_dma_init_user);
1382EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel);
1383EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001384EXPORT_SYMBOL_GPL(videobuf_dma_map);
1385EXPORT_SYMBOL_GPL(videobuf_dma_sync);
1386EXPORT_SYMBOL_GPL(videobuf_dma_unmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387EXPORT_SYMBOL_GPL(videobuf_dma_free);
1388
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -03001389EXPORT_SYMBOL_GPL(videobuf_pci_dma_map);
1390EXPORT_SYMBOL_GPL(videobuf_pci_dma_unmap);
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392EXPORT_SYMBOL_GPL(videobuf_alloc);
1393EXPORT_SYMBOL_GPL(videobuf_waiton);
1394EXPORT_SYMBOL_GPL(videobuf_iolock);
1395
1396EXPORT_SYMBOL_GPL(videobuf_queue_init);
1397EXPORT_SYMBOL_GPL(videobuf_queue_cancel);
1398EXPORT_SYMBOL_GPL(videobuf_queue_is_busy);
1399
1400EXPORT_SYMBOL_GPL(videobuf_next_field);
1401EXPORT_SYMBOL_GPL(videobuf_status);
1402EXPORT_SYMBOL_GPL(videobuf_reqbufs);
1403EXPORT_SYMBOL_GPL(videobuf_querybuf);
1404EXPORT_SYMBOL_GPL(videobuf_qbuf);
1405EXPORT_SYMBOL_GPL(videobuf_dqbuf);
1406EXPORT_SYMBOL_GPL(videobuf_streamon);
1407EXPORT_SYMBOL_GPL(videobuf_streamoff);
1408
1409EXPORT_SYMBOL_GPL(videobuf_read_start);
1410EXPORT_SYMBOL_GPL(videobuf_read_stop);
1411EXPORT_SYMBOL_GPL(videobuf_read_stream);
1412EXPORT_SYMBOL_GPL(videobuf_read_one);
1413EXPORT_SYMBOL_GPL(videobuf_poll_stream);
1414
1415EXPORT_SYMBOL_GPL(videobuf_mmap_setup);
1416EXPORT_SYMBOL_GPL(videobuf_mmap_free);
1417EXPORT_SYMBOL_GPL(videobuf_mmap_mapper);
1418
1419/*
1420 * Local variables:
1421 * c-basic-offset: 8
1422 * End:
1423 */