2 Device Driver Design Patterns
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 This document describes a few common design patterns found in device drivers.
6 It is likely that subsystem maintainers will ask driver developers to
7 conform to these design patterns.
16 While the kernel contains a few device drivers that assume that they will
17 only be probed() once on a certain system (singletons), it is custom to assume
18 that the device the driver binds to will appear in several instances. This
19 means that the probe() function and all callbacks need to be reentrant.
21 The most common way to achieve this is to use the state container design
22 pattern. It usually has this form:
25 spinlock_t lock; /* Example member */
29 static int foo_probe(...)
33 foo = devm_kzalloc(dev, sizeof(*foo), GFP_KERNEL);
36 spin_lock_init(&foo->lock);
40 This will create an instance of struct foo in memory every time probe() is
41 called. This is our state container for this instance of the device driver.
42 Of course it is then necessary to always pass this instance of the
43 state around to all functions that need access to the state and its members.
45 For example, if the driver is registering an interrupt handler, you would
46 pass around a pointer to struct foo like this:
48 static irqreturn_t foo_handler(int irq, void *arg)
50 struct foo *foo = arg;
54 static int foo_probe(...)
59 ret = request_irq(irq, foo_handler, 0, "foo", foo);
62 This way you always get a pointer back to the correct instance of foo in
63 your interrupt handler.
69 Continuing on the above example we add an offloaded work:
73 struct workqueue_struct *wq;
74 struct work_struct offload;
78 static void foo_work(struct work_struct *work)
80 struct foo *foo = container_of(work, struct foo, offload);
85 static irqreturn_t foo_handler(int irq, void *arg)
87 struct foo *foo = arg;
89 queue_work(foo->wq, &foo->offload);
93 static int foo_probe(...)
97 foo->wq = create_singlethread_workqueue("foo-wq");
98 INIT_WORK(&foo->offload, foo_work);
102 The design pattern is the same for an hrtimer or something similar that will
103 return a single argument which is a pointer to a struct member in the
106 container_of() is a macro defined in <linux/kernel.h>
108 What container_of() does is to obtain a pointer to the containing struct from
109 a pointer to a member by a simple subtraction using the offsetof() macro from
110 standard C, which allows something similar to object oriented behaviours.
111 Notice that the contained member must not be a pointer, but an actual member
114 We can see here that we avoid having global pointers to our struct foo *
115 instance this way, while still keeping the number of parameters passed to the
116 work function to a single pointer.