Added new HID_DESCRIPTOR_MOUSE, HID_DESCRIPTOR_KEYBOARD and HID_DESCRIPTOR_JOYSTICK...
[pub/USBasp.git] / Demos / Device / ClassDriver / Joystick / Descriptors.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 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 /** \file
32 *
33 * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
34 * computer-readable structures which the host requests upon device enumeration, to determine
35 * the device's capabilities and functions.
36 */
37
38 #include "Descriptors.h"
39
40 /** HID class report descriptor. This is a special descriptor constructed with values from the
41 * USBIF HID class specification to describe the reports and capabilities of the HID device. This
42 * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
43 * the device will send, and what it may be sent back from the host. Refer to the HID specification for
44 * more details on HID report descriptors.
45 */
46 USB_Descriptor_HIDReport_Datatype_t PROGMEM JoystickReport[] =
47 {
48 /* Use the HID class driver's standard Joystick report.
49 * Min X/Y Axis values: -100
50 * Max X/Y Axis values: 100
51 * Buttons: 2
52 */
53 HID_DESCRIPTOR_JOYSTICK(-100, 100, 2)
54 };
55
56 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
57 * device characteristics, including the supported USB version, control endpoint size and the
58 * number of device configurations. The descriptor is read out by the USB host when the enumeration
59 * process begins.
60 */
61 USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
62 {
63 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
64
65 .USBSpecification = VERSION_BCD(01.10),
66 .Class = USB_CSCP_NoDeviceClass,
67 .SubClass = USB_CSCP_NoDeviceSubclass,
68 .Protocol = USB_CSCP_NoDeviceProtocol,
69
70 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
71
72 .VendorID = 0x03EB,
73 .ProductID = 0x2043,
74 .ReleaseNumber = VERSION_BCD(00.01),
75
76 .ManufacturerStrIndex = 0x01,
77 .ProductStrIndex = 0x02,
78 .SerialNumStrIndex = NO_DESCRIPTOR,
79
80 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
81 };
82
83 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
84 * of the device in one of its supported configurations, including information about any device interfaces
85 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
86 * a configuration so that the host may correctly communicate with the USB device.
87 */
88 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
89 {
90 .Config =
91 {
92 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
93
94 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
95 .TotalInterfaces = 1,
96
97 .ConfigurationNumber = 1,
98 .ConfigurationStrIndex = NO_DESCRIPTOR,
99
100 .ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED),
101
102 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
103 },
104
105 .HID_Interface =
106 {
107 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
108
109 .InterfaceNumber = 0x00,
110 .AlternateSetting = 0x00,
111
112 .TotalEndpoints = 1,
113
114 .Class = HID_CSCP_HIDClass,
115 .SubClass = HID_CSCP_NonBootSubclass,
116 .Protocol = HID_CSCP_NonBootProtocol,
117
118 .InterfaceStrIndex = NO_DESCRIPTOR
119 },
120
121 .HID_JoystickHID =
122 {
123 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
124
125 .HIDSpec = VERSION_BCD(01.11),
126 .CountryCode = 0x00,
127 .TotalReportDescriptors = 1,
128 .HIDReportType = HID_DTYPE_Report,
129 .HIDReportLength = sizeof(JoystickReport)
130 },
131
132 .HID_ReportINEndpoint =
133 {
134 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
135
136 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | JOYSTICK_EPNUM),
137 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
138 .EndpointSize = JOYSTICK_EPSIZE,
139 .PollingIntervalMS = 0x01
140 }
141 };
142
143 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
144 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
145 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
146 */
147 USB_Descriptor_String_t PROGMEM LanguageString =
148 {
149 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
150
151 .UnicodeString = {LANGUAGE_ID_ENG}
152 };
153
154 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
155 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
156 * Descriptor.
157 */
158 USB_Descriptor_String_t PROGMEM ManufacturerString =
159 {
160 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
161
162 .UnicodeString = L"Dean Camera"
163 };
164
165 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
166 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
167 * Descriptor.
168 */
169 USB_Descriptor_String_t PROGMEM ProductString =
170 {
171 .Header = {.Size = USB_STRING_LEN(18), .Type = DTYPE_String},
172
173 .UnicodeString = L"LUFA Joystick Demo"
174 };
175
176 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
177 * documentation) by the application code so that the address and size of a requested descriptor can be given
178 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
179 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
180 * USB host.
181 */
182 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
183 const uint8_t wIndex,
184 const void** const DescriptorAddress)
185 {
186 const uint8_t DescriptorType = (wValue >> 8);
187 const uint8_t DescriptorNumber = (wValue & 0xFF);
188
189 const void* Address = NULL;
190 uint16_t Size = NO_DESCRIPTOR;
191
192 switch (DescriptorType)
193 {
194 case DTYPE_Device:
195 Address = &DeviceDescriptor;
196 Size = sizeof(USB_Descriptor_Device_t);
197 break;
198 case DTYPE_Configuration:
199 Address = &ConfigurationDescriptor;
200 Size = sizeof(USB_Descriptor_Configuration_t);
201 break;
202 case DTYPE_String:
203 switch (DescriptorNumber)
204 {
205 case 0x00:
206 Address = &LanguageString;
207 Size = pgm_read_byte(&LanguageString.Header.Size);
208 break;
209 case 0x01:
210 Address = &ManufacturerString;
211 Size = pgm_read_byte(&ManufacturerString.Header.Size);
212 break;
213 case 0x02:
214 Address = &ProductString;
215 Size = pgm_read_byte(&ProductString.Header.Size);
216 break;
217 }
218
219 break;
220 case HID_DTYPE_HID:
221 Address = &ConfigurationDescriptor.HID_JoystickHID;
222 Size = sizeof(USB_HID_Descriptor_HID_t);
223 break;
224 case HID_DTYPE_Report:
225 Address = &JoystickReport;
226 Size = sizeof(JoystickReport);
227 break;
228 }
229
230 *DescriptorAddress = Address;
231 return Size;
232 }
233