Changeset 97a62fe in mainline


Ignore:
Timestamp:
2011-02-14T21:41:50Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
cd0684d
Parents:
7df0477e
Message:

Refactor create_function(), delete_function() and register_function() into
ddf_fun_create(), ddf_fun_destroy() and ddf_fun_bind(). This is not just
a rename.

Location:
uspace
Files:
9 edited

Legend:

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

    r7df0477e r97a62fe  
    106106};
    107107
    108 static isa_fun_t *isa_fun_create()
     108static isa_fun_t *isa_fun_create(device_t *dev, const char *name)
    109109{
    110110        isa_fun_t *fun = calloc(1, sizeof(isa_fun_t));
     
    112112                return NULL;
    113113
    114         function_t *fnode = create_function();
     114        function_t *fnode = ddf_fun_create(dev, fun_inner, name);
    115115        if (fnode == NULL) {
    116116                free(fun);
     
    417417                return NULL;
    418418
    419         isa_fun_t *fun = isa_fun_create();
     419        isa_fun_t *fun = isa_fun_create(dev, fun_name);
    420420        if (fun == NULL) {
    421421                free(fun_name);
    422422                return NULL;
    423423        }
    424 
    425         function_t *fnode = fun->fnode;
    426         fnode->name = fun_name;
    427         fnode->ftype = fun_inner;
    428424
    429425        /* Allocate buffer for the list of hardware resources of the device. */
     
    447443
    448444        /* Set device operations to the device. */
    449         fnode->ops = &isa_fun_ops;
    450 
    451         printf(NAME ": register_function(fun, dev); function is %s.\n",
    452             fnode->name);
    453         register_function(fnode, dev);
     445        fun->fnode->ops = &isa_fun_ops;
     446
     447        printf(NAME ": Binding function %s.\n", fun->fnode->name);
     448
     449        /* XXX Handle error */
     450        (void) ddf_fun_bind(fun->fnode);
    454451
    455452        return fun_conf;
     
    482479        printf(NAME ": adding a 'ctl' function\n");
    483480
    484         function_t *ctl = create_function();
    485         ctl->ftype = fun_exposed;
    486         ctl->name = "ctl";
    487         register_function(ctl, dev);
     481        function_t *ctl = ddf_fun_create(dev, fun_exposed, "ctl");
     482        if (ctl == NULL) {
     483                printf(NAME ": Error creating control function.\n");
     484                return EXDEV;
     485        }
     486
     487        if (ddf_fun_bind(ctl) != EOK) {
     488                printf(NAME ": Error binding control function.\n");
     489                return EXDEV;
     490        }
    488491
    489492        /* Add functions as specified in the configuration file. */
  • uspace/drv/ns8250/ns8250.c

    r7df0477e r97a62fe  
    761761        }
    762762       
    763         fun = create_function();
    764         fun->ftype = fun_exposed;
    765         fun->name = "a";
     763        fun = ddf_fun_create(dev, fun_exposed, "a");
     764        if (fun == NULL) {
     765                printf(NAME ": error creating function.\n");
     766                goto fail;
     767        }
    766768       
    767769        /* Set device operations. */
    768770        fun->ops = &ns8250_dev_ops;
    769         register_function(fun, dev);
     771        rc = ddf_fun_bind(fun);
     772        if (rc != EOK) {
     773                printf(NAME ": error binding function.\n");
     774                goto fail;
     775        }
     776
    770777        ns->fun = fun;
    771778       
     
    777784        return EOK;
    778785fail:
     786        if (fun != NULL)
     787                ddf_fun_destroy(fun);
    779788        if (need_cleanup)
    780789                ns8250_dev_cleanup(ns);
  • uspace/drv/pciintel/pci.c

    r7df0477e r97a62fe  
    6969
    7070/** Obtain PCI bus soft-state from function soft-state */
    71 #define PCI_BUS_FROM_FUN(fun) (PCI_BUS(fun->fnode->dev))
     71#define PCI_BUS_FROM_FUN(fun) ((fun)->busptr)
    7272
    7373static hw_resource_list_t *pciintel_get_resources(function_t *fnode)
     
    361361void pci_bus_scan(pci_bus_t *bus, int bus_num)
    362362{
    363         function_t *fnode = create_function();
    364         pci_fun_t *fun = pci_fun_new();
    365         fnode->driver_data = fun;
     363        function_t *fnode;
     364        pci_fun_t *fun;
    366365       
    367366        int child_bus = 0;
     
    370369        uint8_t header_type;
    371370       
    372         /* We need this early, before registering. */
    373         fun->fnode = fnode;
    374         fnode->dev = bus->dnode;
    375         fnode->driver_data = fun;
     371        fun = pci_fun_new(bus);
    376372       
    377373        for (dnum = 0; dnum < 32; dnum++) {
     
    402398                        header_type = header_type & 0x7F;
    403399                       
    404                         pci_fun_create_name(fun);
     400                        char *fun_name = pci_fun_create_name(fun);
     401                        if (fun_name == NULL) {
     402                                printf(NAME ": out of memory.\n");
     403                                return;
     404                        }
     405                       
     406                        fnode = ddf_fun_create(bus->dnode, fun_inner, fun_name);
     407                        if (fnode == NULL) {
     408                                printf(NAME ": error creating function.\n");
     409                                return;
     410                        }
     411                       
     412                        free(fun_name);
     413                        fun->fnode = fnode;
    405414                       
    406415                        pci_alloc_resource_list(fun);
     
    408417                        pci_read_interrupt(fun);
    409418                       
    410                         fnode->ftype = fun_inner;
    411419                        fnode->ops = &pci_fun_ops;
     420                        fnode->driver_data = fun;
    412421                       
    413422                        printf(NAME ": adding new function %s.\n",
     
    416425                        pci_fun_create_match_ids(fun);
    417426                       
    418                         if (register_function(fnode, bus->dnode) != EOK) {
     427                        if (ddf_fun_bind(fnode) != EOK) {
    419428                                pci_clean_resource_list(fun);
    420429                                clean_match_ids(&fnode->match_ids);
     
    434443                        }
    435444                       
    436                         /* Alloc new aux. fun. structure. */
    437                         fnode = create_function();
    438 
    439                         /* We need this early, before registering. */
    440                         fnode->dev = bus->dnode;
    441 
    442                         fun = pci_fun_new();
    443                         fun->fnode = fnode;
    444                         fnode->driver_data = fun;
     445                        fun = pci_fun_new(bus);
    445446                }
    446447        }
    447448       
    448449        if (fun->vendor_id == 0xffff) {
    449                 delete_function(fnode);
    450450                /* Free the auxiliary function structure. */
    451451                pci_fun_delete(fun);
     
    455455static int pci_add_device(device_t *dnode)
    456456{
     457        pci_bus_t *bus = NULL;
     458        function_t *ctl = NULL;
     459        bool got_res = false;
    457460        int rc;
    458461       
    459462        printf(NAME ": pci_add_device\n");
    460        
    461         pci_bus_t *bus = pci_bus_new();
     463        dnode->parent_phone = -1;
     464       
     465        bus = pci_bus_new();
    462466        if (bus == NULL) {
    463467                printf(NAME ": pci_add_device allocation failed.\n");
    464                 return ENOMEM;
     468                rc = ENOMEM;
     469                goto fail;
    465470        }
    466471        bus->dnode = dnode;
     
    472477                printf(NAME ": pci_add_device failed to connect to the "
    473478                    "parent's driver.\n");
    474                 pci_bus_delete(bus);
    475                 return dnode->parent_phone;
     479                rc = dnode->parent_phone;
     480                goto fail;
    476481        }
    477482       
     
    482487                printf(NAME ": pci_add_device failed to get hw resources for "
    483488                    "the device.\n");
    484                 pci_bus_delete(bus);
    485                 async_hangup(dnode->parent_phone);
    486                 return rc;
    487         }
     489                goto fail;
     490        }
     491        got_res = true;
    488492       
    489493        printf(NAME ": conf_addr = %" PRIx64 ".\n",
     
    500504            &bus->conf_addr_port)) {
    501505                printf(NAME ": failed to enable configuration ports.\n");
    502                 pci_bus_delete(bus);
    503                 async_hangup(dnode->parent_phone);
    504                 hw_res_clean_resource_list(&hw_resources);
    505                 return EADDRNOTAVAIL;
     506                rc = EADDRNOTAVAIL;
     507                goto fail;
    506508        }
    507509        bus->conf_data_port = (char *) bus->conf_addr_port + 4;
     
    510512        printf(NAME ": adding a 'ctl' function\n");
    511513       
    512         function_t *ctl = create_function();
    513         ctl->ftype = fun_exposed;
    514         ctl->name = "ctl";
    515         register_function(ctl, dnode);
     514        ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
     515        if (ctl == NULL) {
     516                printf(NAME ": error creating control function.\n");
     517                rc = ENOMEM;
     518                goto fail;
     519        }
     520       
     521        rc = ddf_fun_bind(ctl);
     522        if (rc != EOK) {
     523                printf(NAME ": error binding control function.\n");
     524                goto fail;
     525        }
    516526       
    517527        /* Enumerate functions. */
     
    522532       
    523533        return EOK;
     534       
     535fail:
     536        if (bus != NULL)
     537                pci_bus_delete(bus);
     538        if (dnode->parent_phone >= 0)
     539                async_hangup(dnode->parent_phone);
     540        if (got_res)
     541                hw_res_clean_resource_list(&hw_resources);
     542        if (ctl != NULL)
     543                ddf_fun_destroy(ctl);
     544
     545        return rc;
    524546}
    525547
     
    529551}
    530552
    531 pci_fun_t *pci_fun_new(void)
    532 {
    533         pci_fun_t *res;
    534        
    535         res = (pci_fun_t *) calloc(1, sizeof(pci_fun_t));
    536         return res;
     553pci_fun_t *pci_fun_new(pci_bus_t *bus)
     554{
     555        pci_fun_t *fun;
     556       
     557        fun = (pci_fun_t *) calloc(1, sizeof(pci_fun_t));
     558        if (fun == NULL)
     559                return NULL;
     560
     561        fun->busptr = bus;
     562        return fun;
    537563}
    538564
     
    551577}
    552578
    553 void pci_fun_create_name(pci_fun_t *fun)
     579char *pci_fun_create_name(pci_fun_t *fun)
    554580{
    555581        char *name = NULL;
     
    557583        asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
    558584            fun->fn);
    559         fun->fnode->name = name;
     585        return name;
    560586}
    561587
  • uspace/drv/pciintel/pci.h

    r7df0477e r97a62fe  
    4545#define PCI_MAX_HW_RES 8
    4646
     47typedef struct pciintel_bus {
     48        /** DDF device node */
     49        device_t *dnode;
     50        uint32_t conf_io_addr;
     51        void *conf_data_port;
     52        void *conf_addr_port;
     53        fibril_mutex_t conf_mutex;
     54} pci_bus_t;
     55
    4756typedef struct pci_fun_data {
     57        pci_bus_t *busptr;
    4858        function_t *fnode;
    4959
     
    5565        hw_resource_list_t hw_resources;
    5666} pci_fun_t;
    57 
    58 typedef struct pciintel_bus {
    59         /** DDF device node */
    60         device_t *dnode;
    61         uint32_t conf_io_addr;
    62         void *conf_data_port;
    63         void *conf_addr_port;
    64         fibril_mutex_t conf_mutex;
    65 } pci_bus_t;
    6667
    6768extern void pci_fun_create_match_ids(pci_fun_t *);
     
    7980extern void pci_add_interrupt(pci_fun_t *, int);
    8081
    81 extern pci_fun_t *pci_fun_new(void);
     82extern pci_fun_t *pci_fun_new(pci_bus_t *);
    8283extern void pci_fun_init(pci_fun_t *, int, int, int);
    8384extern void pci_fun_delete(pci_fun_t *);
    84 extern void pci_fun_create_name(pci_fun_t *);
     85extern char *pci_fun_create_name(pci_fun_t *);
    8586
    8687extern void pci_bus_scan(pci_bus_t *, int);
  • uspace/drv/rootpc/rootpc.c

    r7df0477e r97a62fe  
    125125       
    126126        /* Create new device. */
    127         fnode = create_function();
     127        fnode = ddf_fun_create(dev, fun_inner, name);
    128128        if (fnode == NULL)
    129129                goto failure;
    130130       
    131         fnode->name = name;
    132131        fnode->driver_data = fun;
    133         fnode->ftype = fun_inner;
    134132       
    135133        /* Initialize match id list */
     
    146144       
    147145        /* Register function. */
    148         if (register_function(fnode, dev) != EOK)
     146        if (ddf_fun_bind(fnode) != EOK) {
     147                printf(NAME ": error binding function %s.\n", name);
    149148                goto failure;
     149        }
    150150        printf(NAME ": registered function handle = %u\n", fnode->handle);
    151151       
     
    156156                match_id->id = NULL;
    157157       
    158         if (fnode != NULL) {
    159                 fnode->name = NULL;
    160                 delete_function(fnode);
    161         }
     158        if (fnode != NULL)
     159                ddf_fun_destroy(fnode);
    162160       
    163161        printf(NAME ": failed to add function '%s'.\n", name);
  • uspace/drv/test1/test1.c

    r7df0477e r97a62fe  
    9292{
    9393        function_t *fun_a;
     94        int rc;
    9495
    9596        printf(NAME ": add_device(name=\"%s\", handle=%d)\n",
    9697            dev->name, (int) dev->handle);
    9798
    98         fun_a = create_function();
    99         fun_a->ftype = fun_exposed;
    100         fun_a->name = "a";
     99        fun_a = ddf_fun_create(dev, fun_exposed, "a");
     100        if (fun_a == NULL) {
     101                printf(NAME ": error creating function 'a'.\n");
     102                return ENOMEM;
     103        }
    101104
    102         register_function(fun_a, dev);
     105        rc = ddf_fun_bind(fun_a);
     106        if (rc != EOK) {
     107                printf(NAME ": error binding function 'a'.\n");
     108                return rc;
     109        }
    103110
    104111        add_function_to_class(fun_a, "virtual");
  • uspace/drv/test2/test2.c

    r7df0477e r97a62fe  
    8282{
    8383        device_t *dev = (device_t *) arg;
    84         function_t *fun;
     84        function_t *fun_a;
     85        int rc;
    8586
    8687        async_usleep(1000);
     
    9192            "test1", "virtual&test1", 10);
    9293
    93         fun = create_function();
    94         fun->ftype = fun_exposed;
    95         fun->name = "a";
     94        fun_a = ddf_fun_create(dev, fun_exposed, "a");
     95        if (fun_a == NULL) {
     96                printf(NAME ": error creating function 'a'.\n");
     97                return ENOMEM;
     98        }
    9699
    97         register_function(fun, dev);
     100        rc = ddf_fun_bind(fun_a);
     101        if (rc != EOK) {
     102                printf(NAME ": error binding function 'a'.\n");
     103                return rc;
     104        }
    98105
    99         add_function_to_class(fun, "virtual");
     106        add_function_to_class(fun_a, "virtual");
    100107
    101108        return EOK;
  • uspace/lib/drv/generic/driver.c

    r7df0477e r97a62fe  
    477477 * @return              The device structure.
    478478 */
    479 function_t *create_function(void)
     479static function_t *create_function(void)
    480480{
    481481        function_t *fun;
    482482
    483         fun = malloc(sizeof(function_t));
     483        fun = calloc(1, sizeof(function_t));
    484484        if (fun == NULL)
    485485                return NULL;
    486486
    487         memset(fun, 0, sizeof(device_t));
    488 
    489487        init_match_ids(&fun->match_ids);
    490488        link_initialize(&fun->link);
     
    506504 * @param dev           The device structure.
    507505 */
    508 void delete_function(function_t *fun)
     506static void delete_function(function_t *fun)
    509507{
    510508        clean_match_ids(&fun->match_ids);
     
    514512}
    515513
     514/** Create a DDF function node.
     515 *
     516 * Create a DDF function (in memory). Both child devices and external clients
     517 * communicate with a device via its functions.
     518 *
     519 * The created function node is fully formed, but only exists in the memory
     520 * of the client task. In order to be visible to the system, the function
     521 * must be bound using ddf_fun_bind().
     522 *
     523 * This function should only fail if there is not enough free memory.
     524 * Specifically, this function succeeds even if @a dev already has
     525 * a (bound) function with the same name.
     526 *
     527 * Type: A function of type fun_inner indicates that DDF should attempt
     528 * to attach child devices to the function. fun_exposed means that
     529 * the function should be exported to external clients (applications).
     530 *
     531 * @param dev           Device to which we are adding function
     532 * @param ftype         Type of function (fun_inner or fun_exposed)
     533 * @param name          Name of function
     534 *
     535 * @return              New function or @c NULL if memory is not available
     536 */
     537function_t *ddf_fun_create(device_t *dev, fun_type_t ftype, const char *name)
     538{
     539        function_t *fun;
     540
     541        fun = create_function();
     542        if (fun == NULL)
     543                return NULL;
     544
     545        fun->bound = false;
     546        fun->dev = dev;
     547        fun->ftype = ftype;
     548
     549        fun->name = str_dup(name);
     550        if (fun->name == NULL) {
     551                delete_function(fun);
     552                return NULL;
     553        }
     554
     555        return fun;
     556}
     557
     558/** Destroy DDF function node.
     559 *
     560 * Destroy a function previously created with ddf_fun_create(). The function
     561 * must not be bound.
     562 *
     563 * @param fun           Function to destroy
     564 */
     565void ddf_fun_destroy(function_t *fun)
     566{
     567        assert(fun->bound == false);
     568        delete_function(fun);
     569}
     570
    516571void *function_get_ops(function_t *fun, dev_inferface_idx_t idx)
    517572{
     
    522577}
    523578
    524 int register_function(function_t *fun, device_t *dev)
     579/** Bind a function node.
     580 *
     581 * Bind the specified function to the system. This effectively makes
     582 * the function visible to the system (uploads it to the server).
     583 *
     584 * This function can fail for several reasons. Specifically,
     585 * it will fail if the device already has a bound function of
     586 * the same name.
     587 *
     588 * @param fun           Function to bind
     589 * @return              EOK on success or negative error code
     590 */
     591int ddf_fun_bind(function_t *fun)
    525592{
    526593        assert(fun->name != NULL);
    527594       
    528595        int res;
    529        
    530         fun->dev = dev;
    531596       
    532597        add_to_functions_list(fun);
    533598        res = devman_add_function(fun->name, fun->ftype, &fun->match_ids,
    534             dev->handle, &fun->handle);
     599            fun->dev->handle, &fun->handle);
    535600        if (res != EOK) {
    536601                remove_from_functions_list(fun);
     
    538603        }
    539604       
     605        fun->bound = true;
    540606        return res;
    541607}
     
    556622        int rc;
    557623       
    558         fun = create_function();
     624        fun = ddf_fun_create(dev, fun_inner, fun_name);
    559625        if (fun == NULL) {
    560626                rc = ENOMEM;
    561627                goto failure;
    562628        }
    563        
    564         fun->dev = dev;
    565         fun->name = fun_name;
    566         fun->ftype = fun_inner;
    567629       
    568630        m_id = create_match_id();
     
    576638        add_match_id(&fun->match_ids, m_id);
    577639       
    578         rc = register_function(fun, dev);
     640        rc = ddf_fun_bind(fun);
    579641        if (rc != EOK)
    580642                goto failure;
  • uspace/lib/drv/include/driver.h

    r7df0477e r97a62fe  
    115115/** Function structure */
    116116struct function {
     117        /** True if bound to the device manager */
     118        bool bound;
    117119        /** Function indentifier (asigned by device manager) */
    118120        devman_handle_t handle;
     
    157159int driver_main(driver_t *);
    158160
    159 /** Create new device structure.
    160  *
    161  * @return              The device structure.
    162  */
    163 extern function_t *create_function(void);
    164 extern void delete_function(function_t *);
     161extern function_t *ddf_fun_create(device_t *, fun_type_t, const char *);
     162extern void ddf_fun_destroy(function_t *);
     163extern int ddf_fun_bind(function_t *);
     164
    165165extern void *function_get_ops(function_t *, dev_inferface_idx_t);
    166166
    167 extern int register_function(function_t *, device_t *);
    168167extern int register_function_wrapper(device_t *, const char *, const char *,
    169168    int);
Note: See TracChangeset for help on using the changeset viewer.