[4e49572] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[4e49572] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic_ipc
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[4e49572] | 35 | #include <synch/spinlock.h>
|
---|
| 36 | #include <ipc/ipc.h>
|
---|
| 37 | #include <arch.h>
|
---|
| 38 | #include <proc/task.h>
|
---|
| 39 | #include <ipc/ipcrsc.h>
|
---|
[63e27ef] | 40 | #include <assert.h>
|
---|
[e8039a86] | 41 | #include <abi/errno.h>
|
---|
[3f74275] | 42 | #include <cap/cap.h>
|
---|
[63d8f43] | 43 | #include <mm/slab.h>
|
---|
[4e49572] | 44 |
|
---|
[48bcf49] | 45 | static void phone_destroy(void *arg)
|
---|
[05ffb41] | 46 | {
|
---|
[48bcf49] | 47 | phone_t *phone = (phone_t *) arg;
|
---|
[b9a2725] | 48 | if (phone->hangup_call)
|
---|
| 49 | kobject_put(phone->hangup_call->kobject);
|
---|
[82d515e9] | 50 | slab_free(phone_cache, phone);
|
---|
[48bcf49] | 51 | }
|
---|
[05ffb41] | 52 |
|
---|
[48bcf49] | 53 | static kobject_ops_t phone_kobject_ops = {
|
---|
| 54 | .destroy = phone_destroy
|
---|
| 55 | };
|
---|
[63d8f43] | 56 |
|
---|
[05ffb41] | 57 | /** Allocate new phone in the specified task.
|
---|
[8b243f2] | 58 | *
|
---|
[334c103] | 59 | * @param[in] task Task for which to allocate a new phone.
|
---|
| 60 | * @param[in] publish If true, the new capability will be published.
|
---|
| 61 | * @param[out] phandle New phone capability handle.
|
---|
| 62 | * @param[out] kobject New phone kobject.
|
---|
[09d01f2] | 63 | *
|
---|
[cde999a] | 64 | * @return An error code if a new capability cannot be allocated.
|
---|
[8b243f2] | 65 | */
|
---|
[eadaeae8] | 66 | errno_t phone_alloc(task_t *task, bool publish, cap_phone_handle_t *phandle,
|
---|
[334c103] | 67 | kobject_t **kobject)
|
---|
[7c7aae16] | 68 | {
|
---|
[09d01f2] | 69 | cap_handle_t handle;
|
---|
[b7fd2a0] | 70 | errno_t rc = cap_alloc(task, &handle);
|
---|
[09d01f2] | 71 | if (rc == EOK) {
|
---|
[82d515e9] | 72 | phone_t *phone = slab_alloc(phone_cache, FRAME_ATOMIC);
|
---|
[63d8f43] | 73 | if (!phone) {
|
---|
| 74 | cap_free(TASK, handle);
|
---|
| 75 | return ENOMEM;
|
---|
| 76 | }
|
---|
[11b285d] | 77 | kobject_t *kobj = malloc(sizeof(kobject_t));
|
---|
[334c103] | 78 | if (!kobj) {
|
---|
[48bcf49] | 79 | cap_free(TASK, handle);
|
---|
[82d515e9] | 80 | slab_free(phone_cache, phone);
|
---|
[48bcf49] | 81 | return ENOMEM;
|
---|
| 82 | }
|
---|
[b9a2725] | 83 | call_t *hcall = ipc_call_alloc(FRAME_ATOMIC);
|
---|
| 84 | if (!hcall) {
|
---|
| 85 | cap_free(TASK, handle);
|
---|
| 86 | slab_free(phone_cache, phone);
|
---|
| 87 | free(kobj);
|
---|
| 88 | return ENOMEM;
|
---|
| 89 | }
|
---|
[48bcf49] | 90 |
|
---|
[63d8f43] | 91 | ipc_phone_init(phone, task);
|
---|
| 92 | phone->state = IPC_PHONE_CONNECTING;
|
---|
[b9a2725] | 93 | phone->hangup_call = hcall;
|
---|
[9e87562] | 94 |
|
---|
[334c103] | 95 | kobject_initialize(kobj, KOBJECT_TYPE_PHONE, phone,
|
---|
[48bcf49] | 96 | &phone_kobject_ops);
|
---|
[334c103] | 97 | phone->kobject = kobj;
|
---|
[a35b458] | 98 |
|
---|
[334c103] | 99 | if (publish)
|
---|
| 100 | cap_publish(task, handle, kobj);
|
---|
[09d01f2] | 101 |
|
---|
[334c103] | 102 | *phandle = handle;
|
---|
| 103 | if (kobject)
|
---|
| 104 | *kobject = kobj;
|
---|
[05ffb41] | 105 | }
|
---|
[09d01f2] | 106 | return rc;
|
---|
[7c7aae16] | 107 | }
|
---|
| 108 |
|
---|
[8b243f2] | 109 | /** Free slot from a disconnected phone.
|
---|
| 110 | *
|
---|
| 111 | * All already sent messages will be correctly processed.
|
---|
[fbcfd458] | 112 | *
|
---|
[3f74275] | 113 | * @param handle Phone capability handle of the phone to be freed.
|
---|
[da1bafb] | 114 | *
|
---|
[fbcfd458] | 115 | */
|
---|
[eadaeae8] | 116 | void phone_dealloc(cap_phone_handle_t handle)
|
---|
[4e49572] | 117 | {
|
---|
[48bcf49] | 118 | kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
|
---|
| 119 | if (!kobj)
|
---|
| 120 | return;
|
---|
[a35b458] | 121 |
|
---|
[48bcf49] | 122 | kobject_put(kobj);
|
---|
[3f74275] | 123 | cap_free(TASK, handle);
|
---|
[4e49572] | 124 | }
|
---|
| 125 |
|
---|
[cc73a8a1] | 126 | /** @}
|
---|
[b45c443] | 127 | */
|
---|