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

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

Standards-compliant boolean type.

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