xref: /linux/Documentation/driver-api/usb/callbacks.rst (revision 9f2a3933beeaeead53829d3a7be53770e41e7869)
13b38e4f2SMauro Carvalho ChehabUSB core callbacks
23b38e4f2SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~
33b38e4f2SMauro Carvalho Chehab
43b38e4f2SMauro Carvalho ChehabWhat callbacks will usbcore do?
53b38e4f2SMauro Carvalho Chehab===============================
63b38e4f2SMauro Carvalho Chehab
73b38e4f2SMauro Carvalho ChehabUsbcore will call into a driver through callbacks defined in the driver
83b38e4f2SMauro Carvalho Chehabstructure and through the completion handler of URBs a driver submits.
93b38e4f2SMauro Carvalho ChehabOnly the former are in the scope of this document. These two kinds of
103b38e4f2SMauro Carvalho Chehabcallbacks are completely independent of each other. Information on the
11e1c3e6e1SMauro Carvalho Chehabcompletion callback can be found in :ref:`usb-urb`.
123b38e4f2SMauro Carvalho Chehab
133b38e4f2SMauro Carvalho ChehabThe callbacks defined in the driver structure are:
143b38e4f2SMauro Carvalho Chehab
153b38e4f2SMauro Carvalho Chehab1. Hotplugging callbacks:
163b38e4f2SMauro Carvalho Chehab
173b38e4f2SMauro Carvalho Chehab - @probe:
183b38e4f2SMauro Carvalho Chehab	Called to see if the driver is willing to manage a particular
193b38e4f2SMauro Carvalho Chehab	interface on a device.
203b38e4f2SMauro Carvalho Chehab
213b38e4f2SMauro Carvalho Chehab - @disconnect:
223b38e4f2SMauro Carvalho Chehab	Called when the interface is no longer accessible, usually
233b38e4f2SMauro Carvalho Chehab	because its device has been (or is being) disconnected or the
243b38e4f2SMauro Carvalho Chehab	driver module is being unloaded.
253b38e4f2SMauro Carvalho Chehab
263b38e4f2SMauro Carvalho Chehab2. Odd backdoor through usbfs:
273b38e4f2SMauro Carvalho Chehab
283b38e4f2SMauro Carvalho Chehab - @ioctl:
293b38e4f2SMauro Carvalho Chehab	Used for drivers that want to talk to userspace through
303b38e4f2SMauro Carvalho Chehab	the "usbfs" filesystem.  This lets devices provide ways to
313b38e4f2SMauro Carvalho Chehab	expose information to user space regardless of where they
323b38e4f2SMauro Carvalho Chehab	do (or don't) show up otherwise in the filesystem.
333b38e4f2SMauro Carvalho Chehab
343b38e4f2SMauro Carvalho Chehab3. Power management (PM) callbacks:
353b38e4f2SMauro Carvalho Chehab
363b38e4f2SMauro Carvalho Chehab - @suspend:
373b38e4f2SMauro Carvalho Chehab	Called when the device is going to be suspended.
383b38e4f2SMauro Carvalho Chehab
393b38e4f2SMauro Carvalho Chehab - @resume:
403b38e4f2SMauro Carvalho Chehab	Called when the device is being resumed.
413b38e4f2SMauro Carvalho Chehab
423b38e4f2SMauro Carvalho Chehab - @reset_resume:
433b38e4f2SMauro Carvalho Chehab	Called when the suspended device has been reset instead
443b38e4f2SMauro Carvalho Chehab	of being resumed.
453b38e4f2SMauro Carvalho Chehab
463b38e4f2SMauro Carvalho Chehab4. Device level operations:
473b38e4f2SMauro Carvalho Chehab
483b38e4f2SMauro Carvalho Chehab - @pre_reset:
493b38e4f2SMauro Carvalho Chehab	Called when the device is about to be reset.
503b38e4f2SMauro Carvalho Chehab
513b38e4f2SMauro Carvalho Chehab - @post_reset:
523b38e4f2SMauro Carvalho Chehab	Called after the device has been reset
533b38e4f2SMauro Carvalho Chehab
543b38e4f2SMauro Carvalho ChehabThe ioctl interface (2) should be used only if you have a very good
553b38e4f2SMauro Carvalho Chehabreason. Sysfs is preferred these days. The PM callbacks are covered
56e1c3e6e1SMauro Carvalho Chehabseparately in :ref:`usb-power-management`.
573b38e4f2SMauro Carvalho Chehab
583b38e4f2SMauro Carvalho ChehabCalling conventions
593b38e4f2SMauro Carvalho Chehab===================
603b38e4f2SMauro Carvalho Chehab
613b38e4f2SMauro Carvalho ChehabAll callbacks are mutually exclusive. There's no need for locking
623b38e4f2SMauro Carvalho Chehabagainst other USB callbacks. All callbacks are called from a task
633b38e4f2SMauro Carvalho Chehabcontext. You may sleep. However, it is important that all sleeps have a
643b38e4f2SMauro Carvalho Chehabsmall fixed upper limit in time. In particular you must not call out to
653b38e4f2SMauro Carvalho Chehabuser space and await results.
663b38e4f2SMauro Carvalho Chehab
673b38e4f2SMauro Carvalho ChehabHotplugging callbacks
683b38e4f2SMauro Carvalho Chehab=====================
693b38e4f2SMauro Carvalho Chehab
703b38e4f2SMauro Carvalho ChehabThese callbacks are intended to associate and disassociate a driver with
713b38e4f2SMauro Carvalho Chehaban interface. A driver's bond to an interface is exclusive.
723b38e4f2SMauro Carvalho Chehab
733b38e4f2SMauro Carvalho ChehabThe probe() callback
743b38e4f2SMauro Carvalho Chehab--------------------
753b38e4f2SMauro Carvalho Chehab
763b38e4f2SMauro Carvalho Chehab::
773b38e4f2SMauro Carvalho Chehab
783b38e4f2SMauro Carvalho Chehab  int (*probe) (struct usb_interface *intf,
793b38e4f2SMauro Carvalho Chehab		const struct usb_device_id *id);
803b38e4f2SMauro Carvalho Chehab
813b38e4f2SMauro Carvalho ChehabAccept or decline an interface. If you accept the device return 0,
823b38e4f2SMauro Carvalho Chehabotherwise -ENODEV or -ENXIO. Other error codes should be used only if a
833b38e4f2SMauro Carvalho Chehabgenuine error occurred during initialisation which prevented a driver
843b38e4f2SMauro Carvalho Chehabfrom accepting a device that would else have been accepted.
853b38e4f2SMauro Carvalho ChehabYou are strongly encouraged to use usbcore's facility,
863b38e4f2SMauro Carvalho Chehabusb_set_intfdata(), to associate a data structure with an interface, so
873b38e4f2SMauro Carvalho Chehabthat you know which internal state and identity you associate with a
883b38e4f2SMauro Carvalho Chehabparticular interface. The device will not be suspended and you may do IO
893b38e4f2SMauro Carvalho Chehabto the interface you are called for and endpoint 0 of the device. Device
903b38e4f2SMauro Carvalho Chehabinitialisation that doesn't take too long is a good idea here.
913b38e4f2SMauro Carvalho Chehab
923b38e4f2SMauro Carvalho ChehabThe disconnect() callback
933b38e4f2SMauro Carvalho Chehab-------------------------
943b38e4f2SMauro Carvalho Chehab
953b38e4f2SMauro Carvalho Chehab::
963b38e4f2SMauro Carvalho Chehab
973b38e4f2SMauro Carvalho Chehab  void (*disconnect) (struct usb_interface *intf);
983b38e4f2SMauro Carvalho Chehab
993b38e4f2SMauro Carvalho ChehabThis callback is a signal to break any connection with an interface.
1003b38e4f2SMauro Carvalho ChehabYou are not allowed any IO to a device after returning from this
1013b38e4f2SMauro Carvalho Chehabcallback. You also may not do any other operation that may interfere
102*9f2a3933SMichal Peciowith another driver bound to the interface, eg. a power management
103*9f2a3933SMichal Peciooperation. Outstanding operations on the device must be completed or
104*9f2a3933SMichal Pecioaborted before this callback may return.
105*9f2a3933SMichal Pecio
1063b38e4f2SMauro Carvalho ChehabIf you are called due to a physical disconnection, all your URBs will be
1073b38e4f2SMauro Carvalho Chehabkilled by usbcore. Note that in this case disconnect will be called some
1083b38e4f2SMauro Carvalho Chehabtime after the physical disconnection. Thus your driver must be prepared
1093b38e4f2SMauro Carvalho Chehabto deal with failing IO even prior to the callback.
1103b38e4f2SMauro Carvalho Chehab
1113b38e4f2SMauro Carvalho ChehabDevice level callbacks
1123b38e4f2SMauro Carvalho Chehab======================
1133b38e4f2SMauro Carvalho Chehab
1143b38e4f2SMauro Carvalho Chehabpre_reset
1153b38e4f2SMauro Carvalho Chehab---------
1163b38e4f2SMauro Carvalho Chehab
1173b38e4f2SMauro Carvalho Chehab::
1183b38e4f2SMauro Carvalho Chehab
1193b38e4f2SMauro Carvalho Chehab  int (*pre_reset)(struct usb_interface *intf);
1203b38e4f2SMauro Carvalho Chehab
1213b38e4f2SMauro Carvalho ChehabA driver or user space is triggering a reset on the device which
1223b38e4f2SMauro Carvalho Chehabcontains the interface passed as an argument. Cease IO, wait for all
1233b38e4f2SMauro Carvalho Chehaboutstanding URBs to complete, and save any device state you need to
1243b38e4f2SMauro Carvalho Chehabrestore.  No more URBs may be submitted until the post_reset method
1253b38e4f2SMauro Carvalho Chehabis called.
1263b38e4f2SMauro Carvalho Chehab
1273b38e4f2SMauro Carvalho ChehabIf you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
1283b38e4f2SMauro Carvalho Chehabare in atomic context.
1293b38e4f2SMauro Carvalho Chehab
1303b38e4f2SMauro Carvalho Chehabpost_reset
1313b38e4f2SMauro Carvalho Chehab----------
1323b38e4f2SMauro Carvalho Chehab
1333b38e4f2SMauro Carvalho Chehab::
1343b38e4f2SMauro Carvalho Chehab
1353b38e4f2SMauro Carvalho Chehab  int (*post_reset)(struct usb_interface *intf);
1363b38e4f2SMauro Carvalho Chehab
1373b38e4f2SMauro Carvalho ChehabThe reset has completed.  Restore any saved device state and begin
1383b38e4f2SMauro Carvalho Chehabusing the device again.
1393b38e4f2SMauro Carvalho Chehab
1403b38e4f2SMauro Carvalho ChehabIf you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
1413b38e4f2SMauro Carvalho Chehabare in atomic context.
1423b38e4f2SMauro Carvalho Chehab
1433b38e4f2SMauro Carvalho ChehabCall sequences
1443b38e4f2SMauro Carvalho Chehab==============
1453b38e4f2SMauro Carvalho Chehab
1463b38e4f2SMauro Carvalho ChehabNo callbacks other than probe will be invoked for an interface
1473b38e4f2SMauro Carvalho Chehabthat isn't bound to your driver.
1483b38e4f2SMauro Carvalho Chehab
1493b38e4f2SMauro Carvalho ChehabProbe will never be called for an interface bound to a driver.
1503b38e4f2SMauro Carvalho ChehabHence following a successful probe, disconnect will be called
1513b38e4f2SMauro Carvalho Chehabbefore there is another probe for the same interface.
1523b38e4f2SMauro Carvalho Chehab
1533b38e4f2SMauro Carvalho ChehabOnce your driver is bound to an interface, disconnect can be
1543b38e4f2SMauro Carvalho Chehabcalled at any time except in between pre_reset and post_reset.
1553b38e4f2SMauro Carvalho Chehabpre_reset is always followed by post_reset, even if the reset
1563b38e4f2SMauro Carvalho Chehabfailed or the device has been unplugged.
1573b38e4f2SMauro Carvalho Chehab
1583b38e4f2SMauro Carvalho Chehabsuspend is always followed by one of: resume, reset_resume, or
1593b38e4f2SMauro Carvalho Chehabdisconnect.
160