source: mainline/uspace/drv/bus/isa/isa.c@ 03362fbd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 03362fbd was 03362fbd, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

Merge mainline changes.

Conflict resulting from bool.h → stdbool.h move and ddf structs turning opaque.
Fails to boot to shell console.

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