design-patterns.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Device Driver Design Patterns
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. This document describes a few common design patterns found in device drivers.
  4. It is likely that subsystem maintainers will ask driver developers to
  5. conform to these design patterns.
  6. 1. State Container
  7. 2. container_of()
  8. 1. State Container
  9. ~~~~~~~~~~~~~~~~~~
  10. While the kernel contains a few device drivers that assume that they will
  11. only be probed() once on a certain system (singletons), it is custom to assume
  12. that the device the driver binds to will appear in several instances. This
  13. means that the probe() function and all callbacks need to be reentrant.
  14. The most common way to achieve this is to use the state container design
  15. pattern. It usually has this form:
  16. struct foo {
  17. spinlock_t lock; /* Example member */
  18. (...)
  19. };
  20. static int foo_probe(...)
  21. {
  22. struct foo *foo;
  23. foo = devm_kzalloc(dev, sizeof(*foo), GFP_KERNEL);
  24. if (!foo)
  25. return -ENOMEM;
  26. spin_lock_init(&foo->lock);
  27. (...)
  28. }
  29. This will create an instance of struct foo in memory every time probe() is
  30. called. This is our state container for this instance of the device driver.
  31. Of course it is then necessary to always pass this instance of the
  32. state around to all functions that need access to the state and its members.
  33. For example, if the driver is registering an interrupt handler, you would
  34. pass around a pointer to struct foo like this:
  35. static irqreturn_t foo_handler(int irq, void *arg)
  36. {
  37. struct foo *foo = arg;
  38. (...)
  39. }
  40. static int foo_probe(...)
  41. {
  42. struct foo *foo;
  43. (...)
  44. ret = request_irq(irq, foo_handler, 0, "foo", foo);
  45. }
  46. This way you always get a pointer back to the correct instance of foo in
  47. your interrupt handler.
  48. 2. container_of()
  49. ~~~~~~~~~~~~~~~~~
  50. Continuing on the above example we add an offloaded work:
  51. struct foo {
  52. spinlock_t lock;
  53. struct workqueue_struct *wq;
  54. struct work_struct offload;
  55. (...)
  56. };
  57. static void foo_work(struct work_struct *work)
  58. {
  59. struct foo *foo = container_of(work, struct foo, offload);
  60. (...)
  61. }
  62. static irqreturn_t foo_handler(int irq, void *arg)
  63. {
  64. struct foo *foo = arg;
  65. queue_work(foo->wq, &foo->offload);
  66. (...)
  67. }
  68. static int foo_probe(...)
  69. {
  70. struct foo *foo;
  71. foo->wq = create_singlethread_workqueue("foo-wq");
  72. INIT_WORK(&foo->offload, foo_work);
  73. (...)
  74. }
  75. The design pattern is the same for an hrtimer or something similar that will
  76. return a single argument which is a pointer to a struct member in the
  77. callback.
  78. container_of() is a macro defined in <linux/kernel.h>
  79. What container_of() does is to obtain a pointer to the containing struct from
  80. a pointer to a member by a simple subtraction using the offsetof() macro from
  81. standard C, which allows something similar to object oriented behaviours.
  82. Notice that the contained member must not be a pointer, but an actual member
  83. for this to work.
  84. We can see here that we avoid having global pointers to our struct foo *
  85. instance this way, while still keeping the number of parameters passed to the
  86. work function to a single pointer.