source: mainline/uspace/drv/bus/isa/isa.c@ 55a8e0cb

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

isa: Add support for DMA channels.

  • Property mode set to 100644
File size: 14.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 <sys/stat.h>
54#include <ipc/irc.h>
55#include <ipc/services.h>
56#include <sysinfo.h>
57#include <ns.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 5
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 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_add_device(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 .add_device = &isa_add_device,
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_dma(isa_fun_t *fun, int dma)
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 if (dma > 0 && dma < 4) {
323 resources[count].type = DMA_CHANNEL_8;
324 resources[count].res.dma_channel.dma8 = dma;
325
326 fun->hw_resources.count++;
327 ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
328 fun->fnode->name);
329 return;
330 }
331
332 if (dma > 4 && dma < 8) {
333 resources[count].type = DMA_CHANNEL_16;
334 resources[count].res.dma_channel.dma16 = dma;
335
336 fun->hw_resources.count++;
337 ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
338 fun->fnode->name);
339 return;
340 }
341
342 ddf_msg(LVL_WARN, "Skipped dma 0x%x for function %s", dma,
343 fun->fnode->name);
344 }
345}
346
347static void isa_fun_set_io_range(isa_fun_t *fun, size_t addr, size_t len)
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 = IO_RANGE;
354 resources[count].res.io_range.address = addr;
355 resources[count].res.io_range.size = len;
356 resources[count].res.io_range.endianness = LITTLE_ENDIAN;
357
358 fun->hw_resources.count++;
359
360 ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
361 "function %s", (unsigned int) addr, (unsigned int) len,
362 fun->fnode->name);
363 }
364}
365
366static void fun_parse_irq(isa_fun_t *fun, char *val)
367{
368 int irq = 0;
369 char *end = NULL;
370
371 val = skip_spaces(val);
372 irq = (int)strtol(val, &end, 0x10);
373
374 if (val != end)
375 isa_fun_set_irq(fun, irq);
376}
377
378static void fun_parse_dma(isa_fun_t *fun, char *val)
379{
380 int dma = 0;
381 char *end = NULL;
382
383 val = skip_spaces(val);
384 dma = (int)strtol(val, &end, 10);
385
386 if (val != end)
387 isa_fun_set_dma(fun, dma);
388}
389
390static void fun_parse_io_range(isa_fun_t *fun, char *val)
391{
392 size_t addr, len;
393 char *end = NULL;
394
395 val = skip_spaces(val);
396 addr = strtol(val, &end, 0x10);
397
398 if (val == end)
399 return;
400
401 val = skip_spaces(end);
402 len = strtol(val, &end, 0x10);
403
404 if (val == end)
405 return;
406
407 isa_fun_set_io_range(fun, addr, len);
408}
409
410static void get_match_id(char **id, char *val)
411{
412 char *end = val;
413
414 while (!isspace(*end))
415 end++;
416
417 size_t size = end - val + 1;
418 *id = (char *)malloc(size);
419 str_cpy(*id, size, val);
420}
421
422static void fun_parse_match_id(isa_fun_t *fun, char *val)
423{
424 char *id = NULL;
425 int score = 0;
426 char *end = NULL;
427 int rc;
428
429 val = skip_spaces(val);
430
431 score = (int)strtol(val, &end, 10);
432 if (val == end) {
433 ddf_msg(LVL_ERROR, "Cannot read match score for function "
434 "%s.", fun->fnode->name);
435 return;
436 }
437
438 val = skip_spaces(end);
439 get_match_id(&id, val);
440 if (id == NULL) {
441 ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
442 fun->fnode->name);
443 return;
444 }
445
446 ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
447 "function %s", id, score, fun->fnode->name);
448
449 rc = ddf_fun_add_match_id(fun->fnode, id, score);
450 if (rc != EOK) {
451 ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
452 str_error(rc));
453 }
454
455 free(id);
456}
457
458static bool prop_parse(isa_fun_t *fun, char *line, const char *prop,
459 void (*read_fn)(isa_fun_t *, char *))
460{
461 size_t proplen = str_size(prop);
462
463 if (str_lcmp(line, prop, proplen) == 0) {
464 line += proplen;
465 line = skip_spaces(line);
466 (*read_fn)(fun, line);
467
468 return true;
469 }
470
471 return false;
472}
473
474static void fun_prop_parse(isa_fun_t *fun, char *line)
475{
476 /* Skip leading spaces. */
477 line = skip_spaces(line);
478
479 if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
480 !prop_parse(fun, line, "irq", &fun_parse_irq) &&
481 !prop_parse(fun, line, "dma", &fun_parse_dma) &&
482 !prop_parse(fun, line, "match", &fun_parse_match_id)) {
483
484 ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
485 line);
486 }
487}
488
489static void fun_hw_res_alloc(isa_fun_t *fun)
490{
491 fun->hw_resources.resources =
492 (hw_resource_t *)malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
493}
494
495static void fun_hw_res_free(isa_fun_t *fun)
496{
497 free(fun->hw_resources.resources);
498 fun->hw_resources.resources = NULL;
499}
500
501static char *isa_fun_read_info(char *fun_conf, isa_bus_t *isa)
502{
503 char *line;
504 char *fun_name = NULL;
505
506 /* Skip empty lines. */
507 while (true) {
508 line = str_get_line(fun_conf, &fun_conf);
509
510 if (line == NULL) {
511 /* no more lines */
512 return NULL;
513 }
514
515 if (!line_empty(line))
516 break;
517 }
518
519 /* Get device name. */
520 fun_name = get_device_name(line);
521 if (fun_name == NULL)
522 return NULL;
523
524 isa_fun_t *fun = isa_fun_create(isa, fun_name);
525 if (fun == NULL) {
526 free(fun_name);
527 return NULL;
528 }
529
530 /* Allocate buffer for the list of hardware resources of the device. */
531 fun_hw_res_alloc(fun);
532
533 /* Get properties of the device (match ids, irq and io range). */
534 while (true) {
535 line = str_get_line(fun_conf, &fun_conf);
536
537 if (line_empty(line)) {
538 /* no more device properties */
539 break;
540 }
541
542 /*
543 * Get the device's property from the configuration line
544 * and store it in the device structure.
545 */
546 fun_prop_parse(fun, line);
547 }
548
549 /* Set device operations to the device. */
550 fun->fnode->ops = &isa_fun_ops;
551
552 ddf_msg(LVL_DEBUG, "Binding function %s.", fun->fnode->name);
553
554 /* XXX Handle error */
555 (void) ddf_fun_bind(fun->fnode);
556
557 list_append(&fun->bus_link, &isa->functions);
558
559 return fun_conf;
560}
561
562static void fun_conf_parse(char *conf, isa_bus_t *isa)
563{
564 while (conf != NULL && *conf != '\0') {
565 conf = isa_fun_read_info(conf, isa);
566 }
567}
568
569static void isa_functions_add(isa_bus_t *isa)
570{
571 char *fun_conf;
572
573 fun_conf = fun_conf_read(CHILD_FUN_CONF_PATH);
574 if (fun_conf != NULL) {
575 fun_conf_parse(fun_conf, isa);
576 free(fun_conf);
577 }
578}
579
580static int isa_add_device(ddf_dev_t *dev)
581{
582 isa_bus_t *isa;
583
584 ddf_msg(LVL_DEBUG, "isa_add_device, device handle = %d",
585 (int) dev->handle);
586
587 isa = ddf_dev_data_alloc(dev, sizeof(isa_bus_t));
588 if (isa == NULL)
589 return ENOMEM;
590
591 fibril_mutex_initialize(&isa->mutex);
592 isa->dev = dev;
593 list_initialize(&isa->functions);
594
595 /* Make the bus device more visible. Does not do anything. */
596 ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
597
598 fibril_mutex_lock(&isa->mutex);
599
600 isa->fctl = ddf_fun_create(dev, fun_exposed, "ctl");
601 if (isa->fctl == NULL) {
602 ddf_msg(LVL_ERROR, "Failed creating control function.");
603 return EXDEV;
604 }
605
606 if (ddf_fun_bind(isa->fctl) != EOK) {
607 ddf_fun_destroy(isa->fctl);
608 ddf_msg(LVL_ERROR, "Failed binding control function.");
609 return EXDEV;
610 }
611
612 /* Add functions as specified in the configuration file. */
613 isa_functions_add(isa);
614 ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
615
616 fibril_mutex_unlock(&isa->mutex);
617
618 return EOK;
619}
620
621static int isa_dev_remove(ddf_dev_t *dev)
622{
623 isa_bus_t *isa = ISA_BUS(dev);
624 int rc;
625
626 fibril_mutex_lock(&isa->mutex);
627
628 while (!list_empty(&isa->functions)) {
629 isa_fun_t *fun = list_get_instance(list_first(&isa->functions),
630 isa_fun_t, bus_link);
631
632 rc = ddf_fun_offline(fun->fnode);
633 if (rc != EOK) {
634 fibril_mutex_unlock(&isa->mutex);
635 ddf_msg(LVL_ERROR, "Failed offlining %s", fun->fnode->name);
636 return rc;
637 }
638
639 rc = ddf_fun_unbind(fun->fnode);
640 if (rc != EOK) {
641 fibril_mutex_unlock(&isa->mutex);
642 ddf_msg(LVL_ERROR, "Failed unbinding %s", fun->fnode->name);
643 return rc;
644 }
645
646 list_remove(&fun->bus_link);
647
648 fun_hw_res_free(fun);
649 ddf_fun_destroy(fun->fnode);
650 }
651
652 if (ddf_fun_unbind(isa->fctl) != EOK) {
653 fibril_mutex_unlock(&isa->mutex);
654 ddf_msg(LVL_ERROR, "Failed unbinding control function.");
655 return EXDEV;
656 }
657
658 fibril_mutex_unlock(&isa->mutex);
659
660 return EOK;
661}
662
663static int isa_fun_online(ddf_fun_t *fun)
664{
665 ddf_msg(LVL_DEBUG, "isa_fun_online()");
666 return ddf_fun_online(fun);
667}
668
669static int isa_fun_offline(ddf_fun_t *fun)
670{
671 ddf_msg(LVL_DEBUG, "isa_fun_offline()");
672 return ddf_fun_offline(fun);
673}
674
675
676static void isa_init()
677{
678 ddf_log_init(NAME, LVL_ERROR);
679 isa_fun_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
680}
681
682int main(int argc, char *argv[])
683{
684 printf(NAME ": HelenOS ISA bus driver\n");
685 isa_init();
686 return ddf_driver_main(&isa_driver);
687}
688
689/**
690 * @}
691 */
Note: See TracBrowser for help on using the repository browser.