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 | |
| 530 | if (me->chan) |
| 531 | (void)smd_close(me->chan); |
| 532 | context_list_dtor(&me->clst); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 533 | if (me->iclient) |
| 534 | ion_client_destroy(me->iclient); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 535 | me->iclient = 0; |
| 536 | me->chan = 0; |
| 537 | } |
| 538 | |
| 539 | static void fastrpc_read_handler(void) |
| 540 | { |
| 541 | struct fastrpc_apps *me = &gfa; |
| 542 | struct smq_invoke_rsp rsp; |
| 543 | int err = 0; |
| 544 | |
| 545 | do { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 546 | VERIFY(err, sizeof(rsp) == |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 547 | smd_read_from_cb(me->chan, &rsp, sizeof(rsp))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 548 | if (err) |
| 549 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 550 | context_notify_user(rsp.ctx, rsp.retval); |
| 551 | } while (!err); |
| 552 | bail: |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | static void smd_event_handler(void *priv, unsigned event) |
| 557 | { |
| 558 | struct fastrpc_apps *me = (struct fastrpc_apps *)priv; |
| 559 | |
| 560 | switch (event) { |
| 561 | case SMD_EVENT_OPEN: |
| 562 | complete(&(me->work)); |
| 563 | break; |
| 564 | case SMD_EVENT_CLOSE: |
| 565 | context_notify_all_users(&me->clst); |
| 566 | break; |
| 567 | case SMD_EVENT_DATA: |
| 568 | fastrpc_read_handler(); |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | static int fastrpc_init(void) |
| 574 | { |
| 575 | int err = 0; |
| 576 | struct fastrpc_apps *me = &gfa; |
| 577 | |
| 578 | if (me->chan == 0) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 579 | int i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 580 | spin_lock_init(&me->hlock); |
| 581 | spin_lock_init(&me->wrlock); |
| 582 | init_completion(&me->work); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 583 | for (i = 0; i < RPC_HASH_SZ; ++i) |
| 584 | INIT_HLIST_HEAD(&me->htbl[i]); |
| 585 | VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K)); |
| 586 | if (err) |
| 587 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 588 | me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK, |
| 589 | DEVICE_NAME); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 590 | VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient)); |
| 591 | if (err) |
| 592 | goto bail; |
| 593 | VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 594 | SMD_APPS_QDSP, &me->chan, |
| 595 | me, smd_event_handler)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 596 | if (err) |
| 597 | goto bail; |
| 598 | VERIFY(err, 0 != wait_for_completion_timeout(&me->work, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 599 | RPC_TIMEOUT)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 600 | if (err) |
| 601 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 602 | } |
| 603 | bail: |
| 604 | if (err) |
| 605 | fastrpc_deinit(); |
| 606 | return err; |
| 607 | } |
| 608 | |
| 609 | static void free_dev(struct fastrpc_device *dev) |
| 610 | { |
| 611 | if (dev) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 612 | free_mem(&dev->buf); |
| 613 | kfree(dev); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 614 | module_put(THIS_MODULE); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
| 618 | static int alloc_dev(struct fastrpc_device **dev) |
| 619 | { |
| 620 | int err = 0; |
| 621 | struct fastrpc_device *fd = 0; |
| 622 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 623 | VERIFY(err, 0 != try_module_get(THIS_MODULE)); |
| 624 | if (err) |
| 625 | goto bail; |
| 626 | VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL))); |
| 627 | if (err) |
| 628 | goto bail; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 629 | |
| 630 | INIT_HLIST_NODE(&fd->hn); |
| 631 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 632 | fd->buf.size = PAGE_SIZE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 633 | VERIFY(err, 0 == alloc_mem(&fd->buf)); |
| 634 | if (err) |
| 635 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 636 | fd->tgid = current->tgid; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 637 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 638 | *dev = fd; |
| 639 | bail: |
| 640 | if (err) |
| 641 | free_dev(fd); |
| 642 | return err; |
| 643 | } |
| 644 | |
| 645 | static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev) |
| 646 | { |
| 647 | struct hlist_head *head; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 648 | struct fastrpc_device *dev = 0, *devfree = 0; |
| 649 | struct hlist_node *pos, *n; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 650 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 651 | int err = 0; |
| 652 | |
| 653 | spin_lock(&me->hlock); |
| 654 | head = &me->htbl[h]; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 655 | hlist_for_each_entry_safe(dev, pos, n, head, hn) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 656 | if (dev->tgid == current->tgid) { |
| 657 | hlist_del(&dev->hn); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 658 | devfree = dev; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | } |
| 662 | spin_unlock(&me->hlock); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 663 | VERIFY(err, devfree != 0); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 664 | if (err) |
| 665 | goto bail; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 666 | *rdev = devfree; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 667 | bail: |
| 668 | if (err) { |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 669 | free_dev(devfree); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 670 | err = alloc_dev(rdev); |
| 671 | } |
| 672 | return err; |
| 673 | } |
| 674 | |
| 675 | static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev) |
| 676 | { |
| 677 | struct hlist_head *head; |
| 678 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 679 | |
| 680 | spin_lock(&me->hlock); |
| 681 | head = &me->htbl[h]; |
| 682 | hlist_add_head(&dev->hn, head); |
| 683 | spin_unlock(&me->hlock); |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | static int fastrpc_release_current_dsp_process(void); |
| 688 | |
| 689 | static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel, |
| 690 | struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra) |
| 691 | { |
| 692 | remote_arg_t *rpra = 0; |
| 693 | struct fastrpc_device *dev = 0; |
| 694 | struct smq_invoke_ctx *ctx = 0; |
| 695 | struct fastrpc_buf obuf, *abufs = 0, *b; |
| 696 | int interrupted = 0; |
| 697 | uint32_t sc; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 698 | int i, nbufs = 0, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 699 | |
| 700 | sc = invoke->sc; |
| 701 | obuf.handle = 0; |
| 702 | if (REMOTE_SCALARS_LENGTH(sc)) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 703 | VERIFY(err, 0 == get_dev(me, &dev)); |
| 704 | if (err) |
| 705 | goto bail; |
| 706 | VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf, |
| 707 | &obuf)); |
| 708 | if (err) |
| 709 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 710 | rpra = (remote_arg_t *)obuf.virt; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 711 | VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra, |
| 712 | &obuf, &abufs, &nbufs)); |
| 713 | if (err) |
| 714 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | context_list_alloc_ctx(&me->clst, &ctx); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 718 | VERIFY(err, 0 == fastrpc_invoke_send(me, kernel, invoke->handle, sc, |
| 719 | ctx, &obuf)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 720 | if (err) |
| 721 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 722 | inv_args(sc, rpra, obuf.used); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 723 | VERIFY(err, 0 == (interrupted = |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 724 | wait_for_completion_interruptible(&ctx->work))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 725 | if (err) |
| 726 | goto bail; |
| 727 | VERIFY(err, 0 == (err = ctx->retval)); |
| 728 | if (err) |
| 729 | goto bail; |
| 730 | VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra)); |
| 731 | if (err) |
| 732 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 733 | bail: |
| 734 | if (interrupted) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 735 | if (!kernel) |
| 736 | (void)fastrpc_release_current_dsp_process(); |
| 737 | wait_for_completion(&ctx->work); |
| 738 | } |
| 739 | context_free(ctx); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 740 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 741 | for (i = 0, b = abufs; i < nbufs; ++i, ++b) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 742 | free_mem(b); |
Mitchel Humpherys | 6b4c68c | 2013-02-06 12:03:20 -0800 | [diff] [blame] | 743 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 744 | kfree(abufs); |
| 745 | if (dev) { |
| 746 | add_dev(me, dev); |
| 747 | if (obuf.handle != dev->buf.handle) |
| 748 | free_mem(&obuf); |
| 749 | } |
| 750 | return err; |
| 751 | } |
| 752 | |
| 753 | static int fastrpc_create_current_dsp_process(void) |
| 754 | { |
| 755 | int err = 0; |
| 756 | struct fastrpc_ioctl_invoke ioctl; |
| 757 | struct fastrpc_apps *me = &gfa; |
| 758 | remote_arg_t ra[1]; |
| 759 | int tgid = 0; |
| 760 | |
| 761 | tgid = current->tgid; |
| 762 | ra[0].buf.pv = &tgid; |
| 763 | ra[0].buf.len = sizeof(tgid); |
| 764 | ioctl.handle = 1; |
| 765 | ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0); |
| 766 | ioctl.pra = ra; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 767 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 768 | return err; |
| 769 | } |
| 770 | |
| 771 | static int fastrpc_release_current_dsp_process(void) |
| 772 | { |
| 773 | int err = 0; |
| 774 | struct fastrpc_apps *me = &gfa; |
| 775 | struct fastrpc_ioctl_invoke ioctl; |
| 776 | remote_arg_t ra[1]; |
| 777 | int tgid = 0; |
| 778 | |
| 779 | tgid = current->tgid; |
| 780 | ra[0].buf.pv = &tgid; |
| 781 | ra[0].buf.len = sizeof(tgid); |
| 782 | ioctl.handle = 1; |
| 783 | ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0); |
| 784 | ioctl.pra = ra; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 785 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 786 | return err; |
| 787 | } |
| 788 | |
| 789 | static int fastrpc_mmap_on_dsp(struct fastrpc_apps *me, |
| 790 | struct fastrpc_ioctl_mmap *mmap, |
| 791 | struct smq_phy_page *pages, |
| 792 | int num) |
| 793 | { |
| 794 | struct fastrpc_ioctl_invoke ioctl; |
| 795 | remote_arg_t ra[3]; |
| 796 | int err = 0; |
| 797 | struct { |
| 798 | int pid; |
| 799 | uint32_t flags; |
| 800 | uint32_t vaddrin; |
| 801 | int num; |
| 802 | } inargs; |
| 803 | |
| 804 | struct { |
| 805 | uint32_t vaddrout; |
| 806 | } routargs; |
| 807 | inargs.pid = current->tgid; |
| 808 | inargs.vaddrin = mmap->vaddrin; |
| 809 | inargs.flags = mmap->flags; |
| 810 | inargs.num = num; |
| 811 | ra[0].buf.pv = &inargs; |
| 812 | ra[0].buf.len = sizeof(inargs); |
| 813 | |
| 814 | ra[1].buf.pv = pages; |
| 815 | ra[1].buf.len = num * sizeof(*pages); |
| 816 | |
| 817 | ra[2].buf.pv = &routargs; |
| 818 | ra[2].buf.len = sizeof(routargs); |
| 819 | |
| 820 | ioctl.handle = 1; |
| 821 | ioctl.sc = REMOTE_SCALARS_MAKE(2, 2, 1); |
| 822 | ioctl.pra = ra; |
| 823 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 824 | mmap->vaddrout = routargs.vaddrout; |
| 825 | if (err) |
| 826 | goto bail; |
| 827 | bail: |
| 828 | return err; |
| 829 | } |
| 830 | |
| 831 | static int fastrpc_munmap_on_dsp(struct fastrpc_apps *me, |
| 832 | struct fastrpc_ioctl_munmap *munmap) |
| 833 | { |
| 834 | struct fastrpc_ioctl_invoke ioctl; |
| 835 | remote_arg_t ra[1]; |
| 836 | int err = 0; |
| 837 | struct { |
| 838 | int pid; |
| 839 | uint32_t vaddrout; |
| 840 | int size; |
| 841 | } inargs; |
| 842 | |
| 843 | inargs.pid = current->tgid; |
| 844 | inargs.size = munmap->size; |
| 845 | inargs.vaddrout = munmap->vaddrout; |
| 846 | ra[0].buf.pv = &inargs; |
| 847 | ra[0].buf.len = sizeof(inargs); |
| 848 | |
| 849 | ioctl.handle = 1; |
| 850 | ioctl.sc = REMOTE_SCALARS_MAKE(3, 1, 0); |
| 851 | ioctl.pra = ra; |
| 852 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 1, &ioctl, ra))); |
| 853 | return err; |
| 854 | } |
| 855 | |
| 856 | static int fastrpc_internal_munmap(struct fastrpc_apps *me, |
| 857 | struct file_data *fdata, |
| 858 | struct fastrpc_ioctl_munmap *munmap) |
| 859 | { |
| 860 | int err = 0; |
| 861 | struct fastrpc_mmap *map = 0, *mapfree = 0; |
| 862 | struct hlist_node *pos, *n; |
| 863 | VERIFY(err, 0 == (err = fastrpc_munmap_on_dsp(me, munmap))); |
| 864 | if (err) |
| 865 | goto bail; |
| 866 | spin_lock(&fdata->hlock); |
| 867 | hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) { |
| 868 | if (map->vaddrout == munmap->vaddrout && |
| 869 | map->size == munmap->size) { |
| 870 | hlist_del(&map->hn); |
| 871 | mapfree = map; |
| 872 | map = 0; |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | spin_unlock(&fdata->hlock); |
| 877 | bail: |
| 878 | if (mapfree) { |
| 879 | free_map(mapfree); |
| 880 | kfree(mapfree); |
| 881 | } |
| 882 | return err; |
| 883 | } |
| 884 | |
| 885 | |
| 886 | static int fastrpc_internal_mmap(struct fastrpc_apps *me, |
| 887 | struct file_data *fdata, |
| 888 | struct fastrpc_ioctl_mmap *mmap) |
| 889 | { |
| 890 | struct ion_client *clnt = gfa.iclient; |
| 891 | struct fastrpc_mmap *map = 0; |
| 892 | struct smq_phy_page *pages = 0; |
| 893 | void *buf; |
| 894 | int len; |
| 895 | int num; |
| 896 | int err = 0; |
| 897 | |
| 898 | VERIFY(err, 0 != (map = kzalloc(sizeof(*map), GFP_KERNEL))); |
| 899 | if (err) |
| 900 | goto bail; |
| 901 | map->handle = ion_import_dma_buf(clnt, mmap->fd); |
| 902 | VERIFY(err, 0 == IS_ERR_OR_NULL(map->handle)); |
| 903 | if (err) |
| 904 | goto bail; |
| 905 | VERIFY(err, 0 != (map->virt = ion_map_kernel(clnt, map->handle))); |
| 906 | if (err) |
| 907 | goto bail; |
| 908 | buf = (void *)mmap->vaddrin; |
| 909 | len = mmap->size; |
| 910 | num = buf_num_pages(buf, len); |
| 911 | VERIFY(err, 0 != (pages = kzalloc(num * sizeof(*pages), GFP_KERNEL))); |
| 912 | if (err) |
| 913 | goto bail; |
| 914 | VERIFY(err, 0 < (num = buf_get_pages(buf, len, num, 1, pages, num))); |
| 915 | if (err) |
| 916 | goto bail; |
| 917 | |
| 918 | VERIFY(err, 0 == fastrpc_mmap_on_dsp(me, mmap, pages, num)); |
| 919 | if (err) |
| 920 | goto bail; |
| 921 | map->vaddrin = mmap->vaddrin; |
| 922 | map->vaddrout = mmap->vaddrout; |
| 923 | map->size = mmap->size; |
| 924 | INIT_HLIST_NODE(&map->hn); |
| 925 | spin_lock(&fdata->hlock); |
| 926 | hlist_add_head(&map->hn, &fdata->hlst); |
| 927 | spin_unlock(&fdata->hlock); |
| 928 | bail: |
| 929 | if (err && map) { |
| 930 | free_map(map); |
| 931 | kfree(map); |
| 932 | } |
| 933 | kfree(pages); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 934 | return err; |
| 935 | } |
| 936 | |
| 937 | static void cleanup_current_dev(void) |
| 938 | { |
| 939 | struct fastrpc_apps *me = &gfa; |
| 940 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 941 | struct hlist_head *head; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 942 | struct hlist_node *pos, *n; |
| 943 | struct fastrpc_device *dev, *devfree; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 944 | |
| 945 | rnext: |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 946 | devfree = dev = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 947 | spin_lock(&me->hlock); |
| 948 | head = &me->htbl[h]; |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 949 | hlist_for_each_entry_safe(dev, pos, n, head, hn) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 950 | if (dev->tgid == current->tgid) { |
| 951 | hlist_del(&dev->hn); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 952 | devfree = dev; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 953 | break; |
| 954 | } |
| 955 | } |
| 956 | spin_unlock(&me->hlock); |
Mitchel Humpherys | 8388b3b | 2013-03-04 18:07:45 -0800 | [diff] [blame] | 957 | if (devfree) { |
| 958 | free_dev(devfree); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 959 | goto rnext; |
| 960 | } |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
| 965 | { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 966 | struct file_data *fdata = (struct file_data *)file->private_data; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 967 | (void)fastrpc_release_current_dsp_process(); |
| 968 | cleanup_current_dev(); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 969 | if (fdata) { |
| 970 | struct fastrpc_mmap *map; |
| 971 | struct hlist_node *n, *pos; |
| 972 | file->private_data = 0; |
| 973 | hlist_for_each_entry_safe(map, pos, n, &fdata->hlst, hn) { |
| 974 | hlist_del(&map->hn); |
| 975 | free_map(map); |
| 976 | kfree(map); |
| 977 | } |
| 978 | kfree(fdata); |
| 979 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | static int fastrpc_device_open(struct inode *inode, struct file *filp) |
| 984 | { |
| 985 | int err = 0; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 986 | filp->private_data = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 987 | if (0 != try_module_get(THIS_MODULE)) { |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 988 | struct file_data *fdata = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 989 | /* This call will cause a dev to be created |
| 990 | * which will addref this module |
| 991 | */ |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 992 | VERIFY(err, 0 != (fdata = kzalloc(sizeof(*fdata), GFP_KERNEL))); |
| 993 | if (err) |
| 994 | goto bail; |
| 995 | |
| 996 | spin_lock_init(&fdata->hlock); |
| 997 | INIT_HLIST_HEAD(&fdata->hlst); |
| 998 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 999 | VERIFY(err, 0 == fastrpc_create_current_dsp_process()); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1000 | if (err) |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 1001 | goto bail; |
| 1002 | filp->private_data = fdata; |
| 1003 | bail: |
| 1004 | if (err) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1005 | cleanup_current_dev(); |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 1006 | kfree(fdata); |
| 1007 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1008 | module_put(THIS_MODULE); |
| 1009 | } |
| 1010 | return err; |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 1015 | unsigned long ioctl_param) |
| 1016 | { |
| 1017 | struct fastrpc_apps *me = &gfa; |
| 1018 | struct fastrpc_ioctl_invoke invoke; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 1019 | struct fastrpc_ioctl_mmap mmap; |
| 1020 | struct fastrpc_ioctl_munmap munmap; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1021 | remote_arg_t *pra = 0; |
| 1022 | void *param = (char *)ioctl_param; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 1023 | struct file_data *fdata = (struct file_data *)file->private_data; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1024 | int bufs, err = 0; |
| 1025 | |
| 1026 | switch (ioctl_num) { |
| 1027 | case FASTRPC_IOCTL_INVOKE: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1028 | VERIFY(err, 0 == copy_from_user(&invoke, param, |
| 1029 | sizeof(invoke))); |
| 1030 | if (err) |
| 1031 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1032 | bufs = REMOTE_SCALARS_INBUFS(invoke.sc) + |
| 1033 | REMOTE_SCALARS_OUTBUFS(invoke.sc); |
| 1034 | if (bufs) { |
| 1035 | bufs = bufs * sizeof(*pra); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1036 | VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL))); |
| 1037 | if (err) |
| 1038 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1039 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1040 | VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs)); |
| 1041 | if (err) |
| 1042 | goto bail; |
| 1043 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1044 | pra))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1045 | if (err) |
| 1046 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1047 | break; |
Mitchel Humpherys | 0d99a79 | 2013-03-05 13:41:14 -0800 | [diff] [blame^] | 1048 | case FASTRPC_IOCTL_MMAP: |
| 1049 | VERIFY(err, 0 == copy_from_user(&mmap, param, |
| 1050 | sizeof(mmap))); |
| 1051 | if (err) |
| 1052 | goto bail; |
| 1053 | VERIFY(err, 0 == (err = fastrpc_internal_mmap(me, fdata, |
| 1054 | &mmap))); |
| 1055 | if (err) |
| 1056 | goto bail; |
| 1057 | VERIFY(err, 0 == copy_to_user(param, &mmap, sizeof(mmap))); |
| 1058 | if (err) |
| 1059 | goto bail; |
| 1060 | break; |
| 1061 | case FASTRPC_IOCTL_MUNMAP: |
| 1062 | VERIFY(err, 0 == copy_from_user(&munmap, param, |
| 1063 | sizeof(munmap))); |
| 1064 | if (err) |
| 1065 | goto bail; |
| 1066 | VERIFY(err, 0 == (err = fastrpc_internal_munmap(me, fdata, |
| 1067 | &munmap))); |
| 1068 | if (err) |
| 1069 | goto bail; |
| 1070 | break; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1071 | default: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1072 | err = -ENOTTY; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1073 | break; |
| 1074 | } |
| 1075 | bail: |
| 1076 | kfree(pra); |
| 1077 | return err; |
| 1078 | } |
| 1079 | |
| 1080 | static const struct file_operations fops = { |
| 1081 | .open = fastrpc_device_open, |
| 1082 | .release = fastrpc_device_release, |
| 1083 | .unlocked_ioctl = fastrpc_device_ioctl, |
| 1084 | }; |
| 1085 | |
| 1086 | static int __init fastrpc_device_init(void) |
| 1087 | { |
| 1088 | struct fastrpc_apps *me = &gfa; |
| 1089 | int err = 0; |
| 1090 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1091 | VERIFY(err, 0 == fastrpc_init()); |
| 1092 | if (err) |
| 1093 | goto bail; |
| 1094 | VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME)); |
| 1095 | if (err) |
| 1096 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1097 | cdev_init(&me->cdev, &fops); |
| 1098 | me->cdev.owner = THIS_MODULE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1099 | VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1)); |
| 1100 | if (err) |
| 1101 | goto bail; |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1102 | me->class = class_create(THIS_MODULE, "chardrv"); |
| 1103 | VERIFY(err, !IS_ERR(me->class)); |
| 1104 | if (err) |
| 1105 | goto bail; |
| 1106 | me->dev = device_create(me->class, NULL, MKDEV(MAJOR(me->dev_no), 0), |
| 1107 | NULL, DEVICE_NAME); |
| 1108 | VERIFY(err, !IS_ERR(me->dev)); |
| 1109 | if (err) |
| 1110 | goto bail; |
| 1111 | pr_info("'created /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1112 | bail: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1113 | if (err) { |
| 1114 | if (me->dev_no) |
| 1115 | unregister_chrdev_region(me->dev_no, 1); |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1116 | if (me->class) |
| 1117 | class_destroy(me->class); |
| 1118 | if (me->cdev.owner) |
| 1119 | cdev_del(&me->cdev); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 1120 | fastrpc_deinit(); |
| 1121 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1122 | return err; |
| 1123 | } |
| 1124 | |
| 1125 | static void __exit fastrpc_device_exit(void) |
| 1126 | { |
| 1127 | struct fastrpc_apps *me = &gfa; |
| 1128 | |
| 1129 | fastrpc_deinit(); |
Mitchel Humpherys | 5587765 | 2013-02-02 11:23:42 -0800 | [diff] [blame] | 1130 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), 0)); |
| 1131 | class_destroy(me->class); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1132 | cdev_del(&me->cdev); |
| 1133 | unregister_chrdev_region(me->dev_no, 1); |
| 1134 | } |
| 1135 | |
| 1136 | module_init(fastrpc_device_init); |
| 1137 | module_exit(fastrpc_device_exit); |
| 1138 | |
| 1139 | MODULE_LICENSE("GPL v2"); |