Reputation: 681
I am currently trying to list all usb devices that are connected to a Linux system, the code will be running in the kernel, as an LSM. The lsm code is known good.
I have looked at this question but it was asked six years ago.
The answer to the question suggested using the following code:
struct usb_device udev;
struct usb_bus *bus;
ssize_t ret;
static int __init usb_fun_init (void)
{
int result;
__le16 idVendor = 0;
__le16 idProduct = 0;
__u8 iManufacturer = 0;
__u8 iSerialNumber = 0;
printk(KERN_INFO "\n************************************ in init\n");
list_for_each_entry(bus, &usb_bus_list, bus_list)
{
printk(KERN_INFO "***************** Begins ****************");
printk(KERN_INFO "Vendor ID = %x", bus->root_hub->descriptor.idVendor);
printk(KERN_INFO "Product ID = %x", bus->root_hub->descriptor.idProduct);
printk(KERN_INFO "Serial Number = %x", bus->root_hub->descriptor.iSerialNumber);
//printk(KERN_INFO "Manu = %s", bus->root_hub->descriptor.iManufacturer);
printk(KERN_INFO "Manu = %s", bus->root_hub->manufacturer);
printk(KERN_INFO "Product = %s", bus->root_hub->product);
printk(KERN_INFO "Serial Number = %s", bus->root_hub->serial);
printk(KERN_INFO "\nManufacturer = %s", udev.bus.iManufacturer);
}
return 0;
}
static void __exit usb_fun_exit (void)
{
printk(KERN_INFO "\n************************************ in exit\n");
}
module_init(usb_fun_init);
module_exit(usb_fun_exit);
MODULE_LICENSE("GPL");
However, the compilation errors out. From what i can see, the kernel structures have changed, i have poked about the header files, and the only likely candidate i can see is usb_bus_id
as it has the same method signature, and return type. However this does not work either. Could someone please point me in the right direction?
Upvotes: 0
Views: 1022
Reputation: 681
The list_for_each_entry
macro has been replaced with idr_for_each_entry
for listing USB devices connected to they system.
The following code enumerates USB devices and any children connected to a system with a Linux kernel. The answer code takes the form as a kernel module, but could easily be ported to an LSM.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#include <linux/list.h>
MODULE_LICENSE("GPL");
struct usb_device udev;
struct usb_bus *bus;
ssize_t ret;
static int __init usb_fun_init (void)
{
int id;
int chix;
struct usb_device *dev, *childdev = NULL;
printk(KERN_INFO "\n************************************ in init\n");
mutex_lock(&usb_bus_idr_lock);
idr_for_each_entry(&usb_bus_idr, bus, id)
{
printk(KERN_INFO "***************** Begins ****************");
printk(KERN_INFO "Vendor ID = %x", bus->root_hub->descriptor.idVendor);
printk(KERN_INFO "Product ID = %x", bus->root_hub->descriptor.idProduct);
printk(KERN_INFO "Serial Number = %x", bus->root_hub->descriptor.iSerialNumber);
//printk(KERN_INFO "Manu = %s", bus->root_hub->descriptor.iManufacturer);
printk(KERN_INFO "Manu = %s", bus->root_hub->manufacturer);
printk(KERN_INFO "Product = %s", bus->root_hub->product);
printk(KERN_INFO "Serial Number = %s", bus->root_hub->serial);
dev = bus->root_hub;
usb_hub_for_each_child(dev, chix, childdev)
{
if(childdev)
{
printk(KERN_INFO "***************** Child device ****************");
usb_lock_device(childdev);
printk(KERN_INFO "Vendor ID = %x", childdev->descriptor.idVendor);
printk(KERN_INFO "Product ID = %x", childdev->descriptor.idProduct);
printk(KERN_INFO "Serial Number = %x", childdev->descriptor.iSerialNumber);
printk(KERN_INFO "Manu = %s", childdev->manufacturer);
printk(KERN_INFO "Product = %s", childdev->product);
printk(KERN_INFO "Serial Number = %s", childdev->serial);
usb_unlock_device(childdev);
}
}
}
mutex_unlock(&usb_bus_idr_lock);
return 0;
}
static void __exit usb_fun_exit (void)
{
printk(KERN_INFO "\n************************************ in exit\n");
}
module_init(usb_fun_init);
module_exit(usb_fun_exit);
Sample output:
Feb 29 03:13:46 ubuntu kernel: [ 2743.505160] ***************** Begins ****************
Feb 29 03:13:46 ubuntu kernel: [ 2743.505161] Vendor ID = 1d6b
Feb 29 03:13:46 ubuntu kernel: [ 2743.505161] Product ID = 2
Feb 29 03:13:46 ubuntu kernel: [ 2743.505161] Serial Number = 1
Feb 29 03:13:46 ubuntu kernel: [ 2743.505162] Manu = Linux 5.0.0-23-generic ehci_hcd
Feb 29 03:13:46 ubuntu kernel: [ 2743.505163] Product = EHCI Host Controller
Feb 29 03:13:46 ubuntu kernel: [ 2743.505163] Serial Number = 0000:02:03.0
Feb 29 03:13:46 ubuntu kernel: [ 2743.505164] ***************** Child device ****************
Feb 29 03:13:46 ubuntu kernel: [ 2743.505165]
Feb 29 03:13:46 ubuntu kernel: [ 2743.505165] Vendor Id:26bd, Product Id:9917
Feb 29 03:13:46 ubuntu kernel: [ 2743.505165] Vendor ID = 26bd
Feb 29 03:13:46 ubuntu kernel: [ 2743.505166] Product ID = 9917
Feb 29 03:13:46 ubuntu kernel: [ 2743.505166] Serial Number = 3
Feb 29 03:13:46 ubuntu kernel: [ 2743.505166] Manu =
Feb 29 03:13:46 ubuntu kernel: [ 2743.505167] Product = USB DISK 2.0
Feb 29 03:13:46 ubuntu kernel: [ 2743.505167] Serial Number = 070172966462EB10
Upvotes: 1