| [892e4e1] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| [68414f4a] | 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| [d9cf684a] | 4 | * Copyright (c) 2011 Jan Vesely
|
|---|
| [892e4e1] | 5 | * All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 8 | * modification, are permitted provided that the following conditions
|
|---|
| 9 | * are met:
|
|---|
| 10 | *
|
|---|
| 11 | * - Redistributions of source code must retain the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 15 | * documentation and/or other materials provided with the distribution.
|
|---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 17 | * derived from this software without specific prior written permission.
|
|---|
| 18 | *
|
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * @defgroup isa ISA bus driver.
|
|---|
| 33 | * @brief HelenOS ISA bus driver.
|
|---|
| 34 | * @{
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | /** @file
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| [f278930] | 40 | #include <adt/list.h>
|
|---|
| [892e4e1] | 41 | #include <assert.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>
|
|---|
| [cd0684d] | 48 | #include <str_error.h>
|
|---|
| [892e4e1] | 49 | #include <ctype.h>
|
|---|
| 50 | #include <macros.h>
|
|---|
| [c1a8ae52] | 51 | #include <malloc.h>
|
|---|
| 52 | #include <dirent.h>
|
|---|
| 53 | #include <fcntl.h>
|
|---|
| [5960b48] | 54 | #include <ipc/irc.h>
|
|---|
| 55 | #include <ipc/services.h>
|
|---|
| 56 | #include <sysinfo.h>
|
|---|
| 57 | #include <ns.h>
|
|---|
| [c1a8ae52] | 58 | #include <sys/stat.h>
|
|---|
| [d9cf684a] | 59 | #include <ipc/irc.h>
|
|---|
| 60 | #include <ipc/services.h>
|
|---|
| 61 | #include <sysinfo.h>
|
|---|
| 62 | #include <ns.h>
|
|---|
| [892e4e1] | 63 |
|
|---|
| [af6b5157] | 64 | #include <ddf/driver.h>
|
|---|
| [fc51296] | 65 | #include <ddf/log.h>
|
|---|
| [41b56084] | 66 | #include <ops/hw_res.h>
|
|---|
| [5dc9622] | 67 |
|
|---|
| [892e4e1] | 68 | #include <devman.h>
|
|---|
| 69 | #include <ipc/devman.h>
|
|---|
| [5dc9622] | 70 | #include <device/hw_res.h>
|
|---|
| [892e4e1] | 71 |
|
|---|
| [d9cf684a] | 72 | #include "i8237.h"
|
|---|
| 73 |
|
|---|
| [892e4e1] | 74 | #define NAME "isa"
|
|---|
| [8b1e15ac] | 75 | #define CHILD_FUN_CONF_PATH "/drv/isa/isa.dev"
|
|---|
| [892e4e1] | 76 |
|
|---|
| [f278930] | 77 | /** Obtain soft-state from device node */
|
|---|
| 78 | #define ISA_BUS(dev) ((isa_bus_t *) ((dev)->driver_data))
|
|---|
| 79 |
|
|---|
| 80 | /** Obtain soft-state from function node */
|
|---|
| 81 | #define ISA_FUN(fun) ((isa_fun_t *) ((fun)->driver_data))
|
|---|
| [68414f4a] | 82 |
|
|---|
| [d9cf684a] | 83 | #define ISA_MAX_HW_RES 5
|
|---|
| [5dc9622] | 84 |
|
|---|
| [f278930] | 85 | typedef struct {
|
|---|
| 86 | fibril_mutex_t mutex;
|
|---|
| 87 | ddf_dev_t *dev;
|
|---|
| 88 | ddf_fun_t *fctl;
|
|---|
| 89 | list_t functions;
|
|---|
| 90 | } isa_bus_t;
|
|---|
| 91 |
|
|---|
| [68414f4a] | 92 | typedef struct isa_fun {
|
|---|
| [f278930] | 93 | fibril_mutex_t mutex;
|
|---|
| [83a2f43] | 94 | ddf_fun_t *fnode;
|
|---|
| [032e0bb] | 95 | hw_resource_list_t hw_resources;
|
|---|
| [f278930] | 96 | link_t bus_link;
|
|---|
| [68414f4a] | 97 | } isa_fun_t;
|
|---|
| [5dc9622] | 98 |
|
|---|
| [83a2f43] | 99 | static hw_resource_list_t *isa_get_fun_resources(ddf_fun_t *fnode)
|
|---|
| [5dc9622] | 100 | {
|
|---|
| [68414f4a] | 101 | isa_fun_t *fun = ISA_FUN(fnode);
|
|---|
| 102 | assert(fun != NULL);
|
|---|
| [032e0bb] | 103 |
|
|---|
| [68414f4a] | 104 | return &fun->hw_resources;
|
|---|
| [5dc9622] | 105 | }
|
|---|
| 106 |
|
|---|
| [83a2f43] | 107 | static bool isa_enable_fun_interrupt(ddf_fun_t *fnode)
|
|---|
| [5dc9622] | 108 | {
|
|---|
| [5960b48] | 109 | /* This is an old ugly way, copied from pci driver */
|
|---|
| 110 | assert(fnode);
|
|---|
| 111 | isa_fun_t *isa_fun = fnode->driver_data;
|
|---|
| 112 |
|
|---|
| 113 | sysarg_t apic;
|
|---|
| 114 | sysarg_t i8259;
|
|---|
| 115 |
|
|---|
| 116 | async_sess_t *irc_sess = NULL;
|
|---|
| 117 |
|
|---|
| 118 | if (((sysinfo_get_value("apic", &apic) == EOK) && (apic))
|
|---|
| 119 | || ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259))) {
|
|---|
| 120 | irc_sess = service_connect_blocking(EXCHANGE_SERIALIZE,
|
|---|
| 121 | SERVICE_IRC, 0, 0);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | if (!irc_sess)
|
|---|
| 125 | return false;
|
|---|
| 126 |
|
|---|
| 127 | assert(isa_fun);
|
|---|
| 128 | const hw_resource_list_t *res = &isa_fun->hw_resources;
|
|---|
| 129 | assert(res);
|
|---|
| 130 | for (size_t i = 0; i < res->count; ++i) {
|
|---|
| 131 | if (res->resources[i].type == INTERRUPT) {
|
|---|
| 132 | const int irq = res->resources[i].res.interrupt.irq;
|
|---|
| 133 |
|
|---|
| 134 | async_exch_t *exch = async_exchange_begin(irc_sess);
|
|---|
| 135 | const int rc =
|
|---|
| 136 | async_req_1_0(exch, IRC_ENABLE_INTERRUPT, irq);
|
|---|
| 137 | async_exchange_end(exch);
|
|---|
| 138 |
|
|---|
| 139 | if (rc != EOK) {
|
|---|
| 140 | async_hangup(irc_sess);
|
|---|
| 141 | return false;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | async_hangup(irc_sess);
|
|---|
| 147 | return true;
|
|---|
| [5dc9622] | 148 | }
|
|---|
| 149 |
|
|---|
| [d9cf684a] | 150 | static int isa_dma_channel_fun_setup(ddf_fun_t *fnode,
|
|---|
| 151 | unsigned int channel, uint32_t pa, uint16_t size, uint8_t mode)
|
|---|
| 152 | {
|
|---|
| 153 | assert(fnode);
|
|---|
| 154 | isa_fun_t *isa_fun = fnode->driver_data;
|
|---|
| 155 | const hw_resource_list_t *res = &isa_fun->hw_resources;
|
|---|
| 156 | assert(res);
|
|---|
| 157 |
|
|---|
| 158 | const unsigned int ch = channel;
|
|---|
| 159 | for (size_t i = 0; i < res->count; ++i) {
|
|---|
| 160 | if (((res->resources[i].type == DMA_CHANNEL_16) &&
|
|---|
| 161 | (res->resources[i].res.dma_channel.dma16 == ch)) ||
|
|---|
| 162 | ((res->resources[i].type == DMA_CHANNEL_8) &&
|
|---|
| 163 | (res->resources[i].res.dma_channel.dma8 == ch))) {
|
|---|
| 164 | return dma_setup_channel(channel, pa, size, mode);
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | return EINVAL;
|
|---|
| [5dc9622] | 169 | }
|
|---|
| 170 |
|
|---|
| [8b1e15ac] | 171 | static hw_res_ops_t isa_fun_hw_res_ops = {
|
|---|
| [d9cf684a] | 172 | .get_resource_list = isa_get_fun_resources,
|
|---|
| 173 | .enable_interrupt = isa_enable_fun_interrupt,
|
|---|
| 174 | .dma_channel_setup = isa_dma_channel_fun_setup,
|
|---|
| [5dc9622] | 175 | };
|
|---|
| 176 |
|
|---|
| [83a2f43] | 177 | static ddf_dev_ops_t isa_fun_ops;
|
|---|
| [5dc9622] | 178 |
|
|---|
| [0c0f823b] | 179 | static int isa_dev_add(ddf_dev_t *dev);
|
|---|
| [f278930] | 180 | static int isa_dev_remove(ddf_dev_t *dev);
|
|---|
| [5b68e0c] | 181 | static int isa_fun_online(ddf_fun_t *fun);
|
|---|
| 182 | static int isa_fun_offline(ddf_fun_t *fun);
|
|---|
| [892e4e1] | 183 |
|
|---|
| [032e0bb] | 184 | /** The isa device driver's standard operations */
|
|---|
| [892e4e1] | 185 | static driver_ops_t isa_ops = {
|
|---|
| [0c0f823b] | 186 | .dev_add = &isa_dev_add,
|
|---|
| [f278930] | 187 | .dev_remove = &isa_dev_remove,
|
|---|
| [5b68e0c] | 188 | .fun_online = &isa_fun_online,
|
|---|
| 189 | .fun_offline = &isa_fun_offline
|
|---|
| [892e4e1] | 190 | };
|
|---|
| 191 |
|
|---|
| [032e0bb] | 192 | /** The isa device driver structure. */
|
|---|
| [892e4e1] | 193 | static driver_t isa_driver = {
|
|---|
| 194 | .name = NAME,
|
|---|
| 195 | .driver_ops = &isa_ops
|
|---|
| 196 | };
|
|---|
| 197 |
|
|---|
| [f278930] | 198 | static isa_fun_t *isa_fun_create(isa_bus_t *isa, const char *name)
|
|---|
| [c1a8ae52] | 199 | {
|
|---|
| [f278930] | 200 | ddf_fun_t *fnode = ddf_fun_create(isa->dev, fun_inner, name);
|
|---|
| 201 | if (fnode == NULL)
|
|---|
| [c1a8ae52] | 202 | return NULL;
|
|---|
| [032e0bb] | 203 |
|
|---|
| [f278930] | 204 | isa_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(isa_fun_t));
|
|---|
| 205 | if (fun == NULL)
|
|---|
| [c1a8ae52] | 206 | return NULL;
|
|---|
| [032e0bb] | 207 |
|
|---|
| [f278930] | 208 | fibril_mutex_initialize(&fun->mutex);
|
|---|
| [68414f4a] | 209 | fun->fnode = fnode;
|
|---|
| [8b1e15ac] | 210 | return fun;
|
|---|
| [c1a8ae52] | 211 | }
|
|---|
| 212 |
|
|---|
| [68414f4a] | 213 | static char *fun_conf_read(const char *conf_path)
|
|---|
| [032e0bb] | 214 | {
|
|---|
| 215 | bool suc = false;
|
|---|
| [c1a8ae52] | 216 | char *buf = NULL;
|
|---|
| 217 | bool opened = false;
|
|---|
| [032e0bb] | 218 | int fd;
|
|---|
| [c47e1a8] | 219 | size_t len = 0;
|
|---|
| [032e0bb] | 220 |
|
|---|
| [c1a8ae52] | 221 | fd = open(conf_path, O_RDONLY);
|
|---|
| 222 | if (fd < 0) {
|
|---|
| [ebcb05a] | 223 | ddf_msg(LVL_ERROR, "Unable to open %s", conf_path);
|
|---|
| [c1a8ae52] | 224 | goto cleanup;
|
|---|
| [032e0bb] | 225 | }
|
|---|
| 226 |
|
|---|
| 227 | opened = true;
|
|---|
| 228 |
|
|---|
| [c1a8ae52] | 229 | len = lseek(fd, 0, SEEK_END);
|
|---|
| [fc51296] | 230 | lseek(fd, 0, SEEK_SET);
|
|---|
| [c1a8ae52] | 231 | if (len == 0) {
|
|---|
| [ebcb05a] | 232 | ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.",
|
|---|
| [fc51296] | 233 | conf_path);
|
|---|
| [032e0bb] | 234 | goto cleanup;
|
|---|
| [c1a8ae52] | 235 | }
|
|---|
| [032e0bb] | 236 |
|
|---|
| [c1a8ae52] | 237 | buf = malloc(len + 1);
|
|---|
| 238 | if (buf == NULL) {
|
|---|
| [ebcb05a] | 239 | ddf_msg(LVL_ERROR, "Memory allocation failed.");
|
|---|
| [c1a8ae52] | 240 | goto cleanup;
|
|---|
| [032e0bb] | 241 | }
|
|---|
| 242 |
|
|---|
| [c1a8ae52] | 243 | if (0 >= read(fd, buf, len)) {
|
|---|
| [ebcb05a] | 244 | ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
|
|---|
| [c1a8ae52] | 245 | goto cleanup;
|
|---|
| 246 | }
|
|---|
| [032e0bb] | 247 |
|
|---|
| [c1a8ae52] | 248 | buf[len] = 0;
|
|---|
| [032e0bb] | 249 |
|
|---|
| [c1a8ae52] | 250 | suc = true;
|
|---|
| [032e0bb] | 251 |
|
|---|
| [c1a8ae52] | 252 | cleanup:
|
|---|
| [032e0bb] | 253 | if (!suc && buf != NULL) {
|
|---|
| 254 | free(buf);
|
|---|
| [c1a8ae52] | 255 | buf = NULL;
|
|---|
| 256 | }
|
|---|
| [032e0bb] | 257 |
|
|---|
| 258 | if (opened)
|
|---|
| 259 | close(fd);
|
|---|
| 260 |
|
|---|
| 261 | return buf;
|
|---|
| [c1a8ae52] | 262 | }
|
|---|
| 263 |
|
|---|
| [032e0bb] | 264 | static char *str_get_line(char *str, char **next)
|
|---|
| 265 | {
|
|---|
| [5dc9622] | 266 | char *line = str;
|
|---|
| [032e0bb] | 267 |
|
|---|
| 268 | if (str == NULL) {
|
|---|
| [f928a8a] | 269 | *next = NULL;
|
|---|
| 270 | return NULL;
|
|---|
| 271 | }
|
|---|
| [032e0bb] | 272 |
|
|---|
| 273 | while (*str != '\0' && *str != '\n') {
|
|---|
| [5dc9622] | 274 | str++;
|
|---|
| [032e0bb] | 275 | }
|
|---|
| 276 |
|
|---|
| 277 | if (*str != '\0') {
|
|---|
| [5dc9622] | 278 | *next = str + 1;
|
|---|
| 279 | } else {
|
|---|
| 280 | *next = NULL;
|
|---|
| [032e0bb] | 281 | }
|
|---|
| 282 |
|
|---|
| 283 | *str = '\0';
|
|---|
| [5dc9622] | 284 | return line;
|
|---|
| [c1a8ae52] | 285 | }
|
|---|
| 286 |
|
|---|
| 287 | static bool line_empty(const char *line)
|
|---|
| 288 | {
|
|---|
| [032e0bb] | 289 | while (line != NULL && *line != 0) {
|
|---|
| 290 | if (!isspace(*line))
|
|---|
| [c1a8ae52] | 291 | return false;
|
|---|
| [032e0bb] | 292 | line++;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | return true;
|
|---|
| [c1a8ae52] | 296 | }
|
|---|
| 297 |
|
|---|
| [032e0bb] | 298 | static char *get_device_name(char *line)
|
|---|
| 299 | {
|
|---|
| 300 | /* Skip leading spaces. */
|
|---|
| 301 | while (*line != '\0' && isspace(*line)) {
|
|---|
| [c1a8ae52] | 302 | line++;
|
|---|
| 303 | }
|
|---|
| [032e0bb] | 304 |
|
|---|
| 305 | /* Get the name part of the rest of the line. */
|
|---|
| 306 | strtok(line, ":");
|
|---|
| 307 |
|
|---|
| 308 | /* Allocate output buffer. */
|
|---|
| [5dc9622] | 309 | size_t size = str_size(line) + 1;
|
|---|
| 310 | char *name = malloc(size);
|
|---|
| [032e0bb] | 311 |
|
|---|
| 312 | if (name != NULL) {
|
|---|
| 313 | /* Copy the result to the output buffer. */
|
|---|
| [5dc9622] | 314 | str_cpy(name, size, line);
|
|---|
| [c1a8ae52] | 315 | }
|
|---|
| 316 |
|
|---|
| 317 | return name;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| [032e0bb] | 320 | static inline char *skip_spaces(char *line)
|
|---|
| [c1a8ae52] | 321 | {
|
|---|
| [032e0bb] | 322 | /* Skip leading spaces. */
|
|---|
| 323 | while (*line != '\0' && isspace(*line))
|
|---|
| [c1a8ae52] | 324 | line++;
|
|---|
| 325 |
|
|---|
| [032e0bb] | 326 | return line;
|
|---|
| 327 | }
|
|---|
| [c1a8ae52] | 328 |
|
|---|
| [68414f4a] | 329 | static void isa_fun_set_irq(isa_fun_t *fun, int irq)
|
|---|
| [c1a8ae52] | 330 | {
|
|---|
| [68414f4a] | 331 | size_t count = fun->hw_resources.count;
|
|---|
| 332 | hw_resource_t *resources = fun->hw_resources.resources;
|
|---|
| [032e0bb] | 333 |
|
|---|
| [5dc9622] | 334 | if (count < ISA_MAX_HW_RES) {
|
|---|
| 335 | resources[count].type = INTERRUPT;
|
|---|
| 336 | resources[count].res.interrupt.irq = irq;
|
|---|
| [032e0bb] | 337 |
|
|---|
| [68414f4a] | 338 | fun->hw_resources.count++;
|
|---|
| [032e0bb] | 339 |
|
|---|
| [ebcb05a] | 340 | ddf_msg(LVL_NOTE, "Added irq 0x%x to function %s", irq,
|
|---|
| [68414f4a] | 341 | fun->fnode->name);
|
|---|
| [032e0bb] | 342 | }
|
|---|
| [5dc9622] | 343 | }
|
|---|
| 344 |
|
|---|
| [d9cf684a] | 345 | static void isa_fun_set_dma(isa_fun_t *fun, int dma)
|
|---|
| 346 | {
|
|---|
| 347 | size_t count = fun->hw_resources.count;
|
|---|
| 348 | hw_resource_t *resources = fun->hw_resources.resources;
|
|---|
| 349 |
|
|---|
| 350 | if (count < ISA_MAX_HW_RES) {
|
|---|
| 351 | if ((dma > 0) && (dma < 4)) {
|
|---|
| 352 | resources[count].type = DMA_CHANNEL_8;
|
|---|
| 353 | resources[count].res.dma_channel.dma8 = dma;
|
|---|
| 354 |
|
|---|
| 355 | fun->hw_resources.count++;
|
|---|
| 356 | ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
|
|---|
| 357 | fun->fnode->name);
|
|---|
| 358 |
|
|---|
| 359 | return;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | if ((dma > 4) && (dma < 8)) {
|
|---|
| 363 | resources[count].type = DMA_CHANNEL_16;
|
|---|
| 364 | resources[count].res.dma_channel.dma16 = dma;
|
|---|
| 365 |
|
|---|
| 366 | fun->hw_resources.count++;
|
|---|
| 367 | ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
|
|---|
| 368 | fun->fnode->name);
|
|---|
| 369 |
|
|---|
| 370 | return;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | ddf_msg(LVL_WARN, "Skipped dma 0x%x for function %s", dma,
|
|---|
| 374 | fun->fnode->name);
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| [68414f4a] | 378 | static void isa_fun_set_io_range(isa_fun_t *fun, size_t addr, size_t len)
|
|---|
| [5dc9622] | 379 | {
|
|---|
| [68414f4a] | 380 | size_t count = fun->hw_resources.count;
|
|---|
| 381 | hw_resource_t *resources = fun->hw_resources.resources;
|
|---|
| [032e0bb] | 382 |
|
|---|
| [5dc9622] | 383 | if (count < ISA_MAX_HW_RES) {
|
|---|
| 384 | resources[count].type = IO_RANGE;
|
|---|
| 385 | resources[count].res.io_range.address = addr;
|
|---|
| 386 | resources[count].res.io_range.size = len;
|
|---|
| [032e0bb] | 387 | resources[count].res.io_range.endianness = LITTLE_ENDIAN;
|
|---|
| 388 |
|
|---|
| [68414f4a] | 389 | fun->hw_resources.count++;
|
|---|
| [032e0bb] | 390 |
|
|---|
| [fc51296] | 391 | ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
|
|---|
| [ebcb05a] | 392 | "function %s", (unsigned int) addr, (unsigned int) len,
|
|---|
| [68414f4a] | 393 | fun->fnode->name);
|
|---|
| [032e0bb] | 394 | }
|
|---|
| [c1a8ae52] | 395 | }
|
|---|
| 396 |
|
|---|
| [68414f4a] | 397 | static void fun_parse_irq(isa_fun_t *fun, char *val)
|
|---|
| [c1a8ae52] | 398 | {
|
|---|
| 399 | int irq = 0;
|
|---|
| 400 | char *end = NULL;
|
|---|
| [032e0bb] | 401 |
|
|---|
| [8b1e15ac] | 402 | val = skip_spaces(val);
|
|---|
| [5cc9eba] | 403 | irq = (int) strtol(val, &end, 10);
|
|---|
| [032e0bb] | 404 |
|
|---|
| 405 | if (val != end)
|
|---|
| [8b1e15ac] | 406 | isa_fun_set_irq(fun, irq);
|
|---|
| [c1a8ae52] | 407 | }
|
|---|
| 408 |
|
|---|
| [d9cf684a] | 409 | static void fun_parse_dma(isa_fun_t *fun, char *val)
|
|---|
| 410 | {
|
|---|
| 411 | unsigned int dma = 0;
|
|---|
| 412 | char *end = NULL;
|
|---|
| 413 |
|
|---|
| 414 | val = skip_spaces(val);
|
|---|
| 415 | dma = (unsigned int) strtol(val, &end, 10);
|
|---|
| 416 |
|
|---|
| 417 | if (val != end)
|
|---|
| 418 | isa_fun_set_dma(fun, dma);
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| [68414f4a] | 421 | static void fun_parse_io_range(isa_fun_t *fun, char *val)
|
|---|
| [c1a8ae52] | 422 | {
|
|---|
| [5dc9622] | 423 | size_t addr, len;
|
|---|
| 424 | char *end = NULL;
|
|---|
| [032e0bb] | 425 |
|
|---|
| [8b1e15ac] | 426 | val = skip_spaces(val);
|
|---|
| [5dc9622] | 427 | addr = strtol(val, &end, 0x10);
|
|---|
| [032e0bb] | 428 |
|
|---|
| 429 | if (val == end)
|
|---|
| [5dc9622] | 430 | return;
|
|---|
| [032e0bb] | 431 |
|
|---|
| [8b1e15ac] | 432 | val = skip_spaces(end);
|
|---|
| [5dc9622] | 433 | len = strtol(val, &end, 0x10);
|
|---|
| [032e0bb] | 434 |
|
|---|
| 435 | if (val == end)
|
|---|
| [5dc9622] | 436 | return;
|
|---|
| [032e0bb] | 437 |
|
|---|
| [8b1e15ac] | 438 | isa_fun_set_io_range(fun, addr, len);
|
|---|
| [c1a8ae52] | 439 | }
|
|---|
| 440 |
|
|---|
| [5dc9622] | 441 | static void get_match_id(char **id, char *val)
|
|---|
| [c1a8ae52] | 442 | {
|
|---|
| [5dc9622] | 443 | char *end = val;
|
|---|
| [032e0bb] | 444 |
|
|---|
| 445 | while (!isspace(*end))
|
|---|
| [5dc9622] | 446 | end++;
|
|---|
| [032e0bb] | 447 |
|
|---|
| [f928a8a] | 448 | size_t size = end - val + 1;
|
|---|
| [5dc9622] | 449 | *id = (char *)malloc(size);
|
|---|
| [032e0bb] | 450 | str_cpy(*id, size, val);
|
|---|
| [5dc9622] | 451 | }
|
|---|
| 452 |
|
|---|
| [68414f4a] | 453 | static void fun_parse_match_id(isa_fun_t *fun, char *val)
|
|---|
| [032e0bb] | 454 | {
|
|---|
| [5dc9622] | 455 | char *id = NULL;
|
|---|
| 456 | int score = 0;
|
|---|
| 457 | char *end = NULL;
|
|---|
| [cd0684d] | 458 | int rc;
|
|---|
| [032e0bb] | 459 |
|
|---|
| [68414f4a] | 460 | val = skip_spaces(val);
|
|---|
| [032e0bb] | 461 |
|
|---|
| [5fe1c32] | 462 | score = (int)strtol(val, &end, 10);
|
|---|
| [5dc9622] | 463 | if (val == end) {
|
|---|
| [fc51296] | 464 | ddf_msg(LVL_ERROR, "Cannot read match score for function "
|
|---|
| [ebcb05a] | 465 | "%s.", fun->fnode->name);
|
|---|
| [5dc9622] | 466 | return;
|
|---|
| 467 | }
|
|---|
| [032e0bb] | 468 |
|
|---|
| [bab6388] | 469 | val = skip_spaces(end);
|
|---|
| [5dc9622] | 470 | get_match_id(&id, val);
|
|---|
| [032e0bb] | 471 | if (id == NULL) {
|
|---|
| [ebcb05a] | 472 | ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
|
|---|
| [fc51296] | 473 | fun->fnode->name);
|
|---|
| [5dc9622] | 474 | return;
|
|---|
| 475 | }
|
|---|
| [032e0bb] | 476 |
|
|---|
| [fc51296] | 477 | ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
|
|---|
| [ebcb05a] | 478 | "function %s", id, score, fun->fnode->name);
|
|---|
| [cd0684d] | 479 |
|
|---|
| 480 | rc = ddf_fun_add_match_id(fun->fnode, id, score);
|
|---|
| [fc51296] | 481 | if (rc != EOK) {
|
|---|
| [ebcb05a] | 482 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
|
|---|
| [fc51296] | 483 | str_error(rc));
|
|---|
| 484 | }
|
|---|
| [ef9460b] | 485 |
|
|---|
| 486 | free(id);
|
|---|
| [c1a8ae52] | 487 | }
|
|---|
| 488 |
|
|---|
| [68414f4a] | 489 | static bool prop_parse(isa_fun_t *fun, char *line, const char *prop,
|
|---|
| 490 | void (*read_fn)(isa_fun_t *, char *))
|
|---|
| [5fe1c32] | 491 | {
|
|---|
| 492 | size_t proplen = str_size(prop);
|
|---|
| [032e0bb] | 493 |
|
|---|
| 494 | if (str_lcmp(line, prop, proplen) == 0) {
|
|---|
| [5fe1c32] | 495 | line += proplen;
|
|---|
| 496 | line = skip_spaces(line);
|
|---|
| [8b1e15ac] | 497 | (*read_fn)(fun, line);
|
|---|
| [032e0bb] | 498 |
|
|---|
| [5fe1c32] | 499 | return true;
|
|---|
| 500 | }
|
|---|
| [032e0bb] | 501 |
|
|---|
| 502 | return false;
|
|---|
| [5fe1c32] | 503 | }
|
|---|
| 504 |
|
|---|
| [68414f4a] | 505 | static void fun_prop_parse(isa_fun_t *fun, char *line)
|
|---|
| [c1a8ae52] | 506 | {
|
|---|
| [032e0bb] | 507 | /* Skip leading spaces. */
|
|---|
| [c1a8ae52] | 508 | line = skip_spaces(line);
|
|---|
| [032e0bb] | 509 |
|
|---|
| [68414f4a] | 510 | if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
|
|---|
| 511 | !prop_parse(fun, line, "irq", &fun_parse_irq) &&
|
|---|
| [d9cf684a] | 512 | !prop_parse(fun, line, "dma", &fun_parse_dma) &&
|
|---|
| [fc51296] | 513 | !prop_parse(fun, line, "match", &fun_parse_match_id)) {
|
|---|
| 514 |
|
|---|
| [ebcb05a] | 515 | ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
|
|---|
| [fc51296] | 516 | line);
|
|---|
| [032e0bb] | 517 | }
|
|---|
| [c1a8ae52] | 518 | }
|
|---|
| 519 |
|
|---|
| [68414f4a] | 520 | static void fun_hw_res_alloc(isa_fun_t *fun)
|
|---|
| [5dc9622] | 521 | {
|
|---|
| [818fffe] | 522 | fun->hw_resources.resources =
|
|---|
| [d9cf684a] | 523 | (hw_resource_t *) malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
|
|---|
| [5dc9622] | 524 | }
|
|---|
| 525 |
|
|---|
| [f278930] | 526 | static void fun_hw_res_free(isa_fun_t *fun)
|
|---|
| 527 | {
|
|---|
| 528 | free(fun->hw_resources.resources);
|
|---|
| 529 | fun->hw_resources.resources = NULL;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | static char *isa_fun_read_info(char *fun_conf, isa_bus_t *isa)
|
|---|
| [c1a8ae52] | 533 | {
|
|---|
| 534 | char *line;
|
|---|
| [8b1e15ac] | 535 | char *fun_name = NULL;
|
|---|
| [032e0bb] | 536 |
|
|---|
| 537 | /* Skip empty lines. */
|
|---|
| 538 | while (true) {
|
|---|
| [8b1e15ac] | 539 | line = str_get_line(fun_conf, &fun_conf);
|
|---|
| [032e0bb] | 540 |
|
|---|
| 541 | if (line == NULL) {
|
|---|
| 542 | /* no more lines */
|
|---|
| [c1a8ae52] | 543 | return NULL;
|
|---|
| 544 | }
|
|---|
| [032e0bb] | 545 |
|
|---|
| 546 | if (!line_empty(line))
|
|---|
| [c1a8ae52] | 547 | break;
|
|---|
| 548 | }
|
|---|
| [032e0bb] | 549 |
|
|---|
| 550 | /* Get device name. */
|
|---|
| [8b1e15ac] | 551 | fun_name = get_device_name(line);
|
|---|
| 552 | if (fun_name == NULL)
|
|---|
| [c1a8ae52] | 553 | return NULL;
|
|---|
| [032e0bb] | 554 |
|
|---|
| [f278930] | 555 | isa_fun_t *fun = isa_fun_create(isa, fun_name);
|
|---|
| [8b1e15ac] | 556 | if (fun == NULL) {
|
|---|
| 557 | free(fun_name);
|
|---|
| [c1a8ae52] | 558 | return NULL;
|
|---|
| 559 | }
|
|---|
| [032e0bb] | 560 |
|
|---|
| 561 | /* Allocate buffer for the list of hardware resources of the device. */
|
|---|
| [68414f4a] | 562 | fun_hw_res_alloc(fun);
|
|---|
| [032e0bb] | 563 |
|
|---|
| 564 | /* Get properties of the device (match ids, irq and io range). */
|
|---|
| 565 | while (true) {
|
|---|
| [8b1e15ac] | 566 | line = str_get_line(fun_conf, &fun_conf);
|
|---|
| [032e0bb] | 567 |
|
|---|
| [5dc9622] | 568 | if (line_empty(line)) {
|
|---|
| [032e0bb] | 569 | /* no more device properties */
|
|---|
| [5dc9622] | 570 | break;
|
|---|
| 571 | }
|
|---|
| [032e0bb] | 572 |
|
|---|
| 573 | /*
|
|---|
| 574 | * Get the device's property from the configuration line
|
|---|
| 575 | * and store it in the device structure.
|
|---|
| 576 | */
|
|---|
| [68414f4a] | 577 | fun_prop_parse(fun, line);
|
|---|
| [c1a8ae52] | 578 | }
|
|---|
| [032e0bb] | 579 |
|
|---|
| 580 | /* Set device operations to the device. */
|
|---|
| [97a62fe] | 581 | fun->fnode->ops = &isa_fun_ops;
|
|---|
| 582 |
|
|---|
| [ebcb05a] | 583 | ddf_msg(LVL_DEBUG, "Binding function %s.", fun->fnode->name);
|
|---|
| [032e0bb] | 584 |
|
|---|
| [97a62fe] | 585 | /* XXX Handle error */
|
|---|
| 586 | (void) ddf_fun_bind(fun->fnode);
|
|---|
| [032e0bb] | 587 |
|
|---|
| [f278930] | 588 | list_append(&fun->bus_link, &isa->functions);
|
|---|
| 589 |
|
|---|
| [8b1e15ac] | 590 | return fun_conf;
|
|---|
| [c1a8ae52] | 591 | }
|
|---|
| 592 |
|
|---|
| [f278930] | 593 | static void fun_conf_parse(char *conf, isa_bus_t *isa)
|
|---|
| [c1a8ae52] | 594 | {
|
|---|
| [032e0bb] | 595 | while (conf != NULL && *conf != '\0') {
|
|---|
| [f278930] | 596 | conf = isa_fun_read_info(conf, isa);
|
|---|
| [032e0bb] | 597 | }
|
|---|
| [c1a8ae52] | 598 | }
|
|---|
| 599 |
|
|---|
| [f278930] | 600 | static void isa_functions_add(isa_bus_t *isa)
|
|---|
| [c1a8ae52] | 601 | {
|
|---|
| [8b1e15ac] | 602 | char *fun_conf;
|
|---|
| [032e0bb] | 603 |
|
|---|
| [68414f4a] | 604 | fun_conf = fun_conf_read(CHILD_FUN_CONF_PATH);
|
|---|
| [8b1e15ac] | 605 | if (fun_conf != NULL) {
|
|---|
| [f278930] | 606 | fun_conf_parse(fun_conf, isa);
|
|---|
| [8b1e15ac] | 607 | free(fun_conf);
|
|---|
| [c1a8ae52] | 608 | }
|
|---|
| 609 | }
|
|---|
| [892e4e1] | 610 |
|
|---|
| [0c0f823b] | 611 | static int isa_dev_add(ddf_dev_t *dev)
|
|---|
| [892e4e1] | 612 | {
|
|---|
| [f278930] | 613 | isa_bus_t *isa;
|
|---|
| 614 |
|
|---|
| [0c0f823b] | 615 | ddf_msg(LVL_DEBUG, "isa_dev_add, device handle = %d",
|
|---|
| [ab3a851] | 616 | (int) dev->handle);
|
|---|
| [032e0bb] | 617 |
|
|---|
| [f278930] | 618 | isa = ddf_dev_data_alloc(dev, sizeof(isa_bus_t));
|
|---|
| 619 | if (isa == NULL)
|
|---|
| 620 | return ENOMEM;
|
|---|
| 621 |
|
|---|
| 622 | fibril_mutex_initialize(&isa->mutex);
|
|---|
| 623 | isa->dev = dev;
|
|---|
| 624 | list_initialize(&isa->functions);
|
|---|
| 625 |
|
|---|
| [8b1e15ac] | 626 | /* Make the bus device more visible. Does not do anything. */
|
|---|
| [ebcb05a] | 627 | ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
|
|---|
| [8b1e15ac] | 628 |
|
|---|
| [f278930] | 629 | fibril_mutex_lock(&isa->mutex);
|
|---|
| 630 |
|
|---|
| 631 | isa->fctl = ddf_fun_create(dev, fun_exposed, "ctl");
|
|---|
| 632 | if (isa->fctl == NULL) {
|
|---|
| [ebcb05a] | 633 | ddf_msg(LVL_ERROR, "Failed creating control function.");
|
|---|
| [97a62fe] | 634 | return EXDEV;
|
|---|
| 635 | }
|
|---|
| 636 |
|
|---|
| [f278930] | 637 | if (ddf_fun_bind(isa->fctl) != EOK) {
|
|---|
| 638 | ddf_fun_destroy(isa->fctl);
|
|---|
| [ebcb05a] | 639 | ddf_msg(LVL_ERROR, "Failed binding control function.");
|
|---|
| [97a62fe] | 640 | return EXDEV;
|
|---|
| 641 | }
|
|---|
| [8b1e15ac] | 642 |
|
|---|
| [68414f4a] | 643 | /* Add functions as specified in the configuration file. */
|
|---|
| [f278930] | 644 | isa_functions_add(isa);
|
|---|
| [ebcb05a] | 645 | ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
|
|---|
| [032e0bb] | 646 |
|
|---|
| [f278930] | 647 | fibril_mutex_unlock(&isa->mutex);
|
|---|
| 648 |
|
|---|
| 649 | return EOK;
|
|---|
| 650 | }
|
|---|
| 651 |
|
|---|
| 652 | static int isa_dev_remove(ddf_dev_t *dev)
|
|---|
| 653 | {
|
|---|
| 654 | isa_bus_t *isa = ISA_BUS(dev);
|
|---|
| 655 | int rc;
|
|---|
| 656 |
|
|---|
| 657 | fibril_mutex_lock(&isa->mutex);
|
|---|
| 658 |
|
|---|
| 659 | while (!list_empty(&isa->functions)) {
|
|---|
| 660 | isa_fun_t *fun = list_get_instance(list_first(&isa->functions),
|
|---|
| 661 | isa_fun_t, bus_link);
|
|---|
| 662 |
|
|---|
| 663 | rc = ddf_fun_offline(fun->fnode);
|
|---|
| 664 | if (rc != EOK) {
|
|---|
| 665 | fibril_mutex_unlock(&isa->mutex);
|
|---|
| 666 | ddf_msg(LVL_ERROR, "Failed offlining %s", fun->fnode->name);
|
|---|
| 667 | return rc;
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | rc = ddf_fun_unbind(fun->fnode);
|
|---|
| 671 | if (rc != EOK) {
|
|---|
| 672 | fibril_mutex_unlock(&isa->mutex);
|
|---|
| 673 | ddf_msg(LVL_ERROR, "Failed unbinding %s", fun->fnode->name);
|
|---|
| 674 | return rc;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | list_remove(&fun->bus_link);
|
|---|
| 678 |
|
|---|
| 679 | fun_hw_res_free(fun);
|
|---|
| 680 | ddf_fun_destroy(fun->fnode);
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | if (ddf_fun_unbind(isa->fctl) != EOK) {
|
|---|
| 684 | fibril_mutex_unlock(&isa->mutex);
|
|---|
| 685 | ddf_msg(LVL_ERROR, "Failed unbinding control function.");
|
|---|
| 686 | return EXDEV;
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | fibril_mutex_unlock(&isa->mutex);
|
|---|
| 690 |
|
|---|
| [df747b9c] | 691 | return EOK;
|
|---|
| [892e4e1] | 692 | }
|
|---|
| 693 |
|
|---|
| [5b68e0c] | 694 | static int isa_fun_online(ddf_fun_t *fun)
|
|---|
| 695 | {
|
|---|
| 696 | ddf_msg(LVL_DEBUG, "isa_fun_online()");
|
|---|
| 697 | return ddf_fun_online(fun);
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | static int isa_fun_offline(ddf_fun_t *fun)
|
|---|
| 701 | {
|
|---|
| 702 | ddf_msg(LVL_DEBUG, "isa_fun_offline()");
|
|---|
| 703 | return ddf_fun_offline(fun);
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| [d9cf684a] | 707 | static void isa_init()
|
|---|
| [5dc9622] | 708 | {
|
|---|
| [fc51296] | 709 | ddf_log_init(NAME, LVL_ERROR);
|
|---|
| [68414f4a] | 710 | isa_fun_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
|
|---|
| [5dc9622] | 711 | }
|
|---|
| 712 |
|
|---|
| [892e4e1] | 713 | int main(int argc, char *argv[])
|
|---|
| 714 | {
|
|---|
| [032e0bb] | 715 | printf(NAME ": HelenOS ISA bus driver\n");
|
|---|
| [5dc9622] | 716 | isa_init();
|
|---|
| [83a2f43] | 717 | return ddf_driver_main(&isa_driver);
|
|---|
| [892e4e1] | 718 | }
|
|---|
| 719 |
|
|---|
| 720 | /**
|
|---|
| 721 | * @}
|
|---|
| 722 | */
|
|---|