3 Copyright (C) Dean Camera, 2014.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2014 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 disclaims 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 "../../Core/USBMode.h"
34 #if defined(USB_CAN_BE_HOST)
36 #define __INCLUDE_FROM_PRINTER_DRIVER
37 #define __INCLUDE_FROM_PRINTER_HOST_C
38 #include "PrinterClassHost.h"
40 uint8_t PRNT_Host_ConfigurePipes(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
41 uint16_t ConfigDescriptorSize,
42 void* ConfigDescriptorData)
44 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
45 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
46 USB_Descriptor_Interface_t* PrinterInterface = NULL;
48 memset(&PRNTInterfaceInfo->State, 0x00, sizeof(PRNTInterfaceInfo->State));
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
51 return PRNT_ENUMERROR_InvalidConfigDescriptor;
53 while (!(DataINEndpoint) || !(DataOUTEndpoint))
55 if (!(PrinterInterface) ||
56 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
57 DCOMP_PRNT_Host_NextPRNTInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
59 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
60 DCOMP_PRNT_Host_NextPRNTInterface) != DESCRIPTOR_SEARCH_COMP_Found)
62 return PRNT_ENUMERROR_NoCompatibleInterfaceFound;
65 PrinterInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
67 DataINEndpoint = NULL;
68 DataOUTEndpoint = NULL;
73 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
75 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
76 DataINEndpoint = EndpointData;
78 DataOUTEndpoint = EndpointData;
81 PRNTInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
82 PRNTInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
83 PRNTInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_BULK;
85 PRNTInterfaceInfo->Config.DataOUTPipe.Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
86 PRNTInterfaceInfo->Config.DataOUTPipe.EndpointAddress = DataOUTEndpoint->EndpointAddress;
87 PRNTInterfaceInfo->Config.DataOUTPipe.Type = EP_TYPE_BULK;
89 if (!(Pipe_ConfigurePipeTable(&PRNTInterfaceInfo->Config.DataINPipe, 1)))
90 return PRNT_ENUMERROR_PipeConfigurationFailed;
92 if (!(Pipe_ConfigurePipeTable(&PRNTInterfaceInfo->Config.DataOUTPipe, 1)))
93 return PRNT_ENUMERROR_PipeConfigurationFailed;
95 PRNTInterfaceInfo->State.InterfaceNumber = PrinterInterface->InterfaceNumber;
96 PRNTInterfaceInfo->State.AlternateSetting = PrinterInterface->AlternateSetting;
97 PRNTInterfaceInfo->State.IsActive = true;
99 return PRNT_ENUMERROR_NoError;
102 static uint8_t DCOMP_PRNT_Host_NextPRNTInterface(void* CurrentDescriptor)
104 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
106 if (Header->Type == DTYPE_Interface)
108 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
110 if ((Interface->Class == PRNT_CSCP_PrinterClass) &&
111 (Interface->SubClass == PRNT_CSCP_PrinterSubclass) &&
112 (Interface->Protocol == PRNT_CSCP_BidirectionalProtocol))
114 return DESCRIPTOR_SEARCH_Found;
118 return DESCRIPTOR_SEARCH_NotFound;
121 static uint8_t DCOMP_PRNT_Host_NextPRNTInterfaceEndpoint(void* CurrentDescriptor)
123 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
125 if (Header->Type == DTYPE_Endpoint)
127 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
129 uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
131 if (EndpointType == EP_TYPE_BULK)
132 return DESCRIPTOR_SEARCH_Found;
134 else if (Header->Type == DTYPE_Interface)
136 return DESCRIPTOR_SEARCH_Fail;
139 return DESCRIPTOR_SEARCH_NotFound;
142 void PRNT_Host_USBTask(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
144 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
147 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
148 PRNT_Host_Flush(PRNTInterfaceInfo);
152 uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
154 if (PRNTInterfaceInfo->State.AlternateSetting)
158 if ((ErrorCode = USB_Host_SetInterfaceAltSetting(PRNTInterfaceInfo->State.InterfaceNumber,
159 PRNTInterfaceInfo->State.AlternateSetting)) != HOST_SENDCONTROL_Successful)
165 return HOST_SENDCONTROL_Successful;
168 uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
169 uint8_t* const PortStatus)
171 USB_ControlRequest = (USB_Request_Header_t)
173 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
174 .bRequest = PRNT_REQ_GetPortStatus,
176 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
177 .wLength = sizeof(uint8_t),
180 Pipe_SelectPipe(PIPE_CONTROLPIPE);
181 return USB_Host_SendControlRequest(PortStatus);
184 uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
186 USB_ControlRequest = (USB_Request_Header_t)
188 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
189 .bRequest = PRNT_REQ_SoftReset,
191 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
195 Pipe_SelectPipe(PIPE_CONTROLPIPE);
196 return USB_Host_SendControlRequest(NULL);
199 uint8_t PRNT_Host_Flush(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
201 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
202 return PIPE_READYWAIT_DeviceDisconnected;
206 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataOUTPipe.Address);
209 if (!(Pipe_BytesInPipe()))
210 return PIPE_READYWAIT_NoError;
212 bool BankFull = !(Pipe_IsReadWriteAllowed());
218 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
226 return PIPE_READYWAIT_NoError;
229 uint8_t PRNT_Host_SendByte(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
232 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
233 return PIPE_READYWAIT_DeviceDisconnected;
237 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataOUTPipe.Address);
240 if (!(Pipe_IsReadWriteAllowed()))
244 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
251 return PIPE_READYWAIT_NoError;
254 uint8_t PRNT_Host_SendString(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
255 const char* const String)
259 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
260 return PIPE_RWSTREAM_DeviceDisconnected;
262 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataOUTPipe.Address);
265 if ((ErrorCode = Pipe_Write_Stream_LE(String, strlen(String), NULL)) != PIPE_RWSTREAM_NoError)
270 ErrorCode = Pipe_WaitUntilReady();
277 uint8_t PRNT_Host_SendData(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
279 const uint16_t Length)
283 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
284 return PIPE_RWSTREAM_DeviceDisconnected;
286 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataOUTPipe.Address);
289 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, Length, NULL)) != PIPE_RWSTREAM_NoError)
294 ErrorCode = Pipe_WaitUntilReady();
301 uint16_t PRNT_Host_BytesReceived(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
303 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
306 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataINPipe.Address);
309 if (Pipe_IsINReceived())
311 if (!(Pipe_BytesInPipe()))
320 return Pipe_BytesInPipe();
331 int16_t PRNT_Host_ReceiveByte(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo)
333 if ((USB_HostState != HOST_STATE_Configured) || !(PRNTInterfaceInfo->State.IsActive))
334 return PIPE_RWSTREAM_DeviceDisconnected;
336 int16_t ReceivedByte = -1;
338 Pipe_SelectPipe(PRNTInterfaceInfo->Config.DataINPipe.Address);
341 if (Pipe_IsINReceived())
343 if (Pipe_BytesInPipe())
344 ReceivedByte = Pipe_Read_8();
346 if (!(Pipe_BytesInPipe()))
355 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t* const PRNTInterfaceInfo,
356 char* const DeviceIDString,
357 const uint16_t BufferSize)
360 uint16_t DeviceIDStringLength = 0;
362 USB_ControlRequest = (USB_Request_Header_t)
364 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
365 .bRequest = PRNT_REQ_GetDeviceID,
367 .wIndex = PRNTInterfaceInfo->State.InterfaceNumber,
368 .wLength = sizeof(DeviceIDStringLength),
371 Pipe_SelectPipe(PIPE_CONTROLPIPE);
373 if ((ErrorCode = USB_Host_SendControlRequest(&DeviceIDStringLength)) != HOST_SENDCONTROL_Successful)
376 if (!(DeviceIDStringLength))
378 DeviceIDString[0] = 0x00;
379 return HOST_SENDCONTROL_Successful;
382 DeviceIDStringLength = be16_to_cpu(DeviceIDStringLength);
384 if (DeviceIDStringLength > BufferSize)
385 DeviceIDStringLength = BufferSize;
387 USB_ControlRequest.wLength = DeviceIDStringLength;
389 if ((ErrorCode = USB_Host_SendControlRequest(DeviceIDString)) != HOST_SENDCONTROL_Successful)
392 memmove(&DeviceIDString[0], &DeviceIDString[2], DeviceIDStringLength - 2);
394 DeviceIDString[DeviceIDStringLength - 2] = 0x00;
396 return HOST_SENDCONTROL_Successful;