blob: 3599ab2506d269ee299b1b8e807746bcbca98391 [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
Kay Sievers60a96a52007-07-08 22:29:26 +020027extern const char *kobject_actions[];
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029int (*platform_notify)(struct device * dev) = NULL;
30int (*platform_notify_remove)(struct device * dev) = NULL;
31
32/*
33 * sysfs bindings for devices.
34 */
35
Alan Stern3e956372006-06-16 17:10:48 -040036/**
37 * dev_driver_string - Return a device's driver name, if at all possible
38 * @dev: struct device to get the name of
39 *
40 * Will return the device's driver's name if it is bound to a device. If
41 * the device is not bound to a device, it will return the name of the bus
42 * it is attached to. If it is not attached to a bus either, an empty
43 * string will be returned.
44 */
45const char *dev_driver_string(struct device *dev)
46{
47 return dev->driver ? dev->driver->name :
Jean Delvarea456b702007-03-09 16:33:10 +010048 (dev->bus ? dev->bus->name :
49 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -040050}
Matthew Wilcox310a9222006-09-23 23:35:04 -060051EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define to_dev(obj) container_of(obj, struct device, kobj)
54#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static ssize_t
57dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
58{
59 struct device_attribute * dev_attr = to_dev_attr(attr);
60 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050061 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -040064 ret = dev_attr->show(dev, dev_attr, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return ret;
66}
67
68static ssize_t
69dev_attr_store(struct kobject * kobj, struct attribute * attr,
70 const char * buf, size_t count)
71{
72 struct device_attribute * dev_attr = to_dev_attr(attr);
73 struct device * dev = to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050074 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -040077 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return ret;
79}
80
81static struct sysfs_ops dev_sysfs_ops = {
82 .show = dev_attr_show,
83 .store = dev_attr_store,
84};
85
86
87/**
88 * device_release - free device structure.
89 * @kobj: device's kobject.
90 *
91 * This is called once the reference count for the object
92 * reaches 0. We forward the call to the device's release
93 * method, which should handle actually freeing the structure.
94 */
95static void device_release(struct kobject * kobj)
96{
97 struct device * dev = to_dev(kobj);
98
99 if (dev->release)
100 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200101 else if (dev->type && dev->type->release)
102 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700103 else if (dev->class && dev->class->dev_release)
104 dev->class->dev_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 else {
106 printk(KERN_ERR "Device '%s' does not have a release() function, "
107 "it is broken and must be fixed.\n",
108 dev->bus_id);
109 WARN_ON(1);
110 }
111}
112
113static struct kobj_type ktype_device = {
114 .release = device_release,
115 .sysfs_ops = &dev_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118
Kay Sievers312c0042005-11-16 09:00:00 +0100119static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct kobj_type *ktype = get_ktype(kobj);
122
123 if (ktype == &ktype_device) {
124 struct device *dev = to_dev(kobj);
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200125 if (dev->uevent_suppress)
126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (dev->bus)
128 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700129 if (dev->class)
130 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132 return 0;
133}
134
Kay Sievers312c0042005-11-16 09:00:00 +0100135static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct device *dev = to_dev(kobj);
138
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700139 if (dev->bus)
140 return dev->bus->name;
141 if (dev->class)
142 return dev->class->name;
143 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Kay Sievers312c0042005-11-16 09:00:00 +0100146static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int num_envp, char *buffer, int buffer_size)
148{
149 struct device *dev = to_dev(kobj);
150 int i = 0;
151 int length = 0;
152 int retval = 0;
153
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700154 /* add the major/minor if present */
155 if (MAJOR(dev->devt)) {
156 add_uevent_var(envp, num_envp, &i,
157 buffer, buffer_size, &length,
158 "MAJOR=%u", MAJOR(dev->devt));
159 add_uevent_var(envp, num_envp, &i,
160 buffer, buffer_size, &length,
161 "MINOR=%u", MINOR(dev->devt));
162 }
163
Kay Sievers414264f2007-03-12 21:08:57 +0100164 if (dev->type && dev->type->name)
165 add_uevent_var(envp, num_envp, &i,
166 buffer, buffer_size, &length,
167 "DEVTYPE=%s", dev->type->name);
168
Kay Sievers239378f2006-10-07 21:54:55 +0200169 if (dev->driver)
Kay Sieversd81d9d62006-08-13 06:17:09 +0200170 add_uevent_var(envp, num_envp, &i,
171 buffer, buffer_size, &length,
172 "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200173
Kay Sieversa87cb2a2006-09-14 11:23:28 +0200174#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers239378f2006-10-07 21:54:55 +0200175 if (dev->class) {
176 struct device *parent = dev->parent;
177
178 /* find first bus device in parent chain */
179 while (parent && !parent->bus)
180 parent = parent->parent;
181 if (parent && parent->bus) {
182 const char *path;
183
184 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
Kay Sievers2c7afd12007-05-01 13:46:26 +0200185 if (path) {
186 add_uevent_var(envp, num_envp, &i,
187 buffer, buffer_size, &length,
188 "PHYSDEVPATH=%s", path);
189 kfree(path);
190 }
Kay Sievers239378f2006-10-07 21:54:55 +0200191
192 add_uevent_var(envp, num_envp, &i,
193 buffer, buffer_size, &length,
194 "PHYSDEVBUS=%s", parent->bus->name);
195
196 if (parent->driver)
197 add_uevent_var(envp, num_envp, &i,
198 buffer, buffer_size, &length,
199 "PHYSDEVDRIVER=%s", parent->driver->name);
200 }
201 } else if (dev->bus) {
Kay Sievers312c0042005-11-16 09:00:00 +0100202 add_uevent_var(envp, num_envp, &i,
203 buffer, buffer_size, &length,
Kay Sievers239378f2006-10-07 21:54:55 +0200204 "PHYSDEVBUS=%s", dev->bus->name);
205
206 if (dev->driver)
207 add_uevent_var(envp, num_envp, &i,
208 buffer, buffer_size, &length,
209 "PHYSDEVDRIVER=%s", dev->driver->name);
Kay Sieversd81d9d62006-08-13 06:17:09 +0200210 }
Kay Sievers239378f2006-10-07 21:54:55 +0200211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* terminate, set to next free slot, shrink available space */
214 envp[i] = NULL;
215 envp = &envp[i];
216 num_envp -= i;
217 buffer = &buffer[length];
218 buffer_size -= length;
219
Kay Sievers312c0042005-11-16 09:00:00 +0100220 if (dev->bus && dev->bus->uevent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100222 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200223 if (retval)
224 pr_debug ("%s: bus uevent() returned %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700228 if (dev->class && dev->class->dev_uevent) {
229 /* have the class specific function add its stuff */
230 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200231 if (retval)
232 pr_debug("%s: class uevent() returned %d\n",
233 __FUNCTION__, retval);
234 }
235
236 if (dev->type && dev->type->uevent) {
237 /* have the device type specific fuction add its stuff */
238 retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
239 if (retval)
240 pr_debug("%s: dev_type uevent() returned %d\n",
241 __FUNCTION__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return retval;
245}
246
Kay Sievers312c0042005-11-16 09:00:00 +0100247static struct kset_uevent_ops device_uevent_ops = {
248 .filter = dev_uevent_filter,
249 .name = dev_uevent_name,
250 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};
252
Kay Sievers16574dc2007-04-06 01:40:38 +0200253static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
254 char *buf)
255{
256 struct kobject *top_kobj;
257 struct kset *kset;
258 char *envp[32];
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200259 char *data = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200260 char *pos;
261 int i;
262 size_t count = 0;
263 int retval;
264
265 /* search the kset, the device belongs to */
266 top_kobj = &dev->kobj;
267 if (!top_kobj->kset && top_kobj->parent) {
268 do {
269 top_kobj = top_kobj->parent;
270 } while (!top_kobj->kset && top_kobj->parent);
271 }
272 if (!top_kobj->kset)
273 goto out;
274 kset = top_kobj->kset;
275 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
276 goto out;
277
278 /* respect filter */
279 if (kset->uevent_ops && kset->uevent_ops->filter)
280 if (!kset->uevent_ops->filter(kset, &dev->kobj))
281 goto out;
282
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200283 data = (char *)get_zeroed_page(GFP_KERNEL);
284 if (!data)
285 return -ENOMEM;
286
Kay Sievers16574dc2007-04-06 01:40:38 +0200287 /* let the kset specific function add its keys */
288 pos = data;
289 retval = kset->uevent_ops->uevent(kset, &dev->kobj,
290 envp, ARRAY_SIZE(envp),
291 pos, PAGE_SIZE);
292 if (retval)
293 goto out;
294
295 /* copy keys to file */
296 for (i = 0; envp[i]; i++) {
297 pos = &buf[count];
298 count += sprintf(pos, "%s\n", envp[i]);
299 }
300out:
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200301 free_page((unsigned long)data);
Kay Sievers16574dc2007-04-06 01:40:38 +0200302 return count;
303}
304
Kay Sieversa7fd6702005-10-01 14:49:43 +0200305static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
306 const char *buf, size_t count)
307{
Kay Sievers60a96a52007-07-08 22:29:26 +0200308 size_t len = count;
309 enum kobject_action action;
310
311 if (len && buf[len-1] == '\n')
312 len--;
313
314 for (action = 0; action < KOBJ_MAX; action++) {
315 if (strncmp(kobject_actions[action], buf, len) != 0)
316 continue;
317 if (kobject_actions[action][len] != '\0')
318 continue;
319 kobject_uevent(&dev->kobj, action);
320 goto out;
321 }
322
323 dev_err(dev, "uevent: unsupported action-string; this will "
324 "be ignored in a future kernel version\n");
Kay Sievers312c0042005-11-16 09:00:00 +0100325 kobject_uevent(&dev->kobj, KOBJ_ADD);
Kay Sievers60a96a52007-07-08 22:29:26 +0200326out:
Kay Sieversa7fd6702005-10-01 14:49:43 +0200327 return count;
328}
329
Tejun Heoad6a1e12007-06-14 03:45:17 +0900330static struct device_attribute uevent_attr =
331 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
332
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500333static int device_add_attributes(struct device *dev,
334 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700335{
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700336 int error = 0;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500337 int i;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700338
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500339 if (attrs) {
340 for (i = 0; attr_name(attrs[i]); i++) {
341 error = device_create_file(dev, &attrs[i]);
342 if (error)
343 break;
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700344 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500345 if (error)
346 while (--i >= 0)
347 device_remove_file(dev, &attrs[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700348 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700349 return error;
350}
351
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500352static void device_remove_attributes(struct device *dev,
353 struct device_attribute *attrs)
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700354{
355 int i;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500356
357 if (attrs)
358 for (i = 0; attr_name(attrs[i]); i++)
359 device_remove_file(dev, &attrs[i]);
360}
361
362static int device_add_groups(struct device *dev,
363 struct attribute_group **groups)
364{
365 int error = 0;
366 int i;
367
368 if (groups) {
369 for (i = 0; groups[i]; i++) {
370 error = sysfs_create_group(&dev->kobj, groups[i]);
371 if (error) {
372 while (--i >= 0)
373 sysfs_remove_group(&dev->kobj, groups[i]);
374 break;
375 }
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700376 }
377 }
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500378 return error;
379}
380
381static void device_remove_groups(struct device *dev,
382 struct attribute_group **groups)
383{
384 int i;
385
386 if (groups)
387 for (i = 0; groups[i]; i++)
388 sysfs_remove_group(&dev->kobj, groups[i]);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -0700389}
390
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700391static int device_add_attrs(struct device *dev)
392{
393 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200394 struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500395 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700396
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500397 if (class) {
398 error = device_add_attributes(dev, class->dev_attrs);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200399 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500400 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700401 }
Kay Sieversf9f852d2006-10-07 21:54:55 +0200402
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500403 if (type) {
404 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200405 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500406 goto err_remove_class_attrs;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200407 }
408
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500409 error = device_add_groups(dev, dev->groups);
410 if (error)
411 goto err_remove_type_groups;
412
413 return 0;
414
415 err_remove_type_groups:
416 if (type)
417 device_remove_groups(dev, type->groups);
418 err_remove_class_attrs:
419 if (class)
420 device_remove_attributes(dev, class->dev_attrs);
421
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700422 return error;
423}
424
425static void device_remove_attrs(struct device *dev)
426{
427 struct class *class = dev->class;
Kay Sieversf9f852d2006-10-07 21:54:55 +0200428 struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700429
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500430 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200431
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500432 if (type)
433 device_remove_groups(dev, type->groups);
434
435 if (class)
436 device_remove_attributes(dev, class->dev_attrs);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700437}
438
439
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700440static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
441 char *buf)
442{
443 return print_dev_t(buf, dev->devt);
444}
445
Tejun Heoad6a1e12007-06-14 03:45:17 +0900446static struct device_attribute devt_attr =
447 __ATTR(dev, S_IRUGO, show_dev, NULL);
448
Martin Waitz0863afb2006-01-09 20:53:55 -0800449/*
450 * devices_subsys - structure to be registered with kobject core.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
452
Kay Sievers312c0042005-11-16 09:00:00 +0100453decl_subsys(devices, &ktype_device, &device_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455
456/**
457 * device_create_file - create sysfs attribute file for device.
458 * @dev: device.
459 * @attr: device attribute descriptor.
460 */
461
462int device_create_file(struct device * dev, struct device_attribute * attr)
463{
464 int error = 0;
465 if (get_device(dev)) {
466 error = sysfs_create_file(&dev->kobj, &attr->attr);
467 put_device(dev);
468 }
469 return error;
470}
471
472/**
473 * device_remove_file - remove sysfs attribute file.
474 * @dev: device.
475 * @attr: device attribute descriptor.
476 */
477
478void device_remove_file(struct device * dev, struct device_attribute * attr)
479{
480 if (get_device(dev)) {
481 sysfs_remove_file(&dev->kobj, &attr->attr);
482 put_device(dev);
483 }
484}
485
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -0700486/**
487 * device_create_bin_file - create sysfs binary attribute file for device.
488 * @dev: device.
489 * @attr: device binary attribute descriptor.
490 */
491int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
492{
493 int error = -EINVAL;
494 if (dev)
495 error = sysfs_create_bin_file(&dev->kobj, attr);
496 return error;
497}
498EXPORT_SYMBOL_GPL(device_create_bin_file);
499
500/**
501 * device_remove_bin_file - remove sysfs binary attribute file
502 * @dev: device.
503 * @attr: device binary attribute descriptor.
504 */
505void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
506{
507 if (dev)
508 sysfs_remove_bin_file(&dev->kobj, attr);
509}
510EXPORT_SYMBOL_GPL(device_remove_bin_file);
511
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400512/**
Alan Stern523ded72007-04-26 00:12:04 -0700513 * device_schedule_callback_owner - helper to schedule a callback for a device
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400514 * @dev: device.
515 * @func: callback function to invoke later.
Alan Stern523ded72007-04-26 00:12:04 -0700516 * @owner: module owning the callback routine
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400517 *
518 * Attribute methods must not unregister themselves or their parent device
519 * (which would amount to the same thing). Attempts to do so will deadlock,
520 * since unregistration is mutually exclusive with driver callbacks.
521 *
522 * Instead methods can call this routine, which will attempt to allocate
523 * and schedule a workqueue request to call back @func with @dev as its
524 * argument in the workqueue's process context. @dev will be pinned until
525 * @func returns.
526 *
Alan Stern523ded72007-04-26 00:12:04 -0700527 * This routine is usually called via the inline device_schedule_callback(),
528 * which automatically sets @owner to THIS_MODULE.
529 *
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400530 * Returns 0 if the request was submitted, -ENOMEM if storage could not
Alan Stern523ded72007-04-26 00:12:04 -0700531 * be allocated, -ENODEV if a reference to @owner isn't available.
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400532 *
533 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
534 * underlying sysfs routine (since it is intended for use by attribute
535 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
536 */
Alan Stern523ded72007-04-26 00:12:04 -0700537int device_schedule_callback_owner(struct device *dev,
538 void (*func)(struct device *), struct module *owner)
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400539{
540 return sysfs_schedule_callback(&dev->kobj,
Alan Stern523ded72007-04-26 00:12:04 -0700541 (void (*)(void *)) func, dev, owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400542}
Alan Stern523ded72007-04-26 00:12:04 -0700543EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
Alan Sternd9a9cdf2007-03-15 15:50:34 -0400544
James Bottomley34bb61f2005-09-06 16:56:51 -0700545static void klist_children_get(struct klist_node *n)
546{
547 struct device *dev = container_of(n, struct device, knode_parent);
548
549 get_device(dev);
550}
551
552static void klist_children_put(struct klist_node *n)
553{
554 struct device *dev = container_of(n, struct device, knode_parent);
555
556 put_device(dev);
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560/**
561 * device_initialize - init device structure.
562 * @dev: device.
563 *
564 * This prepares the device for use by other layers,
565 * including adding it to the device hierarchy.
566 * It is the first half of device_register(), if called by
567 * that, though it can also be called separately, so one
568 * may use @dev's fields (e.g. the refcount).
569 */
570
571void device_initialize(struct device *dev)
572{
573 kobj_set_kset_s(dev, devices_subsys);
574 kobject_init(&dev->kobj);
James Bottomley34bb61f2005-09-06 16:56:51 -0700575 klist_init(&dev->klist_children, klist_children_get,
576 klist_children_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 INIT_LIST_HEAD(&dev->dma_pools);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700578 INIT_LIST_HEAD(&dev->node);
mochel@digitalimplant.orgaf703162005-03-21 10:41:04 -0800579 init_MUTEX(&dev->sem);
Tejun Heo9ac78492007-01-20 16:00:26 +0900580 spin_lock_init(&dev->devres_lock);
581 INIT_LIST_HEAD(&dev->devres_head);
David Brownell0ac85242005-09-12 19:39:34 -0700582 device_init_wakeup(dev, 0);
Christoph Hellwig87348132006-12-06 20:32:33 -0800583 set_dev_node(dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100586#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huckc744aea2007-01-08 20:16:44 +0100587static struct kobject * get_device_parent(struct device *dev,
588 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100589{
590 /* Set the parent to the class, not the parent device */
591 /* this keeps sysfs from having a symlink to make old udevs happy */
592 if (dev->class)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700593 return &dev->class->subsys.kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100594 else if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100595 return &parent->kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100596
Cornelia Huckc744aea2007-01-08 20:16:44 +0100597 return NULL;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100598}
599#else
Kay Sievers86406242007-03-14 03:25:56 +0100600static struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700601{
Kay Sievers86406242007-03-14 03:25:56 +0100602 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700603
Kay Sievers86406242007-03-14 03:25:56 +0100604 if (!virtual_dir)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700605 virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual");
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700606
Kay Sievers86406242007-03-14 03:25:56 +0100607 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -0700608}
609
Cornelia Huckc744aea2007-01-08 20:16:44 +0100610static struct kobject * get_device_parent(struct device *dev,
611 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100612{
Kay Sievers86406242007-03-14 03:25:56 +0100613 if (dev->class) {
614 struct kobject *kobj = NULL;
615 struct kobject *parent_kobj;
616 struct kobject *k;
617
618 /*
619 * If we have no parent, we live in "virtual".
620 * Class-devices with a bus-device as parent, live
621 * in a class-directory to prevent namespace collisions.
622 */
623 if (parent == NULL)
624 parent_kobj = virtual_device_parent(dev);
625 else if (parent->class)
626 return &parent->kobj;
627 else
628 parent_kobj = &parent->kobj;
629
630 /* find our class-directory at the parent and reference it */
631 spin_lock(&dev->class->class_dirs.list_lock);
632 list_for_each_entry(k, &dev->class->class_dirs.list, entry)
633 if (k->parent == parent_kobj) {
634 kobj = kobject_get(k);
635 break;
636 }
637 spin_unlock(&dev->class->class_dirs.list_lock);
638 if (kobj)
639 return kobj;
640
641 /* or create a new class-directory at the parent device */
642 return kobject_kset_add_dir(&dev->class->class_dirs,
643 parent_kobj, dev->class->name);
644 }
645
646 if (parent)
Cornelia Huckc744aea2007-01-08 20:16:44 +0100647 return &parent->kobj;
648 return NULL;
649}
Cornelia Huckc744aea2007-01-08 20:16:44 +0100650#endif
Kay Sievers86406242007-03-14 03:25:56 +0100651
Cornelia Huckc744aea2007-01-08 20:16:44 +0100652static int setup_parent(struct device *dev, struct device *parent)
653{
654 struct kobject *kobj;
655 kobj = get_device_parent(dev, parent);
656 if (IS_ERR(kobj))
657 return PTR_ERR(kobj);
658 if (kobj)
659 dev->kobj.parent = kobj;
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100660 return 0;
661}
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100662
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700663static int device_add_class_symlinks(struct device *dev)
664{
665 int error;
666
667 if (!dev->class)
668 return 0;
669 error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
670 "subsystem");
671 if (error)
672 goto out;
673 /*
674 * If this is not a "fake" compatible device, then create the
675 * symlink from the class to the device.
676 */
677 if (dev->kobj.parent != &dev->class->subsys.kobj) {
678 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
679 dev->bus_id);
680 if (error)
681 goto out_subsys;
682 }
683 /* only bus-device parents get a "device"-link */
684 if (dev->parent && dev->parent->bus) {
685 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
686 "device");
687 if (error)
688 goto out_busid;
689#ifdef CONFIG_SYSFS_DEPRECATED
690 {
691 char * class_name = make_class_name(dev->class->name,
692 &dev->kobj);
693 if (class_name)
694 error = sysfs_create_link(&dev->parent->kobj,
695 &dev->kobj, class_name);
696 kfree(class_name);
697 if (error)
698 goto out_device;
699 }
700#endif
701 }
702 return 0;
703
704#ifdef CONFIG_SYSFS_DEPRECATED
705out_device:
706 if (dev->parent)
707 sysfs_remove_link(&dev->kobj, "device");
708#endif
709out_busid:
710 if (dev->kobj.parent != &dev->class->subsys.kobj)
711 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
712out_subsys:
713 sysfs_remove_link(&dev->kobj, "subsystem");
714out:
715 return error;
716}
717
718static void device_remove_class_symlinks(struct device *dev)
719{
720 if (!dev->class)
721 return;
722 if (dev->parent) {
723#ifdef CONFIG_SYSFS_DEPRECATED
724 char *class_name;
725
726 class_name = make_class_name(dev->class->name, &dev->kobj);
727 if (class_name) {
728 sysfs_remove_link(&dev->parent->kobj, class_name);
729 kfree(class_name);
730 }
731#endif
732 sysfs_remove_link(&dev->kobj, "device");
733 }
734 if (dev->kobj.parent != &dev->class->subsys.kobj)
735 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
736 sysfs_remove_link(&dev->kobj, "subsystem");
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/**
740 * device_add - add device to device hierarchy.
741 * @dev: device.
742 *
743 * This is part 2 of device_register(), though may be called
744 * separately _iff_ device_initialize() has been called separately.
745 *
746 * This adds it to the kobject hierarchy via kobject_add(), adds it
747 * to the global and sibling lists for the device, then
748 * adds it to the other relevant subsystems of the driver model.
749 */
750int device_add(struct device *dev)
751{
752 struct device *parent = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200753 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int error = -EINVAL;
755
756 dev = get_device(dev);
757 if (!dev || !strlen(dev->bus_id))
758 goto Error;
759
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100760 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 parent = get_device(dev->parent);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100763 error = setup_parent(dev, parent);
764 if (error)
765 goto Error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* first, register with generic layer. */
768 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100769 error = kobject_add(&dev->kobj);
770 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 goto Error;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200772
Brian Walsh37022642006-08-14 22:43:19 -0700773 /* notify platform of device entry */
774 if (platform_notify)
775 platform_notify(dev);
776
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000777 /* notify clients of device entry (new way) */
778 if (dev->bus)
779 blocking_notifier_call_chain(&dev->bus->bus_notifier,
780 BUS_NOTIFY_ADD_DEVICE, dev);
781
Tejun Heoad6a1e12007-06-14 03:45:17 +0900782 error = device_create_file(dev, &uevent_attr);
Cornelia Hucka306eea2006-09-22 11:37:13 +0200783 if (error)
784 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +0200785
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700786 if (MAJOR(dev->devt)) {
Tejun Heoad6a1e12007-06-14 03:45:17 +0900787 error = device_create_file(dev, &devt_attr);
788 if (error)
Cornelia Hucka306eea2006-09-22 11:37:13 +0200789 goto ueventattrError;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700790 }
791
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700792 error = device_add_class_symlinks(dev);
793 if (error)
794 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700795 error = device_add_attrs(dev);
796 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700797 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700798 error = device_pm_add(dev);
799 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 goto PMError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700801 error = bus_add_device(dev);
802 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto BusError;
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +0200804 kobject_uevent(&dev->kobj, KOBJ_ADD);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800805 bus_attach_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (parent)
James Bottomleyd856f1e2005-08-19 09:14:01 -0400807 klist_add_tail(&dev->knode_parent, &parent->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700809 if (dev->class) {
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700810 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200811 /* tie the class to the device */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700812 list_add_tail(&dev->node, &dev->class->devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200813
814 /* notify any interfaces that the device is here */
815 list_for_each_entry(class_intf, &dev->class->interfaces, node)
816 if (class_intf->add_dev)
817 class_intf->add_dev(dev, class_intf);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700818 up(&dev->class->sem);
819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 Done:
821 put_device(dev);
822 return error;
823 BusError:
824 device_pm_remove(dev);
825 PMError:
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000826 if (dev->bus)
827 blocking_notifier_call_chain(&dev->bus->bus_notifier,
828 BUS_NOTIFY_DEL_DEVICE, dev);
James Simmons82f0cf92007-02-21 17:44:51 +0000829 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700830 AttrsError:
Cornelia Huck2ee97caf2007-07-18 01:43:47 -0700831 device_remove_class_symlinks(dev);
832 SymlinkError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900833 if (MAJOR(dev->devt))
834 device_remove_file(dev, &devt_attr);
James Simmons82f0cf92007-02-21 17:44:51 +0000835
836 if (dev->class) {
837 sysfs_remove_link(&dev->kobj, "subsystem");
838 /* If this is not a "fake" compatible device, remove the
839 * symlink from the class to the device. */
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700840 if (dev->kobj.parent != &dev->class->subsys.kobj)
841 sysfs_remove_link(&dev->class->subsys.kobj,
James Simmons82f0cf92007-02-21 17:44:51 +0000842 dev->bus_id);
James Simmons82f0cf92007-02-21 17:44:51 +0000843 if (parent) {
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800844#ifdef CONFIG_SYSFS_DEPRECATED
James Simmons82f0cf92007-02-21 17:44:51 +0000845 char *class_name = make_class_name(dev->class->name,
846 &dev->kobj);
847 if (class_name)
848 sysfs_remove_link(&dev->parent->kobj,
849 class_name);
850 kfree(class_name);
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800851#endif
James Simmons82f0cf92007-02-21 17:44:51 +0000852 sysfs_remove_link(&dev->kobj, "device");
853 }
James Simmons82f0cf92007-02-21 17:44:51 +0000854 }
Cornelia Hucka306eea2006-09-22 11:37:13 +0200855 ueventattrError:
Tejun Heoad6a1e12007-06-14 03:45:17 +0900856 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700857 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +0100858 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 kobject_del(&dev->kobj);
860 Error:
861 if (parent)
862 put_device(parent);
863 goto Done;
864}
865
866
867/**
868 * device_register - register a device with the system.
869 * @dev: pointer to the device structure
870 *
871 * This happens in two clean steps - initialize the device
872 * and add it to the system. The two steps can be called
873 * separately, but this is the easiest and most common.
874 * I.e. you should only call the two helpers separately if
875 * have a clearly defined need to use and refcount the device
876 * before it is added to the hierarchy.
877 */
878
879int device_register(struct device *dev)
880{
881 device_initialize(dev);
882 return device_add(dev);
883}
884
885
886/**
887 * get_device - increment reference count for device.
888 * @dev: device.
889 *
890 * This simply forwards the call to kobject_get(), though
891 * we do take care to provide for the case that we get a NULL
892 * pointer passed in.
893 */
894
895struct device * get_device(struct device * dev)
896{
897 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
898}
899
900
901/**
902 * put_device - decrement reference count.
903 * @dev: device in question.
904 */
905void put_device(struct device * dev)
906{
907 if (dev)
908 kobject_put(&dev->kobj);
909}
910
911
912/**
913 * device_del - delete device from system.
914 * @dev: device.
915 *
916 * This is the first part of the device unregistration
917 * sequence. This removes the device from the lists we control
918 * from here, has it removed from the other driver model
919 * subsystems it was added to in device_add(), and removes it
920 * from the kobject hierarchy.
921 *
922 * NOTE: this should be called manually _iff_ device_add() was
923 * also called manually.
924 */
925
926void device_del(struct device * dev)
927{
928 struct device * parent = dev->parent;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200929 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (parent)
Patrick Mocheld62c0f92005-06-24 08:39:33 -0700932 klist_del(&dev->knode_parent);
Tejun Heoad6a1e12007-06-14 03:45:17 +0900933 if (MAJOR(dev->devt))
934 device_remove_file(dev, &devt_attr);
Kay Sieversb9d9c822006-06-15 15:31:56 +0200935 if (dev->class) {
936 sysfs_remove_link(&dev->kobj, "subsystem");
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100937 /* If this is not a "fake" compatible device, remove the
938 * symlink from the class to the device. */
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700939 if (dev->kobj.parent != &dev->class->subsys.kobj)
940 sysfs_remove_link(&dev->class->subsys.kobj,
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +0100941 dev->bus_id);
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700942 if (parent) {
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800943#ifdef CONFIG_SYSFS_DEPRECATED
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200944 char *class_name = make_class_name(dev->class->name,
945 &dev->kobj);
Cornelia Huckcb360bb2006-11-27 10:35:05 +0100946 if (class_name)
947 sysfs_remove_link(&dev->parent->kobj,
948 class_name);
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200949 kfree(class_name);
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -0800950#endif
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200951 sysfs_remove_link(&dev->kobj, "device");
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -0700952 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +0200953
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700954 down(&dev->class->sem);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +0200955 /* notify any interfaces that the device is now gone */
956 list_for_each_entry(class_intf, &dev->class->interfaces, node)
957 if (class_intf->remove_dev)
958 class_intf->remove_dev(dev, class_intf);
959 /* remove the device from the class list */
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -0700960 list_del_init(&dev->node);
961 up(&dev->class->sem);
Kay Sievers86406242007-03-14 03:25:56 +0100962
963 /* If we live in a parent class-directory, unreference it */
964 if (dev->kobj.parent->kset == &dev->class->class_dirs) {
965 struct device *d;
966 int other = 0;
967
968 /*
969 * if we are the last child of our class, delete
970 * our class-directory at this parent
971 */
972 down(&dev->class->sem);
973 list_for_each_entry(d, &dev->class->devices, node) {
974 if (d == dev)
975 continue;
976 if (d->kobj.parent == dev->kobj.parent) {
977 other = 1;
978 break;
979 }
980 }
981 if (!other)
982 kobject_del(dev->kobj.parent);
983
984 kobject_put(dev->kobj.parent);
985 up(&dev->class->sem);
986 }
Kay Sieversb9d9c822006-06-15 15:31:56 +0200987 }
Tejun Heoad6a1e12007-06-14 03:45:17 +0900988 device_remove_file(dev, &uevent_attr);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700989 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -0800990 bus_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Tejun Heo2f8d16a2007-03-09 19:34:19 +0900992 /*
993 * Some platform devices are driven without driver attached
994 * and managed resources may have been acquired. Make sure
995 * all resources are released.
996 */
997 devres_release_all(dev);
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* Notify the platform of the removal, in case they
1000 * need to do anything...
1001 */
1002 if (platform_notify_remove)
1003 platform_notify_remove(dev);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +10001004 if (dev->bus)
1005 blocking_notifier_call_chain(&dev->bus->bus_notifier,
1006 BUS_NOTIFY_DEL_DEVICE, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 device_pm_remove(dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001008 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 kobject_del(&dev->kobj);
1010 if (parent)
1011 put_device(parent);
1012}
1013
1014/**
1015 * device_unregister - unregister device from system.
1016 * @dev: device going away.
1017 *
1018 * We do this in two parts, like we do device_register(). First,
1019 * we remove it from all the subsystems with device_del(), then
1020 * we decrement the reference count via put_device(). If that
1021 * is the final reference count, the device will be cleaned up
1022 * via device_release() above. Otherwise, the structure will
1023 * stick around until the final reference to the device is dropped.
1024 */
1025void device_unregister(struct device * dev)
1026{
1027 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
1028 device_del(dev);
1029 put_device(dev);
1030}
1031
1032
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001033static struct device * next_device(struct klist_iter * i)
1034{
1035 struct klist_node * n = klist_next(i);
1036 return n ? container_of(n, struct device, knode_parent) : NULL;
1037}
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/**
1040 * device_for_each_child - device child iterator.
Randy Dunlapc41455f2005-10-23 11:59:14 -07001041 * @parent: parent struct device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * @data: data for the callback.
1043 * @fn: function to be called for each device.
1044 *
Randy Dunlapc41455f2005-10-23 11:59:14 -07001045 * Iterate over @parent's child devices, and call @fn for each,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 * passing it @data.
1047 *
1048 * We check the return of @fn each time. If it returns anything
1049 * other than 0, we break out and return that value.
1050 */
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001051int device_for_each_child(struct device * parent, void * data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 int (*fn)(struct device *, void *))
1053{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001054 struct klist_iter i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct device * child;
1056 int error = 0;
1057
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001058 klist_iter_init(&parent->klist_children, &i);
1059 while ((child = next_device(&i)) && !error)
1060 error = fn(child, data);
1061 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return error;
1063}
1064
Cornelia Huck5ab69982006-11-16 15:42:07 +01001065/**
1066 * device_find_child - device iterator for locating a particular device.
1067 * @parent: parent struct device
1068 * @data: Data to pass to match function
1069 * @match: Callback function to check device
1070 *
1071 * This is similar to the device_for_each_child() function above, but it
1072 * returns a reference to a device that is 'found' for later use, as
1073 * determined by the @match callback.
1074 *
1075 * The callback should return 0 if the device doesn't match and non-zero
1076 * if it does. If the callback returns non-zero and a reference to the
1077 * current device can be obtained, this function will return to the caller
1078 * and not iterate over any more devices.
1079 */
1080struct device * device_find_child(struct device *parent, void *data,
1081 int (*match)(struct device *, void *))
1082{
1083 struct klist_iter i;
1084 struct device *child;
1085
1086 if (!parent)
1087 return NULL;
1088
1089 klist_iter_init(&parent->klist_children, &i);
1090 while ((child = next_device(&i)))
1091 if (match(child, data) && get_device(child))
1092 break;
1093 klist_iter_exit(&i);
1094 return child;
1095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097int __init devices_init(void)
1098{
1099 return subsystem_register(&devices_subsys);
1100}
1101
1102EXPORT_SYMBOL_GPL(device_for_each_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01001103EXPORT_SYMBOL_GPL(device_find_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105EXPORT_SYMBOL_GPL(device_initialize);
1106EXPORT_SYMBOL_GPL(device_add);
1107EXPORT_SYMBOL_GPL(device_register);
1108
1109EXPORT_SYMBOL_GPL(device_del);
1110EXPORT_SYMBOL_GPL(device_unregister);
1111EXPORT_SYMBOL_GPL(get_device);
1112EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114EXPORT_SYMBOL_GPL(device_create_file);
1115EXPORT_SYMBOL_GPL(device_remove_file);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001116
1117
1118static void device_create_release(struct device *dev)
1119{
1120 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
1121 kfree(dev);
1122}
1123
1124/**
1125 * device_create - creates a device and registers it with sysfs
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001126 * @class: pointer to the struct class that this device should be registered to
1127 * @parent: pointer to the parent struct device of this new device, if any
1128 * @devt: the dev_t for the char device to be added
1129 * @fmt: string for the device's name
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001130 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001131 * This function can be used by char device classes. A struct device
1132 * will be created in sysfs, registered to the specified class.
1133 *
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001134 * A "dev" file will be created, showing the dev_t for the device, if
1135 * the dev_t is not 0,0.
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001136 * If a pointer to a parent struct device is passed in, the newly created
1137 * struct device will be a child of that device in sysfs.
1138 * The pointer to the struct device will be returned from the call.
1139 * Any further sysfs files that might be required can be created using this
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001140 * pointer.
1141 *
1142 * Note: the struct class passed to this function must have previously
1143 * been created with a call to class_create().
1144 */
1145struct device *device_create(struct class *class, struct device *parent,
Greg Kroah-Hartman5cbe5f82006-08-10 01:19:19 -04001146 dev_t devt, const char *fmt, ...)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001147{
1148 va_list args;
1149 struct device *dev = NULL;
1150 int retval = -ENODEV;
1151
1152 if (class == NULL || IS_ERR(class))
1153 goto error;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001154
1155 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1156 if (!dev) {
1157 retval = -ENOMEM;
1158 goto error;
1159 }
1160
1161 dev->devt = devt;
1162 dev->class = class;
1163 dev->parent = parent;
1164 dev->release = device_create_release;
1165
1166 va_start(args, fmt);
1167 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
1168 va_end(args);
1169 retval = device_register(dev);
1170 if (retval)
1171 goto error;
1172
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001173 return dev;
1174
1175error:
1176 kfree(dev);
1177 return ERR_PTR(retval);
1178}
1179EXPORT_SYMBOL_GPL(device_create);
1180
1181/**
1182 * device_destroy - removes a device that was created with device_create()
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001183 * @class: pointer to the struct class that this device was registered with
1184 * @devt: the dev_t of the device that was previously registered
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001185 *
Henrik Kretzschmar42734da2006-07-05 00:53:19 +02001186 * This call unregisters and cleans up a device that was created with a
1187 * call to device_create().
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001188 */
1189void device_destroy(struct class *class, dev_t devt)
1190{
1191 struct device *dev = NULL;
1192 struct device *dev_tmp;
1193
1194 down(&class->sem);
1195 list_for_each_entry(dev_tmp, &class->devices, node) {
1196 if (dev_tmp->devt == devt) {
1197 dev = dev_tmp;
1198 break;
1199 }
1200 }
1201 up(&class->sem);
1202
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001203 if (dev)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001204 device_unregister(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001205}
1206EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001207
1208/**
1209 * device_rename - renames a device
1210 * @dev: the pointer to the struct device to be renamed
1211 * @new_name: the new name of the device
1212 */
1213int device_rename(struct device *dev, char *new_name)
1214{
1215 char *old_class_name = NULL;
1216 char *new_class_name = NULL;
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001217 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001218 int error;
1219
1220 dev = get_device(dev);
1221 if (!dev)
1222 return -EINVAL;
1223
1224 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
1225
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001226#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001227 if ((dev->class) && (dev->parent))
1228 old_class_name = make_class_name(dev->class->name, &dev->kobj);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001229#endif
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001230
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001231 old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
1232 if (!old_device_name) {
1233 error = -ENOMEM;
1234 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001235 }
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001236 strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001237 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1238
1239 error = kobject_rename(&dev->kobj, new_name);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001240 if (error) {
1241 strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
1242 goto out;
1243 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001244
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001245#ifdef CONFIG_SYSFS_DEPRECATED
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001246 if (old_class_name) {
1247 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1248 if (new_class_name) {
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001249 error = sysfs_create_link(&dev->parent->kobj,
1250 &dev->kobj, new_class_name);
1251 if (error)
1252 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001253 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1254 }
1255 }
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001256#endif
1257
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001258 if (dev->class) {
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001259 sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
1260 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
1261 dev->bus_id);
1262 if (error) {
1263 /* Uh... how to unravel this if restoring can fail? */
1264 dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
1265 __FUNCTION__, error);
1266 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001267 }
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001268out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001269 put_device(dev);
1270
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001271 kfree(new_class_name);
Jesper Juhl952ab432006-09-28 23:56:01 +02001272 kfree(old_class_name);
Cornelia Huck2ee97caf2007-07-18 01:43:47 -07001273 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07001274
1275 return error;
1276}
Johannes Berga2807db2007-02-28 12:38:31 +01001277EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01001278
1279static int device_move_class_links(struct device *dev,
1280 struct device *old_parent,
1281 struct device *new_parent)
1282{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001283 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001284#ifdef CONFIG_SYSFS_DEPRECATED
Cornelia Huck8a824722006-11-20 17:07:51 +01001285 char *class_name;
1286
1287 class_name = make_class_name(dev->class->name, &dev->kobj);
1288 if (!class_name) {
Cornelia Huckcb360bb2006-11-27 10:35:05 +01001289 error = -ENOMEM;
Cornelia Huck8a824722006-11-20 17:07:51 +01001290 goto out;
1291 }
1292 if (old_parent) {
1293 sysfs_remove_link(&dev->kobj, "device");
1294 sysfs_remove_link(&old_parent->kobj, class_name);
1295 }
Cornelia Huckc744aea2007-01-08 20:16:44 +01001296 if (new_parent) {
1297 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1298 "device");
1299 if (error)
1300 goto out;
1301 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1302 class_name);
1303 if (error)
1304 sysfs_remove_link(&dev->kobj, "device");
1305 }
1306 else
1307 error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01001308out:
1309 kfree(class_name);
1310 return error;
1311#else
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08001312 if (old_parent)
1313 sysfs_remove_link(&dev->kobj, "device");
1314 if (new_parent)
1315 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1316 "device");
1317 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01001318#endif
1319}
1320
1321/**
1322 * device_move - moves a device to a new parent
1323 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aea2007-01-08 20:16:44 +01001324 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huck8a824722006-11-20 17:07:51 +01001325 */
1326int device_move(struct device *dev, struct device *new_parent)
1327{
1328 int error;
1329 struct device *old_parent;
Cornelia Huckc744aea2007-01-08 20:16:44 +01001330 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01001331
1332 dev = get_device(dev);
1333 if (!dev)
1334 return -EINVAL;
1335
Cornelia Huck8a824722006-11-20 17:07:51 +01001336 new_parent = get_device(new_parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +01001337 new_parent_kobj = get_device_parent (dev, new_parent);
1338 if (IS_ERR(new_parent_kobj)) {
1339 error = PTR_ERR(new_parent_kobj);
1340 put_device(new_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001341 goto out;
1342 }
1343 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
Cornelia Huckc744aea2007-01-08 20:16:44 +01001344 new_parent ? new_parent->bus_id : "<NULL>");
1345 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01001346 if (error) {
1347 put_device(new_parent);
1348 goto out;
1349 }
1350 old_parent = dev->parent;
1351 dev->parent = new_parent;
1352 if (old_parent)
Cornelia Huckacf02d22006-11-22 17:49:39 +01001353 klist_remove(&dev->knode_parent);
Cornelia Huckc744aea2007-01-08 20:16:44 +01001354 if (new_parent)
1355 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
Cornelia Huck8a824722006-11-20 17:07:51 +01001356 if (!dev->class)
1357 goto out_put;
1358 error = device_move_class_links(dev, old_parent, new_parent);
1359 if (error) {
1360 /* We ignore errors on cleanup since we're hosed anyway... */
1361 device_move_class_links(dev, new_parent, old_parent);
1362 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
Cornelia Huckc744aea2007-01-08 20:16:44 +01001363 if (new_parent)
1364 klist_remove(&dev->knode_parent);
Cornelia Huck8a824722006-11-20 17:07:51 +01001365 if (old_parent)
1366 klist_add_tail(&dev->knode_parent,
1367 &old_parent->klist_children);
1368 }
1369 put_device(new_parent);
1370 goto out;
1371 }
1372out_put:
1373 put_device(old_parent);
1374out:
1375 put_device(dev);
1376 return error;
1377}
1378
1379EXPORT_SYMBOL_GPL(device_move);