Fixed minor issue with the RNDISEthernet demo DHCP protocol decoder routine using...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / DevChapter9.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 "../HighLevel/USBMode.h"
32
33 #if defined(USB_CAN_BE_DEVICE)
34
35 #define INCLUDE_FROM_DEVCHAPTER9_C
36 #include "DevChapter9.h"
37
38 uint8_t USB_ConfigurationNumber;
39 bool USB_RemoteWakeupEnabled;
40 bool USB_CurrentlySelfPowered;
41
42 void USB_Device_ProcessControlPacket(void)
43 {
44 uint8_t bmRequestType = Endpoint_Read_Byte();
45 uint8_t bRequest = Endpoint_Read_Byte();
46 bool RequestHandled = false;
47
48 switch (bRequest)
49 {
50 case REQ_GetStatus:
51 if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
52 (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT)))
53 {
54 USB_Device_GetStatus(bmRequestType);
55 RequestHandled = true;
56 }
57
58 break;
59 #if !defined(FEATURELESS_CONTROL_ONLY_DEVICE)
60 case REQ_ClearFeature:
61 case REQ_SetFeature:
62 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT))
63 {
64 USB_Device_ClearSetFeature(bRequest, bmRequestType);
65 RequestHandled = true;
66 }
67
68 break;
69 #endif
70 case REQ_SetAddress:
71 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
72 {
73 USB_Device_SetAddress();
74 RequestHandled = true;
75 }
76
77 break;
78 case REQ_GetDescriptor:
79 if ((bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE)) ||
80 (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE)))
81 {
82 USB_Device_GetDescriptor();
83 RequestHandled = true;
84 }
85
86 break;
87 case REQ_GetConfiguration:
88 if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE))
89 {
90 USB_Device_GetConfiguration();
91 RequestHandled = true;
92 }
93
94 break;
95 case REQ_SetConfiguration:
96 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE))
97 {
98 USB_Device_SetConfiguration();
99 RequestHandled = true;
100 }
101
102 break;
103 }
104
105 if (!(RequestHandled))
106 RAISE_EVENT(USB_UnhandledControlPacket, bRequest, bmRequestType);
107
108 if (Endpoint_IsSetupReceived())
109 {
110 Endpoint_StallTransaction();
111 Endpoint_ClearSetupReceived();
112 }
113 }
114
115 static void USB_Device_SetAddress(void)
116 {
117 uint8_t wValue_LSB = Endpoint_Read_Byte();
118
119 Endpoint_ClearSetupReceived();
120
121 while (!(Endpoint_IsSetupINReady()));
122
123 Endpoint_ClearSetupIN();
124
125 while (!(Endpoint_IsSetupINReady()));
126
127 UDADDR = ((1 << ADDEN) | (wValue_LSB & 0x7F));
128
129 return;
130 }
131
132 static void USB_Device_SetConfiguration(void)
133 {
134 uint8_t wValue_LSB = Endpoint_Read_Byte();
135 bool AlreadyConfigured = (USB_ConfigurationNumber != 0);
136
137 #if defined(USE_SINGLE_DEVICE_CONFIGURATION)
138 if (wValue_LSB > 1)
139 #else
140 USB_Descriptor_Device_t* DevDescriptorPtr;
141
142 if ((USB_GetDescriptor((DTYPE_Device << 8), 0, (void*)&DevDescriptorPtr) == NO_DESCRIPTOR) ||
143 #if defined(USE_RAM_DESCRIPTORS)
144 (wValue_LSB > DevDescriptorPtr->NumberOfConfigurations))
145 #elif defined (USE_EEPROM_DESCRIPTORS)
146 (wValue_LSB > eeprom_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
147 #else
148 (wValue_LSB > pgm_read_byte(&DevDescriptorPtr->NumberOfConfigurations)))
149 #endif
150 #endif
151 {
152 return;
153 }
154
155 Endpoint_ClearSetupReceived();
156
157 USB_ConfigurationNumber = wValue_LSB;
158
159 Endpoint_ClearSetupIN();
160
161 if (!(AlreadyConfigured) && USB_ConfigurationNumber)
162 RAISE_EVENT(USB_DeviceEnumerationComplete);
163
164 RAISE_EVENT(USB_ConfigurationChanged);
165 }
166
167 void USB_Device_GetConfiguration(void)
168 {
169 Endpoint_ClearSetupReceived();
170
171 Endpoint_Write_Byte(USB_ConfigurationNumber);
172
173 Endpoint_ClearSetupIN();
174
175 while (!(Endpoint_IsSetupOUTReceived()));
176 Endpoint_ClearSetupOUT();
177 }
178
179 static void USB_Device_GetDescriptor(void)
180 {
181 uint16_t wValue = Endpoint_Read_Word_LE();
182 uint16_t wIndex = Endpoint_Read_Word_LE();
183 uint16_t wLength = Endpoint_Read_Word_LE();
184
185 void* DescriptorPointer;
186 uint16_t DescriptorSize;
187
188 bool SendZLP;
189
190 if ((DescriptorSize = USB_GetDescriptor(wValue, wIndex, &DescriptorPointer)) == NO_DESCRIPTOR)
191 return;
192
193 Endpoint_ClearSetupReceived();
194
195 if (wLength > DescriptorSize)
196 wLength = DescriptorSize;
197
198 while (wLength)
199 {
200 while (!(Endpoint_IsSetupINReady()))
201 {
202 if (Endpoint_IsSetupOUTReceived())
203 {
204 Endpoint_ClearSetupOUT();
205 return;
206 }
207 }
208
209 while (wLength && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))
210 {
211 #if defined(USE_RAM_DESCRIPTORS)
212 Endpoint_Write_Byte(*((uint8_t*)DescriptorPointer++));
213 #elif defined (USE_EEPROM_DESCRIPTORS)
214 Endpoint_Write_Byte(eeprom_read_byte(DescriptorPointer++));
215 #else
216 Endpoint_Write_Byte(pgm_read_byte(DescriptorPointer++));
217 #endif
218
219 wLength--;
220 }
221
222 SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);
223 Endpoint_ClearSetupIN();
224 }
225
226 if (SendZLP)
227 {
228 while (!(Endpoint_IsSetupINReady()));
229 Endpoint_ClearSetupIN();
230 }
231
232 while (!(Endpoint_IsSetupOUTReceived()));
233 Endpoint_ClearSetupOUT();
234 }
235
236 static void USB_Device_GetStatus(const uint8_t bmRequestType)
237 {
238 uint8_t CurrentStatus = 0;
239
240 Endpoint_Discard_Word();
241
242 #if !defined(FEATURELESS_CONTROL_ONLY_DEVICE)
243 uint8_t wIndex_LSB = Endpoint_Read_Byte();
244 #endif
245
246 switch (bmRequestType)
247 {
248 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE):
249 if (USB_CurrentlySelfPowered)
250 CurrentStatus |= FEATURE_SELFPOWERED_ENABLED;
251
252 if (USB_RemoteWakeupEnabled)
253 CurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED;
254
255 break;
256 #if !defined(FEATURELESS_CONTROL_ONLY_DEVICE)
257 case (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_ENDPOINT):
258 Endpoint_SelectEndpoint(wIndex_LSB);
259
260 CurrentStatus = Endpoint_IsStalled();
261
262 break;
263 #endif
264 }
265
266 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
267 Endpoint_ClearSetupReceived();
268 Endpoint_Write_Word_LE(CurrentStatus);
269
270 Endpoint_ClearSetupIN();
271
272 while (!(Endpoint_IsSetupOUTReceived()));
273 Endpoint_ClearSetupOUT();
274 }
275
276 #if !defined(FEATURELESS_CONTROL_ONLY_DEVICE)
277 static void USB_Device_ClearSetFeature(const uint8_t bRequest, const uint8_t bmRequestType)
278 {
279 uint16_t wValue = Endpoint_Read_Word_LE();
280 uint16_t wIndex = Endpoint_Read_Word_LE();
281
282 switch (bmRequestType & CONTROL_REQTYPE_RECIPIENT)
283 {
284 case REQREC_ENDPOINT:
285 if (wValue == FEATURE_ENDPOINT_HALT)
286 {
287 uint8_t EndpointIndex = (wIndex & ENDPOINT_EPNUM_MASK);
288
289 if (EndpointIndex != ENDPOINT_CONTROLEP)
290 {
291 Endpoint_SelectEndpoint(EndpointIndex);
292
293 if (Endpoint_IsEnabled())
294 {
295 if (bRequest == REQ_ClearFeature)
296 {
297 Endpoint_ClearStall();
298 Endpoint_ResetFIFO(EndpointIndex);
299 Endpoint_ResetDataToggle();
300 }
301 else
302 {
303 Endpoint_StallTransaction();
304 }
305 }
306
307 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
308 Endpoint_ClearSetupReceived();
309 Endpoint_ClearSetupIN();
310 }
311 }
312
313 break;
314 }
315 }
316 #endif
317
318 #endif