2 * fw/usb//usb.h - USB hardware setup and standard device requests
4 * Written 2008, 2009, 2011, 2013, 2015 by Werner Almesberger
5 * Copyright 2008, 2009, 2011, 2013, 2015 Werner Almesberger
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.
23 * Packet identifier types
39 * Reuse libusb naming scheme (/usr/include/usb.h)
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
51 * Reuse libusb naming scheme (/usr/include/usb.h)
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
64 * Configuration attributes
67 #define USB_ATTR_BUS_POWERED 0x80
68 #define USB_ATTR_SELF_POWERED 0x40
69 #define USB_ATTR_REMOTE_WAKEUP 0x20
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
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)
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
108 * USB Language ID codes
110 * http://www.usb.org/developers/docs/USB_LANGIDs.pdf
113 #define USB_LANGID_ENGLISH_US 0x409
117 * Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,
118 * uint8_t. Hence the cast.
121 #define LE(x) ((uint16_t) (x) & 0xff), ((uint16_t) (x) >> 8)
123 #define LO(x) (((uint8_t *) &(x))[0])
124 #define HI(x) (((uint8_t *) &(x))[1])
133 #define EP1_SIZE 64 /* simplify */
148 void (*callback)(void *user);
152 struct setup_request {
153 uint8_t bmRequestType;
161 extern const uint8_t device_descriptor[];
162 extern const uint8_t config_descriptor[];
163 extern struct ep_descr eps[];
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);
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)
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);
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);