Add firmware for the ATUSB IEEE 802.15.4 USB Adapter
[linux-libre-firmware.git] / atusb / usb / usb.h
1 /*
2  * fw/usb//usb.h - USB hardware setup and standard device requests
3  *
4  * Written 2008, 2009, 2011, 2013, 2015 by Werner Almesberger
5  * Copyright 2008, 2009, 2011, 2013, 2015 Werner Almesberger
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13
14 #ifndef USB_H
15 #define USB_H
16
17
18 #include <stdbool.h>
19 #include <stdint.h>
20
21
22 /*
23  * Packet identifier types
24  */
25
26 #define PID_OUT         0x1
27 #define PID_IN          0x9
28 #define PID_SOF         0x5
29 #define PID_SETUP       0xd
30 #define PID_DATA0       0x3
31 #define PID_DATA1       0xb
32 #define PID_ACK         0x2
33 #define PID_NAK         0xa
34 #define PID_STALL       0xe
35
36 /*
37  * Descriptor types
38  *
39  * Reuse libusb naming scheme (/usr/include/usb.h)
40  */
41
42 #define USB_DT_DEVICE           1
43 #define USB_DT_CONFIG           2
44 #define USB_DT_STRING           3
45 #define USB_DT_INTERFACE        4
46 #define USB_DT_ENDPOINT         5
47
48 /*
49  * Device classes
50  *
51  * Reuse libusb naming scheme (/usr/include/usb.h)
52  */
53
54 #define USB_CLASS_PER_INTERFACE 0
55 #define USB_CLASS_COMM          2
56 #define USB_CLASS_HID           3
57 #define USB_CLASS_MASS_STORAGE  8
58 #define USB_CLASS_HUB           9
59 #define USB_CLASS_DATA          10
60 #define USB_CLASS_APP_SPEC      0xfe
61 #define USB_CLASS_VENDOR_SPEC   0xff
62
63 /*
64  * Configuration attributes
65  */
66
67 #define USB_ATTR_BUS_POWERED    0x80
68 #define USB_ATTR_SELF_POWERED   0x40
69 #define USB_ATTR_REMOTE_WAKEUP  0x20
70
71 /*
72  * Endpoint type
73  */
74
75 #define USB_ENDPOINT_TYPE_CONTROL       0
76 #define USB_ENDPOINT_TYPE_ISOCHRONOUS   1
77 #define USB_ENDPOINT_TYPE_BULK          2
78 #define USB_ENDPOINT_TYPE_INTERRUPT     3
79
80 /*
81  * Setup request types
82  */
83
84 #define TO_DEVICE(req)          (0x00 | (req) << 8)
85 #define FROM_DEVICE(req)        (0x80 | (req) << 8)
86 #define TO_INTERFACE(req)       (0x01 | (req) << 8)
87 #define FROM_INTERFACE(req)     (0x81 | (req) << 8)
88 #define TO_ENDPOINT(req)        (0x02 | (req) << 8)
89 #define FROM_ENDPOINT(req)      (0x82 | (req) << 8)
90
91 /*
92  * Setup requests
93  */
94
95 #define GET_STATUS              0x00
96 #define CLEAR_FEATURE           0x01
97 #define SET_FEATURE             0x03
98 #define SET_ADDRESS             0x05
99 #define GET_DESCRIPTOR          0x06
100 #define SET_DESCRIPTOR          0x07
101 #define GET_CONFIGURATION       0x08
102 #define SET_CONFIGURATION       0x09
103 #define GET_INTERFACE           0x0a
104 #define SET_INTERFACE           0x0b
105 #define SYNCH_FRAME             0x0c
106
107 /*
108  * USB Language ID codes
109  *
110  * http://www.usb.org/developers/docs/USB_LANGIDs.pdf
111  */
112
113 #define USB_LANGID_ENGLISH_US   0x409
114
115
116 /*
117  * Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,
118  * uint8_t. Hence the cast.
119  */
120
121 #define LE(x) ((uint16_t) (x) & 0xff), ((uint16_t) (x) >> 8)
122
123 #define LO(x) (((uint8_t *) &(x))[0])
124 #define HI(x) (((uint8_t *) &(x))[1])
125
126
127 #ifdef LOW_SPEED
128 #define EP0_SIZE        8
129 #else
130 #define EP0_SIZE        64
131 #endif
132
133 #define EP1_SIZE        64      /* simplify */
134
135
136 enum ep_state {
137         EP_IDLE,
138         EP_RX,
139         EP_TX,
140         EP_STALL,
141 };
142
143 struct ep_descr {
144         enum ep_state state;
145         uint8_t *buf;
146         uint8_t *end;
147         uint8_t size;
148         void (*callback)(void *user);
149         void *user;
150 };
151
152 struct setup_request {
153         uint8_t bmRequestType;
154         uint8_t bRequest;
155         uint16_t wValue;
156         uint16_t wIndex;
157         uint16_t wLength;
158 };
159
160
161 extern const uint8_t device_descriptor[];
162 extern const uint8_t config_descriptor[];
163 extern struct ep_descr eps[];
164
165 extern bool (*user_setup)(const struct setup_request *setup);
166 extern void (*user_set_interface)(int nth);
167 extern bool (*user_get_descriptor)(uint8_t type, uint8_t index,
168     const uint8_t **reply, uint8_t *size);
169 extern void (*user_reset)(void);
170
171
172 #define usb_left(ep) ((ep)->end-(ep)->buf)
173 #define usb_send(ep, buf, size, callback, user) \
174         usb_io(ep, EP_TX, (void *) buf, size, callback, user)
175 #define usb_recv(ep, buf, size, callback, user) \
176         usb_io(ep, EP_RX, buf, size, callback, user)
177
178 void usb_io(struct ep_descr *ep, enum ep_state state, uint8_t *buf,
179     uint8_t size, void (*callback)(void *user), void *user);
180
181 bool handle_setup(const struct setup_request *setup);
182 void set_addr(uint8_t addr);
183 void usb_ep_change(struct ep_descr *ep);
184 void usb_reset(void);
185 void usb_init(void);
186
187 void ep_init(void);
188
189 #endif /* !USB_H */