Root/atusb/fw/usb/usb.h

Source at commit a6c639bbb35ad975ebff5b3644362da37def9dc4 created 4 years 7 months ago.
By Werner Almesberger, atusb/fw/usb/usb.h: add a few common class codes
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 * Setup request types
73 */
74
75#define TO_DEVICE(req) (0x00 | (req) << 8)
76#define FROM_DEVICE(req) (0x80 | (req) << 8)
77#define TO_INTERFACE(req) (0x01 | (req) << 8)
78#define FROM_INTERFACE(req) (0x81 | (req) << 8)
79#define TO_ENDPOINT(req) (0x02 | (req) << 8)
80#define FROM_ENDPOINT(req) (0x82 | (req) << 8)
81
82/*
83 * Setup requests
84 */
85
86#define GET_STATUS 0x00
87#define CLEAR_FEATURE 0x01
88#define SET_FEATURE 0x03
89#define SET_ADDRESS 0x05
90#define GET_DESCRIPTOR 0x06
91#define SET_DESCRIPTOR 0x07
92#define GET_CONFIGURATION 0x08
93#define SET_CONFIGURATION 0x09
94#define GET_INTERFACE 0x0a
95#define SET_INTERFACE 0x0b
96#define SYNCH_FRAME 0x0c
97
98/*
99 * USB Language ID codes
100 *
101 * http://www.usb.org/developers/docs/USB_LANGIDs.pdf
102 */
103
104#define USB_LANGID_ENGLISH_US 0x409
105
106
107/*
108 * Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,
109 * uint8_t. Hence the cast.
110 */
111
112#define LE(x) ((uint16_t) (x) & 0xff), ((uint16_t) (x) >> 8)
113
114#define LO(x) (((uint8_t *) &(x))[0])
115#define HI(x) (((uint8_t *) &(x))[1])
116
117
118#ifdef LOW_SPEED
119#define EP0_SIZE 8
120#else
121#define EP0_SIZE 64
122#endif
123
124#define EP1_SIZE 64 /* simplify */
125
126
127enum ep_state {
128    EP_IDLE,
129    EP_RX,
130    EP_TX,
131    EP_STALL,
132};
133
134struct ep_descr {
135    enum ep_state state;
136    uint8_t *buf;
137    uint8_t *end;
138    uint8_t size;
139    void (*callback)(void *user);
140    void *user;
141};
142
143struct setup_request {
144    uint8_t bmRequestType;
145    uint8_t bRequest;
146    uint16_t wValue;
147    uint16_t wIndex;
148    uint16_t wLength;
149};
150
151
152extern const uint8_t device_descriptor[];
153extern const uint8_t config_descriptor[];
154extern struct ep_descr eps[];
155
156extern bool (*user_setup)(const struct setup_request *setup);
157extern void (*user_set_interface)(int nth);
158extern bool (*user_get_descriptor)(uint8_t type, uint8_t index,
159    const uint8_t **reply, uint8_t *size);
160extern void (*user_reset)(void);
161
162
163#define usb_left(ep) ((ep)->end-(ep)->buf)
164#define usb_send(ep, buf, size, callback, user) \
165    usb_io(ep, EP_TX, (void *) buf, size, callback, user)
166#define usb_recv(ep, buf, size, callback, user) \
167    usb_io(ep, EP_RX, buf, size, callback, user)
168
169void usb_io(struct ep_descr *ep, enum ep_state state, uint8_t *buf,
170    uint8_t size, void (*callback)(void *user), void *user);
171
172bool handle_setup(const struct setup_request *setup);
173void set_addr(uint8_t addr);
174void usb_ep_change(struct ep_descr *ep);
175void usb_reset(void);
176void usb_enable_bus_reset(void);
177void usb_init(void);
178
179#endif /* !USB_H */
180

Archive Download this file



interactive