source: mainline/uspace/drv/bus/isa/isa.c@ 5960b48

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

isa: Implement interrupt enabling.

  • Property mode set to 100644
File size: 13.6 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * Copyright (c) 2011 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 * @defgroup isa ISA bus driver.
32 * @brief HelenOS ISA bus driver.
33 * @{
34 */
35
36/** @file
37 */
38
39#include <adt/list.h>
40#include <assert.h>
41#include <stdio.h>
42#include <errno.h>
43#include <bool.h>
44#include <fibril_synch.h>
45#include <stdlib.h>
46#include <str.h>
47#include <str_error.h>
48#include <ctype.h>
49#include <macros.h>
50#include <malloc.h>
51#include <dirent.h>
52#include <fcntl.h>
53#include <ipc/irc.h>
54#include <ipc/services.h>
55#include <sysinfo.h>
56#include <ns.h>
57#include <sys/stat.h>
58
59#include <ddf/driver.h>
60#include <ddf/log.h>
61#include <ops/hw_res.h>
62
63#include <devman.h>
64#include <ipc/devman.h>
65#include <device/hw_res.h>
66
67#define NAME "isa"
68#define CHILD_FUN_CONF_PATH "/drv/isa/isa.dev"
69
70/** Obtain soft-state from device node */
71#define ISA_BUS(dev) ((isa_bus_t *) ((dev)->driver_data))
72
73/** Obtain soft-state from function node */
74#define ISA_FUN(fun) ((isa_fun_t *) ((fun)->driver_data))
75
76#define ISA_MAX_HW_RES 4
77
78typedef struct {
79 fibril_mutex_t mutex;
80 ddf_dev_t *dev;
81 ddf_fun_t *fctl;
82 list_t functions;
83} isa_bus_t;
84
85typedef struct isa_fun {
86 fibril_mutex_t mutex;
87 ddf_fun_t *fnode;
88 hw_resource_list_t hw_resources;
89 link_t bus_link;
90} isa_fun_t;
91
92static hw_resource_list_t *isa_get_fun_resources(ddf_fun_t *fnode)
93{
94 isa_fun_t *fun = ISA_FUN(fnode);
95 assert(fun != NULL);
96
97 return &fun->hw_resources;
98}
99
100static bool isa_enable_fun_interrupt(ddf_fun_t *fnode)
101{
102 /* This is an old ugly way, copied from pci driver */
103 assert(fnode);
104 isa_fun_t *isa_fun = fnode->driver_data;
105
106 sysarg_t apic;
107 sysarg_t i8259;
108
109 async_sess_t *irc_sess = NULL;
110
111 if (((sysinfo_get_value("apic", &apic) == EOK) && (apic))
112 || ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259))) {
113 irc_sess = service_connect_blocking(EXCHANGE_SERIALIZE,
114 SERVICE_IRC, 0, 0);
115 }
116
117 if (!irc_sess)
118 return false;
119
120 assert(isa_fun);
121 const hw_resource_list_t *res = &isa_fun->hw_resources;
122 assert(res);
123 for (size_t i = 0; i < res->count; ++i) {
124 if (res->resources[i].type == INTERRUPT) {
125 const int irq = res->resources[i].res.interrupt.irq;
126
127 async_exch_t *exch = async_exchange_begin(irc_sess);
128 const int rc =
129 async_req_1_0(exch, IRC_ENABLE_INTERRUPT, irq);
130 async_exchange_end(exch);
131
132 if (rc != EOK) {
133 async_hangup(irc_sess);
134 return false;
135 }
136 }
137 }
138
139 async_hangup(irc_sess);
140 return true;
141}
142
143static hw_res_ops_t isa_fun_hw_res_ops = {
144 &isa_get_fun_resources,
145 &isa_enable_fun_interrupt
146};
147
148static ddf_dev_ops_t isa_fun_ops;
149
150static int isa_dev_add(ddf_dev_t *dev);
151static int isa_dev_remove(ddf_dev_t *dev);
152static int isa_fun_online(ddf_fun_t *fun);
153static int isa_fun_offline(ddf_fun_t *fun);
154
155/** The isa device driver's standard operations */
156static driver_ops_t isa_ops = {
157 .dev_add = &isa_dev_add,
158 .dev_remove = &isa_dev_remove,
159 .fun_online = &isa_fun_online,
160 .fun_offline = &isa_fun_offline
161};
162
163/** The isa device driver structure. */
164static driver_t isa_driver = {
165 .name = NAME,
166 .driver_ops = &isa_ops
167};
168
169static isa_fun_t *isa_fun_create(isa_bus_t *isa, const char *name)
170{
171 ddf_fun_t *fnode = ddf_fun_create(isa->dev, fun_inner, name);
172 if (fnode == NULL)
173 return NULL;
174
175 isa_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(isa_fun_t));
176 if (fun == NULL)
177 return NULL;
178
179 fibril_mutex_initialize(&fun->mutex);
180 fun->fnode = fnode;
181 return fun;
182}
183
184static char *fun_conf_read(const char *conf_path)
185{
186 bool suc = false;
187 char *buf = NULL;
188 bool opened = false;
189 int fd;
190 size_t len = 0;
191
192 fd = open(conf_path, O_RDONLY);
193 if (fd < 0) {
194 ddf_msg(LVL_ERROR, "Unable to open %s", conf_path);
195 goto cleanup;
196 }
197
198 opened = true;
199
200 len = lseek(fd, 0, SEEK_END);
201 lseek(fd, 0, SEEK_SET);
202 if (len == 0) {
203 ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.",
204 conf_path);
205 goto cleanup;
206 }
207
208 buf = malloc(len + 1);
209 if (buf == NULL) {
210 ddf_msg(LVL_ERROR, "Memory allocation failed.");
211 goto cleanup;
212 }
213
214 if (0 >= read(fd, buf, len)) {
215 ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
216 goto cleanup;
217 }
218
219 buf[len] = 0;
220
221 suc = true;
222
223cleanup:
224 if (!suc && buf != NULL) {
225 free(buf);
226 buf = NULL;
227 }
228
229 if (opened)
230 close(fd);
231
232 return buf;
233}
234
235static char *str_get_line(char *str, char **next)
236{
237 char *line = str;
238
239 if (str == NULL) {
240 *next = NULL;
241 return NULL;
242 }
243
244 while (*str != '\0' && *str != '\n') {
245 str++;
246 }
247
248 if (*str != '\0') {
249 *next = str + 1;
250 } else {
251 *next = NULL;
252 }
253
254 *str = '\0';
255 return line;
256}
257
258static bool line_empty(const char *line)
259{
260 while (line != NULL && *line != 0) {
261 if (!isspace(*line))
262 return false;
263 line++;
264 }
265
266 return true;
267}
268
269static char *get_device_name(char *line)
270{
271 /* Skip leading spaces. */
272 while (*line != '\0' && isspace(*line)) {
273 line++;
274 }
275
276 /* Get the name part of the rest of the line. */
277 strtok(line, ":");
278
279 /* Allocate output buffer. */
280 size_t size = str_size(line) + 1;
281 char *name = malloc(size);
282
283 if (name != NULL) {
284 /* Copy the result to the output buffer. */
285 str_cpy(name, size, line);
286 }
287
288 return name;
289}
290
291static inline char *skip_spaces(char *line)
292{
293 /* Skip leading spaces. */
294 while (*line != '\0' && isspace(*line))
295 line++;
296
297 return line;
298}
299
300static void isa_fun_set_irq(isa_fun_t *fun, int irq)
301{
302 size_t count = fun->hw_resources.count;
303 hw_resource_t *resources = fun->hw_resources.resources;
304
305 if (count < ISA_MAX_HW_RES) {
306 resources[count].type = INTERRUPT;
307 resources[count].res.interrupt.irq = irq;
308
309 fun->hw_resources.count++;
310
311 ddf_msg(LVL_NOTE, "Added irq 0x%x to function %s", irq,
312 fun->fnode->name);
313 }
314}
315
316static void isa_fun_set_io_range(isa_fun_t *fun, size_t addr, size_t len)
317{
318 size_t count = fun->hw_resources.count;
319 hw_resource_t *resources = fun->hw_resources.resources;
320
321 if (count < ISA_MAX_HW_RES) {
322 resources[count].type = IO_RANGE;
323 resources[count].res.io_range.address = addr;
324 resources[count].res.io_range.size = len;
325 resources[count].res.io_range.endianness = LITTLE_ENDIAN;
326
327 fun->hw_resources.count++;
328
329 ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
330 "function %s", (unsigned int) addr, (unsigned int) len,
331 fun->fnode->name);
332 }
333}
334
335static void fun_parse_irq(isa_fun_t *fun, char *val)
336{
337 int irq = 0;
338 char *end = NULL;
339
340 val = skip_spaces(val);
341 irq = (int)strtol(val, &end, 10);
342
343 if (val != end)
344 isa_fun_set_irq(fun, irq);
345}
346
347static void fun_parse_io_range(isa_fun_t *fun, char *val)
348{
349 size_t addr, len;
350 char *end = NULL;
351
352 val = skip_spaces(val);
353 addr = strtol(val, &end, 0x10);
354
355 if (val == end)
356 return;
357
358 val = skip_spaces(end);
359 len = strtol(val, &end, 0x10);
360
361 if (val == end)
362 return;
363
364 isa_fun_set_io_range(fun, addr, len);
365}
366
367static void get_match_id(char **id, char *val)
368{
369 char *end = val;
370
371 while (!isspace(*end))
372 end++;
373
374 size_t size = end - val + 1;
375 *id = (char *)malloc(size);
376 str_cpy(*id, size, val);
377}
378
379static void fun_parse_match_id(isa_fun_t *fun, char *val)
380{
381 char *id = NULL;
382 int score = 0;
383 char *end = NULL;
384 int rc;
385
386 val = skip_spaces(val);
387
388 score = (int)strtol(val, &end, 10);
389 if (val == end) {
390 ddf_msg(LVL_ERROR, "Cannot read match score for function "
391 "%s.", fun->fnode->name);
392 return;
393 }
394
395 val = skip_spaces(end);
396 get_match_id(&id, val);
397 if (id == NULL) {
398 ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
399 fun->fnode->name);
400 return;
401 }
402
403 ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
404 "function %s", id, score, fun->fnode->name);
405
406 rc = ddf_fun_add_match_id(fun->fnode, id, score);
407 if (rc != EOK) {
408 ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
409 str_error(rc));
410 }
411
412 free(id);
413}
414
415static bool prop_parse(isa_fun_t *fun, char *line, const char *prop,
416 void (*read_fn)(isa_fun_t *, char *))
417{
418 size_t proplen = str_size(prop);
419
420 if (str_lcmp(line, prop, proplen) == 0) {
421 line += proplen;
422 line = skip_spaces(line);
423 (*read_fn)(fun, line);
424
425 return true;
426 }
427
428 return false;
429}
430
431static void fun_prop_parse(isa_fun_t *fun, char *line)
432{
433 /* Skip leading spaces. */
434 line = skip_spaces(line);
435
436 if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
437 !prop_parse(fun, line, "irq", &fun_parse_irq) &&
438 !prop_parse(fun, line, "match", &fun_parse_match_id)) {
439
440 ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
441 line);
442 }
443}
444
445static void fun_hw_res_alloc(isa_fun_t *fun)
446{
447 fun->hw_resources.resources =
448 (hw_resource_t *)malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
449}
450
451static void fun_hw_res_free(isa_fun_t *fun)
452{
453 free(fun->hw_resources.resources);
454 fun->hw_resources.resources = NULL;
455}
456
457static char *isa_fun_read_info(char *fun_conf, isa_bus_t *isa)
458{
459 char *line;
460 char *fun_name = NULL;
461
462 /* Skip empty lines. */
463 while (true) {
464 line = str_get_line(fun_conf, &fun_conf);
465
466 if (line == NULL) {
467 /* no more lines */
468 return NULL;
469 }
470
471 if (!line_empty(line))
472 break;
473 }
474
475 /* Get device name. */
476 fun_name = get_device_name(line);
477 if (fun_name == NULL)
478 return NULL;
479
480 isa_fun_t *fun = isa_fun_create(isa, fun_name);
481 if (fun == NULL) {
482 free(fun_name);
483 return NULL;
484 }
485
486 /* Allocate buffer for the list of hardware resources of the device. */
487 fun_hw_res_alloc(fun);
488
489 /* Get properties of the device (match ids, irq and io range). */
490 while (true) {
491 line = str_get_line(fun_conf, &fun_conf);
492
493 if (line_empty(line)) {
494 /* no more device properties */
495 break;
496 }
497
498 /*
499 * Get the device's property from the configuration line
500 * and store it in the device structure.
501 */
502 fun_prop_parse(fun, line);
503 }
504
505 /* Set device operations to the device. */
506 fun->fnode->ops = &isa_fun_ops;
507
508 ddf_msg(LVL_DEBUG, "Binding function %s.", fun->fnode->name);
509
510 /* XXX Handle error */
511 (void) ddf_fun_bind(fun->fnode);
512
513 list_append(&fun->bus_link, &isa->functions);
514
515 return fun_conf;
516}
517
518static void fun_conf_parse(char *conf, isa_bus_t *isa)
519{
520 while (conf != NULL && *conf != '\0') {
521 conf = isa_fun_read_info(conf, isa);
522 }
523}
524
525static void isa_functions_add(isa_bus_t *isa)
526{
527 char *fun_conf;
528
529 fun_conf = fun_conf_read(CHILD_FUN_CONF_PATH);
530 if (fun_conf != NULL) {
531 fun_conf_parse(fun_conf, isa);
532 free(fun_conf);
533 }
534}
535
536static int isa_dev_add(ddf_dev_t *dev)
537{
538 isa_bus_t *isa;
539
540 ddf_msg(LVL_DEBUG, "isa_dev_add, device handle = %d",
541 (int) dev->handle);
542
543 isa = ddf_dev_data_alloc(dev, sizeof(isa_bus_t));
544 if (isa == NULL)
545 return ENOMEM;
546
547 fibril_mutex_initialize(&isa->mutex);
548 isa->dev = dev;
549 list_initialize(&isa->functions);
550
551 /* Make the bus device more visible. Does not do anything. */
552 ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
553
554 fibril_mutex_lock(&isa->mutex);
555
556 isa->fctl = ddf_fun_create(dev, fun_exposed, "ctl");
557 if (isa->fctl == NULL) {
558 ddf_msg(LVL_ERROR, "Failed creating control function.");
559 return EXDEV;
560 }
561
562 if (ddf_fun_bind(isa->fctl) != EOK) {
563 ddf_fun_destroy(isa->fctl);
564 ddf_msg(LVL_ERROR, "Failed binding control function.");
565 return EXDEV;
566 }
567
568 /* Add functions as specified in the configuration file. */
569 isa_functions_add(isa);
570 ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
571
572 fibril_mutex_unlock(&isa->mutex);
573
574 return EOK;
575}
576
577static int isa_dev_remove(ddf_dev_t *dev)
578{
579 isa_bus_t *isa = ISA_BUS(dev);
580 int rc;
581
582 fibril_mutex_lock(&isa->mutex);
583
584 while (!list_empty(&isa->functions)) {
585 isa_fun_t *fun = list_get_instance(list_first(&isa->functions),
586 isa_fun_t, bus_link);
587
588 rc = ddf_fun_offline(fun->fnode);
589 if (rc != EOK) {
590 fibril_mutex_unlock(&isa->mutex);
591 ddf_msg(LVL_ERROR, "Failed offlining %s", fun->fnode->name);
592 return rc;
593 }
594
595 rc = ddf_fun_unbind(fun->fnode);
596 if (rc != EOK) {
597 fibril_mutex_unlock(&isa->mutex);
598 ddf_msg(LVL_ERROR, "Failed unbinding %s", fun->fnode->name);
599 return rc;
600 }
601
602 list_remove(&fun->bus_link);
603
604 fun_hw_res_free(fun);
605 ddf_fun_destroy(fun->fnode);
606 }
607
608 if (ddf_fun_unbind(isa->fctl) != EOK) {
609 fibril_mutex_unlock(&isa->mutex);
610 ddf_msg(LVL_ERROR, "Failed unbinding control function.");
611 return EXDEV;
612 }
613
614 fibril_mutex_unlock(&isa->mutex);
615
616 return EOK;
617}
618
619static int isa_fun_online(ddf_fun_t *fun)
620{
621 ddf_msg(LVL_DEBUG, "isa_fun_online()");
622 return ddf_fun_online(fun);
623}
624
625static int isa_fun_offline(ddf_fun_t *fun)
626{
627 ddf_msg(LVL_DEBUG, "isa_fun_offline()");
628 return ddf_fun_offline(fun);
629}
630
631
632static void isa_init()
633{
634 ddf_log_init(NAME, LVL_ERROR);
635 isa_fun_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
636}
637
638int main(int argc, char *argv[])
639{
640 printf(NAME ": HelenOS ISA bus driver\n");
641 isa_init();
642 return ddf_driver_main(&isa_driver);
643}
644
645/**
646 * @}
647 */
Note: See TracBrowser for help on using the repository browser.