source: mainline/uspace/drv/isa/isa.c@ 8871dba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8871dba was 41b56084, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Move headers declaring ops structures to ops/ subdirectory. The directory name 'ops/' does not imply device drivers because eventually this should be moved to the C library and open for use by all servers/protocols, not just device drivers.

  • Property mode set to 100644
File size: 10.6 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @defgroup isa ISA bus driver.
31 * @brief HelenOS ISA bus driver.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <stdio.h>
40#include <errno.h>
41#include <bool.h>
42#include <fibril_synch.h>
43#include <stdlib.h>
44#include <str.h>
45#include <ctype.h>
46#include <macros.h>
47#include <malloc.h>
48#include <dirent.h>
49#include <fcntl.h>
50#include <sys/stat.h>
51
52#include <driver.h>
53#include <ops/hw_res.h>
54
55#include <devman.h>
56#include <ipc/devman.h>
57#include <device/hw_res.h>
58
59#define NAME "isa"
60#define CHILD_DEV_CONF_PATH "/drv/isa/isa.dev"
61
62#define ISA_MAX_HW_RES 4
63
64typedef struct isa_child_data {
65 hw_resource_list_t hw_resources;
66} isa_child_data_t;
67
68static hw_resource_list_t *isa_get_child_resources(device_t *dev)
69{
70 isa_child_data_t *dev_data;
71
72 dev_data = (isa_child_data_t *)dev->driver_data;
73 if (dev_data == NULL)
74 return NULL;
75
76 return &dev_data->hw_resources;
77}
78
79static bool isa_enable_child_interrupt(device_t *dev)
80{
81 // TODO
82
83 return false;
84}
85
86static hw_res_ops_t isa_child_hw_res_ops = {
87 &isa_get_child_resources,
88 &isa_enable_child_interrupt
89};
90
91static device_ops_t isa_child_dev_ops;
92
93static int isa_add_device(device_t *dev);
94
95/** The isa device driver's standard operations */
96static driver_ops_t isa_ops = {
97 .add_device = &isa_add_device
98};
99
100/** The isa device driver structure. */
101static driver_t isa_driver = {
102 .name = NAME,
103 .driver_ops = &isa_ops
104};
105
106
107static isa_child_data_t *create_isa_child_data()
108{
109 isa_child_data_t *data;
110
111 data = (isa_child_data_t *) malloc(sizeof(isa_child_data_t));
112 if (data != NULL)
113 memset(data, 0, sizeof(isa_child_data_t));
114
115 return data;
116}
117
118static device_t *create_isa_child_dev()
119{
120 device_t *dev = create_device();
121 if (dev == NULL)
122 return NULL;
123
124 isa_child_data_t *data = create_isa_child_data();
125 if (data == NULL) {
126 delete_device(dev);
127 return NULL;
128 }
129
130 dev->driver_data = data;
131 return dev;
132}
133
134static char *read_dev_conf(const char *conf_path)
135{
136 bool suc = false;
137 char *buf = NULL;
138 bool opened = false;
139 int fd;
140 size_t len = 0;
141
142 fd = open(conf_path, O_RDONLY);
143 if (fd < 0) {
144 printf(NAME ": unable to open %s\n", conf_path);
145 goto cleanup;
146 }
147
148 opened = true;
149
150 len = lseek(fd, 0, SEEK_END);
151 lseek(fd, 0, SEEK_SET);
152 if (len == 0) {
153 printf(NAME ": read_dev_conf error: configuration file '%s' "
154 "is empty.\n", conf_path);
155 goto cleanup;
156 }
157
158 buf = malloc(len + 1);
159 if (buf == NULL) {
160 printf(NAME ": read_dev_conf error: memory allocation failed.\n");
161 goto cleanup;
162 }
163
164 if (0 >= read(fd, buf, len)) {
165 printf(NAME ": read_dev_conf error: unable to read file '%s'.\n",
166 conf_path);
167 goto cleanup;
168 }
169
170 buf[len] = 0;
171
172 suc = true;
173
174cleanup:
175 if (!suc && buf != NULL) {
176 free(buf);
177 buf = NULL;
178 }
179
180 if (opened)
181 close(fd);
182
183 return buf;
184}
185
186static char *str_get_line(char *str, char **next)
187{
188 char *line = str;
189
190 if (str == NULL) {
191 *next = NULL;
192 return NULL;
193 }
194
195 while (*str != '\0' && *str != '\n') {
196 str++;
197 }
198
199 if (*str != '\0') {
200 *next = str + 1;
201 } else {
202 *next = NULL;
203 }
204
205 *str = '\0';
206 return line;
207}
208
209static bool line_empty(const char *line)
210{
211 while (line != NULL && *line != 0) {
212 if (!isspace(*line))
213 return false;
214 line++;
215 }
216
217 return true;
218}
219
220static char *get_device_name(char *line)
221{
222 /* Skip leading spaces. */
223 while (*line != '\0' && isspace(*line)) {
224 line++;
225 }
226
227 /* Get the name part of the rest of the line. */
228 strtok(line, ":");
229
230 /* Allocate output buffer. */
231 size_t size = str_size(line) + 1;
232 char *name = malloc(size);
233
234 if (name != NULL) {
235 /* Copy the result to the output buffer. */
236 str_cpy(name, size, line);
237 }
238
239 return name;
240}
241
242static inline char *skip_spaces(char *line)
243{
244 /* Skip leading spaces. */
245 while (*line != '\0' && isspace(*line))
246 line++;
247
248 return line;
249}
250
251static void isa_child_set_irq(device_t *dev, int irq)
252{
253 isa_child_data_t *data = (isa_child_data_t *)dev->driver_data;
254
255 size_t count = data->hw_resources.count;
256 hw_resource_t *resources = data->hw_resources.resources;
257
258 if (count < ISA_MAX_HW_RES) {
259 resources[count].type = INTERRUPT;
260 resources[count].res.interrupt.irq = irq;
261
262 data->hw_resources.count++;
263
264 printf(NAME ": added irq 0x%x to device %s\n", irq, dev->name);
265 }
266}
267
268static void isa_child_set_io_range(device_t *dev, size_t addr, size_t len)
269{
270 isa_child_data_t *data = (isa_child_data_t *)dev->driver_data;
271
272 size_t count = data->hw_resources.count;
273 hw_resource_t *resources = data->hw_resources.resources;
274
275 if (count < ISA_MAX_HW_RES) {
276 resources[count].type = IO_RANGE;
277 resources[count].res.io_range.address = addr;
278 resources[count].res.io_range.size = len;
279 resources[count].res.io_range.endianness = LITTLE_ENDIAN;
280
281 data->hw_resources.count++;
282
283 printf(NAME ": added io range (addr=0x%x, size=0x%x) to "
284 "device %s\n", (unsigned int) addr, (unsigned int) len,
285 dev->name);
286 }
287}
288
289static void get_dev_irq(device_t *dev, char *val)
290{
291 int irq = 0;
292 char *end = NULL;
293
294 val = skip_spaces(val);
295 irq = (int)strtol(val, &end, 0x10);
296
297 if (val != end)
298 isa_child_set_irq(dev, irq);
299}
300
301static void get_dev_io_range(device_t *dev, char *val)
302{
303 size_t addr, len;
304 char *end = NULL;
305
306 val = skip_spaces(val);
307 addr = strtol(val, &end, 0x10);
308
309 if (val == end)
310 return;
311
312 val = skip_spaces(end);
313 len = strtol(val, &end, 0x10);
314
315 if (val == end)
316 return;
317
318 isa_child_set_io_range(dev, addr, len);
319}
320
321static void get_match_id(char **id, char *val)
322{
323 char *end = val;
324
325 while (!isspace(*end))
326 end++;
327
328 size_t size = end - val + 1;
329 *id = (char *)malloc(size);
330 str_cpy(*id, size, val);
331}
332
333static void get_dev_match_id(device_t *dev, char *val)
334{
335 char *id = NULL;
336 int score = 0;
337 char *end = NULL;
338
339 val = skip_spaces(val);
340
341 score = (int)strtol(val, &end, 10);
342 if (val == end) {
343 printf(NAME " : error - could not read match score for "
344 "device %s.\n", dev->name);
345 return;
346 }
347
348 match_id_t *match_id = create_match_id();
349 if (match_id == NULL) {
350 printf(NAME " : failed to allocate match id for device %s.\n",
351 dev->name);
352 return;
353 }
354
355 val = skip_spaces(end);
356 get_match_id(&id, val);
357 if (id == NULL) {
358 printf(NAME " : error - could not read match id for "
359 "device %s.\n", dev->name);
360 delete_match_id(match_id);
361 return;
362 }
363
364 match_id->id = id;
365 match_id->score = score;
366
367 printf(NAME ": adding match id '%s' with score %d to device %s\n", id,
368 score, dev->name);
369 add_match_id(&dev->match_ids, match_id);
370}
371
372static bool read_dev_prop(device_t *dev, char *line, const char *prop,
373 void (*read_fn)(device_t *, char *))
374{
375 size_t proplen = str_size(prop);
376
377 if (str_lcmp(line, prop, proplen) == 0) {
378 line += proplen;
379 line = skip_spaces(line);
380 (*read_fn)(dev, line);
381
382 return true;
383 }
384
385 return false;
386}
387
388static void get_dev_prop(device_t *dev, char *line)
389{
390 /* Skip leading spaces. */
391 line = skip_spaces(line);
392
393 if (!read_dev_prop(dev, line, "io_range", &get_dev_io_range) &&
394 !read_dev_prop(dev, line, "irq", &get_dev_irq) &&
395 !read_dev_prop(dev, line, "match", &get_dev_match_id))
396 {
397 printf(NAME " error undefined device property at line '%s'\n",
398 line);
399 }
400}
401
402static void child_alloc_hw_res(device_t *dev)
403{
404 isa_child_data_t *data = (isa_child_data_t *)dev->driver_data;
405 data->hw_resources.resources =
406 (hw_resource_t *)malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
407}
408
409static char *read_isa_dev_info(char *dev_conf, device_t *parent)
410{
411 char *line;
412 char *dev_name = NULL;
413
414 /* Skip empty lines. */
415 while (true) {
416 line = str_get_line(dev_conf, &dev_conf);
417
418 if (line == NULL) {
419 /* no more lines */
420 return NULL;
421 }
422
423 if (!line_empty(line))
424 break;
425 }
426
427 /* Get device name. */
428 dev_name = get_device_name(line);
429 if (dev_name == NULL)
430 return NULL;
431
432 device_t *dev = create_isa_child_dev();
433 if (dev == NULL) {
434 free(dev_name);
435 return NULL;
436 }
437
438 dev->name = dev_name;
439
440 /* Allocate buffer for the list of hardware resources of the device. */
441 child_alloc_hw_res(dev);
442
443 /* Get properties of the device (match ids, irq and io range). */
444 while (true) {
445 line = str_get_line(dev_conf, &dev_conf);
446
447 if (line_empty(line)) {
448 /* no more device properties */
449 break;
450 }
451
452 /*
453 * Get the device's property from the configuration line
454 * and store it in the device structure.
455 */
456 get_dev_prop(dev, line);
457
458 //printf(NAME ": next line ='%s'\n", dev_conf);
459 //printf(NAME ": current line ='%s'\n", line);
460 }
461
462 /* Set device operations to the device. */
463 dev->ops = &isa_child_dev_ops;
464
465 printf(NAME ": child_device_register(dev, parent); device is %s.\n",
466 dev->name);
467 child_device_register(dev, parent);
468
469 return dev_conf;
470}
471
472static void parse_dev_conf(char *conf, device_t *parent)
473{
474 while (conf != NULL && *conf != '\0') {
475 conf = read_isa_dev_info(conf, parent);
476 }
477}
478
479static void add_legacy_children(device_t *parent)
480{
481 char *dev_conf;
482
483 dev_conf = read_dev_conf(CHILD_DEV_CONF_PATH);
484 if (dev_conf != NULL) {
485 parse_dev_conf(dev_conf, parent);
486 free(dev_conf);
487 }
488}
489
490static int isa_add_device(device_t *dev)
491{
492 printf(NAME ": isa_add_device, device handle = %d\n",
493 (int) dev->handle);
494
495 /* Add child devices. */
496 add_legacy_children(dev);
497 printf(NAME ": finished the enumeration of legacy devices\n");
498
499 return EOK;
500}
501
502static void isa_init()
503{
504 isa_child_dev_ops.interfaces[HW_RES_DEV_IFACE] = &isa_child_hw_res_ops;
505}
506
507int main(int argc, char *argv[])
508{
509 printf(NAME ": HelenOS ISA bus driver\n");
510 isa_init();
511 return driver_main(&isa_driver);
512}
513
514/**
515 * @}
516 */
517
Note: See TracBrowser for help on using the repository browser.