7992c806f21041ff10b9176778b30422a687484e
[fw/altos] / src / kernel / ao_usb.h
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_USB_H_
20 #define _AO_USB_H_
21
22 /*
23  * ao_usb.c
24  */
25
26 /* Put one character to the USB output queue */
27 void
28 ao_usb_putchar(char c);
29
30 /* Get one character from the USB input queue */
31 char
32 ao_usb_getchar(void);
33
34 /* Poll for a charcter on the USB input queue.
35  * returns AO_READ_AGAIN if none are available
36  */
37 int
38 ao_usb_pollchar(void);
39
40 /* Flush the USB output queue */
41 void
42 ao_usb_flush(void);
43
44 #if AO_USB_HAS_IN2
45 void
46 ao_usb_flush2(void);
47
48 void
49 ao_usb_putchar2(char c);
50 #endif
51
52 #if AO_USB_HAS_IN3
53 void
54 ao_usb_flush3(void);
55
56 void
57 ao_usb_putchar3(char c);
58 #endif
59 /* Enable the USB controller */
60
61 void
62 ao_usb_enable(void);
63
64 /* Disable the USB controller */
65 void
66 ao_usb_disable(void);
67
68 /* Initialize the USB system */
69 void
70 ao_usb_init(void);
71
72 extern const __at (0x00aa) uint8_t ao_usb_descriptors [];
73
74 #define AO_USB_SETUP_DIR_MASK   (0x01 << 7)
75 #define AO_USB_SETUP_TYPE_MASK  (0x03 << 5)
76 #define AO_USB_SETUP_RECIP_MASK (0x1f)
77
78 #define AO_USB_DIR_OUT                  0
79 #define AO_USB_DIR_IN                   (1 << 7)
80
81 #define AO_USB_TYPE_STANDARD            0
82 #define AO_USB_TYPE_CLASS               (1 << 5)
83 #define AO_USB_TYPE_VENDOR              (2 << 5)
84 #define AO_USB_TYPE_RESERVED            (3 << 5)
85
86 #define AO_USB_RECIP_DEVICE             0
87 #define AO_USB_RECIP_INTERFACE          1
88 #define AO_USB_RECIP_ENDPOINT           2
89 #define AO_USB_RECIP_OTHER              3
90
91 /* standard requests */
92 #define AO_USB_REQ_GET_STATUS           0x00
93 #define AO_USB_REQ_CLEAR_FEATURE        0x01
94 #define AO_USB_REQ_SET_FEATURE          0x03
95 #define AO_USB_REQ_SET_ADDRESS          0x05
96 #define AO_USB_REQ_GET_DESCRIPTOR       0x06
97 #define AO_USB_REQ_SET_DESCRIPTOR       0x07
98 #define AO_USB_REQ_GET_CONFIGURATION    0x08
99 #define AO_USB_REQ_SET_CONFIGURATION    0x09
100 #define AO_USB_REQ_GET_INTERFACE        0x0A
101 #define AO_USB_REQ_SET_INTERFACE        0x0B
102 #define AO_USB_REQ_SYNCH_FRAME          0x0C
103
104 #define AO_USB_DESC_DEVICE              1
105 #define AO_USB_DESC_CONFIGURATION       2
106 #define AO_USB_DESC_STRING              3
107 #define AO_USB_DESC_INTERFACE           4
108 #define AO_USB_DESC_ENDPOINT            5
109 #define AO_USB_DESC_DEVICE_QUALIFIER    6
110 #define AO_USB_DESC_OTHER_SPEED         7
111 #define AO_USB_DESC_INTERFACE_POWER     8
112
113 #define AO_USB_GET_DESC_TYPE(x)         (((x)>>8)&0xFF)
114 #define AO_USB_GET_DESC_INDEX(x)        ((x)&0xFF)
115
116 #define AO_USB_CONTROL_EP       0
117 #define AO_USB_CONTROL_SIZE     32
118
119 #define AO_USB_INT_EP           1
120 #define AO_USB_INT_SIZE         8
121
122 #ifndef AO_USB_OUT_EP
123 #define AO_USB_OUT_EP           4
124 #define AO_USB_IN_EP            5
125 #define AO_USB_IN2_EP           6
126 #define AO_USB_IN3_EP           7
127 #endif
128
129 #ifndef AO_USB_HAS_OUT
130 #define AO_USB_HAS_OUT  1
131 #endif
132
133 #ifndef AO_USB_HAS_IN
134 #define AO_USB_HAS_IN   1
135 #endif
136
137 #ifndef AO_USB_HAS_INT
138 #define AO_USB_HAS_INT  1
139 #endif
140
141 #ifndef AO_USB_HAS_IN2
142 #define AO_USB_HAS_IN2  0
143 #endif
144
145 #ifndef AO_USB_HAS_IN3
146 #define AO_USB_HAS_IN3  0
147 #endif
148
149 /*
150  * USB bulk packets can only come in 8, 16, 32 and 64
151  * byte sizes, so we'll use 64 for everything
152  */
153 #define AO_USB_IN_SIZE          64
154 #define AO_USB_OUT_SIZE         64
155
156 #define AO_USB_EP0_IDLE         0
157 #define AO_USB_EP0_DATA_IN      1
158 #define AO_USB_EP0_DATA_OUT     2
159 #define AO_USB_EP0_STALL        3
160
161 #define LE_WORD(x)    ((x)&0xFF),((uint8_t) (((uint16_t) (x))>>8))
162
163 /* CDC definitions */
164 #define AO_USB_CS_INTERFACE             0x24
165 #define AO_USB_CS_ENDPOINT              0x25
166
167 #define AO_USB_SET_LINE_CODING          0x20
168 #define AO_USB_GET_LINE_CODING          0x21
169 #define AO_USB_SET_CONTROL_LINE_STATE   0x22
170
171 /* Data structure for GET_LINE_CODING / SET_LINE_CODING class requests */
172 struct ao_usb_line_coding {
173         uint32_t        rate;
174         uint8_t         char_format;
175         uint8_t         parity;
176         uint8_t         data_bits;
177 } ;
178
179 extern struct ao_usb_line_coding ao_usb_line_coding;
180
181 extern uint8_t ao_usb_running;
182
183 #endif /* _AO_USB_H_ */