18c2ecf20Sopenharmony_ci=============================
28c2ecf20Sopenharmony_ciDevice Driver Design Patterns
38c2ecf20Sopenharmony_ci=============================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciThis document describes a few common design patterns found in device drivers.
68c2ecf20Sopenharmony_ciIt is likely that subsystem maintainers will ask driver developers to
78c2ecf20Sopenharmony_ciconform to these design patterns.
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci1. State Container
108c2ecf20Sopenharmony_ci2. container_of()
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci1. State Container
148c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciWhile the kernel contains a few device drivers that assume that they will
178c2ecf20Sopenharmony_cionly be probed() once on a certain system (singletons), it is custom to assume
188c2ecf20Sopenharmony_cithat the device the driver binds to will appear in several instances. This
198c2ecf20Sopenharmony_cimeans that the probe() function and all callbacks need to be reentrant.
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciThe most common way to achieve this is to use the state container design
228c2ecf20Sopenharmony_cipattern. It usually has this form::
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci  struct foo {
258c2ecf20Sopenharmony_ci      spinlock_t lock; /* Example member */
268c2ecf20Sopenharmony_ci      (...)
278c2ecf20Sopenharmony_ci  };
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci  static int foo_probe(...)
308c2ecf20Sopenharmony_ci  {
318c2ecf20Sopenharmony_ci      struct foo *foo;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci      foo = devm_kzalloc(dev, sizeof(*foo), GFP_KERNEL);
348c2ecf20Sopenharmony_ci      if (!foo)
358c2ecf20Sopenharmony_ci          return -ENOMEM;
368c2ecf20Sopenharmony_ci      spin_lock_init(&foo->lock);
378c2ecf20Sopenharmony_ci      (...)
388c2ecf20Sopenharmony_ci  }
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciThis will create an instance of struct foo in memory every time probe() is
418c2ecf20Sopenharmony_cicalled. This is our state container for this instance of the device driver.
428c2ecf20Sopenharmony_ciOf course it is then necessary to always pass this instance of the
438c2ecf20Sopenharmony_cistate around to all functions that need access to the state and its members.
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciFor example, if the driver is registering an interrupt handler, you would
468c2ecf20Sopenharmony_cipass around a pointer to struct foo like this::
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci  static irqreturn_t foo_handler(int irq, void *arg)
498c2ecf20Sopenharmony_ci  {
508c2ecf20Sopenharmony_ci      struct foo *foo = arg;
518c2ecf20Sopenharmony_ci      (...)
528c2ecf20Sopenharmony_ci  }
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci  static int foo_probe(...)
558c2ecf20Sopenharmony_ci  {
568c2ecf20Sopenharmony_ci      struct foo *foo;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci      (...)
598c2ecf20Sopenharmony_ci      ret = request_irq(irq, foo_handler, 0, "foo", foo);
608c2ecf20Sopenharmony_ci  }
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ciThis way you always get a pointer back to the correct instance of foo in
638c2ecf20Sopenharmony_ciyour interrupt handler.
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci2. container_of()
678c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciContinuing on the above example we add an offloaded work::
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci  struct foo {
728c2ecf20Sopenharmony_ci      spinlock_t lock;
738c2ecf20Sopenharmony_ci      struct workqueue_struct *wq;
748c2ecf20Sopenharmony_ci      struct work_struct offload;
758c2ecf20Sopenharmony_ci      (...)
768c2ecf20Sopenharmony_ci  };
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci  static void foo_work(struct work_struct *work)
798c2ecf20Sopenharmony_ci  {
808c2ecf20Sopenharmony_ci      struct foo *foo = container_of(work, struct foo, offload);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci      (...)
838c2ecf20Sopenharmony_ci  }
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci  static irqreturn_t foo_handler(int irq, void *arg)
868c2ecf20Sopenharmony_ci  {
878c2ecf20Sopenharmony_ci      struct foo *foo = arg;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci      queue_work(foo->wq, &foo->offload);
908c2ecf20Sopenharmony_ci      (...)
918c2ecf20Sopenharmony_ci  }
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci  static int foo_probe(...)
948c2ecf20Sopenharmony_ci  {
958c2ecf20Sopenharmony_ci      struct foo *foo;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci      foo->wq = create_singlethread_workqueue("foo-wq");
988c2ecf20Sopenharmony_ci      INIT_WORK(&foo->offload, foo_work);
998c2ecf20Sopenharmony_ci      (...)
1008c2ecf20Sopenharmony_ci  }
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ciThe design pattern is the same for an hrtimer or something similar that will
1038c2ecf20Sopenharmony_cireturn a single argument which is a pointer to a struct member in the
1048c2ecf20Sopenharmony_cicallback.
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cicontainer_of() is a macro defined in <linux/kernel.h>
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ciWhat container_of() does is to obtain a pointer to the containing struct from
1098c2ecf20Sopenharmony_cia pointer to a member by a simple subtraction using the offsetof() macro from
1108c2ecf20Sopenharmony_cistandard C, which allows something similar to object oriented behaviours.
1118c2ecf20Sopenharmony_ciNotice that the contained member must not be a pointer, but an actual member
1128c2ecf20Sopenharmony_cifor this to work.
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciWe can see here that we avoid having global pointers to our struct foo *
1158c2ecf20Sopenharmony_ciinstance this way, while still keeping the number of parameters passed to the
1168c2ecf20Sopenharmony_ciwork function to a single pointer.
117