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