Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, The Linux Foundation. All rights reserved. |
| 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 | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 14 | #include <linux/scatterlist.h> |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 15 | #include "adsprpc.h" |
| 16 | |
| 17 | struct smq_invoke_ctx { |
| 18 | struct completion work; |
| 19 | int retval; |
| 20 | atomic_t free; |
| 21 | }; |
| 22 | |
| 23 | struct smq_context_list { |
| 24 | struct smq_invoke_ctx *ls; |
| 25 | int size; |
| 26 | int last; |
| 27 | }; |
| 28 | |
| 29 | struct fastrpc_apps { |
| 30 | smd_channel_t *chan; |
| 31 | struct smq_context_list clst; |
| 32 | struct completion work; |
| 33 | struct ion_client *iclient; |
| 34 | struct cdev cdev; |
| 35 | dev_t dev_no; |
| 36 | spinlock_t wrlock; |
| 37 | spinlock_t hlock; |
| 38 | struct hlist_head htbl[RPC_HASH_SZ]; |
| 39 | }; |
| 40 | |
| 41 | struct fastrpc_buf { |
| 42 | struct ion_handle *handle; |
| 43 | void *virt; |
| 44 | ion_phys_addr_t phys; |
| 45 | int size; |
| 46 | int used; |
| 47 | }; |
| 48 | |
| 49 | struct fastrpc_device { |
| 50 | uint32_t tgid; |
| 51 | struct hlist_node hn; |
| 52 | struct fastrpc_buf buf; |
| 53 | }; |
| 54 | |
| 55 | static struct fastrpc_apps gfa; |
| 56 | |
| 57 | static void free_mem(struct fastrpc_buf *buf) |
| 58 | { |
| 59 | struct fastrpc_apps *me = &gfa; |
| 60 | |
| 61 | if (buf->handle) { |
| 62 | if (buf->virt) { |
| 63 | ion_unmap_kernel(me->iclient, buf->handle); |
| 64 | buf->virt = 0; |
| 65 | } |
| 66 | ion_free(me->iclient, buf->handle); |
| 67 | buf->handle = 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static int alloc_mem(struct fastrpc_buf *buf) |
| 72 | { |
| 73 | struct ion_client *clnt = gfa.iclient; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 74 | struct sg_table *sg; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 75 | int err = 0; |
| 76 | |
| 77 | buf->handle = ion_alloc(clnt, buf->size, SZ_4K, |
Hanumant Singh | 7d72bad | 2012-08-29 18:39:44 -0700 | [diff] [blame] | 78 | ION_HEAP(ION_AUDIO_HEAP_ID), 0); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 79 | VERIFY(err, 0 == IS_ERR_OR_NULL(buf->handle)); |
| 80 | if (err) |
| 81 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 82 | buf->virt = 0; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 83 | VERIFY(err, 0 != (buf->virt = ion_map_kernel(clnt, buf->handle))); |
| 84 | if (err) |
| 85 | goto bail; |
| 86 | VERIFY(err, 0 != (sg = ion_sg_table(clnt, buf->handle))); |
| 87 | if (err) |
| 88 | goto bail; |
| 89 | VERIFY(err, 1 == sg->nents); |
| 90 | if (err) |
| 91 | goto bail; |
| 92 | buf->phys = sg_dma_address(sg->sgl); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 93 | bail: |
| 94 | if (err && !IS_ERR_OR_NULL(buf->handle)) |
| 95 | free_mem(buf); |
| 96 | return err; |
| 97 | } |
| 98 | |
| 99 | static int context_list_ctor(struct smq_context_list *me, int size) |
| 100 | { |
| 101 | int err = 0; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 102 | VERIFY(err, 0 != (me->ls = kzalloc(size, GFP_KERNEL))); |
| 103 | if (err) |
| 104 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 105 | me->size = size / sizeof(*me->ls); |
| 106 | me->last = 0; |
| 107 | bail: |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | static void context_list_dtor(struct smq_context_list *me) |
| 112 | { |
| 113 | kfree(me->ls); |
| 114 | me->ls = 0; |
| 115 | } |
| 116 | |
| 117 | static void context_list_alloc_ctx(struct smq_context_list *me, |
| 118 | struct smq_invoke_ctx **po) |
| 119 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 120 | int i = me->last; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 121 | struct smq_invoke_ctx *ctx; |
| 122 | |
| 123 | for (;;) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 124 | i = i % me->size; |
| 125 | ctx = &me->ls[i]; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 126 | if (atomic_read(&ctx->free) == 0) |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 127 | if (atomic_cmpxchg(&ctx->free, 0, 1) == 0) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 128 | break; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 129 | i++; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 130 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 131 | me->last = i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 132 | ctx->retval = -1; |
| 133 | init_completion(&ctx->work); |
| 134 | *po = ctx; |
| 135 | } |
| 136 | |
| 137 | static void context_free(struct smq_invoke_ctx *me) |
| 138 | { |
| 139 | if (me) |
| 140 | atomic_set(&me->free, 0); |
| 141 | } |
| 142 | |
| 143 | static void context_notify_user(struct smq_invoke_ctx *me, int retval) |
| 144 | { |
| 145 | me->retval = retval; |
| 146 | complete(&me->work); |
| 147 | } |
| 148 | |
| 149 | static void context_notify_all_users(struct smq_context_list *me) |
| 150 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 151 | int i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 152 | |
| 153 | if (!me->ls) |
| 154 | return; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 155 | for (i = 0; i < me->size; ++i) { |
| 156 | if (atomic_read(&me->ls[i].free) != 0) |
| 157 | complete(&me->ls[i].work); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | static int get_page_list(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 162 | struct fastrpc_buf *ibuf, struct fastrpc_buf *obuf) |
| 163 | { |
| 164 | struct smq_phy_page *pgstart, *pages; |
| 165 | struct smq_invoke_buf *list; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 166 | int i, rlen, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 167 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 168 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 169 | |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 170 | LOCK_MMAP(kernel); |
| 171 | *obuf = *ibuf; |
| 172 | retry: |
| 173 | list = smq_invoke_buf_start((remote_arg_t *)obuf->virt, sc); |
| 174 | pgstart = smq_phy_page_start(sc, list); |
| 175 | pages = pgstart + 1; |
| 176 | rlen = obuf->size - ((uint32_t)pages - (uint32_t)obuf->virt); |
| 177 | if (rlen < 0) { |
| 178 | rlen = ((uint32_t)pages - (uint32_t)obuf->virt) - obuf->size; |
| 179 | obuf->size += buf_page_size(rlen); |
| 180 | obuf->handle = 0; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 181 | VERIFY(err, 0 == alloc_mem(obuf)); |
| 182 | if (err) |
| 183 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 184 | goto retry; |
| 185 | } |
| 186 | pgstart->addr = obuf->phys; |
| 187 | pgstart->size = obuf->size; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 188 | for (i = 0; i < inbufs + outbufs; ++i) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 189 | void *buf; |
| 190 | int len, num; |
| 191 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 192 | len = pra[i].buf.len; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 193 | if (!len) |
| 194 | continue; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 195 | buf = pra[i].buf.pv; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 196 | num = buf_num_pages(buf, len); |
| 197 | if (!kernel) |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 198 | list[i].num = buf_get_pages(buf, len, num, |
| 199 | i >= inbufs, pages, rlen / sizeof(*pages)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 200 | else |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 201 | list[i].num = 0; |
| 202 | VERIFY(err, list[i].num >= 0); |
| 203 | if (err) |
| 204 | goto bail; |
| 205 | if (list[i].num) { |
| 206 | list[i].pgidx = pages - pgstart; |
| 207 | pages = pages + list[i].num; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 208 | } else if (rlen > sizeof(*pages)) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 209 | list[i].pgidx = pages - pgstart; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 210 | pages = pages + 1; |
| 211 | } else { |
| 212 | if (obuf->handle != ibuf->handle) |
| 213 | free_mem(obuf); |
| 214 | obuf->size += buf_page_size(sizeof(*pages)); |
| 215 | obuf->handle = 0; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 216 | VERIFY(err, 0 == alloc_mem(obuf)); |
| 217 | if (err) |
| 218 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 219 | goto retry; |
| 220 | } |
| 221 | rlen = obuf->size - ((uint32_t) pages - (uint32_t) obuf->virt); |
| 222 | } |
| 223 | obuf->used = obuf->size - rlen; |
| 224 | bail: |
| 225 | if (err && (obuf->handle != ibuf->handle)) |
| 226 | free_mem(obuf); |
| 227 | UNLOCK_MMAP(kernel); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 228 | return err; |
| 229 | } |
| 230 | |
| 231 | static int get_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 232 | remote_arg_t *rpra, remote_arg_t *upra, |
| 233 | struct fastrpc_buf *ibuf, struct fastrpc_buf **abufs, |
| 234 | int *nbufs) |
| 235 | { |
| 236 | struct smq_invoke_buf *list; |
| 237 | struct fastrpc_buf *pbuf = ibuf, *obufs = 0; |
| 238 | struct smq_phy_page *pages; |
| 239 | void *args; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 240 | int i, rlen, size, used, inh, bufs = 0, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 241 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 242 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 243 | |
| 244 | list = smq_invoke_buf_start(rpra, sc); |
| 245 | pages = smq_phy_page_start(sc, list); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 246 | used = ALIGN(pbuf->used, BALIGN); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 247 | args = (void *)((char *)pbuf->virt + used); |
| 248 | rlen = pbuf->size - used; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 249 | for (i = 0; i < inbufs + outbufs; ++i) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 250 | int num; |
| 251 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 252 | rpra[i].buf.len = pra[i].buf.len; |
| 253 | if (list[i].num) { |
| 254 | rpra[i].buf.pv = pra[i].buf.pv; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 255 | continue; |
| 256 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 257 | if (rlen < pra[i].buf.len) { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 258 | struct fastrpc_buf *b; |
| 259 | pbuf->used = pbuf->size - rlen; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 260 | VERIFY(err, 0 != (b = krealloc(obufs, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 261 | (bufs + 1) * sizeof(*obufs), GFP_KERNEL))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 262 | if (err) |
| 263 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 264 | obufs = b; |
| 265 | pbuf = obufs + bufs; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 266 | pbuf->size = buf_num_pages(0, pra[i].buf.len) * |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 267 | PAGE_SIZE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 268 | VERIFY(err, 0 == alloc_mem(pbuf)); |
| 269 | if (err) |
| 270 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 271 | bufs++; |
| 272 | args = pbuf->virt; |
| 273 | rlen = pbuf->size; |
| 274 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 275 | num = buf_num_pages(args, pra[i].buf.len); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 276 | if (pbuf == ibuf) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 277 | list[i].num = num; |
| 278 | list[i].pgidx = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 279 | } else { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 280 | list[i].num = 1; |
| 281 | pages[list[i].pgidx].addr = |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 282 | buf_page_start((void *)(pbuf->phys + |
| 283 | (pbuf->size - rlen))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 284 | pages[list[i].pgidx].size = |
| 285 | buf_page_size(pra[i].buf.len); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 286 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 287 | if (i < inbufs) { |
| 288 | if (!kernel) { |
| 289 | VERIFY(err, 0 == copy_from_user(args, |
| 290 | pra[i].buf.pv, pra[i].buf.len)); |
| 291 | if (err) |
| 292 | goto bail; |
| 293 | } else { |
| 294 | memmove(args, pra[i].buf.pv, pra[i].buf.len); |
| 295 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 296 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 297 | rpra[i].buf.pv = args; |
| 298 | args = (void *)((char *)args + ALIGN(pra[i].buf.len, BALIGN)); |
| 299 | rlen -= ALIGN(pra[i].buf.len, BALIGN); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 300 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 301 | for (i = 0; i < inbufs; ++i) { |
| 302 | if (rpra[i].buf.len) |
| 303 | dmac_flush_range(rpra[i].buf.pv, |
| 304 | (char *)rpra[i].buf.pv + rpra[i].buf.len); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 305 | } |
| 306 | pbuf->used = pbuf->size - rlen; |
| 307 | size = sizeof(*rpra) * REMOTE_SCALARS_INHANDLES(sc); |
| 308 | if (size) { |
| 309 | inh = inbufs + outbufs; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 310 | if (!kernel) { |
| 311 | VERIFY(err, 0 == copy_from_user(&rpra[inh], &upra[inh], |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 312 | size)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 313 | if (err) |
| 314 | goto bail; |
| 315 | } else { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 316 | memmove(&rpra[inh], &upra[inh], size); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 317 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 318 | } |
| 319 | dmac_flush_range(rpra, (char *)rpra + used); |
| 320 | bail: |
| 321 | *abufs = obufs; |
| 322 | *nbufs = bufs; |
| 323 | return err; |
| 324 | } |
| 325 | |
| 326 | static int put_args(uint32_t kernel, uint32_t sc, remote_arg_t *pra, |
| 327 | remote_arg_t *rpra, remote_arg_t *upra) |
| 328 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 329 | int i, inbufs, outbufs, outh, size; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 330 | int err = 0; |
| 331 | |
| 332 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 333 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 334 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 335 | if (rpra[i].buf.pv != pra[i].buf.pv) { |
| 336 | VERIFY(err, 0 == copy_to_user(pra[i].buf.pv, |
| 337 | rpra[i].buf.pv, rpra[i].buf.len)); |
| 338 | if (err) |
| 339 | goto bail; |
| 340 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 341 | } |
| 342 | size = sizeof(*rpra) * REMOTE_SCALARS_OUTHANDLES(sc); |
| 343 | if (size) { |
| 344 | outh = inbufs + outbufs + REMOTE_SCALARS_INHANDLES(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 345 | if (!kernel) { |
| 346 | VERIFY(err, 0 == copy_to_user(&upra[outh], &rpra[outh], |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 347 | size)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 348 | if (err) |
| 349 | goto bail; |
| 350 | } else { |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 351 | memmove(&upra[outh], &rpra[outh], size); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 352 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 353 | } |
| 354 | bail: |
| 355 | return err; |
| 356 | } |
| 357 | |
| 358 | static void inv_args(uint32_t sc, remote_arg_t *rpra, int used) |
| 359 | { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 360 | int i, inbufs, outbufs; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 361 | int inv = 0; |
| 362 | |
| 363 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 364 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 365 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 366 | if (buf_page_start(rpra) == buf_page_start(rpra[i].buf.pv)) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 367 | inv = 1; |
| 368 | else |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 369 | dmac_inv_range(rpra[i].buf.pv, |
| 370 | (char *)rpra[i].buf.pv + rpra[i].buf.len); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | if (inv || REMOTE_SCALARS_OUTHANDLES(sc)) |
| 374 | dmac_inv_range(rpra, (char *)rpra + used); |
| 375 | } |
| 376 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 377 | static int fastrpc_invoke_send(struct fastrpc_apps *me, uint32_t handle, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 378 | uint32_t sc, struct smq_invoke_ctx *ctx, |
| 379 | struct fastrpc_buf *buf) |
| 380 | { |
| 381 | struct smq_msg msg; |
| 382 | int err = 0, len; |
| 383 | |
| 384 | msg.pid = current->tgid; |
| 385 | msg.tid = current->pid; |
| 386 | msg.invoke.header.ctx = ctx; |
| 387 | msg.invoke.header.handle = handle; |
| 388 | msg.invoke.header.sc = sc; |
| 389 | msg.invoke.page.addr = buf->phys; |
| 390 | msg.invoke.page.size = buf_page_size(buf->used); |
| 391 | spin_lock(&me->wrlock); |
| 392 | len = smd_write(me->chan, &msg, sizeof(msg)); |
| 393 | spin_unlock(&me->wrlock); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 394 | VERIFY(err, len == sizeof(msg)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 395 | return err; |
| 396 | } |
| 397 | |
| 398 | static void fastrpc_deinit(void) |
| 399 | { |
| 400 | struct fastrpc_apps *me = &gfa; |
| 401 | |
| 402 | if (me->chan) |
| 403 | (void)smd_close(me->chan); |
| 404 | context_list_dtor(&me->clst); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 405 | if (me->iclient) |
| 406 | ion_client_destroy(me->iclient); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 407 | me->iclient = 0; |
| 408 | me->chan = 0; |
| 409 | } |
| 410 | |
| 411 | static void fastrpc_read_handler(void) |
| 412 | { |
| 413 | struct fastrpc_apps *me = &gfa; |
| 414 | struct smq_invoke_rsp rsp; |
| 415 | int err = 0; |
| 416 | |
| 417 | do { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 418 | VERIFY(err, sizeof(rsp) == |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 419 | smd_read_from_cb(me->chan, &rsp, sizeof(rsp))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 420 | if (err) |
| 421 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 422 | context_notify_user(rsp.ctx, rsp.retval); |
| 423 | } while (!err); |
| 424 | bail: |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | static void smd_event_handler(void *priv, unsigned event) |
| 429 | { |
| 430 | struct fastrpc_apps *me = (struct fastrpc_apps *)priv; |
| 431 | |
| 432 | switch (event) { |
| 433 | case SMD_EVENT_OPEN: |
| 434 | complete(&(me->work)); |
| 435 | break; |
| 436 | case SMD_EVENT_CLOSE: |
| 437 | context_notify_all_users(&me->clst); |
| 438 | break; |
| 439 | case SMD_EVENT_DATA: |
| 440 | fastrpc_read_handler(); |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | static int fastrpc_init(void) |
| 446 | { |
| 447 | int err = 0; |
| 448 | struct fastrpc_apps *me = &gfa; |
| 449 | |
| 450 | if (me->chan == 0) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 451 | int i; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 452 | spin_lock_init(&me->hlock); |
| 453 | spin_lock_init(&me->wrlock); |
| 454 | init_completion(&me->work); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 455 | for (i = 0; i < RPC_HASH_SZ; ++i) |
| 456 | INIT_HLIST_HEAD(&me->htbl[i]); |
| 457 | VERIFY(err, 0 == context_list_ctor(&me->clst, SZ_4K)); |
| 458 | if (err) |
| 459 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 460 | me->iclient = msm_ion_client_create(ION_HEAP_CARVEOUT_MASK, |
| 461 | DEVICE_NAME); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 462 | VERIFY(err, 0 == IS_ERR_OR_NULL(me->iclient)); |
| 463 | if (err) |
| 464 | goto bail; |
| 465 | VERIFY(err, 0 == smd_named_open_on_edge(FASTRPC_SMD_GUID, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 466 | SMD_APPS_QDSP, &me->chan, |
| 467 | me, smd_event_handler)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 468 | if (err) |
| 469 | goto bail; |
| 470 | VERIFY(err, 0 != wait_for_completion_timeout(&me->work, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 471 | RPC_TIMEOUT)); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 472 | if (err) |
| 473 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 474 | } |
| 475 | bail: |
| 476 | if (err) |
| 477 | fastrpc_deinit(); |
| 478 | return err; |
| 479 | } |
| 480 | |
| 481 | static void free_dev(struct fastrpc_device *dev) |
| 482 | { |
| 483 | if (dev) { |
| 484 | module_put(THIS_MODULE); |
| 485 | free_mem(&dev->buf); |
| 486 | kfree(dev); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | static int alloc_dev(struct fastrpc_device **dev) |
| 491 | { |
| 492 | int err = 0; |
| 493 | struct fastrpc_device *fd = 0; |
| 494 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 495 | VERIFY(err, 0 != try_module_get(THIS_MODULE)); |
| 496 | if (err) |
| 497 | goto bail; |
| 498 | VERIFY(err, 0 != (fd = kzalloc(sizeof(*fd), GFP_KERNEL))); |
| 499 | if (err) |
| 500 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 501 | fd->buf.size = PAGE_SIZE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 502 | VERIFY(err, 0 == alloc_mem(&fd->buf)); |
| 503 | if (err) |
| 504 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 505 | fd->tgid = current->tgid; |
| 506 | INIT_HLIST_NODE(&fd->hn); |
| 507 | *dev = fd; |
| 508 | bail: |
| 509 | if (err) |
| 510 | free_dev(fd); |
| 511 | return err; |
| 512 | } |
| 513 | |
| 514 | static int get_dev(struct fastrpc_apps *me, struct fastrpc_device **rdev) |
| 515 | { |
| 516 | struct hlist_head *head; |
| 517 | struct fastrpc_device *dev = 0; |
| 518 | struct hlist_node *n; |
| 519 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 520 | int err = 0; |
| 521 | |
| 522 | spin_lock(&me->hlock); |
| 523 | head = &me->htbl[h]; |
| 524 | hlist_for_each_entry(dev, n, head, hn) { |
| 525 | if (dev->tgid == current->tgid) { |
| 526 | hlist_del(&dev->hn); |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | spin_unlock(&me->hlock); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 531 | VERIFY(err, dev != 0); |
| 532 | if (err) |
| 533 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 534 | *rdev = dev; |
| 535 | bail: |
| 536 | if (err) { |
| 537 | free_dev(dev); |
| 538 | err = alloc_dev(rdev); |
| 539 | } |
| 540 | return err; |
| 541 | } |
| 542 | |
| 543 | static void add_dev(struct fastrpc_apps *me, struct fastrpc_device *dev) |
| 544 | { |
| 545 | struct hlist_head *head; |
| 546 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 547 | |
| 548 | spin_lock(&me->hlock); |
| 549 | head = &me->htbl[h]; |
| 550 | hlist_add_head(&dev->hn, head); |
| 551 | spin_unlock(&me->hlock); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | static int fastrpc_release_current_dsp_process(void); |
| 556 | |
| 557 | static int fastrpc_internal_invoke(struct fastrpc_apps *me, uint32_t kernel, |
| 558 | struct fastrpc_ioctl_invoke *invoke, remote_arg_t *pra) |
| 559 | { |
| 560 | remote_arg_t *rpra = 0; |
| 561 | struct fastrpc_device *dev = 0; |
| 562 | struct smq_invoke_ctx *ctx = 0; |
| 563 | struct fastrpc_buf obuf, *abufs = 0, *b; |
| 564 | int interrupted = 0; |
| 565 | uint32_t sc; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 566 | int i, nbufs = 0, err = 0; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 567 | |
| 568 | sc = invoke->sc; |
| 569 | obuf.handle = 0; |
| 570 | if (REMOTE_SCALARS_LENGTH(sc)) { |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 571 | VERIFY(err, 0 == get_dev(me, &dev)); |
| 572 | if (err) |
| 573 | goto bail; |
| 574 | VERIFY(err, 0 == get_page_list(kernel, sc, pra, &dev->buf, |
| 575 | &obuf)); |
| 576 | if (err) |
| 577 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 578 | rpra = (remote_arg_t *)obuf.virt; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 579 | VERIFY(err, 0 == get_args(kernel, sc, pra, rpra, invoke->pra, |
| 580 | &obuf, &abufs, &nbufs)); |
| 581 | if (err) |
| 582 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | context_list_alloc_ctx(&me->clst, &ctx); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 586 | VERIFY(err, 0 == fastrpc_invoke_send(me, invoke->handle, sc, ctx, |
| 587 | &obuf)); |
| 588 | if (err) |
| 589 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 590 | inv_args(sc, rpra, obuf.used); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 591 | VERIFY(err, 0 == (interrupted = |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 592 | wait_for_completion_interruptible(&ctx->work))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 593 | if (err) |
| 594 | goto bail; |
| 595 | VERIFY(err, 0 == (err = ctx->retval)); |
| 596 | if (err) |
| 597 | goto bail; |
| 598 | VERIFY(err, 0 == put_args(kernel, sc, pra, rpra, invoke->pra)); |
| 599 | if (err) |
| 600 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 601 | bail: |
| 602 | if (interrupted) { |
| 603 | init_completion(&ctx->work); |
| 604 | if (!kernel) |
| 605 | (void)fastrpc_release_current_dsp_process(); |
| 606 | wait_for_completion(&ctx->work); |
| 607 | } |
| 608 | context_free(ctx); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 609 | for (i = 0, b = abufs; i < nbufs; ++i, ++b) |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 610 | free_mem(b); |
| 611 | kfree(abufs); |
| 612 | if (dev) { |
| 613 | add_dev(me, dev); |
| 614 | if (obuf.handle != dev->buf.handle) |
| 615 | free_mem(&obuf); |
| 616 | } |
| 617 | return err; |
| 618 | } |
| 619 | |
| 620 | static int fastrpc_create_current_dsp_process(void) |
| 621 | { |
| 622 | int err = 0; |
| 623 | struct fastrpc_ioctl_invoke ioctl; |
| 624 | struct fastrpc_apps *me = &gfa; |
| 625 | remote_arg_t ra[1]; |
| 626 | int tgid = 0; |
| 627 | |
| 628 | tgid = current->tgid; |
| 629 | ra[0].buf.pv = &tgid; |
| 630 | ra[0].buf.len = sizeof(tgid); |
| 631 | ioctl.handle = 1; |
| 632 | ioctl.sc = REMOTE_SCALARS_MAKE(0, 1, 0); |
| 633 | ioctl.pra = ra; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 634 | VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 635 | return err; |
| 636 | } |
| 637 | |
| 638 | static int fastrpc_release_current_dsp_process(void) |
| 639 | { |
| 640 | int err = 0; |
| 641 | struct fastrpc_apps *me = &gfa; |
| 642 | struct fastrpc_ioctl_invoke ioctl; |
| 643 | remote_arg_t ra[1]; |
| 644 | int tgid = 0; |
| 645 | |
| 646 | tgid = current->tgid; |
| 647 | ra[0].buf.pv = &tgid; |
| 648 | ra[0].buf.len = sizeof(tgid); |
| 649 | ioctl.handle = 1; |
| 650 | ioctl.sc = REMOTE_SCALARS_MAKE(1, 1, 0); |
| 651 | ioctl.pra = ra; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 652 | VERIFY(err, 0 == fastrpc_internal_invoke(me, 1, &ioctl, ra)); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 653 | return err; |
| 654 | } |
| 655 | |
| 656 | static void cleanup_current_dev(void) |
| 657 | { |
| 658 | struct fastrpc_apps *me = &gfa; |
| 659 | uint32_t h = hash_32(current->tgid, RPC_HASH_BITS); |
| 660 | struct hlist_head *head; |
| 661 | struct hlist_node *pos; |
| 662 | struct fastrpc_device *dev; |
| 663 | |
| 664 | rnext: |
| 665 | dev = 0; |
| 666 | spin_lock(&me->hlock); |
| 667 | head = &me->htbl[h]; |
| 668 | hlist_for_each_entry(dev, pos, head, hn) { |
| 669 | if (dev->tgid == current->tgid) { |
| 670 | hlist_del(&dev->hn); |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | spin_unlock(&me->hlock); |
| 675 | if (dev) { |
| 676 | free_dev(dev); |
| 677 | goto rnext; |
| 678 | } |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
| 683 | { |
| 684 | (void)fastrpc_release_current_dsp_process(); |
| 685 | cleanup_current_dev(); |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static int fastrpc_device_open(struct inode *inode, struct file *filp) |
| 690 | { |
| 691 | int err = 0; |
| 692 | |
| 693 | if (0 != try_module_get(THIS_MODULE)) { |
| 694 | /* This call will cause a dev to be created |
| 695 | * which will addref this module |
| 696 | */ |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 697 | VERIFY(err, 0 == fastrpc_create_current_dsp_process()); |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 698 | if (err) |
| 699 | cleanup_current_dev(); |
| 700 | module_put(THIS_MODULE); |
| 701 | } |
| 702 | return err; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 707 | unsigned long ioctl_param) |
| 708 | { |
| 709 | struct fastrpc_apps *me = &gfa; |
| 710 | struct fastrpc_ioctl_invoke invoke; |
| 711 | remote_arg_t *pra = 0; |
| 712 | void *param = (char *)ioctl_param; |
| 713 | int bufs, err = 0; |
| 714 | |
| 715 | switch (ioctl_num) { |
| 716 | case FASTRPC_IOCTL_INVOKE: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 717 | VERIFY(err, 0 == copy_from_user(&invoke, param, |
| 718 | sizeof(invoke))); |
| 719 | if (err) |
| 720 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 721 | bufs = REMOTE_SCALARS_INBUFS(invoke.sc) + |
| 722 | REMOTE_SCALARS_OUTBUFS(invoke.sc); |
| 723 | if (bufs) { |
| 724 | bufs = bufs * sizeof(*pra); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 725 | VERIFY(err, 0 != (pra = kmalloc(bufs, GFP_KERNEL))); |
| 726 | if (err) |
| 727 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 728 | } |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 729 | VERIFY(err, 0 == copy_from_user(pra, invoke.pra, bufs)); |
| 730 | if (err) |
| 731 | goto bail; |
| 732 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(me, 0, &invoke, |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 733 | pra))); |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 734 | if (err) |
| 735 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 736 | break; |
| 737 | default: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 738 | err = -ENOTTY; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 739 | break; |
| 740 | } |
| 741 | bail: |
| 742 | kfree(pra); |
| 743 | return err; |
| 744 | } |
| 745 | |
| 746 | static const struct file_operations fops = { |
| 747 | .open = fastrpc_device_open, |
| 748 | .release = fastrpc_device_release, |
| 749 | .unlocked_ioctl = fastrpc_device_ioctl, |
| 750 | }; |
| 751 | |
| 752 | static int __init fastrpc_device_init(void) |
| 753 | { |
| 754 | struct fastrpc_apps *me = &gfa; |
| 755 | int err = 0; |
| 756 | |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 757 | VERIFY(err, 0 == fastrpc_init()); |
| 758 | if (err) |
| 759 | goto bail; |
| 760 | VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, 1, DEVICE_NAME)); |
| 761 | if (err) |
| 762 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 763 | cdev_init(&me->cdev, &fops); |
| 764 | me->cdev.owner = THIS_MODULE; |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 765 | VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), 1)); |
| 766 | if (err) |
| 767 | goto bail; |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 768 | pr_info("'mknod /dev/%s c %d 0'\n", DEVICE_NAME, MAJOR(me->dev_no)); |
| 769 | bail: |
Mitchel Humpherys | 42e806e | 2012-09-30 22:27:53 -0700 | [diff] [blame] | 770 | if (err) { |
| 771 | if (me->dev_no) |
| 772 | unregister_chrdev_region(me->dev_no, 1); |
| 773 | fastrpc_deinit(); |
| 774 | } |
Mitchel Humpherys | 79d361e | 2012-08-29 16:20:15 -0700 | [diff] [blame] | 775 | return err; |
| 776 | } |
| 777 | |
| 778 | static void __exit fastrpc_device_exit(void) |
| 779 | { |
| 780 | struct fastrpc_apps *me = &gfa; |
| 781 | |
| 782 | fastrpc_deinit(); |
| 783 | cdev_del(&me->cdev); |
| 784 | unregister_chrdev_region(me->dev_no, 1); |
| 785 | } |
| 786 | |
| 787 | module_init(fastrpc_device_init); |
| 788 | module_exit(fastrpc_device_exit); |
| 789 | |
| 790 | MODULE_LICENSE("GPL v2"); |