linux下usb驱动代码示例
时间: 2023-07-26 12:18:18 浏览: 238
好的,以下是一个简单的Linux USB驱动程序示例代码,实现了对USB设备的读写操作:
```c
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define USB_VENDOR_ID 0xXXXX // 请替换为实际的USB设备的Vendor ID
#define USB_PRODUCT_ID 0xXXXX // 请替换为实际的USB设备的Product ID
static struct usb_device_id my_usb_table[] = {
{ USB_DEVICE(USB_VENDOR_ID, USB_PRODUCT_ID) },
{ }
};
MODULE_DEVICE_TABLE(usb, my_usb_table);
static int my_usb_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev(interface);
struct usb_endpoint_descriptor *ep_desc;
struct usb_host_interface *iface_desc;
unsigned char *buffer;
int retval = -ENOMEM;
int i;
printk(KERN_INFO "My USB device (%04X:%04X) plugged in\n", id->idVendor, id->idProduct);
printk(KERN_INFO "Number of endpoints: %d\n", interface->cur_altsetting->desc.bNumEndpoints);
iface_desc = interface->cur_altsetting;
for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
ep_desc = &iface_desc->endpoint[i].desc;
if (usb_endpoint_is_bulk_in(ep_desc)) {
printk(KERN_INFO "Bulk IN endpoint found\n");
}
if (usb_endpoint_is_bulk_out(ep_desc)) {
printk(KERN_INFO "Bulk OUT endpoint found\n");
}
}
buffer = kmalloc(64, GFP_KERNEL);
if (!buffer) {
printk(KERN_ERR "Failed to allocate memory for buffer\n");
return -ENOMEM;
}
retval = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, 0x81), buffer, 64, NULL, 5000);
if (retval < 0) {
printk(KERN_ERR "Failed to read data from USB device (%d)\n", retval);
goto error;
}
printk(KERN_INFO "Data read from USB device: ");
for (i = 0; i < 64; i++) {
printk("%02X ", buffer[i]);
}
printk("\n");
retval = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 0x01), "Hello World!", strlen("Hello World!") + 1, NULL, 5000);
if (retval < 0) {
printk(KERN_ERR "Failed to write data to USB device (%d)\n", retval);
goto error;
}
kfree(buffer);
return 0;
error:
kfree(buffer);
return retval;
}
static void my_usb_disconnect(struct usb_interface *interface)
{
printk(KERN_INFO "My USB device removed\n");
}
static struct usb_driver my_usb_driver = {
.name = "my_usb_driver",
.id_table = my_usb_table,
.probe = my_usb_probe,
.disconnect = my_usb_disconnect,
};
static int __init my_usb_init(void)
{
int retval;
retval = usb_register(&my_usb_driver);
if (retval < 0) {
printk(KERN_ERR "Failed to register USB driver (%d)\n", retval);
return retval;
}
printk(KERN_INFO "My USB driver module loaded\n");
return 0;
}
static void __exit my_usb_exit(void)
{
usb_deregister(&my_usb_driver);
printk(KERN_INFO "My USB driver module unloaded\n");
}
module_init(my_usb_init);
module_exit(my_usb_exit);
MODULE_LICENSE("GPL");
```
这段代码实现了一个名为my_usb_driver的USB驱动程序,用于与指定的USB设备通信。在probe函数中,我们获取了设备的Endpoint信息,并使用usb_bulk_msg函数进行了读写操作。在disconnect函数中,我们简单地输出了设备的移除信息。
需要注意的是,此代码中的USB_VENDOR_ID和USB_PRODUCT_ID需要替换为实际的USB设备的Vendor ID和Product ID。这些信息可以通过使用lsusb命令来获取。另外,在编译此代码时,需要链接USB库。可以使用以下命令进行编译:
```shell
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
```
其中,$(uname -r)用于获取当前Linux内核的版本号。
阅读全文