usb.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* USB core: device enumeration, descriptor parsing, hub recursion, bus
  2. scans and the hotplug watcher. Controller-specific work (transfers, root
  3. ports) goes through the usb_hcd_ops vtable (uhci.c / ohci.c / ehci.c /
  4. xhci.c). */
  5. #ifndef USB_H
  6. #define USB_H
  7. #include "usb_hcd.h"
  8. /* `lsusb` command: full scan, print the tree. */
  9. void usb_enumerate_and_print(void);
  10. /* Find + bring up every host controller; 0 when at least one is available. */
  11. int usb_hcd_init(void);
  12. const char *usb_hcd_name(void);
  13. /* Name of the controller type behind one global controller index. */
  14. const char *usb_hcd_name_ctrl(unsigned int ctrl);
  15. /* Full bus scan: resets every connected root port, re-enumerates and calls
  16. the probe for each interface (probe may be NULL to print instead).
  17. Returns 0, -1 (no controller) or -2 (a scan is already running); every
  18. completed scan bumps the generation counter. */
  19. int usb_scan(usb_probe_fn probe);
  20. /* Delay used by the USB core and every HCD. Inside a bus scan owned by a
  21. * sleepable process (lsusb, the kworker's HID rescan), or a command the
  22. * storage rescan issues with the bus claimed, it sleeps, which drops the
  23. * BKL so the WM and other processes keep running through port resets and
  24. * settle times; otherwise it busy-waits as before. */
  25. void usb_delay_ms(unsigned int ms);
  26. /* Call from every HCD completion-poll loop (outside its spinlock) with the
  27. * loop's start time: once the wait has run 2 ms, a sleepable bus owner
  28. * yields (dropping the BKL for any waiter) instead of spinning with it
  29. * held. A no-op otherwise. */
  30. void usb_poll_relax(unsigned int t0);
  31. /* Claim the controllers for the calling process (nests). Returns -1 when
  32. * another context owns them. Every USB transaction that may sleep in a
  33. * wait loop must run inside a claim, so no other context can touch the
  34. * controller while the owner sleeps. */
  35. int usb_bus_claim(void);
  36. void usb_bus_release(void);
  37. /* 1 while another context owns the controllers (a scan, or a claimed
  38. * mass-storage command, possibly sleeping). The transfer wrappers then
  39. * refuse access; pollers such as usbhid skip their tick. */
  40. int usb_scan_foreign(void);
  41. /* Cheap periodic watcher: enumerates devices on root ports whose connect
  42. state changed since the last call (internally throttled). Returns the
  43. number of ports with a new device. */
  44. int usb_hotplug_poll(usb_probe_fn probe);
  45. /* Incremented by every scan; consumers re-init when it changes. */
  46. unsigned int usb_generation(void);
  47. /* Root-port query (used to watch the port the mouse hangs off). */
  48. int usb_port_connected(unsigned int ctrl, unsigned int port);
  49. /* Control request helpers. rt is the raw bmRequestType (standard 0x80/0x00,
  50. class 0xA0/0x20, other 0xA3/0x23); the rest is the raw setup fields. */
  51. int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
  52. int low_speed, unsigned char rt, unsigned char req,
  53. unsigned short value, unsigned short index,
  54. unsigned char *buf, unsigned int len);
  55. int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
  56. int low_speed, unsigned char rt, unsigned char req,
  57. unsigned short value, unsigned short index);
  58. int usb_control_out_data(unsigned int ctrl, unsigned int addr,
  59. unsigned int mps, int low_speed, unsigned char rt,
  60. unsigned char req, unsigned short value,
  61. unsigned short index, const unsigned char *buf,
  62. unsigned int len);
  63. /* Bulk transfers: bulk_in is start-or-poll (see usb_hcd.h). */
  64. int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
  65. unsigned int mps, const unsigned char *buf,
  66. unsigned int len);
  67. int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
  68. unsigned int mps, unsigned char *buf, unsigned int len);
  69. /* Cancel a bulk transfer and reset the endpoint's host-side state (toggle,
  70. * armed qTD/ring) after a timeout or a BOT reset. */
  71. void usb_bulk_reset(unsigned int ctrl, unsigned int addr, unsigned int ep,
  72. int dir_in);
  73. /* 1 when the controller behind `ctrl` implements both bulk directions.
  74. * UHCI/OHCI do not, so device drivers that need bulk must check this. */
  75. int usb_bulk_supported(unsigned int ctrl);
  76. /* Device-level probe for drivers that do not use HID interfaces (the
  77. * USB Ethernet and mass-storage drivers parse the whole configuration
  78. * themselves). Return 1 when the device was claimed. Several probes may
  79. * register; each is called (in registration order) for every non-hub
  80. * device during a scan, before the per-interface HID probe. */
  81. typedef int (*usb_device_probe_fn)(unsigned int ctrl, unsigned int addr,
  82. unsigned int mps, int low_speed,
  83. unsigned int port, unsigned int vendor,
  84. unsigned int product);
  85. void usb_register_device_probe(usb_device_probe_fn fn);
  86. /* Interrupt-IN endpoints (slots 0..USB_HCD_INT_SLOTS-1; usbhid assigns
  87. one per HID interface it claims). */
  88. int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
  89. unsigned int ep, unsigned int mps, int low_speed);
  90. int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len);
  91. void usb_int_in_stop(unsigned int slot);
  92. #endif