Be doubly-certain that the incomming CDC class driver's endpoint/pipe is flushed...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / CDC.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
33
34 #define INCLUDE_FROM_CDC_CLASS_HOST_C
35 #include "CDC.h"
36
37 uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo, uint16_t ConfigDescriptorSize,
38 void* ConfigDescriptorData)
39 {
40 uint8_t FoundEndpoints = 0;
41
42 memset(&CDCInterfaceInfo->State, 0x00, sizeof(CDCInterfaceInfo->State));
43
44 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
45 return CDC_ENUMERROR_InvalidConfigDescriptor;
46
47 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
48 DComp_CDC_Host_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
49 {
50 return CDC_ENUMERROR_NoCDCInterfaceFound;
51 }
52
53 CDCInterfaceInfo->State.ControlInterfaceNumber = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).InterfaceNumber;
54
55 while (FoundEndpoints != (CDC_FOUND_NOTIFICATION_IN | CDC_FOUND_DATAPIPE_IN | CDC_FOUND_DATAPIPE_OUT))
56 {
57 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
58 DComp_CDC_Host_NextCDCInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
59 {
60 if (FoundEndpoints & CDC_FOUND_NOTIFICATION_IN)
61 {
62 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
63 DComp_CDC_Host_NextCDCDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)
64 {
65 return CDC_ENUMERROR_NoCDCInterfaceFound;
66 }
67 }
68 else
69 {
70 FoundEndpoints = 0;
71
72 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipeNumber);
73 Pipe_DisablePipe();
74 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
75 Pipe_DisablePipe();
76 Pipe_SelectPipe(CDCInterfaceInfo->Config.NotificationPipeNumber);
77 Pipe_DisablePipe();
78
79 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
80 DComp_CDC_Host_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
81 {
82 return CDC_ENUMERROR_NoCDCInterfaceFound;
83 }
84 }
85
86 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
87 DComp_CDC_Host_NextCDCInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
88 {
89 return CDC_ENUMERROR_EndpointsNotFound;
90 }
91 }
92
93 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
94
95 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
96 {
97 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
98 {
99 Pipe_ConfigurePipe(CDCInterfaceInfo->Config.NotificationPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
100 EndpointData->EndpointAddress, EndpointData->EndpointSize,
101 CDCInterfaceInfo->Config.NotificationPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
102 CDCInterfaceInfo->State.NotificationPipeSize = EndpointData->EndpointSize;
103
104 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
105
106 FoundEndpoints |= CDC_FOUND_NOTIFICATION_IN;
107 }
108 }
109 else
110 {
111 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
112 {
113 Pipe_ConfigurePipe(CDCInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
114 EndpointData->EndpointAddress, EndpointData->EndpointSize,
115 CDCInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
116
117 CDCInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
118
119 FoundEndpoints |= CDC_FOUND_DATAPIPE_IN;
120 }
121 else
122 {
123 Pipe_ConfigurePipe(CDCInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
124 EndpointData->EndpointAddress, EndpointData->EndpointSize,
125 CDCInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
126
127 CDCInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
128
129 FoundEndpoints |= CDC_FOUND_DATAPIPE_OUT;
130 }
131 }
132 }
133
134 CDCInterfaceInfo->State.ControlLineStates.HostToDevice = (CDC_CONTROL_LINE_OUT_RTS | CDC_CONTROL_LINE_OUT_DTR);
135 CDCInterfaceInfo->State.ControlLineStates.DeviceToHost = (CDC_CONTROL_LINE_IN_DCD | CDC_CONTROL_LINE_IN_DSR);
136 CDCInterfaceInfo->State.IsActive = true;
137 return CDC_ENUMERROR_NoError;
138 }
139
140 static uint8_t DComp_CDC_Host_NextCDCControlInterface(void* const CurrentDescriptor)
141 {
142 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
143 {
144 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
145 USB_Descriptor_Interface_t);
146
147 if ((CurrentInterface->Class == CDC_CONTROL_CLASS) &&
148 (CurrentInterface->SubClass == CDC_CONTROL_SUBCLASS) &&
149 (CurrentInterface->Protocol == CDC_CONTROL_PROTOCOL))
150 {
151 return DESCRIPTOR_SEARCH_Found;
152 }
153 }
154
155 return DESCRIPTOR_SEARCH_NotFound;
156 }
157
158 static uint8_t DComp_CDC_Host_NextCDCDataInterface(void* const CurrentDescriptor)
159 {
160 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
161 {
162 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
163 USB_Descriptor_Interface_t);
164
165 if ((CurrentInterface->Class == CDC_DATA_CLASS) &&
166 (CurrentInterface->SubClass == CDC_DATA_SUBCLASS) &&
167 (CurrentInterface->Protocol == CDC_DATA_PROTOCOL))
168 {
169 return DESCRIPTOR_SEARCH_Found;
170 }
171 }
172
173 return DESCRIPTOR_SEARCH_NotFound;
174 }
175
176 static uint8_t DComp_CDC_Host_NextCDCInterfaceEndpoint(void* const CurrentDescriptor)
177 {
178 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
179 {
180 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
181 USB_Descriptor_Endpoint_t);
182
183 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
184
185 if ((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT))
186 {
187 return DESCRIPTOR_SEARCH_Found;
188 }
189 }
190 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
191 {
192 return DESCRIPTOR_SEARCH_Fail;
193 }
194
195 return DESCRIPTOR_SEARCH_NotFound;
196 }
197
198 void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
199 {
200 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
201 return;
202
203 Pipe_SelectPipe(CDCInterfaceInfo->Config.NotificationPipeNumber);
204 Pipe_SetPipeToken(PIPE_TOKEN_IN);
205 Pipe_Unfreeze();
206
207 if (Pipe_IsINReceived())
208 {
209 USB_Request_Header_t Notification;
210 Pipe_Read_Stream_LE(&Notification, sizeof(USB_Request_Header_t), NO_STREAM_CALLBACK);
211
212 if ((Notification.bRequest == NOTIF_SerialState) &&
213 (Notification.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)))
214 {
215 Pipe_Read_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost,
216 sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost),
217 NO_STREAM_CALLBACK);
218
219 }
220
221 Pipe_ClearIN();
222
223 EVENT_CDC_Host_ControLineStateChanged(CDCInterfaceInfo);
224 }
225
226 Pipe_Freeze();
227 }
228
229 uint8_t CDC_Host_SetLineEncoding(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
230 {
231 USB_ControlRequest = (USB_Request_Header_t)
232 {
233 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
234 .bRequest = REQ_SetLineEncoding,
235 .wValue = 0,
236 .wIndex = CDCInterfaceInfo->State.ControlInterfaceNumber,
237 .wLength = sizeof(CDCInterfaceInfo->State.LineEncoding),
238 };
239
240 Pipe_SelectPipe(PIPE_CONTROLPIPE);
241
242 return USB_Host_SendControlRequest(&CDCInterfaceInfo->State.LineEncoding);
243 }
244
245 uint8_t CDC_Host_SendControlLineStateChange(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
246 {
247 USB_ControlRequest = (USB_Request_Header_t)
248 {
249 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
250 .bRequest = REQ_SetControlLineState,
251 .wValue = CDCInterfaceInfo->State.ControlLineStates.HostToDevice,
252 .wIndex = CDCInterfaceInfo->State.ControlInterfaceNumber,
253 .wLength = 0,
254 };
255
256 Pipe_SelectPipe(PIPE_CONTROLPIPE);
257
258 return USB_Host_SendControlRequest(NULL);
259 }
260
261 uint8_t CDC_Host_SendString(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo, char* Data, const uint16_t Length)
262 {
263 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
264 return PIPE_READYWAIT_DeviceDisconnected;
265
266 uint8_t ErrorCode;
267
268 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
269
270 Pipe_Unfreeze();
271 ErrorCode = Pipe_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK);
272 Pipe_Freeze();
273
274 return ErrorCode;
275 }
276
277 uint8_t CDC_Host_SendByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo, const uint8_t Data)
278 {
279 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
280 return PIPE_READYWAIT_DeviceDisconnected;
281
282 uint8_t ErrorCode;
283
284 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
285 Pipe_Unfreeze();
286
287 if (!(Pipe_IsReadWriteAllowed()))
288 {
289 Pipe_ClearOUT();
290
291 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
292 return ErrorCode;
293 }
294
295 Pipe_Write_Byte(Data);
296 Pipe_Freeze();
297
298 return PIPE_READYWAIT_NoError;
299 }
300
301 uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
302 {
303 uint16_t BytesInPipe = 0;
304
305 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
306 return 0;
307
308 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipeNumber);
309 Pipe_SetPipeToken(PIPE_TOKEN_IN);
310 Pipe_Unfreeze();
311
312 if (Pipe_IsINReceived())
313 {
314 if (!(Pipe_BytesInPipe()))
315 Pipe_ClearIN();
316
317 BytesInPipe = Pipe_BytesInPipe();
318 Pipe_Freeze();
319
320 return BytesInPipe;
321 }
322 else
323 {
324 Pipe_Freeze();
325
326 return 0;
327 }
328 }
329
330 uint8_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
331 {
332 uint8_t ReceivedByte = 0;
333
334 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
335 return 0;
336
337 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipeNumber);
338 Pipe_SetPipeToken(PIPE_TOKEN_IN);
339 Pipe_Unfreeze();
340
341 ReceivedByte = Pipe_Read_Byte();
342
343 if (!(Pipe_BytesInPipe()))
344 Pipe_ClearIN();
345
346 Pipe_Freeze();
347
348 return ReceivedByte;
349 }
350
351 uint8_t CDC_Host_Flush(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
352 {
353 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
354 return PIPE_READYWAIT_DeviceDisconnected;
355
356 uint8_t ErrorCode;
357
358 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
359 Pipe_Unfreeze();
360
361 if (!(Pipe_BytesInPipe()))
362 return PIPE_READYWAIT_NoError;
363
364 bool BankFull = !(Pipe_IsReadWriteAllowed());
365
366 Pipe_ClearOUT();
367
368 if (BankFull)
369 {
370 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
371 return ErrorCode;
372
373 Pipe_ClearOUT();
374 }
375
376 Pipe_Freeze();
377
378 return PIPE_READYWAIT_NoError;
379 }
380
381 void CDC_Host_CreateStream(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, FILE* Stream)
382 {
383 *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar, CDC_Host_getchar, _FDEV_SETUP_RW);
384 fdev_set_udata(Stream, CDCInterfaceInfo);
385 }
386
387 void CDC_Host_CreateBlockingStream(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, FILE* Stream)
388 {
389 *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar, CDC_Host_getchar_Blocking, _FDEV_SETUP_RW);
390 fdev_set_udata(Stream, CDCInterfaceInfo);
391 }
392
393 static int CDC_Host_putchar(char c, FILE* Stream)
394 {
395 return CDC_Host_SendByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream), c) ? _FDEV_ERR : 0;
396 }
397
398 static int CDC_Host_getchar(FILE* Stream)
399 {
400 if (!(CDC_Host_BytesReceived((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream))))
401 return _FDEV_EOF;
402
403 return CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
404 }
405
406 static int CDC_Host_getchar_Blocking(FILE* Stream)
407 {
408 while (!(CDC_Host_BytesReceived((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream))))
409 {
410 if (USB_HostState == HOST_STATE_Unattached)
411 return _FDEV_EOF;
412
413 CDC_Host_USBTask((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
414 USB_USBTask();
415 }
416
417 return CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
418 }
419
420 void CDC_Host_Event_Stub(void)
421 {
422
423 }
424
425 #endif