diff --git a/usb/v1_0/BUILD.gn b/usb/v1_0/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..001677564351c9f5c60140eb078840e9be4fb7e1 --- /dev/null +++ b/usb/v1_0/BUILD.gn @@ -0,0 +1,35 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//drivers/adapter/uhdf2/hdi.gni") +if (defined(ohos_lite)) { + group("libusb_proxy_1.0") { + deps = [] + public_configs = [] + } +} else { + hdi("usb") { + module_name = "usbd" + + sources = [ + "IUsbInterface.idl", + "IUsbdBulkCallback.idl", + "IUsbdSubscriber.idl", + "UsbTypes.idl", + ] + + language = "cpp" + subsystem_name = "hdf" + part_name = "usb_device_driver" + } +} diff --git a/usb/v1_0/IUsbInterface.idl b/usb/v1_0/IUsbInterface.idl new file mode 100755 index 0000000000000000000000000000000000000000..f3972ddf499412c3bf57926b34c251edc8b431e1 --- /dev/null +++ b/usb/v1_0/IUsbInterface.idl @@ -0,0 +1,467 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* * + * @addtogroup HdiUsb + * @{ + * + * @brief Provides unified APIs for usb services to access usb drivers. + * + * A usb service can obtain a usb driver object or agent and then call APIs provided by this object or agent to + * access different types of usb devices based on the usb IDs, thereby obtaining usb information, + * subscribing to or unsubscribing from usb data, enabling or disabling a usb, + * setting the usb data reporting mode, and setting usb options such as the accuracy and measurement range. + * + * @since 3.0 + */ + +/* * + * @file IusbInterface.idl + * + * @brief Declares the APIs provided by the usb module for obtaining usb information, subscribing to or + * unsubscribing from usb data, enabling or disabling a usb, setting the usb data reporting mode, + * and setting usb options such as the accuracy and measurement range. + * + * @since 3.0 + * @version 1.0 + */ + +package ohos.hdi.usb.v1_0; + +import ohos.hdi.usb.v1_0.UsbTypes; +import ohos.hdi.usb.v1_0.IUsbdSubscriber; +import ohos.hdi.usb.v1_0.IUsbdBulkCallback; + +/* * + * @brief Defines the functions for performing basic operations on usb. + * + * The operations include obtaining usb information, subscribing to or unsubscribing from usb data, + * enabling or disabling a usb, setting the usb data reporting mode, and setting usb options such as + * the accuracy and measurement range. + */ +interface IUsbInterface { + + /* * + * @brief Opens a USB device to set up a connection. + * + * @param dev Indicates the USB device address. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + OpenDevice([in] struct UsbDev dev); + + /* * + * @brief Closes a USB device to release all system resources related to the device. + * + * @param dev Indicates the USB device address. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + CloseDevice([in] struct UsbDev dev); + + /* * + * @brief Obtains the USB device descriptor. + * + * @param dev Indicates the USB device address. + * @param descriptor Indicates the USB device descriptor. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + GetDeviceDescriptor([in] struct UsbDev dev, [out] unsigned char[] decriptor); + + /* * + * @brief Obtains the string descriptor of a USB device based on the specified string ID. + * + * @param dev Indicates the USB device address. + * @param descId Indicates string descriptor ID. + * @param descriptor Indicates the configuration descriptor of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + GetStringDescriptor([in] struct UsbDev dev, [in] unsigned char descId, [out] unsigned char[] decriptor); + + /* * + * @brief Obtains the configuration descriptor of a USB device based on the specified config ID. + * + * @param dev Indicates the USB device address. + * @param descId Indicates configuration descriptor ID. + * @param descriptor Indicates the configuration descriptor of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + GetConfigDescriptor([in] struct UsbDev dev, [in] unsigned char descId, [out] unsigned char[] decriptor); + + /* * + * @brief Obtains the raw descriptor. + * + * @param dev Indicates the USB device address. + * @param descriptor Indicates the raw descriptor of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + GetRawDescriptor([in] struct UsbDev dev, [out] unsigned char[] decriptor); + + /* * + * @brief Obtains the file descriptor. + * + * @param dev Indicates the USB device address. + * @param fd USB device file descriptor + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + GetFileDescriptor([in] struct UsbDev dev, [out] int fd); + + /* * + * @brief Sets the configuration information of a USB device. + * + * @param dev Indicates the USB device address. + * @param configIndex Indicates the configuration information of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + SetConfig([in] struct UsbDev dev, [in] unsigned char configIndex); + + /* * + * @brief Obtains the configuration information of a USB device. + * + * @param dev Indicates the USB device address. + * @param configIndex Indicates the configuration information of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + GetConfig([in] struct UsbDev dev, [out] unsigned char configIndex); + + /* * + * @brief Claims a USB interface exclusively. This must be done before data transfer. + * + * @param dev Indicates the USB device address. + * @param interfaceid Indicates the interface ID of the USB device. + * @param force Indicates whether to enable the force mode. The value 1 indicates that the force mode is + * enabled, and value 0 indicates the opposite. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + ClaimInterface([in] struct UsbDev dev, [in] unsigned char interfaceid, [in] unsigned char force); + + /* * + * @brief Releases a USB interface. This is usually done after data transfer. + * + * @param dev Indicates the USB device address. + * @param interfaceid Indicates the interface ID of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + ReleaseInterface([in] struct UsbDev dev, [in] unsigned char interfaceid); + + /* * + * @brief Sets the alternate settings for the specified USB interface. This allows you to switch between two + * interfaces with the same ID but different alternate settings. + * + * @param dev Indicates the USB device address. + * @param interfaceid Indicates the interface ID of the USB device. + * @param altIndex Indicates the alternate settings of the USB interface. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + SetInterface([in] struct UsbDev dev, [in] unsigned char interfaceid, [in] unsigned char altIndex); + + /* * + * @brief Reads data on a specified endpoint during bulk transfer. The endpoint must be in the data reading + * direction. You can specify a timeout duration if needed. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param timeout Indicates the timeout duration. + * @param data Indicates the read data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + BulkTransferRead([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] int timeout, [out] unsigned char[] data); + + /* * + * @brief Writes data on a specified endpoint during bulk transfer. The endpoint must be in the data writing + * direction. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param timeout Indicates the timeout duration. + * @param data Indicates the written data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + BulkTransferWrite([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] int timeout, [in] unsigned char[] data); + + /* * + * @brief Performs control transfer for endpoint 0 of the device. The data transfer direction is determined by the + * request type. If the result of requestType& + * USB_ENDPOINT_DIR_MASK is USB_DIR_OUT, the endpoint is in the data writing direction; if the result + * is USB_DIR_IN, the endpoint is in the data reading direction. + * + * @param dev Indicates the USB device address. + * @param ctrl Indicates the control data packet structure. + * @param data Indicates the read data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + ControlTransferRead([in] struct UsbDev dev, [in] struct UsbCtrlTransfer ctrl, [out] unsigned char[] data); + + /* * + * @brief Performs control transfer for endpoint 0 of the device. The data transfer direction is determined by the + * request type. If the result of requestType& + * USB_ENDPOINT_DIR_MASK is USB_DIR_OUT, the endpoint is in the data writing direction; if the result + * is USB_DIR_IN, the endpoint is in the data writing direction. + * + * @param dev Indicates the USB device address. + * @param ctrl Indicates the control data packet structure. + * @param data Indicates the write data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + ControlTransferWrite([in] struct UsbDev dev, [in] struct UsbCtrlTransfer ctrl, [in] unsigned char[] data); + + /* * + * @brief Reads data on a specified endpoint during interrupt transfer. The endpoint must be in the data reading + * direction. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param timeout Indicates the timeout duration. + * @param data Indicates the read data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + InterruptTransferRead([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] int timeout, [out] unsigned char[] data); + + /* * + * @brief Writes data on a specified endpoint during interrupt transfer. The endpoint must be in the data writing + * direction. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param timeout Indicates the timeout duration. + * @param data Indicates the read data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + InterruptTransferWrite([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] int timeout, [in] unsigned char[] data); + + /* * + * @brief Reads data on a specified endpoint during isochronous transfer. The endpoint must be in the data reading + * direction. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param timeout Indicates the timeout duration. + * @param data Indicates the read data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + IsoTransferRead([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] int timeout, [out] unsigned char[] data); + + /* * + * @brief Writes data on a specified endpoint during isochronous transfer. The endpoint must be in the data writing + * direction. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param timeout Indicates the timeout duration. + * @param data Indicates the read data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + IsoTransferWrite([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] int timeout, [in] unsigned char[] data); + + /* * + * @brief Sends or receives requests for isochronous transfer on a specified endpoint. The data transfer direction + * is determined by the endpoint direction. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param clientData Indicates the client data. + * @param buffer Indicates the transferred data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + RequestQueue([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] unsigned char[] clientData, [in] unsigned char[] buffer); + + /* * + * @brief Waits for the operation result of the isochronous transfer request in RequestQueue. + * + * @param dev Indicates the USB device address. + * @param clientData Indicates the client data. + * @param buffer Indicates the transferred data. + * @param timeout Indicates the timeout duration. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + RequestWait([in] struct UsbDev dev, [out] unsigned char[] clientData, [out] unsigned char[] buffer, [in] int timeout); + + /* * + * @brief Cancels the data transfer requests to be processed. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + RequestCancel([in] struct UsbDev dev, [in] struct UsbPipe pipe); + + /* * + * @brief Obtains the list of functions (represented by bit field) supported by the current device. + * + * @param funcs Indicates the list of functions supported by the current device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + GetCurrentFunctions([out] int funcs); + + /* * + * @brief Sets the list of functions (represented by bit field) supported by the current device. + * + * @param funcs Indicates the list of functions supported by the current device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + SetCurrentFunctions([in] int funcs); + + /* * + * @brief Closes a USB device to release all system resources related to the device. + * + * @param portId Indicates the port ID of the USB interface. + * @param powerRole Indicates the power role. + * @param dataRole Indicates the data role. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + SetPortRole([in] int portId, [in] int powerRole, [in] int dataRole); + + /* * + * @brief Queries the current settings of a port. + * + * @param portId Indicates the port ID of the USB interface. + * @param powerRole Indicates the power role. + * @param dataRole Indicates the data role. + * @param mode Indicates the mode. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + QueryPort([out] int portId, [out] int powerRole, [out] int dataRole, [out] int mode); + + /* * + * @brief Binds a subscriber. + * + * @param subscriber Indicates the subscriber. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + BindUsbdSubscriber([in] IUsbdSubscriber subscriber); + + /* * + * @brief Unbinds a subscriber. + * + * @param subscriber Indicates the subscriber. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + UnbindUsbdSubscriber([in] IUsbdSubscriber subscriber); + + /* * + * @brief Registers a callback for isochronous bulk transfer. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param cb Indicates the reference to the callback. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + RegBulkCallback([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] IUsbdBulkCallback cb); + + /* * + * @brief Unregisters the callback for isochronous bulk transfer. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + UnRegBulkCallback([in] struct UsbDev dev, [in] struct UsbPipe pipe); + + /* * + * @brief Reads data during isochronous bulk transfer. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param ashmem Indicates the shared memory, which is used to store the read data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + BulkRead([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] Ashmem ashmem); + + /* * + * @brief Writes data during isochronous bulk transfer. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * @param ashmem Indicates the shared memory, which is used to store the written data. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + BulkWrite([in] struct UsbDev dev, [in] struct UsbPipe pipe, [in] Ashmem ashmem); + + /* * + * @brief Cancels the isochronous bulk transfer. The read and write operations on the current USB interface will + * be cancelled. + * + * @param dev Indicates the USB device address. + * @param pipe Indicates the pipe of the USB device. + * + * @return Returns 0 if the operation is successful; returns a non-0 value if the operation fails. + * @since 3.0 + */ + BulkCancel([in] struct UsbDev dev, [in] struct UsbPipe pipe); +} \ No newline at end of file diff --git a/usb/v1_0/IUsbdBulkCallback.idl b/usb/v1_0/IUsbdBulkCallback.idl new file mode 100755 index 0000000000000000000000000000000000000000..3cc8d3c68ad593fc647c8b7a5ae46e8f46a5b989 --- /dev/null +++ b/usb/v1_0/IUsbdBulkCallback.idl @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup HdiUsb + * @{ + * + * @brief Provides unified APIs for usb services to access usb drivers. + * + * A usb service can obtain a usb driver object or agent and then call APIs provided by this object or agent to + * access different types of usb devices based on the usb IDs, thereby obtaining usb information, + * subscribing to or unsubscribing from usb data, enabling or disabling a usb, + * setting the usb data reporting mode, and setting usb options such as the accuracy and measurement range. + * + * @version 1.0 + */ + +package ohos.hdi.usb.v1_0; + +/** + * @brief Defines the callback for reporting usb data. This callback needs to be registered when + * a usb user subscribes to usb data. Only after the usb is enabled, the usb data subscriber can receive + * usb data. For details, see {@link IusbInterface}. + * + * @since 3.0 + */ +[callback] interface IUsbdBulkCallback { + OnBulkWriteCallback([in] int status, [in] int actLength); + + OnBulkReadCallback([in] int status, [in] int actLength); +} \ No newline at end of file diff --git a/usb/v1_0/IUsbdSubscriber.idl b/usb/v1_0/IUsbdSubscriber.idl new file mode 100755 index 0000000000000000000000000000000000000000..90771b5f0e0b346459f810b0b91cc24209fda350 --- /dev/null +++ b/usb/v1_0/IUsbdSubscriber.idl @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup HdiUsb + * @{ + * + * @brief Provides unified APIs for usb services to access usb drivers. + * + * A usb service can obtain a usb driver object or agent and then call APIs provided by this object or agent to + * access different types of usb devices based on the usb IDs, thereby obtaining usb information, + * subscribing to or unsubscribing from usb data, enabling or disabling a usb, + * setting the usb data reporting mode, and setting usb options such as the accuracy and measurement range. + * + * @version 1.0 + */ + +package ohos.hdi.usb.v1_0; + +import ohos.hdi.usb.v1_0.UsbTypes; + +/** + * @brief Defines the callback for reporting usb data. This callback needs to be registered when + * a usb user subscribes to usb data. Only after the usb is enabled, the usb data subscriber can receive + * usb data. For details, see {@link IusbInterface}. + * + * @since 3.0 + */ +[callback] interface IUsbdSubscriber { + DeviceEvent([in] struct USBDeviceInfo info); + + PortChangedEvent([in] struct PortInfo info); +} \ No newline at end of file diff --git a/usb/v1_0/UsbTypes.idl b/usb/v1_0/UsbTypes.idl new file mode 100755 index 0000000000000000000000000000000000000000..902375aedf76a896acc49c8249c9a6e3f1839458 --- /dev/null +++ b/usb/v1_0/UsbTypes.idl @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /** + * @addtogroup HdiUsb + * @{ + * + * @brief Provides unified APIs for usb services to access usb drivers. + * + * A usb service can obtain a usb driver object or agent and then call APIs provided by this object or agent to + * access different types of usb devices based on the usb IDs, thereby obtaining usb information, + * subscribing to or unsubscribing from usb data, enabling or disabling a usb, + * setting the usb data reporting mode, and setting usb options such as the accuracy and measurement range. + * + * @since 3.0 + */ + +/** + * @file UsbTypes.idl + * + * @brief Defines the data used by the usb module, including the usb information, + * and reported usb data. + * + * @since3.0 + * @version 1.0 + */ + +package ohos.hdi.usb.v1_0; + +struct PortInfo { + int portId; + int powerRole; + int dataRole; + int mode; +}; + +struct UsbDev { + unsigned char busNum; + unsigned char devAddr; +}; + +struct UsbPipe { + unsigned char interfaceId; + unsigned char endpointId; +}; + +struct UsbCtrlTransfer { + int requestType; + int requestCmd; + int value; + int index; + int timeout; +}; + +struct USBDeviceInfo { + int status; + int busNum; + int devNum; +}; \ No newline at end of file