bus.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. =========
  2. Bus Types
  3. =========
  4. Definition
  5. ~~~~~~~~~~
  6. See the kerneldoc for the struct bus_type.
  7. int bus_register(struct bus_type * bus);
  8. Declaration
  9. ~~~~~~~~~~~
  10. Each bus type in the kernel (PCI, USB, etc) should declare one static
  11. object of this type. They must initialize the name field, and may
  12. optionally initialize the match callback::
  13. struct bus_type pci_bus_type = {
  14. .name = "pci",
  15. .match = pci_bus_match,
  16. };
  17. The structure should be exported to drivers in a header file:
  18. extern struct bus_type pci_bus_type;
  19. Registration
  20. ~~~~~~~~~~~~
  21. When a bus driver is initialized, it calls bus_register. This
  22. initializes the rest of the fields in the bus object and inserts it
  23. into a global list of bus types. Once the bus object is registered,
  24. the fields in it are usable by the bus driver.
  25. Callbacks
  26. ~~~~~~~~~
  27. match(): Attaching Drivers to Devices
  28. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  29. The format of device ID structures and the semantics for comparing
  30. them are inherently bus-specific. Drivers typically declare an array
  31. of device IDs of devices they support that reside in a bus-specific
  32. driver structure.
  33. The purpose of the match callback is to give the bus an opportunity to
  34. determine if a particular driver supports a particular device by
  35. comparing the device IDs the driver supports with the device ID of a
  36. particular device, without sacrificing bus-specific functionality or
  37. type-safety.
  38. When a driver is registered with the bus, the bus's list of devices is
  39. iterated over, and the match callback is called for each device that
  40. does not have a driver associated with it.
  41. Device and Driver Lists
  42. ~~~~~~~~~~~~~~~~~~~~~~~
  43. The lists of devices and drivers are intended to replace the local
  44. lists that many buses keep. They are lists of struct devices and
  45. struct device_drivers, respectively. Bus drivers are free to use the
  46. lists as they please, but conversion to the bus-specific type may be
  47. necessary.
  48. The LDM core provides helper functions for iterating over each list::
  49. int bus_for_each_dev(struct bus_type * bus, struct device * start,
  50. void * data,
  51. int (*fn)(struct device *, void *));
  52. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  53. void * data, int (*fn)(struct device_driver *, void *));
  54. These helpers iterate over the respective list, and call the callback
  55. for each device or driver in the list. All list accesses are
  56. synchronized by taking the bus's lock (read currently). The reference
  57. count on each object in the list is incremented before the callback is
  58. called; it is decremented after the next object has been obtained. The
  59. lock is not held when calling the callback.
  60. sysfs
  61. ~~~~~~~~
  62. There is a top-level directory named 'bus'.
  63. Each bus gets a directory in the bus directory, along with two default
  64. directories::
  65. /sys/bus/pci/
  66. |-- devices
  67. `-- drivers
  68. Drivers registered with the bus get a directory in the bus's drivers
  69. directory::
  70. /sys/bus/pci/
  71. |-- devices
  72. `-- drivers
  73. |-- Intel ICH
  74. |-- Intel ICH Joystick
  75. |-- agpgart
  76. `-- e100
  77. Each device that is discovered on a bus of that type gets a symlink in
  78. the bus's devices directory to the device's directory in the physical
  79. hierarchy::
  80. /sys/bus/pci/
  81. |-- devices
  82. | |-- 00:00.0 -> ../../../root/pci0/00:00.0
  83. | |-- 00:01.0 -> ../../../root/pci0/00:01.0
  84. | `-- 00:02.0 -> ../../../root/pci0/00:02.0
  85. `-- drivers
  86. Exporting Attributes
  87. ~~~~~~~~~~~~~~~~~~~~
  88. ::
  89. struct bus_attribute {
  90. struct attribute attr;
  91. ssize_t (*show)(struct bus_type *, char * buf);
  92. ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
  93. };
  94. Bus drivers can export attributes using the BUS_ATTR_RW macro that works
  95. similarly to the DEVICE_ATTR_RW macro for devices. For example, a
  96. definition like this::
  97. static BUS_ATTR_RW(debug);
  98. is equivalent to declaring::
  99. static bus_attribute bus_attr_debug;
  100. This can then be used to add and remove the attribute from the bus's
  101. sysfs directory using::
  102. int bus_create_file(struct bus_type *, struct bus_attribute *);
  103. void bus_remove_file(struct bus_type *, struct bus_attribute *);