1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci/** 17094332d3Sopenharmony_ci * @file usbfn_device.h 18094332d3Sopenharmony_ci * 19094332d3Sopenharmony_ci * @brief Declares the APIs for managing USB devices. 20094332d3Sopenharmony_ci * 21094332d3Sopenharmony_ci * @since 1.0 22094332d3Sopenharmony_ci * @version 1.0 23094332d3Sopenharmony_ci */ 24094332d3Sopenharmony_ci 25094332d3Sopenharmony_ci#ifndef USBFN_DEVICE_H 26094332d3Sopenharmony_ci#define USBFN_DEVICE_H 27094332d3Sopenharmony_ci 28094332d3Sopenharmony_ci#include "device_resource_if.h" 29094332d3Sopenharmony_ci#include "usb_ddk.h" 30094332d3Sopenharmony_ci#include "usb_object.h" 31094332d3Sopenharmony_ci#include "usbfn_interface.h" 32094332d3Sopenharmony_ci 33094332d3Sopenharmony_ci/** 34094332d3Sopenharmony_ci * @brief Defines a USB device object, which is obtained when a device is created. 35094332d3Sopenharmony_ci * 36094332d3Sopenharmony_ci * <b>UsbFnDevice</b> corresponds to the device object in the USB specifications. 37094332d3Sopenharmony_ci * Wherein, <b>object</b> is the identifier of the device, 38094332d3Sopenharmony_ci * <b>numInterfaces</b>indicates the number of interfaces. 39094332d3Sopenharmony_ci */ 40094332d3Sopenharmony_cistruct UsbFnDevice { 41094332d3Sopenharmony_ci /** USB device object */ 42094332d3Sopenharmony_ci struct UsbObject object; 43094332d3Sopenharmony_ci /** Number of interfaces */ 44094332d3Sopenharmony_ci uint8_t numInterfaces; 45094332d3Sopenharmony_ci}; 46094332d3Sopenharmony_ci 47094332d3Sopenharmony_ci/** 48094332d3Sopenharmony_ci * @brief Defines the descriptor type. 49094332d3Sopenharmony_ci * 50094332d3Sopenharmony_ci * The descriptor type can be defined in the HCS or the driver code. 51094332d3Sopenharmony_ci */ 52094332d3Sopenharmony_citypedef enum { 53094332d3Sopenharmony_ci /** Descriptor defined in the HCS */ 54094332d3Sopenharmony_ci USBFN_DESC_DATA_TYPE_PROP, 55094332d3Sopenharmony_ci /** Descriptor defined in the driver */ 56094332d3Sopenharmony_ci USBFN_DESC_DATA_TYPE_DESC, 57094332d3Sopenharmony_ci} UsbFnDescDataType; 58094332d3Sopenharmony_ci 59094332d3Sopenharmony_ci/** 60094332d3Sopenharmony_ci * @brief Defines a USB string. 61094332d3Sopenharmony_ci */ 62094332d3Sopenharmony_cistruct UsbString { 63094332d3Sopenharmony_ci /** String ID */ 64094332d3Sopenharmony_ci uint8_t id; 65094332d3Sopenharmony_ci /** String encoded in UTF-8 format */ 66094332d3Sopenharmony_ci const char *s; 67094332d3Sopenharmony_ci}; 68094332d3Sopenharmony_ci 69094332d3Sopenharmony_ci/** 70094332d3Sopenharmony_ci * @brief Defines a USB string in a specific language. 71094332d3Sopenharmony_ci * 72094332d3Sopenharmony_ci * 73094332d3Sopenharmony_ci * @see http://www.usb.org/developers/docs/USB_LANGIDs.pdf 74094332d3Sopenharmony_ci */ 75094332d3Sopenharmony_cistruct UsbFnStrings { 76094332d3Sopenharmony_ci /** Language of USB strings, for example, 0x0409 for en-us */ 77094332d3Sopenharmony_ci uint16_t language; 78094332d3Sopenharmony_ci /** Pointer to USB strings */ 79094332d3Sopenharmony_ci struct UsbString *strings; 80094332d3Sopenharmony_ci}; 81094332d3Sopenharmony_ci 82094332d3Sopenharmony_ci/** 83094332d3Sopenharmony_ci * @brief Defines a USB device. 84094332d3Sopenharmony_ci * 85094332d3Sopenharmony_ci * The <b>UsbFnFunction</b> structure contains multiple strings and descriptors. 86094332d3Sopenharmony_ci * It is used to describe a functional device, for example, a serial port or network adapter. 87094332d3Sopenharmony_ci */ 88094332d3Sopenharmony_cistruct UsbFnFunction { 89094332d3Sopenharmony_ci /** Pointer to the function driver name: 90094332d3Sopenharmony_ci * Naming format: f_generic.x acm.x ecm.x 91094332d3Sopenharmony_ci * Use the symbol dot (.) as the separator. <b>f_generic</b> is the common driver 92094332d3Sopenharmony_ci * capability provided by this API, 93094332d3Sopenharmony_ci * whereas <b>acm</b> or <b>ecm</b> is the function drive capability provided by the kernel. 94094332d3Sopenharmony_ci * A value does not need to be assigned to the descriptor. 95094332d3Sopenharmony_ci * You can simply use <b>UsbFnCreateDevice</b> to create a device. 96094332d3Sopenharmony_ci */ 97094332d3Sopenharmony_ci bool enable; 98094332d3Sopenharmony_ci const char *funcName; 99094332d3Sopenharmony_ci /** Double pointer to USB strings in a specified language */ 100094332d3Sopenharmony_ci struct UsbFnStrings **strings; 101094332d3Sopenharmony_ci /** Double pointer to Full-Speed descriptors */ 102094332d3Sopenharmony_ci struct UsbDescriptorHeader **fsDescriptors; 103094332d3Sopenharmony_ci /** Double pointer to High-Speed descriptors */ 104094332d3Sopenharmony_ci struct UsbDescriptorHeader **hsDescriptors; 105094332d3Sopenharmony_ci /** Double pointer to SuperSpeed descriptors */ 106094332d3Sopenharmony_ci struct UsbDescriptorHeader **ssDescriptors; 107094332d3Sopenharmony_ci /** Double pointer to SuperSpeed Plus descriptors */ 108094332d3Sopenharmony_ci struct UsbDescriptorHeader **sspDescriptors; 109094332d3Sopenharmony_ci}; 110094332d3Sopenharmony_ci 111094332d3Sopenharmony_ci/** 112094332d3Sopenharmony_ci * @brief Defines a USB configuration descriptor. 113094332d3Sopenharmony_ci */ 114094332d3Sopenharmony_cistruct UsbFnConfiguration { 115094332d3Sopenharmony_ci /** Configuration ID */ 116094332d3Sopenharmony_ci uint8_t configurationValue; 117094332d3Sopenharmony_ci /** Configuration string index */ 118094332d3Sopenharmony_ci uint8_t iConfiguration; 119094332d3Sopenharmony_ci /** Configuration attributes */ 120094332d3Sopenharmony_ci uint8_t attributes; 121094332d3Sopenharmony_ci /** Maximum current */ 122094332d3Sopenharmony_ci uint16_t maxPower; 123094332d3Sopenharmony_ci /** Double pointer to USB devices */ 124094332d3Sopenharmony_ci struct UsbFnFunction **functions; 125094332d3Sopenharmony_ci}; 126094332d3Sopenharmony_ci 127094332d3Sopenharmony_ci/** 128094332d3Sopenharmony_ci * @brief Defines a USB device descriptor. 129094332d3Sopenharmony_ci */ 130094332d3Sopenharmony_cistruct UsbFnDeviceDesc { 131094332d3Sopenharmony_ci /** Pointer to the standard USB device descriptor */ 132094332d3Sopenharmony_ci struct UsbDeviceDescriptor *deviceDesc; 133094332d3Sopenharmony_ci /** Double pointer to USB strings in a specified language */ 134094332d3Sopenharmony_ci struct UsbFnStrings **deviceStrings; 135094332d3Sopenharmony_ci /** Double pointer to USB configuration descriptors */ 136094332d3Sopenharmony_ci struct UsbFnConfiguration **configs; 137094332d3Sopenharmony_ci}; 138094332d3Sopenharmony_ci 139094332d3Sopenharmony_ci/** 140094332d3Sopenharmony_ci * @brief Defines the descriptor data of USB devices. 141094332d3Sopenharmony_ci */ 142094332d3Sopenharmony_cistruct UsbFnDescriptorData { 143094332d3Sopenharmony_ci union { 144094332d3Sopenharmony_ci /** Pointer to device resource node attributes */ 145094332d3Sopenharmony_ci const struct DeviceResourceNode *property; 146094332d3Sopenharmony_ci /** Pointer to the USB device descriptor */ 147094332d3Sopenharmony_ci struct UsbFnDeviceDesc *descriptor; 148094332d3Sopenharmony_ci }; 149094332d3Sopenharmony_ci /** Descriptor type */ 150094332d3Sopenharmony_ci UsbFnDescDataType type; 151094332d3Sopenharmony_ci uint8_t functionMask; 152094332d3Sopenharmony_ci}; 153094332d3Sopenharmony_ci 154094332d3Sopenharmony_ci#ifdef __cplusplus 155094332d3Sopenharmony_ciextern "C" { 156094332d3Sopenharmony_ci#endif 157094332d3Sopenharmony_ci 158094332d3Sopenharmony_ci/** 159094332d3Sopenharmony_ci * @brief Creates a USB device. 160094332d3Sopenharmony_ci * 161094332d3Sopenharmony_ci * You can use this function to create a descriptor and bind it to a USB device of the specified UDC. 162094332d3Sopenharmony_ci * 163094332d3Sopenharmony_ci * @param udcName Indicates the pointer to the UDC name, which is obtained based on the UDC driver. 164094332d3Sopenharmony_ci * @param descriptor Indicates the pointer to USB device descriptor data. 165094332d3Sopenharmony_ci * 166094332d3Sopenharmony_ci * @return Returns the pointer to the <b>UsbFnDevice</b> if the operation is successful; 167094332d3Sopenharmony_ci * returns <b>NULL</b> otherwise. 168094332d3Sopenharmony_ci */ 169094332d3Sopenharmony_ciconst struct UsbFnDevice *UsbFnCreateDevice(const char *udcName, struct UsbFnDescriptorData *descriptor); 170094332d3Sopenharmony_ci 171094332d3Sopenharmony_ci/** 172094332d3Sopenharmony_ci * @brief Deletes a specified USB device. 173094332d3Sopenharmony_ci * 174094332d3Sopenharmony_ci * 175094332d3Sopenharmony_ci * 176094332d3Sopenharmony_ci * @param fnDevice Indicates the pointer to the USB device object. 177094332d3Sopenharmony_ci * 178094332d3Sopenharmony_ci * @return Returns <b>0</b> if the operation is successful; returns a negative value 179094332d3Sopenharmony_ci * defined in {@link UsbErrorType} otherwise. 180094332d3Sopenharmony_ci */ 181094332d3Sopenharmony_ciint32_t UsbFnRemoveDevice(struct UsbFnDevice *fnDevice); 182094332d3Sopenharmony_ciconst struct UsbFnDevice *UsbFnGetDevice(const char *udcName); 183094332d3Sopenharmony_ciint32_t UsbFnGetDeviceState(struct UsbFnDevice *fnDevice, UsbFnDeviceState *devState); 184094332d3Sopenharmony_ci 185094332d3Sopenharmony_ci/** 186094332d3Sopenharmony_ci * @brief Obtains a USB interface based on the specified interface index. 187094332d3Sopenharmony_ci * 188094332d3Sopenharmony_ci * 189094332d3Sopenharmony_ci * 190094332d3Sopenharmony_ci * @param fnDevice Indicates the pointer to the USB device object. 191094332d3Sopenharmony_ci * @param interfaceIndex Indicates the interface index, which is numbered from <b>0</b>. 192094332d3Sopenharmony_ci * 193094332d3Sopenharmony_ci * @return Returns the pointer to the <b>UsbFnInterface</b> if the operation is successful; 194094332d3Sopenharmony_ci * returns <b>NULL</b> otherwise. 195094332d3Sopenharmony_ci */ 196094332d3Sopenharmony_ciconst struct UsbFnInterface *UsbFnGetInterface(struct UsbFnDevice *fnDevice, uint8_t interfaceIndex); 197094332d3Sopenharmony_ci 198094332d3Sopenharmony_ci#ifdef __cplusplus 199094332d3Sopenharmony_ci} 200094332d3Sopenharmony_ci#endif 201094332d3Sopenharmony_ci 202094332d3Sopenharmony_ci#endif /* USBFN_DEVICE_H */ 203