aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2011-02-12 16:05:06 -0500
committerMauro Carvalho Chehab <mchehab@redhat.com>2011-03-22 03:53:47 -0400
commitad614acb7eca429891313f57bafbe5b3924a5aae (patch)
tree6bafd116bd1d3ca1770932cb6c7afc532b84ca29
parent448de7e7850b804bc8f5efa60305a83333c257bf (diff)
[media] omap3isp: Video devices and buffers queue
The OMAP3 ISP video devices and buffers queue modules implement the V4L2 API on all the ISP video nodes. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Sakari Ailus <sakari.ailus@iki.fi> Signed-off-by: David Cohen <dacohen@gmail.com> Signed-off-by: Stanimir Varbanov <svarbanov@mm-sol.com> Signed-off-by: Vimarsh Zutshi <vimarsh.zutshi@gmail.com> Signed-off-by: Tuukka Toivonen <tuukkat76@gmail.com> Signed-off-by: Sergio Aguirre <saaguirre@ti.com> Signed-off-by: Antti Koskipaa <akoskipa@gmail.com> Signed-off-by: Ivan T. Ivanov <iivanov@mm-sol.com> Signed-off-by: RaniSuneela <r-m@ti.com> Signed-off-by: Atanas Filipov <afilipov@mm-sol.com> Signed-off-by: Gjorgji Rosikopulos <grosikopulos@mm-sol.com> Signed-off-by: Hiroshi DOYU <Hiroshi.DOYU@nokia.com> Signed-off-by: Nayden Kanchev <nkanchev@mm-sol.com> Signed-off-by: Phil Carmody <ext-phil.2.carmody@nokia.com> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com> Signed-off-by: Dominic Curran <dcurran@ti.com> Signed-off-by: Ilkka Myllyperkio <ilkka.myllyperkio@sofica.fi> Signed-off-by: Pallavi Kulkarni <p-kulkarni@ti.com> Signed-off-by: Vaibhav Hiremath <hvaibhav@ti.com> Acked-by: Hans Verkuil <hverkuil@xs4all.nl> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
-rw-r--r--drivers/media/video/omap3isp/ispqueue.c1153
-rw-r--r--drivers/media/video/omap3isp/ispqueue.h187
-rw-r--r--drivers/media/video/omap3isp/ispvideo.c1264
-rw-r--r--drivers/media/video/omap3isp/ispvideo.h202
4 files changed, 2806 insertions, 0 deletions
diff --git a/drivers/media/video/omap3isp/ispqueue.c b/drivers/media/video/omap3isp/ispqueue.c
new file mode 100644
index 000000000000..8fddc5806b0d
--- /dev/null
+++ b/drivers/media/video/omap3isp/ispqueue.c
@@ -0,0 +1,1153 @@
1/*
2 * ispqueue.c
3 *
4 * TI OMAP3 ISP - Video buffers queue handling
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26#include <asm/cacheflush.h>
27#include <linux/dma-mapping.h>
28#include <linux/mm.h>
29#include <linux/pagemap.h>
30#include <linux/poll.h>
31#include <linux/scatterlist.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
34#include <linux/vmalloc.h>
35
36#include "ispqueue.h"
37
38/* -----------------------------------------------------------------------------
39 * Video buffers management
40 */
41
42/*
43 * isp_video_buffer_cache_sync - Keep the buffers coherent between CPU and ISP
44 *
45 * The typical operation required here is Cache Invalidation across
46 * the (user space) buffer address range. And this _must_ be done
47 * at QBUF stage (and *only* at QBUF).
48 *
49 * We try to use optimal cache invalidation function:
50 * - dmac_map_area:
51 * - used when the number of pages are _low_.
52 * - it becomes quite slow as the number of pages increase.
53 * - for 648x492 viewfinder (150 pages) it takes 1.3 ms.
54 * - for 5 Mpix buffer (2491 pages) it takes between 25-50 ms.
55 *
56 * - flush_cache_all:
57 * - used when the number of pages are _high_.
58 * - time taken in the range of 500-900 us.
59 * - has a higher penalty but, as whole dcache + icache is invalidated
60 */
61/*
62 * FIXME: dmac_inv_range crashes randomly on the user space buffer
63 * address. Fall back to flush_cache_all for now.
64 */
65#define ISP_CACHE_FLUSH_PAGES_MAX 0
66
67static void isp_video_buffer_cache_sync(struct isp_video_buffer *buf)
68{
69 if (buf->skip_cache)
70 return;
71
72 if (buf->vbuf.m.userptr == 0 || buf->npages == 0 ||
73 buf->npages > ISP_CACHE_FLUSH_PAGES_MAX)
74 flush_cache_all();
75 else {
76 dmac_map_area((void *)buf->vbuf.m.userptr, buf->vbuf.length,
77 DMA_FROM_DEVICE);
78 outer_inv_range(buf->vbuf.m.userptr,
79 buf->vbuf.m.userptr + buf->vbuf.length);
80 }
81}
82
83/*
84 * isp_video_buffer_lock_vma - Prevent VMAs from being unmapped
85 *
86 * Lock the VMAs underlying the given buffer into memory. This avoids the
87 * userspace buffer mapping from being swapped out, making VIPT cache handling
88 * easier.
89 *
90 * Note that the pages will not be freed as the buffers have been locked to
91 * memory using by a call to get_user_pages(), but the userspace mapping could
92 * still disappear if the VMAs are not locked. This is caused by the memory
93 * management code trying to be as lock-less as possible, which results in the
94 * userspace mapping manager not finding out that the pages are locked under
95 * some conditions.
96 */
97static int isp_video_buffer_lock_vma(struct isp_video_buffer *buf, int lock)
98{
99 struct vm_area_struct *vma;
100 unsigned long start;
101 unsigned long end;
102 int ret = 0;
103
104 if (buf->vbuf.memory == V4L2_MEMORY_MMAP)
105 return 0;
106
107 /* We can be called from workqueue context if the current task dies to
108 * unlock the VMAs. In that case there's no current memory management
109 * context so unlocking can't be performed, but the VMAs have been or
110 * are getting destroyed anyway so it doesn't really matter.
111 */
112 if (!current || !current->mm)
113 return lock ? -EINVAL : 0;
114
115 start = buf->vbuf.m.userptr;
116 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
117
118 down_write(&current->mm->mmap_sem);
119 spin_lock(&current->mm->page_table_lock);
120
121 do {
122 vma = find_vma(current->mm, start);
123 if (vma == NULL) {
124 ret = -EFAULT;
125 goto out;
126 }
127
128 if (lock)
129 vma->vm_flags |= VM_LOCKED;
130 else
131 vma->vm_flags &= ~VM_LOCKED;
132
133 start = vma->vm_end + 1;
134 } while (vma->vm_end < end);
135
136 if (lock)
137 buf->vm_flags |= VM_LOCKED;
138 else
139 buf->vm_flags &= ~VM_LOCKED;
140
141out:
142 spin_unlock(&current->mm->page_table_lock);
143 up_write(&current->mm->mmap_sem);
144 return ret;
145}
146
147/*
148 * isp_video_buffer_sglist_kernel - Build a scatter list for a vmalloc'ed buffer
149 *
150 * Iterate over the vmalloc'ed area and create a scatter list entry for every
151 * page.
152 */
153static int isp_video_buffer_sglist_kernel(struct isp_video_buffer *buf)
154{
155 struct scatterlist *sglist;
156 unsigned int npages;
157 unsigned int i;
158 void *addr;
159
160 addr = buf->vaddr;
161 npages = PAGE_ALIGN(buf->vbuf.length) >> PAGE_SHIFT;
162
163 sglist = vmalloc(npages * sizeof(*sglist));
164 if (sglist == NULL)
165 return -ENOMEM;
166
167 sg_init_table(sglist, npages);
168
169 for (i = 0; i < npages; ++i, addr += PAGE_SIZE) {
170 struct page *page = vmalloc_to_page(addr);
171
172 if (page == NULL || PageHighMem(page)) {
173 vfree(sglist);
174 return -EINVAL;
175 }
176
177 sg_set_page(&sglist[i], page, PAGE_SIZE, 0);
178 }
179
180 buf->sglen = npages;
181 buf->sglist = sglist;
182
183 return 0;
184}
185
186/*
187 * isp_video_buffer_sglist_user - Build a scatter list for a userspace buffer
188 *
189 * Walk the buffer pages list and create a 1:1 mapping to a scatter list.
190 */
191static int isp_video_buffer_sglist_user(struct isp_video_buffer *buf)
192{
193 struct scatterlist *sglist;
194 unsigned int offset = buf->offset;
195 unsigned int i;
196
197 sglist = vmalloc(buf->npages * sizeof(*sglist));
198 if (sglist == NULL)
199 return -ENOMEM;
200
201 sg_init_table(sglist, buf->npages);
202
203 for (i = 0; i < buf->npages; ++i) {
204 if (PageHighMem(buf->pages[i])) {
205 vfree(sglist);
206 return -EINVAL;
207 }
208
209 sg_set_page(&sglist[i], buf->pages[i], PAGE_SIZE - offset,
210 offset);
211 offset = 0;
212 }
213
214 buf->sglen = buf->npages;
215 buf->sglist = sglist;
216
217 return 0;
218}
219
220/*
221 * isp_video_buffer_sglist_pfnmap - Build a scatter list for a VM_PFNMAP buffer
222 *
223 * Create a scatter list of physically contiguous pages starting at the buffer
224 * memory physical address.
225 */
226static int isp_video_buffer_sglist_pfnmap(struct isp_video_buffer *buf)
227{
228 struct scatterlist *sglist;
229 unsigned int offset = buf->offset;
230 unsigned long pfn = buf->paddr >> PAGE_SHIFT;
231 unsigned int i;
232
233 sglist = vmalloc(buf->npages * sizeof(*sglist));
234 if (sglist == NULL)
235 return -ENOMEM;
236
237 sg_init_table(sglist, buf->npages);
238
239 for (i = 0; i < buf->npages; ++i, ++pfn) {
240 sg_set_page(&sglist[i], pfn_to_page(pfn), PAGE_SIZE - offset,
241 offset);
242 /* PFNMAP buffers will not get DMA-mapped, set the DMA address
243 * manually.
244 */
245 sg_dma_address(&sglist[i]) = (pfn << PAGE_SHIFT) + offset;
246 offset = 0;
247 }
248
249 buf->sglen = buf->npages;
250 buf->sglist = sglist;
251
252 return 0;
253}
254
255/*
256 * isp_video_buffer_cleanup - Release pages for a userspace VMA.
257 *
258 * Release pages locked by a call isp_video_buffer_prepare_user and free the
259 * pages table.
260 */
261static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
262{
263 enum dma_data_direction direction;
264 unsigned int i;
265
266 if (buf->queue->ops->buffer_cleanup)
267 buf->queue->ops->buffer_cleanup(buf);
268
269 if (!(buf->vm_flags & VM_PFNMAP)) {
270 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
271 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
272 dma_unmap_sg(buf->queue->dev, buf->sglist, buf->sglen,
273 direction);
274 }
275
276 vfree(buf->sglist);
277 buf->sglist = NULL;
278 buf->sglen = 0;
279
280 if (buf->pages != NULL) {
281 isp_video_buffer_lock_vma(buf, 0);
282
283 for (i = 0; i < buf->npages; ++i)
284 page_cache_release(buf->pages[i]);
285
286 vfree(buf->pages);
287 buf->pages = NULL;
288 }
289
290 buf->npages = 0;
291 buf->skip_cache = false;
292}
293
294/*
295 * isp_video_buffer_prepare_user - Pin userspace VMA pages to memory.
296 *
297 * This function creates a list of pages for a userspace VMA. The number of
298 * pages is first computed based on the buffer size, and pages are then
299 * retrieved by a call to get_user_pages.
300 *
301 * Pages are pinned to memory by get_user_pages, making them available for DMA
302 * transfers. However, due to memory management optimization, it seems the
303 * get_user_pages doesn't guarantee that the pinned pages will not be written
304 * to swap and removed from the userspace mapping(s). When this happens, a page
305 * fault can be generated when accessing those unmapped pages.
306 *
307 * If the fault is triggered by a page table walk caused by VIPT cache
308 * management operations, the page fault handler might oops if the MM semaphore
309 * is held, as it can't handle kernel page faults in that case. To fix that, a
310 * fixup entry needs to be added to the cache management code, or the userspace
311 * VMA must be locked to avoid removing pages from the userspace mapping in the
312 * first place.
313 *
314 * If the number of pages retrieved is smaller than the number required by the
315 * buffer size, the function returns -EFAULT.
316 */
317static int isp_video_buffer_prepare_user(struct isp_video_buffer *buf)
318{
319 unsigned long data;
320 unsigned int first;
321 unsigned int last;
322 int ret;
323
324 data = buf->vbuf.m.userptr;
325 first = (data & PAGE_MASK) >> PAGE_SHIFT;
326 last = ((data + buf->vbuf.length - 1) & PAGE_MASK) >> PAGE_SHIFT;
327
328 buf->offset = data & ~PAGE_MASK;
329 buf->npages = last - first + 1;
330 buf->pages = vmalloc(buf->npages * sizeof(buf->pages[0]));
331 if (buf->pages == NULL)
332 return -ENOMEM;
333
334 down_read(&current->mm->mmap_sem);
335 ret = get_user_pages(current, current->mm, data & PAGE_MASK,
336 buf->npages,
337 buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
338 buf->pages, NULL);
339 up_read(&current->mm->mmap_sem);
340
341 if (ret != buf->npages) {
342 buf->npages = ret;
343 isp_video_buffer_cleanup(buf);
344 return -EFAULT;
345 }
346
347 ret = isp_video_buffer_lock_vma(buf, 1);
348 if (ret < 0)
349 isp_video_buffer_cleanup(buf);
350
351 return ret;
352}
353
354/*
355 * isp_video_buffer_prepare_pfnmap - Validate a VM_PFNMAP userspace buffer
356 *
357 * Userspace VM_PFNMAP buffers are supported only if they are contiguous in
358 * memory and if they span a single VMA.
359 *
360 * Return 0 if the buffer is valid, or -EFAULT otherwise.
361 */
362static int isp_video_buffer_prepare_pfnmap(struct isp_video_buffer *buf)
363{
364 struct vm_area_struct *vma;
365 unsigned long prev_pfn;
366 unsigned long this_pfn;
367 unsigned long start;
368 unsigned long end;
369 dma_addr_t pa;
370 int ret = -EFAULT;
371
372 start = buf->vbuf.m.userptr;
373 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
374
375 buf->offset = start & ~PAGE_MASK;
376 buf->npages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
377 buf->pages = NULL;
378
379 down_read(&current->mm->mmap_sem);
380 vma = find_vma(current->mm, start);
381 if (vma == NULL || vma->vm_end < end)
382 goto done;
383
384 for (prev_pfn = 0; start <= end; start += PAGE_SIZE) {
385 ret = follow_pfn(vma, start, &this_pfn);
386 if (ret)
387 goto done;
388
389 if (prev_pfn == 0)
390 pa = this_pfn << PAGE_SHIFT;
391 else if (this_pfn != prev_pfn + 1) {
392 ret = -EFAULT;
393 goto done;
394 }
395
396 prev_pfn = this_pfn;
397 }
398
399 buf->paddr = pa + buf->offset;
400 ret = 0;
401
402done:
403 up_read(&current->mm->mmap_sem);
404 return ret;
405}
406
407/*
408 * isp_video_buffer_prepare_vm_flags - Get VMA flags for a userspace address
409 *
410 * This function locates the VMAs for the buffer's userspace address and checks
411 * that their flags match. The onlflag that we need to care for at the moment is
412 * VM_PFNMAP.
413 *
414 * The buffer vm_flags field is set to the first VMA flags.
415 *
416 * Return -EFAULT if no VMA can be found for part of the buffer, or if the VMAs
417 * have incompatible flags.
418 */
419static int isp_video_buffer_prepare_vm_flags(struct isp_video_buffer *buf)
420{
421 struct vm_area_struct *vma;
422 pgprot_t vm_page_prot;
423 unsigned long start;
424 unsigned long end;
425 int ret = -EFAULT;
426
427 start = buf->vbuf.m.userptr;
428 end = buf->vbuf.m.userptr + buf->vbuf.length - 1;
429
430 down_read(&current->mm->mmap_sem);
431
432 do {
433 vma = find_vma(current->mm, start);
434 if (vma == NULL)
435 goto done;
436
437 if (start == buf->vbuf.m.userptr) {
438 buf->vm_flags = vma->vm_flags;
439 vm_page_prot = vma->vm_page_prot;
440 }
441
442 if ((buf->vm_flags ^ vma->vm_flags) & VM_PFNMAP)
443 goto done;
444
445 if (vm_page_prot != vma->vm_page_prot)
446 goto done;
447
448 start = vma->vm_end + 1;
449 } while (vma->vm_end < end);
450
451 /* Skip cache management to enhance performances for non-cached or
452 * write-combining buffers.
453 */
454 if (vm_page_prot == pgprot_noncached(vm_page_prot) ||
455 vm_page_prot == pgprot_writecombine(vm_page_prot))
456 buf->skip_cache = true;
457
458 ret = 0;
459
460done:
461 up_read(&current->mm->mmap_sem);
462 return ret;
463}
464
465/*
466 * isp_video_buffer_prepare - Make a buffer ready for operation
467 *
468 * Preparing a buffer involves:
469 *
470 * - validating VMAs (userspace buffers only)
471 * - locking pages and VMAs into memory (userspace buffers only)
472 * - building page and scatter-gather lists
473 * - mapping buffers for DMA operation
474 * - performing driver-specific preparation
475 *
476 * The function must be called in userspace context with a valid mm context
477 * (this excludes cleanup paths such as sys_close when the userspace process
478 * segfaults).
479 */
480static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
481{
482 enum dma_data_direction direction;
483 int ret;
484
485 switch (buf->vbuf.memory) {
486 case V4L2_MEMORY_MMAP:
487 ret = isp_video_buffer_sglist_kernel(buf);
488 break;
489
490 case V4L2_MEMORY_USERPTR:
491 ret = isp_video_buffer_prepare_vm_flags(buf);
492 if (ret < 0)
493 return ret;
494
495 if (buf->vm_flags & VM_PFNMAP) {
496 ret = isp_video_buffer_prepare_pfnmap(buf);
497 if (ret < 0)
498 return ret;
499
500 ret = isp_video_buffer_sglist_pfnmap(buf);
501 } else {
502 ret = isp_video_buffer_prepare_user(buf);
503 if (ret < 0)
504 return ret;
505
506 ret = isp_video_buffer_sglist_user(buf);
507 }
508 break;
509
510 default:
511 return -EINVAL;
512 }
513
514 if (ret < 0)
515 goto done;
516
517 if (!(buf->vm_flags & VM_PFNMAP)) {
518 direction = buf->vbuf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE
519 ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
520 ret = dma_map_sg(buf->queue->dev, buf->sglist, buf->sglen,
521 direction);
522 if (ret != buf->sglen) {
523 ret = -EFAULT;
524 goto done;
525 }
526 }
527
528 if (buf->queue->ops->buffer_prepare)
529 ret = buf->queue->ops->buffer_prepare(buf);
530
531done:
532 if (ret < 0) {
533 isp_video_buffer_cleanup(buf);
534 return ret;
535 }
536
537 return ret;
538}
539
540/*
541 * isp_video_queue_query - Query the status of a given buffer
542 *
543 * Locking: must be called with the queue lock held.
544 */
545static void isp_video_buffer_query(struct isp_video_buffer *buf,
546 struct v4l2_buffer *vbuf)
547{
548 memcpy(vbuf, &buf->vbuf, sizeof(*vbuf));
549
550 if (buf->vma_use_count)
551 vbuf->flags |= V4L2_BUF_FLAG_MAPPED;
552
553 switch (buf->state) {
554 case ISP_BUF_STATE_ERROR:
555 vbuf->flags |= V4L2_BUF_FLAG_ERROR;
556 case ISP_BUF_STATE_DONE:
557 vbuf->flags |= V4L2_BUF_FLAG_DONE;
558 case ISP_BUF_STATE_QUEUED:
559 case ISP_BUF_STATE_ACTIVE:
560 vbuf->flags |= V4L2_BUF_FLAG_QUEUED;
561 break;
562 case ISP_BUF_STATE_IDLE:
563 default:
564 break;
565 }
566}
567
568/*
569 * isp_video_buffer_wait - Wait for a buffer to be ready
570 *
571 * In non-blocking mode, return immediately with 0 if the buffer is ready or
572 * -EAGAIN if the buffer is in the QUEUED or ACTIVE state.
573 *
574 * In blocking mode, wait (interruptibly but with no timeout) on the buffer wait
575 * queue using the same condition.
576 */
577static int isp_video_buffer_wait(struct isp_video_buffer *buf, int nonblocking)
578{
579 if (nonblocking) {
580 return (buf->state != ISP_BUF_STATE_QUEUED &&
581 buf->state != ISP_BUF_STATE_ACTIVE)
582 ? 0 : -EAGAIN;
583 }
584
585 return wait_event_interruptible(buf->wait,
586 buf->state != ISP_BUF_STATE_QUEUED &&
587 buf->state != ISP_BUF_STATE_ACTIVE);
588}
589
590/* -----------------------------------------------------------------------------
591 * Queue management
592 */
593
594/*
595 * isp_video_queue_free - Free video buffers memory
596 *
597 * Buffers can only be freed if the queue isn't streaming and if no buffer is
598 * mapped to userspace. Return -EBUSY if those conditions aren't statisfied.
599 *
600 * This function must be called with the queue lock held.
601 */
602static int isp_video_queue_free(struct isp_video_queue *queue)
603{
604 unsigned int i;
605
606 if (queue->streaming)
607 return -EBUSY;
608
609 for (i = 0; i < queue->count; ++i) {
610 if (queue->buffers[i]->vma_use_count != 0)
611 return -EBUSY;
612 }
613
614 for (i = 0; i < queue->count; ++i) {
615 struct isp_video_buffer *buf = queue->buffers[i];
616
617 isp_video_buffer_cleanup(buf);
618
619 vfree(buf->vaddr);
620 buf->vaddr = NULL;
621
622 kfree(buf);
623 queue->buffers[i] = NULL;
624 }
625
626 INIT_LIST_HEAD(&queue->queue);
627 queue->count = 0;
628 return 0;
629}
630
631/*
632 * isp_video_queue_alloc - Allocate video buffers memory
633 *
634 * This function must be called with the queue lock held.
635 */
636static int isp_video_queue_alloc(struct isp_video_queue *queue,
637 unsigned int nbuffers,
638 unsigned int size, enum v4l2_memory memory)
639{
640 struct isp_video_buffer *buf;
641 unsigned int i;
642 void *mem;
643 int ret;
644
645 /* Start by freeing the buffers. */
646 ret = isp_video_queue_free(queue);
647 if (ret < 0)
648 return ret;
649
650 /* Bail out of no buffers should be allocated. */
651 if (nbuffers == 0)
652 return 0;
653
654 /* Initialize the allocated buffers. */
655 for (i = 0; i < nbuffers; ++i) {
656 buf = kzalloc(queue->bufsize, GFP_KERNEL);
657 if (buf == NULL)
658 break;
659
660 if (memory == V4L2_MEMORY_MMAP) {
661 /* Allocate video buffers memory for mmap mode. Align
662 * the size to the page size.
663 */
664 mem = vmalloc_32_user(PAGE_ALIGN(size));
665 if (mem == NULL) {
666 kfree(buf);
667 break;
668 }
669
670 buf->vbuf.m.offset = i * PAGE_ALIGN(size);
671 buf->vaddr = mem;
672 }
673
674 buf->vbuf.index = i;
675 buf->vbuf.length = size;
676 buf->vbuf.type = queue->type;
677 buf->vbuf.field = V4L2_FIELD_NONE;
678 buf->vbuf.memory = memory;
679
680 buf->queue = queue;
681 init_waitqueue_head(&buf->wait);
682
683 queue->buffers[i] = buf;
684 }
685
686 if (i == 0)
687 return -ENOMEM;
688
689 queue->count = i;
690 return nbuffers;
691}
692
693/**
694 * omap3isp_video_queue_cleanup - Clean up the video buffers queue
695 * @queue: Video buffers queue
696 *
697 * Free all allocated resources and clean up the video buffers queue. The queue
698 * must not be busy (no ongoing video stream) and buffers must have been
699 * unmapped.
700 *
701 * Return 0 on success or -EBUSY if the queue is busy or buffers haven't been
702 * unmapped.
703 */
704int omap3isp_video_queue_cleanup(struct isp_video_queue *queue)
705{
706 return isp_video_queue_free(queue);
707}
708
709/**
710 * omap3isp_video_queue_init - Initialize the video buffers queue
711 * @queue: Video buffers queue
712 * @type: V4L2 buffer type (capture or output)
713 * @ops: Driver-specific queue operations
714 * @dev: Device used for DMA operations
715 * @bufsize: Size of the driver-specific buffer structure
716 *
717 * Initialize the video buffers queue with the supplied parameters.
718 *
719 * The queue type must be one of V4L2_BUF_TYPE_VIDEO_CAPTURE or
720 * V4L2_BUF_TYPE_VIDEO_OUTPUT. Other buffer types are not supported yet.
721 *
722 * Buffer objects will be allocated using the given buffer size to allow room
723 * for driver-specific fields. Driver-specific buffer structures must start
724 * with a struct isp_video_buffer field. Drivers with no driver-specific buffer
725 * structure must pass the size of the isp_video_buffer structure in the bufsize
726 * parameter.
727 *
728 * Return 0 on success.
729 */
730int omap3isp_video_queue_init(struct isp_video_queue *queue,
731 enum v4l2_buf_type type,
732 const struct isp_video_queue_operations *ops,
733 struct device *dev, unsigned int bufsize)
734{
735 INIT_LIST_HEAD(&queue->queue);
736 mutex_init(&queue->lock);
737 spin_lock_init(&queue->irqlock);
738
739 queue->type = type;
740 queue->ops = ops;
741 queue->dev = dev;
742 queue->bufsize = bufsize;
743
744 return 0;
745}
746
747/* -----------------------------------------------------------------------------
748 * V4L2 operations
749 */
750
751/**
752 * omap3isp_video_queue_reqbufs - Allocate video buffers memory
753 *
754 * This function is intended to be used as a VIDIOC_REQBUFS ioctl handler. It
755 * allocated video buffer objects and, for MMAP buffers, buffer memory.
756 *
757 * If the number of buffers is 0, all buffers are freed and the function returns
758 * without performing any allocation.
759 *
760 * If the number of buffers is not 0, currently allocated buffers (if any) are
761 * freed and the requested number of buffers are allocated. Depending on
762 * driver-specific requirements and on memory availability, a number of buffer
763 * smaller or bigger than requested can be allocated. This isn't considered as
764 * an error.
765 *
766 * Return 0 on success or one of the following error codes:
767 *
768 * -EINVAL if the buffer type or index are invalid
769 * -EBUSY if the queue is busy (streaming or buffers mapped)
770 * -ENOMEM if the buffers can't be allocated due to an out-of-memory condition
771 */
772int omap3isp_video_queue_reqbufs(struct isp_video_queue *queue,
773 struct v4l2_requestbuffers *rb)
774{
775 unsigned int nbuffers = rb->count;
776 unsigned int size;
777 int ret;
778
779 if (rb->type != queue->type)
780 return -EINVAL;
781
782 queue->ops->queue_prepare(queue, &nbuffers, &size);
783 if (size == 0)
784 return -EINVAL;
785
786 nbuffers = min_t(unsigned int, nbuffers, ISP_VIDEO_MAX_BUFFERS);
787
788 mutex_lock(&queue->lock);
789
790 ret = isp_video_queue_alloc(queue, nbuffers, size, rb->memory);
791 if (ret < 0)
792 goto done;
793
794 rb->count = ret;
795 ret = 0;
796
797done:
798 mutex_unlock(&queue->lock);
799 return ret;
800}
801
802/**
803 * omap3isp_video_queue_querybuf - Query the status of a buffer in a queue
804 *
805 * This function is intended to be used as a VIDIOC_QUERYBUF ioctl handler. It
806 * returns the status of a given video buffer.
807 *
808 * Return 0 on success or -EINVAL if the buffer type or index are invalid.
809 */
810int omap3isp_video_queue_querybuf(struct isp_video_queue *queue,
811 struct v4l2_buffer *vbuf)
812{
813 struct isp_video_buffer *buf;
814 int ret = 0;
815
816 if (vbuf->type != queue->type)
817 return -EINVAL;
818
819 mutex_lock(&queue->lock);
820
821 if (vbuf->index >= queue->count) {
822 ret = -EINVAL;
823 goto done;
824 }
825
826 buf = queue->buffers[vbuf->index];
827 isp_video_buffer_query(buf, vbuf);
828
829done:
830 mutex_unlock(&queue->lock);
831 return ret;
832}
833
834/**
835 * omap3isp_video_queue_qbuf - Queue a buffer
836 *
837 * This function is intended to be used as a VIDIOC_QBUF ioctl handler.
838 *
839 * The v4l2_buffer structure passed from userspace is first sanity tested. If
840 * sane, the buffer is then processed and added to the main queue and, if the
841 * queue is streaming, to the IRQ queue.
842 *
843 * Before being enqueued, USERPTR buffers are checked for address changes. If
844 * the buffer has a different userspace address, the old memory area is unlocked
845 * and the new memory area is locked.
846 */
847int omap3isp_video_queue_qbuf(struct isp_video_queue *queue,
848 struct v4l2_buffer *vbuf)
849{
850 struct isp_video_buffer *buf;
851 unsigned long flags;
852 int ret = -EINVAL;
853
854 if (vbuf->type != queue->type)
855 goto done;
856
857 mutex_lock(&queue->lock);
858
859 if (vbuf->index >= queue->count)
860 goto done;
861
862 buf = queue->buffers[vbuf->index];
863
864 if (vbuf->memory != buf->vbuf.memory)
865 goto done;
866
867 if (buf->state != ISP_BUF_STATE_IDLE)
868 goto done;
869
870 if (vbuf->memory == V4L2_MEMORY_USERPTR &&
871 vbuf->m.userptr != buf->vbuf.m.userptr) {
872 isp_video_buffer_cleanup(buf);
873 buf->vbuf.m.userptr = vbuf->m.userptr;
874 buf->prepared = 0;
875 }
876
877 if (!buf->prepared) {
878 ret = isp_video_buffer_prepare(buf);
879 if (ret < 0)
880 goto done;
881 buf->prepared = 1;
882 }
883
884 isp_video_buffer_cache_sync(buf);
885
886 buf->state = ISP_BUF_STATE_QUEUED;
887 list_add_tail(&buf->stream, &queue->queue);
888
889 if (queue->streaming) {
890 spin_lock_irqsave(&queue->irqlock, flags);
891 queue->ops->buffer_queue(buf);
892 spin_unlock_irqrestore(&queue->irqlock, flags);
893 }
894
895 ret = 0;
896
897done:
898 mutex_unlock(&queue->lock);
899 return ret;
900}
901
902/**
903 * omap3isp_video_queue_dqbuf - Dequeue a buffer
904 *
905 * This function is intended to be used as a VIDIOC_DQBUF ioctl handler.
906 *
907 * The v4l2_buffer structure passed from userspace is first sanity tested. If
908 * sane, the buffer is then processed and added to the main queue and, if the
909 * queue is streaming, to the IRQ queue.
910 *
911 * Before being enqueued, USERPTR buffers are checked for address changes. If
912 * the buffer has a different userspace address, the old memory area is unlocked
913 * and the new memory area is locked.
914 */
915int omap3isp_video_queue_dqbuf(struct isp_video_queue *queue,
916 struct v4l2_buffer *vbuf, int nonblocking)
917{
918 struct isp_video_buffer *buf;
919 int ret;
920
921 if (vbuf->type != queue->type)
922 return -EINVAL;
923
924 mutex_lock(&queue->lock);
925
926 if (list_empty(&queue->queue)) {
927 ret = -EINVAL;
928 goto done;
929 }
930
931 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
932 ret = isp_video_buffer_wait(buf, nonblocking);
933 if (ret < 0)
934 goto done;
935
936 list_del(&buf->stream);
937
938 isp_video_buffer_query(buf, vbuf);
939 buf->state = ISP_BUF_STATE_IDLE;
940 vbuf->flags &= ~V4L2_BUF_FLAG_QUEUED;
941
942done:
943 mutex_unlock(&queue->lock);
944 return ret;
945}
946
947/**
948 * omap3isp_video_queue_streamon - Start streaming
949 *
950 * This function is intended to be used as a VIDIOC_STREAMON ioctl handler. It
951 * starts streaming on the queue and calls the buffer_queue operation for all
952 * queued buffers.
953 *
954 * Return 0 on success.
955 */
956int omap3isp_video_queue_streamon(struct isp_video_queue *queue)
957{
958 struct isp_video_buffer *buf;
959 unsigned long flags;
960
961 mutex_lock(&queue->lock);
962
963 if (queue->streaming)
964 goto done;
965
966 queue->streaming = 1;
967
968 spin_lock_irqsave(&queue->irqlock, flags);
969 list_for_each_entry(buf, &queue->queue, stream)
970 queue->ops->buffer_queue(buf);
971 spin_unlock_irqrestore(&queue->irqlock, flags);
972
973done:
974 mutex_unlock(&queue->lock);
975 return 0;
976}
977
978/**
979 * omap3isp_video_queue_streamoff - Stop streaming
980 *
981 * This function is intended to be used as a VIDIOC_STREAMOFF ioctl handler. It
982 * stops streaming on the queue and wakes up all the buffers.
983 *
984 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
985 * delayed works before calling this function to make sure no buffer will be
986 * touched by the driver and/or hardware.
987 */
988void omap3isp_video_queue_streamoff(struct isp_video_queue *queue)
989{
990 struct isp_video_buffer *buf;
991 unsigned long flags;
992 unsigned int i;
993
994 mutex_lock(&queue->lock);
995
996 if (!queue->streaming)
997 goto done;
998
999 queue->streaming = 0;
1000
1001 spin_lock_irqsave(&queue->irqlock, flags);
1002 for (i = 0; i < queue->count; ++i) {
1003 buf = queue->buffers[i];
1004
1005 if (buf->state == ISP_BUF_STATE_ACTIVE)
1006 wake_up(&buf->wait);
1007
1008 buf->state = ISP_BUF_STATE_IDLE;
1009 }
1010 spin_unlock_irqrestore(&queue->irqlock, flags);
1011
1012 INIT_LIST_HEAD(&queue->queue);
1013
1014done:
1015 mutex_unlock(&queue->lock);
1016}
1017
1018/**
1019 * omap3isp_video_queue_discard_done - Discard all buffers marked as DONE
1020 *
1021 * This function is intended to be used with suspend/resume operations. It
1022 * discards all 'done' buffers as they would be too old to be requested after
1023 * resume.
1024 *
1025 * Drivers must stop the hardware and synchronize with interrupt handlers and/or
1026 * delayed works before calling this function to make sure no buffer will be
1027 * touched by the driver and/or hardware.
1028 */
1029void omap3isp_video_queue_discard_done(struct isp_video_queue *queue)
1030{
1031 struct isp_video_buffer *buf;
1032 unsigned int i;
1033
1034 mutex_lock(&queue->lock);
1035
1036 if (!queue->streaming)
1037 goto done;
1038
1039 for (i = 0; i < queue->count; ++i) {
1040 buf = queue->buffers[i];
1041
1042 if (buf->state == ISP_BUF_STATE_DONE)
1043 buf->state = ISP_BUF_STATE_ERROR;
1044 }
1045
1046done:
1047 mutex_unlock(&queue->lock);
1048}
1049
1050static void isp_video_queue_vm_open(struct vm_area_struct *vma)
1051{
1052 struct isp_video_buffer *buf = vma->vm_private_data;
1053
1054 buf->vma_use_count++;
1055}
1056
1057static void isp_video_queue_vm_close(struct vm_area_struct *vma)
1058{
1059 struct isp_video_buffer *buf = vma->vm_private_data;
1060
1061 buf->vma_use_count--;
1062}
1063
1064static const struct vm_operations_struct isp_video_queue_vm_ops = {
1065 .open = isp_video_queue_vm_open,
1066 .close = isp_video_queue_vm_close,
1067};
1068
1069/**
1070 * omap3isp_video_queue_mmap - Map buffers to userspace
1071 *
1072 * This function is intended to be used as an mmap() file operation handler. It
1073 * maps a buffer to userspace based on the VMA offset.
1074 *
1075 * Only buffers of memory type MMAP are supported.
1076 */
1077int omap3isp_video_queue_mmap(struct isp_video_queue *queue,
1078 struct vm_area_struct *vma)
1079{
1080 struct isp_video_buffer *uninitialized_var(buf);
1081 unsigned long size;
1082 unsigned int i;
1083 int ret = 0;
1084
1085 mutex_lock(&queue->lock);
1086
1087 for (i = 0; i < queue->count; ++i) {
1088 buf = queue->buffers[i];
1089 if ((buf->vbuf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1090 break;
1091 }
1092
1093 if (i == queue->count) {
1094 ret = -EINVAL;
1095 goto done;
1096 }
1097
1098 size = vma->vm_end - vma->vm_start;
1099
1100 if (buf->vbuf.memory != V4L2_MEMORY_MMAP ||
1101 size != PAGE_ALIGN(buf->vbuf.length)) {
1102 ret = -EINVAL;
1103 goto done;
1104 }
1105
1106 ret = remap_vmalloc_range(vma, buf->vaddr, 0);
1107 if (ret < 0)
1108 goto done;
1109
1110 vma->vm_ops = &isp_video_queue_vm_ops;
1111 vma->vm_private_data = buf;
1112 isp_video_queue_vm_open(vma);
1113
1114done:
1115 mutex_unlock(&queue->lock);
1116 return ret;
1117}
1118
1119/**
1120 * omap3isp_video_queue_poll - Poll video queue state
1121 *
1122 * This function is intended to be used as a poll() file operation handler. It
1123 * polls the state of the video buffer at the front of the queue and returns an
1124 * events mask.
1125 *
1126 * If no buffer is present at the front of the queue, POLLERR is returned.
1127 */
1128unsigned int omap3isp_video_queue_poll(struct isp_video_queue *queue,
1129 struct file *file, poll_table *wait)
1130{
1131 struct isp_video_buffer *buf;
1132 unsigned int mask = 0;
1133
1134 mutex_lock(&queue->lock);
1135 if (list_empty(&queue->queue)) {
1136 mask |= POLLERR;
1137 goto done;
1138 }
1139 buf = list_first_entry(&queue->queue, struct isp_video_buffer, stream);
1140
1141 poll_wait(file, &buf->wait, wait);
1142 if (buf->state == ISP_BUF_STATE_DONE ||
1143 buf->state == ISP_BUF_STATE_ERROR) {
1144 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1145 mask |= POLLIN | POLLRDNORM;
1146 else
1147 mask |= POLLOUT | POLLWRNORM;
1148 }
1149
1150done:
1151 mutex_unlock(&queue->lock);
1152 return mask;
1153}
diff --git a/drivers/media/video/omap3isp/ispqueue.h b/drivers/media/video/omap3isp/ispqueue.h
new file mode 100644
index 000000000000..251de3e1679d
--- /dev/null
+++ b/drivers/media/video/omap3isp/ispqueue.h
@@ -0,0 +1,187 @@
1/*
2 * ispqueue.h
3 *
4 * TI OMAP3 ISP - Video buffers queue handling
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26#ifndef OMAP3_ISP_QUEUE_H
27#define OMAP3_ISP_QUEUE_H
28
29#include <linux/kernel.h>
30#include <linux/list.h>
31#include <linux/mutex.h>
32#include <linux/videodev2.h>
33#include <linux/wait.h>
34
35struct isp_video_queue;
36struct page;
37struct scatterlist;
38
39#define ISP_VIDEO_MAX_BUFFERS 16
40
41/**
42 * enum isp_video_buffer_state - ISP video buffer state
43 * @ISP_BUF_STATE_IDLE: The buffer is under userspace control (dequeued
44 * or not queued yet).
45 * @ISP_BUF_STATE_QUEUED: The buffer has been queued but isn't used by the
46 * device yet.
47 * @ISP_BUF_STATE_ACTIVE: The buffer is in use for an active video transfer.
48 * @ISP_BUF_STATE_ERROR: The device is done with the buffer and an error
49 * occured. For capture device the buffer likely contains corrupted data or
50 * no data at all.
51 * @ISP_BUF_STATE_DONE: The device is done with the buffer and no error occured.
52 * For capture devices the buffer contains valid data.
53 */
54enum isp_video_buffer_state {
55 ISP_BUF_STATE_IDLE,
56 ISP_BUF_STATE_QUEUED,
57 ISP_BUF_STATE_ACTIVE,
58 ISP_BUF_STATE_ERROR,
59 ISP_BUF_STATE_DONE,
60};
61
62/**
63 * struct isp_video_buffer - ISP video buffer
64 * @vma_use_count: Number of times the buffer is mmap'ed to userspace
65 * @stream: List head for insertion into main queue
66 * @queue: ISP buffers queue this buffer belongs to
67 * @prepared: Whether the buffer has been prepared
68 * @skip_cache: Whether to skip cache management operations for this buffer
69 * @vaddr: Memory virtual address (for kernel buffers)
70 * @vm_flags: Buffer VMA flags (for userspace buffers)
71 * @offset: Offset inside the first page (for userspace buffers)
72 * @npages: Number of pages (for userspace buffers)
73 * @pages: Pages table (for userspace non-VM_PFNMAP buffers)
74 * @paddr: Memory physical address (for userspace VM_PFNMAP buffers)
75 * @sglen: Number of elements in the scatter list (for non-VM_PFNMAP buffers)
76 * @sglist: Scatter list (for non-VM_PFNMAP buffers)
77 * @vbuf: V4L2 buffer
78 * @irqlist: List head for insertion into IRQ queue
79 * @state: Current buffer state
80 * @wait: Wait queue to signal buffer completion
81 */
82struct isp_video_buffer {
83 unsigned long vma_use_count;
84 struct list_head stream;
85 struct isp_video_queue *queue;
86 unsigned int prepared:1;
87 bool skip_cache;
88
89 /* For kernel buffers. */
90 void *vaddr;
91
92 /* For userspace buffers. */
93 unsigned long vm_flags;
94 unsigned long offset;
95 unsigned int npages;
96 struct page **pages;
97 dma_addr_t paddr;
98
99 /* For all buffers except VM_PFNMAP. */
100 unsigned int sglen;
101 struct scatterlist *sglist;
102
103 /* Touched by the interrupt handler. */
104 struct v4l2_buffer vbuf;
105 struct list_head irqlist;
106 enum isp_video_buffer_state state;
107 wait_queue_head_t wait;
108};
109
110#define to_isp_video_buffer(vb) container_of(vb, struct isp_video_buffer, vb)
111
112/**
113 * struct isp_video_queue_operations - Driver-specific operations
114 * @queue_prepare: Called before allocating buffers. Drivers should clamp the
115 * number of buffers according to their requirements, and must return the
116 * buffer size in bytes.
117 * @buffer_prepare: Called the first time a buffer is queued, or after changing
118 * the userspace memory address for a USERPTR buffer, with the queue lock
119 * held. Drivers should perform device-specific buffer preparation (such as
120 * mapping the buffer memory in an IOMMU). This operation is optional.
121 * @buffer_queue: Called when a buffer is being added to the queue with the
122 * queue irqlock spinlock held.
123 * @buffer_cleanup: Called before freeing buffers, or before changing the
124 * userspace memory address for a USERPTR buffer, with the queue lock held.
125 * Drivers must perform cleanup operations required to undo the
126 * buffer_prepare call. This operation is optional.
127 */
128struct isp_video_queue_operations {
129 void (*queue_prepare)(struct isp_video_queue *queue,
130 unsigned int *nbuffers, unsigned int *size);
131 int (*buffer_prepare)(struct isp_video_buffer *buf);
132 void (*buffer_queue)(struct isp_video_buffer *buf);
133 void (*buffer_cleanup)(struct isp_video_buffer *buf);
134};
135
136/**
137 * struct isp_video_queue - ISP video buffers queue
138 * @type: Type of video buffers handled by this queue
139 * @ops: Queue operations
140 * @dev: Device used for DMA operations
141 * @bufsize: Size of a driver-specific buffer object
142 * @count: Number of currently allocated buffers
143 * @buffers: ISP video buffers
144 * @lock: Mutex to protect access to the buffers, main queue and state
145 * @irqlock: Spinlock to protect access to the IRQ queue
146 * @streaming: Queue state, indicates whether the queue is streaming
147 * @queue: List of all queued buffers
148 */
149struct isp_video_queue {
150 enum v4l2_buf_type type;
151 const struct isp_video_queue_operations *ops;
152 struct device *dev;
153 unsigned int bufsize;
154
155 unsigned int count;
156 struct isp_video_buffer *buffers[ISP_VIDEO_MAX_BUFFERS];
157 struct mutex lock;
158 spinlock_t irqlock;
159
160 unsigned int streaming:1;
161
162 struct list_head queue;
163};
164
165int omap3isp_video_queue_cleanup(struct isp_video_queue *queue);
166int omap3isp_video_queue_init(struct isp_video_queue *queue,
167 enum v4l2_buf_type type,
168 const struct isp_video_queue_operations *ops,
169 struct device *dev, unsigned int bufsize);
170
171int omap3isp_video_queue_reqbufs(struct isp_video_queue *queue,
172 struct v4l2_requestbuffers *rb);
173int omap3isp_video_queue_querybuf(struct isp_video_queue *queue,
174 struct v4l2_buffer *vbuf);
175int omap3isp_video_queue_qbuf(struct isp_video_queue *queue,
176 struct v4l2_buffer *vbuf);
177int omap3isp_video_queue_dqbuf(struct isp_video_queue *queue,
178 struct v4l2_buffer *vbuf, int nonblocking);
179int omap3isp_video_queue_streamon(struct isp_video_queue *queue);
180void omap3isp_video_queue_streamoff(struct isp_video_queue *queue);
181void omap3isp_video_queue_discard_done(struct isp_video_queue *queue);
182int omap3isp_video_queue_mmap(struct isp_video_queue *queue,
183 struct vm_area_struct *vma);
184unsigned int omap3isp_video_queue_poll(struct isp_video_queue *queue,
185 struct file *file, poll_table *wait);
186
187#endif /* OMAP3_ISP_QUEUE_H */
diff --git a/drivers/media/video/omap3isp/ispvideo.c b/drivers/media/video/omap3isp/ispvideo.c
new file mode 100644
index 000000000000..517a24de5836
--- /dev/null
+++ b/drivers/media/video/omap3isp/ispvideo.c
@@ -0,0 +1,1264 @@
1/*
2 * ispvideo.c
3 *
4 * TI OMAP3 ISP - Generic video node
5 *
6 * Copyright (C) 2009-2010 Nokia Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26#include <asm/cacheflush.h>
27#include <linux/clk.h>
28#include <linux/mm.h>
29#include <linux/pagemap.h>
30#include <linux/scatterlist.h>
31#include <linux/sched.h>
32#include <linux/slab.h>
33#include <linux/vmalloc.h>
34#include <media/v4l2-dev.h>
35#include <media/v4l2-ioctl.h>
36#include <plat/iommu.h>
37#include <plat/iovmm.h>
38#include <plat/omap-pm.h>
39
40#include "ispvideo.h"
41#include "isp.h"
42
43
44/* -----------------------------------------------------------------------------
45 * Helper functions
46 */
47
48static struct isp_format_info formats[] = {
49 { V4L2_MBUS_FMT_Y8_1X8, V4L2_MBUS_FMT_Y8_1X8,
50 V4L2_MBUS_FMT_Y8_1X8, V4L2_PIX_FMT_GREY, 8, },
51 { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8,
52 V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10DPCM8, 8, },
53 { V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_MBUS_FMT_SBGGR10_1X10,
54 V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_PIX_FMT_SBGGR10, 10, },
55 { V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_MBUS_FMT_SGBRG10_1X10,
56 V4L2_MBUS_FMT_SGBRG10_1X10, V4L2_PIX_FMT_SGBRG10, 10, },
57 { V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_MBUS_FMT_SGRBG10_1X10,
58 V4L2_MBUS_FMT_SGRBG10_1X10, V4L2_PIX_FMT_SGRBG10, 10, },
59 { V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_MBUS_FMT_SRGGB10_1X10,
60 V4L2_MBUS_FMT_SRGGB10_1X10, V4L2_PIX_FMT_SRGGB10, 10, },
61 { V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_MBUS_FMT_SBGGR10_1X10,
62 V4L2_MBUS_FMT_SBGGR12_1X12, V4L2_PIX_FMT_SBGGR12, 12, },
63 { V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_MBUS_FMT_SGBRG10_1X10,
64 V4L2_MBUS_FMT_SGBRG12_1X12, V4L2_PIX_FMT_SGBRG12, 12, },
65 { V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_MBUS_FMT_SGRBG10_1X10,
66 V4L2_MBUS_FMT_SGRBG12_1X12, V4L2_PIX_FMT_SGRBG12, 12, },
67 { V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_MBUS_FMT_SRGGB10_1X10,
68 V4L2_MBUS_FMT_SRGGB12_1X12, V4L2_PIX_FMT_SRGGB12, 12, },
69 { V4L2_MBUS_FMT_UYVY8_1X16, V4L2_MBUS_FMT_UYVY8_1X16,
70 V4L2_MBUS_FMT_UYVY8_1X16, V4L2_PIX_FMT_UYVY, 16, },
71 { V4L2_MBUS_FMT_YUYV8_1X16, V4L2_MBUS_FMT_YUYV8_1X16,
72 V4L2_MBUS_FMT_YUYV8_1X16, V4L2_PIX_FMT_YUYV, 16, },
73};
74
75const struct isp_format_info *
76omap3isp_video_format_info(enum v4l2_mbus_pixelcode code)
77{
78 unsigned int i;
79
80 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
81 if (formats[i].code == code)
82 return &formats[i];
83 }
84
85 return NULL;
86}
87
88/*
89 * isp_video_mbus_to_pix - Convert v4l2_mbus_framefmt to v4l2_pix_format
90 * @video: ISP video instance
91 * @mbus: v4l2_mbus_framefmt format (input)
92 * @pix: v4l2_pix_format format (output)
93 *
94 * Fill the output pix structure with information from the input mbus format.
95 * The bytesperline and sizeimage fields are computed from the requested bytes
96 * per line value in the pix format and information from the video instance.
97 *
98 * Return the number of padding bytes at end of line.
99 */
100static unsigned int isp_video_mbus_to_pix(const struct isp_video *video,
101 const struct v4l2_mbus_framefmt *mbus,
102 struct v4l2_pix_format *pix)
103{
104 unsigned int bpl = pix->bytesperline;
105 unsigned int min_bpl;
106 unsigned int i;
107
108 memset(pix, 0, sizeof(*pix));
109 pix->width = mbus->width;
110 pix->height = mbus->height;
111
112 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
113 if (formats[i].code == mbus->code)
114 break;
115 }
116
117 if (WARN_ON(i == ARRAY_SIZE(formats)))
118 return 0;
119
120 min_bpl = pix->width * ALIGN(formats[i].bpp, 8) / 8;
121
122 /* Clamp the requested bytes per line value. If the maximum bytes per
123 * line value is zero, the module doesn't support user configurable line
124 * sizes. Override the requested value with the minimum in that case.
125 */
126 if (video->bpl_max)
127 bpl = clamp(bpl, min_bpl, video->bpl_max);
128 else
129 bpl = min_bpl;
130
131 if (!video->bpl_zero_padding || bpl != min_bpl)
132 bpl = ALIGN(bpl, video->bpl_alignment);
133
134 pix->pixelformat = formats[i].pixelformat;
135 pix->bytesperline = bpl;
136 pix->sizeimage = pix->bytesperline * pix->height;
137 pix->colorspace = mbus->colorspace;
138 pix->field = mbus->field;
139
140 return bpl - min_bpl;
141}
142
143static void isp_video_pix_to_mbus(const struct v4l2_pix_format *pix,
144 struct v4l2_mbus_framefmt *mbus)
145{
146 unsigned int i;
147
148 memset(mbus, 0, sizeof(*mbus));
149 mbus->width = pix->width;
150 mbus->height = pix->height;
151
152 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
153 if (formats[i].pixelformat == pix->pixelformat)
154 break;
155 }
156
157 if (WARN_ON(i == ARRAY_SIZE(formats)))
158 return;
159
160 mbus->code = formats[i].code;
161 mbus->colorspace = pix->colorspace;
162 mbus->field = pix->field;
163}
164
165static struct v4l2_subdev *
166isp_video_remote_subdev(struct isp_video *video, u32 *pad)
167{
168 struct media_pad *remote;
169
170 remote = media_entity_remote_source(&video->pad);
171
172 if (remote == NULL ||
173 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
174 return NULL;
175
176 if (pad)
177 *pad = remote->index;
178
179 return media_entity_to_v4l2_subdev(remote->entity);
180}
181
182/* Return a pointer to the ISP video instance at the far end of the pipeline. */
183static struct isp_video *
184isp_video_far_end(struct isp_video *video)
185{
186 struct media_entity_graph graph;
187 struct media_entity *entity = &video->video.entity;
188 struct media_device *mdev = entity->parent;
189 struct isp_video *far_end = NULL;
190
191 mutex_lock(&mdev->graph_mutex);
192 media_entity_graph_walk_start(&graph, entity);
193
194 while ((entity = media_entity_graph_walk_next(&graph))) {
195 if (entity == &video->video.entity)
196 continue;
197
198 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
199 continue;
200
201 far_end = to_isp_video(media_entity_to_video_device(entity));
202 if (far_end->type != video->type)
203 break;
204
205 far_end = NULL;
206 }
207
208 mutex_unlock(&mdev->graph_mutex);
209 return far_end;
210}
211
212/*
213 * Validate a pipeline by checking both ends of all links for format
214 * discrepancies.
215 *
216 * Compute the minimum time per frame value as the maximum of time per frame
217 * limits reported by every block in the pipeline.
218 *
219 * Return 0 if all formats match, or -EPIPE if at least one link is found with
220 * different formats on its two ends.
221 */
222static int isp_video_validate_pipeline(struct isp_pipeline *pipe)
223{
224 struct isp_device *isp = pipe->output->isp;
225 struct v4l2_subdev_format fmt_source;
226 struct v4l2_subdev_format fmt_sink;
227 struct media_pad *pad;
228 struct v4l2_subdev *subdev;
229 int ret;
230
231 pipe->max_rate = pipe->l3_ick;
232
233 subdev = isp_video_remote_subdev(pipe->output, NULL);
234 if (subdev == NULL)
235 return -EPIPE;
236
237 while (1) {
238 /* Retrieve the sink format */
239 pad = &subdev->entity.pads[0];
240 if (!(pad->flags & MEDIA_PAD_FL_SINK))
241 break;
242
243 fmt_sink.pad = pad->index;
244 fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
245 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_sink);
246 if (ret < 0 && ret != -ENOIOCTLCMD)
247 return -EPIPE;
248
249 /* Update the maximum frame rate */
250 if (subdev == &isp->isp_res.subdev)
251 omap3isp_resizer_max_rate(&isp->isp_res,
252 &pipe->max_rate);
253
254 /* Check ccdc maximum data rate when data comes from sensor
255 * TODO: Include ccdc rate in pipe->max_rate and compare the
256 * total pipe rate with the input data rate from sensor.
257 */
258 if (subdev == &isp->isp_ccdc.subdev && pipe->input == NULL) {
259 unsigned int rate = UINT_MAX;
260
261 omap3isp_ccdc_max_rate(&isp->isp_ccdc, &rate);
262 if (isp->isp_ccdc.vpcfg.pixelclk > rate)
263 return -ENOSPC;
264 }
265
266 /* Retrieve the source format */
267 pad = media_entity_remote_source(pad);
268 if (pad == NULL ||
269 media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
270 break;
271
272 subdev = media_entity_to_v4l2_subdev(pad->entity);
273
274 fmt_source.pad = pad->index;
275 fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
276 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
277 if (ret < 0 && ret != -ENOIOCTLCMD)
278 return -EPIPE;
279
280 /* Check if the two ends match */
281 if (fmt_source.format.code != fmt_sink.format.code ||
282 fmt_source.format.width != fmt_sink.format.width ||
283 fmt_source.format.height != fmt_sink.format.height)
284 return -EPIPE;
285 }
286
287 return 0;
288}
289
290static int
291__isp_video_get_format(struct isp_video *video, struct v4l2_format *format)
292{
293 struct v4l2_subdev_format fmt;
294 struct v4l2_subdev *subdev;
295 u32 pad;
296 int ret;
297
298 subdev = isp_video_remote_subdev(video, &pad);
299 if (subdev == NULL)
300 return -EINVAL;
301
302 mutex_lock(&video->mutex);
303
304 fmt.pad = pad;
305 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
306 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
307 if (ret == -ENOIOCTLCMD)
308 ret = -EINVAL;
309
310 mutex_unlock(&video->mutex);
311
312 if (ret)
313 return ret;
314
315 format->type = video->type;
316 return isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
317}
318
319static int
320isp_video_check_format(struct isp_video *video, struct isp_video_fh *vfh)
321{
322 struct v4l2_format format;
323 int ret;
324
325 memcpy(&format, &vfh->format, sizeof(format));
326 ret = __isp_video_get_format(video, &format);
327 if (ret < 0)
328 return ret;
329
330 if (vfh->format.fmt.pix.pixelformat != format.fmt.pix.pixelformat ||
331 vfh->format.fmt.pix.height != format.fmt.pix.height ||
332 vfh->format.fmt.pix.width != format.fmt.pix.width ||
333 vfh->format.fmt.pix.bytesperline != format.fmt.pix.bytesperline ||
334 vfh->format.fmt.pix.sizeimage != format.fmt.pix.sizeimage)
335 return -EINVAL;
336
337 return ret;
338}
339
340/* -----------------------------------------------------------------------------
341 * IOMMU management
342 */
343
344#define IOMMU_FLAG (IOVMF_ENDIAN_LITTLE | IOVMF_ELSZ_8)
345
346/*
347 * ispmmu_vmap - Wrapper for Virtual memory mapping of a scatter gather list
348 * @dev: Device pointer specific to the OMAP3 ISP.
349 * @sglist: Pointer to source Scatter gather list to allocate.
350 * @sglen: Number of elements of the scatter-gatter list.
351 *
352 * Returns a resulting mapped device address by the ISP MMU, or -ENOMEM if
353 * we ran out of memory.
354 */
355static dma_addr_t
356ispmmu_vmap(struct isp_device *isp, const struct scatterlist *sglist, int sglen)
357{
358 struct sg_table *sgt;
359 u32 da;
360
361 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
362 if (sgt == NULL)
363 return -ENOMEM;
364
365 sgt->sgl = (struct scatterlist *)sglist;
366 sgt->nents = sglen;
367 sgt->orig_nents = sglen;
368
369 da = iommu_vmap(isp->iommu, 0, sgt, IOMMU_FLAG);
370 if (IS_ERR_VALUE(da))
371 kfree(sgt);
372
373 return da;
374}
375
376/*
377 * ispmmu_vunmap - Unmap a device address from the ISP MMU
378 * @dev: Device pointer specific to the OMAP3 ISP.
379 * @da: Device address generated from a ispmmu_vmap call.
380 */
381static void ispmmu_vunmap(struct isp_device *isp, dma_addr_t da)
382{
383 struct sg_table *sgt;
384
385 sgt = iommu_vunmap(isp->iommu, (u32)da);
386 kfree(sgt);
387}
388
389/* -----------------------------------------------------------------------------
390 * Video queue operations
391 */
392
393static void isp_video_queue_prepare(struct isp_video_queue *queue,
394 unsigned int *nbuffers, unsigned int *size)
395{
396 struct isp_video_fh *vfh =
397 container_of(queue, struct isp_video_fh, queue);
398 struct isp_video *video = vfh->video;
399
400 *size = vfh->format.fmt.pix.sizeimage;
401 if (*size == 0)
402 return;
403
404 *nbuffers = min(*nbuffers, video->capture_mem / PAGE_ALIGN(*size));
405}
406
407static void isp_video_buffer_cleanup(struct isp_video_buffer *buf)
408{
409 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
410 struct isp_buffer *buffer = to_isp_buffer(buf);
411 struct isp_video *video = vfh->video;
412
413 if (buffer->isp_addr) {
414 ispmmu_vunmap(video->isp, buffer->isp_addr);
415 buffer->isp_addr = 0;
416 }
417}
418
419static int isp_video_buffer_prepare(struct isp_video_buffer *buf)
420{
421 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
422 struct isp_buffer *buffer = to_isp_buffer(buf);
423 struct isp_video *video = vfh->video;
424 unsigned long addr;
425
426 addr = ispmmu_vmap(video->isp, buf->sglist, buf->sglen);
427 if (IS_ERR_VALUE(addr))
428 return -EIO;
429
430 if (!IS_ALIGNED(addr, 32)) {
431 dev_dbg(video->isp->dev, "Buffer address must be "
432 "aligned to 32 bytes boundary.\n");
433 ispmmu_vunmap(video->isp, buffer->isp_addr);
434 return -EINVAL;
435 }
436
437 buf->vbuf.bytesused = vfh->format.fmt.pix.sizeimage;
438 buffer->isp_addr = addr;
439 return 0;
440}
441
442/*
443 * isp_video_buffer_queue - Add buffer to streaming queue
444 * @buf: Video buffer
445 *
446 * In memory-to-memory mode, start streaming on the pipeline if buffers are
447 * queued on both the input and the output, if the pipeline isn't already busy.
448 * If the pipeline is busy, it will be restarted in the output module interrupt
449 * handler.
450 */
451static void isp_video_buffer_queue(struct isp_video_buffer *buf)
452{
453 struct isp_video_fh *vfh = isp_video_queue_to_isp_video_fh(buf->queue);
454 struct isp_buffer *buffer = to_isp_buffer(buf);
455 struct isp_video *video = vfh->video;
456 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
457 enum isp_pipeline_state state;
458 unsigned long flags;
459 unsigned int empty;
460 unsigned int start;
461
462 empty = list_empty(&video->dmaqueue);
463 list_add_tail(&buffer->buffer.irqlist, &video->dmaqueue);
464
465 if (empty) {
466 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
467 state = ISP_PIPELINE_QUEUE_OUTPUT;
468 else
469 state = ISP_PIPELINE_QUEUE_INPUT;
470
471 spin_lock_irqsave(&pipe->lock, flags);
472 pipe->state |= state;
473 video->ops->queue(video, buffer);
474 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
475
476 start = isp_pipeline_ready(pipe);
477 if (start)
478 pipe->state |= ISP_PIPELINE_STREAM;
479 spin_unlock_irqrestore(&pipe->lock, flags);
480
481 if (start)
482 omap3isp_pipeline_set_stream(pipe,
483 ISP_PIPELINE_STREAM_SINGLESHOT);
484 }
485}
486
487static const struct isp_video_queue_operations isp_video_queue_ops = {
488 .queue_prepare = &isp_video_queue_prepare,
489 .buffer_prepare = &isp_video_buffer_prepare,
490 .buffer_queue = &isp_video_buffer_queue,
491 .buffer_cleanup = &isp_video_buffer_cleanup,
492};
493
494/*
495 * omap3isp_video_buffer_next - Complete the current buffer and return the next
496 * @video: ISP video object
497 * @error: Whether an error occured during capture
498 *
499 * Remove the current video buffer from the DMA queue and fill its timestamp,
500 * field count and state fields before waking up its completion handler.
501 *
502 * The buffer state is set to VIDEOBUF_DONE if no error occured (@error is 0)
503 * or VIDEOBUF_ERROR otherwise (@error is non-zero).
504 *
505 * The DMA queue is expected to contain at least one buffer.
506 *
507 * Return a pointer to the next buffer in the DMA queue, or NULL if the queue is
508 * empty.
509 */
510struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video,
511 unsigned int error)
512{
513 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
514 struct isp_video_queue *queue = video->queue;
515 enum isp_pipeline_state state;
516 struct isp_video_buffer *buf;
517 unsigned long flags;
518 struct timespec ts;
519
520 spin_lock_irqsave(&queue->irqlock, flags);
521 if (WARN_ON(list_empty(&video->dmaqueue))) {
522 spin_unlock_irqrestore(&queue->irqlock, flags);
523 return NULL;
524 }
525
526 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
527 irqlist);
528 list_del(&buf->irqlist);
529 spin_unlock_irqrestore(&queue->irqlock, flags);
530
531 ktime_get_ts(&ts);
532 buf->vbuf.timestamp.tv_sec = ts.tv_sec;
533 buf->vbuf.timestamp.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
534
535 /* Do frame number propagation only if this is the output video node.
536 * Frame number either comes from the CSI receivers or it gets
537 * incremented here if H3A is not active.
538 * Note: There is no guarantee that the output buffer will finish
539 * first, so the input number might lag behind by 1 in some cases.
540 */
541 if (video == pipe->output && !pipe->do_propagation)
542 buf->vbuf.sequence = atomic_inc_return(&pipe->frame_number);
543 else
544 buf->vbuf.sequence = atomic_read(&pipe->frame_number);
545
546 buf->state = error ? ISP_BUF_STATE_ERROR : ISP_BUF_STATE_DONE;
547
548 wake_up(&buf->wait);
549
550 if (list_empty(&video->dmaqueue)) {
551 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
552 state = ISP_PIPELINE_QUEUE_OUTPUT
553 | ISP_PIPELINE_STREAM;
554 else
555 state = ISP_PIPELINE_QUEUE_INPUT
556 | ISP_PIPELINE_STREAM;
557
558 spin_lock_irqsave(&pipe->lock, flags);
559 pipe->state &= ~state;
560 if (video->pipe.stream_state == ISP_PIPELINE_STREAM_CONTINUOUS)
561 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
562 spin_unlock_irqrestore(&pipe->lock, flags);
563 return NULL;
564 }
565
566 if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && pipe->input != NULL) {
567 spin_lock_irqsave(&pipe->lock, flags);
568 pipe->state &= ~ISP_PIPELINE_STREAM;
569 spin_unlock_irqrestore(&pipe->lock, flags);
570 }
571
572 buf = list_first_entry(&video->dmaqueue, struct isp_video_buffer,
573 irqlist);
574 buf->state = ISP_BUF_STATE_ACTIVE;
575 return to_isp_buffer(buf);
576}
577
578/*
579 * omap3isp_video_resume - Perform resume operation on the buffers
580 * @video: ISP video object
581 * @continuous: Pipeline is in single shot mode if 0 or continous mode otherwise
582 *
583 * This function is intended to be used on suspend/resume scenario. It
584 * requests video queue layer to discard buffers marked as DONE if it's in
585 * continuous mode and requests ISP modules to queue again the ACTIVE buffer
586 * if there's any.
587 */
588void omap3isp_video_resume(struct isp_video *video, int continuous)
589{
590 struct isp_buffer *buf = NULL;
591
592 if (continuous && video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
593 omap3isp_video_queue_discard_done(video->queue);
594
595 if (!list_empty(&video->dmaqueue)) {
596 buf = list_first_entry(&video->dmaqueue,
597 struct isp_buffer, buffer.irqlist);
598 video->ops->queue(video, buf);
599 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_QUEUED;
600 } else {
601 if (continuous)
602 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
603 }
604}
605
606/* -----------------------------------------------------------------------------
607 * V4L2 ioctls
608 */
609
610static int
611isp_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
612{
613 struct isp_video *video = video_drvdata(file);
614
615 strlcpy(cap->driver, ISP_VIDEO_DRIVER_NAME, sizeof(cap->driver));
616 strlcpy(cap->card, video->video.name, sizeof(cap->card));
617 strlcpy(cap->bus_info, "media", sizeof(cap->bus_info));
618 cap->version = ISP_VIDEO_DRIVER_VERSION;
619
620 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
621 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
622 else
623 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
624
625 return 0;
626}
627
628static int
629isp_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
630{
631 struct isp_video_fh *vfh = to_isp_video_fh(fh);
632 struct isp_video *video = video_drvdata(file);
633
634 if (format->type != video->type)
635 return -EINVAL;
636
637 mutex_lock(&video->mutex);
638 *format = vfh->format;
639 mutex_unlock(&video->mutex);
640
641 return 0;
642}
643
644static int
645isp_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
646{
647 struct isp_video_fh *vfh = to_isp_video_fh(fh);
648 struct isp_video *video = video_drvdata(file);
649 struct v4l2_mbus_framefmt fmt;
650
651 if (format->type != video->type)
652 return -EINVAL;
653
654 mutex_lock(&video->mutex);
655
656 /* Fill the bytesperline and sizeimage fields by converting to media bus
657 * format and back to pixel format.
658 */
659 isp_video_pix_to_mbus(&format->fmt.pix, &fmt);
660 isp_video_mbus_to_pix(video, &fmt, &format->fmt.pix);
661
662 vfh->format = *format;
663
664 mutex_unlock(&video->mutex);
665 return 0;
666}
667
668static int
669isp_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
670{
671 struct isp_video *video = video_drvdata(file);
672 struct v4l2_subdev_format fmt;
673 struct v4l2_subdev *subdev;
674 u32 pad;
675 int ret;
676
677 if (format->type != video->type)
678 return -EINVAL;
679
680 subdev = isp_video_remote_subdev(video, &pad);
681 if (subdev == NULL)
682 return -EINVAL;
683
684 isp_video_pix_to_mbus(&format->fmt.pix, &fmt.format);
685
686 fmt.pad = pad;
687 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
688 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
689 if (ret)
690 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
691
692 isp_video_mbus_to_pix(video, &fmt.format, &format->fmt.pix);
693 return 0;
694}
695
696static int
697isp_video_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
698{
699 struct isp_video *video = video_drvdata(file);
700 struct v4l2_subdev *subdev;
701 int ret;
702
703 subdev = isp_video_remote_subdev(video, NULL);
704 if (subdev == NULL)
705 return -EINVAL;
706
707 mutex_lock(&video->mutex);
708 ret = v4l2_subdev_call(subdev, video, cropcap, cropcap);
709 mutex_unlock(&video->mutex);
710
711 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
712}
713
714static int
715isp_video_get_crop(struct file *file, void *fh, struct v4l2_crop *crop)
716{
717 struct isp_video *video = video_drvdata(file);
718 struct v4l2_subdev_format format;
719 struct v4l2_subdev *subdev;
720 u32 pad;
721 int ret;
722
723 subdev = isp_video_remote_subdev(video, &pad);
724 if (subdev == NULL)
725 return -EINVAL;
726
727 /* Try the get crop operation first and fallback to get format if not
728 * implemented.
729 */
730 ret = v4l2_subdev_call(subdev, video, g_crop, crop);
731 if (ret != -ENOIOCTLCMD)
732 return ret;
733
734 format.pad = pad;
735 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
736 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &format);
737 if (ret < 0)
738 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
739
740 crop->c.left = 0;
741 crop->c.top = 0;
742 crop->c.width = format.format.width;
743 crop->c.height = format.format.height;
744
745 return 0;
746}
747
748static int
749isp_video_set_crop(struct file *file, void *fh, struct v4l2_crop *crop)
750{
751 struct isp_video *video = video_drvdata(file);
752 struct v4l2_subdev *subdev;
753 int ret;
754
755 subdev = isp_video_remote_subdev(video, NULL);
756 if (subdev == NULL)
757 return -EINVAL;
758
759 mutex_lock(&video->mutex);
760 ret = v4l2_subdev_call(subdev, video, s_crop, crop);
761 mutex_unlock(&video->mutex);
762
763 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
764}
765
766static int
767isp_video_get_param(struct file *file, void *fh, struct v4l2_streamparm *a)
768{
769 struct isp_video_fh *vfh = to_isp_video_fh(fh);
770 struct isp_video *video = video_drvdata(file);
771
772 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
773 video->type != a->type)
774 return -EINVAL;
775
776 memset(a, 0, sizeof(*a));
777 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
778 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
779 a->parm.output.timeperframe = vfh->timeperframe;
780
781 return 0;
782}
783
784static int
785isp_video_set_param(struct file *file, void *fh, struct v4l2_streamparm *a)
786{
787 struct isp_video_fh *vfh = to_isp_video_fh(fh);
788 struct isp_video *video = video_drvdata(file);
789
790 if (video->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
791 video->type != a->type)
792 return -EINVAL;
793
794 if (a->parm.output.timeperframe.denominator == 0)
795 a->parm.output.timeperframe.denominator = 1;
796
797 vfh->timeperframe = a->parm.output.timeperframe;
798
799 return 0;
800}
801
802static int
803isp_video_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *rb)
804{
805 struct isp_video_fh *vfh = to_isp_video_fh(fh);
806
807 return omap3isp_video_queue_reqbufs(&vfh->queue, rb);
808}
809
810static int
811isp_video_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
812{
813 struct isp_video_fh *vfh = to_isp_video_fh(fh);
814
815 return omap3isp_video_queue_querybuf(&vfh->queue, b);
816}
817
818static int
819isp_video_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
820{
821 struct isp_video_fh *vfh = to_isp_video_fh(fh);
822
823 return omap3isp_video_queue_qbuf(&vfh->queue, b);
824}
825
826static int
827isp_video_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
828{
829 struct isp_video_fh *vfh = to_isp_video_fh(fh);
830
831 return omap3isp_video_queue_dqbuf(&vfh->queue, b,
832 file->f_flags & O_NONBLOCK);
833}
834
835/*
836 * Stream management
837 *
838 * Every ISP pipeline has a single input and a single output. The input can be
839 * either a sensor or a video node. The output is always a video node.
840 *
841 * As every pipeline has an output video node, the ISP video objects at the
842 * pipeline output stores the pipeline state. It tracks the streaming state of
843 * both the input and output, as well as the availability of buffers.
844 *
845 * In sensor-to-memory mode, frames are always available at the pipeline input.
846 * Starting the sensor usually requires I2C transfers and must be done in
847 * interruptible context. The pipeline is started and stopped synchronously
848 * to the stream on/off commands. All modules in the pipeline will get their
849 * subdev set stream handler called. The module at the end of the pipeline must
850 * delay starting the hardware until buffers are available at its output.
851 *
852 * In memory-to-memory mode, starting/stopping the stream requires
853 * synchronization between the input and output. ISP modules can't be stopped
854 * in the middle of a frame, and at least some of the modules seem to become
855 * busy as soon as they're started, even if they don't receive a frame start
856 * event. For that reason frames need to be processed in single-shot mode. The
857 * driver needs to wait until a frame is completely processed and written to
858 * memory before restarting the pipeline for the next frame. Pipelined
859 * processing might be possible but requires more testing.
860 *
861 * Stream start must be delayed until buffers are available at both the input
862 * and output. The pipeline must be started in the videobuf queue callback with
863 * the buffers queue spinlock held. The modules subdev set stream operation must
864 * not sleep.
865 */
866static int
867isp_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
868{
869 struct isp_video_fh *vfh = to_isp_video_fh(fh);
870 struct isp_video *video = video_drvdata(file);
871 enum isp_pipeline_state state;
872 struct isp_pipeline *pipe;
873 struct isp_video *far_end;
874 unsigned long flags;
875 int ret;
876
877 if (type != video->type)
878 return -EINVAL;
879
880 mutex_lock(&video->stream_lock);
881
882 if (video->streaming) {
883 mutex_unlock(&video->stream_lock);
884 return -EBUSY;
885 }
886
887 /* Start streaming on the pipeline. No link touching an entity in the
888 * pipeline can be activated or deactivated once streaming is started.
889 */
890 pipe = video->video.entity.pipe
891 ? to_isp_pipeline(&video->video.entity) : &video->pipe;
892 media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
893
894 /* Verify that the currently configured format matches the output of
895 * the connected subdev.
896 */
897 ret = isp_video_check_format(video, vfh);
898 if (ret < 0)
899 goto error;
900
901 video->bpl_padding = ret;
902 video->bpl_value = vfh->format.fmt.pix.bytesperline;
903
904 /* Find the ISP video node connected at the far end of the pipeline and
905 * update the pipeline.
906 */
907 far_end = isp_video_far_end(video);
908
909 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
910 state = ISP_PIPELINE_STREAM_OUTPUT | ISP_PIPELINE_IDLE_OUTPUT;
911 pipe->input = far_end;
912 pipe->output = video;
913 } else {
914 if (far_end == NULL) {
915 ret = -EPIPE;
916 goto error;
917 }
918
919 state = ISP_PIPELINE_STREAM_INPUT | ISP_PIPELINE_IDLE_INPUT;
920 pipe->input = video;
921 pipe->output = far_end;
922 }
923
924 /* Make sure the interconnect clock runs fast enough.
925 *
926 * Formula from: resource34xx.c set_opp()
927 * If MPU freq is above 500MHz, make sure the interconnect
928 * is at 100Mhz or above.
929 * throughput in KiB/s for 100 Mhz = 100 * 1000 * 4.
930 *
931 * We want to be fast enough then set OCP clock to be max as
932 * possible, in that case 185Mhz then:
933 * throughput in KiB/s for 185Mhz = 185 * 1000 * 4 = 740000 KiB/s
934 */
935 omap_pm_set_min_bus_tput(video->isp->dev, OCP_INITIATOR_AGENT, 740000);
936 pipe->l3_ick = clk_get_rate(video->isp->clock[ISP_CLK_L3_ICK]);
937
938 /* Validate the pipeline and update its state. */
939 ret = isp_video_validate_pipeline(pipe);
940 if (ret < 0)
941 goto error;
942
943 spin_lock_irqsave(&pipe->lock, flags);
944 pipe->state &= ~ISP_PIPELINE_STREAM;
945 pipe->state |= state;
946 spin_unlock_irqrestore(&pipe->lock, flags);
947
948 /* Set the maximum time per frame as the value requested by userspace.
949 * This is a soft limit that can be overridden if the hardware doesn't
950 * support the request limit.
951 */
952 if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
953 pipe->max_timeperframe = vfh->timeperframe;
954
955 video->queue = &vfh->queue;
956 INIT_LIST_HEAD(&video->dmaqueue);
957 atomic_set(&pipe->frame_number, -1);
958
959 ret = omap3isp_video_queue_streamon(&vfh->queue);
960 if (ret < 0)
961 goto error;
962
963 /* In sensor-to-memory mode, the stream can be started synchronously
964 * to the stream on command. In memory-to-memory mode, it will be
965 * started when buffers are queued on both the input and output.
966 */
967 if (pipe->input == NULL) {
968 ret = omap3isp_pipeline_set_stream(pipe,
969 ISP_PIPELINE_STREAM_CONTINUOUS);
970 if (ret < 0)
971 goto error;
972 spin_lock_irqsave(&video->queue->irqlock, flags);
973 if (list_empty(&video->dmaqueue))
974 video->dmaqueue_flags |= ISP_VIDEO_DMAQUEUE_UNDERRUN;
975 spin_unlock_irqrestore(&video->queue->irqlock, flags);
976 }
977
978error:
979 if (ret < 0) {
980 omap3isp_video_queue_streamoff(&vfh->queue);
981 omap_pm_set_min_bus_tput(video->isp->dev,
982 OCP_INITIATOR_AGENT, 0);
983 media_entity_pipeline_stop(&video->video.entity);
984 video->queue = NULL;
985 }
986
987 if (!ret)
988 video->streaming = 1;
989
990 mutex_unlock(&video->stream_lock);
991 return ret;
992}
993
994static int
995isp_video_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
996{
997 struct isp_video_fh *vfh = to_isp_video_fh(fh);
998 struct isp_video *video = video_drvdata(file);
999 struct isp_pipeline *pipe = to_isp_pipeline(&video->video.entity);
1000 enum isp_pipeline_state state;
1001 unsigned int streaming;
1002 unsigned long flags;
1003
1004 if (type != video->type)
1005 return -EINVAL;
1006
1007 mutex_lock(&video->stream_lock);
1008
1009 /* Make sure we're not streaming yet. */
1010 mutex_lock(&vfh->queue.lock);
1011 streaming = vfh->queue.streaming;
1012 mutex_unlock(&vfh->queue.lock);
1013
1014 if (!streaming)
1015 goto done;
1016
1017 /* Update the pipeline state. */
1018 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1019 state = ISP_PIPELINE_STREAM_OUTPUT
1020 | ISP_PIPELINE_QUEUE_OUTPUT;
1021 else
1022 state = ISP_PIPELINE_STREAM_INPUT
1023 | ISP_PIPELINE_QUEUE_INPUT;
1024
1025 spin_lock_irqsave(&pipe->lock, flags);
1026 pipe->state &= ~state;
1027 spin_unlock_irqrestore(&pipe->lock, flags);
1028
1029 /* Stop the stream. */
1030 omap3isp_pipeline_set_stream(pipe, ISP_PIPELINE_STREAM_STOPPED);
1031 omap3isp_video_queue_streamoff(&vfh->queue);
1032 video->queue = NULL;
1033 video->streaming = 0;
1034
1035 omap_pm_set_min_bus_tput(video->isp->dev, OCP_INITIATOR_AGENT, 0);
1036 media_entity_pipeline_stop(&video->video.entity);
1037
1038done:
1039 mutex_unlock(&video->stream_lock);
1040 return 0;
1041}
1042
1043static int
1044isp_video_enum_input(struct file *file, void *fh, struct v4l2_input *input)
1045{
1046 if (input->index > 0)
1047 return -EINVAL;
1048
1049 strlcpy(input->name, "camera", sizeof(input->name));
1050 input->type = V4L2_INPUT_TYPE_CAMERA;
1051
1052 return 0;
1053}
1054
1055static int
1056isp_video_g_input(struct file *file, void *fh, unsigned int *input)
1057{
1058 *input = 0;
1059
1060 return 0;
1061}
1062
1063static int
1064isp_video_s_input(struct file *file, void *fh, unsigned int input)
1065{
1066 return input == 0 ? 0 : -EINVAL;
1067}
1068
1069static const struct v4l2_ioctl_ops isp_video_ioctl_ops = {
1070 .vidioc_querycap = isp_video_querycap,
1071 .vidioc_g_fmt_vid_cap = isp_video_get_format,
1072 .vidioc_s_fmt_vid_cap = isp_video_set_format,
1073 .vidioc_try_fmt_vid_cap = isp_video_try_format,
1074 .vidioc_g_fmt_vid_out = isp_video_get_format,
1075 .vidioc_s_fmt_vid_out = isp_video_set_format,
1076 .vidioc_try_fmt_vid_out = isp_video_try_format,
1077 .vidioc_cropcap = isp_video_cropcap,
1078 .vidioc_g_crop = isp_video_get_crop,
1079 .vidioc_s_crop = isp_video_set_crop,
1080 .vidioc_g_parm = isp_video_get_param,
1081 .vidioc_s_parm = isp_video_set_param,
1082 .vidioc_reqbufs = isp_video_reqbufs,
1083 .vidioc_querybuf = isp_video_querybuf,
1084 .vidioc_qbuf = isp_video_qbuf,
1085 .vidioc_dqbuf = isp_video_dqbuf,
1086 .vidioc_streamon = isp_video_streamon,
1087 .vidioc_streamoff = isp_video_streamoff,
1088 .vidioc_enum_input = isp_video_enum_input,
1089 .vidioc_g_input = isp_video_g_input,
1090 .vidioc_s_input = isp_video_s_input,
1091};
1092
1093/* -----------------------------------------------------------------------------
1094 * V4L2 file operations
1095 */
1096
1097static int isp_video_open(struct file *file)
1098{
1099 struct isp_video *video = video_drvdata(file);
1100 struct isp_video_fh *handle;
1101 int ret = 0;
1102
1103 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1104 if (handle == NULL)
1105 return -ENOMEM;
1106
1107 v4l2_fh_init(&handle->vfh, &video->video);
1108 v4l2_fh_add(&handle->vfh);
1109
1110 /* If this is the first user, initialise the pipeline. */
1111 if (omap3isp_get(video->isp) == NULL) {
1112 ret = -EBUSY;
1113 goto done;
1114 }
1115
1116 ret = omap3isp_pipeline_pm_use(&video->video.entity, 1);
1117 if (ret < 0) {
1118 omap3isp_put(video->isp);
1119 goto done;
1120 }
1121
1122 omap3isp_video_queue_init(&handle->queue, video->type,
1123 &isp_video_queue_ops, video->isp->dev,
1124 sizeof(struct isp_buffer));
1125
1126 memset(&handle->format, 0, sizeof(handle->format));
1127 handle->format.type = video->type;
1128 handle->timeperframe.denominator = 1;
1129
1130 handle->video = video;
1131 file->private_data = &handle->vfh;
1132
1133done:
1134 if (ret < 0) {
1135 v4l2_fh_del(&handle->vfh);
1136 kfree(handle);
1137 }
1138
1139 return ret;
1140}
1141
1142static int isp_video_release(struct file *file)
1143{
1144 struct isp_video *video = video_drvdata(file);
1145 struct v4l2_fh *vfh = file->private_data;
1146 struct isp_video_fh *handle = to_isp_video_fh(vfh);
1147
1148 /* Disable streaming and free the buffers queue resources. */
1149 isp_video_streamoff(file, vfh, video->type);
1150
1151 mutex_lock(&handle->queue.lock);
1152 omap3isp_video_queue_cleanup(&handle->queue);
1153 mutex_unlock(&handle->queue.lock);
1154
1155 omap3isp_pipeline_pm_use(&video->video.entity, 0);
1156
1157 /* Release the file handle. */
1158 v4l2_fh_del(vfh);
1159 kfree(handle);
1160 file->private_data = NULL;
1161
1162 omap3isp_put(video->isp);
1163
1164 return 0;
1165}
1166
1167static unsigned int isp_video_poll(struct file *file, poll_table *wait)
1168{
1169 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1170 struct isp_video_queue *queue = &vfh->queue;
1171
1172 return omap3isp_video_queue_poll(queue, file, wait);
1173}
1174
1175static int isp_video_mmap(struct file *file, struct vm_area_struct *vma)
1176{
1177 struct isp_video_fh *vfh = to_isp_video_fh(file->private_data);
1178
1179 return omap3isp_video_queue_mmap(&vfh->queue, vma);
1180}
1181
1182static struct v4l2_file_operations isp_video_fops = {
1183 .owner = THIS_MODULE,
1184 .unlocked_ioctl = video_ioctl2,
1185 .open = isp_video_open,
1186 .release = isp_video_release,
1187 .poll = isp_video_poll,
1188 .mmap = isp_video_mmap,
1189};
1190
1191/* -----------------------------------------------------------------------------
1192 * ISP video core
1193 */
1194
1195static const struct isp_video_operations isp_video_dummy_ops = {
1196};
1197
1198int omap3isp_video_init(struct isp_video *video, const char *name)
1199{
1200 const char *direction;
1201 int ret;
1202
1203 switch (video->type) {
1204 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1205 direction = "output";
1206 video->pad.flags = MEDIA_PAD_FL_SINK;
1207 break;
1208 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1209 direction = "input";
1210 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1211 break;
1212
1213 default:
1214 return -EINVAL;
1215 }
1216
1217 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
1218 if (ret < 0)
1219 return ret;
1220
1221 mutex_init(&video->mutex);
1222 atomic_set(&video->active, 0);
1223
1224 spin_lock_init(&video->pipe.lock);
1225 mutex_init(&video->stream_lock);
1226
1227 /* Initialize the video device. */
1228 if (video->ops == NULL)
1229 video->ops = &isp_video_dummy_ops;
1230
1231 video->video.fops = &isp_video_fops;
1232 snprintf(video->video.name, sizeof(video->video.name),
1233 "OMAP3 ISP %s %s", name, direction);
1234 video->video.vfl_type = VFL_TYPE_GRABBER;
1235 video->video.release = video_device_release_empty;
1236 video->video.ioctl_ops = &isp_video_ioctl_ops;
1237 video->pipe.stream_state = ISP_PIPELINE_STREAM_STOPPED;
1238
1239 video_set_drvdata(&video->video, video);
1240
1241 return 0;
1242}
1243
1244int omap3isp_video_register(struct isp_video *video, struct v4l2_device *vdev)
1245{
1246 int ret;
1247
1248 video->video.v4l2_dev = vdev;
1249
1250 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1251 if (ret < 0)
1252 printk(KERN_ERR "%s: could not register video device (%d)\n",
1253 __func__, ret);
1254
1255 return ret;
1256}
1257
1258void omap3isp_video_unregister(struct isp_video *video)
1259{
1260 if (video_is_registered(&video->video)) {
1261 media_entity_cleanup(&video->video.entity);
1262 video_unregister_device(&video->video);
1263 }
1264}
diff --git a/drivers/media/video/omap3isp/ispvideo.h b/drivers/media/video/omap3isp/ispvideo.h
new file mode 100644
index 000000000000..524a1acd0906
--- /dev/null
+++ b/drivers/media/video/omap3isp/ispvideo.h
@@ -0,0 +1,202 @@
1/*
2 * ispvideo.h
3 *
4 * TI OMAP3 ISP - Generic video node
5 *
6 * Copyright (C) 2009-2010 Nokia Corporation
7 *
8 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * Sakari Ailus <sakari.ailus@iki.fi>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 */
25
26#ifndef OMAP3_ISP_VIDEO_H
27#define OMAP3_ISP_VIDEO_H
28
29#include <linux/v4l2-mediabus.h>
30#include <linux/version.h>
31#include <media/media-entity.h>
32#include <media/v4l2-dev.h>
33#include <media/v4l2-fh.h>
34
35#include "ispqueue.h"
36
37#define ISP_VIDEO_DRIVER_NAME "ispvideo"
38#define ISP_VIDEO_DRIVER_VERSION KERNEL_VERSION(0, 0, 1)
39
40struct isp_device;
41struct isp_video;
42struct v4l2_mbus_framefmt;
43struct v4l2_pix_format;
44
45/*
46 * struct isp_format_info - ISP media bus format information
47 * @code: V4L2 media bus format code
48 * @truncated: V4L2 media bus format code for the same format truncated to 10
49 * bits. Identical to @code if the format is 10 bits wide or less.
50 * @uncompressed: V4L2 media bus format code for the corresponding uncompressed
51 * format. Identical to @code if the format is not DPCM compressed.
52 * @pixelformat: V4L2 pixel format FCC identifier
53 * @bpp: Bits per pixel
54 */
55struct isp_format_info {
56 enum v4l2_mbus_pixelcode code;
57 enum v4l2_mbus_pixelcode truncated;
58 enum v4l2_mbus_pixelcode uncompressed;
59 u32 pixelformat;
60 unsigned int bpp;
61};
62
63enum isp_pipeline_stream_state {
64 ISP_PIPELINE_STREAM_STOPPED = 0,
65 ISP_PIPELINE_STREAM_CONTINUOUS = 1,
66 ISP_PIPELINE_STREAM_SINGLESHOT = 2,
67};
68
69enum isp_pipeline_state {
70 /* The stream has been started on the input video node. */
71 ISP_PIPELINE_STREAM_INPUT = 1,
72 /* The stream has been started on the output video node. */
73 ISP_PIPELINE_STREAM_OUTPUT = 2,
74 /* At least one buffer is queued on the input video node. */
75 ISP_PIPELINE_QUEUE_INPUT = 4,
76 /* At least one buffer is queued on the output video node. */
77 ISP_PIPELINE_QUEUE_OUTPUT = 8,
78 /* The input entity is idle, ready to be started. */
79 ISP_PIPELINE_IDLE_INPUT = 16,
80 /* The output entity is idle, ready to be started. */
81 ISP_PIPELINE_IDLE_OUTPUT = 32,
82 /* The pipeline is currently streaming. */
83 ISP_PIPELINE_STREAM = 64,
84};
85
86struct isp_pipeline {
87 struct media_pipeline pipe;
88 spinlock_t lock; /* Pipeline state and queue flags */
89 unsigned int state;
90 enum isp_pipeline_stream_state stream_state;
91 struct isp_video *input;
92 struct isp_video *output;
93 unsigned long l3_ick;
94 unsigned int max_rate;
95 atomic_t frame_number;
96 bool do_propagation; /* of frame number */
97 struct v4l2_fract max_timeperframe;
98};
99
100#define to_isp_pipeline(__e) \
101 container_of((__e)->pipe, struct isp_pipeline, pipe)
102
103static inline int isp_pipeline_ready(struct isp_pipeline *pipe)
104{
105 return pipe->state == (ISP_PIPELINE_STREAM_INPUT |
106 ISP_PIPELINE_STREAM_OUTPUT |
107 ISP_PIPELINE_QUEUE_INPUT |
108 ISP_PIPELINE_QUEUE_OUTPUT |
109 ISP_PIPELINE_IDLE_INPUT |
110 ISP_PIPELINE_IDLE_OUTPUT);
111}
112
113/*
114 * struct isp_buffer - ISP buffer
115 * @buffer: ISP video buffer
116 * @isp_addr: MMU mapped address (a.k.a. device address) of the buffer.
117 */
118struct isp_buffer {
119 struct isp_video_buffer buffer;
120 dma_addr_t isp_addr;
121};
122
123#define to_isp_buffer(buf) container_of(buf, struct isp_buffer, buffer)
124
125enum isp_video_dmaqueue_flags {
126 /* Set if DMA queue becomes empty when ISP_PIPELINE_STREAM_CONTINUOUS */
127 ISP_VIDEO_DMAQUEUE_UNDERRUN = (1 << 0),
128 /* Set when queuing buffer to an empty DMA queue */
129 ISP_VIDEO_DMAQUEUE_QUEUED = (1 << 1),
130};
131
132#define isp_video_dmaqueue_flags_clr(video) \
133 ({ (video)->dmaqueue_flags = 0; })
134
135/*
136 * struct isp_video_operations - ISP video operations
137 * @queue: Resume streaming when a buffer is queued. Called on VIDIOC_QBUF
138 * if there was no buffer previously queued.
139 */
140struct isp_video_operations {
141 int(*queue)(struct isp_video *video, struct isp_buffer *buffer);
142};
143
144struct isp_video {
145 struct video_device video;
146 enum v4l2_buf_type type;
147 struct media_pad pad;
148
149 struct mutex mutex; /* format and crop settings */
150 atomic_t active;
151
152 struct isp_device *isp;
153
154 unsigned int capture_mem;
155 unsigned int bpl_alignment; /* alignment value */
156 unsigned int bpl_zero_padding; /* whether the alignment is optional */
157 unsigned int bpl_max; /* maximum bytes per line value */
158 unsigned int bpl_value; /* bytes per line value */
159 unsigned int bpl_padding; /* padding at end of line */
160
161 /* Entity video node streaming */
162 unsigned int streaming:1;
163
164 /* Pipeline state */
165 struct isp_pipeline pipe;
166 struct mutex stream_lock; /* pipeline and stream states */
167
168 /* Video buffers queue */
169 struct isp_video_queue *queue;
170 struct list_head dmaqueue;
171 enum isp_video_dmaqueue_flags dmaqueue_flags;
172
173 const struct isp_video_operations *ops;
174};
175
176#define to_isp_video(vdev) container_of(vdev, struct isp_video, video)
177
178struct isp_video_fh {
179 struct v4l2_fh vfh;
180 struct isp_video *video;
181 struct isp_video_queue queue;
182 struct v4l2_format format;
183 struct v4l2_fract timeperframe;
184};
185
186#define to_isp_video_fh(fh) container_of(fh, struct isp_video_fh, vfh)
187#define isp_video_queue_to_isp_video_fh(q) \
188 container_of(q, struct isp_video_fh, queue)
189
190int omap3isp_video_init(struct isp_video *video, const char *name);
191int omap3isp_video_register(struct isp_video *video,
192 struct v4l2_device *vdev);
193void omap3isp_video_unregister(struct isp_video *video);
194struct isp_buffer *omap3isp_video_buffer_next(struct isp_video *video,
195 unsigned int error);
196void omap3isp_video_resume(struct isp_video *video, int continuous);
197struct media_pad *omap3isp_video_remote_pad(struct isp_video *video);
198
199const struct isp_format_info *
200omap3isp_video_format_info(enum v4l2_mbus_pixelcode code);
201
202#endif /* OMAP3_ISP_VIDEO_H */