Subversion Repositories HelenOS

Compare Revisions

Regard whitespace Rev 4377 → Rev 3597

/branches/tracing/uspace/srv/devmap/Makefile
29,9 → 29,10
## Setup toolchain
#
 
include ../../Makefile.config
 
LIBC_PREFIX = ../../lib/libc
SOFTINT_PREFIX = ../../lib/softint
 
include $(LIBC_PREFIX)/Makefile.toolchain
 
CFLAGS += -I../libipc/include
45,7 → 46,7
SOURCES = \
devmap.c
 
CFLAGS += -D$(UARCH)
CFLAGS += -D$(ARCH)
 
OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
 
56,13 → 57,13
-include Makefile.depend
 
clean:
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS)
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend
 
depend:
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend
 
$(OUTPUT): $(OBJECTS) $(LIBS)
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map
$(LD) -T $(LIBC_PREFIX)/arch/$(ARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map
 
disasm: $(OUTPUT).disasm
 
/branches/tracing/uspace/srv/devmap/devmap.c
48,18 → 48,11
 
#define NAME "devmap"
 
/** Pending lookup structure. */
typedef struct {
link_t link;
char *name; /**< Device name */
ipc_callid_t callid; /**< Call ID waiting for the lookup */
} pending_req_t;
 
LIST_INITIALIZE(devices_list);
LIST_INITIALIZE(drivers_list);
LIST_INITIALIZE(pending_req);
 
/* Locking order:
/* order of locking:
* drivers_list_futex
* devices_list_futex
* (devmap_driver_t *)->devices_futex
70,6 → 63,7
static atomic_t drivers_list_futex = FUTEX_INITIALIZER;
static atomic_t create_handle_futex = FUTEX_INITIALIZER;
 
 
static int devmap_create_handle(void)
{
static int last_handle = 0;
76,9 → 70,7
int handle;
/* TODO: allow reusing old handles after their unregistration
* and implement some version of LRU algorithm
*/
and implement some version of LRU algorithm */
/* FIXME: overflow */
futex_down(&create_handle_futex);
107,13 → 99,17
*/
static devmap_device_t *devmap_device_find_name(const char *name)
{
link_t *item = devices_list.next;
link_t *item;
devmap_device_t *device = NULL;
item = devices_list.next;
 
while (item != &devices_list) {
 
device = list_get_instance(item, devmap_device_t, devices);
if (0 == str_cmp(device->name, name))
if (0 == strcmp(device->name, name)) {
break;
}
item = item->next;
}
125,21 → 121,23
}
 
/** Find device with given handle.
*
* @todo: use hash table
*
*/
static devmap_device_t *devmap_device_find_handle(int handle)
{
link_t *item;
devmap_device_t *device = NULL;
futex_down(&devices_list_futex);
link_t *item = (&devices_list)->next;
devmap_device_t *device = NULL;
item = (&devices_list)->next;
while (item != &devices_list) {
 
device = list_get_instance(item, devmap_device_t, devices);
if (device->handle == handle)
if (device->handle == handle) {
break;
}
item = item->next;
}
156,13 → 154,12
}
 
/**
*
* 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)
{
 
list_remove(&(device->devices));
list_remove(&(device->driver_devices));
169,21 → 166,26
free(device->name);
free(device);
 
return EOK;
}
 
/**
*
* Read info about new driver and add it into linked list of registered
* drivers.
*
*/
static void devmap_driver_register(devmap_driver_t **odriver)
{
size_t name_size;
ipc_callid_t callid;
ipc_call_t call;
devmap_driver_t *driver;
ipc_callid_t iid;
ipc_call_t icall;
 
*odriver = NULL;
ipc_call_t icall;
ipc_callid_t iid = async_get_call(&icall);
iid = async_get_call(&icall);
if (IPC_GET_METHOD(icall) != DEVMAP_DRIVER_REGISTER) {
ipc_answer_0(iid, EREFUSED);
190,9 → 192,8
return;
}
devmap_driver_t *driver = (devmap_driver_t *) malloc(sizeof(devmap_driver_t));
if (driver == NULL) {
if (NULL ==
(driver = (devmap_driver_t *)malloc(sizeof(devmap_driver_t)))) {
ipc_answer_0(iid, ENOMEM);
return;
}
200,8 → 201,6
/*
* Get driver name
*/
ipc_callid_t callid;
size_t name_size;
if (!ipc_data_write_receive(&callid, &name_size)) {
free(driver);
ipc_answer_0(callid, EREFUSED);
219,8 → 218,7
/*
* Allocate buffer for device name.
*/
driver->name = (char *) malloc(name_size + 1);
if (driver->name == NULL) {
if (NULL == (driver->name = (char *)malloc(name_size + 1))) {
free(driver);
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(iid, EREFUSED);
250,7 → 248,6
/*
* Create connection to the driver
*/
ipc_call_t call;
callid = async_get_call(&call);
if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) {
285,14 → 282,14
*odriver = driver;
}
 
/**
* Unregister device driver, unregister all its devices and free driver
/** Unregister device driver, unregister all its devices and free driver
* structure.
*
*/
static int devmap_driver_unregister(devmap_driver_t *driver)
{
if (driver == NULL)
devmap_device_t *device;
 
if (NULL == driver)
return EEXISTS;
futex_down(&drivers_list_futex);
308,7 → 305,7
futex_down(&(driver->devices_futex));
while (!list_empty(&(driver->devices))) {
devmap_device_t *device = list_get_instance(driver->devices.next,
device = list_get_instance(driver->devices.next,
devmap_device_t, driver_devices);
devmap_device_unregister_core(device);
}
318,8 → 315,9
futex_up(&drivers_list_futex);
/* free name and driver */
if (NULL != driver->name)
if (NULL != driver->name) {
free(driver->name);
}
free(driver);
327,29 → 325,6
}
 
 
/** 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
*
*/
356,21 → 331,23
static void devmap_device_register(ipc_callid_t iid, ipc_call_t *icall,
devmap_driver_t *driver)
{
if (driver == NULL) {
ipc_callid_t callid;
size_t size;
devmap_device_t *device;
 
if (NULL == driver) {
ipc_answer_0(iid, EREFUSED);
return;
}
/* Create new device entry */
devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
if (device == NULL) {
if (NULL ==
(device = (devmap_device_t *) malloc(sizeof(devmap_device_t)))) {
ipc_answer_0(iid, ENOMEM);
return;
}
/* Get device name */
ipc_callid_t callid;
size_t size;
if (!ipc_data_write_receive(&callid, &size)) {
free(device);
ipc_answer_0(iid, EREFUSED);
387,7 → 364,7
/* +1 for terminating \0 */
device->name = (char *) malloc(size + 1);
if (device->name == NULL) {
if (NULL == device->name) {
free(device);
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(iid, EREFUSED);
429,8 → 406,6
futex_up(&devices_list_futex);
ipc_answer_1(iid, EOK, device->handle);
process_pending_lookup();
}
 
/**
440,22 → 415,24
devmap_driver_t *driver)
{
/* TODO */
 
return EOK;
}
 
/** Connect client to the device.
*
* Find device driver owning requested device and forward
* the message to it.
*
*/
static void devmap_forward(ipc_callid_t callid, ipc_call_t *call)
{
devmap_device_t *dev;
int handle;
 
/*
* Get handle from request
*/
int handle = IPC_GET_ARG2(*call);
devmap_device_t *dev = devmap_device_find_handle(handle);
handle = IPC_GET_ARG2(*call);
dev = devmap_device_find_handle(handle);
if (NULL == dev) {
ipc_answer_0(callid, ENOENT);
467,26 → 444,28
}
 
/** Find handle for device instance identified by name.
*
* In answer will be send EOK and device handle in arg1 or a error
* code from errno.h.
*
*/
static void devmap_get_handle(ipc_callid_t iid, ipc_call_t *icall)
{
char *name = NULL;
size_t name_size;
const devmap_device_t *dev;
ipc_callid_t callid;
ipcarg_t retval;
/*
* Wait for incoming message with device name (but do not
* read the name itself until the buffer is allocated).
*/
ipc_callid_t callid;
size_t size;
if (!ipc_data_write_receive(&callid, &size)) {
if (!ipc_data_write_receive(&callid, &name_size)) {
ipc_answer_0(callid, EREFUSED);
ipc_answer_0(iid, EREFUSED);
return;
}
if ((size < 1) || (size > DEVMAP_NAME_MAXLEN)) {
if (name_size > DEVMAP_NAME_MAXLEN) {
ipc_answer_0(callid, EINVAL);
ipc_answer_0(iid, EREFUSED);
return;
495,8 → 474,7
/*
* Allocate buffer for device name.
*/
char *name = (char *) malloc(size);
if (name == NULL) {
if (NULL == (name = (char *)malloc(name_size))) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(iid, EREFUSED);
return;
505,45 → 483,26
/*
* Send confirmation to sender and get data into buffer.
*/
ipcarg_t retval = ipc_data_write_finalize(callid, name, size);
if (retval != EOK) {
if (EOK != (retval = ipc_data_write_finalize(callid, name,
name_size))) {
ipc_answer_0(iid, EREFUSED);
free(name);
return;
}
name[size] = '\0';
/*
* Find device name in linked list of known devices.
*/
const devmap_device_t *dev = devmap_device_find_name(name);
dev = devmap_device_find_name(name);
/*
* Device was not found.
*/
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;
}
if (NULL == dev) {
ipc_answer_0(iid, ENOENT);
free(name);
return;
}
ipc_answer_1(iid, EOK, dev->handle);
free(name);
}
 
/** Find name of device identified by id and send it to caller.
551,12 → 510,15
*/
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));
const devmap_device_t *device;
size_t name_size;
device = devmap_device_find_handle(IPC_GET_ARG1(*icall));
 
/*
* Device not found.
*/
if (device == NULL) {
if (NULL == device) {
ipc_answer_0(iid, ENOENT);
return;
}
563,19 → 525,19
ipc_answer_0(iid, EOK);
size_t name_size = str_size(device->name);
name_size = strlen(device->name);
 
/* FIXME:
* We have no channel from DEVMAP to client, therefore
* sending must be initiated by client.
*
* int rc = ipc_data_write_send(phone, device->name, name_size);
* if (rc != EOK) {
* async_wait_for(req, NULL);
* return rc;
* }
we have no channel from DEVMAP to client ->
sending must be initiated by client
 
int rc = ipc_data_write_send(phone, device->name, name_size);
if (rc != EOK) {
async_wait_for(req, NULL);
return rc;
}
*/
/* TODO: send name in response */
}
 
584,30 → 546,31
*/
static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
{
/* Accept connection */
ipc_callid_t callid;
ipc_call_t call;
bool cont = true;
devmap_driver_t *driver = NULL;
 
ipc_answer_0(iid, EOK);
devmap_driver_t *driver = NULL;
devmap_driver_register(&driver);
if (NULL == driver)
return;
bool cont = true;
while (cont) {
ipc_call_t call;
ipc_callid_t callid = async_get_call(&call);
callid = async_get_call(&call);
switch (IPC_GET_METHOD(call)) {
case IPC_M_PHONE_HUNGUP:
cont = false;
/* Exit thread */
continue;
continue; /* Exit thread */
case DEVMAP_DRIVER_UNREGISTER:
if (NULL == driver)
if (NULL == driver) {
ipc_answer_0(callid, ENOENT);
else
} else {
ipc_answer_0(callid, EOK);
}
break;
case DEVMAP_DEVICE_REGISTER:
/* Register one instance of device */
624,10 → 587,11
devmap_get_handle(callid, &call);
break;
default:
if (!(callid & IPC_CALLID_NOTIFICATION))
if (!(callid & IPC_CALLID_NOTIFICATION)) {
ipc_answer_0(callid, ENOENT);
}
}
}
if (NULL != driver) {
/*
636,6 → 600,7
devmap_driver_unregister(driver);
driver = NULL;
}
}
 
/** Handle connection with device client.
643,21 → 608,23
*/
static void devmap_connection_client(ipc_callid_t iid, ipc_call_t *icall)
{
/* Accept connection */
ipc_answer_0(iid, EOK);
ipc_callid_t callid;
ipc_call_t call;
bool cont = true;
bool cont = true;
ipc_answer_0(iid, EOK); /* Accept connection */
 
while (cont) {
ipc_call_t call;
ipc_callid_t callid = async_get_call(&call);
callid = async_get_call(&call);
switch (IPC_GET_METHOD(call)) {
case IPC_M_PHONE_HUNGUP:
cont = false;
/* Exit thread */
continue;
continue; /* Exit thread */
 
case DEVMAP_DEVICE_GET_HANDLE:
devmap_get_handle(callid, &call);
 
break;
case DEVMAP_DEVICE_GET_NAME:
/* TODO */
664,11 → 631,12
devmap_get_name(callid, &call);
break;
default:
if (!(callid & IPC_CALLID_NOTIFICATION))
if (!(callid & IPC_CALLID_NOTIFICATION)) {
ipc_answer_0(callid, ENOENT);
}
}
}
}
 
/** Function for handling connections to devmap
*
688,9 → 656,10
devmap_forward(iid, icall);
break;
default:
/* No such interface */
ipc_answer_0(iid, ENOENT);
ipc_answer_0(iid, ENOENT); /* No such interface */
}
 
/* Cleanup */
}
 
/**
700,6 → 669,8
{
printf(NAME ": HelenOS Device Mapper\n");
ipcarg_t phonead;
 
if (devmap_init() != 0) {
printf(NAME ": Error while initializing service\n");
return -1;
709,13 → 680,11
async_set_client_connection(devmap_connection);
/* Register device mapper at naming service */
ipcarg_t phonead;
if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0)
return -1;
printf(NAME ": Accepting connections\n");
async_manager();
/* Never reached */
return 0;
}