[41924f30] | 1 | /*
|
---|
[e0a5d4c] | 2 | * Copyright (c) 2018 Ondrej Hlavaty, Petr Manek
|
---|
[41924f30] | 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
|
---|
[5fd9c30] | 35 | * and endpoints, manage available bandwidth and the toggle bit flipping.
|
---|
[41924f30] | 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 |
|
---|
[20eaa82] | 45 | #include <assert.h>
|
---|
[41924f30] | 46 | #include <fibril_synch.h>
|
---|
| 47 | #include <stdbool.h>
|
---|
[32fb6bce] | 48 | #include <usb/host/hcd.h>
|
---|
| 49 | #include <usb/request.h>
|
---|
| 50 | #include <usb/usb.h>
|
---|
| 51 | #include <usbhc_iface.h>
|
---|
[41924f30] | 52 |
|
---|
| 53 | typedef struct hcd hcd_t;
|
---|
| 54 | typedef struct endpoint endpoint_t;
|
---|
| 55 | typedef struct bus bus_t;
|
---|
[20eaa82] | 56 | typedef struct ddf_fun ddf_fun_t;
|
---|
[5fd9c30] | 57 | typedef struct usb_transfer_batch usb_transfer_batch_t;
|
---|
[20eaa82] | 58 |
|
---|
| 59 | typedef struct device {
|
---|
| 60 | /* Device tree keeping */
|
---|
| 61 | link_t link;
|
---|
| 62 | list_t devices;
|
---|
| 63 | fibril_mutex_t guard;
|
---|
| 64 |
|
---|
| 65 | /* Associated DDF function, if any */
|
---|
| 66 | ddf_fun_t *fun;
|
---|
| 67 |
|
---|
| 68 | /* Invalid for the roothub device */
|
---|
| 69 | unsigned port;
|
---|
[2aaba7e] | 70 |
|
---|
| 71 | /** Hub under which this device is connected */
|
---|
[20eaa82] | 72 | struct device *hub;
|
---|
| 73 |
|
---|
[2aaba7e] | 74 | /** USB Tier of the device */
|
---|
| 75 | uint8_t tier;
|
---|
| 76 |
|
---|
[20eaa82] | 77 | /* Transaction translator */
|
---|
[4c25c2fb] | 78 | struct {
|
---|
| 79 | device_t *dev;
|
---|
| 80 | unsigned port;
|
---|
| 81 | } tt;
|
---|
[20eaa82] | 82 |
|
---|
| 83 | /* The following are not set by the library */
|
---|
| 84 | usb_speed_t speed;
|
---|
| 85 | usb_address_t address;
|
---|
[a6afb4c] | 86 | endpoint_t *endpoints [USB_ENDPOINT_COUNT];
|
---|
[20eaa82] | 87 |
|
---|
[6832245] | 88 | /* Managing bus */
|
---|
| 89 | bus_t *bus;
|
---|
| 90 |
|
---|
[deb2e55] | 91 | /** True if the device can add new endpoints and schedule transfers. */
|
---|
| 92 | volatile bool online;
|
---|
| 93 |
|
---|
[20eaa82] | 94 | /* This structure is meant to be extended by overriding. */
|
---|
| 95 | } device_t;
|
---|
[41924f30] | 96 |
|
---|
[6832245] | 97 | typedef struct bus_ops bus_ops_t;
|
---|
[d37514e] | 98 |
|
---|
[6832245] | 99 | /**
|
---|
| 100 | * Operations structure serving as an interface of hc driver for the library
|
---|
| 101 | * (and the rest of the system).
|
---|
| 102 | */
|
---|
| 103 | struct bus_ops {
|
---|
| 104 | /* Global operations on the bus */
|
---|
[32fb6bce] | 105 | void (*interrupt)(bus_t *, uint32_t);
|
---|
| 106 | int (*status)(bus_t *, uint32_t *);
|
---|
[41924f30] | 107 |
|
---|
[6832245] | 108 | /* Operations on device */
|
---|
| 109 | int (*device_enumerate)(device_t *);
|
---|
[9848c77] | 110 | void (*device_gone)(device_t *);
|
---|
[6832245] | 111 | int (*device_online)(device_t *); /**< Optional */
|
---|
[0892663a] | 112 | void (*device_offline)(device_t *); /**< Optional */
|
---|
[ae3a941] | 113 | endpoint_t *(*endpoint_create)(device_t *,
|
---|
| 114 | const usb_endpoint_descriptors_t *);
|
---|
[6832245] | 115 |
|
---|
| 116 | /* Operations on endpoint */
|
---|
| 117 | int (*endpoint_register)(endpoint_t *);
|
---|
[bad4a05] | 118 | void (*endpoint_unregister)(endpoint_t *);
|
---|
[6832245] | 119 | void (*endpoint_destroy)(endpoint_t *); /**< Optional */
|
---|
| 120 | usb_transfer_batch_t *(*batch_create)(endpoint_t *); /**< Optional */
|
---|
[41924f30] | 121 |
|
---|
[6832245] | 122 | /* Operations on batch */
|
---|
[32fb6bce] | 123 | int (*batch_schedule)(usb_transfer_batch_t *);
|
---|
[bad4a05] | 124 | void (*batch_destroy)(usb_transfer_batch_t *); /**< Optional */
|
---|
[6832245] | 125 | };
|
---|
| 126 |
|
---|
[41924f30] | 127 | /** Endpoint management structure */
|
---|
| 128 | typedef struct bus {
|
---|
| 129 | /* Synchronization of ops */
|
---|
| 130 | fibril_mutex_t guard;
|
---|
| 131 |
|
---|
[32fb6bce] | 132 | /* Size of the device_t extended structure */
|
---|
[20eaa82] | 133 | size_t device_size;
|
---|
| 134 |
|
---|
[41924f30] | 135 | /* Do not call directly, ops are synchronized. */
|
---|
[6832245] | 136 | const bus_ops_t *ops;
|
---|
[41924f30] | 137 |
|
---|
[eeca8a6] | 138 | /* Reserving default address */
|
---|
| 139 | device_t *default_address_owner;
|
---|
| 140 | fibril_condvar_t default_address_cv;
|
---|
[5e2b1ae6] | 141 |
|
---|
[41924f30] | 142 | /* This structure is meant to be extended by overriding. */
|
---|
| 143 | } bus_t;
|
---|
| 144 |
|
---|
[32fb6bce] | 145 | void bus_init(bus_t *, size_t);
|
---|
[6832245] | 146 | int bus_device_init(device_t *, bus_t *);
|
---|
[20eaa82] | 147 |
|
---|
[6832245] | 148 | int bus_device_set_default_name(device_t *);
|
---|
[41924f30] | 149 |
|
---|
[6832245] | 150 | int bus_device_enumerate(device_t *);
|
---|
[9848c77] | 151 | void bus_device_gone(device_t *);
|
---|
[d37514e] | 152 |
|
---|
[6832245] | 153 | int bus_device_online(device_t *);
|
---|
| 154 | int bus_device_offline(device_t *);
|
---|
[41924f30] | 155 |
|
---|
[1d758fc] | 156 | /**
|
---|
| 157 | * A proforma to USB transfer batch. As opposed to transfer batch, which is
|
---|
| 158 | * supposed to be a dynamic structrure, this one is static and descriptive only.
|
---|
| 159 | * Its fields are copied to the final batch.
|
---|
| 160 | */
|
---|
| 161 | typedef struct transfer_request {
|
---|
| 162 | usb_target_t target;
|
---|
| 163 | usb_direction_t dir;
|
---|
| 164 |
|
---|
| 165 | dma_buffer_t buffer;
|
---|
| 166 | size_t offset, size;
|
---|
| 167 | uint64_t setup;
|
---|
| 168 |
|
---|
| 169 | usbhc_iface_transfer_callback_t on_complete;
|
---|
| 170 | void *arg;
|
---|
| 171 |
|
---|
| 172 | const char *name;
|
---|
| 173 | } transfer_request_t;
|
---|
| 174 |
|
---|
| 175 | int bus_issue_transfer(device_t *, const transfer_request_t *);
|
---|
[32fb6bce] | 176 |
|
---|
[fc3dfe6d] | 177 | errno_t bus_device_send_batch_sync(device_t *, usb_target_t,
|
---|
[32fb6bce] | 178 | usb_direction_t direction, char *, size_t, uint64_t,
|
---|
[fc3dfe6d] | 179 | const char *, size_t *);
|
---|
[32fb6bce] | 180 |
|
---|
[9efad54] | 181 | int bus_endpoint_add(device_t *, const usb_endpoint_descriptors_t *, endpoint_t **);
|
---|
[1ed3eb4] | 182 | endpoint_t *bus_find_endpoint(device_t *, usb_endpoint_t, usb_direction_t);
|
---|
[6832245] | 183 | int bus_endpoint_remove(endpoint_t *);
|
---|
[41924f30] | 184 |
|
---|
[eeca8a6] | 185 | int bus_reserve_default_address(bus_t *, device_t *);
|
---|
| 186 | void bus_release_default_address(bus_t *, device_t *);
|
---|
[20eaa82] | 187 |
|
---|
[41924f30] | 188 | #endif
|
---|
| 189 | /**
|
---|
| 190 | * @}
|
---|
| 191 | */
|
---|