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