Added flag to the HID report parser to indicate if a device has multiple reports.
[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 #warning The HID Host mode Class driver is currently incomplete and is for preview purposes only.
38
39 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorSize,
40 uint8_t* ConfigDescriptorData)
41 {
42 uint8_t FoundEndpoints = 0;
43
44 memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
45
46 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
47 return HID_ENUMERROR_InvalidConfigDescriptor;
48
49 USB_Descriptor_Interface_t* CurrentHIDInterface;
50
51 do
52 {
53 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
54 DComp_HID_Host_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
55 {
56 return HID_ENUMERROR_NoHIDInterfaceFound;
57 }
58
59 CurrentHIDInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
60 } while (HIDInterfaceInfo->Config.HIDInterfaceProtocol &&
61 (CurrentHIDInterface->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol));
62
63 HIDInterfaceInfo->State.InterfaceNumber =
64 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES)
65 CurrentHIDInterface->InterfaceNumber;
66 #else
67 CurrentHIDInterface->bInterfaceNumber;
68 #endif
69 HIDInterfaceInfo->State.SupportsBootSubClass = (CurrentHIDInterface->SubClass != 0);
70
71 while (FoundEndpoints != ((1 << HID_FOUND_DATAPIPE_IN) | (1 << HID_FOUND_DATAPIPE_OUT)))
72 {
73 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
74 DComp_HID_Host_NextInterfaceHIDDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
75 {
76 if (FoundEndpoints == (1 << HID_FOUND_DATAPIPE_IN))
77 break;
78
79 return HID_ENUMERROR_EndpointsNotFound;
80 }
81
82 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
83
84 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
85 {
86 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
87 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
88
89 FoundEndpoints |= (1 << HID_FOUND_DATAPIPE_IN);
90 }
91 else
92 {
93 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
94 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
95
96 FoundEndpoints |= (1 << HID_FOUND_DATAPIPE_OUT);
97 }
98 }
99
100 HIDInterfaceInfo->State.Active = true;
101 return HID_ENUMERROR_NoError;
102 }
103
104 static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor)
105 {
106 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
107 {
108 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
109 USB_Descriptor_Interface_t);
110
111 if (CurrentInterface->Class == HID_INTERFACE_CLASS)
112 return DESCRIPTOR_SEARCH_Found;
113 }
114
115 return DESCRIPTOR_SEARCH_NotFound;
116 }
117
118 static uint8_t DComp_HID_Host_NextInterfaceHIDDataEndpoint(void* CurrentDescriptor)
119 {
120 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
121 {
122 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
123 USB_Descriptor_Endpoint_t);
124
125 if (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
126 return DESCRIPTOR_SEARCH_Found;
127 }
128 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
129 {
130 return DESCRIPTOR_SEARCH_Fail;
131 }
132
133 return DESCRIPTOR_SEARCH_NotFound;
134 }
135
136 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
137 {
138
139 }
140
141 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
142 {
143 bool ReportReceived;
144
145 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.Active))
146 return false;
147
148 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
149 Pipe_Unfreeze();
150
151 ReportReceived = Pipe_IsReadWriteAllowed();
152
153 Pipe_Freeze();
154
155 return ReportReceived;
156 }
157
158 uint8_t USB_HID_Host_SetProtocol(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, bool UseReportProtocol)
159 {
160 USB_ControlRequest = (USB_Request_Header_t)
161 {
162 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
163 .bRequest = REQ_SetProtocol,
164 .wValue = UseReportProtocol,
165 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
166 .wLength = 0,
167 };
168
169 Pipe_SelectPipe(PIPE_CONTROLPIPE);
170
171 return USB_Host_SendControlRequest(NULL);
172 }
173
174 #endif