[d1bafbf] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 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 devman
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include <errno.h>
|
---|
| 34 |
|
---|
| 35 | #include "dev.h"
|
---|
| 36 | #include "devman.h"
|
---|
| 37 |
|
---|
| 38 | /** Create a new device node.
|
---|
| 39 | *
|
---|
| 40 | * @return A device node structure.
|
---|
| 41 | */
|
---|
| 42 | dev_node_t *create_dev_node(void)
|
---|
| 43 | {
|
---|
| 44 | dev_node_t *dev;
|
---|
| 45 |
|
---|
| 46 | dev = calloc(1, sizeof(dev_node_t));
|
---|
| 47 | if (dev == NULL)
|
---|
| 48 | return NULL;
|
---|
| 49 |
|
---|
| 50 | atomic_set(&dev->refcnt, 0);
|
---|
| 51 | list_initialize(&dev->functions);
|
---|
| 52 | link_initialize(&dev->driver_devices);
|
---|
| 53 |
|
---|
| 54 | return dev;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /** Delete a device node.
|
---|
| 58 | *
|
---|
| 59 | * @param node The device node structure.
|
---|
| 60 | */
|
---|
| 61 | void delete_dev_node(dev_node_t *dev)
|
---|
| 62 | {
|
---|
| 63 | assert(list_empty(&dev->functions));
|
---|
| 64 | assert(dev->pfun == NULL);
|
---|
| 65 | assert(dev->drv == NULL);
|
---|
| 66 |
|
---|
| 67 | free(dev);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /** Increase device node reference count.
|
---|
| 71 | *
|
---|
| 72 | * @param dev Device node
|
---|
| 73 | */
|
---|
| 74 | void dev_add_ref(dev_node_t *dev)
|
---|
| 75 | {
|
---|
| 76 | atomic_inc(&dev->refcnt);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /** Decrease device node reference count.
|
---|
| 80 | *
|
---|
| 81 | * When the count drops to zero the device node is freed.
|
---|
| 82 | *
|
---|
| 83 | * @param dev Device node
|
---|
| 84 | */
|
---|
| 85 | void dev_del_ref(dev_node_t *dev)
|
---|
| 86 | {
|
---|
| 87 | if (atomic_predec(&dev->refcnt) == 0)
|
---|
| 88 | delete_dev_node(dev);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /** Find the device node structure of the device witch has the specified handle.
|
---|
| 92 | *
|
---|
| 93 | * @param tree The device tree where we look for the device node.
|
---|
| 94 | * @param handle The handle of the device.
|
---|
| 95 | * @return The device node.
|
---|
| 96 | */
|
---|
| 97 | dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
|
---|
| 98 | {
|
---|
| 99 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
---|
| 100 |
|
---|
| 101 | ht_link_t *link = hash_table_find(&tree->devman_devices, &handle);
|
---|
| 102 | if (link == NULL)
|
---|
| 103 | return NULL;
|
---|
| 104 |
|
---|
| 105 | return hash_table_get_inst(link, dev_node_t, devman_dev);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /** Find the device node structure of the device witch has the specified handle.
|
---|
| 109 | *
|
---|
| 110 | * @param tree The device tree where we look for the device node.
|
---|
| 111 | * @param handle The handle of the device.
|
---|
| 112 | * @return The device node.
|
---|
| 113 | */
|
---|
| 114 | dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
|
---|
| 115 | {
|
---|
| 116 | dev_node_t *dev = NULL;
|
---|
| 117 |
|
---|
| 118 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
| 119 | dev = find_dev_node_no_lock(tree, handle);
|
---|
| 120 | if (dev != NULL)
|
---|
| 121 | dev_add_ref(dev);
|
---|
| 122 |
|
---|
| 123 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
| 124 |
|
---|
| 125 | return dev;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /** Get list of device functions. */
|
---|
| 129 | int dev_get_functions(dev_tree_t *tree, dev_node_t *dev,
|
---|
| 130 | devman_handle_t *hdl_buf, size_t buf_size, size_t *act_size)
|
---|
| 131 | {
|
---|
| 132 | size_t act_cnt;
|
---|
| 133 | size_t buf_cnt;
|
---|
| 134 |
|
---|
| 135 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
---|
| 136 |
|
---|
| 137 | buf_cnt = buf_size / sizeof(devman_handle_t);
|
---|
| 138 |
|
---|
| 139 | act_cnt = list_count(&dev->functions);
|
---|
| 140 | *act_size = act_cnt * sizeof(devman_handle_t);
|
---|
| 141 |
|
---|
| 142 | if (buf_size % sizeof(devman_handle_t) != 0)
|
---|
| 143 | return EINVAL;
|
---|
| 144 |
|
---|
| 145 | size_t pos = 0;
|
---|
[08bc23d] | 146 | list_foreach(dev->functions, dev_functions, fun_node_t, fun) {
|
---|
[d1bafbf] | 147 | if (pos < buf_cnt) {
|
---|
| 148 | hdl_buf[pos] = fun->handle;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | pos++;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | return EOK;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /** @}
|
---|
| 158 | */
|
---|