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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d4da860 was d4da860, checked in by jzr <zarevucky.jiri@…>, 8 years ago

Break circular dependency between libdrv and libusb.

  • Property mode set to 100644
File size: 5.2 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 <usb_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
65const char *usb_str_speed(usb_speed_t);
66
67
68/** USB request type target. */
69typedef enum {
70 USB_REQUEST_TYPE_STANDARD = 0,
71 USB_REQUEST_TYPE_CLASS = 1,
72 USB_REQUEST_TYPE_VENDOR = 2
73} usb_request_type_t;
74
75/** USB request recipient. */
76typedef enum {
77 USB_REQUEST_RECIPIENT_DEVICE = 0,
78 USB_REQUEST_RECIPIENT_INTERFACE = 1,
79 USB_REQUEST_RECIPIENT_ENDPOINT = 2,
80 USB_REQUEST_RECIPIENT_OTHER = 3
81} usb_request_recipient_t;
82
83/** Default USB address. */
84#define USB_ADDRESS_DEFAULT 0
85
86/** Maximum address number in USB 1.1. */
87#define USB11_ADDRESS_MAX 127
88#define USB_ADDRESS_COUNT (USB11_ADDRESS_MAX + 1)
89
90/** Check USB address for allowed values.
91 *
92 * @param ep USB address.
93 *
94 * @return True, if value is wihtin limits, false otherwise.
95 *
96 */
97static inline bool usb_address_is_valid(usb_address_t a)
98{
99 return (a >= USB_ADDRESS_DEFAULT) && (a <= USB11_ADDRESS_MAX);
100}
101
102/** Default control endpoint */
103#define USB_ENDPOINT_DEFAULT_CONTROL 0
104
105/** Maximum endpoint number in USB 1.1. */
106#define USB11_ENDPOINT_MAX 16
107
108/** Check USB endpoint for allowed values.
109 *
110 * @param ep USB endpoint number.
111 *
112 * @return True, if value is wihtin limits, false otherwise.
113 *
114 */
115static inline bool usb_endpoint_is_valid(usb_endpoint_t ep)
116{
117 return (ep >= USB_ENDPOINT_DEFAULT_CONTROL) &&
118 (ep < USB11_ENDPOINT_MAX);
119}
120
121/** Check USB target for allowed values (address and endpoint).
122 *
123 * @param target.
124 * @return True, if values are wihtin limits, false otherwise.
125 */
126static inline bool usb_target_is_valid(usb_target_t target)
127{
128 return usb_address_is_valid(target.address) &&
129 usb_endpoint_is_valid(target.endpoint);
130}
131
132/** Compare USB targets (addresses and endpoints).
133 *
134 * @param a First target.
135 * @param b Second target.
136 * @return Whether @p a and @p b points to the same pipe on the same device.
137 */
138static inline int usb_target_same(usb_target_t a, usb_target_t b)
139{
140 return (a.address == b.address)
141 && (a.endpoint == b.endpoint);
142}
143
144/** General handle type.
145 * Used by various USB functions as opaque handle.
146 */
147typedef sysarg_t usb_handle_t;
148
149/** USB packet identifier. */
150typedef enum {
151#define _MAKE_PID_NIBBLE(tag, type) \
152 ((uint8_t)(((tag) << 2) | (type)))
153#define _MAKE_PID(tag, type) \
154 ( \
155 _MAKE_PID_NIBBLE(tag, type) \
156 | (((~_MAKE_PID_NIBBLE(tag, type)) & 0xf) << 4) \
157 )
158 USB_PID_OUT = _MAKE_PID(0, 1),
159 USB_PID_IN = _MAKE_PID(2, 1),
160 USB_PID_SOF = _MAKE_PID(1, 1),
161 USB_PID_SETUP = _MAKE_PID(3, 1),
162
163 USB_PID_DATA0 = _MAKE_PID(0 ,3),
164 USB_PID_DATA1 = _MAKE_PID(2 ,3),
165
166 USB_PID_ACK = _MAKE_PID(0 ,2),
167 USB_PID_NAK = _MAKE_PID(2 ,2),
168 USB_PID_STALL = _MAKE_PID(3 ,2),
169
170 USB_PID_PRE = _MAKE_PID(3 ,0),
171 /* USB_PID_ = _MAKE_PID( ,), */
172#undef _MAKE_PID
173#undef _MAKE_PID_NIBBLE
174} usb_packet_id;
175
176/** Category for USB host controllers. */
177#define USB_HC_CATEGORY "usbhc"
178
179#endif
180/**
181 * @}
182 */
Note: See TracBrowser for help on using the repository browser.