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