Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Template / Template_Endpoint_Control_W.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 bool LastPacketFull = false;
5
6 if (Length > USB_ControlRequest.wLength)
7 Length = USB_ControlRequest.wLength;
8
9 while (Length && !(Endpoint_IsOUTReceived()))
10 {
11 while (!(Endpoint_IsINReady()))
12 {
13 if (USB_DeviceState == DEVICE_STATE_Unattached)
14 return ENDPOINT_RWCSTREAM_DeviceDisconnected;
15 }
16
17 while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))
18 {
19 TEMPLATE_TRANSFER_BYTE(DataStream);
20 Length--;
21 }
22
23 LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);
24 Endpoint_ClearIN();
25 }
26
27 if (Endpoint_IsOUTReceived())
28 return ENDPOINT_RWCSTREAM_HostAborted;
29
30 if (LastPacketFull)
31 {
32 while (!(Endpoint_IsINReady()))
33 {
34 if (USB_DeviceState == DEVICE_STATE_Unattached)
35 return ENDPOINT_RWCSTREAM_DeviceDisconnected;
36 }
37
38 Endpoint_ClearIN();
39 }
40
41 while (!(Endpoint_IsOUTReceived()))
42 {
43 if (USB_DeviceState == DEVICE_STATE_Unattached)
44 return ENDPOINT_RWCSTREAM_DeviceDisconnected;
45 }
46
47 return ENDPOINT_RWCSTREAM_NoError;
48 }
49
50 #undef TEMPLATE_BUFFER_OFFSET
51 #undef TEMPLATE_FUNC_NAME
52 #undef TEMPLATE_TRANSFER_BYTE