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

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

isa: Add DMA controller driver implementation.

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