| 1 | /*
|
|---|
| 2 | * Copyright (c) 2025 Jiri Svoboda
|
|---|
| 3 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup devman
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 |
|
|---|
| 36 | #include "dev.h"
|
|---|
| 37 | #include "devman.h"
|
|---|
| 38 |
|
|---|
| 39 | /** Create a new device node.
|
|---|
| 40 | *
|
|---|
| 41 | * @return A device node structure.
|
|---|
| 42 | */
|
|---|
| 43 | dev_node_t *create_dev_node(void)
|
|---|
| 44 | {
|
|---|
| 45 | dev_node_t *dev;
|
|---|
| 46 |
|
|---|
| 47 | dev = calloc(1, sizeof(dev_node_t));
|
|---|
| 48 | if (dev == NULL)
|
|---|
| 49 | return NULL;
|
|---|
| 50 |
|
|---|
| 51 | refcount_init(&dev->refcnt);
|
|---|
| 52 | list_initialize(&dev->functions);
|
|---|
| 53 | fibril_mutex_initialize(&dev->state_lock);
|
|---|
| 54 | fibril_condvar_initialize(&dev->state_cv);
|
|---|
| 55 | link_initialize(&dev->driver_devices);
|
|---|
| 56 |
|
|---|
| 57 | return dev;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /** Delete a device node.
|
|---|
| 61 | *
|
|---|
| 62 | * @param node The device node structure.
|
|---|
| 63 | */
|
|---|
| 64 | void delete_dev_node(dev_node_t *dev)
|
|---|
| 65 | {
|
|---|
| 66 | assert(list_empty(&dev->functions));
|
|---|
| 67 | assert(dev->pfun == NULL);
|
|---|
| 68 | assert(dev->drv == NULL);
|
|---|
| 69 |
|
|---|
| 70 | free(dev);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Increase device node reference count.
|
|---|
| 74 | *
|
|---|
| 75 | * @param dev Device node
|
|---|
| 76 | */
|
|---|
| 77 | void dev_add_ref(dev_node_t *dev)
|
|---|
| 78 | {
|
|---|
| 79 | refcount_up(&dev->refcnt);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /** Decrease device node reference count.
|
|---|
| 83 | *
|
|---|
| 84 | * When the count drops to zero the device node is freed.
|
|---|
| 85 | *
|
|---|
| 86 | * @param dev Device node
|
|---|
| 87 | */
|
|---|
| 88 | void dev_del_ref(dev_node_t *dev)
|
|---|
| 89 | {
|
|---|
| 90 | if (refcount_down(&dev->refcnt))
|
|---|
| 91 | delete_dev_node(dev);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /** Wait until the device node enters stable state.
|
|---|
| 95 | *
|
|---|
| 96 | * @param dev Device node
|
|---|
| 97 | */
|
|---|
| 98 | void dev_wait_stable(dev_node_t *dev)
|
|---|
| 99 | {
|
|---|
| 100 | fibril_mutex_lock(&dev->state_lock);
|
|---|
| 101 | while (dev->state == DEVICE_ATTACHING)
|
|---|
| 102 | fibril_condvar_wait(&dev->state_cv, &dev->state_lock);
|
|---|
| 103 | fibril_mutex_unlock(&dev->state_lock);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Find the device node structure of the device witch has the specified handle.
|
|---|
| 107 | *
|
|---|
| 108 | * @param tree The device tree where we look for the device node.
|
|---|
| 109 | * @param handle The handle of the device.
|
|---|
| 110 | * @return The device node.
|
|---|
| 111 | */
|
|---|
| 112 | dev_node_t *find_dev_node_no_lock(dev_tree_t *tree, devman_handle_t handle)
|
|---|
| 113 | {
|
|---|
| 114 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
|---|
| 115 |
|
|---|
| 116 | ht_link_t *link = hash_table_find(&tree->devman_devices, &handle);
|
|---|
| 117 | if (link == NULL)
|
|---|
| 118 | return NULL;
|
|---|
| 119 |
|
|---|
| 120 | return hash_table_get_inst(link, dev_node_t, devman_dev);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /** Find the device node structure of the device witch has the specified handle.
|
|---|
| 124 | *
|
|---|
| 125 | * @param tree The device tree where we look for the device node.
|
|---|
| 126 | * @param handle The handle of the device.
|
|---|
| 127 | * @return The device node.
|
|---|
| 128 | */
|
|---|
| 129 | dev_node_t *find_dev_node(dev_tree_t *tree, devman_handle_t handle)
|
|---|
| 130 | {
|
|---|
| 131 | dev_node_t *dev = NULL;
|
|---|
| 132 |
|
|---|
| 133 | fibril_rwlock_read_lock(&tree->rwlock);
|
|---|
| 134 | dev = find_dev_node_no_lock(tree, handle);
|
|---|
| 135 | if (dev != NULL)
|
|---|
| 136 | dev_add_ref(dev);
|
|---|
| 137 |
|
|---|
| 138 | fibril_rwlock_read_unlock(&tree->rwlock);
|
|---|
| 139 |
|
|---|
| 140 | return dev;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** Get list of device functions. */
|
|---|
| 144 | errno_t dev_get_functions(dev_tree_t *tree, dev_node_t *dev,
|
|---|
| 145 | devman_handle_t *hdl_buf, size_t buf_size, size_t *act_size)
|
|---|
| 146 | {
|
|---|
| 147 | size_t act_cnt;
|
|---|
| 148 | size_t buf_cnt;
|
|---|
| 149 |
|
|---|
| 150 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
|---|
| 151 |
|
|---|
| 152 | buf_cnt = buf_size / sizeof(devman_handle_t);
|
|---|
| 153 |
|
|---|
| 154 | act_cnt = list_count(&dev->functions);
|
|---|
| 155 | *act_size = act_cnt * sizeof(devman_handle_t);
|
|---|
| 156 |
|
|---|
| 157 | if (buf_size % sizeof(devman_handle_t) != 0)
|
|---|
| 158 | return EINVAL;
|
|---|
| 159 |
|
|---|
| 160 | size_t pos = 0;
|
|---|
| 161 | list_foreach(dev->functions, dev_functions, fun_node_t, fun) {
|
|---|
| 162 | if (pos < buf_cnt) {
|
|---|
| 163 | hdl_buf[pos] = fun->handle;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | pos++;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | return EOK;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** @}
|
|---|
| 173 | */
|
|---|