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 uint8_t FoundEndpoints
= 0;
45 memset(&PRNTInterfaceInfo
->State
, 0x00, sizeof(PRNTInterfaceInfo
->State
));
47 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor
) != DTYPE_Configuration
)
48 return PRNT_ENUMERROR_InvalidConfigDescriptor
;
50 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &DeviceConfigDescriptor
,
51 DCOMP_PRNT_NextPRNTInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
53 return PRNT_ENUMERROR_NoPrinterInterfaceFound
;
56 USB_Descriptor_Interface_t
* PrinterInterface
= DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Interface_t
);
58 PRNTInterfaceInfo
->State
.InterfaceNumber
= PrinterInterface
->InterfaceNumber
;
59 PRNTInterfaceInfo
->State
.AlternateSetting
= PrinterInterface
->AlternateSetting
;
61 while (FoundEndpoints
!= (PRNT_FOUND_DATAPIPE_IN
| PRNT_FOUND_DATAPIPE_OUT
))
63 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &DeviceConfigDescriptor
,
64 DCOMP_PRNT_NextPRNTInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
66 return PRNT_ENUMERROR_EndpointsNotFound
;
69 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Endpoint_t
);
71 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
73 Pipe_ConfigurePipe(PRNTInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
74 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
75 PRNTInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
76 PRNTInterfaceInfo
->State
.DataINPipeSize
= EndpointData
->EndpointSize
;
78 FoundEndpoints
|= PRNT_FOUND_DATAPIPE_IN
;
82 Pipe_ConfigurePipe(PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
83 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
84 PRNTInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
85 PRNTInterfaceInfo
->State
.DataOUTPipeSize
= EndpointData
->EndpointSize
;
87 FoundEndpoints
|= PRNT_FOUND_DATAPIPE_OUT
;
91 PRNTInterfaceInfo
->State
.IsActive
= true;
92 return PRNT_ENUMERROR_NoError
;
95 static uint8_t DCOMP_PRNT_NextPRNTInterface(void* CurrentDescriptor
)
97 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
99 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== PRINTER_CLASS
) &&
100 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== PRINTER_SUBCLASS
) &&
101 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== PRINTER_PROTOCOL
))
103 return DESCRIPTOR_SEARCH_Found
;
107 return DESCRIPTOR_SEARCH_NotFound
;
110 static uint8_t DCOMP_PRNT_NextPRNTInterfaceEndpoint(void* CurrentDescriptor
)
112 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
114 uint8_t EndpointType
= (DESCRIPTOR_CAST(CurrentDescriptor
,
115 USB_Descriptor_Endpoint_t
).Attributes
& EP_TYPE_MASK
);
117 if (EndpointType
== EP_TYPE_BULK
)
118 return DESCRIPTOR_SEARCH_Found
;
120 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
122 return DESCRIPTOR_SEARCH_Fail
;
125 return DESCRIPTOR_SEARCH_NotFound
;
128 uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
130 if (PRNTInterfaceInfo
->State
.AlternateSetting
)
134 USB_ControlRequest
= (USB_Request_Header_t
)
136 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
137 .bRequest
= REQ_SetInterface
,
138 .wValue
= PRNTInterfaceInfo
->State
.AlternateSetting
,
139 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
143 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
145 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
149 return HOST_SENDCONTROL_Successful
;
152 uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
153 uint8_t* const PortStatus
)
155 USB_ControlRequest
= (USB_Request_Header_t
)
157 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
158 .bRequest
= REQ_GetPortStatus
,
160 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
161 .wLength
= sizeof(uint8_t),
164 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
166 return USB_Host_SendControlRequest(PortStatus
);
169 uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
171 USB_ControlRequest
= (USB_Request_Header_t
)
173 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
174 .bRequest
= REQ_SoftReset
,
176 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
180 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
182 return USB_Host_SendControlRequest(NULL
);
185 uint8_t PRNT_Host_SendData(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
186 void* PrinterCommands
,
187 const uint16_t CommandSize
)
191 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
192 return PIPE_RWSTREAM_DeviceDisconnected
;
194 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
);
197 if ((ErrorCode
= Pipe_Write_Stream_LE(PrinterCommands
, CommandSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
201 while (!(Pipe_IsOUTReady()))
203 if (USB_HostState
== HOST_STATE_Unattached
)
204 return PIPE_RWSTREAM_DeviceDisconnected
;
209 return PIPE_RWSTREAM_NoError
;
212 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
213 char* const DeviceIDString
,
214 const uint16_t BufferSize
)
216 uint8_t ErrorCode
= HOST_SENDCONTROL_Successful
;
217 uint16_t DeviceIDStringLength
= 0;
219 USB_ControlRequest
= (USB_Request_Header_t
)
221 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
222 .bRequest
= REQ_GetDeviceID
,
224 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
225 .wLength
= sizeof(DeviceIDStringLength
),
228 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
230 if ((ErrorCode
= USB_Host_SendControlRequest(&DeviceIDStringLength
)) != HOST_SENDCONTROL_Successful
)
233 if (!(DeviceIDStringLength
))
235 DeviceIDString
[0] = 0x00;
236 return HOST_SENDCONTROL_Successful
;
239 DeviceIDStringLength
= SwapEndian_16(DeviceIDStringLength
);
241 if (DeviceIDStringLength
> BufferSize
)
242 DeviceIDStringLength
= BufferSize
;
244 USB_ControlRequest
.wLength
= DeviceIDStringLength
;
246 if ((ErrorCode
= USB_Host_SendControlRequest(DeviceIDString
)) != HOST_SENDCONTROL_Successful
)
249 memmove(&DeviceIDString
[0], &DeviceIDString
[2], DeviceIDStringLength
- 2);
251 DeviceIDString
[DeviceIDStringLength
- 2] = 0x00;
253 return HOST_SENDCONTROL_Successful
;