TEXT   8

device.txt

Guest on 10th August 2021 01:14:06 AM

  1.  
  2. The Basic Device Structure
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4.  
  5. struct device {
  6.         struct list_head g_list;
  7.         struct list_head node;
  8.         struct list_head bus_list;
  9.         struct list_head driver_list;
  10.         struct list_head intf_list;
  11.         struct list_head children;
  12.         struct device   * parent;
  13.  
  14.         char    name[DEVICE_NAME_SIZE];
  15.         char    bus_id[BUS_ID_SIZE];
  16.  
  17.         spinlock_t      lock;
  18.         atomic_t        refcount;
  19.  
  20.         struct bus_type * bus;
  21.         struct driver_dir_entry dir;
  22.  
  23.         u32             class_num;
  24.  
  25.         struct device_driver *driver;
  26.         void            *driver_data;
  27.         void            *platform_data;
  28.  
  29.         u32             current_state;
  30.         unsigned char *saved_state;
  31.  
  32.         void    (*release)(struct device * dev);
  33. };
  34.  
  35. Fields
  36. ~~~~~~
  37. g_list: Node in the global device list.
  38.  
  39. node:   Node in device's parent's children list.
  40.  
  41. bus_list: Node in device's bus's devices list.
  42.  
  43. driver_list:   Node in device's driver's devices list.
  44.  
  45. intf_list:     List of intf_data. There is one structure allocated for
  46.                each interface that the device supports.
  47.  
  48. children:      List of child devices.
  49.  
  50. name:          ASCII description of device.
  51.                Example: " 3Com Corporation 3c905 100BaseTX [Boomerang]"
  52.  
  53. bus_id:        ASCII representation of device's bus position. This
  54.                field should a name unique across all devices on the
  55.                bus type the device belongs to.
  56.  
  57.                Example: PCI bus_ids are in the form of
  58.                <bus number>:<slot number>.<function number>
  59.                This name is unique across all PCI devices in the system.
  60.  
  61. lock:          Spinlock for the device.
  62.  
  63. refcount:      Reference count on the device.
  64.  
  65. bus:           Pointer to struct bus_type that device belongs to.
  66.  
  67. dir:           Device's driverfs directory.
  68.  
  69. driver:        Pointer to struct device_driver that controls the device.
  70.  
  71. driver_data:   Driver-specific data.
  72.  
  73. class_num:     Class-enumerated value of the device.
  74.  
  75. platform_data: Platform data specific to the device.
  76.  
  77. current_state: Current power state of the device.
  78.  
  79. saved_state:   Pointer to saved state of the device. This is usable by
  80.                the device driver controlling the device.
  81.  
  82. release:       Callback to free the device after all references have
  83.                gone away. This should be set by the allocator of the
  84.                device (i.e. the bus driver that discovered the device).
  85.  
  86.  
  87. Programming Interface
  88. ~~~~~~~~~~~~~~~~~~~~~
  89. The bus driver that discovers the device uses this to register the
  90. device with the core:
  91.  
  92. int device_register(struct device * dev);
  93.  
  94. The bus should initialize the following fields:
  95.  
  96.     - parent
  97.     - name
  98.     - bus_id
  99.     - bus
  100.  
  101. A device is removed from the core when its reference count goes to
  102. 0. The reference count can be adjusted using:
  103.  
  104. struct device * get_device(struct device * dev);
  105. void put_device(struct device * dev);
  106.  
  107. get_device() will return a pointer to the struct device passed to it
  108. if the reference is not already 0 (if it's in the process of being
  109. removed already).
  110.  
  111. A driver can take use the lock in the device structure using:
  112.  
  113. void lock_device(struct device * dev);
  114. void unlock_device(struct device * dev);
  115.  
  116.  
  117. Attributes
  118. ~~~~~~~~~~
  119. struct device_attribute {
  120.         struct attribute        attr;
  121.         ssize_t (*show)(struct device * dev, char * buf, size_t count, loff_t off);
  122.         ssize_t (*store)(struct device * dev, const char * buf, size_t count, loff_t off);
  123. };
  124.  
  125. Attributes of devices can be exported via drivers using a simple
  126. procfs-like interface.
  127.  
  128. Please see Documentation/filesystems/driverfs.txt for more information
  129. on how driverfs works.
  130.  
  131. Attributes are declared using a macro called DEVICE_ATTR:
  132.  
  133. #define DEVICE_ATTR(name,mode,show,store)
  134.  
  135. Example:
  136.  
  137. DEVICE_ATTR(power,0644,show_power,store_power);
  138.  
  139. This declares a structure of type struct device_attribute named
  140. 'dev_attr_power'. This can then be added and removed to the device's
  141. directory using:
  142.  
  143. int device_create_file(struct device *device, struct device_attribute * entry);
  144. void device_remove_file(struct device * dev, struct device_attribute * attr);
  145.  
  146. Example:
  147.  
  148. device_create_file(dev,&dev_attr_power);
  149. device_remove_file(dev,&dev_attr_power);
  150.  
  151. The file name will be 'power' with a mode of 0644 (-rw-r--r--).

Raw Paste


Login or Register to edit or fork this paste. It's free.