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