Root/atusb/fw/usb/usb.h

Source at commit d233c04c86c2fde39b5e069bb6750caef3393f32 created 8 years 8 months ago.
By Werner Almesberger, atusb/fw: implemented USB bus reset (to host) and polling of reset from host
1/*
2 * fw/usb//usb.h - USB hardware setup and standard device requests
3 *
4 * Written 2008, 2009 by Werner Almesberger
5 * Copyright 2008, 2009 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 <stdint.h>
19
20
21/*
22 * Descriptor types
23 *
24 * Reuse libusb naming scheme (/usr/include/usb.h)
25 */
26
27#define USB_DT_DEVICE 1
28#define USB_DT_CONFIG 2
29#define USB_DT_STRING 3
30#define USB_DT_INTERFACE 4
31#define USB_DT_ENDPOINT 5
32
33/*
34 * Device classes
35 *
36 * Reuse libusb naming scheme (/usr/include/usb.h)
37 */
38
39#define USB_CLASS_PER_INTERFACE 0xfe
40#define USB_CLASS_VENDOR_SPEC 0xff
41
42/*
43 * Configuration attributes
44 */
45
46#define USB_ATTR_BUS_POWERED 0x80
47#define USB_ATTR_SELF_POWERED 0x40
48#define USB_ATTR_REMOTE_WAKEUP 0x20
49
50/*
51 * Setup request types
52 */
53
54#define TO_DEVICE(req) (0x00 | (req) << 8)
55#define FROM_DEVICE(req) (0x80 | (req) << 8)
56#define TO_INTERFACE(req) (0x01 | (req) << 8)
57#define FROM_INTERFACE(req) (0x81 | (req) << 8)
58#define TO_ENDPOINT(req) (0x02 | (req) << 8)
59#define FROM_ENDPOINT(req) (0x82 | (req) << 8)
60
61/*
62 * Setup requests
63 */
64
65#define GET_STATUS 0x00
66#define CLEAR_FEATURE 0x01
67#define SET_FEATURE 0x03
68#define SET_ADDRESS 0x05
69#define GET_DESCRIPTOR 0x06
70#define SET_DESCRIPTOR 0x07
71#define GET_CONFIGURATION 0x08
72#define SET_CONFIGURATION 0x09
73#define GET_INTERFACE 0x0a
74#define SET_INTERFACE 0x0b
75#define SYNCH_FRAME 0x0c
76
77
78/*
79 * Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,
80 * uint8_t. Hence the cast.
81 */
82
83#define LE(x) ((uint16_t) (x) & 0xff), ((uint16_t) (x) >> 8)
84
85#define LO(x) (((uint8_t *) &(x))[0])
86#define HI(x) (((uint8_t *) &(x))[1])
87
88
89#ifdef LOW_SPEED
90#define EP0_SIZE 8
91#else
92#define EP0_SIZE 64
93#endif
94
95#define EP1_SIZE 64 /* simplify */
96
97
98enum ep_state {
99    EP_IDLE,
100    EP_RX,
101    EP_TX,
102    EP_STALL,
103};
104
105struct ep_descr {
106    enum ep_state state;
107    uint8_t *buf;
108    uint8_t *end;
109    uint8_t size;
110    void (*callback)(void *user);
111    void *user;
112};
113
114struct setup_request {
115    uint8_t bmRequestType;
116    uint8_t bRequest;
117    uint16_t wValue;
118    uint16_t wIndex;
119    uint16_t wLength;
120};
121
122
123extern const uint8_t device_descriptor[];
124extern const uint8_t config_descriptor[];
125extern struct ep_descr eps[];
126
127extern int (*user_setup)(const struct setup_request *setup);
128extern int (*user_get_descriptor)(uint8_t type, uint8_t index,
129    const uint8_t **reply, uint8_t *size);
130extern void (*user_reset)(void);
131
132
133#define usb_left(ep) ((ep)->end-(ep)->buf)
134#define usb_send(ep, buf, size, callback, user) \
135    usb_io(ep, EP_TX, (void *) buf, size, callback, user)
136#define usb_recv(ep, buf, size, callback, user) \
137    usb_io(ep, EP_RX, buf, size, callback, user)
138
139void usb_io(struct ep_descr *ep, enum ep_state state, uint8_t *buf,
140    uint8_t size, void (*callback)(void *user), void *user);
141
142int handle_setup(const struct setup_request *setup);
143int set_addr(uint8_t addr);
144void usb_reset(void);
145void usb_init(void);
146void usb_poll(void);
147
148#endif /* !USB_H */
149

Archive Download this file



interactive