Subversion Repositories HelenOS

Rev

Rev 4668 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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