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

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

isa: Rename some functions.

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