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

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

isa: Implement DMA remain size query.

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