source: mainline/uspace/srv/drivers/isa/isa.c@ 2300b9d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2300b9d was df747b9c, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

added device states (usable, invalid, not present, not initialized); add_device driver callback method returns integer (errno) instead of bool

  • Property mode set to 100644
File size: 10.7 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 <string.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 <resource.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 "/srv/drivers/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 = (isa_child_data_t *)dev->driver_data;
71 if (NULL == dev_data) {
72 return NULL;
73 }
74 return &dev_data->hw_resources;
75}
76
77static bool isa_enable_child_interrupt(device_t *dev)
78{
79 // TODO
80
81 return false;
82}
83
84static resource_iface_t isa_child_res_iface = {
85 &isa_get_child_resources,
86 &isa_enable_child_interrupt
87};
88
89static device_class_t isa_child_class;
90
91static int isa_add_device(device_t *dev);
92
93/** The isa device driver's standard operations.
94 */
95static driver_ops_t isa_ops = {
96 .add_device = &isa_add_device
97};
98
99/** The isa device driver structure.
100 */
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 = (isa_child_data_t *) malloc(sizeof(isa_child_data_t));
110 if (NULL != data) {
111 memset(data, 0, sizeof(isa_child_data_t));
112 }
113 return data;
114}
115
116static device_t * create_isa_child_dev()
117{
118 device_t *dev = create_device();
119 if (NULL == dev) {
120 return NULL;
121 }
122 isa_child_data_t *data = create_isa_child_data();
123 if (NULL == data) {
124 delete_device(dev);
125 return NULL;
126 }
127
128 dev->driver_data = data;
129 return dev;
130}
131
132static char * read_dev_conf(const char *conf_path)
133{
134 bool suc = false;
135 char *buf = NULL;
136 bool opened = false;
137 int fd;
138 off_t len = 0;
139
140 fd = open(conf_path, O_RDONLY);
141 if (fd < 0) {
142 printf(NAME ": unable to open %s\n", conf_path);
143 goto cleanup;
144 }
145 opened = true;
146
147 len = lseek(fd, 0, SEEK_END);
148 lseek(fd, 0, SEEK_SET);
149 if (len == 0) {
150 printf(NAME ": read_dev_conf error: configuration file '%s' is empty.\n", conf_path);
151 goto cleanup;
152 }
153
154 buf = malloc(len + 1);
155 if (buf == NULL) {
156 printf(NAME ": read_dev_conf error: memory allocation failed.\n");
157 goto cleanup;
158 }
159
160 if (0 >= read(fd, buf, len)) {
161 printf(NAME ": read_dev_conf error: unable to read file '%s'.\n", conf_path);
162 goto cleanup;
163 }
164 buf[len] = 0;
165
166 suc = true;
167
168cleanup:
169
170 if (!suc && NULL != buf) {
171 free(buf);
172 buf = NULL;
173 }
174
175 if(opened) {
176 close(fd);
177 }
178
179 return buf;
180}
181
182static char * str_get_line(char *str, char **next) {
183 char *line = str;
184
185 if (NULL == str) {
186 *next = NULL;
187 return NULL;
188 }
189
190 while (0 != *str && '\n' != *str) {
191 str++;
192 }
193
194 if (0 != *str) {
195 *next = str + 1;
196 } else {
197 *next = NULL;
198 }
199
200 *str = 0;
201
202 return line;
203}
204
205
206static bool line_empty(const char *line)
207{
208 while (NULL != line && 0 != *line) {
209 if(!isspace(*line)) {
210 return false;
211 }
212 line++;
213 }
214 return true;
215}
216
217static char * get_device_name(char *line) {
218 // skip leading spaces
219 while (0 != *line && isspace(*line)) {
220 line++;
221 }
222
223 // get the name part of the rest of the line
224 strtok(line, ":");
225
226 // alloc output buffer
227 size_t size = str_size(line) + 1;
228 char *name = malloc(size);
229
230 if (NULL != name) {
231 // copy the result to the output buffer
232 str_cpy(name, size, line);
233 }
234
235 return name;
236}
237
238static inline char * skip_spaces(char *line)
239{
240 // skip leading spaces
241 while (0 != *line && isspace(*line)) {
242 line++;
243 }
244 return line;
245}
246
247
248static void isa_child_set_irq(device_t *dev, int irq)
249{
250 isa_child_data_t *data = (isa_child_data_t *)dev->driver_data;
251
252 size_t count = data->hw_resources.count;
253 hw_resource_t *resources = data->hw_resources.resources;
254
255 if (count < ISA_MAX_HW_RES) {
256 resources[count].type = INTERRUPT;
257 resources[count].res.interrupt.irq = irq;
258
259 data->hw_resources.count++;
260
261 printf(NAME ": added irq 0x%x to device %s\n", irq, dev->name);
262 }
263}
264
265static void isa_child_set_io_range(device_t *dev, size_t addr, size_t len)
266{
267 isa_child_data_t *data = (isa_child_data_t *)dev->driver_data;
268
269 size_t count = data->hw_resources.count;
270 hw_resource_t *resources = data->hw_resources.resources;
271
272 if (count < ISA_MAX_HW_RES) {
273 resources[count].type = IO_RANGE;
274 resources[count].res.io_range.address = addr;
275 resources[count].res.io_range.size = len;
276 resources[count].res.io_range.endianness = LITTLE_ENDIAN;
277
278 data->hw_resources.count++;
279
280 printf(NAME ": added io range (addr=0x%x, size=0x%x) to device %s\n", addr, len, dev->name);
281 }
282}
283
284static void get_dev_irq(device_t *dev, char *val)
285{
286 int irq = 0;
287 char *end = NULL;
288
289 val = skip_spaces(val);
290 irq = (int)strtol(val, &end, 0x10);
291
292 if (val != end) {
293 isa_child_set_irq(dev, irq);
294 }
295}
296
297static void get_dev_io_range(device_t *dev, char *val)
298{
299 size_t addr, len;
300 char *end = NULL;
301
302 val = skip_spaces(val);
303 addr = strtol(val, &end, 0x10);
304
305 if (val == end) {
306 return;
307 }
308
309 val = skip_spaces(end);
310 len = strtol(val, &end, 0x10);
311
312 if (val == end) {
313 return;
314 }
315
316 isa_child_set_io_range(dev, addr, len);
317}
318
319static void get_match_id(char **id, char *val)
320{
321 char *end = val;
322
323 while (!isspace(*end)) {
324 end++;
325 }
326 size_t size = end - val + 1;
327 *id = (char *)malloc(size);
328 str_cpy(*id, size, val);
329}
330
331static void get_dev_match_id(device_t *dev, char *val)
332{
333 char *id = NULL;
334 int score = 0;
335 char *end = NULL;
336
337 val = skip_spaces(val);
338
339 score = (int)strtol(val, &end, 10);
340 if (val == end) {
341 printf(NAME " : error - could not read match score for device %s.\n", dev->name);
342 return;
343 }
344
345 match_id_t *match_id = create_match_id();
346 if (NULL == match_id) {
347 printf(NAME " : failed to allocate match id for device %s.\n", dev->name);
348 return;
349 }
350
351 val = skip_spaces(end);
352 get_match_id(&id, val);
353 if (NULL == id) {
354 printf(NAME " : error - could not read match id for device %s.\n", dev->name);
355 delete_match_id(match_id);
356 return;
357 }
358
359 match_id->id = id;
360 match_id->score = score;
361
362 printf(NAME ": adding match id '%s' with score %d to device %s\n", id, score, dev->name);
363 add_match_id(&dev->match_ids, match_id);
364}
365
366static bool read_dev_prop(
367 device_t *dev, char *line, const char *prop, void (* read_fn)(device_t *, char *))
368{
369 size_t proplen = str_size(prop);
370 if (0 == str_lcmp(line, prop, proplen)) {
371 line += proplen;
372 line = skip_spaces(line);
373 (*read_fn)(dev, line);
374 return true;
375 }
376 return false;
377}
378
379static void get_dev_prop(device_t *dev, char *line)
380{
381 // skip leading spaces
382 line = skip_spaces(line);
383
384 if (!read_dev_prop(dev, line, "io_range", &get_dev_io_range) &&
385 !read_dev_prop(dev, line, "irq", &get_dev_irq) &&
386 !read_dev_prop(dev, line, "match", &get_dev_match_id)
387 ) {
388 printf(NAME " error undefined device property at line '%s'\n", line);
389 }
390}
391
392static void child_alloc_hw_res(device_t *dev)
393{
394 isa_child_data_t *data = (isa_child_data_t *)dev->driver_data;
395 data->hw_resources.resources =
396 (hw_resource_t *)malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
397
398}
399
400static char * read_isa_dev_info(char *dev_conf, device_t *parent)
401{
402 char *line;
403 char *dev_name = NULL;
404
405 // skip empty lines
406 while (true) {
407 line = str_get_line(dev_conf, &dev_conf);
408
409 if (NULL == line) {
410 // no more lines
411 return NULL;
412 }
413
414 if (!line_empty(line)) {
415 break;
416 }
417 }
418
419 // get device name
420 dev_name = get_device_name(line);
421 if (NULL == dev_name) {
422 return NULL;
423 }
424
425 device_t *dev = create_isa_child_dev();
426 if (NULL == dev) {
427 free(dev_name);
428 return NULL;
429 }
430 dev->name = dev_name;
431
432 // allocate buffer for the list of hardware resources of the device
433 child_alloc_hw_res(dev);
434
435 // get properties of the device (match ids, irq and io range)
436 while (true) {
437 line = str_get_line(dev_conf, &dev_conf);
438
439 if (line_empty(line)) {
440 // no more device properties
441 break;
442 }
443
444 // get the device's property from the configuration line and store it in the device structure
445 get_dev_prop(dev, line);
446
447 //printf(NAME ": next line ='%s'\n", dev_conf);
448 //printf(NAME ": current line ='%s'\n", line);
449 }
450
451 // set a class (including the corresponding set of interfaces) to the device
452 dev->class = &isa_child_class;
453
454 printf(NAME ": child_device_register(dev, parent); device is %s.\n", dev->name);
455 child_device_register(dev, parent);
456
457 return dev_conf;
458}
459
460static char * parse_dev_conf(char *conf, device_t *parent)
461{
462 while (NULL != conf && 0 != *conf) {
463 conf = read_isa_dev_info(conf, parent);
464 }
465}
466
467static void add_legacy_children(device_t *parent)
468{
469 char *dev_conf = read_dev_conf(CHILD_DEV_CONF_PATH);
470 if (NULL != dev_conf) {
471 parse_dev_conf(dev_conf, parent);
472 free(dev_conf);
473 }
474}
475
476static int isa_add_device(device_t *dev)
477{
478 printf(NAME ": isa_add_device, device handle = %d\n", dev->handle);
479
480 // add child devices
481 add_legacy_children(dev);
482 printf(NAME ": finished the enumeration of legacy devices\n", dev->handle);
483
484 return EOK;
485}
486
487static void isa_init()
488{
489 isa_child_class.id = 0; // TODO
490 isa_child_class.interfaces[HW_RES_DEV_IFACE] = &isa_child_res_iface;
491}
492
493int main(int argc, char *argv[])
494{
495 printf(NAME ": HelenOS ISA bus driver\n");
496 isa_init();
497 return driver_main(&isa_driver);
498}
499
500/**
501 * @}
502 */
503
Note: See TracBrowser for help on using the repository browser.