[41df71f9] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
---|
| 3 | * Copyright (c) 2017 Ondrej Hlavaty
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup libdrv
|
---|
| 31 | * @addtogroup usb
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
| 35 | * @brief USB host controler interface definition. This is the interface of
|
---|
| 36 | * USB host controller function, which can be used by usb device drivers.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #ifndef LIBDRV_USBHC_IFACE_H_
|
---|
| 40 | #define LIBDRV_USBHC_IFACE_H_
|
---|
| 41 |
|
---|
| 42 | #include "ddf/driver.h"
|
---|
| 43 | #include <async.h>
|
---|
| 44 |
|
---|
[3cdaa7f] | 45 | /** USB speeds. */
|
---|
| 46 | typedef enum {
|
---|
| 47 | /** USB 1.1 low speed (1.5Mbits/s). */
|
---|
| 48 | USB_SPEED_LOW,
|
---|
| 49 | /** USB 1.1 full speed (12Mbits/s). */
|
---|
| 50 | USB_SPEED_FULL,
|
---|
| 51 | /** USB 2.0 high speed (480Mbits/s). */
|
---|
| 52 | USB_SPEED_HIGH,
|
---|
| 53 | /** USB 3.0 super speed (5Gbits/s). */
|
---|
| 54 | USB_SPEED_SUPER,
|
---|
| 55 | /** Psuedo-speed serving as a boundary. */
|
---|
| 56 | USB_SPEED_MAX
|
---|
| 57 | } usb_speed_t;
|
---|
| 58 |
|
---|
| 59 | /** USB endpoint number type.
|
---|
| 60 | * Negative values could be used to indicate error.
|
---|
| 61 | */
|
---|
[a6afb4c] | 62 | typedef uint16_t usb_endpoint_t;
|
---|
[3cdaa7f] | 63 |
|
---|
| 64 | /** USB address type.
|
---|
| 65 | * Negative values could be used to indicate error.
|
---|
| 66 | */
|
---|
[a6afb4c] | 67 | typedef uint16_t usb_address_t;
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * USB Stream ID type.
|
---|
| 71 | */
|
---|
| 72 | typedef uint16_t usb_stream_t;
|
---|
[3cdaa7f] | 73 |
|
---|
| 74 | /** USB transfer type. */
|
---|
| 75 | typedef enum {
|
---|
| 76 | USB_TRANSFER_CONTROL = 0,
|
---|
| 77 | USB_TRANSFER_ISOCHRONOUS = 1,
|
---|
| 78 | USB_TRANSFER_BULK = 2,
|
---|
[a6afb4c] | 79 | USB_TRANSFER_INTERRUPT = 3,
|
---|
[3cdaa7f] | 80 | } usb_transfer_type_t;
|
---|
| 81 |
|
---|
[a6afb4c] | 82 | #define USB_TRANSFER_COUNT (USB_TRANSFER_INTERRUPT + 1)
|
---|
| 83 |
|
---|
[3cdaa7f] | 84 | /** USB data transfer direction. */
|
---|
| 85 | typedef enum {
|
---|
| 86 | USB_DIRECTION_IN,
|
---|
| 87 | USB_DIRECTION_OUT,
|
---|
[a6afb4c] | 88 | USB_DIRECTION_BOTH,
|
---|
[3cdaa7f] | 89 | } usb_direction_t;
|
---|
| 90 |
|
---|
[a6afb4c] | 91 | #define USB_DIRECTION_COUNT (USB_DIRECTION_BOTH + 1)
|
---|
| 92 |
|
---|
[3cdaa7f] | 93 | /** USB complete address type.
|
---|
| 94 | * Pair address + endpoint is identification of transaction recipient.
|
---|
| 95 | */
|
---|
| 96 | typedef union {
|
---|
| 97 | struct {
|
---|
| 98 | usb_address_t address;
|
---|
| 99 | usb_endpoint_t endpoint;
|
---|
[a6afb4c] | 100 | usb_stream_t stream;
|
---|
[3cdaa7f] | 101 | } __attribute__((packed));
|
---|
| 102 | uint64_t packed;
|
---|
| 103 | } usb_target_t;
|
---|
| 104 |
|
---|
[cc63815] | 105 | // FIXME: DMA buffers shall be part of libdrv anyway.
|
---|
| 106 | typedef unsigned dma_policy_t;
|
---|
| 107 |
|
---|
[9efad54] | 108 | typedef struct usb_pipe_desc {
|
---|
| 109 | /** Endpoint number. */
|
---|
| 110 | usb_endpoint_t endpoint_no;
|
---|
| 111 |
|
---|
| 112 | /** Endpoint transfer type. */
|
---|
| 113 | usb_transfer_type_t transfer_type;
|
---|
| 114 |
|
---|
| 115 | /** Endpoint direction. */
|
---|
| 116 | usb_direction_t direction;
|
---|
| 117 |
|
---|
[af16ebe] | 118 | /**
|
---|
| 119 | * Maximum size of one transfer. Non-periodic endpoints may handle
|
---|
| 120 | * bigger transfers, but those can be split into multiple USB transfers.
|
---|
| 121 | */
|
---|
[9efad54] | 122 | size_t max_transfer_size;
|
---|
[fdc2253b] | 123 |
|
---|
| 124 | /** Constraints on buffers to be transferred without copying */
|
---|
| 125 | dma_policy_t transfer_buffer_policy;
|
---|
[9efad54] | 126 | } usb_pipe_desc_t;
|
---|
| 127 |
|
---|
[239eea41] | 128 | typedef struct usb_pipe_transfer_request {
|
---|
| 129 | usb_direction_t dir;
|
---|
| 130 | usb_endpoint_t endpoint;
|
---|
| 131 | usb_stream_t stream;
|
---|
| 132 |
|
---|
| 133 | uint64_t setup; /**< Valid iff the transfer is of control type */
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * Base address of the buffer to share. Must be at least offset + size
|
---|
| 137 | * large. Is patched after being transmitted over IPC, so the pointer is
|
---|
| 138 | * still valid.
|
---|
[d345ce2] | 139 | *
|
---|
| 140 | * Note that offset might be actually more than PAGE_SIZE.
|
---|
[239eea41] | 141 | */
|
---|
[d345ce2] | 142 | void *base;
|
---|
[239eea41] | 143 | size_t offset; /**< Offset to the buffer */
|
---|
| 144 | size_t size; /**< Requested size. */
|
---|
| 145 | dma_policy_t buffer_policy; /**< Properties of the buffer. */
|
---|
| 146 | } usbhc_iface_transfer_request_t;
|
---|
| 147 |
|
---|
[9efad54] | 148 | /** This structure follows standard endpoint descriptor + superspeed companion
|
---|
| 149 | * descriptor, and exists to avoid dependency of libdrv on libusb. Keep the
|
---|
| 150 | * internal fields named exactly like their source (because we want to use the
|
---|
| 151 | * same macros to access them).
|
---|
| 152 | * Callers shall fill it with bare contents of respective descriptors (in usb endianity).
|
---|
| 153 | */
|
---|
[708d8fcd] | 154 | typedef struct usb_endpoint_descriptors {
|
---|
[9efad54] | 155 | struct {
|
---|
| 156 | uint8_t endpoint_address;
|
---|
| 157 | uint8_t attributes;
|
---|
| 158 | uint16_t max_packet_size;
|
---|
| 159 | uint8_t poll_interval;
|
---|
| 160 | } endpoint;
|
---|
| 161 |
|
---|
| 162 | /* Superspeed companion descriptor */
|
---|
| 163 | struct companion_desc_t {
|
---|
| 164 | uint8_t max_burst;
|
---|
| 165 | uint8_t attributes;
|
---|
| 166 | uint16_t bytes_per_interval;
|
---|
| 167 | } companion;
|
---|
| 168 | } usb_endpoint_descriptors_t;
|
---|
| 169 |
|
---|
[5a6cc679] | 170 | extern errno_t usbhc_reserve_default_address(async_exch_t *);
|
---|
| 171 | extern errno_t usbhc_release_default_address(async_exch_t *);
|
---|
[41df71f9] | 172 |
|
---|
[5a6cc679] | 173 | extern errno_t usbhc_device_enumerate(async_exch_t *, unsigned, usb_speed_t);
|
---|
| 174 | extern errno_t usbhc_device_remove(async_exch_t *, unsigned);
|
---|
[41df71f9] | 175 |
|
---|
[5a6cc679] | 176 | extern errno_t usbhc_register_endpoint(async_exch_t *, usb_pipe_desc_t *, const usb_endpoint_descriptors_t *);
|
---|
| 177 | extern errno_t usbhc_unregister_endpoint(async_exch_t *, const usb_pipe_desc_t *);
|
---|
[9efad54] | 178 |
|
---|
[239eea41] | 179 | extern errno_t usbhc_transfer(async_exch_t *, const usbhc_iface_transfer_request_t *, size_t *);
|
---|
[41df71f9] | 180 |
|
---|
[f9d0a86] | 181 | /** Callback for outgoing transfer */
|
---|
[5a6cc679] | 182 | typedef errno_t (*usbhc_iface_transfer_callback_t)(void *, int, size_t);
|
---|
[41df71f9] | 183 |
|
---|
| 184 | /** USB device communication interface. */
|
---|
| 185 | typedef struct {
|
---|
[5c69377] | 186 | errno_t (*default_address_reservation)(ddf_fun_t *, bool);
|
---|
[41df71f9] | 187 |
|
---|
[5c69377] | 188 | errno_t (*device_enumerate)(ddf_fun_t *, unsigned, usb_speed_t);
|
---|
| 189 | errno_t (*device_remove)(ddf_fun_t *, unsigned);
|
---|
[41df71f9] | 190 |
|
---|
[5c69377] | 191 | errno_t (*register_endpoint)(ddf_fun_t *, usb_pipe_desc_t *, const usb_endpoint_descriptors_t *);
|
---|
| 192 | errno_t (*unregister_endpoint)(ddf_fun_t *, const usb_pipe_desc_t *);
|
---|
[41df71f9] | 193 |
|
---|
[239eea41] | 194 | errno_t (*transfer)(ddf_fun_t *, const usbhc_iface_transfer_request_t *,
|
---|
| 195 | usbhc_iface_transfer_callback_t, void *);
|
---|
[41df71f9] | 196 | } usbhc_iface_t;
|
---|
| 197 |
|
---|
| 198 | #endif
|
---|
| 199 | /**
|
---|
| 200 | * @}
|
---|
| 201 | */
|
---|