xref: /linux/Documentation/driver-api/usb/callbacks.rst (revision 3b38e4f21868d83ed03d5d101c789c4df2b08e8b)
1*3b38e4f2SMauro Carvalho ChehabUSB core callbacks
2*3b38e4f2SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~
3*3b38e4f2SMauro Carvalho Chehab
4*3b38e4f2SMauro Carvalho ChehabWhat callbacks will usbcore do?
5*3b38e4f2SMauro Carvalho Chehab===============================
6*3b38e4f2SMauro Carvalho Chehab
7*3b38e4f2SMauro Carvalho ChehabUsbcore will call into a driver through callbacks defined in the driver
8*3b38e4f2SMauro Carvalho Chehabstructure and through the completion handler of URBs a driver submits.
9*3b38e4f2SMauro Carvalho ChehabOnly the former are in the scope of this document. These two kinds of
10*3b38e4f2SMauro Carvalho Chehabcallbacks are completely independent of each other. Information on the
11*3b38e4f2SMauro Carvalho Chehabcompletion callback can be found in Documentation/usb/URB.txt.
12*3b38e4f2SMauro Carvalho Chehab
13*3b38e4f2SMauro Carvalho ChehabThe callbacks defined in the driver structure are:
14*3b38e4f2SMauro Carvalho Chehab
15*3b38e4f2SMauro Carvalho Chehab1. Hotplugging callbacks:
16*3b38e4f2SMauro Carvalho Chehab
17*3b38e4f2SMauro Carvalho Chehab - @probe:
18*3b38e4f2SMauro Carvalho Chehab	Called to see if the driver is willing to manage a particular
19*3b38e4f2SMauro Carvalho Chehab	interface on a device.
20*3b38e4f2SMauro Carvalho Chehab
21*3b38e4f2SMauro Carvalho Chehab - @disconnect:
22*3b38e4f2SMauro Carvalho Chehab	Called when the interface is no longer accessible, usually
23*3b38e4f2SMauro Carvalho Chehab	because its device has been (or is being) disconnected or the
24*3b38e4f2SMauro Carvalho Chehab	driver module is being unloaded.
25*3b38e4f2SMauro Carvalho Chehab
26*3b38e4f2SMauro Carvalho Chehab2. Odd backdoor through usbfs:
27*3b38e4f2SMauro Carvalho Chehab
28*3b38e4f2SMauro Carvalho Chehab - @ioctl:
29*3b38e4f2SMauro Carvalho Chehab	Used for drivers that want to talk to userspace through
30*3b38e4f2SMauro Carvalho Chehab	the "usbfs" filesystem.  This lets devices provide ways to
31*3b38e4f2SMauro Carvalho Chehab	expose information to user space regardless of where they
32*3b38e4f2SMauro Carvalho Chehab	do (or don't) show up otherwise in the filesystem.
33*3b38e4f2SMauro Carvalho Chehab
34*3b38e4f2SMauro Carvalho Chehab3. Power management (PM) callbacks:
35*3b38e4f2SMauro Carvalho Chehab
36*3b38e4f2SMauro Carvalho Chehab - @suspend:
37*3b38e4f2SMauro Carvalho Chehab	Called when the device is going to be suspended.
38*3b38e4f2SMauro Carvalho Chehab
39*3b38e4f2SMauro Carvalho Chehab - @resume:
40*3b38e4f2SMauro Carvalho Chehab	Called when the device is being resumed.
41*3b38e4f2SMauro Carvalho Chehab
42*3b38e4f2SMauro Carvalho Chehab - @reset_resume:
43*3b38e4f2SMauro Carvalho Chehab	Called when the suspended device has been reset instead
44*3b38e4f2SMauro Carvalho Chehab	of being resumed.
45*3b38e4f2SMauro Carvalho Chehab
46*3b38e4f2SMauro Carvalho Chehab4. Device level operations:
47*3b38e4f2SMauro Carvalho Chehab
48*3b38e4f2SMauro Carvalho Chehab - @pre_reset:
49*3b38e4f2SMauro Carvalho Chehab	Called when the device is about to be reset.
50*3b38e4f2SMauro Carvalho Chehab
51*3b38e4f2SMauro Carvalho Chehab - @post_reset:
52*3b38e4f2SMauro Carvalho Chehab	Called after the device has been reset
53*3b38e4f2SMauro Carvalho Chehab
54*3b38e4f2SMauro Carvalho ChehabThe ioctl interface (2) should be used only if you have a very good
55*3b38e4f2SMauro Carvalho Chehabreason. Sysfs is preferred these days. The PM callbacks are covered
56*3b38e4f2SMauro Carvalho Chehabseparately in Documentation/usb/power-management.txt.
57*3b38e4f2SMauro Carvalho Chehab
58*3b38e4f2SMauro Carvalho ChehabCalling conventions
59*3b38e4f2SMauro Carvalho Chehab===================
60*3b38e4f2SMauro Carvalho Chehab
61*3b38e4f2SMauro Carvalho ChehabAll callbacks are mutually exclusive. There's no need for locking
62*3b38e4f2SMauro Carvalho Chehabagainst other USB callbacks. All callbacks are called from a task
63*3b38e4f2SMauro Carvalho Chehabcontext. You may sleep. However, it is important that all sleeps have a
64*3b38e4f2SMauro Carvalho Chehabsmall fixed upper limit in time. In particular you must not call out to
65*3b38e4f2SMauro Carvalho Chehabuser space and await results.
66*3b38e4f2SMauro Carvalho Chehab
67*3b38e4f2SMauro Carvalho ChehabHotplugging callbacks
68*3b38e4f2SMauro Carvalho Chehab=====================
69*3b38e4f2SMauro Carvalho Chehab
70*3b38e4f2SMauro Carvalho ChehabThese callbacks are intended to associate and disassociate a driver with
71*3b38e4f2SMauro Carvalho Chehaban interface. A driver's bond to an interface is exclusive.
72*3b38e4f2SMauro Carvalho Chehab
73*3b38e4f2SMauro Carvalho ChehabThe probe() callback
74*3b38e4f2SMauro Carvalho Chehab--------------------
75*3b38e4f2SMauro Carvalho Chehab
76*3b38e4f2SMauro Carvalho Chehab::
77*3b38e4f2SMauro Carvalho Chehab
78*3b38e4f2SMauro Carvalho Chehab  int (*probe) (struct usb_interface *intf,
79*3b38e4f2SMauro Carvalho Chehab		const struct usb_device_id *id);
80*3b38e4f2SMauro Carvalho Chehab
81*3b38e4f2SMauro Carvalho ChehabAccept or decline an interface. If you accept the device return 0,
82*3b38e4f2SMauro Carvalho Chehabotherwise -ENODEV or -ENXIO. Other error codes should be used only if a
83*3b38e4f2SMauro Carvalho Chehabgenuine error occurred during initialisation which prevented a driver
84*3b38e4f2SMauro Carvalho Chehabfrom accepting a device that would else have been accepted.
85*3b38e4f2SMauro Carvalho ChehabYou are strongly encouraged to use usbcore's facility,
86*3b38e4f2SMauro Carvalho Chehabusb_set_intfdata(), to associate a data structure with an interface, so
87*3b38e4f2SMauro Carvalho Chehabthat you know which internal state and identity you associate with a
88*3b38e4f2SMauro Carvalho Chehabparticular interface. The device will not be suspended and you may do IO
89*3b38e4f2SMauro Carvalho Chehabto the interface you are called for and endpoint 0 of the device. Device
90*3b38e4f2SMauro Carvalho Chehabinitialisation that doesn't take too long is a good idea here.
91*3b38e4f2SMauro Carvalho Chehab
92*3b38e4f2SMauro Carvalho ChehabThe disconnect() callback
93*3b38e4f2SMauro Carvalho Chehab-------------------------
94*3b38e4f2SMauro Carvalho Chehab
95*3b38e4f2SMauro Carvalho Chehab::
96*3b38e4f2SMauro Carvalho Chehab
97*3b38e4f2SMauro Carvalho Chehab  void (*disconnect) (struct usb_interface *intf);
98*3b38e4f2SMauro Carvalho Chehab
99*3b38e4f2SMauro Carvalho ChehabThis callback is a signal to break any connection with an interface.
100*3b38e4f2SMauro Carvalho ChehabYou are not allowed any IO to a device after returning from this
101*3b38e4f2SMauro Carvalho Chehabcallback. You also may not do any other operation that may interfere
102*3b38e4f2SMauro Carvalho Chehabwith another driver bound the interface, eg. a power management
103*3b38e4f2SMauro Carvalho Chehaboperation.
104*3b38e4f2SMauro Carvalho ChehabIf you are called due to a physical disconnection, all your URBs will be
105*3b38e4f2SMauro Carvalho Chehabkilled by usbcore. Note that in this case disconnect will be called some
106*3b38e4f2SMauro Carvalho Chehabtime after the physical disconnection. Thus your driver must be prepared
107*3b38e4f2SMauro Carvalho Chehabto deal with failing IO even prior to the callback.
108*3b38e4f2SMauro Carvalho Chehab
109*3b38e4f2SMauro Carvalho ChehabDevice level callbacks
110*3b38e4f2SMauro Carvalho Chehab======================
111*3b38e4f2SMauro Carvalho Chehab
112*3b38e4f2SMauro Carvalho Chehabpre_reset
113*3b38e4f2SMauro Carvalho Chehab---------
114*3b38e4f2SMauro Carvalho Chehab
115*3b38e4f2SMauro Carvalho Chehab::
116*3b38e4f2SMauro Carvalho Chehab
117*3b38e4f2SMauro Carvalho Chehab  int (*pre_reset)(struct usb_interface *intf);
118*3b38e4f2SMauro Carvalho Chehab
119*3b38e4f2SMauro Carvalho ChehabA driver or user space is triggering a reset on the device which
120*3b38e4f2SMauro Carvalho Chehabcontains the interface passed as an argument. Cease IO, wait for all
121*3b38e4f2SMauro Carvalho Chehaboutstanding URBs to complete, and save any device state you need to
122*3b38e4f2SMauro Carvalho Chehabrestore.  No more URBs may be submitted until the post_reset method
123*3b38e4f2SMauro Carvalho Chehabis called.
124*3b38e4f2SMauro Carvalho Chehab
125*3b38e4f2SMauro Carvalho ChehabIf you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
126*3b38e4f2SMauro Carvalho Chehabare in atomic context.
127*3b38e4f2SMauro Carvalho Chehab
128*3b38e4f2SMauro Carvalho Chehabpost_reset
129*3b38e4f2SMauro Carvalho Chehab----------
130*3b38e4f2SMauro Carvalho Chehab
131*3b38e4f2SMauro Carvalho Chehab::
132*3b38e4f2SMauro Carvalho Chehab
133*3b38e4f2SMauro Carvalho Chehab  int (*post_reset)(struct usb_interface *intf);
134*3b38e4f2SMauro Carvalho Chehab
135*3b38e4f2SMauro Carvalho ChehabThe reset has completed.  Restore any saved device state and begin
136*3b38e4f2SMauro Carvalho Chehabusing the device again.
137*3b38e4f2SMauro Carvalho Chehab
138*3b38e4f2SMauro Carvalho ChehabIf you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
139*3b38e4f2SMauro Carvalho Chehabare in atomic context.
140*3b38e4f2SMauro Carvalho Chehab
141*3b38e4f2SMauro Carvalho ChehabCall sequences
142*3b38e4f2SMauro Carvalho Chehab==============
143*3b38e4f2SMauro Carvalho Chehab
144*3b38e4f2SMauro Carvalho ChehabNo callbacks other than probe will be invoked for an interface
145*3b38e4f2SMauro Carvalho Chehabthat isn't bound to your driver.
146*3b38e4f2SMauro Carvalho Chehab
147*3b38e4f2SMauro Carvalho ChehabProbe will never be called for an interface bound to a driver.
148*3b38e4f2SMauro Carvalho ChehabHence following a successful probe, disconnect will be called
149*3b38e4f2SMauro Carvalho Chehabbefore there is another probe for the same interface.
150*3b38e4f2SMauro Carvalho Chehab
151*3b38e4f2SMauro Carvalho ChehabOnce your driver is bound to an interface, disconnect can be
152*3b38e4f2SMauro Carvalho Chehabcalled at any time except in between pre_reset and post_reset.
153*3b38e4f2SMauro Carvalho Chehabpre_reset is always followed by post_reset, even if the reset
154*3b38e4f2SMauro Carvalho Chehabfailed or the device has been unplugged.
155*3b38e4f2SMauro Carvalho Chehab
156*3b38e4f2SMauro Carvalho Chehabsuspend is always followed by one of: resume, reset_resume, or
157*3b38e4f2SMauro Carvalho Chehabdisconnect.
158