Run wspurify script on /trunk/ and /branches/ C source files, to remove any trailing...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / CDCClassDevice.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #define __INCLUDE_FROM_CDC_DRIVER
37 #define __INCLUDE_FROM_CDC_DEVICE_C
38 #include "CDCClassDevice.h"
39
40 void CDC_Device_ProcessControlRequest(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
41 {
42 if (!(Endpoint_IsSETUPReceived()))
43 return;
44
45 if (USB_ControlRequest.wIndex != CDCInterfaceInfo->Config.ControlInterfaceNumber)
46 return;
47
48 switch (USB_ControlRequest.bRequest)
49 {
50 case CDC_REQ_GetLineEncoding:
51 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
52 {
53 Endpoint_ClearSETUP();
54
55 while (!(Endpoint_IsINReady()));
56
57 Endpoint_Write_32_LE(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
58 Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.CharFormat);
59 Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.ParityType);
60 Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.DataBits);
61
62 Endpoint_ClearIN();
63 Endpoint_ClearStatusStage();
64 }
65
66 break;
67 case CDC_REQ_SetLineEncoding:
68 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
69 {
70 Endpoint_ClearSETUP();
71
72 while (!(Endpoint_IsOUTReceived()));
73
74 CDCInterfaceInfo->State.LineEncoding.BaudRateBPS = Endpoint_Read_32_LE();
75 CDCInterfaceInfo->State.LineEncoding.CharFormat = Endpoint_Read_8();
76 CDCInterfaceInfo->State.LineEncoding.ParityType = Endpoint_Read_8();
77 CDCInterfaceInfo->State.LineEncoding.DataBits = Endpoint_Read_8();
78
79 Endpoint_ClearOUT();
80 Endpoint_ClearStatusStage();
81
82 EVENT_CDC_Device_LineEncodingChanged(CDCInterfaceInfo);
83 }
84
85 break;
86 case CDC_REQ_SetControlLineState:
87 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
88 {
89 Endpoint_ClearSETUP();
90 Endpoint_ClearStatusStage();
91
92 CDCInterfaceInfo->State.ControlLineStates.HostToDevice = USB_ControlRequest.wValue;
93
94 EVENT_CDC_Device_ControLineStateChanged(CDCInterfaceInfo);
95 }
96
97 break;
98 case CDC_REQ_SendBreak:
99 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
100 {
101 Endpoint_ClearSETUP();
102 Endpoint_ClearStatusStage();
103
104 EVENT_CDC_Device_BreakSent(CDCInterfaceInfo, (uint8_t)USB_ControlRequest.wValue);
105 }
106
107 break;
108 }
109 }
110
111 bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
112 {
113 memset(&CDCInterfaceInfo->State, 0x00, sizeof(CDCInterfaceInfo->State));
114
115 for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
116 {
117 uint16_t Size;
118 uint8_t Type;
119 uint8_t Direction;
120 bool DoubleBanked;
121
122 if (EndpointNum == CDCInterfaceInfo->Config.DataINEndpointNumber)
123 {
124 Size = CDCInterfaceInfo->Config.DataINEndpointSize;
125 Direction = ENDPOINT_DIR_IN;
126 Type = EP_TYPE_BULK;
127 DoubleBanked = CDCInterfaceInfo->Config.DataINEndpointDoubleBank;
128 }
129 else if (EndpointNum == CDCInterfaceInfo->Config.DataOUTEndpointNumber)
130 {
131 Size = CDCInterfaceInfo->Config.DataOUTEndpointSize;
132 Direction = ENDPOINT_DIR_OUT;
133 Type = EP_TYPE_BULK;
134 DoubleBanked = CDCInterfaceInfo->Config.DataOUTEndpointDoubleBank;
135 }
136 else if (EndpointNum == CDCInterfaceInfo->Config.NotificationEndpointNumber)
137 {
138 Size = CDCInterfaceInfo->Config.NotificationEndpointSize;
139 Direction = ENDPOINT_DIR_IN;
140 Type = EP_TYPE_INTERRUPT;
141 DoubleBanked = CDCInterfaceInfo->Config.NotificationEndpointDoubleBank;
142 }
143 else
144 {
145 continue;
146 }
147
148 if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size,
149 DoubleBanked ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
150 {
151 return false;
152 }
153 }
154
155 return true;
156 }
157
158 void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
159 {
160 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
161 return;
162
163 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
164 CDC_Device_Flush(CDCInterfaceInfo);
165 #endif
166 }
167
168 uint8_t CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
169 const char* const String)
170 {
171 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
172 return ENDPOINT_RWSTREAM_DeviceDisconnected;
173
174 Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);
175 return Endpoint_Write_Stream_LE(String, strlen(String), NULL);
176 }
177
178 uint8_t CDC_Device_SendData(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
179 const char* const Buffer,
180 const uint16_t Length)
181 {
182 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
183 return ENDPOINT_RWSTREAM_DeviceDisconnected;
184
185 Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);
186 return Endpoint_Write_Stream_LE(Buffer, Length, NULL);
187 }
188
189 uint8_t CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
190 const uint8_t Data)
191 {
192 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
193 return ENDPOINT_RWSTREAM_DeviceDisconnected;
194
195 Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);
196
197 if (!(Endpoint_IsReadWriteAllowed()))
198 {
199 Endpoint_ClearIN();
200
201 uint8_t ErrorCode;
202
203 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
204 return ErrorCode;
205 }
206
207 Endpoint_Write_8(Data);
208 return ENDPOINT_READYWAIT_NoError;
209 }
210
211 uint8_t CDC_Device_Flush(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
212 {
213 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
214 return ENDPOINT_RWSTREAM_DeviceDisconnected;
215
216 uint8_t ErrorCode;
217
218 Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);
219
220 if (!(Endpoint_BytesInEndpoint()))
221 return ENDPOINT_READYWAIT_NoError;
222
223 bool BankFull = !(Endpoint_IsReadWriteAllowed());
224
225 Endpoint_ClearIN();
226
227 if (BankFull)
228 {
229 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
230 return ErrorCode;
231
232 Endpoint_ClearIN();
233 }
234
235 return ENDPOINT_READYWAIT_NoError;
236 }
237
238 uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
239 {
240 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
241 return 0;
242
243 Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber);
244
245 if (Endpoint_IsOUTReceived())
246 {
247 if (!(Endpoint_BytesInEndpoint()))
248 {
249 Endpoint_ClearOUT();
250 return 0;
251 }
252 else
253 {
254 return Endpoint_BytesInEndpoint();
255 }
256 }
257 else
258 {
259 return 0;
260 }
261 }
262
263 int16_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
264 {
265 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
266 return -1;
267
268 int16_t ReceivedByte = -1;
269
270 Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber);
271
272 if (Endpoint_IsOUTReceived())
273 {
274 if (Endpoint_BytesInEndpoint())
275 ReceivedByte = Endpoint_Read_8();
276
277 if (!(Endpoint_BytesInEndpoint()))
278 Endpoint_ClearOUT();
279 }
280
281 return ReceivedByte;
282 }
283
284 void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
285 {
286 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
287 return;
288
289 Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.NotificationEndpointNumber);
290
291 USB_Request_Header_t Notification = (USB_Request_Header_t)
292 {
293 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
294 .bRequest = CDC_NOTIF_SerialState,
295 .wValue = CPU_TO_LE16(0),
296 .wIndex = CPU_TO_LE16(0),
297 .wLength = CPU_TO_LE16(sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost)),
298 };
299
300 Endpoint_Write_Stream_LE(&Notification, sizeof(USB_Request_Header_t), NULL);
301 Endpoint_Write_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost,
302 sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost),
303 NULL);
304 Endpoint_ClearIN();
305 }
306
307 #if defined(FDEV_SETUP_STREAM)
308 void CDC_Device_CreateStream(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
309 FILE* const Stream)
310 {
311 *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Device_putchar, CDC_Device_getchar, _FDEV_SETUP_RW);
312 fdev_set_udata(Stream, CDCInterfaceInfo);
313 }
314
315 void CDC_Device_CreateBlockingStream(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
316 FILE* const Stream)
317 {
318 *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Device_putchar, CDC_Device_getchar_Blocking, _FDEV_SETUP_RW);
319 fdev_set_udata(Stream, CDCInterfaceInfo);
320 }
321
322 static int CDC_Device_putchar(char c,
323 FILE* Stream)
324 {
325 return CDC_Device_SendByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream), c) ? _FDEV_ERR : 0;
326 }
327
328 static int CDC_Device_getchar(FILE* Stream)
329 {
330 int16_t ReceivedByte = CDC_Device_ReceiveByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream));
331
332 if (ReceivedByte < 0)
333 return _FDEV_EOF;
334
335 return ReceivedByte;
336 }
337
338 static int CDC_Device_getchar_Blocking(FILE* Stream)
339 {
340 int16_t ReceivedByte;
341
342 while ((ReceivedByte = CDC_Device_ReceiveByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream))) < 0)
343 {
344 if (USB_DeviceState == DEVICE_STATE_Unattached)
345 return _FDEV_EOF;
346
347 CDC_Device_USBTask((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream));
348 USB_USBTask();
349 }
350
351 return ReceivedByte;
352 }
353 #endif
354
355 void CDC_Device_Event_Stub(void)
356 {
357
358 }
359
360 #endif
361