source: mainline/uspace/drv/bus/isa/isa.c@ 899f1a9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 899f1a9 was 0c0f823b, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Rename DDF entry point add_device to dev_add.

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