Fix nasty bug in USBInterrupt.c which would cause a lockup if the control endpoint...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Template / Template_Endpoint_Control_R.c
1 uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
2 {
3 uint8_t* DataStream = ((uint8_t*)Buffer + TEMPLATE_BUFFER_OFFSET(Length));
4
5 if (!(Length))
6 Endpoint_ClearOUT();
7
8 while (Length)
9 {
10 if (Endpoint_IsSETUPReceived())
11 return ENDPOINT_RWCSTREAM_HostAborted;
12
13 if (USB_DeviceState == DEVICE_STATE_Unattached)
14 return ENDPOINT_RWCSTREAM_DeviceDisconnected;
15 else if (USB_DeviceState == DEVICE_STATE_Suspended)
16 return ENDPOINT_RWCSTREAM_BusSuspended;
17
18 if (Endpoint_IsOUTReceived())
19 {
20 while (Length && Endpoint_BytesInEndpoint())
21 {
22 TEMPLATE_TRANSFER_BYTE(DataStream);
23 Length--;
24 }
25
26 Endpoint_ClearOUT();
27 }
28 }
29
30 while (!(Endpoint_IsINReady()))
31 {
32 if (USB_DeviceState == DEVICE_STATE_Unattached)
33 return ENDPOINT_RWCSTREAM_DeviceDisconnected;
34 else if (USB_DeviceState == DEVICE_STATE_Suspended)
35 return ENDPOINT_RWCSTREAM_BusSuspended;
36 }
37
38 return ENDPOINT_RWCSTREAM_NoError;
39 }
40
41
42 #undef TEMPLATE_BUFFER_OFFSET
43 #undef TEMPLATE_FUNC_NAME
44 #undef TEMPLATE_TRANSFER_BYTE