Changeset d369b3b in mainline


Ignore:
Timestamp:
2018-01-25T02:05:57Z (6 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
296d22fc
Parents:
b357377
git-author:
Ondřej Hlavatý <aearsis@…> (2018-01-25 01:52:13)
git-committer:
Ondřej Hlavatý <aearsis@…> (2018-01-25 02:05:57)
Message:

usb2_bus: no longer be a bus

As the number of implemented functions got to 3, it's not so beneficial
to inherit usb2 bus to get the functionality. Overall, four trampolines
needed to be added, which is an acceptable number.

Now, the usb2_bus has become a usb2_bus_helper, to be used as
a companion to the common bus.

This is mostly a preparation to remove the runtime binding of the bus
methods.

Location:
uspace
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/ehci/ehci_bus.c

    rb357377 rd369b3b  
    5757}
    5858
     59static int ehci_device_enumerate(device_t *dev)
     60{
     61        ehci_bus_t *bus = (ehci_bus_t *) dev->bus;
     62        return usb2_bus_device_enumerate(&bus->helper, dev);
     63}
    5964
    6065/** Creates new hcd endpoint representation.
     
    101106        ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
    102107
    103         const int err = usb2_bus_ops.endpoint_register(ep);
     108        const int err = usb2_bus_endpoint_register(&bus->helper, ep);
    104109        if (err)
    105110                return err;
     
    119124        assert(ep);
    120125
    121         usb2_bus_ops.endpoint_unregister(ep);
     126        usb2_bus_endpoint_unregister(&bus->helper, ep);
    122127        hc_dequeue_endpoint(hc, ep);
    123128        /*
     
    154159
    155160static const bus_ops_t ehci_bus_ops = {
    156         .parent = &usb2_bus_ops,
    157 
    158161        .interrupt = ehci_hc_interrupt,
    159162        .status = ehci_hc_status,
     163
     164        .device_enumerate = ehci_device_enumerate,
    160165
    161166        .endpoint_destroy = ehci_endpoint_destroy,
     
    174179        assert(bus);
    175180
    176         usb2_bus_t *usb2_bus = (usb2_bus_t *) bus;
    177181        bus_t *bus_base = (bus_t *) bus;
     182        bus_init(bus_base, sizeof(device_t));
     183        bus_base->ops = &ehci_bus_ops;
    178184
    179         usb2_bus_init(usb2_bus, &bandwidth_accounting_usb2);
    180         bus_base->ops = &ehci_bus_ops;
     185        usb2_bus_helper_init(&bus->helper, &bandwidth_accounting_usb2);
    181186
    182187        bus->hc = hc;
  • uspace/drv/bus/usb/ehci/ehci_bus.h

    rb357377 rd369b3b  
    6464
    6565typedef struct {
    66         usb2_bus_t base;
     66        bus_t base;
     67        usb2_bus_helper_t helper;
    6768        hc_t *hc;
    6869} ehci_bus_t;
  • uspace/drv/bus/usb/ohci/ohci_bus.c

    rb357377 rd369b3b  
    5757}
    5858
     59static int ohci_device_enumerate(device_t *dev)
     60{
     61        ohci_bus_t *bus = (ohci_bus_t *) dev->bus;
     62        return usb2_bus_device_enumerate(&bus->helper, dev);
     63}
     64
    5965/** Creates new hcd endpoint representation.
    6066 */
     
    109115        ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
    110116
    111         const int err = usb2_bus_ops.endpoint_register(ep);
     117        const int err = usb2_bus_endpoint_register(&bus->helper, ep);
    112118        if (err)
    113119                return err;
     
    126132        assert(ep);
    127133
    128         usb2_bus_ops.endpoint_unregister(ep);
     134        usb2_bus_endpoint_unregister(&bus->helper, ep);
    129135        hc_dequeue_endpoint(bus->hc, ep);
    130136
     
    162168
    163169static const bus_ops_t ohci_bus_ops = {
    164         .parent = &usb2_bus_ops,
    165 
    166170        .interrupt = ohci_hc_interrupt,
    167171        .status = ohci_hc_status,
     172
     173        .device_enumerate = ohci_device_enumerate,
    168174
    169175        .endpoint_destroy = ohci_endpoint_destroy,
     
    183189        assert(bus);
    184190
    185         usb2_bus_t *usb2_bus = (usb2_bus_t *) bus;
    186191        bus_t *bus_base = (bus_t *) bus;
    187 
    188         usb2_bus_init(usb2_bus, &bandwidth_accounting_usb11);
     192        bus_init(bus_base, sizeof(device_t));
    189193        bus_base->ops = &ohci_bus_ops;
     194
     195        usb2_bus_helper_init(&bus->helper, &bandwidth_accounting_usb11);
    190196
    191197        bus->hc = hc;
  • uspace/drv/bus/usb/ohci/ohci_bus.h

    rb357377 rd369b3b  
    6060
    6161typedef struct {
    62         usb2_bus_t base;
     62        bus_t base;
     63        usb2_bus_helper_t helper;
    6364        hc_t *hc;
    6465} ohci_bus_t;
  • uspace/drv/bus/usb/uhci/hc.c

    rb357377 rd369b3b  
    329329        hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
    330330
    331         const int err = usb2_bus_ops.endpoint_register(ep);
     331        const int err = usb2_bus_endpoint_register(&hc->bus_helper, ep);
    332332        if (err)
    333333                return err;
     
    350350{
    351351        hc_t * const hc = bus_to_hc(endpoint_get_bus(ep));
    352         usb2_bus_ops.endpoint_unregister(ep);
     352        usb2_bus_endpoint_unregister(&hc->bus_helper, ep);
    353353
    354354        // Check for the roothub, as it does not schedule into lists
     
    406406}
    407407
     408static int device_enumerate(device_t *dev)
     409{
     410        hc_t * const hc = bus_to_hc(dev->bus);
     411        return usb2_bus_device_enumerate(&hc->bus_helper, dev);
     412}
     413
    408414static int hc_status(bus_t *, uint32_t *);
    409415static int hc_schedule(usb_transfer_batch_t *);
    410416
    411417static const bus_ops_t uhci_bus_ops = {
    412         .parent = &usb2_bus_ops,
    413 
    414418        .interrupt = hc_interrupt,
    415419        .status = hc_status,
     420
     421        .device_enumerate = device_enumerate,
    416422
    417423        .endpoint_create = endpoint_create,
     
    438444        assert(instance);
    439445
    440         usb2_bus_init(&instance->bus, &bandwidth_accounting_usb11);
    441 
    442         bus_t *bus = (bus_t *) &instance->bus;
    443         bus->ops = &uhci_bus_ops;
    444 
    445         hc_device_setup(&instance->base, bus);
     446        usb2_bus_helper_init(&instance->bus_helper, &bandwidth_accounting_usb11);
     447
     448        bus_init(&instance->bus, sizeof(device_t));
     449        instance->bus.ops = &uhci_bus_ops;
     450
     451        hc_device_setup(&instance->base, &instance->bus);
    446452
    447453        /* Init USB frame list page */
  • uspace/drv/bus/usb/uhci/hc.h

    rb357377 rd369b3b  
    104104
    105105        uhci_rh_t rh;
    106         usb2_bus_t bus;
     106        bus_t bus;
     107        usb2_bus_helper_t bus_helper;
     108
    107109        /** Addresses of I/O registers */
    108110        uhci_regs_t *registers;
  • uspace/drv/bus/usb/vhc/transfer.c

    rb357377 rd369b3b  
    163163}
    164164
     165static int device_enumerate(device_t *device)
     166{
     167        vhc_data_t *vhc = bus_to_vhc(device->bus);
     168        return usb2_bus_device_enumerate(&vhc->bus_helper, device);
     169}
     170
     171static int endpoint_register(endpoint_t *endpoint)
     172{
     173        vhc_data_t *vhc = bus_to_vhc(endpoint->device->bus);
     174        return usb2_bus_endpoint_register(&vhc->bus_helper, endpoint);
     175}
     176
     177static void endpoint_unregister(endpoint_t *endpoint)
     178{
     179        vhc_data_t *vhc = bus_to_vhc(endpoint->device->bus);
     180        usb2_bus_endpoint_unregister(&vhc->bus_helper, endpoint);
     181
     182        // TODO: abort transfer?
     183}
     184
    165185static const bus_ops_t vhc_bus_ops = {
    166         .parent = &usb2_bus_ops,
    167 
    168186        .batch_create = batch_create,
    169187        .batch_schedule = vhc_schedule,
     188
     189        .device_enumerate = device_enumerate,
     190        .endpoint_register = endpoint_register,
     191        .endpoint_unregister = endpoint_unregister,
    170192};
    171193
     
    175197        list_initialize(&instance->devices);
    176198        fibril_mutex_initialize(&instance->guard);
    177         usb2_bus_init(&instance->bus, &bandwidth_accounting_usb11);
    178         instance->bus.base.ops = &vhc_bus_ops;
     199        bus_init(&instance->bus, sizeof(device_t));
     200        usb2_bus_helper_init(&instance->bus_helper, &bandwidth_accounting_usb11);
     201        instance->bus.ops = &vhc_bus_ops;
    179202        return virthub_init(&instance->hub, "root hub");
    180203}
  • uspace/drv/bus/usb/vhc/vhcd.h

    rb357377 rd369b3b  
    6060        hc_device_t base;
    6161
    62         usb2_bus_t bus;
     62        bus_t bus;
     63        usb2_bus_helper_t bus_helper;
     64
    6365        ddf_fun_t *virtual_fun;
    6466        list_t devices;
  • uspace/lib/usbhost/include/usb/host/usb2_bus.h

    rb357377 rd369b3b  
    4747typedef struct endpoint endpoint_t;
    4848
    49 /** Endpoint management structure */
    50 typedef struct usb2_bus {
    51         bus_t base;                     /**< Inheritance - keep this first */
    52 
     49/** Endpoint and bandwidth management structure */
     50typedef struct usb2_bus_helper {
    5351        /** Map of occupied addresses */
    5452        bool address_occupied [USB_ADDRESS_COUNT];
     
    6159        /* Configured bandwidth accounting */
    6260        const bandwidth_accounting_t *bw_accounting;
    63 } usb2_bus_t;
     61} usb2_bus_helper_t;
    6462
    65 extern const bus_ops_t usb2_bus_ops;
     63extern void usb2_bus_helper_init(usb2_bus_helper_t *, const bandwidth_accounting_t *);
    6664
    67 extern void usb2_bus_init(usb2_bus_t *, const bandwidth_accounting_t *);
     65extern int usb2_bus_device_enumerate(usb2_bus_helper_t *, device_t *);
     66extern int usb2_bus_endpoint_register(usb2_bus_helper_t *, endpoint_t *);
     67extern void usb2_bus_endpoint_unregister(usb2_bus_helper_t *, endpoint_t *);
    6868
    6969#endif
  • uspace/lib/usbhost/src/usb2_bus.c

    rb357377 rd369b3b  
    5454
    5555/**
    56  * Ops receive generic bus_t pointer.
    57  */
    58 static inline usb2_bus_t *bus_to_usb2_bus(bus_t *bus_base)
    59 {
    60         assert(bus_base);
    61         return (usb2_bus_t *) bus_base;
    62 }
    63 
    64 /**
    6556 * Request a new address. A free address is found and marked as occupied.
    6657 *
     
    7162 * @param addr Pointer to requested address value, place to store new address
    7263 */
    73 static int request_address(usb2_bus_t *bus, usb_address_t *addr)
     64static int request_address(usb2_bus_helper_t *helper, usb_address_t *addr)
    7465{
    7566        // Find a free address
    76         usb_address_t new_address = bus->last_address;
     67        usb_address_t new_address = helper->last_address;
    7768        do {
    7869                new_address = (new_address + 1) % USB_ADDRESS_COUNT;
    7970                if (new_address == USB_ADDRESS_DEFAULT)
    8071                        new_address = 1;
    81                 if (new_address == bus->last_address)
     72                if (new_address == helper->last_address)
    8273                        return ENOSPC;
    83         } while (bus->address_occupied[new_address]);
    84         bus->last_address = new_address;
     74        } while (helper->address_occupied[new_address]);
     75        helper->last_address = new_address;
    8576
    8677        *addr = new_address;
    87         bus->address_occupied[*addr] = true;
     78        helper->address_occupied[*addr] = true;
    8879
    8980        return EOK;
     
    9384 * Mark address as free.
    9485 */
    95 static void release_address(usb2_bus_t *bus, usb_address_t address)
    96 {
    97         bus->address_occupied[address] = false;
     86static void release_address(usb2_bus_helper_t *helper, usb_address_t address)
     87{
     88        helper->address_occupied[address] = false;
    9889}
    9990
     
    109100 * Configure the device with the new address,
    110101 */
    111 static int address_device(device_t *dev)
     102static int address_device(usb2_bus_helper_t *helper, device_t *dev)
    112103{
    113104        int err;
    114 
    115         usb2_bus_t *bus = (usb2_bus_t *) dev->bus;
    116105
    117106        /* The default address is currently reserved for this device */
     
    120109        /** Reserve address early, we want pretty log messages */
    121110        usb_address_t address = USB_ADDRESS_DEFAULT;
    122         if ((err = request_address(bus, &address))) {
     111        if ((err = request_address(helper, &address))) {
    123112                usb_log_error("Failed to reserve new address: %s.",
    124113                    str_error(err));
     
    141130        }
    142131
    143         if ((err = hc_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, &bus->base, dev)))
     132        if ((err = hc_get_ep0_max_packet_size(&ep0_desc.endpoint.max_packet_size, dev->bus, dev)))
    144133                goto err_address;
    145134
     
    178167        bus_endpoint_remove(default_ep);
    179168err_address:
    180         release_address(bus, address);
     169        release_address(helper, address);
    181170        return err;
    182171}
     
    186175 * to create a DDF function node with proper characteristics.
    187176 */
    188 static int usb2_bus_device_enumerate(device_t *dev)
     177int usb2_bus_device_enumerate(usb2_bus_helper_t *helper, device_t *dev)
    189178{
    190179        int err;
    191         usb2_bus_t *bus = bus_to_usb2_bus(dev->bus);
    192 
    193180        usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
    194181
    195182        /* Assign an address to the device */
    196         if ((err = address_device(dev))) {
     183        if ((err = address_device(helper, dev))) {
    197184                usb_log_error("Failed to setup address of the new device: %s", str_error(err));
    198185                return err;
     
    202189        if ((err = hc_device_explore(dev))) {
    203190                usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
    204                 release_address(bus, dev->address);
     191                release_address(helper, dev->address);
    205192                return err;
    206193        }
     
    210197
    211198/**
    212  * Call the bus operation to count bandwidth.
    213  *
    214  * @param ep Endpoint on which the transfer will take place.
    215  * @param size The payload size.
    216  */
    217 static ssize_t endpoint_count_bw(endpoint_t *ep)
     199 * Register an endpoint to the bus. Reserves bandwidth.
     200 */
     201int usb2_bus_endpoint_register(usb2_bus_helper_t *helper, endpoint_t *ep)
    218202{
    219203        assert(ep);
    220 
    221         usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
    222 
    223         return bus->bw_accounting->count_bw(ep);
    224 }
    225 
    226 /**
    227  * Register an endpoint to the bus. Reserves bandwidth.
    228  */
    229 static int usb2_bus_register_ep(endpoint_t *ep)
    230 {
    231         usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
    232204        assert(fibril_mutex_is_locked(&ep->device->guard));
     205
     206        size_t bw = helper->bw_accounting->count_bw(ep);
     207
     208        /* Check for available bandwidth */
     209        if (bw > helper->free_bw)
     210                return ENOSPC;
     211
     212        helper->free_bw -= bw;
     213
     214        return EOK;
     215}
     216
     217/**
     218 * Release bandwidth reserved by the given endpoint.
     219 */
     220void usb2_bus_endpoint_unregister(usb2_bus_helper_t *helper, endpoint_t *ep)
     221{
     222        assert(helper);
    233223        assert(ep);
    234224
    235         size_t bw = endpoint_count_bw(ep);
    236 
    237         /* Check for available bandwidth */
    238         if (bw > bus->free_bw)
    239                 return ENOSPC;
    240 
    241         bus->free_bw -= bw;
    242 
    243         return EOK;
    244 }
    245 
    246 /**
    247  * Release bandwidth reserved by the given endpoint.
    248  */
    249 static void usb2_bus_unregister_ep(endpoint_t *ep)
    250 {
    251         usb2_bus_t *bus = bus_to_usb2_bus(ep->device->bus);
    252         assert(ep);
    253 
    254         bus->free_bw += endpoint_count_bw(ep);
    255 }
    256 
    257 const bus_ops_t usb2_bus_ops = {
    258         .device_enumerate = usb2_bus_device_enumerate,
    259         .endpoint_register = usb2_bus_register_ep,
    260         .endpoint_unregister = usb2_bus_unregister_ep,
    261 };
    262 
    263 /** Initialize to default state.
    264  *
    265  * @param bus usb_bus structure, non-null.
    266  * @param available_bandwidth Size of the bandwidth pool.
    267  */
    268 void usb2_bus_init(usb2_bus_t *bus, const bandwidth_accounting_t *bw_accounting)
    269 {
    270         assert(bus);
     225        helper->free_bw += helper->bw_accounting->count_bw(ep);
     226}
     227
     228/**
     229 * Initialize to default state.
     230 *
     231 * @param helper usb_bus_helper structure, non-null.
     232 * @param bw_accounting a structure defining bandwidth accounting
     233 */
     234void usb2_bus_helper_init(usb2_bus_helper_t *helper, const bandwidth_accounting_t *bw_accounting)
     235{
     236        assert(helper);
    271237        assert(bw_accounting);
    272238
    273         bus_init(&bus->base, sizeof(device_t));
    274         bus->base.ops = &usb2_bus_ops;
    275 
    276         bus->bw_accounting = bw_accounting;
    277         bus->free_bw = bw_accounting->available_bandwidth;
     239        helper->bw_accounting = bw_accounting;
     240        helper->free_bw = bw_accounting->available_bandwidth;
    278241
    279242        /*
     
    282245         * address 1.
    283246         */
    284         bus->last_address = USB_ADDRESS_COUNT - 2;
     247        helper->last_address = USB_ADDRESS_COUNT - 2;
    285248}
    286249
Note: See TracChangeset for help on using the changeset viewer.