• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #pragma once
19 
20 #include "drivers.h"
21 
22 #ifdef MYUDB_DISABLE
23 #else
24 #define MYUDB_ID_VENDOR           0x248a  // for report
25 #define MYUDB_ID_PRODUCT          0x9218
26 #define MYUDB_ID_VERSION          0x0101
27 #define MYUDB_STRING_VENDOR       L"Telink"
28 #define MYUDB_STRING_PRODUCT      L"HCI_VCD Dongle"
29 #define MYUDB_STRING_SERIAL       L"TLSR9218"
30 
31 ////////////////////////////////////////////////////////
32 
33 /* Public Interface - May be used in end-application: */
34 /* Macros: */
35 /** Mask for the request type parameter, to indicate the direction of the request data (Host to Device
36 	 *  or Device to Host). The result of this mask should then be compared to the request direction masks.
37 	 *
38 	 *  \see \c REQDIR_* macros for masks indicating the request data direction.
39 	 */
40 #define CONTROL_REQTYPE_DIRECTION 0x80
41 
42 /** Mask for the request type parameter, to indicate the type of request (Device, Class or Vendor
43 	 *  Specific). The result of this mask should then be compared to the request type masks.
44 	 *
45 	 *  \see \c REQTYPE_* macros for masks indicating the request type.
46 	 */
47 #define CONTROL_REQTYPE_TYPE      0x60
48 
49 /** Mask for the request type parameter, to indicate the recipient of the request (Device, Interface
50 	 *  Endpoint or Other). The result of this mask should then be compared to the request recipient
51 	 *  masks.
52 	 *
53 	 *  \see \c REQREC_* macros for masks indicating the request recipient.
54 	 */
55 #define CONTROL_REQTYPE_RECIPIENT 0x1F
56 
57 /** \name Control Request Data Direction Masks */
58 // @{
59 /** Request data direction mask, indicating that the request data will flow from host to device.
60 	 *
61 	 *  \see \ref CONTROL_REQTYPE_DIRECTION macro.
62 	 */
63 #define REQDIR_HOSTTODEVICE       (0 << 7)
64 
65 /** Request data direction mask, indicating that the request data will flow from device to host.
66 	 *
67 	 *  \see \ref CONTROL_REQTYPE_DIRECTION macro.
68 	 */
69 #define REQDIR_DEVICETOHOST       (1 << 7)
70 // @}
71 
72 /** \name Control Request Type Masks */
73 // @{
74 /** Request type mask, indicating that the request is a standard request.
75 	 *
76 	 *  \see \ref CONTROL_REQTYPE_TYPE macro.
77 	 */
78 #define REQTYPE_STANDARD          (0 << 5)
79 
80 /** Request type mask, indicating that the request is a class-specific request.
81 	 *
82 	 *  \see \ref CONTROL_REQTYPE_TYPE macro.
83 	 */
84 #define REQTYPE_CLASS             (1 << 5)
85 
86 /** Request type mask, indicating that the request is a vendor specific request.
87 	 *
88 	 *  \see \ref CONTROL_REQTYPE_TYPE macro.
89 	 */
90 #define REQTYPE_VENDOR            (2 << 5)
91 // @}
92 
93 /** \name Control Request Recipient Masks */
94 // @{
95 /** Request recipient mask, indicating that the request is to be issued to the device as a whole.
96 	 *
97 	 *  \see \ref CONTROL_REQTYPE_RECIPIENT macro.
98 	 */
99 #define REQREC_DEVICE             (0 << 0)
100 
101 /** Request recipient mask, indicating that the request is to be issued to an interface in the
102 	 *  currently selected configuration.
103 	 *
104 	 *  \see \ref CONTROL_REQTYPE_RECIPIENT macro.
105 	 */
106 #define REQREC_INTERFACE          (1 << 0)
107 
108 /** Request recipient mask, indicating that the request is to be issued to an endpoint in the
109 	 *  currently selected configuration.
110 	 *
111 	 *  \see \ref CONTROL_REQTYPE_RECIPIENT macro.
112 	 */
113 #define REQREC_ENDPOINT           (2 << 0)
114 
115 /** Request recipient mask, indicating that the request is to be issued to an unspecified element
116 	 *  in the currently selected configuration.
117 	 *
118 	 *  \see \ref CONTROL_REQTYPE_RECIPIENT macro.
119 	 */
120 #define REQREC_OTHER              (3 << 0)
121 // @}
122 
123 /* Type Defines: */
124 /** \brief Standard USB Control Request
125 	 *
126 	 *  Type define for a standard USB control request.
127 	 *
128 	 *  \see The USB 2.0 specification for more information on standard control requests.
129 	 */
130 typedef struct {
131     u8 bmRequestType; /**< Type of the request. */
132     u8 bRequest;      /**< Request command code. */
133     u16 wValue;       /**< wValue parameter of the request. */
134     u16 wIndex;       /**< wIndex parameter of the request. */
135     u16 wLength;      /**< Length of the data to transfer in bytes. */
136 } USB_Request_Header_t;
137 
138 /* Enums: */
139 /** Enumeration for the various standard request commands. These commands are applicable when the
140 	 *  request type is \ref REQTYPE_STANDARD (with the exception of \ref REQ_GetDescriptor, which is always
141 	 *  handled regardless of the request type value).
142 	 *
143 	 *  \see Chapter 9 of the USB 2.0 Specification.
144 	 */
145 enum USB_Control_Request_t {
146     REQ_GetStatus = 0,        /**< Implemented in the library for device and endpoint recipients. Passed
147 		                              *   to the user application for other recipients via the
148 		                              *   \ref EVENT_USB_Device_ControlRequest() event when received in
149 		                              *   device mode. */
150     REQ_ClearFeature = 1,     /**< Implemented in the library for device and endpoint recipients. Passed
151 		                              *   to the user application for other recipients via the
152 		                              *   \ref EVENT_USB_Device_ControlRequest() event when received in
153 		                              *   device mode. */
154     REQ_SetFeature = 3,       /**< Implemented in the library for device and endpoint recipients. Passed
155 		                              *   to the user application for other recipients via the
156 		                              *   \ref EVENT_USB_Device_ControlRequest() event when received in
157 		                              *   device mode. */
158     REQ_SetAddress = 5,       /**< Implemented in the library for the device recipient. Passed
159 		                              *   to the user application for other recipients via the
160 		                              *   \ref EVENT_USB_Device_ControlRequest() event when received in
161 		                              *   device mode. */
162     REQ_GetDescriptor = 6,    /**< Implemented in the library for device and interface recipients. Passed to the
163 		                              *   user application for other recipients via the
164 		                              *   \ref EVENT_USB_Device_ControlRequest() event when received in
165 		                              *   device mode. */
166     REQ_SetDescriptor = 7,    /**< Not implemented in the library, passed to the user application
167 		                              *   via the \ref EVENT_USB_Device_ControlRequest() event when received in
168 		                              *   device mode. */
169     REQ_GetConfiguration = 8, /**< Implemented in the library for the device recipient. Passed
170 		                              *   to the user application for other recipients via the
171 		                              *   \ref EVENT_USB_Device_ControlRequest() event when received in
172 		                              *   device mode. */
173     REQ_SetConfiguration = 9, /**< Implemented in the library for the device recipient. Passed
174 		                              *   to the user application for other recipients via the
175 		                              *   \ref EVENT_USB_Device_ControlRequest() event when received in
176 		                              *   device mode. */
177     REQ_GetInterface = 10,    /**< Not implemented in the library, passed to the user application
178 		                              *   via the \ref EVENT_USB_Device_ControlRequest() event when received in
179 		                              *   device mode. */
180     REQ_SetInterface = 11,    /**< Not implemented in the library, passed to the user application
181 		                              *   via the \ref EVENT_USB_Device_ControlRequest() event when received in
182 		                              *   device mode. */
183     REQ_SynchFrame = 12,      /**< Not implemented in the library, passed to the user application
184 		                              *   via the \ref EVENT_USB_Device_ControlRequest() event when received in
185 		                              *   device mode. */
186 };
187 
188 /* Feature Selector values for Set Feature and Clear Feature standard control requests directed to the device,
189  *  interface and endpoint recipients.
190  */
191 enum USB_Feature_Selectors_t {
192     FEATURE_SEL_EndpointHalt = 0x00,       /* < Feature selector for Clear Feature or Set Feature commands. When
193 		                                    *   used in a Set Feature or Clear Feature request this indicates that an
194 		                                    *   endpoint (whose address is given elsewhere in the request) should have
195 		                                    *   its stall condition changed.
196 		                                    */
197     FEATURE_SEL_DeviceRemoteWakeup = 0x01, /* < Feature selector for Device level Remote Wakeup enable set or clear.
198 	                                        *   This feature can be controlled by the host on devices which indicate
199 	                                        *   remote wakeup support in their descriptors to selectively disable or
200 	                                        *   enable remote wakeup.
201 	                                        */
202     FEATURE_SEL_TestMode = 0x02, /**< Feature selector for Test Mode features, used to test the USB controller
203 	                                            *   to check for incorrect operation.
204 	                                            */
205 };
206 
207 #define FEATURE_SELFPOWERED_ENABLED      (1 << 0)
208 #define FEATURE_REMOTE_WAKEUP_ENABLED    (1 << 1)
209 ////////////////////////////////////////////////////////
210 
211 /* Public Interface - May be used in end-application: */
212 /* Macros: */
213 /** Indicates that a given descriptor does not exist in the device. This can be used inside descriptors
214  *  for string descriptor indexes, or may be use as a return value for GetDescriptor when the specified
215  *  descriptor does not exist.
216  */
217 #define NO_DESCRIPTOR                    0
218 
219 /** Macro to calculate the power value for the configuration descriptor, from a given number of milliamperes.
220  *
221  *  \param[in] mA  Maximum number of milliamps the device consumes when the given configuration is selected.
222  */
223 #define USB_CONFIG_POWER_MA(mA)          ((mA) >> 1)
224 
225 /** Macro to calculate the Unicode length of a string with a given number of Unicode characters.
226  *  Should be used in string descriptor's headers for giving the string descriptor's byte length.
227  *
228  *  \param[in] UnicodeChars  Number of Unicode characters in the string text.
229  */
230 #define USB_STRING_LEN(UnicodeChars)     (sizeof(USB_Descriptor_Header_t) + ((UnicodeChars) << 1))
231 
232 /** String language ID for the English language. Should be used in \ref USB_Descriptor_String_t descriptors
233  *  to indicate that the English language is supported by the device in its string descriptors.
234  */
235 #define LANGUAGE_ID_ENG                  0x0409
236 
237 /** \name USB Configuration Descriptor Attribute Masks */
238 // @{
239 /** Mask for the reserved bit in the Configuration Descriptor's \c ConfigAttributes field, which must be set on all
240  *  devices for historial purposes.
241  */
242 #define USB_CONFIG_ATTR_RESERVED         0x80
243 /** Can be masked with other configuration descriptor attributes for a \ref USB_Descriptor_Configuration_Header_t
244  *  descriptor's \c ConfigAttributes value to indicate that the specified configuration can draw its power
245  *  from the device's own power source.
246  */
247 #define USB_CONFIG_ATTR_SELFPOWERED      0x40
248 
249 /** Can be masked with other configuration descriptor attributes for a \ref USB_Descriptor_Configuration_Header_t
250  *  descriptor's \c ConfigAttributes value to indicate that the specified configuration supports the
251  *  remote wakeup feature of the USB standard, allowing a suspended USB device to wake up the host upon
252  *  request.
253  */
254 #define USB_CONFIG_ATTR_REMOTEWAKEUP     0x20
255 // @}
256 
257 /** \name Endpoint Descriptor Attribute Masks */
258 // @{
259 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
260  *  \c Attributes value to indicate that the specified endpoint is not synchronized.
261  *
262  *  \see The USB specification for more details on the possible Endpoint attributes.
263  */
264 #define ENDPOINT_ATTR_NO_SYNC            (0 << 2)
265 
266 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
267  *  \c Attributes value to indicate that the specified endpoint is asynchronous.
268  *
269  *  \see The USB specification for more details on the possible Endpoint attributes.
270  */
271 #define ENDPOINT_ATTR_ASYNC              (1 << 2)
272 
273 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
274  *  \c Attributes value to indicate that the specified endpoint is adaptive.
275  *
276  *  \see The USB specification for more details on the possible Endpoint attributes.
277  */
278 #define ENDPOINT_ATTR_ADAPTIVE           (2 << 2)
279 
280 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
281  *  \c Attributes value to indicate that the specified endpoint is synchronized.
282  *
283  *  \see The USB specification for more details on the possible Endpoint attributes.
284  */
285 #define ENDPOINT_ATTR_SYNC               (3 << 2)
286 // @}
287 
288 /** \name Endpoint Descriptor Usage Masks */
289 // @{
290 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
291  *  \c Attributes value to indicate that the specified endpoint is used for data transfers.
292  *
293  *  \see The USB specification for more details on the possible Endpoint usage attributes.
294  */
295 #define ENDPOINT_USAGE_DATA              (0 << 4)
296 
297 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
298  *  \c Attributes value to indicate that the specified endpoint is used for feedback.
299  *
300  *  \see The USB specification for more details on the possible Endpoint usage attributes.
301  */
302 #define ENDPOINT_USAGE_FEEDBACK          (1 << 4)
303 
304 /** Can be masked with other endpoint descriptor attributes for a \ref USB_Descriptor_Endpoint_t descriptor's
305  *  \c Attributes value to indicate that the specified endpoint is used for implicit feedback.
306  *
307  *  \see The USB specification for more details on the possible Endpoint usage attributes.
308  */
309 #define ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << 4)
310 // @}
311 
312 /* Enums: */
313 /** Enum for the possible standard descriptor types, as given in each descriptor's header. */
314 enum USB_DescriptorTypes_t {
315     DTYPE_Device = 0x01,               /**< Indicates that the descriptor is a device descriptor. */
316     DTYPE_Configuration = 0x02,        /**< Indicates that the descriptor is a configuration descriptor. */
317     DTYPE_String = 0x03,               /**< Indicates that the descriptor is a string descriptor. */
318     DTYPE_Interface = 0x04,            /**< Indicates that the descriptor is an interface descriptor. */
319     DTYPE_Endpoint = 0x05,             /**< Indicates that the descriptor is an endpoint descriptor. */
320     DTYPE_DeviceQualifier = 0x06,      /**< Indicates that the descriptor is a device qualifier descriptor. */
321     DTYPE_Other = 0x07,                /**< Indicates that the descriptor is of other type. */
322     DTYPE_InterfacePower = 0x08,       /**< Indicates that the descriptor is an interface power descriptor. */
323     DTYPE_InterfaceAssociation = 0x0B, /**< Indicates that the descriptor is an interface association descriptor. */
324     DTYPE_CSInterface = 0x24,          /**< Indicates that the descriptor is a class specific interface descriptor. */
325     DTYPE_CSEndpoint = 0x25,           /**< Indicates that the descriptor is a class specific endpoint descriptor. */
326 };
327 
328 /** Enum for possible Class, Subclass and Protocol values of device and interface descriptors. */
329 enum USB_Descriptor_ClassSubclassProtocol_t {
330     USB_CSCP_NoDeviceClass = 0x00,       /**< Descriptor Class value indicating that the device does not belong
331 	                                         *   to a particular class at the device level.
332 	                                         */
333     USB_CSCP_NoDeviceSubclass = 0x00,    /**< Descriptor Subclass value indicating that the device does not belong
334 	                                         *   to a particular subclass at the device level.
335 	                                         */
336     USB_CSCP_NoDeviceProtocol = 0x00,    /**< Descriptor Protocol value indicating that the device does not belong
337 	                                         *   to a particular protocol at the device level.
338 	                                         */
339     USB_CSCP_VendorSpecificClass = 0xFF, /**< Descriptor Class value indicating that the device/interface belongs
340 	                                         *   to a vendor specific class.
341 	                                         */
342     USB_CSCP_VendorSpecificSubclass =
343         0xFF, /**< Descriptor Subclass value indicating that the device/interface belongs
344 	                                         *   to a vendor specific subclass.
345 	                                         */
346     USB_CSCP_VendorSpecificProtocol =
347         0xFF,                          /**< Descriptor Protocol value indicating that the device/interface belongs
348 	                                         *   to a vendor specific protocol.
349 	                                         */
350     USB_CSCP_IADDeviceClass = 0xEF,    /**< Descriptor Class value indicating that the device belongs to the
351 	                                         *   Interface Association Descriptor class.
352 	                                         */
353     USB_CSCP_IADDeviceSubclass = 0x02, /**< Descriptor Subclass value indicating that the device belongs to the
354 	                                         *   Interface Association Descriptor subclass.
355 	                                         */
356     USB_CSCP_IADDeviceProtocol = 0x01, /**< Descriptor Protocol value indicating that the device belongs to the
357 	                                         *   Interface Association Descriptor protocol.
358 	                                         */
359 };
360 
361 /* Type Defines: */
362 /** \brief Standard USB Descriptor Header (LUFA naming conventions).
363  *
364  *  Type define for all descriptors' standard header, indicating the descriptor's length and type. This structure
365  *  uses LUFA-specific element names to make each element's purpose clearer.
366  *
367  *  \see \ref USB_StdDescriptor_Header_t for the version of this type with standard element names.
368  *
369  *  \note Regardless of CPU architecture, these values should be stored as little endian.
370  */
371 typedef struct {
372     u8 Size; /**< Size of the descriptor, in bytes. */
373     u8 Type; /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
374 	               *   given by the specific class.
375 	               */
376 } USB_Descriptor_Header_t;
377 
378 /** \brief Standard USB Descriptor Header (USB-IF naming conventions).
379  *
380  *  Type define for all descriptors' standard header, indicating the descriptor's length and type. This structure
381  *  uses the relevant standard's given element names to ensure compatibility with the standard.
382  *
383  *  \see \ref USB_Descriptor_Header_t for the version of this type with non-standard LUFA specific element names.
384  *
385  *  \note Regardless of CPU architecture, these values should be stored as little endian.
386  */
387 typedef struct {
388     u8 bLength;         /**< Size of the descriptor, in bytes. */
389     u8 bDescriptorType; /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
390 	                          *   given by the specific class.
391 	                          */
392 } USB_StdDescriptor_Header_t;
393 
394 /** \brief Standard USB Device Descriptor (LUFA naming conventions).
395  *
396  *  Type define for a standard Device Descriptor. This structure uses LUFA-specific element names to make each
397  *  element's purpose clearer.
398  *
399  *  \see \ref USB_StdDescriptor_Device_t for the version of this type with standard element names.
400  *
401  *  \note Regardless of CPU architecture, these values should be stored as little endian.
402  */
403 typedef struct {
404     USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
405 
406     u16 USBSpecification; /**< BCD of the supported USB specification. */
407     u8 Class;             /**< USB device class. */
408     u8 SubClass;          /**< USB device subclass. */
409     u8 Protocol;          /**< USB device protocol. */
410 
411     u8 Endpoint0Size; /**< Size of the control (address 0) endpoint's bank in bytes. */
412 
413     u16 VendorID;      /**< Vendor ID for the USB product. */
414     u16 ProductID;     /**< Unique product ID for the USB product. */
415     u16 ReleaseNumber; /**< Product release (version) number. */
416 
417     u8 ManufacturerStrIndex;   /**< String index for the manufacturer's name. The
418 	                                *   host will request this string via a separate
419 	                                *   control request for the string descriptor.
420 	                                *
421 	                                *   \note If no string supplied, use \ref NO_DESCRIPTOR.
422 	                                */
423     u8 ProductStrIndex;        /**< String index for the product name/details.
424 	                           *
425 	                           *  \see ManufacturerStrIndex structure entry.
426 	                           */
427     u8 SerialNumStrIndex;      /**< String index for the product's globally unique hexadecimal
428 	                             *   serial number, in uppercase Unicode ASCII.
429 	                             *
430 	                             *  \note On some microcontroller models, there is an embedded serial number
431 	                             *        in the chip which can be used for the device serial number.
432 	                             *        To use this serial number, set this to USE_INTERNAL_SERIAL.
433 	                             *        On unsupported devices, this will evaluate to 0 and will cause
434 	                             *        the host to generate a pseudo-unique value for the device upon
435 	                             *        insertion.
436 	                             *
437 	                             *  \see ManufacturerStrIndex structure entry.
438 	                             */
439     u8 NumberOfConfigurations; /**< Total number of configurations supported by
440 	                                  *   the device.
441 	                                  */
442 } USB_Descriptor_Device_t;
443 
444 /** \brief Standard USB Device Descriptor (USB-IF naming conventions).
445  *
446  *  Type define for a standard Device Descriptor. This structure uses the relevant standard's given element names
447  *  to ensure compatibility with the standard.
448  *
449  *  \see \ref USB_Descriptor_Device_t for the version of this type with non-standard LUFA specific element names.
450  *
451  *  \note Regardless of CPU architecture, these values should be stored as little endian.
452  */
453 typedef struct {
454     u8 bLength;            /**< Size of the descriptor, in bytes. */
455     u8 bDescriptorType;    /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
456 	                              *   given by the specific class.
457 	                              */
458     u16 bcdUSB;            /**< BCD of the supported USB specification. */
459     u8 bDeviceClass;       /**< USB device class. */
460     u8 bDeviceSubClass;    /**< USB device subclass. */
461     u8 bDeviceProtocol;    /**< USB device protocol. */
462     u8 bMaxPacketSize0;    /**< Size of the control (address 0) endpoint's bank in bytes. */
463     u16 idVendor;          /**< Vendor ID for the USB product. */
464     u16 idProduct;         /**< Unique product ID for the USB product. */
465     u16 bcdDevice;         /**< Product release (version) number. */
466     u8 iManufacturer;      /**< String index for the manufacturer's name. The
467 	                         *   host will request this string via a separate
468 	                         *   control request for the string descriptor.
469 	                         *
470 	                         *   \note If no string supplied, use \ref NO_DESCRIPTOR.
471 	                         */
472     u8 iProduct;           /**< String index for the product name/details.
473 	                    *
474 	                    *  \see ManufacturerStrIndex structure entry.
475 	                    */
476     u8 iSerialNumber;      /**< String index for the product's globally unique hexadecimal
477 	                        *   serial number, in uppercase Unicode ASCII.
478 	                        *
479 	                        *  \note On some microcontroller models, there is an embedded serial number
480 	                        *        in the chip which can be used for the device serial number.
481 	                        *        To use this serial number, set this to USE_INTERNAL_SERIAL.
482 	                        *        On unsupported devices, this will evaluate to 0 and will cause
483 	                        *        the host to generate a pseudo-unique value for the device upon
484 	                        *        insertion.
485 	                        *
486 	                        *  \see ManufacturerStrIndex structure entry.
487 	                        */
488     u8 bNumConfigurations; /**< Total number of configurations supported by
489 	                              *   the device.
490 	                              */
491 } USB_StdDescriptor_Device_t;
492 
493 /** \brief Standard USB Device Qualifier Descriptor (LUFA naming conventions).
494  *
495  *  Type define for a standard Device Qualifier Descriptor. This structure uses LUFA-specific element names
496  *  to make each element's purpose clearer.
497  *
498  *  \see \ref USB_StdDescriptor_DeviceQualifier_t for the version of this type with standard element names.
499  */
500 typedef struct {
501     USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
502 
503     u16 USBSpecification; /**< BCD of the supported USB specification. */
504     u8 Class;             /**< USB device class. */
505     u8 SubClass;          /**< USB device subclass. */
506     u8 Protocol;          /**< USB device protocol. */
507 
508     u8 Endpoint0Size;          /**< Size of the control (address 0) endpoint's bank in bytes. */
509     u8 NumberOfConfigurations; /**< Total number of configurations supported by
510 	                                  *   the device.
511 	                                  */
512     u8 Reserved;               /**< Reserved for future use, must be 0. */
513 } USB_Descriptor_DeviceQualifier_t;
514 
515 /** \brief Standard USB Device Qualifier Descriptor (USB-IF naming conventions).
516  *
517  *  Type define for a standard Device Qualifier Descriptor. This structure uses the relevant standard's given element
518  *  names to ensure compatibility with the standard.
519  *
520  *  \see \ref USB_Descriptor_DeviceQualifier_t for the version of this type with non-standard LUFA specific element
521  * names.
522  */
523 typedef struct {
524     u8 bLength;            /**< Size of the descriptor, in bytes. */
525     u8 bDescriptorType;    /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
526 	                              *   given by the specific class.
527 	                              */
528     u16 bcdUSB;            /**< BCD of the supported USB specification. */
529     u8 bDeviceClass;       /**< USB device class. */
530     u8 bDeviceSubClass;    /**< USB device subclass. */
531     u8 bDeviceProtocol;    /**< USB device protocol. */
532     u8 bMaxPacketSize0;    /**< Size of the control (address 0) endpoint's bank in bytes. */
533     u8 bNumConfigurations; /**< Total number of configurations supported by
534 	                              *   the device.
535 	                              */
536     u8 bReserved;          /**< Reserved for future use, must be 0. */
537 } USB_StdDescriptor_DeviceQualifier_t;
538 
539 /** \brief Standard USB Configuration Descriptor (LUFA naming conventions).
540  *
541  *  Type define for a standard Configuration Descriptor header. This structure uses LUFA-specific element names
542  *  to make each element's purpose clearer.
543  *
544  *  \see \ref USB_StdDescriptor_Configuration_Header_t for the version of this type with standard element names.
545  *
546  *  \note Regardless of CPU architecture, these values should be stored as little endian.
547  */
548 typedef struct {
549     USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
550 
551     u16 TotalConfigurationSize; /**< Size of the configuration descriptor header,
552 	                                  *   and all sub descriptors inside the configuration.
553 	                                  */
554     u8 TotalInterfaces;         /**< Total number of interfaces in the configuration. */
555 
556     u8 ConfigurationNumber;   /**< Configuration index of the current configuration. */
557     u8 ConfigurationStrIndex; /**< Index of a string descriptor describing the configuration. */
558 
559     u8 ConfigAttributes; /**< Configuration attributes, comprised of a mask of \c USB_CONFIG_ATTR_* masks.
560 	                            *   On all devices, this should include USB_CONFIG_ATTR_RESERVED at a minimum.
561 	                            */
562 
563     u8 MaxPowerConsumption; /**< Maximum power consumption of the device while in the
564 	                               *   current configuration, calculated by the \ref USB_CONFIG_POWER_MA()
565 	                               *   macro.
566 	                               */
567 } __attribute__((packed)) USB_Descriptor_Configuration_Header_t;
568 
569 /** \brief Standard USB Configuration Descriptor (USB-IF naming conventions).
570  *
571  *  Type define for a standard Configuration Descriptor header. This structure uses the relevant standard's given
572  *   element names to ensure compatibility with the standard.
573  *
574  *  \see \ref USB_Descriptor_Device_t for the version of this type with non-standard LUFA specific element names.
575  *
576  *  \note Regardless of CPU architecture, these values should be stored as little endian.
577  */
578 typedef struct {
579     u8 bLength;             /**< Size of the descriptor, in bytes. */
580     u8 bDescriptorType;     /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
581 	                              *   given by the specific class.
582 	                              */
583     u16 wTotalLength;       /**< Size of the configuration descriptor header,
584 	                           *   and all sub descriptors inside the configuration.
585 	                           */
586     u8 bNumInterfaces;      /**< Total number of interfaces in the configuration. */
587     u8 bConfigurationValue; /**< Configuration index of the current configuration. */
588     u8 iConfiguration;      /**< Index of a string descriptor describing the configuration. */
589     u8 bmAttributes;        /**< Configuration attributes, comprised of a mask of \c USB_CONFIG_ATTR_* masks.
590 	                        *   On all devices, this should include USB_CONFIG_ATTR_RESERVED at a minimum.
591 	                        */
592     u8 bMaxPower;           /**< Maximum power consumption of the device while in the
593 	                     *   current configuration, calculated by the \ref USB_CONFIG_POWER_MA()
594 	                     *   macro.
595 	                     */
596 } USB_StdDescriptor_Configuration_Header_t;
597 
598 /** \brief Standard USB Interface Descriptor (LUFA naming conventions).
599  *
600  *  Type define for a standard Interface Descriptor. This structure uses LUFA-specific element names
601  *  to make each element's purpose clearer.
602  *
603  *  \see \ref USB_StdDescriptor_Interface_t for the version of this type with standard element names.
604  *
605  *  \note Regardless of CPU architecture, these values should be stored as little endian.
606  */
607 typedef struct {
608     USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
609 
610     u8 InterfaceNumber;  /**< Index of the interface in the current configuration. */
611     u8 AlternateSetting; /**< Alternate setting for the interface number. The same
612 	                           *   interface number can have multiple alternate settings
613 	                           *   with different endpoint configurations, which can be
614 	                           *   selected by the host.
615 	                           */
616     u8 TotalEndpoints;   /**< Total number of endpoints in the interface. */
617 
618     u8 Class;    /**< Interface class ID. */
619     u8 SubClass; /**< Interface subclass ID. */
620     u8 Protocol; /**< Interface protocol ID. */
621 
622     u8 InterfaceStrIndex; /**< Index of the string descriptor describing the interface. */
623 } __attribute__((packed)) USB_Descriptor_Interface_t;
624 
625 /** \brief Standard USB Interface Descriptor (USB-IF naming conventions).
626  *
627  *  Type define for a standard Interface Descriptor. This structure uses the relevant standard's given element names
628  *  to ensure compatibility with the standard.
629  *
630  *  \see \ref USB_Descriptor_Interface_t for the version of this type with non-standard LUFA specific element names.
631  *
632  *  \note Regardless of CPU architecture, these values should be stored as little endian.
633  */
634 typedef struct {
635     u8 bLength;            /**< Size of the descriptor, in bytes. */
636     u8 bDescriptorType;    /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
637 	                          *   given by the specific class.
638 	                          */
639     u8 bInterfaceNumber;   /**< Index of the interface in the current configuration. */
640     u8 bAlternateSetting;  /**< Alternate setting for the interface number. The same
641 	                            *   interface number can have multiple alternate settings
642 	                            *   with different endpoint configurations, which can be
643 	                            *   selected by the host.
644 	                            */
645     u8 bNumEndpoints;      /**< Total number of endpoints in the interface. */
646     u8 bInterfaceClass;    /**< Interface class ID. */
647     u8 bInterfaceSubClass; /**< Interface subclass ID. */
648     u8 bInterfaceProtocol; /**< Interface protocol ID. */
649     u8 iInterface;         /**< Index of the string descriptor describing the
650 	                     *   interface.
651 	                     */
652 } USB_StdDescriptor_Interface_t;
653 
654 /** \brief Standard USB Interface Association Descriptor (LUFA naming conventions).
655  *
656  *  Type define for a standard Interface Association Descriptor. This structure uses LUFA-specific element names
657  *  to make each element's purpose clearer.
658  *
659  *  This descriptor has been added as a supplement to the USB2.0 standard, in the ECN located at
660  *  <a>http://www.usb.org/developers/docs/InterfaceAssociationDescriptor_ecn.pdf</a>. It allows composite
661  *  devices with multiple interfaces related to the same function to have the multiple interfaces bound
662  *  together at the point of enumeration, loading one generic driver for all the interfaces in the single
663  *  function. Read the ECN for more information.
664  *
665  *  \see \ref USB_StdDescriptor_Interface_Association_t for the version of this type with standard element names.
666  *
667  *  \note Regardless of CPU architecture, these values should be stored as little endian.
668  */
669 typedef struct {
670     USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
671 
672     u8 FirstInterfaceIndex; /**< Index of the first associated interface. */
673     u8 TotalInterfaces;     /**< Total number of associated interfaces. */
674 
675     u8 Class;    /**< Interface class ID. */
676     u8 SubClass; /**< Interface subclass ID. */
677     u8 Protocol; /**< Interface protocol ID. */
678 
679     u8 IADStrIndex; /**< Index of the string descriptor describing the
680 	                      *   interface association.
681 	                      */
682 } USB_Descriptor_Interface_Association_t;
683 
684 /** \brief Standard USB Interface Association Descriptor (USB-IF naming conventions).
685  *
686  *  Type define for a standard Interface Association Descriptor. This structure uses the relevant standard's given
687  *  element names to ensure compatibility with the standard.
688  *
689  *  This descriptor has been added as a supplement to the USB2.0 standard, in the ECN located at
690  *  <a>http://www.usb.org/developers/docs/InterfaceAssociationDescriptor_ecn.pdf</a>. It allows composite
691  *  devices with multiple interfaces related to the same function to have the multiple interfaces bound
692  *  together at the point of enumeration, loading one generic driver for all the interfaces in the single
693  *  function. Read the ECN for more information.
694  *
695  *  \see \ref USB_Descriptor_Interface_Association_t for the version of this type with non-standard LUFA specific
696  *       element names.
697  *
698  *  \note Regardless of CPU architecture, these values should be stored as little endian.
699  */
700 typedef struct {
701     u8 bLength;           /**< Size of the descriptor, in bytes. */
702     u8 bDescriptorType;   /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a value
703 	                          *   given by the specific class.
704 	                          */
705     u8 bFirstInterface;   /**< Index of the first associated interface. */
706     u8 bInterfaceCount;   /**< Total number of associated interfaces. */
707     u8 bFunctionClass;    /**< Interface class ID. */
708     u8 bFunctionSubClass; /**< Interface subclass ID. */
709     u8 bFunctionProtocol; /**< Interface protocol ID. */
710     u8 iFunction;         /**< Index of the string descriptor describing the
711 	                    *   interface association.
712 	                    */
713 } USB_StdDescriptor_Interface_Association_t;
714 
715 /** \brief Standard USB Endpoint Descriptor (LUFA naming conventions).
716  *
717  *  Type define for a standard Endpoint Descriptor. This structure uses LUFA-specific element names
718  *  to make each element's purpose clearer.
719  *
720  *  \see \ref USB_StdDescriptor_Endpoint_t for the version of this type with standard element names.
721  *
722  *  \note Regardless of CPU architecture, these values should be stored as little endian.
723  */
724 typedef struct {
725     USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
726 
727     u8 EndpointAddress;   /**< Logical address of the endpoint within the device for the current
728 	                           *   configuration, including direction mask.
729 	                           */
730     u8 Attributes;        /**< Endpoint attributes, comprised of a mask of the endpoint type (EP_TYPE_*)
731 	                      *   and attributes (ENDPOINT_ATTR_*) masks.
732 	                      */
733     u16 EndpointSize;     /**< Size of the endpoint bank, in bytes. This indicates the maximum packet
734 	                        *   size that the endpoint can receive at a time.
735 	                        */
736     u8 PollingIntervalMS; /**< Polling interval in milliseconds for the endpoint if it is an INTERRUPT
737 	                             *   or ISOCHRONOUS type.
738 	                             */
739 } __attribute__((packed)) USB_Descriptor_Endpoint_t;
740 
741 /** \brief Standard USB Endpoint Descriptor (USB-IF naming conventions).
742  *
743  *  Type define for a standard Endpoint Descriptor. This structure uses the relevant standard's given
744  *  element names to ensure compatibility with the standard.
745  *
746  *  \see \ref USB_Descriptor_Endpoint_t for the version of this type with non-standard LUFA specific
747  *       element names.
748  *
749  *  \note Regardless of CPU architecture, these values should be stored as little endian.
750  */
751 typedef struct {
752     u8 bLength;          /**< Size of the descriptor, in bytes. */
753     u8 bDescriptorType;  /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t or a
754 	                           *   value given by the specific class.
755 	                           */
756     u8 bEndpointAddress; /**< Logical address of the endpoint within the device for the current
757 	                            *   configuration, including direction mask.
758 	                            */
759     u8 bmAttributes;     /**< Endpoint attributes, comprised of a mask of the endpoint type (EP_TYPE_*)
760 	                        *   and attributes (ENDPOINT_ATTR_*) masks.
761 	                        */
762     u16 wMaxPacketSize;  /**< Size of the endpoint bank, in bytes. This indicates the maximum packet size
763 	                          *   that the endpoint can receive at a time.
764 	                          */
765     u8 bInterval;        /**< Polling interval in milliseconds for the endpoint if it is an INTERRUPT or
766 	                     *   ISOCHRONOUS type.
767 	                     */
768 } __attribute__((packed)) USB_StdDescriptor_Endpoint_t;
769 
770 /** \brief Standard USB String Descriptor (LUFA naming conventions).
771  *
772  *  Type define for a standard string descriptor. Unlike other standard descriptors, the length
773  *  of the descriptor for placement in the descriptor header must be determined by the \ref USB_STRING_LEN()
774  *  macro rather than by the size of the descriptor structure, as the length is not fixed.
775  *
776  *  This structure should also be used for string index 0, which contains the supported language IDs for
777  *  the device as an array.
778  *
779  *  This structure uses LUFA-specific element names to make each element's purpose clearer.
780  *
781  *  \see \ref USB_StdDescriptor_String_t for the version of this type with standard element names.
782  *
783  *  \note Regardless of CPU architecture, these values should be stored as little endian.
784  */
785 #ifndef __GNUC__
786 #pragma warning(push)
787 #pragma warning(disable : 4200)  // none standard zero length array
788 #endif
789 
790 typedef struct {
791     USB_Descriptor_Header_t Header; /**< Descriptor header, including type and size. */
792     wchar_t UnicodeString[];        /**< String data, as unicode characters (alternatively,
793 	                           *   string language IDs). If normal ASCII characters are
794 	                           *   to be used, they must be added as an array of characters
795 	                           *   rather than a normal C string so that they are widened to
796 	                           *   Unicode size.
797 	                           *
798 	                           *   Under GCC, strings prefixed with the "L" character (before
799 	                           *   the opening string quotation mark) are considered to be
800 	                           *   Unicode strings, and may be used instead of an explicit
801 	                           *   array of ASCII characters on little endian devices with
802 	                           *   UTF-16-LE \c wchar_t encoding.
803 	                           */
804 } USB_Descriptor_String_t;
805 
806 /** \brief Standard USB String Descriptor (USB-IF naming conventions).
807  *
808  *  Type define for a standard string descriptor. Unlike other standard descriptors, the length
809  *  of the descriptor for placement in the descriptor header must be determined by the \ref USB_STRING_LEN()
810  *  macro rather than by the size of the descriptor structure, as the length is not fixed.
811  *
812  *  This structure should also be used for string index 0, which contains the supported language IDs for
813  *  the device as an array.
814  *
815  *  This structure uses the relevant standard's given element names to ensure compatibility with the standard.
816  *
817  *  \see \ref USB_Descriptor_String_t for the version of this type with with non-standard LUFA specific
818  *       element names.
819  *
820  *  \note Regardless of CPU architecture, these values should be stored as little endian.
821  */
822 typedef struct {
823     u8 bLength;         /**< Size of the descriptor, in bytes. */
824     u8 bDescriptorType; /**< Type of the descriptor, either a value in \ref USB_DescriptorTypes_t
825 	                          *   or a value given by the specific class.
826 	                          */
827     wchar_t bString[];  /**< String data, as unicode characters (alternatively, string language IDs).
828 	                     *   If normal ASCII characters are to be used, they must be added as an array
829 	                     *   of characters rather than a normal C string so that they are widened to
830 	                     *   Unicode size.
831 	                     *
832 	                     *   Under GCC, strings prefixed with the "L" character (before the opening string
833 	                     *   quotation mark) are considered to be Unicode strings, and may be used instead
834 	                     *   of an explicit array of ASCII characters.
835 	                     */
836 } USB_StdDescriptor_String_t;
837 
838 ////////////////////////////////////////////////////////////////////////////////////////////////
839 #if 1
840 
841 #define MYUDB_EDP_IN_HCI  3
842 #define MYUDB_EDP_OUT_HCI 5
843 #define MYUDB_EDP_IN_VCD  8
844 #define MYUDB_EDP_OUT_VCD 6
845 
846 #else
847 
848 #define MYUDB_EDP_IN_HCI  8
849 #define MYUDB_EDP_OUT_HCI 5
850 #define MYUDB_EDP_IN_VCD  3
851 #define MYUDB_EDP_OUT_VCD 6
852 
853 #endif
854 
855 #define ENDPOINT_DIR_MASK   BIT(7)
856 #define ENDPOINT_DIR_OUT    0
857 #define ENDPOINT_DIR_IN     BIT(7)
858 #define EP_TYPE_MASK        3
859 #define EP_TYPE_CONTROL     0
860 #define EP_TYPE_ISOCHRONOUS 1
861 #define EP_TYPE_BULK        2
862 #define EP_TYPE_INTERRUPT   3
863 
864 enum PRNT_Descriptor_ClassSubclassProtocol_t {
865     PRNT_CSCP_PrinterClass = 0x07,
866     PRNT_CSCP_PrinterSubclass = 0x01,
867     PRNT_CSCP_BidirectionalProtocol = 0x02,
868 };
869 
870 enum {
871     MYUDB_USB_STRING_LANGUAGE = 0,
872     MYUDB_USB_STRING_VENDOR,
873     MYUDB_USB_STRING_PRODUCT,
874     MYUDB_USB_STRING_SERIAL,
875 };
876 
877 typedef struct {
878     USB_Descriptor_Configuration_Header_t Config;
879     USB_Descriptor_Interface_t intf0;
880     USB_Descriptor_Endpoint_t endpi0;
881     USB_Descriptor_Endpoint_t endpo0;
882     USB_Descriptor_Interface_t intf1;
883     USB_Descriptor_Endpoint_t endpi1;
884     USB_Descriptor_Endpoint_t endpo1;
885 } MYUDB_USB_Descriptor_Configuration_t;
886 
887 u8 *myudb_usbdesc_get_language(void);
888 u8 *myudb_usbdesc_get_vendor(void);
889 u8 *myudb_usbdesc_get_product(void);
890 u8 *myudb_usbdesc_get_serial(void);
891 u8 *myudb_usbdesc_get_device(void);
892 u8 *myudb_usbdesc_get_configuration(void);
893 
894 #endif
895