usb.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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) it sleeps, which
  22. * drops the BKL so the WM and other processes keep running through port
  23. * resets and settle times; otherwise it busy-waits as before. */
  24. void usb_delay_ms(unsigned int ms);
  25. /* 1 while another context's scan owns the controllers (it is sleeping in
  26. * usb_delay_ms). The transfer wrappers then refuse access; pollers such as
  27. * usbhid skip their tick. */
  28. int usb_scan_foreign(void);
  29. /* Cheap periodic watcher: enumerates devices on root ports whose connect
  30. state changed since the last call (internally throttled). Returns the
  31. number of ports with a new device. */
  32. int usb_hotplug_poll(usb_probe_fn probe);
  33. /* Incremented by every scan; consumers re-init when it changes. */
  34. unsigned int usb_generation(void);
  35. /* Root-port query (used to watch the port the mouse hangs off). */
  36. int usb_port_connected(unsigned int ctrl, unsigned int port);
  37. /* Control request helpers. rt is the raw bmRequestType (standard 0x80/0x00,
  38. class 0xA0/0x20, other 0xA3/0x23); the rest is the raw setup fields. */
  39. int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
  40. int low_speed, unsigned char rt, unsigned char req,
  41. unsigned short value, unsigned short index,
  42. unsigned char *buf, unsigned int len);
  43. int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
  44. int low_speed, unsigned char rt, unsigned char req,
  45. unsigned short value, unsigned short index);
  46. int usb_control_out_data(unsigned int ctrl, unsigned int addr,
  47. unsigned int mps, int low_speed, unsigned char rt,
  48. unsigned char req, unsigned short value,
  49. unsigned short index, const unsigned char *buf,
  50. unsigned int len);
  51. /* Bulk transfers: bulk_in is start-or-poll (see usb_hcd.h). */
  52. int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
  53. unsigned int mps, const unsigned char *buf,
  54. unsigned int len);
  55. int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
  56. unsigned int mps, unsigned char *buf, unsigned int len);
  57. /* 1 when the controller behind `ctrl` implements both bulk directions.
  58. * UHCI/OHCI do not, so device drivers that need bulk must check this. */
  59. int usb_bulk_supported(unsigned int ctrl);
  60. /* Device-level probe for drivers that do not use HID interfaces (the
  61. * USB Ethernet and mass-storage drivers parse the whole configuration
  62. * themselves). Return 1 when the device was claimed. Several probes may
  63. * register; each is called (in registration order) for every non-hub
  64. * device during a scan, before the per-interface HID probe. */
  65. typedef int (*usb_device_probe_fn)(unsigned int ctrl, unsigned int addr,
  66. unsigned int mps, int low_speed,
  67. unsigned int port, unsigned int vendor,
  68. unsigned int product);
  69. void usb_register_device_probe(usb_device_probe_fn fn);
  70. /* Interrupt-IN endpoints (slots 0..USB_HCD_INT_SLOTS-1; usbhid assigns
  71. one per HID interface it claims). */
  72. int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
  73. unsigned int ep, unsigned int mps, int low_speed);
  74. int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len);
  75. void usb_int_in_stop(unsigned int slot);
  76. #endif