Blame view

drivers/base/core.c 59.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
  /*
   * drivers/base/core.c - core driver model code (device registration, etc)
   *
   * Copyright (c) 2002-3 Patrick Mochel
   * Copyright (c) 2002-3 Open Source Development Labs
64bb5d2c1   Greg Kroah-Hartman   Driver core: allo...
6
7
   * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
   * Copyright (c) 2006 Novell, Inc.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
11
   *
   * This file is released under the GPLv2
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
  #include <linux/device.h>
  #include <linux/err.h>
97badf873   Rafael J. Wysocki   device property: ...
14
  #include <linux/fwnode.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
17
18
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/string.h>
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
19
  #include <linux/kdev_t.h>
116af3782   Benjamin Herrenschmidt   Driver core: add ...
20
  #include <linux/notifier.h>
07d57a32f   Grant Likely   drivercore: Outpu...
21
22
  #include <linux/of.h>
  #include <linux/of_device.h>
da231fd5d   Kay Sievers   Driver core: fix ...
23
  #include <linux/genhd.h>
815d2d50d   Andrew Morton   driver core: debu...
24
  #include <linux/kallsyms.h>
f75b1c60f   Dave Young   class: change int...
25
  #include <linux/mutex.h>
af8db1508   Peter Chen   PM / driver core:...
26
  #include <linux/pm_runtime.h>
c4e00daaa   Kay Sievers   driver-core: exte...
27
  #include <linux/netdevice.h>
639676856   Greg Kroah-Hartman   driver core: add ...
28
  #include <linux/sysfs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
  
  #include "base.h"
  #include "power/power.h"
e52eec13c   Andi Kleen   SYSFS: Allow boot...
32
33
34
35
36
37
  #ifdef CONFIG_SYSFS_DEPRECATED
  #ifdef CONFIG_SYSFS_DEPRECATED_V2
  long sysfs_deprecated = 1;
  #else
  long sysfs_deprecated = 0;
  #endif
3454bf960   Hanjun Guo   drivers / base: F...
38
  static int __init sysfs_deprecated_setup(char *arg)
e52eec13c   Andi Kleen   SYSFS: Allow boot...
39
  {
34da5e677   Jingoo Han   driver core: repl...
40
  	return kstrtol(arg, 10, &sysfs_deprecated);
e52eec13c   Andi Kleen   SYSFS: Allow boot...
41
42
43
  }
  early_param("sysfs.deprecated", sysfs_deprecated_setup);
  #endif
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
44
45
  int (*platform_notify)(struct device *dev) = NULL;
  int (*platform_notify_remove)(struct device *dev) = NULL;
e105b8bfc   Dan Williams   sysfs: add /sys/d...
46
47
48
  static struct kobject *dev_kobj;
  struct kobject *sysfs_dev_char_kobj;
  struct kobject *sysfs_dev_block_kobj;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49

5e33bc416   Rafael J. Wysocki   driver core / ACP...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  static DEFINE_MUTEX(device_hotplug_lock);
  
  void lock_device_hotplug(void)
  {
  	mutex_lock(&device_hotplug_lock);
  }
  
  void unlock_device_hotplug(void)
  {
  	mutex_unlock(&device_hotplug_lock);
  }
  
  int lock_device_hotplug_sysfs(void)
  {
  	if (mutex_trylock(&device_hotplug_lock))
  		return 0;
  
  	/* Avoid busy looping (5 ms of sleep should do). */
  	msleep(5);
  	return restart_syscall();
  }
4e886c296   Greg Kroah-Hartman   Driver core: Fix ...
71
72
73
74
75
76
77
78
79
80
81
  #ifdef CONFIG_BLOCK
  static inline int device_is_not_partition(struct device *dev)
  {
  	return !(dev->type == &part_type);
  }
  #else
  static inline int device_is_not_partition(struct device *dev)
  {
  	return 1;
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82

3e95637a4   Alan Stern   [PATCH] Driver Co...
83
84
85
86
87
  /**
   * dev_driver_string - Return a device's driver name, if at all possible
   * @dev: struct device to get the name of
   *
   * Will return the device's driver's name if it is bound to a device.  If
9169c0123   Yan   drivers/base/core...
88
   * the device is not bound to a driver, it will return the name of the bus
3e95637a4   Alan Stern   [PATCH] Driver Co...
89
90
91
   * it is attached to.  If it is not attached to a bus either, an empty
   * string will be returned.
   */
bf9ca69fc   Jean Delvare   dev_printk(): con...
92
  const char *dev_driver_string(const struct device *dev)
3e95637a4   Alan Stern   [PATCH] Driver Co...
93
  {
3589972e5   Alan Stern   Driver core: fix ...
94
95
96
97
98
99
100
101
  	struct device_driver *drv;
  
  	/* dev->driver can change to NULL underneath us because of unbinding,
  	 * so be careful about accessing it.  dev->bus and dev->class should
  	 * never change once they are set, so they don't need special care.
  	 */
  	drv = ACCESS_ONCE(dev->driver);
  	return drv ? drv->name :
a456b7023   Jean Delvare   dev_printk and ne...
102
103
  			(dev->bus ? dev->bus->name :
  			(dev->class ? dev->class->name : ""));
3e95637a4   Alan Stern   [PATCH] Driver Co...
104
  }
310a922d4   Matthew Wilcox   Fix dev_printk() ...
105
  EXPORT_SYMBOL(dev_driver_string);
3e95637a4   Alan Stern   [PATCH] Driver Co...
106

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
108
109
  static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  			     char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
111
  	struct device_attribute *dev_attr = to_dev_attr(attr);
b0d1f807f   Lars-Peter Clausen   driver-core: Use ...
112
  	struct device *dev = kobj_to_dev(kobj);
4a0c20bf8   Dmitry Torokhov   [PATCH] sysfs: (d...
113
  	ssize_t ret = -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
  
  	if (dev_attr->show)
54b6f35c9   Yani Ioannou   [PATCH] Driver co...
116
  		ret = dev_attr->show(dev, dev_attr, buf);
815d2d50d   Andrew Morton   driver core: debu...
117
  	if (ret >= (ssize_t)PAGE_SIZE) {
53a9c87e7   Greg Kroah-Hartman   Revert "drivers: ...
118
119
120
  		print_symbol("dev_attr_show: %s returned bad count
  ",
  				(unsigned long)dev_attr->show);
815d2d50d   Andrew Morton   driver core: debu...
121
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
  	return ret;
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
124
125
  static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  			      const char *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
127
  	struct device_attribute *dev_attr = to_dev_attr(attr);
b0d1f807f   Lars-Peter Clausen   driver-core: Use ...
128
  	struct device *dev = kobj_to_dev(kobj);
4a0c20bf8   Dmitry Torokhov   [PATCH] sysfs: (d...
129
  	ssize_t ret = -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
  
  	if (dev_attr->store)
54b6f35c9   Yani Ioannou   [PATCH] Driver co...
132
  		ret = dev_attr->store(dev, dev_attr, buf, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
  	return ret;
  }
52cf25d0a   Emese Revfy   Driver core: Cons...
135
  static const struct sysfs_ops dev_sysfs_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
  	.show	= dev_attr_show,
  	.store	= dev_attr_store,
  };
ca22e56de   Kay Sievers   driver-core: impl...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
  
  ssize_t device_store_ulong(struct device *dev,
  			   struct device_attribute *attr,
  			   const char *buf, size_t size)
  {
  	struct dev_ext_attribute *ea = to_ext_attr(attr);
  	char *end;
  	unsigned long new = simple_strtoul(buf, &end, 0);
  	if (end == buf)
  		return -EINVAL;
  	*(unsigned long *)(ea->var) = new;
  	/* Always return full write size even if we didn't consume all */
  	return size;
  }
  EXPORT_SYMBOL_GPL(device_store_ulong);
  
  ssize_t device_show_ulong(struct device *dev,
  			  struct device_attribute *attr,
  			  char *buf)
  {
  	struct dev_ext_attribute *ea = to_ext_attr(attr);
  	return snprintf(buf, PAGE_SIZE, "%lx
  ", *(unsigned long *)(ea->var));
  }
  EXPORT_SYMBOL_GPL(device_show_ulong);
  
  ssize_t device_store_int(struct device *dev,
  			 struct device_attribute *attr,
  			 const char *buf, size_t size)
  {
  	struct dev_ext_attribute *ea = to_ext_attr(attr);
  	char *end;
  	long new = simple_strtol(buf, &end, 0);
  	if (end == buf || new > INT_MAX || new < INT_MIN)
  		return -EINVAL;
  	*(int *)(ea->var) = new;
  	/* Always return full write size even if we didn't consume all */
  	return size;
  }
  EXPORT_SYMBOL_GPL(device_store_int);
  
  ssize_t device_show_int(struct device *dev,
  			struct device_attribute *attr,
  			char *buf)
  {
  	struct dev_ext_attribute *ea = to_ext_attr(attr);
  
  	return snprintf(buf, PAGE_SIZE, "%d
  ", *(int *)(ea->var));
  }
  EXPORT_SYMBOL_GPL(device_show_int);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191

91872392f   Borislav Petkov   drivers/base: Add...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
  			  const char *buf, size_t size)
  {
  	struct dev_ext_attribute *ea = to_ext_attr(attr);
  
  	if (strtobool(buf, ea->var) < 0)
  		return -EINVAL;
  
  	return size;
  }
  EXPORT_SYMBOL_GPL(device_store_bool);
  
  ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
  			 char *buf)
  {
  	struct dev_ext_attribute *ea = to_ext_attr(attr);
  
  	return snprintf(buf, PAGE_SIZE, "%d
  ", *(bool *)(ea->var));
  }
  EXPORT_SYMBOL_GPL(device_show_bool);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
  /**
f8878dcb8   Robert P. J. Day   Documentation: Ti...
214
215
   * device_release - free device structure.
   * @kobj: device's kobject.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
   *
f8878dcb8   Robert P. J. Day   Documentation: Ti...
217
218
219
   * This is called once the reference count for the object
   * reaches 0. We forward the call to the device's release
   * method, which should handle actually freeing the structure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
221
  static void device_release(struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222
  {
b0d1f807f   Lars-Peter Clausen   driver-core: Use ...
223
  	struct device *dev = kobj_to_dev(kobj);
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
224
  	struct device_private *p = dev->p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225

a525a3dde   Ming Lei   driver core: free...
226
227
228
229
230
231
232
233
234
235
  	/*
  	 * Some platform devices are driven without driver attached
  	 * and managed resources may have been acquired.  Make sure
  	 * all resources are released.
  	 *
  	 * Drivers still can add resources into device after device
  	 * is deleted but alive, so release devres here to avoid
  	 * possible memory leak.
  	 */
  	devres_release_all(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
237
  	if (dev->release)
  		dev->release(dev);
f9f852df2   Kay Sievers   Driver core: add ...
238
239
  	else if (dev->type && dev->type->release)
  		dev->type->release(dev);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
240
241
  	else if (dev->class && dev->class->dev_release)
  		dev->class->dev_release(dev);
f810a5cf2   Arjan van de Ven   Use WARN() in dri...
242
243
  	else
  		WARN(1, KERN_ERR "Device '%s' does not have a release() "
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
244
245
  			"function, it is broken and must be fixed.
  ",
1e0b2cf93   Kay Sievers   driver core: stru...
246
  			dev_name(dev));
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
247
  	kfree(p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
248
  }
bc451f205   Eric W. Biederman   kobj: Add basic i...
249
250
  static const void *device_namespace(struct kobject *kobj)
  {
b0d1f807f   Lars-Peter Clausen   driver-core: Use ...
251
  	struct device *dev = kobj_to_dev(kobj);
bc451f205   Eric W. Biederman   kobj: Add basic i...
252
253
254
255
256
257
258
  	const void *ns = NULL;
  
  	if (dev->class && dev->class->ns_type)
  		ns = dev->class->namespace(dev);
  
  	return ns;
  }
8f4afc410   Greg Kroah-Hartman   Driver core: rena...
259
  static struct kobj_type device_ktype = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
261
  	.release	= device_release,
  	.sysfs_ops	= &dev_sysfs_ops,
bc451f205   Eric W. Biederman   kobj: Add basic i...
262
  	.namespace	= device_namespace,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  };
312c004d3   Kay Sievers   [PATCH] driver co...
264
  static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
  {
  	struct kobj_type *ktype = get_ktype(kobj);
8f4afc410   Greg Kroah-Hartman   Driver core: rena...
267
  	if (ktype == &device_ktype) {
b0d1f807f   Lars-Peter Clausen   driver-core: Use ...
268
  		struct device *dev = kobj_to_dev(kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
  		if (dev->bus)
  			return 1;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
271
272
  		if (dev->class)
  			return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
275
  	}
  	return 0;
  }
312c004d3   Kay Sievers   [PATCH] driver co...
276
  static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
  {
b0d1f807f   Lars-Peter Clausen   driver-core: Use ...
278
  	struct device *dev = kobj_to_dev(kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279

23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
280
281
282
283
284
  	if (dev->bus)
  		return dev->bus->name;
  	if (dev->class)
  		return dev->class->name;
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
  }
7eff2e7a8   Kay Sievers   Driver core: chan...
286
287
  static int dev_uevent(struct kset *kset, struct kobject *kobj,
  		      struct kobj_uevent_env *env)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
  {
b0d1f807f   Lars-Peter Clausen   driver-core: Use ...
289
  	struct device *dev = kobj_to_dev(kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
290
  	int retval = 0;
6fcf53acc   Kay Sievers   Driver Core: add ...
291
  	/* add device node properties if present */
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
292
  	if (MAJOR(dev->devt)) {
6fcf53acc   Kay Sievers   Driver Core: add ...
293
294
  		const char *tmp;
  		const char *name;
2c9ede55e   Al Viro   switch device_get...
295
  		umode_t mode = 0;
4e4098a3e   Greg Kroah-Hartman   driver core: hand...
296
297
  		kuid_t uid = GLOBAL_ROOT_UID;
  		kgid_t gid = GLOBAL_ROOT_GID;
6fcf53acc   Kay Sievers   Driver Core: add ...
298

7eff2e7a8   Kay Sievers   Driver core: chan...
299
300
  		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
3c2670e65   Kay Sievers   driver core: add ...
301
  		name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
6fcf53acc   Kay Sievers   Driver Core: add ...
302
303
  		if (name) {
  			add_uevent_var(env, "DEVNAME=%s", name);
e454cea20   Kay Sievers   Driver-Core: exte...
304
305
  			if (mode)
  				add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
4e4098a3e   Greg Kroah-Hartman   driver core: hand...
306
307
308
309
  			if (!uid_eq(uid, GLOBAL_ROOT_UID))
  				add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
  			if (!gid_eq(gid, GLOBAL_ROOT_GID))
  				add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
3c2670e65   Kay Sievers   driver core: add ...
310
  			kfree(tmp);
6fcf53acc   Kay Sievers   Driver Core: add ...
311
  		}
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
312
  	}
414264f95   Kay Sievers   Driver core: add ...
313
  	if (dev->type && dev->type->name)
7eff2e7a8   Kay Sievers   Driver core: chan...
314
  		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
414264f95   Kay Sievers   Driver core: add ...
315

239378f16   Kay Sievers   Driver core: add ...
316
  	if (dev->driver)
7eff2e7a8   Kay Sievers   Driver core: chan...
317
  		add_uevent_var(env, "DRIVER=%s", dev->driver->name);
239378f16   Kay Sievers   Driver core: add ...
318

07d57a32f   Grant Likely   drivercore: Outpu...
319
320
  	/* Add common DT information about the device */
  	of_device_uevent(dev, env);
7eff2e7a8   Kay Sievers   Driver core: chan...
321
  	/* have the bus specific function add its stuff */
312c004d3   Kay Sievers   [PATCH] driver co...
322
  	if (dev->bus && dev->bus->uevent) {
7eff2e7a8   Kay Sievers   Driver core: chan...
323
  		retval = dev->bus->uevent(dev, env);
f9f852df2   Kay Sievers   Driver core: add ...
324
  		if (retval)
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
325
326
  			pr_debug("device: '%s': %s: bus uevent() returned %d
  ",
1e0b2cf93   Kay Sievers   driver core: stru...
327
  				 dev_name(dev), __func__, retval);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
  	}
7eff2e7a8   Kay Sievers   Driver core: chan...
329
  	/* have the class specific function add its stuff */
2620efef7   Greg Kroah-Hartman   Driver core: add ...
330
  	if (dev->class && dev->class->dev_uevent) {
7eff2e7a8   Kay Sievers   Driver core: chan...
331
  		retval = dev->class->dev_uevent(dev, env);
f9f852df2   Kay Sievers   Driver core: add ...
332
  		if (retval)
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
333
  			pr_debug("device: '%s': %s: class uevent() "
1e0b2cf93   Kay Sievers   driver core: stru...
334
335
  				 "returned %d
  ", dev_name(dev),
2b3a302a0   Harvey Harrison   driver core: repl...
336
  				 __func__, retval);
f9f852df2   Kay Sievers   Driver core: add ...
337
  	}
eef35c2d4   Stefan Weil   Fix spelling fuct...
338
  	/* have the device type specific function add its stuff */
f9f852df2   Kay Sievers   Driver core: add ...
339
  	if (dev->type && dev->type->uevent) {
7eff2e7a8   Kay Sievers   Driver core: chan...
340
  		retval = dev->type->uevent(dev, env);
f9f852df2   Kay Sievers   Driver core: add ...
341
  		if (retval)
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
342
  			pr_debug("device: '%s': %s: dev_type uevent() "
1e0b2cf93   Kay Sievers   driver core: stru...
343
344
  				 "returned %d
  ", dev_name(dev),
2b3a302a0   Harvey Harrison   driver core: repl...
345
  				 __func__, retval);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
346
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
348
  	return retval;
  }
9cd43611c   Emese Revfy   kobject: Constify...
349
  static const struct kset_uevent_ops device_uevent_ops = {
312c004d3   Kay Sievers   [PATCH] driver co...
350
351
352
  	.filter =	dev_uevent_filter,
  	.name =		dev_uevent_name,
  	.uevent =	dev_uevent,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
  };
c5e064a69   Greg Kroah-Hartman   driver core: core...
354
  static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
16574dccd   Kay Sievers   Driver core: make...
355
356
357
358
  			   char *buf)
  {
  	struct kobject *top_kobj;
  	struct kset *kset;
7eff2e7a8   Kay Sievers   Driver core: chan...
359
  	struct kobj_uevent_env *env = NULL;
16574dccd   Kay Sievers   Driver core: make...
360
361
362
363
364
365
  	int i;
  	size_t count = 0;
  	int retval;
  
  	/* search the kset, the device belongs to */
  	top_kobj = &dev->kobj;
5c5daf657   Kay Sievers   Driver core: excl...
366
367
  	while (!top_kobj->kset && top_kobj->parent)
  		top_kobj = top_kobj->parent;
16574dccd   Kay Sievers   Driver core: make...
368
369
  	if (!top_kobj->kset)
  		goto out;
5c5daf657   Kay Sievers   Driver core: excl...
370

16574dccd   Kay Sievers   Driver core: make...
371
372
373
374
375
376
377
378
  	kset = top_kobj->kset;
  	if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  		goto out;
  
  	/* respect filter */
  	if (kset->uevent_ops && kset->uevent_ops->filter)
  		if (!kset->uevent_ops->filter(kset, &dev->kobj))
  			goto out;
7eff2e7a8   Kay Sievers   Driver core: chan...
379
380
  	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  	if (!env)
c7308c81a   Greg Kroah-Hartman   Driver core: fix ...
381
  		return -ENOMEM;
16574dccd   Kay Sievers   Driver core: make...
382
  	/* let the kset specific function add its keys */
7eff2e7a8   Kay Sievers   Driver core: chan...
383
  	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
16574dccd   Kay Sievers   Driver core: make...
384
385
386
387
  	if (retval)
  		goto out;
  
  	/* copy keys to file */
7eff2e7a8   Kay Sievers   Driver core: chan...
388
389
390
  	for (i = 0; i < env->envp_idx; i++)
  		count += sprintf(&buf[count], "%s
  ", env->envp[i]);
16574dccd   Kay Sievers   Driver core: make...
391
  out:
7eff2e7a8   Kay Sievers   Driver core: chan...
392
  	kfree(env);
16574dccd   Kay Sievers   Driver core: make...
393
394
  	return count;
  }
c5e064a69   Greg Kroah-Hartman   driver core: core...
395
  static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
a7fd67062   Kay Sievers   [PATCH] add sysfs...
396
397
  			    const char *buf, size_t count)
  {
60a96a595   Kay Sievers   Driver core: acce...
398
  	enum kobject_action action;
3f5468c9a   Kay Sievers   Driver-Core: requ...
399
  	if (kobject_action_type(buf, count, &action) == 0)
60a96a595   Kay Sievers   Driver core: acce...
400
  		kobject_uevent(&dev->kobj, action);
3f5468c9a   Kay Sievers   Driver-Core: requ...
401
402
403
  	else
  		dev_err(dev, "uevent: unknown action-string
  ");
a7fd67062   Kay Sievers   [PATCH] add sysfs...
404
405
  	return count;
  }
c5e064a69   Greg Kroah-Hartman   driver core: core...
406
  static DEVICE_ATTR_RW(uevent);
a7fd67062   Kay Sievers   [PATCH] add sysfs...
407

c5e064a69   Greg Kroah-Hartman   driver core: core...
408
  static ssize_t online_show(struct device *dev, struct device_attribute *attr,
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
409
410
411
  			   char *buf)
  {
  	bool val;
5e33bc416   Rafael J. Wysocki   driver core / ACP...
412
  	device_lock(dev);
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
413
  	val = !dev->offline;
5e33bc416   Rafael J. Wysocki   driver core / ACP...
414
  	device_unlock(dev);
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
415
416
417
  	return sprintf(buf, "%u
  ", val);
  }
c5e064a69   Greg Kroah-Hartman   driver core: core...
418
  static ssize_t online_store(struct device *dev, struct device_attribute *attr,
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
419
420
421
422
423
424
425
426
  			    const char *buf, size_t count)
  {
  	bool val;
  	int ret;
  
  	ret = strtobool(buf, &val);
  	if (ret < 0)
  		return ret;
5e33bc416   Rafael J. Wysocki   driver core / ACP...
427
428
429
  	ret = lock_device_hotplug_sysfs();
  	if (ret)
  		return ret;
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
430
431
432
433
  	ret = val ? device_online(dev) : device_offline(dev);
  	unlock_device_hotplug();
  	return ret < 0 ? ret : count;
  }
c5e064a69   Greg Kroah-Hartman   driver core: core...
434
  static DEVICE_ATTR_RW(online);
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
435

fa6fdb33b   Greg Kroah-Hartman   driver core: bus_...
436
  int device_add_groups(struct device *dev, const struct attribute_group **groups)
621a1672f   Dmitry Torokhov   driver core: Use ...
437
  {
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
438
  	return sysfs_create_groups(&dev->kobj, groups);
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
439
  }
fa6fdb33b   Greg Kroah-Hartman   driver core: bus_...
440
441
  void device_remove_groups(struct device *dev,
  			  const struct attribute_group **groups)
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
442
  {
3e9b2bae8   Greg Kroah-Hartman   sysfs: add sysfs_...
443
  	sysfs_remove_groups(&dev->kobj, groups);
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
444
  }
2620efef7   Greg Kroah-Hartman   Driver core: add ...
445
446
447
  static int device_add_attrs(struct device *dev)
  {
  	struct class *class = dev->class;
aed65af1c   Stephen Hemminger   drivers: make dev...
448
  	const struct device_type *type = dev->type;
621a1672f   Dmitry Torokhov   driver core: Use ...
449
  	int error;
2620efef7   Greg Kroah-Hartman   Driver core: add ...
450

621a1672f   Dmitry Torokhov   driver core: Use ...
451
  	if (class) {
d05a6f96c   Greg Kroah-Hartman   driver core: add ...
452
  		error = device_add_groups(dev, class->dev_groups);
f9f852df2   Kay Sievers   Driver core: add ...
453
  		if (error)
621a1672f   Dmitry Torokhov   driver core: Use ...
454
  			return error;
2620efef7   Greg Kroah-Hartman   Driver core: add ...
455
  	}
f9f852df2   Kay Sievers   Driver core: add ...
456

621a1672f   Dmitry Torokhov   driver core: Use ...
457
458
  	if (type) {
  		error = device_add_groups(dev, type->groups);
f9f852df2   Kay Sievers   Driver core: add ...
459
  		if (error)
a6b01deda   Greg Kroah-Hartman   driver core: remo...
460
  			goto err_remove_class_groups;
f9f852df2   Kay Sievers   Driver core: add ...
461
  	}
621a1672f   Dmitry Torokhov   driver core: Use ...
462
463
464
  	error = device_add_groups(dev, dev->groups);
  	if (error)
  		goto err_remove_type_groups;
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
465
  	if (device_supports_offline(dev) && !dev->offline_disabled) {
c5e064a69   Greg Kroah-Hartman   driver core: core...
466
  		error = device_create_file(dev, &dev_attr_online);
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
467
  		if (error)
ecfbf6fd9   Rafael J. Wysocki   Driver core: Fix ...
468
  			goto err_remove_dev_groups;
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
469
  	}
621a1672f   Dmitry Torokhov   driver core: Use ...
470
  	return 0;
ecfbf6fd9   Rafael J. Wysocki   Driver core: Fix ...
471
472
   err_remove_dev_groups:
  	device_remove_groups(dev, dev->groups);
621a1672f   Dmitry Torokhov   driver core: Use ...
473
474
475
   err_remove_type_groups:
  	if (type)
  		device_remove_groups(dev, type->groups);
d05a6f96c   Greg Kroah-Hartman   driver core: add ...
476
477
478
   err_remove_class_groups:
  	if (class)
  		device_remove_groups(dev, class->dev_groups);
621a1672f   Dmitry Torokhov   driver core: Use ...
479

2620efef7   Greg Kroah-Hartman   Driver core: add ...
480
481
482
483
484
485
  	return error;
  }
  
  static void device_remove_attrs(struct device *dev)
  {
  	struct class *class = dev->class;
aed65af1c   Stephen Hemminger   drivers: make dev...
486
  	const struct device_type *type = dev->type;
2620efef7   Greg Kroah-Hartman   Driver core: add ...
487

c5e064a69   Greg Kroah-Hartman   driver core: core...
488
  	device_remove_file(dev, &dev_attr_online);
621a1672f   Dmitry Torokhov   driver core: Use ...
489
  	device_remove_groups(dev, dev->groups);
f9f852df2   Kay Sievers   Driver core: add ...
490

621a1672f   Dmitry Torokhov   driver core: Use ...
491
492
  	if (type)
  		device_remove_groups(dev, type->groups);
a6b01deda   Greg Kroah-Hartman   driver core: remo...
493
  	if (class)
d05a6f96c   Greg Kroah-Hartman   driver core: add ...
494
  		device_remove_groups(dev, class->dev_groups);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
495
  }
c5e064a69   Greg Kroah-Hartman   driver core: core...
496
  static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
497
498
499
500
  			char *buf)
  {
  	return print_dev_t(buf, dev->devt);
  }
c5e064a69   Greg Kroah-Hartman   driver core: core...
501
  static DEVICE_ATTR_RO(dev);
ad6a1e1c6   Tejun Heo   driver-core: make...
502

ca22e56de   Kay Sievers   driver-core: impl...
503
  /* /sys/devices/ */
881c6cfd7   Greg Kroah-Hartman   kset: convert /sy...
504
  struct kset *devices_kset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
  /**
52cdbdd49   Grygorii Strashko   driver core: corr...
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
   * devices_kset_move_before - Move device in the devices_kset's list.
   * @deva: Device to move.
   * @devb: Device @deva should come before.
   */
  static void devices_kset_move_before(struct device *deva, struct device *devb)
  {
  	if (!devices_kset)
  		return;
  	pr_debug("devices_kset: Moving %s before %s
  ",
  		 dev_name(deva), dev_name(devb));
  	spin_lock(&devices_kset->list_lock);
  	list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
  	spin_unlock(&devices_kset->list_lock);
  }
  
  /**
   * devices_kset_move_after - Move device in the devices_kset's list.
   * @deva: Device to move
   * @devb: Device @deva should come after.
   */
  static void devices_kset_move_after(struct device *deva, struct device *devb)
  {
  	if (!devices_kset)
  		return;
  	pr_debug("devices_kset: Moving %s after %s
  ",
  		 dev_name(deva), dev_name(devb));
  	spin_lock(&devices_kset->list_lock);
  	list_move(&deva->kobj.entry, &devb->kobj.entry);
  	spin_unlock(&devices_kset->list_lock);
  }
  
  /**
   * devices_kset_move_last - move the device to the end of devices_kset's list.
   * @dev: device to move
   */
  void devices_kset_move_last(struct device *dev)
  {
  	if (!devices_kset)
  		return;
  	pr_debug("devices_kset: Moving %s to end of list
  ", dev_name(dev));
  	spin_lock(&devices_kset->list_lock);
  	list_move_tail(&dev->kobj.entry, &devices_kset->list);
  	spin_unlock(&devices_kset->list_lock);
  }
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
556
557
558
   * device_create_file - create sysfs attribute file for device.
   * @dev: device.
   * @attr: device attribute descriptor.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
   */
26579ab70   Phil Carmody   Driver core: devi...
560
561
  int device_create_file(struct device *dev,
  		       const struct device_attribute *attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562
563
  {
  	int error = 0;
8f46baaa7   Felipe Balbi   base: core: WARN(...
564
565
566
  
  	if (dev) {
  		WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
97521978c   dyoung@redhat.com   driver core: prin...
567
568
569
  			"Attribute %s: write permission without 'store'
  ",
  			attr->attr.name);
8f46baaa7   Felipe Balbi   base: core: WARN(...
570
  		WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
97521978c   dyoung@redhat.com   driver core: prin...
571
572
573
  			"Attribute %s: read permission without 'show'
  ",
  			attr->attr.name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
  		error = sysfs_create_file(&dev->kobj, &attr->attr);
8f46baaa7   Felipe Balbi   base: core: WARN(...
575
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
576
577
  	return error;
  }
86df26870   David Graham White   drivers:base:core...
578
  EXPORT_SYMBOL_GPL(device_create_file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
579
580
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
581
582
583
   * device_remove_file - remove sysfs attribute file.
   * @dev: device.
   * @attr: device attribute descriptor.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584
   */
26579ab70   Phil Carmody   Driver core: devi...
585
586
  void device_remove_file(struct device *dev,
  			const struct device_attribute *attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
587
  {
0c98b19fe   Cornelia Huck   Driver core: Remo...
588
  	if (dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
  		sysfs_remove_file(&dev->kobj, &attr->attr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
  }
86df26870   David Graham White   drivers:base:core...
591
  EXPORT_SYMBOL_GPL(device_remove_file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592

2589f1887   Greg Kroah-Hartman   Driver core: add ...
593
  /**
6b0afc2a2   Tejun Heo   kernfs, sysfs, dr...
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
   * device_remove_file_self - remove sysfs attribute file from its own method.
   * @dev: device.
   * @attr: device attribute descriptor.
   *
   * See kernfs_remove_self() for details.
   */
  bool device_remove_file_self(struct device *dev,
  			     const struct device_attribute *attr)
  {
  	if (dev)
  		return sysfs_remove_file_self(&dev->kobj, &attr->attr);
  	else
  		return false;
  }
  EXPORT_SYMBOL_GPL(device_remove_file_self);
  
  /**
2589f1887   Greg Kroah-Hartman   Driver core: add ...
611
612
613
614
   * device_create_bin_file - create sysfs binary attribute file for device.
   * @dev: device.
   * @attr: device binary attribute descriptor.
   */
66ecb92be   Phil Carmody   Driver core: bin_...
615
616
  int device_create_bin_file(struct device *dev,
  			   const struct bin_attribute *attr)
2589f1887   Greg Kroah-Hartman   Driver core: add ...
617
618
619
620
621
622
623
624
625
626
627
628
629
  {
  	int error = -EINVAL;
  	if (dev)
  		error = sysfs_create_bin_file(&dev->kobj, attr);
  	return error;
  }
  EXPORT_SYMBOL_GPL(device_create_bin_file);
  
  /**
   * device_remove_bin_file - remove sysfs binary attribute file
   * @dev: device.
   * @attr: device binary attribute descriptor.
   */
66ecb92be   Phil Carmody   Driver core: bin_...
630
631
  void device_remove_bin_file(struct device *dev,
  			    const struct bin_attribute *attr)
2589f1887   Greg Kroah-Hartman   Driver core: add ...
632
633
634
635
636
  {
  	if (dev)
  		sysfs_remove_bin_file(&dev->kobj, attr);
  }
  EXPORT_SYMBOL_GPL(device_remove_bin_file);
34bb61f9d   James Bottomley   [PATCH] fix klist...
637
638
  static void klist_children_get(struct klist_node *n)
  {
f791b8c83   Greg Kroah-Hartman   driver core: move...
639
640
  	struct device_private *p = to_device_private_parent(n);
  	struct device *dev = p->device;
34bb61f9d   James Bottomley   [PATCH] fix klist...
641
642
643
644
645
646
  
  	get_device(dev);
  }
  
  static void klist_children_put(struct klist_node *n)
  {
f791b8c83   Greg Kroah-Hartman   driver core: move...
647
648
  	struct device_private *p = to_device_private_parent(n);
  	struct device *dev = p->device;
34bb61f9d   James Bottomley   [PATCH] fix klist...
649
650
651
  
  	put_device(dev);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
653
654
   * device_initialize - init device structure.
   * @dev: device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
   *
5739411ac   Cornelia Huck   Driver core: Clar...
656
657
   * This prepares the device for use by other layers by initializing
   * its fields.
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
658
   * It is the first half of device_register(), if called by
5739411ac   Cornelia Huck   Driver core: Clar...
659
660
661
662
663
   * that function, though it can also be called separately, so one
   * may use @dev's fields. In particular, get_device()/put_device()
   * may be used for reference counting of @dev after calling this
   * function.
   *
b10d5efdf   Alan Stern   Documentation upd...
664
665
666
667
668
   * All fields in @dev must be initialized by the caller to 0, except
   * for those explicitly set to some other value.  The simplest
   * approach is to use kzalloc() to allocate the structure containing
   * @dev.
   *
5739411ac   Cornelia Huck   Driver core: Clar...
669
670
   * NOTE: Use put_device() to give up your reference instead of freeing
   * @dev directly once you have called this function.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
673
  void device_initialize(struct device *dev)
  {
881c6cfd7   Greg Kroah-Hartman   kset: convert /sy...
674
  	dev->kobj.kset = devices_kset;
f9cb074bf   Greg Kroah-Hartman   Kobject: rename k...
675
  	kobject_init(&dev->kobj, &device_ktype);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
  	INIT_LIST_HEAD(&dev->dma_pools);
3142788b7   Thomas Gleixner   drivers/base: Con...
677
  	mutex_init(&dev->mutex);
1704f47b5   Peter Zijlstra   lockdep: Add nova...
678
  	lockdep_set_novalidate_class(&dev->mutex);
9ac7849e3   Tejun Heo   devres: device re...
679
680
  	spin_lock_init(&dev->devres_lock);
  	INIT_LIST_HEAD(&dev->devres_head);
3b98aeaf3   Alan Stern   PM: don't skip de...
681
  	device_pm_init(dev);
873481367   Christoph Hellwig   [PATCH] add numa ...
682
  	set_dev_node(dev, -1);
4a7cc8316   Jiang Liu   genirq/MSI: Move ...
683
684
685
  #ifdef CONFIG_GENERIC_MSI_IRQ
  	INIT_LIST_HEAD(&dev->msi_list);
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
  }
86df26870   David Graham White   drivers:base:core...
687
  EXPORT_SYMBOL_GPL(device_initialize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688

d73ce0042   Tejun Heo   driver/base: impl...
689
  struct kobject *virtual_device_parent(struct device *dev)
f0ee61a6c   Greg Kroah-Hartman   Driver Core: Move...
690
  {
864062457   Kay Sievers   driver core: fix ...
691
  	static struct kobject *virtual_dir = NULL;
f0ee61a6c   Greg Kroah-Hartman   Driver Core: Move...
692

864062457   Kay Sievers   driver core: fix ...
693
  	if (!virtual_dir)
4ff6abff8   Greg Kroah-Hartman   kobject: get rid ...
694
  		virtual_dir = kobject_create_and_add("virtual",
881c6cfd7   Greg Kroah-Hartman   kset: convert /sy...
695
  						     &devices_kset->kobj);
f0ee61a6c   Greg Kroah-Hartman   Driver Core: Move...
696

864062457   Kay Sievers   driver core: fix ...
697
  	return virtual_dir;
f0ee61a6c   Greg Kroah-Hartman   Driver Core: Move...
698
  }
bc451f205   Eric W. Biederman   kobj: Add basic i...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
  struct class_dir {
  	struct kobject kobj;
  	struct class *class;
  };
  
  #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
  
  static void class_dir_release(struct kobject *kobj)
  {
  	struct class_dir *dir = to_class_dir(kobj);
  	kfree(dir);
  }
  
  static const
  struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
40fa54226   Greg Kroah-Hartman   Driver core: make...
714
  {
bc451f205   Eric W. Biederman   kobj: Add basic i...
715
716
717
718
719
720
721
722
723
724
725
726
727
728
  	struct class_dir *dir = to_class_dir(kobj);
  	return dir->class->ns_type;
  }
  
  static struct kobj_type class_dir_ktype = {
  	.release	= class_dir_release,
  	.sysfs_ops	= &kobj_sysfs_ops,
  	.child_ns_type	= class_dir_child_ns_type
  };
  
  static struct kobject *
  class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
  {
  	struct class_dir *dir;
43968d2f1   Greg Kroah-Hartman   kobject: get rid ...
729
  	int retval;
bc451f205   Eric W. Biederman   kobj: Add basic i...
730
731
732
733
734
735
  	dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  	if (!dir)
  		return NULL;
  
  	dir->class = class;
  	kobject_init(&dir->kobj, &class_dir_ktype);
6b6e39a6a   Kay Sievers   driver-core: merg...
736
  	dir->kobj.kset = &class->p->glue_dirs;
bc451f205   Eric W. Biederman   kobj: Add basic i...
737
738
739
740
741
742
743
744
  
  	retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
  	if (retval < 0) {
  		kobject_put(&dir->kobj);
  		return NULL;
  	}
  	return &dir->kobj;
  }
e4a60d139   Yijing Wang   sysfs: driver cor...
745
  static DEFINE_MUTEX(gdp_mutex);
bc451f205   Eric W. Biederman   kobj: Add basic i...
746
747
748
749
  
  static struct kobject *get_device_parent(struct device *dev,
  					 struct device *parent)
  {
864062457   Kay Sievers   driver core: fix ...
750
751
752
753
  	if (dev->class) {
  		struct kobject *kobj = NULL;
  		struct kobject *parent_kobj;
  		struct kobject *k;
ead454feb   Randy Dunlap   driver core: fix ...
754
  #ifdef CONFIG_BLOCK
39aba963d   Kay Sievers   driver core: remo...
755
  		/* block disks show up in /sys/block */
e52eec13c   Andi Kleen   SYSFS: Allow boot...
756
  		if (sysfs_deprecated && dev->class == &block_class) {
39aba963d   Kay Sievers   driver core: remo...
757
758
  			if (parent && parent->class == &block_class)
  				return &parent->kobj;
6b6e39a6a   Kay Sievers   driver-core: merg...
759
  			return &block_class.p->subsys.kobj;
39aba963d   Kay Sievers   driver core: remo...
760
  		}
ead454feb   Randy Dunlap   driver core: fix ...
761
  #endif
e52eec13c   Andi Kleen   SYSFS: Allow boot...
762

864062457   Kay Sievers   driver core: fix ...
763
764
  		/*
  		 * If we have no parent, we live in "virtual".
0f4dafc05   Kay Sievers   Kobject: auto-cle...
765
766
  		 * Class-devices with a non class-device as parent, live
  		 * in a "glue" directory to prevent namespace collisions.
864062457   Kay Sievers   driver core: fix ...
767
768
769
  		 */
  		if (parent == NULL)
  			parent_kobj = virtual_device_parent(dev);
24b1442d0   Eric W. Biederman   Driver-core: Alwa...
770
  		else if (parent->class && !dev->class->ns_type)
864062457   Kay Sievers   driver core: fix ...
771
772
773
  			return &parent->kobj;
  		else
  			parent_kobj = &parent->kobj;
77d3d7c1d   Tejun Heo   driver-core: fix ...
774
  		mutex_lock(&gdp_mutex);
864062457   Kay Sievers   driver core: fix ...
775
  		/* find our class-directory at the parent and reference it */
6b6e39a6a   Kay Sievers   driver-core: merg...
776
777
  		spin_lock(&dev->class->p->glue_dirs.list_lock);
  		list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
864062457   Kay Sievers   driver core: fix ...
778
779
780
781
  			if (k->parent == parent_kobj) {
  				kobj = kobject_get(k);
  				break;
  			}
6b6e39a6a   Kay Sievers   driver-core: merg...
782
  		spin_unlock(&dev->class->p->glue_dirs.list_lock);
77d3d7c1d   Tejun Heo   driver-core: fix ...
783
784
  		if (kobj) {
  			mutex_unlock(&gdp_mutex);
864062457   Kay Sievers   driver core: fix ...
785
  			return kobj;
77d3d7c1d   Tejun Heo   driver-core: fix ...
786
  		}
864062457   Kay Sievers   driver core: fix ...
787
788
  
  		/* or create a new class-directory at the parent device */
bc451f205   Eric W. Biederman   kobj: Add basic i...
789
  		k = class_dir_create_and_add(dev->class, parent_kobj);
0f4dafc05   Kay Sievers   Kobject: auto-cle...
790
  		/* do not emit an uevent for this simple "glue" directory */
77d3d7c1d   Tejun Heo   driver-core: fix ...
791
  		mutex_unlock(&gdp_mutex);
43968d2f1   Greg Kroah-Hartman   kobject: get rid ...
792
  		return k;
864062457   Kay Sievers   driver core: fix ...
793
  	}
ca22e56de   Kay Sievers   driver-core: impl...
794
795
796
  	/* subsystems can specify a default root directory for their devices */
  	if (!parent && dev->bus && dev->bus->dev_root)
  		return &dev->bus->dev_root->kobj;
864062457   Kay Sievers   driver core: fix ...
797
  	if (parent)
c744aeae9   Cornelia Huck   driver core: Allo...
798
799
800
  		return &parent->kobj;
  	return NULL;
  }
da231fd5d   Kay Sievers   Driver core: fix ...
801

cebf8fd16   Ming Lei   driver core: fix ...
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
  static inline bool live_in_glue_dir(struct kobject *kobj,
  				    struct device *dev)
  {
  	if (!kobj || !dev->class ||
  	    kobj->kset != &dev->class->p->glue_dirs)
  		return false;
  	return true;
  }
  
  static inline struct kobject *get_glue_dir(struct device *dev)
  {
  	return dev->kobj.parent;
  }
  
  /*
   * make sure cleaning up dir as the last step, we need to make
   * sure .release handler of kobject is run with holding the
   * global lock
   */
63b6971a0   Cornelia Huck   Driver core: Clea...
821
  static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
da231fd5d   Kay Sievers   Driver core: fix ...
822
  {
0f4dafc05   Kay Sievers   Kobject: auto-cle...
823
  	/* see if we live in a "glue" directory */
cebf8fd16   Ming Lei   driver core: fix ...
824
  	if (!live_in_glue_dir(glue_dir, dev))
da231fd5d   Kay Sievers   Driver core: fix ...
825
  		return;
e4a60d139   Yijing Wang   sysfs: driver cor...
826
  	mutex_lock(&gdp_mutex);
0f4dafc05   Kay Sievers   Kobject: auto-cle...
827
  	kobject_put(glue_dir);
e4a60d139   Yijing Wang   sysfs: driver cor...
828
  	mutex_unlock(&gdp_mutex);
da231fd5d   Kay Sievers   Driver core: fix ...
829
  }
63b6971a0   Cornelia Huck   Driver core: Clea...
830

2ee97caf0   Cornelia Huck   Driver core: chec...
831
832
  static int device_add_class_symlinks(struct device *dev)
  {
5590f3196   Benjamin Herrenschmidt   drivers/core/of: ...
833
  	struct device_node *of_node = dev_of_node(dev);
2ee97caf0   Cornelia Huck   Driver core: chec...
834
  	int error;
5590f3196   Benjamin Herrenschmidt   drivers/core/of: ...
835
836
837
838
839
840
841
  	if (of_node) {
  		error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
  		if (error)
  			dev_warn(dev, "Error %d creating of_node link
  ",error);
  		/* An error here doesn't warrant bringing down the device */
  	}
2ee97caf0   Cornelia Huck   Driver core: chec...
842
843
  	if (!dev->class)
  		return 0;
da231fd5d   Kay Sievers   Driver core: fix ...
844

1fbfee6c6   Greg Kroah-Hartman   class: rename "su...
845
  	error = sysfs_create_link(&dev->kobj,
6b6e39a6a   Kay Sievers   driver-core: merg...
846
  				  &dev->class->p->subsys.kobj,
2ee97caf0   Cornelia Huck   Driver core: chec...
847
848
  				  "subsystem");
  	if (error)
5590f3196   Benjamin Herrenschmidt   drivers/core/of: ...
849
  		goto out_devnode;
da231fd5d   Kay Sievers   Driver core: fix ...
850

4e886c296   Greg Kroah-Hartman   Driver core: Fix ...
851
  	if (dev->parent && device_is_not_partition(dev)) {
39aba963d   Kay Sievers   driver core: remo...
852
  		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
4f01a757e   Dmitry Torokhov   Driver core: fix ...
853
854
  					  "device");
  		if (error)
39aba963d   Kay Sievers   driver core: remo...
855
  			goto out_subsys;
2ee97caf0   Cornelia Huck   Driver core: chec...
856
  	}
2ee97caf0   Cornelia Huck   Driver core: chec...
857

ead454feb   Randy Dunlap   driver core: fix ...
858
  #ifdef CONFIG_BLOCK
39aba963d   Kay Sievers   driver core: remo...
859
  	/* /sys/block has directories and does not need symlinks */
e52eec13c   Andi Kleen   SYSFS: Allow boot...
860
  	if (sysfs_deprecated && dev->class == &block_class)
39aba963d   Kay Sievers   driver core: remo...
861
  		return 0;
ead454feb   Randy Dunlap   driver core: fix ...
862
  #endif
39aba963d   Kay Sievers   driver core: remo...
863

da231fd5d   Kay Sievers   Driver core: fix ...
864
  	/* link in the class directory pointing to the device */
6b6e39a6a   Kay Sievers   driver-core: merg...
865
  	error = sysfs_create_link(&dev->class->p->subsys.kobj,
1e0b2cf93   Kay Sievers   driver core: stru...
866
  				  &dev->kobj, dev_name(dev));
da231fd5d   Kay Sievers   Driver core: fix ...
867
  	if (error)
39aba963d   Kay Sievers   driver core: remo...
868
  		goto out_device;
da231fd5d   Kay Sievers   Driver core: fix ...
869

da231fd5d   Kay Sievers   Driver core: fix ...
870
  	return 0;
39aba963d   Kay Sievers   driver core: remo...
871
872
  out_device:
  	sysfs_remove_link(&dev->kobj, "device");
da231fd5d   Kay Sievers   Driver core: fix ...
873

2ee97caf0   Cornelia Huck   Driver core: chec...
874
875
  out_subsys:
  	sysfs_remove_link(&dev->kobj, "subsystem");
5590f3196   Benjamin Herrenschmidt   drivers/core/of: ...
876
877
  out_devnode:
  	sysfs_remove_link(&dev->kobj, "of_node");
2ee97caf0   Cornelia Huck   Driver core: chec...
878
879
880
881
882
  	return error;
  }
  
  static void device_remove_class_symlinks(struct device *dev)
  {
5590f3196   Benjamin Herrenschmidt   drivers/core/of: ...
883
884
  	if (dev_of_node(dev))
  		sysfs_remove_link(&dev->kobj, "of_node");
2ee97caf0   Cornelia Huck   Driver core: chec...
885
886
  	if (!dev->class)
  		return;
da231fd5d   Kay Sievers   Driver core: fix ...
887

4e886c296   Greg Kroah-Hartman   Driver core: Fix ...
888
  	if (dev->parent && device_is_not_partition(dev))
da231fd5d   Kay Sievers   Driver core: fix ...
889
  		sysfs_remove_link(&dev->kobj, "device");
2ee97caf0   Cornelia Huck   Driver core: chec...
890
  	sysfs_remove_link(&dev->kobj, "subsystem");
ead454feb   Randy Dunlap   driver core: fix ...
891
  #ifdef CONFIG_BLOCK
e52eec13c   Andi Kleen   SYSFS: Allow boot...
892
  	if (sysfs_deprecated && dev->class == &block_class)
39aba963d   Kay Sievers   driver core: remo...
893
  		return;
ead454feb   Randy Dunlap   driver core: fix ...
894
  #endif
6b6e39a6a   Kay Sievers   driver-core: merg...
895
  	sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2ee97caf0   Cornelia Huck   Driver core: chec...
896
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
897
  /**
413c239fa   Stephen Rothwell   driver-core: prep...
898
899
   * dev_set_name - set a device name
   * @dev: device
462323661   Randy Dunlap   dev_set_name: fix...
900
   * @fmt: format string for the device's name
413c239fa   Stephen Rothwell   driver-core: prep...
901
902
903
904
   */
  int dev_set_name(struct device *dev, const char *fmt, ...)
  {
  	va_list vargs;
1fa5ae857   Kay Sievers   driver core: get ...
905
  	int err;
413c239fa   Stephen Rothwell   driver-core: prep...
906
907
  
  	va_start(vargs, fmt);
1fa5ae857   Kay Sievers   driver core: get ...
908
  	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
413c239fa   Stephen Rothwell   driver-core: prep...
909
  	va_end(vargs);
1fa5ae857   Kay Sievers   driver core: get ...
910
  	return err;
413c239fa   Stephen Rothwell   driver-core: prep...
911
912
913
914
  }
  EXPORT_SYMBOL_GPL(dev_set_name);
  
  /**
e105b8bfc   Dan Williams   sysfs: add /sys/d...
915
916
917
918
919
920
921
   * device_to_dev_kobj - select a /sys/dev/ directory for the device
   * @dev: device
   *
   * By default we select char/ for new entries.  Setting class->dev_obj
   * to NULL prevents an entry from being created.  class->dev_kobj must
   * be set (or cleared) before any devices are registered to the class
   * otherwise device_create_sys_dev_entry() and
0d4e293ca   Peter Korsgaard   core.c: fix 'the ...
922
923
   * device_remove_sys_dev_entry() will disagree about the presence of
   * the link.
e105b8bfc   Dan Williams   sysfs: add /sys/d...
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
   */
  static struct kobject *device_to_dev_kobj(struct device *dev)
  {
  	struct kobject *kobj;
  
  	if (dev->class)
  		kobj = dev->class->dev_kobj;
  	else
  		kobj = sysfs_dev_char_kobj;
  
  	return kobj;
  }
  
  static int device_create_sys_dev_entry(struct device *dev)
  {
  	struct kobject *kobj = device_to_dev_kobj(dev);
  	int error = 0;
  	char devt_str[15];
  
  	if (kobj) {
  		format_dev_t(devt_str, dev->devt);
  		error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  	}
  
  	return error;
  }
  
  static void device_remove_sys_dev_entry(struct device *dev)
  {
  	struct kobject *kobj = device_to_dev_kobj(dev);
  	char devt_str[15];
  
  	if (kobj) {
  		format_dev_t(devt_str, dev->devt);
  		sysfs_remove_link(kobj, devt_str);
  	}
  }
b40284378   Greg Kroah-Hartman   Driver core: move...
961
962
963
964
965
966
967
968
  int device_private_init(struct device *dev)
  {
  	dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  	if (!dev->p)
  		return -ENOMEM;
  	dev->p->device = dev;
  	klist_init(&dev->p->klist_children, klist_children_get,
  		   klist_children_put);
ef8a3fd6e   Greg Kroah-Hartman   driver core: move...
969
  	INIT_LIST_HEAD(&dev->p->deferred_probe);
b40284378   Greg Kroah-Hartman   Driver core: move...
970
971
  	return 0;
  }
e105b8bfc   Dan Williams   sysfs: add /sys/d...
972
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
973
974
   * device_add - add device to device hierarchy.
   * @dev: device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
975
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
976
977
   * This is part 2 of device_register(), though may be called
   * separately _iff_ device_initialize() has been called separately.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
978
   *
5739411ac   Cornelia Huck   Driver core: Clar...
979
   * This adds @dev to the kobject hierarchy via kobject_add(), adds it
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
980
981
   * to the global and sibling lists for the device, then
   * adds it to the other relevant subsystems of the driver model.
5739411ac   Cornelia Huck   Driver core: Clar...
982
   *
b10d5efdf   Alan Stern   Documentation upd...
983
984
985
986
987
988
989
   * Do not call this routine or device_register() more than once for
   * any device structure.  The driver model core is not designed to work
   * with devices that get unregistered and then spring back to life.
   * (Among other things, it's very hard to guarantee that all references
   * to the previous incarnation of @dev have been dropped.)  Allocate
   * and register a fresh new struct device instead.
   *
5739411ac   Cornelia Huck   Driver core: Clar...
990
991
992
   * NOTE: _Never_ directly free @dev after calling this function, even
   * if it returned an error! Always use put_device() to give up your
   * reference instead.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
993
994
995
996
   */
  int device_add(struct device *dev)
  {
  	struct device *parent = NULL;
ca22e56de   Kay Sievers   driver-core: impl...
997
  	struct kobject *kobj;
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
998
  	struct class_interface *class_intf;
c906a48ad   Greg Kroah-Hartman   driver core: add ...
999
  	int error = -EINVAL;
cebf8fd16   Ming Lei   driver core: fix ...
1000
  	struct kobject *glue_dir = NULL;
775b64d2b   Rafael J. Wysocki   PM: Acquire devic...
1001

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1002
  	dev = get_device(dev);
c906a48ad   Greg Kroah-Hartman   driver core: add ...
1003
1004
  	if (!dev)
  		goto done;
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
1005
  	if (!dev->p) {
b40284378   Greg Kroah-Hartman   Driver core: move...
1006
1007
1008
  		error = device_private_init(dev);
  		if (error)
  			goto done;
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
1009
  	}
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
1010

1fa5ae857   Kay Sievers   driver core: get ...
1011
1012
1013
1014
1015
1016
  	/*
  	 * for statically allocated devices, which should all be converted
  	 * some day, we need to initialize the name. We prevent reading back
  	 * the name, and force the use of dev_name()
  	 */
  	if (dev->init_name) {
acc0e90fb   Greg Kroah-Hartman   driver core: fix ...
1017
  		dev_set_name(dev, "%s", dev->init_name);
1fa5ae857   Kay Sievers   driver core: get ...
1018
1019
  		dev->init_name = NULL;
  	}
c906a48ad   Greg Kroah-Hartman   driver core: add ...
1020

ca22e56de   Kay Sievers   driver-core: impl...
1021
1022
1023
  	/* subsystems can specify simple device enumeration */
  	if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
  		dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
e6309e756   Thomas Gleixner   Driver-core: Fix ...
1024
1025
  	if (!dev_name(dev)) {
  		error = -EINVAL;
5c8563d77   Kay Sievers   Driver Core: do n...
1026
  		goto name_error;
e6309e756   Thomas Gleixner   Driver-core: Fix ...
1027
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1028

1e0b2cf93   Kay Sievers   driver core: stru...
1029
1030
  	pr_debug("device: '%s': %s
  ", dev_name(dev), __func__);
c205ef488   Greg Kroah-Hartman   Driver core: crea...
1031

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1032
  	parent = get_device(dev->parent);
ca22e56de   Kay Sievers   driver-core: impl...
1033
1034
1035
  	kobj = get_device_parent(dev, parent);
  	if (kobj)
  		dev->kobj.parent = kobj;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036

0d358f22f   Yinghai Lu   driver core: try ...
1037
  	/* use parent numa_node */
56f2de81e   Zhen Lei   of: to support bi...
1038
  	if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
0d358f22f   Yinghai Lu   driver core: try ...
1039
  		set_dev_node(dev, dev_to_node(parent));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1040
  	/* first, register with generic layer. */
8a577ffc7   Kay Sievers   driver: dont upda...
1041
1042
  	/* we require the name to be set before, and pass NULL */
  	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
cebf8fd16   Ming Lei   driver core: fix ...
1043
1044
  	if (error) {
  		glue_dir = get_glue_dir(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1045
  		goto Error;
cebf8fd16   Ming Lei   driver core: fix ...
1046
  	}
a7fd67062   Kay Sievers   [PATCH] add sysfs...
1047

370226449   Brian Walsh   drivers/base: Pla...
1048
1049
1050
  	/* notify platform of device entry */
  	if (platform_notify)
  		platform_notify(dev);
c5e064a69   Greg Kroah-Hartman   driver core: core...
1051
  	error = device_create_file(dev, &dev_attr_uevent);
a306eea40   Cornelia Huck   driver core fixes...
1052
1053
  	if (error)
  		goto attrError;
a7fd67062   Kay Sievers   [PATCH] add sysfs...
1054

2ee97caf0   Cornelia Huck   Driver core: chec...
1055
1056
1057
  	error = device_add_class_symlinks(dev);
  	if (error)
  		goto SymlinkError;
dc0afa838   Cornelia Huck   Driver core: codi...
1058
1059
  	error = device_add_attrs(dev);
  	if (error)
2620efef7   Greg Kroah-Hartman   Driver core: add ...
1060
  		goto AttrsError;
dc0afa838   Cornelia Huck   Driver core: codi...
1061
1062
  	error = bus_add_device(dev);
  	if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1063
  		goto BusError;
3b98aeaf3   Alan Stern   PM: don't skip de...
1064
  	error = dpm_sysfs_add(dev);
57eee3d23   Rafael J. Wysocki   Driver core: Call...
1065
  	if (error)
3b98aeaf3   Alan Stern   PM: don't skip de...
1066
1067
  		goto DPMError;
  	device_pm_add(dev);
ec0676ee2   Alan Stern   Driver core: move...
1068

0cd75047d   Sergey Klyaus   driver core: fix ...
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
  	if (MAJOR(dev->devt)) {
  		error = device_create_file(dev, &dev_attr_dev);
  		if (error)
  			goto DevAttrError;
  
  		error = device_create_sys_dev_entry(dev);
  		if (error)
  			goto SysEntryError;
  
  		devtmpfs_create_node(dev);
  	}
ec0676ee2   Alan Stern   Driver core: move...
1080
  	/* Notify clients of device addition.  This call must come
268863f43   majianpeng   base/core.c:fix t...
1081
  	 * after dpm_sysfs_add() and before kobject_uevent().
ec0676ee2   Alan Stern   Driver core: move...
1082
1083
1084
1085
  	 */
  	if (dev->bus)
  		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  					     BUS_NOTIFY_ADD_DEVICE, dev);
83b5fb4cc   Cornelia Huck   Driver core: supp...
1086
  	kobject_uevent(&dev->kobj, KOBJ_ADD);
2023c610d   Alan Stern   Driver core: add ...
1087
  	bus_probe_device(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1088
  	if (parent)
f791b8c83   Greg Kroah-Hartman   driver core: move...
1089
1090
  		klist_add_tail(&dev->p->knode_parent,
  			       &parent->p->klist_children);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1091

5d9fd169c   Greg Kroah-Hartman   [PATCH] Driver co...
1092
  	if (dev->class) {
ca22e56de   Kay Sievers   driver-core: impl...
1093
  		mutex_lock(&dev->class->p->mutex);
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1094
  		/* tie the class to the device */
5a3ceb861   Tejun Heo   driver-core: use ...
1095
  		klist_add_tail(&dev->knode_class,
6b6e39a6a   Kay Sievers   driver-core: merg...
1096
  			       &dev->class->p->klist_devices);
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1097
1098
  
  		/* notify any interfaces that the device is here */
184f1f779   Greg Kroah-Hartman   class: rename "in...
1099
  		list_for_each_entry(class_intf,
ca22e56de   Kay Sievers   driver-core: impl...
1100
  				    &dev->class->p->interfaces, node)
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1101
1102
  			if (class_intf->add_dev)
  				class_intf->add_dev(dev, class_intf);
ca22e56de   Kay Sievers   driver-core: impl...
1103
  		mutex_unlock(&dev->class->p->mutex);
5d9fd169c   Greg Kroah-Hartman   [PATCH] Driver co...
1104
  	}
c906a48ad   Greg Kroah-Hartman   driver core: add ...
1105
  done:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1106
1107
  	put_device(dev);
  	return error;
0cd75047d   Sergey Klyaus   driver core: fix ...
1108
1109
1110
1111
1112
1113
   SysEntryError:
  	if (MAJOR(dev->devt))
  		device_remove_file(dev, &dev_attr_dev);
   DevAttrError:
  	device_pm_remove(dev);
  	dpm_sysfs_remove(dev);
3b98aeaf3   Alan Stern   PM: don't skip de...
1114
   DPMError:
57eee3d23   Rafael J. Wysocki   Driver core: Call...
1115
1116
  	bus_remove_device(dev);
   BusError:
82f0cf9b7   James Simmons   Driver core: fix ...
1117
  	device_remove_attrs(dev);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
1118
   AttrsError:
2ee97caf0   Cornelia Huck   Driver core: chec...
1119
1120
  	device_remove_class_symlinks(dev);
   SymlinkError:
c5e064a69   Greg Kroah-Hartman   driver core: core...
1121
  	device_remove_file(dev, &dev_attr_uevent);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1122
   attrError:
312c004d3   Kay Sievers   [PATCH] driver co...
1123
  	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
cebf8fd16   Ming Lei   driver core: fix ...
1124
  	glue_dir = get_glue_dir(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1125
1126
  	kobject_del(&dev->kobj);
   Error:
cebf8fd16   Ming Lei   driver core: fix ...
1127
  	cleanup_glue_dir(dev, glue_dir);
5f0163a5e   Markus Elfring   driver core: Dele...
1128
  	put_device(parent);
5c8563d77   Kay Sievers   Driver Core: do n...
1129
1130
1131
  name_error:
  	kfree(dev->p);
  	dev->p = NULL;
c906a48ad   Greg Kroah-Hartman   driver core: add ...
1132
  	goto done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1133
  }
86df26870   David Graham White   drivers:base:core...
1134
  EXPORT_SYMBOL_GPL(device_add);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1135

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1136
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1137
1138
   * device_register - register a device with the system.
   * @dev: pointer to the device structure
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1139
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1140
1141
1142
1143
1144
1145
   * This happens in two clean steps - initialize the device
   * and add it to the system. The two steps can be called
   * separately, but this is the easiest and most common.
   * I.e. you should only call the two helpers separately if
   * have a clearly defined need to use and refcount the device
   * before it is added to the hierarchy.
5739411ac   Cornelia Huck   Driver core: Clar...
1146
   *
b10d5efdf   Alan Stern   Documentation upd...
1147
1148
1149
   * For more information, see the kerneldoc for device_initialize()
   * and device_add().
   *
5739411ac   Cornelia Huck   Driver core: Clar...
1150
1151
1152
   * NOTE: _Never_ directly free @dev after calling this function, even
   * if it returned an error! Always use put_device() to give up the
   * reference initialized in this function instead.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1153
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1154
1155
1156
1157
1158
  int device_register(struct device *dev)
  {
  	device_initialize(dev);
  	return device_add(dev);
  }
86df26870   David Graham White   drivers:base:core...
1159
  EXPORT_SYMBOL_GPL(device_register);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1160

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1161
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1162
1163
   * get_device - increment reference count for device.
   * @dev: device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1164
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1165
1166
1167
   * This simply forwards the call to kobject_get(), though
   * we do take care to provide for the case that we get a NULL
   * pointer passed in.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1168
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1169
  struct device *get_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1170
  {
b0d1f807f   Lars-Peter Clausen   driver-core: Use ...
1171
  	return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1172
  }
86df26870   David Graham White   drivers:base:core...
1173
  EXPORT_SYMBOL_GPL(get_device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1174

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1175
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1176
1177
   * put_device - decrement reference count.
   * @dev: device in question.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1178
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1179
  void put_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1180
  {
edfaa7c36   Kay Sievers   Driver core: conv...
1181
  	/* might_sleep(); */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1182
1183
1184
  	if (dev)
  		kobject_put(&dev->kobj);
  }
86df26870   David Graham White   drivers:base:core...
1185
  EXPORT_SYMBOL_GPL(put_device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1186

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1187
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1188
1189
   * device_del - delete device from system.
   * @dev: device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1190
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1191
1192
1193
1194
1195
   * This is the first part of the device unregistration
   * sequence. This removes the device from the lists we control
   * from here, has it removed from the other driver model
   * subsystems it was added to in device_add(), and removes it
   * from the kobject hierarchy.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1196
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1197
1198
   * NOTE: this should be called manually _iff_ device_add() was
   * also called manually.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1199
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1200
  void device_del(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1201
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1202
  	struct device *parent = dev->parent;
cebf8fd16   Ming Lei   driver core: fix ...
1203
  	struct kobject *glue_dir = NULL;
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1204
  	struct class_interface *class_intf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1205

ec0676ee2   Alan Stern   Driver core: move...
1206
1207
1208
1209
1210
1211
  	/* Notify clients of device removal.  This call must come
  	 * before dpm_sysfs_remove().
  	 */
  	if (dev->bus)
  		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  					     BUS_NOTIFY_DEL_DEVICE, dev);
3b98aeaf3   Alan Stern   PM: don't skip de...
1212
  	dpm_sysfs_remove(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1213
  	if (parent)
f791b8c83   Greg Kroah-Hartman   driver core: move...
1214
  		klist_del(&dev->p->knode_parent);
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1215
  	if (MAJOR(dev->devt)) {
2b2af54a5   Kay Sievers   Driver Core: devt...
1216
  		devtmpfs_delete_node(dev);
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1217
  		device_remove_sys_dev_entry(dev);
c5e064a69   Greg Kroah-Hartman   driver core: core...
1218
  		device_remove_file(dev, &dev_attr_dev);
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1219
  	}
b9d9c82b4   Kay Sievers   [PATCH] Driver co...
1220
  	if (dev->class) {
da231fd5d   Kay Sievers   Driver core: fix ...
1221
  		device_remove_class_symlinks(dev);
99ef3ef8d   Kay Sievers   CONFIG_SYSFS_DEPR...
1222

ca22e56de   Kay Sievers   driver-core: impl...
1223
  		mutex_lock(&dev->class->p->mutex);
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1224
  		/* notify any interfaces that the device is now gone */
184f1f779   Greg Kroah-Hartman   class: rename "in...
1225
  		list_for_each_entry(class_intf,
ca22e56de   Kay Sievers   driver-core: impl...
1226
  				    &dev->class->p->interfaces, node)
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1227
1228
1229
  			if (class_intf->remove_dev)
  				class_intf->remove_dev(dev, class_intf);
  		/* remove the device from the class list */
5a3ceb861   Tejun Heo   driver-core: use ...
1230
  		klist_del(&dev->knode_class);
ca22e56de   Kay Sievers   driver-core: impl...
1231
  		mutex_unlock(&dev->class->p->mutex);
b9d9c82b4   Kay Sievers   [PATCH] Driver co...
1232
  	}
c5e064a69   Greg Kroah-Hartman   driver core: core...
1233
  	device_remove_file(dev, &dev_attr_uevent);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
1234
  	device_remove_attrs(dev);
289535334   Benjamin Herrenschmidt   Driver core: Call...
1235
  	bus_remove_device(dev);
4b6d1f12f   LongX Zhang   driver core / PM:...
1236
  	device_pm_remove(dev);
d1c3414c2   Grant Likely   drivercore: Add d...
1237
  	driver_deferred_probe_del(dev);
478573c93   Lukas Wunner   driver core: Don'...
1238
  	device_remove_properties(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1239
1240
1241
1242
1243
1244
  
  	/* Notify the platform of the removal, in case they
  	 * need to do anything...
  	 */
  	if (platform_notify_remove)
  		platform_notify_remove(dev);
599bad38c   Joerg Roedel   driver core: Add ...
1245
1246
1247
  	if (dev->bus)
  		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  					     BUS_NOTIFY_REMOVED_DEVICE, dev);
312c004d3   Kay Sievers   [PATCH] driver co...
1248
  	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
cebf8fd16   Ming Lei   driver core: fix ...
1249
  	glue_dir = get_glue_dir(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
  	kobject_del(&dev->kobj);
cebf8fd16   Ming Lei   driver core: fix ...
1251
  	cleanup_glue_dir(dev, glue_dir);
da231fd5d   Kay Sievers   Driver core: fix ...
1252
  	put_device(parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1253
  }
86df26870   David Graham White   drivers:base:core...
1254
  EXPORT_SYMBOL_GPL(device_del);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1255
1256
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1257
1258
   * device_unregister - unregister device from system.
   * @dev: device going away.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1259
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1260
1261
1262
1263
1264
1265
   * We do this in two parts, like we do device_register(). First,
   * we remove it from all the subsystems with device_del(), then
   * we decrement the reference count via put_device(). If that
   * is the final reference count, the device will be cleaned up
   * via device_release() above. Otherwise, the structure will
   * stick around until the final reference to the device is dropped.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1266
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1267
  void device_unregister(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1268
  {
1e0b2cf93   Kay Sievers   driver core: stru...
1269
1270
  	pr_debug("device: '%s': %s
  ", dev_name(dev), __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1271
1272
1273
  	device_del(dev);
  	put_device(dev);
  }
86df26870   David Graham White   drivers:base:core...
1274
  EXPORT_SYMBOL_GPL(device_unregister);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1275

3d060aeb7   Andy Shevchenko   driver core: impl...
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
  static struct device *prev_device(struct klist_iter *i)
  {
  	struct klist_node *n = klist_prev(i);
  	struct device *dev = NULL;
  	struct device_private *p;
  
  	if (n) {
  		p = to_device_private_parent(n);
  		dev = p->device;
  	}
  	return dev;
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1288
  static struct device *next_device(struct klist_iter *i)
36239577c   Patrick Mochel   [PATCH] Use a kli...
1289
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1290
  	struct klist_node *n = klist_next(i);
f791b8c83   Greg Kroah-Hartman   driver core: move...
1291
1292
1293
1294
1295
1296
1297
1298
  	struct device *dev = NULL;
  	struct device_private *p;
  
  	if (n) {
  		p = to_device_private_parent(n);
  		dev = p->device;
  	}
  	return dev;
36239577c   Patrick Mochel   [PATCH] Use a kli...
1299
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1300
  /**
e454cea20   Kay Sievers   Driver-Core: exte...
1301
   * device_get_devnode - path of device node file
6fcf53acc   Kay Sievers   Driver Core: add ...
1302
   * @dev: device
e454cea20   Kay Sievers   Driver-Core: exte...
1303
   * @mode: returned file access mode
3c2670e65   Kay Sievers   driver core: add ...
1304
1305
   * @uid: returned file owner
   * @gid: returned file group
6fcf53acc   Kay Sievers   Driver Core: add ...
1306
1307
1308
1309
1310
1311
1312
   * @tmp: possibly allocated string
   *
   * Return the relative path of a possible device node.
   * Non-default names may need to allocate a memory to compose
   * a name. This memory is returned in tmp and needs to be
   * freed by the caller.
   */
e454cea20   Kay Sievers   Driver-Core: exte...
1313
  const char *device_get_devnode(struct device *dev,
4e4098a3e   Greg Kroah-Hartman   driver core: hand...
1314
  			       umode_t *mode, kuid_t *uid, kgid_t *gid,
3c2670e65   Kay Sievers   driver core: add ...
1315
  			       const char **tmp)
6fcf53acc   Kay Sievers   Driver Core: add ...
1316
1317
1318
1319
1320
1321
  {
  	char *s;
  
  	*tmp = NULL;
  
  	/* the device type may provide a specific name */
e454cea20   Kay Sievers   Driver-Core: exte...
1322
  	if (dev->type && dev->type->devnode)
3c2670e65   Kay Sievers   driver core: add ...
1323
  		*tmp = dev->type->devnode(dev, mode, uid, gid);
6fcf53acc   Kay Sievers   Driver Core: add ...
1324
1325
1326
1327
  	if (*tmp)
  		return *tmp;
  
  	/* the class may provide a specific name */
e454cea20   Kay Sievers   Driver-Core: exte...
1328
1329
  	if (dev->class && dev->class->devnode)
  		*tmp = dev->class->devnode(dev, mode);
6fcf53acc   Kay Sievers   Driver Core: add ...
1330
1331
1332
1333
1334
1335
1336
1337
  	if (*tmp)
  		return *tmp;
  
  	/* return name without allocation, tmp == NULL */
  	if (strchr(dev_name(dev), '!') == NULL)
  		return dev_name(dev);
  
  	/* replace '!' in the name with '/' */
a29fd614a   Rasmus Villemoes   drivers/base/core...
1338
1339
  	s = kstrdup(dev_name(dev), GFP_KERNEL);
  	if (!s)
6fcf53acc   Kay Sievers   Driver Core: add ...
1340
  		return NULL;
a29fd614a   Rasmus Villemoes   drivers/base/core...
1341
1342
  	strreplace(s, '!', '/');
  	return *tmp = s;
6fcf53acc   Kay Sievers   Driver Core: add ...
1343
1344
1345
  }
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1346
1347
   * device_for_each_child - device child iterator.
   * @parent: parent struct device.
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1348
   * @fn: function to be called for each device.
f8878dcb8   Robert P. J. Day   Documentation: Ti...
1349
   * @data: data for the callback.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1350
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1351
1352
   * Iterate over @parent's child devices, and call @fn for each,
   * passing it @data.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1353
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1354
1355
   * We check the return of @fn each time. If it returns anything
   * other than 0, we break out and return that value.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1356
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1357
1358
  int device_for_each_child(struct device *parent, void *data,
  			  int (*fn)(struct device *dev, void *data))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1359
  {
36239577c   Patrick Mochel   [PATCH] Use a kli...
1360
  	struct klist_iter i;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1361
  	struct device *child;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1362
  	int error = 0;
014c90dbb   Greg Kroah-Hartman   driver core: prev...
1363
1364
  	if (!parent->p)
  		return 0;
f791b8c83   Greg Kroah-Hartman   driver core: move...
1365
  	klist_iter_init(&parent->p->klist_children, &i);
36239577c   Patrick Mochel   [PATCH] Use a kli...
1366
1367
1368
  	while ((child = next_device(&i)) && !error)
  		error = fn(child, data);
  	klist_iter_exit(&i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1369
1370
  	return error;
  }
86df26870   David Graham White   drivers:base:core...
1371
  EXPORT_SYMBOL_GPL(device_for_each_child);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1372

5ab699810   Cornelia Huck   driver core: Intr...
1373
  /**
3d060aeb7   Andy Shevchenko   driver core: impl...
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
   * device_for_each_child_reverse - device child iterator in reversed order.
   * @parent: parent struct device.
   * @fn: function to be called for each device.
   * @data: data for the callback.
   *
   * Iterate over @parent's child devices, and call @fn for each,
   * passing it @data.
   *
   * We check the return of @fn each time. If it returns anything
   * other than 0, we break out and return that value.
   */
  int device_for_each_child_reverse(struct device *parent, void *data,
  				  int (*fn)(struct device *dev, void *data))
  {
  	struct klist_iter i;
  	struct device *child;
  	int error = 0;
  
  	if (!parent->p)
  		return 0;
  
  	klist_iter_init(&parent->p->klist_children, &i);
  	while ((child = prev_device(&i)) && !error)
  		error = fn(child, data);
  	klist_iter_exit(&i);
  	return error;
  }
  EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
  
  /**
5ab699810   Cornelia Huck   driver core: Intr...
1404
1405
   * device_find_child - device iterator for locating a particular device.
   * @parent: parent struct device
5ab699810   Cornelia Huck   driver core: Intr...
1406
   * @match: Callback function to check device
f8878dcb8   Robert P. J. Day   Documentation: Ti...
1407
   * @data: Data to pass to match function
5ab699810   Cornelia Huck   driver core: Intr...
1408
1409
1410
1411
1412
1413
1414
1415
1416
   *
   * This is similar to the device_for_each_child() function above, but it
   * returns a reference to a device that is 'found' for later use, as
   * determined by the @match callback.
   *
   * The callback should return 0 if the device doesn't match and non-zero
   * if it does.  If the callback returns non-zero and a reference to the
   * current device can be obtained, this function will return to the caller
   * and not iterate over any more devices.
a4e2400a6   Federico Vaga   base/core.c: impr...
1417
1418
   *
   * NOTE: you will need to drop the reference with put_device() after use.
5ab699810   Cornelia Huck   driver core: Intr...
1419
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1420
1421
  struct device *device_find_child(struct device *parent, void *data,
  				 int (*match)(struct device *dev, void *data))
5ab699810   Cornelia Huck   driver core: Intr...
1422
1423
1424
1425
1426
1427
  {
  	struct klist_iter i;
  	struct device *child;
  
  	if (!parent)
  		return NULL;
f791b8c83   Greg Kroah-Hartman   driver core: move...
1428
  	klist_iter_init(&parent->p->klist_children, &i);
5ab699810   Cornelia Huck   driver core: Intr...
1429
1430
1431
1432
1433
1434
  	while ((child = next_device(&i)))
  		if (match(child, data) && get_device(child))
  			break;
  	klist_iter_exit(&i);
  	return child;
  }
86df26870   David Graham White   drivers:base:core...
1435
  EXPORT_SYMBOL_GPL(device_find_child);
5ab699810   Cornelia Huck   driver core: Intr...
1436

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1437
1438
  int __init devices_init(void)
  {
881c6cfd7   Greg Kroah-Hartman   kset: convert /sy...
1439
1440
1441
  	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  	if (!devices_kset)
  		return -ENOMEM;
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1442
1443
1444
1445
1446
1447
1448
1449
1450
  	dev_kobj = kobject_create_and_add("dev", NULL);
  	if (!dev_kobj)
  		goto dev_kobj_err;
  	sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  	if (!sysfs_dev_block_kobj)
  		goto block_kobj_err;
  	sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  	if (!sysfs_dev_char_kobj)
  		goto char_kobj_err;
881c6cfd7   Greg Kroah-Hartman   kset: convert /sy...
1451
  	return 0;
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1452
1453
1454
1455
1456
1457
1458
1459
  
   char_kobj_err:
  	kobject_put(sysfs_dev_block_kobj);
   block_kobj_err:
  	kobject_put(dev_kobj);
   dev_kobj_err:
  	kset_unregister(devices_kset);
  	return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1460
  }
4f3549d72   Rafael J. Wysocki   Driver core: Add ...
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
  static int device_check_offline(struct device *dev, void *not_used)
  {
  	int ret;
  
  	ret = device_for_each_child(dev, NULL, device_check_offline);
  	if (ret)
  		return ret;
  
  	return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
  }
  
  /**
   * device_offline - Prepare the device for hot-removal.
   * @dev: Device to be put offline.
   *
   * Execute the device bus type's .offline() callback, if present, to prepare
   * the device for a subsequent hot-removal.  If that succeeds, the device must
   * not be used until either it is removed or its bus type's .online() callback
   * is executed.
   *
   * Call under device_hotplug_lock.
   */
  int device_offline(struct device *dev)
  {
  	int ret;
  
  	if (dev->offline_disabled)
  		return -EPERM;
  
  	ret = device_for_each_child(dev, NULL, device_check_offline);
  	if (ret)
  		return ret;
  
  	device_lock(dev);
  	if (device_supports_offline(dev)) {
  		if (dev->offline) {
  			ret = 1;
  		} else {
  			ret = dev->bus->offline(dev);
  			if (!ret) {
  				kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  				dev->offline = true;
  			}
  		}
  	}
  	device_unlock(dev);
  
  	return ret;
  }
  
  /**
   * device_online - Put the device back online after successful device_offline().
   * @dev: Device to be put back online.
   *
   * If device_offline() has been successfully executed for @dev, but the device
   * has not been removed subsequently, execute its bus type's .online() callback
   * to indicate that the device can be used again.
   *
   * Call under device_hotplug_lock.
   */
  int device_online(struct device *dev)
  {
  	int ret = 0;
  
  	device_lock(dev);
  	if (device_supports_offline(dev)) {
  		if (dev->offline) {
  			ret = dev->bus->online(dev);
  			if (!ret) {
  				kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  				dev->offline = false;
  			}
  		} else {
  			ret = 1;
  		}
  	}
  	device_unlock(dev);
  
  	return ret;
  }
7f100d156   Karthigan Srinivasan   drivers/base/core...
1541
  struct root_device {
0aa0dc41b   Mark McLoughlin   driver core: add ...
1542
1543
1544
  	struct device dev;
  	struct module *owner;
  };
93058424a   Josh Triplett   drivers/base/core...
1545
  static inline struct root_device *to_root_device(struct device *d)
481e20799   Ferenc Wagner   driver core: Repl...
1546
1547
1548
  {
  	return container_of(d, struct root_device, dev);
  }
0aa0dc41b   Mark McLoughlin   driver core: add ...
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
  
  static void root_device_release(struct device *dev)
  {
  	kfree(to_root_device(dev));
  }
  
  /**
   * __root_device_register - allocate and register a root device
   * @name: root device name
   * @owner: owner module of the root device, usually THIS_MODULE
   *
   * This function allocates a root device and registers it
   * using device_register(). In order to free the returned
   * device, use root_device_unregister().
   *
   * Root devices are dummy devices which allow other devices
   * to be grouped under /sys/devices. Use this function to
   * allocate a root device and then use it as the parent of
   * any device which should appear under /sys/devices/{name}
   *
   * The /sys/devices/{name} directory will also contain a
   * 'module' symlink which points to the @owner directory
   * in sysfs.
   *
f0eae0ed3   Jani Nikula   driver-core: docu...
1573
1574
   * Returns &struct device pointer on success, or ERR_PTR() on error.
   *
0aa0dc41b   Mark McLoughlin   driver core: add ...
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
   * Note: You probably want to use root_device_register().
   */
  struct device *__root_device_register(const char *name, struct module *owner)
  {
  	struct root_device *root;
  	int err = -ENOMEM;
  
  	root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  	if (!root)
  		return ERR_PTR(err);
acc0e90fb   Greg Kroah-Hartman   driver core: fix ...
1585
  	err = dev_set_name(&root->dev, "%s", name);
0aa0dc41b   Mark McLoughlin   driver core: add ...
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
  	if (err) {
  		kfree(root);
  		return ERR_PTR(err);
  	}
  
  	root->dev.release = root_device_release;
  
  	err = device_register(&root->dev);
  	if (err) {
  		put_device(&root->dev);
  		return ERR_PTR(err);
  	}
1d9e882ba   Christoph Egger   driver-core: fix ...
1598
  #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
0aa0dc41b   Mark McLoughlin   driver core: add ...
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
  	if (owner) {
  		struct module_kobject *mk = &owner->mkobj;
  
  		err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  		if (err) {
  			device_unregister(&root->dev);
  			return ERR_PTR(err);
  		}
  		root->owner = owner;
  	}
  #endif
  
  	return &root->dev;
  }
  EXPORT_SYMBOL_GPL(__root_device_register);
  
  /**
   * root_device_unregister - unregister and free a root device
7cbcf2254   Randy Dunlap   driver-core: fix ...
1617
   * @dev: device going away
0aa0dc41b   Mark McLoughlin   driver core: add ...
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
   *
   * This function unregisters and cleans up a device that was created by
   * root_device_register().
   */
  void root_device_unregister(struct device *dev)
  {
  	struct root_device *root = to_root_device(dev);
  
  	if (root->owner)
  		sysfs_remove_link(&root->dev.kobj, "module");
  
  	device_unregister(dev);
  }
  EXPORT_SYMBOL_GPL(root_device_unregister);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1632
1633
1634
  
  static void device_create_release(struct device *dev)
  {
1e0b2cf93   Kay Sievers   driver core: stru...
1635
1636
  	pr_debug("device: '%s': %s
  ", dev_name(dev), __func__);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1637
1638
  	kfree(dev);
  }
39ef31120   Guenter Roeck   driver core: Intr...
1639
1640
1641
1642
1643
  static struct device *
  device_create_groups_vargs(struct class *class, struct device *parent,
  			   dev_t devt, void *drvdata,
  			   const struct attribute_group **groups,
  			   const char *fmt, va_list args)
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1644
  {
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1645
1646
1647
1648
1649
  	struct device *dev = NULL;
  	int retval = -ENODEV;
  
  	if (class == NULL || IS_ERR(class))
  		goto error;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1650
1651
1652
1653
1654
1655
  
  	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  	if (!dev) {
  		retval = -ENOMEM;
  		goto error;
  	}
bbc780f8b   David Herrmann   driver core: fix ...
1656
  	device_initialize(dev);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1657
1658
1659
  	dev->devt = devt;
  	dev->class = class;
  	dev->parent = parent;
39ef31120   Guenter Roeck   driver core: Intr...
1660
  	dev->groups = groups;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1661
  	dev->release = device_create_release;
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1662
  	dev_set_drvdata(dev, drvdata);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1663

1fa5ae857   Kay Sievers   driver core: get ...
1664
1665
1666
  	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  	if (retval)
  		goto error;
bbc780f8b   David Herrmann   driver core: fix ...
1667
  	retval = device_add(dev);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1668
1669
  	if (retval)
  		goto error;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1670
1671
1672
  	return dev;
  
  error:
286661b37   Cornelia Huck   Driver core: Fix ...
1673
  	put_device(dev);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1674
1675
  	return ERR_PTR(retval);
  }
39ef31120   Guenter Roeck   driver core: Intr...
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
  
  /**
   * device_create_vargs - creates a device and registers it with sysfs
   * @class: pointer to the struct class that this device should be registered to
   * @parent: pointer to the parent struct device of this new device, if any
   * @devt: the dev_t for the char device to be added
   * @drvdata: the data to be added to the device for callbacks
   * @fmt: string for the device's name
   * @args: va_list for the device's name
   *
   * This function can be used by char device classes.  A struct device
   * will be created in sysfs, registered to the specified class.
   *
   * A "dev" file will be created, showing the dev_t for the device, if
   * the dev_t is not 0,0.
   * If a pointer to a parent struct device is passed in, the newly created
   * struct device will be a child of that device in sysfs.
   * The pointer to the struct device will be returned from the call.
   * Any further sysfs files that might be required can be created using this
   * pointer.
   *
   * Returns &struct device pointer on success, or ERR_PTR() on error.
   *
   * Note: the struct class passed to this function must have previously
   * been created with a call to class_create().
   */
  struct device *device_create_vargs(struct class *class, struct device *parent,
  				   dev_t devt, void *drvdata, const char *fmt,
  				   va_list args)
  {
  	return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
  					  fmt, args);
  }
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1709
1710
1711
  EXPORT_SYMBOL_GPL(device_create_vargs);
  
  /**
4e1067394   Greg Kroah-Hartman   device create: co...
1712
   * device_create - creates a device and registers it with sysfs
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
   * @class: pointer to the struct class that this device should be registered to
   * @parent: pointer to the parent struct device of this new device, if any
   * @devt: the dev_t for the char device to be added
   * @drvdata: the data to be added to the device for callbacks
   * @fmt: string for the device's name
   *
   * This function can be used by char device classes.  A struct device
   * will be created in sysfs, registered to the specified class.
   *
   * A "dev" file will be created, showing the dev_t for the device, if
   * the dev_t is not 0,0.
   * If a pointer to a parent struct device is passed in, the newly created
   * struct device will be a child of that device in sysfs.
   * The pointer to the struct device will be returned from the call.
   * Any further sysfs files that might be required can be created using this
   * pointer.
   *
f0eae0ed3   Jani Nikula   driver-core: docu...
1730
1731
   * Returns &struct device pointer on success, or ERR_PTR() on error.
   *
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1732
1733
1734
   * Note: the struct class passed to this function must have previously
   * been created with a call to class_create().
   */
4e1067394   Greg Kroah-Hartman   device create: co...
1735
1736
  struct device *device_create(struct class *class, struct device *parent,
  			     dev_t devt, void *drvdata, const char *fmt, ...)
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1737
1738
1739
1740
1741
1742
1743
1744
1745
  {
  	va_list vargs;
  	struct device *dev;
  
  	va_start(vargs, fmt);
  	dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  	va_end(vargs);
  	return dev;
  }
4e1067394   Greg Kroah-Hartman   device create: co...
1746
  EXPORT_SYMBOL_GPL(device_create);
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1747

39ef31120   Guenter Roeck   driver core: Intr...
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
  /**
   * device_create_with_groups - creates a device and registers it with sysfs
   * @class: pointer to the struct class that this device should be registered to
   * @parent: pointer to the parent struct device of this new device, if any
   * @devt: the dev_t for the char device to be added
   * @drvdata: the data to be added to the device for callbacks
   * @groups: NULL-terminated list of attribute groups to be created
   * @fmt: string for the device's name
   *
   * This function can be used by char device classes.  A struct device
   * will be created in sysfs, registered to the specified class.
   * Additional attributes specified in the groups parameter will also
   * be created automatically.
   *
   * A "dev" file will be created, showing the dev_t for the device, if
   * the dev_t is not 0,0.
   * If a pointer to a parent struct device is passed in, the newly created
   * struct device will be a child of that device in sysfs.
   * The pointer to the struct device will be returned from the call.
   * Any further sysfs files that might be required can be created using this
   * pointer.
   *
   * Returns &struct device pointer on success, or ERR_PTR() on error.
   *
   * Note: the struct class passed to this function must have previously
   * been created with a call to class_create().
   */
  struct device *device_create_with_groups(struct class *class,
  					 struct device *parent, dev_t devt,
  					 void *drvdata,
  					 const struct attribute_group **groups,
  					 const char *fmt, ...)
  {
  	va_list vargs;
  	struct device *dev;
  
  	va_start(vargs, fmt);
  	dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
  					 fmt, vargs);
  	va_end(vargs);
  	return dev;
  }
  EXPORT_SYMBOL_GPL(device_create_with_groups);
9f3b795a6   MichaÅ‚ MirosÅ‚aw   driver-core: cons...
1791
  static int __match_devt(struct device *dev, const void *data)
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1792
  {
9f3b795a6   MichaÅ‚ MirosÅ‚aw   driver-core: cons...
1793
  	const dev_t *devt = data;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1794

cd35449b9   Dave Young   driver core: conv...
1795
  	return dev->devt == *devt;
775b64d2b   Rafael J. Wysocki   PM: Acquire devic...
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
  }
  
  /**
   * device_destroy - removes a device that was created with device_create()
   * @class: pointer to the struct class that this device was registered with
   * @devt: the dev_t of the device that was previously registered
   *
   * This call unregisters and cleans up a device that was created with a
   * call to device_create().
   */
  void device_destroy(struct class *class, dev_t devt)
  {
  	struct device *dev;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1809

695794ae0   Greg Kroah-Hartman   Driver Core: add ...
1810
  	dev = class_find_device(class, NULL, &devt, __match_devt);
cd35449b9   Dave Young   driver core: conv...
1811
1812
  	if (dev) {
  		put_device(dev);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1813
  		device_unregister(dev);
cd35449b9   Dave Young   driver core: conv...
1814
  	}
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1815
1816
  }
  EXPORT_SYMBOL_GPL(device_destroy);
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1817
1818
1819
1820
1821
  
  /**
   * device_rename - renames a device
   * @dev: the pointer to the struct device to be renamed
   * @new_name: the new name of the device
030c1d2bf   Eric W. Biederman   kobject: Fix kobj...
1822
1823
1824
1825
1826
   *
   * It is the responsibility of the caller to provide mutual
   * exclusion between two different calls of device_rename
   * on the same device to ensure that new_name is valid and
   * won't conflict with other devices.
c6c0ac664   Michael Ellerman   driver core: Docu...
1827
   *
a5462516a   Timur Tabi   driver-core: docu...
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
   * Note: Don't call this function.  Currently, the networking layer calls this
   * function, but that will change.  The following text from Kay Sievers offers
   * some insight:
   *
   * Renaming devices is racy at many levels, symlinks and other stuff are not
   * replaced atomically, and you get a "move" uevent, but it's not easy to
   * connect the event to the old and new device. Device nodes are not renamed at
   * all, there isn't even support for that in the kernel now.
   *
   * In the meantime, during renaming, your target name might be taken by another
   * driver, creating conflicts. Or the old name is taken directly after you
   * renamed it -- then you get events for the same DEVPATH, before you even see
   * the "move" event. It's just a mess, and nothing new should ever rely on
   * kernel device renaming. Besides that, it's not even implemented now for
   * other things than (driver-core wise very simple) network devices.
   *
   * We are currently about to change network renaming in udev to completely
   * disallow renaming of devices in the same namespace as the kernel uses,
   * because we can't solve the problems properly, that arise with swapping names
   * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
   * be allowed to some other name than eth[0-9]*, for the aforementioned
   * reasons.
   *
   * Make up a "real" name in the driver before you register anything, or add
   * some other attributes for userspace to find the device, or use udev to add
   * symlinks -- but never rename kernel devices later, it's a complete mess. We
   * don't even want to get into that and try to implement the missing pieces in
   * the core. We really have other pieces to fix in the driver core mess. :)
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1856
   */
6937e8f8c   Johannes Berg   driver core: devi...
1857
  int device_rename(struct device *dev, const char *new_name)
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1858
  {
4b30ee58e   Tejun Heo   sysfs: remove kty...
1859
  	struct kobject *kobj = &dev->kobj;
2ee97caf0   Cornelia Huck   Driver core: chec...
1860
  	char *old_device_name = NULL;
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1861
1862
1863
1864
1865
  	int error;
  
  	dev = get_device(dev);
  	if (!dev)
  		return -EINVAL;
69df75334   ethan.zhao   drivers/base/core...
1866
1867
  	dev_dbg(dev, "renaming to %s
  ", new_name);
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1868

1fa5ae857   Kay Sievers   driver core: get ...
1869
  	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2ee97caf0   Cornelia Huck   Driver core: chec...
1870
1871
1872
  	if (!old_device_name) {
  		error = -ENOMEM;
  		goto out;
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1873
  	}
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1874

f349cf347   Eric W. Biederman   driver core: Impl...
1875
  	if (dev->class) {
4b30ee58e   Tejun Heo   sysfs: remove kty...
1876
1877
1878
  		error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
  					     kobj, old_device_name,
  					     new_name, kobject_namespace(kobj));
f349cf347   Eric W. Biederman   driver core: Impl...
1879
1880
1881
  		if (error)
  			goto out;
  	}
39aba963d   Kay Sievers   driver core: remo...
1882

4b30ee58e   Tejun Heo   sysfs: remove kty...
1883
  	error = kobject_rename(kobj, new_name);
1fa5ae857   Kay Sievers   driver core: get ...
1884
  	if (error)
2ee97caf0   Cornelia Huck   Driver core: chec...
1885
  		goto out;
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1886

2ee97caf0   Cornelia Huck   Driver core: chec...
1887
  out:
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1888
  	put_device(dev);
2ee97caf0   Cornelia Huck   Driver core: chec...
1889
  	kfree(old_device_name);
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1890
1891
1892
  
  	return error;
  }
a2807dbcb   Johannes Berg   driver core: expo...
1893
  EXPORT_SYMBOL_GPL(device_rename);
8a82472f8   Cornelia Huck   driver core: Intr...
1894
1895
1896
1897
1898
  
  static int device_move_class_links(struct device *dev,
  				   struct device *old_parent,
  				   struct device *new_parent)
  {
f7f3461d8   Greg Kroah-Hartman   Driver core: add ...
1899
  	int error = 0;
8a82472f8   Cornelia Huck   driver core: Intr...
1900

f7f3461d8   Greg Kroah-Hartman   Driver core: add ...
1901
1902
1903
1904
1905
1906
  	if (old_parent)
  		sysfs_remove_link(&dev->kobj, "device");
  	if (new_parent)
  		error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  					  "device");
  	return error;
8a82472f8   Cornelia Huck   driver core: Intr...
1907
1908
1909
1910
1911
  }
  
  /**
   * device_move - moves a device to a new parent
   * @dev: the pointer to the struct device to be moved
c744aeae9   Cornelia Huck   driver core: Allo...
1912
   * @new_parent: the new parent of the device (can by NULL)
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1913
   * @dpm_order: how to reorder the dpm_list
8a82472f8   Cornelia Huck   driver core: Intr...
1914
   */
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1915
1916
  int device_move(struct device *dev, struct device *new_parent,
  		enum dpm_order dpm_order)
8a82472f8   Cornelia Huck   driver core: Intr...
1917
1918
1919
  {
  	int error;
  	struct device *old_parent;
c744aeae9   Cornelia Huck   driver core: Allo...
1920
  	struct kobject *new_parent_kobj;
8a82472f8   Cornelia Huck   driver core: Intr...
1921
1922
1923
1924
  
  	dev = get_device(dev);
  	if (!dev)
  		return -EINVAL;
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1925
  	device_pm_lock();
8a82472f8   Cornelia Huck   driver core: Intr...
1926
  	new_parent = get_device(new_parent);
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1927
  	new_parent_kobj = get_device_parent(dev, new_parent);
63b6971a0   Cornelia Huck   Driver core: Clea...
1928

1e0b2cf93   Kay Sievers   driver core: stru...
1929
1930
1931
  	pr_debug("device: '%s': %s: moving to '%s'
  ", dev_name(dev),
  		 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
c744aeae9   Cornelia Huck   driver core: Allo...
1932
  	error = kobject_move(&dev->kobj, new_parent_kobj);
8a82472f8   Cornelia Huck   driver core: Intr...
1933
  	if (error) {
63b6971a0   Cornelia Huck   Driver core: Clea...
1934
  		cleanup_glue_dir(dev, new_parent_kobj);
8a82472f8   Cornelia Huck   driver core: Intr...
1935
1936
1937
1938
1939
1940
  		put_device(new_parent);
  		goto out;
  	}
  	old_parent = dev->parent;
  	dev->parent = new_parent;
  	if (old_parent)
f791b8c83   Greg Kroah-Hartman   driver core: move...
1941
  		klist_remove(&dev->p->knode_parent);
0d358f22f   Yinghai Lu   driver core: try ...
1942
  	if (new_parent) {
f791b8c83   Greg Kroah-Hartman   driver core: move...
1943
1944
  		klist_add_tail(&dev->p->knode_parent,
  			       &new_parent->p->klist_children);
0d358f22f   Yinghai Lu   driver core: try ...
1945
1946
  		set_dev_node(dev, dev_to_node(new_parent));
  	}
bdd4034df   Rabin Vincent   driver core: alwa...
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
  	if (dev->class) {
  		error = device_move_class_links(dev, old_parent, new_parent);
  		if (error) {
  			/* We ignore errors on cleanup since we're hosed anyway... */
  			device_move_class_links(dev, new_parent, old_parent);
  			if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  				if (new_parent)
  					klist_remove(&dev->p->knode_parent);
  				dev->parent = old_parent;
  				if (old_parent) {
  					klist_add_tail(&dev->p->knode_parent,
  						       &old_parent->p->klist_children);
  					set_dev_node(dev, dev_to_node(old_parent));
  				}
0d358f22f   Yinghai Lu   driver core: try ...
1961
  			}
bdd4034df   Rabin Vincent   driver core: alwa...
1962
1963
1964
  			cleanup_glue_dir(dev, new_parent_kobj);
  			put_device(new_parent);
  			goto out;
8a82472f8   Cornelia Huck   driver core: Intr...
1965
  		}
8a82472f8   Cornelia Huck   driver core: Intr...
1966
  	}
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1967
1968
1969
1970
1971
  	switch (dpm_order) {
  	case DPM_ORDER_NONE:
  		break;
  	case DPM_ORDER_DEV_AFTER_PARENT:
  		device_pm_move_after(dev, new_parent);
52cdbdd49   Grygorii Strashko   driver core: corr...
1972
  		devices_kset_move_after(dev, new_parent);
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1973
1974
1975
  		break;
  	case DPM_ORDER_PARENT_BEFORE_DEV:
  		device_pm_move_before(new_parent, dev);
52cdbdd49   Grygorii Strashko   driver core: corr...
1976
  		devices_kset_move_before(new_parent, dev);
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1977
1978
1979
  		break;
  	case DPM_ORDER_DEV_LAST:
  		device_pm_move_last(dev);
52cdbdd49   Grygorii Strashko   driver core: corr...
1980
  		devices_kset_move_last(dev);
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1981
1982
  		break;
  	}
bdd4034df   Rabin Vincent   driver core: alwa...
1983

8a82472f8   Cornelia Huck   driver core: Intr...
1984
1985
  	put_device(old_parent);
  out:
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1986
  	device_pm_unlock();
8a82472f8   Cornelia Huck   driver core: Intr...
1987
1988
1989
  	put_device(dev);
  	return error;
  }
8a82472f8   Cornelia Huck   driver core: Intr...
1990
  EXPORT_SYMBOL_GPL(device_move);
37b0c0203   Greg Kroah-Hartman   driver core: clea...
1991
1992
1993
1994
1995
1996
  
  /**
   * device_shutdown - call ->shutdown() on each device to shutdown.
   */
  void device_shutdown(void)
  {
f123db8e9   Benson Leung   driver core : Fix...
1997
  	struct device *dev, *parent;
6245838fe   Hugh Daschbach   Driver core: Prot...
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
  
  	spin_lock(&devices_kset->list_lock);
  	/*
  	 * Walk the devices list backward, shutting down each in turn.
  	 * Beware that device unplug events may also start pulling
  	 * devices offline, even as the system is shutting down.
  	 */
  	while (!list_empty(&devices_kset->list)) {
  		dev = list_entry(devices_kset->list.prev, struct device,
  				kobj.entry);
d1c6c030f   Ming Lei   driver core: fix ...
2008
2009
2010
2011
2012
2013
  
  		/*
  		 * hold reference count of device's parent to
  		 * prevent it from being freed because parent's
  		 * lock is to be held
  		 */
f123db8e9   Benson Leung   driver core : Fix...
2014
  		parent = get_device(dev->parent);
6245838fe   Hugh Daschbach   Driver core: Prot...
2015
2016
2017
2018
2019
2020
2021
  		get_device(dev);
  		/*
  		 * Make sure the device is off the kset list, in the
  		 * event that dev->*->shutdown() doesn't remove it.
  		 */
  		list_del_init(&dev->kobj.entry);
  		spin_unlock(&devices_kset->list_lock);
fe6b91f47   Alan Stern   PM / Driver core:...
2022

d1c6c030f   Ming Lei   driver core: fix ...
2023
  		/* hold lock to avoid race with probe/release */
f123db8e9   Benson Leung   driver core : Fix...
2024
2025
  		if (parent)
  			device_lock(parent);
d1c6c030f   Ming Lei   driver core: fix ...
2026
  		device_lock(dev);
fe6b91f47   Alan Stern   PM / Driver core:...
2027
2028
2029
  		/* Don't allow any more runtime suspends */
  		pm_runtime_get_noresume(dev);
  		pm_runtime_barrier(dev);
37b0c0203   Greg Kroah-Hartman   driver core: clea...
2030

37b0c0203   Greg Kroah-Hartman   driver core: clea...
2031
  		if (dev->bus && dev->bus->shutdown) {
0246c4faf   ShuoX Liu   driver core: use ...
2032
2033
2034
  			if (initcall_debug)
  				dev_info(dev, "shutdown
  ");
37b0c0203   Greg Kroah-Hartman   driver core: clea...
2035
2036
  			dev->bus->shutdown(dev);
  		} else if (dev->driver && dev->driver->shutdown) {
0246c4faf   ShuoX Liu   driver core: use ...
2037
2038
2039
  			if (initcall_debug)
  				dev_info(dev, "shutdown
  ");
37b0c0203   Greg Kroah-Hartman   driver core: clea...
2040
2041
  			dev->driver->shutdown(dev);
  		}
d1c6c030f   Ming Lei   driver core: fix ...
2042
2043
  
  		device_unlock(dev);
f123db8e9   Benson Leung   driver core : Fix...
2044
2045
  		if (parent)
  			device_unlock(parent);
d1c6c030f   Ming Lei   driver core: fix ...
2046

6245838fe   Hugh Daschbach   Driver core: Prot...
2047
  		put_device(dev);
f123db8e9   Benson Leung   driver core : Fix...
2048
  		put_device(parent);
6245838fe   Hugh Daschbach   Driver core: Prot...
2049
2050
  
  		spin_lock(&devices_kset->list_lock);
37b0c0203   Greg Kroah-Hartman   driver core: clea...
2051
  	}
6245838fe   Hugh Daschbach   Driver core: Prot...
2052
  	spin_unlock(&devices_kset->list_lock);
37b0c0203   Greg Kroah-Hartman   driver core: clea...
2053
  }
99bcf2171   Joe Perches   device.h drivers/...
2054
2055
2056
2057
2058
2059
  
  /*
   * Device logging functions
   */
  
  #ifdef CONFIG_PRINTK
666f355f3   Joe Perches   device and dynami...
2060
2061
  static int
  create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
99bcf2171   Joe Perches   device.h drivers/...
2062
  {
c4e00daaa   Kay Sievers   driver-core: exte...
2063
  	const char *subsys;
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2064
  	size_t pos = 0;
99bcf2171   Joe Perches   device.h drivers/...
2065

c4e00daaa   Kay Sievers   driver-core: exte...
2066
2067
2068
2069
2070
  	if (dev->class)
  		subsys = dev->class->name;
  	else if (dev->bus)
  		subsys = dev->bus->name;
  	else
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2071
  		return 0;
c4e00daaa   Kay Sievers   driver-core: exte...
2072

798efc60e   Joe Perches   dev_dbg/dynamic_d...
2073
  	pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
655e5b7c0   Ben Hutchings   drivers/base: Fix...
2074
2075
  	if (pos >= hdrlen)
  		goto overflow;
c4e00daaa   Kay Sievers   driver-core: exte...
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
  
  	/*
  	 * Add device identifier DEVICE=:
  	 *   b12:8         block dev_t
  	 *   c127:3        char dev_t
  	 *   n8            netdev ifindex
  	 *   +sound:card0  subsystem:devname
  	 */
  	if (MAJOR(dev->devt)) {
  		char c;
  
  		if (strcmp(subsys, "block") == 0)
  			c = 'b';
  		else
  			c = 'c';
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2091
2092
2093
2094
  		pos++;
  		pos += snprintf(hdr + pos, hdrlen - pos,
  				"DEVICE=%c%u:%u",
  				c, MAJOR(dev->devt), MINOR(dev->devt));
c4e00daaa   Kay Sievers   driver-core: exte...
2095
2096
  	} else if (strcmp(subsys, "net") == 0) {
  		struct net_device *net = to_net_dev(dev);
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2097
2098
2099
  		pos++;
  		pos += snprintf(hdr + pos, hdrlen - pos,
  				"DEVICE=n%u", net->ifindex);
c4e00daaa   Kay Sievers   driver-core: exte...
2100
  	} else {
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2101
2102
2103
  		pos++;
  		pos += snprintf(hdr + pos, hdrlen - pos,
  				"DEVICE=+%s:%s", subsys, dev_name(dev));
c4e00daaa   Kay Sievers   driver-core: exte...
2104
  	}
af7f2158f   Jim Cromie   drivers-core: mak...
2105

655e5b7c0   Ben Hutchings   drivers/base: Fix...
2106
2107
  	if (pos >= hdrlen)
  		goto overflow;
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2108
  	return pos;
655e5b7c0   Ben Hutchings   drivers/base: Fix...
2109
2110
2111
2112
  
  overflow:
  	dev_WARN(dev, "device/subsystem name too long");
  	return 0;
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2113
  }
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2114

05e4e5b87   Joe Perches   dev: Add dev_vpri...
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
  int dev_vprintk_emit(int level, const struct device *dev,
  		     const char *fmt, va_list args)
  {
  	char hdr[128];
  	size_t hdrlen;
  
  	hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
  
  	return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
  }
  EXPORT_SYMBOL(dev_vprintk_emit);
  
  int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  {
  	va_list args;
  	int r;
  
  	va_start(args, fmt);
  
  	r = dev_vprintk_emit(level, dev, fmt, args);
  
  	va_end(args);
  
  	return r;
  }
  EXPORT_SYMBOL(dev_printk_emit);
d1f1052c5   Joe Perches   device: Change de...
2141
  static void __dev_printk(const char *level, const struct device *dev,
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2142
2143
  			struct va_format *vaf)
  {
d1f1052c5   Joe Perches   device: Change de...
2144
2145
2146
2147
2148
  	if (dev)
  		dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
  				dev_driver_string(dev), dev_name(dev), vaf);
  	else
  		printk("%s(NULL device *): %pV", level, vaf);
99bcf2171   Joe Perches   device.h drivers/...
2149
  }
d1f1052c5   Joe Perches   device: Change de...
2150
2151
  void dev_printk(const char *level, const struct device *dev,
  		const char *fmt, ...)
99bcf2171   Joe Perches   device.h drivers/...
2152
2153
2154
  {
  	struct va_format vaf;
  	va_list args;
99bcf2171   Joe Perches   device.h drivers/...
2155
2156
2157
2158
2159
  
  	va_start(args, fmt);
  
  	vaf.fmt = fmt;
  	vaf.va = &args;
d1f1052c5   Joe Perches   device: Change de...
2160
  	__dev_printk(level, dev, &vaf);
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2161

99bcf2171   Joe Perches   device.h drivers/...
2162
  	va_end(args);
99bcf2171   Joe Perches   device.h drivers/...
2163
2164
2165
2166
  }
  EXPORT_SYMBOL(dev_printk);
  
  #define define_dev_printk_level(func, kern_level)		\
d1f1052c5   Joe Perches   device: Change de...
2167
  void func(const struct device *dev, const char *fmt, ...)	\
99bcf2171   Joe Perches   device.h drivers/...
2168
2169
2170
  {								\
  	struct va_format vaf;					\
  	va_list args;						\
99bcf2171   Joe Perches   device.h drivers/...
2171
2172
2173
2174
2175
2176
  								\
  	va_start(args, fmt);					\
  								\
  	vaf.fmt = fmt;						\
  	vaf.va = &args;						\
  								\
d1f1052c5   Joe Perches   device: Change de...
2177
  	__dev_printk(kern_level, dev, &vaf);			\
798efc60e   Joe Perches   dev_dbg/dynamic_d...
2178
  								\
99bcf2171   Joe Perches   device.h drivers/...
2179
  	va_end(args);						\
99bcf2171   Joe Perches   device.h drivers/...
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
  }								\
  EXPORT_SYMBOL(func);
  
  define_dev_printk_level(dev_emerg, KERN_EMERG);
  define_dev_printk_level(dev_alert, KERN_ALERT);
  define_dev_printk_level(dev_crit, KERN_CRIT);
  define_dev_printk_level(dev_err, KERN_ERR);
  define_dev_printk_level(dev_warn, KERN_WARNING);
  define_dev_printk_level(dev_notice, KERN_NOTICE);
  define_dev_printk_level(_dev_info, KERN_INFO);
  
  #endif
97badf873   Rafael J. Wysocki   device property: ...
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
  
  static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
  {
  	return fwnode && !IS_ERR(fwnode->secondary);
  }
  
  /**
   * set_primary_fwnode - Change the primary firmware node of a given device.
   * @dev: Device to handle.
   * @fwnode: New primary firmware node of the device.
   *
   * Set the device's firmware node pointer to @fwnode, but if a secondary
   * firmware node of the device is present, preserve it.
   */
  void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  {
  	if (fwnode) {
  		struct fwnode_handle *fn = dev->fwnode;
  
  		if (fwnode_is_primary(fn))
  			fn = fn->secondary;
55f89a8a4   Mika Westerberg   driver core: Do n...
2213
2214
2215
2216
  		if (fn) {
  			WARN_ON(fwnode->secondary);
  			fwnode->secondary = fn;
  		}
97badf873   Rafael J. Wysocki   device property: ...
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
  		dev->fwnode = fwnode;
  	} else {
  		dev->fwnode = fwnode_is_primary(dev->fwnode) ?
  			dev->fwnode->secondary : NULL;
  	}
  }
  EXPORT_SYMBOL_GPL(set_primary_fwnode);
  
  /**
   * set_secondary_fwnode - Change the secondary firmware node of a given device.
   * @dev: Device to handle.
   * @fwnode: New secondary firmware node of the device.
   *
   * If a primary firmware node of the device is present, set its secondary
   * pointer to @fwnode.  Otherwise, set the device's firmware node pointer to
   * @fwnode.
   */
  void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  {
  	if (fwnode)
  		fwnode->secondary = ERR_PTR(-ENODEV);
  
  	if (fwnode_is_primary(dev->fwnode))
  		dev->fwnode->secondary = fwnode;
  	else
  		dev->fwnode = fwnode;
  }