Add more include protection macros to give the user warnings when they try to manuall...
[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_SendString(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo, char* Data, const uint16_t Length)
265 {
266 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
267 return PIPE_READYWAIT_DeviceDisconnected;
268
269 uint8_t ErrorCode;
270
271 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
272
273 Pipe_Unfreeze();
274 ErrorCode = Pipe_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK);
275 Pipe_Freeze();
276
277 return ErrorCode;
278 }
279
280 uint8_t CDC_Host_SendByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo, const uint8_t Data)
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 Pipe_Unfreeze();
289
290 if (!(Pipe_IsReadWriteAllowed()))
291 {
292 Pipe_ClearOUT();
293
294 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
295 return ErrorCode;
296 }
297
298 Pipe_Write_Byte(Data);
299 Pipe_Freeze();
300
301 return PIPE_READYWAIT_NoError;
302 }
303
304 uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
305 {
306 uint16_t BytesInPipe = 0;
307
308 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
309 return 0;
310
311 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipeNumber);
312 Pipe_SetPipeToken(PIPE_TOKEN_IN);
313 Pipe_Unfreeze();
314
315 if (Pipe_IsINReceived())
316 {
317 if (!(Pipe_BytesInPipe()))
318 Pipe_ClearIN();
319
320 BytesInPipe = Pipe_BytesInPipe();
321 Pipe_Freeze();
322
323 return BytesInPipe;
324 }
325 else
326 {
327 Pipe_Freeze();
328
329 return 0;
330 }
331 }
332
333 uint8_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
334 {
335 uint8_t ReceivedByte = 0;
336
337 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
338 return 0;
339
340 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipeNumber);
341 Pipe_SetPipeToken(PIPE_TOKEN_IN);
342 Pipe_Unfreeze();
343
344 ReceivedByte = Pipe_Read_Byte();
345
346 if (!(Pipe_BytesInPipe()))
347 Pipe_ClearIN();
348
349 Pipe_Freeze();
350
351 return ReceivedByte;
352 }
353
354 uint8_t CDC_Host_Flush(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
355 {
356 if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
357 return PIPE_READYWAIT_DeviceDisconnected;
358
359 uint8_t ErrorCode;
360
361 Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipeNumber);
362 Pipe_Unfreeze();
363
364 if (!(Pipe_BytesInPipe()))
365 return PIPE_READYWAIT_NoError;
366
367 bool BankFull = !(Pipe_IsReadWriteAllowed());
368
369 Pipe_ClearOUT();
370
371 if (BankFull)
372 {
373 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
374 return ErrorCode;
375
376 Pipe_ClearOUT();
377 }
378
379 Pipe_Freeze();
380
381 return PIPE_READYWAIT_NoError;
382 }
383
384 void CDC_Host_CreateStream(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, FILE* Stream)
385 {
386 *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar, CDC_Host_getchar, _FDEV_SETUP_RW);
387 fdev_set_udata(Stream, CDCInterfaceInfo);
388 }
389
390 void CDC_Host_CreateBlockingStream(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, FILE* Stream)
391 {
392 *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar, CDC_Host_getchar_Blocking, _FDEV_SETUP_RW);
393 fdev_set_udata(Stream, CDCInterfaceInfo);
394 }
395
396 static int CDC_Host_putchar(char c, FILE* Stream)
397 {
398 return CDC_Host_SendByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream), c) ? _FDEV_ERR : 0;
399 }
400
401 static int CDC_Host_getchar(FILE* Stream)
402 {
403 if (!(CDC_Host_BytesReceived((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream))))
404 return _FDEV_EOF;
405
406 return CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
407 }
408
409 static int CDC_Host_getchar_Blocking(FILE* Stream)
410 {
411 while (!(CDC_Host_BytesReceived((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream))))
412 {
413 if (USB_HostState == HOST_STATE_Unattached)
414 return _FDEV_EOF;
415
416 CDC_Host_USBTask((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
417 USB_USBTask();
418 }
419
420 return CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
421 }
422
423 void CDC_Host_Event_Stub(void)
424 {
425
426 }
427
428 #endif