Rev 456 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
455 | giacomo | 1 | /* |
2 | * drivers/base/core.c - core driver model code (device registration, etc) |
||
3 | * |
||
4 | * Copyright (c) 2002-3 Patrick Mochel |
||
5 | * Copyright (c) 2002-3 Open Source Development Labs |
||
6 | * |
||
7 | * This file is released under the GPLv2 |
||
8 | * |
||
9 | */ |
||
10 | |||
11 | #undef DEBUG |
||
12 | |||
13 | #include <linuxcomp.h> |
||
14 | |||
15 | #include <linux/device.h> |
||
16 | #include <linux/err.h> |
||
17 | #include <linux/init.h> |
||
18 | #include <linux/module.h> |
||
19 | #include <linux/slab.h> |
||
20 | #include <linux/string.h> |
||
21 | |||
22 | #include <asm/semaphore.h> |
||
23 | |||
24 | #include "base.h" |
||
25 | |||
26 | int (*platform_notify)(struct device * dev) = NULL; |
||
27 | int (*platform_notify_remove)(struct device * dev) = NULL; |
||
28 | |||
29 | /* |
||
30 | * sysfs bindings for devices. |
||
31 | */ |
||
32 | |||
33 | #define to_dev(obj) container_of(obj,struct device,kobj) |
||
34 | #define to_dev_attr(_attr) container_of(_attr,struct device_attribute,attr) |
||
35 | |||
36 | extern struct attribute * dev_default_attrs[]; |
||
37 | |||
38 | static ssize_t |
||
39 | dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
||
40 | { |
||
41 | struct device_attribute * dev_attr = to_dev_attr(attr); |
||
42 | struct device * dev = to_dev(kobj); |
||
43 | ssize_t ret = 0; |
||
44 | |||
45 | if (dev_attr->show) |
||
46 | ret = dev_attr->show(dev,buf); |
||
47 | return ret; |
||
48 | } |
||
49 | |||
50 | static ssize_t |
||
51 | dev_attr_store(struct kobject * kobj, struct attribute * attr, |
||
52 | const char * buf, size_t count) |
||
53 | { |
||
54 | struct device_attribute * dev_attr = to_dev_attr(attr); |
||
55 | struct device * dev = to_dev(kobj); |
||
56 | ssize_t ret = 0; |
||
57 | |||
58 | if (dev_attr->store) |
||
59 | ret = dev_attr->store(dev,buf,count); |
||
60 | return ret; |
||
61 | } |
||
62 | |||
63 | static struct sysfs_ops dev_sysfs_ops = { |
||
64 | .show = dev_attr_show, |
||
65 | .store = dev_attr_store, |
||
66 | }; |
||
67 | |||
68 | |||
69 | /** |
||
70 | * device_release - free device structure. |
||
71 | * @kobj: device's kobject. |
||
72 | * |
||
73 | * This is called once the reference count for the object |
||
74 | * reaches 0. We forward the call to the device's release |
||
75 | * method, which should handle actually freeing the structure. |
||
76 | */ |
||
77 | static void device_release(struct kobject * kobj) |
||
78 | { |
||
79 | struct device * dev = to_dev(kobj); |
||
80 | //struct completion * c = dev->complete; |
||
81 | |||
82 | if (dev->release) |
||
83 | dev->release(dev); |
||
84 | else { |
||
85 | printk(KERN_ERR "Device '%s' does not have a release() function, " |
||
86 | "it is broken and must be fixed.\n", |
||
87 | dev->bus_id); |
||
88 | } |
||
89 | //if (c) |
||
90 | // complete(c); |
||
91 | } |
||
92 | |||
93 | static struct kobj_type ktype_device = { |
||
94 | .release = device_release, |
||
95 | .sysfs_ops = &dev_sysfs_ops, |
||
96 | .default_attrs = dev_default_attrs, |
||
97 | }; |
||
98 | |||
99 | |||
100 | static int dev_hotplug_filter(struct kset *kset, struct kobject *kobj) |
||
101 | { |
||
102 | struct kobj_type *ktype = get_ktype(kobj); |
||
103 | |||
104 | if (ktype == &ktype_device) { |
||
105 | struct device *dev = to_dev(kobj); |
||
106 | if (dev->bus) |
||
107 | return 1; |
||
108 | } |
||
109 | return 0; |
||
110 | } |
||
111 | |||
112 | static char *dev_hotplug_name(struct kset *kset, struct kobject *kobj) |
||
113 | { |
||
114 | struct device *dev = to_dev(kobj); |
||
115 | |||
116 | return dev->bus->name; |
||
117 | } |
||
118 | |||
119 | static int dev_hotplug(struct kset *kset, struct kobject *kobj, char **envp, |
||
120 | int num_envp, char *buffer, int buffer_size) |
||
121 | { |
||
122 | struct device *dev = to_dev(kobj); |
||
123 | int retval = 0; |
||
124 | |||
125 | if (dev->bus->hotplug) { |
||
126 | /* have the bus specific function add its stuff */ |
||
127 | retval = dev->bus->hotplug (dev, envp, num_envp, buffer, buffer_size); |
||
128 | if (retval) { |
||
129 | pr_debug ("%s - hotplug() returned %d\n", |
||
130 | __FUNCTION__, retval); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | return retval; |
||
135 | } |
||
136 | |||
137 | static struct kset_hotplug_ops device_hotplug_ops = { |
||
138 | .filter = dev_hotplug_filter, |
||
139 | .name = dev_hotplug_name, |
||
140 | .hotplug = dev_hotplug, |
||
141 | }; |
||
142 | |||
143 | /** |
||
144 | * device_subsys - structure to be registered with kobject core. |
||
145 | */ |
||
146 | |||
147 | decl_subsys(devices, &ktype_device, &device_hotplug_ops); |
||
148 | |||
149 | |||
150 | /** |
||
151 | * device_create_file - create sysfs attribute file for device. |
||
152 | * @dev: device. |
||
153 | * @attr: device attribute descriptor. |
||
154 | */ |
||
155 | |||
156 | int device_create_file(struct device * dev, struct device_attribute * attr) |
||
157 | { |
||
158 | int error = 0; |
||
159 | if (get_device(dev)) { |
||
160 | error = 0;//sysfs_create_file(&dev->kobj,&attr->attr); |
||
161 | put_device(dev); |
||
162 | } |
||
163 | return error; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * device_remove_file - remove sysfs attribute file. |
||
168 | * @dev: device. |
||
169 | * @attr: device attribute descriptor. |
||
170 | */ |
||
171 | |||
172 | void device_remove_file(struct device * dev, struct device_attribute * attr) |
||
173 | { |
||
174 | if (get_device(dev)) { |
||
175 | //sysfs_remove_file(&dev->kobj,&attr->attr); |
||
176 | put_device(dev); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * device_initialize - init device structure. |
||
183 | * @dev: device. |
||
184 | * |
||
185 | * This prepares the device for use by other layers, |
||
186 | * including adding it to the device hierarchy. |
||
187 | * It is the first half of device_register(), if called by |
||
188 | * that, though it can also be called separately, so one |
||
189 | * may use @dev's fields (e.g. the refcount). |
||
190 | */ |
||
191 | |||
192 | void device_initialize(struct device *dev) |
||
193 | { |
||
194 | kobj_set_kset_s(dev,devices_subsys); |
||
195 | kobject_init(&dev->kobj); |
||
196 | INIT_LIST_HEAD(&dev->node); |
||
197 | INIT_LIST_HEAD(&dev->children); |
||
198 | INIT_LIST_HEAD(&dev->driver_list); |
||
199 | INIT_LIST_HEAD(&dev->bus_list); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * device_add - add device to device hierarchy. |
||
204 | * @dev: device. |
||
205 | * |
||
206 | * This is part 2 of device_register(), though may be called |
||
207 | * separately _iff_ device_initialize() has been called separately. |
||
208 | * |
||
209 | * This adds it to the kobject hierarchy via kobject_add(), adds it |
||
210 | * to the global and sibling lists for the device, then |
||
211 | * adds it to the other relevant subsystems of the driver model. |
||
212 | */ |
||
213 | int device_add(struct device *dev) |
||
214 | { |
||
215 | struct device * parent; |
||
216 | int error; |
||
217 | |||
218 | dev = get_device(dev); |
||
457 | giacomo | 219 | |
455 | giacomo | 220 | if (!dev || !strlen(dev->bus_id)) |
221 | return -EINVAL; |
||
222 | |||
223 | parent = get_device(dev->parent); |
||
224 | |||
225 | pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); |
||
226 | |||
227 | /* first, register with generic layer. */ |
||
228 | kobject_set_name(&dev->kobj,dev->bus_id); |
||
457 | giacomo | 229 | |
230 | if (parent) |
||
455 | giacomo | 231 | dev->kobj.parent = &parent->kobj; |
232 | |||
233 | if ((error = kobject_add(&dev->kobj))) |
||
234 | goto Error; |
||
235 | //if ((error = device_pm_add(dev))) |
||
236 | // goto PMError; |
||
457 | giacomo | 237 | |
455 | giacomo | 238 | if ((error = bus_add_device(dev))) |
239 | goto BusError; |
||
457 | giacomo | 240 | |
455 | giacomo | 241 | //down_write(&devices_subsys.rwsem); |
242 | if (parent) |
||
243 | list_add_tail(&dev->node,&parent->children); |
||
244 | //up_write(&devices_subsys.rwsem); |
||
245 | |||
246 | /* notify platform of device entry */ |
||
247 | if (platform_notify) |
||
248 | platform_notify(dev); |
||
249 | Done: |
||
250 | put_device(dev); |
||
251 | return error; |
||
252 | BusError: |
||
253 | //device_pm_remove(dev); |
||
254 | //PMError: |
||
255 | kobject_unregister(&dev->kobj); |
||
256 | Error: |
||
257 | if (parent) |
||
258 | put_device(parent); |
||
259 | goto Done; |
||
260 | } |
||
261 | |||
262 | |||
263 | /** |
||
264 | * device_register - register a device with the system. |
||
265 | * @dev: pointer to the device structure |
||
266 | * |
||
267 | * This happens in two clean steps - initialize the device |
||
268 | * and add it to the system. The two steps can be called |
||
269 | * separately, but this is the easiest and most common. |
||
270 | * I.e. you should only call the two helpers separately if |
||
271 | * have a clearly defined need to use and refcount the device |
||
272 | * before it is added to the hierarchy. |
||
273 | */ |
||
274 | |||
275 | int device_register(struct device *dev) |
||
276 | { |
||
277 | device_initialize(dev); |
||
278 | return device_add(dev); |
||
279 | } |
||
280 | |||
281 | |||
282 | /** |
||
283 | * get_device - increment reference count for device. |
||
284 | * @dev: device. |
||
285 | * |
||
286 | * This simply forwards the call to kobject_get(), though |
||
287 | * we do take care to provide for the case that we get a NULL |
||
288 | * pointer passed in. |
||
289 | */ |
||
290 | |||
291 | struct device * get_device(struct device * dev) |
||
292 | { |
||
293 | return dev ? to_dev(kobject_get(&dev->kobj)) : NULL; |
||
294 | } |
||
295 | |||
296 | |||
297 | /** |
||
298 | * put_device - decrement reference count. |
||
299 | * @dev: device in question. |
||
300 | */ |
||
301 | void put_device(struct device * dev) |
||
302 | { |
||
303 | kobject_put(&dev->kobj); |
||
304 | } |
||
305 | |||
306 | |||
307 | /** |
||
308 | * device_del - delete device from system. |
||
309 | * @dev: device. |
||
310 | * |
||
311 | * This is the first part of the device unregistration |
||
312 | * sequence. This removes the device from the lists we control |
||
313 | * from here, has it removed from the other driver model |
||
314 | * subsystems it was added to in device_add(), and removes it |
||
315 | * from the kobject hierarchy. |
||
316 | * |
||
317 | * NOTE: this should be called manually _iff_ device_add() was |
||
318 | * also called manually. |
||
319 | */ |
||
320 | |||
321 | void device_del(struct device * dev) |
||
322 | { |
||
323 | struct device * parent = dev->parent; |
||
324 | |||
325 | //down_write(&devices_subsys.rwsem); |
||
326 | if (parent) |
||
327 | list_del_init(&dev->node); |
||
328 | //up_write(&devices_subsys.rwsem); |
||
329 | |||
330 | /* Notify the platform of the removal, in case they |
||
331 | * need to do anything... |
||
332 | */ |
||
333 | if (platform_notify_remove) |
||
334 | platform_notify_remove(dev); |
||
335 | bus_remove_device(dev); |
||
336 | //device_pm_remove(dev); |
||
337 | kobject_del(&dev->kobj); |
||
338 | if (parent) |
||
339 | put_device(parent); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * device_unregister - unregister device from system. |
||
344 | * @dev: device going away. |
||
345 | * |
||
346 | * We do this in two parts, like we do device_register(). First, |
||
347 | * we remove it from all the subsystems with device_del(), then |
||
348 | * we decrement the reference count via put_device(). If that |
||
349 | * is the final reference count, the device will be cleaned up |
||
350 | * via device_release() above. Otherwise, the structure will |
||
351 | * stick around until the final reference to the device is dropped. |
||
352 | */ |
||
353 | void device_unregister(struct device * dev) |
||
354 | { |
||
355 | pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id); |
||
356 | device_del(dev); |
||
357 | put_device(dev); |
||
358 | } |
||
359 | |||
360 | |||
361 | /** |
||
362 | * device_unregister_wait - Unregister device and wait for it to be freed. |
||
363 | * @dev: Device to unregister. |
||
364 | * |
||
365 | * For the cases where the caller needs to wait for all references to |
||
366 | * be dropped from the device before continuing (e.g. modules with |
||
367 | * statically allocated devices), this function uses a completion struct |
||
368 | * to wait, along with a matching complete() in device_release() above. |
||
369 | */ |
||
370 | |||
371 | void device_unregister_wait(struct device * dev) |
||
372 | { |
||
373 | struct completion c; |
||
374 | init_completion(&c); |
||
375 | dev->complete = &c; |
||
376 | device_unregister(dev); |
||
377 | //wait_for_completion(&c); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * device_for_each_child - device child iterator. |
||
382 | * @dev: parent struct device. |
||
383 | * @data: data for the callback. |
||
384 | * @fn: function to be called for each device. |
||
385 | * |
||
386 | * Iterate over @dev's child devices, and call @fn for each, |
||
387 | * passing it @data. |
||
388 | * |
||
389 | * We check the return of @fn each time. If it returns anything |
||
390 | * other than 0, we break out and return that value. |
||
391 | */ |
||
392 | int device_for_each_child(struct device * dev, void * data, |
||
393 | int (*fn)(struct device *, void *)) |
||
394 | { |
||
395 | struct device * child; |
||
396 | int error = 0; |
||
397 | |||
398 | //down_read(&devices_subsys.rwsem); |
||
399 | list_for_each_entry(child,&dev->children,node) { |
||
400 | if((error = fn(child,data))) |
||
401 | break; |
||
402 | } |
||
403 | //up_read(&devices_subsys.rwsem); |
||
404 | return error; |
||
405 | } |
||
406 | |||
407 | int __init devices_init(void) |
||
408 | { |
||
409 | return subsystem_register(&devices_subsys); |
||
410 | } |
||
411 | |||
412 | EXPORT_SYMBOL(device_for_each_child); |
||
413 | |||
414 | EXPORT_SYMBOL(device_initialize); |
||
415 | EXPORT_SYMBOL(device_add); |
||
416 | EXPORT_SYMBOL(device_register); |
||
417 | |||
418 | EXPORT_SYMBOL(device_del); |
||
419 | EXPORT_SYMBOL(device_unregister); |
||
420 | EXPORT_SYMBOL(device_unregister_wait); |
||
421 | EXPORT_SYMBOL(get_device); |
||
422 | EXPORT_SYMBOL(put_device); |
||
423 | |||
424 | EXPORT_SYMBOL(device_create_file); |
||
425 | EXPORT_SYMBOL(device_remove_file); |