source: mainline/uspace/drv/bus/isa/isa.c@ 295f658

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 295f658 was d9cf684a, checked in by Martin Decky <martin@…>, 14 years ago

cherrypick and adopt Intel 8237A DMA controller support from bzr://krabicka.net/orome/helenos/audio/

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