[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>
|
---|
[c16cf62] | 50 |
|
---|
[084ff99] | 51 | #include <ipc/driver.h>
|
---|
[c16cf62] | 52 |
|
---|
| 53 | #include "driver.h"
|
---|
| 54 |
|
---|
[7f8b581] | 55 | // driver structure
|
---|
| 56 |
|
---|
[c16cf62] | 57 | static driver_t *driver;
|
---|
[7f8b581] | 58 |
|
---|
| 59 | // devices
|
---|
| 60 |
|
---|
[084ff99] | 61 | LIST_INITIALIZE(devices);
|
---|
[9a66bc2e] | 62 | FIBRIL_MUTEX_INITIALIZE(devices_mutex);
|
---|
[084ff99] | 63 |
|
---|
[7f8b581] | 64 | // interrupts
|
---|
| 65 |
|
---|
| 66 | static interrupt_context_list_t interrupt_contexts;
|
---|
| 67 |
|
---|
| 68 | static irq_cmd_t default_cmds[] = {
|
---|
| 69 | {
|
---|
| 70 | .cmd = CMD_ACCEPT
|
---|
| 71 | }
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | static irq_code_t default_pseudocode = {
|
---|
| 75 | sizeof(default_cmds) / sizeof(irq_cmd_t),
|
---|
| 76 | default_cmds
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[2300b9d] | 81 | {
|
---|
[7f8b581] | 82 | int id = (int)IPC_GET_METHOD(*icall);
|
---|
| 83 | interrupt_context_t *ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
|
---|
[2300b9d] | 84 | if (NULL != ctx && NULL != ctx->handler) {
|
---|
| 85 | (*ctx->handler)(ctx->dev, iid, icall);
|
---|
| 86 | }
|
---|
[7f8b581] | 87 | }
|
---|
| 88 |
|
---|
| 89 | int register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler, irq_code_t *pseudocode)
|
---|
| 90 | {
|
---|
| 91 | interrupt_context_t *ctx = create_interrupt_context();
|
---|
| 92 |
|
---|
| 93 | ctx->dev = dev;
|
---|
| 94 | ctx->irq = irq;
|
---|
| 95 | ctx->handler = handler;
|
---|
| 96 |
|
---|
| 97 | add_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 98 |
|
---|
| 99 | if (NULL == pseudocode) {
|
---|
| 100 | pseudocode = &default_pseudocode;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | int res = ipc_register_irq(irq, dev->handle, ctx->id, pseudocode);
|
---|
| 104 | if (0 != res) {
|
---|
| 105 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 106 | delete_interrupt_context(ctx);
|
---|
| 107 | }
|
---|
| 108 | return res;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | int unregister_interrupt_handler(device_t *dev, int irq)
|
---|
| 112 | {
|
---|
| 113 | interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts, dev, irq);
|
---|
| 114 | int res = ipc_unregister_irq(irq, dev->handle);
|
---|
| 115 | if (NULL != ctx) {
|
---|
| 116 | remove_interrupt_context(&interrupt_contexts, ctx);
|
---|
| 117 | delete_interrupt_context(ctx);
|
---|
| 118 | }
|
---|
| 119 | return res;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[9a66bc2e] | 122 | static void add_to_devices_list(device_t *dev)
|
---|
| 123 | {
|
---|
| 124 | fibril_mutex_lock(&devices_mutex);
|
---|
| 125 | list_append(&dev->link, &devices);
|
---|
| 126 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | static void remove_from_devices_list(device_t *dev)
|
---|
| 130 | {
|
---|
| 131 | fibril_mutex_lock(&devices_mutex);
|
---|
[5af21c5] | 132 | list_remove(&dev->link);
|
---|
[9a66bc2e] | 133 | fibril_mutex_unlock(&devices_mutex);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[52b7b1bb] | 136 | static device_t * driver_get_device(link_t *devices, device_handle_t handle)
|
---|
| 137 | {
|
---|
[a1769ee] | 138 | device_t *dev = NULL;
|
---|
[9a66bc2e] | 139 |
|
---|
| 140 | fibril_mutex_lock(&devices_mutex);
|
---|
[a1769ee] | 141 | link_t *link = devices->next;
|
---|
| 142 | while (link != devices) {
|
---|
| 143 | dev = list_get_instance(link, device_t, link);
|
---|
| 144 | if (handle == dev->handle) {
|
---|
[9a66bc2e] | 145 | fibril_mutex_unlock(&devices_mutex);
|
---|
[a1769ee] | 146 | return dev;
|
---|
| 147 | }
|
---|
[9a66bc2e] | 148 | link = link->next;
|
---|
[a1769ee] | 149 | }
|
---|
[9a66bc2e] | 150 | fibril_mutex_unlock(&devices_mutex);
|
---|
[52b7b1bb] | 151 |
|
---|
[a1769ee] | 152 | return NULL;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[52b7b1bb] | 155 | static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[084ff99] | 156 | {
|
---|
[df747b9c] | 157 | char *dev_name = NULL;
|
---|
| 158 | int res = EOK;
|
---|
[9a66bc2e] | 159 |
|
---|
[52b7b1bb] | 160 | device_handle_t dev_handle = IPC_GET_ARG1(*icall);
|
---|
[5af21c5] | 161 | device_t *dev = create_device();
|
---|
[084ff99] | 162 | dev->handle = dev_handle;
|
---|
[df747b9c] | 163 |
|
---|
[c9f3b45c] | 164 | async_data_write_accept((void **)&dev_name, true, 0, 0, 0, 0);
|
---|
[df747b9c] | 165 | dev->name = dev_name;
|
---|
| 166 |
|
---|
| 167 | add_to_devices_list(dev);
|
---|
| 168 | res = driver->driver_ops->add_device(dev);
|
---|
| 169 | if (0 == res) {
|
---|
[9a66bc2e] | 170 | printf("%s: new device with handle = %x was added.\n", driver->name, dev_handle);
|
---|
| 171 | } else {
|
---|
[df747b9c] | 172 | printf("%s: failed to add a new device with handle = %d.\n", driver->name, dev_handle);
|
---|
[9a66bc2e] | 173 | remove_from_devices_list(dev);
|
---|
[5af21c5] | 174 | delete_device(dev);
|
---|
[084ff99] | 175 | }
|
---|
[df747b9c] | 176 |
|
---|
| 177 | ipc_answer_0(iid, res);
|
---|
[084ff99] | 178 | }
|
---|
[c16cf62] | 179 |
|
---|
| 180 | static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 181 | {
|
---|
| 182 | /* Accept connection */
|
---|
| 183 | ipc_answer_0(iid, EOK);
|
---|
[52b7b1bb] | 184 |
|
---|
[c16cf62] | 185 | bool cont = true;
|
---|
| 186 | while (cont) {
|
---|
| 187 | ipc_call_t call;
|
---|
| 188 | ipc_callid_t callid = async_get_call(&call);
|
---|
[52b7b1bb] | 189 |
|
---|
[c16cf62] | 190 | switch (IPC_GET_METHOD(call)) {
|
---|
| 191 | case IPC_M_PHONE_HUNGUP:
|
---|
| 192 | cont = false;
|
---|
| 193 | continue;
|
---|
| 194 | case DRIVER_ADD_DEVICE:
|
---|
[084ff99] | 195 | driver_add_device(callid, &call);
|
---|
[c16cf62] | 196 | break;
|
---|
| 197 | default:
|
---|
| 198 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
| 199 | ipc_answer_0(callid, ENOENT);
|
---|
| 200 | }
|
---|
[52b7b1bb] | 201 | }
|
---|
[c16cf62] | 202 | }
|
---|
| 203 |
|
---|
[52b7b1bb] | 204 | /**
|
---|
[a1769ee] | 205 | * Generic client connection handler both for applications and drivers.
|
---|
[52b7b1bb] | 206 | *
|
---|
[9a66bc2e] | 207 | * @param drv true for driver client, false for other clients (applications, services etc.).
|
---|
[a1769ee] | 208 | */
|
---|
[9a66bc2e] | 209 | static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
|
---|
[52b7b1bb] | 210 | {
|
---|
| 211 | // Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of the device to which the client connected.
|
---|
[9a66bc2e] | 212 | device_handle_t handle = IPC_GET_ARG2(*icall);
|
---|
[a1769ee] | 213 | device_t *dev = driver_get_device(&devices, handle);
|
---|
[52b7b1bb] | 214 |
|
---|
[a1769ee] | 215 | if (dev == NULL) {
|
---|
[9a66bc2e] | 216 | printf("%s: driver_connection_gen error - no device with handle %x was found.\n", driver->name, handle);
|
---|
[a1769ee] | 217 | ipc_answer_0(iid, ENOENT);
|
---|
| 218 | return;
|
---|
| 219 | }
|
---|
[5cd136ab] | 220 |
|
---|
| 221 |
|
---|
| 222 | // TODO - if the client is not a driver, check whether it is allowed to use the device
|
---|
[a1769ee] | 223 |
|
---|
[25a7e11d] | 224 | int ret = EOK;
|
---|
| 225 | // open the device
|
---|
[5159ae9] | 226 | if (NULL != dev->ops && NULL != dev->ops->open) {
|
---|
| 227 | ret = (*dev->ops->open)(dev);
|
---|
[25a7e11d] | 228 | }
|
---|
[a1769ee] | 229 |
|
---|
[25a7e11d] | 230 | ipc_answer_0(iid, ret);
|
---|
[a6e54c5d] | 231 | if (EOK != ret) {
|
---|
| 232 | return;
|
---|
| 233 | }
|
---|
[52b7b1bb] | 234 |
|
---|
[a1769ee] | 235 | while (1) {
|
---|
| 236 | ipc_callid_t callid;
|
---|
| 237 | ipc_call_t call;
|
---|
| 238 | callid = async_get_call(&call);
|
---|
| 239 | ipcarg_t method = IPC_GET_METHOD(call);
|
---|
[3843ecb] | 240 | int iface_idx;
|
---|
| 241 |
|
---|
[a1769ee] | 242 | switch (method) {
|
---|
[25a7e11d] | 243 | case IPC_M_PHONE_HUNGUP:
|
---|
| 244 | // close the device
|
---|
[5159ae9] | 245 | if (NULL != dev->ops && NULL != dev->ops->close) {
|
---|
| 246 | (*dev->ops->close)(dev);
|
---|
[25a7e11d] | 247 | }
|
---|
[a1769ee] | 248 | ipc_answer_0(callid, EOK);
|
---|
| 249 | return;
|
---|
[3843ecb] | 250 | default:
|
---|
| 251 | // convert ipc interface id to interface index
|
---|
| 252 |
|
---|
| 253 | iface_idx = DEV_IFACE_IDX(method);
|
---|
| 254 |
|
---|
| 255 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
[08d9525a] | 256 | remote_handler_t *default_handler;
|
---|
| 257 | if (NULL != (default_handler = device_get_default_handler(dev))) {
|
---|
| 258 | (*default_handler)(dev, callid, &call);
|
---|
| 259 | break;
|
---|
| 260 | }
|
---|
| 261 | // this is not device's interface and the default handler is not provided
|
---|
[f658458] | 262 | printf("%s: driver_connection_gen error - invalid interface id %d.", driver->name, iface_idx);
|
---|
[52b7b1bb] | 263 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 264 | break;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | // calling one of the device's interfaces
|
---|
| 268 |
|
---|
| 269 | // get the device interface structure
|
---|
[3843ecb] | 270 | void *iface = device_get_iface(dev, iface_idx);
|
---|
[52b7b1bb] | 271 | if (NULL == iface) {
|
---|
[9a66bc2e] | 272 | printf("%s: driver_connection_gen error - ", driver->name);
|
---|
[f658458] | 273 | printf("device with handle %d has no interface with id %d.\n", handle, iface_idx);
|
---|
[a1769ee] | 274 | ipc_answer_0(callid, ENOTSUP);
|
---|
[52b7b1bb] | 275 | break;
|
---|
[a1769ee] | 276 | }
|
---|
[52b7b1bb] | 277 |
|
---|
| 278 | // get the corresponding interface for remote request handling ("remote interface")
|
---|
[3843ecb] | 279 | remote_iface_t* rem_iface = get_remote_iface(iface_idx);
|
---|
[52b7b1bb] | 280 | assert(NULL != rem_iface);
|
---|
| 281 |
|
---|
| 282 | // get the method of the remote interface
|
---|
| 283 | ipcarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
| 284 | remote_iface_func_ptr_t iface_method_ptr = get_remote_method(rem_iface, iface_method_idx);
|
---|
| 285 | if (NULL == iface_method_ptr) {
|
---|
| 286 | // the interface has not such method
|
---|
[9a66bc2e] | 287 | printf("%s: driver_connection_gen error - invalid interface method.", driver->name);
|
---|
[52b7b1bb] | 288 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 289 | break;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | // call the remote interface's method, which will receive parameters from the remote client
|
---|
| 293 | // and it will pass it to the corresponding local interface method associated with the device
|
---|
| 294 | // by its driver
|
---|
| 295 | (*iface_method_ptr)(dev, iface, callid, &call);
|
---|
[a1769ee] | 296 | break;
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[c16cf62] | 301 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 302 | {
|
---|
[52b7b1bb] | 303 | driver_connection_gen(iid, icall, true);
|
---|
[c16cf62] | 304 | }
|
---|
| 305 |
|
---|
| 306 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 307 | {
|
---|
[52b7b1bb] | 308 | driver_connection_gen(iid, icall, false);
|
---|
[c16cf62] | 309 | }
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 | /** Function for handling connections to device driver.
|
---|
| 313 | *
|
---|
| 314 | */
|
---|
| 315 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 316 | {
|
---|
| 317 | /* Select interface */
|
---|
| 318 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
| 319 | case DRIVER_DEVMAN:
|
---|
| 320 | // handle PnP events from device manager
|
---|
| 321 | driver_connection_devman(iid, icall);
|
---|
| 322 | break;
|
---|
| 323 | case DRIVER_DRIVER:
|
---|
| 324 | // handle request from drivers of child devices
|
---|
| 325 | driver_connection_driver(iid, icall);
|
---|
| 326 | break;
|
---|
| 327 | case DRIVER_CLIENT:
|
---|
[52b7b1bb] | 328 | // handle requests from client applications
|
---|
[c16cf62] | 329 | driver_connection_client(iid, icall);
|
---|
| 330 | break;
|
---|
| 331 |
|
---|
| 332 | default:
|
---|
[52b7b1bb] | 333 | /* No such interface */
|
---|
[c16cf62] | 334 | ipc_answer_0(iid, ENOENT);
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 |
|
---|
[df747b9c] | 338 | int child_device_register(device_t *child, device_t *parent)
|
---|
[7707954] | 339 | {
|
---|
[2480e19] | 340 | // printf("%s: child_device_register\n", driver->name);
|
---|
[52b7b1bb] | 341 |
|
---|
[bda60d9] | 342 | assert(NULL != child->name);
|
---|
[52b7b1bb] | 343 |
|
---|
[df747b9c] | 344 | int res;
|
---|
| 345 |
|
---|
[9a66bc2e] | 346 | add_to_devices_list(child);
|
---|
[df747b9c] | 347 | if (EOK == (res = devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle))) {
|
---|
| 348 | return res;
|
---|
[7707954] | 349 | }
|
---|
[9a66bc2e] | 350 | remove_from_devices_list(child);
|
---|
[df747b9c] | 351 | return res;
|
---|
[7707954] | 352 | }
|
---|
| 353 |
|
---|
[52b7b1bb] | 354 | int driver_main(driver_t *drv)
|
---|
[c16cf62] | 355 | {
|
---|
| 356 | // remember the driver structure - driver_ops will be called by generic handler for incoming connections
|
---|
| 357 | driver = drv;
|
---|
[52b7b1bb] | 358 |
|
---|
[7f8b581] | 359 | // initialize the list of interrupt contexts
|
---|
| 360 | init_interrupt_context_list(&interrupt_contexts);
|
---|
| 361 |
|
---|
| 362 | // set generic interrupt handler
|
---|
| 363 | async_set_interrupt_received(driver_irq_handler);
|
---|
| 364 |
|
---|
[c16cf62] | 365 | // register driver by device manager with generic handler for incoming connections
|
---|
[52b7b1bb] | 366 | devman_driver_register(driver->name, driver_connection);
|
---|
[c16cf62] | 367 |
|
---|
| 368 | async_manager();
|
---|
| 369 |
|
---|
| 370 | // Never reached
|
---|
[52b7b1bb] | 371 | return 0;
|
---|
[c16cf62] | 372 | }
|
---|
| 373 |
|
---|
| 374 | /**
|
---|
| 375 | * @}
|
---|
[52b7b1bb] | 376 | */
|
---|