Subversion Repositories HelenOS

Rev

Rev 4420 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4420 Rev 4537
Line 39... Line 39...
39
#include <ipc/ns.h>
39
#include <ipc/ns.h>
40
#include <async.h>
40
#include <async.h>
41
#include <stdio.h>
41
#include <stdio.h>
42
#include <errno.h>
42
#include <errno.h>
43
#include <bool.h>
43
#include <bool.h>
44
#include <futex.h>
44
#include <fibril_sync.h>
45
#include <stdlib.h>
45
#include <stdlib.h>
46
#include <string.h>
46
#include <string.h>
47
#include <ipc/devmap.h>
47
#include <ipc/devmap.h>
48
 
48
 
49
#define NAME  "devmap"
49
#define NAME  "devmap"
Line 60... Line 60...
60
    link_t devices;
60
    link_t devices;
61
    /** Phone asociated with this driver */
61
    /** Phone asociated with this driver */
62
    ipcarg_t phone;
62
    ipcarg_t phone;
63
    /** Device driver name */
63
    /** Device driver name */
64
    char *name;
64
    char *name;
65
    /** Futex for list of devices owned by this driver */
65
    /** Fibril mutex for list of devices owned by this driver */
66
    atomic_t devices_futex;
66
    fibril_mutex_t devices_mutex;
67
} devmap_driver_t;
67
} devmap_driver_t;
68
 
68
 
69
/** Info about registered device
69
/** Info about registered device
70
 *
70
 *
71
 */
71
 */
Line 93... Line 93...
93
LIST_INITIALIZE(devices_list);
93
LIST_INITIALIZE(devices_list);
94
LIST_INITIALIZE(drivers_list);
94
LIST_INITIALIZE(drivers_list);
95
LIST_INITIALIZE(pending_req);
95
LIST_INITIALIZE(pending_req);
96
 
96
 
97
/* Locking order:
97
/* Locking order:
98
 *  drivers_list_futex
98
 *  drivers_list_mutex
99
 *  devices_list_futex
99
 *  devices_list_mutex
100
 *  (devmap_driver_t *)->devices_futex
100
 *  (devmap_driver_t *)->devices_mutex
101
 *  create_handle_futex
101
 *  create_handle_mutex
102
 **/
102
 **/
103
 
103
 
104
static atomic_t devices_list_futex = FUTEX_INITIALIZER;
104
static FIBRIL_MUTEX_INITIALIZE(devices_list_mutex);
105
static atomic_t drivers_list_futex = FUTEX_INITIALIZER;
105
static FIBRIL_MUTEX_INITIALIZE(drivers_list_mutex);
106
static atomic_t create_handle_futex = FUTEX_INITIALIZER;
106
static FIBRIL_MUTEX_INITIALIZE(create_handle_mutex);
107
 
107
 
108
static dev_handle_t last_handle = 0;
108
static dev_handle_t last_handle = 0;
109
 
109
 
110
static dev_handle_t devmap_create_handle(void)
110
static dev_handle_t devmap_create_handle(void)
111
{
111
{
112
    /* TODO: allow reusing old handles after their unregistration
112
    /* TODO: allow reusing old handles after their unregistration
113
     * and implement some version of LRU algorithm
113
     * and implement some version of LRU algorithm, avoid overflow
114
     */
114
     */
115
   
115
   
116
    /* FIXME: overflow */
-
 
117
    futex_down(&create_handle_futex);
116
    fibril_mutex_lock(&create_handle_mutex);
118
    last_handle++;
117
    last_handle++;
119
    futex_up(&create_handle_futex);
118
    fibril_mutex_unlock(&create_handle_mutex);
120
   
119
   
121
    return last_handle;
120
    return last_handle;
122
}
121
}
123
 
122
 
124
/** Find device with given name.
123
/** Find device with given name.
Line 129... Line 128...
129
    link_t *item = devices_list.next;
128
    link_t *item = devices_list.next;
130
    devmap_device_t *device = NULL;
129
    devmap_device_t *device = NULL;
131
   
130
   
132
    while (item != &devices_list) {
131
    while (item != &devices_list) {
133
        device = list_get_instance(item, devmap_device_t, devices);
132
        device = list_get_instance(item, devmap_device_t, devices);
134
        if (0 == str_cmp(device->name, name))
133
        if (str_cmp(device->name, name) == 0)
135
            break;
134
            break;
136
        item = item->next;
135
        item = item->next;
137
    }
136
    }
138
   
137
   
139
    if (item == &devices_list)
138
    if (item == &devices_list)
Line 148... Line 147...
148
 * @todo: use hash table
147
 * @todo: use hash table
149
 *
148
 *
150
 */
149
 */
151
static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
150
static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
152
{
151
{
153
    futex_down(&devices_list_futex);
152
    fibril_mutex_lock(&devices_list_mutex);
154
   
153
   
155
    link_t *item = (&devices_list)->next;
154
    link_t *item = (&devices_list)->next;
156
    devmap_device_t *device = NULL;
155
    devmap_device_t *device = NULL;
157
   
156
   
158
    while (item != &devices_list) {
157
    while (item != &devices_list) {
Line 161... Line 160...
161
            break;
160
            break;
162
        item = item->next;
161
        item = item->next;
163
    }
162
    }
164
   
163
   
165
    if (item == &devices_list) {
164
    if (item == &devices_list) {
166
        futex_up(&devices_list_futex);
165
        fibril_mutex_unlock(&devices_list_mutex);
167
        return NULL;
166
        return NULL;
168
    }
167
    }
169
   
168
   
170
    device = list_get_instance(item, devmap_device_t, devices);
169
    device = list_get_instance(item, devmap_device_t, devices);
171
   
170
   
172
    futex_up(&devices_list_futex);
171
    fibril_mutex_unlock(&devices_list_mutex);
173
   
172
   
174
    return device;
173
    return device;
175
}
174
}
176
 
175
 
177
/**
176
/**
Line 247... Line 246...
247
    }
246
    }
248
   
247
   
249
    /*
248
    /*
250
     * Send confirmation to sender and get data into buffer.
249
     * Send confirmation to sender and get data into buffer.
251
     */
250
     */
252
    if (EOK != ipc_data_write_finalize(callid, driver->name, name_size)) {
251
    if (ipc_data_write_finalize(callid, driver->name, name_size) != EOK) {
253
        free(driver->name);
252
        free(driver->name);
254
        free(driver);
253
        free(driver);
255
        ipc_answer_0(iid, EREFUSED);
254
        ipc_answer_0(iid, EREFUSED);
256
        return;
255
        return;
257
    }
256
    }
258
   
257
   
259
    driver->name[name_size] = 0;
258
    driver->name[name_size] = 0;
260
   
259
   
261
    /* Initialize futex for list of devices owned by this driver */
260
    /* Initialize mutex for list of devices owned by this driver */
262
    futex_initialize(&(driver->devices_futex), 1);
261
    fibril_mutex_initialize(&driver->devices_mutex);
263
   
262
   
264
    /*
263
    /*
265
     * Initialize list of asociated devices
264
     * Initialize list of asociated devices
266
     */
265
     */
267
    list_initialize(&(driver->devices));
266
    list_initialize(&driver->devices);
268
   
267
   
269
    /*
268
    /*
270
     * Create connection to the driver
269
     * Create connection to the driver
271
     */
270
     */
272
    ipc_call_t call;
271
    ipc_call_t call;
273
    callid = async_get_call(&call);
272
    callid = async_get_call(&call);
274
   
273
   
275
    if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) {
274
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
276
        ipc_answer_0(callid, ENOTSUP);
275
        ipc_answer_0(callid, ENOTSUP);
277
       
276
       
278
        free(driver->name);
277
        free(driver->name);
279
        free(driver);
278
        free(driver);
280
        ipc_answer_0(iid, ENOTSUP);
279
        ipc_answer_0(iid, ENOTSUP);
Line 285... Line 284...
285
   
284
   
286
    ipc_answer_0(callid, EOK);
285
    ipc_answer_0(callid, EOK);
287
   
286
   
288
    list_initialize(&(driver->drivers));
287
    list_initialize(&(driver->drivers));
289
   
288
   
290
    futex_down(&drivers_list_futex);
289
    fibril_mutex_lock(&drivers_list_mutex);
291
   
290
   
292
    /* TODO:
291
    /* TODO:
293
     * check that no driver with name equal to driver->name is registered
292
     * check that no driver with name equal to driver->name is registered
294
     */
293
     */
295
   
294
   
296
    /*
295
    /*
297
     * Insert new driver into list of registered drivers
296
     * Insert new driver into list of registered drivers
298
     */
297
     */
299
    list_append(&(driver->drivers), &drivers_list);
298
    list_append(&(driver->drivers), &drivers_list);
300
    futex_up(&drivers_list_futex);
299
    fibril_mutex_unlock(&drivers_list_mutex);
301
   
300
   
302
    ipc_answer_0(iid, EOK);
301
    ipc_answer_0(iid, EOK);
303
   
302
   
304
    *odriver = driver;
303
    *odriver = driver;
305
}
304
}
Line 312... Line 311...
312
static int devmap_driver_unregister(devmap_driver_t *driver)
311
static int devmap_driver_unregister(devmap_driver_t *driver)
313
{
312
{
314
    if (driver == NULL)
313
    if (driver == NULL)
315
        return EEXISTS;
314
        return EEXISTS;
316
   
315
   
317
    futex_down(&drivers_list_futex);
316
    fibril_mutex_lock(&drivers_list_mutex);
318
   
317
   
319
    if (driver->phone != 0)
318
    if (driver->phone != 0)
320
        ipc_hangup(driver->phone);
319
        ipc_hangup(driver->phone);
321
   
320
   
322
    /* Remove it from list of drivers */
321
    /* Remove it from list of drivers */
323
    list_remove(&(driver->drivers));
322
    list_remove(&(driver->drivers));
324
   
323
   
325
    /* Unregister all its devices */
324
    /* Unregister all its devices */
326
    futex_down(&devices_list_futex);
325
    fibril_mutex_lock(&devices_list_mutex);
327
    futex_down(&(driver->devices_futex));
326
    fibril_mutex_lock(&driver->devices_mutex);
328
   
327
   
329
    while (!list_empty(&(driver->devices))) {
328
    while (!list_empty(&(driver->devices))) {
330
        devmap_device_t *device = list_get_instance(driver->devices.next,
329
        devmap_device_t *device = list_get_instance(driver->devices.next,
331
            devmap_device_t, driver_devices);
330
            devmap_device_t, driver_devices);
332
        devmap_device_unregister_core(device);
331
        devmap_device_unregister_core(device);
333
    }
332
    }
334
   
333
   
335
    futex_up(&(driver->devices_futex));
334
    fibril_mutex_unlock(&driver->devices_mutex);
336
    futex_up(&devices_list_futex);
335
    fibril_mutex_unlock(&devices_list_mutex);
337
    futex_up(&drivers_list_futex);
336
    fibril_mutex_unlock(&drivers_list_mutex);
338
   
337
   
339
    /* free name and driver */
338
    /* free name and driver */
340
    if (driver->name != NULL)
339
    if (driver->name != NULL)
341
        free(driver->name);
340
        free(driver->name);
342
   
341
   
Line 345... Line 344...
345
    return EOK;
344
    return EOK;
346
}
345
}
347
 
346
 
348
 
347
 
349
/** Process pending lookup requests */
348
/** Process pending lookup requests */
350
static void process_pending_lookup()
349
static void process_pending_lookup(void)
351
{
350
{
352
    link_t *cur;
351
    link_t *cur;
353
   
352
   
354
loop:
353
loop:
355
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
354
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
Line 362... Line 361...
362
        ipc_answer_1(pr->callid, EOK, dev->handle);
361
        ipc_answer_1(pr->callid, EOK, dev->handle);
363
       
362
       
364
        free(pr->name);
363
        free(pr->name);
365
        list_remove(cur);
364
        list_remove(cur);
366
        free(pr);
365
        free(pr);
-
 
366
       
367
        goto loop;
367
        goto loop;
368
    }
368
    }
369
}
369
}
370
 
370
 
371
 
371
 
Line 417... Line 417...
417
    device->name[size] = 0;
417
    device->name[size] = 0;
418
   
418
   
419
    list_initialize(&(device->devices));
419
    list_initialize(&(device->devices));
420
    list_initialize(&(device->driver_devices));
420
    list_initialize(&(device->driver_devices));
421
   
421
   
422
    futex_down(&devices_list_futex);
422
    fibril_mutex_lock(&devices_list_mutex);
423
   
423
   
424
    /* Check that device with such name is not already registered */
424
    /* Check that device with such name is not already registered */
425
    if (NULL != devmap_device_find_name(device->name)) {
425
    if (NULL != devmap_device_find_name(device->name)) {
426
        printf(NAME ": Device '%s' already registered\n", device->name);
426
        printf(NAME ": Device '%s' already registered\n", device->name);
427
        futex_up(&devices_list_futex); 
427
        fibril_mutex_unlock(&devices_list_mutex);
428
        free(device->name);
428
        free(device->name);
429
        free(device);
429
        free(device);
430
        ipc_answer_0(iid, EEXISTS);
430
        ipc_answer_0(iid, EEXISTS);
431
        return;
431
        return;
432
    }
432
    }
Line 438... Line 438...
438
   
438
   
439
    /* Insert device into list of all devices  */
439
    /* Insert device into list of all devices  */
440
    list_append(&device->devices, &devices_list);
440
    list_append(&device->devices, &devices_list);
441
   
441
   
442
    /* Insert device into list of devices that belog to one driver */
442
    /* Insert device into list of devices that belog to one driver */
443
    futex_down(&device->driver->devices_futex);
443
    fibril_mutex_lock(&device->driver->devices_mutex);
444
   
444
   
445
    list_append(&device->driver_devices, &device->driver->devices);
445
    list_append(&device->driver_devices, &device->driver->devices);
446
   
446
   
447
    futex_up(&device->driver->devices_futex);
447
    fibril_mutex_unlock(&device->driver->devices_mutex);
448
    futex_up(&devices_list_futex);
448
    fibril_mutex_unlock(&devices_list_mutex);
449
   
449
   
450
    ipc_answer_1(iid, EOK, device->handle);
450
    ipc_answer_1(iid, EOK, device->handle);
451
   
-
 
452
    process_pending_lookup();
-
 
453
}
451
}
454
 
452
 
455
/**
453
/**
456
 *
454
 *
457
 */
455
 */
Line 598... Line 596...
598
    /* TODO: send name in response */
596
    /* TODO: send name in response */
599
}
597
}
600
 
598
 
601
static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
599
static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
602
{
600
{
603
    futex_down(&devices_list_futex);
601
    fibril_mutex_lock(&devices_list_mutex);
604
    ipc_answer_1(iid, EOK, list_count(&devices_list));
602
    ipc_answer_1(iid, EOK, list_count(&devices_list));
605
    futex_up(&devices_list_futex);
603
    fibril_mutex_unlock(&devices_list_mutex);
606
}
604
}
607
 
605
 
608
static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
606
static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
609
{
607
{
610
    futex_down(&devices_list_futex);
608
    fibril_mutex_lock(&devices_list_mutex);
611
   
609
   
612
    ipc_callid_t callid;
610
    ipc_callid_t callid;
613
    size_t size;
611
    size_t size;
614
    if (!ipc_data_read_receive(&callid, &size)) {
612
    if (!ipc_data_read_receive(&callid, &size)) {
615
        ipc_answer_0(callid, EREFUSED);
613
        ipc_answer_0(callid, EREFUSED);
Line 621... Line 619...
621
        ipc_answer_0(callid, EINVAL);
619
        ipc_answer_0(callid, EINVAL);
622
        ipc_answer_0(iid, EREFUSED);
620
        ipc_answer_0(iid, EREFUSED);
623
        return;
621
        return;
624
    }
622
    }
625
   
623
   
626
    count_t count = size / sizeof(dev_desc_t);
624
    size_t count = size / sizeof(dev_desc_t);
627
    dev_desc_t *desc = (dev_desc_t *) malloc(size);
625
    dev_desc_t *desc = (dev_desc_t *) malloc(size);
628
    if (desc == NULL) {
626
    if (desc == NULL) {
629
        ipc_answer_0(callid, ENOMEM);
627
        ipc_answer_0(callid, ENOMEM);
630
        ipc_answer_0(iid, EREFUSED);
628
        ipc_answer_0(iid, EREFUSED);
631
        return;
629
        return;
632
    }
630
    }
633
   
631
   
634
    count_t pos = 0;
632
    size_t pos = 0;
635
    link_t *item = devices_list.next;
633
    link_t *item = devices_list.next;
636
   
634
   
637
    while ((item != &devices_list) && (pos < count)) {
635
    while ((item != &devices_list) && (pos < count)) {
638
        devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
636
        devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
639
       
637
       
Line 650... Line 648...
650
        return;
648
        return;
651
    }
649
    }
652
   
650
   
653
    free(desc);
651
    free(desc);
654
   
652
   
655
    futex_up(&devices_list_futex);
653
    fibril_mutex_unlock(&devices_list_mutex);
656
   
654
   
657
    ipc_answer_1(iid, EOK, pos);
655
    ipc_answer_1(iid, EOK, pos);
658
}
656
}
659
 
657
 
660
/** Initialize device mapper.
658
/** Initialize device mapper.
Line 675... Line 673...
675
    }
673
    }
676
   
674
   
677
    list_initialize(&(device->devices));
675
    list_initialize(&(device->devices));
678
    list_initialize(&(device->driver_devices));
676
    list_initialize(&(device->driver_devices));
679
   
677
   
680
    futex_down(&devices_list_futex);
678
    fibril_mutex_lock(&devices_list_mutex);
681
   
679
   
682
    /* Get unique device handle */
680
    /* Get unique device handle */
683
    device->handle = devmap_create_handle();
681
    device->handle = devmap_create_handle();
684
    device->driver = NULL;
682
    device->driver = NULL;
685
   
683
   
686
    /* Insert device into list of all devices  */
684
    /* Insert device into list of all devices  */
687
    list_append(&device->devices, &devices_list);
685
    list_append(&device->devices, &devices_list);
688
   
686
   
689
    futex_up(&devices_list_futex);
687
    fibril_mutex_unlock(&devices_list_mutex);
690
   
688
   
691
    return true;
689
    return true;
692
}
690
}
693
 
691
 
694
/** Handle connection with device driver.
692
/** Handle connection with device driver.
Line 697... Line 695...
697
static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
695
static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
698
{
696
{
699
    /* Accept connection */
697
    /* Accept connection */
700
    ipc_answer_0(iid, EOK);
698
    ipc_answer_0(iid, EOK);
701
   
699
   
702
    devmap_driver_t *driver = NULL;
700
    devmap_driver_t *driver = NULL;
703
    devmap_driver_register(&driver);
701
    devmap_driver_register(&driver);
704
   
702
   
705
    if (NULL == driver)
703
    if (NULL == driver)
706
        return;
704
        return;
707
   
705
   
Line 711... Line 709...
711
        ipc_callid_t callid = async_get_call(&call);
709
        ipc_callid_t callid = async_get_call(&call);
712
       
710
       
713
        switch (IPC_GET_METHOD(call)) {
711
        switch (IPC_GET_METHOD(call)) {
714
        case IPC_M_PHONE_HUNGUP:
712
        case IPC_M_PHONE_HUNGUP:
715
            cont = false;
713
            cont = false;
716
            /* Exit thread */
-
 
717
            continue;
714
            continue;
718
        case DEVMAP_DRIVER_UNREGISTER:
715
        case DEVMAP_DRIVER_UNREGISTER:
719
            if (NULL == driver)
716
            if (NULL == driver)
720
                ipc_answer_0(callid, ENOENT);
717
                ipc_answer_0(callid, ENOENT);
721
            else
718
            else
Line 764... Line 761...
764
        ipc_callid_t callid = async_get_call(&call);
761
        ipc_callid_t callid = async_get_call(&call);
765
       
762
       
766
        switch (IPC_GET_METHOD(call)) {
763
        switch (IPC_GET_METHOD(call)) {
767
        case IPC_M_PHONE_HUNGUP:
764
        case IPC_M_PHONE_HUNGUP:
768
            cont = false;
765
            cont = false;
769
            /* Exit thread */
-
 
770
            continue;
766
            continue;
771
        case DEVMAP_DEVICE_GET_HANDLE:
767
        case DEVMAP_DEVICE_GET_HANDLE:
772
            devmap_get_handle(callid, &call);
768
            devmap_get_handle(callid, &call);
773
            break;
769
            break;
774
        case DEVMAP_DEVICE_GET_NAME:
770
        case DEVMAP_DEVICE_GET_NAME:
Line 804... Line 800...
804
        /* Connect client to selected device */
800
        /* Connect client to selected device */
805
        devmap_forward(iid, icall);
801
        devmap_forward(iid, icall);
806
        break;
802
        break;
807
    default:
803
    default:
808
        /* No such interface */
804
        /* No such interface */
809
        ipc_answer_0(iid, ENOENT);
805
        ipc_answer_0(iid, ENOENT);
810
    }
806
    }
811
}
807
}
812
 
808
 
813
/**
809
/**
814
 *
810
 *
Line 820... Line 816...
820
    if (!devmap_init()) {
816
    if (!devmap_init()) {
821
        printf(NAME ": Error while initializing service\n");
817
        printf(NAME ": Error while initializing service\n");
822
        return -1;
818
        return -1;
823
    }
819
    }
824
   
820
   
825
    /* Set a handler of incomming connections */
821
    /* Set a handler of incomming connections and
-
 
822
       pending operations */
-
 
823
    async_set_pending(process_pending_lookup);
826
    async_set_client_connection(devmap_connection);
824
    async_set_client_connection(devmap_connection);
827
   
825
   
828
    /* Register device mapper at naming service */
826
    /* Register device mapper at naming service */
829
    ipcarg_t phonead;
827
    ipcarg_t phonead;
830
    if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0)
828
    if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0)