]> git.sur5r.net Git - openocd/blob - src/jtag/drivers/OpenULINK/include/usb.h
Remove FSF address from GPL notices
[openocd] / src / jtag / drivers / OpenULINK / include / usb.h
1 /***************************************************************************
2  *   Copyright (C) 2011 by Martin Schmoelzer                               *
3  *   <martin.schmoelzer@student.tuwien.ac.at>                              *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
17  ***************************************************************************/
18
19 #ifndef __USB_H
20 #define __USB_H
21
22 #include "reg_ezusb.h"
23
24 #include <stdint.h>
25 #include <stdbool.h>
26
27 #define NULL        (void *)0;
28
29 /* High and Low byte of a word (uint16_t) */
30 #define HI8(word)   (uint8_t)(((uint16_t)word >> 8) & 0xff)
31 #define LO8(word)   (uint8_t)((uint16_t)word & 0xff)
32
33 /* Convenience functions */
34 #define STALL_EP0()   (EP0CS |= EP0STALL)
35 #define CLEAR_IRQ()   (EXIF &= ~USBINT)
36
37 /*********** USB descriptors. See section 9.5 of the USB 1.1 spec **********/
38
39 /* USB Descriptor Types. See USB 1.1 spec, page 187, table 9-5 */
40 #define DESCRIPTOR_TYPE_DEVICE         0x01
41 #define DESCRIPTOR_TYPE_CONFIGURATION  0x02
42 #define DESCRIPTOR_TYPE_STRING         0x03
43 #define DESCRIPTOR_TYPE_INTERFACE      0x04
44 #define DESCRIPTOR_TYPE_ENDPOINT       0x05
45
46 #define STR_DESCR(len, ...) { len * 2 + 2, DESCRIPTOR_TYPE_STRING, { __VA_ARGS__ } }
47
48 /** USB Device Descriptor. See USB 1.1 spec, pp. 196 - 198 */
49 struct usb_device_descriptor {
50         uint8_t bLength;                /**< Size of this descriptor in bytes. */
51         uint8_t bDescriptorType;        /**< DEVICE Descriptor Type. */
52         uint16_t bcdUSB;                /**< USB specification release number (BCD). */
53         uint8_t bDeviceClass;           /**< Class code. */
54         uint8_t bDeviceSubClass;        /**< Subclass code. */
55         uint8_t bDeviceProtocol;        /**< Protocol code. */
56         uint8_t bMaxPacketSize0;        /**< Maximum packet size for EP0 (8, 16, 32, 64). */
57         uint16_t idVendor;              /**< USB Vendor ID. */
58         uint16_t idProduct;             /**< USB Product ID. */
59         uint16_t bcdDevice;             /**< Device Release Number (BCD). */
60         uint8_t iManufacturer;          /**< Index of manufacturer string descriptor. */
61         uint8_t iProduct;               /**< Index of product string descriptor. */
62         uint8_t iSerialNumber;          /**< Index of string descriptor containing serial #. */
63         uint8_t bNumConfigurations;     /**< Number of possible configurations. */
64 };
65
66 /** USB Configuration Descriptor. See USB 1.1 spec, pp. 199 - 200 */
67 struct usb_config_descriptor {
68         uint8_t bLength;                /**< Size of this descriptor in bytes. */
69         uint8_t bDescriptorType;        /**< CONFIGURATION descriptor type. */
70         uint16_t wTotalLength;          /**< Combined total length of all descriptors. */
71         uint8_t bNumInterfaces;         /**< Number of interfaces in this configuration. */
72         uint8_t bConfigurationValue;    /**< Value used to select this configuration. */
73         uint8_t iConfiguration;         /**< Index of configuration string descriptor. */
74         uint8_t bmAttributes;           /**< Configuration characteristics. */
75         uint8_t MaxPower;               /**< Maximum power consumption in 2 mA units. */
76 };
77
78 /** USB Interface Descriptor. See USB 1.1 spec, pp. 201 - 203 */
79 struct usb_interface_descriptor {
80         uint8_t bLength;                /**< Size of this descriptor in bytes. */
81         uint8_t bDescriptorType;        /**< INTERFACE descriptor type. */
82         uint8_t bInterfaceNumber;       /**< Interface number. */
83         uint8_t bAlternateSetting;      /**< Value used to select alternate setting. */
84         uint8_t bNumEndpoints;          /**< Number of endpoints used by this interface. */
85         uint8_t bInterfaceClass;        /**< Class code. */
86         uint8_t bInterfaceSubclass;     /**< Subclass code. */
87         uint8_t bInterfaceProtocol;     /**< Protocol code. */
88         uint8_t iInterface;             /**< Index of interface string descriptor. */
89 };
90
91 /** USB Endpoint Descriptor. See USB 1.1 spec, pp. 203 - 204 */
92 struct usb_endpoint_descriptor {
93         uint8_t bLength;                /**< Size of this descriptor in bytes. */
94         uint8_t bDescriptorType;        /**< ENDPOINT descriptor type. */
95         uint8_t bEndpointAddress;       /**< Endpoint Address: USB 1.1 spec, table 9-10. */
96         uint8_t bmAttributes;           /**< Endpoint Attributes: USB 1.1 spec, table 9-10. */
97         uint16_t wMaxPacketSize;        /**< Maximum packet size for this endpoint. */
98         uint8_t bInterval;              /**< Polling interval (in ms) for this endpoint. */
99 };
100
101 /** USB Language Descriptor. See USB 1.1 spec, pp. 204 - 205 */
102 struct usb_language_descriptor {
103         uint8_t bLength;                /**< Size of this descriptor in bytes. */
104         uint8_t bDescriptorType;        /**< STRING descriptor type. */
105         uint16_t wLANGID[];             /**< LANGID codes. */
106 };
107
108 /** USB String Descriptor. See USB 1.1 spec, pp. 204 - 205 */
109 struct usb_string_descriptor {
110         uint8_t bLength;                /**< Size of this descriptor in bytes. */
111         uint8_t bDescriptorType;        /**< STRING descriptor type. */
112         uint16_t bString[];             /**< UNICODE encoded string. */
113 };
114
115 /********************** USB Control Endpoint 0 related *********************/
116
117 /** USB Control Setup Data. See USB 1.1 spec, pp. 183 - 185 */
118 struct setup_data {
119         uint8_t bmRequestType;          /**< Characteristics of a request. */
120         uint8_t bRequest;               /**< Specific request. */
121         uint16_t wValue;                /**< Field that varies according to request. */
122         uint16_t wIndex;                /**< Field that varies according to request. */
123         uint16_t wLength;               /**< Number of bytes to transfer in data stage. */
124 };
125
126 /* External declarations for variables that need to be accessed outside of
127  * the USB module */
128 extern volatile bool EP2_out;
129 extern volatile bool EP2_in;
130 extern volatile __xdata __at 0x7FE8 struct setup_data setup_data;
131
132 /*
133  * USB Request Types (bmRequestType): See USB 1.1 spec, page 183, table 9-2
134  *
135  * Bit 7: Data transfer direction
136  *    0 = Host-to-device
137  *    1 = Device-to-host
138  * Bit 6...5: Type
139  *    0 = Standard
140  *    1 = Class
141  *    2 = Vendor
142  *    3 = Reserved
143  * Bit 4...0: Recipient
144  *    0 = Device
145  *    1 = Interface
146  *    2 = Endpoint
147  *    3 = Other
148  *    4...31 = Reserved
149  */
150
151 #define USB_DIR_OUT             0x00
152 #define USB_DIR_IN              0x80
153
154 #define USB_REQ_TYPE_STANDARD   (0x00 << 5)
155 #define USB_REQ_TYPE_CLASS      (0x01 << 5)
156 #define USB_REQ_TYPE_VENDOR     (0x02 << 5)
157 #define USB_REQ_TYPE_RESERVED   (0x03 << 5)
158
159 #define USB_RECIP_DEVICE        0x00
160 #define USB_RECIP_INTERFACE     0x01
161 #define USB_RECIP_ENDPOINT      0x02
162 #define USB_RECIP_OTHER         0x03
163
164 /* bmRequestType for USB Standard Requests */
165
166 /* Clear Interface Request */
167 #define CF_DEVICE    (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
168 #define CF_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
169 #define CF_ENDPOINT  (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
170
171 /* Get Configuration Request */
172 #define GC_DEVICE    (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
173
174 /* Get Descriptor Request */
175 #define GD_DEVICE    (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
176
177 /* Get Interface Request */
178 #define GI_INTERFACE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
179
180 /* Get Status Request: See USB 1.1 spec, page 190 */
181 #define GS_DEVICE    (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
182 #define GS_INTERFACE (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
183 #define GS_ENDPOINT  (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
184
185 /* Set Address Request is handled by EZ-USB core */
186
187 /* Set Configuration Request */
188 #define SC_DEVICE    (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
189
190 /* Set Descriptor Request */
191 #define SD_DEVICE    (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
192
193 /* Set Feature Request */
194 #define SF_DEVICE    (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_DEVICE)
195 #define SF_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
196 #define SF_ENDPOINT  (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
197
198 /* Set Interface Request */
199 #define SI_INTERFACE (USB_DIR_OUT | USB_REQ_TYPE_STANDARD | USB_RECIP_INTERFACE)
200
201 /* Synch Frame Request */
202 #define SY_ENDPOINT  (USB_DIR_IN | USB_REQ_TYPE_STANDARD | USB_RECIP_ENDPOINT)
203
204 /* USB Requests (bRequest): See USB 1.1 spec, table 9-4 on page 187 */
205 #define GET_STATUS               0
206 #define CLEAR_FEATURE            1
207 /* Value '2' is reserved for future use */
208 #define SET_FEATURE              3
209 /* Value '4' is reserved for future use */
210 #define SET_ADDRESS              5
211 #define GET_DESCRIPTOR           6
212 #define SET_DESCRIPTOR           7
213 #define GET_CONFIGURATION        8
214 #define SET_CONFIGURATION        9
215 #define GET_INTERFACE           10
216 #define SET_INTERFACE           11
217 #define SYNCH_FRAME             12
218
219 /* Standard Feature Selectors: See USB 1.1 spec, table 9-6 on page 188 */
220 #define DEVICE_REMOTE_WAKEUP     1
221 #define ENDPOINT_HALT            0
222
223 /************************** EZ-USB specific stuff **************************/
224
225 /** USB Interrupts. See AN2131-TRM, page 9-4 for details */
226 enum usb_isr {
227         SUDAV_ISR = 13,
228         SOF_ISR,
229         SUTOK_ISR,
230         SUSPEND_ISR,
231         USBRESET_ISR,
232         IBN_ISR,
233         EP0IN_ISR,
234         EP0OUT_ISR,
235         EP1IN_ISR,
236         EP1OUT_ISR,
237         EP2IN_ISR,
238         EP2OUT_ISR,
239         EP3IN_ISR,
240         EP3OUT_ISR,
241         EP4IN_ISR,
242         EP4OUT_ISR,
243         EP5IN_ISR,
244         EP5OUT_ISR,
245         EP6IN_ISR,
246         EP6OUT_ISR,
247         EP7IN_ISR,
248         EP7OUT_ISR
249 };
250
251 /*************************** Function Prototypes ***************************/
252
253 __xdata uint8_t *usb_get_endpoint_cs_reg(uint8_t ep);
254 void usb_reset_data_toggle(uint8_t ep);
255
256 bool usb_handle_get_status(void);
257 bool usb_handle_clear_feature(void);
258 bool usb_handle_set_feature(void);
259 bool usb_handle_get_descriptor(void);
260 void usb_handle_set_interface(void);
261
262 void usb_handle_setup_data(void);
263 void usb_init(void);
264
265 #endif