[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
|
---|
| 226 | if (NULL != dev->class && NULL != dev->class->open) {
|
---|
| 227 | ret = (*dev->class->open)(dev);
|
---|
| 228 | }
|
---|
[a1769ee] | 229 |
|
---|
[25a7e11d] | 230 | ipc_answer_0(iid, ret);
|
---|
[52b7b1bb] | 231 |
|
---|
[a1769ee] | 232 | while (1) {
|
---|
| 233 | ipc_callid_t callid;
|
---|
| 234 | ipc_call_t call;
|
---|
| 235 | callid = async_get_call(&call);
|
---|
| 236 | ipcarg_t method = IPC_GET_METHOD(call);
|
---|
[3843ecb] | 237 | int iface_idx;
|
---|
| 238 |
|
---|
[a1769ee] | 239 | switch (method) {
|
---|
[25a7e11d] | 240 | case IPC_M_PHONE_HUNGUP:
|
---|
| 241 | // close the device
|
---|
| 242 | if (NULL != dev->class && NULL != dev->class->close) {
|
---|
| 243 | (*dev->class->close)(dev);
|
---|
| 244 | }
|
---|
[a1769ee] | 245 | ipc_answer_0(callid, EOK);
|
---|
| 246 | return;
|
---|
[3843ecb] | 247 | default:
|
---|
| 248 | // convert ipc interface id to interface index
|
---|
| 249 |
|
---|
| 250 | iface_idx = DEV_IFACE_IDX(method);
|
---|
| 251 |
|
---|
| 252 | if (!is_valid_iface_idx(iface_idx)) {
|
---|
[08d9525a] | 253 | remote_handler_t *default_handler;
|
---|
| 254 | if (NULL != (default_handler = device_get_default_handler(dev))) {
|
---|
| 255 | (*default_handler)(dev, callid, &call);
|
---|
| 256 | break;
|
---|
| 257 | }
|
---|
| 258 | // this is not device's interface and the default handler is not provided
|
---|
[f658458] | 259 | printf("%s: driver_connection_gen error - invalid interface id %d.", driver->name, iface_idx);
|
---|
[52b7b1bb] | 260 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 261 | break;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | // calling one of the device's interfaces
|
---|
| 265 |
|
---|
| 266 | // get the device interface structure
|
---|
[3843ecb] | 267 | void *iface = device_get_iface(dev, iface_idx);
|
---|
[52b7b1bb] | 268 | if (NULL == iface) {
|
---|
[9a66bc2e] | 269 | printf("%s: driver_connection_gen error - ", driver->name);
|
---|
[f658458] | 270 | printf("device with handle %d has no interface with id %d.\n", handle, iface_idx);
|
---|
[a1769ee] | 271 | ipc_answer_0(callid, ENOTSUP);
|
---|
[52b7b1bb] | 272 | break;
|
---|
[a1769ee] | 273 | }
|
---|
[52b7b1bb] | 274 |
|
---|
| 275 | // get the corresponding interface for remote request handling ("remote interface")
|
---|
[3843ecb] | 276 | remote_iface_t* rem_iface = get_remote_iface(iface_idx);
|
---|
[52b7b1bb] | 277 | assert(NULL != rem_iface);
|
---|
| 278 |
|
---|
| 279 | // get the method of the remote interface
|
---|
| 280 | ipcarg_t iface_method_idx = IPC_GET_ARG1(call);
|
---|
| 281 | remote_iface_func_ptr_t iface_method_ptr = get_remote_method(rem_iface, iface_method_idx);
|
---|
| 282 | if (NULL == iface_method_ptr) {
|
---|
| 283 | // the interface has not such method
|
---|
[9a66bc2e] | 284 | printf("%s: driver_connection_gen error - invalid interface method.", driver->name);
|
---|
[52b7b1bb] | 285 | ipc_answer_0(callid, ENOTSUP);
|
---|
| 286 | break;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | // call the remote interface's method, which will receive parameters from the remote client
|
---|
| 290 | // and it will pass it to the corresponding local interface method associated with the device
|
---|
| 291 | // by its driver
|
---|
| 292 | (*iface_method_ptr)(dev, iface, callid, &call);
|
---|
[a1769ee] | 293 | break;
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[c16cf62] | 298 | static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 299 | {
|
---|
[52b7b1bb] | 300 | driver_connection_gen(iid, icall, true);
|
---|
[c16cf62] | 301 | }
|
---|
| 302 |
|
---|
| 303 | static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 304 | {
|
---|
[52b7b1bb] | 305 | driver_connection_gen(iid, icall, false);
|
---|
[c16cf62] | 306 | }
|
---|
| 307 |
|
---|
| 308 |
|
---|
| 309 | /** Function for handling connections to device driver.
|
---|
| 310 | *
|
---|
| 311 | */
|
---|
| 312 | static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 313 | {
|
---|
| 314 | /* Select interface */
|
---|
| 315 | switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
|
---|
| 316 | case DRIVER_DEVMAN:
|
---|
| 317 | // handle PnP events from device manager
|
---|
| 318 | driver_connection_devman(iid, icall);
|
---|
| 319 | break;
|
---|
| 320 | case DRIVER_DRIVER:
|
---|
| 321 | // handle request from drivers of child devices
|
---|
| 322 | driver_connection_driver(iid, icall);
|
---|
| 323 | break;
|
---|
| 324 | case DRIVER_CLIENT:
|
---|
[52b7b1bb] | 325 | // handle requests from client applications
|
---|
[c16cf62] | 326 | driver_connection_client(iid, icall);
|
---|
| 327 | break;
|
---|
| 328 |
|
---|
| 329 | default:
|
---|
[52b7b1bb] | 330 | /* No such interface */
|
---|
[c16cf62] | 331 | ipc_answer_0(iid, ENOENT);
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[df747b9c] | 335 | int child_device_register(device_t *child, device_t *parent)
|
---|
[7707954] | 336 | {
|
---|
[2480e19] | 337 | // printf("%s: child_device_register\n", driver->name);
|
---|
[52b7b1bb] | 338 |
|
---|
[bda60d9] | 339 | assert(NULL != child->name);
|
---|
[52b7b1bb] | 340 |
|
---|
[df747b9c] | 341 | int res;
|
---|
| 342 |
|
---|
[9a66bc2e] | 343 | add_to_devices_list(child);
|
---|
[df747b9c] | 344 | if (EOK == (res = devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle))) {
|
---|
| 345 | return res;
|
---|
[7707954] | 346 | }
|
---|
[9a66bc2e] | 347 | remove_from_devices_list(child);
|
---|
[df747b9c] | 348 | return res;
|
---|
[7707954] | 349 | }
|
---|
| 350 |
|
---|
[52b7b1bb] | 351 | int driver_main(driver_t *drv)
|
---|
[c16cf62] | 352 | {
|
---|
| 353 | // remember the driver structure - driver_ops will be called by generic handler for incoming connections
|
---|
| 354 | driver = drv;
|
---|
[52b7b1bb] | 355 |
|
---|
[7f8b581] | 356 | // initialize the list of interrupt contexts
|
---|
| 357 | init_interrupt_context_list(&interrupt_contexts);
|
---|
| 358 |
|
---|
| 359 | // set generic interrupt handler
|
---|
| 360 | async_set_interrupt_received(driver_irq_handler);
|
---|
| 361 |
|
---|
[c16cf62] | 362 | // register driver by device manager with generic handler for incoming connections
|
---|
[52b7b1bb] | 363 | devman_driver_register(driver->name, driver_connection);
|
---|
[c16cf62] | 364 |
|
---|
| 365 | async_manager();
|
---|
| 366 |
|
---|
| 367 | // Never reached
|
---|
[52b7b1bb] | 368 | return 0;
|
---|
[c16cf62] | 369 | }
|
---|
| 370 |
|
---|
| 371 | /**
|
---|
| 372 | * @}
|
---|
[52b7b1bb] | 373 | */
|
---|