Disclosure of Invention
In order to solve the defects in the prior art, the invention aims to provide a USB mode switching device and method, and a USB device, which can reduce the hardware cost of the USB device, reduce the complexity and compatibility risk of software, and further achieve the effects of convenience, high efficiency and stability in USB mode switching.
To achieve the above object, the present invention provides a USB mode switching device, comprising: an upper application module, a USB CORE module, a USB controller driving module, a USB controller hardware module and a GADGET device driving module,
the upper application module interacts with opposite terminal equipment and confirms the switching of the USB mode;
the USB CORE module enumerates with opposite terminal equipment and sets a mode mark;
the USB controller driving module is used for unloading or loading the USB controller driver so as to realize the configuration of a host mode or an external equipment mode;
the USB controller hardware module is in hardware connection with opposite-end equipment;
the GADGET device driving module provides function driving in an external device mode.
Further, the upper layer application module determines whether to switch to the external device mode through a handshake protocol with the device at the opposite end.
Further, the USB controller driving module powers down or powers up the USB controller hardware module.
Furthermore, the USB controller driver module reads the mode flag of the USB CORE module to determine whether to load the USB driver.
In order to achieve the above object, the method for switching USB modes provided by the present invention comprises the following steps:
the USB mode is a host mode, USB enumeration is carried out, and connected opposite-end equipment is identified;
judging that the mode needs to be switched to an external device mode through a handshake protocol between an upper application module and opposite-end equipment;
unloading the drive of the USB controller, powering down a hardware module of the USB controller, and setting a USB mode flag to be 1;
judging whether the USB mode mark is 1, if so, loading the USB controller drive into an external device mode, and electrifying a USB controller hardware module;
and loading the GADGET device driver, and switching from the host mode to the external device mode.
Further, if the USB mode flag is not 1, loading the USB controller driver to a host mode, and powering on the USB controller hardware module.
In order to achieve the above object, the method for switching USB modes provided by the present invention comprises the following steps:
the USB mode is an external equipment mode, and when the USB interface is judged to be disconnected with opposite-end equipment, the GADGET equipment drive is unloaded;
unloading the drive of the USB controller, powering down a hardware module of the USB controller, and setting a USB mode flag to be 0;
and judging whether the USB mode mark is 0, if so, loading the USB controller drive into a host mode, electrifying a USB controller hardware module, and switching from the external equipment mode to the host mode.
Further, if the USB mode flag is not 0, loading the USB controller driver into an external device mode, and powering on the USB controller hardware module; and loading the GADGET device driver, and switching from the host mode back to the external device mode.
According to the USB mode switching device and method, the upper application module judges whether to switch to the external equipment mode or not through a handshake protocol with opposite-end equipment. Therefore, the realization of the corresponding OTG specification and protocols of different versions is not needed on the software level, the software complexity and compatibility risk are reduced, and the effects of convenience, high efficiency and stability are achieved on the USB mode switching. The USB mode switching device and the method are applied to USB equipment, an OTG module is removed from hardware, and compared with the equipment adopting the OTG module, the hardware cost is reduced.
Detailed Description
The preferred embodiments of the present invention will be described in conjunction with the accompanying drawings, and it will be understood that they are described herein for the purpose of illustration and explanation and not limitation.
Fig. 1 is a schematic structural diagram of a USB mode switching apparatus 101 according to the present invention, as shown in fig. 1, the USB mode switching apparatus 101 of the present invention includes an upper layer application module 102, a USB CORE module 103, a USB controller driver module 104, a USB controller hardware module 105, and a GADGET device driver module 106, wherein,
the upper application module 102 is configured to interact with an opposite device when another USB device is connected to the USB mode switching apparatus 101 through a USB cable, and determine whether to switch to the external device mode through a handshake protocol with the opposite device.
The USB CORE module 103 is configured to enumerate with an opposite device, that is, an external device, when the USB mode switching device 101 is in the host mode, and provide an attribute file for setting a mode flag.
The USB controller driver module 104 is configured to switch between a host mode and an external device mode of the USB mode switching apparatus 101. The USB controller driver module 104 configures the USB controller hardware module 105 into a host mode or an external device mode by unloading or loading the USB driver to perform corresponding functions. A USB controller driver module 104, which is also used to power down or power up the USB controller hardware module.
The USB controller hardware module 105 includes the USB itself and associated peripheral hardware circuitry.
And the GADGET device driver module 106 is configured to provide function driving in the peripheral device mode.
Fig. 2 is a flowchart illustrating a process of switching from the host mode to the external device mode according to the present invention, and the process of switching from the host mode to the external device mode according to the present invention will be described in detail with reference to fig. 2.
First, in step 201, the USB CORE module 103 enumerates the connected devices, and the upper application module 102 performs handshake interaction with the connected devices. In step 201, firstly, the USB mode is set as the host mode by default, when other USB devices are connected to the apparatus through a USB cable, the USB CORE module 103 performs normal USB enumeration, and identifies the connected devices as external devices; next, the upper layer application module 102 and the connected application module confirm whether the host mode needs to be switched to the external device mode through a handshake protocol. For example, when the upper layer application module 102 supports the iap2 protocol of apple Inc. and the connected device is an iPhone, the iPhone will request to switch the host mode to the external device mode through iap2 protocol interaction.
Step 202, judging whether to switch to the external device mode. If the mode does not need to be switched to the external equipment mode, ending the process; and if the mode needs to be switched to the external equipment mode, the next step is carried out.
Step 203, uninstall the USB driver, power down the USB controller hardware module 105, and set the mode flag to 1. In step 203, after confirming that the external device mode needs to be switched, the upper application module 102 controls the USB controller driver module 104 to uninstall the USB driver, and powers down the USB controller hardware module 105. In addition, the USB CORE module 103 provides an attribute file to set the mode flag to 1.
In step 204, it is determined whether the mode flag is 1. In step 204, the USB controller driver module 104 determines whether the mode flag is 1 by reading the attribute file value of the mode flag of the USB CORE module 103. If the mode flag is not 1, go to step 207; if the mode flag is 1, the next step is performed.
Step 205, load the USB controller hardware module 105 into the external device mode, and power up the USB controller hardware module 105. In step 205, the upper application module 102 controls the USB controller driver module 104 to reload the USB driver, loads the USB controller hardware module 105 into the external device mode, and powers on the USB controller hardware module 105.
Step 206, load the GADGET device driver. In step 206, after the USB controller hardware module 105 switches to the external device mode, the GADGET device driver module 106 loads the GADGET device driver.
Step 207 loads the USB controller hardware module 105 into host mode and powers up the USB controller hardware module 105. In step 207, the upper application module 102 controls the USB controller driver module 104 to reload the USB driver, reload the USB controller hardware module 105 into the host mode, and power up the USB controller hardware module 105.
The switching from the host mode to the peripheral mode is completed according to steps 201 to 206. At this time, the device at the opposite end is switched to the host mode, and the USB mode switching device 101 is enumerated as an external device. And then, data interaction is performed between the device at the opposite end and the USB mode switching device 101.
When the interaction is complete or the connection is no longer needed, the connection between the two is broken. After that, the USB mode switching device 101 switches from the external device mode to the host mode.
Fig. 3 is a flowchart of switching from the peripheral mode to the host mode according to the present invention, and the following describes in detail the flow of switching from the peripheral mode to the host mode according to the present invention with reference to fig. 3.
First, in step 301, the USB mode is an external device mode, and it is determined whether the connection is disconnected. If the connection is not disconnected, ending the process; if the connection has been broken, the next step is taken.
Step 302, uninstall the GADGET device driver. In step 302, the GADGET device driver module 106 unloads the GADGET device driver when a connection to disconnect is confirmed.
Step 303, uninstall the USB driver, power down the USB controller hardware module 105, and set the mode flag to 0. In step 303, the upper application module 102 controls the USB controller driver module 104 to uninstall the USB driver and power down the USB controller hardware module 105. In addition, the USB CORE module 103 provides an attribute file to set the mode flag to 0.
In step 304, it is determined whether the mode flag is 0. In step 304, the USB controller driver module 104 determines whether the mode flag is 0 by reading the attribute file value of the mode flag of the USB CORE module 103. If the mode flag is not 0, go to step 306 and step 307; if the mode flag is 0, the next step is performed.
Step 305 loads the USB controller hardware module 105 into host mode and powers up the USB controller hardware module 105. In step 305, the upper application module 102 controls the USB controller driver module 104 to reload the USB driver, loads the USB controller hardware module 105 into the host mode, and powers on the USB controller hardware module 105.
Step 306, reload the USB controller hardware module 105 into the external device mode, and power up the USB controller hardware module 105. In step 306, the upper application module 102 controls the USB controller driver module 104 to reload the USB driver, reload the USB controller hardware module 105 into the peripheral mode, and power up the USB controller hardware module 105.
Step 307, load the GADGET device driver. In step 307, after the USB controller hardware module 105 switches to the external device mode, that is, when the USB mode switching device 101 is in the external device mode, the GADGET device driver module 106 loads the GADGET device driver.
The switching to the host mode is completed according to steps 301 to 305. At this time, the USB mode switching device 101 switches to the host mode.
The USB mode switching device and the method are applied to USB equipment, an OTG module is removed from hardware, and compared with the equipment adopting the OTG module, the hardware cost is reduced.
By adopting the USB mode switching device and method in the USB equipment, the upper application module judges whether to switch to the external equipment mode or not through a handshake protocol with equipment at the opposite end. Therefore, the realization of the corresponding OTG specification and protocols of different versions is not needed on the software level, the software complexity and compatibility risk are reduced, and the effects of convenience, high efficiency and stability are achieved on the USB mode switching.
Those of ordinary skill in the art will understand that: although the present invention has been described in detail with reference to the foregoing embodiments, it will be apparent to those skilled in the art that changes may be made in the embodiments and/or equivalents thereof without departing from the spirit and scope of the invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.