device.txt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. The Basic Device Structure
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. struct device {
  4. struct list_head g_list;
  5. struct list_head node;
  6. struct list_head bus_list;
  7. struct list_head driver_list;
  8. struct list_head intf_list;
  9. struct list_head children;
  10. struct device * parent;
  11. char name[DEVICE_NAME_SIZE];
  12. char bus_id[BUS_ID_SIZE];
  13. spinlock_t lock;
  14. atomic_t refcount;
  15. struct bus_type * bus;
  16. struct driver_dir_entry dir;
  17. u32 class_num;
  18. struct device_driver *driver;
  19. void *driver_data;
  20. void *platform_data;
  21. u32 current_state;
  22. unsigned char *saved_state;
  23. void (*release)(struct device * dev);
  24. };
  25. Fields
  26. ~~~~~~
  27. g_list: Node in the global device list.
  28. node: Node in device's parent's children list.
  29. bus_list: Node in device's bus's devices list.
  30. driver_list: Node in device's driver's devices list.
  31. intf_list: List of intf_data. There is one structure allocated for
  32. each interface that the device supports.
  33. children: List of child devices.
  34. parent: *** FIXME ***
  35. name: ASCII description of device.
  36. Example: " 3Com Corporation 3c905 100BaseTX [Boomerang]"
  37. bus_id: ASCII representation of device's bus position. This
  38. field should be a name unique across all devices on the
  39. bus type the device belongs to.
  40. Example: PCI bus_ids are in the form of
  41. <bus number>:<slot number>.<function number>
  42. This name is unique across all PCI devices in the system.
  43. lock: Spinlock for the device.
  44. refcount: Reference count on the device.
  45. bus: Pointer to struct bus_type that device belongs to.
  46. dir: Device's sysfs directory.
  47. class_num: Class-enumerated value of the device.
  48. driver: Pointer to struct device_driver that controls the device.
  49. driver_data: Driver-specific data.
  50. platform_data: Platform data specific to the device.
  51. Example: for devices on custom boards, as typical of embedded
  52. and SOC based hardware, Linux often uses platform_data to point
  53. to board-specific structures describing devices and how they
  54. are wired. That can include what ports are available, chip
  55. variants, which GPIO pins act in what additional roles, and so
  56. on. This shrinks the "Board Support Packages" (BSPs) and
  57. minimizes board-specific #ifdefs in drivers.
  58. current_state: Current power state of the device.
  59. saved_state: Pointer to saved state of the device. This is usable by
  60. the device driver controlling the device.
  61. release: Callback to free the device after all references have
  62. gone away. This should be set by the allocator of the
  63. device (i.e. the bus driver that discovered the device).
  64. Programming Interface
  65. ~~~~~~~~~~~~~~~~~~~~~
  66. The bus driver that discovers the device uses this to register the
  67. device with the core:
  68. int device_register(struct device * dev);
  69. The bus should initialize the following fields:
  70. - parent
  71. - name
  72. - bus_id
  73. - bus
  74. A device is removed from the core when its reference count goes to
  75. 0. The reference count can be adjusted using:
  76. struct device * get_device(struct device * dev);
  77. void put_device(struct device * dev);
  78. get_device() will return a pointer to the struct device passed to it
  79. if the reference is not already 0 (if it's in the process of being
  80. removed already).
  81. A driver can access the lock in the device structure using:
  82. void lock_device(struct device * dev);
  83. void unlock_device(struct device * dev);
  84. Attributes
  85. ~~~~~~~~~~
  86. struct device_attribute {
  87. struct attribute attr;
  88. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  89. char *buf);
  90. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  91. const char *buf, size_t count);
  92. };
  93. Attributes of devices can be exported via drivers using a simple
  94. procfs-like interface.
  95. Please see Documentation/filesystems/sysfs.txt for more information
  96. on how sysfs works.
  97. Attributes are declared using a macro called DEVICE_ATTR:
  98. #define DEVICE_ATTR(name,mode,show,store)
  99. Example:
  100. DEVICE_ATTR(power,0644,show_power,store_power);
  101. This declares a structure of type struct device_attribute named
  102. 'dev_attr_power'. This can then be added and removed to the device's
  103. directory using:
  104. int device_create_file(struct device *device, struct device_attribute * entry);
  105. void device_remove_file(struct device * dev, struct device_attribute * attr);
  106. Example:
  107. device_create_file(dev,&dev_attr_power);
  108. device_remove_file(dev,&dev_attr_power);
  109. The file name will be 'power' with a mode of 0644 (-rw-r--r--).
  110. Word of warning: While the kernel allows device_create_file() and
  111. device_remove_file() to be called on a device at any time, userspace has
  112. strict expectations on when attributes get created. When a new device is
  113. registered in the kernel, a uevent is generated to notify userspace (like
  114. udev) that a new device is available. If attributes are added after the
  115. device is registered, then userspace won't get notified and userspace will
  116. not know about the new attributes.
  117. This is important for device driver that need to publish additional
  118. attributes for a device at driver probe time. If the device driver simply
  119. calls device_create_file() on the device structure passed to it, then
  120. userspace will never be notified of the new attributes. Instead, it should
  121. probably use class_create() and class->dev_attrs to set up a list of
  122. desired attributes in the modules_init function, and then in the .probe()
  123. hook, and then use device_create() to create a new device as a child
  124. of the probed device. The new device will generate a new uevent and
  125. properly advertise the new attributes to userspace.
  126. For example, if a driver wanted to add the following attributes:
  127. struct device_attribute mydriver_attribs[] = {
  128. __ATTR(port_count, 0444, port_count_show),
  129. __ATTR(serial_number, 0444, serial_number_show),
  130. NULL
  131. };
  132. Then in the module init function is would do:
  133. mydriver_class = class_create(THIS_MODULE, "my_attrs");
  134. mydriver_class.dev_attr = mydriver_attribs;
  135. And assuming 'dev' is the struct device passed into the probe hook, the driver
  136. probe function would do something like:
  137. create_device(&mydriver_class, dev, chrdev, &private_data, "my_name");