Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4691 → Rev 4692

/branches/tracing/uspace/srv/devmap/devmap.c
41,67 → 41,81
#include <stdio.h>
#include <errno.h>
#include <bool.h>
#include <futex.h>
#include <fibril_sync.h>
#include <stdlib.h>
#include <string.h>
#include <ipc/devmap.h>
 
#define NAME "devmap"
#define NAME "devmap"
#define NULL_DEVICES 256
 
/** Pending lookup structure. */
/** Representation of device driver.
*
* Each driver is responsible for a set of devices.
*
*/
typedef struct {
link_t link;
char *name; /**< Device name */
ipc_callid_t callid; /**< Call ID waiting for the lookup */
} pending_req_t;
/** Pointers to previous and next drivers in linked list */
link_t drivers;
/** Pointer to the linked list of devices controlled by this driver */
link_t devices;
/** Phone asociated with this driver */
ipcarg_t phone;
/** Device driver name */
char *name;
/** Fibril mutex for list of devices owned by this driver */
fibril_mutex_t devices_mutex;
} devmap_driver_t;
 
/** Info about registered device
*
*/
typedef struct {
/** Pointer to the previous and next device in the list of all devices */
link_t devices;
/** Pointer to the previous and next device in the list of devices
owned by one driver */
link_t driver_devices;
/** Unique device identifier */
dev_handle_t handle;
/** Device name */
char *name;
/** Device driver handling this device */
devmap_driver_t *driver;
} devmap_device_t;
 
LIST_INITIALIZE(devices_list);
LIST_INITIALIZE(drivers_list);
LIST_INITIALIZE(pending_req);
 
/* Locking order:
* drivers_list_futex
* devices_list_futex
* (devmap_driver_t *)->devices_futex
* create_handle_futex
* drivers_list_mutex
* devices_list_mutex
* (devmap_driver_t *)->devices_mutex
* create_handle_mutex
**/
 
static atomic_t devices_list_futex = FUTEX_INITIALIZER;
static atomic_t drivers_list_futex = FUTEX_INITIALIZER;
static atomic_t create_handle_futex = FUTEX_INITIALIZER;
static FIBRIL_MUTEX_INITIALIZE(devices_list_mutex);
static FIBRIL_CONDVAR_INITIALIZE(devices_list_cv);
static FIBRIL_MUTEX_INITIALIZE(drivers_list_mutex);
static FIBRIL_MUTEX_INITIALIZE(create_handle_mutex);
static FIBRIL_MUTEX_INITIALIZE(null_devices_mutex);
 
static int devmap_create_handle(void)
static dev_handle_t last_handle = 0;
static devmap_device_t *null_devices[NULL_DEVICES];
 
static dev_handle_t devmap_create_handle(void)
{
static int last_handle = 0;
int handle;
/* TODO: allow reusing old handles after their unregistration
* and implement some version of LRU algorithm
* and implement some version of LRU algorithm, avoid overflow
*/
/* FIXME: overflow */
futex_down(&create_handle_futex);
fibril_mutex_lock(&create_handle_mutex);
last_handle++;
fibril_mutex_unlock(&create_handle_mutex);
last_handle += 1;
handle = last_handle;
futex_up(&create_handle_futex);
return handle;
return last_handle;
}
 
 
/** Initialize device mapper.
*
*
*/
static int devmap_init()
{
/* TODO: */
return EOK;
}
 
/** Find device with given name.
*
*/
112,7 → 126,7
while (item != &devices_list) {
device = list_get_instance(item, devmap_device_t, devices);
if (0 == str_cmp(device->name, name))
if (str_cmp(device->name, name) == 0)
break;
item = item->next;
}
129,9 → 143,9
* @todo: use hash table
*
*/
static devmap_device_t *devmap_device_find_handle(int handle)
static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
{
futex_down(&devices_list_futex);
fibril_mutex_lock(&devices_list_mutex);
link_t *item = (&devices_list)->next;
devmap_device_t *device = NULL;
144,22 → 158,20
}
if (item == &devices_list) {
futex_up(&devices_list_futex);
fibril_mutex_unlock(&devices_list_mutex);
return NULL;
}
device = list_get_instance(item, devmap_device_t, devices);
futex_up(&devices_list_futex);
fibril_mutex_unlock(&devices_list_mutex);
return device;
}
 
/**
*
* Unregister device and free it. It's assumed that driver's device list is
* already locked.
*
*/
static int devmap_device_unregister_core(devmap_device_t *device)
{
173,10 → 185,8
}
 
/**
*
* Read info about new driver and add it into linked list of registered
* drivers.
*
*/
static void devmap_driver_register(devmap_driver_t **odriver)
{
230,7 → 240,7
/*
* Send confirmation to sender and get data into buffer.
*/
if (EOK != ipc_data_write_finalize(callid, driver->name, name_size)) {
if (ipc_data_write_finalize(callid, driver->name, name_size) != EOK) {
free(driver->name);
free(driver);
ipc_answer_0(iid, EREFUSED);
239,21 → 249,21
driver->name[name_size] = 0;
/* Initialize futex for list of devices owned by this driver */
futex_initialize(&(driver->devices_futex), 1);
/* Initialize mutex for list of devices owned by this driver */
fibril_mutex_initialize(&driver->devices_mutex);
/*
* Initialize list of asociated devices
*/
list_initialize(&(driver->devices));
list_initialize(&driver->devices);
/*
* Create connection to the driver
* Create connection to the driver
*/
ipc_call_t call;
callid = async_get_call(&call);
if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) {
if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
ipc_answer_0(callid, ENOTSUP);
free(driver->name);
268,7 → 278,7
list_initialize(&(driver->drivers));
futex_down(&drivers_list_futex);
fibril_mutex_lock(&drivers_list_mutex);
/* TODO:
* check that no driver with name equal to driver->name is registered
278,7 → 288,7
* Insert new driver into list of registered drivers
*/
list_append(&(driver->drivers), &drivers_list);
futex_up(&drivers_list_futex);
fibril_mutex_unlock(&drivers_list_mutex);
ipc_answer_0(iid, EOK);
295,18 → 305,18
if (driver == NULL)
return EEXISTS;
futex_down(&drivers_list_futex);
fibril_mutex_lock(&drivers_list_mutex);
ipc_hangup(driver->phone);
if (driver->phone != 0)
ipc_hangup(driver->phone);
/* remove it from list of drivers */
/* Remove it from list of drivers */
list_remove(&(driver->drivers));
/* unregister all its devices */
/* Unregister all its devices */
fibril_mutex_lock(&devices_list_mutex);
fibril_mutex_lock(&driver->devices_mutex);
futex_down(&devices_list_futex);
futex_down(&(driver->devices_futex));
while (!list_empty(&(driver->devices))) {
devmap_device_t *device = list_get_instance(driver->devices.next,
devmap_device_t, driver_devices);
313,12 → 323,12
devmap_device_unregister_core(device);
}
futex_up(&(driver->devices_futex));
futex_up(&devices_list_futex);
futex_up(&drivers_list_futex);
fibril_mutex_unlock(&driver->devices_mutex);
fibril_mutex_unlock(&devices_list_mutex);
fibril_mutex_unlock(&drivers_list_mutex);
/* free name and driver */
if (NULL != driver->name)
if (driver->name != NULL)
free(driver->name);
free(driver);
326,30 → 336,6
return EOK;
}
 
 
/** Process pending lookup requests */
static void process_pending_lookup()
{
link_t *cur;
loop:
for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
const devmap_device_t *dev = devmap_device_find_name(pr->name);
if (!dev)
continue;
ipc_answer_1(pr->callid, EOK, dev->handle);
free(pr->name);
list_remove(cur);
free(pr);
goto loop;
}
}
 
 
/** Register instance of device
*
*/
400,12 → 386,12
list_initialize(&(device->devices));
list_initialize(&(device->driver_devices));
futex_down(&devices_list_futex);
fibril_mutex_lock(&devices_list_mutex);
/* Check that device with such name is not already registered */
if (NULL != devmap_device_find_name(device->name)) {
printf(NAME ": Device '%s' already registered\n", device->name);
futex_up(&devices_list_futex);
fibril_mutex_unlock(&devices_list_mutex);
free(device->name);
free(device);
ipc_answer_0(iid, EEXISTS);
421,16 → 407,15
list_append(&device->devices, &devices_list);
/* Insert device into list of devices that belog to one driver */
futex_down(&device->driver->devices_futex);
fibril_mutex_lock(&device->driver->devices_mutex);
list_append(&device->driver_devices, &device->driver->devices);
futex_up(&device->driver->devices_futex);
futex_up(&devices_list_futex);
fibril_mutex_unlock(&device->driver->devices_mutex);
fibril_condvar_broadcast(&devices_list_cv);
fibril_mutex_unlock(&devices_list_mutex);
ipc_answer_1(iid, EOK, device->handle);
process_pending_lookup();
}
 
/**
454,15 → 439,15
/*
* Get handle from request
*/
int handle = IPC_GET_ARG2(*call);
dev_handle_t handle = IPC_GET_ARG2(*call);
devmap_device_t *dev = devmap_device_find_handle(handle);
if (NULL == dev) {
if ((dev == NULL) || (dev->driver == NULL) || (dev->driver->phone == 0)) {
ipc_answer_0(callid, ENOENT);
return;
}
ipc_forward_fast(callid, dev->driver->phone, (ipcarg_t)(dev->handle),
ipc_forward_fast(callid, dev->driver->phone, dev->handle,
IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
}
 
495,7 → 480,7
/*
* Allocate buffer for device name.
*/
char *name = (char *) malloc(size);
char *name = (char *) malloc(size + 1);
if (name == NULL) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(iid, EREFUSED);
513,10 → 498,14
}
name[size] = '\0';
fibril_mutex_lock(&devices_list_mutex);
const devmap_device_t *dev;
recheck:
 
/*
* Find device name in linked list of known devices.
* Find device name in the list of known devices.
*/
const devmap_device_t *dev = devmap_device_find_name(name);
dev = devmap_device_find_name(name);
/*
* Device was not found.
523,24 → 512,18
*/
if (dev == NULL) {
if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
/* Blocking lookup, add to pending list */
pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
if (!pr) {
ipc_answer_0(iid, ENOMEM);
free(name);
return;
}
pr->name = name;
pr->callid = iid;
list_append(&pr->link, &pending_req);
return;
/* Blocking lookup */
fibril_condvar_wait(&devices_list_cv,
&devices_list_mutex);
goto recheck;
}
ipc_answer_0(iid, ENOENT);
free(name);
fibril_mutex_unlock(&devices_list_mutex);
return;
}
fibril_mutex_unlock(&devices_list_mutex);
ipc_answer_1(iid, EOK, dev->handle);
free(name);
549,7 → 532,7
/** Find name of device identified by id and send it to caller.
*
*/
static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall)
static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall)
{
const devmap_device_t *device = devmap_device_find_handle(IPC_GET_ARG1(*icall));
563,12 → 546,12
ipc_answer_0(iid, EOK);
size_t name_size = str_size(device->name);
/* FIXME:
* We have no channel from DEVMAP to client, therefore
* sending must be initiated by client.
*
* size_t name_size = str_size(device->name);
*
* int rc = ipc_data_write_send(phone, device->name, name_size);
* if (rc != EOK) {
* async_wait_for(req, NULL);
579,6 → 562,160
/* TODO: send name in response */
}
 
static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
{
fibril_mutex_lock(&devices_list_mutex);
ipc_answer_1(iid, EOK, list_count(&devices_list));
fibril_mutex_unlock(&devices_list_mutex);
}
 
static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
{
fibril_mutex_lock(&devices_list_mutex);
ipc_callid_t callid;
size_t size;
if (!ipc_data_read_receive(&callid, &size)) {
ipc_answer_0(callid, EREFUSED);
ipc_answer_0(iid, EREFUSED);
return;
}
if ((size % sizeof(dev_desc_t)) != 0) {
ipc_answer_0(callid, EINVAL);
ipc_answer_0(iid, EREFUSED);
return;
}
size_t count = size / sizeof(dev_desc_t);
dev_desc_t *desc = (dev_desc_t *) malloc(size);
if (desc == NULL) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(iid, EREFUSED);
return;
}
size_t pos = 0;
link_t *item = devices_list.next;
while ((item != &devices_list) && (pos < count)) {
devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
desc[pos].handle = device->handle;
str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, device->name);
pos++;
item = item->next;
}
ipcarg_t retval = ipc_data_read_finalize(callid, desc, pos * sizeof(dev_desc_t));
if (retval != EOK) {
ipc_answer_0(iid, EREFUSED);
free(desc);
return;
}
free(desc);
fibril_mutex_unlock(&devices_list_mutex);
ipc_answer_1(iid, EOK, pos);
}
 
static void devmap_null_create(ipc_callid_t iid, ipc_call_t *icall)
{
fibril_mutex_lock(&null_devices_mutex);
unsigned int i;
bool fnd = false;
for (i = 0; i < NULL_DEVICES; i++) {
if (null_devices[i] == NULL) {
fnd = true;
break;
}
}
if (!fnd) {
fibril_mutex_unlock(&null_devices_mutex);
ipc_answer_0(iid, ENOMEM);
return;
}
/* Create NULL device entry */
devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
if (device == NULL) {
fibril_mutex_unlock(&null_devices_mutex);
ipc_answer_0(iid, ENOMEM);
return;
}
char null[DEVMAP_NAME_MAXLEN];
snprintf(null, DEVMAP_NAME_MAXLEN, "null%u", i);
device->name = str_dup(null);
if (device->name == NULL) {
fibril_mutex_unlock(&null_devices_mutex);
free(device);
ipc_answer_0(iid, ENOMEM);
return;
}
list_initialize(&(device->devices));
list_initialize(&(device->driver_devices));
fibril_mutex_lock(&devices_list_mutex);
/* Get unique device handle */
device->handle = devmap_create_handle();
device->driver = NULL;
/* Insert device into list of all devices
and into null devices array */
list_append(&device->devices, &devices_list);
null_devices[i] = device;
fibril_mutex_unlock(&devices_list_mutex);
fibril_mutex_unlock(&null_devices_mutex);
ipc_answer_1(iid, EOK, (ipcarg_t) i);
}
 
static void devmap_null_destroy(ipc_callid_t iid, ipc_call_t *icall)
{
fibril_mutex_lock(&null_devices_mutex);
ipcarg_t i = IPC_GET_ARG1(*icall);
if (null_devices[i] == NULL) {
ipc_answer_0(iid, ENOENT);
return;
}
devmap_device_unregister_core(null_devices[i]);
null_devices[i] = NULL;
fibril_mutex_unlock(&null_devices_mutex);
ipc_answer_0(iid, EOK);
}
 
/** Initialize device mapper.
*
*
*/
static bool devmap_init(void)
{
fibril_mutex_lock(&null_devices_mutex);
unsigned int i;
for (i = 0; i < NULL_DEVICES; i++)
null_devices[i] = NULL;
fibril_mutex_unlock(&null_devices_mutex);
return true;
}
 
/** Handle connection with device driver.
*
*/
587,7 → 724,7
/* Accept connection */
ipc_answer_0(iid, EOK);
devmap_driver_t *driver = NULL;
devmap_driver_t *driver = NULL;
devmap_driver_register(&driver);
if (NULL == driver)
601,7 → 738,6
switch (IPC_GET_METHOD(call)) {
case IPC_M_PHONE_HUNGUP:
cont = false;
/* Exit thread */
continue;
case DEVMAP_DRIVER_UNREGISTER:
if (NULL == driver)
621,7 → 757,7
devmap_get_handle(callid, &call);
break;
case DEVMAP_DEVICE_GET_NAME:
devmap_get_handle(callid, &call);
devmap_get_name(callid, &call);
break;
default:
if (!(callid & IPC_CALLID_NOTIFICATION))
629,7 → 765,7
}
}
if (NULL != driver) {
if (driver != NULL) {
/*
* Unregister the device driver and all its devices.
*/
654,15 → 790,25
switch (IPC_GET_METHOD(call)) {
case IPC_M_PHONE_HUNGUP:
cont = false;
/* Exit thread */
continue;
case DEVMAP_DEVICE_GET_HANDLE:
devmap_get_handle(callid, &call);
break;
case DEVMAP_DEVICE_GET_NAME:
/* TODO */
devmap_get_name(callid, &call);
break;
case DEVMAP_DEVICE_NULL_CREATE:
devmap_null_create(callid, &call);
break;
case DEVMAP_DEVICE_NULL_DESTROY:
devmap_null_destroy(callid, &call);
break;
case DEVMAP_DEVICE_GET_COUNT:
devmap_get_count(callid, &call);
break;
case DEVMAP_DEVICE_GET_DEVICES:
devmap_get_devices(callid, &call);
break;
default:
if (!(callid & IPC_CALLID_NOTIFICATION))
ipc_answer_0(callid, ENOENT);
689,7 → 835,7
break;
default:
/* No such interface */
ipc_answer_0(iid, ENOENT);
ipc_answer_0(iid, ENOENT);
}
}
 
700,7 → 846,7
{
printf(NAME ": HelenOS Device Mapper\n");
if (devmap_init() != 0) {
if (!devmap_init()) {
printf(NAME ": Error while initializing service\n");
return -1;
}