]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Host/LowLevel/MassStorageHost/ConfigDescriptor.c
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Host / LowLevel / MassStorageHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 disclaims 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 /** \file
32 *
33 * USB Device Configuration Descriptor processing routines, to determine the correct pipe configurations
34 * needed to communication with an attached USB device. Descriptors are special computer-readable structures
35 * which the host requests upon device enumeration, to determine the device's capabilities and functions.
36 */
37
38 #include "ConfigDescriptor.h"
39
40 /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
41 * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
42 * with compatible devices.
43 *
44 * This routine searches for a MSD interface descriptor containing bulk IN and OUT data endpoints.
45 *
46 * \return An error code from the \ref MassStorageHost_GetConfigDescriptorDataCodes_t enum.
47 */
48 uint8_t ProcessConfigurationDescriptor(void)
49 {
50 uint8_t ConfigDescriptorData[512];
51 void* CurrConfigLocation = ConfigDescriptorData;
52 uint16_t CurrConfigBytesRem;
53
54 USB_Descriptor_Interface_t* MSInterface = NULL;
55 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
56 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
57
58 /* Retrieve the entire configuration descriptor into the allocated buffer */
59 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
60 {
61 case HOST_GETCONFIG_Successful:
62 break;
63 case HOST_GETCONFIG_InvalidData:
64 return InvalidConfigDataReturned;
65 case HOST_GETCONFIG_BuffOverflow:
66 return DescriptorTooLarge;
67 default:
68 return ControlError;
69 }
70
71 while (!(DataINEndpoint) || !(DataOUTEndpoint))
72 {
73 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
74 if (!(MSInterface) ||
75 USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
76 DComp_NextMSInterfaceBulkDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
77 {
78 /* Get the next Mass Storage interface from the configuration descriptor */
79 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
80 DComp_NextMSInterface) != DESCRIPTOR_SEARCH_COMP_Found)
81 {
82 /* Descriptor not found, error out */
83 return NoCompatibleInterfaceFound;
84 }
85
86 /* Save the interface in case we need to refer back to it later */
87 MSInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
88
89 /* Clear any found endpoints */
90 DataINEndpoint = NULL;
91 DataOUTEndpoint = NULL;
92
93 /* Skip the remainder of the loop as we have not found an endpoint yet */
94 continue;
95 }
96
97 /* Retrieve the endpoint address from the endpoint descriptor */
98 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
99
100 /* If the endpoint is a IN type endpoint */
101 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
102 DataINEndpoint = EndpointData;
103 else
104 DataOUTEndpoint = EndpointData;
105 }
106
107 /* Configure the Mass Storage data IN pipe */
108 Pipe_ConfigurePipe(MASS_STORE_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
109
110 /* Configure the Mass Storage data OUT pipe */
111 Pipe_ConfigurePipe(MASS_STORE_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
112
113 /* Valid data found, return success */
114 return SuccessfulConfigRead;
115 }
116
117 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
118 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
119 * descriptor processing if an incompatible descriptor configuration is found.
120 *
121 * This comparator searches for the next Interface descriptor of the correct Mass Storage Class, Subclass and Protocol values.
122 *
123 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
124 */
125 uint8_t DComp_NextMSInterface(void* CurrentDescriptor)
126 {
127 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
128
129 if (Header->Type == DTYPE_Interface)
130 {
131 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
132
133 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
134 if ((Interface->Class == MASS_STORE_CLASS) &&
135 (Interface->SubClass == MASS_STORE_SUBCLASS) &&
136 (Interface->Protocol == MASS_STORE_PROTOCOL))
137 {
138 return DESCRIPTOR_SEARCH_Found;
139 }
140 }
141
142 return DESCRIPTOR_SEARCH_NotFound;
143 }
144
145 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
146 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
147 * descriptor processing if an incompatible descriptor configuration is found.
148 *
149 * This comparator searches for the next Bulk Endpoint descriptor of the correct MSD interface, aborting the search if
150 * another interface descriptor is found before the next endpoint.
151 *
152 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
153 */
154 uint8_t DComp_NextMSInterfaceBulkDataEndpoint(void* CurrentDescriptor)
155 {
156 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
157
158 if (Header->Type == DTYPE_Endpoint)
159 {
160 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
161
162 /* Check the endpoint type, break out if correct BULK type endpoint found */
163 if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_BULK)
164 return DESCRIPTOR_SEARCH_Found;
165 }
166 else if (Header->Type == DTYPE_Interface)
167 {
168 return DESCRIPTOR_SEARCH_Fail;
169 }
170
171 return DESCRIPTOR_SEARCH_NotFound;
172 }
173
Imprint / Impressum