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