source: mainline/uspace/lib/usb/include/usb/usb.h@ 711f5fb8

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

libusb: Add address and endpoint sanity helpers.

  • Property mode set to 100644
File size: 6.0 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 <stdbool.h>
38#include <sys/types.h>
39#include <byteorder.h>
40
41/** Convert 16bit value from native (host) endianness to USB endianness. */
42#define uint16_host2usb(n) host2uint16_t_le((n))
43
44/** Convert 32bit value from native (host) endianness to USB endianness. */
45#define uint32_host2usb(n) host2uint32_t_le((n))
46
47/** Convert 16bit value from USB endianness into native (host) one. */
48#define uint16_usb2host(n) uint16_t_le2host((n))
49
50/** Convert 32bit value from USB endianness into native (host) one. */
51#define uint32_usb2host(n) uint32_t_le2host((n))
52
53
54/** USB transfer type. */
55typedef enum {
56 USB_TRANSFER_CONTROL = 0,
57 USB_TRANSFER_ISOCHRONOUS = 1,
58 USB_TRANSFER_BULK = 2,
59 USB_TRANSFER_INTERRUPT = 3
60} usb_transfer_type_t;
61
62const char * usb_str_transfer_type(usb_transfer_type_t t);
63const char * usb_str_transfer_type_short(usb_transfer_type_t t);
64
65/** USB data transfer direction. */
66typedef enum {
67 USB_DIRECTION_IN,
68 USB_DIRECTION_OUT,
69 USB_DIRECTION_BOTH
70} usb_direction_t;
71
72const char *usb_str_direction(usb_direction_t);
73
74/** USB speeds. */
75typedef enum {
76 /** USB 1.1 low speed (1.5Mbits/s). */
77 USB_SPEED_LOW,
78 /** USB 1.1 full speed (12Mbits/s). */
79 USB_SPEED_FULL,
80 /** USB 2.0 high speed (480Mbits/s). */
81 USB_SPEED_HIGH,
82 /** Psuedo-speed serving as a boundary. */
83 USB_SPEED_MAX
84} usb_speed_t;
85
86const char *usb_str_speed(usb_speed_t);
87
88
89/** USB request type target. */
90typedef enum {
91 USB_REQUEST_TYPE_STANDARD = 0,
92 USB_REQUEST_TYPE_CLASS = 1,
93 USB_REQUEST_TYPE_VENDOR = 2
94} usb_request_type_t;
95
96/** USB request recipient. */
97typedef enum {
98 USB_REQUEST_RECIPIENT_DEVICE = 0,
99 USB_REQUEST_RECIPIENT_INTERFACE = 1,
100 USB_REQUEST_RECIPIENT_ENDPOINT = 2,
101 USB_REQUEST_RECIPIENT_OTHER = 3
102} usb_request_recipient_t;
103
104/** USB address type.
105 * Negative values could be used to indicate error.
106 */
107typedef int16_t usb_address_t;
108
109/** Default USB address. */
110#define USB_ADDRESS_DEFAULT 0
111/** Maximum address number in USB 1.1. */
112#define USB11_ADDRESS_MAX 128
113
114/** Check USB address for allowed values.
115 *
116 * @param ep USB address.
117 * @return True, if value is wihtin limits, false otherwise.
118 */
119static inline bool usb_address_is_valid(usb_address_t a)
120{
121 return (a >= USB_ADDRESS_DEFAULT) && (a < USB11_ADDRESS_MAX);
122}
123
124/** USB endpoint number type.
125 * Negative values could be used to indicate error.
126 */
127typedef int16_t usb_endpoint_t;
128
129/** Default control endpoint */
130#define USB_ENDPOINT_DEFAULT_CONTROL 0
131/** Maximum endpoint number in USB 1.1. */
132#define USB11_ENDPOINT_MAX 16
133
134/** Check USB endpoint for allowed values.
135 *
136 * @param ep USB endpoint number.
137 * @return True, if value is wihtin limits, false otherwise.
138 */
139static inline bool usb_endpoint_is_valid(usb_endpoint_t ep)
140{
141 return (ep >= USB_ENDPOINT_DEFAULT_CONTROL) &&
142 (ep < USB11_ENDPOINT_MAX);
143}
144
145
146/** USB complete address type.
147 * Pair address + endpoint is identification of transaction recipient.
148 */
149typedef union {
150 struct {
151 usb_address_t address;
152 usb_endpoint_t endpoint;
153 } __attribute__((packed));
154 uint32_t packed;
155} usb_target_t;
156
157
158/** Check USB target for allowed values (address and endpoint).
159 *
160 * @param target.
161 * @return True, if values are wihtin limits, false otherwise.
162 */
163static inline bool usb_target_is_valid(usb_target_t target)
164{
165 return usb_address_is_valid(target.address) &&
166 usb_endpoint_is_valid(target.endpoint);
167}
168
169/** Compare USB targets (addresses and endpoints).
170 *
171 * @param a First target.
172 * @param b Second target.
173 * @return Whether @p a and @p b points to the same pipe on the same device.
174 */
175static inline int usb_target_same(usb_target_t a, usb_target_t b)
176{
177 return (a.address == b.address)
178 && (a.endpoint == b.endpoint);
179}
180
181/** General handle type.
182 * Used by various USB functions as opaque handle.
183 */
184typedef sysarg_t usb_handle_t;
185
186/** USB packet identifier. */
187typedef enum {
188#define _MAKE_PID_NIBBLE(tag, type) \
189 ((uint8_t)(((tag) << 2) | (type)))
190#define _MAKE_PID(tag, type) \
191 ( \
192 _MAKE_PID_NIBBLE(tag, type) \
193 | ((~_MAKE_PID_NIBBLE(tag, type)) << 4) \
194 )
195 USB_PID_OUT = _MAKE_PID(0, 1),
196 USB_PID_IN = _MAKE_PID(2, 1),
197 USB_PID_SOF = _MAKE_PID(1, 1),
198 USB_PID_SETUP = _MAKE_PID(3, 1),
199
200 USB_PID_DATA0 = _MAKE_PID(0 ,3),
201 USB_PID_DATA1 = _MAKE_PID(2 ,3),
202
203 USB_PID_ACK = _MAKE_PID(0 ,2),
204 USB_PID_NAK = _MAKE_PID(2 ,2),
205 USB_PID_STALL = _MAKE_PID(3 ,2),
206
207 USB_PID_PRE = _MAKE_PID(3 ,0),
208 /* USB_PID_ = _MAKE_PID( ,), */
209#undef _MAKE_PID
210#undef _MAKE_PID_NIBBLE
211} usb_packet_id;
212
213/** Category for USB host controllers. */
214#define USB_HC_CATEGORY "usbhc"
215
216#endif
217/**
218 * @}
219 */
Note: See TracBrowser for help on using the repository browser.