1 |
5 |
ultra_embe |
//-----------------------------------------------------------------
|
2 |
|
|
// USB CDC Device SW
|
3 |
|
|
// V0.1
|
4 |
|
|
// Ultra-Embedded.com
|
5 |
|
|
// Copyright 2014
|
6 |
|
|
//
|
7 |
|
|
// Email: admin@ultra-embedded.com
|
8 |
|
|
//
|
9 |
|
|
// License: GPL
|
10 |
|
|
// If you would like a version with a more permissive license for use in
|
11 |
|
|
// closed source commercial applications please contact me for details.
|
12 |
|
|
//-----------------------------------------------------------------
|
13 |
|
|
//
|
14 |
|
|
// This file is part of USB CDC Device SW.
|
15 |
|
|
//
|
16 |
|
|
// USB CDC Device SW is free software; you can redistribute it and/or modify
|
17 |
|
|
// it under the terms of the GNU General Public License as published by
|
18 |
|
|
// the Free Software Foundation; either version 2 of the License, or
|
19 |
|
|
// (at your option) any later version.
|
20 |
|
|
//
|
21 |
|
|
// USB CDC Device SW is distributed in the hope that it will be useful,
|
22 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
23 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
24 |
|
|
// GNU General Public License for more details.
|
25 |
|
|
//
|
26 |
|
|
// You should have received a copy of the GNU General Public License
|
27 |
|
|
// along with USB CDC Device SW; if not, write to the Free Software
|
28 |
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
29 |
|
|
//-----------------------------------------------------------------
|
30 |
|
|
#include <string.h>
|
31 |
|
|
#include <stdlib.h>
|
32 |
|
|
#include "usbf_defs.h"
|
33 |
|
|
#include "usb_desc.h"
|
34 |
|
|
#include "usb_log.h"
|
35 |
|
|
|
36 |
|
|
//-----------------------------------------------------------------
|
37 |
|
|
// PID/VID:
|
38 |
|
|
//-----------------------------------------------------------------
|
39 |
|
|
#ifndef USB_DEV_VID
|
40 |
|
|
#define USB_DEV_VID 0x9234
|
41 |
|
|
#endif
|
42 |
|
|
#ifndef USB_DEV_PID
|
43 |
|
|
#define USB_DEV_PID 0x5678
|
44 |
|
|
#endif
|
45 |
|
|
#define USB_DEV_VER 0x0101
|
46 |
|
|
|
47 |
|
|
//-----------------------------------------------------------------
|
48 |
|
|
// Defines:
|
49 |
|
|
//-----------------------------------------------------------------
|
50 |
|
|
|
51 |
|
|
// Descriptor defs
|
52 |
|
|
#define SIZE_OF_DEVICE_DESCR 18
|
53 |
|
|
|
54 |
|
|
#ifdef USB_SPEED_HS
|
55 |
|
|
#define EP0_MAX_PACKET_SIZE 64
|
56 |
|
|
#else
|
57 |
|
|
#define EP0_MAX_PACKET_SIZE 8
|
58 |
|
|
#endif
|
59 |
|
|
|
60 |
|
|
// Configuration descriptor
|
61 |
|
|
#define NB_INTERFACE 2
|
62 |
|
|
#define CONF_NB 1
|
63 |
|
|
#define CONF_INDEX 0
|
64 |
|
|
#define CONF_ATTRIBUTES 0x80 // Bit7 bus-powered Bit6 self-powered
|
65 |
|
|
#define MAX_POWER 50 // Bus current = 100 mA
|
66 |
|
|
|
67 |
|
|
// Interface 0 descriptor
|
68 |
|
|
#define INTERFACE0_ID 0
|
69 |
|
|
#define ALTERNATE0 0
|
70 |
|
|
#define NB_ENDPOINT0 1
|
71 |
|
|
#define INTERFACE0_CLASS 0x02
|
72 |
|
|
#define INTERFACE0_SUB_CLASS 0x02
|
73 |
|
|
#define INTERFACE0_PROTOCOL 0x01
|
74 |
|
|
#define INTERFACE0_INDEX 0
|
75 |
|
|
|
76 |
|
|
// Endpoint 3 descriptor (INTR-IN)
|
77 |
|
|
#define ENDPOINT_ID_3 0x83
|
78 |
|
|
#define EP_ATTRIBUTES_3 0x03
|
79 |
|
|
#ifdef USB_SPEED_HS
|
80 |
|
|
#define EP_SIZE_3 64 // TODO: Should be 512 for HS???
|
81 |
|
|
#else
|
82 |
|
|
#define EP_SIZE_3 64
|
83 |
|
|
#endif
|
84 |
|
|
#define EP_INTERVAL_3 2
|
85 |
|
|
|
86 |
|
|
// Interface 1 descriptor
|
87 |
|
|
#define INTERFACE1_ID 1
|
88 |
|
|
#define ALTERNATE1 0
|
89 |
|
|
#define NB_ENDPOINT1 2
|
90 |
|
|
#define INTERFACE1_CLASS 0x0A
|
91 |
|
|
#define INTERFACE1_SUB_CLASS 0
|
92 |
|
|
#define INTERFACE1_PROTOCOL 0
|
93 |
|
|
#define INTERFACE1_INDEX 0
|
94 |
|
|
|
95 |
|
|
// Endpoint 1 descriptor
|
96 |
|
|
#define ENDPOINT_ID_1 0x01
|
97 |
|
|
#define EP_ATTRIBUTES_1 0x02
|
98 |
|
|
#ifdef USB_SPEED_HS
|
99 |
|
|
#define EP_SIZE_1 512
|
100 |
|
|
#else
|
101 |
|
|
#define EP_SIZE_1 64
|
102 |
|
|
#endif
|
103 |
|
|
#define EP_INTERVAL_1 0x00
|
104 |
|
|
|
105 |
|
|
// Endpoint 2 descriptor
|
106 |
|
|
#define ENDPOINT_ID_2 0x82
|
107 |
|
|
#define EP_ATTRIBUTES_2 0x02
|
108 |
|
|
#ifdef USB_SPEED_HS
|
109 |
|
|
#define EP_SIZE_2 512
|
110 |
|
|
#else
|
111 |
|
|
#define EP_SIZE_2 64
|
112 |
|
|
#endif
|
113 |
|
|
#define EP_INTERVAL_2 0x00
|
114 |
|
|
|
115 |
|
|
// String Descriptors
|
116 |
|
|
#define UNICODE_LANGUAGE_STR_ID 0
|
117 |
|
|
#define MANUFACTURER_STR_ID 1
|
118 |
|
|
#define PRODUCT_NAME_STR_ID 2
|
119 |
|
|
#define SERIAL_NUM_STR_ID 3
|
120 |
|
|
#define UNICODE_ENGLISH 0x0409
|
121 |
|
|
|
122 |
|
|
//-----------------------------------------------------------------
|
123 |
|
|
// Descriptors:
|
124 |
|
|
//-----------------------------------------------------------------
|
125 |
|
|
static const unsigned char _device_desc[18] =
|
126 |
|
|
{
|
127 |
|
|
SIZE_OF_DEVICE_DESCR, // Descriptor size (18)
|
128 |
|
|
DESC_DEVICE, // Descriptor type
|
129 |
|
|
LO_BYTE(0x0200), // bcdUSB = 02.00 (BCD word)
|
130 |
|
|
HI_BYTE(0x0200),
|
131 |
|
|
DEV_CLASS_COMMS, // Device class
|
132 |
|
|
0x00, // Device subclass
|
133 |
|
|
0x00, // Device protocol
|
134 |
|
|
EP0_MAX_PACKET_SIZE, // Max packet size for EP0
|
135 |
|
|
LO_BYTE(USB_DEV_VID),
|
136 |
|
|
HI_BYTE(USB_DEV_VID),
|
137 |
|
|
LO_BYTE(USB_DEV_PID),
|
138 |
|
|
HI_BYTE(USB_DEV_PID),
|
139 |
|
|
LO_BYTE(USB_DEV_VER),
|
140 |
|
|
HI_BYTE(USB_DEV_VER),
|
141 |
|
|
0, // MANUFACTURER_STR_ID, // manufacturer string index
|
142 |
|
|
0, // PRODUCT_NAME_STR_ID, // product string index
|
143 |
|
|
0, // SERIAL_NUM_STR_ID, // serial number string index
|
144 |
|
|
1 // number of configurations
|
145 |
|
|
};
|
146 |
|
|
|
147 |
|
|
static const unsigned char _config_desc[67] =
|
148 |
|
|
{
|
149 |
|
|
/* Config. descriptor (short) */
|
150 |
|
|
9, DESC_CONFIGURATION,
|
151 |
|
|
LO_BYTE( sizeof(_config_desc) ), HI_BYTE( sizeof(_config_desc) ),
|
152 |
|
|
NB_INTERFACE, CONF_NB, CONF_INDEX, CONF_ATTRIBUTES, MAX_POWER,
|
153 |
|
|
|
154 |
|
|
/* Interface #0 descriptor */
|
155 |
|
|
9, DESC_INTERFACE, INTERFACE0_ID, ALTERNATE0, NB_ENDPOINT0,
|
156 |
|
|
INTERFACE0_CLASS, INTERFACE0_SUB_CLASS, INTERFACE0_PROTOCOL, INTERFACE0_INDEX,
|
157 |
|
|
|
158 |
|
|
/* CDC-specific Functional Descriptors (type = 0x24) - Ref. USBCDC Spec. $5.2.3 */
|
159 |
|
|
0x05, 0x24, 0x00, 0x10, 0x01, // CDC-specific header
|
160 |
|
|
0x05, 0x24, 0x01, 0x03, 0x01, // Call Management descriptor
|
161 |
|
|
0x04, 0x24, 0x02, 0x06, // Abstract Control Management descriptor
|
162 |
|
|
0x05, 0x24, 0x06, 0x00, 0x01, // Union functional descriptor
|
163 |
|
|
|
164 |
|
|
/* Endpoint descriptor #3 */
|
165 |
|
|
7, DESC_ENDPOINT, ENDPOINT_ID_3, EP_ATTRIBUTES_3,
|
166 |
|
|
LO_BYTE(EP_SIZE_3), HI_BYTE(EP_SIZE_3), EP_INTERVAL_3,
|
167 |
|
|
|
168 |
|
|
/* Interface #1 descriptor */
|
169 |
|
|
9, DESC_INTERFACE, INTERFACE1_ID, ALTERNATE1, NB_ENDPOINT1,
|
170 |
|
|
INTERFACE1_CLASS, INTERFACE1_SUB_CLASS, INTERFACE1_PROTOCOL, INTERFACE1_INDEX,
|
171 |
|
|
|
172 |
|
|
/* Endpoint descriptor #1 */
|
173 |
|
|
7, DESC_ENDPOINT, ENDPOINT_ID_1, EP_ATTRIBUTES_1,
|
174 |
|
|
LO_BYTE(EP_SIZE_1), HI_BYTE(EP_SIZE_1), EP_INTERVAL_1,
|
175 |
|
|
|
176 |
|
|
/* Endpoint descriptor #2 */
|
177 |
|
|
7, DESC_ENDPOINT, ENDPOINT_ID_2, EP_ATTRIBUTES_2,
|
178 |
|
|
LO_BYTE(EP_SIZE_2), HI_BYTE(EP_SIZE_2), EP_INTERVAL_2
|
179 |
|
|
};
|
180 |
|
|
|
181 |
|
|
static const unsigned char _string_desc_lang[] =
|
182 |
|
|
{
|
183 |
|
|
(4), // Descriptor size
|
184 |
|
|
DESC_STRING,
|
185 |
|
|
LO_BYTE( UNICODE_ENGLISH ),
|
186 |
|
|
HI_BYTE( UNICODE_ENGLISH ),
|
187 |
|
|
};
|
188 |
|
|
|
189 |
|
|
static const unsigned char _string_desc_man[] =
|
190 |
|
|
{
|
191 |
|
|
(2 + 28),
|
192 |
|
|
DESC_STRING,
|
193 |
|
|
'U',0,'L',0,'T',0,'R',0,'A',0,'-',0,'E',0,'M',0,'B',0,'E',0,'D',0,'D',0,'E',0,'D',0
|
194 |
|
|
};
|
195 |
|
|
|
196 |
|
|
static const unsigned char _string_desc_prod[] =
|
197 |
|
|
{
|
198 |
|
|
(2 + 28),
|
199 |
|
|
DESC_STRING,
|
200 |
|
|
'U',0,'S',0,'B',0,' ',0,'D',0,'E',0,'M',0,'O',0,' ',0,' ',0,' ',0,' ',0,' ',0,' ',0
|
201 |
|
|
};
|
202 |
|
|
|
203 |
|
|
static const unsigned char _string_desc_serial[] =
|
204 |
|
|
{
|
205 |
|
|
(2 + 12),
|
206 |
|
|
DESC_STRING,
|
207 |
|
|
'0',0,'0',0,'0',0,'0',0,'0',0,'0',0
|
208 |
|
|
};
|
209 |
|
|
|
210 |
|
|
//-----------------------------------------------------------------
|
211 |
|
|
// usb_get_descriptor:
|
212 |
|
|
//-----------------------------------------------------------------
|
213 |
|
|
unsigned char *usb_get_descriptor( unsigned char bDescriptorType, unsigned char bDescriptorIndex, unsigned short wLength, unsigned char *pSize )
|
214 |
|
|
{
|
215 |
|
|
if ( bDescriptorType == DESC_DEVICE )
|
216 |
|
|
{
|
217 |
|
|
*pSize = MIN( wLength, SIZE_OF_DEVICE_DESCR );
|
218 |
|
|
log_printf(USBLOG_DESC, "USB: Get device descriptor %d\n", *pSize);
|
219 |
|
|
|
220 |
|
|
return (unsigned char *)_device_desc;
|
221 |
|
|
}
|
222 |
|
|
else if ( bDescriptorType == DESC_CONFIGURATION )
|
223 |
|
|
{
|
224 |
|
|
*pSize = MIN( sizeof(_config_desc), wLength );
|
225 |
|
|
log_printf(USBLOG_DESC, "USB: Get conf descriptor %d\n", *pSize);
|
226 |
|
|
|
227 |
|
|
return (unsigned char *)_config_desc;
|
228 |
|
|
}
|
229 |
|
|
else if ( bDescriptorType == DESC_STRING )
|
230 |
|
|
{
|
231 |
|
|
log_printf(USBLOG_DESC, "USB: Get string descriptor %x\n", bDescriptorIndex);
|
232 |
|
|
|
233 |
|
|
switch( bDescriptorIndex )
|
234 |
|
|
{
|
235 |
|
|
case UNICODE_LANGUAGE_STR_ID:
|
236 |
|
|
*pSize = MIN( sizeof(_string_desc_lang), wLength );
|
237 |
|
|
return (unsigned char *)_string_desc_lang;
|
238 |
|
|
|
239 |
|
|
case MANUFACTURER_STR_ID:
|
240 |
|
|
*pSize = MIN( sizeof(_string_desc_man), wLength );
|
241 |
|
|
return (unsigned char *)_string_desc_man;
|
242 |
|
|
|
243 |
|
|
case PRODUCT_NAME_STR_ID:
|
244 |
|
|
*pSize = MIN( sizeof(_string_desc_prod), wLength );
|
245 |
|
|
return (unsigned char *)_string_desc_prod;
|
246 |
|
|
|
247 |
|
|
case SERIAL_NUM_STR_ID:
|
248 |
|
|
*pSize = MIN( sizeof(_string_desc_serial), wLength );
|
249 |
|
|
return (unsigned char *)_string_desc_serial;
|
250 |
|
|
|
251 |
|
|
default:
|
252 |
|
|
log_printf(USBLOG_DESC_WARN, "USB: Unknown descriptor index %x, STALL\n", bDescriptorIndex);
|
253 |
|
|
return NULL;
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
else
|
257 |
|
|
{
|
258 |
|
|
log_printf(USBLOG_DESC_WARN, "USB: Unknown descriptor type %x, STALL\n", bDescriptorType);
|
259 |
|
|
return NULL;
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
//-----------------------------------------------------------------
|
263 |
|
|
// usb_is_bus_powered:
|
264 |
|
|
//-----------------------------------------------------------------
|
265 |
|
|
int usb_is_bus_powered(void)
|
266 |
|
|
{
|
267 |
|
|
return (CONF_ATTRIBUTES & 0x80) ? 1 : 0;
|
268 |
|
|
}
|