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

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

isa: Inline isa_init and use structure initializer.

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