Subversion Repositories HelenOS

Rev

Rev 4416 | Rev 4524 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4416 Rev 4458
1
/*
1
/*
2
 * Copyright (c) 2007 Josef Cejka
2
 * Copyright (c) 2007 Josef Cejka
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/**
29
/**
30
 * @defgroup devmap Device mapper.
30
 * @defgroup devmap Device mapper.
31
 * @brief HelenOS device mapper.
31
 * @brief HelenOS device mapper.
32
 * @{
32
 * @{
33
 */
33
 */
34
 
34
 
35
/** @file
35
/** @file
36
 */
36
 */
37
 
37
 
38
#include <ipc/services.h>
38
#include <ipc/services.h>
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>
-
 
45
#include <stdlib.h>
44
#include <stdlib.h>
46
#include <string.h>
45
#include <string.h>
47
#include <ipc/devmap.h>
46
#include <ipc/devmap.h>
48
 
47
 
49
#define NAME  "devmap"
48
#define NAME  "devmap"
50
 
49
 
51
/** Representation of device driver.
50
/** Representation of device driver.
52
 *
51
 *
53
 * Each driver is responsible for a set of devices.
52
 * Each driver is responsible for a set of devices.
54
 *
53
 *
55
 */
54
 */
56
typedef struct {
55
typedef struct {
57
    /** Pointers to previous and next drivers in linked list */
56
    /** Pointers to previous and next drivers in linked list */
58
    link_t drivers;
57
    link_t drivers;
59
    /** Pointer to the linked list of devices controlled by this driver */
58
    /** Pointer to the linked list of devices controlled by this driver */
60
    link_t devices;
59
    link_t devices;
61
    /** Phone asociated with this driver */
60
    /** Phone asociated with this driver */
62
    ipcarg_t phone;
61
    ipcarg_t phone;
63
    /** Device driver name */
62
    /** Device driver name */
64
    char *name;
63
    char *name;
65
    /** Futex for list of devices owned by this driver */
-
 
66
    atomic_t devices_futex;
-
 
67
} devmap_driver_t;
64
} devmap_driver_t;
68
 
65
 
69
/** Info about registered device
66
/** Info about registered device
70
 *
67
 *
71
 */
68
 */
72
typedef struct {
69
typedef struct {
73
    /** Pointer to the previous and next device in the list of all devices */
70
    /** Pointer to the previous and next device in the list of all devices */
74
    link_t devices;
71
    link_t devices;
75
    /** Pointer to the previous and next device in the list of devices
72
    /** Pointer to the previous and next device in the list of devices
76
        owned by one driver */
73
        owned by one driver */
77
    link_t driver_devices;
74
    link_t driver_devices;
78
    /** Unique device identifier  */
75
    /** Unique device identifier  */
79
    dev_handle_t handle;
76
    dev_handle_t handle;
80
    /** Device name */
77
    /** Device name */
81
    char *name;
78
    char *name;
82
    /** Device driver handling this device */
79
    /** Device driver handling this device */
83
    devmap_driver_t *driver;
80
    devmap_driver_t *driver;
84
} devmap_device_t;
81
} devmap_device_t;
85
 
82
 
86
/** Pending lookup structure. */
83
/** Pending lookup structure. */
87
typedef struct {
84
typedef struct {
88
    link_t link;
85
    link_t link;
89
    char *name;              /**< Device name */
86
    char *name;              /**< Device name */
90
    ipc_callid_t callid;     /**< Call ID waiting for the lookup */
87
    ipc_callid_t callid;     /**< Call ID waiting for the lookup */
91
} pending_req_t;
88
} pending_req_t;
92
 
89
 
93
LIST_INITIALIZE(devices_list);
90
LIST_INITIALIZE(devices_list);
94
LIST_INITIALIZE(drivers_list);
91
LIST_INITIALIZE(drivers_list);
95
LIST_INITIALIZE(pending_req);
92
LIST_INITIALIZE(pending_req);
96
 
93
 
97
/* Locking order:
-
 
98
 *  drivers_list_futex
-
 
99
 *  devices_list_futex
-
 
100
 *  (devmap_driver_t *)->devices_futex
-
 
101
 *  create_handle_futex
-
 
102
 **/
-
 
103
 
-
 
104
static atomic_t devices_list_futex = FUTEX_INITIALIZER;
-
 
105
static atomic_t drivers_list_futex = FUTEX_INITIALIZER;
-
 
106
static atomic_t create_handle_futex = FUTEX_INITIALIZER;
-
 
107
 
-
 
108
static dev_handle_t last_handle = 0;
94
static dev_handle_t last_handle = 0;
109
 
95
 
110
static dev_handle_t devmap_create_handle(void)
96
static dev_handle_t devmap_create_handle(void)
111
{
97
{
112
    /* TODO: allow reusing old handles after their unregistration
98
    /* TODO: allow reusing old handles after their unregistration
113
     * and implement some version of LRU algorithm
99
     * and implement some version of LRU algorithm, avoid overflow
114
     */
100
     */
115
   
101
   
116
    /* FIXME: overflow */
-
 
117
    futex_down(&create_handle_futex);
-
 
118
    last_handle++;
102
    last_handle++;
119
    futex_up(&create_handle_futex);
-
 
120
   
103
   
121
    return last_handle;
104
    return last_handle;
122
}
105
}
123
 
106
 
124
/** Find device with given name.
107
/** Find device with given name.
125
 *
108
 *
126
 */
109
 */
127
static devmap_device_t *devmap_device_find_name(const char *name)
110
static devmap_device_t *devmap_device_find_name(const char *name)
128
{
111
{
129
    link_t *item = devices_list.next;
112
    link_t *item = devices_list.next;
130
    devmap_device_t *device = NULL;
113
    devmap_device_t *device = NULL;
131
   
114
   
132
    while (item != &devices_list) {
115
    while (item != &devices_list) {
133
        device = list_get_instance(item, devmap_device_t, devices);
116
        device = list_get_instance(item, devmap_device_t, devices);
134
        if (0 == str_cmp(device->name, name))
117
        if (str_cmp(device->name, name) == 0)
135
            break;
118
            break;
136
        item = item->next;
119
        item = item->next;
137
    }
120
    }
138
   
121
   
139
    if (item == &devices_list)
122
    if (item == &devices_list)
140
        return NULL;
123
        return NULL;
141
   
124
   
142
    device = list_get_instance(item, devmap_device_t, devices);
125
    device = list_get_instance(item, devmap_device_t, devices);
143
    return device;
126
    return device;
144
}
127
}
145
 
128
 
146
/** Find device with given handle.
129
/** Find device with given handle.
147
 *
130
 *
148
 * @todo: use hash table
131
 * @todo: use hash table
149
 *
132
 *
150
 */
133
 */
151
static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
134
static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
152
{
135
{
153
    futex_down(&devices_list_futex);
-
 
154
   
-
 
155
    link_t *item = (&devices_list)->next;
136
    link_t *item = (&devices_list)->next;
156
    devmap_device_t *device = NULL;
137
    devmap_device_t *device = NULL;
157
   
138
   
158
    while (item != &devices_list) {
139
    while (item != &devices_list) {
159
        device = list_get_instance(item, devmap_device_t, devices);
140
        device = list_get_instance(item, devmap_device_t, devices);
160
        if (device->handle == handle)
141
        if (device->handle == handle)
161
            break;
142
            break;
162
        item = item->next;
143
        item = item->next;
163
    }
144
    }
164
   
145
   
165
    if (item == &devices_list) {
146
    if (item == &devices_list)
166
        futex_up(&devices_list_futex);
-
 
167
        return NULL;
147
        return NULL;
168
    }
-
 
169
   
148
   
170
    device = list_get_instance(item, devmap_device_t, devices);
149
    device = list_get_instance(item, devmap_device_t, devices);
171
   
150
   
172
    futex_up(&devices_list_futex);
-
 
173
   
-
 
174
    return device;
151
    return device;
175
}
152
}
176
 
153
 
177
/**
154
/**
178
 *
155
 *
179
 * Unregister device and free it. It's assumed that driver's device list is
156
 * Unregister device and free it. It's assumed that driver's device list is
180
 * already locked.
157
 * already locked.
181
 *
158
 *
182
 */
159
 */
183
static int devmap_device_unregister_core(devmap_device_t *device)
160
static int devmap_device_unregister_core(devmap_device_t *device)
184
{
161
{
185
    list_remove(&(device->devices));
162
    list_remove(&(device->devices));
186
    list_remove(&(device->driver_devices));
163
    list_remove(&(device->driver_devices));
187
   
164
   
188
    free(device->name);
165
    free(device->name);
189
    free(device);
166
    free(device);
190
   
167
   
191
    return EOK;
168
    return EOK;
192
}
169
}
193
 
170
 
194
/**
171
/**
195
 *
172
 *
196
 * Read info about new driver and add it into linked list of registered
173
 * Read info about new driver and add it into linked list of registered
197
 * drivers.
174
 * drivers.
198
 *
175
 *
199
 */
176
 */
200
static void devmap_driver_register(devmap_driver_t **odriver)
177
static void devmap_driver_register(devmap_driver_t **odriver)
201
{
178
{
202
    *odriver = NULL;
179
    *odriver = NULL;
203
   
180
   
204
    ipc_call_t icall;
181
    ipc_call_t icall;
205
    ipc_callid_t iid = async_get_call(&icall);
182
    ipc_callid_t iid = async_get_call(&icall);
206
   
183
   
207
    if (IPC_GET_METHOD(icall) != DEVMAP_DRIVER_REGISTER) {
184
    if (IPC_GET_METHOD(icall) != DEVMAP_DRIVER_REGISTER) {
208
        ipc_answer_0(iid, EREFUSED);
185
        ipc_answer_0(iid, EREFUSED);
209
        return;
186
        return;
210
    }
187
    }
211
   
188
   
212
    devmap_driver_t *driver = (devmap_driver_t *) malloc(sizeof(devmap_driver_t));
189
    devmap_driver_t *driver = (devmap_driver_t *) malloc(sizeof(devmap_driver_t));
213
   
190
   
214
    if (driver == NULL) {
191
    if (driver == NULL) {
215
        ipc_answer_0(iid, ENOMEM);
192
        ipc_answer_0(iid, ENOMEM);
216
        return;
193
        return;
217
    }
194
    }
218
   
195
   
219
    /*
196
    /*
220
     * Get driver name
197
     * Get driver name
221
     */
198
     */
222
    ipc_callid_t callid;
199
    ipc_callid_t callid;
223
    size_t name_size;
200
    size_t name_size;
224
    if (!ipc_data_write_receive(&callid, &name_size)) {
201
    if (!ipc_data_write_receive(&callid, &name_size)) {
225
        free(driver);
202
        free(driver);
226
        ipc_answer_0(callid, EREFUSED);
203
        ipc_answer_0(callid, EREFUSED);
227
        ipc_answer_0(iid, EREFUSED);
204
        ipc_answer_0(iid, EREFUSED);
228
        return;
205
        return;
229
    }
206
    }
230
   
207
   
231
    if (name_size > DEVMAP_NAME_MAXLEN) {
208
    if (name_size > DEVMAP_NAME_MAXLEN) {
232
        free(driver);
209
        free(driver);
233
        ipc_answer_0(callid, EINVAL);
210
        ipc_answer_0(callid, EINVAL);
234
        ipc_answer_0(iid, EREFUSED);
211
        ipc_answer_0(iid, EREFUSED);
235
        return;
212
        return;
236
    }
213
    }
237
   
214
   
238
    /*
215
    /*
239
     * Allocate buffer for device name.
216
     * Allocate buffer for device name.
240
     */
217
     */
241
    driver->name = (char *) malloc(name_size + 1);
218
    driver->name = (char *) malloc(name_size + 1);
242
    if (driver->name == NULL) {
219
    if (driver->name == NULL) {
243
        free(driver);
220
        free(driver);
244
        ipc_answer_0(callid, ENOMEM);
221
        ipc_answer_0(callid, ENOMEM);
245
        ipc_answer_0(iid, EREFUSED);
222
        ipc_answer_0(iid, EREFUSED);
246
        return;
223
        return;
247
    }
224
    }
248
   
225
   
249
    /*
226
    /*
250
     * Send confirmation to sender and get data into buffer.
227
     * Send confirmation to sender and get data into buffer.
251
     */
228
     */
252
    if (EOK != ipc_data_write_finalize(callid, driver->name, name_size)) {
229
    if (ipc_data_write_finalize(callid, driver->name, name_size) != EOK) {
253
        free(driver->name);
230
        free(driver->name);
254
        free(driver);
231
        free(driver);
255
        ipc_answer_0(iid, EREFUSED);
232
        ipc_answer_0(iid, EREFUSED);
256
        return;
233
        return;
257
    }
234
    }
258
   
235
   
259
    driver->name[name_size] = 0;
236
    driver->name[name_size] = 0;
260
   
237
   
261
    /* Initialize futex for list of devices owned by this driver */
-
 
262
    futex_initialize(&(driver->devices_futex), 1);
-
 
263
   
-
 
264
    /*
238
    /*
265
     * Initialize list of asociated devices
239
     * Initialize list of asociated devices
266
     */
240
     */
267
    list_initialize(&(driver->devices));
241
    list_initialize(&(driver->devices));
268
   
242
   
269
    /*
243
    /*
270
     * Create connection to the driver
244
     * Create connection to the driver
271
     */
245
     */
272
    ipc_call_t call;
246
    ipc_call_t call;
273
    callid = async_get_call(&call);
247
    callid = async_get_call(&call);
274
   
248
   
275
    if (IPC_M_CONNECT_TO_ME != IPC_GET_METHOD(call)) {
249
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
276
        ipc_answer_0(callid, ENOTSUP);
250
        ipc_answer_0(callid, ENOTSUP);
277
       
251
       
278
        free(driver->name);
252
        free(driver->name);
279
        free(driver);
253
        free(driver);
280
        ipc_answer_0(iid, ENOTSUP);
254
        ipc_answer_0(iid, ENOTSUP);
281
        return;
255
        return;
282
    }
256
    }
283
   
257
   
284
    driver->phone = IPC_GET_ARG5(call);
258
    driver->phone = IPC_GET_ARG5(call);
285
   
259
   
286
    ipc_answer_0(callid, EOK);
260
    ipc_answer_0(callid, EOK);
287
   
261
   
288
    list_initialize(&(driver->drivers));
262
    list_initialize(&(driver->drivers));
289
   
263
   
290
    futex_down(&drivers_list_futex);
-
 
291
   
-
 
292
    /* TODO:
264
    /* TODO:
293
     * check that no driver with name equal to driver->name is registered
265
     * check that no driver with name equal to driver->name is registered
294
     */
266
     */
295
   
267
   
296
    /*
268
    /*
297
     * Insert new driver into list of registered drivers
269
     * Insert new driver into list of registered drivers
298
     */
270
     */
299
    list_append(&(driver->drivers), &drivers_list);
271
    list_append(&(driver->drivers), &drivers_list);
300
    futex_up(&drivers_list_futex);
-
 
301
   
272
   
302
    ipc_answer_0(iid, EOK);
273
    ipc_answer_0(iid, EOK);
303
   
274
   
304
    *odriver = driver;
275
    *odriver = driver;
305
}
276
}
306
 
277
 
307
/**
278
/**
308
 * Unregister device driver, unregister all its devices and free driver
279
 * Unregister device driver, unregister all its devices and free driver
309
 * structure.
280
 * structure.
310
 *
281
 *
311
 */
282
 */
312
static int devmap_driver_unregister(devmap_driver_t *driver)
283
static int devmap_driver_unregister(devmap_driver_t *driver)
313
{
284
{
314
    if (driver == NULL)
285
    if (driver == NULL)
315
        return EEXISTS;
286
        return EEXISTS;
316
   
287
   
317
    futex_down(&drivers_list_futex);
-
 
318
   
-
 
319
    if (driver->phone != 0)
288
    if (driver->phone != 0)
320
        ipc_hangup(driver->phone);
289
        ipc_hangup(driver->phone);
321
   
290
   
322
    /* Remove it from list of drivers */
291
    /* Remove it from list of drivers */
323
    list_remove(&(driver->drivers));
292
    list_remove(&(driver->drivers));
324
   
293
   
325
    /* Unregister all its devices */
-
 
326
    futex_down(&devices_list_futex);
-
 
327
    futex_down(&(driver->devices_futex));
-
 
328
   
-
 
329
    while (!list_empty(&(driver->devices))) {
294
    while (!list_empty(&(driver->devices))) {
330
        devmap_device_t *device = list_get_instance(driver->devices.next,
295
        devmap_device_t *device = list_get_instance(driver->devices.next,
331
            devmap_device_t, driver_devices);
296
            devmap_device_t, driver_devices);
332
        devmap_device_unregister_core(device);
297
        devmap_device_unregister_core(device);
333
    }
298
    }
334
   
299
   
335
    futex_up(&(driver->devices_futex));
-
 
336
    futex_up(&devices_list_futex);
-
 
337
    futex_up(&drivers_list_futex);
-
 
338
   
-
 
339
    /* free name and driver */
300
    /* free name and driver */
340
    if (driver->name != NULL)
301
    if (driver->name != NULL)
341
        free(driver->name);
302
        free(driver->name);
342
   
303
   
343
    free(driver);
304
    free(driver);
344
   
305
   
345
    return EOK;
306
    return EOK;
346
}
307
}
347
 
308
 
348
 
309
 
349
/** Process pending lookup requests */
310
/** Process pending lookup requests */
350
static void process_pending_lookup()
311
static void process_pending_lookup(void)
351
{
312
{
-
 
313
    async_serialize_start();
-
 
314
   
352
    link_t *cur;
315
    link_t *cur;
353
   
316
   
354
loop:
317
loop:
355
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
318
    for (cur = pending_req.next; cur != &pending_req; cur = cur->next) {
356
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
319
        pending_req_t *pr = list_get_instance(cur, pending_req_t, link);
357
       
320
       
358
        const devmap_device_t *dev = devmap_device_find_name(pr->name);
321
        const devmap_device_t *dev = devmap_device_find_name(pr->name);
359
        if (!dev)
322
        if (!dev)
360
            continue;
323
            continue;
361
       
324
       
362
        ipc_answer_1(pr->callid, EOK, dev->handle);
325
        ipc_answer_1(pr->callid, EOK, dev->handle);
363
       
326
       
364
        free(pr->name);
327
        free(pr->name);
365
        list_remove(cur);
328
        list_remove(cur);
366
        free(pr);
329
        free(pr);
-
 
330
       
367
        goto loop;
331
        goto loop;
368
    }
332
    }
-
 
333
   
-
 
334
    async_serialize_end();
369
}
335
}
370
 
336
 
371
 
337
 
372
/** Register instance of device
338
/** Register instance of device
373
 *
339
 *
374
 */
340
 */
375
static void devmap_device_register(ipc_callid_t iid, ipc_call_t *icall,
341
static void devmap_device_register(ipc_callid_t iid, ipc_call_t *icall,
376
    devmap_driver_t *driver)
342
    devmap_driver_t *driver)
377
{
343
{
378
    if (driver == NULL) {
344
    if (driver == NULL) {
379
        ipc_answer_0(iid, EREFUSED);
345
        ipc_answer_0(iid, EREFUSED);
380
        return;
346
        return;
381
    }
347
    }
382
   
348
   
383
    /* Create new device entry */
349
    /* Create new device entry */
384
    devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
350
    devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
385
    if (device == NULL) {
351
    if (device == NULL) {
386
        ipc_answer_0(iid, ENOMEM);
352
        ipc_answer_0(iid, ENOMEM);
387
        return;
353
        return;
388
    }
354
    }
389
   
355
   
390
    /* Get device name */
356
    /* Get device name */
391
    ipc_callid_t callid;
357
    ipc_callid_t callid;
392
    size_t size;
358
    size_t size;
393
    if (!ipc_data_write_receive(&callid, &size)) {
359
    if (!ipc_data_write_receive(&callid, &size)) {
394
        free(device);
360
        free(device);
395
        ipc_answer_0(iid, EREFUSED);
361
        ipc_answer_0(iid, EREFUSED);
396
        return;
362
        return;
397
    }
363
    }
398
   
364
   
399
    if (size > DEVMAP_NAME_MAXLEN) {
365
    if (size > DEVMAP_NAME_MAXLEN) {
400
        free(device);
366
        free(device);
401
        ipc_answer_0(callid, EINVAL);
367
        ipc_answer_0(callid, EINVAL);
402
        ipc_answer_0(iid, EREFUSED);
368
        ipc_answer_0(iid, EREFUSED);
403
        return;
369
        return;
404
    }
370
    }
405
   
371
   
406
    /* +1 for terminating \0 */
372
    /* +1 for terminating \0 */
407
    device->name = (char *) malloc(size + 1);
373
    device->name = (char *) malloc(size + 1);
408
   
374
   
409
    if (device->name == NULL) {
375
    if (device->name == NULL) {
410
        free(device);
376
        free(device);
411
        ipc_answer_0(callid, ENOMEM);
377
        ipc_answer_0(callid, ENOMEM);
412
        ipc_answer_0(iid, EREFUSED);
378
        ipc_answer_0(iid, EREFUSED);
413
        return;
379
        return;
414
    }
380
    }
415
   
381
   
416
    ipc_data_write_finalize(callid, device->name, size);
382
    ipc_data_write_finalize(callid, device->name, size);
417
    device->name[size] = 0;
383
    device->name[size] = 0;
418
   
384
   
419
    list_initialize(&(device->devices));
385
    list_initialize(&(device->devices));
420
    list_initialize(&(device->driver_devices));
386
    list_initialize(&(device->driver_devices));
421
   
387
   
422
    futex_down(&devices_list_futex);
-
 
423
   
-
 
424
    /* Check that device with such name is not already registered */
388
    /* Check that device with such name is not already registered */
425
    if (NULL != devmap_device_find_name(device->name)) {
389
    if (NULL != devmap_device_find_name(device->name)) {
426
        printf(NAME ": Device '%s' already registered\n", device->name);
390
        printf(NAME ": Device '%s' already registered\n", device->name);
427
        futex_up(&devices_list_futex); 
-
 
428
        free(device->name);
391
        free(device->name);
429
        free(device);
392
        free(device);
430
        ipc_answer_0(iid, EEXISTS);
393
        ipc_answer_0(iid, EEXISTS);
431
        return;
394
        return;
432
    }
395
    }
433
   
396
   
434
    /* Get unique device handle */
397
    /* Get unique device handle */
435
    device->handle = devmap_create_handle();
398
    device->handle = devmap_create_handle();
436
   
399
   
437
    device->driver = driver;
400
    device->driver = driver;
438
   
401
   
439
    /* Insert device into list of all devices  */
402
    /* Insert device into list of all devices  */
440
    list_append(&device->devices, &devices_list);
403
    list_append(&device->devices, &devices_list);
441
   
404
   
442
    /* Insert device into list of devices that belog to one driver */
-
 
443
    futex_down(&device->driver->devices_futex);
-
 
444
   
-
 
445
    list_append(&device->driver_devices, &device->driver->devices);
405
    list_append(&device->driver_devices, &device->driver->devices);
446
   
406
   
447
    futex_up(&device->driver->devices_futex);
-
 
448
    futex_up(&devices_list_futex);
-
 
449
   
-
 
450
    ipc_answer_1(iid, EOK, device->handle);
407
    ipc_answer_1(iid, EOK, device->handle);
451
   
-
 
452
    process_pending_lookup();
-
 
453
}
408
}
454
 
409
 
455
/**
410
/**
456
 *
411
 *
457
 */
412
 */
458
static int devmap_device_unregister(ipc_callid_t iid, ipc_call_t *icall,
413
static int devmap_device_unregister(ipc_callid_t iid, ipc_call_t *icall,
459
    devmap_driver_t *driver)
414
    devmap_driver_t *driver)
460
{
415
{
461
    /* TODO */
416
    /* TODO */
462
    return EOK;
417
    return EOK;
463
}
418
}
464
 
419
 
465
/** Connect client to the device.
420
/** Connect client to the device.
466
 *
421
 *
467
 * Find device driver owning requested device and forward
422
 * Find device driver owning requested device and forward
468
 * the message to it.
423
 * the message to it.
469
 *
424
 *
470
 */
425
 */
471
static void devmap_forward(ipc_callid_t callid, ipc_call_t *call)
426
static void devmap_forward(ipc_callid_t callid, ipc_call_t *call)
472
{
427
{
473
    /*
428
    /*
474
     * Get handle from request
429
     * Get handle from request
475
     */
430
     */
476
    dev_handle_t handle = IPC_GET_ARG2(*call);
431
    dev_handle_t handle = IPC_GET_ARG2(*call);
477
    devmap_device_t *dev = devmap_device_find_handle(handle);
432
    devmap_device_t *dev = devmap_device_find_handle(handle);
478
   
433
   
479
    if ((dev == NULL) || (dev->driver == NULL) || (dev->driver->phone == 0)) {
434
    if ((dev == NULL) || (dev->driver == NULL) || (dev->driver->phone == 0)) {
480
        ipc_answer_0(callid, ENOENT);
435
        ipc_answer_0(callid, ENOENT);
481
        return;
436
        return;
482
    }
437
    }
483
   
438
   
484
    ipc_forward_fast(callid, dev->driver->phone, dev->handle,
439
    ipc_forward_fast(callid, dev->driver->phone, dev->handle,
485
        IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
440
        IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
486
}
441
}
487
 
442
 
488
/** Find handle for device instance identified by name.
443
/** Find handle for device instance identified by name.
489
 *
444
 *
490
 * In answer will be send EOK and device handle in arg1 or a error
445
 * In answer will be send EOK and device handle in arg1 or a error
491
 * code from errno.h.
446
 * code from errno.h.
492
 *
447
 *
493
 */
448
 */
494
static void devmap_get_handle(ipc_callid_t iid, ipc_call_t *icall)
449
static void devmap_get_handle(ipc_callid_t iid, ipc_call_t *icall)
495
{
450
{
496
    /*
451
    /*
497
     * Wait for incoming message with device name (but do not
452
     * Wait for incoming message with device name (but do not
498
     * read the name itself until the buffer is allocated).
453
     * read the name itself until the buffer is allocated).
499
     */
454
     */
500
    ipc_callid_t callid;
455
    ipc_callid_t callid;
501
    size_t size;
456
    size_t size;
502
    if (!ipc_data_write_receive(&callid, &size)) {
457
    if (!ipc_data_write_receive(&callid, &size)) {
503
        ipc_answer_0(callid, EREFUSED);
458
        ipc_answer_0(callid, EREFUSED);
504
        ipc_answer_0(iid, EREFUSED);
459
        ipc_answer_0(iid, EREFUSED);
505
        return;
460
        return;
506
    }
461
    }
507
   
462
   
508
    if ((size < 1) || (size > DEVMAP_NAME_MAXLEN)) {
463
    if ((size < 1) || (size > DEVMAP_NAME_MAXLEN)) {
509
        ipc_answer_0(callid, EINVAL);
464
        ipc_answer_0(callid, EINVAL);
510
        ipc_answer_0(iid, EREFUSED);
465
        ipc_answer_0(iid, EREFUSED);
511
        return;
466
        return;
512
    }
467
    }
513
   
468
   
514
    /*
469
    /*
515
     * Allocate buffer for device name.
470
     * Allocate buffer for device name.
516
     */
471
     */
517
    char *name = (char *) malloc(size + 1);
472
    char *name = (char *) malloc(size + 1);
518
    if (name == NULL) {
473
    if (name == NULL) {
519
        ipc_answer_0(callid, ENOMEM);
474
        ipc_answer_0(callid, ENOMEM);
520
        ipc_answer_0(iid, EREFUSED);
475
        ipc_answer_0(iid, EREFUSED);
521
        return;
476
        return;
522
    }
477
    }
523
   
478
   
524
    /*
479
    /*
525
     * Send confirmation to sender and get data into buffer.
480
     * Send confirmation to sender and get data into buffer.
526
     */
481
     */
527
    ipcarg_t retval = ipc_data_write_finalize(callid, name, size);
482
    ipcarg_t retval = ipc_data_write_finalize(callid, name, size);
528
    if (retval != EOK) {
483
    if (retval != EOK) {
529
        ipc_answer_0(iid, EREFUSED);
484
        ipc_answer_0(iid, EREFUSED);
530
        free(name);
485
        free(name);
531
        return;
486
        return;
532
    }
487
    }
533
    name[size] = '\0';
488
    name[size] = '\0';
534
   
489
   
535
    /*
490
    /*
536
     * Find device name in linked list of known devices.
491
     * Find device name in linked list of known devices.
537
     */
492
     */
538
    const devmap_device_t *dev = devmap_device_find_name(name);
493
    const devmap_device_t *dev = devmap_device_find_name(name);
539
   
494
   
540
    /*
495
    /*
541
     * Device was not found.
496
     * Device was not found.
542
     */
497
     */
543
    if (dev == NULL) {
498
    if (dev == NULL) {
544
        if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
499
        if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
545
            /* Blocking lookup, add to pending list */
500
            /* Blocking lookup, add to pending list */
546
            pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
501
            pending_req_t *pr = (pending_req_t *) malloc(sizeof(pending_req_t));
547
            if (!pr) {
502
            if (!pr) {
548
                ipc_answer_0(iid, ENOMEM);
503
                ipc_answer_0(iid, ENOMEM);
549
                free(name);
504
                free(name);
550
                return;
505
                return;
551
            }
506
            }
552
           
507
           
553
            pr->name = name;
508
            pr->name = name;
554
            pr->callid = iid;
509
            pr->callid = iid;
555
            list_append(&pr->link, &pending_req);
510
            list_append(&pr->link, &pending_req);
556
            return;
511
            return;
557
        }
512
        }
558
       
513
       
559
        ipc_answer_0(iid, ENOENT);
514
        ipc_answer_0(iid, ENOENT);
560
        free(name);
515
        free(name);
561
        return;
516
        return;
562
    }
517
    }
563
   
518
   
564
    ipc_answer_1(iid, EOK, dev->handle);
519
    ipc_answer_1(iid, EOK, dev->handle);
565
    free(name);
520
    free(name);
566
}
521
}
567
 
522
 
568
/** Find name of device identified by id and send it to caller.
523
/** Find name of device identified by id and send it to caller.
569
 *
524
 *
570
 */
525
 */
571
static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall)
526
static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall)
572
{
527
{
573
    const devmap_device_t *device = devmap_device_find_handle(IPC_GET_ARG1(*icall));
528
    const devmap_device_t *device = devmap_device_find_handle(IPC_GET_ARG1(*icall));
574
   
529
   
575
    /*
530
    /*
576
     * Device not found.
531
     * Device not found.
577
     */
532
     */
578
    if (device == NULL) {
533
    if (device == NULL) {
579
        ipc_answer_0(iid, ENOENT);
534
        ipc_answer_0(iid, ENOENT);
580
        return;
535
        return;
581
    }
536
    }
582
   
537
   
583
    ipc_answer_0(iid, EOK);
538
    ipc_answer_0(iid, EOK);
584
   
539
   
585
    size_t name_size = str_size(device->name);
540
    size_t name_size = str_size(device->name);
586
   
541
   
587
    /* FIXME:
542
    /* FIXME:
588
     * We have no channel from DEVMAP to client, therefore
543
     * We have no channel from DEVMAP to client, therefore
589
     * sending must be initiated by client.
544
     * sending must be initiated by client.
590
     *
545
     *
591
     * int rc = ipc_data_write_send(phone, device->name, name_size);
546
     * int rc = ipc_data_write_send(phone, device->name, name_size);
592
     * if (rc != EOK) {
547
     * if (rc != EOK) {
593
     *     async_wait_for(req, NULL);
548
     *     async_wait_for(req, NULL);
594
     *     return rc;
549
     *     return rc;
595
     * }
550
     * }
596
     */
551
     */
597
   
552
   
598
    /* TODO: send name in response */
553
    /* TODO: send name in response */
599
}
554
}
600
 
555
 
601
static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
556
static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
602
{
557
{
603
    futex_down(&devices_list_futex);
-
 
604
    ipc_answer_1(iid, EOK, list_count(&devices_list));
558
    ipc_answer_1(iid, EOK, list_count(&devices_list));
605
    futex_up(&devices_list_futex);
-
 
606
}
559
}
607
 
560
 
608
static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
561
static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
609
{
562
{
610
    futex_down(&devices_list_futex);
-
 
611
   
-
 
612
    ipc_callid_t callid;
563
    ipc_callid_t callid;
613
    size_t size;
564
    size_t size;
614
    if (!ipc_data_read_receive(&callid, &size)) {
565
    if (!ipc_data_read_receive(&callid, &size)) {
615
        ipc_answer_0(callid, EREFUSED);
566
        ipc_answer_0(callid, EREFUSED);
616
        ipc_answer_0(iid, EREFUSED);
567
        ipc_answer_0(iid, EREFUSED);
617
        return;
568
        return;
618
    }
569
    }
619
   
570
   
620
    if ((size % sizeof(dev_desc_t)) != 0) {
571
    if ((size % sizeof(dev_desc_t)) != 0) {
621
        ipc_answer_0(callid, EINVAL);
572
        ipc_answer_0(callid, EINVAL);
622
        ipc_answer_0(iid, EREFUSED);
573
        ipc_answer_0(iid, EREFUSED);
623
        return;
574
        return;
624
    }
575
    }
625
   
576
   
626
    count_t count = size / sizeof(dev_desc_t);
577
    size_t count = size / sizeof(dev_desc_t);
627
    dev_desc_t *desc = (dev_desc_t *) malloc(size);
578
    dev_desc_t *desc = (dev_desc_t *) malloc(size);
628
    if (desc == NULL) {
579
    if (desc == NULL) {
629
        ipc_answer_0(callid, ENOMEM);
580
        ipc_answer_0(callid, ENOMEM);
630
        ipc_answer_0(iid, EREFUSED);
581
        ipc_answer_0(iid, EREFUSED);
631
        return;
582
        return;
632
    }
583
    }
633
   
584
   
634
    count_t pos = 0;
585
    size_t pos = 0;
635
    link_t *item = devices_list.next;
586
    link_t *item = devices_list.next;
636
   
587
   
637
    while ((item != &devices_list) && (pos < count)) {
588
    while ((item != &devices_list) && (pos < count)) {
638
        devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
589
        devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
639
       
590
       
640
        desc[pos].handle = device->handle;
591
        desc[pos].handle = device->handle;
641
        str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, device->name);
592
        str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, device->name);
642
        pos++;
593
        pos++;
643
        item = item->next;
594
        item = item->next;
644
    }
595
    }
645
   
596
   
646
    ipcarg_t retval = ipc_data_read_finalize(callid, desc, pos * sizeof(dev_desc_t));
597
    ipcarg_t retval = ipc_data_read_finalize(callid, desc, pos * sizeof(dev_desc_t));
647
    if (retval != EOK) {
598
    if (retval != EOK) {
648
        ipc_answer_0(iid, EREFUSED);
599
        ipc_answer_0(iid, EREFUSED);
649
        free(desc);
600
        free(desc);
650
        return;
601
        return;
651
    }
602
    }
652
   
603
   
653
    free(desc);
604
    free(desc);
654
   
605
   
655
    futex_up(&devices_list_futex);
-
 
656
   
-
 
657
    ipc_answer_1(iid, EOK, pos);
606
    ipc_answer_1(iid, EOK, pos);
658
}
607
}
659
 
608
 
660
/** Initialize device mapper.
609
/** Initialize device mapper.
661
 *
610
 *
662
 *
611
 *
663
 */
612
 */
664
static bool devmap_init()
613
static bool devmap_init()
665
{
614
{
666
    /* Create NULL device entry */
615
    /* Create NULL device entry */
667
    devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
616
    devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
668
    if (device == NULL)
617
    if (device == NULL)
669
        return false;
618
        return false;
670
   
619
   
671
    device->name = str_dup("null");
620
    device->name = str_dup("null");
672
    if (device->name == NULL) {
621
    if (device->name == NULL) {
673
        free(device);
622
        free(device);
674
        return false;
623
        return false;
675
    }
624
    }
676
   
625
   
677
    list_initialize(&(device->devices));
626
    list_initialize(&(device->devices));
678
    list_initialize(&(device->driver_devices));
627
    list_initialize(&(device->driver_devices));
679
   
628
   
680
    futex_down(&devices_list_futex);
-
 
681
   
-
 
682
    /* Get unique device handle */
629
    /* Get unique device handle */
683
    device->handle = devmap_create_handle();
630
    device->handle = devmap_create_handle();
684
    device->driver = NULL;
631
    device->driver = NULL;
685
   
632
   
686
    /* Insert device into list of all devices  */
633
    /* Insert device into list of all devices  */
687
    list_append(&device->devices, &devices_list);
634
    list_append(&device->devices, &devices_list);
688
   
635
   
689
    futex_up(&devices_list_futex);
-
 
690
   
-
 
691
    return true;
636
    return true;
692
}
637
}
693
 
638
 
694
/** Handle connection with device driver.
639
/** Handle connection with device driver.
695
 *
640
 *
696
 */
641
 */
697
static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
642
static void devmap_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
698
{
643
{
699
    /* Accept connection */
644
    /* Accept connection */
700
    ipc_answer_0(iid, EOK);
645
    ipc_answer_0(iid, EOK);
701
   
646
   
702
    devmap_driver_t *driver = NULL;
647
    devmap_driver_t *driver = NULL;
703
    devmap_driver_register(&driver);
648
    devmap_driver_register(&driver);
704
   
649
   
705
    if (NULL == driver)
650
    if (NULL == driver)
706
        return;
651
        return;
707
   
652
   
708
    bool cont = true;
653
    bool cont = true;
709
    while (cont) {
654
    while (cont) {
710
        ipc_call_t call;
655
        ipc_call_t call;
711
        ipc_callid_t callid = async_get_call(&call);
656
        ipc_callid_t callid = async_get_call(&call);
712
       
657
       
-
 
658
        async_serialize_start();
-
 
659
       
713
        switch (IPC_GET_METHOD(call)) {
660
        switch (IPC_GET_METHOD(call)) {
714
        case IPC_M_PHONE_HUNGUP:
661
        case IPC_M_PHONE_HUNGUP:
715
            cont = false;
662
            cont = false;
716
            /* Exit thread */
663
            async_serialize_end();
717
            continue;
664
            continue;
718
        case DEVMAP_DRIVER_UNREGISTER:
665
        case DEVMAP_DRIVER_UNREGISTER:
719
            if (NULL == driver)
666
            if (NULL == driver)
720
                ipc_answer_0(callid, ENOENT);
667
                ipc_answer_0(callid, ENOENT);
721
            else
668
            else
722
                ipc_answer_0(callid, EOK);
669
                ipc_answer_0(callid, EOK);
723
            break;
670
            break;
724
        case DEVMAP_DEVICE_REGISTER:
671
        case DEVMAP_DEVICE_REGISTER:
725
            /* Register one instance of device */
672
            /* Register one instance of device */
726
            devmap_device_register(callid, &call, driver);
673
            devmap_device_register(callid, &call, driver);
727
            break;
674
            break;
728
        case DEVMAP_DEVICE_UNREGISTER:
675
        case DEVMAP_DEVICE_UNREGISTER:
729
            /* Remove instance of device identified by handler */
676
            /* Remove instance of device identified by handler */
730
            devmap_device_unregister(callid, &call, driver);
677
            devmap_device_unregister(callid, &call, driver);
731
            break;
678
            break;
732
        case DEVMAP_DEVICE_GET_HANDLE:
679
        case DEVMAP_DEVICE_GET_HANDLE:
733
            devmap_get_handle(callid, &call);
680
            devmap_get_handle(callid, &call);
734
            break;
681
            break;
735
        case DEVMAP_DEVICE_GET_NAME:
682
        case DEVMAP_DEVICE_GET_NAME:
736
            devmap_get_name(callid, &call);
683
            devmap_get_name(callid, &call);
737
            break;
684
            break;
738
        default:
685
        default:
739
            if (!(callid & IPC_CALLID_NOTIFICATION))
686
            if (!(callid & IPC_CALLID_NOTIFICATION))
740
                ipc_answer_0(callid, ENOENT);
687
                ipc_answer_0(callid, ENOENT);
741
        }
688
        }
-
 
689
       
-
 
690
        async_serialize_end();
742
    }
691
    }
743
   
692
   
744
    if (NULL != driver) {
693
    if (NULL != driver) {
745
        /*
694
        /*
746
         * Unregister the device driver and all its devices.
695
         * Unregister the device driver and all its devices.
747
         */
696
         */
-
 
697
       
-
 
698
        async_serialize_start();
-
 
699
       
748
        devmap_driver_unregister(driver);
700
        devmap_driver_unregister(driver);
749
        driver = NULL;
701
        driver = NULL;
-
 
702
       
-
 
703
        async_serialize_end();
750
    }
704
    }
751
}
705
}
752
 
706
 
753
/** Handle connection with device client.
707
/** Handle connection with device client.
754
 *
708
 *
755
 */
709
 */
756
static void devmap_connection_client(ipc_callid_t iid, ipc_call_t *icall)
710
static void devmap_connection_client(ipc_callid_t iid, ipc_call_t *icall)
757
{
711
{
758
    /* Accept connection */
712
    /* Accept connection */
759
    ipc_answer_0(iid, EOK);
713
    ipc_answer_0(iid, EOK);
760
   
714
   
761
    bool cont = true;
715
    bool cont = true;
762
    while (cont) {
716
    while (cont) {
763
        ipc_call_t call;
717
        ipc_call_t call;
764
        ipc_callid_t callid = async_get_call(&call);
718
        ipc_callid_t callid = async_get_call(&call);
765
       
719
       
-
 
720
        async_serialize_start();
-
 
721
       
766
        switch (IPC_GET_METHOD(call)) {
722
        switch (IPC_GET_METHOD(call)) {
767
        case IPC_M_PHONE_HUNGUP:
723
        case IPC_M_PHONE_HUNGUP:
768
            cont = false;
724
            cont = false;
769
            /* Exit thread */
725
            async_serialize_end();
770
            continue;
726
            continue;
771
        case DEVMAP_DEVICE_GET_HANDLE:
727
        case DEVMAP_DEVICE_GET_HANDLE:
772
            devmap_get_handle(callid, &call);
728
            devmap_get_handle(callid, &call);
773
            break;
729
            break;
774
        case DEVMAP_DEVICE_GET_NAME:
730
        case DEVMAP_DEVICE_GET_NAME:
775
            devmap_get_name(callid, &call);
731
            devmap_get_name(callid, &call);
776
            break;
732
            break;
777
        case DEVMAP_DEVICE_GET_COUNT:
733
        case DEVMAP_DEVICE_GET_COUNT:
778
            devmap_get_count(callid, &call);
734
            devmap_get_count(callid, &call);
779
            break;
735
            break;
780
        case DEVMAP_DEVICE_GET_DEVICES:
736
        case DEVMAP_DEVICE_GET_DEVICES:
781
            devmap_get_devices(callid, &call);
737
            devmap_get_devices(callid, &call);
782
            break;
738
            break;
783
        default:
739
        default:
784
            if (!(callid & IPC_CALLID_NOTIFICATION))
740
            if (!(callid & IPC_CALLID_NOTIFICATION))
785
                ipc_answer_0(callid, ENOENT);
741
                ipc_answer_0(callid, ENOENT);
786
        }
742
        }
-
 
743
       
-
 
744
        async_serialize_end();
787
    }
745
    }
788
}
746
}
789
 
747
 
790
/** Function for handling connections to devmap
748
/** Function for handling connections to devmap
791
 *
749
 *
792
 */
750
 */
793
static void devmap_connection(ipc_callid_t iid, ipc_call_t *icall)
751
static void devmap_connection(ipc_callid_t iid, ipc_call_t *icall)
794
{
752
{
795
    /* Select interface */
753
    /* Select interface */
796
    switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
754
    switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
797
    case DEVMAP_DRIVER:
755
    case DEVMAP_DRIVER:
798
        devmap_connection_driver(iid, icall);
756
        devmap_connection_driver(iid, icall);
799
        break;
757
        break;
800
    case DEVMAP_CLIENT:
758
    case DEVMAP_CLIENT:
801
        devmap_connection_client(iid, icall);
759
        devmap_connection_client(iid, icall);
802
        break;
760
        break;
803
    case DEVMAP_CONNECT_TO_DEVICE:
761
    case DEVMAP_CONNECT_TO_DEVICE:
804
        /* Connect client to selected device */
762
        /* Connect client to selected device */
805
        devmap_forward(iid, icall);
763
        devmap_forward(iid, icall);
806
        break;
764
        break;
807
    default:
765
    default:
808
        /* No such interface */
766
        /* No such interface */
809
        ipc_answer_0(iid, ENOENT);
767
        ipc_answer_0(iid, ENOENT);
810
    }
768
    }
811
}
769
}
812
 
770
 
813
/**
771
/**
814
 *
772
 *
815
 */
773
 */
816
int main(int argc, char *argv[])
774
int main(int argc, char *argv[])
817
{
775
{
818
    printf(NAME ": HelenOS Device Mapper\n");
776
    printf(NAME ": HelenOS Device Mapper\n");
819
   
777
   
820
    if (!devmap_init()) {
778
    if (!devmap_init()) {
821
        printf(NAME ": Error while initializing service\n");
779
        printf(NAME ": Error while initializing service\n");
822
        return -1;
780
        return -1;
823
    }
781
    }
824
   
782
   
825
    /* Set a handler of incomming connections */
783
    /* Set a handler of incomming connections and
-
 
784
       pending operations */
-
 
785
    async_set_pending(process_pending_lookup);
826
    async_set_client_connection(devmap_connection);
786
    async_set_client_connection(devmap_connection);
827
   
787
   
828
    /* Register device mapper at naming service */
788
    /* Register device mapper at naming service */
829
    ipcarg_t phonead;
789
    ipcarg_t phonead;
830
    if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0)
790
    if (ipc_connect_to_me(PHONE_NS, SERVICE_DEVMAP, 0, 0, &phonead) != 0)
831
        return -1;
791
        return -1;
832
   
792
   
833
    printf(NAME ": Accepting connections\n");
793
    printf(NAME ": Accepting connections\n");
834
    async_manager();
794
    async_manager();
835
   
795
   
836
    /* Never reached */
796
    /* Never reached */
837
    return 0;
797
    return 0;
838
}
798
}
839
 
799
 
840
/**
800
/**
841
 * @}
801
 * @}
842
 */
802
 */
843
 
803