[fd17ab5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
---|
| 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 |
|
---|
[3b77628] | 29 | /** @addtogroup libusb
|
---|
[fd17ab5] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[a6add7a] | 33 | * Common USB functions.
|
---|
[fd17ab5] | 34 | */
|
---|
[4b4c797] | 35 | #include <usb/usb.h>
|
---|
[6326155b] | 36 | #include <usb/request.h>
|
---|
| 37 |
|
---|
| 38 | #include <assert.h>
|
---|
[534dee89] | 39 | #include <byteorder.h>
|
---|
[52f9c57] | 40 | #include <macros.h>
|
---|
[a90fc0c] | 41 |
|
---|
[aaff605] | 42 | static const char *str_speed[] = {
|
---|
[e8d82cb] | 43 | [USB_SPEED_LOW] = "low",
|
---|
| 44 | [USB_SPEED_FULL] = "full",
|
---|
| 45 | [USB_SPEED_HIGH] = "high",
|
---|
[f4eb6c93] | 46 | [USB_SPEED_SUPER] = "super",
|
---|
[aaff605] | 47 | };
|
---|
[a90fc0c] | 48 |
|
---|
| 49 | static const char *str_transfer_type[] = {
|
---|
[e8d82cb] | 50 | [USB_TRANSFER_CONTROL] = "control",
|
---|
| 51 | [USB_TRANSFER_ISOCHRONOUS] = "isochronous",
|
---|
| 52 | [USB_TRANSFER_BULK] = "bulk",
|
---|
| 53 | [USB_TRANSFER_INTERRUPT] = "interrupt",
|
---|
[a90fc0c] | 54 | };
|
---|
| 55 |
|
---|
| 56 | static const char *str_transfer_type_short[] = {
|
---|
[e8d82cb] | 57 | [USB_TRANSFER_CONTROL] = "ctrl",
|
---|
| 58 | [USB_TRANSFER_ISOCHRONOUS] = "iso",
|
---|
| 59 | [USB_TRANSFER_BULK] = "bulk",
|
---|
| 60 | [USB_TRANSFER_INTERRUPT] = "intr",
|
---|
[a90fc0c] | 61 | };
|
---|
[fd17ab5] | 62 |
|
---|
[84eb7432] | 63 | static const char *str_direction[] = {
|
---|
[e8d82cb] | 64 | [USB_DIRECTION_IN] = "in",
|
---|
| 65 | [USB_DIRECTION_OUT] = "out",
|
---|
| 66 | [USB_DIRECTION_BOTH] = "both",
|
---|
[84eb7432] | 67 | };
|
---|
| 68 |
|
---|
[a6add7a] | 69 | /** String representation for USB transfer type.
|
---|
| 70 | *
|
---|
| 71 | * @param t Transfer type.
|
---|
| 72 | * @return Transfer type as a string (in English).
|
---|
| 73 | */
|
---|
[a90fc0c] | 74 | const char *usb_str_transfer_type(usb_transfer_type_t t)
|
---|
| 75 | {
|
---|
[05b59393] | 76 | if ((size_t)t >= ARRAY_SIZE(str_transfer_type)) {
|
---|
[a90fc0c] | 77 | return "invalid";
|
---|
| 78 | }
|
---|
| 79 | return str_transfer_type[t];
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /** String representation for USB transfer type (short version).
|
---|
| 83 | *
|
---|
| 84 | * @param t Transfer type.
|
---|
| 85 | * @return Transfer type as a short string for debugging messages.
|
---|
| 86 | */
|
---|
| 87 | const char *usb_str_transfer_type_short(usb_transfer_type_t t)
|
---|
[fd17ab5] | 88 | {
|
---|
[05b59393] | 89 | if ((size_t)t >= ARRAY_SIZE(str_transfer_type_short)) {
|
---|
[a90fc0c] | 90 | return "invl";
|
---|
[fd17ab5] | 91 | }
|
---|
[a90fc0c] | 92 | return str_transfer_type_short[t];
|
---|
[fd17ab5] | 93 | }
|
---|
| 94 |
|
---|
[84eb7432] | 95 | /** String representation of USB direction.
|
---|
| 96 | *
|
---|
| 97 | * @param d The direction.
|
---|
| 98 | * @return Direction as a string (in English).
|
---|
| 99 | */
|
---|
| 100 | const char *usb_str_direction(usb_direction_t d)
|
---|
| 101 | {
|
---|
[52f9c57] | 102 | if (d >= ARRAY_SIZE(str_direction)) {
|
---|
[84eb7432] | 103 | return "invalid";
|
---|
| 104 | }
|
---|
| 105 | return str_direction[d];
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[aaff605] | 108 | /** String representation of USB speed.
|
---|
| 109 | *
|
---|
| 110 | * @param s The speed.
|
---|
| 111 | * @return USB speed as a string (in English).
|
---|
| 112 | */
|
---|
| 113 | const char *usb_str_speed(usb_speed_t s)
|
---|
| 114 | {
|
---|
[52f9c57] | 115 | if (s >= ARRAY_SIZE(str_speed)) {
|
---|
[aaff605] | 116 | return "invalid";
|
---|
| 117 | }
|
---|
| 118 | return str_speed[s];
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[6326155b] | 121 | /** Check setup packet data for signs of toggle reset.
|
---|
| 122 | *
|
---|
| 123 | * @param[in] requst Setup requst data.
|
---|
[58563585] | 124 | *
|
---|
[6326155b] | 125 | * @retval -1 No endpoints need reset.
|
---|
| 126 | * @retval 0 All endpoints need reset.
|
---|
| 127 | * @retval >0 Specified endpoint needs reset.
|
---|
[58563585] | 128 | *
|
---|
[6326155b] | 129 | */
|
---|
[5fd9c30] | 130 | toggle_reset_mode_t usb_request_get_toggle_reset_mode(
|
---|
[6326155b] | 131 | const usb_device_request_setup_packet_t *request)
|
---|
| 132 | {
|
---|
| 133 | assert(request);
|
---|
| 134 | switch (request->request)
|
---|
| 135 | {
|
---|
| 136 | /* Clear Feature ENPOINT_STALL */
|
---|
| 137 | case USB_DEVREQ_CLEAR_FEATURE: /*resets only cleared ep */
|
---|
| 138 | /* 0x2 ( HOST to device | STANDART | TO ENPOINT) */
|
---|
| 139 | if ((request->request_type == 0x2) &&
|
---|
[4ee5272] | 140 | (request->value == USB_FEATURE_ENDPOINT_HALT))
|
---|
[5fd9c30] | 141 | return RESET_EP;
|
---|
[6326155b] | 142 | break;
|
---|
| 143 | case USB_DEVREQ_SET_CONFIGURATION:
|
---|
| 144 | case USB_DEVREQ_SET_INTERFACE:
|
---|
| 145 | /* Recipient must be device, this resets all endpoints,
|
---|
| 146 | * In fact there should be no endpoints but EP 0 registered
|
---|
| 147 | * as different interfaces use different endpoints,
|
---|
| 148 | * unless you're changing configuration or alternative
|
---|
| 149 | * interface of an already setup device. */
|
---|
| 150 | if (!(request->request_type & SETUP_REQUEST_TYPE_DEVICE_TO_HOST))
|
---|
[5fd9c30] | 151 | return RESET_ALL;
|
---|
[6326155b] | 152 | break;
|
---|
| 153 | default:
|
---|
| 154 | break;
|
---|
| 155 | }
|
---|
[5fd9c30] | 156 |
|
---|
| 157 | return RESET_NONE;
|
---|
[6326155b] | 158 | }
|
---|
| 159 |
|
---|
[fd17ab5] | 160 | /**
|
---|
| 161 | * @}
|
---|
| 162 | */
|
---|