]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/LUFA/Drivers/USB/Class/Device/PrinterClassDevice.h
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Class / Device / PrinterClassDevice.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 * \brief Device mode driver for the library USB Printer Class driver.
33 *
34 * Device mode driver for the library USB Printer Class driver.
35 *
36 * \note This file should not be included directly. It is automatically included as needed by the USB module driver
37 * dispatch header located in LUFA/Drivers/USB.h.
38 */
39
40 /** \ingroup Group_USBClassPrinter
41 * \defgroup Group_USBClassPrinterDevice Printer Class Device Mode Driver
42 *
43 * \section Sec_USBClassPrinterDevice_Dependencies Module Source Dependencies
44 * The following files must be built with any user project that uses this module:
45 * - LUFA/Drivers/USB/Class/Device/PrinterClassDevice.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
46 *
47 * \section Sec_USBClassPrinterDevice_ModDescription Module Description
48 * Device Mode USB Class driver framework interface, for the Printer USB Class driver.
49 *
50 * @{
51 */
52
53 #ifndef _PRINTER_CLASS_DEVICE_H_
54 #define _PRINTER_CLASS_DEVICE_H_
55
56 /* Includes: */
57 #include "../../USB.h"
58 #include "../Common/PrinterClassCommon.h"
59
60 #include <stdio.h>
61
62 /* Enable C linkage for C++ Compilers: */
63 #if defined(__cplusplus)
64 extern "C" {
65 #endif
66
67 /* Preprocessor Checks: */
68 #if !defined(__INCLUDE_FROM_PRINTER_DRIVER)
69 #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
70 #endif
71
72 /* Public Interface - May be used in end-application: */
73 /* Type Defines: */
74 /** \brief Printer Class Device Mode Configuration and State Structure.
75 *
76 * Class state structure. An instance of this structure should be made for each Printer interface
77 * within the user application, and passed to each of the Printer class driver functions as the
78 * PRNTInterfaceInfo parameter. This stores each Printer interface's configuration and state information.
79 */
80 typedef struct
81 {
82 struct
83 {
84 uint8_t InterfaceNumber; /**< Interface number of the Printer interface within the device. */
85
86 USB_Endpoint_Table_t DataINEndpoint; /**< Data IN endpoint configuration table. */
87 USB_Endpoint_Table_t DataOUTEndpoint; /**< Data OUT endpoint configuration table. */
88
89 char* IEEE1284String; /**< IEEE 1284 identification string, sent to the host during enumeration
90 * to identify the printer model, manufacturer and other characteristics.
91 */
92 } Config; /**< Config data for the USB class interface within the device. All elements in this section
93 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
94 */
95 struct
96 {
97 uint8_t PortStatus; /**< Current status of the Printer virtual port, a collection of \c PRNT_PORTSTATUS_*
98 * bitmask values.
99 */
100
101 volatile bool IsPrinterReset; /**< Flag indicating that the host has requested that the Printer interface be reset
102 * and that all current Mass Storage operations should immediately abort.
103 */
104 } State; /**< State data for the USB class interface within the device. All elements in this section
105 * are reset to their defaults when the interface is enumerated.
106 */
107 } USB_ClassInfo_PRNT_Device_t;
108
109 /* Function Prototypes: */
110 /** Configures the endpoints of a given Printer interface, ready for use. This should be linked to the library
111 * \ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration containing
112 * the given Printer interface is selected.
113 *
114 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
115 *
116 * \return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
117 */
118 bool PRNT_Device_ConfigureEndpoints(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
119
120 /** Processes incoming control requests from the host, that are directed to the given Printer class interface. This should be
121 * linked to the library \ref EVENT_USB_Device_ControlRequest() event.
122 *
123 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
124 */
125 void PRNT_Device_ProcessControlRequest(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
126
127 /** General management task for a given Printer class interface, required for the correct operation of the interface. This should
128 * be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
129 *
130 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
131 */
132 void PRNT_Device_USBTask(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
133
134 /** Printer class driver event for a soft reset request on a Printer interface. This event fires each time the host
135 * requests a reset of the printer interface's internal state, and may be hooked in the user program by declaring a
136 * handler function with the same name and parameters listed here.
137 *
138 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
139 */
140 void EVENT_PRNT_Device_SoftReset(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
141
142 /** Sends a given data buffer to the attached USB host, if connected. If a host is not connected when the function is
143 * called, the string is discarded. Bytes will be queued for transmission to the host until either the endpoint bank
144 * becomes full, or the \ref PRNT_Device_Flush() function is called to flush the pending data to the host. This allows
145 * for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
146 *
147 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
148 * the call will fail.
149 *
150 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
151 * \param[in] Buffer Pointer to a buffer containing the data to send to the device.
152 * \param[in] Length Length of the data to send to the host.
153 *
154 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
155 */
156 uint8_t PRNT_Device_SendData(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
157 const void* const Buffer,
158 const uint16_t Length) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
159
160 /** Sends a given null terminated string to the attached USB host, if connected. If a host is not connected when
161 * the function is called, the string is discarded. Bytes will be queued for transmission to the host until either
162 * the endpoint bank becomes full, or the \ref PRNT_Device_Flush() function is called to flush the pending data to
163 * the host. This allows for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
164 *
165 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
166 * the call will fail.
167 *
168 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
169 * \param[in] String Pointer to the null terminated string to send to the host.
170 *
171 * \return A value from the \ref Endpoint_Stream_RW_ErrorCodes_t enum.
172 */
173 uint8_t PRNT_Device_SendString(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
174 const char* const String) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
175
176 /** Sends a given byte to the attached USB host, if connected. If a host is not connected when the function is called, the
177 * byte is discarded. Bytes will be queued for transmission to the host until either the endpoint bank becomes full, or the
178 * \ref PRNT_Device_Flush() function is called to flush the pending data to the host. This allows for multiple bytes to be
179 * packed into a single endpoint packet, increasing data throughput.
180 *
181 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
182 * the call will fail.
183 *
184 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
185 * \param[in] Data Byte of data to send to the host.
186 *
187 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
188 */
189 uint8_t PRNT_Device_SendByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
190 const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1);
191
192 /** Determines the number of bytes received by the Printer interface from the host, waiting to be read. This indicates the number
193 * of bytes in the OUT endpoint bank only, and thus the number of calls to \ref PRNT_Device_ReceiveByte() which are guaranteed to
194 * succeed immediately. If multiple bytes are to be received, they should be buffered by the user application, as the endpoint
195 * bank will not be released back to the USB controller until all bytes are read.
196 *
197 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
198 * the call will fail.
199 *
200 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
201 *
202 * \return Total number of buffered bytes received from the host.
203 */
204 uint16_t PRNT_Device_BytesReceived(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
205
206 /** Reads a byte of data from the host. If no data is waiting to be read of if a USB host is not connected, the function
207 * returns a negative value. The \ref PRNT_Device_BytesReceived() function may be queried in advance to determine how many
208 * bytes are currently buffered in the Printer interface's data receive endpoint bank, and thus how many repeated calls to this
209 * function which are guaranteed to succeed.
210 *
211 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
212 * the call will fail.
213 *
214 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
215 *
216 * \return Next received byte from the host, or a negative value if no data received.
217 */
218 int16_t PRNT_Device_ReceiveByte(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
219
220 /** Flushes any data waiting to be sent, ensuring that the send buffer is cleared.
221 *
222 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
223 * the call will fail.
224 *
225 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
226 *
227 * \return A value from the \ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
228 */
229 uint8_t PRNT_Device_Flush(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
230
231 #if defined(FDEV_SETUP_STREAM) || defined(__DOXYGEN__)
232 /** Creates a standard character stream for the given Printer Device instance so that it can be used with all the regular
233 * functions in the standard <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf()). The created
234 * stream is bidirectional and can be used for both input and output functions.
235 *
236 * Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
237 * fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
238 * be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
239 * line buffering.
240 *
241 * \note The created stream can be given as \c stdout if desired to direct the standard output from all \c <stdio.h> functions
242 * to the given Printer interface.
243 * \n\n
244 *
245 * \note This function is not available on all microcontroller architectures.
246 *
247 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
248 * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed.
249 */
250 void PRNT_Device_CreateStream(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
251 FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
252
253 /** Identical to \ref PRNT_Device_CreateStream(), except that reads are blocking until the calling stream function terminates
254 * the transfer. While blocking, the USB and Printer service tasks are called repeatedly to maintain USB communications.
255 *
256 * \note This function is not available on all microcontroller architectures.
257 *
258 * \param[in,out] PRNTInterfaceInfo Pointer to a structure containing a Printer Class configuration and state.
259 * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed.
260 */
261 void PRNT_Device_CreateBlockingStream(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo,
262 FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
263 #endif
264
265 /* Private Interface - For use in library only: */
266 #if !defined(__DOXYGEN__)
267 /* Function Prototypes: */
268 #if defined(__INCLUDE_FROM_PRINTER_DEVICE_C)
269 #if defined(FDEV_SETUP_STREAM)
270 static int PRNT_Device_putchar(char c,
271 FILE* Stream) ATTR_NON_NULL_PTR_ARG(2);
272 static int PRNT_Device_getchar(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
273 static int PRNT_Device_getchar_Blocking(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
274 #endif
275
276 void PRNT_Device_Event_Stub(void) ATTR_CONST;
277
278 void EVENT_PRNT_Device_SoftReset(USB_ClassInfo_PRNT_Device_t* const PRNTInterfaceInfo)
279 ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(PRNT_Device_Event_Stub);
280
281 #endif
282
283 #endif
284
285 /* Disable C linkage for C++ Compilers: */
286 #if defined(__cplusplus)
287 }
288 #endif
289
290 #endif
291
292 /** @} */
293
Imprint / Impressum