source: mainline/uspace/lib/usb/src/usb.c@ 6326155b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6326155b was 6326155b, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

libusb: Add toggle guessing function.

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