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