source: mainline/uspace/drv/isa/isa.c@ 8b1e15ac

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

Finish splitting device node: devman client in C library, drv library. Update device drivers accordingly.

  • Property mode set to 100644
File size: 10.8 KB
RevLine 
[892e4e1]1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @defgroup isa ISA bus driver.
31 * @brief HelenOS ISA bus driver.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <stdio.h>
40#include <errno.h>
41#include <bool.h>
42#include <fibril_synch.h>
43#include <stdlib.h>
[c47e1a8]44#include <str.h>
[892e4e1]45#include <ctype.h>
46#include <macros.h>
[c1a8ae52]47#include <malloc.h>
48#include <dirent.h>
49#include <fcntl.h>
50#include <sys/stat.h>
[892e4e1]51
52#include <driver.h>
[41b56084]53#include <ops/hw_res.h>
[5dc9622]54
[892e4e1]55#include <devman.h>
56#include <ipc/devman.h>
[5dc9622]57#include <device/hw_res.h>
[892e4e1]58
59#define NAME "isa"
[8b1e15ac]60#define CHILD_FUN_CONF_PATH "/drv/isa/isa.dev"
[892e4e1]61
[5dc9622]62#define ISA_MAX_HW_RES 4
63
[8b1e15ac]64typedef struct isa_fun_data {
[032e0bb]65 hw_resource_list_t hw_resources;
[8b1e15ac]66} isa_fun_data_t;
[5dc9622]67
[8b1e15ac]68static hw_resource_list_t *isa_get_fun_resources(function_t *fun)
[5dc9622]69{
[8b1e15ac]70 isa_fun_data_t *fun_data;
[032e0bb]71
[8b1e15ac]72 fun_data = (isa_fun_data_t *)fun->driver_data;
73 if (fun_data == NULL)
[5dc9622]74 return NULL;
[032e0bb]75
[8b1e15ac]76 return &fun_data->hw_resources;
[5dc9622]77}
78
[8b1e15ac]79static bool isa_enable_fun_interrupt(function_t *fun)
[5dc9622]80{
81 // TODO
[032e0bb]82
[5dc9622]83 return false;
84}
85
[8b1e15ac]86static hw_res_ops_t isa_fun_hw_res_ops = {
87 &isa_get_fun_resources,
88 &isa_enable_fun_interrupt
[5dc9622]89};
90
[8b1e15ac]91static device_ops_t isa_fun_dev_ops;
[5dc9622]92
[df747b9c]93static int isa_add_device(device_t *dev);
[892e4e1]94
[032e0bb]95/** The isa device driver's standard operations */
[892e4e1]96static driver_ops_t isa_ops = {
97 .add_device = &isa_add_device
98};
99
[032e0bb]100/** The isa device driver structure. */
[892e4e1]101static driver_t isa_driver = {
102 .name = NAME,
103 .driver_ops = &isa_ops
104};
105
[c1a8ae52]106
[8b1e15ac]107static isa_fun_data_t *create_isa_fun_data()
[c1a8ae52]108{
[8b1e15ac]109 isa_fun_data_t *data;
[032e0bb]110
[8b1e15ac]111 data = (isa_fun_data_t *) malloc(sizeof(isa_fun_data_t));
[032e0bb]112 if (data != NULL)
[8b1e15ac]113 memset(data, 0, sizeof(isa_fun_data_t));
[032e0bb]114
115 return data;
[c1a8ae52]116}
117
[8b1e15ac]118static function_t *create_isa_fun()
[c1a8ae52]119{
[8b1e15ac]120 function_t *fun = create_function();
121 if (fun == NULL)
[c1a8ae52]122 return NULL;
[032e0bb]123
[8b1e15ac]124 isa_fun_data_t *data = create_isa_fun_data();
[032e0bb]125 if (data == NULL) {
[8b1e15ac]126 delete_function(fun);
[c1a8ae52]127 return NULL;
128 }
[032e0bb]129
[8b1e15ac]130 fun->driver_data = data;
131 return fun;
[c1a8ae52]132}
133
[8b1e15ac]134static char *read_fun_conf(const char *conf_path)
[032e0bb]135{
136 bool suc = false;
[c1a8ae52]137 char *buf = NULL;
138 bool opened = false;
[032e0bb]139 int fd;
[c47e1a8]140 size_t len = 0;
[032e0bb]141
[c1a8ae52]142 fd = open(conf_path, O_RDONLY);
143 if (fd < 0) {
144 printf(NAME ": unable to open %s\n", conf_path);
145 goto cleanup;
[032e0bb]146 }
147
148 opened = true;
149
[c1a8ae52]150 len = lseek(fd, 0, SEEK_END);
151 lseek(fd, 0, SEEK_SET);
152 if (len == 0) {
[8b1e15ac]153 printf(NAME ": read_fun_conf error: configuration file '%s' "
[032e0bb]154 "is empty.\n", conf_path);
155 goto cleanup;
[c1a8ae52]156 }
[032e0bb]157
[c1a8ae52]158 buf = malloc(len + 1);
159 if (buf == NULL) {
[8b1e15ac]160 printf(NAME ": read_fun_conf error: memory allocation failed.\n");
[c1a8ae52]161 goto cleanup;
[032e0bb]162 }
163
[c1a8ae52]164 if (0 >= read(fd, buf, len)) {
[8b1e15ac]165 printf(NAME ": read_fun_conf error: unable to read file '%s'.\n",
[032e0bb]166 conf_path);
[c1a8ae52]167 goto cleanup;
168 }
[032e0bb]169
[c1a8ae52]170 buf[len] = 0;
[032e0bb]171
[c1a8ae52]172 suc = true;
[032e0bb]173
[c1a8ae52]174cleanup:
[032e0bb]175 if (!suc && buf != NULL) {
176 free(buf);
[c1a8ae52]177 buf = NULL;
178 }
[032e0bb]179
180 if (opened)
181 close(fd);
182
183 return buf;
[c1a8ae52]184}
185
[032e0bb]186static char *str_get_line(char *str, char **next)
187{
[5dc9622]188 char *line = str;
[032e0bb]189
190 if (str == NULL) {
[f928a8a]191 *next = NULL;
192 return NULL;
193 }
[032e0bb]194
195 while (*str != '\0' && *str != '\n') {
[5dc9622]196 str++;
[032e0bb]197 }
198
199 if (*str != '\0') {
[5dc9622]200 *next = str + 1;
201 } else {
202 *next = NULL;
[032e0bb]203 }
204
205 *str = '\0';
[5dc9622]206 return line;
[c1a8ae52]207}
208
209static bool line_empty(const char *line)
210{
[032e0bb]211 while (line != NULL && *line != 0) {
212 if (!isspace(*line))
[c1a8ae52]213 return false;
[032e0bb]214 line++;
215 }
216
217 return true;
[c1a8ae52]218}
219
[032e0bb]220static char *get_device_name(char *line)
221{
222 /* Skip leading spaces. */
223 while (*line != '\0' && isspace(*line)) {
[c1a8ae52]224 line++;
225 }
[032e0bb]226
227 /* Get the name part of the rest of the line. */
228 strtok(line, ":");
229
230 /* Allocate output buffer. */
[5dc9622]231 size_t size = str_size(line) + 1;
232 char *name = malloc(size);
[032e0bb]233
234 if (name != NULL) {
235 /* Copy the result to the output buffer. */
[5dc9622]236 str_cpy(name, size, line);
[c1a8ae52]237 }
238
239 return name;
240}
241
[032e0bb]242static inline char *skip_spaces(char *line)
[c1a8ae52]243{
[032e0bb]244 /* Skip leading spaces. */
245 while (*line != '\0' && isspace(*line))
[c1a8ae52]246 line++;
247
[032e0bb]248 return line;
249}
[c1a8ae52]250
[8b1e15ac]251static void isa_fun_set_irq(function_t *fun, int irq)
[c1a8ae52]252{
[8b1e15ac]253 isa_fun_data_t *data = (isa_fun_data_t *)fun->driver_data;
[032e0bb]254
[5dc9622]255 size_t count = data->hw_resources.count;
256 hw_resource_t *resources = data->hw_resources.resources;
[032e0bb]257
[5dc9622]258 if (count < ISA_MAX_HW_RES) {
259 resources[count].type = INTERRUPT;
260 resources[count].res.interrupt.irq = irq;
[032e0bb]261
[5dc9622]262 data->hw_resources.count++;
[032e0bb]263
[8b1e15ac]264 printf(NAME ": added irq 0x%x to function %s\n", irq, fun->name);
[032e0bb]265 }
[5dc9622]266}
267
[8b1e15ac]268static void isa_fun_set_io_range(function_t *fun, size_t addr, size_t len)
[5dc9622]269{
[8b1e15ac]270 isa_fun_data_t *data = (isa_fun_data_t *)fun->driver_data;
[032e0bb]271
[5dc9622]272 size_t count = data->hw_resources.count;
273 hw_resource_t *resources = data->hw_resources.resources;
[032e0bb]274
[5dc9622]275 if (count < ISA_MAX_HW_RES) {
276 resources[count].type = IO_RANGE;
277 resources[count].res.io_range.address = addr;
278 resources[count].res.io_range.size = len;
[032e0bb]279 resources[count].res.io_range.endianness = LITTLE_ENDIAN;
280
[5dc9622]281 data->hw_resources.count++;
[032e0bb]282
283 printf(NAME ": added io range (addr=0x%x, size=0x%x) to "
[8b1e15ac]284 "function %s\n", (unsigned int) addr, (unsigned int) len,
285 fun->name);
[032e0bb]286 }
[c1a8ae52]287}
288
[8b1e15ac]289static void get_dev_irq(function_t *fun, char *val)
[c1a8ae52]290{
291 int irq = 0;
292 char *end = NULL;
[032e0bb]293
[8b1e15ac]294 val = skip_spaces(val);
[5dc9622]295 irq = (int)strtol(val, &end, 0x10);
[032e0bb]296
297 if (val != end)
[8b1e15ac]298 isa_fun_set_irq(fun, irq);
[c1a8ae52]299}
300
[8b1e15ac]301static void get_dev_io_range(function_t *fun, char *val)
[c1a8ae52]302{
[5dc9622]303 size_t addr, len;
304 char *end = NULL;
[032e0bb]305
[8b1e15ac]306 val = skip_spaces(val);
[5dc9622]307 addr = strtol(val, &end, 0x10);
[032e0bb]308
309 if (val == end)
[5dc9622]310 return;
[032e0bb]311
[8b1e15ac]312 val = skip_spaces(end);
[5dc9622]313 len = strtol(val, &end, 0x10);
[032e0bb]314
315 if (val == end)
[5dc9622]316 return;
[032e0bb]317
[8b1e15ac]318 isa_fun_set_io_range(fun, addr, len);
[c1a8ae52]319}
320
[5dc9622]321static void get_match_id(char **id, char *val)
[c1a8ae52]322{
[5dc9622]323 char *end = val;
[032e0bb]324
325 while (!isspace(*end))
[5dc9622]326 end++;
[032e0bb]327
[f928a8a]328 size_t size = end - val + 1;
[5dc9622]329 *id = (char *)malloc(size);
[032e0bb]330 str_cpy(*id, size, val);
[5dc9622]331}
332
[8b1e15ac]333static void get_fun_match_id(function_t *fun, char *val)
[032e0bb]334{
[5dc9622]335 char *id = NULL;
336 int score = 0;
337 char *end = NULL;
[032e0bb]338
[5dc9622]339 val = skip_spaces(val);
[032e0bb]340
[5fe1c32]341 score = (int)strtol(val, &end, 10);
[5dc9622]342 if (val == end) {
[032e0bb]343 printf(NAME " : error - could not read match score for "
[8b1e15ac]344 "function %s.\n", fun->name);
[5dc9622]345 return;
346 }
[032e0bb]347
[5dc9622]348 match_id_t *match_id = create_match_id();
[032e0bb]349 if (match_id == NULL) {
[8b1e15ac]350 printf(NAME " : failed to allocate match id for function %s.\n",
351 fun->name);
[5dc9622]352 return;
353 }
[032e0bb]354
[5dc9622]355 val = skip_spaces(end);
356 get_match_id(&id, val);
[032e0bb]357 if (id == NULL) {
358 printf(NAME " : error - could not read match id for "
[8b1e15ac]359 "function %s.\n", fun->name);
[5dc9622]360 delete_match_id(match_id);
361 return;
362 }
[032e0bb]363
[5dc9622]364 match_id->id = id;
365 match_id->score = score;
[032e0bb]366
[8b1e15ac]367 printf(NAME ": adding match id '%s' with score %d to function %s\n", id,
368 score, fun->name);
369 add_match_id(&fun->match_ids, match_id);
[c1a8ae52]370}
371
[8b1e15ac]372static bool read_fun_prop(function_t *fun, char *line, const char *prop,
373 void (*read_fn)(function_t *, char *))
[5fe1c32]374{
375 size_t proplen = str_size(prop);
[032e0bb]376
377 if (str_lcmp(line, prop, proplen) == 0) {
[5fe1c32]378 line += proplen;
379 line = skip_spaces(line);
[8b1e15ac]380 (*read_fn)(fun, line);
[032e0bb]381
[5fe1c32]382 return true;
383 }
[032e0bb]384
385 return false;
[5fe1c32]386}
387
[8b1e15ac]388static void get_fun_prop(function_t *fun, char *line)
[c1a8ae52]389{
[032e0bb]390 /* Skip leading spaces. */
[c1a8ae52]391 line = skip_spaces(line);
[032e0bb]392
[8b1e15ac]393 if (!read_fun_prop(fun, line, "io_range", &get_dev_io_range) &&
394 !read_fun_prop(fun, line, "irq", &get_dev_irq) &&
395 !read_fun_prop(fun, line, "match", &get_fun_match_id))
[032e0bb]396 {
397 printf(NAME " error undefined device property at line '%s'\n",
398 line);
399 }
[c1a8ae52]400}
401
[8b1e15ac]402static void child_alloc_hw_res(function_t *fun)
[5dc9622]403{
[8b1e15ac]404 isa_fun_data_t *data = (isa_fun_data_t *)fun->driver_data;
[5dc9622]405 data->hw_resources.resources =
[032e0bb]406 (hw_resource_t *)malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
[5dc9622]407}
408
[8b1e15ac]409static char *read_isa_fun_info(char *fun_conf, device_t *dev)
[c1a8ae52]410{
411 char *line;
[8b1e15ac]412 char *fun_name = NULL;
[032e0bb]413
414 /* Skip empty lines. */
415 while (true) {
[8b1e15ac]416 line = str_get_line(fun_conf, &fun_conf);
[032e0bb]417
418 if (line == NULL) {
419 /* no more lines */
[c1a8ae52]420 return NULL;
421 }
[032e0bb]422
423 if (!line_empty(line))
[c1a8ae52]424 break;
425 }
[032e0bb]426
427 /* Get device name. */
[8b1e15ac]428 fun_name = get_device_name(line);
429 if (fun_name == NULL)
[c1a8ae52]430 return NULL;
[032e0bb]431
[8b1e15ac]432 function_t *fun = create_isa_fun();
433 if (fun == NULL) {
434 free(fun_name);
[c1a8ae52]435 return NULL;
436 }
[032e0bb]437
[8b1e15ac]438 fun->name = fun_name;
439 fun->ftype = fun_inner;
[032e0bb]440
441 /* Allocate buffer for the list of hardware resources of the device. */
[8b1e15ac]442 child_alloc_hw_res(fun);
[032e0bb]443
444 /* Get properties of the device (match ids, irq and io range). */
445 while (true) {
[8b1e15ac]446 line = str_get_line(fun_conf, &fun_conf);
[032e0bb]447
[5dc9622]448 if (line_empty(line)) {
[032e0bb]449 /* no more device properties */
[5dc9622]450 break;
451 }
[032e0bb]452
453 /*
454 * Get the device's property from the configuration line
455 * and store it in the device structure.
456 */
[8b1e15ac]457 get_fun_prop(fun, line);
[032e0bb]458
[8b1e15ac]459 //printf(NAME ": next line ='%s'\n", fun_conf);
[032e0bb]460 //printf(NAME ": current line ='%s'\n", line);
[c1a8ae52]461 }
[032e0bb]462
463 /* Set device operations to the device. */
[8b1e15ac]464 fun->ops = &isa_fun_dev_ops;
[032e0bb]465
[8b1e15ac]466 printf(NAME ": register_function(fun, dev); function is %s.\n",
467 fun->name);
468 register_function(fun, dev);
[032e0bb]469
[8b1e15ac]470 return fun_conf;
[c1a8ae52]471}
472
[8b1e15ac]473static void parse_fun_conf(char *conf, device_t *dev)
[c1a8ae52]474{
[032e0bb]475 while (conf != NULL && *conf != '\0') {
[8b1e15ac]476 conf = read_isa_fun_info(conf, dev);
[032e0bb]477 }
[c1a8ae52]478}
479
[8b1e15ac]480static void add_legacy_children(device_t *dev)
[c1a8ae52]481{
[8b1e15ac]482 char *fun_conf;
[032e0bb]483
[8b1e15ac]484 fun_conf = read_fun_conf(CHILD_FUN_CONF_PATH);
485 if (fun_conf != NULL) {
486 parse_fun_conf(fun_conf, dev);
487 free(fun_conf);
[c1a8ae52]488 }
489}
[892e4e1]490
[032e0bb]491static int isa_add_device(device_t *dev)
[892e4e1]492{
[ab3a851]493 printf(NAME ": isa_add_device, device handle = %d\n",
494 (int) dev->handle);
[032e0bb]495
[8b1e15ac]496 /* Make the bus device more visible. Does not do anything. */
497 printf(NAME ": adding a 'ctl' function\n");
498
499 function_t *ctl = create_function();
500 ctl->ftype = fun_exposed;
501 ctl->name = "ctl";
502 register_function(ctl, dev);
503
[032e0bb]504 /* Add child devices. */
[c1a8ae52]505 add_legacy_children(dev);
[7e752b2]506 printf(NAME ": finished the enumeration of legacy devices\n");
[032e0bb]507
[df747b9c]508 return EOK;
[892e4e1]509}
510
[5dc9622]511static void isa_init()
512{
[8b1e15ac]513 isa_fun_dev_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
[5dc9622]514}
515
[892e4e1]516int main(int argc, char *argv[])
517{
[032e0bb]518 printf(NAME ": HelenOS ISA bus driver\n");
[5dc9622]519 isa_init();
[892e4e1]520 return driver_main(&isa_driver);
521}
522
523/**
524 * @}
525 */
[ab3a851]526
Note: See TracBrowser for help on using the repository browser.