Subversion Repositories HelenOS

Rev

Rev 4537 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4537 Rev 4668
Line 39... Line 39...
39
#include <bool.h>
39
#include <bool.h>
40
#include <errno.h>
40
#include <errno.h>
41
#include <malloc.h>
41
#include <malloc.h>
42
#include <string.h>
42
#include <string.h>
43
#include <libfs.h>
43
#include <libfs.h>
-
 
44
#include <fibril_sync.h>
44
#include <adt/hash_table.h>
45
#include <adt/hash_table.h>
-
 
46
#include <sys/stat.h>
45
#include "devfs.h"
47
#include "devfs.h"
46
#include "devfs_ops.h"
48
#include "devfs_ops.h"
47
 
49
 
48
#define PLB_GET_CHAR(pos)  (devfs_reg.plb_ro[pos % PLB_SIZE])
50
#define PLB_GET_CHAR(pos)  (devfs_reg.plb_ro[pos % PLB_SIZE])
49
 
51
 
Line 56... Line 58...
56
} device_t;
58
} device_t;
57
 
59
 
58
/** Hash table of opened devices */
60
/** Hash table of opened devices */
59
static hash_table_t devices;
61
static hash_table_t devices;
60
 
62
 
-
 
63
/** Hash table mutex */
-
 
64
static FIBRIL_MUTEX_INITIALIZE(devices_mutex);
-
 
65
 
61
#define DEVICES_KEYS        1
66
#define DEVICES_KEYS        1
62
#define DEVICES_KEY_HANDLE  0
67
#define DEVICES_KEY_HANDLE  0
63
#define DEVICES_BUCKETS     256
68
#define DEVICES_BUCKETS     256
64
 
69
 
65
/* Implementation of hash table interface for the nodes hash table. */
70
/* Implementation of hash table interface for the nodes hash table. */
Line 157... Line 162...
157
        first %= PLB_SIZE;
162
        first %= PLB_SIZE;
158
    }
163
    }
159
   
164
   
160
    if (first >= last) {
165
    if (first >= last) {
161
        /* Root entry */
166
        /* Root entry */
162
        if (lflag & L_DIRECTORY)
167
        if (!(lflag & L_FILE))
163
            ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0);
168
            ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0);
164
        else
169
        else
165
            ipc_answer_0(rid, ENOENT);
170
            ipc_answer_0(rid, ENOENT);
166
    } else {
171
    } else {
167
        if (lflag & L_FILE) {
172
        if (!(lflag & L_DIRECTORY)) {
168
            size_t len;
173
            size_t len;
169
            if (last >= first)
174
            if (last >= first)
170
                len = last - first + 1;
175
                len = last - first + 1;
171
            else
176
            else
172
                len = first + PLB_SIZE - last + 1;
177
                len = first + PLB_SIZE - last + 1;
Line 193... Line 198...
193
            if (lflag & L_OPEN) {
198
            if (lflag & L_OPEN) {
194
                unsigned long key[] = {
199
                unsigned long key[] = {
195
                    [DEVICES_KEY_HANDLE] = (unsigned long) handle
200
                    [DEVICES_KEY_HANDLE] = (unsigned long) handle
196
                };
201
                };
197
               
202
               
-
 
203
                fibril_mutex_lock(&devices_mutex);
198
                link_t *lnk = hash_table_find(&devices, key);
204
                link_t *lnk = hash_table_find(&devices, key);
199
                if (lnk == NULL) {
205
                if (lnk == NULL) {
200
                    int phone = devmap_device_connect(handle, 0);
206
                    int phone = devmap_device_connect(handle, 0);
201
                    if (phone < 0) {
207
                    if (phone < 0) {
-
 
208
                        fibril_mutex_unlock(&devices_mutex);
202
                        free(name);
209
                        free(name);
203
                        ipc_answer_0(rid, ENOENT);
210
                        ipc_answer_0(rid, ENOENT);
204
                        return;
211
                        return;
205
                    }
212
                    }
206
                   
213
                   
207
                    device_t *dev = (device_t *) malloc(sizeof(device_t));
214
                    device_t *dev = (device_t *) malloc(sizeof(device_t));
208
                    if (dev == NULL) {
215
                    if (dev == NULL) {
-
 
216
                        fibril_mutex_unlock(&devices_mutex);
209
                        free(name);
217
                        free(name);
210
                        ipc_answer_0(rid, ENOMEM);
218
                        ipc_answer_0(rid, ENOMEM);
211
                        return;
219
                        return;
212
                    }
220
                    }
213
                   
221
                   
Line 218... Line 226...
218
                    hash_table_insert(&devices, key, &dev->link);
226
                    hash_table_insert(&devices, key, &dev->link);
219
                } else {
227
                } else {
220
                    device_t *dev = hash_table_get_instance(lnk, device_t, link);
228
                    device_t *dev = hash_table_get_instance(lnk, device_t, link);
221
                    dev->refcount++;
229
                    dev->refcount++;
222
                }
230
                }
-
 
231
                fibril_mutex_unlock(&devices_mutex);
223
            }
232
            }
224
           
233
           
225
            free(name);
234
            free(name);
226
           
235
           
227
            ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1);
236
            ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1);
Line 236... Line 245...
236
   
245
   
237
    unsigned long key[] = {
246
    unsigned long key[] = {
238
        [DEVICES_KEY_HANDLE] = (unsigned long) handle
247
        [DEVICES_KEY_HANDLE] = (unsigned long) handle
239
    };
248
    };
240
   
249
   
-
 
250
    fibril_mutex_lock(&devices_mutex);
241
    link_t *lnk = hash_table_find(&devices, key);
251
    link_t *lnk = hash_table_find(&devices, key);
242
    if (lnk == NULL) {
252
    if (lnk == NULL) {
243
        int phone = devmap_device_connect(handle, 0);
253
        int phone = devmap_device_connect(handle, 0);
244
        if (phone < 0) {
254
        if (phone < 0) {
-
 
255
            fibril_mutex_unlock(&devices_mutex);
245
            ipc_answer_0(rid, ENOENT);
256
            ipc_answer_0(rid, ENOENT);
246
            return;
257
            return;
247
        }
258
        }
248
       
259
       
249
        device_t *dev = (device_t *) malloc(sizeof(device_t));
260
        device_t *dev = (device_t *) malloc(sizeof(device_t));
250
        if (dev == NULL) {
261
        if (dev == NULL) {
-
 
262
            fibril_mutex_unlock(&devices_mutex);
251
            ipc_answer_0(rid, ENOMEM);
263
            ipc_answer_0(rid, ENOMEM);
252
            return;
264
            return;
253
        }
265
        }
254
       
266
       
255
        dev->handle = handle;
267
        dev->handle = handle;
Line 259... Line 271...
259
        hash_table_insert(&devices, key, &dev->link);
271
        hash_table_insert(&devices, key, &dev->link);
260
    } else {
272
    } else {
261
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
273
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
262
        dev->refcount++;
274
        dev->refcount++;
263
    }
275
    }
-
 
276
    fibril_mutex_unlock(&devices_mutex);
264
   
277
   
265
    ipc_answer_3(rid, EOK, 0, 1, L_FILE);
278
    ipc_answer_3(rid, EOK, 0, 1, L_FILE);
266
}
279
}
267
 
280
 
268
void devfs_device(ipc_callid_t rid, ipc_call_t *request)
281
void devfs_stat(ipc_callid_t rid, ipc_call_t *request)
269
{
282
{
-
 
283
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
270
    fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
284
    fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
271
   
285
   
-
 
286
    ipc_callid_t callid;
-
 
287
    size_t size;
-
 
288
    if (!ipc_data_read_receive(&callid, &size) ||
-
 
289
        size != sizeof(struct stat)) {
-
 
290
        ipc_answer_0(callid, EINVAL);
-
 
291
        ipc_answer_0(rid, EINVAL);
-
 
292
        return;
-
 
293
    }
-
 
294
 
-
 
295
    struct stat stat;
-
 
296
    memset(&stat, 0, sizeof(struct stat));
-
 
297
 
-
 
298
    stat.fs_handle = devfs_reg.fs_handle;
-
 
299
    stat.dev_handle = dev_handle;
-
 
300
    stat.index = index;
-
 
301
    stat.lnkcnt = 1;
-
 
302
    stat.is_file = (index != 0);
-
 
303
    stat.size = 0;
-
 
304
   
272
    if (index != 0) {
305
    if (index != 0) {
273
        unsigned long key[] = {
306
        unsigned long key[] = {
274
            [DEVICES_KEY_HANDLE] = (unsigned long) index
307
            [DEVICES_KEY_HANDLE] = (unsigned long) index
275
        };
308
        };
276
       
309
       
-
 
310
        fibril_mutex_lock(&devices_mutex);
277
        link_t *lnk = hash_table_find(&devices, key);
311
        link_t *lnk = hash_table_find(&devices, key);
278
        if (lnk == NULL) {
312
        if (lnk != NULL)
279
            ipc_answer_0(rid, ENOENT);
313
            stat.devfs_stat.device = (dev_handle_t)index;
280
            return;
314
        fibril_mutex_unlock(&devices_mutex);
281
        }
315
    }
282
       
316
 
283
        ipc_answer_1(rid, EOK, (ipcarg_t) index);
317
    ipc_data_read_finalize(callid, &stat, sizeof(struct stat));
284
    } else
-
 
285
        ipc_answer_0(rid, ENOTSUP);
318
    ipc_answer_0(rid, EOK);
286
}
319
}
287
 
320
 
288
void devfs_read(ipc_callid_t rid, ipc_call_t *request)
321
void devfs_read(ipc_callid_t rid, ipc_call_t *request)
289
{
322
{
290
    fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
323
    fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
Line 293... Line 326...
293
    if (index != 0) {
326
    if (index != 0) {
294
        unsigned long key[] = {
327
        unsigned long key[] = {
295
            [DEVICES_KEY_HANDLE] = (unsigned long) index
328
            [DEVICES_KEY_HANDLE] = (unsigned long) index
296
        };
329
        };
297
       
330
       
-
 
331
        fibril_mutex_lock(&devices_mutex);
298
        link_t *lnk = hash_table_find(&devices, key);
332
        link_t *lnk = hash_table_find(&devices, key);
299
        if (lnk == NULL) {
333
        if (lnk == NULL) {
-
 
334
            fibril_mutex_unlock(&devices_mutex);
300
            ipc_answer_0(rid, ENOENT);
335
            ipc_answer_0(rid, ENOENT);
301
            return;
336
            return;
302
        }
337
        }
303
       
338
       
304
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
339
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
305
       
340
       
306
        ipc_callid_t callid;
341
        ipc_callid_t callid;
307
        if (!ipc_data_read_receive(&callid, NULL)) {
342
        if (!ipc_data_read_receive(&callid, NULL)) {
-
 
343
            fibril_mutex_unlock(&devices_mutex);
308
            ipc_answer_0(callid, EINVAL);
344
            ipc_answer_0(callid, EINVAL);
309
            ipc_answer_0(rid, EINVAL);
345
            ipc_answer_0(rid, EINVAL);
310
            return;
346
            return;
311
        }
347
        }
312
       
348
       
Line 316... Line 352...
316
            IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
352
            IPC_GET_ARG1(*request), IPC_GET_ARG2(*request),
317
            IPC_GET_ARG3(*request), &answer);
353
            IPC_GET_ARG3(*request), &answer);
318
       
354
       
319
        /* Forward the IPC_M_DATA_READ request to the driver */
355
        /* Forward the IPC_M_DATA_READ request to the driver */
320
        ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
356
        ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
-
 
357
        fibril_mutex_unlock(&devices_mutex);
321
       
358
       
322
        /* Wait for reply from the driver. */
359
        /* Wait for reply from the driver. */
323
        ipcarg_t rc;
360
        ipcarg_t rc;
324
        async_wait_for(msg, &rc);
361
        async_wait_for(msg, &rc);
325
        size_t bytes = IPC_GET_ARG1(answer);
362
        size_t bytes = IPC_GET_ARG1(answer);
Line 367... Line 404...
367
    if (index != 0) {
404
    if (index != 0) {
368
        unsigned long key[] = {
405
        unsigned long key[] = {
369
            [DEVICES_KEY_HANDLE] = (unsigned long) index
406
            [DEVICES_KEY_HANDLE] = (unsigned long) index
370
        };
407
        };
371
       
408
       
-
 
409
        fibril_mutex_lock(&devices_mutex);
372
        link_t *lnk = hash_table_find(&devices, key);
410
        link_t *lnk = hash_table_find(&devices, key);
373
        if (lnk == NULL) {
411
        if (lnk == NULL) {
-
 
412
            fibril_mutex_unlock(&devices_mutex);
374
            ipc_answer_0(rid, ENOENT);
413
            ipc_answer_0(rid, ENOENT);
375
            return;
414
            return;
376
        }
415
        }
377
       
416
       
378
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
417
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
379
       
418
       
380
        ipc_callid_t callid;
419
        ipc_callid_t callid;
381
        if (!ipc_data_write_receive(&callid, NULL)) {
420
        if (!ipc_data_write_receive(&callid, NULL)) {
-
 
421
            fibril_mutex_unlock(&devices_mutex);
382
            ipc_answer_0(callid, EINVAL);
422
            ipc_answer_0(callid, EINVAL);
383
            ipc_answer_0(rid, EINVAL);
423
            ipc_answer_0(rid, EINVAL);
384
            return;
424
            return;
385
        }
425
        }
386
       
426
       
Line 391... Line 431...
391
            IPC_GET_ARG3(*request), &answer);
431
            IPC_GET_ARG3(*request), &answer);
392
       
432
       
393
        /* Forward the IPC_M_DATA_WRITE request to the driver */
433
        /* Forward the IPC_M_DATA_WRITE request to the driver */
394
        ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
434
        ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
395
       
435
       
-
 
436
        fibril_mutex_unlock(&devices_mutex);
-
 
437
       
396
        /* Wait for reply from the driver. */
438
        /* Wait for reply from the driver. */
397
        ipcarg_t rc;
439
        ipcarg_t rc;
398
        async_wait_for(msg, &rc);
440
        async_wait_for(msg, &rc);
399
        size_t bytes = IPC_GET_ARG1(answer);
441
        size_t bytes = IPC_GET_ARG1(answer);
400
       
442
       
Line 418... Line 460...
418
    if (index != 0) {
460
    if (index != 0) {
419
        unsigned long key[] = {
461
        unsigned long key[] = {
420
            [DEVICES_KEY_HANDLE] = (unsigned long) index
462
            [DEVICES_KEY_HANDLE] = (unsigned long) index
421
        };
463
        };
422
       
464
       
-
 
465
        fibril_mutex_lock(&devices_mutex);
423
        link_t *lnk = hash_table_find(&devices, key);
466
        link_t *lnk = hash_table_find(&devices, key);
424
        if (lnk == NULL) {
467
        if (lnk == NULL) {
-
 
468
            fibril_mutex_unlock(&devices_mutex);
425
            ipc_answer_0(rid, ENOENT);
469
            ipc_answer_0(rid, ENOENT);
426
            return;
470
            return;
427
        }
471
        }
428
       
472
       
429
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
473
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
Line 432... Line 476...
432
        if (dev->refcount == 0) {
476
        if (dev->refcount == 0) {
433
            ipc_hangup(dev->phone);
477
            ipc_hangup(dev->phone);
434
            hash_table_remove(&devices, key, DEVICES_KEYS);
478
            hash_table_remove(&devices, key, DEVICES_KEYS);
435
        }
479
        }
436
       
480
       
-
 
481
        fibril_mutex_unlock(&devices_mutex);
-
 
482
       
437
        ipc_answer_0(rid, EOK);
483
        ipc_answer_0(rid, EOK);
438
    } else
484
    } else
439
        ipc_answer_0(rid, ENOTSUP);
485
        ipc_answer_0(rid, ENOTSUP);
440
}
486
}
441
 
487
 
Line 446... Line 492...
446
    if (index != 0) {
492
    if (index != 0) {
447
        unsigned long key[] = {
493
        unsigned long key[] = {
448
            [DEVICES_KEY_HANDLE] = (unsigned long) index
494
            [DEVICES_KEY_HANDLE] = (unsigned long) index
449
        };
495
        };
450
       
496
       
-
 
497
        fibril_mutex_lock(&devices_mutex);
451
        link_t *lnk = hash_table_find(&devices, key);
498
        link_t *lnk = hash_table_find(&devices, key);
452
        if (lnk == NULL) {
499
        if (lnk == NULL) {
-
 
500
            fibril_mutex_unlock(&devices_mutex);
453
            ipc_answer_0(rid, ENOENT);
501
            ipc_answer_0(rid, ENOENT);
454
            return;
502
            return;
455
        }
503
        }
456
       
504
       
457
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
505
        device_t *dev = hash_table_get_instance(lnk, device_t, link);
Line 459... Line 507...
459
        /* Make a request at the driver */
507
        /* Make a request at the driver */
460
        ipc_call_t answer;
508
        ipc_call_t answer;
461
        aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request),
509
        aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request),
462
            IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
510
            IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer);
463
       
511
       
-
 
512
        fibril_mutex_unlock(&devices_mutex);
-
 
513
       
464
        /* Wait for reply from the driver */
514
        /* Wait for reply from the driver */
465
        ipcarg_t rc;
515
        ipcarg_t rc;
466
        async_wait_for(msg, &rc);
516
        async_wait_for(msg, &rc);
467
       
517
       
468
        /* Driver reply is the final result of the whole operation */
518
        /* Driver reply is the final result of the whole operation */