]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Core/XMEGA/Template/Template_Endpoint_RW.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Core / XMEGA / Template / Template_Endpoint_RW.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 (TEMPLATE_BUFFER_TYPE const Buffer,
34                             uint16_t Length,
35                             uint16_t* const BytesProcessed)
36 {
37         uint8_t* DataStream      = ((uint8_t*)Buffer + TEMPLATE_BUFFER_OFFSET(Length));
38         uint16_t BytesInTransfer = 0;
39         uint8_t  ErrorCode;
40
41         if ((ErrorCode = Endpoint_WaitUntilReady()))
42           return ErrorCode;
43
44         if (BytesProcessed != NULL)
45         {
46                 Length -= *BytesProcessed;
47                 TEMPLATE_BUFFER_MOVE(DataStream, *BytesProcessed);
48         }
49
50         while (Length)
51         {
52                 if (!(Endpoint_IsReadWriteAllowed()))
53                 {
54                         TEMPLATE_CLEAR_ENDPOINT();
55
56                         #if !defined(INTERRUPT_CONTROL_ENDPOINT)
57                         USB_USBTask();
58                         #endif
59
60                         if (BytesProcessed != NULL)
61                         {
62                                 *BytesProcessed += BytesInTransfer;
63                                 return ENDPOINT_RWSTREAM_IncompleteTransfer;
64                         }
65
66                         if ((ErrorCode = Endpoint_WaitUntilReady()))
67                           return ErrorCode;
68                 }
69                 else
70                 {
71                         TEMPLATE_TRANSFER_BYTE(DataStream);
72                         TEMPLATE_BUFFER_MOVE(DataStream, 1);
73                         Length--;
74                         BytesInTransfer++;
75                 }
76         }
77
78         return ENDPOINT_RWSTREAM_NoError;
79 }
80
81 #undef TEMPLATE_FUNC_NAME
82 #undef TEMPLATE_BUFFER_TYPE
83 #undef TEMPLATE_TRANSFER_BYTE
84 #undef TEMPLATE_CLEAR_ENDPOINT
85 #undef TEMPLATE_BUFFER_OFFSET
86 #undef TEMPLATE_BUFFER_MOVE
87
88 #endif
89