Collapse configuration descriptor size retrieval and size testing into a single if...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / HID.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 #if defined(USB_CAN_BE_HOST)
33
34 #define INCLUDE_FROM_HID_CLASS_HOST_C
35 #include "HID.h"
36
37 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorSize,
38 uint8_t* ConfigDescriptorData)
39 {
40 uint8_t FoundEndpoints = 0;
41
42 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
43 return HID_ENUMERROR_InvalidConfigDescriptor;
44
45 do
46 {
47 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
48 DComp_HID_Host_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
49 {
50 return HID_ENUMERROR_NoHIDInterfaceFound;
51 }
52 } while (HIDInterfaceInfo->Config.MatchInterfaceProtocol &&
53 DESCRIPTOR_PCAST(ConfigDescriptorData,
54 USB_Descriptor_Interface_t)->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol);
55
56 while (FoundEndpoints != ((1 << HID_FOUND_DATAPIPE_IN) | (1 << HID_FOUND_DATAPIPE_OUT)))
57 {
58 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
59 DComp_HID_Host_NextInterfaceHIDDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
60 {
61 if (FoundEndpoints == (1 << HID_FOUND_DATAPIPE_IN))
62 break;
63
64 return HID_ENUMERROR_EndpointsNotFound;
65 }
66
67 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
68
69 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
70 {
71 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
72 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
73
74 FoundEndpoints |= (1 << HID_FOUND_DATAPIPE_IN);
75 }
76 else
77 {
78 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
79 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
80
81 FoundEndpoints |= (1 << HID_FOUND_DATAPIPE_OUT);
82 }
83 }
84
85 return HID_ENUMERROR_NoError;
86 }
87
88 static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor)
89 {
90 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
91 {
92 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
93 USB_Descriptor_Interface_t);
94
95 if (CurrentInterface->Class == HID_INTERFACE_CLASS)
96 return DESCRIPTOR_SEARCH_Found;
97 }
98
99 return DESCRIPTOR_SEARCH_NotFound;
100 }
101
102 static uint8_t DComp_HID_Host_NextInterfaceHIDDataEndpoint(void* CurrentDescriptor)
103 {
104 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
105 {
106 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
107 USB_Descriptor_Endpoint_t);
108
109 if (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
110 return DESCRIPTOR_SEARCH_Found;
111 }
112 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
113 {
114 return DESCRIPTOR_SEARCH_Fail;
115 }
116
117 return DESCRIPTOR_SEARCH_NotFound;
118 }
119
120 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
121 {
122
123 }
124
125 #endif