[892e4e1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
[68414f4a] | 3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[ec388d7] | 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>
|
---|
[3e6a98c5] | 44 | #include <stdbool.h>
|
---|
[892e4e1] | 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>
|
---|
[c1a8ae52] | 56 | #include <sys/stat.h>
|
---|
[ebc9c2c] | 57 | #include <irc.h>
|
---|
[1a11a16] | 58 | #include <ns.h>
|
---|
[892e4e1] | 59 |
|
---|
[af6b5157] | 60 | #include <ddf/driver.h>
|
---|
[fc51296] | 61 | #include <ddf/log.h>
|
---|
[41b56084] | 62 | #include <ops/hw_res.h>
|
---|
[ff97957] | 63 | #include <ops/pio_window.h>
|
---|
[5dc9622] | 64 |
|
---|
| 65 | #include <device/hw_res.h>
|
---|
[0a428943] | 66 | #include <device/pio_window.h>
|
---|
[892e4e1] | 67 |
|
---|
[3c5b86c] | 68 | #include <pci_dev_iface.h>
|
---|
| 69 |
|
---|
[d9cf684a] | 70 | #include "i8237.h"
|
---|
[ec388d7] | 71 |
|
---|
[892e4e1] | 72 | #define NAME "isa"
|
---|
[3c5b86c] | 73 | #define ISA_CHILD_FUN_CONF_PATH "/drv/isa/isa.dev"
|
---|
| 74 | #define EBUS_CHILD_FUN_CONF_PATH "/drv/isa/ebus.dev"
|
---|
[892e4e1] | 75 |
|
---|
[55a8e0cb] | 76 | #define ISA_MAX_HW_RES 5
|
---|
[5dc9622] | 77 |
|
---|
[f278930] | 78 | typedef struct {
|
---|
| 79 | fibril_mutex_t mutex;
|
---|
[7e9e652] | 80 | uint16_t pci_vendor_id;
|
---|
| 81 | uint16_t pci_device_id;
|
---|
| 82 | uint8_t pci_class;
|
---|
| 83 | uint8_t pci_subclass;
|
---|
[f278930] | 84 | ddf_dev_t *dev;
|
---|
| 85 | ddf_fun_t *fctl;
|
---|
[0a428943] | 86 | pio_window_t pio_win;
|
---|
[f278930] | 87 | list_t functions;
|
---|
| 88 | } isa_bus_t;
|
---|
| 89 |
|
---|
[68414f4a] | 90 | typedef struct isa_fun {
|
---|
[f278930] | 91 | fibril_mutex_t mutex;
|
---|
[83a2f43] | 92 | ddf_fun_t *fnode;
|
---|
[85c4cc45] | 93 | hw_resource_t resources[ISA_MAX_HW_RES];
|
---|
[032e0bb] | 94 | hw_resource_list_t hw_resources;
|
---|
[f278930] | 95 | link_t bus_link;
|
---|
[68414f4a] | 96 | } isa_fun_t;
|
---|
[5dc9622] | 97 |
|
---|
[56fd7cf] | 98 | /** Obtain soft-state from device node */
|
---|
| 99 | static isa_bus_t *isa_bus(ddf_dev_t *dev)
|
---|
| 100 | {
|
---|
| 101 | return ddf_dev_data_get(dev);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /** Obtain soft-state from function node */
|
---|
| 105 | static isa_fun_t *isa_fun(ddf_fun_t *fun)
|
---|
| 106 | {
|
---|
| 107 | return ddf_fun_data_get(fun);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[ff97957] | 110 | static hw_resource_list_t *isa_fun_get_resources(ddf_fun_t *fnode)
|
---|
[5dc9622] | 111 | {
|
---|
[ff97957] | 112 | isa_fun_t *fun = isa_fun(fnode);
|
---|
| 113 | assert(fun);
|
---|
[032e0bb] | 114 |
|
---|
[ff97957] | 115 | return &fun->hw_resources;
|
---|
[5dc9622] | 116 | }
|
---|
| 117 |
|
---|
[bc6762f] | 118 | static bool isa_fun_enable_interrupt(ddf_fun_t *fnode)
|
---|
[5dc9622] | 119 | {
|
---|
[1a11a16] | 120 | /* This is an old ugly way, copied from pci driver */
|
---|
| 121 | assert(fnode);
|
---|
[ff97957] | 122 | isa_fun_t *fun = isa_fun(fnode);
|
---|
| 123 | assert(fun);
|
---|
[032e0bb] | 124 |
|
---|
[ff97957] | 125 | const hw_resource_list_t *res = &fun->hw_resources;
|
---|
[1a11a16] | 126 | assert(res);
|
---|
[ec388d7] | 127 | for (size_t i = 0; i < res->count; ++i) {
|
---|
[1a11a16] | 128 | if (res->resources[i].type == INTERRUPT) {
|
---|
[ebc9c2c] | 129 | int rc = irc_enable_interrupt(
|
---|
| 130 | res->resources[i].res.interrupt.irq);
|
---|
[1a11a16] | 131 |
|
---|
[ebc9c2c] | 132 | if (rc != EOK)
|
---|
[1a11a16] | 133 | return false;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | return true;
|
---|
[5dc9622] | 138 | }
|
---|
| 139 |
|
---|
[bc6762f] | 140 | static int isa_fun_setup_dma(ddf_fun_t *fnode,
|
---|
[301032a] | 141 | unsigned int channel, uint32_t pa, uint32_t size, uint8_t mode)
|
---|
[ec388d7] | 142 | {
|
---|
| 143 | assert(fnode);
|
---|
[ff97957] | 144 | isa_fun_t *fun = isa_fun(fnode);
|
---|
| 145 | assert(fun);
|
---|
| 146 | const hw_resource_list_t *res = &fun->hw_resources;
|
---|
[ec388d7] | 147 | assert(res);
|
---|
[03362fbd] | 148 |
|
---|
[ec388d7] | 149 | for (size_t i = 0; i < res->count; ++i) {
|
---|
[1ec394e] | 150 | /* Check for assigned channel */
|
---|
[d9cf684a] | 151 | if (((res->resources[i].type == DMA_CHANNEL_16) &&
|
---|
[1ec394e] | 152 | (res->resources[i].res.dma_channel.dma16 == channel)) ||
|
---|
[d9cf684a] | 153 | ((res->resources[i].type == DMA_CHANNEL_8) &&
|
---|
[1ec394e] | 154 | (res->resources[i].res.dma_channel.dma8 == channel))) {
|
---|
| 155 | return dma_channel_setup(channel, pa, size, mode);
|
---|
[ec388d7] | 156 | }
|
---|
| 157 | }
|
---|
[03362fbd] | 158 |
|
---|
[ec388d7] | 159 | return EINVAL;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[1864948] | 162 | static int isa_fun_remain_dma(ddf_fun_t *fnode,
|
---|
[3869c596] | 163 | unsigned channel, size_t *size)
|
---|
[1864948] | 164 | {
|
---|
| 165 | assert(size);
|
---|
| 166 | assert(fnode);
|
---|
[ff97957] | 167 | isa_fun_t *fun = isa_fun(fnode);
|
---|
| 168 | assert(fun);
|
---|
| 169 | const hw_resource_list_t *res = &fun->hw_resources;
|
---|
[1864948] | 170 | assert(res);
|
---|
[03362fbd] | 171 |
|
---|
[1864948] | 172 | for (size_t i = 0; i < res->count; ++i) {
|
---|
| 173 | /* Check for assigned channel */
|
---|
| 174 | if (((res->resources[i].type == DMA_CHANNEL_16) &&
|
---|
| 175 | (res->resources[i].res.dma_channel.dma16 == channel)) ||
|
---|
| 176 | ((res->resources[i].type == DMA_CHANNEL_8) &&
|
---|
| 177 | (res->resources[i].res.dma_channel.dma8 == channel))) {
|
---|
| 178 | return dma_channel_remain(channel, size);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
[03362fbd] | 181 |
|
---|
[1864948] | 182 | return EINVAL;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[8b1e15ac] | 185 | static hw_res_ops_t isa_fun_hw_res_ops = {
|
---|
[ff97957] | 186 | .get_resource_list = isa_fun_get_resources,
|
---|
[bc6762f] | 187 | .enable_interrupt = isa_fun_enable_interrupt,
|
---|
| 188 | .dma_channel_setup = isa_fun_setup_dma,
|
---|
[1864948] | 189 | .dma_channel_remain = isa_fun_remain_dma,
|
---|
[5dc9622] | 190 | };
|
---|
| 191 |
|
---|
[ff97957] | 192 | static pio_window_t *isa_fun_get_pio_window(ddf_fun_t *fnode)
|
---|
| 193 | {
|
---|
| 194 | ddf_dev_t *dev = ddf_fun_get_dev(fnode);
|
---|
| 195 | isa_bus_t *isa = isa_bus(dev);
|
---|
| 196 | assert(isa);
|
---|
| 197 |
|
---|
| 198 | return &isa->pio_win;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | static pio_window_ops_t isa_fun_pio_window_ops = {
|
---|
| 202 | .get_pio_window = isa_fun_get_pio_window
|
---|
| 203 | };
|
---|
| 204 |
|
---|
[b28dabe] | 205 | static ddf_dev_ops_t isa_fun_ops= {
|
---|
| 206 | .interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops,
|
---|
[ff97957] | 207 | .interfaces[PIO_WINDOW_DEV_IFACE] = &isa_fun_pio_window_ops,
|
---|
[b28dabe] | 208 | };
|
---|
[5dc9622] | 209 |
|
---|
[0c0f823b] | 210 | static int isa_dev_add(ddf_dev_t *dev);
|
---|
[f278930] | 211 | static int isa_dev_remove(ddf_dev_t *dev);
|
---|
[5b68e0c] | 212 | static int isa_fun_online(ddf_fun_t *fun);
|
---|
| 213 | static int isa_fun_offline(ddf_fun_t *fun);
|
---|
[892e4e1] | 214 |
|
---|
[032e0bb] | 215 | /** The isa device driver's standard operations */
|
---|
[892e4e1] | 216 | static driver_ops_t isa_ops = {
|
---|
[0c0f823b] | 217 | .dev_add = &isa_dev_add,
|
---|
[f278930] | 218 | .dev_remove = &isa_dev_remove,
|
---|
[5b68e0c] | 219 | .fun_online = &isa_fun_online,
|
---|
| 220 | .fun_offline = &isa_fun_offline
|
---|
[892e4e1] | 221 | };
|
---|
| 222 |
|
---|
[032e0bb] | 223 | /** The isa device driver structure. */
|
---|
[892e4e1] | 224 | static driver_t isa_driver = {
|
---|
| 225 | .name = NAME,
|
---|
| 226 | .driver_ops = &isa_ops
|
---|
| 227 | };
|
---|
| 228 |
|
---|
[f278930] | 229 | static isa_fun_t *isa_fun_create(isa_bus_t *isa, const char *name)
|
---|
[c1a8ae52] | 230 | {
|
---|
[f278930] | 231 | ddf_fun_t *fnode = ddf_fun_create(isa->dev, fun_inner, name);
|
---|
| 232 | if (fnode == NULL)
|
---|
[c1a8ae52] | 233 | return NULL;
|
---|
[032e0bb] | 234 |
|
---|
[f278930] | 235 | isa_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(isa_fun_t));
|
---|
[0bbd13e] | 236 | if (fun == NULL) {
|
---|
| 237 | ddf_fun_destroy(fnode);
|
---|
[c1a8ae52] | 238 | return NULL;
|
---|
[0bbd13e] | 239 | }
|
---|
[032e0bb] | 240 |
|
---|
[f278930] | 241 | fibril_mutex_initialize(&fun->mutex);
|
---|
[85c4cc45] | 242 | fun->hw_resources.resources = fun->resources;
|
---|
| 243 |
|
---|
[68414f4a] | 244 | fun->fnode = fnode;
|
---|
[8b1e15ac] | 245 | return fun;
|
---|
[c1a8ae52] | 246 | }
|
---|
| 247 |
|
---|
[68414f4a] | 248 | static char *fun_conf_read(const char *conf_path)
|
---|
[032e0bb] | 249 | {
|
---|
| 250 | bool suc = false;
|
---|
[c1a8ae52] | 251 | char *buf = NULL;
|
---|
| 252 | bool opened = false;
|
---|
[032e0bb] | 253 | int fd;
|
---|
[6efd162] | 254 | size_t len;
|
---|
[c0393db] | 255 | ssize_t r;
|
---|
[58898d1d] | 256 | struct stat st;
|
---|
[032e0bb] | 257 |
|
---|
[c1a8ae52] | 258 | fd = open(conf_path, O_RDONLY);
|
---|
| 259 | if (fd < 0) {
|
---|
[ebcb05a] | 260 | ddf_msg(LVL_ERROR, "Unable to open %s", conf_path);
|
---|
[c1a8ae52] | 261 | goto cleanup;
|
---|
[032e0bb] | 262 | }
|
---|
| 263 |
|
---|
| 264 | opened = true;
|
---|
| 265 |
|
---|
[58898d1d] | 266 | if (fstat(fd, &st) != EOK) {
|
---|
| 267 | ddf_msg(LVL_ERROR, "Unable to fstat %d", fd);
|
---|
| 268 | goto cleanup;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | len = st.size;
|
---|
[6efd162] | 272 | if (len == 0) {
|
---|
[ebcb05a] | 273 | ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.",
|
---|
[fc51296] | 274 | conf_path);
|
---|
[032e0bb] | 275 | goto cleanup;
|
---|
[c1a8ae52] | 276 | }
|
---|
[032e0bb] | 277 |
|
---|
[6efd162] | 278 | buf = malloc(len + 1);
|
---|
[c1a8ae52] | 279 | if (buf == NULL) {
|
---|
[ebcb05a] | 280 | ddf_msg(LVL_ERROR, "Memory allocation failed.");
|
---|
[c1a8ae52] | 281 | goto cleanup;
|
---|
[032e0bb] | 282 | }
|
---|
| 283 |
|
---|
[58898d1d] | 284 | r = read(fd, (aoff64_t []) {0}, buf, len);
|
---|
[6efd162] | 285 | if (r < 0) {
|
---|
| 286 | ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
|
---|
| 287 | goto cleanup;
|
---|
| 288 | }
|
---|
[032e0bb] | 289 |
|
---|
[6efd162] | 290 | buf[len] = 0;
|
---|
[032e0bb] | 291 |
|
---|
[c1a8ae52] | 292 | suc = true;
|
---|
[032e0bb] | 293 |
|
---|
[c1a8ae52] | 294 | cleanup:
|
---|
[032e0bb] | 295 | if (!suc && buf != NULL) {
|
---|
| 296 | free(buf);
|
---|
[c1a8ae52] | 297 | buf = NULL;
|
---|
| 298 | }
|
---|
[032e0bb] | 299 |
|
---|
| 300 | if (opened)
|
---|
| 301 | close(fd);
|
---|
| 302 |
|
---|
| 303 | return buf;
|
---|
[c1a8ae52] | 304 | }
|
---|
| 305 |
|
---|
[032e0bb] | 306 | static char *str_get_line(char *str, char **next)
|
---|
| 307 | {
|
---|
[5dc9622] | 308 | char *line = str;
|
---|
[cf56332] | 309 | *next = NULL;
|
---|
[032e0bb] | 310 |
|
---|
| 311 | if (str == NULL) {
|
---|
[f928a8a] | 312 | return NULL;
|
---|
| 313 | }
|
---|
[032e0bb] | 314 |
|
---|
| 315 | while (*str != '\0' && *str != '\n') {
|
---|
[5dc9622] | 316 | str++;
|
---|
[032e0bb] | 317 | }
|
---|
| 318 |
|
---|
| 319 | if (*str != '\0') {
|
---|
[5dc9622] | 320 | *next = str + 1;
|
---|
[032e0bb] | 321 | }
|
---|
| 322 |
|
---|
| 323 | *str = '\0';
|
---|
[5dc9622] | 324 | return line;
|
---|
[c1a8ae52] | 325 | }
|
---|
| 326 |
|
---|
| 327 | static bool line_empty(const char *line)
|
---|
| 328 | {
|
---|
[032e0bb] | 329 | while (line != NULL && *line != 0) {
|
---|
| 330 | if (!isspace(*line))
|
---|
[c1a8ae52] | 331 | return false;
|
---|
[032e0bb] | 332 | line++;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | return true;
|
---|
[c1a8ae52] | 336 | }
|
---|
| 337 |
|
---|
[032e0bb] | 338 | static char *get_device_name(char *line)
|
---|
| 339 | {
|
---|
| 340 | /* Skip leading spaces. */
|
---|
| 341 | while (*line != '\0' && isspace(*line)) {
|
---|
[c1a8ae52] | 342 | line++;
|
---|
| 343 | }
|
---|
[032e0bb] | 344 |
|
---|
| 345 | /* Get the name part of the rest of the line. */
|
---|
[ee3f6f6] | 346 | str_tok(line, ":", NULL);
|
---|
[3d4ad475] | 347 | return line;
|
---|
[c1a8ae52] | 348 | }
|
---|
| 349 |
|
---|
[b5f305b] | 350 | static inline const char *skip_spaces(const char *line)
|
---|
[c1a8ae52] | 351 | {
|
---|
[032e0bb] | 352 | /* Skip leading spaces. */
|
---|
| 353 | while (*line != '\0' && isspace(*line))
|
---|
[c1a8ae52] | 354 | line++;
|
---|
| 355 |
|
---|
[032e0bb] | 356 | return line;
|
---|
| 357 | }
|
---|
[c1a8ae52] | 358 |
|
---|
[82e4005] | 359 | static void isa_fun_add_irq(isa_fun_t *fun, int irq)
|
---|
[c1a8ae52] | 360 | {
|
---|
[68414f4a] | 361 | size_t count = fun->hw_resources.count;
|
---|
| 362 | hw_resource_t *resources = fun->hw_resources.resources;
|
---|
[032e0bb] | 363 |
|
---|
[5dc9622] | 364 | if (count < ISA_MAX_HW_RES) {
|
---|
| 365 | resources[count].type = INTERRUPT;
|
---|
| 366 | resources[count].res.interrupt.irq = irq;
|
---|
[032e0bb] | 367 |
|
---|
[68414f4a] | 368 | fun->hw_resources.count++;
|
---|
[032e0bb] | 369 |
|
---|
[ebcb05a] | 370 | ddf_msg(LVL_NOTE, "Added irq 0x%x to function %s", irq,
|
---|
[56fd7cf] | 371 | ddf_fun_get_name(fun->fnode));
|
---|
[032e0bb] | 372 | }
|
---|
[5dc9622] | 373 | }
|
---|
| 374 |
|
---|
[82e4005] | 375 | static void isa_fun_add_dma(isa_fun_t *fun, int dma)
|
---|
[55a8e0cb] | 376 | {
|
---|
| 377 | size_t count = fun->hw_resources.count;
|
---|
| 378 | hw_resource_t *resources = fun->hw_resources.resources;
|
---|
[d9cf684a] | 379 |
|
---|
[55a8e0cb] | 380 | if (count < ISA_MAX_HW_RES) {
|
---|
[d9cf684a] | 381 | if ((dma > 0) && (dma < 4)) {
|
---|
[55a8e0cb] | 382 | resources[count].type = DMA_CHANNEL_8;
|
---|
| 383 | resources[count].res.dma_channel.dma8 = dma;
|
---|
[d9cf684a] | 384 |
|
---|
[55a8e0cb] | 385 | fun->hw_resources.count++;
|
---|
| 386 | ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
|
---|
[56fd7cf] | 387 | ddf_fun_get_name(fun->fnode));
|
---|
[d9cf684a] | 388 |
|
---|
[55a8e0cb] | 389 | return;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[d9cf684a] | 392 | if ((dma > 4) && (dma < 8)) {
|
---|
[55a8e0cb] | 393 | resources[count].type = DMA_CHANNEL_16;
|
---|
| 394 | resources[count].res.dma_channel.dma16 = dma;
|
---|
[d9cf684a] | 395 |
|
---|
[55a8e0cb] | 396 | fun->hw_resources.count++;
|
---|
| 397 | ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
|
---|
[56fd7cf] | 398 | ddf_fun_get_name(fun->fnode));
|
---|
[d9cf684a] | 399 |
|
---|
[55a8e0cb] | 400 | return;
|
---|
| 401 | }
|
---|
[d9cf684a] | 402 |
|
---|
[55a8e0cb] | 403 | ddf_msg(LVL_WARN, "Skipped dma 0x%x for function %s", dma,
|
---|
[56fd7cf] | 404 | ddf_fun_get_name(fun->fnode));
|
---|
[55a8e0cb] | 405 | }
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[82e4005] | 408 | static void isa_fun_add_io_range(isa_fun_t *fun, size_t addr, size_t len)
|
---|
[5dc9622] | 409 | {
|
---|
[68414f4a] | 410 | size_t count = fun->hw_resources.count;
|
---|
| 411 | hw_resource_t *resources = fun->hw_resources.resources;
|
---|
[032e0bb] | 412 |
|
---|
[0a428943] | 413 | isa_bus_t *isa = isa_bus(ddf_fun_get_dev(fun->fnode));
|
---|
| 414 |
|
---|
[5dc9622] | 415 | if (count < ISA_MAX_HW_RES) {
|
---|
| 416 | resources[count].type = IO_RANGE;
|
---|
| 417 | resources[count].res.io_range.address = addr;
|
---|
[0a428943] | 418 | resources[count].res.io_range.address += isa->pio_win.io.base;
|
---|
[5dc9622] | 419 | resources[count].res.io_range.size = len;
|
---|
[9e470c0] | 420 | resources[count].res.io_range.relative = false;
|
---|
[032e0bb] | 421 | resources[count].res.io_range.endianness = LITTLE_ENDIAN;
|
---|
| 422 |
|
---|
[68414f4a] | 423 | fun->hw_resources.count++;
|
---|
[032e0bb] | 424 |
|
---|
[fc51296] | 425 | ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
|
---|
[ebcb05a] | 426 | "function %s", (unsigned int) addr, (unsigned int) len,
|
---|
[56fd7cf] | 427 | ddf_fun_get_name(fun->fnode));
|
---|
[032e0bb] | 428 | }
|
---|
[c1a8ae52] | 429 | }
|
---|
| 430 |
|
---|
[b5f305b] | 431 | static void fun_parse_irq(isa_fun_t *fun, const char *val)
|
---|
[c1a8ae52] | 432 | {
|
---|
| 433 | int irq = 0;
|
---|
| 434 | char *end = NULL;
|
---|
[032e0bb] | 435 |
|
---|
[8b1e15ac] | 436 | val = skip_spaces(val);
|
---|
[5cc9eba] | 437 | irq = (int) strtol(val, &end, 10);
|
---|
[032e0bb] | 438 |
|
---|
| 439 | if (val != end)
|
---|
[82e4005] | 440 | isa_fun_add_irq(fun, irq);
|
---|
[c1a8ae52] | 441 | }
|
---|
| 442 |
|
---|
[b5f305b] | 443 | static void fun_parse_dma(isa_fun_t *fun, const char *val)
|
---|
[55a8e0cb] | 444 | {
|
---|
| 445 | char *end = NULL;
|
---|
[d9cf684a] | 446 |
|
---|
[55a8e0cb] | 447 | val = skip_spaces(val);
|
---|
[1ec394e] | 448 | const int dma = strtol(val, &end, 10);
|
---|
[d9cf684a] | 449 |
|
---|
[55a8e0cb] | 450 | if (val != end)
|
---|
[82e4005] | 451 | isa_fun_add_dma(fun, dma);
|
---|
[55a8e0cb] | 452 | }
|
---|
| 453 |
|
---|
[b5f305b] | 454 | static void fun_parse_io_range(isa_fun_t *fun, const char *val)
|
---|
[c1a8ae52] | 455 | {
|
---|
[5dc9622] | 456 | size_t addr, len;
|
---|
| 457 | char *end = NULL;
|
---|
[032e0bb] | 458 |
|
---|
[8b1e15ac] | 459 | val = skip_spaces(val);
|
---|
[5dc9622] | 460 | addr = strtol(val, &end, 0x10);
|
---|
[032e0bb] | 461 |
|
---|
| 462 | if (val == end)
|
---|
[5dc9622] | 463 | return;
|
---|
[032e0bb] | 464 |
|
---|
[8b1e15ac] | 465 | val = skip_spaces(end);
|
---|
[5dc9622] | 466 | len = strtol(val, &end, 0x10);
|
---|
[032e0bb] | 467 |
|
---|
| 468 | if (val == end)
|
---|
[5dc9622] | 469 | return;
|
---|
[032e0bb] | 470 |
|
---|
[82e4005] | 471 | isa_fun_add_io_range(fun, addr, len);
|
---|
[c1a8ae52] | 472 | }
|
---|
| 473 |
|
---|
[b5f305b] | 474 | static void get_match_id(char **id, const char *val)
|
---|
[c1a8ae52] | 475 | {
|
---|
[b5f305b] | 476 | const char *end = val;
|
---|
[032e0bb] | 477 |
|
---|
| 478 | while (!isspace(*end))
|
---|
[5dc9622] | 479 | end++;
|
---|
[032e0bb] | 480 |
|
---|
[f928a8a] | 481 | size_t size = end - val + 1;
|
---|
[5dc9622] | 482 | *id = (char *)malloc(size);
|
---|
[032e0bb] | 483 | str_cpy(*id, size, val);
|
---|
[5dc9622] | 484 | }
|
---|
| 485 |
|
---|
[b5f305b] | 486 | static void fun_parse_match_id(isa_fun_t *fun, const char *val)
|
---|
[032e0bb] | 487 | {
|
---|
[5dc9622] | 488 | char *id = NULL;
|
---|
| 489 | char *end = NULL;
|
---|
[032e0bb] | 490 |
|
---|
[68414f4a] | 491 | val = skip_spaces(val);
|
---|
[032e0bb] | 492 |
|
---|
[cf56332] | 493 | int score = (int)strtol(val, &end, 10);
|
---|
[5dc9622] | 494 | if (val == end) {
|
---|
[fc51296] | 495 | ddf_msg(LVL_ERROR, "Cannot read match score for function "
|
---|
[56fd7cf] | 496 | "%s.", ddf_fun_get_name(fun->fnode));
|
---|
[5dc9622] | 497 | return;
|
---|
| 498 | }
|
---|
[032e0bb] | 499 |
|
---|
[bab6388] | 500 | val = skip_spaces(end);
|
---|
[5dc9622] | 501 | get_match_id(&id, val);
|
---|
[032e0bb] | 502 | if (id == NULL) {
|
---|
[ebcb05a] | 503 | ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
|
---|
[56fd7cf] | 504 | ddf_fun_get_name(fun->fnode));
|
---|
[5dc9622] | 505 | return;
|
---|
| 506 | }
|
---|
[032e0bb] | 507 |
|
---|
[fc51296] | 508 | ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
|
---|
[56fd7cf] | 509 | "function %s", id, score, ddf_fun_get_name(fun->fnode));
|
---|
[cd0684d] | 510 |
|
---|
[cf56332] | 511 | int rc = ddf_fun_add_match_id(fun->fnode, id, score);
|
---|
[fc51296] | 512 | if (rc != EOK) {
|
---|
[ebcb05a] | 513 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
|
---|
[fc51296] | 514 | str_error(rc));
|
---|
| 515 | }
|
---|
[ef9460b] | 516 |
|
---|
| 517 | free(id);
|
---|
[c1a8ae52] | 518 | }
|
---|
| 519 |
|
---|
[b5f305b] | 520 | static bool prop_parse(isa_fun_t *fun, const char *line, const char *prop,
|
---|
| 521 | void (*read_fn)(isa_fun_t *, const char *))
|
---|
[5fe1c32] | 522 | {
|
---|
| 523 | size_t proplen = str_size(prop);
|
---|
[032e0bb] | 524 |
|
---|
| 525 | if (str_lcmp(line, prop, proplen) == 0) {
|
---|
[5fe1c32] | 526 | line += proplen;
|
---|
| 527 | line = skip_spaces(line);
|
---|
[8b1e15ac] | 528 | (*read_fn)(fun, line);
|
---|
[032e0bb] | 529 |
|
---|
[5fe1c32] | 530 | return true;
|
---|
| 531 | }
|
---|
[032e0bb] | 532 |
|
---|
| 533 | return false;
|
---|
[5fe1c32] | 534 | }
|
---|
| 535 |
|
---|
[b5f305b] | 536 | static void fun_prop_parse(isa_fun_t *fun, const char *line)
|
---|
[c1a8ae52] | 537 | {
|
---|
[032e0bb] | 538 | /* Skip leading spaces. */
|
---|
[c1a8ae52] | 539 | line = skip_spaces(line);
|
---|
[032e0bb] | 540 |
|
---|
[68414f4a] | 541 | if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
|
---|
| 542 | !prop_parse(fun, line, "irq", &fun_parse_irq) &&
|
---|
[55a8e0cb] | 543 | !prop_parse(fun, line, "dma", &fun_parse_dma) &&
|
---|
[fc51296] | 544 | !prop_parse(fun, line, "match", &fun_parse_match_id)) {
|
---|
| 545 |
|
---|
[ebcb05a] | 546 | ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
|
---|
[fc51296] | 547 | line);
|
---|
[032e0bb] | 548 | }
|
---|
[c1a8ae52] | 549 | }
|
---|
| 550 |
|
---|
[f278930] | 551 | static char *isa_fun_read_info(char *fun_conf, isa_bus_t *isa)
|
---|
[c1a8ae52] | 552 | {
|
---|
| 553 | char *line;
|
---|
[032e0bb] | 554 |
|
---|
| 555 | /* Skip empty lines. */
|
---|
[1de97fe6] | 556 | do {
|
---|
[8b1e15ac] | 557 | line = str_get_line(fun_conf, &fun_conf);
|
---|
[032e0bb] | 558 |
|
---|
| 559 | if (line == NULL) {
|
---|
| 560 | /* no more lines */
|
---|
[c1a8ae52] | 561 | return NULL;
|
---|
| 562 | }
|
---|
[032e0bb] | 563 |
|
---|
[1de97fe6] | 564 | } while (line_empty(line));
|
---|
[032e0bb] | 565 |
|
---|
| 566 | /* Get device name. */
|
---|
[3d4ad475] | 567 | const char *fun_name = get_device_name(line);
|
---|
[8b1e15ac] | 568 | if (fun_name == NULL)
|
---|
[c1a8ae52] | 569 | return NULL;
|
---|
[032e0bb] | 570 |
|
---|
[f278930] | 571 | isa_fun_t *fun = isa_fun_create(isa, fun_name);
|
---|
[8b1e15ac] | 572 | if (fun == NULL) {
|
---|
[c1a8ae52] | 573 | return NULL;
|
---|
| 574 | }
|
---|
[032e0bb] | 575 |
|
---|
| 576 | /* Get properties of the device (match ids, irq and io range). */
|
---|
| 577 | while (true) {
|
---|
[8b1e15ac] | 578 | line = str_get_line(fun_conf, &fun_conf);
|
---|
[032e0bb] | 579 |
|
---|
[5dc9622] | 580 | if (line_empty(line)) {
|
---|
[032e0bb] | 581 | /* no more device properties */
|
---|
[5dc9622] | 582 | break;
|
---|
| 583 | }
|
---|
[032e0bb] | 584 |
|
---|
| 585 | /*
|
---|
| 586 | * Get the device's property from the configuration line
|
---|
| 587 | * and store it in the device structure.
|
---|
| 588 | */
|
---|
[68414f4a] | 589 | fun_prop_parse(fun, line);
|
---|
[c1a8ae52] | 590 | }
|
---|
[032e0bb] | 591 |
|
---|
| 592 | /* Set device operations to the device. */
|
---|
[56fd7cf] | 593 | ddf_fun_set_ops(fun->fnode, &isa_fun_ops);
|
---|
[97a62fe] | 594 |
|
---|
[56fd7cf] | 595 | ddf_msg(LVL_DEBUG, "Binding function %s.", ddf_fun_get_name(fun->fnode));
|
---|
[032e0bb] | 596 |
|
---|
[97a62fe] | 597 | /* XXX Handle error */
|
---|
| 598 | (void) ddf_fun_bind(fun->fnode);
|
---|
[032e0bb] | 599 |
|
---|
[f278930] | 600 | list_append(&fun->bus_link, &isa->functions);
|
---|
| 601 |
|
---|
[8b1e15ac] | 602 | return fun_conf;
|
---|
[c1a8ae52] | 603 | }
|
---|
| 604 |
|
---|
[0d7eb0f] | 605 | static void isa_functions_add(isa_bus_t *isa)
|
---|
[c1a8ae52] | 606 | {
|
---|
[7e9e652] | 607 | #define BASE_CLASS_BRIDGE 0x06
|
---|
| 608 | #define SUB_CLASS_BRIDGE_ISA 0x01
|
---|
| 609 | bool isa_bridge = ((isa->pci_class == BASE_CLASS_BRIDGE) &&
|
---|
| 610 | (isa->pci_subclass == SUB_CLASS_BRIDGE_ISA));
|
---|
| 611 |
|
---|
[3c5b86c] | 612 | #define VENDOR_ID_SUN 0x108e
|
---|
| 613 | #define DEVICE_ID_EBUS 0x1000
|
---|
[7e9e652] | 614 | bool ebus = ((isa->pci_vendor_id == VENDOR_ID_SUN) &&
|
---|
| 615 | (isa->pci_device_id == DEVICE_ID_EBUS));
|
---|
[3c5b86c] | 616 |
|
---|
[7e9e652] | 617 | const char *conf_path = NULL;
|
---|
| 618 | if (isa_bridge)
|
---|
[3c5b86c] | 619 | conf_path = ISA_CHILD_FUN_CONF_PATH;
|
---|
[7e9e652] | 620 | else if (ebus)
|
---|
| 621 | conf_path = EBUS_CHILD_FUN_CONF_PATH;
|
---|
[3c5b86c] | 622 |
|
---|
| 623 | char *conf = fun_conf_read(conf_path);
|
---|
[032e0bb] | 624 | while (conf != NULL && *conf != '\0') {
|
---|
[f278930] | 625 | conf = isa_fun_read_info(conf, isa);
|
---|
[032e0bb] | 626 | }
|
---|
[0d7eb0f] | 627 | free(conf);
|
---|
[c1a8ae52] | 628 | }
|
---|
[892e4e1] | 629 |
|
---|
[0c0f823b] | 630 | static int isa_dev_add(ddf_dev_t *dev)
|
---|
[892e4e1] | 631 | {
|
---|
[0a428943] | 632 | async_sess_t *sess;
|
---|
| 633 | int rc;
|
---|
| 634 |
|
---|
[0c0f823b] | 635 | ddf_msg(LVL_DEBUG, "isa_dev_add, device handle = %d",
|
---|
[56fd7cf] | 636 | (int) ddf_dev_get_handle(dev));
|
---|
[032e0bb] | 637 |
|
---|
[cf56332] | 638 | isa_bus_t *isa = ddf_dev_data_alloc(dev, sizeof(isa_bus_t));
|
---|
[f278930] | 639 | if (isa == NULL)
|
---|
| 640 | return ENOMEM;
|
---|
| 641 |
|
---|
| 642 | fibril_mutex_initialize(&isa->mutex);
|
---|
| 643 | isa->dev = dev;
|
---|
| 644 | list_initialize(&isa->functions);
|
---|
| 645 |
|
---|
[f9b2cb4c] | 646 | sess = ddf_dev_parent_sess_create(dev);
|
---|
[0a428943] | 647 | if (sess == NULL) {
|
---|
| 648 | ddf_msg(LVL_ERROR, "isa_dev_add failed to connect to the "
|
---|
| 649 | "parent driver.");
|
---|
| 650 | return ENOENT;
|
---|
| 651 | }
|
---|
| 652 |
|
---|
[7e9e652] | 653 | rc = pci_config_space_read_16(sess, PCI_VENDOR_ID, &isa->pci_vendor_id);
|
---|
| 654 | if (rc != EOK)
|
---|
| 655 | return rc;
|
---|
| 656 | rc = pci_config_space_read_16(sess, PCI_DEVICE_ID, &isa->pci_device_id);
|
---|
| 657 | if (rc != EOK)
|
---|
| 658 | return rc;
|
---|
| 659 | rc = pci_config_space_read_8(sess, PCI_BASE_CLASS, &isa->pci_class);
|
---|
[3c5b86c] | 660 | if (rc != EOK)
|
---|
| 661 | return rc;
|
---|
[7e9e652] | 662 | rc = pci_config_space_read_8(sess, PCI_SUB_CLASS, &isa->pci_subclass);
|
---|
[3c5b86c] | 663 | if (rc != EOK)
|
---|
| 664 | return rc;
|
---|
| 665 |
|
---|
[0a428943] | 666 | rc = pio_window_get(sess, &isa->pio_win);
|
---|
| 667 | if (rc != EOK) {
|
---|
| 668 | ddf_msg(LVL_ERROR, "isa_dev_add failed to get PIO window "
|
---|
| 669 | "for the device.");
|
---|
| 670 | return rc;
|
---|
| 671 | }
|
---|
| 672 |
|
---|
[8b1e15ac] | 673 | /* Make the bus device more visible. Does not do anything. */
|
---|
[ebcb05a] | 674 | ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
|
---|
[8b1e15ac] | 675 |
|
---|
[f278930] | 676 | fibril_mutex_lock(&isa->mutex);
|
---|
| 677 |
|
---|
| 678 | isa->fctl = ddf_fun_create(dev, fun_exposed, "ctl");
|
---|
| 679 | if (isa->fctl == NULL) {
|
---|
[ebcb05a] | 680 | ddf_msg(LVL_ERROR, "Failed creating control function.");
|
---|
[97a62fe] | 681 | return EXDEV;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
[f278930] | 684 | if (ddf_fun_bind(isa->fctl) != EOK) {
|
---|
| 685 | ddf_fun_destroy(isa->fctl);
|
---|
[ebcb05a] | 686 | ddf_msg(LVL_ERROR, "Failed binding control function.");
|
---|
[97a62fe] | 687 | return EXDEV;
|
---|
| 688 | }
|
---|
[8b1e15ac] | 689 |
|
---|
[68414f4a] | 690 | /* Add functions as specified in the configuration file. */
|
---|
[f278930] | 691 | isa_functions_add(isa);
|
---|
[ebcb05a] | 692 | ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
|
---|
[032e0bb] | 693 |
|
---|
[f278930] | 694 | fibril_mutex_unlock(&isa->mutex);
|
---|
| 695 |
|
---|
| 696 | return EOK;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | static int isa_dev_remove(ddf_dev_t *dev)
|
---|
| 700 | {
|
---|
[56fd7cf] | 701 | isa_bus_t *isa = isa_bus(dev);
|
---|
[f278930] | 702 |
|
---|
| 703 | fibril_mutex_lock(&isa->mutex);
|
---|
| 704 |
|
---|
| 705 | while (!list_empty(&isa->functions)) {
|
---|
| 706 | isa_fun_t *fun = list_get_instance(list_first(&isa->functions),
|
---|
| 707 | isa_fun_t, bus_link);
|
---|
| 708 |
|
---|
[cf56332] | 709 | int rc = ddf_fun_offline(fun->fnode);
|
---|
[f278930] | 710 | if (rc != EOK) {
|
---|
| 711 | fibril_mutex_unlock(&isa->mutex);
|
---|
[56fd7cf] | 712 | ddf_msg(LVL_ERROR, "Failed offlining %s", ddf_fun_get_name(fun->fnode));
|
---|
[f278930] | 713 | return rc;
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | rc = ddf_fun_unbind(fun->fnode);
|
---|
| 717 | if (rc != EOK) {
|
---|
| 718 | fibril_mutex_unlock(&isa->mutex);
|
---|
[56fd7cf] | 719 | ddf_msg(LVL_ERROR, "Failed unbinding %s", ddf_fun_get_name(fun->fnode));
|
---|
[f278930] | 720 | return rc;
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | list_remove(&fun->bus_link);
|
---|
| 724 |
|
---|
| 725 | ddf_fun_destroy(fun->fnode);
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | if (ddf_fun_unbind(isa->fctl) != EOK) {
|
---|
| 729 | fibril_mutex_unlock(&isa->mutex);
|
---|
| 730 | ddf_msg(LVL_ERROR, "Failed unbinding control function.");
|
---|
| 731 | return EXDEV;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 | fibril_mutex_unlock(&isa->mutex);
|
---|
| 735 |
|
---|
[df747b9c] | 736 | return EOK;
|
---|
[892e4e1] | 737 | }
|
---|
| 738 |
|
---|
[5b68e0c] | 739 | static int isa_fun_online(ddf_fun_t *fun)
|
---|
| 740 | {
|
---|
| 741 | ddf_msg(LVL_DEBUG, "isa_fun_online()");
|
---|
| 742 | return ddf_fun_online(fun);
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | static int isa_fun_offline(ddf_fun_t *fun)
|
---|
| 746 | {
|
---|
| 747 | ddf_msg(LVL_DEBUG, "isa_fun_offline()");
|
---|
| 748 | return ddf_fun_offline(fun);
|
---|
| 749 | }
|
---|
| 750 |
|
---|
[892e4e1] | 751 | int main(int argc, char *argv[])
|
---|
| 752 | {
|
---|
[032e0bb] | 753 | printf(NAME ": HelenOS ISA bus driver\n");
|
---|
[03362fbd] | 754 | ddf_log_init(NAME);
|
---|
[83a2f43] | 755 | return ddf_driver_main(&isa_driver);
|
---|
[892e4e1] | 756 | }
|
---|
| 757 |
|
---|
| 758 | /**
|
---|
| 759 | * @}
|
---|
| 760 | */
|
---|