]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Core/XMEGA/Template/Template_Endpoint_Control_R.c
0bd384348561a27a5cf2f8688b67d0bfe42745c5
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Core / XMEGA / Template / Template_Endpoint_Control_R.c
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 #if defined(TEMPLATE_FUNC_NAME)
32
33 uint8_t TEMPLATE_FUNC_NAME (void* const Buffer,
34                             uint16_t Length)
35 {
36         uint8_t* DataStream = ((uint8_t*)Buffer + TEMPLATE_BUFFER_OFFSET(Length));
37
38         Endpoint_SelectEndpoint(USB_Endpoint_SelectedEndpoint & ~ENDPOINT_DIR_IN);
39
40         if (!(Length))
41           Endpoint_ClearOUT();
42
43         while (Length)
44         {
45                 uint8_t USB_DeviceState_LCL = USB_DeviceState;
46
47                 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
48                   return ENDPOINT_RWCSTREAM_DeviceDisconnected;
49                 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
50                   return ENDPOINT_RWCSTREAM_BusSuspended;
51                 else if (Endpoint_IsSETUPReceived())
52                   return ENDPOINT_RWCSTREAM_HostAborted;
53
54                 if (Endpoint_IsOUTReceived())
55                 {
56                         while (Length && Endpoint_BytesInEndpoint())
57                         {
58                                 TEMPLATE_TRANSFER_BYTE(DataStream);
59                                 TEMPLATE_BUFFER_MOVE(DataStream, 1);
60                                 Length--;
61                         }
62
63                         Endpoint_ClearOUT();
64                 }
65         }
66
67         while (!(Endpoint_IsINReady()))
68         {
69                 uint8_t USB_DeviceState_LCL = USB_DeviceState;
70
71                 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
72                   return ENDPOINT_RWCSTREAM_DeviceDisconnected;
73                 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
74                   return ENDPOINT_RWCSTREAM_BusSuspended;
75         }
76
77         return ENDPOINT_RWCSTREAM_NoError;
78 }
79
80 #undef TEMPLATE_BUFFER_OFFSET
81 #undef TEMPLATE_BUFFER_MOVE
82 #undef TEMPLATE_FUNC_NAME
83 #undef TEMPLATE_TRANSFER_BYTE
84
85 #endif
86