Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1 | /* |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 2 | * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 14 | #include "adsprpc_shared.h" |
| 15 | |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/completion.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/cdev.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/hash.h> |
| 26 | #include <linux/msm_ion.h> |
| 27 | #include <mach/msm_smd.h> |
| 28 | #include <mach/ion.h> |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 29 | #include <linux/scatterlist.h> |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 30 | #include <linux/fs.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | #include <linux/device.h> |
| 33 | |
| 34 | #ifndef ION_ADSPRPC_HEAP_ID |
| 35 | #define ION_ADSPRPC_HEAP_ID ION_AUDIO_HEAP_ID |
| 36 | #endif /*ION_ADSPRPC_HEAP_ID*/ |
| 37 | |
| 38 | #define RPC_TIMEOUT (5 * HZ) |
| 39 | #define RPC_HASH_BITS 5 |
| 40 | #define RPC_HASH_SZ (1 << RPC_HASH_BITS) |
| 41 | #define BALIGN 32 |
| 42 | |
| 43 | #define LOCK_MMAP(kernel)\ |
| 44 | do {\ |
| 45 | if (!kernel)\ |
| 46 | down_read(¤t->mm->mmap_sem);\ |
| 47 | } while (0) |
| 48 | |
| 49 | #define UNLOCK_MMAP(kernel)\ |
| 50 | do {\ |
| 51 | if (!kernel)\ |
| 52 | up_read(¤t->mm->mmap_sem);\ |
| 53 | } while (0) |
| 54 | |
| 55 | |
| 56 | static inline uint32_t buf_page_start(void *buf) |
| 57 | { |
| 58 | uint32_t start = (uint32_t) buf & PAGE_MASK; |
| 59 | return start; |
| 60 | } |
| 61 | |
| 62 | static inline uint32_t buf_page_offset(void *buf) |
| 63 | { |
| 64 | uint32_t offset = (uint32_t) buf & (PAGE_SIZE - 1); |
| 65 | return offset; |
| 66 | } |
| 67 | |
| 68 | static inline int buf_num_pages(void *buf, int len) |
| 69 | { |
| 70 | uint32_t start = buf_page_start(buf) >> PAGE_SHIFT; |
| 71 | uint32_t end = (((uint32_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT; |
| 72 | int nPages = end - start + 1; |
| 73 | return nPages; |
| 74 | } |
| 75 | |
| 76 | static inline uint32_t buf_page_size(uint32_t size) |
| 77 | { |
| 78 | uint32_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK; |
| 79 | return sz > PAGE_SIZE ? sz : PAGE_SIZE; |
| 80 | } |
| 81 | |
| 82 | static inline int buf_get_pages(void *addr, int sz, int nr_pages, int access, |
| 83 | struct smq_phy_page *pages, int nr_elems) |
| 84 | { |
| 85 | struct vm_area_struct *vma; |
| 86 | uint32_t start = buf_page_start(addr); |
| 87 | uint32_t len = nr_pages << PAGE_SHIFT; |
| 88 | unsigned long pfn; |
| 89 | int n = -1, err = 0; |
| 90 | |
| 91 | VERIFY(err, 0 != access_ok(access ? VERIFY_WRITE : VERIFY_READ, |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 92 | (void __user *)start, len)); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 93 | if (err) |
| 94 | goto bail; |
| 95 | VERIFY(err, 0 != (vma = find_vma(current->mm, start))); |
| 96 | if (err) |
| 97 | goto bail; |
| 98 | VERIFY(err, ((uint32_t)addr + sz) <= vma->vm_end); |
| 99 | if (err) |
| 100 | goto bail; |
| 101 | n = 0; |
| 102 | VERIFY(err, 0 == follow_pfn(vma, start, &pfn)); |
| 103 | if (err) |
| 104 | goto bail; |
| 105 | VERIFY(err, nr_elems > 0); |
| 106 | if (err) |
| 107 | goto bail; |
| 108 | pages->addr = __pfn_to_phys(pfn); |
| 109 | pages->size = len; |
| 110 | n++; |
| 111 | bail: |
| 112 | return n; |
| 113 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 114 | |
| 115 | struct smq_invoke_ctx { |
| 116 | struct completion work; |
| 117 | int retval; |
| 118 | atomic_t free; |
| 119 | }; |
| 120 | |
| 121 | struct smq_context_list { |
| 122 | struct smq_invoke_ctx *ls; |
| 123 | int size; |
| 124 | int last; |
| 125 | }; |
| 126 | |
| 127 | struct fastrpc_apps { |
| 128 | smd_channel_t *chan; |
| 129 | struct smq_context_list clst; |
| 130 | struct completion work; |
| 131 | struct ion_client *iclient; |
| 132 | struct cdev cdev; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 133 | struct class *class; |
| 134 | struct device *dev; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 135 | dev_t dev_no; |
| 136 | spinlock_t wrlock; |
| 137 | spinlock_t hlock; |
| 138 | struct hlist_head htbl[RPC_HASH_SZ]; |
| 139 | }; |
| 140 | |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 141 | struct fastrpc_mmap { |
| 142 | struct hlist_node hn; |
| 143 | struct ion_handle *handle; |
| 144 | void *virt; |
| 145 | uint32_t vaddrin; |
| 146 | uint32_t vaddrout; |
| 147 | int size; |
| 148 | }; |
| 149 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 150 | struct fastrpc_buf { |
| 151 | struct ion_handle *handle; |
| 152 | void *virt; |
| 153 | ion_phys_addr_t phys; |
| 154 | int size; |
| 155 | int used; |
| 156 | }; |
| 157 | |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 158 | struct file_data { |
| 159 | spinlock_t hlock; |
| 160 | struct hlist_head hlst; |
| 161 | }; |
| 162 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 163 | struct fastrpc_device { |
| 164 | uint32_t tgid; |
| 165 | struct hlist_node hn; |
| 166 | struct fastrpc_buf buf; |
| 167 | }; |
| 168 | |
| 169 | static struct fastrpc_apps gfa; |
| 170 | |
| 171 | static void free_mem(struct fastrpc_buf *buf) |
| 172 | { |
| 173 | struct fastrpc_apps *me = &gfa; |
| 174 | |
| 175 | if (buf->handle) { |
| 176 | if (buf->virt) { |
| 177 | ion_unmap_kernel(me->iclient, buf->handle); |
| 178 | buf->virt = 0; |
| 179 | } |
| 180 | ion_free(me->iclient, buf->handle); |
| 181 | buf->handle = 0; |
| 182 | } |
| 183 | } |
| 184 | |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 185 | static void free_map(struct fastrpc_mmap *map) |
| 186 | { |
| 187 | struct fastrpc_apps *me = &gfa; |
| 188 | if (map->handle) { |
| 189 | if (map->virt) { |
| 190 | ion_unmap_kernel(me->iclient, map->handle); |
| 191 | map->virt = 0; |
| 192 | } |
| 193 | ion_free(me->iclient, map->handle); |
| 194 | } |
| 195 | map->handle = 0; |
| 196 | } |
| 197 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 198 | static int alloc_mem(struct fastrpc_buf *buf) |
| 199 | { |
| 200 | struct ion_client *clnt = gfa.iclient; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 201 | struct sg_table *sg; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 202 | int err = 0; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 203 | buf->handle = 0; |
| 204 | buf->virt = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 205 | buf->handle = ion_alloc(clnt, buf->size, SZ_4K, |
Hanumant Singh | 7d72bad | 2012-08-29 18:39:44 -0700 | [diff] [blame] | 206 | ION_HEAP(ION_AUDIO_HEAP_ID), 0); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 207 | VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle)); |
| 208 | if (err) |
| 209 | goto bail; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 210 | VERIFY(err, 0 != (buf->virt = ion_map_kernel(clnt, buf->handle))); |
| 211 | if (err) |
| 212 | goto bail; |
| 213 | VERIFY(err, 0 != (sg = ion_sg_table(clnt, buf->handle))); |
| 214 | if (err) |
| 215 | goto bail; |
| 216 | VERIFY(err, 1 == sg->nents); |
| 217 | if (err) |
| 218 | goto bail; |
| 219 | buf->phys = sg_dma_address(sg->sgl); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 220 | bail: |
| 221 | if (err && !IS_ERR_OR_NULL(buf->handle)) |
| 222 | free_mem(buf); |
| 223 | return err; |
| 224 | } |
| 225 | |
| 226 | static int context_list_ctor(struct smq_context_list *me, int size) |
| 227 | { |
| 228 | int err = 0; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 229 | VERIFY(err, 0 != (me->ls = kzalloc(size, GFP_KERNEL))); |
| 230 | if (err) |
| 231 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 232 | me->size = size / sizeof(*me->ls); |
| 233 | me->last = 0; |
| 234 | bail: |
| 235 | return err; |
| 236 | } |
| 237 | |
| 238 | static void context_list_dtor(struct smq_context_list *me) |
| 239 | { |
| 240 | kfree(me->ls); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void context_list_alloc_ctx(struct smq_context_list *me, |
| 244 | struct smq_invoke_ctx **po) |
| 245 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 246 | int i = me->last; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 247 | struct smq_invoke_ctx *ctx; |
| 248 | |
| 249 | for (;;) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 250 | i = i % me->size; |
| 251 | ctx = &me->ls[i]; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 252 | if (atomic_read(&ctx->free) == 0) |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 253 | if (atomic_cmpxchg(&ctx->free, 0, 1) == 0) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 254 | break; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 255 | i++; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 256 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 257 | me->last = i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 258 | ctx->retval = -1; |
| 259 | init_completion(&ctx->work); |
| 260 | *po = ctx; |
| 261 | } |
| 262 | |
| 263 | static void context_free(struct smq_invoke_ctx *me) |
| 264 | { |
| 265 | if (me) |
| 266 | atomic_set(&me->free, 0); |
| 267 | } |
| 268 | |
| 269 | static void context_notify_user(struct smq_invoke_ctx *me, int retval) |
| 270 | { |
| 271 | me->retval = retval; |
| 272 | complete(&me->work); |
| 273 | } |
| 274 | |
| 275 | static void context_notify_all_users(struct smq_context_list *me) |
| 276 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 277 | int i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 278 | |
| 279 | if (!me->ls) |
| 280 | return; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 281 | for (i = 0; i < me->size; ++i) { |
| 282 | if (atomic_read(&me->ls[i].free) != 0) |
| 283 | complete(&me->ls[i].work); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | static int get_page_list(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 288 | struct fastrpc_buf *ibuf, struct fastrpc_buf *obuf) |
| 289 | { |
| 290 | struct smq_phy_page *pgstart, *pages; |
| 291 | struct smq_invoke_buf *list; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 292 | int i, rlen, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 293 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 294 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 295 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 296 | LOCK_MMAP(kernel); |
| 297 | *obuf = *ibuf; |
| 298 | retry: |
| 299 | list = smq_invoke_buf_start((remote_arg_t *)obuf->virt, sc); |
| 300 | pgstart = smq_phy_page_start(sc, list); |
| 301 | pages = pgstart + 1; |
| 302 | rlen = obuf->size - ((uint32_t)pages - (uint32_t)obuf->virt); |
| 303 | if (rlen < 0) { |
| 304 | rlen = ((uint32_t)pages - (uint32_t)obuf->virt) - obuf->size; |
| 305 | obuf->size += buf_page_size(rlen); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 306 | VERIFY(err, 0 == alloc_mem(obuf)); |
| 307 | if (err) |
| 308 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 309 | goto retry; |
| 310 | } |
| 311 | pgstart->addr = obuf->phys; |
| 312 | pgstart->size = obuf->size; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 313 | for (i = 0; i < inbufs + outbufs; ++i) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 314 | void *buf; |
| 315 | int len, num; |
| 316 | |
Mitchel Humpherys | f581c51 | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 317 | list[i].num = 0; |
| 318 | list[i].pgidx = 0; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 319 | len = pra[i].buf.len; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 320 | if (!len) |
| 321 | continue; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 322 | buf = pra[i].buf.pv; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 323 | num = buf_num_pages(buf, len); |
| 324 | if (!kernel) |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 325 | list[i].num = buf_get_pages(buf, len, num, |
| 326 | i >= inbufs, pages, rlen / sizeof(*pages)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 327 | else |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 328 | list[i].num = 0; |
| 329 | VERIFY(err, list[i].num >= 0); |
| 330 | if (err) |
| 331 | goto bail; |
| 332 | if (list[i].num) { |
| 333 | list[i].pgidx = pages - pgstart; |
| 334 | pages = pages + list[i].num; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 335 | } else if (rlen > sizeof(*pages)) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 336 | list[i].pgidx = pages - pgstart; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 337 | pages = pages + 1; |
| 338 | } else { |
| 339 | if (obuf->handle != ibuf->handle) |
| 340 | free_mem(obuf); |
| 341 | obuf->size += buf_page_size(sizeof(*pages)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 342 | VERIFY(err, 0 == alloc_mem(obuf)); |
| 343 | if (err) |
| 344 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 345 | goto retry; |
| 346 | } |
| 347 | rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt); |
| 348 | } |
| 349 | obuf->used = obuf->size - rlen; |
| 350 | bail: |
| 351 | if (err && (obuf->handle != ibuf->handle)) |
| 352 | free_mem(obuf); |
| 353 | UNLOCK_MMAP(kernel); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 354 | return err; |
| 355 | } |
| 356 | |
| 357 | static int get_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 358 | remote_arg_t *rpra, remote_arg_t *upra, |
| 359 | struct fastrpc_buf *ibuf, struct fastrpc_buf **abufs, |
| 360 | int *nbufs) |
| 361 | { |
| 362 | struct smq_invoke_buf *list; |
| 363 | struct fastrpc_buf *pbuf = ibuf, *obufs = 0; |
| 364 | struct smq_phy_page *pages; |
| 365 | void *args; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 366 | int i, rlen, size, used, inh, bufs = 0, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 367 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 368 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 369 | |
| 370 | list = smq_invoke_buf_start(rpra, sc); |
| 371 | pages = smq_phy_page_start(sc, list); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 372 | used = ALIGN(pbuf->used, BALIGN); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 373 | args = (void *)((char *)pbuf->virt + used); |
| 374 | rlen = pbuf->size - used; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 375 | for (i = 0; i < inbufs + outbufs; ++i) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 376 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 377 | rpra[i].buf.len = pra[i].buf.len; |
Mitchel Humpherys | f581c51 | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 378 | if (!rpra[i].buf.len) |
| 379 | continue; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 380 | if (list[i].num) { |
| 381 | rpra[i].buf.pv = pra[i].buf.pv; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 382 | continue; |
| 383 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 384 | if (rlen < pra[i].buf.len) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 385 | struct fastrpc_buf *b; |
| 386 | pbuf->used = pbuf->size - rlen; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 387 | VERIFY(err, 0 != (b = krealloc(obufs, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 388 | (bufs + 1) * sizeof(*obufs), GFP_KERNEL))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 389 | if (err) |
| 390 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 391 | obufs = b; |
| 392 | pbuf = obufs + bufs; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 393 | pbuf->size = buf_num_pages(0, pra[i].buf.len) * |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 394 | PAGE_SIZE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 395 | VERIFY(err, 0 == alloc_mem(pbuf)); |
| 396 | if (err) |
| 397 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 398 | bufs++; |
| 399 | args = pbuf->virt; |
| 400 | rlen = pbuf->size; |
| 401 | } |
Mitchel Humpherys | 0bc3aa5 | 2013-02-02 11:31:15 -0800 | [diff] [blame] | 402 | list[i].num = 1; |
| 403 | pages[list[i].pgidx].addr = |
| 404 | buf_page_start((void *)(pbuf->phys + |
| 405 | (pbuf->size - rlen))); |
| 406 | pages[list[i].pgidx].size = |
| 407 | buf_page_size(pra[i].buf.len); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 408 | if (i < inbufs) { |
| 409 | if (!kernel) { |
| 410 | VERIFY(err, 0 == copy_from_user(args, |
| 411 | pra[i].buf.pv, pra[i].buf.len)); |
| 412 | if (err) |
| 413 | goto bail; |
| 414 | } else { |
| 415 | memmove(args, pra[i].buf.pv, pra[i].buf.len); |
| 416 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 417 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 418 | rpra[i].buf.pv = args; |
| 419 | args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN)); |
| 420 | rlen -= ALIGN(pra[i].buf.len, BALIGN); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 421 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 422 | for (i = 0; i < inbufs; ++i) { |
| 423 | if (rpra[i].buf.len) |
| 424 | dmac_flush_range(rpra[i].buf.pv, |
| 425 | (char *)rpra[i].buf.pv + rpra[i].buf.len); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 426 | } |
| 427 | pbuf->used = pbuf->size - rlen; |
| 428 | size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc); |
| 429 | if (size) { |
| 430 | inh = inbufs + outbufs; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 431 | if (!kernel) { |
| 432 | VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh], |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 433 | size)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 434 | if (err) |
| 435 | goto bail; |
| 436 | } else { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 437 | memmove(&rpra[inh], &upra[inh], size); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 438 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 439 | } |
| 440 | dmac_flush_range(rpra, (char *)rpra + used); |
| 441 | bail: |
| 442 | *abufs = obufs; |
| 443 | *nbufs = bufs; |
| 444 | return err; |
| 445 | } |
| 446 | |
| 447 | static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 448 | remote_arg_t *rpra, remote_arg_t *upra) |
| 449 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 450 | int i, inbufs, outbufs, outh, size; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 451 | int err = 0; |
| 452 | |
| 453 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 454 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 455 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 456 | if (rpra[i].buf.pv != pra[i].buf.pv) { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 457 | if (!kernel) { |
| 458 | VERIFY(err, 0 == copy_to_user(pra[i].buf.pv, |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 459 | rpra[i].buf.pv, rpra[i].buf.len)); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 460 | if (err) |
| 461 | goto bail; |
| 462 | } else { |
| 463 | memmove(pra[i].buf.pv, rpra[i].buf.pv, |
| 464 | rpra[i].buf.len); |
| 465 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 466 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 467 | } |
| 468 | size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc); |
| 469 | if (size) { |
| 470 | outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 471 | if (!kernel) { |
| 472 | VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh], |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 473 | size)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 474 | if (err) |
| 475 | goto bail; |
| 476 | } else { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 477 | memmove(&upra[outh], &rpra[outh], size); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 478 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 479 | } |
| 480 | bail: |
| 481 | return err; |
| 482 | } |
| 483 | |
| 484 | static void inv_args(uint32_t sc, remote_arg_t *rpra, int used) |
| 485 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 486 | int i, inbufs, outbufs; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 487 | int inv = 0; |
| 488 | |
| 489 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 490 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 491 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 492 | if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv)) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 493 | inv = 1; |
Mitchel Humpherys | f581c51 | 2012-10-19 11:29:36 -0700 | [diff] [blame] | 494 | else if (rpra[i].buf.len) |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 495 | dmac_inv_range(rpra[i].buf.pv, |
| 496 | (char *)rpra[i].buf.pv + rpra[i].buf.len); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | if (inv || REMOTE_SCALARS_OUTHANDLES(sc)) |
| 500 | dmac_inv_range(rpra, (char *)rpra + used); |
| 501 | } |
| 502 | |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 503 | static int fastrpc_invoke_send(struct fastrpc_apps *me, |
| 504 | uint32_t kernel, uint32_t handle, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 505 | uint32_t sc, struct smq_invoke_ctx *ctx, |
| 506 | struct fastrpc_buf *buf) |
| 507 | { |
| 508 | struct smq_msg msg; |
| 509 | int err = 0, len; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 510 | msg.pid = current->tgid; |
| 511 | msg.tid = current->pid; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 512 | if (kernel) |
| 513 | msg.pid = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 514 | msg.invoke.header.ctx = ctx; |
| 515 | msg.invoke.header.handle = handle; |
| 516 | msg.invoke.header.sc = sc; |
| 517 | msg.invoke.page.addr = buf->phys; |
| 518 | msg.invoke.page.size = buf_page_size(buf->used); |
| 519 | spin_lock(&me->wrlock); |
| 520 | len = smd_write(me->chan, &msg, sizeof(msg)); |
| 521 | spin_unlock(&me->wrlock); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 522 | VERIFY(err, len == sizeof(msg)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 523 | return err; |
| 524 | } |
| 525 | |
| 526 | static void fastrpc_deinit(void) |
| 527 | { |
| 528 | struct fastrpc_apps *me = &gfa; |
| 529 | |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 530 | smd_close(me->chan); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 531 | context_list_dtor(&me->clst); |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 532 | ion_client_destroy(me->iclient); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 533 | me->iclient = 0; |
| 534 | me->chan = 0; |
| 535 | } |
| 536 | |
| 537 | static void fastrpc_read_handler(void) |
| 538 | { |
| 539 | struct fastrpc_apps *me = &gfa; |
| 540 | struct smq_invoke_rsp rsp; |
| 541 | int err = 0; |
| 542 | |
| 543 | do { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 544 | VERIFY(err, sizeof(rsp) == |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 545 | smd_read_from_cb(me->chan, &rsp, sizeof(rsp))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 546 | if (err) |
| 547 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 548 | context_notify_user(rsp.ctx, rsp.retval); |
| 549 | } while (!err); |
| 550 | bail: |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | static void smd_event_handler(void *priv, unsigned event) |
| 555 | { |
| 556 | struct fastrpc_apps *me = (struct fastrpc_apps *)priv; |
| 557 | |
| 558 | switch (event) { |
| 559 | case SMD_EVENT_OPEN: |
| 560 | complete(&(me->work)); |
| 561 | break; |
| 562 | case SMD_EVENT_CLOSE: |
| 563 | context_notify_all_users(&me->clst); |
| 564 | break; |
| 565 | case SMD_EVENT_DATA: |
| 566 | fastrpc_read_handler(); |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | static int fastrpc_init(void) |
| 572 | { |
| 573 | int err = 0; |
| 574 | struct fastrpc_apps *me = &gfa; |
| 575 | |
| 576 | if (me->chan == 0) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 577 | int i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 578 | spin_lock_init(&me->hlock); |
| 579 | spin_lock_init(&me->wrlock); |
| 580 | init_completion(&me->work); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 581 | for (i = 0; i < RPC_HASH_SZ; ++i) |
| 582 | INIT_HLIST_HEAD(&me->htbl[i]); |
| 583 | VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K)); |
| 584 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 585 | goto context_list_bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 586 | me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK, |
| 587 | DEVICE_NAME); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 588 | VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient)); |
| 589 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 590 | goto ion_bail; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 591 | VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 592 | SMD_APPS_QDSP, &me->chan, |
| 593 | me, smd_event_handler)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 594 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 595 | goto smd_bail; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 596 | VERIFY(err, 0 != wait_for_completion_timeout(&me->work, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 597 | RPC_TIMEOUT)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 598 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 599 | goto completion_bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 600 | } |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 601 | |
| 602 | return 0; |
| 603 | |
| 604 | completion_bail: |
| 605 | smd_close(me->chan); |
| 606 | smd_bail: |
| 607 | ion_client_destroy(me->iclient); |
| 608 | ion_bail: |
| 609 | context_list_dtor(&me->clst); |
| 610 | context_list_bail: |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 611 | return err; |
| 612 | } |
| 613 | |
| 614 | static void free_dev(struct fastrpc_device *dev) |
| 615 | { |
| 616 | if (dev) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 617 | free_mem(&dev->buf); |
| 618 | kfree(dev); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 619 | module_put(THIS_MODULE); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | static int alloc_dev(struct fastrpc_device **dev) |
| 624 | { |
| 625 | int err = 0; |
| 626 | struct fastrpc_device *fd = 0; |
| 627 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 628 | VERIFY(err, 0 != try_module_get(THIS_MODULE)); |
| 629 | if (err) |
| 630 | goto bail; |
| 631 | VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL))); |
| 632 | if (err) |
| 633 | goto bail; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 634 | |
| 635 | INIT_HLIST_NODE(&fd->hn); |
| 636 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 637 | fd->buf.size = PAGE_SIZE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 638 | VERIFY(err, 0 == alloc_mem(&fd->buf)); |
| 639 | if (err) |
| 640 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 641 | fd->tgid = current->tgid; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 642 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 643 | *dev = fd; |
| 644 | bail: |
| 645 | if (err) |
| 646 | free_dev(fd); |
| 647 | return err; |
| 648 | } |
| 649 | |
| 650 | static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev) |
| 651 | { |
| 652 | struct hlist_head *head; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 653 | struct fastrpc_device *dev = 0, *devfree = 0; |
| 654 | struct hlist_node *pos, *n; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 655 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 656 | int err = 0; |
| 657 | |
| 658 | spin_lock(&me->hlock); |
| 659 | head = &me->htbl[h]; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 660 | hlist_for_each_entry_safe(dev, pos, n, head, hn) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 661 | if (dev->tgid == current->tgid) { |
| 662 | hlist_del(&dev->hn); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 663 | devfree = dev; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 664 | break; |
| 665 | } |
| 666 | } |
| 667 | spin_unlock(&me->hlock); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 668 | VERIFY(err, devfree != 0); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 669 | if (err) |
| 670 | goto bail; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 671 | *rdev = devfree; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 672 | bail: |
| 673 | if (err) { |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 674 | free_dev(devfree); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 675 | err = alloc_dev(rdev); |
| 676 | } |
| 677 | return err; |
| 678 | } |
| 679 | |
| 680 | static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev) |
| 681 | { |
| 682 | struct hlist_head *head; |
| 683 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 684 | |
| 685 | spin_lock(&me->hlock); |
| 686 | head = &me->htbl[h]; |
| 687 | hlist_add_head(&dev->hn, head); |
| 688 | spin_unlock(&me->hlock); |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | static int fastrpc_release_current_dsp_process(void); |
| 693 | |
| 694 | static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel, |
| 695 | struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra) |
| 696 | { |
| 697 | remote_arg_t *rpra = 0; |
| 698 | struct fastrpc_device *dev = 0; |
| 699 | struct smq_invoke_ctx *ctx = 0; |
| 700 | struct fastrpc_buf obuf, *abufs = 0, *b; |
| 701 | int interrupted = 0; |
| 702 | uint32_t sc; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 703 | int i, nbufs = 0, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 704 | |
| 705 | sc = invoke->sc; |
| 706 | obuf.handle = 0; |
| 707 | if (REMOTE_SCALARS_LENGTH(sc)) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 708 | VERIFY(err, 0 == get_dev(me, &dev)); |
| 709 | if (err) |
| 710 | goto bail; |
| 711 | VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf, |
| 712 | &obuf)); |
| 713 | if (err) |
| 714 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 715 | rpra = (remote_arg_t *)obuf.virt; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 716 | VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra, |
| 717 | &obuf, &abufs, &nbufs)); |
| 718 | if (err) |
| 719 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | context_list_alloc_ctx(&me->clst, &ctx); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 723 | VERIFY(err, 0 == fastrpc_invoke_send(me, kernel, invoke->handle, sc, |
| 724 | ctx, &obuf)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 725 | if (err) |
| 726 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 727 | inv_args(sc, rpra, obuf.used); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 728 | VERIFY(err, 0 == (interrupted = |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 729 | wait_for_completion_interruptible(&ctx->work))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 730 | if (err) |
| 731 | goto bail; |
| 732 | VERIFY(err, 0 == (err = ctx->retval)); |
| 733 | if (err) |
| 734 | goto bail; |
| 735 | VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra)); |
| 736 | if (err) |
| 737 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 738 | bail: |
| 739 | if (interrupted) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 740 | if (!kernel) |
| 741 | (void)fastrpc_release_current_dsp_process(); |
| 742 | wait_for_completion(&ctx->work); |
| 743 | } |
| 744 | context_free(ctx); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 745 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 746 | for (i = 0, b = abufs; i < nbufs; ++i, ++b) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 747 | free_mem(b); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 748 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 749 | kfree(abufs); |
| 750 | if (dev) { |
| 751 | add_dev(me, dev); |
| 752 | if (obuf.handle != dev->buf.handle) |
| 753 | free_mem(&obuf); |
| 754 | } |
| 755 | return err; |
| 756 | } |
| 757 | |
| 758 | static int fastrpc_create_current_dsp_process(void) |
| 759 | { |
| 760 | int err = 0; |
| 761 | struct fastrpc_ioctl_invoke ioctl; |
| 762 | struct fastrpc_apps *me = &gfa; |
| 763 | remote_arg_t ra[1]; |
| 764 | int tgid = 0; |
| 765 | |
| 766 | tgid = current->tgid; |
| 767 | ra[0].buf.pv = &tgid; |
| 768 | ra[0].buf.len = sizeof(tgid); |
| 769 | ioctl.handle = 1; |
| 770 | ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0); |
| 771 | ioctl.pra = ra; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 772 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 773 | return err; |
| 774 | } |
| 775 | |
| 776 | static int fastrpc_release_current_dsp_process(void) |
| 777 | { |
| 778 | int err = 0; |
| 779 | struct fastrpc_apps *me = &gfa; |
| 780 | struct fastrpc_ioctl_invoke ioctl; |
| 781 | remote_arg_t ra[1]; |
| 782 | int tgid = 0; |
| 783 | |
| 784 | tgid = current->tgid; |
| 785 | ra[0].buf.pv = &tgid; |
| 786 | ra[0].buf.len = sizeof(tgid); |
| 787 | ioctl.handle = 1; |
| 788 | ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0); |
| 789 | ioctl.pra = ra; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 790 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 791 | return err; |
| 792 | } |
| 793 | |
| 794 | static int fastrpc_mmap_on_dsp(struct fastrpc_apps *me, |
| 795 | struct fastrpc_ioctl_mmap *mmap, |
| 796 | struct smq_phy_page *pages, |
| 797 | int num) |
| 798 | { |
| 799 | struct fastrpc_ioctl_invoke ioctl; |
| 800 | remote_arg_t ra[3]; |
| 801 | int err = 0; |
| 802 | struct { |
| 803 | int pid; |
| 804 | uint32_t flags; |
| 805 | uint32_t vaddrin; |
| 806 | int num; |
| 807 | } inargs; |
| 808 | |
| 809 | struct { |
| 810 | uint32_t vaddrout; |
| 811 | } routargs; |
| 812 | inargs.pid = current->tgid; |
| 813 | inargs.vaddrin = mmap->vaddrin; |
| 814 | inargs.flags = mmap->flags; |
| 815 | inargs.num = num; |
| 816 | ra[0].buf.pv = &inargs; |
| 817 | ra[0].buf.len = sizeof(inargs); |
| 818 | |
| 819 | ra[1].buf.pv = pages; |
| 820 | ra[1].buf.len = num * sizeof(*pages); |
| 821 | |
| 822 | ra[2].buf.pv = &routargs; |
| 823 | ra[2].buf.len = sizeof(routargs); |
| 824 | |
| 825 | ioctl.handle = 1; |
| 826 | ioctl.sc = REMOTE_SCALARS_MAKE(2, 2, 1); |
| 827 | ioctl.pra = ra; |
| 828 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 829 | mmap->vaddrout = routargs.vaddrout; |
| 830 | if (err) |
| 831 | goto bail; |
| 832 | bail: |
| 833 | return err; |
| 834 | } |
| 835 | |
| 836 | static int fastrpc_munmap_on_dsp(struct fastrpc_apps *me, |
| 837 | struct fastrpc_ioctl_munmap *munmap) |
| 838 | { |
| 839 | struct fastrpc_ioctl_invoke ioctl; |
| 840 | remote_arg_t ra[1]; |
| 841 | int err = 0; |
| 842 | struct { |
| 843 | int pid; |
| 844 | uint32_t vaddrout; |
| 845 | int size; |
| 846 | } inargs; |
| 847 | |
| 848 | inargs.pid = current->tgid; |
| 849 | inargs.size = munmap->size; |
| 850 | inargs.vaddrout = munmap->vaddrout; |
| 851 | ra[0].buf.pv = &inargs; |
| 852 | ra[0].buf.len = sizeof(inargs); |
| 853 | |
| 854 | ioctl.handle = 1; |
| 855 | ioctl.sc = REMOTE_SCALARS_MAKE(3, 1, 0); |
| 856 | ioctl.pra = ra; |
| 857 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 858 | return err; |
| 859 | } |
| 860 | |
| 861 | static int fastrpc_internal_munmap(struct fastrpc_apps *me, |
| 862 | struct file_data *fdata, |
| 863 | struct fastrpc_ioctl_munmap *munmap) |
| 864 | { |
| 865 | int err = 0; |
| 866 | struct fastrpc_mmap *map = 0, *mapfree = 0; |
| 867 | struct hlist_node *pos, *n; |
| 868 | VERIFY(err, 0 == (err = fastrpc_munmap_on_dsp(me, munmap))); |
| 869 | if (err) |
| 870 | goto bail; |
| 871 | spin_lock(&fdata->hlock); |
| 872 | hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) { |
| 873 | if (map->vaddrout == munmap->vaddrout && |
| 874 | map->size == munmap->size) { |
| 875 | hlist_del(&map->hn); |
| 876 | mapfree = map; |
| 877 | map = 0; |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | spin_unlock(&fdata->hlock); |
| 882 | bail: |
| 883 | if (mapfree) { |
| 884 | free_map(mapfree); |
| 885 | kfree(mapfree); |
| 886 | } |
| 887 | return err; |
| 888 | } |
| 889 | |
| 890 | |
| 891 | static int fastrpc_internal_mmap(struct fastrpc_apps *me, |
| 892 | struct file_data *fdata, |
| 893 | struct fastrpc_ioctl_mmap *mmap) |
| 894 | { |
| 895 | struct ion_client *clnt = gfa.iclient; |
| 896 | struct fastrpc_mmap *map = 0; |
| 897 | struct smq_phy_page *pages = 0; |
| 898 | void *buf; |
| 899 | int len; |
| 900 | int num; |
| 901 | int err = 0; |
| 902 | |
| 903 | VERIFY(err, 0 != (map = kzalloc(sizeof(*map), GFP_KERNEL))); |
| 904 | if (err) |
| 905 | goto bail; |
| 906 | map->handle = ion_import_dma_buf(clnt, mmap->fd); |
| 907 | VERIFY(err, 0 == IS_ERR_OR_NULL(map->handle)); |
| 908 | if (err) |
| 909 | goto bail; |
| 910 | VERIFY(err, 0 != (map->virt = ion_map_kernel(clnt, map->handle))); |
| 911 | if (err) |
| 912 | goto bail; |
| 913 | buf = (void *)mmap->vaddrin; |
| 914 | len = mmap->size; |
| 915 | num = buf_num_pages(buf, len); |
| 916 | VERIFY(err, 0 != (pages = kzalloc(num * sizeof(*pages), GFP_KERNEL))); |
| 917 | if (err) |
| 918 | goto bail; |
| 919 | VERIFY(err, 0 < (num = buf_get_pages(buf, len, num, 1, pages, num))); |
| 920 | if (err) |
| 921 | goto bail; |
| 922 | |
| 923 | VERIFY(err, 0 == fastrpc_mmap_on_dsp(me, mmap, pages, num)); |
| 924 | if (err) |
| 925 | goto bail; |
| 926 | map->vaddrin = mmap->vaddrin; |
| 927 | map->vaddrout = mmap->vaddrout; |
| 928 | map->size = mmap->size; |
| 929 | INIT_HLIST_NODE(&map->hn); |
| 930 | spin_lock(&fdata->hlock); |
| 931 | hlist_add_head(&map->hn, &fdata->hlst); |
| 932 | spin_unlock(&fdata->hlock); |
| 933 | bail: |
| 934 | if (err && map) { |
| 935 | free_map(map); |
| 936 | kfree(map); |
| 937 | } |
| 938 | kfree(pages); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 939 | return err; |
| 940 | } |
| 941 | |
| 942 | static void cleanup_current_dev(void) |
| 943 | { |
| 944 | struct fastrpc_apps *me = &gfa; |
| 945 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 946 | struct hlist_head *head; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 947 | struct hlist_node *pos, *n; |
| 948 | struct fastrpc_device *dev, *devfree; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 949 | |
| 950 | rnext: |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 951 | devfree = dev = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 952 | spin_lock(&me->hlock); |
| 953 | head = &me->htbl[h]; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 954 | hlist_for_each_entry_safe(dev, pos, n, head, hn) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 955 | if (dev->tgid == current->tgid) { |
| 956 | hlist_del(&dev->hn); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 957 | devfree = dev; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 958 | break; |
| 959 | } |
| 960 | } |
| 961 | spin_unlock(&me->hlock); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 962 | if (devfree) { |
| 963 | free_dev(devfree); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 964 | goto rnext; |
| 965 | } |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
| 970 | { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 971 | struct file_data *fdata = (struct file_data *)file->private_data; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 972 | (void)fastrpc_release_current_dsp_process(); |
| 973 | cleanup_current_dev(); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 974 | if (fdata) { |
| 975 | struct fastrpc_mmap *map; |
| 976 | struct hlist_node *n, *pos; |
| 977 | file->private_data = 0; |
| 978 | hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) { |
| 979 | hlist_del(&map->hn); |
| 980 | free_map(map); |
| 981 | kfree(map); |
| 982 | } |
| 983 | kfree(fdata); |
| 984 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static int fastrpc_device_open(struct inode *inode, struct file *filp) |
| 989 | { |
| 990 | int err = 0; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 991 | filp->private_data = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 992 | if (0 != try_module_get(THIS_MODULE)) { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 993 | struct file_data *fdata = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 994 | /* This call will cause a dev to be created |
| 995 | * which will addref this module |
| 996 | */ |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 997 | VERIFY(err, 0 != (fdata = kzalloc(sizeof(*fdata), GFP_KERNEL))); |
| 998 | if (err) |
| 999 | goto bail; |
| 1000 | |
| 1001 | spin_lock_init(&fdata->hlock); |
| 1002 | INIT_HLIST_HEAD(&fdata->hlst); |
| 1003 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1004 | VERIFY(err, 0 == fastrpc_create_current_dsp_process()); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1005 | if (err) |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1006 | goto bail; |
| 1007 | filp->private_data = fdata; |
| 1008 | bail: |
| 1009 | if (err) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1010 | cleanup_current_dev(); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1011 | kfree(fdata); |
| 1012 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1013 | module_put(THIS_MODULE); |
| 1014 | } |
| 1015 | return err; |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 1020 | unsigned long ioctl_param) |
| 1021 | { |
| 1022 | struct fastrpc_apps *me = &gfa; |
| 1023 | struct fastrpc_ioctl_invoke invoke; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1024 | struct fastrpc_ioctl_mmap mmap; |
| 1025 | struct fastrpc_ioctl_munmap munmap; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1026 | remote_arg_t *pra = 0; |
| 1027 | void *param = (char *)ioctl_param; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1028 | struct file_data *fdata = (struct file_data *)file->private_data; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1029 | int bufs, err = 0; |
| 1030 | |
| 1031 | switch (ioctl_num) { |
| 1032 | case FASTRPC_IOCTL_INVOKE: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1033 | VERIFY(err, 0 == copy_from_user(&invoke, param, |
| 1034 | sizeof(invoke))); |
| 1035 | if (err) |
| 1036 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1037 | bufs = REMOTE_SCALARS_INBUFS(invoke.sc) + |
| 1038 | REMOTE_SCALARS_OUTBUFS(invoke.sc); |
| 1039 | if (bufs) { |
| 1040 | bufs = bufs * sizeof(*pra); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1041 | VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL))); |
| 1042 | if (err) |
| 1043 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1044 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1045 | VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs)); |
| 1046 | if (err) |
| 1047 | goto bail; |
| 1048 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1049 | pra))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1050 | if (err) |
| 1051 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1052 | break; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame] | 1053 | case FASTRPC_IOCTL_MMAP: |
| 1054 | VERIFY(err, 0 == copy_from_user(&mmap, param, |
| 1055 | sizeof(mmap))); |
| 1056 | if (err) |
| 1057 | goto bail; |
| 1058 | VERIFY(err, 0 == (err = fastrpc_internal_mmap(me, fdata, |
| 1059 | &mmap))); |
| 1060 | if (err) |
| 1061 | goto bail; |
| 1062 | VERIFY(err, 0 == copy_to_user(param, &mmap, sizeof(mmap))); |
| 1063 | if (err) |
| 1064 | goto bail; |
| 1065 | break; |
| 1066 | case FASTRPC_IOCTL_MUNMAP: |
| 1067 | VERIFY(err, 0 == copy_from_user(&munmap, param, |
| 1068 | sizeof(munmap))); |
| 1069 | if (err) |
| 1070 | goto bail; |
| 1071 | VERIFY(err, 0 == (err = fastrpc_internal_munmap(me, fdata, |
| 1072 | &munmap))); |
| 1073 | if (err) |
| 1074 | goto bail; |
| 1075 | break; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1076 | default: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1077 | err = -ENOTTY; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1078 | break; |
| 1079 | } |
| 1080 | bail: |
| 1081 | kfree(pra); |
| 1082 | return err; |
| 1083 | } |
| 1084 | |
| 1085 | static const struct file_operations fops = { |
| 1086 | .open = fastrpc_device_open, |
| 1087 | .release = fastrpc_device_release, |
| 1088 | .unlocked_ioctl = fastrpc_device_ioctl, |
| 1089 | }; |
| 1090 | |
| 1091 | static int __init fastrpc_device_init(void) |
| 1092 | { |
| 1093 | struct fastrpc_apps *me = &gfa; |
| 1094 | int err = 0; |
| 1095 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1096 | VERIFY(err, 0 == fastrpc_init()); |
| 1097 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 1098 | goto fastrpc_bail; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1099 | VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME)); |
| 1100 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 1101 | goto alloc_chrdev_bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1102 | cdev_init(&me->cdev, &fops); |
| 1103 | me->cdev.owner = THIS_MODULE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1104 | VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1)); |
| 1105 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 1106 | goto cdev_init_bail; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1107 | me->class = class_create(THIS_MODULE, "chardrv"); |
| 1108 | VERIFY(err, !IS_ERR(me->class)); |
| 1109 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 1110 | goto class_create_bail; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1111 | me->dev = device_create(me->class, NULL, MKDEV(MAJOR(me->dev_no), 0), |
| 1112 | NULL, DEVICE_NAME); |
| 1113 | VERIFY(err, !IS_ERR(me->dev)); |
| 1114 | if (err) |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 1115 | goto device_create_bail; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1116 | pr_info("'created /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no)); |
Matt Wagantall | 818e23f | 2013-04-22 20:36:52 -0700 | [diff] [blame^] | 1117 | |
| 1118 | return 0; |
| 1119 | |
| 1120 | device_create_bail: |
| 1121 | class_destroy(me->class); |
| 1122 | class_create_bail: |
| 1123 | cdev_del(&me->cdev); |
| 1124 | cdev_init_bail: |
| 1125 | unregister_chrdev_region(me->dev_no, 1); |
| 1126 | alloc_chrdev_bail: |
| 1127 | fastrpc_deinit(); |
| 1128 | fastrpc_bail: |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1129 | return err; |
| 1130 | } |
| 1131 | |
| 1132 | static void __exit fastrpc_device_exit(void) |
| 1133 | { |
| 1134 | struct fastrpc_apps *me = &gfa; |
| 1135 | |
| 1136 | fastrpc_deinit(); |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1137 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0)); |
| 1138 | class_destroy(me->class); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1139 | cdev_del(&me->cdev); |
| 1140 | unregister_chrdev_region(me->dev_no, 1); |
| 1141 | } |
| 1142 | |
| 1143 | module_init(fastrpc_device_init); |
| 1144 | module_exit(fastrpc_device_exit); |
| 1145 | |
| 1146 | MODULE_LICENSE("GPL v2"); |