Hi I am learning linux kernel and wrote a simple function as below and this is the USB port I want to know whether it is plugged in or unplugged. However when I print it out I don't see any relevant information. What am I doing wrong ?
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/usb.h>
static int pen_probe(struct usb_interface *interface, const struct usb_device_id* id)
{
printk(KERN_INFO "Pen drive(%04X:%04X) plugged\n", id->idVendor, id->idProduct);
return 0;
}
static void pen_disconnect(struct usb_interface * interface)
{
printk(KERN_INFO "PEN drive removed \n");
}
static struct usb_device_id pen_table[]=
{
{ USB_DEVICE(0x046d, 0xc077) },
{}
};
MODULE_DEVICE_TABLE(usb, pen_table);
static struct usb_driver pen_driver=
{
.name ="pen_driver",
.id_table = pen_table,
.probe = pen_probe,
.disconnect = pen_disconnect,
};
static int __init pen_init(void)
{
return usb_register(&pen_driver);
}
static void __exit pen_exit(void)
{
usb_deregister(&pen_driver);
}
module_init(pen_init);
module_exit(pen_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("DY");
MODULE_DESCRIPTION("USB PEN REGISTRATION DRIVER");
This is the usb device I want to know about its status
Bus 001 Device 015: ID 046d:c077 Logitech, Inc. M105 Optical Mouse
I declared it in the pen_table as { USB_DEVICE(0x046d, 0xc077) },
When I load the module I see it gets registered.
If you see something that is odd please let me know : )
Thank you :)
update april 17
I cleared dmesg and rmmod and insmod usbDriver.ko
/usbDriver2$ sudo rmmod usbDriver.ko
/usbDriver2$ sudo insmod usbDriver.ko
/usbDriver2$ dmesg
[226424.175602] usbcore: deregistering interface driver pen_driver
[226430.328011] usbcore: registered new interface driver pen_driver
after unplugged/plugged the device
[226424.175602] usbcore: deregistering interface driver pen_driver
[226430.328011] usbcore: registered new interface driver pen_driver
[226450.316160] usb 1-13.1: USB disconnect, device number 6
[226450.316172] usb 1-13.1.2: USB disconnect, device number 8
[226452.076365] usb 1-13.1: new high-speed USB device number 9 using xhci_hcd
[226452.184835] usb 1-13.1: New USB device found, idVendor=05ac, idProduct=1006, bcdDevice=96.15
[226452.184848] usb 1-13.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[226452.184854] usb 1-13.1: Product: Keyboard Hub
[226452.184858] usb 1-13.1: Manufacturer: Apple, Inc.
[226452.184862] usb 1-13.1: SerialNumber: 000000000000
[226452.185928] hub 1-13.1:1.0: USB hub found
[226452.185947] hub 1-13.1:1.0: 3 ports detected
[226452.364192] usb 1-13.4: USB disconnect, device number 7
[226452.476398] usb 1-13.1.2: new low-speed USB device number 10 using xhci_hcd
[226452.583584] usb 1-13.1.2: New USB device found, idVendor=05ac, idProduct=024f, bcdDevice= 0.74
[226452.583597] usb 1-13.1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[226452.583603] usb 1-13.1.2: Product: Apple Keyboard
[226452.583607] usb 1-13.1.2: Manufacturer: Apple Inc.
[226452.593082] input: Apple Inc. Apple Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-13/1-13.1/1-13.1.2/1-13.1.2:1.0/0003:05AC:024F.0005/input/input19
[226452.653036] apple 0003:05AC:024F.0005: input,hidraw1: USB HID v1.11 Keyboard [Apple Inc. Apple Keyboard] on usb-0000:00:14.0-13.1.2/input0
[226452.655754] apple 0003:05AC:024F.0006: Fn key not found (Apple Wireless Keyboard clone?), disabling Fn key handling
[226452.655851] input: Apple Inc. Apple Keyboard as /devices/pci0000:00/0000:00:14.0/usb1/1-13/1-13.1/1-13.1.2/1-13.1.2:1.1/0003:05AC:024F.0006/input/input20
[226452.712769] apple 0003:05AC:024F.0006: input,hidraw2: USB HID v1.11 Device [Apple Inc. Apple Keyboard] on usb-0000:00:14.0-13.1.2/input1
[226454.124363] usb 1-13.4: new low-speed USB device number 11 using xhci_hcd
[226454.232775] usb 1-13.4: New USB device found, idVendor=046d, idProduct=c077, bcdDevice=72.00
[226454.232789] usb 1-13.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[226454.232795] usb 1-13.4: Product: USB Optical Mouse
[226454.232800] usb 1-13.4: Manufacturer: Logitech
[226454.238523] input: Logitech USB Optical Mouse as /devices/pci0000:00/0000:00:14.0/usb1/1-13/1-13.4/1-13.4:1.0/0003:046D:C077.0007/input/input21
[226454.239027] hid-generic 0003:046D:C077.0007: input,hidraw3: USB HID v1.11 Mouse [Logitech USB Optical Mouse] on usb-0000:00:14.0-13.4/input0
/usbDriver2$ lspci
00:00.0 Host bridge: Intel Corporation 8th Gen Core Processor Host Bridge/DRAM Registers (rev 0a)
00:02.0 VGA compatible controller: Intel Corporation UHD Graphics 630 (Desktop 9 Series)
00:12.0 Signal processing controller: Intel Corporation Cannon Lake PCH Thermal Controller (rev 10)
00:14.0 USB controller: Intel Corporation Cannon Lake PCH USB 3.1 xHCI Host Controller (rev 10)
00:14.2 RAM memory: Intel Corporation Cannon Lake PCH Shared SRAM (rev 10)
00:14.3 Network controller: Intel Corporation Wireless-AC 9560 [Jefferson Peak] (rev 10)
00:16.0 Communication controller: Intel Corporation Cannon Lake PCH HECI Controller (rev 10)
00:17.0 RAID bus controller: Intel Corporation SATA Controller [RAID mode] (rev 10)
00:1b.0 PCI bridge: Intel Corporation Cannon Lake PCH PCI Express Root Port #17 (rev f0)
00:1c.0 PCI bridge: Intel Corporation Cannon Lake PCH PCI Express Root Port #1 (rev f0)
00:1d.0 PCI bridge: Intel Corporation Cannon Lake PCH PCI Express Root Port #9 (rev f0)
00:1f.0 ISA bridge: Intel Corporation Z390 Chipset LPC/eSPI Controller (rev 10)
00:1f.3 Audio device: Intel Corporation Cannon Lake PCH cAVS (rev 10)
00:1f.4 SMBus: Intel Corporation Cannon Lake PCH SMBus Controller (rev 10)
00:1f.5 Serial bus controller [0c80]: Intel Corporation Cannon Lake PCH SPI Controller (rev 10)
00:1f.6 Ethernet controller: Intel Corporation Ethernet Connection (7) I219-V (rev 10)