3 Copyright (C) Dean Camera, 2020.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2020 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_DEVICE)
36 #define __INCLUDE_FROM_PRINTER_DRIVER
37 #define __INCLUDE_FROM_PRINTER_DEVICE_C
38 #include "PrinterClassDevice.h"
40 void PRNT_Device_ProcessControlRequest(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
)
42 if (!(Endpoint_IsSETUPReceived()))
45 if (USB_ControlRequest
.wIndex
!= PRNTInterfaceInfo
->Config
.InterfaceNumber
)
48 switch (USB_ControlRequest
.bRequest
)
50 case PRNT_REQ_GetDeviceID
:
51 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
53 Endpoint_ClearSETUP();
55 while (!(Endpoint_IsINReady()))
57 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
61 uint16_t IEEEStringLen
= strlen(PRNTInterfaceInfo
->Config
.IEEE1284String
);
62 Endpoint_Write_16_BE(IEEEStringLen
+ sizeof(uint16_t));
63 Endpoint_Write_Control_Stream_LE(PRNTInterfaceInfo
->Config
.IEEE1284String
, IEEEStringLen
);
64 Endpoint_ClearStatusStage();
68 case PRNT_REQ_GetPortStatus
:
69 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
71 Endpoint_ClearSETUP();
73 while (!(Endpoint_IsINReady()))
75 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
79 Endpoint_Write_8(PRNTInterfaceInfo
->State
.PortStatus
);
82 Endpoint_ClearStatusStage();
86 case PRNT_REQ_SoftReset
:
87 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
89 Endpoint_ClearSETUP();
90 Endpoint_ClearStatusStage();
92 PRNTInterfaceInfo
->State
.IsPrinterReset
= true;
94 EVENT_PRNT_Device_SoftReset(PRNTInterfaceInfo
);
101 bool PRNT_Device_ConfigureEndpoints(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
)
103 memset(&PRNTInterfaceInfo
->State
, 0x00, sizeof(PRNTInterfaceInfo
->State
));
104 PRNTInterfaceInfo
->State
.PortStatus
= PRNT_PORTSTATUS_NOTERROR
| PRNT_PORTSTATUS_SELECT
;
106 PRNTInterfaceInfo
->Config
.DataINEndpoint
.Type
= EP_TYPE_BULK
;
107 PRNTInterfaceInfo
->Config
.DataOUTEndpoint
.Type
= EP_TYPE_BULK
;
109 if (!(Endpoint_ConfigureEndpointTable(&PRNTInterfaceInfo
->Config
.DataINEndpoint
, 1)))
112 if (!(Endpoint_ConfigureEndpointTable(&PRNTInterfaceInfo
->Config
.DataOUTEndpoint
, 1)))
118 void PRNT_Device_USBTask(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
)
120 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
123 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
124 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataINEndpoint
.Address
);
126 if (Endpoint_IsINReady())
127 PRNT_Device_Flush(PRNTInterfaceInfo
);
130 if (PRNTInterfaceInfo
->State
.IsPrinterReset
)
132 Endpoint_ResetEndpoint(PRNTInterfaceInfo
->Config
.DataOUTEndpoint
.Address
);
133 Endpoint_ResetEndpoint(PRNTInterfaceInfo
->Config
.DataINEndpoint
.Address
);
135 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataOUTEndpoint
.Address
);
136 Endpoint_ClearStall();
137 Endpoint_ResetDataToggle();
138 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataINEndpoint
.Address
);
139 Endpoint_ClearStall();
140 Endpoint_ResetDataToggle();
142 PRNTInterfaceInfo
->State
.IsPrinterReset
= false;
146 uint8_t PRNT_Device_SendString(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
,
147 const char* const String
)
149 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
150 return ENDPOINT_RWSTREAM_DeviceDisconnected
;
152 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataINEndpoint
.Address
);
153 return Endpoint_Write_Stream_LE(String
, strlen(String
), NULL
);
156 uint8_t PRNT_Device_SendData(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
,
157 const void* const Buffer
,
158 const uint16_t Length
)
160 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
161 return ENDPOINT_RWSTREAM_DeviceDisconnected
;
163 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataINEndpoint
.Address
);
164 return Endpoint_Write_Stream_LE(Buffer
, Length
, NULL
);
167 uint8_t PRNT_Device_SendByte(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
,
170 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
171 return ENDPOINT_RWSTREAM_DeviceDisconnected
;
173 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataINEndpoint
.Address
);
175 if (!(Endpoint_IsReadWriteAllowed()))
181 if ((ErrorCode
= Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError
)
185 Endpoint_Write_8(Data
);
186 return ENDPOINT_READYWAIT_NoError
;
189 uint8_t PRNT_Device_Flush(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
)
191 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
192 return ENDPOINT_RWSTREAM_DeviceDisconnected
;
196 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataINEndpoint
.Address
);
198 if (!(Endpoint_BytesInEndpoint()))
199 return ENDPOINT_READYWAIT_NoError
;
201 bool BankFull
= !(Endpoint_IsReadWriteAllowed());
207 if ((ErrorCode
= Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError
)
213 return ENDPOINT_READYWAIT_NoError
;
216 uint16_t PRNT_Device_BytesReceived(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
)
218 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
221 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataOUTEndpoint
.Address
);
223 if (Endpoint_IsOUTReceived())
225 if (!(Endpoint_BytesInEndpoint()))
232 return Endpoint_BytesInEndpoint();
241 int16_t PRNT_Device_ReceiveByte(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
)
243 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
246 int16_t ReceivedByte
= -1;
248 Endpoint_SelectEndpoint(PRNTInterfaceInfo
->Config
.DataOUTEndpoint
.Address
);
250 if (Endpoint_IsOUTReceived())
252 if (Endpoint_BytesInEndpoint())
253 ReceivedByte
= Endpoint_Read_8();
255 if (!(Endpoint_BytesInEndpoint()))
262 #if defined(FDEV_SETUP_STREAM)
263 void PRNT_Device_CreateStream(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
,
266 *Stream
= (FILE)FDEV_SETUP_STREAM(PRNT_Device_putchar
, PRNT_Device_getchar
, _FDEV_SETUP_RW
);
267 fdev_set_udata(Stream
, PRNTInterfaceInfo
);
270 void PRNT_Device_CreateBlockingStream(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
,
273 *Stream
= (FILE)FDEV_SETUP_STREAM(PRNT_Device_putchar
, PRNT_Device_getchar_Blocking
, _FDEV_SETUP_RW
);
274 fdev_set_udata(Stream
, PRNTInterfaceInfo
);
277 static int PRNT_Device_putchar(char c
,
280 return PRNT_Device_SendByte((USB_ClassInfo_PRNT_Device_t
*)fdev_get_udata(Stream
), c
) ? _FDEV_ERR
: 0;
283 static int PRNT_Device_getchar(FILE* Stream
)
285 int16_t ReceivedByte
= PRNT_Device_ReceiveByte((USB_ClassInfo_PRNT_Device_t
*)fdev_get_udata(Stream
));
287 if (ReceivedByte
< 0)
293 static int PRNT_Device_getchar_Blocking(FILE* Stream
)
295 int16_t ReceivedByte
;
297 while ((ReceivedByte
= PRNT_Device_ReceiveByte((USB_ClassInfo_PRNT_Device_t
*)fdev_get_udata(Stream
))) < 0)
299 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
302 PRNT_Device_USBTask((USB_ClassInfo_PRNT_Device_t
*)fdev_get_udata(Stream
));
310 void PRNT_Device_Event_Stub(USB_ClassInfo_PRNT_Device_t
* const PRNTInterfaceInfo
)