blob: e87017f3685356854629a439ae4ec5900a7129e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/base/map.c
3 *
4 * (C) Copyright Al Viro 2002,2003
5 * Released under GPL v2.
6 *
7 * NOTE: data structure needs to be changed. It works, but for large dev_t
8 * it will be too slow. It is isolated, though, so these changes will be
9 * local to that file.
10 */
11
12#include <linux/module.h>
13#include <linux/slab.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080014#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kdev_t.h>
16#include <linux/kobject.h>
17#include <linux/kobj_map.h>
18
19struct kobj_map {
20 struct probe {
21 struct probe *next;
22 dev_t dev;
23 unsigned long range;
24 struct module *owner;
25 kobj_probe_t *get;
26 int (*lock)(dev_t, void *);
27 void *data;
28 } *probes[255];
Jes Sorensen58383af2006-02-06 14:12:43 -080029 struct mutex *lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
32int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range,
33 struct module *module, kobj_probe_t *probe,
34 int (*lock)(dev_t, void *), void *data)
35{
36 unsigned n = MAJOR(dev + range - 1) - MAJOR(dev) + 1;
37 unsigned index = MAJOR(dev);
38 unsigned i;
39 struct probe *p;
40
41 if (n > 255)
42 n = 255;
43
44 p = kmalloc(sizeof(struct probe) * n, GFP_KERNEL);
45
46 if (p == NULL)
47 return -ENOMEM;
48
49 for (i = 0; i < n; i++, p++) {
50 p->owner = module;
51 p->get = probe;
52 p->lock = lock;
53 p->dev = dev;
54 p->range = range;
55 p->data = data;
56 }
Jes Sorensen58383af2006-02-06 14:12:43 -080057 mutex_lock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 for (i = 0, p -= n; i < n; i++, p++, index++) {
59 struct probe **s = &domain->probes[index % 255];
60 while (*s && (*s)->range < range)
61 s = &(*s)->next;
62 p->next = *s;
63 *s = p;
64 }
Jes Sorensen58383af2006-02-06 14:12:43 -080065 mutex_unlock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67}
68
69void kobj_unmap(struct kobj_map *domain, dev_t dev, unsigned long range)
70{
71 unsigned n = MAJOR(dev + range - 1) - MAJOR(dev) + 1;
72 unsigned index = MAJOR(dev);
73 unsigned i;
74 struct probe *found = NULL;
75
76 if (n > 255)
77 n = 255;
78
Jes Sorensen58383af2006-02-06 14:12:43 -080079 mutex_lock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 for (i = 0; i < n; i++, index++) {
81 struct probe **s;
82 for (s = &domain->probes[index % 255]; *s; s = &(*s)->next) {
83 struct probe *p = *s;
84 if (p->dev == dev && p->range == range) {
85 *s = p->next;
86 if (!found)
87 found = p;
88 break;
89 }
90 }
91 }
Jes Sorensen58383af2006-02-06 14:12:43 -080092 mutex_unlock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 kfree(found);
94}
95
96struct kobject *kobj_lookup(struct kobj_map *domain, dev_t dev, int *index)
97{
98 struct kobject *kobj;
99 struct probe *p;
100 unsigned long best = ~0UL;
101
102retry:
Jes Sorensen58383af2006-02-06 14:12:43 -0800103 mutex_lock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 for (p = domain->probes[MAJOR(dev) % 255]; p; p = p->next) {
105 struct kobject *(*probe)(dev_t, int *, void *);
106 struct module *owner;
107 void *data;
108
109 if (p->dev > dev || p->dev + p->range - 1 < dev)
110 continue;
111 if (p->range - 1 >= best)
112 break;
113 if (!try_module_get(p->owner))
114 continue;
115 owner = p->owner;
116 data = p->data;
117 probe = p->get;
118 best = p->range - 1;
119 *index = dev - p->dev;
120 if (p->lock && p->lock(dev, data) < 0) {
121 module_put(owner);
122 continue;
123 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800124 mutex_unlock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 kobj = probe(dev, index, data);
126 /* Currently ->owner protects _only_ ->probe() itself. */
127 module_put(owner);
128 if (kobj)
129 return kobj;
130 goto retry;
131 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800132 mutex_unlock(domain->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return NULL;
134}
135
Jes Sorensen58383af2006-02-06 14:12:43 -0800136struct kobj_map *kobj_map_init(kobj_probe_t *base_probe, struct mutex *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 struct kobj_map *p = kmalloc(sizeof(struct kobj_map), GFP_KERNEL);
Jiri Slaby4aed0642005-09-13 01:25:01 -0700139 struct probe *base = kzalloc(sizeof(*base), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int i;
141
142 if ((p == NULL) || (base == NULL)) {
143 kfree(p);
144 kfree(base);
145 return NULL;
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 base->dev = 1;
149 base->range = ~0;
150 base->get = base_probe;
151 for (i = 0; i < 255; i++)
152 p->probes[i] = base;
Jes Sorensen58383af2006-02-06 14:12:43 -0800153 p->lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return p;
155}