162306a36Sopenharmony_ciUSB core callbacks 262306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~ 362306a36Sopenharmony_ci 462306a36Sopenharmony_ciWhat callbacks will usbcore do? 562306a36Sopenharmony_ci=============================== 662306a36Sopenharmony_ci 762306a36Sopenharmony_ciUsbcore will call into a driver through callbacks defined in the driver 862306a36Sopenharmony_cistructure and through the completion handler of URBs a driver submits. 962306a36Sopenharmony_ciOnly the former are in the scope of this document. These two kinds of 1062306a36Sopenharmony_cicallbacks are completely independent of each other. Information on the 1162306a36Sopenharmony_cicompletion callback can be found in :ref:`usb-urb`. 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciThe callbacks defined in the driver structure are: 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci1. Hotplugging callbacks: 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci - @probe: 1862306a36Sopenharmony_ci Called to see if the driver is willing to manage a particular 1962306a36Sopenharmony_ci interface on a device. 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci - @disconnect: 2262306a36Sopenharmony_ci Called when the interface is no longer accessible, usually 2362306a36Sopenharmony_ci because its device has been (or is being) disconnected or the 2462306a36Sopenharmony_ci driver module is being unloaded. 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci2. Odd backdoor through usbfs: 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci - @ioctl: 2962306a36Sopenharmony_ci Used for drivers that want to talk to userspace through 3062306a36Sopenharmony_ci the "usbfs" filesystem. This lets devices provide ways to 3162306a36Sopenharmony_ci expose information to user space regardless of where they 3262306a36Sopenharmony_ci do (or don't) show up otherwise in the filesystem. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci3. Power management (PM) callbacks: 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci - @suspend: 3762306a36Sopenharmony_ci Called when the device is going to be suspended. 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci - @resume: 4062306a36Sopenharmony_ci Called when the device is being resumed. 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci - @reset_resume: 4362306a36Sopenharmony_ci Called when the suspended device has been reset instead 4462306a36Sopenharmony_ci of being resumed. 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci4. Device level operations: 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci - @pre_reset: 4962306a36Sopenharmony_ci Called when the device is about to be reset. 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci - @post_reset: 5262306a36Sopenharmony_ci Called after the device has been reset 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ciThe ioctl interface (2) should be used only if you have a very good 5562306a36Sopenharmony_cireason. Sysfs is preferred these days. The PM callbacks are covered 5662306a36Sopenharmony_ciseparately in :ref:`usb-power-management`. 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ciCalling conventions 5962306a36Sopenharmony_ci=================== 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ciAll callbacks are mutually exclusive. There's no need for locking 6262306a36Sopenharmony_ciagainst other USB callbacks. All callbacks are called from a task 6362306a36Sopenharmony_cicontext. You may sleep. However, it is important that all sleeps have a 6462306a36Sopenharmony_cismall fixed upper limit in time. In particular you must not call out to 6562306a36Sopenharmony_ciuser space and await results. 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ciHotplugging callbacks 6862306a36Sopenharmony_ci===================== 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ciThese callbacks are intended to associate and disassociate a driver with 7162306a36Sopenharmony_cian interface. A driver's bond to an interface is exclusive. 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ciThe probe() callback 7462306a36Sopenharmony_ci-------------------- 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci:: 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci int (*probe) (struct usb_interface *intf, 7962306a36Sopenharmony_ci const struct usb_device_id *id); 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ciAccept or decline an interface. If you accept the device return 0, 8262306a36Sopenharmony_ciotherwise -ENODEV or -ENXIO. Other error codes should be used only if a 8362306a36Sopenharmony_cigenuine error occurred during initialisation which prevented a driver 8462306a36Sopenharmony_cifrom accepting a device that would else have been accepted. 8562306a36Sopenharmony_ciYou are strongly encouraged to use usbcore's facility, 8662306a36Sopenharmony_ciusb_set_intfdata(), to associate a data structure with an interface, so 8762306a36Sopenharmony_cithat you know which internal state and identity you associate with a 8862306a36Sopenharmony_ciparticular interface. The device will not be suspended and you may do IO 8962306a36Sopenharmony_cito the interface you are called for and endpoint 0 of the device. Device 9062306a36Sopenharmony_ciinitialisation that doesn't take too long is a good idea here. 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ciThe disconnect() callback 9362306a36Sopenharmony_ci------------------------- 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci:: 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci void (*disconnect) (struct usb_interface *intf); 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ciThis callback is a signal to break any connection with an interface. 10062306a36Sopenharmony_ciYou are not allowed any IO to a device after returning from this 10162306a36Sopenharmony_cicallback. You also may not do any other operation that may interfere 10262306a36Sopenharmony_ciwith another driver bound the interface, eg. a power management 10362306a36Sopenharmony_cioperation. 10462306a36Sopenharmony_ciIf you are called due to a physical disconnection, all your URBs will be 10562306a36Sopenharmony_cikilled by usbcore. Note that in this case disconnect will be called some 10662306a36Sopenharmony_citime after the physical disconnection. Thus your driver must be prepared 10762306a36Sopenharmony_cito deal with failing IO even prior to the callback. 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ciDevice level callbacks 11062306a36Sopenharmony_ci====================== 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_cipre_reset 11362306a36Sopenharmony_ci--------- 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci:: 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci int (*pre_reset)(struct usb_interface *intf); 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ciA driver or user space is triggering a reset on the device which 12062306a36Sopenharmony_cicontains the interface passed as an argument. Cease IO, wait for all 12162306a36Sopenharmony_cioutstanding URBs to complete, and save any device state you need to 12262306a36Sopenharmony_cirestore. No more URBs may be submitted until the post_reset method 12362306a36Sopenharmony_ciis called. 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ciIf you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you 12662306a36Sopenharmony_ciare in atomic context. 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_cipost_reset 12962306a36Sopenharmony_ci---------- 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci:: 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci int (*post_reset)(struct usb_interface *intf); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ciThe reset has completed. Restore any saved device state and begin 13662306a36Sopenharmony_ciusing the device again. 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciIf you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you 13962306a36Sopenharmony_ciare in atomic context. 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ciCall sequences 14262306a36Sopenharmony_ci============== 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ciNo callbacks other than probe will be invoked for an interface 14562306a36Sopenharmony_cithat isn't bound to your driver. 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ciProbe will never be called for an interface bound to a driver. 14862306a36Sopenharmony_ciHence following a successful probe, disconnect will be called 14962306a36Sopenharmony_cibefore there is another probe for the same interface. 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ciOnce your driver is bound to an interface, disconnect can be 15262306a36Sopenharmony_cicalled at any time except in between pre_reset and post_reset. 15362306a36Sopenharmony_cipre_reset is always followed by post_reset, even if the reset 15462306a36Sopenharmony_cifailed or the device has been unplugged. 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_cisuspend is always followed by one of: resume, reset_resume, or 15762306a36Sopenharmony_cidisconnect. 158