added missing files
[clinton/Smoothieware.git] / src / libs / USBDevice / USBSERIAL / USBCDC.cpp
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 */
18
19 #include "stdint.h"
20 #include "USBCDC.h"
21 #include "USBBusInterface.h"
22
23 static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
24
25 #define DEFAULT_CONFIGURATION (1)
26
27 #define CDC_SET_LINE_CODING 0x20
28 #define CDC_GET_LINE_CODING 0x21
29 #define CDC_SET_CONTROL_LINE_STATE 0x22
30
31 #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK
32
33 USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
34 USBDevice::connect();
35 }
36
37 bool USBCDC::USBCallback_request(void) {
38 /* Called in ISR context */
39
40 bool success = false;
41 CONTROL_TRANSFER * transfer = getTransferPtr();
42
43 /* Process class-specific requests */
44
45 if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
46 switch (transfer->setup.bRequest) {
47 case CDC_GET_LINE_CODING:
48 transfer->remaining = 7;
49 transfer->ptr = cdc_line_coding;
50 transfer->direction = DEVICE_TO_HOST;
51 success = true;
52 break;
53 case CDC_SET_LINE_CODING:
54 transfer->remaining = 7;
55 success = true;
56 break;
57 case CDC_SET_CONTROL_LINE_STATE:
58 success = true;
59 break;
60 default:
61 break;
62 }
63 }
64
65 return success;
66 }
67
68
69 // Called in ISR context
70 // Set configuration. Return false if the
71 // configuration is not supported.
72 bool USBCDC::USBCallback_setConfiguration(uint8_t configuration) {
73 if (configuration != DEFAULT_CONFIGURATION) {
74 return false;
75 }
76
77 // Configure endpoints > 0
78 addEndpoint(EPINT_IN, MAX_PACKET_SIZE_EPINT);
79 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
80 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
81
82 // We activate the endpoint to be able to recceive data
83 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
84 return true;
85 }
86
87 bool USBCDC::send(uint8_t * buffer, uint16_t size) {
88 return USBDevice::write(EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE);
89 }
90
91 bool USBCDC::readEP(uint8_t * buffer, uint16_t * size) {
92 if (!USBDevice::readEP(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
93 return false;
94 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
95 return false;
96 return true;
97 }
98
99 bool USBCDC::readEP_NB(uint8_t * buffer, uint16_t * size) {
100 if (!USBDevice::readEP_NB(EPBULK_OUT, buffer, size, MAX_CDC_REPORT_SIZE))
101 return false;
102 if (!readStart(EPBULK_OUT, MAX_CDC_REPORT_SIZE))
103 return false;
104 return true;
105 }
106
107
108 uint8_t * USBCDC::deviceDesc() {
109 static uint8_t deviceDescriptor[] = {
110 18, // bLength
111 1, // bDescriptorType
112 0x10, 0x01, // bcdUSB
113 2, // bDeviceClass
114 0, // bDeviceSubClass
115 0, // bDeviceProtocol
116 MAX_PACKET_SIZE_EP0, // bMaxPacketSize0
117 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
118 LSB(PRODUCT_ID), MSB(PRODUCT_ID),// idProduct
119 0x00, 0x01, // bcdDevice
120 1, // iManufacturer
121 2, // iProduct
122 3, // iSerialNumber
123 1 // bNumConfigurations
124 };
125 return deviceDescriptor;
126 }
127
128 uint8_t * USBCDC::stringIinterfaceDesc() {
129 static uint8_t stringIinterfaceDescriptor[] = {
130 0x08,
131 STRING_DESCRIPTOR,
132 'C',0,'D',0,'C',0,
133 };
134 return stringIinterfaceDescriptor;
135 }
136
137 uint8_t * USBCDC::stringIproductDesc() {
138 static uint8_t stringIproductDescriptor[] = {
139 0x16,
140 STRING_DESCRIPTOR,
141 'C',0,'D',0,'C',0,' ',0,'D',0,'E',0,'V',0,'I',0,'C',0,'E',0
142 };
143 return stringIproductDescriptor;
144 }
145
146
147 #define CONFIG1_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
148
149 uint8_t * USBCDC::configurationDesc() {
150 static uint8_t configDescriptor[] = {
151 9, // bLength;
152 2, // bDescriptorType;
153 LSB(CONFIG1_DESC_SIZE), // wTotalLength
154 MSB(CONFIG1_DESC_SIZE),
155 2, // bNumInterfaces
156 1, // bConfigurationValue
157 0, // iConfiguration
158 0x80, // bmAttributes
159 50, // bMaxPower
160
161 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
162 9, // bLength
163 4, // bDescriptorType
164 0, // bInterfaceNumber
165 0, // bAlternateSetting
166 1, // bNumEndpoints
167 0x02, // bInterfaceClass
168 0x02, // bInterfaceSubClass
169 0x01, // bInterfaceProtocol
170 0, // iInterface
171
172 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
173 5, // bFunctionLength
174 0x24, // bDescriptorType
175 0x00, // bDescriptorSubtype
176 0x10, 0x01, // bcdCDC
177
178 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
179 5, // bFunctionLength
180 0x24, // bDescriptorType
181 0x01, // bDescriptorSubtype
182 0x03, // bmCapabilities
183 1, // bDataInterface
184
185 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
186 4, // bFunctionLength
187 0x24, // bDescriptorType
188 0x02, // bDescriptorSubtype
189 0x06, // bmCapabilities
190
191 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
192 5, // bFunctionLength
193 0x24, // bDescriptorType
194 0x06, // bDescriptorSubtype
195 0, // bMasterInterface
196 1, // bSlaveInterface0
197
198 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
199 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
200 ENDPOINT_DESCRIPTOR, // bDescriptorType
201 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
202 E_INTERRUPT, // bmAttributes (0x03=intr)
203 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
204 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
205 16, // bInterval
206
207
208
209
210 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
211 9, // bLength
212 4, // bDescriptorType
213 1, // bInterfaceNumber
214 0, // bAlternateSetting
215 2, // bNumEndpoints
216 0x0A, // bInterfaceClass
217 0x00, // bInterfaceSubClass
218 0x00, // bInterfaceProtocol
219 0, // iInterface
220
221 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
222 7, // bLength
223 5, // bDescriptorType
224 PHY_TO_DESC(EPBULK_IN), // bEndpointAddress
225 0x02, // bmAttributes (0x02=bulk)
226 LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)
227 MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)
228 0, // bInterval
229
230 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
231 7, // bLength
232 5, // bDescriptorType
233 PHY_TO_DESC(EPBULK_OUT),// bEndpointAddress
234 0x02, // bmAttributes (0x02=bulk)
235 LSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (LSB)
236 MSB(MAX_PACKET_SIZE_EPBULK), // wMaxPacketSize (MSB)
237 0 // bInterval
238 };
239 return configDescriptor;
240 }