📄 bus.txt
字号:
Bus Types Definition~~~~~~~~~~struct bus_type { char * name; struct subsystem subsys; struct kset drivers; struct kset devices; struct bus_attribute * bus_attrs; struct device_attribute * dev_attrs; struct driver_attribute * drv_attrs; int (*match)(struct device * dev, struct device_driver * drv); int (*hotplug) (struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size); int (*suspend)(struct device * dev, pm_message_t state); int (*resume)(struct device * dev);};int bus_register(struct bus_type * bus);Declaration~~~~~~~~~~~Each bus type in the kernel (PCI, USB, etc) should declare one staticobject of this type. They must initialize the name field, and mayoptionally initialize the match callback.struct bus_type pci_bus_type = { .name = "pci", .match = pci_bus_match,};The structure should be exported to drivers in a header file:extern struct bus_type pci_bus_type;Registration~~~~~~~~~~~~When a bus driver is initialized, it calls bus_register. Thisinitializes the rest of the fields in the bus object and inserts itinto a global list of bus types. Once the bus object is registered, the fields in it are usable by the bus driver. Callbacks~~~~~~~~~match(): Attaching Drivers to Devices~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The format of device ID structures and the semantics for comparingthem are inherently bus-specific. Drivers typically declare an arrayof device IDs of devices they support that reside in a bus-specificdriver structure. The purpose of the match callback is provide the bus an opportunity todetermine if a particular driver supports a particular device bycomparing the device IDs the driver supports with the device ID of aparticular device, without sacrificing bus-specific functionality ortype-safety. When a driver is registered with the bus, the bus's list of devices isiterated over, and the match callback is called for each device thatdoes not have a driver associated with it. Device and Driver Lists~~~~~~~~~~~~~~~~~~~~~~~The lists of devices and drivers are intended to replace the locallists that many buses keep. They are lists of struct devices andstruct device_drivers, respectively. Bus drivers are free to use thelists as they please, but conversion to the bus-specific type may benecessary. The LDM core provides helper functions for iterating over each list.int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data, int (*fn)(struct device *, void *));int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, void * data, int (*fn)(struct device_driver *, void *));These helpers iterate over the respective list, and call the callbackfor each device or driver in the list. All list accesses aresynchronized by taking the bus's lock (read currently). The referencecount on each object in the list is incremented before the callback iscalled; it is decremented after the next object has been obtained. Thelock is not held when calling the callback. sysfs~~~~~~~~There is a top-level directory named 'bus'.Each bus gets a directory in the bus directory, along with two defaultdirectories: /sys/bus/pci/ |-- devices `-- driversDrivers registered with the bus get a directory in the bus's driversdirectory: /sys/bus/pci/ |-- devices `-- drivers |-- Intel ICH |-- Intel ICH Joystick |-- agpgart `-- e100Each device that is discovered on a bus of that type gets a symlink inthe bus's devices directory to the device's directory in the physicalhierarchy: /sys/bus/pci/ |-- devices | |-- 00:00.0 -> ../../../root/pci0/00:00.0 | |-- 00:01.0 -> ../../../root/pci0/00:01.0 | `-- 00:02.0 -> ../../../root/pci0/00:02.0 `-- driversExporting Attributes~~~~~~~~~~~~~~~~~~~~struct bus_attribute { struct attribute attr; ssize_t (*show)(struct bus_type *, char * buf); ssize_t (*store)(struct bus_type *, const char * buf, size_t count);};Bus drivers can export attributes using the BUS_ATTR macro that workssimilarly to the DEVICE_ATTR macro for devices. For example, a definition like this:static BUS_ATTR(debug,0644,show_debug,store_debug);is equivalent to declaring:static bus_attribute bus_attr_debug;This can then be used to add and remove the attribute from the bus'ssysfs directory using:int bus_create_file(struct bus_type *, struct bus_attribute *);void bus_remove_file(struct bus_type *, struct bus_attribute *);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -