source: mainline/uspace/drv/bus/isa/isa.c@ 7d364fb8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7d364fb8 was 818fffe, checked in by Martin Decky <martin@…>, 14 years ago

cstyle

  • Property mode set to 100644
File size: 12.6 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * Copyright (c) 2011 Jiri Svoboda
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
39#include <adt/list.h>
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>
46#include <str.h>
47#include <str_error.h>
48#include <ctype.h>
49#include <macros.h>
50#include <malloc.h>
51#include <dirent.h>
52#include <fcntl.h>
53#include <sys/stat.h>
54
55#include <ddf/driver.h>
56#include <ddf/log.h>
57#include <ops/hw_res.h>
58
59#include <devman.h>
60#include <ipc/devman.h>
61#include <device/hw_res.h>
62
63#define NAME "isa"
64#define CHILD_FUN_CONF_PATH "/drv/isa/isa.dev"
65
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))
71
72#define ISA_MAX_HW_RES 4
73
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
81typedef struct isa_fun {
82 fibril_mutex_t mutex;
83 ddf_fun_t *fnode;
84 hw_resource_list_t hw_resources;
85 link_t bus_link;
86} isa_fun_t;
87
88static hw_resource_list_t *isa_get_fun_resources(ddf_fun_t *fnode)
89{
90 isa_fun_t *fun = ISA_FUN(fnode);
91 assert(fun != NULL);
92
93 return &fun->hw_resources;
94}
95
96static bool isa_enable_fun_interrupt(ddf_fun_t *fnode)
97{
98 /* TODO */
99
100 return false;
101}
102
103static hw_res_ops_t isa_fun_hw_res_ops = {
104 &isa_get_fun_resources,
105 &isa_enable_fun_interrupt
106};
107
108static ddf_dev_ops_t isa_fun_ops;
109
110static int isa_add_device(ddf_dev_t *dev);
111static int isa_dev_remove(ddf_dev_t *dev);
112static int isa_fun_online(ddf_fun_t *fun);
113static int isa_fun_offline(ddf_fun_t *fun);
114
115/** The isa device driver's standard operations */
116static driver_ops_t isa_ops = {
117 .add_device = &isa_add_device,
118 .dev_remove = &isa_dev_remove,
119 .fun_online = &isa_fun_online,
120 .fun_offline = &isa_fun_offline
121};
122
123/** The isa device driver structure. */
124static driver_t isa_driver = {
125 .name = NAME,
126 .driver_ops = &isa_ops
127};
128
129static isa_fun_t *isa_fun_create(isa_bus_t *isa, const char *name)
130{
131 ddf_fun_t *fnode = ddf_fun_create(isa->dev, fun_inner, name);
132 if (fnode == NULL)
133 return NULL;
134
135 isa_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(isa_fun_t));
136 if (fun == NULL)
137 return NULL;
138
139 fibril_mutex_initialize(&fun->mutex);
140 fun->fnode = fnode;
141 return fun;
142}
143
144static char *fun_conf_read(const char *conf_path)
145{
146 bool suc = false;
147 char *buf = NULL;
148 bool opened = false;
149 int fd;
150 size_t len = 0;
151
152 fd = open(conf_path, O_RDONLY);
153 if (fd < 0) {
154 ddf_msg(LVL_ERROR, "Unable to open %s", conf_path);
155 goto cleanup;
156 }
157
158 opened = true;
159
160 len = lseek(fd, 0, SEEK_END);
161 lseek(fd, 0, SEEK_SET);
162 if (len == 0) {
163 ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.",
164 conf_path);
165 goto cleanup;
166 }
167
168 buf = malloc(len + 1);
169 if (buf == NULL) {
170 ddf_msg(LVL_ERROR, "Memory allocation failed.");
171 goto cleanup;
172 }
173
174 if (0 >= read(fd, buf, len)) {
175 ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
176 goto cleanup;
177 }
178
179 buf[len] = 0;
180
181 suc = true;
182
183cleanup:
184 if (!suc && buf != NULL) {
185 free(buf);
186 buf = NULL;
187 }
188
189 if (opened)
190 close(fd);
191
192 return buf;
193}
194
195static char *str_get_line(char *str, char **next)
196{
197 char *line = str;
198
199 if (str == NULL) {
200 *next = NULL;
201 return NULL;
202 }
203
204 while (*str != '\0' && *str != '\n') {
205 str++;
206 }
207
208 if (*str != '\0') {
209 *next = str + 1;
210 } else {
211 *next = NULL;
212 }
213
214 *str = '\0';
215 return line;
216}
217
218static bool line_empty(const char *line)
219{
220 while (line != NULL && *line != 0) {
221 if (!isspace(*line))
222 return false;
223 line++;
224 }
225
226 return true;
227}
228
229static char *get_device_name(char *line)
230{
231 /* Skip leading spaces. */
232 while (*line != '\0' && isspace(*line)) {
233 line++;
234 }
235
236 /* Get the name part of the rest of the line. */
237 strtok(line, ":");
238
239 /* Allocate output buffer. */
240 size_t size = str_size(line) + 1;
241 char *name = malloc(size);
242
243 if (name != NULL) {
244 /* Copy the result to the output buffer. */
245 str_cpy(name, size, line);
246 }
247
248 return name;
249}
250
251static inline char *skip_spaces(char *line)
252{
253 /* Skip leading spaces. */
254 while (*line != '\0' && isspace(*line))
255 line++;
256
257 return line;
258}
259
260static void isa_fun_set_irq(isa_fun_t *fun, int irq)
261{
262 size_t count = fun->hw_resources.count;
263 hw_resource_t *resources = fun->hw_resources.resources;
264
265 if (count < ISA_MAX_HW_RES) {
266 resources[count].type = INTERRUPT;
267 resources[count].res.interrupt.irq = irq;
268
269 fun->hw_resources.count++;
270
271 ddf_msg(LVL_NOTE, "Added irq 0x%x to function %s", irq,
272 fun->fnode->name);
273 }
274}
275
276static void isa_fun_set_io_range(isa_fun_t *fun, size_t addr, size_t len)
277{
278 size_t count = fun->hw_resources.count;
279 hw_resource_t *resources = fun->hw_resources.resources;
280
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;
285 resources[count].res.io_range.endianness = LITTLE_ENDIAN;
286
287 fun->hw_resources.count++;
288
289 ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
290 "function %s", (unsigned int) addr, (unsigned int) len,
291 fun->fnode->name);
292 }
293}
294
295static void fun_parse_irq(isa_fun_t *fun, char *val)
296{
297 int irq = 0;
298 char *end = NULL;
299
300 val = skip_spaces(val);
301 irq = (int)strtol(val, &end, 0x10);
302
303 if (val != end)
304 isa_fun_set_irq(fun, irq);
305}
306
307static void fun_parse_io_range(isa_fun_t *fun, char *val)
308{
309 size_t addr, len;
310 char *end = NULL;
311
312 val = skip_spaces(val);
313 addr = strtol(val, &end, 0x10);
314
315 if (val == end)
316 return;
317
318 val = skip_spaces(end);
319 len = strtol(val, &end, 0x10);
320
321 if (val == end)
322 return;
323
324 isa_fun_set_io_range(fun, addr, len);
325}
326
327static void get_match_id(char **id, char *val)
328{
329 char *end = val;
330
331 while (!isspace(*end))
332 end++;
333
334 size_t size = end - val + 1;
335 *id = (char *)malloc(size);
336 str_cpy(*id, size, val);
337}
338
339static void fun_parse_match_id(isa_fun_t *fun, char *val)
340{
341 char *id = NULL;
342 int score = 0;
343 char *end = NULL;
344 int rc;
345
346 val = skip_spaces(val);
347
348 score = (int)strtol(val, &end, 10);
349 if (val == end) {
350 ddf_msg(LVL_ERROR, "Cannot read match score for function "
351 "%s.", fun->fnode->name);
352 return;
353 }
354
355 val = skip_spaces(end);
356 get_match_id(&id, val);
357 if (id == NULL) {
358 ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
359 fun->fnode->name);
360 return;
361 }
362
363 ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
364 "function %s", id, score, fun->fnode->name);
365
366 rc = ddf_fun_add_match_id(fun->fnode, id, score);
367 if (rc != EOK) {
368 ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
369 str_error(rc));
370 }
371
372 free(id);
373}
374
375static bool prop_parse(isa_fun_t *fun, char *line, const char *prop,
376 void (*read_fn)(isa_fun_t *, char *))
377{
378 size_t proplen = str_size(prop);
379
380 if (str_lcmp(line, prop, proplen) == 0) {
381 line += proplen;
382 line = skip_spaces(line);
383 (*read_fn)(fun, line);
384
385 return true;
386 }
387
388 return false;
389}
390
391static void fun_prop_parse(isa_fun_t *fun, char *line)
392{
393 /* Skip leading spaces. */
394 line = skip_spaces(line);
395
396 if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
397 !prop_parse(fun, line, "irq", &fun_parse_irq) &&
398 !prop_parse(fun, line, "match", &fun_parse_match_id)) {
399
400 ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
401 line);
402 }
403}
404
405static void fun_hw_res_alloc(isa_fun_t *fun)
406{
407 fun->hw_resources.resources =
408 (hw_resource_t *)malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
409}
410
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)
418{
419 char *line;
420 char *fun_name = NULL;
421
422 /* Skip empty lines. */
423 while (true) {
424 line = str_get_line(fun_conf, &fun_conf);
425
426 if (line == NULL) {
427 /* no more lines */
428 return NULL;
429 }
430
431 if (!line_empty(line))
432 break;
433 }
434
435 /* Get device name. */
436 fun_name = get_device_name(line);
437 if (fun_name == NULL)
438 return NULL;
439
440 isa_fun_t *fun = isa_fun_create(isa, fun_name);
441 if (fun == NULL) {
442 free(fun_name);
443 return NULL;
444 }
445
446 /* Allocate buffer for the list of hardware resources of the device. */
447 fun_hw_res_alloc(fun);
448
449 /* Get properties of the device (match ids, irq and io range). */
450 while (true) {
451 line = str_get_line(fun_conf, &fun_conf);
452
453 if (line_empty(line)) {
454 /* no more device properties */
455 break;
456 }
457
458 /*
459 * Get the device's property from the configuration line
460 * and store it in the device structure.
461 */
462 fun_prop_parse(fun, line);
463 }
464
465 /* Set device operations to the device. */
466 fun->fnode->ops = &isa_fun_ops;
467
468 ddf_msg(LVL_DEBUG, "Binding function %s.", fun->fnode->name);
469
470 /* XXX Handle error */
471 (void) ddf_fun_bind(fun->fnode);
472
473 list_append(&fun->bus_link, &isa->functions);
474
475 return fun_conf;
476}
477
478static void fun_conf_parse(char *conf, isa_bus_t *isa)
479{
480 while (conf != NULL && *conf != '\0') {
481 conf = isa_fun_read_info(conf, isa);
482 }
483}
484
485static void isa_functions_add(isa_bus_t *isa)
486{
487 char *fun_conf;
488
489 fun_conf = fun_conf_read(CHILD_FUN_CONF_PATH);
490 if (fun_conf != NULL) {
491 fun_conf_parse(fun_conf, isa);
492 free(fun_conf);
493 }
494}
495
496static int isa_add_device(ddf_dev_t *dev)
497{
498 isa_bus_t *isa;
499
500 ddf_msg(LVL_DEBUG, "isa_add_device, device handle = %d",
501 (int) dev->handle);
502
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
511 /* Make the bus device more visible. Does not do anything. */
512 ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
513
514 fibril_mutex_lock(&isa->mutex);
515
516 isa->fctl = ddf_fun_create(dev, fun_exposed, "ctl");
517 if (isa->fctl == NULL) {
518 ddf_msg(LVL_ERROR, "Failed creating control function.");
519 return EXDEV;
520 }
521
522 if (ddf_fun_bind(isa->fctl) != EOK) {
523 ddf_fun_destroy(isa->fctl);
524 ddf_msg(LVL_ERROR, "Failed binding control function.");
525 return EXDEV;
526 }
527
528 /* Add functions as specified in the configuration file. */
529 isa_functions_add(isa);
530 ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
531
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
576 return EOK;
577}
578
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
592static void isa_init()
593{
594 ddf_log_init(NAME, LVL_ERROR);
595 isa_fun_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
596}
597
598int main(int argc, char *argv[])
599{
600 printf(NAME ": HelenOS ISA bus driver\n");
601 isa_init();
602 return ddf_driver_main(&isa_driver);
603}
604
605/**
606 * @}
607 */
Note: See TracBrowser for help on using the repository browser.