A Linux-based dynamic USB device optimization method
By initializing USB device information and registering hot-plug event callback functions in the Linux system, the dimming fault of USB devices can be monitored and restored in real time, solving the problem of USB device identification and dimming after plugging and unplugging, and improving the stability and smoothness of the user experience.
Patent Information
- Application Number
- CN202411969838.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-12-26
- Estimated Expiration
- 2044-12-30
AI Technical Summary
Under the Linux operating system, the current methods for handling USB devices that fail to be recognized or have dimming problems after being plugged in or unplugged lack automation and intelligence, resulting in a poor user experience.
By initializing USB device information during system startup, registering USB hot-plug event callback functions, monitoring device status changes in real time, and calling the libusb_reset_device() function to restore dimming functionality when a fault is detected.
It enables automated management of USB devices and rapid fault recovery, improving the stability and smoothness of the user experience.
Smart Images

Figure CN120066593B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application belongs to the technical field of computer operating systems and embedded systems, and particularly relates to a dynamic USB device optimization method based on Linux. BACKGROUND
[0002] With the rapid development of computer technology, the application of USB (Universal Serial Bus) devices is becoming increasingly widespread, including storage devices, printers, external displays, etc. These devices greatly improve user convenience due to their plug-and-play characteristics. However, in actual use, users often encounter problems such as failure to recognize or adjust the brightness of the device after plugging it in, which not only affects work efficiency but also can cause system instability or even crashes.
[0003] Under the Linux operating system environment, the management of USB devices faces greater challenges. Although the Linux system provides some basic USB support, existing management solutions often appear insufficient when dealing with frequent device plugging and state changes. When users plug in or out USB devices, the system may not be able to timely recognize the changes in the device, resulting in operation delays or failures. In addition, the brightness adjustment function of some USB displays may not work properly after connection, showing abnormal brightness and contrast, or even black screen phenomenon. This brightness adjustment failure not only causes users to feel troubled, but also negatively affects the visual experience.
[0004] Existing methods usually rely on manual configuration and restart for handling the recognition and brightness adjustment failure of USB devices, lacking automation and intelligent support. Users often need to repeatedly plug in and out devices and set them up, increasing the complexity of use. At the same time, existing solutions lack efficient mechanisms for detecting and recovering from brightness adjustment failure, and cannot respond to user needs in real time.
[0005] Therefore, it is particularly important to develop an efficient and intelligent USB device management and brightness adjustment optimization method. This method should be able to monitor the state of USB devices in real time, automatically handle plugging and unplugging events, and quickly restore functionality when brightness adjustment failure occurs, thereby significantly improving user experience and ensuring stable operation of USB devices in various use scenarios. SUMMARY
[0006] The present application aims to solve the problems raised in the background art and proposes a dynamic USB device optimization method based on Linux.
[0007] To achieve the purpose of the present application, a dynamic USB device optimization method based on Linux is disclosed, which is used to solve the brightness adjustment failure caused by plugging in and out of USB display devices, and specifically includes the following steps:
[0008] Step 1, at system startup, initialize the relevant information of the USB display device, including device identifier (such as manufacturer number and device number), interface number, and required driver information; the system obtains the detailed data of the connected device by calling the corresponding USB interface function, and records these information in a global structure for management when the device is inserted or removed;
[0009] Step 2, register the callback function of USB hot plug event, monitor the insertion and removal events of USB display device through libusb library, so as to respond to the device state change in real time and handle it;
[0010] Step 3, when detecting the insertion event of USB display device, if the interface information of USB display device matches the registered driver, try to open the device and separate the kernel driver; after successfully declaring the interface, confirm the device connection, if dimming failure is found, call the reset function to restore normal dimming;
[0011] Step 4, when the USB display device is removed, release the interface and close the device to ensure that the resources are released, avoiding potential memory leakage or resource conflict.
[0012] Further, the USB display device is a device supporting USB HID protocol.
[0013] Further, the step of initializing the USB display device includes assigning device identifier, interface number and driver information to each device and recording them in the global structure.
[0014] Further, the step of registering USB hot plug event includes registering the callback by calling the related function in libusb library to respond to the insertion and removal of the device.
[0015] Further, the device reset function includes calling the libusb_reset_device() function to restore when dimming failure occurs.
[0016] Further, step 3 specifically includes the following steps:
[0017] Step 3.1, when detecting the insertion event of USB display device, obtain the interface information of the device and check whether it matches the registered driver;
[0018] Step 3.2, if the interface information of the device matches, try to open the device and separate the kernel driver, and then declare the device interface to prepare for subsequent operation;
[0019] Step 3.3, if a dimming fault is found in the device initialization process, a device reset function is called to reset the device and redeclare the interface to restore normal dimming function.
[0020] Compared with the prior art, the significant progress of the present application is that: 1) the method of the present application is particularly suitable for various USB devices, especially display devices supporting the USB HID protocol, and by dynamically managing USB devices and optimizing dimming function, the stability and reliability of display dimming function are ensured; 2) in addition, the functions of automatically detecting and repairing dimming faults enable users to enjoy more stable and smooth operation experience when using the device.
[0021] In order to more clearly illustrate the functional characteristics and structural parameters of the present application, the following further describes the present application in conjunction with the accompanying drawings and specific embodiments. BRIEF DESCRIPTION OF DRAWINGS
[0022] The accompanying drawings, which are included to provide a further understanding of the present application, constitute a part of this application and illustrate the illustrative embodiments of the present application and their description serve to explain the present application, and do not constitute an improper limitation of the present application. In the drawings:
[0023] Figure 1 is a flow chart of a Linux-based dynamic USB device optimization method;
[0024] Figure 2 is a flow chart of USB device insertion and removal event processing. DETAILED DESCRIPTION
[0025] The technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the accompanying drawings of the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments of the present application; based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor fall within the scope of protection of the present application.
[0026] As shown in Figure 1 , a Linux-based dynamic USB device optimization method is used to solve dimming faults caused by insertion and removal of USB display devices, and specifically includes the following steps:
[0027] Step 1, when the system starts, initialize the relevant information of the USB display device, including device identifier (such as manufacturer number and device number), interface number and required driver information; the system obtains detailed data of the connected device by calling the corresponding USB interface function, and records these information in a global structure body for management when the device is inserted or removed;
[0028] Step 2, register the callback function of USB hot plug event, monitor the insertion and removal events of USB display device through libusb library, so as to respond to the device state change in real time and handle it;
[0029] Step 3, when detecting the insertion event of USB display device, if the interface information of USB display device matches the registered driver, try to open the device and separate the kernel driver; After successfully claiming the interface, confirm the device connection, if dimming failure is found, call the reset function to restore normal dimming;
[0030] Step 4, when the USB display device is removed, release the interface and close the device, ensure that the resources are released, avoid potential memory leakage or resource conflict.
[0031] Further, the USB display device is a device supporting USB HID protocol.
[0032] Further, the step of initializing the USB display device includes: allocating device identifier, interface number and driver information for each device, and recording them in the global structure.
[0033] Further, the step of registering USB hot plug event includes: registering the callback by calling the related function in libusb library to respond to the insertion and removal of the device.
[0034] Further, the device reset function includes calling the libusb_reset_device() function to restore when dimming failure occurs.
[0035] Further, step 3 specifically includes the following steps:
[0036] Step 3.1, when detecting the insertion event of USB display device, get the interface information of the device and check whether it matches the registered driver;
[0037] Step 3.2, if the interface information of the device matches, try to open the device and separate the kernel driver, and then claim the device interface to prepare for subsequent operation;
[0038] Step 3.3, during the device initialization process, if dimming failure is found, call the device reset function to reset the device and re-claim the interface to restore normal dimming function.
[0039] Embodiment
[0040] In one embodiment, a Linux-based dynamic USB device management and dimming optimization method, the specific implementation steps are as follows:
[0041] Step 1: At system startup, the first step is to initialize the relevant information of the USB display device, including the device identifier (such as the manufacturer number and device number), interface number, and required driver information. The specific steps are as follows:
[0042] Step 1.1: Get the USB device list. The system uses the libusb_get_device_list() function to get the list of currently connected USB devices. This function returns a pointer to the device list, which contains information about all connected devices. Developers need to traverse this list to find the target USB display device.
[0043] Step 1.2: Extract device information. For each device, the system calls the libusb_get_device_descriptor() function to get its device descriptor information. This descriptor contains the Vendor ID and Product ID, which are crucial for subsequent device management. In addition, the device's interface information, including interface number, endpoint information, and driver requirements, needs to be obtained.
[0044] Step 1.3: The obtained device information will be recorded in a global structure. This structure is designed as an array, with each element representing a USB device. The fields in the structure include:
[0045] vendor_id: Vendor ID
[0046] product_id: Product ID
[0047] interface_number: Interface number
[0048] driver_info: Driver information
[0049] In this way, the system can efficiently manage multiple USB devices to quickly find and update the corresponding information when devices are plugged in or out.
[0050] Step 2: After initialization, the system will register the callback function for USB hot plug events to monitor USB device insertion and removal events in real time.
[0051] Step 2-1: Define the callback function. The main task of the callback function usbHotPlugCallback is to handle the state changes of USB devices. The parameters of this function include the libusb context, device pointer, event type, and user data. According to the event type (device insertion or removal), the callback function will perform different operations.
[0052] Step 2-2, Register the hotplug callback function by calling the libusb_hotplug_register_callback() function to bind the callback function with device events. The parameters of this function include the context, event type (such as device arrival and departure), vendor ID, product ID, etc. After successful registration, the system will automatically call the callback function when detecting device insertion or removal.
[0053] Step 3, Handle USB device insertion events. When the USB display device is inserted, the system performs the following operations to ensure the correct opening of the device and the successful declaration of the interface:
[0054] Step 3-1, The hotplug callback function will first detect the insertion event of the USB device. Then, call the libusb_get_active_config_descriptor() function to obtain the interface information of the inserted device. By comparing with the information recorded in the global structure, confirm whether the interface information of the device matches the registered driver, if not, find the next USB device.
[0055] Step 3-2, Open the device and detach the kernel driver. If the interface information of the device matches, the system will call the libusb_open() function to try to open the device. If the device has been occupied by the kernel driver, detach the kernel driver through the libusb_detach_kernel_driver() function to ensure that the user space can fully control the device.
[0056] Step 3-3, Declare the interface and handle fault devices. After successful opening, the system will call the libusb_claim_interface() function to declare the interface. If dimming faults are found during the declaration process (such as abnormal display brightness), call the libusb_reset_device() function to reset the device. After resetting, the system will again try to declare the interface to ensure the restoration of normal dimming function. If the reset operation is successful, the device can work normally, and the system will print relevant information to confirm that the device has been successfully connected.
[0057] Step 4, Handle USB device removal events. When the USB display device is removed, the system will perform the following steps to ensure the correct release of resources and avoid memory leaks or resource conflicts:
[0058] Step 4-1, Detect device removal events. In the callback function, the system will judge whether the event type is device removal. If the handle pointer is empty, it means the device has not been opened, and the system will print the corresponding message and return.
[0059] Step 4-2, Releasing the interface and closing the device. If the device is already open, the system will call the libusb_release_interface() function to release the interface declared previously. Next, the device is closed using the libusb_close() function to ensure that all related resources are released. In this way, the system effectively avoids possible memory leaks or resource conflicts.
[0060] Step 4-3, The system updates the information of the corresponding device in the global structure, marking the device as unconnected. This update is crucial for subsequent device management, ensuring that accurate information can be obtained when the device is inserted next time.
[0061] It should be noted that the relational terms herein such as first and second and the like are used solely to distinguish one from another entity or action without necessarily requiring or implying any actual relationship or order between such entities or actions. Moreover, the terms "comprises", "comprising", or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can also include other elements not expressly listed or inherent to such process, method, article, or apparatus.
[0062] Although embodiments of the present application have been shown and described, it is to be understood that various modifications, substitutions, alternatives, and variations can be made in the embodiments without departing from the spirit and scope of the present application as defined by the appended claims and their equivalents.
Claims
1. A Linux-based dynamic USB device optimization method, characterized in that, Specifically comprising the following steps: Step 1, at system startup, initialize the relevant information of the USB display device, including device identifier, interface number and required driver information; The system calls the corresponding USB interface function to obtain the detailed data of the connected device, and records these information in a global structure for management when the device is inserted or removed; Step 2, register the callback function of USB hot plug event, monitor the insertion and removal events of USB display device through libusb library, so as to respond to the device state change in real time and handle it; Step 3, when detecting the insertion event of USB display device, if the interface information of USB display device matches the registered driver, try to open the device and separate the kernel driver; Confirm the device connection after successfully declaring the interface, and call the reset function to restore normal dimming if dimming failure is found; Step 4, when the USB display device is removed, release the interface and close the device to ensure that the resources are released, avoiding potential memory leakage or resource conflict.
2. The method of claim 1, wherein the Linux-based dynamic USB device optimization method is characterized by, The USB display device is a device supporting USB HID protocol.
3. The method of claim 1, wherein the Linux-based dynamic USB device optimization method is characterized by, The steps of initializing the USB display device include assigning device identifier, interface number and driver information for each device and recording them in the global structure.
4. The method of claim 1, wherein the Linux-based dynamic USB device optimization method is characterized by, The steps of registering USB hot plug event include registering the callback through the related function in libusb library to respond to the insertion and removal of the device.
5. The method of claim 1, wherein the Linux-based dynamic USB device optimization method is characterized by, The device reset function includes calling the libusb_reset_device() function to restore when dimming failure occurs.
6. The method of claim 1, wherein the Linux-based dynamic USB device optimization method is characterized by, Step 3 specifically comprises the following steps: Step 3.1, when detecting the insertion event of USB display device, obtain the interface information of the device and check whether it matches the registered driver; Step 3.2, if the interface information of the device matches, try to open the device and separate the kernel driver, and then declare the device interface to prepare for subsequent operations; Step 3.3, during the device initialization process, if dimming failure is found, call the device reset function to reset the device and redeclare the interface to restore normal dimming function.
Citation Information
Patent Citations
Method and system for redirecting cloud terminal USB
CN104991871A
USB equipment management and control system based on Linux user space
CN110598428A