41,7 → 41,9 |
#include <malloc.h> |
#include <string.h> |
#include <libfs.h> |
#include <fibril_sync.h> |
#include <adt/hash_table.h> |
#include <sys/stat.h> |
#include "devfs.h" |
#include "devfs_ops.h" |
|
58,6 → 60,9 |
/** Hash table of opened devices */ |
static hash_table_t devices; |
|
/** Hash table mutex */ |
static FIBRIL_MUTEX_INITIALIZE(devices_mutex); |
|
#define DEVICES_KEYS 1 |
#define DEVICES_KEY_HANDLE 0 |
#define DEVICES_BUCKETS 256 |
159,12 → 164,12 |
|
if (first >= last) { |
/* Root entry */ |
if (lflag & L_DIRECTORY) |
if (!(lflag & L_FILE)) |
ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0); |
else |
ipc_answer_0(rid, ENOENT); |
} else { |
if (lflag & L_FILE) { |
if (!(lflag & L_DIRECTORY)) { |
size_t len; |
if (last >= first) |
len = last - first + 1; |
195,10 → 200,12 |
[DEVICES_KEY_HANDLE] = (unsigned long) handle |
}; |
|
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
int phone = devmap_device_connect(handle, 0); |
if (phone < 0) { |
fibril_mutex_unlock(&devices_mutex); |
free(name); |
ipc_answer_0(rid, ENOENT); |
return; |
206,6 → 213,7 |
|
device_t *dev = (device_t *) malloc(sizeof(device_t)); |
if (dev == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
free(name); |
ipc_answer_0(rid, ENOMEM); |
return; |
220,6 → 228,7 |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
dev->refcount++; |
} |
fibril_mutex_unlock(&devices_mutex); |
} |
|
free(name); |
238,10 → 247,12 |
[DEVICES_KEY_HANDLE] = (unsigned long) handle |
}; |
|
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
int phone = devmap_device_connect(handle, 0); |
if (phone < 0) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
248,6 → 259,7 |
|
device_t *dev = (device_t *) malloc(sizeof(device_t)); |
if (dev == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
261,28 → 273,49 |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
dev->refcount++; |
} |
fibril_mutex_unlock(&devices_mutex); |
|
ipc_answer_3(rid, EOK, 0, 1, L_FILE); |
} |
|
void devfs_device(ipc_callid_t rid, ipc_call_t *request) |
void devfs_stat(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
|
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_read_receive(&callid, &size) || |
size != sizeof(struct stat)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
|
struct stat stat; |
memset(&stat, 0, sizeof(struct stat)); |
|
stat.fs_handle = devfs_reg.fs_handle; |
stat.dev_handle = dev_handle; |
stat.index = index; |
stat.lnkcnt = 1; |
stat.is_file = (index != 0); |
stat.size = 0; |
|
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
|
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
|
ipc_answer_1(rid, EOK, (ipcarg_t) index); |
} else |
ipc_answer_0(rid, ENOTSUP); |
if (lnk != NULL) |
stat.devfs_stat.device = (dev_handle_t)index; |
fibril_mutex_unlock(&devices_mutex); |
} |
|
ipc_data_read_finalize(callid, &stat, sizeof(struct stat)); |
ipc_answer_0(rid, EOK); |
} |
|
void devfs_read(ipc_callid_t rid, ipc_call_t *request) |
295,8 → 328,10 |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
|
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
305,6 → 340,7 |
|
ipc_callid_t callid; |
if (!ipc_data_read_receive(&callid, NULL)) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
318,6 → 354,7 |
|
/* Forward the IPC_M_DATA_READ request to the driver */ |
ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
fibril_mutex_unlock(&devices_mutex); |
|
/* Wait for reply from the driver. */ |
ipcarg_t rc; |
369,8 → 406,10 |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
|
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
379,6 → 418,7 |
|
ipc_callid_t callid; |
if (!ipc_data_write_receive(&callid, NULL)) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
393,6 → 433,8 |
/* Forward the IPC_M_DATA_WRITE request to the driver */ |
ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
|
fibril_mutex_unlock(&devices_mutex); |
|
/* Wait for reply from the driver. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
420,8 → 462,10 |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
|
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
434,6 → 478,8 |
hash_table_remove(&devices, key, DEVICES_KEYS); |
} |
|
fibril_mutex_unlock(&devices_mutex); |
|
ipc_answer_0(rid, EOK); |
} else |
ipc_answer_0(rid, ENOTSUP); |
448,8 → 494,10 |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
|
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
461,6 → 509,8 |
aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request), |
IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer); |
|
fibril_mutex_unlock(&devices_mutex); |
|
/* Wait for reply from the driver */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |