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_PRINTER_CLASS_HOST_C
36 #define __INCLUDE_FROM_PRINTER_DRIVER
39 uint8_t PRNT_Host_ConfigurePipes(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* DeviceConfigDescriptor
)
43 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
44 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
45 USB_Descriptor_Interface_t
* PrinterInterface
= NULL
;
47 memset(&PRNTInterfaceInfo
->State
, 0x00, sizeof(PRNTInterfaceInfo
->State
));
49 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor
) != DTYPE_Configuration
)
50 return PRNT_ENUMERROR_InvalidConfigDescriptor
;
52 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
54 if (!(PrinterInterface
) ||
55 USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &DeviceConfigDescriptor
,
56 DCOMP_PRNT_Host_NextPRNTInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
58 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &DeviceConfigDescriptor
,
59 DCOMP_PRNT_Host_NextPRNTInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
61 return PRNT_ENUMERROR_NoCompatibleInterfaceFound
;
64 PrinterInterface
= DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Interface_t
);
66 DataINEndpoint
= NULL
;
67 DataOUTEndpoint
= NULL
;
72 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Endpoint_t
);
74 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
75 DataINEndpoint
= EndpointData
;
77 DataOUTEndpoint
= EndpointData
;
80 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
82 if (PipeNum
== PRNTInterfaceInfo
->Config
.DataINPipeNumber
)
84 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
85 DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
,
86 PRNTInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
88 PRNTInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
90 else if (PipeNum
== PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
)
92 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
93 DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
,
94 PRNTInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
96 PRNTInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
100 PRNTInterfaceInfo
->State
.InterfaceNumber
= PrinterInterface
->InterfaceNumber
;
101 PRNTInterfaceInfo
->State
.AlternateSetting
= PrinterInterface
->AlternateSetting
;
102 PRNTInterfaceInfo
->State
.IsActive
= true;
104 return PRNT_ENUMERROR_NoError
;
107 static uint8_t DCOMP_PRNT_Host_NextPRNTInterface(void* CurrentDescriptor
)
109 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
111 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== PRINTER_CLASS
) &&
112 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== PRINTER_SUBCLASS
) &&
113 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== PRINTER_PROTOCOL
))
115 return DESCRIPTOR_SEARCH_Found
;
119 return DESCRIPTOR_SEARCH_NotFound
;
122 static uint8_t DCOMP_PRNT_Host_NextPRNTInterfaceEndpoint(void* CurrentDescriptor
)
124 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
126 uint8_t EndpointType
= (DESCRIPTOR_CAST(CurrentDescriptor
,
127 USB_Descriptor_Endpoint_t
).Attributes
& EP_TYPE_MASK
);
129 if (EndpointType
== EP_TYPE_BULK
)
130 return DESCRIPTOR_SEARCH_Found
;
132 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
134 return DESCRIPTOR_SEARCH_Fail
;
137 return DESCRIPTOR_SEARCH_NotFound
;
140 void PRNT_Host_USBTask(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
142 PRNT_Host_Flush(PRNTInterfaceInfo
);
145 uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
147 if (PRNTInterfaceInfo
->State
.AlternateSetting
)
151 USB_ControlRequest
= (USB_Request_Header_t
)
153 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
154 .bRequest
= REQ_SetInterface
,
155 .wValue
= PRNTInterfaceInfo
->State
.AlternateSetting
,
156 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
160 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
162 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
166 return HOST_SENDCONTROL_Successful
;
169 uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
170 uint8_t* const PortStatus
)
172 USB_ControlRequest
= (USB_Request_Header_t
)
174 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
175 .bRequest
= REQ_GetPortStatus
,
177 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
178 .wLength
= sizeof(uint8_t),
181 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
183 return USB_Host_SendControlRequest(PortStatus
);
186 uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
188 USB_ControlRequest
= (USB_Request_Header_t
)
190 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
191 .bRequest
= REQ_SoftReset
,
193 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
197 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
199 return USB_Host_SendControlRequest(NULL
);
202 uint8_t PRNT_Host_Flush(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
204 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
205 return PIPE_READYWAIT_DeviceDisconnected
;
209 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
);
212 if (!(Pipe_BytesInPipe()))
213 return PIPE_READYWAIT_NoError
;
215 bool BankFull
= !(Pipe_IsReadWriteAllowed());
221 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
229 return PIPE_READYWAIT_NoError
;
232 uint8_t PRNT_Host_SendByte(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
235 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
236 return PIPE_READYWAIT_DeviceDisconnected
;
240 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
);
243 if (!(Pipe_IsReadWriteAllowed()))
247 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
251 Pipe_Write_Byte(Data
);
254 return PIPE_READYWAIT_NoError
;
257 uint8_t PRNT_Host_SendString(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
259 const uint16_t Length
)
263 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
264 return PIPE_RWSTREAM_DeviceDisconnected
;
266 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
);
269 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, Length
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
274 ErrorCode
= Pipe_WaitUntilReady();
281 uint16_t PRNT_Host_BytesReceived(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
283 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
286 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataINPipeNumber
);
289 if (Pipe_IsINReceived())
291 if (!(Pipe_BytesInPipe()))
300 return Pipe_BytesInPipe();
311 int16_t PRNT_Host_ReceiveByte(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
313 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
314 return PIPE_RWSTREAM_DeviceDisconnected
;
316 int16_t ReceivedByte
= -1;
318 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataINPipeNumber
);
321 if (Pipe_IsINReceived())
323 if (Pipe_BytesInPipe())
324 ReceivedByte
= Pipe_Read_Byte();
326 if (!(Pipe_BytesInPipe()))
335 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
336 char* const DeviceIDString
,
337 const uint16_t BufferSize
)
339 uint8_t ErrorCode
= HOST_SENDCONTROL_Successful
;
340 uint16_t DeviceIDStringLength
= 0;
342 USB_ControlRequest
= (USB_Request_Header_t
)
344 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
345 .bRequest
= REQ_GetDeviceID
,
347 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
348 .wLength
= sizeof(DeviceIDStringLength
),
351 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
353 if ((ErrorCode
= USB_Host_SendControlRequest(&DeviceIDStringLength
)) != HOST_SENDCONTROL_Successful
)
356 if (!(DeviceIDStringLength
))
358 DeviceIDString
[0] = 0x00;
359 return HOST_SENDCONTROL_Successful
;
362 DeviceIDStringLength
= SwapEndian_16(DeviceIDStringLength
);
364 if (DeviceIDStringLength
> BufferSize
)
365 DeviceIDStringLength
= BufferSize
;
367 USB_ControlRequest
.wLength
= DeviceIDStringLength
;
369 if ((ErrorCode
= USB_Host_SendControlRequest(DeviceIDString
)) != HOST_SENDCONTROL_Successful
)
372 memmove(&DeviceIDString
[0], &DeviceIDString
[2], DeviceIDStringLength
- 2);
374 DeviceIDString
[DeviceIDStringLength
- 2] = 0x00;
376 return HOST_SENDCONTROL_Successful
;