diff --git a/src/add-ons/kernel/drivers/audio/ac97/ichaudio/ichaudio.c b/src/add-ons/kernel/drivers/audio/ac97/ichaudio/ichaudio.c index ce2eea1c71..91cce758c1 100644 --- a/src/add-ons/kernel/drivers/audio/ac97/ichaudio/ichaudio.c +++ b/src/add-ons/kernel/drivers/audio/ac97/ichaudio/ichaudio.c @@ -43,10 +43,7 @@ ichaudio_attach(drv_t *drv, void **_cookie) if (!cookie) return B_ERROR; *_cookie = cookie; - if (get_module(B_PCI_MODULE_NAME,(module_info **)&cookie->pci) < 0) { - return B_ERROR; - } - + dprintf("ichaudio_attach\n"); return B_OK; err: @@ -60,6 +57,8 @@ ichaudio_detach(drv_t *drv, void *_cookie) { ichaudio_cookie *cookie = (ichaudio_cookie *)_cookie; + dprintf("ichaudio_detach\n"); + free(cookie); return B_OK; } diff --git a/src/add-ons/kernel/drivers/audio/ac97/ichaudio/lala/driver.c b/src/add-ons/kernel/drivers/audio/ac97/ichaudio/lala/driver.c index 7a14aa15be..55d1a9d9e0 100644 --- a/src/add-ons/kernel/drivers/audio/ac97/ichaudio/lala/driver.c +++ b/src/add-ons/kernel/drivers/audio/ac97/ichaudio/lala/driver.c @@ -13,6 +13,7 @@ int32 api_version = B_CUR_DRIVER_API_VERSION; #define MAX_DEVICES 8 +sem_id drv_sem; char * drv_path[MAX_DEVICES + 1]; drv_t * drv_data[MAX_DEVICES]; int drv_count; @@ -43,6 +44,8 @@ init_driver(void) return B_ERROR; } + drv_sem = create_sem(1, "drv_sem"); + drv_count = 0; for (pciindex = 0; B_OK == pcimodule->get_nth_pci_info(pciindex, pciinfo); pciindex++) { @@ -80,6 +83,8 @@ init_driver(void) drv_data[drv_count]->function = pciinfo->function; drv_data[drv_count]->name = driver_info.table[devindex].name; drv_data[drv_count]->flags = driver_info.table[devindex].flags; + drv_data[drv_count]->open_count = 0; + drv_data[drv_count]->cookie = 0; drv_count++; break; @@ -103,13 +108,38 @@ uninit_driver(void) free(drv_path[i]); free(drv_data[i]); } + delete_sem(drv_sem); } static status_t ich_open(const char *name, uint32 flags, void** cookie) { - dprintf("open\n"); - return B_OK; + int index; + status_t res; + + acquire_sem(drv_sem); + + for (index = 0; index < drv_count; index++) { + if (0 == strcmp(drv_path[index], name)) + break; + } + if (index == drv_count) { // name not found + release_sem(drv_sem); + return B_ERROR; + } + *cookie = (void *) index; + + if (drv_data[drv_count]->open_count == 0) { + res = driver_info.attach(drv_data[index], &drv_data[index]->cookie); + drv_data[drv_count]->open_count = (res == B_OK) ? 1 : 0; + } else { + res = B_OK; + drv_data[drv_count]->open_count++; + } + + release_sem(drv_sem); + + return res; } static status_t @@ -122,8 +152,25 @@ ich_close(void* cookie) static status_t ich_free(void* cookie) { + int index; + status_t res; + dprintf("free\n"); - return B_OK; + + index = (int) cookie; + + acquire_sem(drv_sem); + + drv_data[drv_count]->open_count--; + + if (drv_data[drv_count]->open_count == 0) + res = driver_info.detach(drv_data[index], drv_data[index]->cookie); + else + res = B_OK; + + release_sem(drv_sem); + + return res; } static status_t diff --git a/src/add-ons/kernel/drivers/audio/ac97/ichaudio/lala/lala.h b/src/add-ons/kernel/drivers/audio/ac97/ichaudio/lala/lala.h index 1ec4708e53..ecdc69f7cc 100644 --- a/src/add-ons/kernel/drivers/audio/ac97/ichaudio/lala/lala.h +++ b/src/add-ons/kernel/drivers/audio/ac97/ichaudio/lala/lala.h @@ -15,6 +15,9 @@ typedef struct const char * name; uint32 flags; +// private: + int32 open_count; + void * cookie; } drv_t; typedef void stream_id;