Fix nasty bug in USBInterrupt.c which would cause a lockup if the control endpoint...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / Events.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 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
28 this software.
29 */
30
31 /** \file
32 * \brief USB controller events manager.
33 *
34 * This file contains macros and functions relating to the management of library events, which are small
35 * pieces of code similar to ISRs which are run when a given condition is met. Each event can be fired from
36 * multiple places in the user or library code, which may or may not be inside an ISR, thus each handler
37 * should be written to be as small and fast as possible to prevent possible problems.
38 *
39 * Events can be hooked by the user application by declaring a handler function with the same name and parameters
40 * listed here. If an event with no user-associated handler is fired within the library, it by default maps to an
41 * internal empty stub function.
42 *
43 * Each event must only have one associated event handler, but can be raised by multiple sources by calling the
44 * event handler function (with any required event parameters).
45 *
46 * \note This file should not be included directly. It is automatically included as needed by the USB driver
47 * dispatch header located in LUFA/Drivers/USB/USB.h.
48 */
49
50 /** \ingroup Group_USB
51 * @defgroup Group_Events USB Events
52 *
53 * This module contains macros and functions relating to the management of library events, which are small
54 * pieces of code similar to ISRs which are run when a given condition is met. Each event can be fired from
55 * multiple places in the user or library code, which may or may not be inside an ISR, thus each handler
56 * should be written to be as small and fast as possible to prevent possible problems.
57 *
58 * Events can be hooked by the user application by declaring a handler function with the same name and parameters
59 * listed here. If an event with no user-associated handler is fired within the library, it by default maps to an
60 * internal empty stub function.
61 *
62 * Each event must only have one associated event handler, but can be raised by multiple sources by calling the
63 * event handler function (with any required event parameters).
64 *
65 * @{
66 */
67
68 #ifndef __USBEVENTS_H__
69 #define __USBEVENTS_H__
70
71 /* Includes: */
72 #include <avr/io.h>
73
74 #include "../../../Common/Common.h"
75 #include "USBMode.h"
76
77 /* Enable C linkage for C++ Compilers: */
78 #if defined(__cplusplus)
79 extern "C" {
80 #endif
81
82 /* Preprocessor Checks: */
83 #if !defined(__INCLUDE_FROM_USB_DRIVER)
84 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
85 #endif
86
87 /* Public Interface - May be used in end-application: */
88 /* Pseudo-Functions for Doxygen: */
89 #if !defined(__INCLUDE_FROM_EVENTS_C) || defined(__DOXYGEN__)
90 /** Event for USB stack initialization failure. This event fires when the USB interface fails to
91 * initialize correctly due to a hardware or software fault.
92 *
93 * \note This event only exists on USB AVR models which support dual role modes.
94 *
95 * \param[in] ErrorCode Error code indicating the failure reason, a value in \ref USB_InitErrorCodes_t
96 */
97 void EVENT_USB_InitFailure(const uint8_t ErrorCode);
98
99 /** Event for USB mode pin level change. This event fires when the USB interface is set to dual role
100 * mode, and the UID pin level has changed to indicate a new mode (device or host). This event fires
101 * before the mode is switched to the newly indicated mode but after the \ref EVENT_USB_Device_Disconnect
102 * event has fired (if connected before the role change).
103 *
104 * \note This event only exists on USB AVR models which support dual role modes.
105 * \n\n
106 *
107 * \note This event does not exist if the USB_DEVICE_ONLY or USB_HOST_ONLY tokens have been supplied
108 * to the compiler (see \ref Group_USBManagement documentation).
109 */
110 void EVENT_USB_UIDChange(void);
111
112 /** Event for USB host error. This event fires when a hardware fault has occurred whilst the USB
113 * interface is in host mode.
114 *
115 * \param[in] ErrorCode Error code indicating the failure reason, a value in \ref USB_Host_ErrorCodes_t
116 *
117 * \note This event only exists on USB AVR models which supports host mode.
118 * \n\n
119 *
120 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
121 * \ref Group_USBManagement documentation).
122 */
123 void EVENT_USB_Host_HostError(const uint8_t ErrorCode);
124
125 /** Event for USB device attachment. This event fires when a the USB interface is in host mode, and
126 * a USB device has been connected to the USB interface. This is interrupt driven, thus fires before
127 * the standard \ref EVENT_USB_Device_Connect() event and so can be used to programmatically start the USB
128 * management task to reduce CPU consumption.
129 *
130 * \note This event only exists on USB AVR models which supports host mode.
131 * \n\n
132 *
133 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
134 * \ref Group_USBManagement documentation).
135 *
136 * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
137 */
138 void EVENT_USB_Host_DeviceAttached(void);
139
140 /** Event for USB device removal. This event fires when a the USB interface is in host mode, and
141 * a USB device has been removed the USB interface whether or not it has been enumerated. This
142 * can be used to programmatically stop the USB management task to reduce CPU consumption.
143 *
144 * \note This event only exists on USB AVR models which supports host mode.
145 * \n\n
146 *
147 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
148 * \ref Group_USBManagement documentation).
149 *
150 * \see \ref USB_USBTask() for more information on the USB management task and reducing CPU usage.
151 */
152 void EVENT_USB_Host_DeviceUnattached(void);
153
154 /** Event for USB device enumeration failure. This event fires when a the USB interface is
155 * in host mode, and an attached USB device has failed to enumerate completely.
156 *
157 * \param[in] ErrorCode Error code indicating the failure reason, a value in
158 * \ref USB_Host_EnumerationErrorCodes_t
159 *
160 * \param[in] SubErrorCode Sub error code indicating the reason for failure - for example, if the
161 * ErrorCode parameter indicates a control error, this will give the error
162 * code returned by the \ref USB_Host_SendControlRequest() function.
163 *
164 * \note This event only exists on USB AVR models which supports host mode.
165 * \n\n
166 *
167 * \note This event does not exist if the USB_DEVICE_ONLY token is supplied to the compiler (see
168 * \ref Group_USBManagement documentation).
169 */
170 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode);
171
172 /** Event for USB device enumeration completion. This event fires when a the USB interface is
173 * in host mode and an attached USB device has been completely enumerated and is ready to be
174 * controlled by the user application.
175 *
176 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
177 * 1 second) when a transaction is waiting to be processed by the device will prevent break communications
178 * and cause the host to reset the USB bus.
179 */
180 void EVENT_USB_Host_DeviceEnumerationComplete(void);
181
182 /** Event for USB device connection. This event fires when the AVR in device mode and the device is connected
183 * to a host, beginning the enumeration process, measured by a rising level on the AVR's VBUS pin.
184 *
185 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
186 * two seconds) will prevent the device from enumerating correctly.
187 *
188 * \note For the smaller series 2 USB AVRs with limited USB controllers, VBUS is not available to the USB controller.
189 * this means that the current connection state is derived from the bus suspension and wake up events by default,
190 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
191 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
192 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
193 * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
194 * \n\n
195 *
196 * \note This event may fire multiple times during device enumeration on the series 2 USB AVRs with limited USB controllers
197 * if NO_LIMITED_CONTROLLER_CONNECT is not defined.
198 *
199 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
200 */
201 void EVENT_USB_Device_Connect(void);
202
203 /** Event for USB device disconnection. This event fires when the AVR in device mode and the device is disconnected
204 * from a host, measured by a falling level on the AVR's VBUS pin.
205 *
206 * \note For the smaller series 2 USB AVRs with limited USB controllers, VBUS is not available to the USB controller.
207 * this means that the current connection state is derived from the bus suspension and wake up events by default,
208 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
209 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
210 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
211 * and disconnection events may be manually fired, and the \ref USB_DeviceState global changed manually.
212 * \n\n
213 *
214 * \note This event may fire multiple times during device enumeration on the series 2 USB AVRs with limited USB controllers
215 * if NO_LIMITED_CONTROLLER_CONNECT is not defined.
216 *
217 * \see USBTask.h for more information on the USB management task and reducing CPU usage.
218 */
219 void EVENT_USB_Device_Disconnect(void);
220
221 /** Event for unhandled control requests. This event fires when a the USB host issues a control
222 * request to the control endpoint (address 0) that the library does not handle. This may either
223 * be a standard request that the library has no handler code for, or a class specific request
224 * issued to the device which must be handled appropriately.
225 *
226 * This event is time-critical; each packet within the request transaction must be acknowledged or
227 * sent within 50ms or the host will abort the transfer.
228 *
229 * The library interally handles all standard control requests with the exceptions of SYNC FRAME,
230 * SET DESCRIPTOR and SET INTERFACE. These and all other non-standard control requests will be left
231 * for the user to process via this event if desired. If not handled in the user application, requests
232 * are automatically STALLed.
233 *
234 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
235 * \ref Group_USBManagement documentation).
236 * \n\n
237 *
238 * \note Requests should be handled in the same manner as described in the USB 2.0 Specification,
239 * or appropriate class specification. In all instances, the library has already read the
240 * request SETUP parameters into the \ref USB_ControlRequest structure which should then be used
241 * by the application to determine how to handle the issued request.
242 */
243 void EVENT_USB_Device_UnhandledControlRequest(void);
244
245 /** Event for USB configuration number changed. This event fires when a the USB host changes the
246 * selected configuration number while in device mode. This event should be hooked in device
247 * applications to create the endpoints and configure the device for the selected configuration.
248 *
249 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
250 * one second) will prevent the device from enumerating correctly.
251 *
252 * This event fires after the value of \ref USB_ConfigurationNumber has been changed.
253 *
254 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
255 * \ref Group_USBManagement documentation).
256 */
257 void EVENT_USB_Device_ConfigurationChanged(void);
258
259 /** Event for USB suspend. This event fires when a the USB host suspends the device by halting its
260 * transmission of Start Of Frame pulses to the device. This is generally hooked in order to move
261 * the device over to a low power state until the host wakes up the device. If the USB interface is
262 * enumerated with the \ref USB_OPT_AUTO_PLL option set, the library will automatically suspend the
263 * USB PLL before the event is fired to save power.
264 *
265 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
266 * \ref Group_USBManagement documentation).
267 * \n\n
268 *
269 * \note This event does not exist on the series 2 USB AVRs when the NO_LIMITED_CONTROLLER_CONNECT
270 * compile time token is not set - see \ref EVENT_USB_Device_Disconnect.
271 *
272 * \see \ref EVENT_USB_Device_WakeUp() event for accompanying Wake Up event.
273 */
274 void EVENT_USB_Device_Suspend(void);
275
276 /** Event for USB wake up. This event fires when a the USB interface is suspended while in device
277 * mode, and the host wakes up the device by supplying Start Of Frame pulses. This is generally
278 * hooked to pull the user application out of a low power state and back into normal operating
279 * mode. If the USB interface is enumerated with the \ref USB_OPT_AUTO_PLL option set, the library
280 * will automatically restart the USB PLL before the event is fired.
281 *
282 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
283 * \ref Group_USBManagement documentation).
284 * \n\n
285 *
286 * \note This event does not exist on the series 2 USB AVRs when the NO_LIMITED_CONTROLLER_CONNECT
287 * compile time token is not set - see \ref EVENT_USB_Device_Connect.
288 *
289 * \see \ref EVENT_USB_Device_Suspend() event for accompanying Suspend event.
290 */
291 void EVENT_USB_Device_WakeUp(void);
292
293 /** Event for USB interface reset. This event fires when the USB interface is in device mode, and
294 * a the USB host requests that the device reset its interface. This event fires after the control
295 * endpoint has been automatically configured by the library.
296 *
297 * This event is time-critical; exceeding OS-specific delays within this event handler (typically of around
298 * two seconds) will prevent the device from enumerating correctly.
299 *
300 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
301 * \ref Group_USBManagement documentation).
302 */
303 void EVENT_USB_Device_Reset(void);
304
305 /** Event for USB Start Of Frame detection, when enabled. This event fires at the start of each USB
306 * frame, once per millisecond, and is synchronized to the USB bus. This can be used as an accurate
307 * millisecond timer source when the USB bus is enumerated in device mode to a USB host.
308 *
309 * This event is time-critical; it is run once per millisecond and thus long handlers will significantly
310 * degrade device performance. This event should only be enabled when needed to reduce device wakeups.
311 *
312 * \note This event is not normally active - it must be manually enabled and disabled via the
313 * \ref USB_Device_EnableSOFEvents() and \ref USB_Device_DisableSOFEvents() commands after enumeration.
314 * \n\n
315 *
316 * \note This event does not exist if the USB_HOST_ONLY token is supplied to the compiler (see
317 * \ref Group_USBManagement documentation).
318 */
319 void EVENT_USB_Device_StartOfFrame(void);
320 #endif
321
322 /* Private Interface - For use in library only: */
323 #if !defined(__DOXYGEN__)
324 /* Function Prototypes: */
325 #if defined(__INCLUDE_FROM_EVENTS_C)
326 void USB_Event_Stub(void) ATTR_CONST;
327
328 #if defined(USB_CAN_BE_BOTH)
329 void EVENT_USB_InitFailure(const uint8_t ErrorCode) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
330 void EVENT_USB_UIDChange(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
331 #endif
332
333 #if defined(USB_CAN_BE_HOST)
334 void EVENT_USB_Host_HostError(const uint8_t ErrorCode) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
335 void EVENT_USB_Host_DeviceAttached(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
336 void EVENT_USB_Host_DeviceUnattached(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
337 void EVENT_USB_Host_DeviceEnumerationComplete(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
338 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
339 ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
340 #endif
341
342 #if defined(USB_CAN_BE_DEVICE)
343 void EVENT_USB_Device_Connect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
344 void EVENT_USB_Device_Disconnect(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
345 void EVENT_USB_Device_UnhandledControlRequest(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
346 void EVENT_USB_Device_ConfigurationChanged(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
347 void EVENT_USB_Device_Suspend(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
348 void EVENT_USB_Device_WakeUp(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
349 void EVENT_USB_Device_Reset(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
350 void EVENT_USB_Device_StartOfFrame(void) ATTR_WEAK ATTR_ALIAS(USB_Event_Stub);
351 #endif
352 #endif
353 #endif
354
355 /* Disable C linkage for C++ Compilers: */
356 #if defined(__cplusplus)
357 }
358 #endif
359
360 #endif
361
362 /** @} */