Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.13.7.
  1
  2The Basic Device Structure
  3~~~~~~~~~~~~~~~~~~~~~~~~~~
  4
  5See the kerneldoc for the struct device.
  6
  7
  8Programming Interface
  9~~~~~~~~~~~~~~~~~~~~~
 10The bus driver that discovers the device uses this to register the
 11device with the core:
 12
 13int device_register(struct device * dev);
 14
 15The bus should initialize the following fields:
 16
 17    - parent
 18    - name
 19    - bus_id
 20    - bus
 21
 22A device is removed from the core when its reference count goes to
 230. The reference count can be adjusted using:
 24
 25struct device * get_device(struct device * dev);
 26void put_device(struct device * dev);
 27
 28get_device() will return a pointer to the struct device passed to it
 29if the reference is not already 0 (if it's in the process of being
 30removed already).
 31
 32A driver can access the lock in the device structure using: 
 33
 34void lock_device(struct device * dev);
 35void unlock_device(struct device * dev);
 36
 37
 38Attributes
 39~~~~~~~~~~
 40struct device_attribute {
 41	struct attribute	attr;
 42	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
 43			char *buf);
 44	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
 45			 const char *buf, size_t count);
 46};
 47
 48Attributes of devices can be exported via drivers using a simple
 49procfs-like interface. 
 50
 51Please see Documentation/filesystems/sysfs.txt for more information
 52on how sysfs works.
 53
 54Attributes are declared using a macro called DEVICE_ATTR:
 55
 56#define DEVICE_ATTR(name,mode,show,store)
 57
 58Example:
 59
 60DEVICE_ATTR(power,0644,show_power,store_power);
 61
 62This declares a structure of type struct device_attribute named
 63'dev_attr_power'. This can then be added and removed to the device's
 64directory using:
 65
 66int device_create_file(struct device *device, struct device_attribute * entry);
 67void device_remove_file(struct device * dev, struct device_attribute * attr);
 68
 69Example:
 70
 71device_create_file(dev,&dev_attr_power);
 72device_remove_file(dev,&dev_attr_power);
 73
 74The file name will be 'power' with a mode of 0644 (-rw-r--r--).
 75
 76Word of warning:  While the kernel allows device_create_file() and
 77device_remove_file() to be called on a device at any time, userspace has
 78strict expectations on when attributes get created.  When a new device is
 79registered in the kernel, a uevent is generated to notify userspace (like
 80udev) that a new device is available.  If attributes are added after the
 81device is registered, then userspace won't get notified and userspace will
 82not know about the new attributes.
 83
 84This is important for device driver that need to publish additional
 85attributes for a device at driver probe time.  If the device driver simply
 86calls device_create_file() on the device structure passed to it, then
 87userspace will never be notified of the new attributes.  Instead, it should
 88probably use class_create() and class->dev_attrs to set up a list of
 89desired attributes in the modules_init function, and then in the .probe()
 90hook, and then use device_create() to create a new device as a child
 91of the probed device.  The new device will generate a new uevent and
 92properly advertise the new attributes to userspace.
 93
 94For example, if a driver wanted to add the following attributes:
 95struct device_attribute mydriver_attribs[] = {
 96	__ATTR(port_count, 0444, port_count_show),
 97	__ATTR(serial_number, 0444, serial_number_show),
 98	NULL
 99};
100
101Then in the module init function is would do:
102	mydriver_class = class_create(THIS_MODULE, "my_attrs");
103	mydriver_class.dev_attr = mydriver_attribs;
104
105And assuming 'dev' is the struct device passed into the probe hook, the driver
106probe function would do something like:
107	device_create(&mydriver_class, dev, chrdev, &private_data, "my_name");