[c16cf62] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 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
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * @defgroup libdrv generic device driver support.
|
---|
| 31 | * @brief HelenOS generic device driver support.
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | /** @file
|
---|
| 36 | */
|
---|
[52b7b1bb] | 37 |
|
---|
[c16cf62] | 38 | #include <assert.h>
|
---|
| 39 | #include <ipc/services.h>
|
---|
| 40 | #include <ipc/ns.h>
|
---|
| 41 | #include <async.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <errno.h>
|
---|
| 44 | #include <bool.h>
|
---|
| 45 | #include <fibril_synch.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
[c47e1a8] | 47 | #include <str.h>
|
---|
[c16cf62] | 48 | #include <ctype.h>
|
---|
[66babbd] | 49 | #include <errno.h>
|
---|
[7e752b2] | 50 | #include <inttypes.h>
|
---|
[c16cf62] | 51 |
|
---|
[084ff99] | 52 | #include <ipc/driver.h>
|
---|
[c16cf62] | 53 |
|
---|
| 54 | #include "driver.h"
|
---|
| 55 |
|
---|
[7a252ec8] | 56 | /* driver structure */
|
---|
[7f8b581] | 57 |
|
---|
[c16cf62] | 58 | static driver_t *driver;
|
---|
[7f8b581] | 59 |
|
---|
[7a252ec8] | 60 | /* devices */
|
---|
[7f8b581] | 61 |
|
---|
[084ff99] | 62 | LIST_INITIALIZE(devices);
|
---|
[9a66bc2e] | 63 | FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
---|
[084ff99] | 64 |
|
---|
[7a252ec8] | 65 | /* interrupts */
|
---|
[7f8b581] | 66 |
|
---|
| 67 | static interrupt_context_list_t interrupt_contexts;
|
---|
| 68 |
|
---|
| 69 | static irq_cmd_t default_cmds[] = {
|
---|
| 70 | {
|
---|
| 71 | .cmd = CMD_ACCEPT
|
---|
| 72 | }
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | static irq_code_t default_pseudocode = {
|
---|
| 76 | sizeof(default_cmds) / sizeof(irq_cmd_t),
|
---|
| 77 | default_cmds
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[7a252ec8] | 82 | {
|
---|
[7f8b581] | 83 | int id = (int)IPC_GET_METHOD(*icall);
|
---|
[7a252ec8] | 84 | interrupt_context_t *ctx;
|
---|
| 85 |
|
---|
| 86 | ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
|
---|
| 87 | if (NULL != ctx && NULL != ctx->handler)
|
---|
| 88 | (*ctx->handler)(ctx->dev, iid, icall);
|
---|
[7f8b581] | 89 | }
|
---|
| 90 |
|
---|
[7a252ec8] | 91 | int
|
---|
| 92 | register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler,
|
---|
| 93 | irq_code_t *pseudocode)
|
---|
[7f8b581] | 94 | {
|
---|
| 95 | interrupt_context_t *ctx = create_interrupt_context();
|
---|
| 96 |
|
---|
| 97 | ctx->dev = dev;
|
---|
| 98 | ctx->irq = irq;
|
---|
| 99 | ctx->handler = handler;
|
---|
| 100 |
|
---|
| 101 | add_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 102 |
|
---|
[7a252ec8] | 103 | if (NULL == pseudocode)
|
---|
[7f8b581] | 104 | pseudocode = &default_pseudocode;
|
---|
| 105 |
|
---|
| 106 | int res = ipc_register_irq(irq, dev->handle, ctx->id, pseudocode);
|
---|
| 107 | if (0 != res) {
|
---|
| 108 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 109 | delete_interrupt_context(ctx);
|
---|
| 110 | }
|
---|
[7a252ec8] | 111 |
|
---|
| 112 | return res;
|
---|
[7f8b581] | 113 | }
|
---|
| 114 |
|
---|
| 115 | int unregister_interrupt_handler(device_t *dev, int irq)
|
---|
| 116 | {
|
---|
[7a252ec8] | 117 | interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts,
|
---|
| 118 | dev, irq);
|
---|
[7f8b581] | 119 | int res = ipc_unregister_irq(irq, dev->handle);
|
---|
[7a252ec8] | 120 |
|
---|
[7f8b581] | 121 | if (NULL != ctx) {
|
---|
| 122 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
[7a252ec8] | 123 | delete_interrupt_context(ctx);
|
---|
[7f8b581] | 124 | }
|
---|
| 125 | return res;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[9a66bc2e] | 128 | static void add_to_devices_list(device_t *dev)
|
---|
| 129 | {
|
---|
| 130 | fibril_mutex_lock(&devices_mutex);
|
---|
| 131 | list_append(&dev->link, &devices);
|
---|
| 132 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | static void remove_from_devices_list(device_t *dev)
|
---|
| 136 | {
|
---|
| 137 | fibril_mutex_lock(&devices_mutex);
|
---|
[5af21c5] | 138 | list_remove(&dev->link);
|
---|
[9a66bc2e] | 139 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[0b5a4131] | 142 | static device_t * driver_get_device(link_t *devices, devman_handle_t handle)
|
---|
[52b7b1bb] | 143 | {
|
---|
[a1769ee] | 144 | device_t *dev = NULL;
|
---|
[9a66bc2e] | 145 |
|
---|
[7a252ec8] | 146 | fibril_mutex_lock(&devices_mutex);
|
---|
[a1769ee] | 147 | link_t *link = devices->next;
|
---|
| 148 | while (link != devices) {
|
---|
| 149 | dev = list_get_instance(link, device_t, link);
|
---|
| 150 | if (handle == dev->handle) {
|
---|
[9a66bc2e] | 151 | fibril_mutex_unlock(&devices_mutex);
|
---|
[a1769ee] | 152 | return dev;
|
---|
| 153 | }
|
---|
[9a66bc2e] | 154 | link = link->next;
|
---|
[a1769ee] | 155 | }
|
---|
[9a66bc2e] | 156 | fibril_mutex_unlock(&devices_mutex);
|
---|
[52b7b1bb] | 157 |
|
---|
[a1769ee] | 158 | return NULL;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[52b7b1bb] | 161 | static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[084ff99] | 162 | {
|
---|
[df747b9c] | 163 | char *dev_name = NULL;
|
---|
| 164 | int res = EOK;
|
---|
[9a66bc2e] | 165 |
|
---|
[0b5a4131] | 166 | devman_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
[5af21c5] | 167 | device_t *dev = create_device();
|
---|
[084ff99] | 168 | dev->handle = dev_handle;
|
---|
[df747b9c] | 169 |
|
---|
[7a252ec8] | 170 | async_data_write_accept((void **) &dev_name, true, 0, 0, 0, 0);
|
---|
[df747b9c] | 171 | dev->name = dev_name;
|
---|
| 172 |
|
---|
[7a252ec8] | 173 | add_to_devices_list(dev);
|
---|
[df747b9c] | 174 | res = driver->driver_ops->add_device(dev);
|
---|
| 175 | if (0 == res) {
|
---|
[7e752b2] | 176 | printf("%s: new device with handle=%" PRIun " was added.\n",
|
---|
[7a252ec8] | 177 | driver->name, dev_handle);
|
---|
[9a66bc2e] | 178 | } else {
|
---|
[7e752b2] | 179 | printf("%s: failed to add a new device with handle = %" PRIun ".\n",
|
---|
[7a252ec8] | 180 | driver->name, dev_handle);
|
---|
[9a66bc2e] | 181 | remove_from_devices_list(dev);
|
---|
[7a252ec8] | 182 | delete_device(dev);
|
---|
[084ff99] | 183 | }
|
---|
[df747b9c] | 184 |
|
---|
| 185 | ipc_answer_0(iid, res);
|
---|
[084ff99] | 186 | }
|
---|
[c16cf62] | 187 |
|
---|
| 188 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 189 | {
|
---|
| 190 | /* Accept connection */
|
---|
| 191 | ipc_answer_0(iid, EOK);
|
---|
[52b7b1bb] | 192 |
|
---|
[c16cf62] | 193 | bool cont = true;
|
---|
| 194 | while (cont) {
|
---|
| 195 | ipc_call_t call;
|
---|
| 196 | ipc_callid_t callid = async_get_call(&call);
|
---|
[52b7b1bb] | 197 |
|
---|
[c16cf62] | 198 | switch (IPC_GET_METHOD(call)) {
|
---|
| 199 | case IPC_M_PHONE_HUNGUP:
|
---|
| 200 | cont = false;
|
---|
| 201 | continue;
|
---|
| 202 | case DRIVER_ADD_DEVICE:
|
---|
[084ff99] | 203 | driver_add_device(callid, &call);
|
---|
[c16cf62] | 204 | break;
|
---|
| 205 | default:
|
---|
[cb819f9] | 206 | ipc_answer_0(callid, ENOENT);
|
---|
[c16cf62] | 207 | }
|
---|
[52b7b1bb] | 208 | }
|
---|
[c16cf62] | 209 | }
|
---|
| 210 |
|
---|
[52b7b1bb] | 211 | /**
|
---|
[a1769ee] | 212 | * Generic client connection handler both for applications and drivers.
|
---|
[52b7b1bb] | 213 | *
|
---|
[7a252ec8] | 214 | * @param drv True for driver client, false for other clients
|
---|
| 215 | * (applications, services etc.).
|
---|
[a1769ee] | 216 | */
|
---|
[9a66bc2e] | 217 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
[52b7b1bb] | 218 | {
|
---|
[7a252ec8] | 219 | /*
|
---|
| 220 | * Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of
|
---|
| 221 | * the device to which the client connected.
|
---|
| 222 | */
|
---|
[0b5a4131] | 223 | devman_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
[a1769ee] | 224 | device_t *dev = driver_get_device(&devices, handle);
|
---|
[52b7b1bb] | 225 |
|
---|
[a1769ee] | 226 | if (dev == NULL) {
|
---|
[7a252ec8] | 227 | printf("%s: driver_connection_gen error - no device with handle"
|
---|
[7e752b2] | 228 | " %" PRIun " was found.\n", driver->name, handle);
|
---|
[a1769ee] | 229 | ipc_answer_0(iid, ENOENT);
|
---|
| 230 | return;
|
---|
| 231 | }
|
---|
[5cd136ab] | 232 |
|
---|
| 233 |
|
---|
[7a252ec8] | 234 | /*
|
---|
| 235 | * TODO - if the client is not a driver, check whether it is allowed to
|
---|
| 236 | * use the device.
|
---|
| 237 | */
|
---|
[a1769ee] | 238 |
|
---|
[25a7e11d] | 239 | int ret = EOK;
|
---|
[7a252ec8] | 240 | /* open the device */
|
---|
| 241 | if (NULL != dev->ops && NULL != dev->ops->open)
|
---|
[5159ae9] | 242 | ret = (*dev->ops->open)(dev);
|
---|
[a1769ee] | 243 |
|
---|
[25a7e11d] | 244 | ipc_answer_0(iid, ret);
|
---|
[7a252ec8] | 245 | if (EOK != ret)
|
---|
[a6e54c5d] | 246 | return;
|
---|
[52b7b1bb] | 247 |
|
---|
[a1769ee] | 248 | while (1) {
|
---|
| 249 | ipc_callid_t callid;
|
---|
| 250 | ipc_call_t call;
|
---|
| 251 | callid = async_get_call(&call);
|
---|
| 252 | ipcarg_t method = IPC_GET_METHOD(call);
|
---|
[3843ecb] | 253 | int iface_idx;
|
---|
| 254 |
|
---|
[a1769ee] | 255 | switch (method) {
|
---|
[25a7e11d] | 256 | case IPC_M_PHONE_HUNGUP:
|
---|
[7a252ec8] | 257 | /* close the device */
|
---|
| 258 | if (NULL != dev->ops && NULL != dev->ops->close)
|
---|
[5159ae9] | 259 | (*dev->ops->close)(dev);
|
---|
[a1769ee] | 260 | ipc_answer_0(callid, EOK);
|
---|
| 261 | return;
|
---|
[3843ecb] | 262 | default:
|
---|
[7a252ec8] | 263 | /* convert ipc interface id to interface index */
|
---|
[3843ecb] | 264 |
|
---|
| 265 | iface_idx = DEV_IFACE_IDX(method);
|
---|
| 266 |
|
---|
| 267 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
[7a252ec8] | 268 | remote_handler_t *default_handler =
|
---|
| 269 | device_get_default_handler(dev);
|
---|
| 270 | if (NULL != default_handler) {
|
---|
[08d9525a] | 271 | (*default_handler)(dev, callid, &call);
|
---|
| 272 | break;
|
---|
| 273 | }
|
---|
[7a252ec8] | 274 | /*
|
---|
| 275 | * This is not device's interface and the
|
---|
| 276 | * default handler is not provided.
|
---|
| 277 | */
|
---|
| 278 | printf("%s: driver_connection_gen error - "
|
---|
| 279 | "invalid interface id %d.",
|
---|
| 280 | driver->name, iface_idx);
|
---|
[52b7b1bb] | 281 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 282 | break;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[7a252ec8] | 285 | /* calling one of the device's interfaces */
|
---|
[52b7b1bb] | 286 |
|
---|
[7a252ec8] | 287 | /* get the device interface structure */
|
---|
[3843ecb] | 288 | void *iface = device_get_iface(dev, iface_idx);
|
---|
[52b7b1bb] | 289 | if (NULL == iface) {
|
---|
[7a252ec8] | 290 | printf("%s: driver_connection_gen error - ",
|
---|
| 291 | driver->name);
|
---|
[7e752b2] | 292 | printf("device with handle %" PRIun " has no interface "
|
---|
[7a252ec8] | 293 | "with id %d.\n", handle, iface_idx);
|
---|
[a1769ee] | 294 | ipc_answer_0(callid, ENOTSUP);
|
---|
[52b7b1bb] | 295 | break;
|
---|
[a1769ee] | 296 | }
|
---|
[52b7b1bb] | 297 |
|
---|
[7a252ec8] | 298 | /*
|
---|
| 299 | * Get the corresponding interface for remote request
|
---|
| 300 | * handling ("remote interface").
|
---|
| 301 | */
|
---|
[3843ecb] | 302 | remote_iface_t* rem_iface = get_remote_iface(iface_idx);
|
---|
[52b7b1bb] | 303 | assert(NULL != rem_iface);
|
---|
| 304 |
|
---|
[7a252ec8] | 305 | /* get the method of the remote interface */
|
---|
[52b7b1bb] | 306 | ipcarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
[7a252ec8] | 307 | remote_iface_func_ptr_t iface_method_ptr =
|
---|
| 308 | get_remote_method(rem_iface, iface_method_idx);
|
---|
[52b7b1bb] | 309 | if (NULL == iface_method_ptr) {
|
---|
| 310 | // the interface has not such method
|
---|
[7a252ec8] | 311 | printf("%s: driver_connection_gen error - "
|
---|
| 312 | "invalid interface method.", driver->name);
|
---|
[52b7b1bb] | 313 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 314 | break;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[7a252ec8] | 317 | /*
|
---|
| 318 | * Call the remote interface's method, which will
|
---|
| 319 | * receive parameters from the remote client and it will
|
---|
| 320 | * pass it to the corresponding local interface method
|
---|
| 321 | * associated with the device by its driver.
|
---|
| 322 | */
|
---|
[52b7b1bb] | 323 | (*iface_method_ptr)(dev, iface, callid, &call);
|
---|
[a1769ee] | 324 | break;
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[c16cf62] | 329 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 330 | {
|
---|
[52b7b1bb] | 331 | driver_connection_gen(iid, icall, true);
|
---|
[c16cf62] | 332 | }
|
---|
| 333 |
|
---|
| 334 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 335 | {
|
---|
[52b7b1bb] | 336 | driver_connection_gen(iid, icall, false);
|
---|
[c16cf62] | 337 | }
|
---|
| 338 |
|
---|
| 339 |
|
---|
[7a252ec8] | 340 | /** Function for handling connections to device driver. */
|
---|
[c16cf62] | 341 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 342 | {
|
---|
| 343 | /* Select interface */
|
---|
| 344 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
| 345 | case DRIVER_DEVMAN:
|
---|
[7a252ec8] | 346 | /* handle PnP events from device manager */
|
---|
[c16cf62] | 347 | driver_connection_devman(iid, icall);
|
---|
| 348 | break;
|
---|
| 349 | case DRIVER_DRIVER:
|
---|
[7a252ec8] | 350 | /* handle request from drivers of child devices */
|
---|
[c16cf62] | 351 | driver_connection_driver(iid, icall);
|
---|
| 352 | break;
|
---|
| 353 | case DRIVER_CLIENT:
|
---|
[7a252ec8] | 354 | /* handle requests from client applications */
|
---|
[c16cf62] | 355 | driver_connection_client(iid, icall);
|
---|
| 356 | break;
|
---|
| 357 |
|
---|
| 358 | default:
|
---|
[52b7b1bb] | 359 | /* No such interface */
|
---|
[c16cf62] | 360 | ipc_answer_0(iid, ENOENT);
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[df747b9c] | 364 | int child_device_register(device_t *child, device_t *parent)
|
---|
[7707954] | 365 | {
|
---|
[bda60d9] | 366 | assert(NULL != child->name);
|
---|
[52b7b1bb] | 367 |
|
---|
[df747b9c] | 368 | int res;
|
---|
| 369 |
|
---|
[9a66bc2e] | 370 | add_to_devices_list(child);
|
---|
[7a252ec8] | 371 | res = devman_child_device_register(child->name, &child->match_ids,
|
---|
| 372 | parent->handle, &child->handle);
|
---|
| 373 | if (EOK == res)
|
---|
[df747b9c] | 374 | return res;
|
---|
[9a66bc2e] | 375 | remove_from_devices_list(child);
|
---|
[df747b9c] | 376 | return res;
|
---|
[7707954] | 377 | }
|
---|
| 378 |
|
---|
[52b7b1bb] | 379 | int driver_main(driver_t *drv)
|
---|
[c16cf62] | 380 | {
|
---|
[7a252ec8] | 381 | /*
|
---|
| 382 | * Remember the driver structure - driver_ops will be called by generic
|
---|
| 383 | * handler for incoming connections.
|
---|
| 384 | */
|
---|
[c16cf62] | 385 | driver = drv;
|
---|
[52b7b1bb] | 386 |
|
---|
[7a252ec8] | 387 | /* Initialize the list of interrupt contexts. */
|
---|
[7f8b581] | 388 | init_interrupt_context_list(&interrupt_contexts);
|
---|
| 389 |
|
---|
[7a252ec8] | 390 | /* Set generic interrupt handler. */
|
---|
[7f8b581] | 391 | async_set_interrupt_received(driver_irq_handler);
|
---|
| 392 |
|
---|
[7a252ec8] | 393 | /*
|
---|
| 394 | * Register driver by device manager with generic handler for incoming
|
---|
| 395 | * connections.
|
---|
| 396 | */
|
---|
[52b7b1bb] | 397 | devman_driver_register(driver->name, driver_connection);
|
---|
[c16cf62] | 398 |
|
---|
| 399 | async_manager();
|
---|
| 400 |
|
---|
[7a252ec8] | 401 | /* Never reached. */
|
---|
[52b7b1bb] | 402 | return 0;
|
---|
[c16cf62] | 403 | }
|
---|
| 404 |
|
---|
| 405 | /**
|
---|
| 406 | * @}
|
---|
[52b7b1bb] | 407 | */
|
---|