source: mainline/uspace/lib/usb/include/usb/usb.h@ 3e6a98c5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e6a98c5 was 3e6a98c5, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Standards-compliant boolean type.

  • Property mode set to 100644
File size: 5.4 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/** USB endpoint number type.
115 * Negative values could be used to indicate error.
116 */
117typedef int16_t usb_endpoint_t;
118
119/** Maximum endpoint number in USB 1.1.
120 */
121#define USB11_ENDPOINT_MAX 16
122
123
124/** USB complete address type.
125 * Pair address + endpoint is identification of transaction recipient.
126 */
127typedef union {
128 struct {
129 usb_address_t address;
130 usb_endpoint_t endpoint;
131 } __attribute__((packed));
132 uint32_t packed;
133} usb_target_t;
134
135/** Check USB target for allowed values (address and endpoint).
136 *
137 * @param target.
138 * @return True, if values are wihtin limits, false otherwise.
139 */
140static inline bool usb_target_is_valid(usb_target_t target)
141{
142 return !(target.endpoint > 15 || target.endpoint < 0
143 || target.address >= USB11_ADDRESS_MAX || target.address < 0);
144}
145
146/** Compare USB targets (addresses and endpoints).
147 *
148 * @param a First target.
149 * @param b Second target.
150 * @return Whether @p a and @p b points to the same pipe on the same device.
151 */
152static inline int usb_target_same(usb_target_t a, usb_target_t b)
153{
154 return (a.address == b.address)
155 && (a.endpoint == b.endpoint);
156}
157
158/** General handle type.
159 * Used by various USB functions as opaque handle.
160 */
161typedef sysarg_t usb_handle_t;
162
163/** USB packet identifier. */
164typedef enum {
165#define _MAKE_PID_NIBBLE(tag, type) \
166 ((uint8_t)(((tag) << 2) | (type)))
167#define _MAKE_PID(tag, type) \
168 ( \
169 _MAKE_PID_NIBBLE(tag, type) \
170 | ((~_MAKE_PID_NIBBLE(tag, type)) << 4) \
171 )
172 USB_PID_OUT = _MAKE_PID(0, 1),
173 USB_PID_IN = _MAKE_PID(2, 1),
174 USB_PID_SOF = _MAKE_PID(1, 1),
175 USB_PID_SETUP = _MAKE_PID(3, 1),
176
177 USB_PID_DATA0 = _MAKE_PID(0 ,3),
178 USB_PID_DATA1 = _MAKE_PID(2 ,3),
179
180 USB_PID_ACK = _MAKE_PID(0 ,2),
181 USB_PID_NAK = _MAKE_PID(2 ,2),
182 USB_PID_STALL = _MAKE_PID(3 ,2),
183
184 USB_PID_PRE = _MAKE_PID(3 ,0),
185 /* USB_PID_ = _MAKE_PID( ,), */
186#undef _MAKE_PID
187#undef _MAKE_PID_NIBBLE
188} usb_packet_id;
189
190/** Category for USB host controllers. */
191#define USB_HC_CATEGORY "usbhc"
192
193#endif
194/**
195 * @}
196 */
Note: See TracBrowser for help on using the repository browser.