[PATCH] add usb_add_bus() function for usb bus list

From: Tomohiro Kusumi
Date: Fri Apr 23 2010 - 01:45:57 EST


Hi,

This patch makes usbcore driver sort usb bus on the usb_bus_list by busnum when
registering a new bus instance.

In current implementation a new bus instance is simply added in front of existing busses.
Although there is nothing wrong with it in kernel code, reloading HCD may break bus order
of /proc/bus/usb/devices as it makes usbcore re-register the bus to the list as shown in
the example below. and it doesn't look good or it may confuse some users.

# lsmod | grep hcd
uhci_hcd 19324 0
ehci_hcd 39109 0
# grep "Bus=" /proc/bus/usb/devices
T: Bus=05 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=04 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=03 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=1.5 MxCh= 0
T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 3 Spd=12 MxCh= 0
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8
T: Bus=01 Lev=01 Prnt=01 Port=02 Cnt=01 Dev#= 4 Spd=480 MxCh= 4
# rmmod ehci-hcd; modprobe ehci-hcd
# grep "Bus=" /proc/bus/usb/devices
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8
T: Bus=01 Lev=01 Prnt=01 Port=02 Cnt=01 Dev#= 4 Spd=480 MxCh= 4
T: Bus=05 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=04 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=03 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 4 Spd=1.5 MxCh= 0
T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 5 Spd=12 MxCh= 0

With this patch, bus instances on the usb_bus_list is kept in busnum order (from large to
small busnum). As a result /proc/bus/usb/devices prints devices in busnum order regardless
of HCD reloading. Although a few applications (eg.lsusb) use /proc/bus/usb/devices, this
patch has no effect to them.

# lsmod | grep hcd
uhci_hcd 19324 0
ehci_hcd 39109 0
# grep "Bus=" /proc/bus/usb/devices
T: Bus=05 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=04 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=03 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=1.5 MxCh= 0
T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 3 Spd=12 MxCh= 0
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8
T: Bus=01 Lev=01 Prnt=01 Port=02 Cnt=01 Dev#= 4 Spd=480 MxCh= 4
# rmmod ehci-hcd; modprobe ehci-hcd
# grep "Bus=" /proc/bus/usb/devices
T: Bus=05 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=04 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=03 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 4 Spd=1.5 MxCh= 0
T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 5 Spd=12 MxCh= 0
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8
T: Bus=01 Lev=01 Prnt=01 Port=02 Cnt=01 Dev#= 4 Spd=480 MxCh= 4

Thanks,
Tomohiro Kusumi




Signed-off-by: Tomohiro Kusumi <kusumi.tomohiro@xxxxxxxxxxxxxx>
---

diff -aNur linux-2.6.34-rc5.org/drivers/usb/core/hcd.c linux-2.6.34-rc5/drivers/usb/core/hcd.c
--- linux-2.6.34-rc5.org/drivers/usb/core/hcd.c 2010-04-20 08:29:56.000000000 +0900
+++ linux-2.6.34-rc5/drivers/usb/core/hcd.c 2010-04-23 14:19:50.000000000 +0900
@@ -877,6 +877,23 @@
/*-------------------------------------------------------------------------*/

/**
+ * usb_add_bus - register the bus to the usb_bus_list,
+ * caller need to hold usb_bus_list_lock
+ * @bus: pointer to the bus to register
+ */
+static void usb_add_bus(struct usb_bus *bus)
+{
+ struct usb_bus *b;
+ list_for_each_entry(b, &usb_bus_list, bus_list) {
+ if (bus->busnum > b->busnum) {
+ list_add_tail(&bus->bus_list, &b->bus_list);
+ return;
+ }
+ }
+ list_add_tail(&bus->bus_list, &usb_bus_list);
+}
+
+/**
* usb_register_bus - registers the USB host controller with the usb core
* @bus: pointer to the bus to register
* Context: !in_interrupt()
@@ -899,7 +916,7 @@
bus->busnum = busnum;

/* Add it to the local list of buses */
- list_add (&bus->bus_list, &usb_bus_list);
+ usb_add_bus(bus);
mutex_unlock(&usb_bus_list_lock);

usb_notify_add_bus(bus);

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/