source: mainline/uspace/drv/bus/isa/isa.c@ 82e4005

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

isa: Rename set_* functions to add_*.

Because that's what those functions do.

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