bus.txt 4.4 KB

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