]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/UC3/Pipe_UC3.c
Change TOP_DIR to TMK_DIR in makefiles
[tmk_keyboard.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / UC3 / Pipe_UC3.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 #include "../../../../Common/Common.h"
32 #if (ARCH == ARCH_UC3)
33
34 #define __INCLUDE_FROM_USB_DRIVER
35 #include "../USBMode.h"
36
37 #if defined(USB_CAN_BE_HOST)
38
39 #include "../Pipe.h"
40
41 uint8_t USB_Host_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
42
43 volatile uint32_t USB_Pipe_SelectedPipe = PIPE_CONTROLPIPE;
44 volatile uint8_t* USB_Pipe_FIFOPos[PIPE_TOTAL_PIPES];
45
46 bool Pipe_ConfigurePipeTable(const USB_Pipe_Table_t* const Table,
47 const uint8_t Entries)
48 {
49 for (uint8_t i = 0; i < Entries; i++)
50 {
51 if (!(Table[i].Address))
52 continue;
53
54 if (!(Pipe_ConfigurePipe(Table[i].Address, Table[i].Type, Table[i].EndpointAddress, Table[i].Size, Table[i].Banks)))
55 {
56 return false;
57 }
58 }
59
60 return true;
61 }
62
63 bool Pipe_ConfigurePipe(const uint8_t Address,
64 const uint8_t Type,
65 const uint8_t EndpointAddress,
66 const uint16_t Size,
67 const uint8_t Banks)
68 {
69 uint8_t Number = (Address & PIPE_EPNUM_MASK);
70 uint8_t Token = (Address & PIPE_DIR_IN) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT;
71
72 if (Number >= PIPE_TOTAL_PIPES)
73 return false;
74
75 if (Type == EP_TYPE_CONTROL)
76 Token = PIPE_TOKEN_SETUP;
77
78 USB_Pipe_FIFOPos[Number] = &AVR32_USBB_SLAVE[Number * PIPE_HSB_ADDRESS_SPACE_SIZE];
79
80 #if defined(ORDERED_EP_CONFIG)
81 Pipe_SelectPipe(Number);
82 Pipe_EnablePipe();
83
84 (&AVR32_USBB.upcfg0)[Number] = 0;
85 (&AVR32_USBB.upcfg0)[Number] = (AVR32_USBB_ALLOC_MASK |
86 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
87 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
88 ((Banks > 1) ? AVR32_USBB_PBK_MASK : 0) |
89 Pipe_BytesToEPSizeMask(Size) |
90 ((uint32_t)Number << AVR32_USBB_PEPNUM_OFFSET));
91
92 Pipe_SetInfiniteINRequests();
93
94 return Pipe_IsConfigured();
95 #else
96 for (uint8_t PNum = Number; PNum < PIPE_TOTAL_PIPES; PNum++)
97 {
98 uint32_t UPCFG0Temp;
99
100 Pipe_SelectPipe(PNum);
101
102 if (PNum == Number)
103 {
104 UPCFG0Temp = (AVR32_USBB_ALLOC_MASK |
105 ((uint32_t)Type << AVR32_USBB_PTYPE_OFFSET) |
106 ((uint32_t)Token << AVR32_USBB_PTOKEN_OFFSET) |
107 ((Banks > 1) ? AVR32_USBB_PBK_MASK : 0) |
108 Pipe_BytesToEPSizeMask(Size) |
109 ((EndpointAddress & PIPE_EPNUM_MASK) << AVR32_USBB_PEPNUM_OFFSET));
110 }
111 else
112 {
113 UPCFG0Temp = (&AVR32_USBB.upcfg0)[PNum];
114 }
115
116 if (!(UPCFG0Temp & AVR32_USBB_ALLOC_MASK))
117 continue;
118
119 Pipe_DisablePipe();
120 (&AVR32_USBB.upcfg0)[PNum] &= ~AVR32_USBB_ALLOC_MASK;
121
122 Pipe_EnablePipe();
123 (&AVR32_USBB.upcfg0)[PNum] = UPCFG0Temp;
124
125 Pipe_SetInfiniteINRequests();
126
127 if (!(Pipe_IsConfigured()))
128 return false;
129 }
130
131 Pipe_SelectPipe(Number);
132 return true;
133 #endif
134 }
135
136 void Pipe_ClearPipes(void)
137 {
138 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
139 {
140 Pipe_SelectPipe(PNum);
141 (&AVR32_USBB.upcfg0)[PNum] = 0;
142 (&AVR32_USBB.upcon0clr)[PNum] = -1;
143 USB_Pipe_FIFOPos[PNum] = &AVR32_USBB_SLAVE[PNum * 0x10000];
144 Pipe_DisablePipe();
145 }
146 }
147
148 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
149 {
150 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
151
152 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
153 {
154 Pipe_SelectPipe(PNum);
155
156 if (!(Pipe_IsConfigured()))
157 continue;
158
159 if (Pipe_GetBoundEndpointAddress() == EndpointAddress)
160 return true;
161 }
162
163 Pipe_SelectPipe(PrevPipeNumber);
164 return false;
165 }
166
167 uint8_t Pipe_WaitUntilReady(void)
168 {
169 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
170 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
171 #else
172 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
173 #endif
174
175 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
176
177 for (;;)
178 {
179 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
180 {
181 if (Pipe_IsINReceived())
182 return PIPE_READYWAIT_NoError;
183 }
184 else
185 {
186 if (Pipe_IsOUTReady())
187 return PIPE_READYWAIT_NoError;
188 }
189
190 if (Pipe_IsStalled())
191 return PIPE_READYWAIT_PipeStalled;
192 else if (USB_HostState == HOST_STATE_Unattached)
193 return PIPE_READYWAIT_DeviceDisconnected;
194
195 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
196
197 if (CurrentFrameNumber != PreviousFrameNumber)
198 {
199 PreviousFrameNumber = CurrentFrameNumber;
200
201 if (!(TimeoutMSRem--))
202 return PIPE_READYWAIT_Timeout;
203 }
204 }
205 }
206
207 #endif
208
209 #endif
Imprint / Impressum