Subversion Repositories HelenOS

Rev

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

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