URL
https://opencores.org/ocsvn/test_project/test_project/trunk
Subversion Repositories test_project
[/] [test_project/] [trunk/] [linux_sd_driver/] [Documentation/] [sysfs-rules.txt] - Rev 62
Compare with Previous | Blame | View Log
Rules on how to access information in the Linux kernel sysfsThe kernel-exported sysfs exports internal kernel implementation detailsand depends on internal kernel structures and layout. It is agreed uponby the kernel developers that the Linux kernel does not provide a stableinternal API. As sysfs is a direct export of kernel internalstructures, the sysfs interface cannot provide a stable interface either;it may always change along with internal kernel changes.To minimize the risk of breaking users of sysfs, which are in most caseslow-level userspace applications, with a new kernel release, the usersof sysfs must follow some rules to use an as-abstract-as-possible way toaccess this filesystem. The current udev and HAL programs alreadyimplement this and users are encouraged to plug, if possible, into theabstractions these programs provide instead of accessing sysfs directly.But if you really do want or need to access sysfs directly, please followthe following rules and then your programs should work with futureversions of the sysfs interface.- Do not use libsysfsIt makes assumptions about sysfs which are not true. Its API does notoffer any abstraction, it exposes all the kernel driver-coreimplementation details in its own API. Therefore it is not better thanreading directories and opening the files yourself.Also, it is not actively maintained, in the sense of reflecting thecurrent kernel development. The goal of providing a stable interfaceto sysfs has failed; it causes more problems than it solves. Itviolates many of the rules in this document.- sysfs is always at /sysParsing /proc/mounts is a waste of time. Other mount points are asystem configuration bug you should not try to solve. For test cases,possibly support a SYSFS_PATH environment variable to overwrite theapplication's behavior, but never try to search for sysfs. Never tryto mount it, if you are not an early boot script.- devices are only "devices"There is no such thing like class-, bus-, physical devices,interfaces, and such that you can rely on in userspace. Everything isjust simply a "device". Class-, bus-, physical, ... types are justkernel implementation details which should not be expected byapplications that look for devices in sysfs.The properties of a device are:o devpath (/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0)- identical to the DEVPATH value in the event sent from the kernelat device creation and removal- the unique key to the device at that point in time- the kernel's path to the device directory without the leading/sys, and always starting with with a slash- all elements of a devpath must be real directories. Symlinkspointing to /sys/devices must always be resolved to their realtarget and the target path must be used to access the device.That way the devpath to the device matches the devpath of thekernel used at event time.- using or exposing symlink values as elements in a devpath stringis a bug in the applicationo kernel name (sda, tty, 0000:00:1f.2, ...)- a directory name, identical to the last element of the devpath- applications need to handle spaces and characters like '!' inthe nameo subsystem (block, tty, pci, ...)- simple string, never a path or a link- retrieved by reading the "subsystem"-link and using only thelast element of the target patho driver (tg3, ata_piix, uhci_hcd)- a simple string, which may contain spaces, never a path or alink- it is retrieved by reading the "driver"-link and using only thelast element of the target path- devices which do not have "driver"-link just do not have adriver; copying the driver value in a child device context is abug in the applicationo attributes- the files in the device directory or files below subdirectoriesof the same device directory- accessing attributes reached by a symlink pointing to another device,like the "device"-link, is a bug in the applicationEverything else is just a kernel driver-core implementation detailthat should not be assumed to be stable across kernel releases.- Properties of parent devices never belong into a child device.Always look at the parent devices themselves for determining devicecontext properties. If the device 'eth0' or 'sda' does not have a"driver"-link, then this device does not have a driver. Its value is empty.Never copy any property of the parent-device into a child-device. Parentdevice properties may change dynamically without any notice to thechild device.- Hierarchy in a single device treeThere is only one valid place in sysfs where hierarchy can be examinedand this is below: /sys/devices.It is planned that all device directories will end up in the treebelow this directory.- Classification by subsystemThere are currently three places for classification of devices:/sys/block, /sys/class and /sys/bus. It is planned that these willnot contain any device directories themselves, but only flat lists ofsymlinks pointing to the unified /sys/devices tree.All three places have completely different rules on how to accessdevice information. It is planned to merge all threeclassification directories into one place at /sys/subsystem,following the layout of the bus directories. All buses andclasses, including the converted block subsystem, will show upthere.The devices belonging to a subsystem will create a symlink in the"devices" directory at /sys/subsystem/<name>/devices.If /sys/subsystem exists, /sys/bus, /sys/class and /sys/block can beignored. If it does not exist, you have always to scan all threeplaces, as the kernel is free to move a subsystem from one place tothe other, as long as the devices are still reachable by the samesubsystem name.Assuming /sys/class/<subsystem> and /sys/bus/<subsystem>, or/sys/block and /sys/class/block are not interchangeable is a bug inthe application.- BlockThe converted block subsystem at /sys/class/block or/sys/subsystem/block will contain the links for disks and partitionsat the same level, never in a hierarchy. Assuming the block subsytem tocontain only disks and not partition devices in the same flat list isa bug in the application.- "device"-link and <subsystem>:<kernel name>-linksNever depend on the "device"-link. The "device"-link is a workaroundfor the old layout, where class devices are not created in/sys/devices/ like the bus devices. If the link-resolving of adevice directory does not end in /sys/devices/, you can use the"device"-link to find the parent devices in /sys/devices/. That is thesingle valid use of the "device"-link; it must never appear in anypath as an element. Assuming the existence of the "device"-link fora device in /sys/devices/ is a bug in the application.Accessing /sys/class/net/eth0/device is a bug in the application.Never depend on the class-specific links back to the /sys/classdirectory. These links are also a workaround for the design mistakethat class devices are not created in /sys/devices. If a devicedirectory does not contain directories for child devices, these linksmay be used to find the child devices in /sys/class. That is the singlevalid use of these links; they must never appear in any path as anelement. Assuming the existence of these links for devices which arereal child device directories in the /sys/devices tree is a bug inthe application.It is planned to remove all these links when all class devicedirectories live in /sys/devices.- Position of devices along device chain can change.Never depend on a specific parent device position in the devpath,or the chain of parent devices. The kernel is free to insert devices intothe chain. You must always request the parent device you are looking forby its subsystem value. You need to walk up the chain until you findthe device that matches the expected subsystem. Depending on a specificposition of a parent device or exposing relative paths using "../" toaccess the chain of parents is a bug in the application.
