source: mainline/uspace/drv/bus/isa/isa.c@ 85c4cc45

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

isa: One less malloc. Fix name memory leak.

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