cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

device.rst (3658B)


      1==========================
      2The Basic Device Structure
      3==========================
      4
      5See the kerneldoc for the struct device.
      6
      7
      8Programming Interface
      9~~~~~~~~~~~~~~~~~~~~~
     10The bus driver that discovers the device uses this to register the
     11device with the core::
     12
     13  int device_register(struct device * dev);
     14
     15The bus should initialize the following fields:
     16
     17    - parent
     18    - name
     19    - bus_id
     20    - bus
     21
     22A device is removed from the core when its reference count goes to
     230. The reference count can be adjusted using::
     24
     25  struct device * get_device(struct device * dev);
     26  void put_device(struct device * dev);
     27
     28get_device() will return a pointer to the struct device passed to it
     29if the reference is not already 0 (if it's in the process of being
     30removed already).
     31
     32A driver can access the lock in the device structure using::
     33
     34  void lock_device(struct device * dev);
     35  void unlock_device(struct device * dev);
     36
     37
     38Attributes
     39~~~~~~~~~~
     40
     41::
     42
     43  struct device_attribute {
     44	struct attribute	attr;
     45	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
     46			char *buf);
     47	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
     48			 const char *buf, size_t count);
     49  };
     50
     51Attributes of devices can be exported by a device driver through sysfs.
     52
     53Please see Documentation/filesystems/sysfs.rst for more information
     54on how sysfs works.
     55
     56As explained in Documentation/core-api/kobject.rst, device attributes must be
     57created before the KOBJ_ADD uevent is generated. The only way to realize
     58that is by defining an attribute group.
     59
     60Attributes are declared using a macro called DEVICE_ATTR::
     61
     62  #define DEVICE_ATTR(name,mode,show,store)
     63
     64Example:::
     65
     66  static DEVICE_ATTR(type, 0444, type_show, NULL);
     67  static DEVICE_ATTR(power, 0644, power_show, power_store);
     68
     69Helper macros are available for common values of mode, so the above examples
     70can be simplified to:::
     71
     72  static DEVICE_ATTR_RO(type);
     73  static DEVICE_ATTR_RW(power);
     74
     75This declares two structures of type struct device_attribute with respective
     76names 'dev_attr_type' and 'dev_attr_power'. These two attributes can be
     77organized as follows into a group::
     78
     79  static struct attribute *dev_attrs[] = {
     80	&dev_attr_type.attr,
     81	&dev_attr_power.attr,
     82	NULL,
     83  };
     84
     85  static struct attribute_group dev_group = {
     86	.attrs = dev_attrs,
     87  };
     88
     89  static const struct attribute_group *dev_groups[] = {
     90	&dev_group,
     91	NULL,
     92  };
     93
     94A helper macro is available for the common case of a single group, so the
     95above two structures can be declared using:::
     96
     97  ATTRIBUTE_GROUPS(dev);
     98
     99This array of groups can then be associated with a device by setting the
    100group pointer in struct device before device_register() is invoked::
    101
    102        dev->groups = dev_groups;
    103        device_register(dev);
    104
    105The device_register() function will use the 'groups' pointer to create the
    106device attributes and the device_unregister() function will use this pointer
    107to remove the device attributes.
    108
    109Word of warning:  While the kernel allows device_create_file() and
    110device_remove_file() to be called on a device at any time, userspace has
    111strict expectations on when attributes get created.  When a new device is
    112registered in the kernel, a uevent is generated to notify userspace (like
    113udev) that a new device is available.  If attributes are added after the
    114device is registered, then userspace won't get notified and userspace will
    115not know about the new attributes.
    116
    117This is important for device driver that need to publish additional
    118attributes for a device at driver probe time.  If the device driver simply
    119calls device_create_file() on the device structure passed to it, then
    120userspace will never be notified of the new attributes.