| 1 | /*
|
|---|
| 2 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 | /** @addtogroup libusbhost
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * Virtual base for usb bus implementations.
|
|---|
| 33 | *
|
|---|
| 34 | * The purpose of this structure is to keep information about connected devices
|
|---|
| 35 | * and endpoints, manage available bandwidth and the toggle bit flipping.
|
|---|
| 36 | *
|
|---|
| 37 | * The generic implementation is provided for USB 1 and 2 in usb2_bus.c. Some
|
|---|
| 38 | * details in [OUE]HCI are solved through overriding some functions. XHCI does
|
|---|
| 39 | * not need the bookkeeping functionality, because addresses are managed by HC
|
|---|
| 40 | * itself.
|
|---|
| 41 | */
|
|---|
| 42 | #ifndef LIBUSBHOST_HOST_BUS_H
|
|---|
| 43 | #define LIBUSBHOST_HOST_BUS_H
|
|---|
| 44 |
|
|---|
| 45 | #include <usb/usb.h>
|
|---|
| 46 |
|
|---|
| 47 | #include <assert.h>
|
|---|
| 48 | #include <fibril_synch.h>
|
|---|
| 49 | #include <stdbool.h>
|
|---|
| 50 |
|
|---|
| 51 | typedef struct hcd hcd_t;
|
|---|
| 52 | typedef struct endpoint endpoint_t;
|
|---|
| 53 | typedef struct bus bus_t;
|
|---|
| 54 | typedef struct ddf_fun ddf_fun_t;
|
|---|
| 55 | typedef struct usb_transfer_batch usb_transfer_batch_t;
|
|---|
| 56 |
|
|---|
| 57 | typedef struct device {
|
|---|
| 58 | /* Device tree keeping */
|
|---|
| 59 | link_t link;
|
|---|
| 60 | list_t devices;
|
|---|
| 61 | fibril_mutex_t guard;
|
|---|
| 62 |
|
|---|
| 63 | /* Associated DDF function, if any */
|
|---|
| 64 | ddf_fun_t *fun;
|
|---|
| 65 |
|
|---|
| 66 | /* Invalid for the roothub device */
|
|---|
| 67 | unsigned port;
|
|---|
| 68 | struct device *hub;
|
|---|
| 69 |
|
|---|
| 70 | /* Transaction translator */
|
|---|
| 71 | usb_tt_address_t tt;
|
|---|
| 72 |
|
|---|
| 73 | /* The following are not set by the library */
|
|---|
| 74 | usb_speed_t speed;
|
|---|
| 75 | usb_address_t address;
|
|---|
| 76 |
|
|---|
| 77 | /* This structure is meant to be extended by overriding. */
|
|---|
| 78 | } device_t;
|
|---|
| 79 |
|
|---|
| 80 | typedef struct {
|
|---|
| 81 | int (*enumerate_device)(bus_t *, hcd_t *, device_t *);
|
|---|
| 82 | int (*remove_device)(bus_t *, hcd_t *, device_t *);
|
|---|
| 83 |
|
|---|
| 84 | endpoint_t *(*create_endpoint)(bus_t *);
|
|---|
| 85 | int (*register_endpoint)(bus_t *, endpoint_t *);
|
|---|
| 86 | int (*release_endpoint)(bus_t *, endpoint_t *);
|
|---|
| 87 | endpoint_t *(*find_endpoint)(bus_t *, usb_target_t, usb_direction_t);
|
|---|
| 88 | void (*destroy_endpoint)(endpoint_t *); /**< Optional */
|
|---|
| 89 | bool (*endpoint_get_toggle)(endpoint_t *); /**< Optional */
|
|---|
| 90 | void (*endpoint_set_toggle)(endpoint_t *, bool); /**< Optional */
|
|---|
| 91 |
|
|---|
| 92 | int (*request_address)(bus_t *, usb_address_t*, bool, usb_speed_t);
|
|---|
| 93 | int (*release_address)(bus_t *, usb_address_t);
|
|---|
| 94 |
|
|---|
| 95 | int (*reset_toggle)(bus_t *, usb_target_t, bool);
|
|---|
| 96 |
|
|---|
| 97 | size_t (*count_bw) (endpoint_t *, size_t);
|
|---|
| 98 |
|
|---|
| 99 | usb_transfer_batch_t *(*create_batch)(bus_t *, endpoint_t *); /**< Optional */
|
|---|
| 100 | void (*destroy_batch)(usb_transfer_batch_t *); /**< Optional */
|
|---|
| 101 | } bus_ops_t;
|
|---|
| 102 |
|
|---|
| 103 | /** Endpoint management structure */
|
|---|
| 104 | typedef struct bus {
|
|---|
| 105 | /* Synchronization of ops */
|
|---|
| 106 | fibril_mutex_t guard;
|
|---|
| 107 |
|
|---|
| 108 | size_t device_size;
|
|---|
| 109 |
|
|---|
| 110 | /* Do not call directly, ops are synchronized. */
|
|---|
| 111 | bus_ops_t ops;
|
|---|
| 112 |
|
|---|
| 113 | /* This structure is meant to be extended by overriding. */
|
|---|
| 114 | } bus_t;
|
|---|
| 115 |
|
|---|
| 116 | void bus_init(bus_t *, size_t);
|
|---|
| 117 | int device_init(device_t *);
|
|---|
| 118 |
|
|---|
| 119 | extern int bus_add_ep(bus_t *bus, device_t *device, usb_endpoint_t endpoint,
|
|---|
| 120 | usb_direction_t dir, usb_transfer_type_t type, size_t max_packet_size,
|
|---|
| 121 | unsigned packets, size_t size);
|
|---|
| 122 | extern int bus_remove_ep(bus_t *bus, usb_target_t target, usb_direction_t dir);
|
|---|
| 123 |
|
|---|
| 124 | int device_set_default_name(device_t *);
|
|---|
| 125 |
|
|---|
| 126 | int bus_enumerate_device(bus_t *, hcd_t *, device_t *);
|
|---|
| 127 | int bus_remove_device(bus_t *, hcd_t *, device_t *);
|
|---|
| 128 |
|
|---|
| 129 | endpoint_t *bus_create_endpoint(bus_t *);
|
|---|
| 130 | int bus_register_endpoint(bus_t *, endpoint_t *);
|
|---|
| 131 | int bus_release_endpoint(bus_t *, endpoint_t *);
|
|---|
| 132 | endpoint_t *bus_find_endpoint(bus_t *, usb_target_t, usb_direction_t);
|
|---|
| 133 |
|
|---|
| 134 | size_t bus_count_bw(endpoint_t *, size_t);
|
|---|
| 135 |
|
|---|
| 136 | int bus_request_address(bus_t *, usb_address_t *, bool, usb_speed_t);
|
|---|
| 137 | int bus_release_address(bus_t *, usb_address_t);
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | static inline int bus_reserve_default_address(bus_t *bus, usb_speed_t speed) {
|
|---|
| 141 | usb_address_t addr = USB_ADDRESS_DEFAULT;
|
|---|
| 142 |
|
|---|
| 143 | const int r = bus_request_address(bus, &addr, true, speed);
|
|---|
| 144 | assert(addr == USB_ADDRESS_DEFAULT);
|
|---|
| 145 | return r;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | static inline int bus_release_default_address(bus_t *bus) {
|
|---|
| 149 | return bus_release_address(bus, USB_ADDRESS_DEFAULT);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | int bus_reset_toggle(bus_t *, usb_target_t, bool);
|
|---|
| 153 |
|
|---|
| 154 | #endif
|
|---|
| 155 | /**
|
|---|
| 156 | * @}
|
|---|
| 157 | */
|
|---|