Back to home page

OSCL-LXR

 
 

    


0001 ==========================
0002 The Basic Device Structure
0003 ==========================
0004 
0005 See the kerneldoc for the struct device.
0006 
0007 
0008 Programming Interface
0009 ~~~~~~~~~~~~~~~~~~~~~
0010 The bus driver that discovers the device uses this to register the
0011 device with the core::
0012 
0013   int device_register(struct device * dev);
0014 
0015 The bus should initialize the following fields:
0016 
0017     - parent
0018     - name
0019     - bus_id
0020     - bus
0021 
0022 A device is removed from the core when its reference count goes to
0023 0. The reference count can be adjusted using::
0024 
0025   struct device * get_device(struct device * dev);
0026   void put_device(struct device * dev);
0027 
0028 get_device() will return a pointer to the struct device passed to it
0029 if the reference is not already 0 (if it's in the process of being
0030 removed already).
0031 
0032 A driver can access the lock in the device structure using::
0033 
0034   void lock_device(struct device * dev);
0035   void unlock_device(struct device * dev);
0036 
0037 
0038 Attributes
0039 ~~~~~~~~~~
0040 
0041 ::
0042 
0043   struct device_attribute {
0044         struct attribute        attr;
0045         ssize_t (*show)(struct device *dev, struct device_attribute *attr,
0046                         char *buf);
0047         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
0048                          const char *buf, size_t count);
0049   };
0050 
0051 Attributes of devices can be exported by a device driver through sysfs.
0052 
0053 Please see Documentation/filesystems/sysfs.rst for more information
0054 on how sysfs works.
0055 
0056 As explained in Documentation/core-api/kobject.rst, device attributes must be
0057 created before the KOBJ_ADD uevent is generated. The only way to realize
0058 that is by defining an attribute group.
0059 
0060 Attributes are declared using a macro called DEVICE_ATTR::
0061 
0062   #define DEVICE_ATTR(name,mode,show,store)
0063 
0064 Example:::
0065 
0066   static DEVICE_ATTR(type, 0444, type_show, NULL);
0067   static DEVICE_ATTR(power, 0644, power_show, power_store);
0068 
0069 Helper macros are available for common values of mode, so the above examples
0070 can be simplified to:::
0071 
0072   static DEVICE_ATTR_RO(type);
0073   static DEVICE_ATTR_RW(power);
0074 
0075 This declares two structures of type struct device_attribute with respective
0076 names 'dev_attr_type' and 'dev_attr_power'. These two attributes can be
0077 organized as follows into a group::
0078 
0079   static struct attribute *dev_attrs[] = {
0080         &dev_attr_type.attr,
0081         &dev_attr_power.attr,
0082         NULL,
0083   };
0084 
0085   static struct attribute_group dev_group = {
0086         .attrs = dev_attrs,
0087   };
0088 
0089   static const struct attribute_group *dev_groups[] = {
0090         &dev_group,
0091         NULL,
0092   };
0093 
0094 A helper macro is available for the common case of a single group, so the
0095 above two structures can be declared using:::
0096 
0097   ATTRIBUTE_GROUPS(dev);
0098 
0099 This array of groups can then be associated with a device by setting the
0100 group pointer in struct device before device_register() is invoked::
0101 
0102         dev->groups = dev_groups;
0103         device_register(dev);
0104 
0105 The device_register() function will use the 'groups' pointer to create the
0106 device attributes and the device_unregister() function will use this pointer
0107 to remove the device attributes.
0108 
0109 Word of warning:  While the kernel allows device_create_file() and
0110 device_remove_file() to be called on a device at any time, userspace has
0111 strict expectations on when attributes get created.  When a new device is
0112 registered in the kernel, a uevent is generated to notify userspace (like
0113 udev) that a new device is available.  If attributes are added after the
0114 device is registered, then userspace won't get notified and userspace will
0115 not know about the new attributes.
0116 
0117 This is important for device driver that need to publish additional
0118 attributes for a device at driver probe time.  If the device driver simply
0119 calls device_create_file() on the device structure passed to it, then
0120 userspace will never be notified of the new attributes.