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