1 | /*
|
---|
2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @defgroup libdrv generic device driver support.
|
---|
32 | * @brief HelenOS generic device driver support.
|
---|
33 | * @{
|
---|
34 | */
|
---|
35 |
|
---|
36 | /** @file
|
---|
37 | */
|
---|
38 |
|
---|
39 | #define _DDF_DATA_IMPLANT
|
---|
40 |
|
---|
41 | #include <assert.h>
|
---|
42 | #include <ipc/services.h>
|
---|
43 | #include <ipc/ns.h>
|
---|
44 | #include <async.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <errno.h>
|
---|
47 | #include <stdbool.h>
|
---|
48 | #include <fibril_synch.h>
|
---|
49 | #include <stdlib.h>
|
---|
50 | #include <str.h>
|
---|
51 | #include <str_error.h>
|
---|
52 | #include <ctype.h>
|
---|
53 | #include <errno.h>
|
---|
54 | #include <inttypes.h>
|
---|
55 | #include <devman.h>
|
---|
56 |
|
---|
57 | #include <ipc/driver.h>
|
---|
58 |
|
---|
59 | #include "dev_iface.h"
|
---|
60 | #include "ddf/driver.h"
|
---|
61 | #include "ddf/interrupt.h"
|
---|
62 | #include "private/driver.h"
|
---|
63 |
|
---|
64 | /** Driver structure */
|
---|
65 | static driver_t *driver;
|
---|
66 |
|
---|
67 | /** Devices */
|
---|
68 | LIST_INITIALIZE(devices);
|
---|
69 | FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
---|
70 |
|
---|
71 | /** Functions */
|
---|
72 | LIST_INITIALIZE(functions);
|
---|
73 | FIBRIL_MUTEX_INITIALIZE(functions_mutex);
|
---|
74 |
|
---|
75 | static ddf_dev_t *create_device(void);
|
---|
76 | static void delete_device(ddf_dev_t *);
|
---|
77 | static void dev_add_ref(ddf_dev_t *);
|
---|
78 | static void dev_del_ref(ddf_dev_t *);
|
---|
79 | static void fun_add_ref(ddf_fun_t *);
|
---|
80 | static void fun_del_ref(ddf_fun_t *);
|
---|
81 | static remote_handler_t *function_get_default_handler(ddf_fun_t *);
|
---|
82 | static void *function_get_ops(ddf_fun_t *, dev_inferface_idx_t);
|
---|
83 |
|
---|
84 | static void add_to_functions_list(ddf_fun_t *fun)
|
---|
85 | {
|
---|
86 | fibril_mutex_lock(&functions_mutex);
|
---|
87 | list_append(&fun->link, &functions);
|
---|
88 | fibril_mutex_unlock(&functions_mutex);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void remove_from_functions_list(ddf_fun_t *fun)
|
---|
92 | {
|
---|
93 | fibril_mutex_lock(&functions_mutex);
|
---|
94 | list_remove(&fun->link);
|
---|
95 | fibril_mutex_unlock(&functions_mutex);
|
---|
96 | }
|
---|
97 |
|
---|
98 | static ddf_dev_t *driver_get_device(devman_handle_t handle)
|
---|
99 | {
|
---|
100 | ddf_dev_t *dev = NULL;
|
---|
101 |
|
---|
102 | assert(fibril_mutex_is_locked(&devices_mutex));
|
---|
103 |
|
---|
104 | list_foreach(devices, link) {
|
---|
105 | dev = list_get_instance(link, ddf_dev_t, link);
|
---|
106 | if (dev->handle == handle)
|
---|
107 | return dev;
|
---|
108 | }
|
---|
109 |
|
---|
110 | return NULL;
|
---|
111 | }
|
---|
112 |
|
---|
113 | static ddf_fun_t *driver_get_function(devman_handle_t handle)
|
---|
114 | {
|
---|
115 | ddf_fun_t *fun = NULL;
|
---|
116 |
|
---|
117 | assert(fibril_mutex_is_locked(&functions_mutex));
|
---|
118 |
|
---|
119 | list_foreach(functions, link) {
|
---|
120 | fun = list_get_instance(link, ddf_fun_t, link);
|
---|
121 | if (fun->handle == handle)
|
---|
122 | return fun;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return NULL;
|
---|
126 | }
|
---|
127 |
|
---|
128 | static void driver_dev_add(ipc_callid_t iid, ipc_call_t *icall)
|
---|
129 | {
|
---|
130 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
131 | devman_handle_t parent_fun_handle = IPC_GET_ARG2(*icall);
|
---|
132 |
|
---|
133 | ddf_dev_t *dev = create_device();
|
---|
134 |
|
---|
135 | /* Add one reference that will be dropped by driver_dev_remove() */
|
---|
136 | dev_add_ref(dev);
|
---|
137 | dev->handle = dev_handle;
|
---|
138 |
|
---|
139 | char *dev_name = NULL;
|
---|
140 | async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
---|
141 | dev->name = dev_name;
|
---|
142 |
|
---|
143 | /*
|
---|
144 | * Currently not used, parent fun handle is stored in context
|
---|
145 | * of the connection to the parent device driver.
|
---|
146 | */
|
---|
147 | (void) parent_fun_handle;
|
---|
148 |
|
---|
149 | int res = driver->driver_ops->dev_add(dev);
|
---|
150 |
|
---|
151 | if (res != EOK) {
|
---|
152 | dev_del_ref(dev);
|
---|
153 | async_answer_0(iid, res);
|
---|
154 | return;
|
---|
155 | }
|
---|
156 |
|
---|
157 | fibril_mutex_lock(&devices_mutex);
|
---|
158 | list_append(&dev->link, &devices);
|
---|
159 | fibril_mutex_unlock(&devices_mutex);
|
---|
160 |
|
---|
161 | async_answer_0(iid, res);
|
---|
162 | }
|
---|
163 |
|
---|
164 | static void driver_dev_remove(ipc_callid_t iid, ipc_call_t *icall)
|
---|
165 | {
|
---|
166 | devman_handle_t devh = IPC_GET_ARG1(*icall);
|
---|
167 |
|
---|
168 | fibril_mutex_lock(&devices_mutex);
|
---|
169 | ddf_dev_t *dev = driver_get_device(devh);
|
---|
170 | if (dev != NULL)
|
---|
171 | dev_add_ref(dev);
|
---|
172 | fibril_mutex_unlock(&devices_mutex);
|
---|
173 |
|
---|
174 | if (dev == NULL) {
|
---|
175 | async_answer_0(iid, ENOENT);
|
---|
176 | return;
|
---|
177 | }
|
---|
178 |
|
---|
179 | int rc;
|
---|
180 |
|
---|
181 | if (driver->driver_ops->dev_remove != NULL)
|
---|
182 | rc = driver->driver_ops->dev_remove(dev);
|
---|
183 | else
|
---|
184 | rc = ENOTSUP;
|
---|
185 |
|
---|
186 | if (rc == EOK)
|
---|
187 | dev_del_ref(dev);
|
---|
188 |
|
---|
189 | async_answer_0(iid, (sysarg_t) rc);
|
---|
190 | }
|
---|
191 |
|
---|
192 | static void driver_dev_gone(ipc_callid_t iid, ipc_call_t *icall)
|
---|
193 | {
|
---|
194 | devman_handle_t devh = IPC_GET_ARG1(*icall);
|
---|
195 |
|
---|
196 | fibril_mutex_lock(&devices_mutex);
|
---|
197 | ddf_dev_t *dev = driver_get_device(devh);
|
---|
198 | if (dev != NULL)
|
---|
199 | dev_add_ref(dev);
|
---|
200 | fibril_mutex_unlock(&devices_mutex);
|
---|
201 |
|
---|
202 | if (dev == NULL) {
|
---|
203 | async_answer_0(iid, ENOENT);
|
---|
204 | return;
|
---|
205 | }
|
---|
206 |
|
---|
207 | int rc;
|
---|
208 |
|
---|
209 | if (driver->driver_ops->dev_gone != NULL)
|
---|
210 | rc = driver->driver_ops->dev_gone(dev);
|
---|
211 | else
|
---|
212 | rc = ENOTSUP;
|
---|
213 |
|
---|
214 | if (rc == EOK)
|
---|
215 | dev_del_ref(dev);
|
---|
216 |
|
---|
217 | async_answer_0(iid, (sysarg_t) rc);
|
---|
218 | }
|
---|
219 |
|
---|
220 | static void driver_fun_online(ipc_callid_t iid, ipc_call_t *icall)
|
---|
221 | {
|
---|
222 | devman_handle_t funh = IPC_GET_ARG1(*icall);
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * Look the function up. Bump reference count so that
|
---|
226 | * the function continues to exist until we return
|
---|
227 | * from the driver.
|
---|
228 | */
|
---|
229 | fibril_mutex_lock(&functions_mutex);
|
---|
230 |
|
---|
231 | ddf_fun_t *fun = driver_get_function(funh);
|
---|
232 | if (fun != NULL)
|
---|
233 | fun_add_ref(fun);
|
---|
234 |
|
---|
235 | fibril_mutex_unlock(&functions_mutex);
|
---|
236 |
|
---|
237 | if (fun == NULL) {
|
---|
238 | async_answer_0(iid, ENOENT);
|
---|
239 | return;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /* Call driver entry point */
|
---|
243 | int rc;
|
---|
244 |
|
---|
245 | if (driver->driver_ops->fun_online != NULL)
|
---|
246 | rc = driver->driver_ops->fun_online(fun);
|
---|
247 | else
|
---|
248 | rc = ENOTSUP;
|
---|
249 |
|
---|
250 | fun_del_ref(fun);
|
---|
251 |
|
---|
252 | async_answer_0(iid, (sysarg_t) rc);
|
---|
253 | }
|
---|
254 |
|
---|
255 | static void driver_fun_offline(ipc_callid_t iid, ipc_call_t *icall)
|
---|
256 | {
|
---|
257 | devman_handle_t funh = IPC_GET_ARG1(*icall);
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * Look the function up. Bump reference count so that
|
---|
261 | * the function continues to exist until we return
|
---|
262 | * from the driver.
|
---|
263 | */
|
---|
264 | fibril_mutex_lock(&functions_mutex);
|
---|
265 |
|
---|
266 | ddf_fun_t *fun = driver_get_function(funh);
|
---|
267 | if (fun != NULL)
|
---|
268 | fun_add_ref(fun);
|
---|
269 |
|
---|
270 | fibril_mutex_unlock(&functions_mutex);
|
---|
271 |
|
---|
272 | if (fun == NULL) {
|
---|
273 | async_answer_0(iid, ENOENT);
|
---|
274 | return;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /* Call driver entry point */
|
---|
278 | int rc;
|
---|
279 |
|
---|
280 | if (driver->driver_ops->fun_offline != NULL)
|
---|
281 | rc = driver->driver_ops->fun_offline(fun);
|
---|
282 | else
|
---|
283 | rc = ENOTSUP;
|
---|
284 |
|
---|
285 | async_answer_0(iid, (sysarg_t) rc);
|
---|
286 | }
|
---|
287 |
|
---|
288 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
289 | {
|
---|
290 | /* Accept connection */
|
---|
291 | async_answer_0(iid, EOK);
|
---|
292 |
|
---|
293 | while (true) {
|
---|
294 | ipc_call_t call;
|
---|
295 | ipc_callid_t callid = async_get_call(&call);
|
---|
296 |
|
---|
297 | if (!IPC_GET_IMETHOD(call))
|
---|
298 | break;
|
---|
299 |
|
---|
300 | switch (IPC_GET_IMETHOD(call)) {
|
---|
301 | case DRIVER_DEV_ADD:
|
---|
302 | driver_dev_add(callid, &call);
|
---|
303 | break;
|
---|
304 | case DRIVER_DEV_REMOVE:
|
---|
305 | driver_dev_remove(callid, &call);
|
---|
306 | break;
|
---|
307 | case DRIVER_DEV_GONE:
|
---|
308 | driver_dev_gone(callid, &call);
|
---|
309 | break;
|
---|
310 | case DRIVER_FUN_ONLINE:
|
---|
311 | driver_fun_online(callid, &call);
|
---|
312 | break;
|
---|
313 | case DRIVER_FUN_OFFLINE:
|
---|
314 | driver_fun_offline(callid, &call);
|
---|
315 | break;
|
---|
316 | default:
|
---|
317 | async_answer_0(callid, ENOTSUP);
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | /** Generic client connection handler both for applications and drivers.
|
---|
323 | *
|
---|
324 | * @param drv True for driver client, false for other clients
|
---|
325 | * (applications, services, etc.).
|
---|
326 | *
|
---|
327 | */
|
---|
328 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
329 | {
|
---|
330 | /*
|
---|
331 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
---|
332 | * the device to which the client connected.
|
---|
333 | */
|
---|
334 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
335 |
|
---|
336 | fibril_mutex_lock(&functions_mutex);
|
---|
337 | ddf_fun_t *fun = driver_get_function(handle);
|
---|
338 | fibril_mutex_unlock(&functions_mutex);
|
---|
339 | /* XXX Need a lock on fun */
|
---|
340 |
|
---|
341 | if (fun == NULL) {
|
---|
342 | printf("%s: driver_connection_gen error - no function with handle"
|
---|
343 | " %" PRIun " was found.\n", driver->name, handle);
|
---|
344 | async_answer_0(iid, ENOENT);
|
---|
345 | return;
|
---|
346 | }
|
---|
347 |
|
---|
348 | if (fun->conn_handler != NULL) {
|
---|
349 | /* Driver has a custom connection handler. */
|
---|
350 | (*fun->conn_handler)(iid, icall, (void *)fun);
|
---|
351 | return;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /*
|
---|
355 | * TODO - if the client is not a driver, check whether it is allowed to
|
---|
356 | * use the device.
|
---|
357 | */
|
---|
358 |
|
---|
359 | int ret = EOK;
|
---|
360 | /* Open device function */
|
---|
361 | if (fun->ops != NULL && fun->ops->open != NULL)
|
---|
362 | ret = (*fun->ops->open)(fun);
|
---|
363 |
|
---|
364 | async_answer_0(iid, ret);
|
---|
365 | if (ret != EOK)
|
---|
366 | return;
|
---|
367 |
|
---|
368 | while (true) {
|
---|
369 | ipc_callid_t callid;
|
---|
370 | ipc_call_t call;
|
---|
371 | callid = async_get_call(&call);
|
---|
372 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
373 |
|
---|
374 | if (!method) {
|
---|
375 | /* Close device function */
|
---|
376 | if (fun->ops != NULL && fun->ops->close != NULL)
|
---|
377 | (*fun->ops->close)(fun);
|
---|
378 | async_answer_0(callid, EOK);
|
---|
379 | return;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* Convert ipc interface id to interface index */
|
---|
383 |
|
---|
384 | int iface_idx = DEV_IFACE_IDX(method);
|
---|
385 |
|
---|
386 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
387 | remote_handler_t *default_handler =
|
---|
388 | function_get_default_handler(fun);
|
---|
389 | if (default_handler != NULL) {
|
---|
390 | (*default_handler)(fun, callid, &call);
|
---|
391 | continue;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * Function has no such interface and
|
---|
396 | * default handler is not provided.
|
---|
397 | */
|
---|
398 | printf("%s: driver_connection_gen error - "
|
---|
399 | "invalid interface id %d.",
|
---|
400 | driver->name, iface_idx);
|
---|
401 | async_answer_0(callid, ENOTSUP);
|
---|
402 | continue;
|
---|
403 | }
|
---|
404 |
|
---|
405 | /* Calling one of the function's interfaces */
|
---|
406 |
|
---|
407 | /* Get the interface ops structure. */
|
---|
408 | void *ops = function_get_ops(fun, iface_idx);
|
---|
409 | if (ops == NULL) {
|
---|
410 | printf("%s: driver_connection_gen error - ", driver->name);
|
---|
411 | printf("Function with handle %" PRIun " has no interface "
|
---|
412 | "with id %d.\n", handle, iface_idx);
|
---|
413 | async_answer_0(callid, ENOTSUP);
|
---|
414 | continue;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /*
|
---|
418 | * Get the corresponding interface for remote request
|
---|
419 | * handling ("remote interface").
|
---|
420 | */
|
---|
421 | remote_iface_t *rem_iface = get_remote_iface(iface_idx);
|
---|
422 | assert(rem_iface != NULL);
|
---|
423 |
|
---|
424 | /* get the method of the remote interface */
|
---|
425 | sysarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
426 | remote_iface_func_ptr_t iface_method_ptr =
|
---|
427 | get_remote_method(rem_iface, iface_method_idx);
|
---|
428 | if (iface_method_ptr == NULL) {
|
---|
429 | /* The interface has not such method */
|
---|
430 | printf("%s: driver_connection_gen error - "
|
---|
431 | "invalid interface method.", driver->name);
|
---|
432 | async_answer_0(callid, ENOTSUP);
|
---|
433 | continue;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Call the remote interface's method, which will
|
---|
438 | * receive parameters from the remote client and it will
|
---|
439 | * pass it to the corresponding local interface method
|
---|
440 | * associated with the function by its driver.
|
---|
441 | */
|
---|
442 | (*iface_method_ptr)(fun, ops, callid, &call);
|
---|
443 | }
|
---|
444 | }
|
---|
445 |
|
---|
446 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
447 | {
|
---|
448 | driver_connection_gen(iid, icall, true);
|
---|
449 | }
|
---|
450 |
|
---|
451 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
452 | {
|
---|
453 | driver_connection_gen(iid, icall, false);
|
---|
454 | }
|
---|
455 |
|
---|
456 | /** Function for handling connections to device driver. */
|
---|
457 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
458 | {
|
---|
459 | sysarg_t conn_type;
|
---|
460 |
|
---|
461 | if (iid == 0) {
|
---|
462 | /* Callback connection from devman */
|
---|
463 | /* XXX Use separate handler for this type of connection */
|
---|
464 | conn_type = DRIVER_DEVMAN;
|
---|
465 | } else {
|
---|
466 | conn_type = IPC_GET_ARG1(*icall);
|
---|
467 | }
|
---|
468 |
|
---|
469 | /* Select interface */
|
---|
470 | switch (conn_type) {
|
---|
471 | case DRIVER_DEVMAN:
|
---|
472 | /* Handle request from device manager */
|
---|
473 | driver_connection_devman(iid, icall);
|
---|
474 | break;
|
---|
475 | case DRIVER_DRIVER:
|
---|
476 | /* Handle request from drivers of child devices */
|
---|
477 | driver_connection_driver(iid, icall);
|
---|
478 | break;
|
---|
479 | case DRIVER_CLIENT:
|
---|
480 | /* Handle request from client applications */
|
---|
481 | driver_connection_client(iid, icall);
|
---|
482 | break;
|
---|
483 | default:
|
---|
484 | /* No such interface */
|
---|
485 | async_answer_0(iid, ENOENT);
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | /** Create new device structure.
|
---|
490 | *
|
---|
491 | * @return The device structure.
|
---|
492 | */
|
---|
493 | static ddf_dev_t *create_device(void)
|
---|
494 | {
|
---|
495 | ddf_dev_t *dev;
|
---|
496 |
|
---|
497 | dev = calloc(1, sizeof(ddf_dev_t));
|
---|
498 | if (dev == NULL)
|
---|
499 | return NULL;
|
---|
500 |
|
---|
501 | return dev;
|
---|
502 | }
|
---|
503 |
|
---|
504 | /** Create new function structure.
|
---|
505 | *
|
---|
506 | * @return The device structure.
|
---|
507 | */
|
---|
508 | static ddf_fun_t *create_function(void)
|
---|
509 | {
|
---|
510 | ddf_fun_t *fun;
|
---|
511 |
|
---|
512 | fun = calloc(1, sizeof(ddf_fun_t));
|
---|
513 | if (fun == NULL)
|
---|
514 | return NULL;
|
---|
515 |
|
---|
516 | init_match_ids(&fun->match_ids);
|
---|
517 | link_initialize(&fun->link);
|
---|
518 |
|
---|
519 | return fun;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /** Delete device structure.
|
---|
523 | *
|
---|
524 | * @param dev The device structure.
|
---|
525 | */
|
---|
526 | static void delete_device(ddf_dev_t *dev)
|
---|
527 | {
|
---|
528 | if (dev->parent_sess)
|
---|
529 | async_hangup(dev->parent_sess);
|
---|
530 | if (dev->driver_data != NULL)
|
---|
531 | free(dev->driver_data);
|
---|
532 | free(dev);
|
---|
533 | }
|
---|
534 |
|
---|
535 | /** Delete function structure.
|
---|
536 | *
|
---|
537 | * @param dev The device structure.
|
---|
538 | */
|
---|
539 | static void delete_function(ddf_fun_t *fun)
|
---|
540 | {
|
---|
541 | clean_match_ids(&fun->match_ids);
|
---|
542 | if (fun->driver_data != NULL)
|
---|
543 | free(fun->driver_data);
|
---|
544 | if (fun->name != NULL)
|
---|
545 | free(fun->name);
|
---|
546 | free(fun);
|
---|
547 | }
|
---|
548 |
|
---|
549 | /** Increase device reference count. */
|
---|
550 | static void dev_add_ref(ddf_dev_t *dev)
|
---|
551 | {
|
---|
552 | atomic_inc(&dev->refcnt);
|
---|
553 | }
|
---|
554 |
|
---|
555 | /** Decrease device reference count.
|
---|
556 | *
|
---|
557 | * Free the device structure if the reference count drops to zero.
|
---|
558 | */
|
---|
559 | static void dev_del_ref(ddf_dev_t *dev)
|
---|
560 | {
|
---|
561 | if (atomic_predec(&dev->refcnt) == 0)
|
---|
562 | delete_device(dev);
|
---|
563 | }
|
---|
564 |
|
---|
565 | /** Increase function reference count.
|
---|
566 | *
|
---|
567 | * This also increases reference count on the device. The device structure
|
---|
568 | * will thus not be deallocated while there are some associated function
|
---|
569 | * structures.
|
---|
570 | */
|
---|
571 | static void fun_add_ref(ddf_fun_t *fun)
|
---|
572 | {
|
---|
573 | dev_add_ref(fun->dev);
|
---|
574 | atomic_inc(&fun->refcnt);
|
---|
575 | }
|
---|
576 |
|
---|
577 | /** Decrease function reference count.
|
---|
578 | *
|
---|
579 | * Free the function structure if the reference count drops to zero.
|
---|
580 | */
|
---|
581 | static void fun_del_ref(ddf_fun_t *fun)
|
---|
582 | {
|
---|
583 | ddf_dev_t *dev = fun->dev;
|
---|
584 |
|
---|
585 | if (atomic_predec(&fun->refcnt) == 0)
|
---|
586 | delete_function(fun);
|
---|
587 |
|
---|
588 | dev_del_ref(dev);
|
---|
589 | }
|
---|
590 |
|
---|
591 | /** Allocate driver-specific device data. */
|
---|
592 | void *ddf_dev_data_alloc(ddf_dev_t *dev, size_t size)
|
---|
593 | {
|
---|
594 | assert(dev->driver_data == NULL);
|
---|
595 |
|
---|
596 | void *data = calloc(1, size);
|
---|
597 | if (data == NULL)
|
---|
598 | return NULL;
|
---|
599 |
|
---|
600 | dev->driver_data = data;
|
---|
601 | return data;
|
---|
602 | }
|
---|
603 |
|
---|
604 | /** Implant foreign driver-specific device data.
|
---|
605 | *
|
---|
606 | * XXX This is used to transition USB to new interface. Do not use
|
---|
607 | * in new code. Use of this function must be removed.
|
---|
608 | */
|
---|
609 | void ddf_fun_data_implant(ddf_fun_t *fun, void *data)
|
---|
610 | {
|
---|
611 | assert(fun->driver_data == NULL);
|
---|
612 | fun->driver_data = data;
|
---|
613 | }
|
---|
614 |
|
---|
615 | /** Return driver-specific device data. */
|
---|
616 | void *ddf_dev_data_get(ddf_dev_t *dev)
|
---|
617 | {
|
---|
618 | return dev->driver_data;
|
---|
619 | }
|
---|
620 |
|
---|
621 | /** Get device handle. */
|
---|
622 | devman_handle_t ddf_dev_get_handle(ddf_dev_t *dev)
|
---|
623 | {
|
---|
624 | return dev->handle;
|
---|
625 | }
|
---|
626 |
|
---|
627 | /** Return device name.
|
---|
628 | *
|
---|
629 | * @param dev Device
|
---|
630 | * @return Device name. Valid as long as @a dev is valid.
|
---|
631 | */
|
---|
632 | const char *ddf_dev_get_name(ddf_dev_t *dev)
|
---|
633 | {
|
---|
634 | return dev->name;
|
---|
635 | }
|
---|
636 |
|
---|
637 | /** Create session with the parent function.
|
---|
638 | *
|
---|
639 | * The session will be automatically closed when @a dev is destroyed.
|
---|
640 | *
|
---|
641 | * @param dev Device
|
---|
642 | * @param mgmt Exchange management style
|
---|
643 | * @return New session or NULL if session could not be created
|
---|
644 | */
|
---|
645 | async_sess_t *ddf_dev_parent_sess_create(ddf_dev_t *dev, exch_mgmt_t mgmt)
|
---|
646 | {
|
---|
647 | assert(dev->parent_sess == NULL);
|
---|
648 | dev->parent_sess = devman_parent_device_connect(mgmt, dev->handle,
|
---|
649 | IPC_FLAG_BLOCKING);
|
---|
650 |
|
---|
651 | return dev->parent_sess;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /** Return existing session with the parent function.
|
---|
655 | *
|
---|
656 | * @param dev Device
|
---|
657 | * @return Existing session or NULL if there is no session
|
---|
658 | */
|
---|
659 | async_sess_t *ddf_dev_parent_sess_get(ddf_dev_t *dev)
|
---|
660 | {
|
---|
661 | return dev->parent_sess;
|
---|
662 | }
|
---|
663 |
|
---|
664 | /** Set function name (if it was not specified when node was created.)
|
---|
665 | *
|
---|
666 | * @param dev Device whose name has not been set yet
|
---|
667 | * @param name Name, will be copied
|
---|
668 | * @return EOK on success, ENOMEM if out of memory
|
---|
669 | */
|
---|
670 | int ddf_fun_set_name(ddf_fun_t *dev, const char *name)
|
---|
671 | {
|
---|
672 | assert(dev->name == NULL);
|
---|
673 |
|
---|
674 | dev->name = str_dup(name);
|
---|
675 | if (dev->name == NULL)
|
---|
676 | return ENOENT;
|
---|
677 |
|
---|
678 | return EOK;
|
---|
679 | }
|
---|
680 |
|
---|
681 | /** Get device to which function belongs. */
|
---|
682 | ddf_dev_t *ddf_fun_get_dev(ddf_fun_t *fun)
|
---|
683 | {
|
---|
684 | return fun->dev;
|
---|
685 | }
|
---|
686 |
|
---|
687 | /** Get function handle.
|
---|
688 | *
|
---|
689 | * XXX USB uses this, but its use should be eliminated.
|
---|
690 | */
|
---|
691 | devman_handle_t ddf_fun_get_handle(ddf_fun_t *fun)
|
---|
692 | {
|
---|
693 | return fun->handle;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /** Create a DDF function node.
|
---|
697 | *
|
---|
698 | * Create a DDF function (in memory). Both child devices and external clients
|
---|
699 | * communicate with a device via its functions.
|
---|
700 | *
|
---|
701 | * The created function node is fully formed, but only exists in the memory
|
---|
702 | * of the client task. In order to be visible to the system, the function
|
---|
703 | * must be bound using ddf_fun_bind().
|
---|
704 | *
|
---|
705 | * This function should only fail if there is not enough free memory.
|
---|
706 | * Specifically, this function succeeds even if @a dev already has
|
---|
707 | * a (bound) function with the same name. @a name can be NULL in which
|
---|
708 | * case the caller will set the name later using ddf_fun_set_name().
|
---|
709 | * He must do this before binding the function.
|
---|
710 | *
|
---|
711 | * Type: A function of type fun_inner indicates that DDF should attempt
|
---|
712 | * to attach child devices to the function. fun_exposed means that
|
---|
713 | * the function should be exported to external clients (applications).
|
---|
714 | *
|
---|
715 | * @param dev Device to which we are adding function
|
---|
716 | * @param ftype Type of function (fun_inner or fun_exposed)
|
---|
717 | * @param name Name of function or NULL
|
---|
718 | *
|
---|
719 | * @return New function or @c NULL if memory is not available
|
---|
720 | */
|
---|
721 | ddf_fun_t *ddf_fun_create(ddf_dev_t *dev, fun_type_t ftype, const char *name)
|
---|
722 | {
|
---|
723 | ddf_fun_t *fun = create_function();
|
---|
724 | if (fun == NULL)
|
---|
725 | return NULL;
|
---|
726 |
|
---|
727 | /* Add one reference that will be dropped by ddf_fun_destroy() */
|
---|
728 | fun->dev = dev;
|
---|
729 | fun_add_ref(fun);
|
---|
730 |
|
---|
731 | fun->bound = false;
|
---|
732 | fun->ftype = ftype;
|
---|
733 |
|
---|
734 | if (name != NULL) {
|
---|
735 | fun->name = str_dup(name);
|
---|
736 | if (fun->name == NULL) {
|
---|
737 | delete_function(fun);
|
---|
738 | return NULL;
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | return fun;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /** Allocate driver-specific function data. */
|
---|
746 | void *ddf_fun_data_alloc(ddf_fun_t *fun, size_t size)
|
---|
747 | {
|
---|
748 | assert(fun->bound == false);
|
---|
749 | assert(fun->driver_data == NULL);
|
---|
750 |
|
---|
751 | void *data = calloc(1, size);
|
---|
752 | if (data == NULL)
|
---|
753 | return NULL;
|
---|
754 |
|
---|
755 | fun->driver_data = data;
|
---|
756 | return data;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /** Return driver-specific function data. */
|
---|
760 | void *ddf_fun_data_get(ddf_fun_t *fun)
|
---|
761 | {
|
---|
762 | return fun->driver_data;
|
---|
763 | }
|
---|
764 |
|
---|
765 | /** Return function name.
|
---|
766 | *
|
---|
767 | * @param fun Function
|
---|
768 | * @return Function name. Valid as long as @a fun is valid.
|
---|
769 | */
|
---|
770 | const char *ddf_fun_get_name(ddf_fun_t *fun)
|
---|
771 | {
|
---|
772 | return fun->name;
|
---|
773 | }
|
---|
774 |
|
---|
775 | /** Destroy DDF function node.
|
---|
776 | *
|
---|
777 | * Destroy a function previously created with ddf_fun_create(). The function
|
---|
778 | * must not be bound.
|
---|
779 | *
|
---|
780 | * @param fun Function to destroy
|
---|
781 | *
|
---|
782 | */
|
---|
783 | void ddf_fun_destroy(ddf_fun_t *fun)
|
---|
784 | {
|
---|
785 | assert(fun->bound == false);
|
---|
786 |
|
---|
787 | /*
|
---|
788 | * Drop the reference added by ddf_fun_create(). This will deallocate
|
---|
789 | * the function as soon as all other references are dropped (i.e.
|
---|
790 | * as soon control leaves all driver entry points called in context
|
---|
791 | * of this function.
|
---|
792 | */
|
---|
793 | fun_del_ref(fun);
|
---|
794 | }
|
---|
795 |
|
---|
796 | static void *function_get_ops(ddf_fun_t *fun, dev_inferface_idx_t idx)
|
---|
797 | {
|
---|
798 | assert(is_valid_iface_idx(idx));
|
---|
799 | if (fun->ops == NULL)
|
---|
800 | return NULL;
|
---|
801 |
|
---|
802 | return fun->ops->interfaces[idx];
|
---|
803 | }
|
---|
804 |
|
---|
805 | /** Bind a function node.
|
---|
806 | *
|
---|
807 | * Bind the specified function to the system. This effectively makes
|
---|
808 | * the function visible to the system (uploads it to the server).
|
---|
809 | *
|
---|
810 | * This function can fail for several reasons. Specifically,
|
---|
811 | * it will fail if the device already has a bound function of
|
---|
812 | * the same name.
|
---|
813 | *
|
---|
814 | * @param fun Function to bind
|
---|
815 | *
|
---|
816 | * @return EOK on success or negative error code
|
---|
817 | *
|
---|
818 | */
|
---|
819 | int ddf_fun_bind(ddf_fun_t *fun)
|
---|
820 | {
|
---|
821 | assert(fun->bound == false);
|
---|
822 | assert(fun->name != NULL);
|
---|
823 |
|
---|
824 | add_to_functions_list(fun);
|
---|
825 | int res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
|
---|
826 | fun->dev->handle, &fun->handle);
|
---|
827 | if (res != EOK) {
|
---|
828 | remove_from_functions_list(fun);
|
---|
829 | return res;
|
---|
830 | }
|
---|
831 |
|
---|
832 | fun->bound = true;
|
---|
833 | return res;
|
---|
834 | }
|
---|
835 |
|
---|
836 | /** Unbind a function node.
|
---|
837 | *
|
---|
838 | * Unbind the specified function from the system. This effectively makes
|
---|
839 | * the function invisible to the system.
|
---|
840 | *
|
---|
841 | * @param fun Function to unbind
|
---|
842 | *
|
---|
843 | * @return EOK on success or negative error code
|
---|
844 | *
|
---|
845 | */
|
---|
846 | int ddf_fun_unbind(ddf_fun_t *fun)
|
---|
847 | {
|
---|
848 | assert(fun->bound == true);
|
---|
849 |
|
---|
850 | int res = devman_remove_function(fun->handle);
|
---|
851 | if (res != EOK)
|
---|
852 | return res;
|
---|
853 |
|
---|
854 | remove_from_functions_list(fun);
|
---|
855 |
|
---|
856 | fun->bound = false;
|
---|
857 | return EOK;
|
---|
858 | }
|
---|
859 |
|
---|
860 | /** Online function.
|
---|
861 | *
|
---|
862 | * @param fun Function to online
|
---|
863 | *
|
---|
864 | * @return EOK on success or negative error code
|
---|
865 | *
|
---|
866 | */
|
---|
867 | int ddf_fun_online(ddf_fun_t *fun)
|
---|
868 | {
|
---|
869 | assert(fun->bound == true);
|
---|
870 |
|
---|
871 | int res = devman_drv_fun_online(fun->handle);
|
---|
872 | if (res != EOK)
|
---|
873 | return res;
|
---|
874 |
|
---|
875 | return EOK;
|
---|
876 | }
|
---|
877 |
|
---|
878 | /** Offline function.
|
---|
879 | *
|
---|
880 | * @param fun Function to offline
|
---|
881 | *
|
---|
882 | * @return EOK on success or negative error code
|
---|
883 | *
|
---|
884 | */
|
---|
885 | int ddf_fun_offline(ddf_fun_t *fun)
|
---|
886 | {
|
---|
887 | assert(fun->bound == true);
|
---|
888 |
|
---|
889 | int res = devman_drv_fun_offline(fun->handle);
|
---|
890 | if (res != EOK)
|
---|
891 | return res;
|
---|
892 |
|
---|
893 | return EOK;
|
---|
894 | }
|
---|
895 |
|
---|
896 | /** Add single match ID to inner function.
|
---|
897 | *
|
---|
898 | * Construct and add a single match ID to the specified function.
|
---|
899 | * Cannot be called when the function node is bound.
|
---|
900 | *
|
---|
901 | * @param fun Function
|
---|
902 | * @param match_id_str Match string
|
---|
903 | * @param match_score Match score
|
---|
904 | *
|
---|
905 | * @return EOK on success.
|
---|
906 | * @return ENOMEM if out of memory.
|
---|
907 | *
|
---|
908 | */
|
---|
909 | int ddf_fun_add_match_id(ddf_fun_t *fun, const char *match_id_str,
|
---|
910 | int match_score)
|
---|
911 | {
|
---|
912 | assert(fun->bound == false);
|
---|
913 | assert(fun->ftype == fun_inner);
|
---|
914 |
|
---|
915 | match_id_t *match_id = create_match_id();
|
---|
916 | if (match_id == NULL)
|
---|
917 | return ENOMEM;
|
---|
918 |
|
---|
919 | match_id->id = str_dup(match_id_str);
|
---|
920 | match_id->score = match_score;
|
---|
921 |
|
---|
922 | add_match_id(&fun->match_ids, match_id);
|
---|
923 | return EOK;
|
---|
924 | }
|
---|
925 |
|
---|
926 | /** Set function ops. */
|
---|
927 | void ddf_fun_set_ops(ddf_fun_t *fun, ddf_dev_ops_t *dev_ops)
|
---|
928 | {
|
---|
929 | assert(fun->conn_handler == NULL);
|
---|
930 | fun->ops = dev_ops;
|
---|
931 | }
|
---|
932 |
|
---|
933 | /** Set user-defined connection handler.
|
---|
934 | *
|
---|
935 | * This allows handling connections the non-devman way.
|
---|
936 | */
|
---|
937 | void ddf_fun_set_conn_handler(ddf_fun_t *fun, async_client_conn_t conn)
|
---|
938 | {
|
---|
939 | assert(fun->ops == NULL);
|
---|
940 | fun->conn_handler = conn;
|
---|
941 | }
|
---|
942 |
|
---|
943 | /** Get default handler for client requests */
|
---|
944 | static remote_handler_t *function_get_default_handler(ddf_fun_t *fun)
|
---|
945 | {
|
---|
946 | if (fun->ops == NULL)
|
---|
947 | return NULL;
|
---|
948 | return fun->ops->default_handler;
|
---|
949 | }
|
---|
950 |
|
---|
951 | /** Add exposed function to category.
|
---|
952 | *
|
---|
953 | * Must only be called when the function is bound.
|
---|
954 | *
|
---|
955 | */
|
---|
956 | int ddf_fun_add_to_category(ddf_fun_t *fun, const char *cat_name)
|
---|
957 | {
|
---|
958 | assert(fun->bound == true);
|
---|
959 | assert(fun->ftype == fun_exposed);
|
---|
960 |
|
---|
961 | return devman_add_device_to_category(fun->handle, cat_name);
|
---|
962 | }
|
---|
963 |
|
---|
964 | int ddf_driver_main(driver_t *drv)
|
---|
965 | {
|
---|
966 | /*
|
---|
967 | * Remember the driver structure - driver_ops will be called by generic
|
---|
968 | * handler for incoming connections.
|
---|
969 | */
|
---|
970 | driver = drv;
|
---|
971 |
|
---|
972 | /* Initialize interrupt module */
|
---|
973 | interrupt_init();
|
---|
974 |
|
---|
975 | /*
|
---|
976 | * Register driver with device manager using generic handler for
|
---|
977 | * incoming connections.
|
---|
978 | */
|
---|
979 | async_set_client_connection(driver_connection);
|
---|
980 | int rc = devman_driver_register(driver->name);
|
---|
981 | if (rc != EOK) {
|
---|
982 | printf("Error: Failed to register driver with device manager "
|
---|
983 | "(%s).\n", (rc == EEXISTS) ? "driver already started" :
|
---|
984 | str_error(rc));
|
---|
985 |
|
---|
986 | return rc;
|
---|
987 | }
|
---|
988 |
|
---|
989 | /* Return success from the task since server has started. */
|
---|
990 | rc = task_retval(0);
|
---|
991 | if (rc != EOK)
|
---|
992 | return rc;
|
---|
993 |
|
---|
994 | async_manager();
|
---|
995 |
|
---|
996 | /* Never reached. */
|
---|
997 | return EOK;
|
---|
998 | }
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * @}
|
---|
1002 | */
|
---|