Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4327 | Rev 4581 | ||
|---|---|---|---|
| Line 39... | Line 39... | ||
| 39 | #include <ipc/ns.h> |
39 | #include <ipc/ns.h> |
| 40 | #include <async.h> |
40 | #include <async.h> |
| 41 | #include <stdio.h> |
41 | #include <stdio.h> |
| 42 | #include <errno.h> |
42 | #include <errno.h> |
| 43 | #include <bool.h> |
43 | #include <bool.h> |
| 44 | #include <futex.h> |
44 | #include <fibril_sync.h> |
| 45 | #include <stdlib.h> |
45 | #include <stdlib.h> |
| 46 | #include <string.h> |
46 | #include <string.h> |
| 47 | #include <ipc/devmap.h> |
47 | #include <ipc/devmap.h> |
| 48 | 48 | ||
| 49 | #define NAME "devmap" |
49 | #define NAME "devmap" |
| 50 | 50 | ||
| - | 51 | /** Representation of device driver. |
|
| - | 52 | * |
|
| - | 53 | * Each driver is responsible for a set of devices. |
|
| - | 54 | * |
|
| - | 55 | */ |
|
| - | 56 | typedef struct { |
|
| - | 57 | /** Pointers to previous and next drivers in linked list */ |
|
| - | 58 | link_t drivers; |
|
| - | 59 | /** Pointer to the linked list of devices controlled by this driver */ |
|
| - | 60 | link_t devices; |
|
| - | 61 | /** Phone asociated with this driver */ |
|
| - | 62 | ipcarg_t phone; |
|
| - | 63 | /** Device driver name */ |
|
| - | 64 | char *name; |
|
| - | 65 | /** Fibril mutex for list of devices owned by this driver */ |
|
| - | 66 | fibril_mutex_t devices_mutex; |
|
| - | 67 | } devmap_driver_t; |
|
| - | 68 | ||
| 51 | /** Pending lookup structure. */ |
69 | /** Info about registered device |
| - | 70 | * |
|
| - | 71 | */ |
|
| 52 | typedef struct { |
72 | typedef struct { |
| - | 73 | /** Pointer to the previous and next device in the list of all devices */ |
|
| 53 | link_t link; |
74 | link_t devices; |
| - | 75 | /** Pointer to the previous and next device in the list of devices |
|
| - | 76 | owned by one driver */ |
|
| - | 77 | link_t driver_devices; |
|
| - | 78 | /** Unique device identifier */ |
|
| - | 79 | dev_handle_t handle; |
|
| 54 | char *name; /**< Device name */ |
80 | /** Device name */ |
| - | 81 | char *name; |
|
| 55 | ipc_callid_t callid; /**< Call ID waiting for the lookup */ |
82 | /** Device driver handling this device */ |
| - | 83 | devmap_driver_t *driver; |
|
| 56 | } pending_req_t; |
84 | } devmap_device_t; |
| 57 | 85 | ||
| 58 | LIST_INITIALIZE(devices_list); |
86 | LIST_INITIALIZE(devices_list); |
| 59 | LIST_INITIALIZE(drivers_list); |
87 | LIST_INITIALIZE(drivers_list); |
| 60 | LIST_INITIALIZE(pending_req); |
- | |
| 61 | 88 | ||
| 62 | /* Locking order: |
89 | /* Locking order: |
| 63 | * drivers_list_futex |
90 | * drivers_list_mutex |
| 64 | * devices_list_futex |
91 | * devices_list_mutex |
| 65 | * (devmap_driver_t *)->devices_futex |
92 | * (devmap_driver_t *)->devices_mutex |
| 66 | * create_handle_futex |
93 | * create_handle_mutex |
| 67 | **/ |
94 | **/ |
| 68 | 95 | ||
| 69 | static atomic_t devices_list_futex = FUTEX_INITIALIZER; |
96 | static FIBRIL_MUTEX_INITIALIZE(devices_list_mutex); |
| - | 97 | static FIBRIL_CONDVAR_INITIALIZE(devices_list_cv); |
|
| 70 | static atomic_t drivers_list_futex = FUTEX_INITIALIZER; |
98 | static FIBRIL_MUTEX_INITIALIZE(drivers_list_mutex); |
| 71 | static atomic_t create_handle_futex = FUTEX_INITIALIZER; |
99 | static FIBRIL_MUTEX_INITIALIZE(create_handle_mutex); |
| 72 | 100 | ||
| - | 101 | static dev_handle_t last_handle = 0; |
|
| - | 102 | ||
| 73 | static int devmap_create_handle(void) |
103 | static dev_handle_t devmap_create_handle(void) |
| 74 | { |
104 | { |
| 75 | static int last_handle = 0; |
- | |
| 76 | int handle; |
- | |
| 77 | - | ||
| 78 | /* TODO: allow reusing old handles after their unregistration |
105 | /* TODO: allow reusing old handles after their unregistration |
| 79 | * and implement some version of LRU algorithm |
106 | * and implement some version of LRU algorithm, avoid overflow |
| 80 | */ |
107 | */ |
| 81 | 108 | ||
| 82 | /* FIXME: overflow */ |
- | |
| 83 | futex_down(&create_handle_futex); |
109 | fibril_mutex_lock(&create_handle_mutex); |
| 84 | - | ||
| 85 | last_handle += 1; |
110 | last_handle++; |
| 86 | handle = last_handle; |
- | |
| 87 | - | ||
| 88 | futex_up(&create_handle_futex); |
111 | fibril_mutex_unlock(&create_handle_mutex); |
| 89 | 112 | ||
| 90 | return handle; |
113 | return last_handle; |
| 91 | } |
- | |
| 92 | - | ||
| 93 | - | ||
| 94 | /** Initialize device mapper. |
- | |
| 95 | * |
- | |
| 96 | * |
- | |
| 97 | */ |
- | |
| 98 | static int devmap_init() |
- | |
| 99 | { |
- | |
| 100 | /* TODO: */ |
- | |
| 101 | - | ||
| 102 | return EOK; |
- | |
| 103 | } |
114 | } |
| 104 | 115 | ||
| 105 | /** Find device with given name. |
116 | /** Find device with given name. |
| 106 | * |
117 | * |
| 107 | */ |
118 | */ |
| Line 110... | Line 121... | ||
| 110 | link_t *item = devices_list.next; |
121 | link_t *item = devices_list.next; |
| 111 | devmap_device_t *device = NULL; |
122 | devmap_device_t *device = NULL; |
| 112 | 123 | ||
| 113 | while (item != &devices_list) { |
124 | while (item != &devices_list) { |
| 114 | device = list_get_instance(item, devmap_device_t, devices); |
125 | device = list_get_instance(item, devmap_device_t, devices); |
| 115 | if (0 == str_cmp(device->name, name)) |
126 | if (str_cmp(device->name, name) == 0) |
| 116 | break; |
127 | break; |
| 117 | item = item->next; |
128 | item = item->next; |
| 118 | } |
129 | } |
| 119 | 130 | ||
| 120 | if (item == &devices_list) |
131 | if (item == &devices_list) |
| Line 127... | Line 138... | ||
| 127 | /** Find device with given handle. |
138 | /** Find device with given handle. |
| 128 | * |
139 | * |
| 129 | * @todo: use hash table |
140 | * @todo: use hash table |
| 130 | * |
141 | * |
| 131 | */ |
142 | */ |
| 132 | static devmap_device_t *devmap_device_find_handle(int handle) |
143 | static devmap_device_t *devmap_device_find_handle(dev_handle_t handle) |
| 133 | { |
144 | { |
| 134 | futex_down(&devices_list_futex); |
145 | fibril_mutex_lock(&devices_list_mutex); |
| 135 | 146 | ||
| 136 | link_t *item = (&devices_list)->next; |
147 | link_t *item = (&devices_list)->next; |
| 137 | devmap_device_t *device = NULL; |
148 | devmap_device_t *device = NULL; |
| 138 | 149 | ||
| 139 | while (item != &devices_list) { |
150 | while (item != &devices_list) { |
| Line 142... | Line 153... | ||
| 142 | break; |
153 | break; |
| 143 | item = item->next; |
154 | item = item->next; |
| 144 | } |
155 | } |
| 145 | 156 | ||
| 146 | if (item == &devices_list) { |
157 | if (item == &devices_list) { |
| 147 | futex_up(&devices_list_futex); |
158 | fibril_mutex_unlock(&devices_list_mutex); |
| 148 | return NULL; |
159 | return NULL; |
| 149 | } |
160 | } |
| 150 | 161 | ||
| 151 | device = list_get_instance(item, devmap_device_t, devices); |
162 | device = list_get_instance(item, devmap_device_t, devices); |
| 152 | 163 | ||
| 153 | futex_up(&devices_list_futex); |
164 | fibril_mutex_unlock(&devices_list_mutex); |
| 154 | 165 | ||
| 155 | return device; |
166 | return device; |
| 156 | } |
167 | } |
| 157 | 168 | ||
| 158 | /** |
169 | /** |
| 159 | * |
- | |
| 160 | * Unregister device and free it. It's assumed that driver's device list is |
170 | * Unregister device and free it. It's assumed that driver's device list is |
| 161 | * already locked. |
171 | * already locked. |
| 162 | * |
- | |
| 163 | */ |
172 | */ |
| 164 | static int devmap_device_unregister_core(devmap_device_t *device) |
173 | static int devmap_device_unregister_core(devmap_device_t *device) |
| 165 | { |
174 | { |
| 166 | list_remove(&(device->devices)); |
175 | list_remove(&(device->devices)); |
| 167 | list_remove(&(device->driver_devices)); |
176 | list_remove(&(device->driver_devices)); |
| Line 171... | Line 180... | ||
| 171 | 180 | ||
| 172 | return EOK; |
181 | return EOK; |
| 173 | } |
182 | } |
| 174 | 183 | ||
| 175 | /** |
184 | /** |
| 176 | * |
- | |
| 177 | * Read info about new driver and add it into linked list of registered |
185 | * Read info about new driver and add it into linked list of registered |
| 178 | * drivers. |
186 | * drivers. |
| 179 | * |
- | |
| 180 | */ |
187 | */ |
| 181 | static void devmap_driver_register(devmap_driver_t **odriver) |
188 | static void devmap_driver_register(devmap_driver_t **odriver) |
| 182 | { |
189 | { |
| 183 | *odriver = NULL; |
190 | *odriver = NULL; |
| 184 | 191 | ||
| Line 228... | Line 235... | ||
| 228 | } |
235 | } |
| 229 | 236 | ||
| 230 | /* |
237 | /* |
| 231 | * Send confirmation to sender and get data into buffer. |
238 | * Send confirmation to sender and get data into buffer. |
| 232 | */ |
239 | */ |
| 233 | if (EOK != ipc_data_write_finalize(callid, driver->name, name_size)) { |
240 | if (ipc_data_write_finalize(callid, driver->name, name_size) != EOK) { |
| 234 | free(driver->name); |
241 | free(driver->name); |
| 235 | free(driver); |
242 | free(driver); |
| 236 | ipc_answer_0(iid, EREFUSED); |
243 | ipc_answer_0(iid, EREFUSED); |
| 237 | return; |
244 | return; |
| 238 | } |
245 | } |
| 239 | 246 | ||
| 240 | driver->name[name_size] = 0; |
247 | driver->name[name_size] = 0; |
| 241 | 248 | ||
| 242 | /* Initialize futex for list of devices owned by this driver */ |
249 | /* Initialize mutex for list of devices owned by this driver */ |
| 243 | futex_initialize(&(driver->devices_futex), 1); |
250 | fibril_mutex_initialize(&driver->devices_mutex); |
| 244 | 251 | ||
| 245 | /* |
252 | /* |
| 246 | * Initialize list of asociated devices |
253 | * Initialize list of asociated devices |
| 247 | */ |
254 | */ |
| 248 | list_initialize(&(driver->devices)); |
255 | list_initialize(&driver->devices); |
| 249 | 256 | ||
| 250 | /* |
257 | /* |
| 251 | * Create connection to the driver |
258 | * Create connection to the driver |
| 252 | */ |
259 | */ |
| 253 | ipc_call_t call; |
260 | ipc_call_t call; |
| 254 | callid = async_get_call(&call); |
261 | callid = async_get_call(&call); |
| 255 | 262 | ||
| 256 | if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) { |
263 | if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) { |
| 257 | ipc_answer_0(callid, ENOTSUP); |
264 | ipc_answer_0(callid, ENOTSUP); |
| 258 | 265 | ||
| 259 | free(driver->name); |
266 | free(driver->name); |
| 260 | free(driver); |
267 | free(driver); |
| 261 | ipc_answer_0(iid, ENOTSUP); |
268 | ipc_answer_0(iid, ENOTSUP); |
| Line 266... | Line 273... | ||
| 266 | 273 | ||
| 267 | ipc_answer_0(callid, EOK); |
274 | ipc_answer_0(callid, EOK); |
| 268 | 275 | ||
| 269 | list_initialize(&(driver->drivers)); |
276 | list_initialize(&(driver->drivers)); |
| 270 | 277 | ||
| 271 | futex_down(&drivers_list_futex); |
278 | fibril_mutex_lock(&drivers_list_mutex); |
| 272 | 279 | ||
| 273 | /* TODO: |
280 | /* TODO: |
| 274 | * check that no driver with name equal to driver->name is registered |
281 | * check that no driver with name equal to driver->name is registered |
| 275 | */ |
282 | */ |
| 276 | 283 | ||
| 277 | /* |
284 | /* |
| 278 | * Insert new driver into list of registered drivers |
285 | * Insert new driver into list of registered drivers |
| 279 | */ |
286 | */ |
| 280 | list_append(&(driver->drivers), &drivers_list); |
287 | list_append(&(driver->drivers), &drivers_list); |
| 281 | futex_up(&drivers_list_futex); |
288 | fibril_mutex_unlock(&drivers_list_mutex); |
| 282 | 289 | ||
| 283 | ipc_answer_0(iid, EOK); |
290 | ipc_answer_0(iid, EOK); |
| 284 | 291 | ||
| 285 | *odriver = driver; |
292 | *odriver = driver; |
| 286 | } |
293 | } |
| Line 293... | Line 300... | ||
| 293 | static int devmap_driver_unregister(devmap_driver_t *driver) |
300 | static int devmap_driver_unregister(devmap_driver_t *driver) |
| 294 | { |
301 | { |
| 295 | if (driver == NULL) |
302 | if (driver == NULL) |
| 296 | return EEXISTS; |
303 | return EEXISTS; |
| 297 | 304 | ||
| 298 | futex_down(&drivers_list_futex); |
305 | fibril_mutex_lock(&drivers_list_mutex); |
| 299 | 306 | ||
| - | 307 | if (driver->phone != 0) |
|
| 300 | ipc_hangup(driver->phone); |
308 | ipc_hangup(driver->phone); |
| 301 | 309 | ||
| 302 | /* remove it from list of drivers */ |
310 | /* Remove it from list of drivers */ |
| 303 | list_remove(&(driver->drivers)); |
311 | list_remove(&(driver->drivers)); |
| 304 | 312 | ||
| 305 | /* unregister all its devices */ |
313 | /* Unregister all its devices */ |
| 306 | - | ||
| 307 | futex_down(&devices_list_futex); |
314 | fibril_mutex_lock(&devices_list_mutex); |
| 308 | futex_down(&(driver->devices_futex)); |
315 | fibril_mutex_lock(&driver->devices_mutex); |
| 309 | 316 | ||
| 310 | while (!list_empty(&(driver->devices))) { |
317 | while (!list_empty(&(driver->devices))) { |
| 311 | devmap_device_t *device = list_get_instance(driver->devices.next, |
318 | devmap_device_t *device = list_get_instance(driver->devices.next, |
| 312 | devmap_device_t, driver_devices); |
319 | devmap_device_t, driver_devices); |
| 313 | devmap_device_unregister_core(device); |
320 | devmap_device_unregister_core(device); |
| 314 | } |
321 | } |
| 315 | 322 | ||
| 316 | futex_up(&(driver->devices_futex)); |
323 | fibril_mutex_unlock(&driver->devices_mutex); |
| 317 | futex_up(&devices_list_futex); |
324 | fibril_mutex_unlock(&devices_list_mutex); |
| 318 | futex_up(&drivers_list_futex); |
325 | fibril_mutex_unlock(&drivers_list_mutex); |
| 319 | 326 | ||
| 320 | /* free name and driver */ |
327 | /* free name and driver */ |
| 321 | if (NULL != driver->name) |
328 | if (driver->name != NULL) |
| 322 | free(driver->name); |
329 | free(driver->name); |
| 323 | 330 | ||
| 324 | free(driver); |
331 | free(driver); |
| 325 | 332 | ||
| 326 | return EOK; |
333 | return EOK; |
| 327 | } |
334 | } |
| 328 | 335 | ||
| 329 | - | ||
| 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 | - | ||
| 353 | /** Register instance of device |
336 | /** Register instance of device |
| 354 | * |
337 | * |
| 355 | */ |
338 | */ |
| 356 | static void devmap_device_register(ipc_callid_t iid, ipc_call_t *icall, |
339 | static void devmap_device_register(ipc_callid_t iid, ipc_call_t *icall, |
| 357 | devmap_driver_t *driver) |
340 | devmap_driver_t *driver) |
| Line 398... | Line 381... | ||
| 398 | device->name[size] = 0; |
381 | device->name[size] = 0; |
| 399 | 382 | ||
| 400 | list_initialize(&(device->devices)); |
383 | list_initialize(&(device->devices)); |
| 401 | list_initialize(&(device->driver_devices)); |
384 | list_initialize(&(device->driver_devices)); |
| 402 | 385 | ||
| 403 | futex_down(&devices_list_futex); |
386 | fibril_mutex_lock(&devices_list_mutex); |
| 404 | 387 | ||
| 405 | /* Check that device with such name is not already registered */ |
388 | /* Check that device with such name is not already registered */ |
| 406 | if (NULL != devmap_device_find_name(device->name)) { |
389 | if (NULL != devmap_device_find_name(device->name)) { |
| 407 | printf(NAME ": Device '%s' already registered\n", device->name); |
390 | printf(NAME ": Device '%s' already registered\n", device->name); |
| 408 | futex_up(&devices_list_futex); |
391 | fibril_mutex_unlock(&devices_list_mutex); |
| 409 | free(device->name); |
392 | free(device->name); |
| 410 | free(device); |
393 | free(device); |
| 411 | ipc_answer_0(iid, EEXISTS); |
394 | ipc_answer_0(iid, EEXISTS); |
| 412 | return; |
395 | return; |
| 413 | } |
396 | } |
| Line 419... | Line 402... | ||
| 419 | 402 | ||
| 420 | /* Insert device into list of all devices */ |
403 | /* Insert device into list of all devices */ |
| 421 | list_append(&device->devices, &devices_list); |
404 | list_append(&device->devices, &devices_list); |
| 422 | 405 | ||
| 423 | /* Insert device into list of devices that belog to one driver */ |
406 | /* Insert device into list of devices that belog to one driver */ |
| 424 | futex_down(&device->driver->devices_futex); |
407 | fibril_mutex_lock(&device->driver->devices_mutex); |
| 425 | 408 | ||
| 426 | list_append(&device->driver_devices, &device->driver->devices); |
409 | list_append(&device->driver_devices, &device->driver->devices); |
| 427 | 410 | ||
| 428 | futex_up(&device->driver->devices_futex); |
411 | fibril_mutex_unlock(&device->driver->devices_mutex); |
| - | 412 | fibril_condvar_broadcast(&devices_list_cv); |
|
| 429 | futex_up(&devices_list_futex); |
413 | fibril_mutex_unlock(&devices_list_mutex); |
| 430 | 414 | ||
| 431 | ipc_answer_1(iid, EOK, device->handle); |
415 | ipc_answer_1(iid, EOK, device->handle); |
| 432 | - | ||
| 433 | process_pending_lookup(); |
- | |
| 434 | } |
416 | } |
| 435 | 417 | ||
| 436 | /** |
418 | /** |
| 437 | * |
419 | * |
| 438 | */ |
420 | */ |
| Line 452... | Line 434... | ||
| 452 | static void devmap_forward(ipc_callid_t callid, ipc_call_t *call) |
434 | static void devmap_forward(ipc_callid_t callid, ipc_call_t *call) |
| 453 | { |
435 | { |
| 454 | /* |
436 | /* |
| 455 | * Get handle from request |
437 | * Get handle from request |
| 456 | */ |
438 | */ |
| 457 | int handle = IPC_GET_ARG2(*call); |
439 | dev_handle_t handle = IPC_GET_ARG2(*call); |
| 458 | devmap_device_t *dev = devmap_device_find_handle(handle); |
440 | devmap_device_t *dev = devmap_device_find_handle(handle); |
| 459 | 441 | ||
| 460 | if (NULL == dev) { |
442 | if ((dev == NULL) || (dev->driver == NULL) || (dev->driver->phone == 0)) { |
| 461 | ipc_answer_0(callid, ENOENT); |
443 | ipc_answer_0(callid, ENOENT); |
| 462 | return; |
444 | return; |
| 463 | } |
445 | } |
| 464 | 446 | ||
| 465 | ipc_forward_fast(callid, dev->driver->phone, (ipcarg_t)(dev->handle), |
447 | ipc_forward_fast(callid, dev->driver->phone, dev->handle, |
| 466 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE); |
448 | IPC_GET_ARG3(*call), 0, IPC_FF_NONE); |
| 467 | } |
449 | } |
| 468 | 450 | ||
| 469 | /** Find handle for device instance identified by name. |
451 | /** Find handle for device instance identified by name. |
| 470 | * |
452 | * |
| Line 493... | Line 475... | ||
| 493 | } |
475 | } |
| 494 | 476 | ||
| 495 | /* |
477 | /* |
| 496 | * Allocate buffer for device name. |
478 | * Allocate buffer for device name. |
| 497 | */ |
479 | */ |
| 498 | char *name = (char *) malloc(size); |
480 | char *name = (char *) malloc(size + 1); |
| 499 | if (name == NULL) { |
481 | if (name == NULL) { |
| 500 | ipc_answer_0(callid, ENOMEM); |
482 | ipc_answer_0(callid, ENOMEM); |
| 501 | ipc_answer_0(iid, EREFUSED); |
483 | ipc_answer_0(iid, EREFUSED); |
| 502 | return; |
484 | return; |
| 503 | } |
485 | } |
| Line 511... | Line 493... | ||
| 511 | free(name); |
493 | free(name); |
| 512 | return; |
494 | return; |
| 513 | } |
495 | } |
| 514 | name[size] = '\0'; |
496 | name[size] = '\0'; |
| 515 | 497 | ||
| - | 498 | fibril_mutex_lock(&devices_list_mutex); |
|
| - | 499 | const devmap_device_t *dev; |
|
| - | 500 | recheck: |
|
| - | 501 | ||
| 516 | /* |
502 | /* |
| 517 | * Find device name in linked list of known devices. |
503 | * Find device name in the list of known devices. |
| 518 | */ |
504 | */ |
| 519 | const devmap_device_t *dev = devmap_device_find_name(name); |
505 | dev = devmap_device_find_name(name); |
| 520 | 506 | ||
| 521 | /* |
507 | /* |
| 522 | * Device was not found. |
508 | * Device was not found. |
| 523 | */ |
509 | */ |
| 524 | if (dev == NULL) { |
510 | if (dev == NULL) { |
| 525 | if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) { |
511 | if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) { |
| 526 | /* Blocking lookup, add to pending list */ |
512 | /* Blocking lookup */ |
| 527 | pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t)); |
- | |
| 528 | if (!pr) { |
- | |
| 529 | ipc_answer_0(iid, ENOMEM); |
513 | fibril_condvar_wait(&devices_list_cv, |
| 530 | free(name); |
514 | &devices_list_mutex); |
| 531 | return; |
515 | goto recheck; |
| 532 | } |
- | |
| 533 | - | ||
| 534 | pr->name = name; |
- | |
| 535 | pr->callid = iid; |
- | |
| 536 | list_append(&pr->link, &pending_req); |
- | |
| 537 | return; |
- | |
| 538 | } |
516 | } |
| 539 | 517 | ||
| 540 | ipc_answer_0(iid, ENOENT); |
518 | ipc_answer_0(iid, ENOENT); |
| 541 | free(name); |
519 | free(name); |
| - | 520 | fibril_mutex_unlock(&devices_list_mutex); |
|
| 542 | return; |
521 | return; |
| 543 | } |
522 | } |
| - | 523 | fibril_mutex_unlock(&devices_list_mutex); |
|
| 544 | 524 | ||
| 545 | ipc_answer_1(iid, EOK, dev->handle); |
525 | ipc_answer_1(iid, EOK, dev->handle); |
| 546 | free(name); |
526 | free(name); |
| 547 | } |
527 | } |
| 548 | 528 | ||
| 549 | /** Find name of device identified by id and send it to caller. |
529 | /** Find name of device identified by id and send it to caller. |
| 550 | * |
530 | * |
| 551 | */ |
531 | */ |
| 552 | static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall) |
532 | static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall) |
| 553 | { |
533 | { |
| 554 | const devmap_device_t *device = devmap_device_find_handle(IPC_GET_ARG1(*icall)); |
534 | const devmap_device_t *device = devmap_device_find_handle(IPC_GET_ARG1(*icall)); |
| 555 | 535 | ||
| 556 | /* |
536 | /* |
| 557 | * Device not found. |
537 | * Device not found. |
| Line 577... | Line 557... | ||
| 577 | */ |
557 | */ |
| 578 | 558 | ||
| 579 | /* TODO: send name in response */ |
559 | /* TODO: send name in response */ |
| 580 | } |
560 | } |
| 581 | 561 | ||
| - | 562 | static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall) |
|
| - | 563 | { |
|
| - | 564 | fibril_mutex_lock(&devices_list_mutex); |
|
| - | 565 | ipc_answer_1(iid, EOK, list_count(&devices_list)); |
|
| - | 566 | fibril_mutex_unlock(&devices_list_mutex); |
|
| - | 567 | } |
|
| - | 568 | ||
| - | 569 | static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall) |
|
| - | 570 | { |
|
| - | 571 | fibril_mutex_lock(&devices_list_mutex); |
|
| - | 572 | ||
| - | 573 | ipc_callid_t callid; |
|
| - | 574 | size_t size; |
|
| - | 575 | if (!ipc_data_read_receive(&callid, &size)) { |
|
| - | 576 | ipc_answer_0(callid, EREFUSED); |
|
| - | 577 | ipc_answer_0(iid, EREFUSED); |
|
| - | 578 | return; |
|
| - | 579 | } |
|
| - | 580 | ||
| - | 581 | if ((size % sizeof(dev_desc_t)) != 0) { |
|
| - | 582 | ipc_answer_0(callid, EINVAL); |
|
| - | 583 | ipc_answer_0(iid, EREFUSED); |
|
| - | 584 | return; |
|
| - | 585 | } |
|
| - | 586 | ||
| - | 587 | size_t count = size / sizeof(dev_desc_t); |
|
| - | 588 | dev_desc_t *desc = (dev_desc_t *) malloc(size); |
|
| - | 589 | if (desc == NULL) { |
|
| - | 590 | ipc_answer_0(callid, ENOMEM); |
|
| - | 591 | ipc_answer_0(iid, EREFUSED); |
|
| - | 592 | return; |
|
| - | 593 | } |
|
| - | 594 | ||
| - | 595 | size_t pos = 0; |
|
| - | 596 | link_t *item = devices_list.next; |
|
| - | 597 | ||
| - | 598 | while ((item != &devices_list) && (pos < count)) { |
|
| - | 599 | devmap_device_t *device = list_get_instance(item, devmap_device_t, devices); |
|
| - | 600 | ||
| - | 601 | desc[pos].handle = device->handle; |
|
| - | 602 | str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, device->name); |
|
| - | 603 | pos++; |
|
| - | 604 | item = item->next; |
|
| - | 605 | } |
|
| - | 606 | ||
| - | 607 | ipcarg_t retval = ipc_data_read_finalize(callid, desc, pos * sizeof(dev_desc_t)); |
|
| - | 608 | if (retval != EOK) { |
|
| - | 609 | ipc_answer_0(iid, EREFUSED); |
|
| - | 610 | free(desc); |
|
| - | 611 | return; |
|
| - | 612 | } |
|
| - | 613 | ||
| - | 614 | free(desc); |
|
| - | 615 | ||
| - | 616 | fibril_mutex_unlock(&devices_list_mutex); |
|
| - | 617 | ||
| - | 618 | ipc_answer_1(iid, EOK, pos); |
|
| - | 619 | } |
|
| - | 620 | ||
| - | 621 | /** Initialize device mapper. |
|
| - | 622 | * |
|
| - | 623 | * |
|
| - | 624 | */ |
|
| - | 625 | static bool devmap_init(void) |
|
| - | 626 | { |
|
| - | 627 | /* Create NULL device entry */ |
|
| - | 628 | devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t)); |
|
| - | 629 | if (device == NULL) |
|
| - | 630 | return false; |
|
| - | 631 | ||
| - | 632 | device->name = str_dup("null"); |
|
| - | 633 | if (device->name == NULL) { |
|
| - | 634 | free(device); |
|
| - | 635 | return false; |
|
| - | 636 | } |
|
| - | 637 | ||
| - | 638 | list_initialize(&(device->devices)); |
|
| - | 639 | list_initialize(&(device->driver_devices)); |
|
| - | 640 | ||
| - | 641 | fibril_mutex_lock(&devices_list_mutex); |
|
| - | 642 | ||
| - | 643 | /* Get unique device handle */ |
|
| - | 644 | device->handle = devmap_create_handle(); |
|
| - | 645 | device->driver = NULL; |
|
| - | 646 | ||
| - | 647 | /* Insert device into list of all devices */ |
|
| - | 648 | list_append(&device->devices, &devices_list); |
|
| - | 649 | ||
| - | 650 | fibril_mutex_unlock(&devices_list_mutex); |
|
| - | 651 | ||
| - | 652 | return true; |
|
| - | 653 | } |
|
| - | 654 | ||
| 582 | /** Handle connection with device driver. |
655 | /** Handle connection with device driver. |
| 583 | * |
656 | * |
| 584 | */ |
657 | */ |
| 585 | static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall) |
658 | static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall) |
| 586 | { |
659 | { |
| 587 | /* Accept connection */ |
660 | /* Accept connection */ |
| 588 | ipc_answer_0(iid, EOK); |
661 | ipc_answer_0(iid, EOK); |
| 589 | 662 | ||
| 590 | devmap_driver_t *driver = NULL; |
663 | devmap_driver_t *driver = NULL; |
| 591 | devmap_driver_register(&driver); |
664 | devmap_driver_register(&driver); |
| 592 | 665 | ||
| 593 | if (NULL == driver) |
666 | if (NULL == driver) |
| 594 | return; |
667 | return; |
| 595 | 668 | ||
| Line 599... | Line 672... | ||
| 599 | ipc_callid_t callid = async_get_call(&call); |
672 | ipc_callid_t callid = async_get_call(&call); |
| 600 | 673 | ||
| 601 | switch (IPC_GET_METHOD(call)) { |
674 | switch (IPC_GET_METHOD(call)) { |
| 602 | case IPC_M_PHONE_HUNGUP: |
675 | case IPC_M_PHONE_HUNGUP: |
| 603 | cont = false; |
676 | cont = false; |
| 604 | /* Exit thread */ |
- | |
| 605 | continue; |
677 | continue; |
| 606 | case DEVMAP_DRIVER_UNREGISTER: |
678 | case DEVMAP_DRIVER_UNREGISTER: |
| 607 | if (NULL == driver) |
679 | if (NULL == driver) |
| 608 | ipc_answer_0(callid, ENOENT); |
680 | ipc_answer_0(callid, ENOENT); |
| 609 | else |
681 | else |
| Line 619... | Line 691... | ||
| 619 | break; |
691 | break; |
| 620 | case DEVMAP_DEVICE_GET_HANDLE: |
692 | case DEVMAP_DEVICE_GET_HANDLE: |
| 621 | devmap_get_handle(callid, &call); |
693 | devmap_get_handle(callid, &call); |
| 622 | break; |
694 | break; |
| 623 | case DEVMAP_DEVICE_GET_NAME: |
695 | case DEVMAP_DEVICE_GET_NAME: |
| 624 | devmap_get_handle(callid, &call); |
696 | devmap_get_name(callid, &call); |
| 625 | break; |
697 | break; |
| 626 | default: |
698 | default: |
| 627 | if (!(callid & IPC_CALLID_NOTIFICATION)) |
699 | if (!(callid & IPC_CALLID_NOTIFICATION)) |
| 628 | ipc_answer_0(callid, ENOENT); |
700 | ipc_answer_0(callid, ENOENT); |
| 629 | } |
701 | } |
| Line 652... | Line 724... | ||
| 652 | ipc_callid_t callid = async_get_call(&call); |
724 | ipc_callid_t callid = async_get_call(&call); |
| 653 | 725 | ||
| 654 | switch (IPC_GET_METHOD(call)) { |
726 | switch (IPC_GET_METHOD(call)) { |
| 655 | case IPC_M_PHONE_HUNGUP: |
727 | case IPC_M_PHONE_HUNGUP: |
| 656 | cont = false; |
728 | cont = false; |
| 657 | /* Exit thread */ |
- | |
| 658 | continue; |
729 | continue; |
| 659 | case DEVMAP_DEVICE_GET_HANDLE: |
730 | case DEVMAP_DEVICE_GET_HANDLE: |
| 660 | devmap_get_handle(callid, &call); |
731 | devmap_get_handle(callid, &call); |
| 661 | break; |
732 | break; |
| 662 | case DEVMAP_DEVICE_GET_NAME: |
733 | case DEVMAP_DEVICE_GET_NAME: |
| 663 | /* TODO */ |
- | |
| 664 | devmap_get_name(callid, &call); |
734 | devmap_get_name(callid, &call); |
| 665 | break; |
735 | break; |
| - | 736 | case DEVMAP_DEVICE_GET_COUNT: |
|
| - | 737 | devmap_get_count(callid, &call); |
|
| - | 738 | break; |
|
| - | 739 | case DEVMAP_DEVICE_GET_DEVICES: |
|
| - | 740 | devmap_get_devices(callid, &call); |
|
| - | 741 | break; |
|
| 666 | default: |
742 | default: |
| 667 | if (!(callid & IPC_CALLID_NOTIFICATION)) |
743 | if (!(callid & IPC_CALLID_NOTIFICATION)) |
| 668 | ipc_answer_0(callid, ENOENT); |
744 | ipc_answer_0(callid, ENOENT); |
| 669 | } |
745 | } |
| 670 | } |
746 | } |
| Line 687... | Line 763... | ||
| 687 | /* Connect client to selected device */ |
763 | /* Connect client to selected device */ |
| 688 | devmap_forward(iid, icall); |
764 | devmap_forward(iid, icall); |
| 689 | break; |
765 | break; |
| 690 | default: |
766 | default: |
| 691 | /* No such interface */ |
767 | /* No such interface */ |
| 692 | ipc_answer_0(iid, ENOENT); |
768 | ipc_answer_0(iid, ENOENT); |
| 693 | } |
769 | } |
| 694 | } |
770 | } |
| 695 | 771 | ||
| 696 | /** |
772 | /** |
| 697 | * |
773 | * |
| 698 | */ |
774 | */ |
| 699 | int main(int argc, char *argv[]) |
775 | int main(int argc, char *argv[]) |
| 700 | { |
776 | { |
| 701 | printf(NAME ": HelenOS Device Mapper\n"); |
777 | printf(NAME ": HelenOS Device Mapper\n"); |
| 702 | 778 | ||
| 703 | if (devmap_init() != 0) { |
779 | if (!devmap_init()) { |
| 704 | printf(NAME ": Error while initializing service\n"); |
780 | printf(NAME ": Error while initializing service\n"); |
| 705 | return -1; |
781 | return -1; |
| 706 | } |
782 | } |
| 707 | 783 | ||
| 708 | /* Set a handler of incomming connections */ |
784 | /* Set a handler of incomming connections */ |
| 709 | async_set_client_connection(devmap_connection); |
785 | async_set_client_connection(devmap_connection); |
| 710 | 786 | ||
| 711 | /* Register device mapper at naming service */ |
787 | /* Register device mapper at naming service */ |
| 712 | ipcarg_t phonead; |
788 | ipcarg_t phonead; |
| 713 | if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0) |
789 | if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0) |
| 714 | return -1; |
790 | return -1; |
| 715 | 791 | ||