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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a90fc0c was a90fc0c, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Refactoring of "enum to string" functions

  • Property mode set to 100644
File size: 4.8 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 types and functions.
34 */
35#ifndef LIBUSB_USB_H_
36#define LIBUSB_USB_H_
37
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
72/** USB speeds. */
73typedef enum {
74 /** USB 1.1 low speed (1.5Mbits/s). */
75 USB_SPEED_LOW,
76 /** USB 1.1 full speed (12Mbits/s). */
77 USB_SPEED_FULL,
78 /** USB 2.0 high speed (480Mbits/s). */
79 USB_SPEED_HIGH
80} usb_speed_t;
81
82const char *usb_str_speed(usb_speed_t);
83
84
85/** USB request type target. */
86typedef enum {
87 USB_REQUEST_TYPE_STANDARD = 0,
88 USB_REQUEST_TYPE_CLASS = 1,
89 USB_REQUEST_TYPE_VENDOR = 2
90} usb_request_type_t;
91
92/** USB request recipient. */
93typedef enum {
94 USB_REQUEST_RECIPIENT_DEVICE = 0,
95 USB_REQUEST_RECIPIENT_INTERFACE = 1,
96 USB_REQUEST_RECIPIENT_ENDPOINT = 2
97} usb_request_recipient_t;
98
99/** USB address type.
100 * Negative values could be used to indicate error.
101 */
102typedef int usb_address_t;
103
104/** Default USB address. */
105#define USB_ADDRESS_DEFAULT 0
106/** Maximum address number in USB 1.1. */
107#define USB11_ADDRESS_MAX 128
108
109/** USB endpoint number type.
110 * Negative values could be used to indicate error.
111 */
112typedef int usb_endpoint_t;
113
114/** Maximum endpoint number in USB 1.1.
115 */
116#define USB11_ENDPOINT_MAX 16
117
118
119/** USB complete address type.
120 * Pair address + endpoint is identification of transaction recipient.
121 */
122typedef struct {
123 usb_address_t address;
124 usb_endpoint_t endpoint;
125} usb_target_t;
126
127/** Compare USB targets (addresses and endpoints).
128 *
129 * @param a First target.
130 * @param b Second target.
131 * @return Whether @p a and @p b points to the same pipe on the same device.
132 */
133static inline int usb_target_same(usb_target_t a, usb_target_t b)
134{
135 return (a.address == b.address)
136 && (a.endpoint == b.endpoint);
137}
138
139/** General handle type.
140 * Used by various USB functions as opaque handle.
141 */
142typedef sysarg_t usb_handle_t;
143
144/** USB packet identifier. */
145typedef enum {
146#define _MAKE_PID_NIBBLE(tag, type) \
147 ((uint8_t)(((tag) << 2) | (type)))
148#define _MAKE_PID(tag, type) \
149 ( \
150 _MAKE_PID_NIBBLE(tag, type) \
151 | ((~_MAKE_PID_NIBBLE(tag, type)) << 4) \
152 )
153 USB_PID_OUT = _MAKE_PID(0, 1),
154 USB_PID_IN = _MAKE_PID(2, 1),
155 USB_PID_SOF = _MAKE_PID(1, 1),
156 USB_PID_SETUP = _MAKE_PID(3, 1),
157
158 USB_PID_DATA0 = _MAKE_PID(0 ,3),
159 USB_PID_DATA1 = _MAKE_PID(2 ,3),
160
161 USB_PID_ACK = _MAKE_PID(0 ,2),
162 USB_PID_NAK = _MAKE_PID(2 ,2),
163 USB_PID_STALL = _MAKE_PID(3 ,2),
164
165 USB_PID_PRE = _MAKE_PID(3 ,0),
166 /* USB_PID_ = _MAKE_PID( ,), */
167#undef _MAKE_PID
168#undef _MAKE_PID_NIBBLE
169} usb_packet_id;
170
171#endif
172/**
173 * @}
174 */
Note: See TracBrowser for help on using the repository browser.