Rev 4509 | Rev 4585 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4509 | Rev 4561 | ||
---|---|---|---|
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> |
45 | #include "devfs.h" |
46 | #include "devfs.h" |
46 | #include "devfs_ops.h" |
47 | #include "devfs_ops.h" |
47 | 48 | ||
48 | #define PLB_GET_CHAR(pos) (devfs_reg.plb_ro[pos % PLB_SIZE]) |
49 | #define PLB_GET_CHAR(pos) (devfs_reg.plb_ro[pos % PLB_SIZE]) |
Line 56... | Line 57... | ||
56 | } device_t; |
57 | } device_t; |
57 | 58 | ||
58 | /** Hash table of opened devices */ |
59 | /** Hash table of opened devices */ |
59 | static hash_table_t devices; |
60 | static hash_table_t devices; |
60 | 61 | ||
- | 62 | /** Hash table mutex */ |
|
- | 63 | static FIBRIL_MUTEX_INITIALIZE(devices_mutex); |
|
- | 64 | ||
61 | #define DEVICES_KEYS 1 |
65 | #define DEVICES_KEYS 1 |
62 | #define DEVICES_KEY_HANDLE 0 |
66 | #define DEVICES_KEY_HANDLE 0 |
63 | #define DEVICES_BUCKETS 256 |
67 | #define DEVICES_BUCKETS 256 |
64 | 68 | ||
65 | /* Implementation of hash table interface for the nodes hash table. */ |
69 | /* Implementation of hash table interface for the nodes hash table. */ |
Line 193... | Line 197... | ||
193 | if (lflag & L_OPEN) { |
197 | if (lflag & L_OPEN) { |
194 | unsigned long key[] = { |
198 | unsigned long key[] = { |
195 | [DEVICES_KEY_HANDLE] = (unsigned long) handle |
199 | [DEVICES_KEY_HANDLE] = (unsigned long) handle |
196 | }; |
200 | }; |
197 | 201 | ||
- | 202 | fibril_mutex_lock(&devices_mutex); |
|
198 | link_t *lnk = hash_table_find(&devices, key); |
203 | link_t *lnk = hash_table_find(&devices, key); |
199 | if (lnk == NULL) { |
204 | if (lnk == NULL) { |
200 | int phone = devmap_device_connect(handle, 0); |
205 | int phone = devmap_device_connect(handle, 0); |
201 | if (phone < 0) { |
206 | if (phone < 0) { |
- | 207 | fibril_mutex_unlock(&devices_mutex); |
|
202 | free(name); |
208 | free(name); |
203 | ipc_answer_0(rid, ENOENT); |
209 | ipc_answer_0(rid, ENOENT); |
204 | return; |
210 | return; |
205 | } |
211 | } |
206 | 212 | ||
207 | device_t *dev = (device_t *) malloc(sizeof(device_t)); |
213 | device_t *dev = (device_t *) malloc(sizeof(device_t)); |
208 | if (dev == NULL) { |
214 | if (dev == NULL) { |
- | 215 | fibril_mutex_unlock(&devices_mutex); |
|
209 | free(name); |
216 | free(name); |
210 | ipc_answer_0(rid, ENOMEM); |
217 | ipc_answer_0(rid, ENOMEM); |
211 | return; |
218 | return; |
212 | } |
219 | } |
213 | 220 | ||
Line 218... | Line 225... | ||
218 | hash_table_insert(&devices, key, &dev->link); |
225 | hash_table_insert(&devices, key, &dev->link); |
219 | } else { |
226 | } else { |
220 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
227 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
221 | dev->refcount++; |
228 | dev->refcount++; |
222 | } |
229 | } |
- | 230 | fibril_mutex_unlock(&devices_mutex); |
|
223 | } |
231 | } |
224 | 232 | ||
225 | free(name); |
233 | free(name); |
226 | 234 | ||
227 | ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1); |
235 | ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1); |
Line 236... | Line 244... | ||
236 | 244 | ||
237 | unsigned long key[] = { |
245 | unsigned long key[] = { |
238 | [DEVICES_KEY_HANDLE] = (unsigned long) handle |
246 | [DEVICES_KEY_HANDLE] = (unsigned long) handle |
239 | }; |
247 | }; |
240 | 248 | ||
- | 249 | fibril_mutex_lock(&devices_mutex); |
|
241 | link_t *lnk = hash_table_find(&devices, key); |
250 | link_t *lnk = hash_table_find(&devices, key); |
242 | if (lnk == NULL) { |
251 | if (lnk == NULL) { |
243 | int phone = devmap_device_connect(handle, 0); |
252 | int phone = devmap_device_connect(handle, 0); |
244 | if (phone < 0) { |
253 | if (phone < 0) { |
- | 254 | fibril_mutex_unlock(&devices_mutex); |
|
245 | ipc_answer_0(rid, ENOENT); |
255 | ipc_answer_0(rid, ENOENT); |
246 | return; |
256 | return; |
247 | } |
257 | } |
248 | 258 | ||
249 | device_t *dev = (device_t *) malloc(sizeof(device_t)); |
259 | device_t *dev = (device_t *) malloc(sizeof(device_t)); |
250 | if (dev == NULL) { |
260 | if (dev == NULL) { |
- | 261 | fibril_mutex_unlock(&devices_mutex); |
|
251 | ipc_answer_0(rid, ENOMEM); |
262 | ipc_answer_0(rid, ENOMEM); |
252 | return; |
263 | return; |
253 | } |
264 | } |
254 | 265 | ||
255 | dev->handle = handle; |
266 | dev->handle = handle; |
Line 259... | Line 270... | ||
259 | hash_table_insert(&devices, key, &dev->link); |
270 | hash_table_insert(&devices, key, &dev->link); |
260 | } else { |
271 | } else { |
261 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
272 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
262 | dev->refcount++; |
273 | dev->refcount++; |
263 | } |
274 | } |
- | 275 | fibril_mutex_unlock(&devices_mutex); |
|
264 | 276 | ||
265 | ipc_answer_3(rid, EOK, 0, 1, L_FILE); |
277 | ipc_answer_3(rid, EOK, 0, 1, L_FILE); |
266 | } |
278 | } |
267 | 279 | ||
268 | void devfs_device(ipc_callid_t rid, ipc_call_t *request) |
280 | void devfs_device(ipc_callid_t rid, ipc_call_t *request) |
Line 272... | Line 284... | ||
272 | if (index != 0) { |
284 | if (index != 0) { |
273 | unsigned long key[] = { |
285 | unsigned long key[] = { |
274 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
286 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
275 | }; |
287 | }; |
276 | 288 | ||
- | 289 | fibril_mutex_lock(&devices_mutex); |
|
277 | link_t *lnk = hash_table_find(&devices, key); |
290 | link_t *lnk = hash_table_find(&devices, key); |
278 | if (lnk == NULL) { |
291 | if (lnk == NULL) { |
- | 292 | fibril_mutex_unlock(&devices_mutex); |
|
279 | ipc_answer_0(rid, ENOENT); |
293 | ipc_answer_0(rid, ENOENT); |
280 | return; |
294 | return; |
281 | } |
295 | } |
- | 296 | fibril_mutex_unlock(&devices_mutex); |
|
282 | 297 | ||
283 | ipc_answer_1(rid, EOK, (ipcarg_t) index); |
298 | ipc_answer_1(rid, EOK, (ipcarg_t) index); |
284 | } else |
299 | } else |
285 | ipc_answer_0(rid, ENOTSUP); |
300 | ipc_answer_0(rid, ENOTSUP); |
286 | } |
301 | } |
Line 293... | Line 308... | ||
293 | if (index != 0) { |
308 | if (index != 0) { |
294 | unsigned long key[] = { |
309 | unsigned long key[] = { |
295 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
310 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
296 | }; |
311 | }; |
297 | 312 | ||
- | 313 | fibril_mutex_lock(&devices_mutex); |
|
298 | link_t *lnk = hash_table_find(&devices, key); |
314 | link_t *lnk = hash_table_find(&devices, key); |
299 | if (lnk == NULL) { |
315 | if (lnk == NULL) { |
- | 316 | fibril_mutex_unlock(&devices_mutex); |
|
300 | ipc_answer_0(rid, ENOENT); |
317 | ipc_answer_0(rid, ENOENT); |
301 | return; |
318 | return; |
302 | } |
319 | } |
303 | 320 | ||
304 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
321 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
305 | 322 | ||
306 | ipc_callid_t callid; |
323 | ipc_callid_t callid; |
307 | if (!ipc_data_read_receive(&callid, NULL)) { |
324 | if (!ipc_data_read_receive(&callid, NULL)) { |
- | 325 | fibril_mutex_unlock(&devices_mutex); |
|
308 | ipc_answer_0(callid, EINVAL); |
326 | ipc_answer_0(callid, EINVAL); |
309 | ipc_answer_0(rid, EINVAL); |
327 | ipc_answer_0(rid, EINVAL); |
310 | return; |
328 | return; |
311 | } |
329 | } |
312 | 330 | ||
Line 316... | Line 334... | ||
316 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
334 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
317 | IPC_GET_ARG3(*request), &answer); |
335 | IPC_GET_ARG3(*request), &answer); |
318 | 336 | ||
319 | /* Forward the IPC_M_DATA_READ request to the driver */ |
337 | /* 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); |
338 | ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
- | 339 | fibril_mutex_unlock(&devices_mutex); |
|
321 | 340 | ||
322 | /* Wait for reply from the driver. */ |
341 | /* Wait for reply from the driver. */ |
323 | ipcarg_t rc; |
342 | ipcarg_t rc; |
324 | async_wait_for(msg, &rc); |
343 | async_wait_for(msg, &rc); |
325 | size_t bytes = IPC_GET_ARG1(answer); |
344 | size_t bytes = IPC_GET_ARG1(answer); |
Line 367... | Line 386... | ||
367 | if (index != 0) { |
386 | if (index != 0) { |
368 | unsigned long key[] = { |
387 | unsigned long key[] = { |
369 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
388 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
370 | }; |
389 | }; |
371 | 390 | ||
- | 391 | fibril_mutex_lock(&devices_mutex); |
|
372 | link_t *lnk = hash_table_find(&devices, key); |
392 | link_t *lnk = hash_table_find(&devices, key); |
373 | if (lnk == NULL) { |
393 | if (lnk == NULL) { |
- | 394 | fibril_mutex_unlock(&devices_mutex); |
|
374 | ipc_answer_0(rid, ENOENT); |
395 | ipc_answer_0(rid, ENOENT); |
375 | return; |
396 | return; |
376 | } |
397 | } |
377 | 398 | ||
378 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
399 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
379 | 400 | ||
380 | ipc_callid_t callid; |
401 | ipc_callid_t callid; |
381 | if (!ipc_data_write_receive(&callid, NULL)) { |
402 | if (!ipc_data_write_receive(&callid, NULL)) { |
- | 403 | fibril_mutex_unlock(&devices_mutex); |
|
382 | ipc_answer_0(callid, EINVAL); |
404 | ipc_answer_0(callid, EINVAL); |
383 | ipc_answer_0(rid, EINVAL); |
405 | ipc_answer_0(rid, EINVAL); |
384 | return; |
406 | return; |
385 | } |
407 | } |
386 | 408 | ||
Line 391... | Line 413... | ||
391 | IPC_GET_ARG3(*request), &answer); |
413 | IPC_GET_ARG3(*request), &answer); |
392 | 414 | ||
393 | /* Forward the IPC_M_DATA_WRITE request to the driver */ |
415 | /* 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); |
416 | ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
395 | 417 | ||
- | 418 | fibril_mutex_unlock(&devices_mutex); |
|
- | 419 | ||
396 | /* Wait for reply from the driver. */ |
420 | /* Wait for reply from the driver. */ |
397 | ipcarg_t rc; |
421 | ipcarg_t rc; |
398 | async_wait_for(msg, &rc); |
422 | async_wait_for(msg, &rc); |
399 | size_t bytes = IPC_GET_ARG1(answer); |
423 | size_t bytes = IPC_GET_ARG1(answer); |
400 | 424 | ||
Line 418... | Line 442... | ||
418 | if (index != 0) { |
442 | if (index != 0) { |
419 | unsigned long key[] = { |
443 | unsigned long key[] = { |
420 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
444 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
421 | }; |
445 | }; |
422 | 446 | ||
- | 447 | fibril_mutex_lock(&devices_mutex); |
|
423 | link_t *lnk = hash_table_find(&devices, key); |
448 | link_t *lnk = hash_table_find(&devices, key); |
424 | if (lnk == NULL) { |
449 | if (lnk == NULL) { |
- | 450 | fibril_mutex_unlock(&devices_mutex); |
|
425 | ipc_answer_0(rid, ENOENT); |
451 | ipc_answer_0(rid, ENOENT); |
426 | return; |
452 | return; |
427 | } |
453 | } |
428 | 454 | ||
429 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
455 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
Line 432... | Line 458... | ||
432 | if (dev->refcount == 0) { |
458 | if (dev->refcount == 0) { |
433 | ipc_hangup(dev->phone); |
459 | ipc_hangup(dev->phone); |
434 | hash_table_remove(&devices, key, DEVICES_KEYS); |
460 | hash_table_remove(&devices, key, DEVICES_KEYS); |
435 | } |
461 | } |
436 | 462 | ||
- | 463 | fibril_mutex_unlock(&devices_mutex); |
|
- | 464 | ||
437 | ipc_answer_0(rid, EOK); |
465 | ipc_answer_0(rid, EOK); |
438 | } else |
466 | } else |
439 | ipc_answer_0(rid, ENOTSUP); |
467 | ipc_answer_0(rid, ENOTSUP); |
440 | } |
468 | } |
441 | 469 | ||
Line 446... | Line 474... | ||
446 | if (index != 0) { |
474 | if (index != 0) { |
447 | unsigned long key[] = { |
475 | unsigned long key[] = { |
448 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
476 | [DEVICES_KEY_HANDLE] = (unsigned long) index |
449 | }; |
477 | }; |
450 | 478 | ||
- | 479 | fibril_mutex_lock(&devices_mutex); |
|
451 | link_t *lnk = hash_table_find(&devices, key); |
480 | link_t *lnk = hash_table_find(&devices, key); |
452 | if (lnk == NULL) { |
481 | if (lnk == NULL) { |
- | 482 | fibril_mutex_unlock(&devices_mutex); |
|
453 | ipc_answer_0(rid, ENOENT); |
483 | ipc_answer_0(rid, ENOENT); |
454 | return; |
484 | return; |
455 | } |
485 | } |
456 | 486 | ||
457 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
487 | device_t *dev = hash_table_get_instance(lnk, device_t, link); |
Line 459... | Line 489... | ||
459 | /* Make a request at the driver */ |
489 | /* Make a request at the driver */ |
460 | ipc_call_t answer; |
490 | ipc_call_t answer; |
461 | aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request), |
491 | aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request), |
462 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer); |
492 | IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer); |
463 | 493 | ||
- | 494 | fibril_mutex_unlock(&devices_mutex); |
|
- | 495 | ||
464 | /* Wait for reply from the driver */ |
496 | /* Wait for reply from the driver */ |
465 | ipcarg_t rc; |
497 | ipcarg_t rc; |
466 | async_wait_for(msg, &rc); |
498 | async_wait_for(msg, &rc); |
467 | 499 | ||
468 | /* Driver reply is the final result of the whole operation */ |
500 | /* Driver reply is the final result of the whole operation */ |