bus.txt 4.0 KB

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