Root/atusb/fw/usb/usb.h

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

Archive Download this file



interactive