Subversion Repositories HelenOS

Rev

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

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