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 <stdbool.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 <sys/stat.h>
|
---|
57 | #include <irc.h>
|
---|
58 | #include <ns.h>
|
---|
59 |
|
---|
60 | #include <ddf/driver.h>
|
---|
61 | #include <ddf/log.h>
|
---|
62 | #include <ops/hw_res.h>
|
---|
63 | #include <ops/pio_window.h>
|
---|
64 |
|
---|
65 | #include <device/hw_res.h>
|
---|
66 | #include <device/pio_window.h>
|
---|
67 |
|
---|
68 | #include <pci_dev_iface.h>
|
---|
69 |
|
---|
70 | #include "i8237.h"
|
---|
71 |
|
---|
72 | #define NAME "isa"
|
---|
73 | #define ISA_CHILD_FUN_CONF_PATH "/drv/isa/isa.dev"
|
---|
74 | #define EBUS_CHILD_FUN_CONF_PATH "/drv/isa/ebus.dev"
|
---|
75 |
|
---|
76 | #define ISA_MAX_HW_RES 5
|
---|
77 |
|
---|
78 | typedef struct {
|
---|
79 | fibril_mutex_t mutex;
|
---|
80 | uint16_t pci_vendor_id;
|
---|
81 | uint16_t pci_device_id;
|
---|
82 | uint8_t pci_class;
|
---|
83 | uint8_t pci_subclass;
|
---|
84 | ddf_dev_t *dev;
|
---|
85 | ddf_fun_t *fctl;
|
---|
86 | pio_window_t pio_win;
|
---|
87 | list_t functions;
|
---|
88 | } isa_bus_t;
|
---|
89 |
|
---|
90 | typedef 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 |
|
---|
98 | /** Obtain soft-state from device node */
|
---|
99 | static isa_bus_t *isa_bus(ddf_dev_t *dev)
|
---|
100 | {
|
---|
101 | return ddf_dev_data_get(dev);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Obtain soft-state from function node */
|
---|
105 | static isa_fun_t *isa_fun(ddf_fun_t *fun)
|
---|
106 | {
|
---|
107 | return ddf_fun_data_get(fun);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static hw_resource_list_t *isa_fun_get_resources(ddf_fun_t *fnode)
|
---|
111 | {
|
---|
112 | isa_fun_t *fun = isa_fun(fnode);
|
---|
113 | assert(fun);
|
---|
114 |
|
---|
115 | return &fun->hw_resources;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static bool isa_fun_enable_interrupt(ddf_fun_t *fnode)
|
---|
119 | {
|
---|
120 | /* This is an old ugly way, copied from pci driver */
|
---|
121 | assert(fnode);
|
---|
122 | isa_fun_t *fun = isa_fun(fnode);
|
---|
123 | assert(fun);
|
---|
124 |
|
---|
125 | const hw_resource_list_t *res = &fun->hw_resources;
|
---|
126 | assert(res);
|
---|
127 | for (size_t i = 0; i < res->count; ++i) {
|
---|
128 | if (res->resources[i].type == INTERRUPT) {
|
---|
129 | int rc = irc_enable_interrupt(
|
---|
130 | res->resources[i].res.interrupt.irq);
|
---|
131 |
|
---|
132 | if (rc != EOK)
|
---|
133 | return false;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static int isa_fun_setup_dma(ddf_fun_t *fnode,
|
---|
141 | unsigned int channel, uint32_t pa, uint32_t size, uint8_t mode)
|
---|
142 | {
|
---|
143 | assert(fnode);
|
---|
144 | isa_fun_t *fun = isa_fun(fnode);
|
---|
145 | assert(fun);
|
---|
146 | const hw_resource_list_t *res = &fun->hw_resources;
|
---|
147 | assert(res);
|
---|
148 |
|
---|
149 | for (size_t i = 0; i < res->count; ++i) {
|
---|
150 | /* Check for assigned channel */
|
---|
151 | if (((res->resources[i].type == DMA_CHANNEL_16) &&
|
---|
152 | (res->resources[i].res.dma_channel.dma16 == channel)) ||
|
---|
153 | ((res->resources[i].type == DMA_CHANNEL_8) &&
|
---|
154 | (res->resources[i].res.dma_channel.dma8 == channel))) {
|
---|
155 | return dma_channel_setup(channel, pa, size, mode);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | return EINVAL;
|
---|
160 | }
|
---|
161 |
|
---|
162 | static int isa_fun_remain_dma(ddf_fun_t *fnode,
|
---|
163 | unsigned channel, size_t *size)
|
---|
164 | {
|
---|
165 | assert(size);
|
---|
166 | assert(fnode);
|
---|
167 | isa_fun_t *fun = isa_fun(fnode);
|
---|
168 | assert(fun);
|
---|
169 | const hw_resource_list_t *res = &fun->hw_resources;
|
---|
170 | assert(res);
|
---|
171 |
|
---|
172 | for (size_t i = 0; i < res->count; ++i) {
|
---|
173 | /* Check for assigned channel */
|
---|
174 | if (((res->resources[i].type == DMA_CHANNEL_16) &&
|
---|
175 | (res->resources[i].res.dma_channel.dma16 == channel)) ||
|
---|
176 | ((res->resources[i].type == DMA_CHANNEL_8) &&
|
---|
177 | (res->resources[i].res.dma_channel.dma8 == channel))) {
|
---|
178 | return dma_channel_remain(channel, size);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | return EINVAL;
|
---|
183 | }
|
---|
184 |
|
---|
185 | static hw_res_ops_t isa_fun_hw_res_ops = {
|
---|
186 | .get_resource_list = isa_fun_get_resources,
|
---|
187 | .enable_interrupt = isa_fun_enable_interrupt,
|
---|
188 | .dma_channel_setup = isa_fun_setup_dma,
|
---|
189 | .dma_channel_remain = isa_fun_remain_dma,
|
---|
190 | };
|
---|
191 |
|
---|
192 | static pio_window_t *isa_fun_get_pio_window(ddf_fun_t *fnode)
|
---|
193 | {
|
---|
194 | ddf_dev_t *dev = ddf_fun_get_dev(fnode);
|
---|
195 | isa_bus_t *isa = isa_bus(dev);
|
---|
196 | assert(isa);
|
---|
197 |
|
---|
198 | return &isa->pio_win;
|
---|
199 | }
|
---|
200 |
|
---|
201 | static pio_window_ops_t isa_fun_pio_window_ops = {
|
---|
202 | .get_pio_window = isa_fun_get_pio_window
|
---|
203 | };
|
---|
204 |
|
---|
205 | static ddf_dev_ops_t isa_fun_ops= {
|
---|
206 | .interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops,
|
---|
207 | .interfaces[PIO_WINDOW_DEV_IFACE] = &isa_fun_pio_window_ops,
|
---|
208 | };
|
---|
209 |
|
---|
210 | static int isa_dev_add(ddf_dev_t *dev);
|
---|
211 | static int isa_dev_remove(ddf_dev_t *dev);
|
---|
212 | static int isa_fun_online(ddf_fun_t *fun);
|
---|
213 | static int isa_fun_offline(ddf_fun_t *fun);
|
---|
214 |
|
---|
215 | /** The isa device driver's standard operations */
|
---|
216 | static driver_ops_t isa_ops = {
|
---|
217 | .dev_add = &isa_dev_add,
|
---|
218 | .dev_remove = &isa_dev_remove,
|
---|
219 | .fun_online = &isa_fun_online,
|
---|
220 | .fun_offline = &isa_fun_offline
|
---|
221 | };
|
---|
222 |
|
---|
223 | /** The isa device driver structure. */
|
---|
224 | static driver_t isa_driver = {
|
---|
225 | .name = NAME,
|
---|
226 | .driver_ops = &isa_ops
|
---|
227 | };
|
---|
228 |
|
---|
229 | static isa_fun_t *isa_fun_create(isa_bus_t *isa, const char *name)
|
---|
230 | {
|
---|
231 | ddf_fun_t *fnode = ddf_fun_create(isa->dev, fun_inner, name);
|
---|
232 | if (fnode == NULL)
|
---|
233 | return NULL;
|
---|
234 |
|
---|
235 | isa_fun_t *fun = ddf_fun_data_alloc(fnode, sizeof(isa_fun_t));
|
---|
236 | if (fun == NULL) {
|
---|
237 | ddf_fun_destroy(fnode);
|
---|
238 | return NULL;
|
---|
239 | }
|
---|
240 |
|
---|
241 | fibril_mutex_initialize(&fun->mutex);
|
---|
242 | fun->hw_resources.resources = fun->resources;
|
---|
243 |
|
---|
244 | fun->fnode = fnode;
|
---|
245 | return fun;
|
---|
246 | }
|
---|
247 |
|
---|
248 | static char *fun_conf_read(const char *conf_path)
|
---|
249 | {
|
---|
250 | bool suc = false;
|
---|
251 | char *buf = NULL;
|
---|
252 | bool opened = false;
|
---|
253 | int fd;
|
---|
254 | size_t len;
|
---|
255 | ssize_t r;
|
---|
256 | struct stat st;
|
---|
257 |
|
---|
258 | fd = open(conf_path, O_RDONLY);
|
---|
259 | if (fd < 0) {
|
---|
260 | ddf_msg(LVL_ERROR, "Unable to open %s", conf_path);
|
---|
261 | goto cleanup;
|
---|
262 | }
|
---|
263 |
|
---|
264 | opened = true;
|
---|
265 |
|
---|
266 | if (fstat(fd, &st) != EOK) {
|
---|
267 | ddf_msg(LVL_ERROR, "Unable to fstat %d", fd);
|
---|
268 | goto cleanup;
|
---|
269 | }
|
---|
270 |
|
---|
271 | len = st.size;
|
---|
272 | if (len == 0) {
|
---|
273 | ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.",
|
---|
274 | conf_path);
|
---|
275 | goto cleanup;
|
---|
276 | }
|
---|
277 |
|
---|
278 | buf = malloc(len + 1);
|
---|
279 | if (buf == NULL) {
|
---|
280 | ddf_msg(LVL_ERROR, "Memory allocation failed.");
|
---|
281 | goto cleanup;
|
---|
282 | }
|
---|
283 |
|
---|
284 | r = read(fd, (aoff64_t []) {0}, buf, len);
|
---|
285 | if (r < 0) {
|
---|
286 | ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
|
---|
287 | goto cleanup;
|
---|
288 | }
|
---|
289 |
|
---|
290 | buf[len] = 0;
|
---|
291 |
|
---|
292 | suc = true;
|
---|
293 |
|
---|
294 | cleanup:
|
---|
295 | if (!suc && buf != NULL) {
|
---|
296 | free(buf);
|
---|
297 | buf = NULL;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (opened)
|
---|
301 | close(fd);
|
---|
302 |
|
---|
303 | return buf;
|
---|
304 | }
|
---|
305 |
|
---|
306 | static char *str_get_line(char *str, char **next)
|
---|
307 | {
|
---|
308 | char *line = str;
|
---|
309 | *next = NULL;
|
---|
310 |
|
---|
311 | if (str == NULL) {
|
---|
312 | return NULL;
|
---|
313 | }
|
---|
314 |
|
---|
315 | while (*str != '\0' && *str != '\n') {
|
---|
316 | str++;
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (*str != '\0') {
|
---|
320 | *next = str + 1;
|
---|
321 | }
|
---|
322 |
|
---|
323 | *str = '\0';
|
---|
324 | return line;
|
---|
325 | }
|
---|
326 |
|
---|
327 | static bool line_empty(const char *line)
|
---|
328 | {
|
---|
329 | while (line != NULL && *line != 0) {
|
---|
330 | if (!isspace(*line))
|
---|
331 | return false;
|
---|
332 | line++;
|
---|
333 | }
|
---|
334 |
|
---|
335 | return true;
|
---|
336 | }
|
---|
337 |
|
---|
338 | static char *get_device_name(char *line)
|
---|
339 | {
|
---|
340 | /* Skip leading spaces. */
|
---|
341 | while (*line != '\0' && isspace(*line)) {
|
---|
342 | line++;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /* Get the name part of the rest of the line. */
|
---|
346 | str_tok(line, ":", NULL);
|
---|
347 | return line;
|
---|
348 | }
|
---|
349 |
|
---|
350 | static inline const char *skip_spaces(const char *line)
|
---|
351 | {
|
---|
352 | /* Skip leading spaces. */
|
---|
353 | while (*line != '\0' && isspace(*line))
|
---|
354 | line++;
|
---|
355 |
|
---|
356 | return line;
|
---|
357 | }
|
---|
358 |
|
---|
359 | static void isa_fun_add_irq(isa_fun_t *fun, int irq)
|
---|
360 | {
|
---|
361 | size_t count = fun->hw_resources.count;
|
---|
362 | hw_resource_t *resources = fun->hw_resources.resources;
|
---|
363 |
|
---|
364 | if (count < ISA_MAX_HW_RES) {
|
---|
365 | resources[count].type = INTERRUPT;
|
---|
366 | resources[count].res.interrupt.irq = irq;
|
---|
367 |
|
---|
368 | fun->hw_resources.count++;
|
---|
369 |
|
---|
370 | ddf_msg(LVL_NOTE, "Added irq 0x%x to function %s", irq,
|
---|
371 | ddf_fun_get_name(fun->fnode));
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | static void isa_fun_add_dma(isa_fun_t *fun, int dma)
|
---|
376 | {
|
---|
377 | size_t count = fun->hw_resources.count;
|
---|
378 | hw_resource_t *resources = fun->hw_resources.resources;
|
---|
379 |
|
---|
380 | if (count < ISA_MAX_HW_RES) {
|
---|
381 | if ((dma > 0) && (dma < 4)) {
|
---|
382 | resources[count].type = DMA_CHANNEL_8;
|
---|
383 | resources[count].res.dma_channel.dma8 = dma;
|
---|
384 |
|
---|
385 | fun->hw_resources.count++;
|
---|
386 | ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
|
---|
387 | ddf_fun_get_name(fun->fnode));
|
---|
388 |
|
---|
389 | return;
|
---|
390 | }
|
---|
391 |
|
---|
392 | if ((dma > 4) && (dma < 8)) {
|
---|
393 | resources[count].type = DMA_CHANNEL_16;
|
---|
394 | resources[count].res.dma_channel.dma16 = dma;
|
---|
395 |
|
---|
396 | fun->hw_resources.count++;
|
---|
397 | ddf_msg(LVL_NOTE, "Added dma 0x%x to function %s", dma,
|
---|
398 | ddf_fun_get_name(fun->fnode));
|
---|
399 |
|
---|
400 | return;
|
---|
401 | }
|
---|
402 |
|
---|
403 | ddf_msg(LVL_WARN, "Skipped dma 0x%x for function %s", dma,
|
---|
404 | ddf_fun_get_name(fun->fnode));
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | static void isa_fun_add_io_range(isa_fun_t *fun, size_t addr, size_t len)
|
---|
409 | {
|
---|
410 | size_t count = fun->hw_resources.count;
|
---|
411 | hw_resource_t *resources = fun->hw_resources.resources;
|
---|
412 |
|
---|
413 | isa_bus_t *isa = isa_bus(ddf_fun_get_dev(fun->fnode));
|
---|
414 |
|
---|
415 | if (count < ISA_MAX_HW_RES) {
|
---|
416 | resources[count].type = IO_RANGE;
|
---|
417 | resources[count].res.io_range.address = addr;
|
---|
418 | resources[count].res.io_range.address += isa->pio_win.io.base;
|
---|
419 | resources[count].res.io_range.size = len;
|
---|
420 | resources[count].res.io_range.relative = false;
|
---|
421 | resources[count].res.io_range.endianness = LITTLE_ENDIAN;
|
---|
422 |
|
---|
423 | fun->hw_resources.count++;
|
---|
424 |
|
---|
425 | ddf_msg(LVL_NOTE, "Added io range (addr=0x%x, size=0x%x) to "
|
---|
426 | "function %s", (unsigned int) addr, (unsigned int) len,
|
---|
427 | ddf_fun_get_name(fun->fnode));
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | static void fun_parse_irq(isa_fun_t *fun, const char *val)
|
---|
432 | {
|
---|
433 | int irq = 0;
|
---|
434 | char *end = NULL;
|
---|
435 |
|
---|
436 | val = skip_spaces(val);
|
---|
437 | irq = (int) strtol(val, &end, 10);
|
---|
438 |
|
---|
439 | if (val != end)
|
---|
440 | isa_fun_add_irq(fun, irq);
|
---|
441 | }
|
---|
442 |
|
---|
443 | static void fun_parse_dma(isa_fun_t *fun, const char *val)
|
---|
444 | {
|
---|
445 | char *end = NULL;
|
---|
446 |
|
---|
447 | val = skip_spaces(val);
|
---|
448 | const int dma = strtol(val, &end, 10);
|
---|
449 |
|
---|
450 | if (val != end)
|
---|
451 | isa_fun_add_dma(fun, dma);
|
---|
452 | }
|
---|
453 |
|
---|
454 | static void fun_parse_io_range(isa_fun_t *fun, const char *val)
|
---|
455 | {
|
---|
456 | size_t addr, len;
|
---|
457 | char *end = NULL;
|
---|
458 |
|
---|
459 | val = skip_spaces(val);
|
---|
460 | addr = strtol(val, &end, 0x10);
|
---|
461 |
|
---|
462 | if (val == end)
|
---|
463 | return;
|
---|
464 |
|
---|
465 | val = skip_spaces(end);
|
---|
466 | len = strtol(val, &end, 0x10);
|
---|
467 |
|
---|
468 | if (val == end)
|
---|
469 | return;
|
---|
470 |
|
---|
471 | isa_fun_add_io_range(fun, addr, len);
|
---|
472 | }
|
---|
473 |
|
---|
474 | static void get_match_id(char **id, const char *val)
|
---|
475 | {
|
---|
476 | const char *end = val;
|
---|
477 |
|
---|
478 | while (!isspace(*end))
|
---|
479 | end++;
|
---|
480 |
|
---|
481 | size_t size = end - val + 1;
|
---|
482 | *id = (char *)malloc(size);
|
---|
483 | str_cpy(*id, size, val);
|
---|
484 | }
|
---|
485 |
|
---|
486 | static void fun_parse_match_id(isa_fun_t *fun, const char *val)
|
---|
487 | {
|
---|
488 | char *id = NULL;
|
---|
489 | char *end = NULL;
|
---|
490 |
|
---|
491 | val = skip_spaces(val);
|
---|
492 |
|
---|
493 | int score = (int)strtol(val, &end, 10);
|
---|
494 | if (val == end) {
|
---|
495 | ddf_msg(LVL_ERROR, "Cannot read match score for function "
|
---|
496 | "%s.", ddf_fun_get_name(fun->fnode));
|
---|
497 | return;
|
---|
498 | }
|
---|
499 |
|
---|
500 | val = skip_spaces(end);
|
---|
501 | get_match_id(&id, val);
|
---|
502 | if (id == NULL) {
|
---|
503 | ddf_msg(LVL_ERROR, "Cannot read match ID for function %s.",
|
---|
504 | ddf_fun_get_name(fun->fnode));
|
---|
505 | return;
|
---|
506 | }
|
---|
507 |
|
---|
508 | ddf_msg(LVL_DEBUG, "Adding match id '%s' with score %d to "
|
---|
509 | "function %s", id, score, ddf_fun_get_name(fun->fnode));
|
---|
510 |
|
---|
511 | int rc = ddf_fun_add_match_id(fun->fnode, id, score);
|
---|
512 | if (rc != EOK) {
|
---|
513 | ddf_msg(LVL_ERROR, "Failed adding match ID: %s",
|
---|
514 | str_error(rc));
|
---|
515 | }
|
---|
516 |
|
---|
517 | free(id);
|
---|
518 | }
|
---|
519 |
|
---|
520 | static bool prop_parse(isa_fun_t *fun, const char *line, const char *prop,
|
---|
521 | void (*read_fn)(isa_fun_t *, const char *))
|
---|
522 | {
|
---|
523 | size_t proplen = str_size(prop);
|
---|
524 |
|
---|
525 | if (str_lcmp(line, prop, proplen) == 0) {
|
---|
526 | line += proplen;
|
---|
527 | line = skip_spaces(line);
|
---|
528 | (*read_fn)(fun, line);
|
---|
529 |
|
---|
530 | return true;
|
---|
531 | }
|
---|
532 |
|
---|
533 | return false;
|
---|
534 | }
|
---|
535 |
|
---|
536 | static void fun_prop_parse(isa_fun_t *fun, const char *line)
|
---|
537 | {
|
---|
538 | /* Skip leading spaces. */
|
---|
539 | line = skip_spaces(line);
|
---|
540 |
|
---|
541 | if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
|
---|
542 | !prop_parse(fun, line, "irq", &fun_parse_irq) &&
|
---|
543 | !prop_parse(fun, line, "dma", &fun_parse_dma) &&
|
---|
544 | !prop_parse(fun, line, "match", &fun_parse_match_id)) {
|
---|
545 |
|
---|
546 | ddf_msg(LVL_ERROR, "Undefined device property at line '%s'",
|
---|
547 | line);
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 | static char *isa_fun_read_info(char *fun_conf, isa_bus_t *isa)
|
---|
552 | {
|
---|
553 | char *line;
|
---|
554 |
|
---|
555 | /* Skip empty lines. */
|
---|
556 | do {
|
---|
557 | line = str_get_line(fun_conf, &fun_conf);
|
---|
558 |
|
---|
559 | if (line == NULL) {
|
---|
560 | /* no more lines */
|
---|
561 | return NULL;
|
---|
562 | }
|
---|
563 |
|
---|
564 | } while (line_empty(line));
|
---|
565 |
|
---|
566 | /* Get device name. */
|
---|
567 | const char *fun_name = get_device_name(line);
|
---|
568 | if (fun_name == NULL)
|
---|
569 | return NULL;
|
---|
570 |
|
---|
571 | isa_fun_t *fun = isa_fun_create(isa, fun_name);
|
---|
572 | if (fun == NULL) {
|
---|
573 | return NULL;
|
---|
574 | }
|
---|
575 |
|
---|
576 | /* Get properties of the device (match ids, irq and io range). */
|
---|
577 | while (true) {
|
---|
578 | line = str_get_line(fun_conf, &fun_conf);
|
---|
579 |
|
---|
580 | if (line_empty(line)) {
|
---|
581 | /* no more device properties */
|
---|
582 | break;
|
---|
583 | }
|
---|
584 |
|
---|
585 | /*
|
---|
586 | * Get the device's property from the configuration line
|
---|
587 | * and store it in the device structure.
|
---|
588 | */
|
---|
589 | fun_prop_parse(fun, line);
|
---|
590 | }
|
---|
591 |
|
---|
592 | /* Set device operations to the device. */
|
---|
593 | ddf_fun_set_ops(fun->fnode, &isa_fun_ops);
|
---|
594 |
|
---|
595 | ddf_msg(LVL_DEBUG, "Binding function %s.", ddf_fun_get_name(fun->fnode));
|
---|
596 |
|
---|
597 | /* XXX Handle error */
|
---|
598 | (void) ddf_fun_bind(fun->fnode);
|
---|
599 |
|
---|
600 | list_append(&fun->bus_link, &isa->functions);
|
---|
601 |
|
---|
602 | return fun_conf;
|
---|
603 | }
|
---|
604 |
|
---|
605 | static void isa_functions_add(isa_bus_t *isa)
|
---|
606 | {
|
---|
607 | #define BASE_CLASS_BRIDGE 0x06
|
---|
608 | #define SUB_CLASS_BRIDGE_ISA 0x01
|
---|
609 | bool isa_bridge = ((isa->pci_class == BASE_CLASS_BRIDGE) &&
|
---|
610 | (isa->pci_subclass == SUB_CLASS_BRIDGE_ISA));
|
---|
611 |
|
---|
612 | #define VENDOR_ID_SUN 0x108e
|
---|
613 | #define DEVICE_ID_EBUS 0x1000
|
---|
614 | bool ebus = ((isa->pci_vendor_id == VENDOR_ID_SUN) &&
|
---|
615 | (isa->pci_device_id == DEVICE_ID_EBUS));
|
---|
616 |
|
---|
617 | const char *conf_path = NULL;
|
---|
618 | if (isa_bridge)
|
---|
619 | conf_path = ISA_CHILD_FUN_CONF_PATH;
|
---|
620 | else if (ebus)
|
---|
621 | conf_path = EBUS_CHILD_FUN_CONF_PATH;
|
---|
622 |
|
---|
623 | char *conf = fun_conf_read(conf_path);
|
---|
624 | while (conf != NULL && *conf != '\0') {
|
---|
625 | conf = isa_fun_read_info(conf, isa);
|
---|
626 | }
|
---|
627 | free(conf);
|
---|
628 | }
|
---|
629 |
|
---|
630 | static int isa_dev_add(ddf_dev_t *dev)
|
---|
631 | {
|
---|
632 | async_sess_t *sess;
|
---|
633 | int rc;
|
---|
634 |
|
---|
635 | ddf_msg(LVL_DEBUG, "isa_dev_add, device handle = %d",
|
---|
636 | (int) ddf_dev_get_handle(dev));
|
---|
637 |
|
---|
638 | isa_bus_t *isa = ddf_dev_data_alloc(dev, sizeof(isa_bus_t));
|
---|
639 | if (isa == NULL)
|
---|
640 | return ENOMEM;
|
---|
641 |
|
---|
642 | fibril_mutex_initialize(&isa->mutex);
|
---|
643 | isa->dev = dev;
|
---|
644 | list_initialize(&isa->functions);
|
---|
645 |
|
---|
646 | sess = ddf_dev_parent_sess_create(dev);
|
---|
647 | if (sess == NULL) {
|
---|
648 | ddf_msg(LVL_ERROR, "isa_dev_add failed to connect to the "
|
---|
649 | "parent driver.");
|
---|
650 | return ENOENT;
|
---|
651 | }
|
---|
652 |
|
---|
653 | rc = pci_config_space_read_16(sess, PCI_VENDOR_ID, &isa->pci_vendor_id);
|
---|
654 | if (rc != EOK)
|
---|
655 | return rc;
|
---|
656 | rc = pci_config_space_read_16(sess, PCI_DEVICE_ID, &isa->pci_device_id);
|
---|
657 | if (rc != EOK)
|
---|
658 | return rc;
|
---|
659 | rc = pci_config_space_read_8(sess, PCI_BASE_CLASS, &isa->pci_class);
|
---|
660 | if (rc != EOK)
|
---|
661 | return rc;
|
---|
662 | rc = pci_config_space_read_8(sess, PCI_SUB_CLASS, &isa->pci_subclass);
|
---|
663 | if (rc != EOK)
|
---|
664 | return rc;
|
---|
665 |
|
---|
666 | rc = pio_window_get(sess, &isa->pio_win);
|
---|
667 | if (rc != EOK) {
|
---|
668 | ddf_msg(LVL_ERROR, "isa_dev_add failed to get PIO window "
|
---|
669 | "for the device.");
|
---|
670 | return rc;
|
---|
671 | }
|
---|
672 |
|
---|
673 | /* Make the bus device more visible. Does not do anything. */
|
---|
674 | ddf_msg(LVL_DEBUG, "Adding a 'ctl' function");
|
---|
675 |
|
---|
676 | fibril_mutex_lock(&isa->mutex);
|
---|
677 |
|
---|
678 | isa->fctl = ddf_fun_create(dev, fun_exposed, "ctl");
|
---|
679 | if (isa->fctl == NULL) {
|
---|
680 | ddf_msg(LVL_ERROR, "Failed creating control function.");
|
---|
681 | return EXDEV;
|
---|
682 | }
|
---|
683 |
|
---|
684 | if (ddf_fun_bind(isa->fctl) != EOK) {
|
---|
685 | ddf_fun_destroy(isa->fctl);
|
---|
686 | ddf_msg(LVL_ERROR, "Failed binding control function.");
|
---|
687 | return EXDEV;
|
---|
688 | }
|
---|
689 |
|
---|
690 | /* Add functions as specified in the configuration file. */
|
---|
691 | isa_functions_add(isa);
|
---|
692 | ddf_msg(LVL_NOTE, "Finished enumerating legacy functions");
|
---|
693 |
|
---|
694 | fibril_mutex_unlock(&isa->mutex);
|
---|
695 |
|
---|
696 | return EOK;
|
---|
697 | }
|
---|
698 |
|
---|
699 | static int isa_dev_remove(ddf_dev_t *dev)
|
---|
700 | {
|
---|
701 | isa_bus_t *isa = isa_bus(dev);
|
---|
702 |
|
---|
703 | fibril_mutex_lock(&isa->mutex);
|
---|
704 |
|
---|
705 | while (!list_empty(&isa->functions)) {
|
---|
706 | isa_fun_t *fun = list_get_instance(list_first(&isa->functions),
|
---|
707 | isa_fun_t, bus_link);
|
---|
708 |
|
---|
709 | int rc = ddf_fun_offline(fun->fnode);
|
---|
710 | if (rc != EOK) {
|
---|
711 | fibril_mutex_unlock(&isa->mutex);
|
---|
712 | ddf_msg(LVL_ERROR, "Failed offlining %s", ddf_fun_get_name(fun->fnode));
|
---|
713 | return rc;
|
---|
714 | }
|
---|
715 |
|
---|
716 | rc = ddf_fun_unbind(fun->fnode);
|
---|
717 | if (rc != EOK) {
|
---|
718 | fibril_mutex_unlock(&isa->mutex);
|
---|
719 | ddf_msg(LVL_ERROR, "Failed unbinding %s", ddf_fun_get_name(fun->fnode));
|
---|
720 | return rc;
|
---|
721 | }
|
---|
722 |
|
---|
723 | list_remove(&fun->bus_link);
|
---|
724 |
|
---|
725 | ddf_fun_destroy(fun->fnode);
|
---|
726 | }
|
---|
727 |
|
---|
728 | if (ddf_fun_unbind(isa->fctl) != EOK) {
|
---|
729 | fibril_mutex_unlock(&isa->mutex);
|
---|
730 | ddf_msg(LVL_ERROR, "Failed unbinding control function.");
|
---|
731 | return EXDEV;
|
---|
732 | }
|
---|
733 |
|
---|
734 | fibril_mutex_unlock(&isa->mutex);
|
---|
735 |
|
---|
736 | return EOK;
|
---|
737 | }
|
---|
738 |
|
---|
739 | static int isa_fun_online(ddf_fun_t *fun)
|
---|
740 | {
|
---|
741 | ddf_msg(LVL_DEBUG, "isa_fun_online()");
|
---|
742 | return ddf_fun_online(fun);
|
---|
743 | }
|
---|
744 |
|
---|
745 | static int isa_fun_offline(ddf_fun_t *fun)
|
---|
746 | {
|
---|
747 | ddf_msg(LVL_DEBUG, "isa_fun_offline()");
|
---|
748 | return ddf_fun_offline(fun);
|
---|
749 | }
|
---|
750 |
|
---|
751 | int main(int argc, char *argv[])
|
---|
752 | {
|
---|
753 | printf(NAME ": HelenOS ISA bus driver\n");
|
---|
754 | ddf_log_init(NAME);
|
---|
755 | return ddf_driver_main(&isa_driver);
|
---|
756 | }
|
---|
757 |
|
---|
758 | /**
|
---|
759 | * @}
|
---|
760 | */
|
---|