From 04066801731cb914e3c6505188970ef00874d8e1 Mon Sep 17 00:00:00 2001 From: zhji Date: Sun, 15 Mar 2026 21:11:47 +0800 Subject: [PATCH] [feat] add usb_detect driver --- usb/usb_detect/Makefile | 12 ++++++++++++ usb/usb_detect/usb_detect.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 usb/usb_detect/Makefile create mode 100644 usb/usb_detect/usb_detect.c diff --git a/usb/usb_detect/Makefile b/usb/usb_detect/Makefile new file mode 100644 index 0000000..87e9629 --- /dev/null +++ b/usb/usb_detect/Makefile @@ -0,0 +1,12 @@ +ARCH ?= arm +CROSS_COMPILE ?= /home/jzh/work/study/weidongshan/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin/arm-linux- +KERN_DIR ?= /home/jzh/work/study/weidongshan/100ask_stm32mp157_pro-sdk/Linux-5.4 + +all: + make -C $(KERN_DIR) M=`pwd` ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules + +clean: + make -C $(KERN_DIR) M=`pwd` ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules clean + rm -rf modules.order + +obj-m += usb_detect.o diff --git a/usb/usb_detect/usb_detect.c b/usb/usb_detect/usb_detect.c new file mode 100644 index 0000000..266ccfa --- /dev/null +++ b/usb/usb_detect/usb_detect.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +static struct usb_device_id usb_detect_id_table[] = { + { USB_DEVICE_AND_INTERFACE_INFO(0xffff, 0xffff, 0xff, 0xff, 0x02), .driver_info = 1 }, // ctrl + { USB_DEVICE_AND_INTERFACE_INFO(0xffff, 0xffff, 0xff, 0xff, 0x0a), .driver_info = 2 }, // data + { } +}; + +static int usb_detect_probe(struct usb_interface *intf, const struct usb_device_id *id) +{ + printk("%s %s %d\r\n", __FILE__, __FUNCTION__, __LINE__); + printk("USB device detected: vendor=0x%04X, product=0x%04X, info=%ld\r\n", + id->idVendor, id->idProduct, id->driver_info); + return 0; +} + +static void usb_detect_disconnect(struct usb_interface *intf) +{ + printk("%s %s %d\r\n", __FILE__, __FUNCTION__, __LINE__); + printk("USB device disconnected\r\n"); +} + +static struct usb_driver usb_detect_driver = { + .name = "usb_detect_driver", + .probe = usb_detect_probe, + .disconnect = usb_detect_disconnect, + .id_table = usb_detect_id_table, +}; + +module_usb_driver(usb_detect_driver); +MODULE_LICENSE("GPL");