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