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