source: mainline/uspace/lib/usb/include/usb/usb.h@ a6afb4c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a6afb4c was a6afb4c, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhost: check validity of arguments, cleanup

  • Property mode set to 100644
File size: 5.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/** @addtogroup libusb
29 * @{
30 */
31/** @file
32 * Common USB types and functions.
33 */
34#ifndef LIBUSB_USB_H_
35#define LIBUSB_USB_H_
36
37#include <byteorder.h>
38#include <stdbool.h>
39#include <stdint.h>
40#include <types/common.h>
41#include <usbhc_iface.h>
42
43/** Convert 16bit value from native (host) endianness to USB endianness. */
44#define uint16_host2usb(n) host2uint16_t_le((n))
45
46/** Convert 32bit value from native (host) endianness to USB endianness. */
47#define uint32_host2usb(n) host2uint32_t_le((n))
48
49/** Convert 16bit value from USB endianness into native (host) one. */
50#define uint16_usb2host(n) uint16_t_le2host((n))
51
52/** Convert 32bit value from USB endianness into native (host) one. */
53#define uint32_usb2host(n) uint32_t_le2host((n))
54
55const char * usb_str_transfer_type(usb_transfer_type_t t);
56const char * usb_str_transfer_type_short(usb_transfer_type_t t);
57
58const char *usb_str_direction(usb_direction_t);
59
60static inline bool usb_speed_is_11(const usb_speed_t s)
61{
62 return (s == USB_SPEED_FULL) || (s == USB_SPEED_LOW);
63}
64
65static inline bool usb_speed_is_valid(const usb_speed_t s)
66{
67 return (s >= USB_SPEED_LOW) && (s < USB_SPEED_MAX);
68}
69
70const char *usb_str_speed(usb_speed_t);
71
72
73/** USB request type target. */
74typedef enum {
75 USB_REQUEST_TYPE_STANDARD = 0,
76 USB_REQUEST_TYPE_CLASS = 1,
77 USB_REQUEST_TYPE_VENDOR = 2
78} usb_request_type_t;
79
80/** USB request recipient. */
81typedef enum {
82 USB_REQUEST_RECIPIENT_DEVICE = 0,
83 USB_REQUEST_RECIPIENT_INTERFACE = 1,
84 USB_REQUEST_RECIPIENT_ENDPOINT = 2,
85 USB_REQUEST_RECIPIENT_OTHER = 3
86} usb_request_recipient_t;
87
88/** Default USB address. */
89#define USB_ADDRESS_DEFAULT 0
90
91/** Maximum address number in USB 1.1. */
92#define USB11_ADDRESS_MAX 127
93#define USB_ADDRESS_COUNT (USB11_ADDRESS_MAX + 1)
94
95/** Check USB address for allowed values.
96 *
97 * @param ep USB address.
98 *
99 * @return True, if value is wihtin limits, false otherwise.
100 *
101 */
102static inline bool usb_address_is_valid(usb_address_t a)
103{
104 return a <= USB11_ADDRESS_MAX;
105}
106
107/** Default control endpoint */
108#define USB_ENDPOINT_DEFAULT_CONTROL 0
109
110/** Maximum endpoint number in USB */
111#define USB_ENDPOINT_MAX 16
112
113/** There might be two directions for every endpoint number (except 0) */
114#define USB_ENDPOINT_COUNT (2 * USB_ENDPOINT_MAX)
115
116/** Check USB endpoint for allowed values.
117 *
118 * @param ep USB endpoint number.
119 *
120 * @return True, if value is wihtin limits, false otherwise.
121 *
122 */
123static inline bool usb_endpoint_is_valid(usb_endpoint_t ep)
124{
125 return ep < USB_ENDPOINT_MAX;
126}
127
128/**
129 * Check USB target for allowed values (address, endpoint, stream).
130 *
131 * @param target.
132 * @return True, if values are wihtin limits, false otherwise.
133 */
134static inline bool usb_target_is_valid(usb_target_t *target)
135{
136 return usb_address_is_valid(target->address) &&
137 usb_endpoint_is_valid(target->endpoint);
138
139 // A 16-bit Stream ID is always valid.
140}
141
142/** Compare USB targets (addresses and endpoints).
143 *
144 * @param a First target.
145 * @param b Second target.
146 * @return Whether @p a and @p b points to the same pipe on the same device.
147 */
148static inline bool usb_target_same(usb_target_t a, usb_target_t b)
149{
150 return (a.address == b.address)
151 && (a.endpoint == b.endpoint);
152}
153
154static inline bool usb_transfer_type_is_valid(usb_transfer_type_t type)
155{
156 return (type >= 0) && (type < USB_TRANSFER_COUNT);
157}
158
159static inline bool usb_direction_is_valid(usb_direction_t dir)
160{
161 return (dir >= 0) && (dir < USB_DIRECTION_COUNT);
162}
163
164/** USB packet identifier. */
165typedef enum {
166#define _MAKE_PID_NIBBLE(tag, type) \
167 ((uint8_t)(((tag) << 2) | (type)))
168#define _MAKE_PID(tag, type) \
169 ( \
170 _MAKE_PID_NIBBLE(tag, type) \
171 | (((~_MAKE_PID_NIBBLE(tag, type)) & 0xf) << 4) \
172 )
173 USB_PID_OUT = _MAKE_PID(0, 1),
174 USB_PID_IN = _MAKE_PID(2, 1),
175 USB_PID_SOF = _MAKE_PID(1, 1),
176 USB_PID_SETUP = _MAKE_PID(3, 1),
177
178 USB_PID_DATA0 = _MAKE_PID(0 ,3),
179 USB_PID_DATA1 = _MAKE_PID(2 ,3),
180
181 USB_PID_ACK = _MAKE_PID(0 ,2),
182 USB_PID_NAK = _MAKE_PID(2 ,2),
183 USB_PID_STALL = _MAKE_PID(3 ,2),
184
185 USB_PID_PRE = _MAKE_PID(3 ,0),
186 /* USB_PID_ = _MAKE_PID( ,), */
187#undef _MAKE_PID
188#undef _MAKE_PID_NIBBLE
189} usb_packet_id;
190
191/** Category for USB host controllers. */
192#define USB_HC_CATEGORY "usbhc"
193
194#endif
195/**
196 * @}
197 */
Note: See TracBrowser for help on using the repository browser.