device.rst 3.6 KB

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