1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __USB_DEVICE_PRINTER_H__
10 #define __USB_DEVICE_PRINTER_H__
11 
12 /*!
13  * @addtogroup usb_device_printer_drv
14  * @{
15  */
16 
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 
21 /*! @brief The class code of the printer class */
22 #define USB_DEVICE_CONFIG_PRINTER_CLASS_CODE (0x07U)
23 
24 /*! @brief class-specific request GET_DEVICE_ID */
25 #define USB_DEVICE_PRINTER_GET_DEVICE_ID (0x00U)
26 /*! @brief class-specific request GET_PORT_STATUS */
27 #define USB_DEVICE_PRINTER_GET_PORT_STATUS (0x01U)
28 /*! @brief class-specific request SOFT_RESET */
29 #define USB_DEVICE_PRINTER_SOFT_RESET (0x02U)
30 
31 /*! @brief Paper empty bit mask for GET_PORT_STATUS */
32 #define USB_DEVICE_PRINTER_PORT_STATUS_PAPER_EMPTRY_MASK (0x20U)
33 /*! @brief Select bit mask for GET_PORT_STATUS */
34 #define USB_DEVICE_PRINTER_PORT_STATUS_SELECT_MASK (0x10U)
35 /*! @brief Error bit mask for GET_PORT_STATUS */
36 #define USB_DEVICE_PRINTER_PORT_STATUS_NOT_ERROR_MASK (0x08U)
37 
38 #define USB_DEVICE_PRINTER_PORT_STATUS_DEFAULT_VALUE \
39     (USB_DEVICE_PRINTER_PORT_STATUS_SELECT_MASK | USB_DEVICE_PRINTER_PORT_STATUS_NOT_ERROR_MASK)
40 
41 /*! @brief Available common EVENT types in printer class callback */
42 typedef enum _usb_device_printer_event
43 {
44     kUSB_DevicePrinterEventRecvResponse = 0x01U, /*!< Data received or cancelled etc*/
45     kUSB_DevicePrinterEventSendResponse,         /*!< Data send done or cancelled etc */
46     kUSB_DevicePrinterEventGetDeviceId,          /*!< Get device ID request */
47     kUSB_DevicePrinterEventGetPortStatus,        /*!< Get port status request */
48     kUSB_DevicePrinterEventSoftReset,            /*!< Soft reset request */
49 } usb_device_printer_event_t;
50 
51 typedef struct _usb_device_printer_class_request
52 {
53     uint8_t *buffer;          /*!< The class specific request buffer address */
54     uint32_t length;          /*!< The class specific request buffer length */
55     uint8_t interface;        /*!< The class specific request interface index */
56     uint8_t alternateSetting; /*!< GET_DEVICE_ID request alternate setting */
57     uint8_t configIndex;      /*!< GET_DEVICE_ID request config index */
58 } usb_device_printer_class_request_t;
59 
60 /*! @brief The printer device class instance structure */
61 typedef struct _usb_device_printer_struct
62 {
63     usb_device_handle deviceHandle;                 /*!< The device handle */
64     usb_device_class_config_struct_t *classConfig;  /*!< The configuration of the class. */
65     usb_device_interface_struct_t *interfaceHandle; /*!< Current interface handle */
66     uint8_t *bulkInPipeDataBuffer;                  /*!< IN pipe data buffer backup when stall */
67     uint32_t bulkInPipeDataLen;                     /*!< IN pipe data length backup when stall  */
68     uint8_t *bulkOutPipeDataBuffer;                 /*!< OUT pipe data buffer backup when stall */
69     uint32_t bulkOutPipeDataLen;                    /*!< OUT pipe data length backup when stall  */
70     uint8_t configuration;                          /*!< Current configuration */
71     uint8_t interfaceNumber;                        /*!< Interface number in the device descriptor */
72     uint8_t alternate;                              /*!< Interface alternate value */
73     uint8_t bulkInBusy;                             /*!< BULK IN pipe busy flag */
74     uint8_t bulkOutBusy;                            /*!< BULK OUT pipe busy flag */
75     uint8_t bulkInPipeStall;                        /*!< bulk IN pipe stall flag */
76     uint8_t bulkOutPipeStall;                       /*!< bulk OUT pipe stall flag */
77 } usb_device_printer_struct_t;
78 
79 /*******************************************************************************
80  * API
81  ******************************************************************************/
82 
83 #if defined(__cplusplus)
84 extern "C" {
85 #endif
86 
87 /*!
88  * @brief Initializes the printer class.
89  *
90  * This function is used to initialize the printer class. This function only can be called by #USB_DeviceClassInit.
91  *
92  * @param[in] controllerId   The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
93  * @param[in] config         The class configuration information.
94  * @param[out] handle        A parameter used to return a pointer of the printer class handle to the caller.
95  *
96  * @return A USB error code or kStatus_USB_Success.
97  */
98 extern usb_status_t USB_DevicePrinterInit(uint8_t controllerId,
99                                           usb_device_class_config_struct_t *config,
100                                           class_handle_t *handle);
101 
102 /*!
103  * @brief De-initializes the device printer class.
104  *
105  * The function de-initializes the device printer class. This function only can be called by #USB_DeviceClassDeinit.
106  *
107  * @param[in] handle The printer class handle got from usb_device_class_config_struct_t::classHandle.
108  *
109  * @return A USB error code or kStatus_USB_Success.
110  */
111 extern usb_status_t USB_DevicePrinterDeinit(class_handle_t handle);
112 
113 /*!
114  * @brief Handles the event passed to the printer class.
115  *
116  * This function handles the event passed to the printer class. This function only can be called by
117  * #USB_DeviceClassEvent.
118  *
119  * @param[in] handle          The printer class handle received from the usb_device_class_config_struct_t::classHandle.
120  * @param[in] event           The event codes. See the enumeration usb_device_class_event_t.
121  * @param[in,out] param       The parameter type is determined by the event code.
122  *
123  * @return A USB error code or kStatus_USB_Success.
124  * @retval kStatus_USB_Success              Process event successfully.
125  * @retval kStatus_USB_InvalidHandle        The device handle or parameter is invalid.
126  * @retval kStatus_USB_InvalidRequest       The request is invalid, and the control pipe is stalled by the caller.
127  */
128 extern usb_status_t USB_DevicePrinterEvent(void *handle, uint32_t event, void *param);
129 
130 /*!
131  * @name USB device printer class APIs
132  * @{
133  */
134 
135 /*!
136  * @brief Sends data through a specified endpoint.
137  *
138  * The function is used to send data through a specified endpoint.
139  * The function calls #USB_DeviceSendRequest internally.
140  *
141  * @param[in] handle The printer class handle received from usb_device_class_config_struct_t::classHandle.
142  * @param[in] ep     Endpoint index.
143  * @param[in] buffer The memory address to hold the data need to be sent.
144  * @param[in] length The data length to be sent.
145  *
146  * @return A USB error code or kStatus_USB_Success.
147  *
148  * @note The function can only be called in the same context.
149  *
150  * @note The return value indicates whether the sending request is successful or not.
151  * Currently, only one transfer request can be supported for one specific endpoint.
152  * If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
153  * should implement a queue in the application level.
154  * The subsequent transfer can begin only when the previous transfer is done (a notification is received through the
155  * callback).
156  */
157 extern usb_status_t USB_DevicePrinterSend(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length);
158 
159 /*!
160  * @brief Receives data through a specified endpoint.
161  *
162  * The function is used to receive data through a specified endpoint.
163  * The function calls #USB_DeviceSendRequest internally.
164  *
165  * @param[in] handle The printer class handle received from usb_device_class_config_struct_t::classHandle.
166  * @param[in] ep     Endpoint index.
167  * @param[in] buffer The memory address to hold the data need to be sent.
168  * @param[in] length The data length to be sent.
169  *
170  * @return A USB error code or kStatus_USB_Success.
171  *
172  * @note The function can only be called in the same context.
173  *
174  * @note The return value indicates whether the sending request is successful or not.
175  * Currently, only one transfer request can be supported for one specific endpoint.
176  * If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
177  * should implement a queue in the application level.
178  * The subsequent transfer can begin only when the previous transfer is done (a notification is received through the
179  * callback).
180  */
181 extern usb_status_t USB_DevicePrinterRecv(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length);
182 
183 /*! @}*/
184 
185 #if defined(__cplusplus)
186 }
187 #endif
188 
189 /*! @}*/
190 
191 #endif /* __USB_DEVICE_PRINTER_H__ */
192