source: mainline/uspace/drv/bus/isa/isa.c@ 01784d2

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

Other clients should also use the IRC client stub.

  • 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 = 0;
248
249 fd = open(conf_path, O_RDONLY);
250 if (fd < 0) {
251 ddf_msg(LVL_ERROR, "Unable to open %s", conf_path);
252 goto cleanup;
253 }
254
255 opened = true;
256
257 len = lseek(fd, 0, SEEK_END);
258 lseek(fd, 0, SEEK_SET);
259 if (len == 0) {
260 ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.",
261 conf_path);
262 goto cleanup;
263 }
264
265 buf = malloc(len + 1);
266 if (buf == NULL) {
267 ddf_msg(LVL_ERROR, "Memory allocation failed.");
268 goto cleanup;
269 }
270
271 if (0 >= read(fd, buf, len)) {
272 ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
273 goto cleanup;
274 }
275
276 buf[len] = 0;
277
278 suc = true;
279
280cleanup:
281 if (!suc && buf != NULL) {
282 free(buf);
283 buf = NULL;
284 }
285
286 if (opened)
287 close(fd);
288
289 return buf;
290}
291
292static char *str_get_line(char *str, char **next)
293{
294 char *line = str;
295 *next = NULL;
296
297 if (str == NULL) {
298 return NULL;
299 }
300
301 while (*str != '\0' && *str != '\n') {
302 str++;
303 }
304
305 if (*str != '\0') {
306 *next = str + 1;
307 }
308
309 *str = '\0';
310 return line;
311}
312
313static bool line_empty(const char *line)
314{
315 while (line != NULL && *line != 0) {
316 if (!isspace(*line))
317 return false;
318 line++;
319 }
320
321 return true;
322}
323
324static char *get_device_name(char *line)
325{
326 /* Skip leading spaces. */
327 while (*line != '\0' && isspace(*line)) {
328 line++;
329 }
330
331 /* Get the name part of the rest of the line. */
332 str_tok(line, ":", NULL);
333 return line;
334}
335
336static inline const char *skip_spaces(const char *line)
337{
338 /* Skip leading spaces. */
339 while (*line != '\0' && isspace(*line))
340 line++;
341
342 return line;
343}
344
345static void isa_fun_add_irq(isa_fun_t *fun, int irq)
346{
347 size_t count = fun->hw_resources.count;
348 hw_resource_t *resources = fun->hw_resources.resources;
349
350 if (count < ISA_MAX_HW_RES) {
351 resources[count].type = INTERRUPT;
352 resources[count].res.interrupt.irq = irq;
353
354 fun->hw_resources.count++;
355
356 ddf_msg(LVL_NOTE, "Added irq 0x%x to function %s", irq,
357 ddf_fun_get_name(fun->fnode));
358 }
359}
360
361static void isa_fun_add_dma(isa_fun_t *fun, int dma)
362{
363 size_t count = fun->hw_resources.count;
364 hw_resource_t *resources = fun->hw_resources.resources;
365
366 if (count < ISA_MAX_HW_RES) {
367 if ((dma > 0) && (dma < 4)) {
368 resources[count].type = DMA_CHANNEL_8;
369 resources[count].res.dma_channel.dma8 = 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 if ((dma > 4) && (dma < 8)) {
379 resources[count].type = DMA_CHANNEL_16;
380 resources[count].res.dma_channel.dma16 = dma;
381
382 fun->hw_resources.count++;
383 ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
384 ddf_fun_get_name(fun->fnode));
385
386 return;
387 }
388
389 ddf_msg(LVL_WARN, "Skipped dma 0x%x for function %s", dma,
390 ddf_fun_get_name(fun->fnode));
391 }
392}
393
394static void isa_fun_add_io_range(isa_fun_t *fun, size_t addr, size_t len)
395{
396 size_t count = fun->hw_resources.count;
397 hw_resource_t *resources = fun->hw_resources.resources;
398
399 isa_bus_t *isa = isa_bus(ddf_fun_get_dev(fun->fnode));
400
401 if (count < ISA_MAX_HW_RES) {
402 resources[count].type = IO_RANGE;
403 resources[count].res.io_range.address = addr;
404 resources[count].res.io_range.address += isa->pio_win.io.base;
405 resources[count].res.io_range.size = len;
406 resources[count].res.io_range.relative = false;
407 resources[count].res.io_range.endianness = LITTLE_ENDIAN;
408
409 fun->hw_resources.count++;
410
411 ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
412 "function %s", (unsigned int) addr, (unsigned int) len,
413 ddf_fun_get_name(fun->fnode));
414 }
415}
416
417static void fun_parse_irq(isa_fun_t *fun, const char *val)
418{
419 int irq = 0;
420 char *end = NULL;
421
422 val = skip_spaces(val);
423 irq = (int) strtol(val, &end, 10);
424
425 if (val != end)
426 isa_fun_add_irq(fun, irq);
427}
428
429static void fun_parse_dma(isa_fun_t *fun, const char *val)
430{
431 char *end = NULL;
432
433 val = skip_spaces(val);
434 const int dma = strtol(val, &end, 10);
435
436 if (val != end)
437 isa_fun_add_dma(fun, dma);
438}
439
440static void fun_parse_io_range(isa_fun_t *fun, const char *val)
441{
442 size_t addr, len;
443 char *end = NULL;
444
445 val = skip_spaces(val);
446 addr = strtol(val, &end, 0x10);
447
448 if (val == end)
449 return;
450
451 val = skip_spaces(end);
452 len = strtol(val, &end, 0x10);
453
454 if (val == end)
455 return;
456
457 isa_fun_add_io_range(fun, addr, len);
458}
459
460static void get_match_id(char **id, const char *val)
461{
462 const char *end = val;
463
464 while (!isspace(*end))
465 end++;
466
467 size_t size = end - val + 1;
468 *id = (char *)malloc(size);
469 str_cpy(*id, size, val);
470}
471
472static void fun_parse_match_id(isa_fun_t *fun, const char *val)
473{
474 char *id = NULL;
475 char *end = NULL;
476
477 val = skip_spaces(val);
478
479 int score = (int)strtol(val, &end, 10);
480 if (val == end) {
481 ddf_msg(LVL_ERROR, "Cannot read match score for function "
482 "%s.", ddf_fun_get_name(fun->fnode));
483 return;
484 }
485
486 val = skip_spaces(end);
487 get_match_id(&id, val);
488 if (id == NULL) {
489 ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
490 ddf_fun_get_name(fun->fnode));
491 return;
492 }
493
494 ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
495 "function %s", id, score, ddf_fun_get_name(fun->fnode));
496
497 int rc = ddf_fun_add_match_id(fun->fnode, id, score);
498 if (rc != EOK) {
499 ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
500 str_error(rc));
501 }
502
503 free(id);
504}
505
506static bool prop_parse(isa_fun_t *fun, const char *line, const char *prop,
507 void (*read_fn)(isa_fun_t *, const char *))
508{
509 size_t proplen = str_size(prop);
510
511 if (str_lcmp(line, prop, proplen) == 0) {
512 line += proplen;
513 line = skip_spaces(line);
514 (*read_fn)(fun, line);
515
516 return true;
517 }
518
519 return false;
520}
521
522static void fun_prop_parse(isa_fun_t *fun, const char *line)
523{
524 /* Skip leading spaces. */
525 line = skip_spaces(line);
526
527 if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
528 !prop_parse(fun, line, "irq", &fun_parse_irq) &&
529 !prop_parse(fun, line, "dma", &fun_parse_dma) &&
530 !prop_parse(fun, line, "match", &fun_parse_match_id)) {
531
532 ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
533 line);
534 }
535}
536
537static char *isa_fun_read_info(char *fun_conf, isa_bus_t *isa)
538{
539 char *line;
540
541 /* Skip empty lines. */
542 do {
543 line = str_get_line(fun_conf, &fun_conf);
544
545 if (line == NULL) {
546 /* no more lines */
547 return NULL;
548 }
549
550 } while (line_empty(line));
551
552 /* Get device name. */
553 const char *fun_name = get_device_name(line);
554 if (fun_name == NULL)
555 return NULL;
556
557 isa_fun_t *fun = isa_fun_create(isa, fun_name);
558 if (fun == NULL) {
559 return NULL;
560 }
561
562 /* Get properties of the device (match ids, irq and io range). */
563 while (true) {
564 line = str_get_line(fun_conf, &fun_conf);
565
566 if (line_empty(line)) {
567 /* no more device properties */
568 break;
569 }
570
571 /*
572 * Get the device's property from the configuration line
573 * and store it in the device structure.
574 */
575 fun_prop_parse(fun, line);
576 }
577
578 /* Set device operations to the device. */
579 ddf_fun_set_ops(fun->fnode, &isa_fun_ops);
580
581 ddf_msg(LVL_DEBUG, "Binding function %s.", ddf_fun_get_name(fun->fnode));
582
583 /* XXX Handle error */
584 (void) ddf_fun_bind(fun->fnode);
585
586 list_append(&fun->bus_link, &isa->functions);
587
588 return fun_conf;
589}
590
591static void isa_functions_add(isa_bus_t *isa)
592{
593 char *conf = fun_conf_read(CHILD_FUN_CONF_PATH);
594 while (conf != NULL && *conf != '\0') {
595 conf = isa_fun_read_info(conf, isa);
596 }
597 free(conf);
598}
599
600static int isa_dev_add(ddf_dev_t *dev)
601{
602 async_sess_t *sess;
603 int rc;
604
605 ddf_msg(LVL_DEBUG, "isa_dev_add, device handle = %d",
606 (int) ddf_dev_get_handle(dev));
607
608 isa_bus_t *isa = ddf_dev_data_alloc(dev, sizeof(isa_bus_t));
609 if (isa == NULL)
610 return ENOMEM;
611
612 fibril_mutex_initialize(&isa->mutex);
613 isa->dev = dev;
614 list_initialize(&isa->functions);
615
616 sess = ddf_dev_parent_sess_create(dev, EXCHANGE_SERIALIZE);
617 if (sess == NULL) {
618 ddf_msg(LVL_ERROR, "isa_dev_add failed to connect to the "
619 "parent driver.");
620 return ENOENT;
621 }
622
623 rc = pio_window_get(sess, &isa->pio_win);
624 if (rc != EOK) {
625 ddf_msg(LVL_ERROR, "isa_dev_add failed to get PIO window "
626 "for the device.");
627 return rc;
628 }
629
630 /* Make the bus device more visible. Does not do anything. */
631 ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
632
633 fibril_mutex_lock(&isa->mutex);
634
635 isa->fctl = ddf_fun_create(dev, fun_exposed, "ctl");
636 if (isa->fctl == NULL) {
637 ddf_msg(LVL_ERROR, "Failed creating control function.");
638 return EXDEV;
639 }
640
641 if (ddf_fun_bind(isa->fctl) != EOK) {
642 ddf_fun_destroy(isa->fctl);
643 ddf_msg(LVL_ERROR, "Failed binding control function.");
644 return EXDEV;
645 }
646
647 /* Add functions as specified in the configuration file. */
648 isa_functions_add(isa);
649 ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
650
651 fibril_mutex_unlock(&isa->mutex);
652
653 return EOK;
654}
655
656static int isa_dev_remove(ddf_dev_t *dev)
657{
658 isa_bus_t *isa = isa_bus(dev);
659
660 fibril_mutex_lock(&isa->mutex);
661
662 while (!list_empty(&isa->functions)) {
663 isa_fun_t *fun = list_get_instance(list_first(&isa->functions),
664 isa_fun_t, bus_link);
665
666 int rc = ddf_fun_offline(fun->fnode);
667 if (rc != EOK) {
668 fibril_mutex_unlock(&isa->mutex);
669 ddf_msg(LVL_ERROR, "Failed offlining %s", ddf_fun_get_name(fun->fnode));
670 return rc;
671 }
672
673 rc = ddf_fun_unbind(fun->fnode);
674 if (rc != EOK) {
675 fibril_mutex_unlock(&isa->mutex);
676 ddf_msg(LVL_ERROR, "Failed unbinding %s", ddf_fun_get_name(fun->fnode));
677 return rc;
678 }
679
680 list_remove(&fun->bus_link);
681
682 ddf_fun_destroy(fun->fnode);
683 }
684
685 if (ddf_fun_unbind(isa->fctl) != EOK) {
686 fibril_mutex_unlock(&isa->mutex);
687 ddf_msg(LVL_ERROR, "Failed unbinding control function.");
688 return EXDEV;
689 }
690
691 fibril_mutex_unlock(&isa->mutex);
692
693 return EOK;
694}
695
696static int isa_fun_online(ddf_fun_t *fun)
697{
698 ddf_msg(LVL_DEBUG, "isa_fun_online()");
699 return ddf_fun_online(fun);
700}
701
702static int isa_fun_offline(ddf_fun_t *fun)
703{
704 ddf_msg(LVL_DEBUG, "isa_fun_offline()");
705 return ddf_fun_offline(fun);
706}
707
708int main(int argc, char *argv[])
709{
710 printf(NAME ": HelenOS ISA bus driver\n");
711 ddf_log_init(NAME);
712 return ddf_driver_main(&isa_driver);
713}
714
715/**
716 * @}
717 */
Note: See TracBrowser for help on using the repository browser.