Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 1 | /* |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 2 | * helper functions for SG DMA video4linux capture buffers |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 3 | * |
Magnus Damm | 5d6aaf5 | 2008-07-16 21:27:49 -0300 | [diff] [blame] | 4 | * The functions expect the hardware being able to scatter gather |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 5 | * (i.e. the buffers are not linear in physical memory, but fragmented |
| 6 | * into PAGE_SIZE chunks). They also assume the driver does not need |
| 7 | * to touch the video data. |
| 8 | * |
| 9 | * (c) 2007 Mauro Carvalho Chehab, <mchehab@infradead.org> |
| 10 | * |
| 11 | * Highly based on video-buf written originally by: |
| 12 | * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> |
| 13 | * (c) 2006 Mauro Carvalho Chehab, <mchehab@infradead.org> |
| 14 | * (c) 2006 Ted Walther and John Sokol |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/moduleparam.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 24 | #include <linux/sched.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 28 | #include <linux/dma-mapping.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/pagemap.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 32 | #include <asm/page.h> |
| 33 | #include <asm/pgtable.h> |
| 34 | |
| 35 | #include <media/videobuf-dma-sg.h> |
| 36 | |
| 37 | #define MAGIC_DMABUF 0x19721112 |
| 38 | #define MAGIC_SG_MEM 0x17890714 |
| 39 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 40 | #define MAGIC_CHECK(is, should) \ |
| 41 | if (unlikely((is) != (should))) { \ |
| 42 | printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \ |
| 43 | is, should); \ |
| 44 | BUG(); \ |
| 45 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 46 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 47 | static int debug; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 48 | module_param(debug, int, 0644); |
| 49 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 50 | MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 51 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
| 52 | MODULE_LICENSE("GPL"); |
| 53 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 54 | #define dprintk(level, fmt, arg...) \ |
| 55 | if (debug >= level) \ |
| 56 | printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 57 | |
| 58 | /* --------------------------------------------------------------------- */ |
| 59 | |
Laurent Pinchart | 7181772 | 2010-05-11 10:36:32 -0300 | [diff] [blame] | 60 | /* |
| 61 | * Return a scatterlist for some page-aligned vmalloc()'ed memory |
| 62 | * block (NULL on errors). Memory for the scatterlist is allocated |
| 63 | * using kmalloc. The caller must free the memory. |
| 64 | */ |
| 65 | static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt, |
| 66 | int nr_pages) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 67 | { |
| 68 | struct scatterlist *sglist; |
| 69 | struct page *pg; |
| 70 | int i; |
| 71 | |
Joe Perches | 101b25b | 2010-11-05 00:07:39 -0300 | [diff] [blame^] | 72 | sglist = vzalloc(nr_pages * sizeof(*sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 73 | if (NULL == sglist) |
| 74 | return NULL; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 75 | sg_init_table(sglist, nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 76 | for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) { |
| 77 | pg = vmalloc_to_page(virt); |
| 78 | if (NULL == pg) |
| 79 | goto err; |
| 80 | BUG_ON(PageHighMem(pg)); |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 81 | sg_set_page(&sglist[i], pg, PAGE_SIZE, 0); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 82 | } |
| 83 | return sglist; |
| 84 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 85 | err: |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 86 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
| 89 | |
Laurent Pinchart | 7181772 | 2010-05-11 10:36:32 -0300 | [diff] [blame] | 90 | /* |
| 91 | * Return a scatterlist for a an array of userpages (NULL on errors). |
| 92 | * Memory for the scatterlist is allocated using kmalloc. The caller |
| 93 | * must free the memory. |
| 94 | */ |
| 95 | static struct scatterlist *videobuf_pages_to_sg(struct page **pages, |
Hans Verkuil | 2fc1153 | 2010-09-07 06:10:45 -0300 | [diff] [blame] | 96 | int nr_pages, int offset, size_t size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 97 | { |
| 98 | struct scatterlist *sglist; |
Christophe Jaillet | a47cacb | 2008-07-04 06:33:22 -0300 | [diff] [blame] | 99 | int i; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 100 | |
| 101 | if (NULL == pages[0]) |
| 102 | return NULL; |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 103 | sglist = vmalloc(nr_pages * sizeof(*sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 104 | if (NULL == sglist) |
| 105 | return NULL; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 106 | sg_init_table(sglist, nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 107 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 108 | if (PageHighMem(pages[0])) |
| 109 | /* DMA to highmem pages might not work */ |
| 110 | goto highmem; |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 111 | sg_set_page(&sglist[0], pages[0], PAGE_SIZE - offset, offset); |
Hans Verkuil | 2fc1153 | 2010-09-07 06:10:45 -0300 | [diff] [blame] | 112 | size -= PAGE_SIZE - offset; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 113 | for (i = 1; i < nr_pages; i++) { |
| 114 | if (NULL == pages[i]) |
| 115 | goto nopage; |
| 116 | if (PageHighMem(pages[i])) |
| 117 | goto highmem; |
Mauro Carvalho Chehab | 1bede75 | 2010-10-07 09:31:33 -0300 | [diff] [blame] | 118 | sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0); |
| 119 | size -= min_t(size_t, PAGE_SIZE, size); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 120 | } |
| 121 | return sglist; |
| 122 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 123 | nopage: |
| 124 | dprintk(2, "sgl: oops - no page\n"); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 125 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 126 | return NULL; |
| 127 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 128 | highmem: |
| 129 | dprintk(2, "sgl: oops - highmem page\n"); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 130 | vfree(sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | /* --------------------------------------------------------------------- */ |
| 135 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 136 | struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 137 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 138 | struct videobuf_dma_sg_memory *mem = buf->priv; |
| 139 | BUG_ON(!mem); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 140 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 141 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 142 | |
| 143 | return &mem->dma; |
| 144 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 145 | EXPORT_SYMBOL_GPL(videobuf_to_dma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 146 | |
| 147 | void videobuf_dma_init(struct videobuf_dmabuf *dma) |
| 148 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 149 | memset(dma, 0, sizeof(*dma)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 150 | dma->magic = MAGIC_DMABUF; |
| 151 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 152 | EXPORT_SYMBOL_GPL(videobuf_dma_init); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 153 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 154 | static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma, |
| 155 | int direction, unsigned long data, unsigned long size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 156 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 157 | unsigned long first, last; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 158 | int err, rw = 0; |
| 159 | |
| 160 | dma->direction = direction; |
| 161 | switch (dma->direction) { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 162 | case DMA_FROM_DEVICE: |
| 163 | rw = READ; |
| 164 | break; |
| 165 | case DMA_TO_DEVICE: |
| 166 | rw = WRITE; |
| 167 | break; |
| 168 | default: |
| 169 | BUG(); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | first = (data & PAGE_MASK) >> PAGE_SHIFT; |
| 173 | last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT; |
Hans Verkuil | 2fc1153 | 2010-09-07 06:10:45 -0300 | [diff] [blame] | 174 | dma->offset = data & ~PAGE_MASK; |
| 175 | dma->size = size; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 176 | dma->nr_pages = last-first+1; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 177 | dma->pages = kmalloc(dma->nr_pages * sizeof(struct page *), GFP_KERNEL); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 178 | if (NULL == dma->pages) |
| 179 | return -ENOMEM; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 180 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 181 | dprintk(1, "init user [0x%lx+0x%lx => %d pages]\n", |
| 182 | data, size, dma->nr_pages); |
| 183 | |
| 184 | err = get_user_pages(current, current->mm, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 185 | data & PAGE_MASK, dma->nr_pages, |
| 186 | rw == READ, 1, /* force */ |
| 187 | dma->pages, NULL); |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 188 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 189 | if (err != dma->nr_pages) { |
| 190 | dma->nr_pages = (err >= 0) ? err : 0; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 191 | dprintk(1, "get_user_pages: err=%d [%d]\n", err, dma->nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 192 | return err < 0 ? err : -EINVAL; |
| 193 | } |
| 194 | return 0; |
| 195 | } |
| 196 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 197 | int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction, |
| 198 | unsigned long data, unsigned long size) |
| 199 | { |
| 200 | int ret; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 201 | |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 202 | down_read(¤t->mm->mmap_sem); |
| 203 | ret = videobuf_dma_init_user_locked(dma, direction, data, size); |
| 204 | up_read(¤t->mm->mmap_sem); |
| 205 | |
| 206 | return ret; |
| 207 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 208 | EXPORT_SYMBOL_GPL(videobuf_dma_init_user); |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 209 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 210 | int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, |
| 211 | int nr_pages) |
| 212 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 213 | dprintk(1, "init kernel [%d pages]\n", nr_pages); |
| 214 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 215 | dma->direction = direction; |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 216 | dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT); |
| 217 | if (NULL == dma->vaddr) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 218 | dprintk(1, "vmalloc_32(%d pages) failed\n", nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 219 | return -ENOMEM; |
| 220 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 221 | |
| 222 | dprintk(1, "vmalloc is at addr 0x%08lx, size=%d\n", |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 223 | (unsigned long)dma->vaddr, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 224 | nr_pages << PAGE_SHIFT); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 225 | |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 226 | memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 227 | dma->nr_pages = nr_pages; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 228 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 229 | return 0; |
| 230 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 231 | EXPORT_SYMBOL_GPL(videobuf_dma_init_kernel); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 232 | |
| 233 | int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction, |
| 234 | dma_addr_t addr, int nr_pages) |
| 235 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 236 | dprintk(1, "init overlay [%d pages @ bus 0x%lx]\n", |
| 237 | nr_pages, (unsigned long)addr); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 238 | dma->direction = direction; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 239 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 240 | if (0 == addr) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | dma->bus_addr = addr; |
| 244 | dma->nr_pages = nr_pages; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 245 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 246 | return 0; |
| 247 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 248 | EXPORT_SYMBOL_GPL(videobuf_dma_init_overlay); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 249 | |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 250 | int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 251 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 252 | MAGIC_CHECK(dma->magic, MAGIC_DMABUF); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 253 | BUG_ON(0 == dma->nr_pages); |
| 254 | |
| 255 | if (dma->pages) { |
| 256 | dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages, |
Hans Verkuil | 2fc1153 | 2010-09-07 06:10:45 -0300 | [diff] [blame] | 257 | dma->offset, dma->size); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 258 | } |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 259 | if (dma->vaddr) { |
| 260 | dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr, |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 261 | dma->nr_pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 262 | } |
| 263 | if (dma->bus_addr) { |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 264 | dma->sglist = vmalloc(sizeof(*dma->sglist)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 265 | if (NULL != dma->sglist) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 266 | dma->sglen = 1; |
| 267 | sg_dma_address(&dma->sglist[0]) = dma->bus_addr |
| 268 | & PAGE_MASK; |
| 269 | dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK; |
| 270 | sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | if (NULL == dma->sglist) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 274 | dprintk(1, "scatterlist is NULL\n"); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 275 | return -ENOMEM; |
| 276 | } |
| 277 | if (!dma->bus_addr) { |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 278 | dma->sglen = dma_map_sg(dev, dma->sglist, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 279 | dma->nr_pages, dma->direction); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 280 | if (0 == dma->sglen) { |
| 281 | printk(KERN_WARNING |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 282 | "%s: videobuf_map_sg failed\n", __func__); |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 283 | vfree(dma->sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 284 | dma->sglist = NULL; |
| 285 | dma->sglen = 0; |
Figo.zhang | 92051b2 | 2009-06-10 23:17:27 -0300 | [diff] [blame] | 286 | return -ENOMEM; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 287 | } |
| 288 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 289 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 290 | return 0; |
| 291 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 292 | EXPORT_SYMBOL_GPL(videobuf_dma_map); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 293 | |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 294 | int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 295 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 296 | MAGIC_CHECK(dma->magic, MAGIC_DMABUF); |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 297 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 298 | if (!dma->sglen) |
| 299 | return 0; |
| 300 | |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 301 | dma_unmap_sg(dev, dma->sglist, dma->sglen, dma->direction); |
Mauro Carvalho Chehab | 5ddff43 | 2007-10-08 11:43:49 -0300 | [diff] [blame] | 302 | |
Cohen David.A | 1010ed1 | 2009-05-11 11:00:20 -0300 | [diff] [blame] | 303 | vfree(dma->sglist); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 304 | dma->sglist = NULL; |
| 305 | dma->sglen = 0; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 306 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 307 | return 0; |
| 308 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 309 | EXPORT_SYMBOL_GPL(videobuf_dma_unmap); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 310 | |
| 311 | int videobuf_dma_free(struct videobuf_dmabuf *dma) |
| 312 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 313 | int i; |
| 314 | MAGIC_CHECK(dma->magic, MAGIC_DMABUF); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 315 | BUG_ON(dma->sglen); |
| 316 | |
| 317 | if (dma->pages) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 318 | for (i = 0; i < dma->nr_pages; i++) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 319 | page_cache_release(dma->pages[i]); |
| 320 | kfree(dma->pages); |
| 321 | dma->pages = NULL; |
| 322 | } |
| 323 | |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 324 | vfree(dma->vaddr); |
| 325 | dma->vaddr = NULL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 326 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 327 | if (dma->bus_addr) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 328 | dma->bus_addr = 0; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 329 | dma->direction = DMA_NONE; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 330 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 331 | return 0; |
| 332 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 333 | EXPORT_SYMBOL_GPL(videobuf_dma_free); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 334 | |
| 335 | /* --------------------------------------------------------------------- */ |
| 336 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 337 | static void videobuf_vm_open(struct vm_area_struct *vma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 338 | { |
| 339 | struct videobuf_mapping *map = vma->vm_private_data; |
| 340 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 341 | dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map, |
| 342 | map->count, vma->vm_start, vma->vm_end); |
| 343 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 344 | map->count++; |
| 345 | } |
| 346 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 347 | static void videobuf_vm_close(struct vm_area_struct *vma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 348 | { |
| 349 | struct videobuf_mapping *map = vma->vm_private_data; |
| 350 | struct videobuf_queue *q = map->q; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 351 | struct videobuf_dma_sg_memory *mem; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 352 | int i; |
| 353 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 354 | dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map, |
| 355 | map->count, vma->vm_start, vma->vm_end); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 356 | |
| 357 | map->count--; |
| 358 | if (0 == map->count) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 359 | dprintk(1, "munmap %p q=%p\n", map, q); |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 360 | videobuf_queue_lock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 361 | for (i = 0; i < VIDEO_MAX_FRAME; i++) { |
| 362 | if (NULL == q->bufs[i]) |
| 363 | continue; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 364 | mem = q->bufs[i]->priv; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 365 | if (!mem) |
| 366 | continue; |
| 367 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 368 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 369 | |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 370 | if (q->bufs[i]->map != map) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 371 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 372 | q->bufs[i]->map = NULL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 373 | q->bufs[i]->baddr = 0; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 374 | q->ops->buf_release(q, q->bufs[i]); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 375 | } |
Hans Verkuil | 9739768 | 2010-09-20 17:24:30 -0300 | [diff] [blame] | 376 | videobuf_queue_unlock(q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 377 | kfree(map); |
| 378 | } |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Get a anonymous page for the mapping. Make sure we can DMA to that |
| 384 | * memory location with 32bit PCI devices (i.e. don't use highmem for |
| 385 | * now ...). Bounce buffers don't work very well for the data rates |
| 386 | * video capture has. |
| 387 | */ |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 388 | static int videobuf_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 389 | { |
| 390 | struct page *page; |
| 391 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 392 | dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n", |
| 393 | (unsigned long)vmf->virtual_address, |
| 394 | vma->vm_start, vma->vm_end); |
| 395 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 396 | page = alloc_page(GFP_USER | __GFP_DMA32); |
| 397 | if (!page) |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 398 | return VM_FAULT_OOM; |
Guennadi Liakhovetski | c0cd501 | 2009-01-03 18:20:04 -0300 | [diff] [blame] | 399 | clear_user_highpage(page, (unsigned long)vmf->virtual_address); |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 400 | vmf->page = page; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 401 | |
Nick Piggin | 105354a | 2007-12-07 17:57:38 -0300 | [diff] [blame] | 402 | return 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 403 | } |
| 404 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 405 | static const struct vm_operations_struct videobuf_vm_ops = { |
| 406 | .open = videobuf_vm_open, |
| 407 | .close = videobuf_vm_close, |
| 408 | .fault = videobuf_vm_fault, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | /* --------------------------------------------------------------------- |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 412 | * SG handlers for the generic methods |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 413 | */ |
| 414 | |
| 415 | /* Allocated area consists on 3 parts: |
| 416 | struct video_buffer |
| 417 | struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 418 | struct videobuf_dma_sg_memory |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 419 | */ |
| 420 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 421 | static struct videobuf_buffer *__videobuf_alloc_vb(size_t size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 422 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 423 | struct videobuf_dma_sg_memory *mem; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 424 | struct videobuf_buffer *vb; |
| 425 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 426 | vb = kzalloc(size + sizeof(*mem), GFP_KERNEL); |
Pawel Osciak | bee527f | 2010-02-22 13:10:06 -0300 | [diff] [blame] | 427 | if (!vb) |
| 428 | return vb; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 429 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 430 | mem = vb->priv = ((char *)vb) + size; |
| 431 | mem->magic = MAGIC_SG_MEM; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 432 | |
| 433 | videobuf_dma_init(&mem->dma); |
| 434 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 435 | dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n", |
| 436 | __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb), |
| 437 | mem, (long)sizeof(*mem)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 438 | |
| 439 | return vb; |
| 440 | } |
| 441 | |
Hans Verkuil | 037c75e | 2010-03-28 08:18:37 -0300 | [diff] [blame] | 442 | static void *__videobuf_to_vaddr(struct videobuf_buffer *buf) |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 443 | { |
| 444 | struct videobuf_dma_sg_memory *mem = buf->priv; |
| 445 | BUG_ON(!mem); |
| 446 | |
| 447 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
| 448 | |
Laurent Pinchart | bb6dbe7 | 2010-05-11 10:36:34 -0300 | [diff] [blame] | 449 | return mem->dma.vaddr; |
Mauro Carvalho Chehab | 59d3448 | 2008-04-13 15:10:00 -0300 | [diff] [blame] | 450 | } |
| 451 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 452 | static int __videobuf_iolock(struct videobuf_queue *q, |
| 453 | struct videobuf_buffer *vb, |
| 454 | struct v4l2_framebuffer *fbuf) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 455 | { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 456 | int err, pages; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 457 | dma_addr_t bus; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 458 | struct videobuf_dma_sg_memory *mem = vb->priv; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 459 | BUG_ON(!mem); |
| 460 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 461 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 462 | |
| 463 | switch (vb->memory) { |
| 464 | case V4L2_MEMORY_MMAP: |
| 465 | case V4L2_MEMORY_USERPTR: |
| 466 | if (0 == vb->baddr) { |
| 467 | /* no userspace addr -- kernel bounce buffer */ |
| 468 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 469 | err = videobuf_dma_init_kernel(&mem->dma, |
| 470 | DMA_FROM_DEVICE, |
| 471 | pages); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 472 | if (0 != err) |
| 473 | return err; |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 474 | } else if (vb->memory == V4L2_MEMORY_USERPTR) { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 475 | /* dma directly to userspace */ |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 476 | err = videobuf_dma_init_user(&mem->dma, |
| 477 | DMA_FROM_DEVICE, |
| 478 | vb->baddr, vb->bsize); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 479 | if (0 != err) |
| 480 | return err; |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 481 | } else { |
| 482 | /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP |
| 483 | buffers can only be called from videobuf_qbuf |
| 484 | we take current->mm->mmap_sem there, to prevent |
| 485 | locking inversion, so don't take it here */ |
| 486 | |
| 487 | err = videobuf_dma_init_user_locked(&mem->dma, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 488 | DMA_FROM_DEVICE, |
Maxim Levitsky | 9900132 | 2007-09-27 20:34:09 -0300 | [diff] [blame] | 489 | vb->baddr, vb->bsize); |
| 490 | if (0 != err) |
| 491 | return err; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 492 | } |
| 493 | break; |
| 494 | case V4L2_MEMORY_OVERLAY: |
| 495 | if (NULL == fbuf) |
| 496 | return -EINVAL; |
| 497 | /* FIXME: need sanity checks for vb->boff */ |
| 498 | /* |
| 499 | * Using a double cast to avoid compiler warnings when |
| 500 | * building for PAE. Compiler doesn't like direct casting |
| 501 | * of a 32 bit ptr to 64 bit integer. |
| 502 | */ |
| 503 | bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff; |
| 504 | pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 505 | err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 506 | bus, pages); |
| 507 | if (0 != err) |
| 508 | return err; |
| 509 | break; |
| 510 | default: |
| 511 | BUG(); |
| 512 | } |
Laurent Pinchart | 9526840 | 2010-05-11 10:36:30 -0300 | [diff] [blame] | 513 | err = videobuf_dma_map(q->dev, &mem->dma); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 514 | if (0 != err) |
| 515 | return err; |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static int __videobuf_sync(struct videobuf_queue *q, |
| 521 | struct videobuf_buffer *buf) |
| 522 | { |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 523 | struct videobuf_dma_sg_memory *mem = buf->priv; |
Mauro Carvalho Chehab | 97f8105 | 2010-05-05 16:23:09 -0300 | [diff] [blame] | 524 | BUG_ON(!mem || !mem->dma.sglen); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 525 | |
Mauro Carvalho Chehab | 97f8105 | 2010-05-05 16:23:09 -0300 | [diff] [blame] | 526 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
| 527 | MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF); |
| 528 | |
| 529 | dma_sync_sg_for_cpu(q->dev, mem->dma.sglist, |
Arnout Vandecappelle | fc7f8fd | 2010-03-17 19:53:04 -0300 | [diff] [blame] | 530 | mem->dma.sglen, mem->dma.direction); |
Mauro Carvalho Chehab | 97f8105 | 2010-05-05 16:23:09 -0300 | [diff] [blame] | 531 | |
| 532 | return 0; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 533 | } |
| 534 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 535 | static int __videobuf_mmap_mapper(struct videobuf_queue *q, |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 536 | struct videobuf_buffer *buf, |
| 537 | struct vm_area_struct *vma) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 538 | { |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 539 | struct videobuf_dma_sg_memory *mem = buf->priv; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 540 | struct videobuf_mapping *map; |
Mauro Carvalho Chehab | 474675a | 2010-04-25 11:23:52 -0300 | [diff] [blame] | 541 | unsigned int first, last, size = 0, i; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 542 | int retval; |
| 543 | |
| 544 | retval = -EINVAL; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 545 | |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 546 | /* This function maintains backwards compatibility with V4L1 and will |
| 547 | * map more than one buffer if the vma length is equal to the combined |
| 548 | * size of multiple buffers than it will map them together. See |
| 549 | * VIDIOCGMBUF in the v4l spec |
| 550 | * |
| 551 | * TODO: Allow drivers to specify if they support this mode |
| 552 | */ |
| 553 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 554 | BUG_ON(!mem); |
| 555 | MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); |
| 556 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 557 | /* look for first buffer to map */ |
| 558 | for (first = 0; first < VIDEO_MAX_FRAME; first++) { |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 559 | if (buf == q->bufs[first]) { |
| 560 | size = PAGE_ALIGN(q->bufs[first]->bsize); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 561 | break; |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 562 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 563 | } |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 564 | |
| 565 | /* paranoia, should never happen since buf is always valid. */ |
Mauro Carvalho Chehab | 474675a | 2010-04-25 11:23:52 -0300 | [diff] [blame] | 566 | if (!size) { |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 567 | dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n", |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 568 | (vma->vm_pgoff << PAGE_SHIFT)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 569 | goto done; |
| 570 | } |
| 571 | |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 572 | last = first; |
| 573 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 574 | if (size != (vma->vm_end - vma->vm_start)) { |
| 575 | /* look for last buffer to map */ |
| 576 | for (last = first + 1; last < VIDEO_MAX_FRAME; last++) { |
| 577 | if (NULL == q->bufs[last]) |
| 578 | continue; |
| 579 | if (V4L2_MEMORY_MMAP != q->bufs[last]->memory) |
| 580 | continue; |
| 581 | if (q->bufs[last]->map) { |
| 582 | retval = -EBUSY; |
| 583 | goto done; |
| 584 | } |
| 585 | size += PAGE_ALIGN(q->bufs[last]->bsize); |
| 586 | if (size == (vma->vm_end - vma->vm_start)) |
| 587 | break; |
| 588 | } |
| 589 | if (VIDEO_MAX_FRAME == last) { |
| 590 | dprintk(1, "mmap app bug: size invalid [size=0x%lx]\n", |
| 591 | (vma->vm_end - vma->vm_start)); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 592 | goto done; |
| 593 | } |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 594 | } |
Hans Verkuil | 0b62b73 | 2010-03-28 09:09:05 -0300 | [diff] [blame] | 595 | #endif |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 596 | |
| 597 | /* create mapping + update buffer list */ |
| 598 | retval = -ENOMEM; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 599 | map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 600 | if (NULL == map) |
| 601 | goto done; |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 602 | |
| 603 | size = 0; |
| 604 | for (i = first; i <= last; i++) { |
| 605 | if (NULL == q->bufs[i]) |
| 606 | continue; |
Mauro Carvalho Chehab | 851c0c9 | 2007-09-27 18:25:44 -0300 | [diff] [blame] | 607 | q->bufs[i]->map = map; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 608 | q->bufs[i]->baddr = vma->vm_start + size; |
Tuukka Toivonen | 7cbefad | 2009-07-23 10:56:25 -0300 | [diff] [blame] | 609 | size += PAGE_ALIGN(q->bufs[i]->bsize); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 610 | } |
Brandon Philips | ce54093 | 2008-04-02 18:10:59 -0300 | [diff] [blame] | 611 | |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 612 | map->count = 1; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 613 | map->q = q; |
| 614 | vma->vm_ops = &videobuf_vm_ops; |
| 615 | vma->vm_flags |= VM_DONTEXPAND | VM_RESERVED; |
| 616 | vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */ |
| 617 | vma->vm_private_data = map; |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 618 | dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n", |
| 619 | map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 620 | retval = 0; |
| 621 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 622 | done: |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 623 | return retval; |
| 624 | } |
| 625 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 626 | static struct videobuf_qtype_ops sg_ops = { |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 627 | .magic = MAGIC_QTYPE_OPS, |
| 628 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 629 | .alloc_vb = __videobuf_alloc_vb, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 630 | .iolock = __videobuf_iolock, |
| 631 | .sync = __videobuf_sync, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 632 | .mmap_mapper = __videobuf_mmap_mapper, |
Hans Verkuil | 037c75e | 2010-03-28 08:18:37 -0300 | [diff] [blame] | 633 | .vaddr = __videobuf_to_vaddr, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 634 | }; |
| 635 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 636 | void *videobuf_sg_alloc(size_t size) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 637 | { |
| 638 | struct videobuf_queue q; |
| 639 | |
| 640 | /* Required to make generic handler to call __videobuf_alloc */ |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 641 | q.int_ops = &sg_ops; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 642 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 643 | q.msize = size; |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 644 | |
Pawel Osciak | 33c3828 | 2010-05-11 10:36:28 -0300 | [diff] [blame] | 645 | return videobuf_alloc_vb(&q); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 646 | } |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 647 | EXPORT_SYMBOL_GPL(videobuf_sg_alloc); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 648 | |
Pawel Osciak | 7a02264 | 2010-03-17 04:01:04 -0300 | [diff] [blame] | 649 | void videobuf_queue_sg_init(struct videobuf_queue *q, |
Jonathan Corbet | 38a54f3 | 2009-11-17 19:43:41 -0300 | [diff] [blame] | 650 | const struct videobuf_queue_ops *ops, |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 651 | struct device *dev, |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 652 | spinlock_t *irqlock, |
| 653 | enum v4l2_buf_type type, |
| 654 | enum v4l2_field field, |
| 655 | unsigned int msize, |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 656 | void *priv, |
| 657 | struct mutex *ext_lock) |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 658 | { |
Mauro Carvalho Chehab | d4cae5a | 2007-10-08 12:20:02 -0300 | [diff] [blame] | 659 | videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 660 | priv, &sg_ops, ext_lock); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 661 | } |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 662 | EXPORT_SYMBOL_GPL(videobuf_queue_sg_init); |
Mauro Carvalho Chehab | 7a7d9a8 | 2007-08-23 16:26:14 -0300 | [diff] [blame] | 663 | |