device.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 via drivers using a simple
  34. procfs-like interface.
  35. Please see Documentation/filesystems/sysfs.txt for more information
  36. on how sysfs works.
  37. Attributes are declared using a macro called DEVICE_ATTR:
  38. #define DEVICE_ATTR(name,mode,show,store)
  39. Example:
  40. DEVICE_ATTR(power,0644,show_power,store_power);
  41. This declares a structure of type struct device_attribute named
  42. 'dev_attr_power'. This can then be added and removed to the device's
  43. directory using:
  44. int device_create_file(struct device *device, struct device_attribute * entry);
  45. void device_remove_file(struct device * dev, struct device_attribute * attr);
  46. Example:
  47. device_create_file(dev,&dev_attr_power);
  48. device_remove_file(dev,&dev_attr_power);
  49. The file name will be 'power' with a mode of 0644 (-rw-r--r--).
  50. Word of warning: While the kernel allows device_create_file() and
  51. device_remove_file() to be called on a device at any time, userspace has
  52. strict expectations on when attributes get created. When a new device is
  53. registered in the kernel, a uevent is generated to notify userspace (like
  54. udev) that a new device is available. If attributes are added after the
  55. device is registered, then userspace won't get notified and userspace will
  56. not know about the new attributes.
  57. This is important for device driver that need to publish additional
  58. attributes for a device at driver probe time. If the device driver simply
  59. calls device_create_file() on the device structure passed to it, then
  60. userspace will never be notified of the new attributes. Instead, it should
  61. probably use class_create() and class->dev_attrs to set up a list of
  62. desired attributes in the modules_init function, and then in the .probe()
  63. hook, and then use device_create() to create a new device as a child
  64. of the probed device. The new device will generate a new uevent and
  65. properly advertise the new attributes to userspace.
  66. For example, if a driver wanted to add the following attributes:
  67. struct device_attribute mydriver_attribs[] = {
  68. __ATTR(port_count, 0444, port_count_show),
  69. __ATTR(serial_number, 0444, serial_number_show),
  70. NULL
  71. };
  72. Then in the module init function is would do:
  73. mydriver_class = class_create(THIS_MODULE, "my_attrs");
  74. mydriver_class.dev_attr = mydriver_attribs;
  75. And assuming 'dev' is the struct device passed into the probe hook, the driver
  76. probe function would do something like:
  77. create_device(&mydriver_class, dev, chrdev, &private_data, "my_name");