source: mainline/uspace/drv/bus/isa/isa.c@ 0195374

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0195374 was 7e9e652, checked in by Jakub Jermar <jakub@…>, 9 years ago

Detect the default ISA bridge by its PCI class and subclass

  • Property mode set to 100644
File size: 16.9 KB
RevLine 
[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]78typedef 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]90typedef 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 */
99static 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 */
105static isa_fun_t *isa_fun(ddf_fun_t *fun)
106{
107 return ddf_fun_data_get(fun);
108}
109
[ff97957]110static 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]118static 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]140static 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]162static 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]185static 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]192static 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
201static pio_window_ops_t isa_fun_pio_window_ops = {
202 .get_pio_window = isa_fun_get_pio_window
203};
204
[b28dabe]205static 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]210static int isa_dev_add(ddf_dev_t *dev);
[f278930]211static int isa_dev_remove(ddf_dev_t *dev);
[5b68e0c]212static int isa_fun_online(ddf_fun_t *fun);
213static int isa_fun_offline(ddf_fun_t *fun);
[892e4e1]214
[032e0bb]215/** The isa device driver's standard operations */
[892e4e1]216static 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]224static driver_t isa_driver = {
225 .name = NAME,
226 .driver_ops = &isa_ops
227};
228
[f278930]229static 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]248static 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;
[032e0bb]256
[c1a8ae52]257 fd = open(conf_path, O_RDONLY);
258 if (fd < 0) {
[ebcb05a]259 ddf_msg(LVL_ERROR, "Unable to open %s", conf_path);
[c1a8ae52]260 goto cleanup;
[032e0bb]261 }
262
263 opened = true;
264
[6efd162]265 len = lseek(fd, 0, SEEK_END);
[fc51296]266 lseek(fd, 0, SEEK_SET);
[6efd162]267 if (len == 0) {
[ebcb05a]268 ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.",
[fc51296]269 conf_path);
[032e0bb]270 goto cleanup;
[c1a8ae52]271 }
[032e0bb]272
[6efd162]273 buf = malloc(len + 1);
[c1a8ae52]274 if (buf == NULL) {
[ebcb05a]275 ddf_msg(LVL_ERROR, "Memory allocation failed.");
[c1a8ae52]276 goto cleanup;
[032e0bb]277 }
278
[6afc9d7]279 r = read(fd, buf, len);
[6efd162]280 if (r < 0) {
281 ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
282 goto cleanup;
283 }
[032e0bb]284
[6efd162]285 buf[len] = 0;
[032e0bb]286
[c1a8ae52]287 suc = true;
[032e0bb]288
[c1a8ae52]289cleanup:
[032e0bb]290 if (!suc && buf != NULL) {
291 free(buf);
[c1a8ae52]292 buf = NULL;
293 }
[032e0bb]294
295 if (opened)
296 close(fd);
297
298 return buf;
[c1a8ae52]299}
300
[032e0bb]301static char *str_get_line(char *str, char **next)
302{
[5dc9622]303 char *line = str;
[cf56332]304 *next = NULL;
[032e0bb]305
306 if (str == NULL) {
[f928a8a]307 return NULL;
308 }
[032e0bb]309
310 while (*str != '\0' && *str != '\n') {
[5dc9622]311 str++;
[032e0bb]312 }
313
314 if (*str != '\0') {
[5dc9622]315 *next = str + 1;
[032e0bb]316 }
317
318 *str = '\0';
[5dc9622]319 return line;
[c1a8ae52]320}
321
322static bool line_empty(const char *line)
323{
[032e0bb]324 while (line != NULL && *line != 0) {
325 if (!isspace(*line))
[c1a8ae52]326 return false;
[032e0bb]327 line++;
328 }
329
330 return true;
[c1a8ae52]331}
332
[032e0bb]333static char *get_device_name(char *line)
334{
335 /* Skip leading spaces. */
336 while (*line != '\0' && isspace(*line)) {
[c1a8ae52]337 line++;
338 }
[032e0bb]339
340 /* Get the name part of the rest of the line. */
[ee3f6f6]341 str_tok(line, ":", NULL);
[3d4ad475]342 return line;
[c1a8ae52]343}
344
[b5f305b]345static inline const char *skip_spaces(const char *line)
[c1a8ae52]346{
[032e0bb]347 /* Skip leading spaces. */
348 while (*line != '\0' && isspace(*line))
[c1a8ae52]349 line++;
350
[032e0bb]351 return line;
352}
[c1a8ae52]353
[82e4005]354static void isa_fun_add_irq(isa_fun_t *fun, int irq)
[c1a8ae52]355{
[68414f4a]356 size_t count = fun->hw_resources.count;
357 hw_resource_t *resources = fun->hw_resources.resources;
[032e0bb]358
[5dc9622]359 if (count < ISA_MAX_HW_RES) {
360 resources[count].type = INTERRUPT;
361 resources[count].res.interrupt.irq = irq;
[032e0bb]362
[68414f4a]363 fun->hw_resources.count++;
[032e0bb]364
[ebcb05a]365 ddf_msg(LVL_NOTE, "Added irq 0x%x to function %s", irq,
[56fd7cf]366 ddf_fun_get_name(fun->fnode));
[032e0bb]367 }
[5dc9622]368}
369
[82e4005]370static void isa_fun_add_dma(isa_fun_t *fun, int dma)
[55a8e0cb]371{
372 size_t count = fun->hw_resources.count;
373 hw_resource_t *resources = fun->hw_resources.resources;
[d9cf684a]374
[55a8e0cb]375 if (count < ISA_MAX_HW_RES) {
[d9cf684a]376 if ((dma > 0) && (dma < 4)) {
[55a8e0cb]377 resources[count].type = DMA_CHANNEL_8;
378 resources[count].res.dma_channel.dma8 = dma;
[d9cf684a]379
[55a8e0cb]380 fun->hw_resources.count++;
381 ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
[56fd7cf]382 ddf_fun_get_name(fun->fnode));
[d9cf684a]383
[55a8e0cb]384 return;
385 }
386
[d9cf684a]387 if ((dma > 4) && (dma < 8)) {
[55a8e0cb]388 resources[count].type = DMA_CHANNEL_16;
389 resources[count].res.dma_channel.dma16 = dma;
[d9cf684a]390
[55a8e0cb]391 fun->hw_resources.count++;
392 ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
[56fd7cf]393 ddf_fun_get_name(fun->fnode));
[d9cf684a]394
[55a8e0cb]395 return;
396 }
[d9cf684a]397
[55a8e0cb]398 ddf_msg(LVL_WARN, "Skipped dma 0x%x for function %s", dma,
[56fd7cf]399 ddf_fun_get_name(fun->fnode));
[55a8e0cb]400 }
401}
402
[82e4005]403static void isa_fun_add_io_range(isa_fun_t *fun, size_t addr, size_t len)
[5dc9622]404{
[68414f4a]405 size_t count = fun->hw_resources.count;
406 hw_resource_t *resources = fun->hw_resources.resources;
[032e0bb]407
[0a428943]408 isa_bus_t *isa = isa_bus(ddf_fun_get_dev(fun->fnode));
409
[5dc9622]410 if (count < ISA_MAX_HW_RES) {
411 resources[count].type = IO_RANGE;
412 resources[count].res.io_range.address = addr;
[0a428943]413 resources[count].res.io_range.address += isa->pio_win.io.base;
[5dc9622]414 resources[count].res.io_range.size = len;
[9e470c0]415 resources[count].res.io_range.relative = false;
[032e0bb]416 resources[count].res.io_range.endianness = LITTLE_ENDIAN;
417
[68414f4a]418 fun->hw_resources.count++;
[032e0bb]419
[fc51296]420 ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
[ebcb05a]421 "function %s", (unsigned int) addr, (unsigned int) len,
[56fd7cf]422 ddf_fun_get_name(fun->fnode));
[032e0bb]423 }
[c1a8ae52]424}
425
[b5f305b]426static void fun_parse_irq(isa_fun_t *fun, const char *val)
[c1a8ae52]427{
428 int irq = 0;
429 char *end = NULL;
[032e0bb]430
[8b1e15ac]431 val = skip_spaces(val);
[5cc9eba]432 irq = (int) strtol(val, &end, 10);
[032e0bb]433
434 if (val != end)
[82e4005]435 isa_fun_add_irq(fun, irq);
[c1a8ae52]436}
437
[b5f305b]438static void fun_parse_dma(isa_fun_t *fun, const char *val)
[55a8e0cb]439{
440 char *end = NULL;
[d9cf684a]441
[55a8e0cb]442 val = skip_spaces(val);
[1ec394e]443 const int dma = strtol(val, &end, 10);
[d9cf684a]444
[55a8e0cb]445 if (val != end)
[82e4005]446 isa_fun_add_dma(fun, dma);
[55a8e0cb]447}
448
[b5f305b]449static void fun_parse_io_range(isa_fun_t *fun, const char *val)
[c1a8ae52]450{
[5dc9622]451 size_t addr, len;
452 char *end = NULL;
[032e0bb]453
[8b1e15ac]454 val = skip_spaces(val);
[5dc9622]455 addr = strtol(val, &end, 0x10);
[032e0bb]456
457 if (val == end)
[5dc9622]458 return;
[032e0bb]459
[8b1e15ac]460 val = skip_spaces(end);
[5dc9622]461 len = strtol(val, &end, 0x10);
[032e0bb]462
463 if (val == end)
[5dc9622]464 return;
[032e0bb]465
[82e4005]466 isa_fun_add_io_range(fun, addr, len);
[c1a8ae52]467}
468
[b5f305b]469static void get_match_id(char **id, const char *val)
[c1a8ae52]470{
[b5f305b]471 const char *end = val;
[032e0bb]472
473 while (!isspace(*end))
[5dc9622]474 end++;
[032e0bb]475
[f928a8a]476 size_t size = end - val + 1;
[5dc9622]477 *id = (char *)malloc(size);
[032e0bb]478 str_cpy(*id, size, val);
[5dc9622]479}
480
[b5f305b]481static void fun_parse_match_id(isa_fun_t *fun, const char *val)
[032e0bb]482{
[5dc9622]483 char *id = NULL;
484 char *end = NULL;
[032e0bb]485
[68414f4a]486 val = skip_spaces(val);
[032e0bb]487
[cf56332]488 int score = (int)strtol(val, &end, 10);
[5dc9622]489 if (val == end) {
[fc51296]490 ddf_msg(LVL_ERROR, "Cannot read match score for function "
[56fd7cf]491 "%s.", ddf_fun_get_name(fun->fnode));
[5dc9622]492 return;
493 }
[032e0bb]494
[bab6388]495 val = skip_spaces(end);
[5dc9622]496 get_match_id(&id, val);
[032e0bb]497 if (id == NULL) {
[ebcb05a]498 ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
[56fd7cf]499 ddf_fun_get_name(fun->fnode));
[5dc9622]500 return;
501 }
[032e0bb]502
[fc51296]503 ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
[56fd7cf]504 "function %s", id, score, ddf_fun_get_name(fun->fnode));
[cd0684d]505
[cf56332]506 int rc = ddf_fun_add_match_id(fun->fnode, id, score);
[fc51296]507 if (rc != EOK) {
[ebcb05a]508 ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
[fc51296]509 str_error(rc));
510 }
[ef9460b]511
512 free(id);
[c1a8ae52]513}
514
[b5f305b]515static bool prop_parse(isa_fun_t *fun, const char *line, const char *prop,
516 void (*read_fn)(isa_fun_t *, const char *))
[5fe1c32]517{
518 size_t proplen = str_size(prop);
[032e0bb]519
520 if (str_lcmp(line, prop, proplen) == 0) {
[5fe1c32]521 line += proplen;
522 line = skip_spaces(line);
[8b1e15ac]523 (*read_fn)(fun, line);
[032e0bb]524
[5fe1c32]525 return true;
526 }
[032e0bb]527
528 return false;
[5fe1c32]529}
530
[b5f305b]531static void fun_prop_parse(isa_fun_t *fun, const char *line)
[c1a8ae52]532{
[032e0bb]533 /* Skip leading spaces. */
[c1a8ae52]534 line = skip_spaces(line);
[032e0bb]535
[68414f4a]536 if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
537 !prop_parse(fun, line, "irq", &fun_parse_irq) &&
[55a8e0cb]538 !prop_parse(fun, line, "dma", &fun_parse_dma) &&
[fc51296]539 !prop_parse(fun, line, "match", &fun_parse_match_id)) {
540
[ebcb05a]541 ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
[fc51296]542 line);
[032e0bb]543 }
[c1a8ae52]544}
545
[f278930]546static char *isa_fun_read_info(char *fun_conf, isa_bus_t *isa)
[c1a8ae52]547{
548 char *line;
[032e0bb]549
550 /* Skip empty lines. */
[1de97fe6]551 do {
[8b1e15ac]552 line = str_get_line(fun_conf, &fun_conf);
[032e0bb]553
554 if (line == NULL) {
555 /* no more lines */
[c1a8ae52]556 return NULL;
557 }
[032e0bb]558
[1de97fe6]559 } while (line_empty(line));
[032e0bb]560
561 /* Get device name. */
[3d4ad475]562 const char *fun_name = get_device_name(line);
[8b1e15ac]563 if (fun_name == NULL)
[c1a8ae52]564 return NULL;
[032e0bb]565
[f278930]566 isa_fun_t *fun = isa_fun_create(isa, fun_name);
[8b1e15ac]567 if (fun == NULL) {
[c1a8ae52]568 return NULL;
569 }
[032e0bb]570
571 /* Get properties of the device (match ids, irq and io range). */
572 while (true) {
[8b1e15ac]573 line = str_get_line(fun_conf, &fun_conf);
[032e0bb]574
[5dc9622]575 if (line_empty(line)) {
[032e0bb]576 /* no more device properties */
[5dc9622]577 break;
578 }
[032e0bb]579
580 /*
581 * Get the device's property from the configuration line
582 * and store it in the device structure.
583 */
[68414f4a]584 fun_prop_parse(fun, line);
[c1a8ae52]585 }
[032e0bb]586
587 /* Set device operations to the device. */
[56fd7cf]588 ddf_fun_set_ops(fun->fnode, &isa_fun_ops);
[97a62fe]589
[56fd7cf]590 ddf_msg(LVL_DEBUG, "Binding function %s.", ddf_fun_get_name(fun->fnode));
[032e0bb]591
[97a62fe]592 /* XXX Handle error */
593 (void) ddf_fun_bind(fun->fnode);
[032e0bb]594
[f278930]595 list_append(&fun->bus_link, &isa->functions);
596
[8b1e15ac]597 return fun_conf;
[c1a8ae52]598}
599
[0d7eb0f]600static void isa_functions_add(isa_bus_t *isa)
[c1a8ae52]601{
[7e9e652]602#define BASE_CLASS_BRIDGE 0x06
603#define SUB_CLASS_BRIDGE_ISA 0x01
604 bool isa_bridge = ((isa->pci_class == BASE_CLASS_BRIDGE) &&
605 (isa->pci_subclass == SUB_CLASS_BRIDGE_ISA));
606
[3c5b86c]607#define VENDOR_ID_SUN 0x108e
608#define DEVICE_ID_EBUS 0x1000
[7e9e652]609 bool ebus = ((isa->pci_vendor_id == VENDOR_ID_SUN) &&
610 (isa->pci_device_id == DEVICE_ID_EBUS));
[3c5b86c]611
[7e9e652]612 const char *conf_path = NULL;
613 if (isa_bridge)
[3c5b86c]614 conf_path = ISA_CHILD_FUN_CONF_PATH;
[7e9e652]615 else if (ebus)
616 conf_path = EBUS_CHILD_FUN_CONF_PATH;
[3c5b86c]617
618 char *conf = fun_conf_read(conf_path);
[032e0bb]619 while (conf != NULL && *conf != '\0') {
[f278930]620 conf = isa_fun_read_info(conf, isa);
[032e0bb]621 }
[0d7eb0f]622 free(conf);
[c1a8ae52]623}
[892e4e1]624
[0c0f823b]625static int isa_dev_add(ddf_dev_t *dev)
[892e4e1]626{
[0a428943]627 async_sess_t *sess;
628 int rc;
629
[0c0f823b]630 ddf_msg(LVL_DEBUG, "isa_dev_add, device handle = %d",
[56fd7cf]631 (int) ddf_dev_get_handle(dev));
[032e0bb]632
[cf56332]633 isa_bus_t *isa = ddf_dev_data_alloc(dev, sizeof(isa_bus_t));
[f278930]634 if (isa == NULL)
635 return ENOMEM;
636
637 fibril_mutex_initialize(&isa->mutex);
638 isa->dev = dev;
639 list_initialize(&isa->functions);
640
[f9b2cb4c]641 sess = ddf_dev_parent_sess_create(dev);
[0a428943]642 if (sess == NULL) {
643 ddf_msg(LVL_ERROR, "isa_dev_add failed to connect to the "
644 "parent driver.");
645 return ENOENT;
646 }
647
[7e9e652]648 rc = pci_config_space_read_16(sess, PCI_VENDOR_ID, &isa->pci_vendor_id);
649 if (rc != EOK)
650 return rc;
651 rc = pci_config_space_read_16(sess, PCI_DEVICE_ID, &isa->pci_device_id);
652 if (rc != EOK)
653 return rc;
654 rc = pci_config_space_read_8(sess, PCI_BASE_CLASS, &isa->pci_class);
[3c5b86c]655 if (rc != EOK)
656 return rc;
[7e9e652]657 rc = pci_config_space_read_8(sess, PCI_SUB_CLASS, &isa->pci_subclass);
[3c5b86c]658 if (rc != EOK)
659 return rc;
660
[0a428943]661 rc = pio_window_get(sess, &isa->pio_win);
662 if (rc != EOK) {
663 ddf_msg(LVL_ERROR, "isa_dev_add failed to get PIO window "
664 "for the device.");
665 return rc;
666 }
667
[8b1e15ac]668 /* Make the bus device more visible. Does not do anything. */
[ebcb05a]669 ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
[8b1e15ac]670
[f278930]671 fibril_mutex_lock(&isa->mutex);
672
673 isa->fctl = ddf_fun_create(dev, fun_exposed, "ctl");
674 if (isa->fctl == NULL) {
[ebcb05a]675 ddf_msg(LVL_ERROR, "Failed creating control function.");
[97a62fe]676 return EXDEV;
677 }
678
[f278930]679 if (ddf_fun_bind(isa->fctl) != EOK) {
680 ddf_fun_destroy(isa->fctl);
[ebcb05a]681 ddf_msg(LVL_ERROR, "Failed binding control function.");
[97a62fe]682 return EXDEV;
683 }
[8b1e15ac]684
[68414f4a]685 /* Add functions as specified in the configuration file. */
[f278930]686 isa_functions_add(isa);
[ebcb05a]687 ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
[032e0bb]688
[f278930]689 fibril_mutex_unlock(&isa->mutex);
690
691 return EOK;
692}
693
694static int isa_dev_remove(ddf_dev_t *dev)
695{
[56fd7cf]696 isa_bus_t *isa = isa_bus(dev);
[f278930]697
698 fibril_mutex_lock(&isa->mutex);
699
700 while (!list_empty(&isa->functions)) {
701 isa_fun_t *fun = list_get_instance(list_first(&isa->functions),
702 isa_fun_t, bus_link);
703
[cf56332]704 int rc = ddf_fun_offline(fun->fnode);
[f278930]705 if (rc != EOK) {
706 fibril_mutex_unlock(&isa->mutex);
[56fd7cf]707 ddf_msg(LVL_ERROR, "Failed offlining %s", ddf_fun_get_name(fun->fnode));
[f278930]708 return rc;
709 }
710
711 rc = ddf_fun_unbind(fun->fnode);
712 if (rc != EOK) {
713 fibril_mutex_unlock(&isa->mutex);
[56fd7cf]714 ddf_msg(LVL_ERROR, "Failed unbinding %s", ddf_fun_get_name(fun->fnode));
[f278930]715 return rc;
716 }
717
718 list_remove(&fun->bus_link);
719
720 ddf_fun_destroy(fun->fnode);
721 }
722
723 if (ddf_fun_unbind(isa->fctl) != EOK) {
724 fibril_mutex_unlock(&isa->mutex);
725 ddf_msg(LVL_ERROR, "Failed unbinding control function.");
726 return EXDEV;
727 }
728
729 fibril_mutex_unlock(&isa->mutex);
730
[df747b9c]731 return EOK;
[892e4e1]732}
733
[5b68e0c]734static int isa_fun_online(ddf_fun_t *fun)
735{
736 ddf_msg(LVL_DEBUG, "isa_fun_online()");
737 return ddf_fun_online(fun);
738}
739
740static int isa_fun_offline(ddf_fun_t *fun)
741{
742 ddf_msg(LVL_DEBUG, "isa_fun_offline()");
743 return ddf_fun_offline(fun);
744}
745
[892e4e1]746int main(int argc, char *argv[])
747{
[032e0bb]748 printf(NAME ": HelenOS ISA bus driver\n");
[03362fbd]749 ddf_log_init(NAME);
[83a2f43]750 return ddf_driver_main(&isa_driver);
[892e4e1]751}
752
753/**
754 * @}
755 */
Note: See TracBrowser for help on using the repository browser.