source: mainline/uspace/drv/bus/isa/isa.c@ 0d4b110

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

Add IO_RANGE and MEM_RANGE members to distinguish whether the range is
parent-relative or absolute. pio_read/write_n(), pio_enable() and
IRQ pseudocode need to be passed absolute values.

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