device.txt 3.2 KB

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