interface.txt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Device Interfaces
  2. Introduction
  3. ~~~~~~~~~~~~
  4. Device interfaces are the logical interfaces of device classes that correlate
  5. directly to userspace interfaces, like device nodes.
  6. Each device class may have multiple interfaces through which you can
  7. access the same device. An input device may support the mouse interface,
  8. the 'evdev' interface, and the touchscreen interface. A SCSI disk would
  9. support the disk interface, the SCSI generic interface, and possibly a raw
  10. device interface.
  11. Device interfaces are registered with the class they belong to. As devices
  12. are added to the class, they are added to each interface registered with
  13. the class. The interface is responsible for determining whether the device
  14. supports the interface or not.
  15. Programming Interface
  16. ~~~~~~~~~~~~~~~~~~~~~
  17. struct device_interface {
  18. char * name;
  19. rwlock_t lock;
  20. u32 devnum;
  21. struct device_class * devclass;
  22. struct list_head node;
  23. struct driver_dir_entry dir;
  24. int (*add_device)(struct device *);
  25. int (*add_device)(struct intf_data *);
  26. };
  27. int interface_register(struct device_interface *);
  28. void interface_unregister(struct device_interface *);
  29. An interface must specify the device class it belongs to. It is added
  30. to that class's list of interfaces on registration.
  31. Interfaces can be added to a device class at any time. Whenever it is
  32. added, each device in the class is passed to the interface's
  33. add_device callback. When an interface is removed, each device is
  34. removed from the interface.
  35. Devices
  36. ~~~~~~~
  37. Once a device is added to a device class, it is added to each
  38. interface that is registered with the device class. The class
  39. is expected to place a class-specific data structure in
  40. struct device::class_data. The interface can use that (along with
  41. other fields of struct device) to determine whether or not the driver
  42. and/or device support that particular interface.
  43. Data
  44. ~~~~
  45. struct intf_data {
  46. struct list_head node;
  47. struct device_interface * intf;
  48. struct device * dev;
  49. u32 intf_num;
  50. };
  51. int interface_add_data(struct interface_data *);
  52. The interface is responsible for allocating and initializing a struct
  53. intf_data and calling interface_add_data() to add it to the device's list
  54. of interfaces it belongs to. This list will be iterated over when the device
  55. is removed from the class (instead of all possible interfaces for a class).
  56. This structure should probably be embedded in whatever per-device data
  57. structure the interface is allocating anyway.
  58. Devices are enumerated within the interface. This happens in interface_add_data()
  59. and the enumerated value is stored in the struct intf_data for that device.
  60. sysfs
  61. ~~~~~
  62. Each interface is given a directory in the directory of the device
  63. class it belongs to:
  64. Interfaces get a directory in the class's directory as well:
  65. class/
  66. `-- input
  67. |-- devices
  68. |-- drivers
  69. |-- mouse
  70. `-- evdev
  71. When a device is added to the interface, a symlink is created that points
  72. to the device's directory in the physical hierarchy:
  73. class/
  74. `-- input
  75. |-- devices
  76. | `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
  77. |-- drivers
  78. | `-- usb:usb_mouse -> ../../../bus/drivers/usb_mouse/
  79. |-- mouse
  80. | `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
  81. `-- evdev
  82. `-- 1 -> ../../../root/pci0/00:1f.0/usb_bus/00:1f.2-1:0/
  83. Future Plans
  84. ~~~~~~~~~~~~
  85. A device interface is correlated directly with a userspace interface
  86. for a device, specifically a device node. For instance, a SCSI disk
  87. exposes at least two interfaces to userspace: the standard SCSI disk
  88. interface and the SCSI generic interface. It might also export a raw
  89. device interface.
  90. Many interfaces have a major number associated with them and each
  91. device gets a minor number. Or, multiple interfaces might share one
  92. major number, and each will receive a range of minor numbers (like in
  93. the case of input devices).
  94. These major and minor numbers could be stored in the interface
  95. structure. Major and minor allocations could happen when the interface
  96. is registered with the class, or via a helper function.