PM: platform_bus and late_suspend/early_resume
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / base / core.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
11#include <linux/device.h>
12#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/string.h>
23681e47 17#include <linux/kdev_t.h>
1da177e4
LT
18
19#include <asm/semaphore.h>
20
21#include "base.h"
22#include "power/power.h"
23
24int (*platform_notify)(struct device * dev) = NULL;
25int (*platform_notify_remove)(struct device * dev) = NULL;
26
27/*
28 * sysfs bindings for devices.
29 */
30
3e95637a
AS
31/**
32 * dev_driver_string - Return a device's driver name, if at all possible
33 * @dev: struct device to get the name of
34 *
35 * Will return the device's driver's name if it is bound to a device. If
36 * the device is not bound to a device, it will return the name of the bus
37 * it is attached to. If it is not attached to a bus either, an empty
38 * string will be returned.
39 */
40const char *dev_driver_string(struct device *dev)
41{
42 return dev->driver ? dev->driver->name :
43 (dev->bus ? dev->bus->name : "");
44}
45EXPORT_SYMBOL_GPL(dev_driver_string);
46
1da177e4
LT
47#define to_dev(obj) container_of(obj, struct device, kobj)
48#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
49
1da177e4
LT
50static ssize_t
51dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
52{
53 struct device_attribute * dev_attr = to_dev_attr(attr);
54 struct device * dev = to_dev(kobj);
4a0c20bf 55 ssize_t ret = -EIO;
1da177e4
LT
56
57 if (dev_attr->show)
54b6f35c 58 ret = dev_attr->show(dev, dev_attr, buf);
1da177e4
LT
59 return ret;
60}
61
62static ssize_t
63dev_attr_store(struct kobject * kobj, struct attribute * attr,
64 const char * buf, size_t count)
65{
66 struct device_attribute * dev_attr = to_dev_attr(attr);
67 struct device * dev = to_dev(kobj);
4a0c20bf 68 ssize_t ret = -EIO;
1da177e4
LT
69
70 if (dev_attr->store)
54b6f35c 71 ret = dev_attr->store(dev, dev_attr, buf, count);
1da177e4
LT
72 return ret;
73}
74
75static struct sysfs_ops dev_sysfs_ops = {
76 .show = dev_attr_show,
77 .store = dev_attr_store,
78};
79
80
81/**
82 * device_release - free device structure.
83 * @kobj: device's kobject.
84 *
85 * This is called once the reference count for the object
86 * reaches 0. We forward the call to the device's release
87 * method, which should handle actually freeing the structure.
88 */
89static void device_release(struct kobject * kobj)
90{
91 struct device * dev = to_dev(kobj);
92
93 if (dev->release)
94 dev->release(dev);
95 else {
96 printk(KERN_ERR "Device '%s' does not have a release() function, "
97 "it is broken and must be fixed.\n",
98 dev->bus_id);
99 WARN_ON(1);
100 }
101}
102
103static struct kobj_type ktype_device = {
104 .release = device_release,
105 .sysfs_ops = &dev_sysfs_ops,
1da177e4
LT
106};
107
108
312c004d 109static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4
LT
110{
111 struct kobj_type *ktype = get_ktype(kobj);
112
113 if (ktype == &ktype_device) {
114 struct device *dev = to_dev(kobj);
115 if (dev->bus)
116 return 1;
23681e47
GKH
117 if (dev->class)
118 return 1;
1da177e4
LT
119 }
120 return 0;
121}
122
312c004d 123static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1da177e4
LT
124{
125 struct device *dev = to_dev(kobj);
126
23681e47
GKH
127 if (dev->bus)
128 return dev->bus->name;
129 if (dev->class)
130 return dev->class->name;
131 return NULL;
1da177e4
LT
132}
133
312c004d 134static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
1da177e4
LT
135 int num_envp, char *buffer, int buffer_size)
136{
137 struct device *dev = to_dev(kobj);
138 int i = 0;
139 int length = 0;
140 int retval = 0;
141
23681e47
GKH
142 /* add the major/minor if present */
143 if (MAJOR(dev->devt)) {
144 add_uevent_var(envp, num_envp, &i,
145 buffer, buffer_size, &length,
146 "MAJOR=%u", MAJOR(dev->devt));
147 add_uevent_var(envp, num_envp, &i,
148 buffer, buffer_size, &length,
149 "MINOR=%u", MINOR(dev->devt));
150 }
151
d81d9d6b 152 /* add bus name (same as SUBSYSTEM, deprecated) */
1da177e4 153 if (dev->bus)
312c004d
KS
154 add_uevent_var(envp, num_envp, &i,
155 buffer, buffer_size, &length,
156 "PHYSDEVBUS=%s", dev->bus->name);
1da177e4 157
d81d9d6b
KS
158 /* add driver name (PHYSDEV* values are deprecated)*/
159 if (dev->driver) {
160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "DRIVER=%s", dev->driver->name);
312c004d
KS
163 add_uevent_var(envp, num_envp, &i,
164 buffer, buffer_size, &length,
165 "PHYSDEVDRIVER=%s", dev->driver->name);
d81d9d6b 166 }
1da177e4
LT
167
168 /* terminate, set to next free slot, shrink available space */
169 envp[i] = NULL;
170 envp = &envp[i];
171 num_envp -= i;
172 buffer = &buffer[length];
173 buffer_size -= length;
174
312c004d 175 if (dev->bus && dev->bus->uevent) {
1da177e4 176 /* have the bus specific function add its stuff */
312c004d 177 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
1da177e4 178 if (retval) {
312c004d 179 pr_debug ("%s - uevent() returned %d\n",
1da177e4
LT
180 __FUNCTION__, retval);
181 }
182 }
183
184 return retval;
185}
186
312c004d
KS
187static struct kset_uevent_ops device_uevent_ops = {
188 .filter = dev_uevent_filter,
189 .name = dev_uevent_name,
190 .uevent = dev_uevent,
1da177e4
LT
191};
192
a7fd6706
KS
193static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
194 const char *buf, size_t count)
195{
312c004d 196 kobject_uevent(&dev->kobj, KOBJ_ADD);
a7fd6706
KS
197 return count;
198}
199
23681e47
GKH
200static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
201 char *buf)
202{
203 return print_dev_t(buf, dev->devt);
204}
205
0863afb3
MW
206/*
207 * devices_subsys - structure to be registered with kobject core.
1da177e4
LT
208 */
209
312c004d 210decl_subsys(devices, &ktype_device, &device_uevent_ops);
1da177e4
LT
211
212
213/**
214 * device_create_file - create sysfs attribute file for device.
215 * @dev: device.
216 * @attr: device attribute descriptor.
217 */
218
219int device_create_file(struct device * dev, struct device_attribute * attr)
220{
221 int error = 0;
222 if (get_device(dev)) {
223 error = sysfs_create_file(&dev->kobj, &attr->attr);
224 put_device(dev);
225 }
226 return error;
227}
228
229/**
230 * device_remove_file - remove sysfs attribute file.
231 * @dev: device.
232 * @attr: device attribute descriptor.
233 */
234
235void device_remove_file(struct device * dev, struct device_attribute * attr)
236{
237 if (get_device(dev)) {
238 sysfs_remove_file(&dev->kobj, &attr->attr);
239 put_device(dev);
240 }
241}
242
34bb61f9
JB
243static void klist_children_get(struct klist_node *n)
244{
245 struct device *dev = container_of(n, struct device, knode_parent);
246
247 get_device(dev);
248}
249
250static void klist_children_put(struct klist_node *n)
251{
252 struct device *dev = container_of(n, struct device, knode_parent);
253
254 put_device(dev);
255}
256
1da177e4
LT
257
258/**
259 * device_initialize - init device structure.
260 * @dev: device.
261 *
262 * This prepares the device for use by other layers,
263 * including adding it to the device hierarchy.
264 * It is the first half of device_register(), if called by
265 * that, though it can also be called separately, so one
266 * may use @dev's fields (e.g. the refcount).
267 */
268
269void device_initialize(struct device *dev)
270{
271 kobj_set_kset_s(dev, devices_subsys);
272 kobject_init(&dev->kobj);
34bb61f9
JB
273 klist_init(&dev->klist_children, klist_children_get,
274 klist_children_put);
1da177e4 275 INIT_LIST_HEAD(&dev->dma_pools);
23681e47 276 INIT_LIST_HEAD(&dev->node);
af70316a 277 init_MUTEX(&dev->sem);
0ac85241 278 device_init_wakeup(dev, 0);
1da177e4
LT
279}
280
281/**
282 * device_add - add device to device hierarchy.
283 * @dev: device.
284 *
285 * This is part 2 of device_register(), though may be called
286 * separately _iff_ device_initialize() has been called separately.
287 *
288 * This adds it to the kobject hierarchy via kobject_add(), adds it
289 * to the global and sibling lists for the device, then
290 * adds it to the other relevant subsystems of the driver model.
291 */
292int device_add(struct device *dev)
293{
294 struct device *parent = NULL;
e9a7d305 295 char *class_name = NULL;
1da177e4
LT
296 int error = -EINVAL;
297
298 dev = get_device(dev);
299 if (!dev || !strlen(dev->bus_id))
300 goto Error;
301
302 parent = get_device(dev->parent);
303
304 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
305
306 /* first, register with generic layer. */
307 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
308 if (parent)
309 dev->kobj.parent = &parent->kobj;
310
311 if ((error = kobject_add(&dev->kobj)))
312 goto Error;
a7fd6706
KS
313
314 dev->uevent_attr.attr.name = "uevent";
315 dev->uevent_attr.attr.mode = S_IWUSR;
316 if (dev->driver)
317 dev->uevent_attr.attr.owner = dev->driver->owner;
318 dev->uevent_attr.store = store_uevent;
319 device_create_file(dev, &dev->uevent_attr);
320
23681e47
GKH
321 if (MAJOR(dev->devt)) {
322 struct device_attribute *attr;
323 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
324 if (!attr) {
325 error = -ENOMEM;
326 goto PMError;
327 }
328 attr->attr.name = "dev";
329 attr->attr.mode = S_IRUGO;
330 if (dev->driver)
331 attr->attr.owner = dev->driver->owner;
332 attr->show = show_dev;
333 error = device_create_file(dev, attr);
334 if (error) {
335 kfree(attr);
336 goto attrError;
337 }
338
339 dev->devt_attr = attr;
340 }
341
b9d9c82b
KS
342 if (dev->class) {
343 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
344 "subsystem");
23681e47
GKH
345 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
346 dev->bus_id);
e9a7d305
GKH
347
348 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
349 class_name = make_class_name(dev->class->name, &dev->kobj);
350 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
b9d9c82b 351 }
23681e47 352
1da177e4
LT
353 if ((error = device_pm_add(dev)))
354 goto PMError;
355 if ((error = bus_add_device(dev)))
356 goto BusError;
53877d06
KS
357 kobject_uevent(&dev->kobj, KOBJ_ADD);
358 bus_attach_device(dev);
1da177e4 359 if (parent)
d856f1e3 360 klist_add_tail(&dev->knode_parent, &parent->klist_children);
1da177e4 361
5d9fd169
GKH
362 if (dev->class) {
363 /* tie the class to the device */
364 down(&dev->class->sem);
365 list_add_tail(&dev->node, &dev->class->devices);
366 up(&dev->class->sem);
367 }
368
1da177e4
LT
369 /* notify platform of device entry */
370 if (platform_notify)
371 platform_notify(dev);
372 Done:
e9a7d305 373 kfree(class_name);
1da177e4
LT
374 put_device(dev);
375 return error;
376 BusError:
377 device_pm_remove(dev);
378 PMError:
23681e47
GKH
379 if (dev->devt_attr) {
380 device_remove_file(dev, dev->devt_attr);
381 kfree(dev->devt_attr);
382 }
383 attrError:
312c004d 384 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
385 kobject_del(&dev->kobj);
386 Error:
387 if (parent)
388 put_device(parent);
389 goto Done;
390}
391
392
393/**
394 * device_register - register a device with the system.
395 * @dev: pointer to the device structure
396 *
397 * This happens in two clean steps - initialize the device
398 * and add it to the system. The two steps can be called
399 * separately, but this is the easiest and most common.
400 * I.e. you should only call the two helpers separately if
401 * have a clearly defined need to use and refcount the device
402 * before it is added to the hierarchy.
403 */
404
405int device_register(struct device *dev)
406{
407 device_initialize(dev);
408 return device_add(dev);
409}
410
411
412/**
413 * get_device - increment reference count for device.
414 * @dev: device.
415 *
416 * This simply forwards the call to kobject_get(), though
417 * we do take care to provide for the case that we get a NULL
418 * pointer passed in.
419 */
420
421struct device * get_device(struct device * dev)
422{
423 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
424}
425
426
427/**
428 * put_device - decrement reference count.
429 * @dev: device in question.
430 */
431void put_device(struct device * dev)
432{
433 if (dev)
434 kobject_put(&dev->kobj);
435}
436
437
438/**
439 * device_del - delete device from system.
440 * @dev: device.
441 *
442 * This is the first part of the device unregistration
443 * sequence. This removes the device from the lists we control
444 * from here, has it removed from the other driver model
445 * subsystems it was added to in device_add(), and removes it
446 * from the kobject hierarchy.
447 *
448 * NOTE: this should be called manually _iff_ device_add() was
449 * also called manually.
450 */
451
452void device_del(struct device * dev)
453{
454 struct device * parent = dev->parent;
e9a7d305 455 char *class_name = NULL;
1da177e4 456
1da177e4 457 if (parent)
d62c0f9f 458 klist_del(&dev->knode_parent);
23681e47
GKH
459 if (dev->devt_attr)
460 device_remove_file(dev, dev->devt_attr);
b9d9c82b
KS
461 if (dev->class) {
462 sysfs_remove_link(&dev->kobj, "subsystem");
23681e47 463 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
e9a7d305
GKH
464 class_name = make_class_name(dev->class->name, &dev->kobj);
465 sysfs_remove_link(&dev->kobj, "device");
466 sysfs_remove_link(&dev->parent->kobj, class_name);
467 kfree(class_name);
5d9fd169
GKH
468 down(&dev->class->sem);
469 list_del_init(&dev->node);
470 up(&dev->class->sem);
b9d9c82b 471 }
a7fd6706 472 device_remove_file(dev, &dev->uevent_attr);
1da177e4
LT
473
474 /* Notify the platform of the removal, in case they
475 * need to do anything...
476 */
477 if (platform_notify_remove)
478 platform_notify_remove(dev);
479 bus_remove_device(dev);
480 device_pm_remove(dev);
312c004d 481 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1da177e4
LT
482 kobject_del(&dev->kobj);
483 if (parent)
484 put_device(parent);
485}
486
487/**
488 * device_unregister - unregister device from system.
489 * @dev: device going away.
490 *
491 * We do this in two parts, like we do device_register(). First,
492 * we remove it from all the subsystems with device_del(), then
493 * we decrement the reference count via put_device(). If that
494 * is the final reference count, the device will be cleaned up
495 * via device_release() above. Otherwise, the structure will
496 * stick around until the final reference to the device is dropped.
497 */
498void device_unregister(struct device * dev)
499{
500 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
501 device_del(dev);
502 put_device(dev);
503}
504
505
36239577
PM
506static struct device * next_device(struct klist_iter * i)
507{
508 struct klist_node * n = klist_next(i);
509 return n ? container_of(n, struct device, knode_parent) : NULL;
510}
511
1da177e4
LT
512/**
513 * device_for_each_child - device child iterator.
c41455fb 514 * @parent: parent struct device.
1da177e4
LT
515 * @data: data for the callback.
516 * @fn: function to be called for each device.
517 *
c41455fb 518 * Iterate over @parent's child devices, and call @fn for each,
1da177e4
LT
519 * passing it @data.
520 *
521 * We check the return of @fn each time. If it returns anything
522 * other than 0, we break out and return that value.
523 */
36239577 524int device_for_each_child(struct device * parent, void * data,
1da177e4
LT
525 int (*fn)(struct device *, void *))
526{
36239577 527 struct klist_iter i;
1da177e4
LT
528 struct device * child;
529 int error = 0;
530
36239577
PM
531 klist_iter_init(&parent->klist_children, &i);
532 while ((child = next_device(&i)) && !error)
533 error = fn(child, data);
534 klist_iter_exit(&i);
1da177e4
LT
535 return error;
536}
537
1da177e4
LT
538int __init devices_init(void)
539{
540 return subsystem_register(&devices_subsys);
541}
542
543EXPORT_SYMBOL_GPL(device_for_each_child);
544
545EXPORT_SYMBOL_GPL(device_initialize);
546EXPORT_SYMBOL_GPL(device_add);
547EXPORT_SYMBOL_GPL(device_register);
548
549EXPORT_SYMBOL_GPL(device_del);
550EXPORT_SYMBOL_GPL(device_unregister);
551EXPORT_SYMBOL_GPL(get_device);
552EXPORT_SYMBOL_GPL(put_device);
1da177e4
LT
553
554EXPORT_SYMBOL_GPL(device_create_file);
555EXPORT_SYMBOL_GPL(device_remove_file);
23681e47
GKH
556
557
558static void device_create_release(struct device *dev)
559{
560 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
561 kfree(dev);
562}
563
564/**
565 * device_create - creates a device and registers it with sysfs
42734daf
HK
566 * @class: pointer to the struct class that this device should be registered to
567 * @parent: pointer to the parent struct device of this new device, if any
568 * @devt: the dev_t for the char device to be added
569 * @fmt: string for the device's name
570 *
571 * This function can be used by char device classes. A struct device
572 * will be created in sysfs, registered to the specified class.
23681e47 573 *
23681e47
GKH
574 * A "dev" file will be created, showing the dev_t for the device, if
575 * the dev_t is not 0,0.
42734daf
HK
576 * If a pointer to a parent struct device is passed in, the newly created
577 * struct device will be a child of that device in sysfs.
578 * The pointer to the struct device will be returned from the call.
579 * Any further sysfs files that might be required can be created using this
23681e47
GKH
580 * pointer.
581 *
582 * Note: the struct class passed to this function must have previously
583 * been created with a call to class_create().
584 */
585struct device *device_create(struct class *class, struct device *parent,
5cbe5f8a 586 dev_t devt, const char *fmt, ...)
23681e47
GKH
587{
588 va_list args;
589 struct device *dev = NULL;
590 int retval = -ENODEV;
591
592 if (class == NULL || IS_ERR(class))
593 goto error;
594 if (parent == NULL) {
595 printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
596 goto error;
597 }
598
599 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
600 if (!dev) {
601 retval = -ENOMEM;
602 goto error;
603 }
604
605 dev->devt = devt;
606 dev->class = class;
607 dev->parent = parent;
608 dev->release = device_create_release;
609
610 va_start(args, fmt);
611 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
612 va_end(args);
613 retval = device_register(dev);
614 if (retval)
615 goto error;
616
23681e47
GKH
617 return dev;
618
619error:
620 kfree(dev);
621 return ERR_PTR(retval);
622}
623EXPORT_SYMBOL_GPL(device_create);
624
625/**
626 * device_destroy - removes a device that was created with device_create()
42734daf
HK
627 * @class: pointer to the struct class that this device was registered with
628 * @devt: the dev_t of the device that was previously registered
23681e47 629 *
42734daf
HK
630 * This call unregisters and cleans up a device that was created with a
631 * call to device_create().
23681e47
GKH
632 */
633void device_destroy(struct class *class, dev_t devt)
634{
635 struct device *dev = NULL;
636 struct device *dev_tmp;
637
638 down(&class->sem);
639 list_for_each_entry(dev_tmp, &class->devices, node) {
640 if (dev_tmp->devt == devt) {
641 dev = dev_tmp;
642 break;
643 }
644 }
645 up(&class->sem);
646
5d9fd169 647 if (dev)
23681e47 648 device_unregister(dev);
23681e47
GKH
649}
650EXPORT_SYMBOL_GPL(device_destroy);