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

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

UNIX-like I/O functions should use errno to return error code for many reasons.

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