3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define __INCLUDE_FROM_CDC_CLASS_HOST_C
36 #define __INCLUDE_FROM_CDC_DRIVER
39 uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* ConfigDescriptorData
)
43 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
44 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
45 USB_Descriptor_Endpoint_t
* NotificationEndpoint
= NULL
;
47 memset(&CDCInterfaceInfo
->State
, 0x00, sizeof(CDCInterfaceInfo
->State
));
49 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
50 return CDC_ENUMERROR_InvalidConfigDescriptor
;
52 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
53 DCOMP_CDC_Host_NextCDCControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
55 return CDC_ENUMERROR_NoCompatibleInterfaceFound
;
58 CDCInterfaceInfo
->State
.ControlInterfaceNumber
= DESCRIPTOR_PCAST(ConfigDescriptorData
,
59 USB_Descriptor_Interface_t
)->InterfaceNumber
;
61 while (!(DataINEndpoint
) || !(DataOUTEndpoint
) || !(NotificationEndpoint
))
63 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
64 DCOMP_CDC_Host_NextCDCInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
66 if (NotificationEndpoint
)
68 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
69 DCOMP_CDC_Host_NextCDCDataInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
71 return CDC_ENUMERROR_NoCompatibleInterfaceFound
;
74 DataINEndpoint
= NULL
;
75 DataOUTEndpoint
= NULL
;
79 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
80 DCOMP_CDC_Host_NextCDCControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
82 return CDC_ENUMERROR_NoCompatibleInterfaceFound
;
85 CDCInterfaceInfo
->State
.ControlInterfaceNumber
= DESCRIPTOR_PCAST(ConfigDescriptorData
,
86 USB_Descriptor_Interface_t
)->InterfaceNumber
;
88 NotificationEndpoint
= NULL
;
94 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
96 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
98 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
99 NotificationEndpoint
= EndpointData
;
101 DataINEndpoint
= EndpointData
;
105 DataOUTEndpoint
= EndpointData
;
109 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
111 if (PipeNum
== CDCInterfaceInfo
->Config
.DataINPipeNumber
)
113 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
114 DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
,
115 CDCInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
117 CDCInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
119 else if (PipeNum
== CDCInterfaceInfo
->Config
.DataOUTPipeNumber
)
121 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
122 DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
,
123 CDCInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
125 CDCInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
127 else if (PipeNum
== CDCInterfaceInfo
->Config
.NotificationPipeNumber
)
129 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
130 NotificationEndpoint
->EndpointAddress
, NotificationEndpoint
->EndpointSize
,
131 CDCInterfaceInfo
->Config
.NotificationPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
132 Pipe_SetInterruptPeriod(NotificationEndpoint
->PollingIntervalMS
);
134 CDCInterfaceInfo
->State
.NotificationPipeSize
= NotificationEndpoint
->EndpointSize
;
138 CDCInterfaceInfo
->State
.ControlLineStates
.HostToDevice
= (CDC_CONTROL_LINE_OUT_RTS
| CDC_CONTROL_LINE_OUT_DTR
);
139 CDCInterfaceInfo
->State
.ControlLineStates
.DeviceToHost
= (CDC_CONTROL_LINE_IN_DCD
| CDC_CONTROL_LINE_IN_DSR
);
140 CDCInterfaceInfo
->State
.IsActive
= true;
142 return CDC_ENUMERROR_NoError
;
145 static uint8_t DCOMP_CDC_Host_NextCDCControlInterface(void* const CurrentDescriptor
)
147 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
149 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
150 USB_Descriptor_Interface_t
);
152 if ((CurrentInterface
->Class
== CDC_CONTROL_CLASS
) &&
153 (CurrentInterface
->SubClass
== CDC_CONTROL_SUBCLASS
) &&
154 (CurrentInterface
->Protocol
== CDC_CONTROL_PROTOCOL
))
156 return DESCRIPTOR_SEARCH_Found
;
160 return DESCRIPTOR_SEARCH_NotFound
;
163 static uint8_t DCOMP_CDC_Host_NextCDCDataInterface(void* const CurrentDescriptor
)
165 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
167 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
168 USB_Descriptor_Interface_t
);
170 if ((CurrentInterface
->Class
== CDC_DATA_CLASS
) &&
171 (CurrentInterface
->SubClass
== CDC_DATA_SUBCLASS
) &&
172 (CurrentInterface
->Protocol
== CDC_DATA_PROTOCOL
))
174 return DESCRIPTOR_SEARCH_Found
;
178 return DESCRIPTOR_SEARCH_NotFound
;
181 static uint8_t DCOMP_CDC_Host_NextCDCInterfaceEndpoint(void* const CurrentDescriptor
)
183 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
185 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
186 USB_Descriptor_Endpoint_t
);
188 uint8_t EndpointType
= (CurrentEndpoint
->Attributes
& EP_TYPE_MASK
);
190 if (((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
)) &&
191 !(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
193 return DESCRIPTOR_SEARCH_Found
;
196 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
198 return DESCRIPTOR_SEARCH_Fail
;
201 return DESCRIPTOR_SEARCH_NotFound
;
204 void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
)
206 if ((USB_HostState
!= HOST_STATE_Configured
) || !(CDCInterfaceInfo
->State
.IsActive
))
209 Pipe_SelectPipe(CDCInterfaceInfo
->Config
.NotificationPipeNumber
);
212 if (Pipe_IsINReceived())
214 USB_Request_Header_t Notification
;
215 Pipe_Read_Stream_LE(&Notification
, sizeof(USB_Request_Header_t
), NO_STREAM_CALLBACK
);
217 if ((Notification
.bRequest
== CDC_NOTIF_SerialState
) &&
218 (Notification
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
)))
220 Pipe_Read_Stream_LE(&CDCInterfaceInfo
->State
.ControlLineStates
.DeviceToHost
,
221 sizeof(CDCInterfaceInfo
->State
.ControlLineStates
.DeviceToHost
),
226 EVENT_CDC_Host_ControLineStateChanged(CDCInterfaceInfo
);
236 CDC_Host_Flush(CDCInterfaceInfo
);
239 uint8_t CDC_Host_SetLineEncoding(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
)
241 USB_ControlRequest
= (USB_Request_Header_t
)
243 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
244 .bRequest
= CDC_REQ_SetLineEncoding
,
246 .wIndex
= CDCInterfaceInfo
->State
.ControlInterfaceNumber
,
247 .wLength
= sizeof(CDCInterfaceInfo
->State
.LineEncoding
),
250 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
252 return USB_Host_SendControlRequest(&CDCInterfaceInfo
->State
.LineEncoding
);
255 uint8_t CDC_Host_SendControlLineStateChange(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
)
257 USB_ControlRequest
= (USB_Request_Header_t
)
259 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
260 .bRequest
= CDC_REQ_SetControlLineState
,
261 .wValue
= CDCInterfaceInfo
->State
.ControlLineStates
.HostToDevice
,
262 .wIndex
= CDCInterfaceInfo
->State
.ControlInterfaceNumber
,
266 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
268 return USB_Host_SendControlRequest(NULL
);
271 uint8_t CDC_Host_SendBreak(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
,
272 const uint8_t Duration
)
274 USB_ControlRequest
= (USB_Request_Header_t
)
276 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
277 .bRequest
= CDC_REQ_SendBreak
,
279 .wIndex
= CDCInterfaceInfo
->State
.ControlInterfaceNumber
,
283 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
285 return USB_Host_SendControlRequest(NULL
);
288 uint8_t CDC_Host_SendString(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
,
289 const char* const Data
,
290 const uint16_t Length
)
292 if ((USB_HostState
!= HOST_STATE_Configured
) || !(CDCInterfaceInfo
->State
.IsActive
))
293 return PIPE_READYWAIT_DeviceDisconnected
;
297 Pipe_SelectPipe(CDCInterfaceInfo
->Config
.DataOUTPipeNumber
);
300 ErrorCode
= Pipe_Write_Stream_LE(Data
, Length
, NO_STREAM_CALLBACK
);
306 uint8_t CDC_Host_SendByte(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
,
309 if ((USB_HostState
!= HOST_STATE_Configured
) || !(CDCInterfaceInfo
->State
.IsActive
))
310 return PIPE_READYWAIT_DeviceDisconnected
;
314 Pipe_SelectPipe(CDCInterfaceInfo
->Config
.DataOUTPipeNumber
);
317 if (!(Pipe_IsReadWriteAllowed()))
321 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
325 Pipe_Write_Byte(Data
);
328 return PIPE_READYWAIT_NoError
;
331 uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
)
333 if ((USB_HostState
!= HOST_STATE_Configured
) || !(CDCInterfaceInfo
->State
.IsActive
))
336 Pipe_SelectPipe(CDCInterfaceInfo
->Config
.DataINPipeNumber
);
339 if (Pipe_IsINReceived())
341 if (!(Pipe_BytesInPipe()))
350 return Pipe_BytesInPipe();
361 int16_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
)
363 if ((USB_HostState
!= HOST_STATE_Configured
) || !(CDCInterfaceInfo
->State
.IsActive
))
366 int16_t ReceivedByte
= -1;
368 Pipe_SelectPipe(CDCInterfaceInfo
->Config
.DataINPipeNumber
);
371 if (Pipe_IsINReceived())
373 if (Pipe_BytesInPipe())
374 ReceivedByte
= Pipe_Read_Byte();
376 if (!(Pipe_BytesInPipe()))
385 uint8_t CDC_Host_Flush(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
)
387 if ((USB_HostState
!= HOST_STATE_Configured
) || !(CDCInterfaceInfo
->State
.IsActive
))
388 return PIPE_READYWAIT_DeviceDisconnected
;
392 Pipe_SelectPipe(CDCInterfaceInfo
->Config
.DataOUTPipeNumber
);
395 if (!(Pipe_BytesInPipe()))
396 return PIPE_READYWAIT_NoError
;
398 bool BankFull
= !(Pipe_IsReadWriteAllowed());
404 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
412 return PIPE_READYWAIT_NoError
;
415 void CDC_Host_CreateStream(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
,
418 *Stream
= (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar
, CDC_Host_getchar
, _FDEV_SETUP_RW
);
419 fdev_set_udata(Stream
, CDCInterfaceInfo
);
422 void CDC_Host_CreateBlockingStream(USB_ClassInfo_CDC_Host_t
* const CDCInterfaceInfo
,
425 *Stream
= (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar
, CDC_Host_getchar_Blocking
, _FDEV_SETUP_RW
);
426 fdev_set_udata(Stream
, CDCInterfaceInfo
);
429 static int CDC_Host_putchar(char c
,
432 return CDC_Host_SendByte((USB_ClassInfo_CDC_Host_t
*)fdev_get_udata(Stream
), c
) ? _FDEV_ERR
: 0;
435 static int CDC_Host_getchar(FILE* Stream
)
437 int16_t ReceivedByte
= CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t
*)fdev_get_udata(Stream
));
439 if (ReceivedByte
< 0)
445 static int CDC_Host_getchar_Blocking(FILE* Stream
)
447 int16_t ReceivedByte
;
449 while ((ReceivedByte
= CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t
*)fdev_get_udata(Stream
))) < 0)
451 if (USB_HostState
== HOST_STATE_Unattached
)
454 CDC_Host_USBTask((USB_ClassInfo_CDC_Host_t
*)fdev_get_udata(Stream
));
461 void CDC_Host_Event_Stub(void)