TEXT   58

porting.txt

Guest on 1st August 2021 06:10:39 AM

  1.  
  2. Porting Drivers to the New Driver Model
  3.  
  4. Patrick Mochel
  5.  
  6.  
  7.  
  8. Overview
  9.  
  10. Please refer to Documentation/driver-model/*.txt for definitions of
  11. various driver types and concepts.
  12.  
  13. Most of the work of porting devices drivers to the new model happens
  14. at the bus driver layer. This was intentional, to minimize the
  15. negative effect on kernel drivers, and to allow a gradual transition
  16. of bus drivers.
  17.  
  18. In a nutshell, the driver model consists of a set of objects that can
  19. be embedded in larger, bus-specific objects. Fields in these generic
  20. objects can replace fields in the bus-specific objects.
  21.  
  22. The generic objects must be registered with the driver model core. By
  23. doing so, they will exported via the sysfs filesystem. sysfs can be
  24. mounted by doing
  25.  
  26.         # mount -t sysfs sysfs /sys
  27.  
  28.  
  29.  
  30. The Process
  31.  
  32. Step 0: Read include/linux/device.h for object and function definitions.
  33.  
  34. Step 1: Registering the bus driver.
  35.  
  36.  
  37. - Define a struct bus_type for the bus driver.
  38.  
  39. struct bus_type pci_bus_type = {
  40.         .name           = "pci",
  41. };
  42.  
  43.  
  44. - Register the bus type.
  45.   This should be done in the initialization function for the bus type,
  46.   which is usually the module_init(), or equivalent, function.
  47.  
  48. static int __init pci_driver_init(void)
  49. {
  50.         return bus_register(&pci_bus_type);
  51. }
  52.  
  53. subsys_initcall(pci_driver_init);
  54.  
  55.  
  56.   The bus type may be unregistered (if the bus driver may be compiled
  57.   as a module) by doing:
  58.  
  59.      bus_unregister(&pci_bus_type);
  60.  
  61.  
  62. - Export the bus type for others to use.
  63.  
  64.   Other code may wish to reference the bus type, so declare it in a
  65.   shared header file and export the symbol.
  66.  
  67. From include/linux/pci.h:
  68.  
  69. extern struct bus_type pci_bus_type;
  70.  
  71.  
  72. From file the above code appears in:
  73.  
  74. EXPORT_SYMBOL(pci_bus_type);
  75.  
  76.  
  77.  
  78. - This will cause the bus to show up in /sys/bus/pci/ with two
  79.   subdirectories: 'devices' and 'drivers'.
  80.  
  81. # tree -d /sys/bus/pci/
  82. /sys/bus/pci/
  83. |-- devices
  84. `-- drivers
  85.  
  86.  
  87.  
  88. Step 2: Registering Devices.
  89.  
  90. struct device represents a single device. It mainly contains metadata
  91. describing the relationship the device has to other entities.
  92.  
  93.  
  94. - Embed a struct device in the bus-specific device type.
  95.  
  96.  
  97. struct pci_dev {
  98.        ...
  99.        struct  device  dev;            /* Generic device interface */
  100.        ...
  101. };
  102.  
  103.   It is recommended that the generic device not be the first item in
  104.   the struct to discourage programmers from doing mindless casts
  105.   between the object types. Instead macros, or inline functions,
  106.   should be created to convert from the generic object type.
  107.  
  108.  
  109. #define to_pci_dev(n) container_of(n, struct pci_dev, dev)
  110.  
  111. or
  112.  
  113. static inline struct pci_dev * to_pci_dev(struct kobject * kobj)
  114. {
  115.         return container_of(n, struct pci_dev, dev);
  116. }
  117.  
  118.   This allows the compiler to verify type-safety of the operations
  119.   that are performed (which is Good).
  120.  
  121.  
  122. - Initialize the device on registration.
  123.  
  124.   When devices are discovered or registered with the bus type, the
  125.   bus driver should initialize the generic device. The most important
  126.   things to initialize are the bus_id, parent, and bus fields.
  127.  
  128.   The bus_id is an ASCII string that contains the device's address on
  129.   the bus. The format of this string is bus-specific. This is
  130.   necessary for representing devices in sysfs.
  131.  
  132.   parent is the physical parent of the device. It is important that
  133.   the bus driver sets this field correctly.
  134.  
  135.   The driver model maintains an ordered list of devices that it uses
  136.   for power management. This list must be in order to guarantee that
  137.   devices are shutdown before their physical parents, and vice versa.
  138.   The order of this list is determined by the parent of registered
  139.   devices.
  140.  
  141.   Also, the location of the device's sysfs directory depends on a
  142.   device's parent. sysfs exports a directory structure that mirrors
  143.   the device hierarchy. Accurately setting the parent guarantees that
  144.   sysfs will accurately represent the hierarchy.
  145.  
  146.   The device's bus field is a pointer to the bus type the device
  147.   belongs to. This should be set to the bus_type that was declared
  148.   and initialized before.
  149.  
  150.   Optionally, the bus driver may set the device's name and release
  151.   fields.
  152.  
  153.   The name field is an ASCII string describing the device, like
  154.  
  155.      "ATI Technologies Inc Radeon QD"
  156.  
  157.   The release field is a callback that the driver model core calls
  158.   when the device has been removed, and all references to it have
  159.   been released. More on this in a moment.
  160.  
  161.  
  162. - Register the device.
  163.  
  164.   Once the generic device has been initialized, it can be registered
  165.   with the driver model core by doing:
  166.  
  167.        device_register(&dev->dev);
  168.  
  169.   It can later be unregistered by doing:
  170.  
  171.        device_unregister(&dev->dev);
  172.  
  173.   This should happen on buses that support hotpluggable devices.
  174.   If a bus driver unregisters a device, it should not immediately free
  175.   it. It should instead wait for the driver model core to call the
  176.   device's release method, then free the bus-specific object.
  177.   (There may be other code that is currently referencing the device
  178.   structure, and it would be rude to free the device while that is
  179.   happening).
  180.  
  181.  
  182.   When the device is registered, a directory in sysfs is created.
  183.   The PCI tree in sysfs looks like:
  184.  
  185. /sys/devices/pci0/
  186. |-- 00:00.0
  187. |-- 00:01.0
  188. |   `-- 01:00.0
  189. |-- 00:02.0
  190. |   `-- 02:1f.0
  191. |       `-- 03:00.0
  192. |-- 00:1e.0
  193. |   `-- 04:04.0
  194. |-- 00:1f.0
  195. |-- 00:1f.1
  196. |   |-- ide0
  197. |   |   |-- 0.0
  198. |   |   `-- 0.1
  199. |   `-- ide1
  200. |       `-- 1.0
  201. |-- 00:1f.2
  202. |-- 00:1f.3
  203. `-- 00:1f.5
  204.  
  205.   Also, symlinks are created in the bus's 'devices' directory
  206.   that point to the device's directory in the physical hierarchy.
  207.  
  208. /sys/bus/pci/devices/
  209. |-- 00:00.0 -> ../../../devices/pci0/00:00.0
  210. |-- 00:01.0 -> ../../../devices/pci0/00:01.0
  211. |-- 00:02.0 -> ../../../devices/pci0/00:02.0
  212. |-- 00:1e.0 -> ../../../devices/pci0/00:1e.0
  213. |-- 00:1f.0 -> ../../../devices/pci0/00:1f.0
  214. |-- 00:1f.1 -> ../../../devices/pci0/00:1f.1
  215. |-- 00:1f.2 -> ../../../devices/pci0/00:1f.2
  216. |-- 00:1f.3 -> ../../../devices/pci0/00:1f.3
  217. |-- 00:1f.5 -> ../../../devices/pci0/00:1f.5
  218. |-- 01:00.0 -> ../../../devices/pci0/00:01.0/01:00.0
  219. |-- 02:1f.0 -> ../../../devices/pci0/00:02.0/02:1f.0
  220. |-- 03:00.0 -> ../../../devices/pci0/00:02.0/02:1f.0/03:00.0
  221. `-- 04:04.0 -> ../../../devices/pci0/00:1e.0/04:04.0
  222.  
  223.  
  224.  
  225. Step 3: Registering Drivers.
  226.  
  227. struct device_driver is a simple driver structure that contains a set
  228. of operations that the driver model core may call.
  229.  
  230.  
  231. - Embed a struct device_driver in the bus-specific driver.
  232.  
  233.   Just like with devices, do something like:
  234.  
  235. struct pci_driver {
  236.        ...
  237.        struct device_driver    driver;
  238. };
  239.  
  240.  
  241. - Initialize the generic driver structure.
  242.  
  243.   When the driver registers with the bus (e.g. doing pci_register_driver()),
  244.   initialize the necessary fields of the driver: the name and bus
  245.   fields.
  246.  
  247.  
  248. - Register the driver.
  249.  
  250.   After the generic driver has been initialized, call
  251.  
  252.         driver_register(&drv->driver);
  253.  
  254.   to register the driver with the core.
  255.  
  256.   When the driver is unregistered from the bus, unregister it from the
  257.   core by doing:
  258.  
  259.         driver_unregister(&drv->driver);
  260.  
  261.   Note that this will block until all references to the driver have
  262.   gone away. Normally, there will not be any.
  263.  
  264.  
  265. - Sysfs representation.
  266.  
  267.   Drivers are exported via sysfs in their bus's 'driver's directory.
  268.   For example:
  269.  
  270. /sys/bus/pci/drivers/
  271. |-- 3c59x
  272. |-- Ensoniq AudioPCI
  273. |-- agpgart-amdk7
  274. |-- e100
  275. `-- serial
  276.  
  277.  
  278. Step 4: Define Generic Methods for Drivers.
  279.  
  280. struct device_driver defines a set of operations that the driver model
  281. core calls. Most of these operations are probably similar to
  282. operations the bus already defines for drivers, but taking different
  283. parameters.
  284.  
  285. It would be difficult and tedious to force every driver on a bus to
  286. simultaneously convert their drivers to generic format. Instead, the
  287. bus driver should define single instances of the generic methods that
  288. forward call to the bus-specific drivers. For instance:
  289.  
  290.  
  291. static int pci_device_remove(struct device * dev)
  292. {
  293.         struct pci_dev * pci_dev = to_pci_dev(dev);
  294.         struct pci_driver * drv = pci_dev->driver;
  295.  
  296.         if (drv) {
  297.                 if (drv->remove)
  298.                         drv->remove(pci_dev);
  299.                 pci_dev->driver = NULL;
  300.         }
  301.         return 0;
  302. }
  303.  
  304.  
  305. The generic driver should be initialized with these methods before it
  306. is registered.
  307.  
  308.         /* initialize common driver fields */
  309.         drv->driver.name = drv->name;
  310.         drv->driver.bus = &pci_bus_type;
  311.         drv->driver.probe = pci_device_probe;
  312.         drv->driver.resume = pci_device_resume;
  313.         drv->driver.suspend = pci_device_suspend;
  314.         drv->driver.remove = pci_device_remove;
  315.  
  316.         /* register with core */
  317.         driver_register(&drv->driver);
  318.  
  319.  
  320. Ideally, the bus should only initialize the fields if they are not
  321. already set. This allows the drivers to implement their own generic
  322. methods.
  323.  
  324.  
  325. Step 5: Support generic driver binding.
  326.  
  327. The model assumes that a device or driver can be dynamically
  328. registered with the bus at any time. When registration happens,
  329. devices must be bound to a driver, or drivers must be bound to all
  330. devices that it supports.
  331.  
  332. A driver typically contains a list of device IDs that it supports. The
  333. bus driver compares these IDs to the IDs of devices registered with it.
  334. The format of the device IDs, and the semantics for comparing them are
  335. bus-specific, so the generic model does attempt to generalize them.
  336.  
  337. Instead, a bus may supply a method in struct bus_type that does the
  338. comparison:
  339.  
  340.   int (*match)(struct device * dev, struct device_driver * drv);
  341.  
  342. match should return positive value if the driver supports the device,
  343. and zero otherwise. It may also return error code (for example
  344. -EPROBE_DEFER) if determining that given driver supports the device is
  345. not possible.
  346.  
  347. When a device is registered, the bus's list of drivers is iterated
  348. over. bus->match() is called for each one until a match is found.
  349.  
  350. When a driver is registered, the bus's list of devices is iterated
  351. over. bus->match() is called for each device that is not already
  352. claimed by a driver.
  353.  
  354. When a device is successfully bound to a driver, device->driver is
  355. set, the device is added to a per-driver list of devices, and a
  356. symlink is created in the driver's sysfs directory that points to the
  357. device's physical directory:
  358.  
  359. /sys/bus/pci/drivers/
  360. |-- 3c59x
  361. |   `-- 00:0b.0 -> ../../../../devices/pci0/00:0b.0
  362. |-- Ensoniq AudioPCI
  363. |-- agpgart-amdk7
  364. |   `-- 00:00.0 -> ../../../../devices/pci0/00:00.0
  365. |-- e100
  366. |   `-- 00:0c.0 -> ../../../../devices/pci0/00:0c.0
  367. `-- serial
  368.  
  369.  
  370. This driver binding should replace the existing driver binding
  371. mechanism the bus currently uses.
  372.  
  373.  
  374. Step 6: Supply a hotplug callback.
  375.  
  376. Whenever a device is registered with the driver model core, the
  377. userspace program /sbin/hotplug is called to notify userspace.
  378. Users can define actions to perform when a device is inserted or
  379. removed.
  380.  
  381. The driver model core passes several arguments to userspace via
  382. environment variables, including
  383.  
  384. - ACTION: set to 'add' or 'remove'
  385. - DEVPATH: set to the device's physical path in sysfs.
  386.  
  387. A bus driver may also supply additional parameters for userspace to
  388. consume. To do this, a bus must implement the 'hotplug' method in
  389. struct bus_type:
  390.  
  391.      int (*hotplug) (struct device *dev, char **envp,
  392.                      int num_envp, char *buffer, int buffer_size);
  393.  
  394. This is called immediately before /sbin/hotplug is executed.
  395.  
  396.  
  397. Step 7: Cleaning up the bus driver.
  398.  
  399. The generic bus, device, and driver structures provide several fields
  400. that can replace those defined privately to the bus driver.
  401.  
  402. - Device list.
  403.  
  404. struct bus_type contains a list of all devices registered with the bus
  405. type. This includes all devices on all instances of that bus type.
  406. An internal list that the bus uses may be removed, in favor of using
  407. this one.
  408.  
  409. The core provides an iterator to access these devices.
  410.  
  411. int bus_for_each_dev(struct bus_type * bus, struct device * start,
  412.                      void * data, int (*fn)(struct device *, void *));
  413.  
  414.  
  415. - Driver list.
  416.  
  417. struct bus_type also contains a list of all drivers registered with
  418. it. An internal list of drivers that the bus driver maintains may
  419. be removed in favor of using the generic one.
  420.  
  421. The drivers may be iterated over, like devices:
  422.  
  423. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  424.                      void * data, int (*fn)(struct device_driver *, void *));
  425.  
  426.  
  427. Please see drivers/base/bus.c for more information.
  428.  
  429.  
  430. - rwsem
  431.  
  432. struct bus_type contains an rwsem that protects all core accesses to
  433. the device and driver lists. This can be used by the bus driver
  434. internally, and should be used when accessing the device or driver
  435. lists the bus maintains.
  436.  
  437.  
  438. - Device and driver fields.
  439.  
  440. Some of the fields in struct device and struct device_driver duplicate
  441. fields in the bus-specific representations of these objects. Feel free
  442. to remove the bus-specific ones and favor the generic ones. Note
  443. though, that this will likely mean fixing up all the drivers that
  444. reference the bus-specific fields (though those should all be 1-line
  445. changes).

Raw Paste


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