| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup kernel_generic_ipc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <synch/spinlock.h>
|
|---|
| 36 | #include <ipc/ipc.h>
|
|---|
| 37 | #include <arch.h>
|
|---|
| 38 | #include <proc/task.h>
|
|---|
| 39 | #include <ipc/ipcrsc.h>
|
|---|
| 40 | #include <assert.h>
|
|---|
| 41 | #include <abi/errno.h>
|
|---|
| 42 | #include <cap/cap.h>
|
|---|
| 43 | #include <mm/slab.h>
|
|---|
| 44 |
|
|---|
| 45 | static void phone_destroy(void *arg)
|
|---|
| 46 | {
|
|---|
| 47 | phone_t *phone = (phone_t *) arg;
|
|---|
| 48 | slab_free(phone_cache, phone);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | static kobject_ops_t phone_kobject_ops = {
|
|---|
| 52 | .destroy = phone_destroy
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | /** Allocate new phone in the specified task.
|
|---|
| 56 | *
|
|---|
| 57 | * @param[in] task Task for which to allocate a new phone.
|
|---|
| 58 | * @param[in] publish If true, the new capability will be published.
|
|---|
| 59 | * @param[out] phandle New phone capability handle.
|
|---|
| 60 | * @param[out] kobject New phone kobject.
|
|---|
| 61 | *
|
|---|
| 62 | * @return An error code if a new capability cannot be allocated.
|
|---|
| 63 | */
|
|---|
| 64 | errno_t phone_alloc(task_t *task, bool publish, cap_phone_handle_t *phandle,
|
|---|
| 65 | kobject_t **kobject)
|
|---|
| 66 | {
|
|---|
| 67 | cap_handle_t handle;
|
|---|
| 68 | errno_t rc = cap_alloc(task, &handle);
|
|---|
| 69 | if (rc == EOK) {
|
|---|
| 70 | phone_t *phone = slab_alloc(phone_cache, FRAME_ATOMIC);
|
|---|
| 71 | if (!phone) {
|
|---|
| 72 | cap_free(TASK, handle);
|
|---|
| 73 | return ENOMEM;
|
|---|
| 74 | }
|
|---|
| 75 | kobject_t *kobj = malloc(sizeof(kobject_t));
|
|---|
| 76 | if (!kobj) {
|
|---|
| 77 | cap_free(TASK, handle);
|
|---|
| 78 | slab_free(phone_cache, phone);
|
|---|
| 79 | return ENOMEM;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | ipc_phone_init(phone, task);
|
|---|
| 83 | phone->state = IPC_PHONE_CONNECTING;
|
|---|
| 84 |
|
|---|
| 85 | kobject_initialize(kobj, KOBJECT_TYPE_PHONE, phone,
|
|---|
| 86 | &phone_kobject_ops);
|
|---|
| 87 | phone->kobject = kobj;
|
|---|
| 88 |
|
|---|
| 89 | if (publish)
|
|---|
| 90 | cap_publish(task, handle, kobj);
|
|---|
| 91 |
|
|---|
| 92 | *phandle = handle;
|
|---|
| 93 | if (kobject)
|
|---|
| 94 | *kobject = kobj;
|
|---|
| 95 | }
|
|---|
| 96 | return rc;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /** Free slot from a disconnected phone.
|
|---|
| 100 | *
|
|---|
| 101 | * All already sent messages will be correctly processed.
|
|---|
| 102 | *
|
|---|
| 103 | * @param handle Phone capability handle of the phone to be freed.
|
|---|
| 104 | *
|
|---|
| 105 | */
|
|---|
| 106 | void phone_dealloc(cap_phone_handle_t handle)
|
|---|
| 107 | {
|
|---|
| 108 | kobject_t *kobj = cap_unpublish(TASK, handle, KOBJECT_TYPE_PHONE);
|
|---|
| 109 | if (!kobj)
|
|---|
| 110 | return;
|
|---|
| 111 |
|
|---|
| 112 | kobject_put(kobj);
|
|---|
| 113 | cap_free(TASK, handle);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** @}
|
|---|
| 117 | */
|
|---|