callbacks.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. USB core callbacks
  2. ~~~~~~~~~~~~~~~~~~
  3. What callbacks will usbcore do?
  4. ===============================
  5. Usbcore will call into a driver through callbacks defined in the driver
  6. structure and through the completion handler of URBs a driver submits.
  7. Only the former are in the scope of this document. These two kinds of
  8. callbacks are completely independent of each other. Information on the
  9. completion callback can be found in :ref:`usb-urb`.
  10. The callbacks defined in the driver structure are:
  11. 1. Hotplugging callbacks:
  12. - @probe:
  13. Called to see if the driver is willing to manage a particular
  14. interface on a device.
  15. - @disconnect:
  16. Called when the interface is no longer accessible, usually
  17. because its device has been (or is being) disconnected or the
  18. driver module is being unloaded.
  19. 2. Odd backdoor through usbfs:
  20. - @ioctl:
  21. Used for drivers that want to talk to userspace through
  22. the "usbfs" filesystem. This lets devices provide ways to
  23. expose information to user space regardless of where they
  24. do (or don't) show up otherwise in the filesystem.
  25. 3. Power management (PM) callbacks:
  26. - @suspend:
  27. Called when the device is going to be suspended.
  28. - @resume:
  29. Called when the device is being resumed.
  30. - @reset_resume:
  31. Called when the suspended device has been reset instead
  32. of being resumed.
  33. 4. Device level operations:
  34. - @pre_reset:
  35. Called when the device is about to be reset.
  36. - @post_reset:
  37. Called after the device has been reset
  38. The ioctl interface (2) should be used only if you have a very good
  39. reason. Sysfs is preferred these days. The PM callbacks are covered
  40. separately in :ref:`usb-power-management`.
  41. Calling conventions
  42. ===================
  43. All callbacks are mutually exclusive. There's no need for locking
  44. against other USB callbacks. All callbacks are called from a task
  45. context. You may sleep. However, it is important that all sleeps have a
  46. small fixed upper limit in time. In particular you must not call out to
  47. user space and await results.
  48. Hotplugging callbacks
  49. =====================
  50. These callbacks are intended to associate and disassociate a driver with
  51. an interface. A driver's bond to an interface is exclusive.
  52. The probe() callback
  53. --------------------
  54. ::
  55. int (*probe) (struct usb_interface *intf,
  56. const struct usb_device_id *id);
  57. Accept or decline an interface. If you accept the device return 0,
  58. otherwise -ENODEV or -ENXIO. Other error codes should be used only if a
  59. genuine error occurred during initialisation which prevented a driver
  60. from accepting a device that would else have been accepted.
  61. You are strongly encouraged to use usbcore's facility,
  62. usb_set_intfdata(), to associate a data structure with an interface, so
  63. that you know which internal state and identity you associate with a
  64. particular interface. The device will not be suspended and you may do IO
  65. to the interface you are called for and endpoint 0 of the device. Device
  66. initialisation that doesn't take too long is a good idea here.
  67. The disconnect() callback
  68. -------------------------
  69. ::
  70. void (*disconnect) (struct usb_interface *intf);
  71. This callback is a signal to break any connection with an interface.
  72. You are not allowed any IO to a device after returning from this
  73. callback. You also may not do any other operation that may interfere
  74. with another driver bound the interface, eg. a power management
  75. operation.
  76. If you are called due to a physical disconnection, all your URBs will be
  77. killed by usbcore. Note that in this case disconnect will be called some
  78. time after the physical disconnection. Thus your driver must be prepared
  79. to deal with failing IO even prior to the callback.
  80. Device level callbacks
  81. ======================
  82. pre_reset
  83. ---------
  84. ::
  85. int (*pre_reset)(struct usb_interface *intf);
  86. A driver or user space is triggering a reset on the device which
  87. contains the interface passed as an argument. Cease IO, wait for all
  88. outstanding URBs to complete, and save any device state you need to
  89. restore. No more URBs may be submitted until the post_reset method
  90. is called.
  91. If you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
  92. are in atomic context.
  93. post_reset
  94. ----------
  95. ::
  96. int (*post_reset)(struct usb_interface *intf);
  97. The reset has completed. Restore any saved device state and begin
  98. using the device again.
  99. If you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if you
  100. are in atomic context.
  101. Call sequences
  102. ==============
  103. No callbacks other than probe will be invoked for an interface
  104. that isn't bound to your driver.
  105. Probe will never be called for an interface bound to a driver.
  106. Hence following a successful probe, disconnect will be called
  107. before there is another probe for the same interface.
  108. Once your driver is bound to an interface, disconnect can be
  109. called at any time except in between pre_reset and post_reset.
  110. pre_reset is always followed by post_reset, even if the reset
  111. failed or the device has been unplugged.
  112. suspend is always followed by one of: resume, reset_resume, or
  113. disconnect.