Subversion Repositories HelenOS

Rev

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

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