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
32 * \brief Host mode driver for the library USB Printer Class driver.
34 * Host mode driver for the library USB Printer Class driver.
36 * \note This file should not be included directly. It is automatically included as needed by the class driver
37 * dispatch header located in LUFA/Drivers/USB/Class/Printer.h.
40 /** \ingroup Group_USBClassPrinter
41 * @defgroup Group_USBClassPrinterHost Printer Class Host Mode Driver
43 * \section Sec_Dependencies Module Source Dependencies
44 * The following files must be built with any user project that uses this module:
45 * - LUFA/Drivers/USB/Class/Host/Printer.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
47 * \section Module Description
48 * Host Mode USB Class driver framework interface, for the Printer USB Class driver.
53 #ifndef __PRINTER_CLASS_HOST_H__
54 #define __PRINTER_CLASS_HOST_H__
57 #include "../../USB.h"
58 #include "../Common/Printer.h"
60 /* Enable C linkage for C++ Compilers: */
61 #if defined(__cplusplus)
65 /* Preprocessor Checks: */
66 #if !defined(__INCLUDE_FROM_PRINTER_DRIVER)
67 #error Do not include this file directly. Include LUFA/Drivers/Class/Printer.h instead.
70 /* Public Interface - May be used in end-application: */
72 /** \brief Printer Class Host Mode Configuration and State Structure.
74 * Class state structure. An instance of this structure should be made within the user application,
75 * and passed to each of the Printer class driver functions as the PRNTInterfaceInfo parameter. This
76 * stores each Printer interface's configuration and state information.
82 uint8_t DataINPipeNumber
; /**< Pipe number of the Printer interface's IN data pipe. */
83 bool DataINPipeDoubleBank
; /**< Indicates if the Printer interface's IN data pipe should use double banking. */
85 uint8_t DataOUTPipeNumber
; /**< Pipe number of the Printer interface's OUT data pipe. */
86 bool DataOUTPipeDoubleBank
; /**< Indicates if the Printer interface's OUT data pipe should use double banking. */
87 } Config
; /**< Config data for the USB class interface within the device. All elements in this section
88 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
92 bool IsActive
; /**< Indicates if the current interface instance is connected to an attached device, valid
93 * after \ref PRNT_Host_ConfigurePipes() is called and the Host state machine is in the
96 uint8_t InterfaceNumber
; /**< Interface index of the Printer interface within the attached device. */
97 uint8_t AlternateSetting
; /**< Alternate setting within the Printer Interface in the attached device. */
99 uint16_t DataINPipeSize
; /**< Size in bytes of the Printer interface's IN data pipe. */
100 uint16_t DataOUTPipeSize
; /**< Size in bytes of the Printer interface's OUT data pipe. */
101 } State
; /**< State data for the USB class interface within the device. All elements in this section
102 * <b>may</b> be set to initial values, but may also be ignored to default to sane values when
103 * the interface is enumerated.
105 } USB_ClassInfo_PRNT_Host_t
;
108 enum PRNTHost_EnumerationFailure_ErrorCodes_t
110 PRNT_ENUMERROR_NoError
= 0, /**< Configuration Descriptor was processed successfully. */
111 PRNT_ENUMERROR_InvalidConfigDescriptor
= 1, /**< The device returned an invalid Configuration Descriptor. */
112 PRNT_ENUMERROR_NoPrinterInterfaceFound
= 2, /**< A compatible Printer interface was not found in the device's Configuration Descriptor. */
113 PRNT_ENUMERROR_EndpointsNotFound
= 3, /**< Compatible Printer endpoints were not found in the device's interfaces. */
116 /* Function Prototypes: */
117 /** Host interface configuration routine, to configure a given Printer host interface instance using the
118 * Configuration Descriptor read from an attached USB device. This function automatically updates the given Printer
119 * instance's state values and configures the pipes required to communicate with the interface if it is found within
120 * the device. This should be called once after the stack has enumerated the attached device, while the host state
121 * machine is in the Addressed state.
123 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
124 * \param[in] ConfigDescriptorSize Length of the attached device's Configuration Descriptor.
125 * \param[in] DeviceConfigDescriptor Pointer to a buffer containing the attached device's Configuration Descriptor.
127 * \return A value from the \ref PRNTHost_EnumerationFailure_ErrorCodes_t enum.
129 uint8_t PRNT_Host_ConfigurePipes(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
130 uint16_t ConfigDescriptorSize
,
131 void* DeviceConfigDescriptor
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);
133 /** Configures the printer to enable Bidirectional mode, if it is not already in this mode. This should be called
134 * once the connected device's configuration has been set, to ensure the printer is ready to accept commands.
136 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
138 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
140 uint8_t PRNT_Host_SetBidirectionalMode(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
) ATTR_NON_NULL_PTR_ARG(1);
142 /** Retrieves the status of the virtual Printer port's inbound status lines. The result can then be masked against the
143 * PRNT_PORTSTATUS_* macros to determine the printer port's status.
145 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
146 * \param[out] PortStatus Location where the retrieved port status should be stored.
148 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
150 uint8_t PRNT_Host_GetPortStatus(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
151 uint8_t* const PortStatus
)
152 ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
154 /** Soft-resets the attached printer, readying it for new commands.
156 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
158 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
160 uint8_t PRNT_Host_SoftReset(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
) ATTR_NON_NULL_PTR_ARG(1);
162 /** Sends the given raw data stream to the attached printer's input endpoint. This should contain commands that the
163 * printer is able to understand - for example, PCL data. Not all printers accept all printer languages; see
164 * \ref PRNT_Host_GetDeviceID() for details on determining acceptable languages for an attached printer.
166 * \pre This function must only be called when the Host state machine is in the HOST_STATE_Configured state or the
169 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
170 * \param[in] Buffer Pointer to a buffer containing the raw command stream to send to the printer.
171 * \param[in] Length Size in bytes of the command stream to be sent.
173 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
175 uint8_t PRNT_Host_SendString(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
176 void* PrinterCommands
,
177 const uint16_t Length
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
179 /** Determines the number of bytes received by the printer interface from the device, waiting to be read. This indicates the number
180 * of bytes in the IN pipe bank only, and thus the number of calls to \ref PRNT_Host_ReceiveByte() which are guaranteed to succeed
181 * immediately. If multiple bytes are to be received, they should be buffered by the user application, as the pipe bank will not be
182 * released back to the USB controller until all bytes are read.
184 * \pre This function must only be called when the Host state machine is in the HOST_STATE_Configured state or the
187 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
189 * \return Total number of buffered bytes received from the device.
191 uint16_t PRNT_Host_BytesReceived(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
);
193 /** Reads a byte of data from the device. If no data is waiting to be read of if a USB device is not connected, the function
194 * returns a negative value. The \ref PRNT_Host_BytesReceived() function may be queried in advance to determine how many bytes
195 * are currently buffered in the Printer interface's data receive pipe.
197 * \pre This function must only be called when the Host state machine is in the HOST_STATE_Configured state or the
200 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
202 * \return Next received byte from the device, or a negative value if no data received.
204 int16_t PRNT_Host_ReceiveByte(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
);
206 /** Retrieves the attached printer device's ID string, formatted according to IEEE 1284. This string is sent as a
207 * Unicode string from the device and is automatically converted to an ASCII encoded C string by this function, thus
208 * the maximum reportable string length is two less than the size given (to accommodate the Unicode string length
209 * bytes which are removed).
211 * This string, when supported, contains the model, manufacturer and acceptable printer languages for the attached device.
213 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
214 * \param[out] DeviceIDString Pointer to a buffer where the Device ID string should be stored, in ASCII format.
215 * \param[in] BufferSize Size in bytes of the buffer allocated for the Device ID string.
217 * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
219 uint8_t PRNT_Host_GetDeviceID(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
,
220 char* const DeviceIDString
,
221 const uint16_t BufferSize
) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
223 /* Inline Functions: */
224 /** General management task for a given Printer host class interface, required for the correct operation of
225 * the interface. This should be called frequently in the main program loop, before the master USB management task
226 * \ref USB_USBTask().
228 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class host configuration and state.
230 static inline void PRNT_Host_USBTask(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
) ATTR_NON_NULL_PTR_ARG(1);
231 static inline void PRNT_Host_USBTask(USB_ClassInfo_PRNT_Host_t
* const PRNTInterfaceInfo
)
233 (void)PRNTInterfaceInfo
;
236 /* Private Interface - For use in library only: */
237 #if !defined(__DOXYGEN__)
239 #define PRINTER_CLASS 0x07
240 #define PRINTER_SUBCLASS 0x01
241 #define PRINTER_PROTOCOL 0x02
243 #define REQ_GetDeviceID 0
244 #define REQ_GetPortStatus 1
245 #define REQ_SoftReset 2
247 #define PRNT_FOUND_DATAPIPE_IN (1 << 0)
248 #define PRNT_FOUND_DATAPIPE_OUT (1 << 1)
250 /* Function Prototypes: */
251 #if defined(__INCLUDE_FROM_PRINTER_CLASS_HOST_C)
252 static uint8_t DCOMP_PRNT_NextPRNTInterface(void* const CurrentDescriptor
) ATTR_NON_NULL_PTR_ARG(1);
253 static uint8_t DCOMP_PRNT_NextPRNTInterfaceEndpoint(void* const CurrentDescriptor
) ATTR_NON_NULL_PTR_ARG(1);
257 /* Disable C linkage for C++ Compilers: */
258 #if defined(__cplusplus)