0001 USB core callbacks
0002 ~~~~~~~~~~~~~~~~~~
0003
0004 What callbacks will usbcore do?
0005 ===============================
0006
0007 Usbcore will call into a driver through callbacks defined in the driver
0008 structure and through the completion handler of URBs a driver submits.
0009 Only the former are in the scope of this document. These two kinds of
0010 callbacks are completely independent of each other. Information on the
0011 completion callback can be found in :ref:`usb-urb`.
0012
0013 The callbacks defined in the driver structure are:
0014
0015 1. Hotplugging callbacks:
0016
0017 - @probe:
0018 Called to see if the driver is willing to manage a particular
0019 interface on a device.
0020
0021 - @disconnect:
0022 Called when the interface is no longer accessible, usually
0023 because its device has been (or is being) disconnected or the
0024 driver module is being unloaded.
0025
0026 2. Odd backdoor through usbfs:
0027
0028 - @ioctl:
0029 Used for drivers that want to talk to userspace through
0030 the "usbfs" filesystem. This lets devices provide ways to
0031 expose information to user space regardless of where they
0032 do (or don't) show up otherwise in the filesystem.
0033
0034 3. Power management (PM) callbacks:
0035
0036 - @suspend:
0037 Called when the device is going to be suspended.
0038
0039 - @resume:
0040 Called when the device is being resumed.
0041
0042 - @reset_resume:
0043 Called when the suspended device has been reset instead
0044 of being resumed.
0045
0046 4. Device level operations:
0047
0048 - @pre_reset:
0049 Called when the device is about to be reset.
0050
0051 - @post_reset:
0052 Called after the device has been reset
0053
0054 The ioctl interface (2) should be used only if you have a very good
0055 reason. Sysfs is preferred these days. The PM callbacks are covered
0056 separately in :ref:`usb-power-management`.
0057
0058 Calling conventions
0059 ===================
0060
0061 All callbacks are mutually exclusive. There's no need for locking
0062 against other USB callbacks. All callbacks are called from a task
0063 context. You may sleep. However, it is important that all sleeps have a
0064 small fixed upper limit in time. In particular you must not call out to
0065 user space and await results.
0066
0067 Hotplugging callbacks
0068 =====================
0069
0070 These callbacks are intended to associate and disassociate a driver with
0071 an interface. A driver's bond to an interface is exclusive.
0072
0073 The probe() callback
0074 --------------------
0075
0076 ::
0077
0078 int (*probe) (struct usb_interface *intf,
0079 const struct usb_device_id *id);
0080
0081 Accept or decline an interface. If you accept the device return 0,
0082 otherwise -ENODEV or -ENXIO. Other error codes should be used only if a
0083 genuine error occurred during initialisation which prevented a driver
0084 from accepting a device that would else have been accepted.
0085 You are strongly encouraged to use usbcore's facility,
0086 usb_set_intfdata(), to associate a data structure with an interface, so
0087 that you know which internal state and identity you associate with a
0088 particular interface. The device will not be suspended and you may do IO
0089 to the interface you are called for and endpoint 0 of the device. Device
0090 initialisation that doesn't take too long is a good idea here.
0091
0092 The disconnect() callback
0093 -------------------------
0094
0095 ::
0096
0097 void (*disconnect) (struct usb_interface *intf);
0098
0099 This callback is a signal to break any connection with an interface.
0100 You are not allowed any IO to a device after returning from this
0101 callback. You also may not do any other operation that may interfere
0102 with another driver bound the interface, eg. a power management
0103 operation.
0104 If you are called due to a physical disconnection, all your URBs will be
0105 killed by usbcore. Note that in this case disconnect will be called some
0106 time after the physical disconnection. Thus your driver must be prepared
0107 to deal with failing IO even prior to the callback.
0108
0109 Device level callbacks
0110 ======================
0111
0112 pre_reset
0113 ---------
0114
0115 ::
0116
0117 int (*pre_reset)(struct usb_interface *intf);
0118
0119 A driver or user space is triggering a reset on the device which
0120 contains the interface passed as an argument. Cease IO, wait for all
0121 outstanding URBs to complete, and save any device state you need to
0122 restore. No more URBs may be submitted until the post_reset method
0123 is called.
0124
0125 If you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
0126 are in atomic context.
0127
0128 post_reset
0129 ----------
0130
0131 ::
0132
0133 int (*post_reset)(struct usb_interface *intf);
0134
0135 The reset has completed. Restore any saved device state and begin
0136 using the device again.
0137
0138 If you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
0139 are in atomic context.
0140
0141 Call sequences
0142 ==============
0143
0144 No callbacks other than probe will be invoked for an interface
0145 that isn't bound to your driver.
0146
0147 Probe will never be called for an interface bound to a driver.
0148 Hence following a successful probe, disconnect will be called
0149 before there is another probe for the same interface.
0150
0151 Once your driver is bound to an interface, disconnect can be
0152 called at any time except in between pre_reset and post_reset.
0153 pre_reset is always followed by post_reset, even if the reset
0154 failed or the device has been unplugged.
0155
0156 suspend is always followed by one of: resume, reset_resume, or
0157 disconnect.