| 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 <assert.h>
|
|---|
| 46 | #include <fibril_synch.h>
|
|---|
| 47 | #include <stdbool.h>
|
|---|
| 48 | #include <usb/host/hcd.h>
|
|---|
| 49 | #include <usb/request.h>
|
|---|
| 50 | #include <usb/usb.h>
|
|---|
| 51 | #include <usbhc_iface.h>
|
|---|
| 52 |
|
|---|
| 53 | typedef struct hcd hcd_t;
|
|---|
| 54 | typedef struct endpoint endpoint_t;
|
|---|
| 55 | typedef struct bus bus_t;
|
|---|
| 56 | typedef struct ddf_fun ddf_fun_t;
|
|---|
| 57 | typedef struct usb_transfer_batch usb_transfer_batch_t;
|
|---|
| 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;
|
|---|
| 70 | struct device *hub;
|
|---|
| 71 |
|
|---|
| 72 | /* Transaction translator */
|
|---|
| 73 | struct {
|
|---|
| 74 | device_t *dev;
|
|---|
| 75 | unsigned port;
|
|---|
| 76 | } tt;
|
|---|
| 77 |
|
|---|
| 78 | /* The following are not set by the library */
|
|---|
| 79 | usb_speed_t speed;
|
|---|
| 80 | usb_address_t address;
|
|---|
| 81 | endpoint_t *endpoints [2 * USB_ENDPOINT_MAX];
|
|---|
| 82 |
|
|---|
| 83 | /* Managing bus */
|
|---|
| 84 | bus_t *bus;
|
|---|
| 85 |
|
|---|
| 86 | /** True if the device can add new endpoints and schedule transfers. */
|
|---|
| 87 | volatile bool online;
|
|---|
| 88 |
|
|---|
| 89 | /* This structure is meant to be extended by overriding. */
|
|---|
| 90 | } device_t;
|
|---|
| 91 |
|
|---|
| 92 | typedef struct bus_ops bus_ops_t;
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Operations structure serving as an interface of hc driver for the library
|
|---|
| 96 | * (and the rest of the system).
|
|---|
| 97 | */
|
|---|
| 98 | struct bus_ops {
|
|---|
| 99 | /* Undefined operations will be delegated to parent ops */
|
|---|
| 100 | const bus_ops_t *parent;
|
|---|
| 101 |
|
|---|
| 102 | /* Global operations on the bus */
|
|---|
| 103 | void (*interrupt)(bus_t *, uint32_t);
|
|---|
| 104 | int (*status)(bus_t *, uint32_t *);
|
|---|
| 105 |
|
|---|
| 106 | /* Operations on device */
|
|---|
| 107 | int (*device_enumerate)(device_t *);
|
|---|
| 108 | void (*device_gone)(device_t *);
|
|---|
| 109 | int (*device_online)(device_t *); /**< Optional */
|
|---|
| 110 | void (*device_offline)(device_t *); /**< Optional */
|
|---|
| 111 | endpoint_t *(*endpoint_create)(device_t *, const usb_endpoint_descriptors_t *);
|
|---|
| 112 |
|
|---|
| 113 | /* Operations on endpoint */
|
|---|
| 114 | int (*endpoint_register)(endpoint_t *);
|
|---|
| 115 | void (*endpoint_unregister)(endpoint_t *);
|
|---|
| 116 | void (*endpoint_destroy)(endpoint_t *); /**< Optional */
|
|---|
| 117 | ssize_t (*endpoint_count_bw) (endpoint_t *, size_t); /**< Optional */
|
|---|
| 118 | usb_transfer_batch_t *(*batch_create)(endpoint_t *); /**< Optional */
|
|---|
| 119 |
|
|---|
| 120 | /* Operations on batch */
|
|---|
| 121 | int (*batch_schedule)(usb_transfer_batch_t *);
|
|---|
| 122 | void (*batch_destroy)(usb_transfer_batch_t *); /**< Optional */
|
|---|
| 123 | };
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Use this macro to lookup virtual function.
|
|---|
| 127 | */
|
|---|
| 128 | #define BUS_OPS_LOOKUP(start, fn) ({ bus_ops_t const * ops = (start); while (ops && ops->fn == NULL) ops = ops->parent; ops; })
|
|---|
| 129 |
|
|---|
| 130 | /** Endpoint management structure */
|
|---|
| 131 | typedef struct bus {
|
|---|
| 132 | /* Synchronization of ops */
|
|---|
| 133 | fibril_mutex_t guard;
|
|---|
| 134 |
|
|---|
| 135 | /* Size of the device_t extended structure */
|
|---|
| 136 | size_t device_size;
|
|---|
| 137 |
|
|---|
| 138 | /* Do not call directly, ops are synchronized. */
|
|---|
| 139 | const bus_ops_t *ops;
|
|---|
| 140 |
|
|---|
| 141 | /* Reserving default address */
|
|---|
| 142 | device_t *default_address_owner;
|
|---|
| 143 | fibril_condvar_t default_address_cv;
|
|---|
| 144 |
|
|---|
| 145 | /* This structure is meant to be extended by overriding. */
|
|---|
| 146 | } bus_t;
|
|---|
| 147 |
|
|---|
| 148 | void bus_init(bus_t *, size_t);
|
|---|
| 149 | int bus_device_init(device_t *, bus_t *);
|
|---|
| 150 |
|
|---|
| 151 | int bus_device_set_default_name(device_t *);
|
|---|
| 152 |
|
|---|
| 153 | int bus_device_enumerate(device_t *);
|
|---|
| 154 | void bus_device_gone(device_t *);
|
|---|
| 155 |
|
|---|
| 156 | int bus_device_online(device_t *);
|
|---|
| 157 | int bus_device_offline(device_t *);
|
|---|
| 158 |
|
|---|
| 159 | int bus_device_send_batch(device_t *, usb_target_t,
|
|---|
| 160 | usb_direction_t direction, char *, size_t, uint64_t,
|
|---|
| 161 | usbhc_iface_transfer_callback_t, void *, const char *);
|
|---|
| 162 |
|
|---|
| 163 | ssize_t bus_device_send_batch_sync(device_t *, usb_target_t,
|
|---|
| 164 | usb_direction_t direction, char *, size_t, uint64_t,
|
|---|
| 165 | const char *);
|
|---|
| 166 |
|
|---|
| 167 | int bus_endpoint_add(device_t *, const usb_endpoint_descriptors_t *, endpoint_t **);
|
|---|
| 168 | endpoint_t *bus_find_endpoint(device_t *, usb_endpoint_t, usb_direction_t);
|
|---|
| 169 | int bus_endpoint_remove(endpoint_t *);
|
|---|
| 170 |
|
|---|
| 171 | int bus_reserve_default_address(bus_t *, device_t *);
|
|---|
| 172 | void bus_release_default_address(bus_t *, device_t *);
|
|---|
| 173 |
|
|---|
| 174 | #endif
|
|---|
| 175 | /**
|
|---|
| 176 | * @}
|
|---|
| 177 | */
|
|---|