Sungju's Slow Life

Personal journal


How USB assign an address for a new device

When you plug a new usb device, it assigns a new address information even though it is the same device and there was no addition in between this unplug and plug operation.

This is because kernel assign a new address each time, it is connected as you can see in below.

 914 static void choose_address(struct usb_device *udev)
 915 {
 916         int             devnum;
 917         struct usb_bus  *bus = udev->bus;
 918 
 919         /* If khubd ever becomes multithreaded, this will need a lock */
 920 
 921         /* Try to allocate the next devnum beginning at bus->devnum_next. */
 922         devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
 923                         bus->devnum_next);
 924         if (devnum >= 128)
 925                 devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1);
 926 
 927         bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
 928 
 929         if (devnum devmap.devicemap);
 931                 udev->devnum = devnum;
 932         }
 933 }
 934 
 935 static void release_address(struct usb_device *udev)
 936 {
 937         if (udev->devnum > 0) {
 938                 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
 939                 udev->devnum = -1;
 940         }
 941 }


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: