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 void PRNT_Host_USBTask(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
130 PRNT_Host_Flush(PRNTInterfaceInfo
);
133 uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
135 if (PRNTInterfaceInfo
->State
.AlternateSetting
)
139 USB_ControlRequest
= (USB_Request_Header_t
)
141 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
142 .bRequest
= REQ_SetInterface
,
143 .wValue
= PRNTInterfaceInfo
->State
.AlternateSetting
,
144 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
148 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
150 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
154 return HOST_SENDCONTROL_Successful
;
157 uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
158 uint8_t* const PortStatus
)
160 USB_ControlRequest
= (USB_Request_Header_t
)
162 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
163 .bRequest
= REQ_GetPortStatus
,
165 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
166 .wLength
= sizeof(uint8_t),
169 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
171 return USB_Host_SendControlRequest(PortStatus
);
174 uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
176 USB_ControlRequest
= (USB_Request_Header_t
)
178 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
179 .bRequest
= REQ_SoftReset
,
181 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
185 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
187 return USB_Host_SendControlRequest(NULL
);
190 uint8_t PRNT_Host_Flush(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
192 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
193 return PIPE_READYWAIT_DeviceDisconnected
;
197 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
);
200 if (!(Pipe_BytesInPipe()))
201 return PIPE_READYWAIT_NoError
;
203 bool BankFull
= !(Pipe_IsReadWriteAllowed());
209 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
217 return PIPE_READYWAIT_NoError
;
220 uint8_t PRNT_Host_SendByte(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
223 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
224 return PIPE_READYWAIT_DeviceDisconnected
;
228 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
);
231 if (!(Pipe_IsReadWriteAllowed()))
235 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
239 Pipe_Write_Byte(Data
);
242 return PIPE_READYWAIT_NoError
;
245 uint8_t PRNT_Host_SendString(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
247 const uint16_t Length
)
251 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
252 return PIPE_RWSTREAM_DeviceDisconnected
;
254 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataOUTPipeNumber
);
257 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, Length
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
262 ErrorCode
= Pipe_WaitUntilReady();
269 uint16_t PRNT_Host_BytesReceived(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
271 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
274 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataINPipeNumber
);
277 if (Pipe_IsINReceived())
279 if (!(Pipe_BytesInPipe()))
288 return Pipe_BytesInPipe();
299 int16_t PRNT_Host_ReceiveByte(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
301 if ((USB_HostState
!= HOST_STATE_Configured
) || !(PRNTInterfaceInfo
->State
.IsActive
))
302 return PIPE_RWSTREAM_DeviceDisconnected
;
304 int16_t ReceivedByte
= -1;
306 Pipe_SelectPipe(PRNTInterfaceInfo
->Config
.DataINPipeNumber
);
309 if (Pipe_IsINReceived())
311 if (Pipe_BytesInPipe())
312 ReceivedByte
= Pipe_Read_Byte();
314 if (!(Pipe_BytesInPipe()))
323 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
324 char* const DeviceIDString
,
325 const uint16_t BufferSize
)
327 uint8_t ErrorCode
= HOST_SENDCONTROL_Successful
;
328 uint16_t DeviceIDStringLength
= 0;
330 USB_ControlRequest
= (USB_Request_Header_t
)
332 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
333 .bRequest
= REQ_GetDeviceID
,
335 .wIndex
= PRNTInterfaceInfo
->State
.InterfaceNumber
,
336 .wLength
= sizeof(DeviceIDStringLength
),
339 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
341 if ((ErrorCode
= USB_Host_SendControlRequest(&DeviceIDStringLength
)) != HOST_SENDCONTROL_Successful
)
344 if (!(DeviceIDStringLength
))
346 DeviceIDString
[0] = 0x00;
347 return HOST_SENDCONTROL_Successful
;
350 DeviceIDStringLength
= SwapEndian_16(DeviceIDStringLength
);
352 if (DeviceIDStringLength
> BufferSize
)
353 DeviceIDStringLength
= BufferSize
;
355 USB_ControlRequest
.wLength
= DeviceIDStringLength
;
357 if ((ErrorCode
= USB_Host_SendControlRequest(DeviceIDString
)) != HOST_SENDCONTROL_Successful
)
360 memmove(&DeviceIDString
[0], &DeviceIDString
[2], DeviceIDStringLength
- 2);
362 DeviceIDString
[DeviceIDStringLength
- 2] = 0x00;
364 return HOST_SENDCONTROL_Successful
;