I guess if I can give you some hint here. Once USP port plug in, the USB controller detect and read low level info like,
VendorID, ProductID then it send to USB-core code, look for it. USB-core will send info to Kernel, Kernel will search, in this case I believe is USB_HID category, for the match ProductIS and VendorID to luanch the driver to handle that specific device.
In your case is Pendrive. From there you can search and go on.... Udev and Udev rules will involve at some point. good luck.