| 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 |
|
|---|
| 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 | */
|
|---|
| 62 | typedef int16_t usb_endpoint_t;
|
|---|
| 63 |
|
|---|
| 64 | /** USB address type.
|
|---|
| 65 | * Negative values could be used to indicate error.
|
|---|
| 66 | */
|
|---|
| 67 | typedef int16_t usb_address_t;
|
|---|
| 68 |
|
|---|
| 69 | /** USB transfer type. */
|
|---|
| 70 | typedef enum {
|
|---|
| 71 | USB_TRANSFER_CONTROL = 0,
|
|---|
| 72 | USB_TRANSFER_ISOCHRONOUS = 1,
|
|---|
| 73 | USB_TRANSFER_BULK = 2,
|
|---|
| 74 | USB_TRANSFER_INTERRUPT = 3
|
|---|
| 75 | } usb_transfer_type_t;
|
|---|
| 76 |
|
|---|
| 77 | /** USB data transfer direction. */
|
|---|
| 78 | typedef enum {
|
|---|
| 79 | USB_DIRECTION_IN,
|
|---|
| 80 | USB_DIRECTION_OUT,
|
|---|
| 81 | USB_DIRECTION_BOTH
|
|---|
| 82 | } usb_direction_t;
|
|---|
| 83 |
|
|---|
| 84 | /** USB complete address type.
|
|---|
| 85 | * Pair address + endpoint is identification of transaction recipient.
|
|---|
| 86 | */
|
|---|
| 87 | typedef union {
|
|---|
| 88 | struct {
|
|---|
| 89 | usb_address_t address;
|
|---|
| 90 | usb_endpoint_t endpoint;
|
|---|
| 91 | uint32_t stream;
|
|---|
| 92 | } __attribute__((packed));
|
|---|
| 93 | uint64_t packed;
|
|---|
| 94 | } usb_target_t;
|
|---|
| 95 |
|
|---|
| 96 | typedef struct usb_pipe_desc {
|
|---|
| 97 | /** Endpoint number. */
|
|---|
| 98 | usb_endpoint_t endpoint_no;
|
|---|
| 99 |
|
|---|
| 100 | /** Endpoint transfer type. */
|
|---|
| 101 | usb_transfer_type_t transfer_type;
|
|---|
| 102 |
|
|---|
| 103 | /** Endpoint direction. */
|
|---|
| 104 | usb_direction_t direction;
|
|---|
| 105 |
|
|---|
| 106 | /** Maximum size of one transfer */
|
|---|
| 107 | size_t max_transfer_size;
|
|---|
| 108 | } usb_pipe_desc_t;
|
|---|
| 109 |
|
|---|
| 110 | /** This structure follows standard endpoint descriptor + superspeed companion
|
|---|
| 111 | * descriptor, and exists to avoid dependency of libdrv on libusb. Keep the
|
|---|
| 112 | * internal fields named exactly like their source (because we want to use the
|
|---|
| 113 | * same macros to access them).
|
|---|
| 114 | * Callers shall fill it with bare contents of respective descriptors (in usb endianity).
|
|---|
| 115 | */
|
|---|
| 116 | typedef struct usb_endpoint_descriptors {
|
|---|
| 117 | struct {
|
|---|
| 118 | uint8_t endpoint_address;
|
|---|
| 119 | uint8_t attributes;
|
|---|
| 120 | uint16_t max_packet_size;
|
|---|
| 121 | uint8_t poll_interval;
|
|---|
| 122 | } endpoint;
|
|---|
| 123 |
|
|---|
| 124 | /* Superspeed companion descriptor */
|
|---|
| 125 | struct companion_desc_t {
|
|---|
| 126 | uint8_t max_burst;
|
|---|
| 127 | uint8_t attributes;
|
|---|
| 128 | uint16_t bytes_per_interval;
|
|---|
| 129 | } companion;
|
|---|
| 130 | } usb_endpoint_descriptors_t;
|
|---|
| 131 |
|
|---|
| 132 | extern int usbhc_reserve_default_address(async_exch_t *);
|
|---|
| 133 | extern int usbhc_release_default_address(async_exch_t *);
|
|---|
| 134 |
|
|---|
| 135 | extern int usbhc_device_enumerate(async_exch_t *, unsigned, usb_speed_t);
|
|---|
| 136 | extern int usbhc_device_remove(async_exch_t *, unsigned);
|
|---|
| 137 |
|
|---|
| 138 | extern int usbhc_register_endpoint(async_exch_t *, usb_pipe_desc_t *, const usb_endpoint_descriptors_t *);
|
|---|
| 139 | extern int usbhc_unregister_endpoint(async_exch_t *, const usb_pipe_desc_t *);
|
|---|
| 140 |
|
|---|
| 141 | extern int usbhc_read(async_exch_t *, usb_endpoint_t, uint64_t, void *, size_t,
|
|---|
| 142 | size_t *);
|
|---|
| 143 | extern int usbhc_write(async_exch_t *, usb_endpoint_t, uint64_t, const void *,
|
|---|
| 144 | size_t);
|
|---|
| 145 |
|
|---|
| 146 | /** Callback for outgoing transfer */
|
|---|
| 147 | typedef int (*usbhc_iface_transfer_callback_t)(void *, int, size_t);
|
|---|
| 148 |
|
|---|
| 149 | /** USB device communication interface. */
|
|---|
| 150 | typedef struct {
|
|---|
| 151 | int (*default_address_reservation)(ddf_fun_t *, bool);
|
|---|
| 152 |
|
|---|
| 153 | int (*device_enumerate)(ddf_fun_t *, unsigned, usb_speed_t);
|
|---|
| 154 | int (*device_remove)(ddf_fun_t *, unsigned);
|
|---|
| 155 |
|
|---|
| 156 | int (*register_endpoint)(ddf_fun_t *, usb_pipe_desc_t *, const usb_endpoint_descriptors_t *);
|
|---|
| 157 | int (*unregister_endpoint)(ddf_fun_t *, const usb_pipe_desc_t *);
|
|---|
| 158 |
|
|---|
| 159 | int (*read)(ddf_fun_t *, usb_target_t,
|
|---|
| 160 | uint64_t, char *, size_t,
|
|---|
| 161 | usbhc_iface_transfer_callback_t, void *);
|
|---|
| 162 | int (*write)(ddf_fun_t *, usb_target_t,
|
|---|
| 163 | uint64_t, const char *, size_t,
|
|---|
| 164 | usbhc_iface_transfer_callback_t, void *);
|
|---|
| 165 | } usbhc_iface_t;
|
|---|
| 166 |
|
|---|
| 167 | #endif
|
|---|
| 168 | /**
|
|---|
| 169 | * @}
|
|---|
| 170 | */
|
|---|