MCUXpresso_LPC55S69/boards/lpcxpresso55s69/usb_examples/usb_device_printer_virtual_plain_text/bm/cm33_core0/usb_device_descriptor.c
2022-04-08 22:42:47 +08:00

422 lines
18 KiB
C

/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016, 2019 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "usb_device_config.h"
#include "usb.h"
#include "usb_device.h"
#include "usb_device_class.h"
#include "usb_device_printer.h"
#include "usb_device_descriptor.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
/* printer endpoints information */
usb_device_endpoint_struct_t g_UsbDevicePrinterEndpoints[USB_PRINTER_ENDPOINT_COUNT] = {
/* printer BULK OUT pipe */
{
USB_PRINTER_BULK_ENDPOINT_OUT | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
USB_ENDPOINT_BULK,
FS_PRINTER_BULK_OUT_PACKET_SIZE,
0U,
},
/* printer BULK IN pipe */
{
USB_PRINTER_BULK_ENDPOINT_IN | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
USB_ENDPOINT_BULK,
FS_PRINTER_BULK_IN_PACKET_SIZE,
0U,
},
};
/* printer interface information */
usb_device_interface_struct_t g_UsbDevicePrinterInterface[] = {{
USB_PRINTER_INTERFACE_ALTERNATE_0, /* The alternate setting of the interface */
{
USB_PRINTER_ENDPOINT_COUNT, /* Endpoint count */
g_UsbDevicePrinterEndpoints, /* Endpoints handle */
},
NULL,
}};
usb_device_interfaces_struct_t g_UsbDevicePrinterInterfaces[USB_PRINTER_INTERFACE_COUNT] = {
{
USB_PRINTER_CLASS, /* printer demo class code */
USB_PRINTER_SUBCLASS, /* printer demo subclass code */
USB_PRINTER_PROTOCOL, /* printer demo protocol code */
USB_PRINTER_INTERFACE_INDEX, /* The interface number of the printer demo */
g_UsbDevicePrinterInterface, /* Interfaces handle */
sizeof(g_UsbDevicePrinterInterface) / sizeof(usb_device_interface_struct_t),
},
};
usb_device_interface_list_t g_UsbDevicePrinterInterfaceList[USB_DEVICE_CONFIGURATION_COUNT] = {
{
USB_PRINTER_INTERFACE_COUNT, /* The interface count of the printer demo */
g_UsbDevicePrinterInterfaces, /* The interfaces handle */
},
};
usb_device_class_struct_t g_UsbDevicePrinterClass = {
g_UsbDevicePrinterInterfaceList, /* The interface list of the printer demo */
kUSB_DeviceClassTypePrinter, /* The printer demo class type */
USB_DEVICE_CONFIGURATION_COUNT, /* The configuration count */
};
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceDescriptor[] = {
USB_DESCRIPTOR_LENGTH_DEVICE, /* Size of this descriptor in bytes */
USB_DESCRIPTOR_TYPE_DEVICE, /* DEVICE Descriptor Type */
USB_SHORT_GET_LOW(USB_DEVICE_SPECIFIC_BCD_VERSION),
USB_SHORT_GET_HIGH(USB_DEVICE_SPECIFIC_BCD_VERSION), /* USB Specification Release Number in
Binary-Coded Decimal (i.e., 2.10 is 210H). */
USB_DEVICE_CLASS, /* Class code (assigned by the USB-IF). */
USB_DEVICE_SUBCLASS, /* Subclass code (assigned by the USB-IF). */
USB_DEVICE_PROTOCOL, /* Protocol code (assigned by the USB-IF). */
USB_CONTROL_MAX_PACKET_SIZE, /* Maximum packet size for endpoint zero
(only 8, 16, 32, or 64 are valid) */
USB_SHORT_GET_LOW(USB_DEVICE_VID),
USB_SHORT_GET_HIGH(USB_DEVICE_VID), /* Vendor ID (assigned by the USB-IF) */
USB_SHORT_GET_LOW(USB_DEVICE_PID),
USB_SHORT_GET_HIGH(USB_DEVICE_PID), /* Product ID (assigned by the manufacturer) */
USB_SHORT_GET_LOW(USB_DEVICE_DEMO_BCD_VERSION),
USB_SHORT_GET_HIGH(USB_DEVICE_DEMO_BCD_VERSION), /* Device release number in binary-coded decimal */
0x01U, /* Index of string descriptor describing manufacturer */
0x02U, /* Index of string descriptor describing product */
0x00U, /* Index of string descriptor describing the
device's serial number */
USB_DEVICE_CONFIGURATION_COUNT, /* Number of possible configurations */
};
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceConfigurationDescriptor[] = {
USB_DESCRIPTOR_LENGTH_CONFIGURE, /* Size of this descriptor in bytes */
USB_DESCRIPTOR_TYPE_CONFIGURE, /* CONFIGURATION Descriptor Type */
USB_SHORT_GET_LOW(USB_DESCRIPTOR_LENGTH_CONFIGURE + USB_DESCRIPTOR_LENGTH_INTERFACE +
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_ENDPOINT),
USB_SHORT_GET_HIGH(USB_DESCRIPTOR_LENGTH_CONFIGURE + USB_DESCRIPTOR_LENGTH_INTERFACE +
USB_DESCRIPTOR_LENGTH_ENDPOINT +
USB_DESCRIPTOR_LENGTH_ENDPOINT), /* Total length of data returned for this configuration. */
USB_PRINTER_INTERFACE_COUNT, /* Number of interfaces supported by this configuration */
USB_PRINTER_CONFIGURE_INDEX, /* Value to use as an argument to the
SetConfiguration() request to select this configuration */
0x00U, /* Index of string descriptor describing this configuration */
(USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_MASK) |
(USB_DEVICE_CONFIG_SELF_POWER << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_SHIFT) |
(USB_DEVICE_CONFIG_REMOTE_WAKEUP << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_SHIFT),
/* Configuration characteristics
D7: Reserved (set to one)
D6: Self-powered
D5: Remote Wakeup
D4...0: Reserved (reset to zero)
*/
USB_DEVICE_MAX_POWER, /* Maximum power consumption of the USB
* device from the bus in this specific
* configuration when the device is fully
* operational. Expressed in 2 mA units
* (i.e., 50 = 100 mA).
*/
USB_DESCRIPTOR_LENGTH_INTERFACE, /* Size of this descriptor in bytes */
USB_DESCRIPTOR_TYPE_INTERFACE, /* INTERFACE Descriptor Type */
USB_PRINTER_INTERFACE_INDEX, /* Number of this interface. */
USB_PRINTER_INTERFACE_ALTERNATE_0, /* Value used to select this alternate setting
for the interface identified in the prior field */
USB_PRINTER_ENDPOINT_COUNT, /* Number of endpoints used by this
interface (excluding endpoint zero). */
USB_PRINTER_CLASS, /* Class code (assigned by the USB-IF). */
USB_PRINTER_SUBCLASS, /* Subclass code (assigned by the USB-IF). */
USB_PRINTER_PROTOCOL, /* Protocol code (assigned by the USB). */
0x00U, /* Index of string descriptor describing this interface */
USB_DESCRIPTOR_LENGTH_ENDPOINT, /* Size of this descriptor in bytes */
USB_DESCRIPTOR_TYPE_ENDPOINT, /* ENDPOINT Descriptor Type */
USB_PRINTER_BULK_ENDPOINT_OUT | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
/* The address of the endpoint on the USB device
described by this descriptor. */
USB_ENDPOINT_BULK, /* This field describes the endpoint's attributes */
USB_SHORT_GET_LOW(FS_PRINTER_BULK_OUT_PACKET_SIZE), USB_SHORT_GET_HIGH(FS_PRINTER_BULK_OUT_PACKET_SIZE),
/* Maximum packet size this endpoint is capable of
sending or receiving when this configuration is
selected. */
FS_PRINTER_BULK_OUT_INTERVAL, /* Interval for polling endpoint for data transfers. */
USB_DESCRIPTOR_LENGTH_ENDPOINT, /* Size of this descriptor in bytes */
USB_DESCRIPTOR_TYPE_ENDPOINT, /* ENDPOINT Descriptor Type */
USB_PRINTER_BULK_ENDPOINT_IN | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT),
/* The address of the endpoint on the USB device
described by this descriptor. */
USB_ENDPOINT_BULK, /* This field describes the endpoint's attributes */
USB_SHORT_GET_LOW(FS_PRINTER_BULK_IN_PACKET_SIZE), USB_SHORT_GET_HIGH(FS_PRINTER_BULK_IN_PACKET_SIZE),
/* Maximum packet size this endpoint is capable of
sending or receiving when this configuration is
selected. */
FS_PRINTER_BULK_IN_INTERVAL, /* Interval for polling endpoint for data transfers. */
};
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceString0[] = {
2U + 2U,
USB_DESCRIPTOR_TYPE_STRING,
0x09U,
0x04U,
};
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceString1[] = {
2U + 2U * 18U, USB_DESCRIPTOR_TYPE_STRING,
'N', 0x00U,
'X', 0x00U,
'P', 0x00U,
' ', 0x00U,
'S', 0x00U,
'E', 0x00U,
'M', 0x00U,
'I', 0x00U,
'C', 0x00U,
'O', 0x00U,
'N', 0x00U,
'D', 0x00U,
'U', 0x00U,
'C', 0x00U,
'T', 0x00U,
'O', 0x00U,
'R', 0x00U,
'S', 0x00U,
};
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
uint8_t g_UsbDeviceString2[] = {
2U + 2U * 16U, USB_DESCRIPTOR_TYPE_STRING,
'M', 0x00U,
'C', 0x00U,
'U', 0x00U,
' ', 0x00U,
'P', 0x00U,
'R', 0x00U,
'I', 0x00U,
'N', 0x00U,
'T', 0x00U,
'E', 0x00U,
'R', 0x00U,
' ', 0x00U,
'D', 0x00U,
'E', 0x00U,
'M', 0x00U,
'O', 0x00U,
};
uint32_t g_UsbDeviceStringDescriptorLength[USB_DEVICE_STRING_COUNT] = {
sizeof(g_UsbDeviceString0),
sizeof(g_UsbDeviceString1),
sizeof(g_UsbDeviceString2),
};
uint8_t *g_UsbDeviceStringDescriptorArray[USB_DEVICE_STRING_COUNT] = {
g_UsbDeviceString0,
g_UsbDeviceString1,
g_UsbDeviceString2,
};
usb_language_t g_UsbDeviceLanguage[USB_DEVICE_LANGUAGE_COUNT] = {{
g_UsbDeviceStringDescriptorArray,
g_UsbDeviceStringDescriptorLength,
(uint16_t)0x0409U,
}};
usb_language_list_t g_UsbDeviceLanguageList = {
g_UsbDeviceString0,
sizeof(g_UsbDeviceString0),
g_UsbDeviceLanguage,
USB_DEVICE_LANGUAGE_COUNT,
};
/*******************************************************************************
* Code
******************************************************************************/
/* Get device descriptor request */
usb_status_t USB_DeviceGetDeviceDescriptor(usb_device_handle handle,
usb_device_get_device_descriptor_struct_t *deviceDescriptor)
{
deviceDescriptor->buffer = g_UsbDeviceDescriptor;
deviceDescriptor->length = USB_DESCRIPTOR_LENGTH_DEVICE;
return kStatus_USB_Success;
}
/* Get device configuration descriptor request */
usb_status_t USB_DeviceGetConfigurationDescriptor(
usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configurationDescriptor)
{
if (USB_PRINTER_CONFIGURE_INDEX > configurationDescriptor->configuration)
{
configurationDescriptor->buffer = g_UsbDeviceConfigurationDescriptor;
configurationDescriptor->length = USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL;
return kStatus_USB_Success;
}
return kStatus_USB_InvalidRequest;
}
/* Get device string descriptor request */
usb_status_t USB_DeviceGetStringDescriptor(usb_device_handle handle,
usb_device_get_string_descriptor_struct_t *stringDescriptor)
{
uint8_t languageIndex = 0U;
uint8_t stringIndex = USB_DEVICE_STRING_COUNT;
if (stringDescriptor->stringIndex == 0U)
{
stringDescriptor->buffer = (uint8_t *)g_UsbDeviceLanguageList.languageString;
stringDescriptor->length = g_UsbDeviceLanguageList.stringLength;
}
else
{
for (; languageIndex < USB_DEVICE_LANGUAGE_COUNT; languageIndex++)
{
if (stringDescriptor->languageId == g_UsbDeviceLanguageList.languageList[languageIndex].languageId)
{
if (stringDescriptor->stringIndex < USB_DEVICE_STRING_COUNT)
{
stringIndex = stringDescriptor->stringIndex;
}
break;
}
}
if (USB_DEVICE_STRING_COUNT == stringIndex)
{
return kStatus_USB_InvalidRequest;
}
stringDescriptor->buffer = (uint8_t *)g_UsbDeviceLanguageList.languageList[languageIndex].string[stringIndex];
stringDescriptor->length = g_UsbDeviceLanguageList.languageList[languageIndex].length[stringIndex];
}
return kStatus_USB_Success;
}
/* Due to the difference of HS and FS descriptors, the device descriptors and configurations need to be updated to match
* current speed.
* As the default, the device descriptors and configurations are configured by using FS parameters for both EHCI and
* KHCI.
* When the EHCI is enabled, the application needs to call this function to update device by using current speed.
* The updated information includes endpoint max packet size, endpoint interval, etc. */
usb_status_t USB_DeviceSetSpeed(usb_device_handle handle, uint8_t speed)
{
usb_descriptor_union_t *descriptorHead;
usb_descriptor_union_t *descriptorTail;
descriptorHead = (usb_descriptor_union_t *)&g_UsbDeviceConfigurationDescriptor[0];
descriptorTail =
(usb_descriptor_union_t *)(&g_UsbDeviceConfigurationDescriptor[USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL - 1U]);
while (descriptorHead < descriptorTail)
{
if (descriptorHead->common.bDescriptorType == USB_DESCRIPTOR_TYPE_ENDPOINT)
{
if (USB_SPEED_HIGH == speed)
{
if (((descriptorHead->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT) &&
(USB_PRINTER_BULK_ENDPOINT_OUT ==
(descriptorHead->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
{
descriptorHead->endpoint.bInterval = HS_PRINTER_BULK_OUT_INTERVAL;
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_PRINTER_BULK_OUT_PACKET_SIZE,
descriptorHead->endpoint.wMaxPacketSize);
}
else if (((descriptorHead->endpoint.bEndpointAddress &
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
(USB_PRINTER_BULK_ENDPOINT_IN ==
(descriptorHead->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
{
descriptorHead->endpoint.bInterval = HS_PRINTER_BULK_IN_INTERVAL;
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_PRINTER_BULK_IN_PACKET_SIZE,
descriptorHead->endpoint.wMaxPacketSize);
}
else
{
}
}
else
{
if (((descriptorHead->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT) &&
(USB_PRINTER_BULK_ENDPOINT_OUT ==
(descriptorHead->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
{
descriptorHead->endpoint.bInterval = FS_PRINTER_BULK_OUT_INTERVAL;
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_PRINTER_BULK_OUT_PACKET_SIZE,
descriptorHead->endpoint.wMaxPacketSize);
}
else if (((descriptorHead->endpoint.bEndpointAddress &
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
(USB_PRINTER_BULK_ENDPOINT_IN ==
(descriptorHead->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
{
descriptorHead->endpoint.bInterval = FS_PRINTER_BULK_IN_INTERVAL;
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_PRINTER_BULK_IN_PACKET_SIZE,
descriptorHead->endpoint.wMaxPacketSize);
}
else
{
}
}
}
descriptorHead = (usb_descriptor_union_t *)((uint8_t *)descriptorHead + descriptorHead->common.bLength);
}
for (int i = 0U; i < USB_PRINTER_ENDPOINT_COUNT; i++)
{
if (USB_SPEED_HIGH == speed)
{
if ((g_UsbDevicePrinterEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT)
{
g_UsbDevicePrinterEndpoints[i].maxPacketSize = HS_PRINTER_BULK_OUT_PACKET_SIZE;
}
else if ((g_UsbDevicePrinterEndpoints[i].endpointAddress &
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) == USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN)
{
g_UsbDevicePrinterEndpoints[i].maxPacketSize = HS_PRINTER_BULK_IN_PACKET_SIZE;
}
else
{
}
}
else
{
if ((g_UsbDevicePrinterEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT)
{
g_UsbDevicePrinterEndpoints[i].maxPacketSize = FS_PRINTER_BULK_OUT_PACKET_SIZE;
}
else if ((g_UsbDevicePrinterEndpoints[i].endpointAddress &
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) == USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN)
{
g_UsbDevicePrinterEndpoints[i].maxPacketSize = FS_PRINTER_BULK_IN_PACKET_SIZE;
}
else
{
}
}
}
return kStatus_USB_Success;
}