Fixed Pipe_IsEndpointBound() function not taking the endpoint's direction into account.
[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 !(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
187 {
188 return DESCRIPTOR_SEARCH_Found;
189 }
190 }
191 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
192 {
193 return DESCRIPTOR_SEARCH_Fail;
194 }
195
196 return DESCRIPTOR_SEARCH_NotFound;
197 }
198
199 void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
200 {
201 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
202 return;
203
204 Pipe_SelectPipe(CDCInterfaceInfo->Config.NotificationPipeNumber);
205 Pipe_SetPipeToken(PIPE_TOKEN_IN);
206 Pipe_Unfreeze();
207
208 if (Pipe_IsINReceived())
209 {
210 USB_Request_Header_t Notification;
211 Pipe_Read_Stream_LE(&Notification, sizeof(USB_Request_Header_t), NO_STREAM_CALLBACK);
212
213 if ((Notification.bRequest == NOTIF_SerialState) &&
214 (Notification.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)))
215 {
216 Pipe_Read_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost,
217 sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost),
218 NO_STREAM_CALLBACK);
219
220 }
221
222 Pipe_ClearIN();
223
224 EVENT_CDC_Host_ControLineStateChanged(CDCInterfaceInfo);
225 }
226
227 Pipe_Freeze();
228 }
229
230 uint8_t CDC_Host_SetLineEncoding(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
231 {
232 USB_ControlRequest = (USB_Request_Header_t)
233 {
234 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
235 .bRequest = REQ_SetLineEncoding,
236 .wValue = 0,
237 .wIndex = CDCInterfaceInfo->State.ControlInterfaceNumber,
238 .wLength = sizeof(CDCInterfaceInfo->State.LineEncoding),
239 };
240
241 Pipe_SelectPipe(PIPE_CONTROLPIPE);
242
243 return USB_Host_SendControlRequest(&CDCInterfaceInfo->State.LineEncoding);
244 }
245
246 uint8_t CDC_Host_SendControlLineStateChange(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
247 {
248 USB_ControlRequest = (USB_Request_Header_t)
249 {
250 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
251 .bRequest = REQ_SetControlLineState,
252 .wValue = CDCInterfaceInfo->State.ControlLineStates.HostToDevice,
253 .wIndex = CDCInterfaceInfo->State.ControlInterfaceNumber,
254 .wLength = 0,
255 };
256
257 Pipe_SelectPipe(PIPE_CONTROLPIPE);
258
259 return USB_Host_SendControlRequest(NULL);
260 }
261
262 uint8_t CDC_Host_SendString(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo, char* Data, const uint16_t Length)
263 {
264 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
265 return PIPE_READYWAIT_DeviceDisconnected;
266
267 uint8_t ErrorCode;
268
269 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
270
271 Pipe_Unfreeze();
272 ErrorCode = Pipe_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK);
273 Pipe_Freeze();
274
275 return ErrorCode;
276 }
277
278 uint8_t CDC_Host_SendByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo, const uint8_t Data)
279 {
280 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
281 return PIPE_READYWAIT_DeviceDisconnected;
282
283 uint8_t ErrorCode;
284
285 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
286 Pipe_Unfreeze();
287
288 if (!(Pipe_IsReadWriteAllowed()))
289 {
290 Pipe_ClearOUT();
291
292 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
293 return ErrorCode;
294 }
295
296 Pipe_Write_Byte(Data);
297 Pipe_Freeze();
298
299 return PIPE_READYWAIT_NoError;
300 }
301
302 uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
303 {
304 uint16_t BytesInPipe = 0;
305
306 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
307 return 0;
308
309 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipeNumber);
310 Pipe_SetPipeToken(PIPE_TOKEN_IN);
311 Pipe_Unfreeze();
312
313 if (Pipe_IsINReceived())
314 {
315 if (!(Pipe_BytesInPipe()))
316 Pipe_ClearIN();
317
318 BytesInPipe = Pipe_BytesInPipe();
319 Pipe_Freeze();
320
321 return BytesInPipe;
322 }
323 else
324 {
325 Pipe_Freeze();
326
327 return 0;
328 }
329 }
330
331 uint8_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
332 {
333 uint8_t ReceivedByte = 0;
334
335 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
336 return 0;
337
338 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipeNumber);
339 Pipe_SetPipeToken(PIPE_TOKEN_IN);
340 Pipe_Unfreeze();
341
342 ReceivedByte = Pipe_Read_Byte();
343
344 if (!(Pipe_BytesInPipe()))
345 Pipe_ClearIN();
346
347 Pipe_Freeze();
348
349 return ReceivedByte;
350 }
351
352 uint8_t CDC_Host_Flush(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
353 {
354 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
355 return PIPE_READYWAIT_DeviceDisconnected;
356
357 uint8_t ErrorCode;
358
359 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
360 Pipe_Unfreeze();
361
362 if (!(Pipe_BytesInPipe()))
363 return PIPE_READYWAIT_NoError;
364
365 bool BankFull = !(Pipe_IsReadWriteAllowed());
366
367 Pipe_ClearOUT();
368
369 if (BankFull)
370 {
371 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
372 return ErrorCode;
373
374 Pipe_ClearOUT();
375 }
376
377 Pipe_Freeze();
378
379 return PIPE_READYWAIT_NoError;
380 }
381
382 void CDC_Host_CreateStream(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, FILE* Stream)
383 {
384 *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar, CDC_Host_getchar, _FDEV_SETUP_RW);
385 fdev_set_udata(Stream, CDCInterfaceInfo);
386 }
387
388 void CDC_Host_CreateBlockingStream(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, FILE* Stream)
389 {
390 *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar, CDC_Host_getchar_Blocking, _FDEV_SETUP_RW);
391 fdev_set_udata(Stream, CDCInterfaceInfo);
392 }
393
394 static int CDC_Host_putchar(char c, FILE* Stream)
395 {
396 return CDC_Host_SendByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream), c) ? _FDEV_ERR : 0;
397 }
398
399 static int CDC_Host_getchar(FILE* Stream)
400 {
401 if (!(CDC_Host_BytesReceived((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream))))
402 return _FDEV_EOF;
403
404 return CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
405 }
406
407 static int CDC_Host_getchar_Blocking(FILE* Stream)
408 {
409 while (!(CDC_Host_BytesReceived((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream))))
410 {
411 if (USB_HostState == HOST_STATE_Unattached)
412 return _FDEV_EOF;
413
414 CDC_Host_USBTask((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
415 USB_USBTask();
416 }
417
418 return CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
419 }
420
421 void CDC_Host_Event_Stub(void)
422 {
423
424 }
425
426 #endif