2 * The Clear BSD License
3 * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4 * Copyright 2016-2017 NXP
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted (subject to the limitations in the disclaimer below) provided
9 * that the following conditions are met:
11 * o Redistributions of source code must retain the above copyright notice, this list
12 * of conditions and the following disclaimer.
14 * o Redistributions in binary form must reproduce the above copyright notice, this
15 * list of conditions and the following disclaimer in the documentation and/or
16 * other materials provided with the distribution.
18 * o Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "usb_device_config.h"
36 #include "usb_device.h"
38 #include "usb_device_cdc_acm.h"
40 #include "usb_device_descriptor.h"
42 /*******************************************************************************
44 ******************************************************************************/
45 uint8_t g_currentConfigure = 0;
46 uint8_t g_interface[USB_CDC_VCOM_INTERFACE_COUNT];
48 /* Define device descriptor */
49 uint8_t g_UsbDeviceDescriptor[USB_DESCRIPTOR_LENGTH_DEVICE] = {
50 /* Size of this descriptor in bytes */
51 USB_DESCRIPTOR_LENGTH_DEVICE,
52 /* DEVICE Descriptor Type */
53 USB_DESCRIPTOR_TYPE_DEVICE,
54 /* USB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). */
55 USB_SHORT_GET_LOW(USB_DEVICE_SPECIFIC_BCD_VERSION), USB_SHORT_GET_HIGH(USB_DEVICE_SPECIFIC_BCD_VERSION),
56 /* Class code (assigned by the USB-IF). */
58 /* Subclass code (assigned by the USB-IF). */
60 /* Protocol code (assigned by the USB-IF). */
62 /* Maximum packet size for endpoint zero (only 8, 16, 32, or 64 are valid) */
63 USB_CONTROL_MAX_PACKET_SIZE,
64 /* Vendor ID (assigned by the USB-IF) */
66 /* Product ID (assigned by the manufacturer) */
68 /* Device release number in binary-coded decimal */
69 USB_SHORT_GET_LOW(USB_DEVICE_DEMO_BCD_VERSION), USB_SHORT_GET_HIGH(USB_DEVICE_DEMO_BCD_VERSION),
70 /* Index of string descriptor describing manufacturer */
72 /* Index of string descriptor describing product */
74 /* Index of string descriptor describing the device's serial number */
76 /* Number of possible configurations */
77 USB_DEVICE_CONFIGURATION_COUNT,
80 /* Define configuration descriptor */
81 uint8_t g_UsbDeviceConfigurationDescriptor[USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL] = {
82 /* Size of this descriptor in bytes */
83 USB_DESCRIPTOR_LENGTH_CONFIGURE,
84 /* CONFIGURATION Descriptor Type */
85 USB_DESCRIPTOR_TYPE_CONFIGURE,
86 /* Total length of data returned for this configuration. */
87 USB_SHORT_GET_LOW(USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL),
88 USB_SHORT_GET_HIGH(USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL),
89 /* Number of interfaces supported by this configuration */
90 USB_CDC_VCOM_INTERFACE_COUNT,
91 /* Value to use as an argument to the SetConfiguration() request to select this configuration */
92 USB_CDC_VCOM_CONFIGURE_INDEX,
93 /* Index of string descriptor describing this configuration */
95 /* Configuration characteristics D7: Reserved (set to one) D6: Self-powered D5: Remote Wakeup D4...0: Reserved
97 (USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_MASK) |
98 (USB_DEVICE_CONFIG_SELF_POWER << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_SHIFT) |
99 (USB_DEVICE_CONFIG_REMOTE_WAKEUP << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_SHIFT),
100 /* Maximum power consumption of the USB * device from the bus in this specific * configuration when the device is
101 fully * operational. Expressed in 2 mA units * (i.e., 50 = 100 mA). */
102 USB_DEVICE_MAX_POWER,
104 /* Communication Interface Descriptor */
105 USB_DESCRIPTOR_LENGTH_INTERFACE, USB_DESCRIPTOR_TYPE_INTERFACE, USB_CDC_VCOM_COMM_INTERFACE_INDEX, 0x00,
106 USB_CDC_VCOM_ENDPOINT_CIC_COUNT, USB_CDC_VCOM_CIC_CLASS, USB_CDC_VCOM_CIC_SUBCLASS, USB_CDC_VCOM_CIC_PROTOCOL,
107 0x00, /* Interface Description String Index*/
109 /* CDC Class-Specific descriptor */
110 USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC, /* Size of this descriptor in bytes */
111 USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
112 USB_CDC_HEADER_FUNC_DESC, 0x10,
113 0x01, /* USB Class Definitions for Communications the Communication specification version 1.10 */
115 USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG, /* Size of this descriptor in bytes */
116 USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
117 USB_CDC_CALL_MANAGEMENT_FUNC_DESC,
118 0x01, /*Bit 0: Whether device handle call management itself 1, Bit 1: Whether device can send/receive call
119 management information over a Data Class Interface 0 */
120 0x01, /* Indicates multiplexed commands are handled via data interface */
122 USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT, /* Size of this descriptor in bytes */
123 USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
124 USB_CDC_ABSTRACT_CONTROL_FUNC_DESC,
125 0x06, /* Bit 0: Whether device supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and
126 Get_Comm_Feature 0, Bit 1: Whether device supports the request combination of Set_Line_Coding,
127 Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State 1, Bit ... */
129 USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC, /* Size of this descriptor in bytes */
130 USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
131 USB_CDC_UNION_FUNC_DESC, 0x00, /* The interface number of the Communications or Data Class interface */
132 0x01, /* Interface number of subordinate interface in the Union */
134 /*Notification Endpoint descriptor */
135 USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT | (USB_IN << 7U),
136 USB_ENDPOINT_INTERRUPT, USB_SHORT_GET_LOW(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE),
137 USB_SHORT_GET_HIGH(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE), FS_CDC_VCOM_INTERRUPT_IN_INTERVAL,
139 /* Data Interface Descriptor */
140 USB_DESCRIPTOR_LENGTH_INTERFACE, USB_DESCRIPTOR_TYPE_INTERFACE, USB_CDC_VCOM_DATA_INTERFACE_INDEX, 0x00,
141 USB_CDC_VCOM_ENDPOINT_DIC_COUNT, USB_CDC_VCOM_DIC_CLASS, USB_CDC_VCOM_DIC_SUBCLASS, USB_CDC_VCOM_DIC_PROTOCOL,
142 0x00, /* Interface Description String Index*/
144 /*Bulk IN Endpoint descriptor */
145 USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_BULK_IN_ENDPOINT | (USB_IN << 7U),
146 USB_ENDPOINT_BULK, USB_SHORT_GET_LOW(FS_CDC_VCOM_BULK_IN_PACKET_SIZE),
147 USB_SHORT_GET_HIGH(FS_CDC_VCOM_BULK_IN_PACKET_SIZE), 0x00, /* The polling interval value is every 0 Frames */
149 /*Bulk OUT Endpoint descriptor */
150 USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_BULK_OUT_ENDPOINT | (USB_OUT << 7U),
151 USB_ENDPOINT_BULK, USB_SHORT_GET_LOW(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE),
152 USB_SHORT_GET_HIGH(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE), 0x00, /* The polling interval value is every 0 Frames */
155 /* Define string descriptor */
156 uint8_t g_UsbDeviceString0[USB_DESCRIPTOR_LENGTH_STRING0] = {sizeof(g_UsbDeviceString0), USB_DESCRIPTOR_TYPE_STRING,
159 uint8_t g_UsbDeviceString1[USB_DESCRIPTOR_LENGTH_STRING1] = {
160 sizeof(g_UsbDeviceString1),
161 USB_DESCRIPTOR_TYPE_STRING,
200 uint8_t g_UsbDeviceString2[USB_DESCRIPTOR_LENGTH_STRING2] = {sizeof(g_UsbDeviceString2),
201 USB_DESCRIPTOR_TYPE_STRING,
243 uint8_t *g_UsbDeviceStringDescriptorArray[USB_DEVICE_STRING_COUNT] = {g_UsbDeviceString0, g_UsbDeviceString1,
246 /* Define string descriptor size */
247 uint32_t g_UsbDeviceStringDescriptorLength[USB_DEVICE_STRING_COUNT] = {
248 sizeof(g_UsbDeviceString0), sizeof(g_UsbDeviceString1), sizeof(g_UsbDeviceString2)};
249 usb_language_t g_UsbDeviceLanguage[USB_DEVICE_LANGUAGE_COUNT] = {{
250 g_UsbDeviceStringDescriptorArray, g_UsbDeviceStringDescriptorLength, (uint16_t)0x0409,
253 usb_language_list_t g_UsbDeviceLanguageList = {
254 g_UsbDeviceString0, sizeof(g_UsbDeviceString0), g_UsbDeviceLanguage, USB_DEVICE_LANGUAGE_COUNT,
257 /*******************************************************************************
259 ******************************************************************************/
261 * @brief Get the descritpor.
263 * The function is used to get the descritpor, including the device descritpor, configuration descriptor, and string
266 * @param handle The device handle.
267 * @param setup The setup packet buffer address.
268 * @param length It is an OUT parameter, return the data length need to be sent to host.
269 * @param buffer It is an OUT parameter, return the data buffer address.
271 * @return A USB error code or kStatus_USB_Success.
273 usb_status_t USB_DeviceGetDescriptor(usb_device_handle handle,
274 usb_setup_struct_t *setup,
278 uint8_t descriptorType = (uint8_t)((setup->wValue & 0xFF00U) >> 8U);
279 uint8_t descriptorIndex = (uint8_t)((setup->wValue & 0x00FFU));
280 usb_status_t ret = kStatus_USB_Success;
281 if (USB_REQUEST_STANDARD_GET_DESCRIPTOR != setup->bRequest)
283 return kStatus_USB_InvalidRequest;
285 switch (descriptorType)
287 case USB_DESCRIPTOR_TYPE_STRING:
289 if (descriptorIndex == 0)
291 *buffer = (uint8_t *)g_UsbDeviceLanguageList.languageString;
292 *length = g_UsbDeviceLanguageList.stringLength;
297 uint8_t langIndex = USB_DEVICE_STRING_COUNT;
299 for (; langId < USB_DEVICE_LANGUAGE_COUNT; langId++)
301 if (setup->wIndex == g_UsbDeviceLanguageList.languageList[langId].languageId)
303 if (descriptorIndex < USB_DEVICE_STRING_COUNT)
305 langIndex = descriptorIndex;
311 if (USB_DEVICE_STRING_COUNT == langIndex)
315 *buffer = (uint8_t *)g_UsbDeviceLanguageList.languageList[langId].string[langIndex];
316 *length = g_UsbDeviceLanguageList.languageList[langId].length[langIndex];
320 case USB_DESCRIPTOR_TYPE_DEVICE:
322 *buffer = g_UsbDeviceDescriptor;
323 *length = USB_DESCRIPTOR_LENGTH_DEVICE;
326 case USB_DESCRIPTOR_TYPE_CONFIGURE:
328 *buffer = g_UsbDeviceConfigurationDescriptor;
329 *length = USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL;
333 ret = kStatus_USB_InvalidRequest;
340 * @brief Set the device configuration.
342 * The function is used to set the device configuration.
344 * @param handle The device handle.
345 * @param configure The configuration value.
347 * @return A USB error code or kStatus_USB_Success.
349 usb_status_t USB_DeviceSetConfigure(usb_device_handle handle, uint8_t configure)
353 return kStatus_USB_Error;
355 g_currentConfigure = configure;
356 return USB_DeviceCallback(handle, kUSB_DeviceEventSetConfiguration, &configure);
360 * @brief Get the device configuration.
362 * The function is used to get the device configuration.
364 * @param handle The device handle.
365 * @param configure It is an OUT parameter, save the current configuration value.
367 * @return A USB error code or kStatus_USB_Success.
369 usb_status_t USB_DeviceGetConfigure(usb_device_handle handle, uint8_t *configure)
371 *configure = g_currentConfigure;
372 return kStatus_USB_Success;
376 * @brief Set an interface alternate setting.
378 * The function is used to set an interface alternate setting.
380 * @param handle The device handle.
381 * @param interface The interface index.
382 * @param alternateSetting The new alternate setting value.
384 * @return A USB error code or kStatus_USB_Success.
386 usb_status_t USB_DeviceSetInterface(usb_device_handle handle, uint8_t interface, uint8_t alternateSetting)
388 g_interface[interface] = alternateSetting;
389 return USB_DeviceCallback(handle, kUSB_DeviceEventSetInterface, &interface);
393 * @brief Get an interface alternate setting.
395 * The function is used to get an interface alternate setting.
397 * @param handle The device handle.
398 * @param interface The interface index.
399 * @param alternateSetting It is an OUT parameter, save the new alternate setting value of the interface.
401 * @return A USB error code or kStatus_USB_Success.
403 usb_status_t USB_DeviceGetInterface(usb_device_handle handle, uint8_t interface, uint8_t *alternateSetting)
405 *alternateSetting = g_interface[interface];
406 return kStatus_USB_Success;
410 * @brief USB device set speed function.
412 * This function sets the speed of the USB device.
414 * Due to the difference of HS and FS descriptors, the device descriptors and configurations need to be updated to match
416 * As the default, the device descriptors and configurations are configured by using FS parameters for both EHCI and
418 * When the EHCI is enabled, the application needs to call this fucntion to update device by using current speed.
419 * The updated information includes endpoint max packet size, endpoint interval, etc.
421 * @param handle The USB device handle.
422 * @param speed Speed type. USB_SPEED_HIGH/USB_SPEED_FULL/USB_SPEED_LOW.
424 * @return A USB error code or kStatus_USB_Success.
426 usb_status_t USB_DeviceSetSpeed(usb_device_handle handle, uint8_t speed)
428 usb_descriptor_union_t *ptr1;
429 usb_descriptor_union_t *ptr2;
431 ptr1 = (usb_descriptor_union_t *)(&g_UsbDeviceConfigurationDescriptor[0]);
432 ptr2 = (usb_descriptor_union_t *)(&g_UsbDeviceConfigurationDescriptor[USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL - 1]);
436 if (ptr1->common.bDescriptorType == USB_DESCRIPTOR_TYPE_ENDPOINT)
438 if (USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT == (ptr1->endpoint.bEndpointAddress & 0x0FU))
440 if (USB_SPEED_HIGH == speed)
442 ptr1->endpoint.bInterval = HS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
443 USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
444 ptr1->endpoint.wMaxPacketSize);
448 ptr1->endpoint.bInterval = FS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
449 USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
450 ptr1->endpoint.wMaxPacketSize);
453 else if (USB_CDC_VCOM_BULK_IN_ENDPOINT == (ptr1->endpoint.bEndpointAddress & 0x0FU))
455 if (USB_SPEED_HIGH == speed)
457 USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
461 USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
464 else if (USB_CDC_VCOM_BULK_OUT_ENDPOINT == (ptr1->endpoint.bEndpointAddress & 0x0FU))
466 if (USB_SPEED_HIGH == speed)
468 USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
472 USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
479 ptr1 = (usb_descriptor_union_t *)((uint8_t *)ptr1 + ptr1->common.bLength);
481 return kStatus_USB_Success;
485 USB_DeviceSetVendorId (uint16_t vid)
487 g_UsbDeviceDescriptor[8] = (vid >> 0) & 0xff;
488 g_UsbDeviceDescriptor[9] = (vid >> 8) & 0xff;
492 USB_DeviceSetProductId (uint16_t pid)
494 g_UsbDeviceDescriptor[10] = (pid >> 0) & 0xff;
495 g_UsbDeviceDescriptor[11] = (pid >> 8) & 0xff;