]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/UC3/Pipe_UC3.c
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / UC3 / Pipe_UC3.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 #include "../../../../Common/Common.h"\r
32 #if (ARCH == ARCH_UC3)\r
33 \r
34 #define  __INCLUDE_FROM_USB_DRIVER\r
35 #include "../USBMode.h"\r
36 \r
37 #if defined(USB_CAN_BE_HOST)\r
38 \r
39 #include "../Pipe.h"\r
40 \r
41 uint8_t USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;\r
42 \r
43 volatile uint32_t USB_Pipe_SelectedPipe = PIPE_CONTROLPIPE;\r
44 volatile uint8_t* USB_Pipe_FIFOPos[PIPE_TOTAL_PIPES];\r
45 \r
46 bool Pipe_ConfigurePipeTable(const USB_Pipe_Table_t* const Table,\r
47                              const uint8_t Entries)\r
48 {\r
49         for (uint8_t i = 0; i < Entries; i++)\r
50         {\r
51                 if (!(Table[i].Address))\r
52                   continue;\r
53         \r
54                 if (!(Pipe_ConfigurePipe(Table[i].Address, Table[i].Type, Table[i].EndpointAddress, Table[i].Size, Table[i].Banks)))\r
55                 {\r
56                         return false;\r
57                 }\r
58         }\r
59         \r
60         return true;\r
61 }\r
62 \r
63 bool Pipe_ConfigurePipe(const uint8_t Address,\r
64                         const uint8_t Type,\r
65                         const uint8_t EndpointAddress,\r
66                         const uint16_t Size,\r
67                         const uint8_t Banks)\r
68 {\r
69         uint8_t Number = (Address & PIPE_EPNUM_MASK);\r
70         uint8_t Token  = (Address & PIPE_DIR_IN) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT;\r
71         \r
72         if (Number >= PIPE_TOTAL_PIPES)\r
73           return false;\r
74 \r
75         if (Type == EP_TYPE_CONTROL)\r
76           Token = PIPE_TOKEN_SETUP;\r
77 \r
78         USB_Pipe_FIFOPos[Number]     = &AVR32_USBB_SLAVE[Number * PIPE_HSB_ADDRESS_SPACE_SIZE];\r
79 \r
80 #if defined(ORDERED_EP_CONFIG)\r
81         Pipe_SelectPipe(Number);\r
82         Pipe_EnablePipe();\r
83 \r
84         (&AVR32_USBB.upcfg0)[Number] = 0;\r
85         (&AVR32_USBB.upcfg0)[Number] = (AVR32_USBB_ALLOC_MASK |\r
86                                         ((uint32_t)Type  << AVR32_USBB_PTYPE_OFFSET)  |\r
87                                         ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |\r
88                                         ((Banks > 1) ? AVR32_USBB_PBK_MASK : 0)       |\r
89                                         Pipe_BytesToEPSizeMask(Size) |\r
90                                         ((uint32_t)Number << AVR32_USBB_PEPNUM_OFFSET));\r
91 \r
92         Pipe_SetInfiniteINRequests();\r
93 \r
94         return Pipe_IsConfigured();\r
95 #else\r
96         for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)\r
97         {\r
98                 uint32_t UPCFG0Temp;\r
99 \r
100                 Pipe_SelectPipe(PNum);\r
101 \r
102                 if (PNum == Number)\r
103                 {\r
104                         UPCFG0Temp = (AVR32_USBB_ALLOC_MASK |\r
105                                       ((uint32_t)Type  << AVR32_USBB_PTYPE_OFFSET)  |\r
106                                       ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |\r
107                                       ((Banks > 1) ? AVR32_USBB_PBK_MASK : 0)       |\r
108                                       Pipe_BytesToEPSizeMask(Size) |\r
109                                       ((EndpointAddress & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));\r
110                 }\r
111                 else\r
112                 {\r
113                         UPCFG0Temp = (&AVR32_USBB.upcfg0)[PNum];\r
114                 }\r
115 \r
116                 if (!(UPCFG0Temp & AVR32_USBB_ALLOC_MASK))\r
117                   continue;\r
118 \r
119                 Pipe_DisablePipe();\r
120                 (&AVR32_USBB.upcfg0)[PNum] &= ~AVR32_USBB_ALLOC_MASK;\r
121 \r
122                 Pipe_EnablePipe();\r
123                 (&AVR32_USBB.upcfg0)[PNum] = UPCFG0Temp;\r
124 \r
125                 Pipe_SetInfiniteINRequests();\r
126 \r
127                 if (!(Pipe_IsConfigured()))\r
128                   return false;\r
129         }\r
130 \r
131         Pipe_SelectPipe(Number);\r
132         return true;\r
133 #endif\r
134 }\r
135 \r
136 void Pipe_ClearPipes(void)\r
137 {\r
138         for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)\r
139         {\r
140                 Pipe_SelectPipe(PNum);\r
141                 (&AVR32_USBB.upcfg0)[PNum]    = 0;\r
142                 (&AVR32_USBB.upcon0clr)[PNum] = -1;\r
143                 USB_Pipe_FIFOPos[PNum]        = &AVR32_USBB_SLAVE[PNum * 0x10000];\r
144                 Pipe_DisablePipe();\r
145         }\r
146 }\r
147 \r
148 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)\r
149 {\r
150         uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();\r
151 \r
152         for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)\r
153         {\r
154                 Pipe_SelectPipe(PNum);\r
155 \r
156                 if (!(Pipe_IsConfigured()))\r
157                   continue;\r
158 \r
159                 if (Pipe_GetBoundEndpointAddress() == EndpointAddress)\r
160                   return true;\r
161         }\r
162 \r
163         Pipe_SelectPipe(PrevPipeNumber);\r
164         return false;\r
165 }\r
166 \r
167 uint8_t Pipe_WaitUntilReady(void)\r
168 {\r
169         #if (USB_STREAM_TIMEOUT_MS < 0xFF)\r
170         uint8_t  TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
171         #else\r
172         uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
173         #endif\r
174 \r
175         uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();\r
176 \r
177         for (;;)\r
178         {\r
179                 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)\r
180                 {\r
181                         if (Pipe_IsINReceived())\r
182                           return PIPE_READYWAIT_NoError;\r
183                 }\r
184                 else\r
185                 {\r
186                         if (Pipe_IsOUTReady())\r
187                           return PIPE_READYWAIT_NoError;\r
188                 }\r
189 \r
190                 if (Pipe_IsStalled())\r
191                   return PIPE_READYWAIT_PipeStalled;\r
192                 else if (USB_HostState == HOST_STATE_Unattached)\r
193                   return PIPE_READYWAIT_DeviceDisconnected;\r
194 \r
195                 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();\r
196 \r
197                 if (CurrentFrameNumber != PreviousFrameNumber)\r
198                 {\r
199                         PreviousFrameNumber = CurrentFrameNumber;\r
200 \r
201                         if (!(TimeoutMSRem--))\r
202                           return PIPE_READYWAIT_Timeout;\r
203                 }\r
204         }\r
205 }\r
206 \r
207 #endif\r
208 \r
209 #endif\r