Changeset eb1a2f4 in mainline for uspace/drv


Ignore:
Timestamp:
2011-02-22T23:30:56Z (15 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3b5d1535, a9c674e0
Parents:
dbe25f1 (diff), 664af708 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes (DDF refactoring)

This merge includes DDF refactoring that brought multifunctional devices
(i.e. ddf_dev_t and ddf_fun_t). Please, see ticket #295 at HelenOS
upstream Trac.

The conflicts themselves were easy to solve (merely several renamings).

Changes to USB subsystem:

  • drivers uses ddf_dev_t and ddf_fun_t
  • different signatures of many library functions
  • several hacks around communication with parent device (now the communication is clearer and somehow what we have now is hack about other hacks)
    • will repair and clean later
  • maybe added some extra debugging messages (the diff has about 240K, and I admit I have no energy to double check that)

WARNING:

  • the diff is VERY long, recommended is viewing partial diffs of the merge (i.e. merges in mainline branch that lead to the parent one)
  • merging with your branches might involve huge renamings, sorry, no other way is possible

BUGS:

  • hub driver will not work (no function created)

GOOD NEWS:

  • QEMU keyboard seems to work with QEMU 0.13 and 0.14
  • we are up-to-date with mainline again
Location:
uspace/drv
Files:
47 edited

Legend:

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

    rdbe25f1 reb1a2f4  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
     3 * Copyright (c) 2011 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    4344#include <stdlib.h>
    4445#include <str.h>
     46#include <str_error.h>
    4547#include <ctype.h>
    4648#include <macros.h>
     
    5052#include <sys/stat.h>
    5153
    52 #include <driver.h>
     54#include <ddf/driver.h>
    5355#include <ops/hw_res.h>
    5456
     
    5860
    5961#define NAME "isa"
    60 #define CHILD_DEV_CONF_PATH "/drv/isa/isa.dev"
     62#define CHILD_FUN_CONF_PATH "/drv/isa/isa.dev"
     63
     64/** Obtain soft-state pointer from function node pointer */
     65#define ISA_FUN(fnode) ((isa_fun_t *) ((fnode)->driver_data))
    6166
    6267#define ISA_MAX_HW_RES 4
    6368
    64 typedef struct isa_child_data {
     69typedef struct isa_fun {
     70        ddf_fun_t *fnode;
    6571        hw_resource_list_t hw_resources;
    66 } isa_child_data_t;
    67 
    68 static 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 
    79 static bool isa_enable_child_interrupt(device_t *dev)
     72} isa_fun_t;
     73
     74static hw_resource_list_t *isa_get_fun_resources(ddf_fun_t *fnode)
     75{
     76        isa_fun_t *fun = ISA_FUN(fnode);
     77        assert(fun != NULL);
     78
     79        return &fun->hw_resources;
     80}
     81
     82static bool isa_enable_fun_interrupt(ddf_fun_t *fnode)
    8083{
    8184        // TODO
     
    8487}
    8588
    86 static hw_res_ops_t isa_child_hw_res_ops = {
    87         &isa_get_child_resources,
    88         &isa_enable_child_interrupt
     89static hw_res_ops_t isa_fun_hw_res_ops = {
     90        &isa_get_fun_resources,
     91        &isa_enable_fun_interrupt
    8992};
    9093
    91 static device_ops_t isa_child_dev_ops;
    92 
    93 static int isa_add_device(device_t *dev);
     94static ddf_dev_ops_t isa_fun_ops;
     95
     96static int isa_add_device(ddf_dev_t *dev);
    9497
    9598/** The isa device driver's standard operations */
     
    104107};
    105108
    106 
    107 static 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 
    118 static device_t *create_isa_child_dev()
    119 {
    120         device_t *dev = create_device();
    121         if (dev == NULL)
     109static isa_fun_t *isa_fun_create(ddf_dev_t *dev, const char *name)
     110{
     111        isa_fun_t *fun = calloc(1, sizeof(isa_fun_t));
     112        if (fun == NULL)
    122113                return NULL;
    123114
    124         isa_child_data_t *data = create_isa_child_data();
    125         if (data == NULL) {
    126                 delete_device(dev);
     115        ddf_fun_t *fnode = ddf_fun_create(dev, fun_inner, name);
     116        if (fnode == NULL) {
     117                free(fun);
    127118                return NULL;
    128119        }
    129120
    130         dev->driver_data = data;
    131         return dev;
    132 }
    133 
    134 static char *read_dev_conf(const char *conf_path)
     121        fun->fnode = fnode;
     122        fnode->driver_data = fun;
     123        return fun;
     124}
     125
     126static char *fun_conf_read(const char *conf_path)
    135127{
    136128        bool suc = false;
     
    151143        lseek(fd, 0, SEEK_SET);
    152144        if (len == 0) {
    153                 printf(NAME ": read_dev_conf error: configuration file '%s' "
     145                printf(NAME ": fun_conf_read error: configuration file '%s' "
    154146                    "is empty.\n", conf_path);
    155147                goto cleanup;
     
    158150        buf = malloc(len + 1);
    159151        if (buf == NULL) {
    160                 printf(NAME ": read_dev_conf error: memory allocation failed.\n");
     152                printf(NAME ": fun_conf_read error: memory allocation failed.\n");
    161153                goto cleanup;
    162154        }
    163155
    164156        if (0 >= read(fd, buf, len)) {
    165                 printf(NAME ": read_dev_conf error: unable to read file '%s'.\n",
     157                printf(NAME ": fun_conf_read error: unable to read file '%s'.\n",
    166158                    conf_path);
    167159                goto cleanup;
     
    249241}
    250242
    251 static 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;
     243static void isa_fun_set_irq(isa_fun_t *fun, int irq)
     244{
     245        size_t count = fun->hw_resources.count;
     246        hw_resource_t *resources = fun->hw_resources.resources;
    257247
    258248        if (count < ISA_MAX_HW_RES) {
     
    260250                resources[count].res.interrupt.irq = irq;
    261251
    262                 data->hw_resources.count++;
    263 
    264                 printf(NAME ": added irq 0x%x to device %s\n", irq, dev->name);
    265         }
    266 }
    267 
    268 static 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;
     252                fun->hw_resources.count++;
     253
     254                printf(NAME ": added irq 0x%x to function %s\n", irq,
     255                    fun->fnode->name);
     256        }
     257}
     258
     259static void isa_fun_set_io_range(isa_fun_t *fun, size_t addr, size_t len)
     260{
     261        size_t count = fun->hw_resources.count;
     262        hw_resource_t *resources = fun->hw_resources.resources;
    274263
    275264        if (count < ISA_MAX_HW_RES) {
     
    279268                resources[count].res.io_range.endianness = LITTLE_ENDIAN;
    280269
    281                 data->hw_resources.count++;
     270                fun->hw_resources.count++;
    282271
    283272                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 
    289 static void get_dev_irq(device_t *dev, char *val)
     273                    "function %s\n", (unsigned int) addr, (unsigned int) len,
     274                    fun->fnode->name);
     275        }
     276}
     277
     278static void fun_parse_irq(isa_fun_t *fun, char *val)
    290279{
    291280        int irq = 0;
    292281        char *end = NULL;
    293282
    294         val = skip_spaces(val); 
     283        val = skip_spaces(val);
    295284        irq = (int)strtol(val, &end, 0x10);
    296285
    297286        if (val != end)
    298                 isa_child_set_irq(dev, irq);
    299 }
    300 
    301 static void get_dev_io_range(device_t *dev, char *val)
     287                isa_fun_set_irq(fun, irq);
     288}
     289
     290static void fun_parse_io_range(isa_fun_t *fun, char *val)
    302291{
    303292        size_t addr, len;
    304293        char *end = NULL;
    305294
    306         val = skip_spaces(val); 
     295        val = skip_spaces(val);
    307296        addr = strtol(val, &end, 0x10);
    308297
     
    310299                return;
    311300
    312         val = skip_spaces(end); 
     301        val = skip_spaces(end);
    313302        len = strtol(val, &end, 0x10);
    314303
     
    316305                return;
    317306
    318         isa_child_set_io_range(dev, addr, len);
     307        isa_fun_set_io_range(fun, addr, len);
    319308}
    320309
     
    331320}
    332321
    333 static void get_dev_match_id(device_t *dev, char *val)
     322static void fun_parse_match_id(isa_fun_t *fun, char *val)
    334323{
    335324        char *id = NULL;
    336325        int score = 0;
    337326        char *end = NULL;
    338 
    339         val = skip_spaces(val);
     327        int rc;
     328
     329        val = skip_spaces(val);
    340330
    341331        score = (int)strtol(val, &end, 10);
    342332        if (val == end) {
    343333                printf(NAME " : error - could not read match score for "
    344                     "device %s.\n", dev->name);
     334                    "function %s.\n", fun->fnode->name);
    345335                return;
    346336        }
    347337
    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);
     338        val = skip_spaces(end);
    356339        get_match_id(&id, val);
    357340        if (id == NULL) {
    358341                printf(NAME " : error - could not read match id for "
    359                     "device %s.\n", dev->name);
    360                 delete_match_id(match_id);
     342                    "function %s.\n", fun->fnode->name);
    361343                return;
    362344        }
    363345
    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 
    372 static bool read_dev_prop(device_t *dev, char *line, const char *prop,
    373     void (*read_fn)(device_t *, char *))
     346        printf(NAME ": adding match id '%s' with score %d to function %s\n", id,
     347            score, fun->fnode->name);
     348
     349        rc = ddf_fun_add_match_id(fun->fnode, id, score);
     350        if (rc != EOK)
     351                printf(NAME ": error adding match ID: %s\n", str_error(rc));
     352}
     353
     354static bool prop_parse(isa_fun_t *fun, char *line, const char *prop,
     355    void (*read_fn)(isa_fun_t *, char *))
    374356{
    375357        size_t proplen = str_size(prop);
     
    378360                line += proplen;
    379361                line = skip_spaces(line);
    380                 (*read_fn)(dev, line);
     362                (*read_fn)(fun, line);
    381363
    382364                return true;
     
    386368}
    387369
    388 static void get_dev_prop(device_t *dev, char *line)
     370static void fun_prop_parse(isa_fun_t *fun, char *line)
    389371{
    390372        /* Skip leading spaces. */
    391373        line = skip_spaces(line);
    392374
    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))
     375        if (!prop_parse(fun, line, "io_range", &fun_parse_io_range) &&
     376            !prop_parse(fun, line, "irq", &fun_parse_irq) &&
     377            !prop_parse(fun, line, "match", &fun_parse_match_id))
    396378        {
    397379            printf(NAME " error undefined device property at line '%s'\n",
     
    400382}
    401383
    402 static 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 =
     384static void fun_hw_res_alloc(isa_fun_t *fun)
     385{
     386        fun->hw_resources.resources =
    406387            (hw_resource_t *)malloc(sizeof(hw_resource_t) * ISA_MAX_HW_RES);
    407388}
    408389
    409 static char *read_isa_dev_info(char *dev_conf, device_t *parent)
     390static char *isa_fun_read_info(char *fun_conf, ddf_dev_t *dev)
    410391{
    411392        char *line;
    412         char *dev_name = NULL;
     393        char *fun_name = NULL;
    413394
    414395        /* Skip empty lines. */
    415396        while (true) {
    416                 line = str_get_line(dev_conf, &dev_conf);
     397                line = str_get_line(fun_conf, &fun_conf);
    417398
    418399                if (line == NULL) {
     
    426407
    427408        /* Get device name. */
    428         dev_name = get_device_name(line);
    429         if (dev_name == NULL)
     409        fun_name = get_device_name(line);
     410        if (fun_name == NULL)
    430411                return NULL;
    431412
    432         device_t *dev = create_isa_child_dev();
    433         if (dev == NULL) {
    434                 free(dev_name);
     413        isa_fun_t *fun = isa_fun_create(dev, fun_name);
     414        if (fun == NULL) {
     415                free(fun_name);
    435416                return NULL;
    436417        }
    437418
    438         dev->name = dev_name;
    439 
    440419        /* Allocate buffer for the list of hardware resources of the device. */
    441         child_alloc_hw_res(dev);
     420        fun_hw_res_alloc(fun);
    442421
    443422        /* Get properties of the device (match ids, irq and io range). */
    444423        while (true) {
    445                 line = str_get_line(dev_conf, &dev_conf);
     424                line = str_get_line(fun_conf, &fun_conf);
    446425
    447426                if (line_empty(line)) {
     
    454433                 * and store it in the device structure.
    455434                 */
    456                 get_dev_prop(dev, line);
    457 
    458                 //printf(NAME ": next line ='%s'\n", dev_conf);
    459                 //printf(NAME ": current line ='%s'\n", line);
     435                fun_prop_parse(fun, line);
    460436        }
    461437
    462438        /* 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 
    472 static void parse_dev_conf(char *conf, device_t *parent)
     439        fun->fnode->ops = &isa_fun_ops;
     440
     441        printf(NAME ": Binding function %s.\n", fun->fnode->name);
     442
     443        /* XXX Handle error */
     444        (void) ddf_fun_bind(fun->fnode);
     445
     446        return fun_conf;
     447}
     448
     449static void fun_conf_parse(char *conf, ddf_dev_t *dev)
    473450{
    474451        while (conf != NULL && *conf != '\0') {
    475                 conf = read_isa_dev_info(conf, parent);
    476         }
    477 }
    478 
    479 static 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 
    490 static int isa_add_device(device_t *dev)
     452                conf = isa_fun_read_info(conf, dev);
     453        }
     454}
     455
     456static void isa_functions_add(ddf_dev_t *dev)
     457{
     458        char *fun_conf;
     459
     460        fun_conf = fun_conf_read(CHILD_FUN_CONF_PATH);
     461        if (fun_conf != NULL) {
     462                fun_conf_parse(fun_conf, dev);
     463                free(fun_conf);
     464        }
     465}
     466
     467static int isa_add_device(ddf_dev_t *dev)
    491468{
    492469        printf(NAME ": isa_add_device, device handle = %d\n",
    493470            (int) dev->handle);
    494471
    495         /* Add child devices. */
    496         add_legacy_children(dev);
    497         printf(NAME ": finished the enumeration of legacy devices\n");
     472        /* Make the bus device more visible. Does not do anything. */
     473        printf(NAME ": adding a 'ctl' function\n");
     474
     475        ddf_fun_t *ctl = ddf_fun_create(dev, fun_exposed, "ctl");
     476        if (ctl == NULL) {
     477                printf(NAME ": Error creating control function.\n");
     478                return EXDEV;
     479        }
     480
     481        if (ddf_fun_bind(ctl) != EOK) {
     482                printf(NAME ": Error binding control function.\n");
     483                return EXDEV;
     484        }
     485
     486        /* Add functions as specified in the configuration file. */
     487        isa_functions_add(dev);
     488        printf(NAME ": finished the enumeration of legacy functions\n");
    498489
    499490        return EOK;
     
    502493static void isa_init()
    503494{
    504         isa_child_dev_ops.interfaces[HW_RES_DEV_IFACE] = &isa_child_hw_res_ops;
     495        isa_fun_ops.interfaces[HW_RES_DEV_IFACE] = &isa_fun_hw_res_ops;
    505496}
    506497
     
    509500        printf(NAME ": HelenOS ISA bus driver\n");
    510501        isa_init();
    511         return driver_main(&isa_driver);
     502        return ddf_driver_main(&isa_driver);
    512503}
    513504
     
    515506 * @}
    516507 */
    517  
  • uspace/drv/ns8250/ns8250.c

    rdbe25f1 reb1a2f4  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
     3 * Copyright (c) 2011 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    5253#include <libarch/ddi.h>
    5354
    54 #include <driver.h>
     55#include <ddf/driver.h>
     56#include <ddf/interrupt.h>
    5557#include <ops/char_dev.h>
    5658
     
    6769#define MAX_BAUD_RATE 115200
    6870#define DLAB_MASK (1 << 7)
     71
     72/** Obtain soft-state structure from function node */
     73#define NS8250(fnode) ((ns8250_t *) ((fnode)->dev->driver_data))
     74
     75/** Obtain soft-state structure from device node */
     76#define NS8250_FROM_DEV(dnode) ((ns8250_t *) ((dnode)->driver_data))
    6977
    7078/** The number of bits of one data unit send by the serial port. */
     
    8593
    8694/** The driver data for the serial port devices. */
    87 typedef struct ns8250_dev_data {
     95typedef struct ns8250 {
     96        /** DDF device node */
     97        ddf_dev_t *dev;
     98        /** DDF function node */
     99        ddf_fun_t *fun;
    88100        /** Is there any client conntected to the device? */
    89101        bool client_connected;
     
    98110        /** The fibril mutex for synchronizing the access to the device. */
    99111        fibril_mutex_t mutex;
    100 } ns8250_dev_data_t;
    101 
    102 /** Create driver data for a device.
    103  *
    104  * @return              The driver data.
    105  */
    106 static ns8250_dev_data_t *create_ns8250_dev_data(void)
    107 {
    108         ns8250_dev_data_t *data;
    109        
    110         data = (ns8250_dev_data_t *) malloc(sizeof(ns8250_dev_data_t));
    111         if (NULL != data) {
    112                 memset(data, 0, sizeof(ns8250_dev_data_t));
    113                 fibril_mutex_initialize(&data->mutex);
    114         }
    115         return data;
    116 }
    117 
    118 /** Delete driver data.
    119  *
    120  * @param data          The driver data structure.
    121  */
    122 static void delete_ns8250_dev_data(ns8250_dev_data_t *data)
    123 {
    124         if (data != NULL)
    125                 free(data);
     112} ns8250_t;
     113
     114/** Create per-device soft-state structure.
     115 *
     116 * @return      Pointer to soft-state structure.
     117 */
     118static ns8250_t *ns8250_new(void)
     119{
     120        ns8250_t *ns;
     121       
     122        ns = (ns8250_t *) calloc(1, sizeof(ns8250_t));
     123        if (ns == NULL)
     124                return NULL;
     125       
     126        fibril_mutex_initialize(&ns->mutex);
     127        return ns;
     128}
     129
     130/** Delete soft-state structure.
     131 *
     132 * @param ns    The driver data structure.
     133 */
     134static void ns8250_delete(ns8250_t *ns)
     135{
     136        assert(ns != NULL);
     137        free(ns);
    126138}
    127139
     
    171183/** Read data from the serial port device.
    172184 *
    173  * @param dev           The serial port device.
     185 * @param fun           The serial port function
    174186 * @param buf           The ouput buffer for read data.
    175187 * @param count         The number of bytes to be read.
     
    178190 *                      error number otherwise.
    179191 */
    180 static int ns8250_read(device_t *dev, char *buf, size_t count)
    181 {
     192static int ns8250_read(ddf_fun_t *fun, char *buf, size_t count)
     193{
     194        ns8250_t *ns = NS8250(fun);
    182195        int ret = EOK;
    183         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
    184        
    185         fibril_mutex_lock(&data->mutex);
    186         while (!buf_is_empty(&data->input_buffer) && (size_t)ret < count) {
    187                 buf[ret] = (char)buf_pop_front(&data->input_buffer);
     196       
     197        fibril_mutex_lock(&ns->mutex);
     198        while (!buf_is_empty(&ns->input_buffer) && (size_t)ret < count) {
     199                buf[ret] = (char)buf_pop_front(&ns->input_buffer);
    188200                ret++;
    189201        }
    190         fibril_mutex_unlock(&data->mutex);
     202        fibril_mutex_unlock(&ns->mutex);
    191203       
    192204        return ret;
     
    195207/** Write a character to the serial port.
    196208 *
    197  * @param data          The serial port device's driver data.
    198  * @param c             The character to be written.
    199  */
    200 static inline void ns8250_putchar(ns8250_dev_data_t *data, uint8_t c)
    201 {
    202         fibril_mutex_lock(&data->mutex);
    203         ns8250_write_8(data->port, c);
    204         fibril_mutex_unlock(&data->mutex);
     209 * @param ns            Serial port device
     210 * @param c             The character to be written
     211 */
     212static inline void ns8250_putchar(ns8250_t *ns, uint8_t c)
     213{
     214        fibril_mutex_lock(&ns->mutex);
     215        ns8250_write_8(ns->port, c);
     216        fibril_mutex_unlock(&ns->mutex);
    205217}
    206218
    207219/** Write data to the serial port.
    208220 *
    209  * @param dev           The serial port device.
    210  * @param buf           The data to be written.
    211  * @param count         The number of bytes to be written.
    212  * @return              Zero on success.
    213  */
    214 static int ns8250_write(device_t *dev, char *buf, size_t count)
    215 {
    216         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
     221 * @param fun           The serial port function
     222 * @param buf           The data to be written
     223 * @param count         The number of bytes to be written
     224 * @return              Zero on success
     225 */
     226static int ns8250_write(ddf_fun_t *fun, char *buf, size_t count)
     227{
     228        ns8250_t *ns = NS8250(fun);
    217229        size_t idx;
    218230       
    219231        for (idx = 0; idx < count; idx++)
    220                 ns8250_putchar(data, (uint8_t) buf[idx]);
     232                ns8250_putchar(ns, (uint8_t) buf[idx]);
    221233       
    222234        return 0;
    223235}
    224236
    225 static device_ops_t ns8250_dev_ops;
     237static ddf_dev_ops_t ns8250_dev_ops;
    226238
    227239/** The character interface's callbacks. */
     
    231243};
    232244
    233 static int ns8250_add_device(device_t *dev);
     245static int ns8250_add_device(ddf_dev_t *dev);
    234246
    235247/** The serial port device driver's standard operations. */
     
    244256};
    245257
    246 /** Clean up the serial port device structure.
    247  *
    248  * @param dev           The device structure.
    249  */
    250 static void ns8250_dev_cleanup(device_t *dev)
    251 {
    252         if (dev->driver_data != NULL) {
    253                 delete_ns8250_dev_data((ns8250_dev_data_t*) dev->driver_data);
    254                 dev->driver_data = NULL;
    255         }
    256        
    257         if (dev->parent_phone > 0) {
    258                 async_hangup(dev->parent_phone);
    259                 dev->parent_phone = 0;
     258/** Clean up the serial port soft-state
     259 *
     260 * @param ns            Serial port device
     261 */
     262static void ns8250_dev_cleanup(ns8250_t *ns)
     263{
     264        if (ns->dev->parent_phone > 0) {
     265                async_hangup(ns->dev->parent_phone);
     266                ns->dev->parent_phone = 0;
    260267        }
    261268}
     
    263270/** Enable the i/o ports of the device.
    264271 *
    265  * @param dev           The serial port device.
    266  * @return              True on success, false otherwise.
    267  */
    268 static bool ns8250_pio_enable(device_t *dev)
    269 {
    270         printf(NAME ": ns8250_pio_enable %s\n", dev->name);
    271        
    272         ns8250_dev_data_t *data = (ns8250_dev_data_t *)dev->driver_data;
     272 * @param ns            Serial port device
     273 * @return              True on success, false otherwise
     274 */
     275static bool ns8250_pio_enable(ns8250_t *ns)
     276{
     277        printf(NAME ": ns8250_pio_enable %s\n", ns->dev->name);
    273278       
    274279        /* Gain control over port's registers. */
    275         if (pio_enable((void *)(uintptr_t) data->io_addr, REG_COUNT,
    276             (void **) &data->port)) {
     280        if (pio_enable((void *)(uintptr_t) ns->io_addr, REG_COUNT,
     281            (void **) &ns->port)) {
    277282                printf(NAME ": error - cannot gain the port %#" PRIx32 " for device "
    278                     "%s.\n", data->io_addr, dev->name);
     283                    "%s.\n", ns->io_addr, ns->dev->name);
    279284                return false;
    280285        }
     
    285290/** Probe the serial port device for its presence.
    286291 *
    287  * @param dev           The serial port device.
    288  * @return              True if the device is present, false otherwise.
    289  */
    290 static bool ns8250_dev_probe(device_t *dev)
    291 {
    292         printf(NAME ": ns8250_dev_probe %s\n", dev->name);
    293        
    294         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
    295         ioport8_t *port_addr = data->port;
     292 * @param ns            Serial port device
     293 * @return              True if the device is present, false otherwise
     294 */
     295static bool ns8250_dev_probe(ns8250_t *ns)
     296{
     297        printf(NAME ": ns8250_dev_probe %s\n", ns->dev->name);
     298       
     299        ioport8_t *port_addr = ns->port;
    296300        bool res = true;
    297301        uint8_t olddata;
     
    310314       
    311315        if (!res)
    312                 printf(NAME ": device %s is not present.\n", dev->name);
     316                printf(NAME ": device %s is not present.\n", ns->dev->name);
    313317       
    314318        return res;
     
    317321/** Initialize serial port device.
    318322 *
    319  * @param dev           The serial port device.
    320  * @return              Zero on success, negative error number otherwise.
    321  */
    322 static int ns8250_dev_initialize(device_t *dev)
    323 {
    324         printf(NAME ": ns8250_dev_initialize %s\n", dev->name);
     323 * @param ns            Serial port device
     324 * @return              Zero on success, negative error number otherwise
     325 */
     326static int ns8250_dev_initialize(ns8250_t *ns)
     327{
     328        printf(NAME ": ns8250_dev_initialize %s\n", ns->dev->name);
    325329       
    326330        int ret = EOK;
     
    329333        memset(&hw_resources, 0, sizeof(hw_resource_list_t));
    330334       
    331         /* Allocate driver data for the device. */
    332         ns8250_dev_data_t *data = create_ns8250_dev_data();
    333         if (data == NULL)
    334                 return ENOMEM;
    335         dev->driver_data = data;
    336        
    337335        /* Connect to the parent's driver. */
    338         dev->parent_phone = devman_parent_device_connect(dev->handle,
     336        ns->dev->parent_phone = devman_parent_device_connect(ns->dev->handle,
    339337            IPC_FLAG_BLOCKING);
    340         if (dev->parent_phone < 0) {
     338        if (ns->dev->parent_phone < 0) {
    341339                printf(NAME ": failed to connect to the parent driver of the "
    342                     "device %s.\n", dev->name);
    343                 ret = dev->parent_phone;
     340                    "device %s.\n", ns->dev->name);
     341                ret = ns->dev->parent_phone;
    344342                goto failed;
    345343        }
    346344       
    347345        /* Get hw resources. */
    348         ret = hw_res_get_resource_list(dev->parent_phone, &hw_resources);
     346        ret = hw_res_get_resource_list(ns->dev->parent_phone, &hw_resources);
    349347        if (ret != EOK) {
    350348                printf(NAME ": failed to get hw resources for the device "
    351                     "%s.\n", dev->name);
     349                    "%s.\n", ns->dev->name);
    352350                goto failed;
    353351        }
     
    362360                switch (res->type) {
    363361                case INTERRUPT:
    364                         data->irq = res->res.interrupt.irq;
     362                        ns->irq = res->res.interrupt.irq;
    365363                        irq = true;
    366364                        printf(NAME ": the %s device was asigned irq = 0x%x.\n",
    367                             dev->name, data->irq);
     365                            ns->dev->name, ns->irq);
    368366                        break;
    369367                       
    370368                case IO_RANGE:
    371                         data->io_addr = res->res.io_range.address;
     369                        ns->io_addr = res->res.io_range.address;
    372370                        if (res->res.io_range.size < REG_COUNT) {
    373371                                printf(NAME ": i/o range assigned to the device "
    374                                     "%s is too small.\n", dev->name);
     372                                    "%s is too small.\n", ns->dev->name);
    375373                                ret = ELIMIT;
    376374                                goto failed;
     
    378376                        ioport = true;
    379377                        printf(NAME ": the %s device was asigned i/o address = "
    380                             "0x%x.\n", dev->name, data->io_addr);
     378                            "0x%x.\n", ns->dev->name, ns->io_addr);
    381379                        break;
    382380                       
     
    388386        if (!irq || !ioport) {
    389387                printf(NAME ": missing hw resource(s) for the device %s.\n",
    390                     dev->name);
     388                    ns->dev->name);
    391389                ret = ENOENT;
    392390                goto failed;
     
    397395       
    398396failed:
    399         ns8250_dev_cleanup(dev);
     397        ns8250_dev_cleanup(ns);
    400398        hw_res_clean_resource_list(&hw_resources);
    401399        return ret;
     
    404402/** Enable interrupts on the serial port device.
    405403 *
    406  * Interrupt when data is received.
     404 * Interrupt when data is received
    407405 *
    408406 * @param port          The base address of the serial port device's ports.
    409407 */
    410408static inline void ns8250_port_interrupts_enable(ioport8_t *port)
    411 {       
     409{
    412410        pio_write_8(port + 1, 0x1);     /* Interrupt when data received. */
    413411        pio_write_8(port + 4, 0xB);
     
    416414/** Disable interrupts on the serial port device.
    417415 *
    418  * @param port          The base address of the serial port device's ports.
     416 * @param port          The base address of the serial port device's ports
    419417 */
    420418static inline void ns8250_port_interrupts_disable(ioport8_t *port)
     
    425423/** Enable interrupts for the serial port device.
    426424 *
    427  * @param dev           The device.
    428  * @return              Zero on success, negative error number otherwise.
    429  */
    430 static int ns8250_interrupt_enable(device_t *dev)
    431 {
    432         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
    433        
     425 * @param ns            Serial port device
     426 * @return              Zero on success, negative error number otherwise
     427 */
     428static int ns8250_interrupt_enable(ns8250_t *ns)
     429{
    434430        /* Enable interrupt on the serial port. */
    435         ns8250_port_interrupts_enable(data->port);
     431        ns8250_port_interrupts_enable(ns->port);
    436432       
    437433        return EOK;
     
    618614 * Set the default parameters of the serial communication.
    619615 *
    620  * @param dev           The serial port device.
    621  */
    622 static void ns8250_initialize_port(device_t *dev)
    623 {
    624         ns8250_dev_data_t *data = (ns8250_dev_data_t *)dev->driver_data;
    625         ioport8_t *port = data->port;
     616 * @param ns            Serial port device
     617 */
     618static void ns8250_initialize_port(ns8250_t *ns)
     619{
     620        ioport8_t *port = ns->port;
    626621       
    627622        /* Disable interrupts. */
     
    643638 * buffer.
    644639 *
    645  * @param dev           The serial port device.
    646  */
    647 static void ns8250_read_from_device(device_t *dev)
    648 {
    649         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
    650         ioport8_t *port = data->port;
     640 * @param ns            Serial port device
     641 */
     642static void ns8250_read_from_device(ns8250_t *ns)
     643{
     644        ioport8_t *port = ns->port;
    651645        bool cont = true;
    652646       
    653647        while (cont) {
    654                 fibril_mutex_lock(&data->mutex);
     648                fibril_mutex_lock(&ns->mutex);
    655649               
    656650                cont = ns8250_received(port);
     
    658652                        uint8_t val = ns8250_read_8(port);
    659653                       
    660                         if (data->client_connected) {
    661                                 if (!buf_push_back(&data->input_buffer, val)) {
     654                        if (ns->client_connected) {
     655                                if (!buf_push_back(&ns->input_buffer, val)) {
    662656                                        printf(NAME ": buffer overflow on "
    663                                             "%s.\n", dev->name);
     657                                            "%s.\n", ns->dev->name);
    664658                                } else {
    665659                                        printf(NAME ": the character %c saved "
    666660                                            "to the buffer of %s.\n",
    667                                             val, dev->name);
     661                                            val, ns->dev->name);
    668662                                }
    669663                        }
    670664                }
    671665               
    672                 fibril_mutex_unlock(&data->mutex);
     666                fibril_mutex_unlock(&ns->mutex);
    673667                fibril_yield();
    674668        }
     
    682676 * @param dev           The serial port device.
    683677 */
    684 static inline void ns8250_interrupt_handler(device_t *dev, ipc_callid_t iid,
     678static inline void ns8250_interrupt_handler(ddf_dev_t *dev, ipc_callid_t iid,
    685679    ipc_call_t *icall)
    686680{
    687         ns8250_read_from_device(dev);
     681        ns8250_read_from_device(NS8250_FROM_DEV(dev));
    688682}
    689683
    690684/** Register the interrupt handler for the device.
    691685 *
     686 * @param ns            Serial port device
     687 */
     688static inline int ns8250_register_interrupt_handler(ns8250_t *ns)
     689{
     690        return register_interrupt_handler(ns->dev, ns->irq,
     691            ns8250_interrupt_handler, NULL);
     692}
     693
     694/** Unregister the interrupt handler for the device.
     695 *
     696 * @param ns            Serial port device
     697 */
     698static inline int ns8250_unregister_interrupt_handler(ns8250_t *ns)
     699{
     700        return unregister_interrupt_handler(ns->dev, ns->irq);
     701}
     702
     703/** The add_device callback method of the serial port driver.
     704 *
     705 * Probe and initialize the newly added device.
     706 *
    692707 * @param dev           The serial port device.
    693708 */
    694 static inline int ns8250_register_interrupt_handler(device_t *dev)
    695 {
    696         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
    697        
    698         return register_interrupt_handler(dev, data->irq,
    699             ns8250_interrupt_handler, NULL);
    700 }
    701 
    702 /** Unregister the interrupt handler for the device.
    703  *
    704  * @param dev           The serial port device.
    705  */
    706 static inline int ns8250_unregister_interrupt_handler(device_t *dev)
    707 {
    708         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
    709        
    710         return unregister_interrupt_handler(dev, data->irq);
    711 }
    712 
    713 /** The add_device callback method of the serial port driver.
    714  *
    715  * Probe and initialize the newly added device.
    716  *
    717  * @param dev           The serial port device.
    718  */
    719 static int ns8250_add_device(device_t *dev)
    720 {
     709static int ns8250_add_device(ddf_dev_t *dev)
     710{
     711        ns8250_t *ns = NULL;
     712        ddf_fun_t *fun = NULL;
     713        bool need_cleanup = false;
     714        int rc;
     715       
    721716        printf(NAME ": ns8250_add_device %s (handle = %d)\n",
    722717            dev->name, (int) dev->handle);
    723718       
    724         int res = ns8250_dev_initialize(dev);
    725         if (res != EOK)
    726                 return res;
    727        
    728         if (!ns8250_pio_enable(dev)) {
    729                 ns8250_dev_cleanup(dev);
    730                 return EADDRNOTAVAIL;
     719        /* Allocate soft-state for the device */
     720        ns = ns8250_new();
     721        if (ns == NULL) {
     722                rc = ENOMEM;
     723                goto fail;
     724        }
     725       
     726        ns->dev = dev;
     727        dev->driver_data = ns;
     728       
     729        rc = ns8250_dev_initialize(ns);
     730        if (rc != EOK)
     731                goto fail;
     732       
     733        need_cleanup = true;
     734       
     735        if (!ns8250_pio_enable(ns)) {
     736                rc = EADDRNOTAVAIL;
     737                goto fail;
    731738        }
    732739       
    733740        /* Find out whether the device is present. */
    734         if (!ns8250_dev_probe(dev)) {
    735                 ns8250_dev_cleanup(dev);
    736                 return ENOENT;
     741        if (!ns8250_dev_probe(ns)) {
     742                rc = ENOENT;
     743                goto fail;
    737744        }
    738745       
    739746        /* Serial port initialization (baud rate etc.). */
    740         ns8250_initialize_port(dev);
     747        ns8250_initialize_port(ns);
    741748       
    742749        /* Register interrupt handler. */
    743         if (ns8250_register_interrupt_handler(dev) != EOK) {
     750        if (ns8250_register_interrupt_handler(ns) != EOK) {
    744751                printf(NAME ": failed to register interrupt handler.\n");
    745                 ns8250_dev_cleanup(dev);
    746                 return res;
     752                rc = EADDRNOTAVAIL;
     753                goto fail;
    747754        }
    748755       
    749756        /* Enable interrupt. */
    750         res = ns8250_interrupt_enable(dev);
    751         if (res != EOK) {
     757        rc = ns8250_interrupt_enable(ns);
     758        if (rc != EOK) {
    752759                printf(NAME ": failed to enable the interrupt. Error code = "
    753                     "%d.\n", res);
    754                 ns8250_dev_cleanup(dev);
    755                 ns8250_unregister_interrupt_handler(dev);
    756                 return res;
     760                    "%d.\n", rc);
     761                goto fail;
     762        }
     763       
     764        fun = ddf_fun_create(dev, fun_exposed, "a");
     765        if (fun == NULL) {
     766                printf(NAME ": error creating function.\n");
     767                goto fail;
    757768        }
    758769       
    759770        /* Set device operations. */
    760         dev->ops = &ns8250_dev_ops;
    761        
    762         add_device_to_class(dev, "serial");
     771        fun->ops = &ns8250_dev_ops;
     772        rc = ddf_fun_bind(fun);
     773        if (rc != EOK) {
     774                printf(NAME ": error binding function.\n");
     775                goto fail;
     776        }
     777
     778        ns->fun = fun;
     779       
     780        ddf_fun_add_to_class(fun, "serial");
    763781       
    764782        printf(NAME ": the %s device has been successfully initialized.\n",
     
    766784       
    767785        return EOK;
     786fail:
     787        if (fun != NULL)
     788                ddf_fun_destroy(fun);
     789        if (need_cleanup)
     790                ns8250_dev_cleanup(ns);
     791        if (ns != NULL)
     792                ns8250_delete(ns);
     793        return rc;
    768794}
    769795
     
    775801 * @param dev           The device.
    776802 */
    777 static int ns8250_open(device_t *dev)
    778 {
    779         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
     803static int ns8250_open(ddf_fun_t *fun)
     804{
     805        ns8250_t *data = (ns8250_t *) fun->dev->driver_data;
    780806        int res;
    781807       
     
    788814        }
    789815        fibril_mutex_unlock(&data->mutex);
    790 
     816       
    791817        return res;
    792818}
     
    799825 * @param dev           The device.
    800826 */
    801 static void ns8250_close(device_t *dev)
    802 {
    803         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
     827static void ns8250_close(ddf_fun_t *fun)
     828{
     829        ns8250_t *data = (ns8250_t *) fun->dev->driver_data;
    804830       
    805831        fibril_mutex_lock(&data->mutex);
     
    823849 */
    824850static void
    825 ns8250_get_props(device_t *dev, unsigned int *baud_rate, unsigned int *parity,
     851ns8250_get_props(ddf_dev_t *dev, unsigned int *baud_rate, unsigned int *parity,
    826852    unsigned int *word_length, unsigned int* stop_bits)
    827853{
    828         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
     854        ns8250_t *data = (ns8250_t *) dev->driver_data;
    829855        ioport8_t *port = data->port;
    830856       
     
    850876 * @param stop_bits     The number of stop bits to be used.
    851877 */
    852 static int ns8250_set_props(device_t *dev, unsigned int baud_rate,
     878static int ns8250_set_props(ddf_dev_t *dev, unsigned int baud_rate,
    853879    unsigned int parity, unsigned int word_length, unsigned int stop_bits)
    854880{
     
    857883            stop_bits);
    858884       
    859         ns8250_dev_data_t *data = (ns8250_dev_data_t *) dev->driver_data;
     885        ns8250_t *data = (ns8250_t *) dev->driver_data;
    860886        ioport8_t *port = data->port;
    861887        int ret;
     
    877903 * Configure the parameters of the serial communication.
    878904 */
    879 static void ns8250_default_handler(device_t *dev, ipc_callid_t callid,
     905static void ns8250_default_handler(ddf_fun_t *fun, ipc_callid_t callid,
    880906    ipc_call_t *call)
    881907{
     
    886912        switch (method) {
    887913        case SERIAL_GET_COM_PROPS:
    888                 ns8250_get_props(dev, &baud_rate, &parity, &word_length,
     914                ns8250_get_props(fun->dev, &baud_rate, &parity, &word_length,
    889915                    &stop_bits);
    890916                async_answer_4(callid, EOK, baud_rate, parity, word_length,
     
    897923                word_length = IPC_GET_ARG3(*call);
    898924                stop_bits = IPC_GET_ARG4(*call);
    899                 ret = ns8250_set_props(dev, baud_rate, parity, word_length,
     925                ret = ns8250_set_props(fun->dev, baud_rate, parity, word_length,
    900926                    stop_bits);
    901927                async_answer_0(callid, ret);
     
    925951        printf(NAME ": HelenOS serial port driver\n");
    926952        ns8250_init();
    927         return driver_main(&ns8250_driver);
     953        return ddf_driver_main(&ns8250_driver);
    928954}
    929955
  • uspace/drv/pciintel/pci.c

    rdbe25f1 reb1a2f4  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
     3 * Copyright (c) 2011 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    4445#include <ctype.h>
    4546#include <macros.h>
    46 
    47 #include <driver.h>
     47#include <str_error.h>
     48
     49#include <ddf/driver.h>
    4850#include <devman.h>
    4951#include <ipc/devman.h>
     
    6567        ((1 << 31) | (bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
    6668
    67 static hw_resource_list_t *pciintel_get_child_resources(device_t *dev)
    68 {
    69         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
    70        
    71         if (dev_data == NULL)
     69/** Obtain PCI function soft-state from DDF function node */
     70#define PCI_FUN(fnode) ((pci_fun_t *) (fnode)->driver_data)
     71
     72/** Obtain PCI bus soft-state from DDF device node */
     73#define PCI_BUS(dnode) ((pci_bus_t *) (dnode)->driver_data)
     74
     75/** Obtain PCI bus soft-state from function soft-state */
     76#define PCI_BUS_FROM_FUN(fun) ((fun)->busptr)
     77
     78static hw_resource_list_t *pciintel_get_resources(ddf_fun_t *fnode)
     79{
     80        pci_fun_t *fun = PCI_FUN(fnode);
     81       
     82        if (fun == NULL)
    7283                return NULL;
    73         return &dev_data->hw_resources;
    74 }
    75 
    76 static bool pciintel_enable_child_interrupt(device_t *dev)
     84        return &fun->hw_resources;
     85}
     86
     87static bool pciintel_enable_interrupt(ddf_fun_t *fnode)
    7788{
    7889        /* This is an old ugly way, copied from ne2000 driver */
    79         assert(dev);
    80         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     90        assert(fnode);
     91        pci_fun_t *dev_data = (pci_fun_t *) fnode->driver_data;
    8192
    8293  sysarg_t apic;
     
    110121}
    111122
    112 static hw_res_ops_t pciintel_child_hw_res_ops = {
    113         &pciintel_get_child_resources,
    114         &pciintel_enable_child_interrupt
     123static hw_res_ops_t pciintel_hw_res_ops = {
     124        &pciintel_get_resources,
     125        &pciintel_enable_interrupt
    115126};
    116127
    117 static device_ops_t pci_child_ops;
    118 
    119 static int pci_add_device(device_t *);
    120 
    121 /** The pci bus driver's standard operations. */
     128static ddf_dev_ops_t pci_fun_ops;
     129
     130static int pci_add_device(ddf_dev_t *);
     131
     132/** PCI bus driver standard operations */
    122133static driver_ops_t pci_ops = {
    123134        .add_device = &pci_add_device
    124135};
    125136
    126 /** The pci bus driver structure. */
     137/** PCI bus driver structure */
    127138static driver_t pci_driver = {
    128139        .name = NAME,
     
    130141};
    131142
    132 typedef struct pciintel_bus_data {
    133         uint32_t conf_io_addr;
    134         void *conf_data_port;
    135         void *conf_addr_port;
    136         fibril_mutex_t conf_mutex;
    137 } pci_bus_data_t;
    138 
    139 static pci_bus_data_t *create_pci_bus_data(void)
    140 {
    141         pci_bus_data_t *bus_data;
    142        
    143         bus_data = (pci_bus_data_t *) malloc(sizeof(pci_bus_data_t));
    144         if (bus_data != NULL) {
    145                 memset(bus_data, 0, sizeof(pci_bus_data_t));
    146                 fibril_mutex_initialize(&bus_data->conf_mutex);
    147         }
    148 
    149         return bus_data;
    150 }
    151 
    152 static void delete_pci_bus_data(pci_bus_data_t *bus_data)
    153 {
    154         free(bus_data);
    155 }
    156 
    157 static void pci_conf_read(device_t *dev, int reg, uint8_t *buf, size_t len)
    158 {
    159         assert(dev->parent != NULL);
    160        
    161         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
    162         pci_bus_data_t *bus_data = (pci_bus_data_t *) dev->parent->driver_data;
    163        
    164         fibril_mutex_lock(&bus_data->conf_mutex);
     143static pci_bus_t *pci_bus_new(void)
     144{
     145        pci_bus_t *bus;
     146       
     147        bus = (pci_bus_t *) calloc(1, sizeof(pci_bus_t));
     148        if (bus == NULL)
     149                return NULL;
     150       
     151        fibril_mutex_initialize(&bus->conf_mutex);
     152        return bus;
     153}
     154
     155static void pci_bus_delete(pci_bus_t *bus)
     156{
     157        assert(bus != NULL);
     158        free(bus);
     159}
     160
     161static void pci_conf_read(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
     162{
     163        pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
     164       
     165        fibril_mutex_lock(&bus->conf_mutex);
    165166       
    166167        uint32_t conf_addr;
    167         conf_addr = CONF_ADDR(dev_data->bus, dev_data->dev, dev_data->fn, reg);
    168         void *addr = bus_data->conf_data_port + (reg & 3);
    169        
    170         pio_write_32(bus_data->conf_addr_port, conf_addr);
     168        conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
     169        void *addr = bus->conf_data_port + (reg & 3);
     170       
     171        pio_write_32(bus->conf_addr_port, conf_addr);
    171172       
    172173        switch (len) {
     
    182183        }
    183184       
    184         fibril_mutex_unlock(&bus_data->conf_mutex);
    185 }
    186 
    187 static void pci_conf_write(device_t *dev, int reg, uint8_t *buf, size_t len)
    188 {
    189         assert(dev->parent != NULL);
    190        
    191         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
    192         pci_bus_data_t *bus_data = (pci_bus_data_t *) dev->parent->driver_data;
    193        
    194         fibril_mutex_lock(&bus_data->conf_mutex);
     185        fibril_mutex_unlock(&bus->conf_mutex);
     186}
     187
     188static void pci_conf_write(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
     189{
     190        pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
     191       
     192        fibril_mutex_lock(&bus->conf_mutex);
    195193       
    196194        uint32_t conf_addr;
    197         conf_addr = CONF_ADDR(dev_data->bus, dev_data->dev, dev_data->fn, reg);
    198         void *addr = bus_data->conf_data_port + (reg & 3);
    199        
    200         pio_write_32(bus_data->conf_addr_port, conf_addr);
     195        conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
     196        void *addr = bus->conf_data_port + (reg & 3);
     197       
     198        pio_write_32(bus->conf_addr_port, conf_addr);
    201199       
    202200        switch (len) {
     
    212210        }
    213211       
    214         fibril_mutex_unlock(&bus_data->conf_mutex);
    215 }
    216 
    217 uint8_t pci_conf_read_8(device_t *dev, int reg)
     212        fibril_mutex_unlock(&bus->conf_mutex);
     213}
     214
     215uint8_t pci_conf_read_8(pci_fun_t *fun, int reg)
    218216{
    219217        uint8_t res;
    220         pci_conf_read(dev, reg, &res, 1);
     218        pci_conf_read(fun, reg, &res, 1);
    221219        return res;
    222220}
    223221
    224 uint16_t pci_conf_read_16(device_t *dev, int reg)
     222uint16_t pci_conf_read_16(pci_fun_t *fun, int reg)
    225223{
    226224        uint16_t res;
    227         pci_conf_read(dev, reg, (uint8_t *) &res, 2);
     225        pci_conf_read(fun, reg, (uint8_t *) &res, 2);
    228226        return res;
    229227}
    230228
    231 uint32_t pci_conf_read_32(device_t *dev, int reg)
     229uint32_t pci_conf_read_32(pci_fun_t *fun, int reg)
    232230{
    233231        uint32_t res;
    234         pci_conf_read(dev, reg, (uint8_t *) &res, 4);
     232        pci_conf_read(fun, reg, (uint8_t *) &res, 4);
    235233        return res;
    236234}
    237235
    238 void pci_conf_write_8(device_t *dev, int reg, uint8_t val)
    239 {
    240         pci_conf_write(dev, reg, (uint8_t *) &val, 1);
    241 }
    242 
    243 void pci_conf_write_16(device_t *dev, int reg, uint16_t val)
    244 {
    245         pci_conf_write(dev, reg, (uint8_t *) &val, 2);
    246 }
    247 
    248 void pci_conf_write_32(device_t *dev, int reg, uint32_t val)
    249 {
    250         pci_conf_write(dev, reg, (uint8_t *) &val, 4);
    251 }
    252 
    253 void create_pci_match_ids(device_t *dev)
    254 {
    255         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
    256         match_id_t *match_id = NULL;
     236void pci_conf_write_8(pci_fun_t *fun, int reg, uint8_t val)
     237{
     238        pci_conf_write(fun, reg, (uint8_t *) &val, 1);
     239}
     240
     241void pci_conf_write_16(pci_fun_t *fun, int reg, uint16_t val)
     242{
     243        pci_conf_write(fun, reg, (uint8_t *) &val, 2);
     244}
     245
     246void pci_conf_write_32(pci_fun_t *fun, int reg, uint32_t val)
     247{
     248        pci_conf_write(fun, reg, (uint8_t *) &val, 4);
     249}
     250
     251void pci_fun_create_match_ids(pci_fun_t *fun)
     252{
    257253        char *match_id_str;
    258        
    259         match_id = create_match_id();
    260         if (match_id != NULL) {
    261                 asprintf(&match_id_str, "pci/ven=%04x&dev=%04x",
    262                     dev_data->vendor_id, dev_data->device_id);
    263                 match_id->id = match_id_str;
    264                 match_id->score = 90;
    265                 add_match_id(&dev->match_ids, match_id);
    266         }
    267 
     254        int rc;
     255       
     256        asprintf(&match_id_str, "pci/ven=%04x&dev=%04x",
     257            fun->vendor_id, fun->device_id);
     258
     259        if (match_id_str == NULL) {
     260                printf(NAME ": out of memory creating match ID.\n");
     261                return;
     262        }
     263
     264        rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
     265        if (rc != EOK) {
     266                printf(NAME ": error adding match ID: %s\n",
     267                    str_error(rc));
     268        }
     269       
    268270        /* TODO add more ids (with subsys ids, using class id etc.) */
    269271}
    270272
    271 void
    272 pci_add_range(device_t *dev, uint64_t range_addr, size_t range_size, bool io)
    273 {
    274         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
    275         hw_resource_list_t *hw_res_list = &dev_data->hw_resources;
     273void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
     274    bool io)
     275{
     276        hw_resource_list_t *hw_res_list = &fun->hw_resources;
    276277        hw_resource_t *hw_resources =  hw_res_list->resources;
    277278        size_t count = hw_res_list->count;
     
    298299 * address add it to the devices hw resource list.
    299300 *
    300  * @param dev   The pci device.
     301 * @param fun   PCI function
    301302 * @param addr  The address of the BAR in the PCI configuration address space of
    302  *              the device.
    303  * @return      The addr the address of the BAR which should be read next.
     303 *              the device
     304 * @return      The addr the address of the BAR which should be read next
    304305 */
    305 int pci_read_bar(device_t *dev, int addr)
    306 {       
     306int pci_read_bar(pci_fun_t *fun, int addr)
     307{
    307308        /* Value of the BAR */
    308309        uint32_t val, mask;
     
    318319       
    319320        /* Get the value of the BAR. */
    320         val = pci_conf_read_32(dev, addr);
     321        val = pci_conf_read_32(fun, addr);
    321322       
    322323        io = (bool) (val & 1);
     
    338339       
    339340        /* Get the address mask. */
    340         pci_conf_write_32(dev, addr, 0xffffffff);
    341         mask = pci_conf_read_32(dev, addr);
     341        pci_conf_write_32(fun, addr, 0xffffffff);
     342        mask = pci_conf_read_32(fun, addr);
    342343       
    343344        /* Restore the original value. */
    344         pci_conf_write_32(dev, addr, val);
    345         val = pci_conf_read_32(dev, addr);
     345        pci_conf_write_32(fun, addr, val);
     346        val = pci_conf_read_32(fun, addr);
    346347       
    347348        range_size = pci_bar_mask_to_size(mask);
    348349       
    349350        if (addrw64) {
    350                 range_addr = ((uint64_t)pci_conf_read_32(dev, addr + 4) << 32) |
     351                range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
    351352                    (val & 0xfffffff0);
    352353        } else {
     
    355356       
    356357        if (range_addr != 0) {
    357                 printf(NAME ": device %s : ", dev->name);
     358                printf(NAME ": function %s : ", fun->fnode->name);
    358359                printf("address = %" PRIx64, range_addr);
    359360                printf(", size = %x\n", (unsigned int) range_size);
    360361        }
    361362       
    362         pci_add_range(dev, range_addr, range_size, io);
     363        pci_add_range(fun, range_addr, range_size, io);
    363364       
    364365        if (addrw64)
     
    368369}
    369370
    370 void pci_add_interrupt(device_t *dev, int irq)
    371 {
    372         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
    373         hw_resource_list_t *hw_res_list = &dev_data->hw_resources;
     371void pci_add_interrupt(pci_fun_t *fun, int irq)
     372{
     373        hw_resource_list_t *hw_res_list = &fun->hw_resources;
    374374        hw_resource_t *hw_resources = hw_res_list->resources;
    375375        size_t count = hw_res_list->count;
     
    383383        hw_res_list->count++;
    384384       
    385         printf(NAME ": device %s uses irq %x.\n", dev->name, irq);
    386 }
    387 
    388 void pci_read_interrupt(device_t *dev)
    389 {
    390         uint8_t irq = pci_conf_read_8(dev, PCI_BRIDGE_INT_LINE);
     385        printf(NAME ": function %s uses irq %x.\n", fun->fnode->name, irq);
     386}
     387
     388void pci_read_interrupt(pci_fun_t *fun)
     389{
     390        uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
    391391        if (irq != 0xff)
    392                 pci_add_interrupt(dev, irq);
     392                pci_add_interrupt(fun, irq);
    393393}
    394394
    395395/** Enumerate (recursively) and register the devices connected to a pci bus.
    396396 *
    397  * @param parent        The host-to-pci bridge device.
    398  * @param bus_num       The bus number.
     397 * @param bus           Host-to-PCI bridge
     398 * @param bus_num       Bus number
    399399 */
    400 void pci_bus_scan(device_t *parent, int bus_num)
    401 {
    402         device_t *dev = create_device();
    403         pci_dev_data_t *dev_data = create_pci_dev_data();
    404         dev->driver_data = dev_data;
    405         dev->parent = parent;
     400void pci_bus_scan(pci_bus_t *bus, int bus_num)
     401{
     402        ddf_fun_t *fnode;
     403        pci_fun_t *fun;
    406404       
    407405        int child_bus = 0;
    408406        int dnum, fnum;
    409407        bool multi;
    410         uint8_t header_type;
     408        uint8_t header_type;
     409       
     410        fun = pci_fun_new(bus);
    411411       
    412412        for (dnum = 0; dnum < 32; dnum++) {
    413413                multi = true;
    414414                for (fnum = 0; multi && fnum < 8; fnum++) {
    415                         init_pci_dev_data(dev_data, bus_num, dnum, fnum);
    416                         dev_data->vendor_id = pci_conf_read_16(dev,
     415                        pci_fun_init(fun, bus_num, dnum, fnum);
     416                        fun->vendor_id = pci_conf_read_16(fun,
    417417                            PCI_VENDOR_ID);
    418                         dev_data->device_id = pci_conf_read_16(dev,
     418                        fun->device_id = pci_conf_read_16(fun,
    419419                            PCI_DEVICE_ID);
    420                         if (dev_data->vendor_id == 0xffff) {
     420                        if (fun->vendor_id == 0xffff) {
    421421                                /*
    422422                                 * The device is not present, go on scanning the
     
    429429                        }
    430430                       
    431                         header_type = pci_conf_read_8(dev, PCI_HEADER_TYPE);
     431                        header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
    432432                        if (fnum == 0) {
    433433                                /* Is the device multifunction? */
     
    437437                        header_type = header_type & 0x7F;
    438438                       
    439                         create_pci_dev_name(dev);
    440                        
    441                         pci_alloc_resource_list(dev);
    442                         pci_read_bars(dev);
    443                         pci_read_interrupt(dev);
    444                        
    445                         dev->ops = &pci_child_ops;
    446                        
    447                         printf(NAME ": adding new child device %s.\n",
    448                             dev->name);
    449                        
    450                         create_pci_match_ids(dev);
    451                        
    452                         if (child_device_register(dev, parent) != EOK) {
    453                                 pci_clean_resource_list(dev);
    454                                 clean_match_ids(&dev->match_ids);
    455                                 free((char *) dev->name);
    456                                 dev->name = NULL;
     439                        char *fun_name = pci_fun_create_name(fun);
     440                        if (fun_name == NULL) {
     441                                printf(NAME ": out of memory.\n");
     442                                return;
     443                        }
     444                       
     445                        fnode = ddf_fun_create(bus->dnode, fun_inner, fun_name);
     446                        if (fnode == NULL) {
     447                                printf(NAME ": error creating function.\n");
     448                                return;
     449                        }
     450                       
     451                        free(fun_name);
     452                        fun->fnode = fnode;
     453                       
     454                        pci_alloc_resource_list(fun);
     455                        pci_read_bars(fun);
     456                        pci_read_interrupt(fun);
     457                       
     458                        fnode->ops = &pci_fun_ops;
     459                        fnode->driver_data = fun;
     460                       
     461                        printf(NAME ": adding new function %s.\n",
     462                            fnode->name);
     463                       
     464                        pci_fun_create_match_ids(fun);
     465                       
     466                        if (ddf_fun_bind(fnode) != EOK) {
     467                                pci_clean_resource_list(fun);
     468                                clean_match_ids(&fnode->match_ids);
     469                                free((char *) fnode->name);
     470                                fnode->name = NULL;
    457471                                continue;
    458472                        }
     
    460474                        if (header_type == PCI_HEADER_TYPE_BRIDGE ||
    461475                            header_type == PCI_HEADER_TYPE_CARDBUS) {
    462                                 child_bus = pci_conf_read_8(dev,
     476                                child_bus = pci_conf_read_8(fun,
    463477                                    PCI_BRIDGE_SEC_BUS_NUM);
    464478                                printf(NAME ": device is pci-to-pci bridge, "
    465479                                    "secondary bus number = %d.\n", bus_num);
    466480                                if (child_bus > bus_num)
    467                                         pci_bus_scan(parent, child_bus);
     481                                        pci_bus_scan(bus, child_bus);
    468482                        }
    469483                       
    470                         /* Alloc new aux. dev. structure. */
    471                         dev = create_device();
    472                         dev_data = create_pci_dev_data();
    473                         dev->driver_data = dev_data;
    474                         dev->parent = parent;
     484                        fun = pci_fun_new(bus);
    475485                }
    476486        }
    477487       
    478         if (dev_data->vendor_id == 0xffff) {
    479                 delete_device(dev);
    480                 /* Free the auxiliary device structure. */
    481                 delete_pci_dev_data(dev_data);
    482         }
    483 }
    484 
    485 static int pci_add_device(device_t *dev)
    486 {
     488        if (fun->vendor_id == 0xffff) {
     489                /* Free the auxiliary function structure. */
     490                pci_fun_delete(fun);
     491        }
     492}
     493
     494static int pci_add_device(ddf_dev_t *dnode)
     495{
     496        pci_bus_t *bus = NULL;
     497        ddf_fun_t *ctl = NULL;
     498        bool got_res = false;
    487499        int rc;
    488 
     500       
    489501        printf(NAME ": pci_add_device\n");
    490        
    491         pci_bus_data_t *bus_data = create_pci_bus_data();
    492         if (bus_data == NULL) {
     502        dnode->parent_phone = -1;
     503       
     504        bus = pci_bus_new();
     505        if (bus == NULL) {
    493506                printf(NAME ": pci_add_device allocation failed.\n");
    494                 return ENOMEM;
    495         }
    496        
    497         dev->parent_phone = devman_parent_device_connect(dev->handle,
     507                rc = ENOMEM;
     508                goto fail;
     509        }
     510        bus->dnode = dnode;
     511        dnode->driver_data = bus;
     512       
     513        dnode->parent_phone = devman_parent_device_connect(dnode->handle,
    498514            IPC_FLAG_BLOCKING);
    499         if (dev->parent_phone < 0) {
     515        if (dnode->parent_phone < 0) {
    500516                printf(NAME ": pci_add_device failed to connect to the "
    501517                    "parent's driver.\n");
    502                 delete_pci_bus_data(bus_data);
    503                 return dev->parent_phone;
     518                rc = dnode->parent_phone;
     519                goto fail;
    504520        }
    505521       
    506522        hw_resource_list_t hw_resources;
    507523       
    508         rc = hw_res_get_resource_list(dev->parent_phone, &hw_resources);
     524        rc = hw_res_get_resource_list(dnode->parent_phone, &hw_resources);
    509525        if (rc != EOK) {
    510526                printf(NAME ": pci_add_device failed to get hw resources for "
    511527                    "the device.\n");
    512                 delete_pci_bus_data(bus_data);
    513                 async_hangup(dev->parent_phone);
    514                 return rc;
    515         }       
     528                goto fail;
     529        }
     530        got_res = true;
    516531       
    517532        printf(NAME ": conf_addr = %" PRIx64 ".\n",
     
    522537        assert(hw_resources.resources[0].res.io_range.size == 8);
    523538       
    524         bus_data->conf_io_addr =
     539        bus->conf_io_addr =
    525540            (uint32_t) hw_resources.resources[0].res.io_range.address;
    526541       
    527         if (pio_enable((void *)(uintptr_t)bus_data->conf_io_addr, 8,
    528             &bus_data->conf_addr_port)) {
     542        if (pio_enable((void *)(uintptr_t)bus->conf_io_addr, 8,
     543            &bus->conf_addr_port)) {
    529544                printf(NAME ": failed to enable configuration ports.\n");
    530                 delete_pci_bus_data(bus_data);
    531                 async_hangup(dev->parent_phone);
     545                rc = EADDRNOTAVAIL;
     546                goto fail;
     547        }
     548        bus->conf_data_port = (char *) bus->conf_addr_port + 4;
     549       
     550        /* Make the bus device more visible. It has no use yet. */
     551        printf(NAME ": adding a 'ctl' function\n");
     552       
     553        ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
     554        if (ctl == NULL) {
     555                printf(NAME ": error creating control function.\n");
     556                rc = ENOMEM;
     557                goto fail;
     558        }
     559       
     560        rc = ddf_fun_bind(ctl);
     561        if (rc != EOK) {
     562                printf(NAME ": error binding control function.\n");
     563                goto fail;
     564        }
     565       
     566        /* Enumerate functions. */
     567        printf(NAME ": scanning the bus\n");
     568        pci_bus_scan(bus, 0);
     569       
     570        hw_res_clean_resource_list(&hw_resources);
     571       
     572        return EOK;
     573       
     574fail:
     575        if (bus != NULL)
     576                pci_bus_delete(bus);
     577        if (dnode->parent_phone >= 0)
     578                async_hangup(dnode->parent_phone);
     579        if (got_res)
    532580                hw_res_clean_resource_list(&hw_resources);
    533                 return EADDRNOTAVAIL;
    534         }
    535         bus_data->conf_data_port = (char *) bus_data->conf_addr_port + 4;
    536        
    537         dev->driver_data = bus_data;
    538        
    539         /* Enumerate child devices. */
    540         printf(NAME ": scanning the bus\n");
    541         pci_bus_scan(dev, 0);
    542        
    543         hw_res_clean_resource_list(&hw_resources);
    544        
    545         return EOK;
     581        if (ctl != NULL)
     582                ddf_fun_destroy(ctl);
     583
     584        return rc;
    546585}
    547586
    548587static void pciintel_init(void)
    549588{
    550         pci_child_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_child_hw_res_ops;
    551 }
    552 
    553 pci_dev_data_t *create_pci_dev_data(void)
    554 {
    555         pci_dev_data_t *res = (pci_dev_data_t *) malloc(sizeof(pci_dev_data_t));
    556        
    557         if (res != NULL)
    558                 memset(res, 0, sizeof(pci_dev_data_t));
    559         return res;
    560 }
    561 
    562 void init_pci_dev_data(pci_dev_data_t *dev_data, int bus, int dev, int fn)
    563 {
    564         dev_data->bus = bus;
    565         dev_data->dev = dev;
    566         dev_data->fn = fn;
    567 }
    568 
    569 void delete_pci_dev_data(pci_dev_data_t *dev_data)
    570 {
    571         if (dev_data != NULL) {
    572                 hw_res_clean_resource_list(&dev_data->hw_resources);
    573                 free(dev_data);
    574         }
    575 }
    576 
    577 void create_pci_dev_name(device_t *dev)
    578 {
    579         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     589        pci_fun_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops;
     590}
     591
     592pci_fun_t *pci_fun_new(pci_bus_t *bus)
     593{
     594        pci_fun_t *fun;
     595       
     596        fun = (pci_fun_t *) calloc(1, sizeof(pci_fun_t));
     597        if (fun == NULL)
     598                return NULL;
     599
     600        fun->busptr = bus;
     601        return fun;
     602}
     603
     604void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
     605{
     606        fun->bus = bus;
     607        fun->dev = dev;
     608        fun->fn = fn;
     609}
     610
     611void pci_fun_delete(pci_fun_t *fun)
     612{
     613        assert(fun != NULL);
     614        hw_res_clean_resource_list(&fun->hw_resources);
     615        free(fun);
     616}
     617
     618char *pci_fun_create_name(pci_fun_t *fun)
     619{
    580620        char *name = NULL;
    581621       
    582         asprintf(&name, "%02x:%02x.%01x", dev_data->bus, dev_data->dev,
    583             dev_data->fn);
    584         dev->name = name;
    585 }
    586 
    587 bool pci_alloc_resource_list(device_t *dev)
    588 {
    589         pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
    590        
    591         dev_data->hw_resources.resources =
     622        asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
     623            fun->fn);
     624        return name;
     625}
     626
     627bool pci_alloc_resource_list(pci_fun_t *fun)
     628{
     629        fun->hw_resources.resources =
    592630            (hw_resource_t *) malloc(PCI_MAX_HW_RES * sizeof(hw_resource_t));
    593         return dev_data->hw_resources.resources != NULL;
    594 }
    595 
    596 void pci_clean_resource_list(device_t *dev)
    597 {
    598         pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
    599        
    600         if (dev_data->hw_resources.resources != NULL) {
    601                 free(dev_data->hw_resources.resources);
    602                 dev_data->hw_resources.resources = NULL;
    603         }
    604 }
    605 
    606 /** Read the base address registers (BARs) of the device and adds the addresses
    607  * to its hw resource list.
     631        return fun->hw_resources.resources != NULL;
     632}
     633
     634void pci_clean_resource_list(pci_fun_t *fun)
     635{
     636        if (fun->hw_resources.resources != NULL) {
     637                free(fun->hw_resources.resources);
     638                fun->hw_resources.resources = NULL;
     639        }
     640}
     641
     642/** Read the base address registers (BARs) of the function and add the addresses
     643 * to its HW resource list.
    608644 *
    609  * @param dev the pci device.
     645 * @param fun   PCI function
    610646 */
    611 void pci_read_bars(device_t *dev)
     647void pci_read_bars(pci_fun_t *fun)
    612648{
    613649        /*
     
    618654       
    619655        while (addr <= PCI_BASE_ADDR_5)
    620                 addr = pci_read_bar(dev, addr);
     656                addr = pci_read_bar(fun, addr);
    621657}
    622658
     
    630666        printf(NAME ": HelenOS pci bus driver (intel method 1).\n");
    631667        pciintel_init();
    632         return driver_main(&pci_driver);
     668        return ddf_driver_main(&pci_driver);
    633669}
    634670
  • uspace/drv/pciintel/pci.h

    rdbe25f1 reb1a2f4  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
     3 * Copyright (c) 2011 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    3637#define PCI_H_
    3738
    38 #include <stdlib.h>
    39 #include <driver.h>
    40 #include <malloc.h>
    41 
     39#include <ddf/driver.h>
    4240#include "pci_regs.h"
    4341
    4442#define PCI_MAX_HW_RES 8
    4543
    46 typedef struct pci_dev_data {
     44typedef struct pciintel_bus {
     45        /** DDF device node */
     46        ddf_dev_t *dnode;
     47        uint32_t conf_io_addr;
     48        void *conf_data_port;
     49        void *conf_addr_port;
     50        fibril_mutex_t conf_mutex;
     51} pci_bus_t;
     52
     53typedef struct pci_fun_data {
     54        pci_bus_t *busptr;
     55        ddf_fun_t *fnode;
     56
    4757        int bus;
    4858        int dev;
     
    5161        int device_id;
    5262        hw_resource_list_t hw_resources;
    53 } pci_dev_data_t;
     63} pci_fun_t;
    5464
    55 extern void create_pci_match_ids(device_t *);
     65extern void pci_fun_create_match_ids(pci_fun_t *);
    5666
    57 extern uint8_t pci_conf_read_8(device_t *, int);
    58 extern uint16_t pci_conf_read_16(device_t *, int);
    59 extern uint32_t pci_conf_read_32(device_t *, int);
    60 extern void pci_conf_write_8(device_t *, int, uint8_t);
    61 extern void pci_conf_write_16(device_t *, int, uint16_t);
    62 extern void pci_conf_write_32(device_t *, int, uint32_t);
     67extern uint8_t pci_conf_read_8(pci_fun_t *, int);
     68extern uint16_t pci_conf_read_16(pci_fun_t *, int);
     69extern uint32_t pci_conf_read_32(pci_fun_t *, int);
     70extern void pci_conf_write_8(pci_fun_t *, int, uint8_t);
     71extern void pci_conf_write_16(pci_fun_t *, int, uint16_t);
     72extern void pci_conf_write_32(pci_fun_t *, int, uint32_t);
    6373
    64 extern void pci_add_range(device_t *, uint64_t, size_t, bool);
    65 extern int pci_read_bar(device_t *, int);
    66 extern void pci_read_interrupt(device_t *);
    67 extern void pci_add_interrupt(device_t *, int);
     74extern void pci_add_range(pci_fun_t *, uint64_t, size_t, bool);
     75extern int pci_read_bar(pci_fun_t *, int);
     76extern void pci_read_interrupt(pci_fun_t *);
     77extern void pci_add_interrupt(pci_fun_t *, int);
    6878
    69 extern void pci_bus_scan(device_t *, int);
     79extern pci_fun_t *pci_fun_new(pci_bus_t *);
     80extern void pci_fun_init(pci_fun_t *, int, int, int);
     81extern void pci_fun_delete(pci_fun_t *);
     82extern char *pci_fun_create_name(pci_fun_t *);
    7083
    71 extern pci_dev_data_t *create_pci_dev_data(void);
    72 extern void init_pci_dev_data(pci_dev_data_t *, int, int, int);
    73 extern void delete_pci_dev_data(pci_dev_data_t *);
    74 extern void create_pci_dev_name(device_t *);
     84extern void pci_bus_scan(pci_bus_t *, int);
    7585
    76 extern bool pci_alloc_resource_list(device_t *);
    77 extern void pci_clean_resource_list(device_t *);
     86extern bool pci_alloc_resource_list(pci_fun_t *);
     87extern void pci_clean_resource_list(pci_fun_t *);
    7888
    79 extern void pci_read_bars(device_t *);
     89extern void pci_read_bars(pci_fun_t *);
    8090extern size_t pci_bar_mask_to_size(uint32_t);
    8191
  • uspace/drv/root/root.c

    rdbe25f1 reb1a2f4  
    22 * Copyright (c) 2010 Lenka Trochtova
    33 * Copyright (c) 2010 Vojtech Horky
     4 * Copyright (c) 2011 Jiri Svoboda
    45 * All rights reserved.
    56 *
     
    4445#include <stdlib.h>
    4546#include <str.h>
     47#include <str_error.h>
    4648#include <ctype.h>
    4749#include <macros.h>
     
    4951#include <sysinfo.h>
    5052
    51 #include <driver.h>
     53#include <ddf/driver.h>
    5254#include <devman.h>
    5355#include <ipc/devman.h>
     
    5557#define NAME "root"
    5658
    57 #define PLATFORM_DEVICE_NAME "hw"
    58 #define PLATFORM_DEVICE_MATCH_ID_FMT "platform/%s"
    59 #define PLATFORM_DEVICE_MATCH_SCORE 100
    60 
    61 #define VIRTUAL_DEVICE_NAME "virt"
    62 #define VIRTUAL_DEVICE_MATCH_ID "rootvirt"
    63 #define VIRTUAL_DEVICE_MATCH_SCORE 100
    64 
    65 static int root_add_device(device_t *dev);
     59#define PLATFORM_FUN_NAME "hw"
     60#define PLATFORM_FUN_MATCH_ID_FMT "platform/%s"
     61#define PLATFORM_FUN_MATCH_SCORE 100
     62
     63#define VIRTUAL_FUN_NAME "virt"
     64#define VIRTUAL_FUN_MATCH_ID "rootvirt"
     65#define VIRTUAL_FUN_MATCH_SCORE 100
     66
     67static int root_add_device(ddf_dev_t *dev);
    6668
    6769/** The root device driver's standard operations. */
     
    7678};
    7779
    78 /** Create the device which represents the root of virtual device tree.
    79  *
    80  * @param parent Parent of the newly created device.
    81  * @return Error code.
    82  */
    83 static int add_virtual_root_child(device_t *parent)
    84 {
    85         printf(NAME ": adding new child for virtual devices.\n");
    86         printf(NAME ":   device node is `%s' (%d %s)\n", VIRTUAL_DEVICE_NAME,
    87             VIRTUAL_DEVICE_MATCH_SCORE, VIRTUAL_DEVICE_MATCH_ID);
    88 
    89         int res = child_device_register_wrapper(parent, VIRTUAL_DEVICE_NAME,
    90             VIRTUAL_DEVICE_MATCH_ID, VIRTUAL_DEVICE_MATCH_SCORE,
    91             NULL);
    92 
    93         return res;
    94 }
    95 
    96 /** Create the device which represents the root of HW device tree.
    97  *
    98  * @param parent        Parent of the newly created device.
    99  * @return 0 on success, negative error number otherwise.
    100  */
    101 static int add_platform_child(device_t *parent)
     80/** Create the function which represents the root of virtual device tree.
     81 *
     82 * @param dev   Device
     83 * @return      EOK on success or negative error code
     84 */
     85static int add_virtual_root_fun(ddf_dev_t *dev)
     86{
     87        const char *name = VIRTUAL_FUN_NAME;
     88        ddf_fun_t *fun;
     89        int rc;
     90
     91        printf(NAME ": adding new function for virtual devices.\n");
     92        printf(NAME ":   function node is `%s' (%d %s)\n", name,
     93            VIRTUAL_FUN_MATCH_SCORE, VIRTUAL_FUN_MATCH_ID);
     94
     95        fun = ddf_fun_create(dev, fun_inner, name);
     96        if (fun == NULL) {
     97                printf(NAME ": error creating function %s\n", name);
     98                return ENOMEM;
     99        }
     100
     101        rc = ddf_fun_add_match_id(fun, VIRTUAL_FUN_MATCH_ID,
     102            VIRTUAL_FUN_MATCH_SCORE);
     103        if (rc != EOK) {
     104                printf(NAME ": error adding match IDs to function %s\n", name);
     105                ddf_fun_destroy(fun);
     106                return rc;
     107        }
     108
     109        rc = ddf_fun_bind(fun);
     110        if (rc != EOK) {
     111                printf(NAME ": error binding function %s: %s\n", name,
     112                    str_error(rc));
     113                ddf_fun_destroy(fun);
     114                return rc;
     115        }
     116
     117        return EOK;
     118}
     119
     120/** Create the function which represents the root of HW device tree.
     121 *
     122 * @param dev   Device
     123 * @return      EOK on success or negative error code
     124 */
     125static int add_platform_fun(ddf_dev_t *dev)
    102126{
    103127        char *match_id;
    104128        char *platform;
    105129        size_t platform_size;
    106         int res;
     130
     131        const char *name = PLATFORM_FUN_NAME;
     132        ddf_fun_t *fun;
     133        int rc;
    107134
    108135        /* Get platform name from sysinfo. */
    109 
    110136        platform = sysinfo_get_data("platform", &platform_size);
    111137        if (platform == NULL) {
     
    124150
    125151        /* Construct match ID. */
    126 
    127         if (asprintf(&match_id, PLATFORM_DEVICE_MATCH_ID_FMT, platform) == -1) {
     152        if (asprintf(&match_id, PLATFORM_FUN_MATCH_ID_FMT, platform) == -1) {
    128153                printf(NAME ": Memory allocation failed.\n");
    129154                return ENOMEM;
    130155        }
    131156
    132         /* Add child. */
    133 
    134         printf(NAME ": adding new child for platform device.\n");
    135         printf(NAME ":   device node is `%s' (%d %s)\n", PLATFORM_DEVICE_NAME,
    136             PLATFORM_DEVICE_MATCH_SCORE, match_id);
    137 
    138         res = child_device_register_wrapper(parent, PLATFORM_DEVICE_NAME,
    139             match_id, PLATFORM_DEVICE_MATCH_SCORE, NULL);
    140 
    141         return res;
     157        /* Add function. */
     158        printf(NAME ": adding platform function\n");
     159        printf(NAME ":   function node is `%s' (%d %s)\n", PLATFORM_FUN_NAME,
     160            PLATFORM_FUN_MATCH_SCORE, match_id);
     161
     162        fun = ddf_fun_create(dev, fun_inner, name);
     163        if (fun == NULL) {
     164                printf(NAME ": error creating function %s\n", name);
     165                return ENOMEM;
     166        }
     167
     168        rc = ddf_fun_add_match_id(fun, match_id, PLATFORM_FUN_MATCH_SCORE);
     169        if (rc != EOK) {
     170                printf(NAME ": error adding match IDs to function %s\n", name);
     171                ddf_fun_destroy(fun);
     172                return rc;
     173        }
     174
     175        rc = ddf_fun_bind(fun);
     176        if (rc != EOK) {
     177                printf(NAME ": error binding function %s: %s\n", name,
     178                    str_error(rc));
     179                ddf_fun_destroy(fun);
     180                return rc;
     181        }
     182
     183        return EOK;
    142184}
    143185
     
    147189 *                      of HW and pseudo devices).
    148190 */
    149 static int root_add_device(device_t *dev)
     191static int root_add_device(ddf_dev_t *dev)
    150192{
    151193        printf(NAME ": root_add_device, device handle=%" PRIun "\n",
    152194            dev->handle);
    153        
     195
    154196        /*
    155197         * Register virtual devices root.
     
    157199         * vital for the system.
    158200         */
    159         add_virtual_root_child(dev);
     201        add_virtual_root_fun(dev);
    160202
    161203        /* Register root device's children. */
    162         int res = add_platform_child(dev);
     204        int res = add_platform_fun(dev);
    163205        if (EOK != res)
    164206                printf(NAME ": failed to add child device for platform.\n");
    165        
     207
    166208        return res;
    167209}
     
    170212{
    171213        printf(NAME ": HelenOS root device driver\n");
    172         return driver_main(&root_driver);
     214        return ddf_driver_main(&root_driver);
    173215}
    174216
  • uspace/drv/rootpc/rootpc.c

    rdbe25f1 reb1a2f4  
    4646#include <macros.h>
    4747
    48 #include <driver.h>
     48#include <ddf/driver.h>
    4949#include <devman.h>
    5050#include <ipc/devman.h>
     
    5555#define NAME "rootpc"
    5656
    57 typedef struct rootpc_child_dev_data {
     57/** Obtain function soft-state from DDF function node */
     58#define ROOTPC_FUN(fnode) ((rootpc_fun_t *) (fnode)->driver_data)
     59
     60typedef struct rootpc_fun {
    5861        hw_resource_list_t hw_resources;
    59 } rootpc_child_dev_data_t;
    60 
    61 static int rootpc_add_device(device_t *dev);
     62} rootpc_fun_t;
     63
     64static int rootpc_add_device(ddf_dev_t *dev);
    6265static void root_pc_init(void);
    6366
     
    8285};
    8386
    84 static rootpc_child_dev_data_t pci_data = {
     87static rootpc_fun_t pci_data = {
    8588        .hw_resources = {
    8689                1,
     
    8992};
    9093
    91 static hw_resource_list_t *rootpc_get_child_resources(device_t *dev)
    92 {
    93         rootpc_child_dev_data_t *data;
    94        
    95         data = (rootpc_child_dev_data_t *) dev->driver_data;
    96         if (NULL == data)
    97                 return NULL;
    98        
    99         return &data->hw_resources;
    100 }
    101 
    102 static bool rootpc_enable_child_interrupt(device_t *dev)
     94static hw_resource_list_t *rootpc_get_resources(ddf_fun_t *fnode)
     95{
     96        rootpc_fun_t *fun = ROOTPC_FUN(fnode);
     97       
     98        assert(fun != NULL);
     99        return &fun->hw_resources;
     100}
     101
     102static bool rootpc_enable_interrupt(ddf_fun_t *fun)
    103103{
    104104        /* TODO */
     
    107107}
    108108
    109 static hw_res_ops_t child_hw_res_ops = {
    110         &rootpc_get_child_resources,
    111         &rootpc_enable_child_interrupt
     109static hw_res_ops_t fun_hw_res_ops = {
     110        &rootpc_get_resources,
     111        &rootpc_enable_interrupt
    112112};
    113113
    114114/* Initialized in root_pc_init() function. */
    115 static device_ops_t rootpc_child_ops;
     115static ddf_dev_ops_t rootpc_fun_ops;
    116116
    117117static bool
    118 rootpc_add_child(device_t *parent, const char *name, const char *str_match_id,
    119     rootpc_child_dev_data_t *drv_data)
    120 {
    121         printf(NAME ": adding new child device '%s'.\n", name);
    122        
    123         device_t *child = NULL;
     118rootpc_add_fun(ddf_dev_t *dev, const char *name, const char *str_match_id,
     119    rootpc_fun_t *fun)
     120{
     121        printf(NAME ": adding new function '%s'.\n", name);
     122       
     123        ddf_fun_t *fnode = NULL;
    124124        match_id_t *match_id = NULL;
    125125       
    126126        /* Create new device. */
    127         child = create_device();
    128         if (NULL == child)
     127        fnode = ddf_fun_create(dev, fun_inner, name);
     128        if (fnode == NULL)
    129129                goto failure;
    130130       
    131         child->name = name;
    132         child->driver_data = drv_data;
     131        fnode->driver_data = fun;
    133132       
    134133        /* Initialize match id list */
    135134        match_id = create_match_id();
    136         if (NULL == match_id)
     135        if (match_id == NULL)
    137136                goto failure;
    138137       
    139138        match_id->id = str_match_id;
    140139        match_id->score = 100;
    141         add_match_id(&child->match_ids, match_id);
     140        add_match_id(&fnode->match_ids, match_id);
    142141       
    143142        /* Set provided operations to the device. */
    144         child->ops = &rootpc_child_ops;
    145        
    146         /* Register child device. */
    147         if (EOK != child_device_register(child, parent))
     143        fnode->ops = &rootpc_fun_ops;
     144       
     145        /* Register function. */
     146        if (ddf_fun_bind(fnode) != EOK) {
     147                printf(NAME ": error binding function %s.\n", name);
    148148                goto failure;
     149        }
    149150       
    150151        return true;
    151152       
    152153failure:
    153         if (NULL != match_id)
     154        if (match_id != NULL)
    154155                match_id->id = NULL;
    155156       
    156         if (NULL != child) {
    157                 child->name = NULL;
    158                 delete_device(child);
    159         }
    160        
    161         printf(NAME ": failed to add child device '%s'.\n", name);
     157        if (fnode != NULL)
     158                ddf_fun_destroy(fnode);
     159       
     160        printf(NAME ": failed to add function '%s'.\n", name);
    162161       
    163162        return false;
    164163}
    165164
    166 static bool rootpc_add_children(device_t *dev)
    167 {
    168         return rootpc_add_child(dev, "pci0", "intel_pci", &pci_data);
     165static bool rootpc_add_functions(ddf_dev_t *dev)
     166{
     167        return rootpc_add_fun(dev, "pci0", "intel_pci", &pci_data);
    169168}
    170169
     
    175174 * @return              Zero on success, negative error number otherwise.
    176175 */
    177 static int rootpc_add_device(device_t *dev)
     176static int rootpc_add_device(ddf_dev_t *dev)
    178177{
    179178        printf(NAME ": rootpc_add_device, device handle = %d\n",
    180179            (int)dev->handle);
    181180       
    182         /* Register child devices. */
    183         if (!rootpc_add_children(dev)) {
    184                 printf(NAME ": failed to add child devices for PC platform.\n");
     181        /* Register functions. */
     182        if (!rootpc_add_functions(dev)) {
     183                printf(NAME ": failed to add functions for PC platform.\n");
    185184        }
    186185       
     
    190189static void root_pc_init(void)
    191190{
    192         rootpc_child_ops.interfaces[HW_RES_DEV_IFACE] = &child_hw_res_ops;
     191        rootpc_fun_ops.interfaces[HW_RES_DEV_IFACE] = &fun_hw_res_ops;
    193192}
    194193
     
    197196        printf(NAME ": HelenOS PC platform driver\n");
    198197        root_pc_init();
    199         return driver_main(&rootpc_driver);
     198        return ddf_driver_main(&rootpc_driver);
    200199}
    201200
  • uspace/drv/rootvirt/rootvirt.c

    rdbe25f1 reb1a2f4  
    3939#include <errno.h>
    4040#include <str_error.h>
    41 #include <driver.h>
     41#include <ddf/driver.h>
    4242
    4343#define NAME "rootvirt"
    4444
    45 /** Virtual device entry. */
     45/** Virtual function entry */
    4646typedef struct {
    47         /** Device name. */
     47        /** Function name */
    4848        const char *name;
    49         /** Device match id. */
     49        /** Function match ID */
    5050        const char *match_id;
    51 } virtual_device_t;
     51} virtual_function_t;
    5252
    53 /** List of existing virtual devices. */
    54 virtual_device_t virtual_devices[] = {
     53/** List of existing virtual functions */
     54virtual_function_t virtual_functions[] = {
    5555#include "devices.def"
    56         /* Terminating item. */
     56        /* Terminating item */
    5757        {
    5858                .name = NULL,
     
    6161};
    6262
    63 static int add_device(device_t *dev);
     63static int rootvirt_add_device(ddf_dev_t *dev);
    6464
    6565static driver_ops_t rootvirt_ops = {
    66         .add_device = &add_device
     66        .add_device = &rootvirt_add_device
    6767};
    6868
     
    7272};
    7373
    74 /** Add child device.
     74/** Add function to the virtual device.
    7575 *
    76  * @param parent Parent device.
    77  * @param virt_dev Virtual device to add.
    78  * @return Error code.
     76 * @param vdev          The virtual device
     77 * @param vfun          Virtual function description
     78 * @return              EOK on success or negative error code.
    7979 */
    80 static int add_child(device_t *parent, virtual_device_t *virt_dev)
     80static int rootvirt_add_fun(ddf_dev_t *vdev, virtual_function_t *vfun)
    8181{
    82         printf(NAME ": registering child device `%s' (match \"%s\")\n",
    83             virt_dev->name, virt_dev->match_id);
     82        ddf_fun_t *fun;
     83        int rc;
    8484
    85         int rc = child_device_register_wrapper(parent, virt_dev->name,
    86             virt_dev->match_id, 10, NULL);
     85        printf(NAME ": registering function `%s' (match \"%s\")\n",
     86            vfun->name, vfun->match_id);
    8787
    88         if (rc == EOK) {
    89                 printf(NAME ": registered child device `%s'\n",
    90                     virt_dev->name);
    91         } else {
    92                 printf(NAME ": failed to register child device `%s': %s\n",
    93                     virt_dev->name, str_error(rc));
     88        fun = ddf_fun_create(vdev, fun_inner, vfun->name);
     89        if (fun == NULL) {
     90                printf(NAME ": error creating function %s\n", vfun->name);
     91                return ENOMEM;
    9492        }
    9593
    96         return rc;
     94        rc = ddf_fun_add_match_id(fun, vfun->match_id, 10);
     95        if (rc != EOK) {
     96                printf(NAME ": error adding match IDs to function %s\n",
     97                    vfun->name);
     98                ddf_fun_destroy(fun);
     99                return rc;
     100        }
     101
     102        rc = ddf_fun_bind(fun);
     103        if (rc != EOK) {
     104                printf(NAME ": error binding function %s: %s\n", vfun->name,
     105                    str_error(rc));
     106                ddf_fun_destroy(fun);
     107                return rc;
     108        }
     109
     110        printf(NAME ": registered child device `%s'\n", vfun->name);
     111        return EOK;
    97112}
    98113
    99 static int add_device(device_t *dev)
     114static int rootvirt_add_device(ddf_dev_t *dev)
    100115{
    101116        static int instances = 0;
     
    109124        }
    110125
    111         printf(NAME ": add_device(name=\"%s\", handle=%d)\n",
    112             dev->name, (int)dev->handle);
    113        
     126        printf(NAME ": add_device(handle=%d)\n", (int)dev->handle);
     127
    114128        /*
    115          * Go through all virtual devices and try to add them.
     129         * Go through all virtual functions and try to add them.
    116130         * We silently ignore failures.
    117131         */
    118         virtual_device_t *virt_dev = virtual_devices;
    119         while (virt_dev->name != NULL) {
    120                 (void) add_child(dev, virt_dev);
    121                 virt_dev++;
     132        virtual_function_t *vfun = virtual_functions;
     133        while (vfun->name != NULL) {
     134                (void) rootvirt_add_fun(dev, vfun);
     135                vfun++;
    122136        }
    123137
     
    128142{
    129143        printf(NAME ": HelenOS virtual devices root driver\n");
    130         return driver_main(&rootvirt_driver);
     144        return ddf_driver_main(&rootvirt_driver);
    131145}
    132146
  • uspace/drv/test1/char.c

    rdbe25f1 reb1a2f4  
    3737#include "test1.h"
    3838
    39 static int impl_char_read(device_t *dev, char *buf, size_t count) {
     39static int impl_char_read(ddf_fun_t *fun, char *buf, size_t count) {
    4040        memset(buf, 0, count);
    4141        return count;
    4242}
    4343
    44 static int imp_char_write(device_t *dev, char *buf, size_t count) {
     44static int imp_char_write(ddf_fun_t *fun, char *buf, size_t count) {
    4545        return count;
    4646}
     
    5151};
    5252
    53 device_ops_t char_device_ops = {
     53ddf_dev_ops_t char_device_ops = {
    5454        .interfaces[CHAR_DEV_IFACE] = &char_dev_ops
    5555};
  • uspace/drv/test1/test1.c

    rdbe25f1 reb1a2f4  
    3434#include <errno.h>
    3535#include <str_error.h>
     36#include <ddf/driver.h>
     37
    3638#include "test1.h"
    3739
    38 static int add_device(device_t *dev);
     40static int test1_add_device(ddf_dev_t *dev);
    3941
    4042static driver_ops_t driver_ops = {
    41         .add_device = &add_device
     43        .add_device = &test1_add_device
    4244};
    4345
    44 static driver_t the_driver = {
     46static driver_t test1_driver = {
    4547        .name = NAME,
    4648        .driver_ops = &driver_ops
     
    5557 * @param score Device match score.
    5658 */
    57 static void register_child_verbose(device_t *parent, const char *message,
     59static int register_fun_verbose(ddf_dev_t *parent, const char *message,
    5860    const char *name, const char *match_id, int match_score)
    5961{
    60         printf(NAME ": registering child device `%s': %s.\n",
    61            name, message);
     62        ddf_fun_t *fun;
     63        int rc;
    6264
    63         int rc = child_device_register_wrapper(parent, name,
    64             match_id, match_score, NULL);
     65        printf(NAME ": registering function `%s': %s.\n", name, message);
    6566
    66         if (rc == EOK) {
    67                 printf(NAME ": registered child device `%s'.\n", name);
    68         } else {
    69                 printf(NAME ": failed to register child `%s' (%s).\n",
    70                     name, str_error(rc));
     67        fun = ddf_fun_create(parent, fun_inner, name);
     68        if (fun == NULL) {
     69                printf(NAME ": error creating function %s\n", name);
     70                return ENOMEM;
    7171        }
     72
     73        rc = ddf_fun_add_match_id(fun, match_id, match_score);
     74        if (rc != EOK) {
     75                printf(NAME ": error adding match IDs to function %s\n", name);
     76                ddf_fun_destroy(fun);
     77                return rc;
     78        }
     79
     80        rc = ddf_fun_bind(fun);
     81        if (rc != EOK) {
     82                printf(NAME ": error binding function %s: %s\n", name,
     83                    str_error(rc));
     84                ddf_fun_destroy(fun);
     85                return rc;
     86        }
     87
     88        printf(NAME ": registered child device `%s'\n", name);
     89        return EOK;
    7290}
    7391
     
    89107 * @return Error code reporting success of the operation.
    90108 */
    91 static int add_device(device_t *dev)
     109static int test1_add_device(ddf_dev_t *dev)
    92110{
     111        ddf_fun_t *fun_a;
     112        int rc;
     113
    93114        printf(NAME ": add_device(name=\"%s\", handle=%d)\n",
    94115            dev->name, (int) dev->handle);
    95116
    96         add_device_to_class(dev, "virtual");
     117        fun_a = ddf_fun_create(dev, fun_exposed, "a");
     118        if (fun_a == NULL) {
     119                printf(NAME ": error creating function 'a'.\n");
     120                return ENOMEM;
     121        }
     122
     123        rc = ddf_fun_bind(fun_a);
     124        if (rc != EOK) {
     125                printf(NAME ": error binding function 'a'.\n");
     126                return rc;
     127        }
     128
     129        ddf_fun_add_to_class(fun_a, "virtual");
    97130
    98131        if (str_cmp(dev->name, "null") == 0) {
    99                 dev->ops = &char_device_ops;
    100                 add_device_to_class(dev, "virt-null");
    101         } else if (dev->parent == NULL) {
    102                 register_child_verbose(dev, "cloning myself ;-)", "clone",
     132                fun_a->ops = &char_device_ops;
     133                ddf_fun_add_to_class(fun_a, "virt-null");
     134        } else if (str_cmp(dev->name, "test1") == 0) {
     135                (void) register_fun_verbose(dev, "cloning myself ;-)", "clone",
    103136                    "virtual&test1", 10);
    104137        } else if (str_cmp(dev->name, "clone") == 0) {
    105                 register_child_verbose(dev, "run by the same task", "child",
     138                (void) register_fun_verbose(dev, "run by the same task", "child",
    106139                    "virtual&test1&child", 10);
    107140        }
     
    115148{
    116149        printf(NAME ": HelenOS test1 virtual device driver\n");
    117         return driver_main(&the_driver);
     150        return ddf_driver_main(&test1_driver);
    118151}
    119152
  • uspace/drv/test1/test1.h

    rdbe25f1 reb1a2f4  
    3232#define DRV_TEST1_TEST1_H_
    3333
    34 #include <driver.h>
     34#include <ddf/driver.h>
    3535
    3636#define NAME "test1"
    3737
    38 extern device_ops_t char_device_ops;
     38extern ddf_dev_ops_t char_device_ops;
    3939
    4040#endif
  • uspace/drv/test2/test2.c

    rdbe25f1 reb1a2f4  
    3131
    3232#include <assert.h>
     33#include <async.h>
    3334#include <stdio.h>
    3435#include <errno.h>
    3536#include <str_error.h>
    36 #include <driver.h>
     37#include <ddf/driver.h>
    3738
    3839#define NAME "test2"
    3940
    40 static int add_device(device_t *dev);
     41static int test2_add_device(ddf_dev_t *dev);
    4142
    4243static driver_ops_t driver_ops = {
    43         .add_device = &add_device
     44        .add_device = &test2_add_device
    4445};
    4546
    46 static driver_t the_driver = {
     47static driver_t test2_driver = {
    4748        .name = NAME,
    4849        .driver_ops = &driver_ops
     
    5758 * @param score Device match score.
    5859 */
    59 static void register_child_verbose(device_t *parent, const char *message,
     60static int register_fun_verbose(ddf_dev_t *parent, const char *message,
    6061    const char *name, const char *match_id, int match_score)
    6162{
    62         printf(NAME ": registering child device `%s': %s.\n",
    63            name, message);
     63        ddf_fun_t *fun;
     64        int rc;
    6465
    65         int rc = child_device_register_wrapper(parent, name,
    66             match_id, match_score, NULL);
     66        printf(NAME ": registering function `%s': %s.\n", name, message);
    6767
    68         if (rc == EOK) {
    69                 printf(NAME ": registered child device `%s'.\n", name);
    70         } else {
    71                 printf(NAME ": failed to register child `%s' (%s).\n",
    72                     name, str_error(rc));
     68        fun = ddf_fun_create(parent, fun_inner, name);
     69        if (fun == NULL) {
     70                printf(NAME ": error creating function %s\n", name);
     71                return ENOMEM;
    7372        }
     73
     74        rc = ddf_fun_add_match_id(fun, match_id, match_score);
     75        if (rc != EOK) {
     76                printf(NAME ": error adding match IDs to function %s\n", name);
     77                ddf_fun_destroy(fun);
     78                return rc;
     79        }
     80
     81        rc = ddf_fun_bind(fun);
     82        if (rc != EOK) {
     83                printf(NAME ": error binding function %s: %s\n", name,
     84                    str_error(rc));
     85                ddf_fun_destroy(fun);
     86                return rc;
     87        }
     88
     89        printf(NAME ": registered child device `%s'\n", name);
     90        return EOK;
    7491}
    7592
    7693/** Add child devices after some sleep.
    7794 *
    78  * @param arg Parent device structure (device_t *).
     95 * @param arg Parent device structure (ddf_dev_t *).
    7996 * @return Always EOK.
    8097 */
    8198static int postponed_birth(void *arg)
    8299{
    83         device_t *dev = (device_t *) arg;
     100        ddf_dev_t *dev = (ddf_dev_t *) arg;
     101        ddf_fun_t *fun_a;
     102        int rc;
    84103
    85104        async_usleep(1000);
    86105
    87         register_child_verbose(dev, "child driven by the same task",
     106        (void) register_fun_verbose(dev, "child driven by the same task",
    88107            "child", "virtual&test2", 10);
    89         register_child_verbose(dev, "child driven by test1",
     108        (void) register_fun_verbose(dev, "child driven by test1",
    90109            "test1", "virtual&test1", 10);
    91110
    92         add_device_to_class(dev, "virtual");
     111        fun_a = ddf_fun_create(dev, fun_exposed, "a");
     112        if (fun_a == NULL) {
     113                printf(NAME ": error creating function 'a'.\n");
     114                return ENOMEM;
     115        }
     116
     117        rc = ddf_fun_bind(fun_a);
     118        if (rc != EOK) {
     119                printf(NAME ": error binding function 'a'.\n");
     120                return rc;
     121        }
     122
     123        ddf_fun_add_to_class(fun_a, "virtual");
    93124
    94125        return EOK;
    95126}
    96127
    97 
    98 static int add_device(device_t *dev)
     128static int test2_add_device(ddf_dev_t *dev)
    99129{
    100         printf(NAME ": add_device(name=\"%s\", handle=%d)\n",
     130        printf(NAME ": test2_add_device(name=\"%s\", handle=%d)\n",
    101131            dev->name, (int) dev->handle);
    102132
    103         if (dev->parent == NULL) {
     133        if (str_cmp(dev->name, "child") != 0) {
    104134                fid_t postpone = fibril_create(postponed_birth, dev);
    105135                if (postpone == 0) {
     
    109139                fibril_add_ready(postpone);
    110140        } else {
    111                 register_child_verbose(dev, "child without available driver",
     141                (void) register_fun_verbose(dev, "child without available driver",
    112142                    "ERROR", "non-existent.match.id", 10);
    113143        }
     
    119149{
    120150        printf(NAME ": HelenOS test2 virtual device driver\n");
    121         return driver_main(&the_driver);
     151        return ddf_driver_main(&test2_driver);
    122152}
    123153
  • uspace/drv/uhci-hcd/batch.c

    rdbe25f1 reb1a2f4  
    5151
    5252
    53 batch_t * batch_get(device_t *dev, usb_target_t target,
     53batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
    5454    usb_transfer_type_t transfer_type, size_t max_packet_size,
    5555    dev_speed_t speed, char *buffer, size_t size,
     
    128128        instance->buffer_size = size;
    129129        instance->setup_size = setup_size;
    130         instance->dev = dev;
     130        instance->fun = fun;
    131131        instance->arg = arg;
    132132        instance->speed = speed;
     
    291291            err, instance->transfered_size);
    292292
    293         instance->callback_in(instance->dev,
     293        instance->callback_in(instance->fun,
    294294            err, instance->transfered_size,
    295295            instance->arg);
     
    303303        int err = instance->error;
    304304        usb_log_info("Callback OUT(%d): %d.\n", instance->transfer_type, err);
    305         instance->callback_out(instance->dev,
     305        instance->callback_out(instance->fun,
    306306            err, instance->arg);
    307307}
     
    334334{
    335335        assert(instance);
    336         uhci_t *hc = dev_to_uhci(instance->dev);
     336        uhci_t *hc = fun_to_uhci(instance->fun);
    337337        assert(hc);
    338338        return uhci_schedule(hc, instance);
  • uspace/drv/uhci-hcd/batch.h

    rdbe25f1 reb1a2f4  
    6868        size_t transfered_size;
    6969        int error;
    70         device_t *dev;
     70        ddf_fun_t *fun;
    7171        queue_head_t *qh;
    7272        transfer_descriptor_t *tds;
     
    7474} batch_t;
    7575
    76 batch_t * batch_get(device_t *dev, usb_target_t target,
     76batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
    7777    usb_transfer_type_t transfer_type, size_t max_packet_size,
    7878    dev_speed_t speed, char *buffer, size_t size,
  • uspace/drv/uhci-hcd/iface.c

    rdbe25f1 reb1a2f4  
    3232 * @brief UHCI driver
    3333 */
    34 #include <driver.h>
     34#include <ddf/driver.h>
    3535#include <remote_usbhc.h>
    3636
     
    4343
    4444/*----------------------------------------------------------------------------*/
    45 static int reserve_default_address(device_t *dev, usb_speed_t speed)
     45static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
    4646{
    47         assert(dev);
    48         uhci_t *hc = dev_to_uhci(dev);
     47        assert(fun);
     48        uhci_t *hc = fun_to_uhci(fun);
    4949        assert(hc);
    5050        usb_address_keeping_reserve_default(&hc->address_manager);
     
    5252}
    5353/*----------------------------------------------------------------------------*/
    54 static int release_default_address(device_t *dev)
     54static int release_default_address(ddf_fun_t *fun)
    5555{
    56         assert(dev);
    57         uhci_t *hc = dev_to_uhci(dev);
     56        assert(fun);
     57        uhci_t *hc = fun_to_uhci(fun);
    5858        assert(hc);
    5959        usb_address_keeping_release_default(&hc->address_manager);
     
    6161}
    6262/*----------------------------------------------------------------------------*/
    63 static int request_address(device_t *dev, usb_speed_t speed,
     63static int request_address(ddf_fun_t *fun, usb_speed_t speed,
    6464    usb_address_t *address)
    6565{
    66         assert(dev);
    67         uhci_t *hc = dev_to_uhci(dev);
     66        assert(fun);
     67        uhci_t *hc = fun_to_uhci(fun);
    6868        assert(hc);
    6969        *address = usb_address_keeping_request(&hc->address_manager);
     
    7474/*----------------------------------------------------------------------------*/
    7575static int bind_address(
    76   device_t *dev, usb_address_t address, devman_handle_t handle)
     76  ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
    7777{
    78         assert(dev);
    79         uhci_t *hc = dev_to_uhci(dev);
     78        assert(fun);
     79        uhci_t *hc = fun_to_uhci(fun);
    8080        assert(hc);
    8181        usb_address_keeping_devman_bind(&hc->address_manager, address, handle);
     
    8383}
    8484/*----------------------------------------------------------------------------*/
    85 static int release_address(device_t *dev, usb_address_t address)
     85static int release_address(ddf_fun_t *fun, usb_address_t address)
    8686{
    87         assert(dev);
    88         uhci_t *hc = dev_to_uhci(dev);
     87        assert(fun);
     88        uhci_t *hc = fun_to_uhci(fun);
    8989        assert(hc);
    9090        usb_address_keeping_release_default(&hc->address_manager);
     
    9292}
    9393/*----------------------------------------------------------------------------*/
    94 static int interrupt_out(device_t *dev, usb_target_t target,
     94static int interrupt_out(ddf_fun_t *fun, usb_target_t target,
    9595    size_t max_packet_size,
    9696    void *data, size_t size,
     
    9999        dev_speed_t speed = FULL_SPEED;
    100100
    101         batch_t *batch = batch_get(dev, target, USB_TRANSFER_INTERRUPT,
     101        batch_t *batch = batch_get(fun, target, USB_TRANSFER_INTERRUPT,
    102102            max_packet_size, speed, data, size, NULL, 0, NULL, callback, arg);
    103103        if (!batch)
     
    107107}
    108108/*----------------------------------------------------------------------------*/
    109 static int interrupt_in(device_t *dev, usb_target_t target,
     109static int interrupt_in(ddf_fun_t *fun, usb_target_t target,
    110110    size_t max_packet_size,
    111111    void *data, size_t size,
     
    114114        dev_speed_t speed = FULL_SPEED;
    115115
    116         batch_t *batch = batch_get(dev, target, USB_TRANSFER_INTERRUPT,
     116        batch_t *batch = batch_get(fun, target, USB_TRANSFER_INTERRUPT,
    117117            max_packet_size, speed, data, size, NULL, 0, callback, NULL, arg);
    118118        if (!batch)
     
    122122}
    123123/*----------------------------------------------------------------------------*/
    124 static int control_write(device_t *dev, usb_target_t target,
     124static int control_write(ddf_fun_t *fun, usb_target_t target,
    125125    size_t max_packet_size,
    126126    void *setup_data, size_t setup_size, void *data, size_t size,
     
    129129        dev_speed_t speed = FULL_SPEED;
    130130
    131         batch_t *batch = batch_get(dev, target, USB_TRANSFER_CONTROL,
     131        batch_t *batch = batch_get(fun, target, USB_TRANSFER_CONTROL,
    132132            max_packet_size, speed, data, size, setup_data, setup_size,
    133133            NULL, callback, arg);
     
    138138}
    139139/*----------------------------------------------------------------------------*/
    140 static int control_read(device_t *dev, usb_target_t target,
     140static int control_read(ddf_fun_t *fun, usb_target_t target,
    141141    size_t max_packet_size,
    142142    void *setup_data, size_t setup_size, void *data, size_t size,
     
    145145        dev_speed_t speed = FULL_SPEED;
    146146
    147         batch_t *batch = batch_get(dev, target, USB_TRANSFER_CONTROL,
     147        batch_t *batch = batch_get(fun, target, USB_TRANSFER_CONTROL,
    148148            max_packet_size, speed, data, size, setup_data, setup_size, callback,
    149149            NULL, arg);
  • uspace/drv/uhci-hcd/iface.h

    rdbe25f1 reb1a2f4  
    3838#include <usbhc_iface.h>
    3939
    40 usbhc_iface_t uhci_iface;
     40extern usbhc_iface_t uhci_iface;
    4141
    4242#endif
  • uspace/drv/uhci-hcd/main.c

    rdbe25f1 reb1a2f4  
    3232 * @brief UHCI driver
    3333 */
    34 #include <driver.h>
     34#include <ddf/driver.h>
     35#include <ddf/interrupt.h>
    3536#include <usb_iface.h>
    3637#include <usb/ddfiface.h>
     
    4849#define NAME "uhci-hcd"
    4950
    50 static int uhci_add_device(device_t *device);
     51static int uhci_add_device(ddf_dev_t *device);
    5152
    52 static int usb_iface_get_address(device_t *dev, devman_handle_t handle,
    53     usb_address_t *address)
    54 {
    55         assert(dev);
    56         uhci_t *hc = dev_to_uhci(dev);
    57         assert(hc);
    58 
    59         usb_address_t addr = usb_address_keeping_find(&hc->address_manager,
    60             handle);
    61         if (addr < 0) {
    62                 return addr;
    63         }
    64 
    65         if (address != NULL) {
    66                 *address = addr;
    67         }
    68 
    69         return EOK;
    70 }
    71 
    72 
    73 static usb_iface_t hc_usb_iface = {
    74         .get_hc_handle = usb_iface_get_hc_handle_hc_impl,
    75         .get_address = usb_iface_get_address
    76 };
    77 /*----------------------------------------------------------------------------*/
    78 static device_ops_t uhci_ops = {
    79         .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
    80         .interfaces[USBHC_DEV_IFACE] = &uhci_iface
    81 };
    8253/*----------------------------------------------------------------------------*/
    8354static driver_ops_t uhci_driver_ops = {
     
    9061};
    9162/*----------------------------------------------------------------------------*/
    92 static void irq_handler(device_t *device, ipc_callid_t iid, ipc_call_t *call)
     63static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
    9364{
    94         assert(device);
    95         uhci_t *hc = dev_to_uhci(device);
     65        assert(dev);
     66        uhci_t *hc = dev_to_uhci(dev);
    9667        uint16_t status = IPC_GET_ARG1(*call);
    9768        assert(hc);
     
    10576}
    10677
    107 static int uhci_add_device(device_t *device)
     78static int uhci_add_device(ddf_dev_t *device)
    10879{
    10980        assert(device);
    11081
    11182        usb_log_info("uhci_add_device() called\n");
    112         device->ops = &uhci_ops;
     83
    11384
    11485        uintptr_t io_reg_base;
     
    131102        CHECK_RET_RETURN(ret, "Failed to allocate memory for uhci hcd driver.\n");
    132103
    133         ret = uhci_init(uhci_hc, (void*)io_reg_base, io_reg_size);
     104        ret = uhci_init(uhci_hc, device, (void*)io_reg_base, io_reg_size);
    134105        if (ret != EOK) {
    135106                usb_log_error("Failed to init uhci-hcd.\n");
     
    137108                return ret;
    138109        }
     110
     111        /*
     112         * We might free uhci_hc, but that does not matter since no one
     113         * else would access driver_data anyway.
     114         */
     115        device->driver_data = uhci_hc;
    139116
    140117        ret = register_interrupt_handler(device, irq, irq_handler,
     
    147124        }
    148125
    149         device_t *rh;
     126        ddf_fun_t *rh;
    150127        ret = setup_root_hub(&rh, device);
    151128        if (ret != EOK) {
     
    155132                return ret;
    156133        }
     134        rh->driver_data = uhci_hc->ddf_instance;
    157135
    158         ret = child_device_register(rh, device);
     136        ret = ddf_fun_bind(rh);
    159137        if (ret != EOK) {
    160138                usb_log_error("Failed to register root hub.\n");
     
    165143        }
    166144
    167         device->driver_data = uhci_hc;
    168145        return EOK;
    169146}
     
    172149{
    173150        sleep(3);
    174         usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
     151        usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
    175152
    176         return driver_main(&uhci_driver);
     153        return ddf_driver_main(&uhci_driver);
    177154}
    178155/**
  • uspace/drv/uhci-hcd/pci.c

    rdbe25f1 reb1a2f4  
    4949 * @return Error code.
    5050 */
    51 int pci_get_my_registers(device_t *dev,
     51int pci_get_my_registers(ddf_dev_t *dev,
    5252    uintptr_t *io_reg_address, size_t *io_reg_size,
    5353    int *irq_no)
     
    122122}
    123123/*----------------------------------------------------------------------------*/
    124 int pci_enable_interrupts(device_t *device)
     124int pci_enable_interrupts(ddf_dev_t *device)
    125125{
    126126        int parent_phone = devman_parent_device_connect(device->handle,
  • uspace/drv/uhci-hcd/pci.h

    rdbe25f1 reb1a2f4  
    3636#define DRV_UHCI_PCI_H
    3737
    38 #include <driver.h>
     38#include <ddf/driver.h>
    3939
    40 int pci_get_my_registers(device_t *, uintptr_t *, size_t *, int *);
    41 int pci_enable_interrupts(device_t *device);
     40int pci_get_my_registers(ddf_dev_t *, uintptr_t *, size_t *, int *);
     41int pci_enable_interrupts(ddf_dev_t *);
    4242
    4343#endif
  • uspace/drv/uhci-hcd/root_hub.c

    rdbe25f1 reb1a2f4  
    3939
    4040#include "root_hub.h"
     41#include "uhci.h"
    4142
    42 extern device_ops_t child_ops;
     43static int usb_iface_get_hc_handle_rh_impl(ddf_fun_t *root_hub_fun,
     44    devman_handle_t *handle)
     45{
     46        ddf_fun_t *hc_fun = root_hub_fun->driver_data;
     47        assert(hc_fun != NULL);
     48
     49        *handle = hc_fun->handle;
     50
     51        return EOK;
     52}
     53
     54static int usb_iface_get_address_rh_impl(ddf_fun_t *fun, devman_handle_t handle,
     55    usb_address_t *address)
     56{
     57        assert(fun);
     58        ddf_fun_t *hc_fun = fun->driver_data;
     59        assert(hc_fun);
     60        uhci_t *hc = fun_to_uhci(hc_fun);
     61        assert(hc);
     62
     63        usb_address_t addr = usb_address_keeping_find(&hc->address_manager,
     64            handle);
     65        if (addr < 0) {
     66                return addr;
     67        }
     68
     69        if (address != NULL) {
     70                *address = addr;
     71        }
     72
     73        return EOK;
     74}
     75
     76usb_iface_t usb_iface_root_hub_fun_impl = {
     77        .get_hc_handle = usb_iface_get_hc_handle_rh_impl,
     78        .get_address = usb_iface_get_address_rh_impl
     79};
     80
     81static ddf_dev_ops_t root_hub_ops = {
     82        .interfaces[USB_DEV_IFACE] = &usb_iface_root_hub_fun_impl
     83};
     84
    4385/*----------------------------------------------------------------------------*/
    44 int setup_root_hub(device_t **device, device_t *hc)
     86int setup_root_hub(ddf_fun_t **fun, ddf_dev_t *hc)
    4587{
    46         assert(device);
    47         device_t *hub = create_device();
     88        assert(fun);
     89        int ret;
     90
     91        ddf_fun_t *hub = ddf_fun_create(hc, fun_inner, "root-hub");
    4892        if (!hub) {
    4993                usb_log_error("Failed to create root hub device structure.\n");
    50                 return ENOMEM;
    51         }
    52         char *name;
    53         int ret = asprintf(&name, "UHCI Root Hub");
    54         if (ret < 0) {
    55                 usb_log_error("Failed to create root hub name.\n");
    56                 free(hub);
    5794                return ENOMEM;
    5895        }
     
    6299        if (ret < 0) {
    63100                usb_log_error("Failed to create root hub match string.\n");
    64                 free(hub);
    65                 free(name);
     101                ddf_fun_destroy(hub);
    66102                return ENOMEM;
    67103        }
    68104
    69         match_id_t *match_id = create_match_id();
    70         if (!match_id) {
    71                 usb_log_error("Failed to create root hub match id.\n");
    72                 free(hub);
    73                 free(match_str);
     105        ret = ddf_fun_add_match_id(hub, match_str, 100);
     106        if (ret != EOK) {
     107                usb_log_error("Failed to add root hub match id.\n");
     108                ddf_fun_destroy(hub);
    74109                return ENOMEM;
    75110        }
    76         match_id->id = match_str;
    77         match_id->score = 90;
    78111
    79         add_match_id(&hub->match_ids, match_id);
    80         hub->name = name;
    81         hub->parent = hc;
    82         hub->ops = &child_ops;
     112        hub->ops = &root_hub_ops;
    83113
    84         *device = hub;
     114        *fun = hub;
    85115        return EOK;
    86116}
  • uspace/drv/uhci-hcd/root_hub.h

    rdbe25f1 reb1a2f4  
    3636#define DRV_UHCI_ROOT_HUB_H
    3737
    38 #include <driver.h>
     38#include <ddf/driver.h>
    3939
    40 int setup_root_hub(device_t **device, device_t *hc);
     40int setup_root_hub(ddf_fun_t **device, ddf_dev_t *hc);
    4141
    4242#endif
  • uspace/drv/uhci-hcd/uhci.c

    rdbe25f1 reb1a2f4  
    3333 */
    3434#include <errno.h>
     35#include <str_error.h>
    3536#include <adt/list.h>
     37#include <libarch/ddi.h>
    3638
    3739#include <usb/debug.h>
    3840#include <usb/usb.h>
     41#include <usb/ddfiface.h>
     42#include <usb_iface.h>
    3943
    4044#include "uhci.h"
     45#include "iface.h"
     46
    4147static irq_cmd_t uhci_cmds[] = {
    4248        {
     
    5561};
    5662
     63static int usb_iface_get_address(ddf_fun_t *fun, devman_handle_t handle,
     64    usb_address_t *address)
     65{
     66        assert(fun);
     67        uhci_t *hc = fun_to_uhci(fun);
     68        assert(hc);
     69
     70        usb_address_t addr = usb_address_keeping_find(&hc->address_manager,
     71            handle);
     72        if (addr < 0) {
     73                return addr;
     74        }
     75
     76        if (address != NULL) {
     77                *address = addr;
     78        }
     79
     80        return EOK;
     81}
     82
     83
     84static usb_iface_t hc_usb_iface = {
     85        .get_hc_handle = usb_iface_get_hc_handle_hc_impl,
     86        .get_address = usb_iface_get_address
     87};
     88/*----------------------------------------------------------------------------*/
     89static ddf_dev_ops_t uhci_ops = {
     90        .interfaces[USB_DEV_IFACE] = &hc_usb_iface,
     91        .interfaces[USBHC_DEV_IFACE] = &uhci_iface
     92};
     93
    5794static int uhci_init_transfer_lists(uhci_t *instance);
    5895static int uhci_init_mem_structures(uhci_t *instance);
     
    71108        } else (void) 0
    72109
    73 int uhci_init(uhci_t *instance, void *regs, size_t reg_size)
     110int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size)
    74111{
    75112        assert(reg_size >= sizeof(regs_t));
     113        int ret;
     114
     115        /*
     116         * Create UHCI function.
     117         */
     118        instance->ddf_instance = ddf_fun_create(dev, fun_exposed, "uhci");
     119        if (instance->ddf_instance == NULL) {
     120                usb_log_error("Failed to create UHCI device function.\n");
     121                return ENOMEM;
     122        }
     123        instance->ddf_instance->ops = &uhci_ops;
     124        instance->ddf_instance->driver_data = instance;
     125
     126        ret = ddf_fun_bind(instance->ddf_instance);
     127        CHECK_RET_RETURN(ret, "Failed to bind UHCI device function: %s.\n",
     128            str_error(ret));
    76129
    77130        /* allow access to hc control registers */
    78131        regs_t *io;
    79         int ret = pio_enable(regs, reg_size, (void**)&io);
     132        ret = pio_enable(regs, reg_size, (void**)&io);
    80133        CHECK_RET_RETURN(ret, "Failed to gain access to registers at %p.\n", io);
    81134        instance->registers = io;
  • uspace/drv/uhci-hcd/uhci.h

    rdbe25f1 reb1a2f4  
    3939#include <fibril_synch.h>
    4040#include <adt/list.h>
     41#include <ddi.h>
    4142
    4243#include <usb/addrkeep.h>
     
    9798        fid_t cleaner;
    9899        fid_t debug_checker;
     100
     101        ddf_fun_t *ddf_instance;
    99102} uhci_t;
    100103
    101104/* init uhci specifics in device.driver_data */
    102 int uhci_init(uhci_t *instance, void *regs, size_t reg_size);
     105int uhci_init(uhci_t *instance, ddf_dev_t *dev, void *regs, size_t reg_size);
    103106
    104107static inline void uhci_fini(uhci_t *instance) {};
     
    108111void uhci_interrupt(uhci_t *instance, uint16_t status);
    109112
    110 static inline uhci_t * dev_to_uhci(device_t *dev)
     113static inline uhci_t * dev_to_uhci(ddf_dev_t *dev)
    111114        { return (uhci_t*)dev->driver_data; }
     115
     116static inline uhci_t * fun_to_uhci(ddf_fun_t *fun)
     117        { return (uhci_t*)fun->driver_data; }
    112118
    113119
  • uspace/drv/uhci-rhd/main.c

    rdbe25f1 reb1a2f4  
    3232 * @brief UHCI driver
    3333 */
    34 #include <driver.h>
     34#include <ddf/driver.h>
    3535#include <usb_iface.h>
    3636#include <usb/ddfiface.h>
     
    4444#define NAME "uhci-rhd"
    4545
    46 static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle)
     46static int usb_iface_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
    4747{
    48         assert(dev);
    49         assert(dev->driver_data);
     48        assert(fun);
     49        assert(fun->driver_data);
    5050        assert(handle);
    5151
    52         *handle = ((uhci_root_hub_t*)dev->driver_data)->hc_handle;
    53         usb_log_debug("Answering HC handle: %d.\n", *handle);
     52        *handle = ((uhci_root_hub_t*)fun->driver_data)->hc_handle;
    5453
    5554        return EOK;
     
    6160};
    6261
    63 static device_ops_t uhci_rh_ops = {
     62static ddf_dev_ops_t uhci_rh_ops = {
    6463        .interfaces[USB_DEV_IFACE] = &uhci_rh_usb_iface,
    6564};
    6665
    67 static int uhci_rh_add_device(device_t *device)
     66static int uhci_rh_add_device(ddf_dev_t *device)
    6867{
    6968        if (!device)
     
    7170
    7271        usb_log_debug2("%s called device %d\n", __FUNCTION__, device->handle);
    73         device->ops = &uhci_rh_ops;
     72
     73        //device->ops = &uhci_rh_ops;
     74        (void) uhci_rh_ops;
    7475
    7576        uhci_root_hub_t *rh = malloc(sizeof(uhci_root_hub_t));
     
    104105int main(int argc, char *argv[])
    105106{
    106         usb_log_enable(USB_LOG_LEVEL_ERROR, NAME);
    107         return driver_main(&uhci_rh_driver);
     107        usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
     108        return ddf_driver_main(&uhci_rh_driver);
    108109}
    109110/**
  • uspace/drv/uhci-rhd/port.c

    rdbe25f1 reb1a2f4  
    5252int uhci_port_init(
    5353  uhci_port_t *port, port_status_t *address, unsigned number,
    54   unsigned usec, device_t *rh, int parent_phone)
     54  unsigned usec, ddf_dev_t *rh, int parent_phone)
    5555{
    5656        assert(port);
     
    187187            USB_SPEED_FULL,
    188188            new_device_enable_port, port->number, port,
    189             &dev_addr, &port->attached_device);
     189            &dev_addr, &port->attached_device, NULL, NULL, NULL);
    190190        if (rc != EOK) {
    191191                usb_log_error("Failed adding new device on port %u: %s.\n",
  • uspace/drv/uhci-rhd/port.h

    rdbe25f1 reb1a2f4  
    3636
    3737#include <assert.h>
    38 #include <driver.h> /* device_t */
     38#include <ddf/driver.h>
    3939#include <stdint.h>
    4040#include <usb/usbdevice.h>
     
    4848        unsigned wait_period_usec;
    4949        usb_hc_connection_t hc_connection;
    50         device_t *rh;
     50        ddf_dev_t *rh;
    5151        devman_handle_t attached_device;
    5252        fid_t checker;
     
    5555int uhci_port_init(
    5656  uhci_port_t *port, port_status_t *address, unsigned number,
    57   unsigned usec, device_t *rh, int parent_phone);
     57  unsigned usec, ddf_dev_t *rh, int parent_phone);
    5858
    5959void uhci_port_fini(uhci_port_t *port);
  • uspace/drv/uhci-rhd/root_hub.c

    rdbe25f1 reb1a2f4  
    3434#include <errno.h>
    3535#include <stdint.h>
    36 
     36#include <ddi.h>
     37#include <devman.h>
    3738#include <usb/debug.h>
    3839
     
    4142
    4243int uhci_root_hub_init(
    43   uhci_root_hub_t *instance, void *addr, size_t size, device_t *rh)
     44  uhci_root_hub_t *instance, void *addr, size_t size, ddf_dev_t *rh)
    4445{
    4546        assert(instance);
  • uspace/drv/uhci-rhd/root_hub.h

    rdbe25f1 reb1a2f4  
    3636
    3737#include <fibril.h>
    38 #include <driver.h> /* for device_t */
     38#include <ddf/driver.h>
    3939
    4040#include "port.h"
     
    5050
    5151int uhci_root_hub_init(
    52   uhci_root_hub_t *instance, void *addr, size_t size, device_t *rh);
     52  uhci_root_hub_t *instance, void *addr, size_t size, ddf_dev_t *rh);
    5353
    5454int uhci_root_hub_fini(uhci_root_hub_t *instance);
  • uspace/drv/usbhid/descdump.c

    rdbe25f1 reb1a2f4  
    3535
    3636#include <usb/classes/hid.h>
     37#include <stdio.h>
     38#include <assert.h>
    3739
    3840#include "descdump.h"
  • uspace/drv/usbhid/descparser.c

    rdbe25f1 reb1a2f4  
    3636#include <errno.h>
    3737#include <stdint.h>
     38#include <stdio.h>
    3839#include <usb/usb.h>
    3940#include <usb/classes/hid.h>
  • uspace/drv/usbhid/hid.h

    rdbe25f1 reb1a2f4  
    3838
    3939#include <usb/classes/hid.h>
    40 #include <driver.h>
     40#include <ddf/driver.h>
    4141#include <usb/pipes.h>
    4242
     
    6767 */
    6868typedef struct {
    69         device_t *device;
     69        ddf_dev_t *device;
    7070        usb_hid_configuration_t *conf;
    7171        usb_hid_report_parser_t *parser;
  • uspace/drv/usbhid/main.c

    rdbe25f1 reb1a2f4  
    3636 */
    3737
    38 #include <driver.h>
     38#include <ddf/driver.h>
    3939#include <ipc/driver.h>
    4040#include <ipc/kbd.h>
     
    7272};
    7373
    74 static void default_connection_handler(device_t *, ipc_callid_t, ipc_call_t *);
    75 static device_ops_t keyboard_ops = {
     74static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
     75static ddf_dev_ops_t keyboard_ops = {
    7676        .default_handler = default_connection_handler
    7777};
     
    8585 * @param icall Call data.
    8686 */
    87 void default_connection_handler(device_t *dev,
     87void default_connection_handler(ddf_fun_t *fun,
    8888    ipc_callid_t icallid, ipc_call_t *icall)
    8989{
     
    413413}
    414414
    415 static usb_hid_dev_kbd_t *usbkbd_init_device(device_t *dev)
     415static usb_hid_dev_kbd_t *usbkbd_init_device(ddf_dev_t *dev)
    416416{
    417417        int rc;
     
    554554        }
    555555
    556         device_t *dev = (device_t *)arg;
     556        ddf_dev_t *dev = (ddf_dev_t *)arg;
    557557
    558558        // initialize device (get and process descriptors, get address, etc.)
     
    568568}
    569569
    570 static int usbkbd_add_device(device_t *dev)
     570static int usbkbd_add_device(ddf_dev_t *dev)
    571571{
    572572        /* For now, fail immediately. */
     
    590590
    591591        /*
     592         * Create default function.
     593         */
     594        // FIXME - check for errors
     595        ddf_fun_t *kbd_fun = ddf_fun_create(dev, fun_exposed, "keyboard");
     596        assert(kbd_fun != NULL);
     597        kbd_fun->ops = &keyboard_ops;
     598
     599        int rc = ddf_fun_bind(kbd_fun);
     600        assert(rc == EOK);
     601        rc = ddf_fun_add_to_class(kbd_fun, "keyboard");
     602        assert(rc == EOK);
     603
     604        /*
    592605         * Create new fibril for handling this keyboard
    593606         */
     
    599612        fibril_add_ready(fid);
    600613
    601         dev->ops = &keyboard_ops;
    602 
    603         add_device_to_class(dev, "keyboard");
     614        //dev->ops = &keyboard_ops;
     615        (void)keyboard_ops;
     616
     617        //add_device_to_class(dev, "keyboard");
    604618
    605619        /*
     
    621635{
    622636        usb_log_enable(USB_LOG_LEVEL_INFO, "usbhid");
    623         return driver_main(&kbd_driver);
     637        return ddf_driver_main(&kbd_driver);
    624638}
    625639
  • uspace/drv/usbhub/main.c

    rdbe25f1 reb1a2f4  
    3131 */
    3232
    33 #include <driver.h>
     33#include <ddf/driver.h>
    3434#include <errno.h>
    3535#include <async.h>
     
    6262int main(int argc, char *argv[])
    6363{
    64         usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
     64        usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
    6565       
    6666        fibril_mutex_initialize(&usb_hub_list_lock);
     
    7777        fibril_add_ready(fid);
    7878
    79         return driver_main(&hub_driver);
     79        return ddf_driver_main(&hub_driver);
    8080}
    8181
  • uspace/drv/usbhub/usbhub.c

    rdbe25f1 reb1a2f4  
    3333 */
    3434
    35 #include <driver.h>
     35#include <ddf/driver.h>
    3636#include <bool.h>
    3737#include <errno.h>
     
    5252#include "usb/classes/classes.h"
    5353
    54 static device_ops_t hub_device_ops = {
     54static ddf_dev_ops_t hub_device_ops = {
    5555        .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
    5656};
     
    7979 */
    8080static int usb_hub_init_communication(usb_hub_info_t * hub){
     81        usb_log_debug("Initializing hub USB communication (hub->device->handle=%zu).\n", hub->device->handle);
    8182        int opResult;
    8283        opResult = usb_device_connection_initialize_from_device(
     
    8889                return opResult;
    8990        }
     91        usb_log_debug("Initializing USB wire abstraction.\n");
    9092        opResult = usb_hc_connection_initialize_from_device(&hub->connection,
    9193                        hub->device);
     
    9597                return opResult;
    9698        }
     99        usb_log_debug("Initializing default control pipe.\n");
    97100        opResult = usb_endpoint_pipe_initialize_default_control(&hub->endpoints.control,
    98101            &hub->device_connection);
     
    222225 * @return pointer to created structure or NULL in case of error
    223226 */
    224 usb_hub_info_t * usb_create_hub_info(device_t * device) {
     227usb_hub_info_t * usb_create_hub_info(ddf_dev_t * device) {
    225228        usb_hub_info_t* result = usb_new(usb_hub_info_t);
    226229        result->device = device;
     
    291294 * @return
    292295 */
    293 int usb_add_hub_device(device_t *dev) {
     296int usb_add_hub_device(ddf_dev_t *dev) {
    294297        dprintf(USB_LOG_LEVEL_INFO, "add_hub_device(handle=%d)", (int) dev->handle);
    295298
    296         dev->ops = &hub_device_ops;
     299        //dev->ops = &hub_device_ops;
     300        (void) hub_device_ops;
    297301
    298302        usb_hub_info_t * hub_info = usb_create_hub_info(dev);
     
    450454        //??
    451455    opResult = usb_device_register_child_in_devman(new_device_address,
    452             hub->connection.hc_handle, hub->device, &child_handle);
     456            hub->connection.hc_handle, hub->device, &child_handle,
     457            NULL, NULL, NULL);
    453458
    454459        if (opResult != EOK) {
  • uspace/drv/usbhub/usbhub.h

    rdbe25f1 reb1a2f4  
    3838#include <ipc/devman.h>
    3939#include <usb/usb.h>
    40 #include <driver.h>
     40#include <ddf/driver.h>
    4141
    4242#define NAME "usbhub"
     
    6363        //usb_hcd_attached_device_info_t * usb_device;
    6464        /** General device info*/
    65         device_t * device;
     65        ddf_dev_t * device;
    6666        /** connection to hcd */
    6767        //usb_device_connection_t connection;
     
    8484 * @return Error code.
    8585 */
    86 int usb_add_hub_device(device_t *dev);
     86int usb_add_hub_device(ddf_dev_t *dev);
    8787
    8888/**
  • uspace/drv/usbhub/usbhub_private.h

    rdbe25f1 reb1a2f4  
    4242#include <adt/list.h>
    4343#include <bool.h>
    44 #include <driver.h>
     44#include <ddf/driver.h>
    4545#include <fibril_synch.h>
    4646
     
    7676 * @return
    7777 */
    78 usb_hub_info_t * usb_create_hub_info(device_t * device);
     78usb_hub_info_t * usb_create_hub_info(ddf_dev_t * device);
    7979
    8080/** List of hubs maanged by this driver */
  • uspace/drv/usbhub/utils.c

    rdbe25f1 reb1a2f4  
    3333 * @brief various utilities
    3434 */
    35 #include <driver.h>
     35#include <ddf/driver.h>
    3636#include <bool.h>
    3737#include <errno.h>
  • uspace/drv/usbmid/main.c

    rdbe25f1 reb1a2f4  
    4444#include "usbmid.h"
    4545
    46 static int usbmid_add_device(device_t *gen_dev)
     46static int usbmid_add_device(ddf_dev_t *gen_dev)
    4747{
    4848        usbmid_device_t *dev = usbmid_device_create(gen_dev);
     
    9999        printf(NAME ": USB multi interface device driver.\n");
    100100
    101         usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
    102         return driver_main(&mid_driver);
     101        usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
     102        return ddf_driver_main(&mid_driver);
    103103}
    104104
  • uspace/drv/usbmid/usbmid.c

    rdbe25f1 reb1a2f4  
    4545
    4646/** Callback for DDF USB interface. */
    47 static int usb_iface_get_address_impl(device_t *device, devman_handle_t handle,
     47static int usb_iface_get_address_impl(ddf_fun_t *fun, devman_handle_t handle,
    4848    usb_address_t *address)
    4949{
    50         assert(device);
    51         device_t *parent = device->parent;
    52 
    53         /* Default error, device does not support this operation. */
    54         int rc = ENOTSUP;
    55 
    56         if (parent && parent->ops && parent->ops->interfaces[USB_DEV_IFACE]) {
    57                 usb_iface_t *usb_iface
    58                     = (usb_iface_t *) parent->ops->interfaces[USB_DEV_IFACE];
    59                 assert(usb_iface != NULL);
    60 
    61                 if (usb_iface->get_address) {
    62                         rc = usb_iface->get_address(parent, parent->handle,
    63                             address);
    64                 }
    65         }
    66 
    67         return rc;
     50        return usb_iface_get_address_hub_impl(fun, handle, address);
    6851}
    6952
    7053/** Callback for DDF USB interface. */
    71 static int usb_iface_get_interface_impl(device_t *device, devman_handle_t handle,
     54static int usb_iface_get_interface_impl(ddf_fun_t *fun, devman_handle_t handle,
    7255    int *iface_no)
    7356{
    74         assert(device);
    75 
    76         usbmid_interface_t *iface = device->driver_data;
     57        assert(fun);
     58
     59        usbmid_interface_t *iface = fun->driver_data;
    7760        assert(iface);
    7861
     
    9174
    9275
    93 static device_ops_t child_device_ops = {
     76static ddf_dev_ops_t child_device_ops = {
    9477        .interfaces[USB_DEV_IFACE] = &child_usb_iface
    9578};
    9679
    97 static device_ops_t mid_device_ops = {
     80static ddf_dev_ops_t mid_device_ops = {
    9881        .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
    9982};
     
    10588 * @retval NULL Error occured.
    10689 */
    107 usbmid_device_t *usbmid_device_create(device_t *dev)
     90usbmid_device_t *usbmid_device_create(ddf_dev_t *dev)
    10891{
    10992        usbmid_device_t *mid = malloc(sizeof(usbmid_device_t));
     
    133116
    134117        mid->dev = dev;
    135         dev->ops = &mid_device_ops;
     118        (void) &mid_device_ops;
    136119
    137120        return mid;
     
    145128 * @retval NULL Error occured.
    146129 */
    147 usbmid_interface_t *usbmid_interface_create(device_t *dev, int iface_no)
     130usbmid_interface_t *usbmid_interface_create(ddf_fun_t *fun, int iface_no)
    148131{
    149132        usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));
     
    154137        }
    155138
    156         iface->dev = dev;
     139        iface->fun = fun;
    157140        iface->interface_no = iface_no;
    158141
     
    172155    const usb_standard_interface_descriptor_t *interface_descriptor)
    173156{
    174         device_t *child = NULL;
     157        ddf_fun_t *child = NULL;
    175158        char *child_name = NULL;
    176159        usbmid_interface_t *child_as_interface = NULL;
    177160        int rc;
    178 
    179         /* Create the device. */
    180         child = create_device();
    181         if (child == NULL) {
    182                 rc = ENOMEM;
    183                 goto error_leave;
    184         }
    185161
    186162        /*
     
    196172        }
    197173
     174        /* Create the device. */
     175        child = ddf_fun_create(parent->dev, fun_inner, child_name);
     176        if (child == NULL) {
     177                rc = ENOMEM;
     178                goto error_leave;
     179        }
     180
     181
     182
    198183        child_as_interface = usbmid_interface_create(child,
    199184            (int) interface_descriptor->interface_number);
     
    204189
    205190        child->driver_data = child_as_interface;
    206         child->parent = parent->dev;
    207         child->name = child_name;
    208191        child->ops = &child_device_ops;
    209192
     
    215198        }
    216199
    217         rc = child_device_register(child, parent->dev);
     200        rc = ddf_fun_bind(child);
    218201        if (rc != EOK) {
    219202                goto error_leave;
     
    226209                child->name = NULL;
    227210                /* This takes care of match_id deallocation as well. */
    228                 delete_device(child);
     211                ddf_fun_destroy(child);
    229212        }
    230213        if (child_name != NULL) {
  • uspace/drv/usbmid/usbmid.h

    rdbe25f1 reb1a2f4  
    3737#define USBMID_H_
    3838
    39 #include <driver.h>
     39#include <ddf/driver.h>
    4040#include <usb/usb.h>
    4141#include <usb/pipes.h>
     
    4646typedef struct {
    4747        /** Device container. */
    48         device_t *dev;
     48        ddf_dev_t *dev;
    4949
    5050        /** Representation of USB wire. */
     
    5555
    5656typedef struct {
    57         /** Device container. */
    58         device_t *dev;
     57        /** Function container. */
     58        ddf_fun_t *fun;
    5959
    6060        /** Interface number. */
     
    6262} usbmid_interface_t;
    6363
    64 usbmid_device_t *usbmid_device_create(device_t *);
    65 usbmid_interface_t *usbmid_interface_create(device_t *, int);
     64usbmid_device_t *usbmid_device_create(ddf_dev_t *);
     65usbmid_interface_t *usbmid_interface_create(ddf_fun_t *, int);
    6666bool usbmid_explore_device(usbmid_device_t *);
    6767int usbmid_spawn_interface_child(usbmid_device_t *,
  • uspace/drv/vhc/conn.h

    rdbe25f1 reb1a2f4  
    4646extern usbhc_iface_t vhc_iface;
    4747extern usb_iface_t vhc_usb_iface;
     48extern usb_iface_t rh_usb_iface;
    4849
    4950void address_init(void);
    5051
    5152
    52 void default_connection_handler(device_t *, ipc_callid_t, ipc_call_t *);
    53 void on_client_close(device_t *);
     53void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
     54void on_client_close(ddf_fun_t *);
    5455
    5556
  • uspace/drv/vhc/conndev.c

    rdbe25f1 reb1a2f4  
    7676/** Default handler for IPC methods not handled by DDF.
    7777 *
    78  * @param dev Device handling the call.
     78 * @param fun Device handling the call.
    7979 * @param icallid Call id.
    8080 * @param icall Call data.
    8181 */
    82 void default_connection_handler(device_t *dev,
     82void default_connection_handler(ddf_fun_t *fun,
    8383    ipc_callid_t icallid, ipc_call_t *icall)
    8484{
     
    112112 * @param d Device the client was connected to.
    113113 */
    114 void on_client_close(device_t *d)
     114void on_client_close(ddf_fun_t *fun)
    115115{
    116116        /*
  • uspace/drv/vhc/connhost.c

    rdbe25f1 reb1a2f4  
    4848        usbhc_iface_transfer_out_callback_t out_callback;
    4949        usbhc_iface_transfer_in_callback_t in_callback;
    50         device_t *dev;
     50        ddf_fun_t *fun;
    5151        size_t reported_size;
    5252        void *arg;
     
    5858        usbhc_iface_transfer_out_callback_t out_callback;
    5959        usbhc_iface_transfer_in_callback_t in_callback;
    60         device_t *dev;
     60        ddf_fun_t *fun;
    6161        void *arg;
    6262        void *data_buffer;
     
    7575        switch (transfer->direction) {
    7676                case USB_DIRECTION_IN:
    77                         transfer->in_callback(transfer->dev,
     77                        transfer->in_callback(transfer->fun,
    7878                            outcome, size,
    7979                            transfer->arg);
    8080                        break;
    8181                case USB_DIRECTION_OUT:
    82                         transfer->out_callback(transfer->dev,
     82                        transfer->out_callback(transfer->fun,
    8383                            outcome,
    8484                            transfer->arg);
     
    9292}
    9393
    94 static transfer_info_t *create_transfer_info(device_t *dev,
     94static transfer_info_t *create_transfer_info(ddf_fun_t *fun,
    9595    usb_direction_t direction, void *arg)
    9696{
     
    101101        transfer->out_callback = NULL;
    102102        transfer->arg = arg;
    103         transfer->dev = dev;
     103        transfer->fun = fun;
    104104        transfer->reported_size = (size_t) -1;
    105105
     
    112112        switch (transfer->direction) {
    113113                case USB_DIRECTION_IN:
    114                         transfer->in_callback(transfer->dev,
     114                        transfer->in_callback(transfer->fun,
    115115                            outcome, size,
    116116                            transfer->arg);
    117117                        break;
    118118                case USB_DIRECTION_OUT:
    119                         transfer->out_callback(transfer->dev,
     119                        transfer->out_callback(transfer->fun,
    120120                            outcome,
    121121                            transfer->arg);
     
    138138        }
    139139
    140         transfer_info_t *transfer  = create_transfer_info(ctrl_transfer->dev,
     140        transfer_info_t *transfer  = create_transfer_info(ctrl_transfer->fun,
    141141            ctrl_transfer->direction, ctrl_transfer->arg);
    142142        transfer->out_callback = ctrl_transfer->out_callback;
     
    195195}
    196196
    197 static control_transfer_info_t *create_control_transfer_info(device_t *dev,
     197static control_transfer_info_t *create_control_transfer_info(ddf_fun_t *fun,
    198198    usb_direction_t direction, usb_target_t target,
    199199    void *data_buffer, size_t data_buffer_size,
     
    208208        transfer->out_callback = NULL;
    209209        transfer->arg = arg;
    210         transfer->dev = dev;
     210        transfer->fun = fun;
    211211        transfer->data_buffer = data_buffer;
    212212        transfer->data_buffer_size = data_buffer_size;
     
    215215}
    216216
    217 static int enqueue_transfer_out(device_t *dev,
     217static int enqueue_transfer_out(ddf_fun_t *fun,
    218218    usb_target_t target, usb_transfer_type_t transfer_type,
    219219    void *buffer, size_t size,
     
    226226
    227227        transfer_info_t *transfer
    228             = create_transfer_info(dev, USB_DIRECTION_OUT, arg);
     228            = create_transfer_info(fun, USB_DIRECTION_OUT, arg);
    229229        transfer->out_callback = callback;
    230230
     
    235235}
    236236
    237 static int enqueue_transfer_in(device_t *dev,
     237static int enqueue_transfer_in(ddf_fun_t *fun,
    238238    usb_target_t target, usb_transfer_type_t transfer_type,
    239239    void *buffer, size_t size,
     
    246246
    247247        transfer_info_t *transfer
    248             = create_transfer_info(dev, USB_DIRECTION_IN, arg);
     248            = create_transfer_info(fun, USB_DIRECTION_IN, arg);
    249249        transfer->in_callback = callback;
    250250
     
    256256
    257257
    258 static int interrupt_out(device_t *dev, usb_target_t target,
     258static int interrupt_out(ddf_fun_t *fun, usb_target_t target,
    259259    size_t max_packet_size,
    260260    void *data, size_t size,
    261261    usbhc_iface_transfer_out_callback_t callback, void *arg)
    262262{
    263         return enqueue_transfer_out(dev, target, USB_TRANSFER_INTERRUPT,
     263        return enqueue_transfer_out(fun, target, USB_TRANSFER_INTERRUPT,
    264264            data, size,
    265265            callback, arg);
    266266}
    267267
    268 static int interrupt_in(device_t *dev, usb_target_t target,
     268static int interrupt_in(ddf_fun_t *fun, usb_target_t target,
    269269    size_t max_packet_size,
    270270    void *data, size_t size,
    271271    usbhc_iface_transfer_in_callback_t callback, void *arg)
    272272{
    273         return enqueue_transfer_in(dev, target, USB_TRANSFER_INTERRUPT,
     273        return enqueue_transfer_in(fun, target, USB_TRANSFER_INTERRUPT,
    274274            data, size,
    275275            callback, arg);
    276276}
    277277
    278 static int control_write(device_t *dev, usb_target_t target,
     278static int control_write(ddf_fun_t *fun, usb_target_t target,
    279279    size_t max_packet_size,
    280280    void *setup_packet, size_t setup_packet_size,
     
    283283{
    284284        control_transfer_info_t *transfer
    285             = create_control_transfer_info(dev, USB_DIRECTION_OUT, target,
     285            = create_control_transfer_info(fun, USB_DIRECTION_OUT, target,
    286286            data, data_size, arg);
    287287        transfer->out_callback = callback;
     
    294294}
    295295
    296 static int control_read(device_t *dev, usb_target_t target,
     296static int control_read(ddf_fun_t *fun, usb_target_t target,
    297297    size_t max_packet_size,
    298298    void *setup_packet, size_t setup_packet_size,
     
    301301{
    302302        control_transfer_info_t *transfer
    303             = create_control_transfer_info(dev, USB_DIRECTION_IN, target,
     303            = create_control_transfer_info(fun, USB_DIRECTION_IN, target,
    304304            data, data_size, arg);
    305305        transfer->in_callback = callback;
     
    314314static usb_address_keeping_t addresses;
    315315
    316 static int tell_address(device_t *dev, devman_handle_t handle,
     316static int tell_address(ddf_fun_t *fun, devman_handle_t handle,
    317317    usb_address_t *address)
    318318{
     319        usb_log_debug("tell_address(fun \"%s\", handle %zu)\n",
     320            fun->name, (size_t) fun->handle);
    319321        usb_address_t addr = usb_address_keeping_find(&addresses, handle);
    320322        if (addr < 0) {
     
    326328}
    327329
    328 static int reserve_default_address(device_t *dev, usb_speed_t ignored)
     330static int reserve_default_address(ddf_fun_t *fun, usb_speed_t ignored)
    329331{
    330332        usb_address_keeping_reserve_default(&addresses);
     
    332334}
    333335
    334 static int release_default_address(device_t *dev)
     336static int release_default_address(ddf_fun_t *fun)
    335337{
    336338        usb_address_keeping_release_default(&addresses);
     
    338340}
    339341
    340 static int request_address(device_t *dev, usb_speed_t ignored,
     342static int request_address(ddf_fun_t *fun, usb_speed_t ignored,
    341343    usb_address_t *address)
    342344{
     
    350352}
    351353
    352 static int release_address(device_t *dev, usb_address_t address)
     354static int release_address(ddf_fun_t *fun, usb_address_t address)
    353355{
    354356        return usb_address_keeping_release(&addresses, address);
    355357}
    356358
    357 static int bind_address(device_t *dev, usb_address_t address,
     359static int bind_address(ddf_fun_t *fun, usb_address_t address,
    358360    devman_handle_t handle)
    359361{
    360362        usb_address_keeping_devman_bind(&addresses, address, handle);
    361363        return EOK;
     364}
     365
     366static int usb_iface_get_hc_handle_rh_impl(ddf_fun_t *root_hub_fun,
     367    devman_handle_t *handle)
     368{
     369        ddf_fun_t *hc_fun = root_hub_fun->driver_data;
     370        assert(hc_fun != NULL);
     371
     372        *handle = hc_fun->handle;
     373
     374        usb_log_debug("usb_iface_get_hc_handle_rh_impl returns %zu\n", *handle);
     375
     376        return EOK;
     377}
     378
     379static int tell_address_rh(ddf_fun_t *root_hub_fun, devman_handle_t handle,
     380    usb_address_t *address)
     381{
     382        ddf_fun_t *hc_fun = root_hub_fun->driver_data;
     383        assert(hc_fun != NULL);
     384
     385        return tell_address(hc_fun, root_hub_fun->handle, address);
    362386}
    363387
     
    386410};
    387411
     412usb_iface_t rh_usb_iface = {
     413        .get_hc_handle = usb_iface_get_hc_handle_rh_impl,
     414        .get_address = tell_address_rh
     415};
     416
    388417
    389418/**
  • uspace/drv/vhc/hcd.c

    rdbe25f1 reb1a2f4  
    4242#include <errno.h>
    4343#include <str_error.h>
    44 #include <driver.h>
     44#include <ddf/driver.h>
    4545
    4646#include <usb/usb.h>
     
    5353#include "conn.h"
    5454
    55 static device_ops_t vhc_ops = {
     55static ddf_dev_ops_t vhc_ops = {
    5656        .interfaces[USBHC_DEV_IFACE] = &vhc_iface,
    5757        .interfaces[USB_DEV_IFACE] = &vhc_usb_iface,
     
    6060};
    6161
    62 static int vhc_count = 0;
    63 static int vhc_add_device(device_t *dev)
     62static int vhc_add_device(ddf_dev_t *dev)
    6463{
     64        static int vhc_count = 0;
     65        int rc;
     66
    6567        /*
    6668         * Currently, we know how to simulate only single HC.
     
    7072        }
    7173
    72         vhc_count++;
     74        /*
     75         * Create exposed function representing the host controller
     76         * itself.
     77         */
     78        ddf_fun_t *hc = ddf_fun_create(dev, fun_exposed, "hc");
     79        if (hc == NULL) {
     80                usb_log_fatal("Failed to create device function.\n");
     81                return ENOMEM;
     82        }
    7383
    74         dev->ops = &vhc_ops;
     84        hc->ops = &vhc_ops;
    7585
    76         devman_add_device_to_class(dev->handle, "usbhc");
     86        rc = ddf_fun_bind(hc);
     87        if (rc != EOK) {
     88                usb_log_fatal("Failed to bind HC function: %s.\n",
     89                    str_error(rc));
     90                return rc;
     91        }
     92
     93        ddf_fun_add_to_class(hc, "usbhc");
    7794
    7895        /*
    7996         * Initialize our hub and announce its presence.
    8097         */
    81         virtual_hub_device_init(dev);
     98        virtual_hub_device_init(hc);
    8299
    83         usb_log_info("Virtual USB host controller ready (id = %zu).\n",
    84             (size_t) dev->handle);
     100        usb_log_info("Virtual USB host controller ready (dev %zu, hc %zu).\n",
     101            (size_t) dev->handle, (size_t) hc->handle);
    85102
    86103        return EOK;
     
    103120         * in devman output.
    104121         */
    105         sleep(5);
     122        //sleep(5);
    106123
    107         usb_log_enable(USB_LOG_LEVEL_INFO, NAME);
     124        usb_log_enable(USB_LOG_LEVEL_DEBUG, NAME);
    108125
    109126        printf(NAME ": virtual USB host controller driver.\n");
     
    122139         * We are also a driver within devman framework.
    123140         */
    124         return driver_main(&vhc_driver);
     141        return ddf_driver_main(&vhc_driver);
    125142}
    126143
  • uspace/drv/vhc/hub.c

    rdbe25f1 reb1a2f4  
    3939#include <str_error.h>
    4040#include <stdlib.h>
    41 #include <driver.h>
     41#include <ddf/driver.h>
     42#include <devman.h>
    4243#include <usb/hub.h>
    4344#include <usb/recognise.h>
     
    4647#include "hub/virthub.h"
    4748#include "vhcd.h"
     49#include "conn.h"
    4850
    4951usbvirt_device_t virtual_hub_device;
     52static ddf_dev_ops_t rh_ops = {
     53        .interfaces[USB_DEV_IFACE] = &rh_usb_iface,
     54};
    5055
    5156static int hub_register_in_devman_fibril(void *arg);
    5257
    53 void virtual_hub_device_init(device_t *hc_dev)
     58void virtual_hub_device_init(ddf_fun_t *hc_dev)
    5459{
    5560        virthub_init(&virtual_hub_device);
     
    8388int hub_register_in_devman_fibril(void *arg)
    8489{
    85         device_t *hc_dev = (device_t *) arg;
     90        ddf_fun_t *hc_dev = (ddf_fun_t *) arg;
    8691
    8792        /*
     
    9499        async_hangup(phone);
    95100
     101        int rc;
     102
    96103        usb_hc_connection_t hc_conn;
    97         usb_hc_connection_initialize(&hc_conn, hc_dev->handle);
     104        rc = usb_hc_connection_initialize(&hc_conn, hc_dev->handle);
     105        assert(rc == EOK);
    98106
    99         usb_hc_connection_open(&hc_conn);
     107        rc = usb_hc_connection_open(&hc_conn);
     108        assert(rc == EOK);
    100109
    101         int rc = usb_hc_new_device_wrapper(hc_dev, &hc_conn, USB_SPEED_FULL,
     110        ddf_fun_t *hub_dev;
     111        rc = usb_hc_new_device_wrapper(hc_dev->dev, &hc_conn,
     112            USB_SPEED_FULL,
    102113            pretend_port_rest, 0, NULL,
    103             NULL, NULL);
     114            NULL, NULL, &rh_ops, hc_dev, &hub_dev);
    104115        if (rc != EOK) {
    105116                usb_log_fatal("Failed to create root hub: %s.\n",
     
    108119
    109120        usb_hc_connection_close(&hc_conn);
     121
     122        usb_log_info("Created root hub function (handle %zu).\n",
     123            (size_t) hub_dev->handle);
    110124
    111125        return 0;
  • uspace/drv/vhc/hub.h

    rdbe25f1 reb1a2f4  
    3737
    3838#include <usbvirt/device.h>
    39 #include <driver.h>
     39#include <ddf/driver.h>
    4040
    4141#include "devices.h"
     
    4545extern usbvirt_device_t virtual_hub_device;
    4646
    47 void virtual_hub_device_init(device_t *);
     47void virtual_hub_device_init(ddf_fun_t *);
    4848
    4949#endif
  • uspace/drv/vhc/hub/hub.c

    rdbe25f1 reb1a2f4  
    3838#include <errno.h>
    3939#include <str_error.h>
     40#include <assert.h>
    4041#include <stdlib.h>
    41 #include <driver.h>
     42#include <ddf/driver.h>
    4243
    4344#include "hub.h"
  • uspace/drv/vhc/hub/virthub.c

    rdbe25f1 reb1a2f4  
    3939#include <errno.h>
    4040#include <str_error.h>
     41#include <assert.h>
    4142#include <stdlib.h>
    42 #include <driver.h>
     43#include <ddf/driver.h>
    4344
    4445#include "virthub.h"
Note: See TracChangeset for help on using the changeset viewer.