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

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

Mainline changes.

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