| 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 | #include <usb/request.h>
|
|---|
| 47 | #include <usb/host/hcd.h>
|
|---|
| 48 |
|
|---|
| 49 | #include <assert.h>
|
|---|
| 50 | #include <fibril_synch.h>
|
|---|
| 51 | #include <stdbool.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 | usb_tt_address_t tt;
|
|---|
| 74 |
|
|---|
| 75 | /* The following are not set by the library */
|
|---|
| 76 | usb_speed_t speed;
|
|---|
| 77 | usb_address_t address;
|
|---|
| 78 |
|
|---|
| 79 | /* This structure is meant to be extended by overriding. */
|
|---|
| 80 | } device_t;
|
|---|
| 81 |
|
|---|
| 82 | typedef struct {
|
|---|
| 83 | int (*enumerate_device)(bus_t *, hcd_t *, device_t *);
|
|---|
| 84 | int (*remove_device)(bus_t *, hcd_t *, device_t *);
|
|---|
| 85 |
|
|---|
| 86 | int (*online_device)(bus_t *, hcd_t *, device_t *); /**< Optional */
|
|---|
| 87 | int (*offline_device)(bus_t *, hcd_t *, device_t *); /**< Optional */
|
|---|
| 88 |
|
|---|
| 89 | /* The following operations are protected by a bus guard. */
|
|---|
| 90 | endpoint_t *(*create_endpoint)(bus_t *);
|
|---|
| 91 | int (*register_endpoint)(bus_t *, device_t *, endpoint_t *, const usb_endpoint_desc_t *);
|
|---|
| 92 | int (*unregister_endpoint)(bus_t *, endpoint_t *);
|
|---|
| 93 | endpoint_t *(*find_endpoint)(bus_t *, device_t*, usb_target_t, usb_direction_t);
|
|---|
| 94 | void (*destroy_endpoint)(endpoint_t *); /**< Optional */
|
|---|
| 95 | bool (*endpoint_get_toggle)(endpoint_t *); /**< Optional */
|
|---|
| 96 | void (*endpoint_set_toggle)(endpoint_t *, bool); /**< Optional */
|
|---|
| 97 |
|
|---|
| 98 | int (*request_address)(bus_t *, usb_address_t*, bool, usb_speed_t);
|
|---|
| 99 | int (*release_address)(bus_t *, usb_address_t);
|
|---|
| 100 |
|
|---|
| 101 | int (*reset_toggle)(bus_t *, usb_target_t, toggle_reset_mode_t);
|
|---|
| 102 |
|
|---|
| 103 | size_t (*count_bw) (endpoint_t *, size_t);
|
|---|
| 104 |
|
|---|
| 105 | usb_transfer_batch_t *(*create_batch)(bus_t *, endpoint_t *); /**< Optional */
|
|---|
| 106 | void (*destroy_batch)(usb_transfer_batch_t *); /**< Optional */
|
|---|
| 107 | } bus_ops_t;
|
|---|
| 108 |
|
|---|
| 109 | /** Endpoint management structure */
|
|---|
| 110 | typedef struct bus {
|
|---|
| 111 | /* Synchronization of ops */
|
|---|
| 112 | fibril_mutex_t guard;
|
|---|
| 113 |
|
|---|
| 114 | size_t device_size;
|
|---|
| 115 |
|
|---|
| 116 | /* Do not call directly, ops are synchronized. */
|
|---|
| 117 | bus_ops_t ops;
|
|---|
| 118 |
|
|---|
| 119 | /* This structure is meant to be extended by overriding. */
|
|---|
| 120 | } bus_t;
|
|---|
| 121 |
|
|---|
| 122 | void bus_init(bus_t *, size_t);
|
|---|
| 123 | int device_init(device_t *);
|
|---|
| 124 |
|
|---|
| 125 | int device_set_default_name(device_t *);
|
|---|
| 126 |
|
|---|
| 127 | int bus_enumerate_device(bus_t *, hcd_t *, device_t *);
|
|---|
| 128 | int bus_remove_device(bus_t *, hcd_t *, device_t *);
|
|---|
| 129 |
|
|---|
| 130 | int bus_online_device(bus_t *, hcd_t *, device_t *);
|
|---|
| 131 | int bus_offline_device(bus_t *, hcd_t *, device_t *);
|
|---|
| 132 |
|
|---|
| 133 | int bus_add_endpoint(bus_t *, device_t *, const usb_endpoint_desc_t *, endpoint_t **);
|
|---|
| 134 | endpoint_t *bus_find_endpoint(bus_t *, device_t *, usb_target_t, usb_direction_t);
|
|---|
| 135 | int bus_remove_endpoint(bus_t *, endpoint_t *);
|
|---|
| 136 |
|
|---|
| 137 | size_t bus_count_bw(endpoint_t *, size_t);
|
|---|
| 138 |
|
|---|
| 139 | int bus_request_address(bus_t *, usb_address_t *, bool, usb_speed_t);
|
|---|
| 140 | int bus_release_address(bus_t *, usb_address_t);
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | static inline int bus_reserve_default_address(bus_t *bus, usb_speed_t speed) {
|
|---|
| 144 | usb_address_t addr = USB_ADDRESS_DEFAULT;
|
|---|
| 145 |
|
|---|
| 146 | const int r = bus_request_address(bus, &addr, true, speed);
|
|---|
| 147 | assert(addr == USB_ADDRESS_DEFAULT);
|
|---|
| 148 | return r;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | static inline int bus_release_default_address(bus_t *bus) {
|
|---|
| 152 | return bus_release_address(bus, USB_ADDRESS_DEFAULT);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | int bus_reset_toggle(bus_t *, usb_target_t, bool);
|
|---|
| 156 |
|
|---|
| 157 | #endif
|
|---|
| 158 | /**
|
|---|
| 159 | * @}
|
|---|
| 160 | */
|
|---|