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