blob: c34a4d8842e12c1fb911c405b290637876a6ce92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070019#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100020#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/semaphore.h>
23
24#include "base.h"
25#include "power/power.h"
26
27int (*platform_notify)(struct device * dev) = NULL;
28int (*platform_notify_remove)(struct device * dev) = NULL;
29
30/*
31 * sysfs bindings for devices.
32 */
33
Alan Stern3e956372006-06-16 17:10:48 -040034/**
35 * dev_driver_string - Return a device's driver name, if at all possible
36 * @dev: struct device to get the name of
37 *
38 * Will return the device's driver's name if it is bound to a device. If
39 * the device is not bound to a device, it will return the name of the bus
40 * it is attached to. If it is not attached to a bus either, an empty
41 * string will be returned.
42 */
43const char *dev_driver_string(struct device *dev)
44{
45 return dev->driver ? dev->driver->name :
Jean Delvarea456b702007-03-09 16:33:10 +010046 (dev->bus ? dev->bus->name :
47 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040048}
Matthew Wilcox310a9222006-09-23 23:35:04 -060049EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define to_dev(obj) container_of(obj, struct device, kobj)
52#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static ssize_t
55dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
56{
57 struct device_attribute * dev_attr = to_dev_attr(attr);
58 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050059 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040062 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return ret;
64}
65
66static ssize_t
67dev_attr_store(struct kobject * kobj, struct attribute * attr,
68 const char * buf, size_t count)
69{
70 struct device_attribute * dev_attr = to_dev_attr(attr);
71 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050072 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040075 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return ret;
77}
78
79static struct sysfs_ops dev_sysfs_ops = {
80 .show = dev_attr_show,
81 .store = dev_attr_store,
82};
83
84
85/**
86 * device_release - free device structure.
87 * @kobj: device's kobject.
88 *
89 * This is called once the reference count for the object
90 * reaches 0. We forward the call to the device's release
91 * method, which should handle actually freeing the structure.
92 */
93static void device_release(struct kobject * kobj)
94{
95 struct device * dev = to_dev(kobj);
96
97 if (dev->release)
98 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +020099 else if (dev->type && dev->type->release)
100 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700101 else if (dev->class && dev->class->dev_release)
102 dev->class->dev_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 else {
104 printk(KERN_ERR "Device '%s' does not have a release() function, "
105 "it is broken and must be fixed.\n",
106 dev->bus_id);
107 WARN_ON(1);
108 }
109}
110
111static struct kobj_type ktype_device = {
112 .release = device_release,
113 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116
Kay Sievers312c0042005-11-16 09:00:00 +0100117static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct kobj_type *ktype = get_ktype(kobj);
120
121 if (ktype == &ktype_device) {
122 struct device *dev = to_dev(kobj);
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200123 if (dev->uevent_suppress)
124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (dev->bus)
126 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700127 if (dev->class)
128 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130 return 0;
131}
132
Kay Sievers312c0042005-11-16 09:00:00 +0100133static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct device *dev = to_dev(kobj);
136
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700137 if (dev->bus)
138 return dev->bus->name;
139 if (dev->class)
140 return dev->class->name;
141 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Kay Sievers312c0042005-11-16 09:00:00 +0100144static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int num_envp, char *buffer, int buffer_size)
146{
147 struct device *dev = to_dev(kobj);
148 int i = 0;
149 int length = 0;
150 int retval = 0;
151
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700152 /* add the major/minor if present */
153 if (MAJOR(dev->devt)) {
154 add_uevent_var(envp, num_envp, &i,
155 buffer, buffer_size, &length,
156 "MAJOR=%u", MAJOR(dev->devt));
157 add_uevent_var(envp, num_envp, &i,
158 buffer, buffer_size, &length,
159 "MINOR=%u", MINOR(dev->devt));
160 }
161
Kay Sievers414264f2007-03-12 21:08:57 +0100162 if (dev->type && dev->type->name)
163 add_uevent_var(envp, num_envp, &i,
164 buffer, buffer_size, &length,
165 "DEVTYPE=%s", dev->type->name);
166
Kay Sievers239378f2006-10-07 21:54:55 +0200167 if (dev->driver)
Kay Sieversd81d9d62006-08-13 06:17:09 +0200168 add_uevent_var(envp, num_envp, &i,
169 buffer, buffer_size, &length,
170 "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200171
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200172#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200173 if (dev->class) {
174 struct device *parent = dev->parent;
175
176 /* find first bus device in parent chain */
177 while (parent && !parent->bus)
178 parent = parent->parent;
179 if (parent && parent->bus) {
180 const char *path;
181
182 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
183 add_uevent_var(envp, num_envp, &i,
184 buffer, buffer_size, &length,
185 "PHYSDEVPATH=%s", path);
186 kfree(path);
187
188 add_uevent_var(envp, num_envp, &i,
189 buffer, buffer_size, &length,
190 "PHYSDEVBUS=%s", parent->bus->name);
191
192 if (parent->driver)
193 add_uevent_var(envp, num_envp, &i,
194 buffer, buffer_size, &length,
195 "PHYSDEVDRIVER=%s", parent->driver->name);
196 }
197 } else if (dev->bus) {
Kay Sievers312c0042005-11-16 09:00:00 +0100198 add_uevent_var(envp, num_envp, &i,
199 buffer, buffer_size, &length,
Kay Sievers239378f2006-10-07 21:54:55 +0200200 "PHYSDEVBUS=%s", dev->bus->name);
201
202 if (dev->driver)
203 add_uevent_var(envp, num_envp, &i,
204 buffer, buffer_size, &length,
205 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200206 }
Kay Sievers239378f2006-10-07 21:54:55 +0200207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* terminate, set to next free slot, shrink available space */
210 envp[i] = NULL;
211 envp = &envp[i];
212 num_envp -= i;
213 buffer = &buffer[length];
214 buffer_size -= length;
215
Kay Sievers312c0042005-11-16 09:00:00 +0100216 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100218 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200219 if (retval)
220 pr_debug ("%s: bus uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700224 if (dev->class && dev->class->dev_uevent) {
225 /* have the class specific function add its stuff */
226 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200227 if (retval)
228 pr_debug("%s: class uevent() returned %d\n",
229 __FUNCTION__, retval);
230 }
231
232 if (dev->type && dev->type->uevent) {
233 /* have the device type specific fuction add its stuff */
234 retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
235 if (retval)
236 pr_debug("%s: dev_type uevent() returned %d\n",
237 __FUNCTION__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700238 }
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return retval;
241}
242
Kay Sievers312c0042005-11-16 09:00:00 +0100243static struct kset_uevent_ops device_uevent_ops = {
244 .filter = dev_uevent_filter,
245 .name = dev_uevent_name,
246 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247};
248
Kay Sieversa7fd6702005-10-01 14:49:43 +0200249static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
250 const char *buf, size_t count)
251{
Kay Sievers312c0042005-11-16 09:00:00 +0100252 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200253 return count;
254}
255
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500256static int device_add_attributes(struct device *dev,
257 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700258{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700259 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500260 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700261
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500262 if (attrs) {
263 for (i = 0; attr_name(attrs[i]); i++) {
264 error = device_create_file(dev, &attrs[i]);
265 if (error)
266 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700267 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500268 if (error)
269 while (--i >= 0)
270 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700271 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700272 return error;
273}
274
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500275static void device_remove_attributes(struct device *dev,
276 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700277{
278 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500279
280 if (attrs)
281 for (i = 0; attr_name(attrs[i]); i++)
282 device_remove_file(dev, &attrs[i]);
283}
284
285static int device_add_groups(struct device *dev,
286 struct attribute_group **groups)
287{
288 int error = 0;
289 int i;
290
291 if (groups) {
292 for (i = 0; groups[i]; i++) {
293 error = sysfs_create_group(&dev->kobj, groups[i]);
294 if (error) {
295 while (--i >= 0)
296 sysfs_remove_group(&dev->kobj, groups[i]);
297 break;
298 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700299 }
300 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500301 return error;
302}
303
304static void device_remove_groups(struct device *dev,
305 struct attribute_group **groups)
306{
307 int i;
308
309 if (groups)
310 for (i = 0; groups[i]; i++)
311 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700312}
313
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700314static int device_add_attrs(struct device *dev)
315{
316 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200317 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500318 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700319
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500320 if (class) {
321 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200322 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500323 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700324 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200325
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500326 if (type) {
327 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200328 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500329 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200330 }
331
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500332 error = device_add_groups(dev, dev->groups);
333 if (error)
334 goto err_remove_type_groups;
335
336 return 0;
337
338 err_remove_type_groups:
339 if (type)
340 device_remove_groups(dev, type->groups);
341 err_remove_class_attrs:
342 if (class)
343 device_remove_attributes(dev, class->dev_attrs);
344
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700345 return error;
346}
347
348static void device_remove_attrs(struct device *dev)
349{
350 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200351 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700352
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500353 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200354
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500355 if (type)
356 device_remove_groups(dev, type->groups);
357
358 if (class)
359 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700360}
361
362
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700363static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
364 char *buf)
365{
366 return print_dev_t(buf, dev->devt);
367}
368
Martin Waitz0863afb2006-01-09 20:53:55 -0800369/*
370 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
372
Kay Sievers312c0042005-11-16 09:00:00 +0100373decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375
376/**
377 * device_create_file - create sysfs attribute file for device.
378 * @dev: device.
379 * @attr: device attribute descriptor.
380 */
381
382int device_create_file(struct device * dev, struct device_attribute * attr)
383{
384 int error = 0;
385 if (get_device(dev)) {
386 error = sysfs_create_file(&dev->kobj, &attr->attr);
387 put_device(dev);
388 }
389 return error;
390}
391
392/**
393 * device_remove_file - remove sysfs attribute file.
394 * @dev: device.
395 * @attr: device attribute descriptor.
396 */
397
398void device_remove_file(struct device * dev, struct device_attribute * attr)
399{
400 if (get_device(dev)) {
401 sysfs_remove_file(&dev->kobj, &attr->attr);
402 put_device(dev);
403 }
404}
405
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700406/**
407 * device_create_bin_file - create sysfs binary attribute file for device.
408 * @dev: device.
409 * @attr: device binary attribute descriptor.
410 */
411int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
412{
413 int error = -EINVAL;
414 if (dev)
415 error = sysfs_create_bin_file(&dev->kobj, attr);
416 return error;
417}
418EXPORT_SYMBOL_GPL(device_create_bin_file);
419
420/**
421 * device_remove_bin_file - remove sysfs binary attribute file
422 * @dev: device.
423 * @attr: device binary attribute descriptor.
424 */
425void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
426{
427 if (dev)
428 sysfs_remove_bin_file(&dev->kobj, attr);
429}
430EXPORT_SYMBOL_GPL(device_remove_bin_file);
431
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400432/**
433 * device_schedule_callback - helper to schedule a callback for a device
434 * @dev: device.
435 * @func: callback function to invoke later.
436 *
437 * Attribute methods must not unregister themselves or their parent device
438 * (which would amount to the same thing). Attempts to do so will deadlock,
439 * since unregistration is mutually exclusive with driver callbacks.
440 *
441 * Instead methods can call this routine, which will attempt to allocate
442 * and schedule a workqueue request to call back @func with @dev as its
443 * argument in the workqueue's process context. @dev will be pinned until
444 * @func returns.
445 *
446 * Returns 0 if the request was submitted, -ENOMEM if storage could not
447 * be allocated.
448 *
449 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
450 * underlying sysfs routine (since it is intended for use by attribute
451 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
452 */
453int device_schedule_callback(struct device *dev,
454 void (*func)(struct device *))
455{
456 return sysfs_schedule_callback(&dev->kobj,
457 (void (*)(void *)) func, dev);
458}
459EXPORT_SYMBOL_GPL(device_schedule_callback);
460
James Bottomley34bb61f2005-09-06 16:56:51 -0700461static void klist_children_get(struct klist_node *n)
462{
463 struct device *dev = container_of(n, struct device, knode_parent);
464
465 get_device(dev);
466}
467
468static void klist_children_put(struct klist_node *n)
469{
470 struct device *dev = container_of(n, struct device, knode_parent);
471
472 put_device(dev);
473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476/**
477 * device_initialize - init device structure.
478 * @dev: device.
479 *
480 * This prepares the device for use by other layers,
481 * including adding it to the device hierarchy.
482 * It is the first half of device_register(), if called by
483 * that, though it can also be called separately, so one
484 * may use @dev's fields (e.g. the refcount).
485 */
486
487void device_initialize(struct device *dev)
488{
489 kobj_set_kset_s(dev, devices_subsys);
490 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700491 klist_init(&dev->klist_children, klist_children_get,
492 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700494 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800495 init_MUTEX(&dev->sem);
Tejun Heo9ac78492007-01-20 16:00:26 +0900496 spin_lock_init(&dev->devres_lock);
497 INIT_LIST_HEAD(&dev->devres_head);
David Brownell0ac85242005-09-12 19:39:34 -0700498 device_init_wakeup(dev, 0);
Christoph Hellwig87348132006-12-06 20:32:33 -0800499 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100502#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huckc744aea2007-01-08 20:16:44 +0100503static struct kobject * get_device_parent(struct device *dev,
504 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100505{
506 /* Set the parent to the class, not the parent device */
507 /* this keeps sysfs from having a symlink to make old udevs happy */
508 if (dev->class)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100509 return &dev->class->subsys.kset.kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100510 else if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100511 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100512
Cornelia Huckc744aea2007-01-08 20:16:44 +0100513 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100514}
515#else
Kay Sievers86406242007-03-14 03:25:56 +0100516static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700517{
Kay Sievers86406242007-03-14 03:25:56 +0100518 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700519
Kay Sievers86406242007-03-14 03:25:56 +0100520 if (!virtual_dir)
521 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700522
Kay Sievers86406242007-03-14 03:25:56 +0100523 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700524}
525
Cornelia Huckc744aea2007-01-08 20:16:44 +0100526static struct kobject * get_device_parent(struct device *dev,
527 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100528{
Kay Sievers86406242007-03-14 03:25:56 +0100529 if (dev->class) {
530 struct kobject *kobj = NULL;
531 struct kobject *parent_kobj;
532 struct kobject *k;
533
534 /*
535 * If we have no parent, we live in "virtual".
536 * Class-devices with a bus-device as parent, live
537 * in a class-directory to prevent namespace collisions.
538 */
539 if (parent == NULL)
540 parent_kobj = virtual_device_parent(dev);
541 else if (parent->class)
542 return &parent->kobj;
543 else
544 parent_kobj = &parent->kobj;
545
546 /* find our class-directory at the parent and reference it */
547 spin_lock(&dev->class->class_dirs.list_lock);
548 list_for_each_entry(k, &dev->class->class_dirs.list, entry)
549 if (k->parent == parent_kobj) {
550 kobj = kobject_get(k);
551 break;
552 }
553 spin_unlock(&dev->class->class_dirs.list_lock);
554 if (kobj)
555 return kobj;
556
557 /* or create a new class-directory at the parent device */
558 return kobject_kset_add_dir(&dev->class->class_dirs,
559 parent_kobj, dev->class->name);
560 }
561
562 if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100563 return &parent->kobj;
564 return NULL;
565}
Cornelia Huckc744aea2007-01-08 20:16:44 +0100566#endif
Kay Sievers86406242007-03-14 03:25:56 +0100567
Cornelia Huckc744aea2007-01-08 20:16:44 +0100568static int setup_parent(struct device *dev, struct device *parent)
569{
570 struct kobject *kobj;
571 kobj = get_device_parent(dev, parent);
572 if (IS_ERR(kobj))
573 return PTR_ERR(kobj);
574 if (kobj)
575 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100576 return 0;
577}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/**
580 * device_add - add device to device hierarchy.
581 * @dev: device.
582 *
583 * This is part 2 of device_register(), though may be called
584 * separately _iff_ device_initialize() has been called separately.
585 *
586 * This adds it to the kobject hierarchy via kobject_add(), adds it
587 * to the global and sibling lists for the device, then
588 * adds it to the other relevant subsystems of the driver model.
589 */
590int device_add(struct device *dev)
591{
592 struct device *parent = NULL;
Greg Kroah-Hartmane9a7d302006-06-20 13:59:20 -0700593 char *class_name = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200594 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int error = -EINVAL;
596
597 dev = get_device(dev);
598 if (!dev || !strlen(dev->bus_id))
599 goto Error;
600
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100601 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 parent = get_device(dev->parent);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100604 error = setup_parent(dev, parent);
605 if (error)
606 goto Error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* first, register with generic layer. */
609 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100610 error = kobject_add(&dev->kobj);
611 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200613
Brian Walsh37022642006-08-14 22:43:19 -0700614 /* notify platform of device entry */
615 if (platform_notify)
616 platform_notify(dev);
617
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000618 /* notify clients of device entry (new way) */
619 if (dev->bus)
620 blocking_notifier_call_chain(&dev->bus->bus_notifier,
621 BUS_NOTIFY_ADD_DEVICE, dev);
622
Kay Sieversa7fd6702005-10-01 14:49:43 +0200623 dev->uevent_attr.attr.name = "uevent";
624 dev->uevent_attr.attr.mode = S_IWUSR;
625 if (dev->driver)
626 dev->uevent_attr.attr.owner = dev->driver->owner;
627 dev->uevent_attr.store = store_uevent;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200628 error = device_create_file(dev, &dev->uevent_attr);
629 if (error)
630 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200631
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700632 if (MAJOR(dev->devt)) {
633 struct device_attribute *attr;
634 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
635 if (!attr) {
636 error = -ENOMEM;
Cornelia Hucka306eea2006-09-22 11:37:13 +0200637 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700638 }
639 attr->attr.name = "dev";
640 attr->attr.mode = S_IRUGO;
641 if (dev->driver)
642 attr->attr.owner = dev->driver->owner;
643 attr->show = show_dev;
644 error = device_create_file(dev, attr);
645 if (error) {
646 kfree(attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200647 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700648 }
649
650 dev->devt_attr = attr;
651 }
652
Kay Sieversb9d9c822006-06-15 15:31:56 +0200653 if (dev->class) {
654 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
655 "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100656 /* If this is not a "fake" compatible device, then create the
657 * symlink from the class to the device. */
658 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
659 sysfs_create_link(&dev->class->subsys.kset.kobj,
660 &dev->kobj, dev->bus_id);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700661 if (parent) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100662 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
663 "device");
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800664#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100665 class_name = make_class_name(dev->class->name,
666 &dev->kobj);
667 if (class_name)
668 sysfs_create_link(&dev->parent->kobj,
669 &dev->kobj, class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200670#endif
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800671 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200672 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700673
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700674 if ((error = device_add_attrs(dev)))
675 goto AttrsError;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if ((error = device_pm_add(dev)))
677 goto PMError;
678 if ((error = bus_add_device(dev)))
679 goto BusError;
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200680 kobject_uevent(&dev->kobj, KOBJ_ADD);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800681 bus_attach_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400683 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700685 if (dev->class) {
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700686 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200687 /* tie the class to the device */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700688 list_add_tail(&dev->node, &dev->class->devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200689
690 /* notify any interfaces that the device is here */
691 list_for_each_entry(class_intf, &dev->class->interfaces, node)
692 if (class_intf->add_dev)
693 class_intf->add_dev(dev, class_intf);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700694 up(&dev->class->sem);
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 Done:
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500697 kfree(class_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 put_device(dev);
699 return error;
700 BusError:
701 device_pm_remove(dev);
702 PMError:
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000703 if (dev->bus)
704 blocking_notifier_call_chain(&dev->bus->bus_notifier,
705 BUS_NOTIFY_DEL_DEVICE, dev);
James Simmons82f0cf92007-02-21 17:44:51 +0000706 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700707 AttrsError:
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700708 if (dev->devt_attr) {
709 device_remove_file(dev, dev->devt_attr);
710 kfree(dev->devt_attr);
711 }
James Simmons82f0cf92007-02-21 17:44:51 +0000712
713 if (dev->class) {
714 sysfs_remove_link(&dev->kobj, "subsystem");
715 /* If this is not a "fake" compatible device, remove the
716 * symlink from the class to the device. */
717 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
718 sysfs_remove_link(&dev->class->subsys.kset.kobj,
719 dev->bus_id);
James Simmons82f0cf92007-02-21 17:44:51 +0000720 if (parent) {
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800721#ifdef CONFIG_SYSFS_DEPRECATED
James Simmons82f0cf92007-02-21 17:44:51 +0000722 char *class_name = make_class_name(dev->class->name,
723 &dev->kobj);
724 if (class_name)
725 sysfs_remove_link(&dev->parent->kobj,
726 class_name);
727 kfree(class_name);
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800728#endif
James Simmons82f0cf92007-02-21 17:44:51 +0000729 sysfs_remove_link(&dev->kobj, "device");
730 }
James Simmons82f0cf92007-02-21 17:44:51 +0000731 }
Cornelia Hucka306eea2006-09-22 11:37:13 +0200732 ueventattrError:
733 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700734 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100735 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 kobject_del(&dev->kobj);
737 Error:
738 if (parent)
739 put_device(parent);
740 goto Done;
741}
742
743
744/**
745 * device_register - register a device with the system.
746 * @dev: pointer to the device structure
747 *
748 * This happens in two clean steps - initialize the device
749 * and add it to the system. The two steps can be called
750 * separately, but this is the easiest and most common.
751 * I.e. you should only call the two helpers separately if
752 * have a clearly defined need to use and refcount the device
753 * before it is added to the hierarchy.
754 */
755
756int device_register(struct device *dev)
757{
758 device_initialize(dev);
759 return device_add(dev);
760}
761
762
763/**
764 * get_device - increment reference count for device.
765 * @dev: device.
766 *
767 * This simply forwards the call to kobject_get(), though
768 * we do take care to provide for the case that we get a NULL
769 * pointer passed in.
770 */
771
772struct device * get_device(struct device * dev)
773{
774 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
775}
776
777
778/**
779 * put_device - decrement reference count.
780 * @dev: device in question.
781 */
782void put_device(struct device * dev)
783{
784 if (dev)
785 kobject_put(&dev->kobj);
786}
787
788
789/**
790 * device_del - delete device from system.
791 * @dev: device.
792 *
793 * This is the first part of the device unregistration
794 * sequence. This removes the device from the lists we control
795 * from here, has it removed from the other driver model
796 * subsystems it was added to in device_add(), and removes it
797 * from the kobject hierarchy.
798 *
799 * NOTE: this should be called manually _iff_ device_add() was
800 * also called manually.
801 */
802
803void device_del(struct device * dev)
804{
805 struct device * parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200806 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700809 klist_del(&dev->knode_parent);
Catalin Marinas82189b92006-11-25 11:09:30 -0800810 if (dev->devt_attr) {
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700811 device_remove_file(dev, dev->devt_attr);
Catalin Marinas82189b92006-11-25 11:09:30 -0800812 kfree(dev->devt_attr);
813 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200814 if (dev->class) {
815 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100816 /* If this is not a "fake" compatible device, remove the
817 * symlink from the class to the device. */
818 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
819 sysfs_remove_link(&dev->class->subsys.kset.kobj,
820 dev->bus_id);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700821 if (parent) {
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800822#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200823 char *class_name = make_class_name(dev->class->name,
824 &dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100825 if (class_name)
826 sysfs_remove_link(&dev->parent->kobj,
827 class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200828 kfree(class_name);
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800829#endif
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200830 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700831 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200832
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700833 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200834 /* notify any interfaces that the device is now gone */
835 list_for_each_entry(class_intf, &dev->class->interfaces, node)
836 if (class_intf->remove_dev)
837 class_intf->remove_dev(dev, class_intf);
838 /* remove the device from the class list */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700839 list_del_init(&dev->node);
840 up(&dev->class->sem);
Kay Sievers86406242007-03-14 03:25:56 +0100841
842 /* If we live in a parent class-directory, unreference it */
843 if (dev->kobj.parent->kset == &dev->class->class_dirs) {
844 struct device *d;
845 int other = 0;
846
847 /*
848 * if we are the last child of our class, delete
849 * our class-directory at this parent
850 */
851 down(&dev->class->sem);
852 list_for_each_entry(d, &dev->class->devices, node) {
853 if (d == dev)
854 continue;
855 if (d->kobj.parent == dev->kobj.parent) {
856 other = 1;
857 break;
858 }
859 }
860 if (!other)
861 kobject_del(dev->kobj.parent);
862
863 kobject_put(dev->kobj.parent);
864 up(&dev->class->sem);
865 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200866 }
Kay Sieversa7fd6702005-10-01 14:49:43 +0200867 device_remove_file(dev, &dev->uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700868 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -0800869 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Tejun Heo2f8d16a2007-03-09 19:34:19 +0900871 /*
872 * Some platform devices are driven without driver attached
873 * and managed resources may have been acquired. Make sure
874 * all resources are released.
875 */
876 devres_release_all(dev);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* Notify the platform of the removal, in case they
879 * need to do anything...
880 */
881 if (platform_notify_remove)
882 platform_notify_remove(dev);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000883 if (dev->bus)
884 blocking_notifier_call_chain(&dev->bus->bus_notifier,
885 BUS_NOTIFY_DEL_DEVICE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +0100887 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 kobject_del(&dev->kobj);
889 if (parent)
890 put_device(parent);
891}
892
893/**
894 * device_unregister - unregister device from system.
895 * @dev: device going away.
896 *
897 * We do this in two parts, like we do device_register(). First,
898 * we remove it from all the subsystems with device_del(), then
899 * we decrement the reference count via put_device(). If that
900 * is the final reference count, the device will be cleaned up
901 * via device_release() above. Otherwise, the structure will
902 * stick around until the final reference to the device is dropped.
903 */
904void device_unregister(struct device * dev)
905{
906 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
907 device_del(dev);
908 put_device(dev);
909}
910
911
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800912static struct device * next_device(struct klist_iter * i)
913{
914 struct klist_node * n = klist_next(i);
915 return n ? container_of(n, struct device, knode_parent) : NULL;
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/**
919 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -0700920 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * @data: data for the callback.
922 * @fn: function to be called for each device.
923 *
Randy Dunlapc41455f2005-10-23 11:59:14 -0700924 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * passing it @data.
926 *
927 * We check the return of @fn each time. If it returns anything
928 * other than 0, we break out and return that value.
929 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800930int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int (*fn)(struct device *, void *))
932{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800933 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 struct device * child;
935 int error = 0;
936
mochel@digitalimplant.org36239572005-03-24 19:08:30 -0800937 klist_iter_init(&parent->klist_children, &i);
938 while ((child = next_device(&i)) && !error)
939 error = fn(child, data);
940 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return error;
942}
943
Cornelia Huck5ab69982006-11-16 15:42:07 +0100944/**
945 * device_find_child - device iterator for locating a particular device.
946 * @parent: parent struct device
947 * @data: Data to pass to match function
948 * @match: Callback function to check device
949 *
950 * This is similar to the device_for_each_child() function above, but it
951 * returns a reference to a device that is 'found' for later use, as
952 * determined by the @match callback.
953 *
954 * The callback should return 0 if the device doesn't match and non-zero
955 * if it does. If the callback returns non-zero and a reference to the
956 * current device can be obtained, this function will return to the caller
957 * and not iterate over any more devices.
958 */
959struct device * device_find_child(struct device *parent, void *data,
960 int (*match)(struct device *, void *))
961{
962 struct klist_iter i;
963 struct device *child;
964
965 if (!parent)
966 return NULL;
967
968 klist_iter_init(&parent->klist_children, &i);
969 while ((child = next_device(&i)))
970 if (match(child, data) && get_device(child))
971 break;
972 klist_iter_exit(&i);
973 return child;
974}
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976int __init devices_init(void)
977{
978 return subsystem_register(&devices_subsys);
979}
980
981EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +0100982EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984EXPORT_SYMBOL_GPL(device_initialize);
985EXPORT_SYMBOL_GPL(device_add);
986EXPORT_SYMBOL_GPL(device_register);
987
988EXPORT_SYMBOL_GPL(device_del);
989EXPORT_SYMBOL_GPL(device_unregister);
990EXPORT_SYMBOL_GPL(get_device);
991EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993EXPORT_SYMBOL_GPL(device_create_file);
994EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700995
996
997static void device_create_release(struct device *dev)
998{
999 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
1000 kfree(dev);
1001}
1002
1003/**
1004 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001005 * @class: pointer to the struct class that this device should be registered to
1006 * @parent: pointer to the parent struct device of this new device, if any
1007 * @devt: the dev_t for the char device to be added
1008 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001009 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001010 * This function can be used by char device classes. A struct device
1011 * will be created in sysfs, registered to the specified class.
1012 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001013 * A "dev" file will be created, showing the dev_t for the device, if
1014 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001015 * If a pointer to a parent struct device is passed in, the newly created
1016 * struct device will be a child of that device in sysfs.
1017 * The pointer to the struct device will be returned from the call.
1018 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001019 * pointer.
1020 *
1021 * Note: the struct class passed to this function must have previously
1022 * been created with a call to class_create().
1023 */
1024struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -04001025 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001026{
1027 va_list args;
1028 struct device *dev = NULL;
1029 int retval = -ENODEV;
1030
1031 if (class == NULL || IS_ERR(class))
1032 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001033
1034 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1035 if (!dev) {
1036 retval = -ENOMEM;
1037 goto error;
1038 }
1039
1040 dev->devt = devt;
1041 dev->class = class;
1042 dev->parent = parent;
1043 dev->release = device_create_release;
1044
1045 va_start(args, fmt);
1046 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
1047 va_end(args);
1048 retval = device_register(dev);
1049 if (retval)
1050 goto error;
1051
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001052 return dev;
1053
1054error:
1055 kfree(dev);
1056 return ERR_PTR(retval);
1057}
1058EXPORT_SYMBOL_GPL(device_create);
1059
1060/**
1061 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001062 * @class: pointer to the struct class that this device was registered with
1063 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001064 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001065 * This call unregisters and cleans up a device that was created with a
1066 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001067 */
1068void device_destroy(struct class *class, dev_t devt)
1069{
1070 struct device *dev = NULL;
1071 struct device *dev_tmp;
1072
1073 down(&class->sem);
1074 list_for_each_entry(dev_tmp, &class->devices, node) {
1075 if (dev_tmp->devt == devt) {
1076 dev = dev_tmp;
1077 break;
1078 }
1079 }
1080 up(&class->sem);
1081
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001082 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001083 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001084}
1085EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001086
1087/**
1088 * device_rename - renames a device
1089 * @dev: the pointer to the struct device to be renamed
1090 * @new_name: the new name of the device
1091 */
1092int device_rename(struct device *dev, char *new_name)
1093{
1094 char *old_class_name = NULL;
1095 char *new_class_name = NULL;
1096 char *old_symlink_name = NULL;
1097 int error;
1098
1099 dev = get_device(dev);
1100 if (!dev)
1101 return -EINVAL;
1102
1103 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
1104
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001105#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001106 if ((dev->class) && (dev->parent))
1107 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001108#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001109
1110 if (dev->class) {
1111 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
Jesper Juhl952ab432006-09-28 23:56:01 +02001112 if (!old_symlink_name) {
1113 error = -ENOMEM;
1114 goto out_free_old_class;
1115 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001116 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
1117 }
1118
1119 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1120
1121 error = kobject_rename(&dev->kobj, new_name);
1122
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001123#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001124 if (old_class_name) {
1125 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1126 if (new_class_name) {
1127 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
1128 new_class_name);
1129 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1130 }
1131 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001132#endif
1133
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001134 if (dev->class) {
1135 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1136 old_symlink_name);
1137 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1138 dev->bus_id);
1139 }
1140 put_device(dev);
1141
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001142 kfree(new_class_name);
1143 kfree(old_symlink_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001144 out_free_old_class:
1145 kfree(old_class_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001146
1147 return error;
1148}
Johannes Berga2807db2007-02-28 12:38:31 +01001149EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001150
1151static int device_move_class_links(struct device *dev,
1152 struct device *old_parent,
1153 struct device *new_parent)
1154{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001155 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001156#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huck8a824722006-11-20 17:07:51 +01001157 char *class_name;
1158
1159 class_name = make_class_name(dev->class->name, &dev->kobj);
1160 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001161 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001162 goto out;
1163 }
1164 if (old_parent) {
1165 sysfs_remove_link(&dev->kobj, "device");
1166 sysfs_remove_link(&old_parent->kobj, class_name);
1167 }
Cornelia Huckc744aea2007-01-08 20:16:44 +01001168 if (new_parent) {
1169 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1170 "device");
1171 if (error)
1172 goto out;
1173 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1174 class_name);
1175 if (error)
1176 sysfs_remove_link(&dev->kobj, "device");
1177 }
1178 else
1179 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001180out:
1181 kfree(class_name);
1182 return error;
1183#else
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001184 if (old_parent)
1185 sysfs_remove_link(&dev->kobj, "device");
1186 if (new_parent)
1187 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1188 "device");
1189 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001190#endif
1191}
1192
1193/**
1194 * device_move - moves a device to a new parent
1195 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aea2007-01-08 20:16:44 +01001196 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +01001197 */
1198int device_move(struct device *dev, struct device *new_parent)
1199{
1200 int error;
1201 struct device *old_parent;
Cornelia Huckc744aea2007-01-08 20:16:44 +01001202 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001203
1204 dev = get_device(dev);
1205 if (!dev)
1206 return -EINVAL;
1207
Cornelia Huck8a824722006-11-20 17:07:51 +01001208 new_parent = get_device(new_parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +01001209 new_parent_kobj = get_device_parent (dev, new_parent);
1210 if (IS_ERR(new_parent_kobj)) {
1211 error = PTR_ERR(new_parent_kobj);
1212 put_device(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001213 goto out;
1214 }
1215 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
Cornelia Huckc744aea2007-01-08 20:16:44 +01001216 new_parent ? new_parent->bus_id : "<NULL>");
1217 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001218 if (error) {
1219 put_device(new_parent);
1220 goto out;
1221 }
1222 old_parent = dev->parent;
1223 dev->parent = new_parent;
1224 if (old_parent)
Cornelia Huckacf02d22006-11-22 17:49:39 +01001225 klist_remove(&dev->knode_parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +01001226 if (new_parent)
1227 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
Cornelia Huck8a824722006-11-20 17:07:51 +01001228 if (!dev->class)
1229 goto out_put;
1230 error = device_move_class_links(dev, old_parent, new_parent);
1231 if (error) {
1232 /* We ignore errors on cleanup since we're hosed anyway... */
1233 device_move_class_links(dev, new_parent, old_parent);
1234 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +01001235 if (new_parent)
1236 klist_remove(&dev->knode_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001237 if (old_parent)
1238 klist_add_tail(&dev->knode_parent,
1239 &old_parent->klist_children);
1240 }
1241 put_device(new_parent);
1242 goto out;
1243 }
1244out_put:
1245 put_device(old_parent);
1246out:
1247 put_device(dev);
1248 return error;
1249}
1250
1251EXPORT_SYMBOL_GPL(device_move);