2
* Copyright (c) 2007 Josef Cejka
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
30
* @defgroup devmap Device mapper.
31
* @brief HelenOS device mapper.
38
#include <ipc/services.h>
44
#include <fibril_sync.h>
47
#include <ipc/devmap.h>
50
#define NULL_DEVICES 256
52
/** Representation of device driver.
54
* Each driver is responsible for a set of devices.
58
/** Pointers to previous and next drivers in linked list */
60
/** Pointer to the linked list of devices controlled by this driver */
62
/** Phone asociated with this driver */
64
/** Device driver name */
66
/** Fibril mutex for list of devices owned by this driver */
67
fibril_mutex_t devices_mutex;
70
/** Info about registered device
74
/** Pointer to the previous and next device in the list of all devices */
76
/** Pointer to the previous and next device in the list of devices
77
owned by one driver */
78
link_t driver_devices;
79
/** Unique device identifier */
83
/** Device driver handling this device */
84
devmap_driver_t *driver;
87
LIST_INITIALIZE(devices_list);
88
LIST_INITIALIZE(drivers_list);
93
* (devmap_driver_t *)->devices_mutex
97
static FIBRIL_MUTEX_INITIALIZE(devices_list_mutex);
98
static FIBRIL_CONDVAR_INITIALIZE(devices_list_cv);
99
static FIBRIL_MUTEX_INITIALIZE(drivers_list_mutex);
100
static FIBRIL_MUTEX_INITIALIZE(create_handle_mutex);
101
static FIBRIL_MUTEX_INITIALIZE(null_devices_mutex);
103
static dev_handle_t last_handle = 0;
104
static devmap_device_t *null_devices[NULL_DEVICES];
106
static dev_handle_t devmap_create_handle(void)
108
/* TODO: allow reusing old handles after their unregistration
109
* and implement some version of LRU algorithm, avoid overflow
112
fibril_mutex_lock(&create_handle_mutex);
114
fibril_mutex_unlock(&create_handle_mutex);
119
/** Find device with given name.
122
static devmap_device_t *devmap_device_find_name(const char *name)
124
link_t *item = devices_list.next;
125
devmap_device_t *device = NULL;
127
while (item != &devices_list) {
128
device = list_get_instance(item, devmap_device_t, devices);
129
if (str_cmp(device->name, name) == 0)
134
if (item == &devices_list)
137
device = list_get_instance(item, devmap_device_t, devices);
141
/** Find device with given handle.
143
* @todo: use hash table
146
static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
148
fibril_mutex_lock(&devices_list_mutex);
150
link_t *item = (&devices_list)->next;
151
devmap_device_t *device = NULL;
153
while (item != &devices_list) {
154
device = list_get_instance(item, devmap_device_t, devices);
155
if (device->handle == handle)
160
if (item == &devices_list) {
161
fibril_mutex_unlock(&devices_list_mutex);
165
device = list_get_instance(item, devmap_device_t, devices);
167
fibril_mutex_unlock(&devices_list_mutex);
173
* Unregister device and free it. It's assumed that driver's device list is
176
static int devmap_device_unregister_core(devmap_device_t *device)
178
list_remove(&(device->devices));
179
list_remove(&(device->driver_devices));
188
* Read info about new driver and add it into linked list of registered
191
static void devmap_driver_register(devmap_driver_t **odriver)
196
ipc_callid_t iid = async_get_call(&icall);
198
if (IPC_GET_METHOD(icall) != DEVMAP_DRIVER_REGISTER) {
199
ipc_answer_0(iid, EREFUSED);
203
devmap_driver_t *driver = (devmap_driver_t *) malloc(sizeof(devmap_driver_t));
205
if (driver == NULL) {
206
ipc_answer_0(iid, ENOMEM);
215
if (!ipc_data_write_receive(&callid, &name_size)) {
217
ipc_answer_0(callid, EREFUSED);
218
ipc_answer_0(iid, EREFUSED);
222
if (name_size > DEVMAP_NAME_MAXLEN) {
224
ipc_answer_0(callid, EINVAL);
225
ipc_answer_0(iid, EREFUSED);
230
* Allocate buffer for device name.
232
driver->name = (char *) malloc(name_size + 1);
233
if (driver->name == NULL) {
235
ipc_answer_0(callid, ENOMEM);
236
ipc_answer_0(iid, EREFUSED);
241
* Send confirmation to sender and get data into buffer.
243
if (ipc_data_write_finalize(callid, driver->name, name_size) != EOK) {
246
ipc_answer_0(iid, EREFUSED);
250
driver->name[name_size] = 0;
252
/* Initialize mutex for list of devices owned by this driver */
253
fibril_mutex_initialize(&driver->devices_mutex);
256
* Initialize list of asociated devices
258
list_initialize(&driver->devices);
261
* Create connection to the driver
264
callid = async_get_call(&call);
266
if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
267
ipc_answer_0(callid, ENOTSUP);
271
ipc_answer_0(iid, ENOTSUP);
275
driver->phone = IPC_GET_ARG5(call);
277
ipc_answer_0(callid, EOK);
279
list_initialize(&(driver->drivers));
281
fibril_mutex_lock(&drivers_list_mutex);
284
* check that no driver with name equal to driver->name is registered
288
* Insert new driver into list of registered drivers
290
list_append(&(driver->drivers), &drivers_list);
291
fibril_mutex_unlock(&drivers_list_mutex);
293
ipc_answer_0(iid, EOK);
299
* Unregister device driver, unregister all its devices and free driver
303
static int devmap_driver_unregister(devmap_driver_t *driver)
308
fibril_mutex_lock(&drivers_list_mutex);
310
if (driver->phone != 0)
311
ipc_hangup(driver->phone);
313
/* Remove it from list of drivers */
314
list_remove(&(driver->drivers));
316
/* Unregister all its devices */
317
fibril_mutex_lock(&devices_list_mutex);
318
fibril_mutex_lock(&driver->devices_mutex);
320
while (!list_empty(&(driver->devices))) {
321
devmap_device_t *device = list_get_instance(driver->devices.next,
322
devmap_device_t, driver_devices);
323
devmap_device_unregister_core(device);
326
fibril_mutex_unlock(&driver->devices_mutex);
327
fibril_mutex_unlock(&devices_list_mutex);
328
fibril_mutex_unlock(&drivers_list_mutex);
330
/* free name and driver */
331
if (driver->name != NULL)
339
/** Register instance of device
342
static void devmap_device_register(ipc_callid_t iid, ipc_call_t *icall,
343
devmap_driver_t *driver)
345
if (driver == NULL) {
346
ipc_answer_0(iid, EREFUSED);
350
/* Create new device entry */
351
devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
352
if (device == NULL) {
353
ipc_answer_0(iid, ENOMEM);
357
/* Get device name */
360
if (!ipc_data_write_receive(&callid, &size)) {
362
ipc_answer_0(iid, EREFUSED);
366
if (size > DEVMAP_NAME_MAXLEN) {
368
ipc_answer_0(callid, EINVAL);
369
ipc_answer_0(iid, EREFUSED);
373
/* +1 for terminating \0 */
374
device->name = (char *) malloc(size + 1);
376
if (device->name == NULL) {
378
ipc_answer_0(callid, ENOMEM);
379
ipc_answer_0(iid, EREFUSED);
383
ipc_data_write_finalize(callid, device->name, size);
384
device->name[size] = 0;
386
list_initialize(&(device->devices));
387
list_initialize(&(device->driver_devices));
389
fibril_mutex_lock(&devices_list_mutex);
391
/* Check that device with such name is not already registered */
392
if (NULL != devmap_device_find_name(device->name)) {
393
printf(NAME ": Device '%s' already registered\n", device->name);
394
fibril_mutex_unlock(&devices_list_mutex);
397
ipc_answer_0(iid, EEXISTS);
401
/* Get unique device handle */
402
device->handle = devmap_create_handle();
404
device->driver = driver;
406
/* Insert device into list of all devices */
407
list_append(&device->devices, &devices_list);
409
/* Insert device into list of devices that belog to one driver */
410
fibril_mutex_lock(&device->driver->devices_mutex);
412
list_append(&device->driver_devices, &device->driver->devices);
414
fibril_mutex_unlock(&device->driver->devices_mutex);
415
fibril_condvar_broadcast(&devices_list_cv);
416
fibril_mutex_unlock(&devices_list_mutex);
418
ipc_answer_1(iid, EOK, device->handle);
424
static int devmap_device_unregister(ipc_callid_t iid, ipc_call_t *icall,
425
devmap_driver_t *driver)
431
/** Connect client to the device.
433
* Find device driver owning requested device and forward
437
static void devmap_forward(ipc_callid_t callid, ipc_call_t *call)
440
* Get handle from request
442
dev_handle_t handle = IPC_GET_ARG2(*call);
443
devmap_device_t *dev = devmap_device_find_handle(handle);
445
if ((dev == NULL) || (dev->driver == NULL) || (dev->driver->phone == 0)) {
446
ipc_answer_0(callid, ENOENT);
450
ipc_forward_fast(callid, dev->driver->phone, dev->handle,
451
IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
454
/** Find handle for device instance identified by name.
456
* In answer will be send EOK and device handle in arg1 or a error
460
static void devmap_get_handle(ipc_callid_t iid, ipc_call_t *icall)
463
* Wait for incoming message with device name (but do not
464
* read the name itself until the buffer is allocated).
468
if (!ipc_data_write_receive(&callid, &size)) {
469
ipc_answer_0(callid, EREFUSED);
470
ipc_answer_0(iid, EREFUSED);
474
if ((size < 1) || (size > DEVMAP_NAME_MAXLEN)) {
475
ipc_answer_0(callid, EINVAL);
476
ipc_answer_0(iid, EREFUSED);
481
* Allocate buffer for device name.
483
char *name = (char *) malloc(size + 1);
485
ipc_answer_0(callid, ENOMEM);
486
ipc_answer_0(iid, EREFUSED);
491
* Send confirmation to sender and get data into buffer.
493
ipcarg_t retval = ipc_data_write_finalize(callid, name, size);
495
ipc_answer_0(iid, EREFUSED);
501
fibril_mutex_lock(&devices_list_mutex);
502
const devmap_device_t *dev;
506
* Find device name in the list of known devices.
508
dev = devmap_device_find_name(name);
511
* Device was not found.
514
if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
515
/* Blocking lookup */
516
fibril_condvar_wait(&devices_list_cv,
517
&devices_list_mutex);
521
ipc_answer_0(iid, ENOENT);
523
fibril_mutex_unlock(&devices_list_mutex);
526
fibril_mutex_unlock(&devices_list_mutex);
528
ipc_answer_1(iid, EOK, dev->handle);
532
/** Find name of device identified by id and send it to caller.
535
static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall)
537
const devmap_device_t *device = devmap_device_find_handle(IPC_GET_ARG1(*icall));
542
if (device == NULL) {
543
ipc_answer_0(iid, ENOENT);
547
ipc_answer_0(iid, EOK);
550
* We have no channel from DEVMAP to client, therefore
551
* sending must be initiated by client.
553
* size_t name_size = str_size(device->name);
555
* int rc = ipc_data_write_send(phone, device->name, name_size);
557
* async_wait_for(req, NULL);
562
/* TODO: send name in response */
565
static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
567
fibril_mutex_lock(&devices_list_mutex);
568
ipc_answer_1(iid, EOK, list_count(&devices_list));
569
fibril_mutex_unlock(&devices_list_mutex);
572
static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
574
fibril_mutex_lock(&devices_list_mutex);
578
if (!ipc_data_read_receive(&callid, &size)) {
579
ipc_answer_0(callid, EREFUSED);
580
ipc_answer_0(iid, EREFUSED);
584
if ((size % sizeof(dev_desc_t)) != 0) {
585
ipc_answer_0(callid, EINVAL);
586
ipc_answer_0(iid, EREFUSED);
590
size_t count = size / sizeof(dev_desc_t);
591
dev_desc_t *desc = (dev_desc_t *) malloc(size);
593
ipc_answer_0(callid, ENOMEM);
594
ipc_answer_0(iid, EREFUSED);
599
link_t *item = devices_list.next;
601
while ((item != &devices_list) && (pos < count)) {
602
devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
604
desc[pos].handle = device->handle;
605
str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, device->name);
610
ipcarg_t retval = ipc_data_read_finalize(callid, desc, pos * sizeof(dev_desc_t));
612
ipc_answer_0(iid, EREFUSED);
619
fibril_mutex_unlock(&devices_list_mutex);
621
ipc_answer_1(iid, EOK, pos);
624
static void devmap_null_create(ipc_callid_t iid, ipc_call_t *icall)
626
fibril_mutex_lock(&null_devices_mutex);
631
for (i = 0; i < NULL_DEVICES; i++) {
632
if (null_devices[i] == NULL) {
639
fibril_mutex_unlock(&null_devices_mutex);
640
ipc_answer_0(iid, ENOMEM);
644
/* Create NULL device entry */
645
devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
646
if (device == NULL) {
647
fibril_mutex_unlock(&null_devices_mutex);
648
ipc_answer_0(iid, ENOMEM);
652
char null[DEVMAP_NAME_MAXLEN];
653
snprintf(null, DEVMAP_NAME_MAXLEN, "null%u", i);
655
device->name = str_dup(null);
656
if (device->name == NULL) {
657
fibril_mutex_unlock(&null_devices_mutex);
659
ipc_answer_0(iid, ENOMEM);
663
list_initialize(&(device->devices));
664
list_initialize(&(device->driver_devices));
666
fibril_mutex_lock(&devices_list_mutex);
668
/* Get unique device handle */
669
device->handle = devmap_create_handle();
670
device->driver = NULL;
672
/* Insert device into list of all devices
673
and into null devices array */
674
list_append(&device->devices, &devices_list);
675
null_devices[i] = device;
677
fibril_mutex_unlock(&devices_list_mutex);
678
fibril_mutex_unlock(&null_devices_mutex);
680
ipc_answer_1(iid, EOK, (ipcarg_t) i);
683
static void devmap_null_destroy(ipc_callid_t iid, ipc_call_t *icall)
685
fibril_mutex_lock(&null_devices_mutex);
687
ipcarg_t i = IPC_GET_ARG1(*icall);
689
if (null_devices[i] == NULL) {
690
ipc_answer_0(iid, ENOENT);
694
devmap_device_unregister_core(null_devices[i]);
695
null_devices[i] = NULL;
697
fibril_mutex_unlock(&null_devices_mutex);
699
ipc_answer_0(iid, EOK);
702
/** Initialize device mapper.
706
static bool devmap_init(void)
708
fibril_mutex_lock(&null_devices_mutex);
711
for (i = 0; i < NULL_DEVICES; i++)
712
null_devices[i] = NULL;
714
fibril_mutex_unlock(&null_devices_mutex);
719
/** Handle connection with device driver.
722
static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
724
/* Accept connection */
725
ipc_answer_0(iid, EOK);
727
devmap_driver_t *driver = NULL;
728
devmap_driver_register(&driver);
736
ipc_callid_t callid = async_get_call(&call);
738
switch (IPC_GET_METHOD(call)) {
739
case IPC_M_PHONE_HUNGUP:
742
case DEVMAP_DRIVER_UNREGISTER:
744
ipc_answer_0(callid, ENOENT);
746
ipc_answer_0(callid, EOK);
748
case DEVMAP_DEVICE_REGISTER:
749
/* Register one instance of device */
750
devmap_device_register(callid, &call, driver);
752
case DEVMAP_DEVICE_UNREGISTER:
753
/* Remove instance of device identified by handler */
754
devmap_device_unregister(callid, &call, driver);
756
case DEVMAP_DEVICE_GET_HANDLE:
757
devmap_get_handle(callid, &call);
759
case DEVMAP_DEVICE_GET_NAME:
760
devmap_get_name(callid, &call);
763
if (!(callid & IPC_CALLID_NOTIFICATION))
764
ipc_answer_0(callid, ENOENT);
768
if (driver != NULL) {
770
* Unregister the device driver and all its devices.
772
devmap_driver_unregister(driver);
777
/** Handle connection with device client.
780
static void devmap_connection_client(ipc_callid_t iid, ipc_call_t *icall)
782
/* Accept connection */
783
ipc_answer_0(iid, EOK);
788
ipc_callid_t callid = async_get_call(&call);
790
switch (IPC_GET_METHOD(call)) {
791
case IPC_M_PHONE_HUNGUP:
794
case DEVMAP_DEVICE_GET_HANDLE:
795
devmap_get_handle(callid, &call);
797
case DEVMAP_DEVICE_GET_NAME:
798
devmap_get_name(callid, &call);
800
case DEVMAP_DEVICE_NULL_CREATE:
801
devmap_null_create(callid, &call);
803
case DEVMAP_DEVICE_NULL_DESTROY:
804
devmap_null_destroy(callid, &call);
806
case DEVMAP_DEVICE_GET_COUNT:
807
devmap_get_count(callid, &call);
809
case DEVMAP_DEVICE_GET_DEVICES:
810
devmap_get_devices(callid, &call);
813
if (!(callid & IPC_CALLID_NOTIFICATION))
814
ipc_answer_0(callid, ENOENT);
819
/** Function for handling connections to devmap
822
static void devmap_connection(ipc_callid_t iid, ipc_call_t *icall)
824
/* Select interface */
825
switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
827
devmap_connection_driver(iid, icall);
830
devmap_connection_client(iid, icall);
832
case DEVMAP_CONNECT_TO_DEVICE:
833
/* Connect client to selected device */
834
devmap_forward(iid, icall);
837
/* No such interface */
838
ipc_answer_0(iid, ENOENT);
845
int main(int argc, char *argv[])
847
printf(NAME ": HelenOS Device Mapper\n");
849
if (!devmap_init()) {
850
printf(NAME ": Error while initializing service\n");
854
/* Set a handler of incomming connections */
855
async_set_client_connection(devmap_connection);
857
/* Register device mapper at naming service */
859
if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0)
862
printf(NAME ": Accepting connections\n");