Descriptors.c 11.4 KB
Newer Older
1
2
/*
             LUFA Library
Dean Camera's avatar
Dean Camera committed
3
     Copyright (C) Dean Camera, 2018.
4

5
  dean [at] fourwalledcubicle [dot] com
6
           www.lufa-lib.org
7
8
9
*/

/*
Dean Camera's avatar
Dean Camera committed
10
  Copyright 2018  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
17
18
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
19
20
  software without specific, written prior permission.

21
  The author disclaims all warranties with regard to this
22
23
24
25
26
27
28
29
30
31
32
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
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
39
40
41
42
43
44
45
46
 */

#include "Descriptors.h"


/** HID class report descriptor. This is a special descriptor constructed with values from the
 *  USBIF HID class specification to describe the reports and capabilities of the HID device. This
 *  descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
 *  the device will send, and what it may be sent back from the host. Refer to the HID specification for
 *  more details on HID report descriptors.
 */
47
const USB_Descriptor_HIDReport_Datatype_t PROGMEM MouseReport[] =
48
{
49
50
51
52
53
54
55
56
57
	/* Use the HID class driver's standard Mouse report.
	 *   Min X/Y Axis values: -1
	 *   Max X/Y Axis values:  1
	 *   Min physical X/Y Axis values (used to determine resolution): -1
	 *   Max physical X/Y Axis values (used to determine resolution):  1
	 *   Buttons: 3
	 *   Absolute screen coordinates: false
	 */
	HID_DESCRIPTOR_MOUSE(-1, 1, -1, 1, 3, false)
58
59
60
61
62
63
64
};

/** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
 *  device characteristics, including the supported USB version, control endpoint size and the
 *  number of device configurations. The descriptor is read out by the USB host when the enumeration
 *  process begins.
 */
65
const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
66
67
{
	.Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
68

69
	.USBSpecification       = VERSION_BCD(1,1,0),
70
71
72
	.Class                  = USB_CSCP_IADDeviceClass,
	.SubClass               = USB_CSCP_IADDeviceSubclass,
	.Protocol               = USB_CSCP_IADDeviceProtocol,
73

74
	.Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,
75

76
77
	.VendorID               = 0x03EB,
	.ProductID              = 0x2062,
78
	.ReleaseNumber          = VERSION_BCD(0,0,1),
79

80
81
	.ManufacturerStrIndex   = STRING_ID_Manufacturer,
	.ProductStrIndex        = STRING_ID_Product,
82
	.SerialNumStrIndex      = USE_INTERNAL_SERIAL,
83

84
85
86
87
88
89
90
91
	.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
};

/** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
 *  of the device in one of its supported configurations, including information about any device interfaces
 *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
 *  a configuration so that the host may correctly communicate with the USB device.
 */
92
const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
93
{
94
	.Config =
95
96
97
98
99
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},

			.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
			.TotalInterfaces        = 3,
100

101
102
			.ConfigurationNumber    = 1,
			.ConfigurationStrIndex  = NO_DESCRIPTOR,
103

104
			.ConfigAttributes       = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
105

106
107
108
			.MaxPowerConsumption    = USB_CONFIG_POWER_MA(100)
		},

109
	.CDC_IAD =
110
111
112
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation},

113
			.FirstInterfaceIndex    = INTERFACE_ID_CDC_CCI,
114
115
			.TotalInterfaces        = 2,

116
117
118
			.Class                  = CDC_CSCP_CDCClass,
			.SubClass               = CDC_CSCP_ACMSubclass,
			.Protocol               = CDC_CSCP_ATCommandProtocol,
119
120
121

			.IADStrIndex            = NO_DESCRIPTOR
		},
122
123

	.CDC_CCI_Interface =
124
125
126
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},

127
			.InterfaceNumber        = INTERFACE_ID_CDC_CCI,
128
			.AlternateSetting       = 0,
129

130
			.TotalEndpoints         = 1,
131

132
133
134
			.Class                  = CDC_CSCP_CDCClass,
			.SubClass               = CDC_CSCP_ACMSubclass,
			.Protocol               = CDC_CSCP_ATCommandProtocol,
135

136
137
138
			.InterfaceStrIndex      = NO_DESCRIPTOR
		},

139
	.CDC_Functional_Header =
140
		{
141
			.Header                 = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = CDC_DTYPE_CSInterface},
142
			.Subtype                = CDC_DSUBTYPE_CSInterface_Header,
143

144
			.CDCSpecification       = VERSION_BCD(1,1,0),
145
146
		},

147
	.CDC_Functional_ACM =
148
		{
149
			.Header                 = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = CDC_DTYPE_CSInterface},
150
			.Subtype                = CDC_DSUBTYPE_CSInterface_ACM,
151

152
			.Capabilities           = 0x06,
153
		},
154
155

	.CDC_Functional_Union =
156
		{
157
			.Header                 = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = CDC_DTYPE_CSInterface},
158
			.Subtype                = CDC_DSUBTYPE_CSInterface_Union,
159

160
161
			.MasterInterfaceNumber  = INTERFACE_ID_CDC_CCI,
			.SlaveInterfaceNumber   = INTERFACE_ID_CDC_DCI,
162
163
		},

164
	.CDC_NotificationEndpoint =
165
166
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
167

168
			.EndpointAddress        = CDC_NOTIFICATION_EPADDR,
169
170
171
172
173
			.Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
			.EndpointSize           = CDC_NOTIFICATION_EPSIZE,
			.PollingIntervalMS      = 0xFF
		},

174
	.CDC_DCI_Interface =
175
176
177
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},

178
			.InterfaceNumber        = INTERFACE_ID_CDC_DCI,
179
			.AlternateSetting       = 0,
180

181
			.TotalEndpoints         = 2,
182

183
184
185
			.Class                  = CDC_CSCP_CDCDataClass,
			.SubClass               = CDC_CSCP_NoDataSubclass,
			.Protocol               = CDC_CSCP_NoDataProtocol,
186

187
188
189
			.InterfaceStrIndex      = NO_DESCRIPTOR
		},

190
	.CDC_DataOutEndpoint =
191
192
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
193

194
			.EndpointAddress        = CDC_RX_EPADDR,
195
196
			.Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
			.EndpointSize           = CDC_TXRX_EPSIZE,
197
			.PollingIntervalMS      = 0x05
198
		},
199
200

	.CDC_DataInEndpoint =
201
202
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
203

204
			.EndpointAddress        = CDC_TX_EPADDR,
205
206
			.Attributes             = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
			.EndpointSize           = CDC_TXRX_EPSIZE,
207
			.PollingIntervalMS      = 0x05
208
209
		},

210
	.HID_Interface =
211
212
213
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},

214
			.InterfaceNumber        = INTERFACE_ID_Mouse,
215
			.AlternateSetting       = 0,
216

217
			.TotalEndpoints         = 1,
218

219
220
221
			.Class                  = HID_CSCP_HIDClass,
			.SubClass               = HID_CSCP_BootSubclass,
			.Protocol               = HID_CSCP_MouseBootProtocol,
222

223
224
225
			.InterfaceStrIndex      = NO_DESCRIPTOR
		},

226
	.HID_MouseHID =
227
		{
228
			.Header                 = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
229

230
			.HIDSpec                = VERSION_BCD(1,1,1),
231
232
			.CountryCode            = 0x00,
			.TotalReportDescriptors = 1,
233
			.HIDReportType          = HID_DTYPE_Report,
234
235
236
			.HIDReportLength        = sizeof(MouseReport)
		},

237
	.HID_ReportINEndpoint =
238
239
240
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},

241
			.EndpointAddress        = MOUSE_EPADDR,
242
243
			.Attributes             = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
			.EndpointSize           = MOUSE_EPSIZE,
244
			.PollingIntervalMS      = 0x05
245
246
247
248
249
250
251
		}
};

/** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
 *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
 *  via the language ID table available at USB.org what languages the device supports for its string descriptors.
 */
252
const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
253
254
255
256
257

/** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
 *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
 *  Descriptor.
 */
258
const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"LUFA Library");
259
260
261
262
263

/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
 *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
 *  Descriptor.
 */
264
const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA CDC and Mouse Demo");
265
266
267
268
269
270
271

/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
 *  documentation) by the application code so that the address and size of a requested descriptor can be given
 *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
 *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
 *  USB host.
 */
272
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
273
                                    const uint16_t wIndex,
274
                                    const void** const DescriptorAddress)
275
276
277
278
{
	const uint8_t  DescriptorType   = (wValue >> 8);
	const uint8_t  DescriptorNumber = (wValue & 0xFF);

279
280
	const void* Address = NULL;
	uint16_t    Size    = NO_DESCRIPTOR;
281
282
283
284

	switch (DescriptorType)
	{
		case DTYPE_Device:
285
			Address = &DeviceDescriptor;
286
287
			Size    = sizeof(USB_Descriptor_Device_t);
			break;
288
		case DTYPE_Configuration:
289
			Address = &ConfigurationDescriptor;
290
291
			Size    = sizeof(USB_Descriptor_Configuration_t);
			break;
292
		case DTYPE_String:
293
294
			switch (DescriptorNumber)
			{
295
				case STRING_ID_Language:
296
					Address = &LanguageString;
297
298
					Size    = pgm_read_byte(&LanguageString.Header.Size);
					break;
299
				case STRING_ID_Manufacturer:
300
					Address = &ManufacturerString;
301
302
					Size    = pgm_read_byte(&ManufacturerString.Header.Size);
					break;
303
				case STRING_ID_Product:
304
					Address = &ProductString;
305
306
307
					Size    = pgm_read_byte(&ProductString.Header.Size);
					break;
			}
308

309
			break;
310
		case HID_DTYPE_HID:
311
			Address = &ConfigurationDescriptor.HID_MouseHID;
312
			Size    = sizeof(USB_HID_Descriptor_HID_t);
313
			break;
314
		case HID_DTYPE_Report:
315
			Address = &MouseReport;
316
317
318
			Size    = sizeof(MouseReport);
			break;
	}
319

320
321
322
	*DescriptorAddress = Address;
	return Size;
}
323