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