Changeset 68414f4a in mainline for uspace/drv/isa/isa.c


Ignore:
Timestamp:
2011-02-13T20:03:45Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bab6388
Parents:
8b1e15ac
Message:

Refactor drivers

  • Rename soft-state structures to have the simplest names
  • Use soft-state structures as a starting point instead of DDF device or function nodes
  • Convert to standard naming scheme
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/isa/isa.c

    r8b1e15ac r68414f4a  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
     3 * Copyright (c) 2011 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    6061#define CHILD_FUN_CONF_PATH "/drv/isa/isa.dev"
    6162
     63/** Obtain soft-state pointer from function node pointer */
     64#define ISA_FUN(fnode) ((isa_fun_t *) ((fnode)->driver_data))
     65
    6266#define ISA_MAX_HW_RES 4
    6367
    64 typedef struct isa_fun_data {
     68typedef struct isa_fun {
     69        function_t *fnode;
    6570        hw_resource_list_t hw_resources;
    66 } isa_fun_data_t;
    67 
    68 static hw_resource_list_t *isa_get_fun_resources(function_t *fun)
    69 {
    70         isa_fun_data_t *fun_data;
    71 
    72         fun_data = (isa_fun_data_t *)fun->driver_data;
    73         if (fun_data == NULL)
    74                 return NULL;
    75 
    76         return &fun_data->hw_resources;
    77 }
    78 
    79 static bool isa_enable_fun_interrupt(function_t *fun)
     71} isa_fun_t;
     72
     73static hw_resource_list_t *isa_get_fun_resources(function_t *fnode)
     74{
     75        isa_fun_t *fun = ISA_FUN(fnode);
     76        assert(fun != NULL);
     77
     78        return &fun->hw_resources;
     79}
     80
     81static bool isa_enable_fun_interrupt(function_t *fnode)
    8082{
    8183        // TODO
     
    8991};
    9092
    91 static device_ops_t isa_fun_dev_ops;
     93static device_ops_t isa_fun_ops;
    9294
    9395static int isa_add_device(device_t *dev);
     
    104106};
    105107
    106 
    107 static isa_fun_data_t *create_isa_fun_data()
    108 {
    109         isa_fun_data_t *data;
    110 
    111         data = (isa_fun_data_t *) malloc(sizeof(isa_fun_data_t));
    112         if (data != NULL)
    113                 memset(data, 0, sizeof(isa_fun_data_t));
    114 
    115         return data;
    116 }
    117 
    118 static function_t *create_isa_fun()
    119 {
    120         function_t *fun = create_function();
     108static isa_fun_t *isa_fun_create()
     109{
     110        isa_fun_t *fun = calloc(1, sizeof(isa_fun_t));
    121111        if (fun == NULL)
    122112                return NULL;
    123113
    124         isa_fun_data_t *data = create_isa_fun_data();
    125         if (data == NULL) {
    126                 delete_function(fun);
     114        function_t *fnode = create_function();
     115        if (fnode == NULL) {
     116                free(fun);
    127117                return NULL;
    128118        }
    129119
    130         fun->driver_data = data;
     120        fun->fnode = fnode;
     121        fnode->driver_data = fun;
    131122        return fun;
    132123}
    133124
    134 static char *read_fun_conf(const char *conf_path)
     125static char *fun_conf_read(const char *conf_path)
    135126{
    136127        bool suc = false;
     
    151142        lseek(fd, 0, SEEK_SET);
    152143        if (len == 0) {
    153                 printf(NAME ": read_fun_conf error: configuration file '%s' "
     144                printf(NAME ": fun_conf_read error: configuration file '%s' "
    154145                    "is empty.\n", conf_path);
    155146                goto cleanup;
     
    158149        buf = malloc(len + 1);
    159150        if (buf == NULL) {
    160                 printf(NAME ": read_fun_conf error: memory allocation failed.\n");
     151                printf(NAME ": fun_conf_read error: memory allocation failed.\n");
    161152                goto cleanup;
    162153        }
    163154
    164155        if (0 >= read(fd, buf, len)) {
    165                 printf(NAME ": read_fun_conf error: unable to read file '%s'.\n",
     156                printf(NAME ": fun_conf_read error: unable to read file '%s'.\n",
    166157                    conf_path);
    167158                goto cleanup;
     
    249240}
    250241
    251 static void isa_fun_set_irq(function_t *fun, int irq)
    252 {
    253         isa_fun_data_t *data = (isa_fun_data_t *)fun->driver_data;
    254 
    255         size_t count = data->hw_resources.count;
    256         hw_resource_t *resources = data->hw_resources.resources;
     242static void isa_fun_set_irq(isa_fun_t *fun, int irq)
     243{
     244        size_t count = fun->hw_resources.count;
     245        hw_resource_t *resources = fun->hw_resources.resources;
    257246
    258247        if (count < ISA_MAX_HW_RES) {
     
    260249                resources[count].res.interrupt.irq = irq;
    261250
    262                 data->hw_resources.count++;
    263 
    264                 printf(NAME ": added irq 0x%x to function %s\n", irq, fun->name);
    265         }
    266 }
    267 
    268 static void isa_fun_set_io_range(function_t *fun, size_t addr, size_t len)
    269 {
    270         isa_fun_data_t *data = (isa_fun_data_t *)fun->driver_data;
    271 
    272         size_t count = data->hw_resources.count;
    273         hw_resource_t *resources = data->hw_resources.resources;
     251                fun->hw_resources.count++;
     252
     253                printf(NAME ": added irq 0x%x to function %s\n", irq,
     254                    fun->fnode->name);
     255        }
     256}
     257
     258static void isa_fun_set_io_range(isa_fun_t *fun, size_t addr, size_t len)
     259{
     260        size_t count = fun->hw_resources.count;
     261        hw_resource_t *resources = fun->hw_resources.resources;
    274262
    275263        if (count < ISA_MAX_HW_RES) {
     
    279267                resources[count].res.io_range.endianness = LITTLE_ENDIAN;
    280268
    281                 data->hw_resources.count++;
     269                fun->hw_resources.count++;
    282270
    283271                printf(NAME ": added io range (addr=0x%x, size=0x%x) to "
    284272                    "function %s\n", (unsigned int) addr, (unsigned int) len,
    285                     fun->name);
    286         }
    287 }
    288 
    289 static void get_dev_irq(function_t *fun, char *val)
     273                    fun->fnode->name);
     274        }
     275}
     276
     277static void fun_parse_irq(isa_fun_t *fun, char *val)
    290278{
    291279        int irq = 0;
     
    299287}
    300288
    301 static void get_dev_io_range(function_t *fun, char *val)
     289static void fun_parse_io_range(isa_fun_t *fun, char *val)
    302290{
    303291        size_t addr, len;
     
    331319}
    332320
    333 static void get_fun_match_id(function_t *fun, char *val)
     321static void fun_parse_match_id(isa_fun_t *fun, char *val)
    334322{
    335323        char *id = NULL;
     
    337325        char *end = NULL;
    338326
    339         val = skip_spaces(val); 
     327        val = skip_spaces(val);
    340328
    341329        score = (int)strtol(val, &end, 10);
    342330        if (val == end) {
    343331                printf(NAME " : error - could not read match score for "
    344                     "function %s.\n", fun->name);
     332                    "function %s.\n", fun->fnode->name);
    345333                return;
    346334        }
     
    349337        if (match_id == NULL) {
    350338                printf(NAME " : failed to allocate match id for function %s.\n",
    351                     fun->name);
     339                    fun->fnode->name);
    352340                return;
    353341        }
     
    357345        if (id == NULL) {
    358346                printf(NAME " : error - could not read match id for "
    359                     "function %s.\n", fun->name);
     347                    "function %s.\n", fun->fnode->name);
    360348                delete_match_id(match_id);
    361349                return;
     
    366354
    367355        printf(NAME ": adding match id '%s' with score %d to function %s\n", id,
    368             score, fun->name);
    369         add_match_id(&fun->match_ids, match_id);
    370 }
    371 
    372 static bool read_fun_prop(function_t *fun, char *line, const char *prop,
    373     void (*read_fn)(function_t *, char *))
     356            score, fun->fnode->name);
     357        add_match_id(&fun->fnode->match_ids, match_id);
     358}
     359
     360static bool prop_parse(isa_fun_t *fun, char *line, const char *prop,
     361    void (*read_fn)(isa_fun_t *, char *))
    374362{
    375363        size_t proplen = str_size(prop);
     
    386374}
    387375
    388 static void get_fun_prop(function_t *fun, char *line)
     376static void fun_prop_parse(isa_fun_t *fun, char *line)
    389377{
    390378        /* Skip leading spaces. */
    391379        line = skip_spaces(line);
    392380
    393         if (!read_fun_prop(fun, line, "io_range", &get_dev_io_range) &&
    394             !read_fun_prop(fun, line, "irq", &get_dev_irq) &&
    395             !read_fun_prop(fun, line, "match", &get_fun_match_id))
     381        if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
     382            !prop_parse(fun, line, "irq", &fun_parse_irq) &&
     383            !prop_parse(fun, line, "match", &fun_parse_match_id))
    396384        {
    397385            printf(NAME " error undefined device property at line '%s'\n",
     
    400388}
    401389
    402 static void child_alloc_hw_res(function_t *fun)
    403 {
    404         isa_fun_data_t *data = (isa_fun_data_t *)fun->driver_data;
    405         data->hw_resources.resources =
     390static void fun_hw_res_alloc(isa_fun_t *fun)
     391{
     392        fun->hw_resources.resources =
    406393            (hw_resource_t *)malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
    407394}
    408395
    409 static char *read_isa_fun_info(char *fun_conf, device_t *dev)
     396static char *isa_fun_read_info(char *fun_conf, device_t *dev)
    410397{
    411398        char *line;
     
    430417                return NULL;
    431418
    432         function_t *fun = create_isa_fun();
     419        isa_fun_t *fun = isa_fun_create();
    433420        if (fun == NULL) {
    434421                free(fun_name);
     
    436423        }
    437424
    438         fun->name = fun_name;
    439         fun->ftype = fun_inner;
     425        function_t *fnode = fun->fnode;
     426        fnode->name = fun_name;
     427        fnode->ftype = fun_inner;
    440428
    441429        /* Allocate buffer for the list of hardware resources of the device. */
    442         child_alloc_hw_res(fun);
     430        fun_hw_res_alloc(fun);
    443431
    444432        /* Get properties of the device (match ids, irq and io range). */
     
    455443                 * and store it in the device structure.
    456444                 */
    457                 get_fun_prop(fun, line);
    458 
    459                 //printf(NAME ": next line ='%s'\n", fun_conf);
    460                 //printf(NAME ": current line ='%s'\n", line);
     445                fun_prop_parse(fun, line);
    461446        }
    462447
    463448        /* Set device operations to the device. */
    464         fun->ops = &isa_fun_dev_ops;
     449        fnode->ops = &isa_fun_ops;
    465450
    466451        printf(NAME ": register_function(fun, dev); function is %s.\n",
    467             fun->name);
    468         register_function(fun, dev);
     452            fnode->name);
     453        register_function(fnode, dev);
    469454
    470455        return fun_conf;
    471456}
    472457
    473 static void parse_fun_conf(char *conf, device_t *dev)
     458static void fun_conf_parse(char *conf, device_t *dev)
    474459{
    475460        while (conf != NULL && *conf != '\0') {
    476                 conf = read_isa_fun_info(conf, dev);
    477         }
    478 }
    479 
    480 static void add_legacy_children(device_t *dev)
     461                conf = isa_fun_read_info(conf, dev);
     462        }
     463}
     464
     465static void isa_functions_add(device_t *dev)
    481466{
    482467        char *fun_conf;
    483468
    484         fun_conf = read_fun_conf(CHILD_FUN_CONF_PATH);
     469        fun_conf = fun_conf_read(CHILD_FUN_CONF_PATH);
    485470        if (fun_conf != NULL) {
    486                 parse_fun_conf(fun_conf, dev);
     471                fun_conf_parse(fun_conf, dev);
    487472                free(fun_conf);
    488473        }
     
    502487        register_function(ctl, dev);
    503488
    504         /* Add child devices. */
    505         add_legacy_children(dev);
    506         printf(NAME ": finished the enumeration of legacy devices\n");
     489        /* Add functions as specified in the configuration file. */
     490        isa_functions_add(dev);
     491        printf(NAME ": finished the enumeration of legacy functions\n");
    507492
    508493        return EOK;
     
    511496static void isa_init()
    512497{
    513         isa_fun_dev_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
     498        isa_fun_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
    514499}
    515500
     
    524509 * @}
    525510 */
    526  
Note: See TracChangeset for help on using the changeset viewer.