Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

Linux/drivers/staging/android/ion/ion_test.c

  1 /*
  2  *
  3  * Copyright (C) 2013 Google, Inc.
  4  *
  5  * This software is licensed under the terms of the GNU General Public
  6  * License version 2, as published by the Free Software Foundation, and
  7  * may be copied, distributed, and modified under those terms.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  *
 14  */
 15 
 16 #define pr_fmt(fmt) "ion-test: " fmt
 17 
 18 #include <linux/dma-buf.h>
 19 #include <linux/dma-direction.h>
 20 #include <linux/fs.h>
 21 #include <linux/miscdevice.h>
 22 #include <linux/mm.h>
 23 #include <linux/module.h>
 24 #include <linux/platform_device.h>
 25 #include <linux/sched.h>
 26 #include <linux/slab.h>
 27 #include <linux/uaccess.h>
 28 #include <linux/vmalloc.h>
 29 
 30 #include "ion.h"
 31 #include "../uapi/ion_test.h"
 32 
 33 #define u64_to_uptr(x) ((void __user *)(unsigned long)(x))
 34 
 35 struct ion_test_device {
 36         struct miscdevice misc;
 37 };
 38 
 39 struct ion_test_data {
 40         struct dma_buf *dma_buf;
 41         struct device *dev;
 42 };
 43 
 44 static int ion_handle_test_dma(struct device *dev, struct dma_buf *dma_buf,
 45                                void __user *ptr, size_t offset, size_t size,
 46                                bool write)
 47 {
 48         int ret = 0;
 49         struct dma_buf_attachment *attach;
 50         struct sg_table *table;
 51         pgprot_t pgprot = pgprot_writecombine(PAGE_KERNEL);
 52         enum dma_data_direction dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
 53         struct sg_page_iter sg_iter;
 54         unsigned long offset_page;
 55 
 56         attach = dma_buf_attach(dma_buf, dev);
 57         if (IS_ERR(attach))
 58                 return PTR_ERR(attach);
 59 
 60         table = dma_buf_map_attachment(attach, dir);
 61         if (IS_ERR(table))
 62                 return PTR_ERR(table);
 63 
 64         offset_page = offset >> PAGE_SHIFT;
 65         offset %= PAGE_SIZE;
 66 
 67         for_each_sg_page(table->sgl, &sg_iter, table->nents, offset_page) {
 68                 struct page *page = sg_page_iter_page(&sg_iter);
 69                 void *vaddr = vmap(&page, 1, VM_MAP, pgprot);
 70                 size_t to_copy = PAGE_SIZE - offset;
 71 
 72                 to_copy = min(to_copy, size);
 73                 if (!vaddr) {
 74                         ret = -ENOMEM;
 75                         goto err;
 76                 }
 77 
 78                 if (write)
 79                         ret = copy_from_user(vaddr + offset, ptr, to_copy);
 80                 else
 81                         ret = copy_to_user(ptr, vaddr + offset, to_copy);
 82 
 83                 vunmap(vaddr);
 84                 if (ret) {
 85                         ret = -EFAULT;
 86                         goto err;
 87                 }
 88                 size -= to_copy;
 89                 if (!size)
 90                         break;
 91                 ptr += to_copy;
 92                 offset = 0;
 93         }
 94 
 95 err:
 96         dma_buf_unmap_attachment(attach, table, dir);
 97         dma_buf_detach(dma_buf, attach);
 98         return ret;
 99 }
100 
101 static int ion_handle_test_kernel(struct dma_buf *dma_buf, void __user *ptr,
102                                   size_t offset, size_t size, bool write)
103 {
104         int ret;
105         unsigned long page_offset = offset >> PAGE_SHIFT;
106         size_t copy_offset = offset % PAGE_SIZE;
107         size_t copy_size = size;
108         enum dma_data_direction dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
109 
110         if (offset > dma_buf->size || size > dma_buf->size - offset)
111                 return -EINVAL;
112 
113         ret = dma_buf_begin_cpu_access(dma_buf, dir);
114         if (ret)
115                 return ret;
116 
117         while (copy_size > 0) {
118                 size_t to_copy;
119                 void *vaddr = dma_buf_kmap(dma_buf, page_offset);
120 
121                 if (!vaddr)
122                         goto err;
123 
124                 to_copy = min_t(size_t, PAGE_SIZE - copy_offset, copy_size);
125 
126                 if (write)
127                         ret = copy_from_user(vaddr + copy_offset, ptr, to_copy);
128                 else
129                         ret = copy_to_user(ptr, vaddr + copy_offset, to_copy);
130 
131                 dma_buf_kunmap(dma_buf, page_offset, vaddr);
132                 if (ret) {
133                         ret = -EFAULT;
134                         goto err;
135                 }
136 
137                 copy_size -= to_copy;
138                 ptr += to_copy;
139                 page_offset++;
140                 copy_offset = 0;
141         }
142 err:
143         dma_buf_end_cpu_access(dma_buf, dir);
144         return ret;
145 }
146 
147 static long ion_test_ioctl(struct file *filp, unsigned int cmd,
148                            unsigned long arg)
149 {
150         struct ion_test_data *test_data = filp->private_data;
151         int ret = 0;
152 
153         union {
154                 struct ion_test_rw_data test_rw;
155         } data;
156 
157         if (_IOC_SIZE(cmd) > sizeof(data))
158                 return -EINVAL;
159 
160         if (_IOC_DIR(cmd) & _IOC_WRITE)
161                 if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
162                         return -EFAULT;
163 
164         switch (cmd) {
165         case ION_IOC_TEST_SET_FD:
166         {
167                 struct dma_buf *dma_buf = NULL;
168                 int fd = arg;
169 
170                 if (fd >= 0) {
171                         dma_buf = dma_buf_get((int)arg);
172                         if (IS_ERR(dma_buf))
173                                 return PTR_ERR(dma_buf);
174                 }
175                 if (test_data->dma_buf)
176                         dma_buf_put(test_data->dma_buf);
177                 test_data->dma_buf = dma_buf;
178                 break;
179         }
180         case ION_IOC_TEST_DMA_MAPPING:
181         {
182                 ret = ion_handle_test_dma(test_data->dev, test_data->dma_buf,
183                                           u64_to_uptr(data.test_rw.ptr),
184                                           data.test_rw.offset,
185                                           data.test_rw.size,
186                                           data.test_rw.write);
187                 break;
188         }
189         case ION_IOC_TEST_KERNEL_MAPPING:
190         {
191                 ret = ion_handle_test_kernel(test_data->dma_buf,
192                                              u64_to_uptr(data.test_rw.ptr),
193                                              data.test_rw.offset,
194                                              data.test_rw.size,
195                                              data.test_rw.write);
196                 break;
197         }
198         default:
199                 return -ENOTTY;
200         }
201 
202         if (_IOC_DIR(cmd) & _IOC_READ) {
203                 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
204                         return -EFAULT;
205         }
206         return ret;
207 }
208 
209 static int ion_test_open(struct inode *inode, struct file *file)
210 {
211         struct ion_test_data *data;
212         struct miscdevice *miscdev = file->private_data;
213 
214         data = kzalloc(sizeof(*data), GFP_KERNEL);
215         if (!data)
216                 return -ENOMEM;
217 
218         data->dev = miscdev->parent;
219 
220         file->private_data = data;
221 
222         return 0;
223 }
224 
225 static int ion_test_release(struct inode *inode, struct file *file)
226 {
227         struct ion_test_data *data = file->private_data;
228 
229         kfree(data);
230 
231         return 0;
232 }
233 
234 static const struct file_operations ion_test_fops = {
235         .owner = THIS_MODULE,
236         .unlocked_ioctl = ion_test_ioctl,
237         .compat_ioctl = ion_test_ioctl,
238         .open = ion_test_open,
239         .release = ion_test_release,
240 };
241 
242 static int __init ion_test_probe(struct platform_device *pdev)
243 {
244         int ret;
245         struct ion_test_device *testdev;
246 
247         testdev = devm_kzalloc(&pdev->dev, sizeof(struct ion_test_device),
248                                GFP_KERNEL);
249         if (!testdev)
250                 return -ENOMEM;
251 
252         testdev->misc.minor = MISC_DYNAMIC_MINOR;
253         testdev->misc.name = "ion-test";
254         testdev->misc.fops = &ion_test_fops;
255         testdev->misc.parent = &pdev->dev;
256         ret = misc_register(&testdev->misc);
257         if (ret) {
258                 pr_err("failed to register misc device.\n");
259                 return ret;
260         }
261 
262         platform_set_drvdata(pdev, testdev);
263 
264         return 0;
265 }
266 
267 static int ion_test_remove(struct platform_device *pdev)
268 {
269         struct ion_test_device *testdev;
270 
271         testdev = platform_get_drvdata(pdev);
272         if (!testdev)
273                 return -ENODATA;
274 
275         misc_deregister(&testdev->misc);
276         return 0;
277 }
278 
279 static struct platform_device *ion_test_pdev;
280 static struct platform_driver ion_test_platform_driver = {
281         .remove = ion_test_remove,
282         .driver = {
283                 .name = "ion-test",
284         },
285 };
286 
287 static int __init ion_test_init(void)
288 {
289         ion_test_pdev = platform_device_register_simple("ion-test",
290                                                         -1, NULL, 0);
291         if (IS_ERR(ion_test_pdev))
292                 return PTR_ERR(ion_test_pdev);
293 
294         return platform_driver_probe(&ion_test_platform_driver, ion_test_probe);
295 }
296 
297 static void __exit ion_test_exit(void)
298 {
299         platform_driver_unregister(&ion_test_platform_driver);
300         platform_device_unregister(ion_test_pdev);
301 }
302 
303 module_init(ion_test_init);
304 module_exit(ion_test_exit);
305 MODULE_LICENSE("GPL v2");
306 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us