[13125d3] | 1 | /*
|
---|
| 2 | * Copyright (c) 2007 Josef Cejka
|
---|
| 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 | /**
|
---|
| 30 | * @defgroup devmap Device mapper.
|
---|
[6519d6f] | 31 | * @brief HelenOS device mapper.
|
---|
[13125d3] | 32 | * @{
|
---|
[6519d6f] | 33 | */
|
---|
[13125d3] | 34 |
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <ipc/services.h>
|
---|
| 39 | #include <ipc/ns.h>
|
---|
| 40 | #include <async.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <errno.h>
|
---|
[07e4a3c] | 43 | #include <bool.h>
|
---|
| 44 | #include <futex.h>
|
---|
[798f364] | 45 | #include <stdlib.h>
|
---|
| 46 | #include <string.h>
|
---|
[21c5d41] | 47 | #include <ipc/devmap.h>
|
---|
[13125d3] | 48 |
|
---|
[6519d6f] | 49 | #define NAME "devmap"
|
---|
[13125d3] | 50 |
|
---|
[6519d6f] | 51 | /** Pending lookup structure. */
|
---|
| 52 | typedef struct {
|
---|
| 53 | link_t link;
|
---|
| 54 | char *name; /**< Device name */
|
---|
| 55 | ipc_callid_t callid; /**< Call ID waiting for the lookup */
|
---|
| 56 | } pending_req_t;
|
---|
[07e4a3c] | 57 |
|
---|
[798f364] | 58 | LIST_INITIALIZE(devices_list);
|
---|
| 59 | LIST_INITIALIZE(drivers_list);
|
---|
[6519d6f] | 60 | LIST_INITIALIZE(pending_req);
|
---|
[798f364] | 61 |
|
---|
[6519d6f] | 62 | /* Locking order:
|
---|
| 63 | * drivers_list_futex
|
---|
| 64 | * devices_list_futex
|
---|
| 65 | * (devmap_driver_t *)->devices_futex
|
---|
| 66 | * create_handle_futex
|
---|
[798f364] | 67 | **/
|
---|
| 68 |
|
---|
| 69 | static atomic_t devices_list_futex = FUTEX_INITIALIZER;
|
---|
| 70 | static atomic_t drivers_list_futex = FUTEX_INITIALIZER;
|
---|
| 71 | static atomic_t create_handle_futex = FUTEX_INITIALIZER;
|
---|
| 72 |
|
---|
| 73 | static int devmap_create_handle(void)
|
---|
| 74 | {
|
---|
| 75 | static int last_handle = 0;
|
---|
| 76 | int handle;
|
---|
[6519d6f] | 77 |
|
---|
[798f364] | 78 | /* TODO: allow reusing old handles after their unregistration
|
---|
[6519d6f] | 79 | * and implement some version of LRU algorithm
|
---|
| 80 | */
|
---|
| 81 |
|
---|
[798f364] | 82 | /* FIXME: overflow */
|
---|
[6519d6f] | 83 | futex_down(&create_handle_futex);
|
---|
| 84 |
|
---|
[798f364] | 85 | last_handle += 1;
|
---|
| 86 | handle = last_handle;
|
---|
[6519d6f] | 87 |
|
---|
| 88 | futex_up(&create_handle_futex);
|
---|
| 89 |
|
---|
[798f364] | 90 | return handle;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[07e4a3c] | 93 |
|
---|
[6519d6f] | 94 | /** Initialize device mapper.
|
---|
[13125d3] | 95 | *
|
---|
| 96 | *
|
---|
| 97 | */
|
---|
| 98 | static int devmap_init()
|
---|
| 99 | {
|
---|
[798f364] | 100 | /* TODO: */
|
---|
[6519d6f] | 101 |
|
---|
[07e4a3c] | 102 | return EOK;
|
---|
[13125d3] | 103 | }
|
---|
| 104 |
|
---|
[798f364] | 105 | /** Find device with given name.
|
---|
| 106 | *
|
---|
| 107 | */
|
---|
| 108 | static devmap_device_t *devmap_device_find_name(const char *name)
|
---|
[07e4a3c] | 109 | {
|
---|
[6519d6f] | 110 | link_t *item = devices_list.next;
|
---|
[798f364] | 111 | devmap_device_t *device = NULL;
|
---|
[6519d6f] | 112 |
|
---|
[798f364] | 113 | while (item != &devices_list) {
|
---|
| 114 | device = list_get_instance(item, devmap_device_t, devices);
|
---|
[8be693b] | 115 | if (0 == str_cmp(device->name, name))
|
---|
[798f364] | 116 | break;
|
---|
| 117 | item = item->next;
|
---|
| 118 | }
|
---|
[6519d6f] | 119 |
|
---|
[21c5d41] | 120 | if (item == &devices_list)
|
---|
[798f364] | 121 | return NULL;
|
---|
[6519d6f] | 122 |
|
---|
[798f364] | 123 | device = list_get_instance(item, devmap_device_t, devices);
|
---|
| 124 | return device;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /** Find device with given handle.
|
---|
[6519d6f] | 128 | *
|
---|
[798f364] | 129 | * @todo: use hash table
|
---|
[6519d6f] | 130 | *
|
---|
[798f364] | 131 | */
|
---|
| 132 | static devmap_device_t *devmap_device_find_handle(int handle)
|
---|
| 133 | {
|
---|
[6519d6f] | 134 | futex_down(&devices_list_futex);
|
---|
| 135 |
|
---|
| 136 | link_t *item = (&devices_list)->next;
|
---|
[798f364] | 137 | devmap_device_t *device = NULL;
|
---|
| 138 |
|
---|
| 139 | while (item != &devices_list) {
|
---|
| 140 | device = list_get_instance(item, devmap_device_t, devices);
|
---|
[6519d6f] | 141 | if (device->handle == handle)
|
---|
[798f364] | 142 | break;
|
---|
| 143 | item = item->next;
|
---|
| 144 | }
|
---|
[6519d6f] | 145 |
|
---|
[798f364] | 146 | if (item == &devices_list) {
|
---|
| 147 | futex_up(&devices_list_futex);
|
---|
| 148 | return NULL;
|
---|
| 149 | }
|
---|
[6519d6f] | 150 |
|
---|
[798f364] | 151 | device = list_get_instance(item, devmap_device_t, devices);
|
---|
| 152 |
|
---|
| 153 | futex_up(&devices_list_futex);
|
---|
[6519d6f] | 154 |
|
---|
[798f364] | 155 | return device;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[7f3e3e7] | 158 | /**
|
---|
[6519d6f] | 159 | *
|
---|
[7f3e3e7] | 160 | * Unregister device and free it. It's assumed that driver's device list is
|
---|
| 161 | * already locked.
|
---|
[6519d6f] | 162 | *
|
---|
[798f364] | 163 | */
|
---|
| 164 | static int devmap_device_unregister_core(devmap_device_t *device)
|
---|
| 165 | {
|
---|
| 166 | list_remove(&(device->devices));
|
---|
| 167 | list_remove(&(device->driver_devices));
|
---|
[6519d6f] | 168 |
|
---|
| 169 | free(device->name);
|
---|
[798f364] | 170 | free(device);
|
---|
[6519d6f] | 171 |
|
---|
[798f364] | 172 | return EOK;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[7f3e3e7] | 175 | /**
|
---|
[6519d6f] | 176 | *
|
---|
[7f3e3e7] | 177 | * Read info about new driver and add it into linked list of registered
|
---|
| 178 | * drivers.
|
---|
[6519d6f] | 179 | *
|
---|
[798f364] | 180 | */
|
---|
| 181 | static void devmap_driver_register(devmap_driver_t **odriver)
|
---|
| 182 | {
|
---|
| 183 | *odriver = NULL;
|
---|
[07e4a3c] | 184 |
|
---|
[6519d6f] | 185 | ipc_call_t icall;
|
---|
| 186 | ipc_callid_t iid = async_get_call(&icall);
|
---|
| 187 |
|
---|
[798f364] | 188 | if (IPC_GET_METHOD(icall) != DEVMAP_DRIVER_REGISTER) {
|
---|
[b74959bd] | 189 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 190 | return;
|
---|
[6519d6f] | 191 | }
|
---|
| 192 |
|
---|
| 193 | devmap_driver_t *driver = (devmap_driver_t *) malloc(sizeof(devmap_driver_t));
|
---|
| 194 |
|
---|
| 195 | if (driver == NULL) {
|
---|
[b74959bd] | 196 | ipc_answer_0(iid, ENOMEM);
|
---|
[798f364] | 197 | return;
|
---|
[07e4a3c] | 198 | }
|
---|
[6519d6f] | 199 |
|
---|
| 200 | /*
|
---|
[798f364] | 201 | * Get driver name
|
---|
| 202 | */
|
---|
[6519d6f] | 203 | ipc_callid_t callid;
|
---|
| 204 | size_t name_size;
|
---|
[3115355] | 205 | if (!ipc_data_write_receive(&callid, &name_size)) {
|
---|
[798f364] | 206 | free(driver);
|
---|
[b74959bd] | 207 | ipc_answer_0(callid, EREFUSED);
|
---|
| 208 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 209 | return;
|
---|
[07e4a3c] | 210 | }
|
---|
[6519d6f] | 211 |
|
---|
[798f364] | 212 | if (name_size > DEVMAP_NAME_MAXLEN) {
|
---|
| 213 | free(driver);
|
---|
[b74959bd] | 214 | ipc_answer_0(callid, EINVAL);
|
---|
| 215 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 216 | return;
|
---|
| 217 | }
|
---|
[6519d6f] | 218 |
|
---|
[798f364] | 219 | /*
|
---|
| 220 | * Allocate buffer for device name.
|
---|
| 221 | */
|
---|
[6519d6f] | 222 | driver->name = (char *) malloc(name_size + 1);
|
---|
| 223 | if (driver->name == NULL) {
|
---|
[798f364] | 224 | free(driver);
|
---|
[b74959bd] | 225 | ipc_answer_0(callid, ENOMEM);
|
---|
| 226 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 227 | return;
|
---|
[6519d6f] | 228 | }
|
---|
| 229 |
|
---|
[798f364] | 230 | /*
|
---|
| 231 | * Send confirmation to sender and get data into buffer.
|
---|
| 232 | */
|
---|
[215e375] | 233 | if (EOK != ipc_data_write_finalize(callid, driver->name, name_size)) {
|
---|
[798f364] | 234 | free(driver->name);
|
---|
| 235 | free(driver);
|
---|
[b74959bd] | 236 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 237 | return;
|
---|
| 238 | }
|
---|
[6519d6f] | 239 |
|
---|
[798f364] | 240 | driver->name[name_size] = 0;
|
---|
[6519d6f] | 241 |
|
---|
[798f364] | 242 | /* Initialize futex for list of devices owned by this driver */
|
---|
[7f3e3e7] | 243 | futex_initialize(&(driver->devices_futex), 1);
|
---|
[6519d6f] | 244 |
|
---|
| 245 | /*
|
---|
[798f364] | 246 | * Initialize list of asociated devices
|
---|
[b74959bd] | 247 | */
|
---|
[798f364] | 248 | list_initialize(&(driver->devices));
|
---|
[6519d6f] | 249 |
|
---|
| 250 | /*
|
---|
[798f364] | 251 | * Create connection to the driver
|
---|
| 252 | */
|
---|
[6519d6f] | 253 | ipc_call_t call;
|
---|
[798f364] | 254 | callid = async_get_call(&call);
|
---|
[6519d6f] | 255 |
|
---|
[798f364] | 256 | if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) {
|
---|
[b74959bd] | 257 | ipc_answer_0(callid, ENOTSUP);
|
---|
[798f364] | 258 |
|
---|
| 259 | free(driver->name);
|
---|
| 260 | free(driver);
|
---|
[b74959bd] | 261 | ipc_answer_0(iid, ENOTSUP);
|
---|
[798f364] | 262 | return;
|
---|
| 263 | }
|
---|
[6519d6f] | 264 |
|
---|
[38c706cc] | 265 | driver->phone = IPC_GET_ARG5(call);
|
---|
[798f364] | 266 |
|
---|
[b74959bd] | 267 | ipc_answer_0(callid, EOK);
|
---|
[798f364] | 268 |
|
---|
| 269 | list_initialize(&(driver->drivers));
|
---|
[6519d6f] | 270 |
|
---|
| 271 | futex_down(&drivers_list_futex);
|
---|
[798f364] | 272 |
|
---|
[7f3e3e7] | 273 | /* TODO:
|
---|
| 274 | * check that no driver with name equal to driver->name is registered
|
---|
| 275 | */
|
---|
[6519d6f] | 276 |
|
---|
| 277 | /*
|
---|
[798f364] | 278 | * Insert new driver into list of registered drivers
|
---|
| 279 | */
|
---|
| 280 | list_append(&(driver->drivers), &drivers_list);
|
---|
[6519d6f] | 281 | futex_up(&drivers_list_futex);
|
---|
[798f364] | 282 |
|
---|
[b74959bd] | 283 | ipc_answer_0(iid, EOK);
|
---|
[6519d6f] | 284 |
|
---|
[798f364] | 285 | *odriver = driver;
|
---|
[07e4a3c] | 286 | }
|
---|
| 287 |
|
---|
[6519d6f] | 288 | /**
|
---|
| 289 | * Unregister device driver, unregister all its devices and free driver
|
---|
[b74959bd] | 290 | * structure.
|
---|
[6519d6f] | 291 | *
|
---|
[798f364] | 292 | */
|
---|
| 293 | static int devmap_driver_unregister(devmap_driver_t *driver)
|
---|
[07e4a3c] | 294 | {
|
---|
[6519d6f] | 295 | if (driver == NULL)
|
---|
[798f364] | 296 | return EEXISTS;
|
---|
[21c5d41] | 297 |
|
---|
[6519d6f] | 298 | futex_down(&drivers_list_futex);
|
---|
| 299 |
|
---|
[798f364] | 300 | ipc_hangup(driver->phone);
|
---|
| 301 |
|
---|
| 302 | /* remove it from list of drivers */
|
---|
| 303 | list_remove(&(driver->drivers));
|
---|
[6519d6f] | 304 |
|
---|
[798f364] | 305 | /* unregister all its devices */
|
---|
| 306 |
|
---|
[6519d6f] | 307 | futex_down(&devices_list_futex);
|
---|
[798f364] | 308 | futex_down(&(driver->devices_futex));
|
---|
[6519d6f] | 309 |
|
---|
[798f364] | 310 | while (!list_empty(&(driver->devices))) {
|
---|
[6519d6f] | 311 | devmap_device_t *device = list_get_instance(driver->devices.next,
|
---|
[7f3e3e7] | 312 | devmap_device_t, driver_devices);
|
---|
[798f364] | 313 | devmap_device_unregister_core(device);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | futex_up(&(driver->devices_futex));
|
---|
[6519d6f] | 317 | futex_up(&devices_list_futex);
|
---|
| 318 | futex_up(&drivers_list_futex);
|
---|
| 319 |
|
---|
[798f364] | 320 | /* free name and driver */
|
---|
[6519d6f] | 321 | if (NULL != driver->name)
|
---|
[798f364] | 322 | free(driver->name);
|
---|
[6519d6f] | 323 |
|
---|
[798f364] | 324 | free(driver);
|
---|
[6519d6f] | 325 |
|
---|
[07e4a3c] | 326 | return EOK;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[798f364] | 329 |
|
---|
[6519d6f] | 330 | /** Process pending lookup requests */
|
---|
| 331 | static void process_pending_lookup()
|
---|
| 332 | {
|
---|
| 333 | link_t *cur;
|
---|
| 334 |
|
---|
| 335 | loop:
|
---|
| 336 | for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
|
---|
| 337 | pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
|
---|
| 338 |
|
---|
| 339 | const devmap_device_t *dev = devmap_device_find_name(pr->name);
|
---|
| 340 | if (!dev)
|
---|
| 341 | continue;
|
---|
| 342 |
|
---|
| 343 | ipc_answer_1(pr->callid, EOK, dev->handle);
|
---|
| 344 |
|
---|
| 345 | free(pr->name);
|
---|
| 346 | list_remove(cur);
|
---|
| 347 | free(pr);
|
---|
| 348 | goto loop;
|
---|
| 349 | }
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 |
|
---|
[798f364] | 353 | /** Register instance of device
|
---|
| 354 | *
|
---|
| 355 | */
|
---|
| 356 | static void devmap_device_register(ipc_callid_t iid, ipc_call_t *icall,
|
---|
[b74959bd] | 357 | devmap_driver_t *driver)
|
---|
[07e4a3c] | 358 | {
|
---|
[6519d6f] | 359 | if (driver == NULL) {
|
---|
[b74959bd] | 360 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 361 | return;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /* Create new device entry */
|
---|
[6519d6f] | 365 | devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
|
---|
| 366 | if (device == NULL) {
|
---|
[b74959bd] | 367 | ipc_answer_0(iid, ENOMEM);
|
---|
[798f364] | 368 | return;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | /* Get device name */
|
---|
[6519d6f] | 372 | ipc_callid_t callid;
|
---|
| 373 | size_t size;
|
---|
[3115355] | 374 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
[798f364] | 375 | free(device);
|
---|
[b74959bd] | 376 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 377 | return;
|
---|
| 378 | }
|
---|
[6519d6f] | 379 |
|
---|
[798f364] | 380 | if (size > DEVMAP_NAME_MAXLEN) {
|
---|
| 381 | free(device);
|
---|
[b74959bd] | 382 | ipc_answer_0(callid, EINVAL);
|
---|
| 383 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 384 | return;
|
---|
| 385 | }
|
---|
[21c5d41] | 386 |
|
---|
| 387 | /* +1 for terminating \0 */
|
---|
| 388 | device->name = (char *) malloc(size + 1);
|
---|
[6519d6f] | 389 |
|
---|
| 390 | if (device->name == NULL) {
|
---|
[798f364] | 391 | free(device);
|
---|
[b74959bd] | 392 | ipc_answer_0(callid, ENOMEM);
|
---|
| 393 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 394 | return;
|
---|
[07e4a3c] | 395 | }
|
---|
[798f364] | 396 |
|
---|
[215e375] | 397 | ipc_data_write_finalize(callid, device->name, size);
|
---|
[798f364] | 398 | device->name[size] = 0;
|
---|
[6519d6f] | 399 |
|
---|
[798f364] | 400 | list_initialize(&(device->devices));
|
---|
| 401 | list_initialize(&(device->driver_devices));
|
---|
[6519d6f] | 402 |
|
---|
| 403 | futex_down(&devices_list_futex);
|
---|
| 404 |
|
---|
[798f364] | 405 | /* Check that device with such name is not already registered */
|
---|
| 406 | if (NULL != devmap_device_find_name(device->name)) {
|
---|
[21c5d41] | 407 | printf(NAME ": Device '%s' already registered\n", device->name);
|
---|
[798f364] | 408 | futex_up(&devices_list_futex);
|
---|
| 409 | free(device->name);
|
---|
| 410 | free(device);
|
---|
[b74959bd] | 411 | ipc_answer_0(iid, EEXISTS);
|
---|
[798f364] | 412 | return;
|
---|
[07e4a3c] | 413 | }
|
---|
[6519d6f] | 414 |
|
---|
[798f364] | 415 | /* Get unique device handle */
|
---|
[6519d6f] | 416 | device->handle = devmap_create_handle();
|
---|
| 417 |
|
---|
[798f364] | 418 | device->driver = driver;
|
---|
[07e4a3c] | 419 |
|
---|
[798f364] | 420 | /* Insert device into list of all devices */
|
---|
[b74959bd] | 421 | list_append(&device->devices, &devices_list);
|
---|
[6519d6f] | 422 |
|
---|
[798f364] | 423 | /* Insert device into list of devices that belog to one driver */
|
---|
[b74959bd] | 424 | futex_down(&device->driver->devices_futex);
|
---|
[798f364] | 425 |
|
---|
[b74959bd] | 426 | list_append(&device->driver_devices, &device->driver->devices);
|
---|
[798f364] | 427 |
|
---|
[6519d6f] | 428 | futex_up(&device->driver->devices_futex);
|
---|
| 429 | futex_up(&devices_list_futex);
|
---|
| 430 |
|
---|
[b74959bd] | 431 | ipc_answer_1(iid, EOK, device->handle);
|
---|
[6519d6f] | 432 |
|
---|
| 433 | process_pending_lookup();
|
---|
[07e4a3c] | 434 | }
|
---|
| 435 |
|
---|
[798f364] | 436 | /**
|
---|
| 437 | *
|
---|
| 438 | */
|
---|
| 439 | static int devmap_device_unregister(ipc_callid_t iid, ipc_call_t *icall,
|
---|
[b74959bd] | 440 | devmap_driver_t *driver)
|
---|
[07e4a3c] | 441 | {
|
---|
[798f364] | 442 | /* TODO */
|
---|
[07e4a3c] | 443 | return EOK;
|
---|
| 444 | }
|
---|
[13125d3] | 445 |
|
---|
[798f364] | 446 | /** Connect client to the device.
|
---|
[6519d6f] | 447 | *
|
---|
[798f364] | 448 | * Find device driver owning requested device and forward
|
---|
| 449 | * the message to it.
|
---|
[6519d6f] | 450 | *
|
---|
[798f364] | 451 | */
|
---|
| 452 | static void devmap_forward(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 453 | {
|
---|
| 454 | /*
|
---|
| 455 | * Get handle from request
|
---|
| 456 | */
|
---|
[6519d6f] | 457 | int handle = IPC_GET_ARG2(*call);
|
---|
| 458 | devmap_device_t *dev = devmap_device_find_handle(handle);
|
---|
| 459 |
|
---|
[798f364] | 460 | if (NULL == dev) {
|
---|
[b74959bd] | 461 | ipc_answer_0(callid, ENOENT);
|
---|
[798f364] | 462 | return;
|
---|
[6519d6f] | 463 | }
|
---|
| 464 |
|
---|
[7f3e3e7] | 465 | ipc_forward_fast(callid, dev->driver->phone, (ipcarg_t)(dev->handle),
|
---|
[b61d47d] | 466 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
|
---|
[798f364] | 467 | }
|
---|
| 468 |
|
---|
| 469 | /** Find handle for device instance identified by name.
|
---|
[6519d6f] | 470 | *
|
---|
[798f364] | 471 | * In answer will be send EOK and device handle in arg1 or a error
|
---|
[6519d6f] | 472 | * code from errno.h.
|
---|
| 473 | *
|
---|
[798f364] | 474 | */
|
---|
| 475 | static void devmap_get_handle(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 476 | {
|
---|
[6519d6f] | 477 | /*
|
---|
[798f364] | 478 | * Wait for incoming message with device name (but do not
|
---|
| 479 | * read the name itself until the buffer is allocated).
|
---|
| 480 | */
|
---|
[6519d6f] | 481 | ipc_callid_t callid;
|
---|
| 482 | size_t size;
|
---|
| 483 | if (!ipc_data_write_receive(&callid, &size)) {
|
---|
[b74959bd] | 484 | ipc_answer_0(callid, EREFUSED);
|
---|
| 485 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 486 | return;
|
---|
| 487 | }
|
---|
[6519d6f] | 488 |
|
---|
| 489 | if ((size < 1) || (size > DEVMAP_NAME_MAXLEN)) {
|
---|
[b74959bd] | 490 | ipc_answer_0(callid, EINVAL);
|
---|
| 491 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 492 | return;
|
---|
| 493 | }
|
---|
[6519d6f] | 494 |
|
---|
[798f364] | 495 | /*
|
---|
| 496 | * Allocate buffer for device name.
|
---|
| 497 | */
|
---|
[6519d6f] | 498 | char *name = (char *) malloc(size);
|
---|
| 499 | if (name == NULL) {
|
---|
[b74959bd] | 500 | ipc_answer_0(callid, ENOMEM);
|
---|
| 501 | ipc_answer_0(iid, EREFUSED);
|
---|
[798f364] | 502 | return;
|
---|
[6519d6f] | 503 | }
|
---|
| 504 |
|
---|
[798f364] | 505 | /*
|
---|
| 506 | * Send confirmation to sender and get data into buffer.
|
---|
| 507 | */
|
---|
[6519d6f] | 508 | ipcarg_t retval = ipc_data_write_finalize(callid, name, size);
|
---|
| 509 | if (retval != EOK) {
|
---|
[b74959bd] | 510 | ipc_answer_0(iid, EREFUSED);
|
---|
[6519d6f] | 511 | free(name);
|
---|
[798f364] | 512 | return;
|
---|
| 513 | }
|
---|
[6519d6f] | 514 | name[size] = '\0';
|
---|
| 515 |
|
---|
[798f364] | 516 | /*
|
---|
| 517 | * Find device name in linked list of known devices.
|
---|
| 518 | */
|
---|
[6519d6f] | 519 | const devmap_device_t *dev = devmap_device_find_name(name);
|
---|
| 520 |
|
---|
[798f364] | 521 | /*
|
---|
| 522 | * Device was not found.
|
---|
| 523 | */
|
---|
[6519d6f] | 524 | if (dev == NULL) {
|
---|
| 525 | if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
|
---|
| 526 | /* Blocking lookup, add to pending list */
|
---|
| 527 | pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
|
---|
| 528 | if (!pr) {
|
---|
| 529 | ipc_answer_0(iid, ENOMEM);
|
---|
| 530 | free(name);
|
---|
| 531 | return;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | pr->name = name;
|
---|
| 535 | pr->callid = iid;
|
---|
| 536 | list_append(&pr->link, &pending_req);
|
---|
| 537 | return;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
[b74959bd] | 540 | ipc_answer_0(iid, ENOENT);
|
---|
[6519d6f] | 541 | free(name);
|
---|
[798f364] | 542 | return;
|
---|
| 543 | }
|
---|
[6519d6f] | 544 |
|
---|
[b74959bd] | 545 | ipc_answer_1(iid, EOK, dev->handle);
|
---|
[6519d6f] | 546 | free(name);
|
---|
[798f364] | 547 | }
|
---|
| 548 |
|
---|
[6519d6f] | 549 | /** Find name of device identified by id and send it to caller.
|
---|
[798f364] | 550 | *
|
---|
| 551 | */
|
---|
| 552 | static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 553 | {
|
---|
[6519d6f] | 554 | const devmap_device_t *device = devmap_device_find_handle(IPC_GET_ARG1(*icall));
|
---|
| 555 |
|
---|
[798f364] | 556 | /*
|
---|
| 557 | * Device not found.
|
---|
| 558 | */
|
---|
[6519d6f] | 559 | if (device == NULL) {
|
---|
[b74959bd] | 560 | ipc_answer_0(iid, ENOENT);
|
---|
[798f364] | 561 | return;
|
---|
| 562 | }
|
---|
[6519d6f] | 563 |
|
---|
| 564 | ipc_answer_0(iid, EOK);
|
---|
| 565 |
|
---|
| 566 | size_t name_size = strlen(device->name);
|
---|
| 567 |
|
---|
| 568 | /* FIXME:
|
---|
| 569 | * We have no channel from DEVMAP to client, therefore
|
---|
| 570 | * sending must be initiated by client.
|
---|
| 571 | *
|
---|
| 572 | * int rc = ipc_data_write_send(phone, device->name, name_size);
|
---|
| 573 | * if (rc != EOK) {
|
---|
| 574 | * async_wait_for(req, NULL);
|
---|
| 575 | * return rc;
|
---|
| 576 | * }
|
---|
| 577 | */
|
---|
| 578 |
|
---|
[798f364] | 579 | /* TODO: send name in response */
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | /** Handle connection with device driver.
|
---|
[13125d3] | 583 | *
|
---|
| 584 | */
|
---|
[21c5d41] | 585 | static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[13125d3] | 586 | {
|
---|
[6519d6f] | 587 | /* Accept connection */
|
---|
| 588 | ipc_answer_0(iid, EOK);
|
---|
| 589 |
|
---|
[798f364] | 590 | devmap_driver_t *driver = NULL;
|
---|
| 591 | devmap_driver_register(&driver);
|
---|
[6519d6f] | 592 |
|
---|
[21c5d41] | 593 | if (NULL == driver)
|
---|
[798f364] | 594 | return;
|
---|
| 595 |
|
---|
[6519d6f] | 596 | bool cont = true;
|
---|
[07e4a3c] | 597 | while (cont) {
|
---|
[6519d6f] | 598 | ipc_call_t call;
|
---|
| 599 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 600 |
|
---|
| 601 | switch (IPC_GET_METHOD(call)) {
|
---|
[13125d3] | 602 | case IPC_M_PHONE_HUNGUP:
|
---|
[07e4a3c] | 603 | cont = false;
|
---|
[6519d6f] | 604 | /* Exit thread */
|
---|
| 605 | continue;
|
---|
[798f364] | 606 | case DEVMAP_DRIVER_UNREGISTER:
|
---|
[6519d6f] | 607 | if (NULL == driver)
|
---|
[b74959bd] | 608 | ipc_answer_0(callid, ENOENT);
|
---|
[6519d6f] | 609 | else
|
---|
[b74959bd] | 610 | ipc_answer_0(callid, EOK);
|
---|
[07e4a3c] | 611 | break;
|
---|
[798f364] | 612 | case DEVMAP_DEVICE_REGISTER:
|
---|
| 613 | /* Register one instance of device */
|
---|
| 614 | devmap_device_register(callid, &call, driver);
|
---|
[07e4a3c] | 615 | break;
|
---|
[798f364] | 616 | case DEVMAP_DEVICE_UNREGISTER:
|
---|
| 617 | /* Remove instance of device identified by handler */
|
---|
| 618 | devmap_device_unregister(callid, &call, driver);
|
---|
| 619 | break;
|
---|
| 620 | case DEVMAP_DEVICE_GET_HANDLE:
|
---|
| 621 | devmap_get_handle(callid, &call);
|
---|
| 622 | break;
|
---|
| 623 | case DEVMAP_DEVICE_GET_NAME:
|
---|
| 624 | devmap_get_handle(callid, &call);
|
---|
| 625 | break;
|
---|
| 626 | default:
|
---|
[6519d6f] | 627 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
[b74959bd] | 628 | ipc_answer_0(callid, ENOENT);
|
---|
[798f364] | 629 | }
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | if (NULL != driver) {
|
---|
[6519d6f] | 633 | /*
|
---|
[b74959bd] | 634 | * Unregister the device driver and all its devices.
|
---|
| 635 | */
|
---|
[798f364] | 636 | devmap_driver_unregister(driver);
|
---|
| 637 | driver = NULL;
|
---|
| 638 | }
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | /** Handle connection with device client.
|
---|
| 642 | *
|
---|
| 643 | */
|
---|
[21c5d41] | 644 | static void devmap_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[798f364] | 645 | {
|
---|
[6519d6f] | 646 | /* Accept connection */
|
---|
| 647 | ipc_answer_0(iid, EOK);
|
---|
| 648 |
|
---|
[798f364] | 649 | bool cont = true;
|
---|
| 650 | while (cont) {
|
---|
[6519d6f] | 651 | ipc_call_t call;
|
---|
| 652 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 653 |
|
---|
| 654 | switch (IPC_GET_METHOD(call)) {
|
---|
[798f364] | 655 | case IPC_M_PHONE_HUNGUP:
|
---|
[07e4a3c] | 656 | cont = false;
|
---|
[6519d6f] | 657 | /* Exit thread */
|
---|
| 658 | continue;
|
---|
[798f364] | 659 | case DEVMAP_DEVICE_GET_HANDLE:
|
---|
[6519d6f] | 660 | devmap_get_handle(callid, &call);
|
---|
[07e4a3c] | 661 | break;
|
---|
[798f364] | 662 | case DEVMAP_DEVICE_GET_NAME:
|
---|
| 663 | /* TODO */
|
---|
| 664 | devmap_get_name(callid, &call);
|
---|
[13125d3] | 665 | break;
|
---|
| 666 | default:
|
---|
[6519d6f] | 667 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
[b74959bd] | 668 | ipc_answer_0(callid, ENOENT);
|
---|
[13125d3] | 669 | }
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 |
|
---|
[6519d6f] | 673 | /** Function for handling connections to devmap
|
---|
[798f364] | 674 | *
|
---|
| 675 | */
|
---|
[21c5d41] | 676 | static void devmap_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[798f364] | 677 | {
|
---|
[21c5d41] | 678 | /* Select interface */
|
---|
| 679 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
[7f3e3e7] | 680 | case DEVMAP_DRIVER:
|
---|
| 681 | devmap_connection_driver(iid, icall);
|
---|
| 682 | break;
|
---|
| 683 | case DEVMAP_CLIENT:
|
---|
| 684 | devmap_connection_client(iid, icall);
|
---|
| 685 | break;
|
---|
[b61d47d] | 686 | case DEVMAP_CONNECT_TO_DEVICE:
|
---|
[21c5d41] | 687 | /* Connect client to selected device */
|
---|
[b61d47d] | 688 | devmap_forward(iid, icall);
|
---|
| 689 | break;
|
---|
[7f3e3e7] | 690 | default:
|
---|
[6519d6f] | 691 | /* No such interface */
|
---|
| 692 | ipc_answer_0(iid, ENOENT);
|
---|
[798f364] | 693 | }
|
---|
| 694 | }
|
---|
[13125d3] | 695 |
|
---|
[798f364] | 696 | /**
|
---|
| 697 | *
|
---|
| 698 | */
|
---|
[13125d3] | 699 | int main(int argc, char *argv[])
|
---|
| 700 | {
|
---|
[21c5d41] | 701 | printf(NAME ": HelenOS Device Mapper\n");
|
---|
| 702 |
|
---|
[13125d3] | 703 | if (devmap_init() != 0) {
|
---|
[21c5d41] | 704 | printf(NAME ": Error while initializing service\n");
|
---|
[13125d3] | 705 | return -1;
|
---|
| 706 | }
|
---|
[21c5d41] | 707 |
|
---|
| 708 | /* Set a handler of incomming connections */
|
---|
[798f364] | 709 | async_set_client_connection(devmap_connection);
|
---|
[6519d6f] | 710 |
|
---|
[7f3e3e7] | 711 | /* Register device mapper at naming service */
|
---|
[6519d6f] | 712 | ipcarg_t phonead;
|
---|
[38c706cc] | 713 | if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0)
|
---|
[13125d3] | 714 | return -1;
|
---|
| 715 |
|
---|
[21c5d41] | 716 | printf(NAME ": Accepting connections\n");
|
---|
[13125d3] | 717 | async_manager();
|
---|
[6519d6f] | 718 |
|
---|
[13125d3] | 719 | /* Never reached */
|
---|
| 720 | return 0;
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | /**
|
---|
| 724 | * @}
|
---|
| 725 | */
|
---|