Blame view

drivers/base/core.c 47.7 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
14
15
16
17
  #include <linux/device.h>
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/string.h>
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
18
  #include <linux/kdev_t.h>
116af3782   Benjamin Herrenschmidt   Driver core: add ...
19
  #include <linux/notifier.h>
da231fd5d   Kay Sievers   Driver core: fix ...
20
  #include <linux/genhd.h>
815d2d50d   Andrew Morton   driver core: debu...
21
  #include <linux/kallsyms.h>
f75b1c60f   Dave Young   class: change int...
22
  #include <linux/mutex.h>
401097ea4   Shaohua Li   driver core: sync...
23
  #include <linux/async.h>
af8db1508   Peter Chen   PM / driver core:...
24
  #include <linux/pm_runtime.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
  
  #include "base.h"
  #include "power/power.h"
e52eec13c   Andi Kleen   SYSFS: Allow boot...
28
29
30
31
32
33
34
35
36
37
38
39
  #ifdef CONFIG_SYSFS_DEPRECATED
  #ifdef CONFIG_SYSFS_DEPRECATED_V2
  long sysfs_deprecated = 1;
  #else
  long sysfs_deprecated = 0;
  #endif
  static __init int sysfs_deprecated_setup(char *arg)
  {
  	return strict_strtol(arg, 10, &sysfs_deprecated);
  }
  early_param("sysfs.deprecated", sysfs_deprecated_setup);
  #endif
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
40
41
  int (*platform_notify)(struct device *dev) = NULL;
  int (*platform_notify_remove)(struct device *dev) = NULL;
e105b8bfc   Dan Williams   sysfs: add /sys/d...
42
43
44
  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
45

4e886c296   Greg Kroah-Hartman   Driver core: Fix ...
46
47
48
49
50
51
52
53
54
55
56
  #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
57

3e95637a4   Alan Stern   [PATCH] Driver Co...
58
59
60
61
62
63
64
65
66
  /**
   * 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
   * the device is not bound to a device, it will return the name of the bus
   * 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...
67
  const char *dev_driver_string(const struct device *dev)
3e95637a4   Alan Stern   [PATCH] Driver Co...
68
  {
3589972e5   Alan Stern   Driver core: fix ...
69
70
71
72
73
74
75
76
  	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...
77
78
  			(dev->bus ? dev->bus->name :
  			(dev->class ? dev->class->name : ""));
3e95637a4   Alan Stern   [PATCH] Driver Co...
79
  }
310a922d4   Matthew Wilcox   Fix dev_printk() ...
80
  EXPORT_SYMBOL(dev_driver_string);
3e95637a4   Alan Stern   [PATCH] Driver Co...
81

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
  #define to_dev(obj) container_of(obj, struct device, kobj)
  #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
84
85
  static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  			     char *buf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
87
88
  	struct device_attribute *dev_attr = to_dev_attr(attr);
  	struct device *dev = to_dev(kobj);
4a0c20bf8   Dmitry Torokhov   [PATCH] sysfs: (d...
89
  	ssize_t ret = -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
  
  	if (dev_attr->show)
54b6f35c9   Yani Ioannou   [PATCH] Driver co...
92
  		ret = dev_attr->show(dev, dev_attr, buf);
815d2d50d   Andrew Morton   driver core: debu...
93
94
95
96
97
  	if (ret >= (ssize_t)PAGE_SIZE) {
  		print_symbol("dev_attr_show: %s returned bad count
  ",
  				(unsigned long)dev_attr->show);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
  	return ret;
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
100
101
  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
102
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
103
104
  	struct device_attribute *dev_attr = to_dev_attr(attr);
  	struct device *dev = to_dev(kobj);
4a0c20bf8   Dmitry Torokhov   [PATCH] sysfs: (d...
105
  	ssize_t ret = -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
107
  
  	if (dev_attr->store)
54b6f35c9   Yani Ioannou   [PATCH] Driver co...
108
  		ret = dev_attr->store(dev, dev_attr, buf, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  	return ret;
  }
52cf25d0a   Emese Revfy   Driver core: Cons...
111
  static const struct sysfs_ops dev_sysfs_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
114
  	.show	= dev_attr_show,
  	.store	= dev_attr_store,
  };
ca22e56de   Kay Sievers   driver-core: impl...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
  #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
167
168
169
170
171
172
173
174
175
  
  /**
   *	device_release - free device structure.
   *	@kobj:	device's kobject.
   *
   *	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.
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
176
  static void device_release(struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
178
  	struct device *dev = to_dev(kobj);
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
179
  	struct device_private *p = dev->p;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
182
  
  	if (dev->release)
  		dev->release(dev);
f9f852df2   Kay Sievers   Driver core: add ...
183
184
  	else if (dev->type && dev->type->release)
  		dev->type->release(dev);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
185
186
  	else if (dev->class && dev->class->dev_release)
  		dev->class->dev_release(dev);
f810a5cf2   Arjan van de Ven   Use WARN() in dri...
187
188
  	else
  		WARN(1, KERN_ERR "Device '%s' does not have a release() "
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
189
190
  			"function, it is broken and must be fixed.
  ",
1e0b2cf93   Kay Sievers   driver core: stru...
191
  			dev_name(dev));
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
192
  	kfree(p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
  }
bc451f205   Eric W. Biederman   kobj: Add basic i...
194
195
196
197
198
199
200
201
202
203
  static const void *device_namespace(struct kobject *kobj)
  {
  	struct device *dev = to_dev(kobj);
  	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...
204
  static struct kobj_type device_ktype = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
  	.release	= device_release,
  	.sysfs_ops	= &dev_sysfs_ops,
bc451f205   Eric W. Biederman   kobj: Add basic i...
207
  	.namespace	= device_namespace,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
  };
312c004d3   Kay Sievers   [PATCH] driver co...
209
  static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
  {
  	struct kobj_type *ktype = get_ktype(kobj);
8f4afc410   Greg Kroah-Hartman   Driver core: rena...
212
  	if (ktype == &device_ktype) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
  		struct device *dev = to_dev(kobj);
  		if (dev->bus)
  			return 1;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
216
217
  		if (dev->class)
  			return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
220
  	}
  	return 0;
  }
312c004d3   Kay Sievers   [PATCH] driver co...
221
  static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222
223
  {
  	struct device *dev = to_dev(kobj);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
224
225
226
227
228
  	if (dev->bus)
  		return dev->bus->name;
  	if (dev->class)
  		return dev->class->name;
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
  }
7eff2e7a8   Kay Sievers   Driver core: chan...
230
231
  static int dev_uevent(struct kset *kset, struct kobject *kobj,
  		      struct kobj_uevent_env *env)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
  {
  	struct device *dev = to_dev(kobj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
  	int retval = 0;
6fcf53acc   Kay Sievers   Driver Core: add ...
235
  	/* add device node properties if present */
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
236
  	if (MAJOR(dev->devt)) {
6fcf53acc   Kay Sievers   Driver Core: add ...
237
238
  		const char *tmp;
  		const char *name;
2c9ede55e   Al Viro   switch device_get...
239
  		umode_t mode = 0;
6fcf53acc   Kay Sievers   Driver Core: add ...
240

7eff2e7a8   Kay Sievers   Driver core: chan...
241
242
  		add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  		add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
e454cea20   Kay Sievers   Driver-Core: exte...
243
  		name = device_get_devnode(dev, &mode, &tmp);
6fcf53acc   Kay Sievers   Driver Core: add ...
244
245
246
  		if (name) {
  			add_uevent_var(env, "DEVNAME=%s", name);
  			kfree(tmp);
e454cea20   Kay Sievers   Driver-Core: exte...
247
248
  			if (mode)
  				add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
6fcf53acc   Kay Sievers   Driver Core: add ...
249
  		}
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
250
  	}
414264f95   Kay Sievers   Driver core: add ...
251
  	if (dev->type && dev->type->name)
7eff2e7a8   Kay Sievers   Driver core: chan...
252
  		add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
414264f95   Kay Sievers   Driver core: add ...
253

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

7eff2e7a8   Kay Sievers   Driver core: chan...
257
  	/* have the bus specific function add its stuff */
312c004d3   Kay Sievers   [PATCH] driver co...
258
  	if (dev->bus && dev->bus->uevent) {
7eff2e7a8   Kay Sievers   Driver core: chan...
259
  		retval = dev->bus->uevent(dev, env);
f9f852df2   Kay Sievers   Driver core: add ...
260
  		if (retval)
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
261
262
  			pr_debug("device: '%s': %s: bus uevent() returned %d
  ",
1e0b2cf93   Kay Sievers   driver core: stru...
263
  				 dev_name(dev), __func__, retval);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  	}
7eff2e7a8   Kay Sievers   Driver core: chan...
265
  	/* have the class specific function add its stuff */
2620efef7   Greg Kroah-Hartman   Driver core: add ...
266
  	if (dev->class && dev->class->dev_uevent) {
7eff2e7a8   Kay Sievers   Driver core: chan...
267
  		retval = dev->class->dev_uevent(dev, env);
f9f852df2   Kay Sievers   Driver core: add ...
268
  		if (retval)
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
269
  			pr_debug("device: '%s': %s: class uevent() "
1e0b2cf93   Kay Sievers   driver core: stru...
270
271
  				 "returned %d
  ", dev_name(dev),
2b3a302a0   Harvey Harrison   driver core: repl...
272
  				 __func__, retval);
f9f852df2   Kay Sievers   Driver core: add ...
273
  	}
eef35c2d4   Stefan Weil   Fix spelling fuct...
274
  	/* have the device type specific function add its stuff */
f9f852df2   Kay Sievers   Driver core: add ...
275
  	if (dev->type && dev->type->uevent) {
7eff2e7a8   Kay Sievers   Driver core: chan...
276
  		retval = dev->type->uevent(dev, env);
f9f852df2   Kay Sievers   Driver core: add ...
277
  		if (retval)
7dc72b284   Greg Kroah-Hartman   Driver core: clea...
278
  			pr_debug("device: '%s': %s: dev_type uevent() "
1e0b2cf93   Kay Sievers   driver core: stru...
279
280
  				 "returned %d
  ", dev_name(dev),
2b3a302a0   Harvey Harrison   driver core: repl...
281
  				 __func__, retval);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
282
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
  	return retval;
  }
9cd43611c   Emese Revfy   kobject: Constify...
285
  static const struct kset_uevent_ops device_uevent_ops = {
312c004d3   Kay Sievers   [PATCH] driver co...
286
287
288
  	.filter =	dev_uevent_filter,
  	.name =		dev_uevent_name,
  	.uevent =	dev_uevent,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
  };
16574dccd   Kay Sievers   Driver core: make...
290
291
292
293
294
  static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  			   char *buf)
  {
  	struct kobject *top_kobj;
  	struct kset *kset;
7eff2e7a8   Kay Sievers   Driver core: chan...
295
  	struct kobj_uevent_env *env = NULL;
16574dccd   Kay Sievers   Driver core: make...
296
297
298
299
300
301
  	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...
302
303
  	while (!top_kobj->kset && top_kobj->parent)
  		top_kobj = top_kobj->parent;
16574dccd   Kay Sievers   Driver core: make...
304
305
  	if (!top_kobj->kset)
  		goto out;
5c5daf657   Kay Sievers   Driver core: excl...
306

16574dccd   Kay Sievers   Driver core: make...
307
308
309
310
311
312
313
314
  	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...
315
316
  	env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  	if (!env)
c7308c81a   Greg Kroah-Hartman   Driver core: fix ...
317
  		return -ENOMEM;
16574dccd   Kay Sievers   Driver core: make...
318
  	/* let the kset specific function add its keys */
7eff2e7a8   Kay Sievers   Driver core: chan...
319
  	retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
16574dccd   Kay Sievers   Driver core: make...
320
321
322
323
  	if (retval)
  		goto out;
  
  	/* copy keys to file */
7eff2e7a8   Kay Sievers   Driver core: chan...
324
325
326
  	for (i = 0; i < env->envp_idx; i++)
  		count += sprintf(&buf[count], "%s
  ", env->envp[i]);
16574dccd   Kay Sievers   Driver core: make...
327
  out:
7eff2e7a8   Kay Sievers   Driver core: chan...
328
  	kfree(env);
16574dccd   Kay Sievers   Driver core: make...
329
330
  	return count;
  }
a7fd67062   Kay Sievers   [PATCH] add sysfs...
331
332
333
  static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  			    const char *buf, size_t count)
  {
60a96a595   Kay Sievers   Driver core: acce...
334
  	enum kobject_action action;
3f5468c9a   Kay Sievers   Driver-Core: requ...
335
  	if (kobject_action_type(buf, count, &action) == 0)
60a96a595   Kay Sievers   Driver core: acce...
336
  		kobject_uevent(&dev->kobj, action);
3f5468c9a   Kay Sievers   Driver-Core: requ...
337
338
339
  	else
  		dev_err(dev, "uevent: unknown action-string
  ");
a7fd67062   Kay Sievers   [PATCH] add sysfs...
340
341
  	return count;
  }
ad6a1e1c6   Tejun Heo   driver-core: make...
342
343
  static struct device_attribute uevent_attr =
  	__ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
621a1672f   Dmitry Torokhov   driver core: Use ...
344
345
  static int device_add_attributes(struct device *dev,
  				 struct device_attribute *attrs)
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
346
  {
621a1672f   Dmitry Torokhov   driver core: Use ...
347
  	int error = 0;
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
348
  	int i;
621a1672f   Dmitry Torokhov   driver core: Use ...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  
  	if (attrs) {
  		for (i = 0; attr_name(attrs[i]); i++) {
  			error = device_create_file(dev, &attrs[i]);
  			if (error)
  				break;
  		}
  		if (error)
  			while (--i >= 0)
  				device_remove_file(dev, &attrs[i]);
  	}
  	return error;
  }
  
  static void device_remove_attributes(struct device *dev,
  				     struct device_attribute *attrs)
  {
  	int i;
  
  	if (attrs)
  		for (i = 0; attr_name(attrs[i]); i++)
  			device_remove_file(dev, &attrs[i]);
  }
c97415a72   Stefan Achatz   sysfs: Introducin...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  static int device_add_bin_attributes(struct device *dev,
  				     struct bin_attribute *attrs)
  {
  	int error = 0;
  	int i;
  
  	if (attrs) {
  		for (i = 0; attr_name(attrs[i]); i++) {
  			error = device_create_bin_file(dev, &attrs[i]);
  			if (error)
  				break;
  		}
  		if (error)
  			while (--i >= 0)
  				device_remove_bin_file(dev, &attrs[i]);
  	}
  	return error;
  }
  
  static void device_remove_bin_attributes(struct device *dev,
  					 struct bin_attribute *attrs)
  {
  	int i;
  
  	if (attrs)
  		for (i = 0; attr_name(attrs[i]); i++)
  			device_remove_bin_file(dev, &attrs[i]);
  }
621a1672f   Dmitry Torokhov   driver core: Use ...
400
  static int device_add_groups(struct device *dev,
a4dbd6740   David Brownell   driver model: con...
401
  			     const struct attribute_group **groups)
621a1672f   Dmitry Torokhov   driver core: Use ...
402
  {
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
403
  	int error = 0;
621a1672f   Dmitry Torokhov   driver core: Use ...
404
  	int i;
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
405

621a1672f   Dmitry Torokhov   driver core: Use ...
406
407
408
  	if (groups) {
  		for (i = 0; groups[i]; i++) {
  			error = sysfs_create_group(&dev->kobj, groups[i]);
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
409
410
  			if (error) {
  				while (--i >= 0)
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
411
412
  					sysfs_remove_group(&dev->kobj,
  							   groups[i]);
621a1672f   Dmitry Torokhov   driver core: Use ...
413
  				break;
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
414
415
416
  			}
  		}
  	}
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
417
418
  	return error;
  }
621a1672f   Dmitry Torokhov   driver core: Use ...
419
  static void device_remove_groups(struct device *dev,
a4dbd6740   David Brownell   driver model: con...
420
  				 const struct attribute_group **groups)
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
421
422
  {
  	int i;
621a1672f   Dmitry Torokhov   driver core: Use ...
423
424
425
426
  
  	if (groups)
  		for (i = 0; groups[i]; i++)
  			sysfs_remove_group(&dev->kobj, groups[i]);
de0ff00d7   Greg Kroah-Hartman   Driver core: add ...
427
  }
2620efef7   Greg Kroah-Hartman   Driver core: add ...
428
429
430
  static int device_add_attrs(struct device *dev)
  {
  	struct class *class = dev->class;
aed65af1c   Stephen Hemminger   drivers: make dev...
431
  	const struct device_type *type = dev->type;
621a1672f   Dmitry Torokhov   driver core: Use ...
432
  	int error;
2620efef7   Greg Kroah-Hartman   Driver core: add ...
433

621a1672f   Dmitry Torokhov   driver core: Use ...
434
435
  	if (class) {
  		error = device_add_attributes(dev, class->dev_attrs);
f9f852df2   Kay Sievers   Driver core: add ...
436
  		if (error)
621a1672f   Dmitry Torokhov   driver core: Use ...
437
  			return error;
c97415a72   Stefan Achatz   sysfs: Introducin...
438
439
440
  		error = device_add_bin_attributes(dev, class->dev_bin_attrs);
  		if (error)
  			goto err_remove_class_attrs;
2620efef7   Greg Kroah-Hartman   Driver core: add ...
441
  	}
f9f852df2   Kay Sievers   Driver core: add ...
442

621a1672f   Dmitry Torokhov   driver core: Use ...
443
444
  	if (type) {
  		error = device_add_groups(dev, type->groups);
f9f852df2   Kay Sievers   Driver core: add ...
445
  		if (error)
c97415a72   Stefan Achatz   sysfs: Introducin...
446
  			goto err_remove_class_bin_attrs;
f9f852df2   Kay Sievers   Driver core: add ...
447
  	}
621a1672f   Dmitry Torokhov   driver core: Use ...
448
449
450
451
452
453
454
455
456
  	error = device_add_groups(dev, dev->groups);
  	if (error)
  		goto err_remove_type_groups;
  
  	return 0;
  
   err_remove_type_groups:
  	if (type)
  		device_remove_groups(dev, type->groups);
c97415a72   Stefan Achatz   sysfs: Introducin...
457
458
459
   err_remove_class_bin_attrs:
  	if (class)
  		device_remove_bin_attributes(dev, class->dev_bin_attrs);
621a1672f   Dmitry Torokhov   driver core: Use ...
460
461
462
   err_remove_class_attrs:
  	if (class)
  		device_remove_attributes(dev, class->dev_attrs);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
463
464
465
466
467
468
  	return error;
  }
  
  static void device_remove_attrs(struct device *dev)
  {
  	struct class *class = dev->class;
aed65af1c   Stephen Hemminger   drivers: make dev...
469
  	const struct device_type *type = dev->type;
2620efef7   Greg Kroah-Hartman   Driver core: add ...
470

621a1672f   Dmitry Torokhov   driver core: Use ...
471
  	device_remove_groups(dev, dev->groups);
f9f852df2   Kay Sievers   Driver core: add ...
472

621a1672f   Dmitry Torokhov   driver core: Use ...
473
474
  	if (type)
  		device_remove_groups(dev, type->groups);
c97415a72   Stefan Achatz   sysfs: Introducin...
475
  	if (class) {
621a1672f   Dmitry Torokhov   driver core: Use ...
476
  		device_remove_attributes(dev, class->dev_attrs);
c97415a72   Stefan Achatz   sysfs: Introducin...
477
478
  		device_remove_bin_attributes(dev, class->dev_bin_attrs);
  	}
2620efef7   Greg Kroah-Hartman   Driver core: add ...
479
  }
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
480
481
482
483
484
  static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  			char *buf)
  {
  	return print_dev_t(buf, dev->devt);
  }
ad6a1e1c6   Tejun Heo   driver-core: make...
485
486
  static struct device_attribute devt_attr =
  	__ATTR(dev, S_IRUGO, show_dev, NULL);
ca22e56de   Kay Sievers   driver-core: impl...
487
  /* /sys/devices/ */
881c6cfd7   Greg Kroah-Hartman   kset: convert /sy...
488
  struct kset *devices_kset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
489

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
491
492
493
   * device_create_file - create sysfs attribute file for device.
   * @dev: device.
   * @attr: device attribute descriptor.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
   */
26579ab70   Phil Carmody   Driver core: devi...
495
496
  int device_create_file(struct device *dev,
  		       const struct device_attribute *attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
  {
  	int error = 0;
0c98b19fe   Cornelia Huck   Driver core: Remo...
499
  	if (dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
  		error = sysfs_create_file(&dev->kobj, &attr->attr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
501
502
503
504
  	return error;
  }
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
505
506
507
   * device_remove_file - remove sysfs attribute file.
   * @dev: device.
   * @attr: device attribute descriptor.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
508
   */
26579ab70   Phil Carmody   Driver core: devi...
509
510
  void device_remove_file(struct device *dev,
  			const struct device_attribute *attr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
  {
0c98b19fe   Cornelia Huck   Driver core: Remo...
512
  	if (dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
  		sysfs_remove_file(&dev->kobj, &attr->attr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514
  }
2589f1887   Greg Kroah-Hartman   Driver core: add ...
515
516
517
518
519
  /**
   * device_create_bin_file - create sysfs binary attribute file for device.
   * @dev: device.
   * @attr: device binary attribute descriptor.
   */
66ecb92be   Phil Carmody   Driver core: bin_...
520
521
  int device_create_bin_file(struct device *dev,
  			   const struct bin_attribute *attr)
2589f1887   Greg Kroah-Hartman   Driver core: add ...
522
523
524
525
526
527
528
529
530
531
532
533
534
  {
  	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_...
535
536
  void device_remove_bin_file(struct device *dev,
  			    const struct bin_attribute *attr)
2589f1887   Greg Kroah-Hartman   Driver core: add ...
537
538
539
540
541
  {
  	if (dev)
  		sysfs_remove_bin_file(&dev->kobj, attr);
  }
  EXPORT_SYMBOL_GPL(device_remove_bin_file);
d9a9cdfb0   Alan Stern   [PATCH] sysfs and...
542
  /**
523ded71d   Alan Stern   device_schedule_c...
543
   * device_schedule_callback_owner - helper to schedule a callback for a device
d9a9cdfb0   Alan Stern   [PATCH] sysfs and...
544
545
   * @dev: device.
   * @func: callback function to invoke later.
523ded71d   Alan Stern   device_schedule_c...
546
   * @owner: module owning the callback routine
d9a9cdfb0   Alan Stern   [PATCH] sysfs and...
547
548
549
550
551
552
553
554
555
556
   *
   * Attribute methods must not unregister themselves or their parent device
   * (which would amount to the same thing).  Attempts to do so will deadlock,
   * since unregistration is mutually exclusive with driver callbacks.
   *
   * Instead methods can call this routine, which will attempt to allocate
   * and schedule a workqueue request to call back @func with @dev as its
   * argument in the workqueue's process context.  @dev will be pinned until
   * @func returns.
   *
523ded71d   Alan Stern   device_schedule_c...
557
558
559
   * This routine is usually called via the inline device_schedule_callback(),
   * which automatically sets @owner to THIS_MODULE.
   *
d9a9cdfb0   Alan Stern   [PATCH] sysfs and...
560
   * Returns 0 if the request was submitted, -ENOMEM if storage could not
523ded71d   Alan Stern   device_schedule_c...
561
   * be allocated, -ENODEV if a reference to @owner isn't available.
d9a9cdfb0   Alan Stern   [PATCH] sysfs and...
562
563
564
565
566
   *
   * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
   * underlying sysfs routine (since it is intended for use by attribute
   * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
   */
523ded71d   Alan Stern   device_schedule_c...
567
568
  int device_schedule_callback_owner(struct device *dev,
  		void (*func)(struct device *), struct module *owner)
d9a9cdfb0   Alan Stern   [PATCH] sysfs and...
569
570
  {
  	return sysfs_schedule_callback(&dev->kobj,
523ded71d   Alan Stern   device_schedule_c...
571
  			(void (*)(void *)) func, dev, owner);
d9a9cdfb0   Alan Stern   [PATCH] sysfs and...
572
  }
523ded71d   Alan Stern   device_schedule_c...
573
  EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
d9a9cdfb0   Alan Stern   [PATCH] sysfs and...
574

34bb61f9d   James Bottomley   [PATCH] fix klist...
575
576
  static void klist_children_get(struct klist_node *n)
  {
f791b8c83   Greg Kroah-Hartman   driver core: move...
577
578
  	struct device_private *p = to_device_private_parent(n);
  	struct device *dev = p->device;
34bb61f9d   James Bottomley   [PATCH] fix klist...
579
580
581
582
583
584
  
  	get_device(dev);
  }
  
  static void klist_children_put(struct klist_node *n)
  {
f791b8c83   Greg Kroah-Hartman   driver core: move...
585
586
  	struct device_private *p = to_device_private_parent(n);
  	struct device *dev = p->device;
34bb61f9d   James Bottomley   [PATCH] fix klist...
587
588
589
  
  	put_device(dev);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
591
592
   * device_initialize - init device structure.
   * @dev: device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
   *
5739411ac   Cornelia Huck   Driver core: Clar...
594
595
   * This prepares the device for use by other layers by initializing
   * its fields.
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
596
   * It is the first half of device_register(), if called by
5739411ac   Cornelia Huck   Driver core: Clar...
597
598
599
600
601
602
603
   * 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.
   *
   * 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
604
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
605
606
  void device_initialize(struct device *dev)
  {
881c6cfd7   Greg Kroah-Hartman   kset: convert /sy...
607
  	dev->kobj.kset = devices_kset;
f9cb074bf   Greg Kroah-Hartman   Kobject: rename k...
608
  	kobject_init(&dev->kobj, &device_ktype);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
609
  	INIT_LIST_HEAD(&dev->dma_pools);
3142788b7   Thomas Gleixner   drivers/base: Con...
610
  	mutex_init(&dev->mutex);
1704f47b5   Peter Zijlstra   lockdep: Add nova...
611
  	lockdep_set_novalidate_class(&dev->mutex);
9ac7849e3   Tejun Heo   devres: device re...
612
613
  	spin_lock_init(&dev->devres_lock);
  	INIT_LIST_HEAD(&dev->devres_head);
3b98aeaf3   Alan Stern   PM: don't skip de...
614
  	device_pm_init(dev);
873481367   Christoph Hellwig   [PATCH] add numa ...
615
  	set_dev_node(dev, -1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
616
  }
864062457   Kay Sievers   driver core: fix ...
617
  static struct kobject *virtual_device_parent(struct device *dev)
f0ee61a6c   Greg Kroah-Hartman   Driver Core: Move...
618
  {
864062457   Kay Sievers   driver core: fix ...
619
  	static struct kobject *virtual_dir = NULL;
f0ee61a6c   Greg Kroah-Hartman   Driver Core: Move...
620

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

864062457   Kay Sievers   driver core: fix ...
625
  	return virtual_dir;
f0ee61a6c   Greg Kroah-Hartman   Driver Core: Move...
626
  }
bc451f205   Eric W. Biederman   kobj: Add basic i...
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
  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...
642
  {
bc451f205   Eric W. Biederman   kobj: Add basic i...
643
644
645
646
647
648
649
650
651
652
653
654
655
656
  	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 ...
657
  	int retval;
bc451f205   Eric W. Biederman   kobj: Add basic i...
658
659
660
661
662
663
  	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...
664
  	dir->kobj.kset = &class->p->glue_dirs;
bc451f205   Eric W. Biederman   kobj: Add basic i...
665
666
667
668
669
670
671
672
673
674
675
676
677
  
  	retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
  	if (retval < 0) {
  		kobject_put(&dir->kobj);
  		return NULL;
  	}
  	return &dir->kobj;
  }
  
  
  static struct kobject *get_device_parent(struct device *dev,
  					 struct device *parent)
  {
864062457   Kay Sievers   driver core: fix ...
678
  	if (dev->class) {
77d3d7c1d   Tejun Heo   driver-core: fix ...
679
  		static DEFINE_MUTEX(gdp_mutex);
864062457   Kay Sievers   driver core: fix ...
680
681
682
  		struct kobject *kobj = NULL;
  		struct kobject *parent_kobj;
  		struct kobject *k;
ead454feb   Randy Dunlap   driver core: fix ...
683
  #ifdef CONFIG_BLOCK
39aba963d   Kay Sievers   driver core: remo...
684
  		/* block disks show up in /sys/block */
e52eec13c   Andi Kleen   SYSFS: Allow boot...
685
  		if (sysfs_deprecated && dev->class == &block_class) {
39aba963d   Kay Sievers   driver core: remo...
686
687
  			if (parent && parent->class == &block_class)
  				return &parent->kobj;
6b6e39a6a   Kay Sievers   driver-core: merg...
688
  			return &block_class.p->subsys.kobj;
39aba963d   Kay Sievers   driver core: remo...
689
  		}
ead454feb   Randy Dunlap   driver core: fix ...
690
  #endif
e52eec13c   Andi Kleen   SYSFS: Allow boot...
691

864062457   Kay Sievers   driver core: fix ...
692
693
  		/*
  		 * If we have no parent, we live in "virtual".
0f4dafc05   Kay Sievers   Kobject: auto-cle...
694
695
  		 * Class-devices with a non class-device as parent, live
  		 * in a "glue" directory to prevent namespace collisions.
864062457   Kay Sievers   driver core: fix ...
696
697
698
  		 */
  		if (parent == NULL)
  			parent_kobj = virtual_device_parent(dev);
24b1442d0   Eric W. Biederman   Driver-core: Alwa...
699
  		else if (parent->class && !dev->class->ns_type)
864062457   Kay Sievers   driver core: fix ...
700
701
702
  			return &parent->kobj;
  		else
  			parent_kobj = &parent->kobj;
77d3d7c1d   Tejun Heo   driver-core: fix ...
703
  		mutex_lock(&gdp_mutex);
864062457   Kay Sievers   driver core: fix ...
704
  		/* find our class-directory at the parent and reference it */
6b6e39a6a   Kay Sievers   driver-core: merg...
705
706
  		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 ...
707
708
709
710
  			if (k->parent == parent_kobj) {
  				kobj = kobject_get(k);
  				break;
  			}
6b6e39a6a   Kay Sievers   driver-core: merg...
711
  		spin_unlock(&dev->class->p->glue_dirs.list_lock);
77d3d7c1d   Tejun Heo   driver-core: fix ...
712
713
  		if (kobj) {
  			mutex_unlock(&gdp_mutex);
864062457   Kay Sievers   driver core: fix ...
714
  			return kobj;
77d3d7c1d   Tejun Heo   driver-core: fix ...
715
  		}
864062457   Kay Sievers   driver core: fix ...
716
717
  
  		/* or create a new class-directory at the parent device */
bc451f205   Eric W. Biederman   kobj: Add basic i...
718
  		k = class_dir_create_and_add(dev->class, parent_kobj);
0f4dafc05   Kay Sievers   Kobject: auto-cle...
719
  		/* do not emit an uevent for this simple "glue" directory */
77d3d7c1d   Tejun Heo   driver-core: fix ...
720
  		mutex_unlock(&gdp_mutex);
43968d2f1   Greg Kroah-Hartman   kobject: get rid ...
721
  		return k;
864062457   Kay Sievers   driver core: fix ...
722
  	}
ca22e56de   Kay Sievers   driver-core: impl...
723
724
725
  	/* 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 ...
726
  	if (parent)
c744aeae9   Cornelia Huck   driver core: Allo...
727
728
729
  		return &parent->kobj;
  	return NULL;
  }
da231fd5d   Kay Sievers   Driver core: fix ...
730

63b6971a0   Cornelia Huck   Driver core: Clea...
731
  static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
da231fd5d   Kay Sievers   Driver core: fix ...
732
  {
0f4dafc05   Kay Sievers   Kobject: auto-cle...
733
  	/* see if we live in a "glue" directory */
c1fe539a7   Cornelia Huck   Driver core: Fix ...
734
  	if (!glue_dir || !dev->class ||
6b6e39a6a   Kay Sievers   driver-core: merg...
735
  	    glue_dir->kset != &dev->class->p->glue_dirs)
da231fd5d   Kay Sievers   Driver core: fix ...
736
  		return;
0f4dafc05   Kay Sievers   Kobject: auto-cle...
737
  	kobject_put(glue_dir);
da231fd5d   Kay Sievers   Driver core: fix ...
738
  }
63b6971a0   Cornelia Huck   Driver core: Clea...
739
740
741
742
743
  
  static void cleanup_device_parent(struct device *dev)
  {
  	cleanup_glue_dir(dev, dev->kobj.parent);
  }
864062457   Kay Sievers   driver core: fix ...
744

2ee97caf0   Cornelia Huck   Driver core: chec...
745
746
747
748
749
750
  static int device_add_class_symlinks(struct device *dev)
  {
  	int error;
  
  	if (!dev->class)
  		return 0;
da231fd5d   Kay Sievers   Driver core: fix ...
751

1fbfee6c6   Greg Kroah-Hartman   class: rename "su...
752
  	error = sysfs_create_link(&dev->kobj,
6b6e39a6a   Kay Sievers   driver-core: merg...
753
  				  &dev->class->p->subsys.kobj,
2ee97caf0   Cornelia Huck   Driver core: chec...
754
755
756
  				  "subsystem");
  	if (error)
  		goto out;
da231fd5d   Kay Sievers   Driver core: fix ...
757

4e886c296   Greg Kroah-Hartman   Driver core: Fix ...
758
  	if (dev->parent && device_is_not_partition(dev)) {
39aba963d   Kay Sievers   driver core: remo...
759
  		error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
4f01a757e   Dmitry Torokhov   Driver core: fix ...
760
761
  					  "device");
  		if (error)
39aba963d   Kay Sievers   driver core: remo...
762
  			goto out_subsys;
2ee97caf0   Cornelia Huck   Driver core: chec...
763
  	}
2ee97caf0   Cornelia Huck   Driver core: chec...
764

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

da231fd5d   Kay Sievers   Driver core: fix ...
771
  	/* link in the class directory pointing to the device */
6b6e39a6a   Kay Sievers   driver-core: merg...
772
  	error = sysfs_create_link(&dev->class->p->subsys.kobj,
1e0b2cf93   Kay Sievers   driver core: stru...
773
  				  &dev->kobj, dev_name(dev));
da231fd5d   Kay Sievers   Driver core: fix ...
774
  	if (error)
39aba963d   Kay Sievers   driver core: remo...
775
  		goto out_device;
da231fd5d   Kay Sievers   Driver core: fix ...
776

da231fd5d   Kay Sievers   Driver core: fix ...
777
  	return 0;
39aba963d   Kay Sievers   driver core: remo...
778
779
  out_device:
  	sysfs_remove_link(&dev->kobj, "device");
da231fd5d   Kay Sievers   Driver core: fix ...
780

2ee97caf0   Cornelia Huck   Driver core: chec...
781
782
783
784
785
786
787
788
789
790
  out_subsys:
  	sysfs_remove_link(&dev->kobj, "subsystem");
  out:
  	return error;
  }
  
  static void device_remove_class_symlinks(struct device *dev)
  {
  	if (!dev->class)
  		return;
da231fd5d   Kay Sievers   Driver core: fix ...
791

4e886c296   Greg Kroah-Hartman   Driver core: Fix ...
792
  	if (dev->parent && device_is_not_partition(dev))
da231fd5d   Kay Sievers   Driver core: fix ...
793
  		sysfs_remove_link(&dev->kobj, "device");
2ee97caf0   Cornelia Huck   Driver core: chec...
794
  	sysfs_remove_link(&dev->kobj, "subsystem");
ead454feb   Randy Dunlap   driver core: fix ...
795
  #ifdef CONFIG_BLOCK
e52eec13c   Andi Kleen   SYSFS: Allow boot...
796
  	if (sysfs_deprecated && dev->class == &block_class)
39aba963d   Kay Sievers   driver core: remo...
797
  		return;
ead454feb   Randy Dunlap   driver core: fix ...
798
  #endif
6b6e39a6a   Kay Sievers   driver-core: merg...
799
  	sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
2ee97caf0   Cornelia Huck   Driver core: chec...
800
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
  /**
413c239fa   Stephen Rothwell   driver-core: prep...
802
803
   * dev_set_name - set a device name
   * @dev: device
462323661   Randy Dunlap   dev_set_name: fix...
804
   * @fmt: format string for the device's name
413c239fa   Stephen Rothwell   driver-core: prep...
805
806
807
808
   */
  int dev_set_name(struct device *dev, const char *fmt, ...)
  {
  	va_list vargs;
1fa5ae857   Kay Sievers   driver core: get ...
809
  	int err;
413c239fa   Stephen Rothwell   driver-core: prep...
810
811
  
  	va_start(vargs, fmt);
1fa5ae857   Kay Sievers   driver core: get ...
812
  	err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
413c239fa   Stephen Rothwell   driver-core: prep...
813
  	va_end(vargs);
1fa5ae857   Kay Sievers   driver core: get ...
814
  	return err;
413c239fa   Stephen Rothwell   driver-core: prep...
815
816
817
818
  }
  EXPORT_SYMBOL_GPL(dev_set_name);
  
  /**
e105b8bfc   Dan Williams   sysfs: add /sys/d...
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
   * 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
   * device_remove_sys_dev_entry() will disagree about the the presence
   * of the link.
   */
  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...
865
866
867
868
869
870
871
872
873
874
  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);
  	return 0;
  }
e105b8bfc   Dan Williams   sysfs: add /sys/d...
875
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
876
877
   * device_add - add device to device hierarchy.
   * @dev: device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
879
880
   * 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
881
   *
5739411ac   Cornelia Huck   Driver core: Clar...
882
   * This adds @dev to the kobject hierarchy via kobject_add(), adds it
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
883
884
   * 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...
885
886
887
888
   *
   * 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
889
890
891
892
   */
  int device_add(struct device *dev)
  {
  	struct device *parent = NULL;
ca22e56de   Kay Sievers   driver-core: impl...
893
  	struct kobject *kobj;
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
894
  	struct class_interface *class_intf;
c906a48ad   Greg Kroah-Hartman   driver core: add ...
895
  	int error = -EINVAL;
775b64d2b   Rafael J. Wysocki   PM: Acquire devic...
896

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
897
  	dev = get_device(dev);
c906a48ad   Greg Kroah-Hartman   driver core: add ...
898
899
  	if (!dev)
  		goto done;
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
900
  	if (!dev->p) {
b40284378   Greg Kroah-Hartman   Driver core: move...
901
902
903
  		error = device_private_init(dev);
  		if (error)
  			goto done;
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
904
  	}
fb069a5d1   Greg Kroah-Hartman   driver core: crea...
905

1fa5ae857   Kay Sievers   driver core: get ...
906
907
908
909
910
911
  	/*
  	 * 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 ...
912
  		dev_set_name(dev, "%s", dev->init_name);
1fa5ae857   Kay Sievers   driver core: get ...
913
914
  		dev->init_name = NULL;
  	}
c906a48ad   Greg Kroah-Hartman   driver core: add ...
915

ca22e56de   Kay Sievers   driver-core: impl...
916
917
918
  	/* 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 ...
919
920
  	if (!dev_name(dev)) {
  		error = -EINVAL;
5c8563d77   Kay Sievers   Driver Core: do n...
921
  		goto name_error;
e6309e756   Thomas Gleixner   Driver-core: Fix ...
922
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
923

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
  	parent = get_device(dev->parent);
ca22e56de   Kay Sievers   driver-core: impl...
928
929
930
  	kobj = get_device_parent(dev, parent);
  	if (kobj)
  		dev->kobj.parent = kobj;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931

0d358f22f   Yinghai Lu   driver core: try ...
932
933
934
  	/* use parent numa_node */
  	if (parent)
  		set_dev_node(dev, dev_to_node(parent));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
  	/* first, register with generic layer. */
8a577ffc7   Kay Sievers   driver: dont upda...
936
937
  	/* we require the name to be set before, and pass NULL */
  	error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
40fa54226   Greg Kroah-Hartman   Driver core: make...
938
  	if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
  		goto Error;
a7fd67062   Kay Sievers   [PATCH] add sysfs...
940

370226449   Brian Walsh   drivers/base: Pla...
941
942
943
  	/* notify platform of device entry */
  	if (platform_notify)
  		platform_notify(dev);
ad6a1e1c6   Tejun Heo   driver-core: make...
944
  	error = device_create_file(dev, &uevent_attr);
a306eea40   Cornelia Huck   driver core fixes...
945
946
  	if (error)
  		goto attrError;
a7fd67062   Kay Sievers   [PATCH] add sysfs...
947

23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
948
  	if (MAJOR(dev->devt)) {
ad6a1e1c6   Tejun Heo   driver-core: make...
949
950
  		error = device_create_file(dev, &devt_attr);
  		if (error)
a306eea40   Cornelia Huck   driver core fixes...
951
  			goto ueventattrError;
e105b8bfc   Dan Williams   sysfs: add /sys/d...
952
953
954
955
  
  		error = device_create_sys_dev_entry(dev);
  		if (error)
  			goto devtattrError;
2b2af54a5   Kay Sievers   Driver Core: devt...
956
957
  
  		devtmpfs_create_node(dev);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
958
  	}
2ee97caf0   Cornelia Huck   Driver core: chec...
959
960
961
  	error = device_add_class_symlinks(dev);
  	if (error)
  		goto SymlinkError;
dc0afa838   Cornelia Huck   Driver core: codi...
962
963
  	error = device_add_attrs(dev);
  	if (error)
2620efef7   Greg Kroah-Hartman   Driver core: add ...
964
  		goto AttrsError;
dc0afa838   Cornelia Huck   Driver core: codi...
965
966
  	error = bus_add_device(dev);
  	if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967
  		goto BusError;
3b98aeaf3   Alan Stern   PM: don't skip de...
968
  	error = dpm_sysfs_add(dev);
57eee3d23   Rafael J. Wysocki   Driver core: Call...
969
  	if (error)
3b98aeaf3   Alan Stern   PM: don't skip de...
970
971
  		goto DPMError;
  	device_pm_add(dev);
ec0676ee2   Alan Stern   Driver core: move...
972
973
974
975
976
977
978
  
  	/* Notify clients of device addition.  This call must come
  	 * after dpm_sysf_add() and before kobject_uevent().
  	 */
  	if (dev->bus)
  		blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  					     BUS_NOTIFY_ADD_DEVICE, dev);
83b5fb4cc   Cornelia Huck   Driver core: supp...
979
  	kobject_uevent(&dev->kobj, KOBJ_ADD);
2023c610d   Alan Stern   Driver core: add ...
980
  	bus_probe_device(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
981
  	if (parent)
f791b8c83   Greg Kroah-Hartman   driver core: move...
982
983
  		klist_add_tail(&dev->p->knode_parent,
  			       &parent->p->klist_children);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
984

5d9fd169c   Greg Kroah-Hartman   [PATCH] Driver co...
985
  	if (dev->class) {
ca22e56de   Kay Sievers   driver-core: impl...
986
  		mutex_lock(&dev->class->p->mutex);
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
987
  		/* tie the class to the device */
5a3ceb861   Tejun Heo   driver-core: use ...
988
  		klist_add_tail(&dev->knode_class,
6b6e39a6a   Kay Sievers   driver-core: merg...
989
  			       &dev->class->p->klist_devices);
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
990
991
  
  		/* notify any interfaces that the device is here */
184f1f779   Greg Kroah-Hartman   class: rename "in...
992
  		list_for_each_entry(class_intf,
ca22e56de   Kay Sievers   driver-core: impl...
993
  				    &dev->class->p->interfaces, node)
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
994
995
  			if (class_intf->add_dev)
  				class_intf->add_dev(dev, class_intf);
ca22e56de   Kay Sievers   driver-core: impl...
996
  		mutex_unlock(&dev->class->p->mutex);
5d9fd169c   Greg Kroah-Hartman   [PATCH] Driver co...
997
  	}
c906a48ad   Greg Kroah-Hartman   driver core: add ...
998
  done:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
1000
  	put_device(dev);
  	return error;
3b98aeaf3   Alan Stern   PM: don't skip de...
1001
   DPMError:
57eee3d23   Rafael J. Wysocki   Driver core: Call...
1002
1003
  	bus_remove_device(dev);
   BusError:
82f0cf9b7   James Simmons   Driver core: fix ...
1004
  	device_remove_attrs(dev);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
1005
   AttrsError:
2ee97caf0   Cornelia Huck   Driver core: chec...
1006
1007
  	device_remove_class_symlinks(dev);
   SymlinkError:
ad6a1e1c6   Tejun Heo   driver-core: make...
1008
  	if (MAJOR(dev->devt))
ad72956df   Kay Sievers   Driver Core: devt...
1009
1010
  		devtmpfs_delete_node(dev);
  	if (MAJOR(dev->devt))
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1011
1012
1013
  		device_remove_sys_dev_entry(dev);
   devtattrError:
  	if (MAJOR(dev->devt))
ad6a1e1c6   Tejun Heo   driver-core: make...
1014
  		device_remove_file(dev, &devt_attr);
a306eea40   Cornelia Huck   driver core fixes...
1015
   ueventattrError:
ad6a1e1c6   Tejun Heo   driver-core: make...
1016
  	device_remove_file(dev, &uevent_attr);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1017
   attrError:
312c004d3   Kay Sievers   [PATCH] driver co...
1018
  	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1019
1020
  	kobject_del(&dev->kobj);
   Error:
63b6971a0   Cornelia Huck   Driver core: Clea...
1021
  	cleanup_device_parent(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1022
1023
  	if (parent)
  		put_device(parent);
5c8563d77   Kay Sievers   Driver Core: do n...
1024
1025
1026
  name_error:
  	kfree(dev->p);
  	dev->p = NULL;
c906a48ad   Greg Kroah-Hartman   driver core: add ...
1027
  	goto done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1028
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1029
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1030
1031
   * device_register - register a device with the system.
   * @dev: pointer to the device structure
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1032
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1033
1034
1035
1036
1037
1038
   * 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...
1039
1040
1041
1042
   *
   * 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
1043
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1044
1045
1046
1047
1048
  int device_register(struct device *dev)
  {
  	device_initialize(dev);
  	return device_add(dev);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1049
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1050
1051
   * get_device - increment reference count for device.
   * @dev: device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1052
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1053
1054
1055
   * 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
1056
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1057
  struct device *get_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1058
1059
1060
  {
  	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1061
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1062
1063
   * put_device - decrement reference count.
   * @dev: device in question.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1064
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1065
  void put_device(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1066
  {
edfaa7c36   Kay Sievers   Driver core: conv...
1067
  	/* might_sleep(); */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1068
1069
1070
  	if (dev)
  		kobject_put(&dev->kobj);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1071
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1072
1073
   * device_del - delete device from system.
   * @dev: device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1074
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1075
1076
1077
1078
1079
   * 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
1080
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1081
1082
   * NOTE: this should be called manually _iff_ device_add() was
   * also called manually.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1083
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1084
  void device_del(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1085
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1086
  	struct device *parent = dev->parent;
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1087
  	struct class_interface *class_intf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1088

ec0676ee2   Alan Stern   Driver core: move...
1089
1090
1091
1092
1093
1094
  	/* 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);
775b64d2b   Rafael J. Wysocki   PM: Acquire devic...
1095
  	device_pm_remove(dev);
3b98aeaf3   Alan Stern   PM: don't skip de...
1096
  	dpm_sysfs_remove(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1097
  	if (parent)
f791b8c83   Greg Kroah-Hartman   driver core: move...
1098
  		klist_del(&dev->p->knode_parent);
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1099
  	if (MAJOR(dev->devt)) {
2b2af54a5   Kay Sievers   Driver Core: devt...
1100
  		devtmpfs_delete_node(dev);
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1101
  		device_remove_sys_dev_entry(dev);
ad6a1e1c6   Tejun Heo   driver-core: make...
1102
  		device_remove_file(dev, &devt_attr);
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1103
  	}
b9d9c82b4   Kay Sievers   [PATCH] Driver co...
1104
  	if (dev->class) {
da231fd5d   Kay Sievers   Driver core: fix ...
1105
  		device_remove_class_symlinks(dev);
99ef3ef8d   Kay Sievers   CONFIG_SYSFS_DEPR...
1106

ca22e56de   Kay Sievers   driver-core: impl...
1107
  		mutex_lock(&dev->class->p->mutex);
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1108
  		/* notify any interfaces that the device is now gone */
184f1f779   Greg Kroah-Hartman   class: rename "in...
1109
  		list_for_each_entry(class_intf,
ca22e56de   Kay Sievers   driver-core: impl...
1110
  				    &dev->class->p->interfaces, node)
c47ed219b   Greg Kroah-Hartman   Class: add suppor...
1111
1112
1113
  			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 ...
1114
  		klist_del(&dev->knode_class);
ca22e56de   Kay Sievers   driver-core: impl...
1115
  		mutex_unlock(&dev->class->p->mutex);
b9d9c82b4   Kay Sievers   [PATCH] Driver co...
1116
  	}
ad6a1e1c6   Tejun Heo   driver-core: make...
1117
  	device_remove_file(dev, &uevent_attr);
2620efef7   Greg Kroah-Hartman   Driver core: add ...
1118
  	device_remove_attrs(dev);
289535334   Benjamin Herrenschmidt   Driver core: Call...
1119
  	bus_remove_device(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1120

2f8d16a99   Tejun Heo   devres: release r...
1121
1122
1123
1124
1125
1126
  	/*
  	 * Some platform devices are driven without driver attached
  	 * and managed resources may have been acquired.  Make sure
  	 * all resources are released.
  	 */
  	devres_release_all(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1127
1128
1129
1130
1131
  	/* Notify the platform of the removal, in case they
  	 * need to do anything...
  	 */
  	if (platform_notify_remove)
  		platform_notify_remove(dev);
312c004d3   Kay Sievers   [PATCH] driver co...
1132
  	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
da231fd5d   Kay Sievers   Driver core: fix ...
1133
  	cleanup_device_parent(dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1134
  	kobject_del(&dev->kobj);
da231fd5d   Kay Sievers   Driver core: fix ...
1135
  	put_device(parent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1136
1137
1138
  }
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1139
1140
   * device_unregister - unregister device from system.
   * @dev: device going away.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1141
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1142
1143
1144
1145
1146
1147
   * 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
1148
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1149
  void device_unregister(struct device *dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1150
  {
1e0b2cf93   Kay Sievers   driver core: stru...
1151
1152
  	pr_debug("device: '%s': %s
  ", dev_name(dev), __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1153
1154
1155
  	device_del(dev);
  	put_device(dev);
  }
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1156
  static struct device *next_device(struct klist_iter *i)
36239577c   Patrick Mochel   [PATCH] Use a kli...
1157
  {
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1158
  	struct klist_node *n = klist_next(i);
f791b8c83   Greg Kroah-Hartman   driver core: move...
1159
1160
1161
1162
1163
1164
1165
1166
  	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...
1167
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1168
  /**
e454cea20   Kay Sievers   Driver-Core: exte...
1169
   * device_get_devnode - path of device node file
6fcf53acc   Kay Sievers   Driver Core: add ...
1170
   * @dev: device
e454cea20   Kay Sievers   Driver-Core: exte...
1171
   * @mode: returned file access mode
6fcf53acc   Kay Sievers   Driver Core: add ...
1172
1173
1174
1175
1176
1177
1178
   * @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...
1179
  const char *device_get_devnode(struct device *dev,
2c9ede55e   Al Viro   switch device_get...
1180
  			       umode_t *mode, const char **tmp)
6fcf53acc   Kay Sievers   Driver Core: add ...
1181
1182
1183
1184
1185
1186
  {
  	char *s;
  
  	*tmp = NULL;
  
  	/* the device type may provide a specific name */
e454cea20   Kay Sievers   Driver-Core: exte...
1187
1188
  	if (dev->type && dev->type->devnode)
  		*tmp = dev->type->devnode(dev, mode);
6fcf53acc   Kay Sievers   Driver Core: add ...
1189
1190
1191
1192
  	if (*tmp)
  		return *tmp;
  
  	/* the class may provide a specific name */
e454cea20   Kay Sievers   Driver-Core: exte...
1193
1194
  	if (dev->class && dev->class->devnode)
  		*tmp = dev->class->devnode(dev, mode);
6fcf53acc   Kay Sievers   Driver Core: add ...
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
  	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 '/' */
  	*tmp = kstrdup(dev_name(dev), GFP_KERNEL);
  	if (!*tmp)
  		return NULL;
  	while ((s = strchr(*tmp, '!')))
  		s[0] = '/';
  	return *tmp;
  }
  
  /**
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1212
1213
1214
1215
   * device_for_each_child - device child iterator.
   * @parent: parent struct device.
   * @data: data for the callback.
   * @fn: function to be called for each device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1216
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1217
1218
   * Iterate over @parent's child devices, and call @fn for each,
   * passing it @data.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1219
   *
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1220
1221
   * 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
1222
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1223
1224
  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
1225
  {
36239577c   Patrick Mochel   [PATCH] Use a kli...
1226
  	struct klist_iter i;
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1227
  	struct device *child;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1228
  	int error = 0;
014c90dbb   Greg Kroah-Hartman   driver core: prev...
1229
1230
  	if (!parent->p)
  		return 0;
f791b8c83   Greg Kroah-Hartman   driver core: move...
1231
  	klist_iter_init(&parent->p->klist_children, &i);
36239577c   Patrick Mochel   [PATCH] Use a kli...
1232
1233
1234
  	while ((child = next_device(&i)) && !error)
  		error = fn(child, data);
  	klist_iter_exit(&i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1235
1236
  	return error;
  }
5ab699810   Cornelia Huck   driver core: Intr...
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
  /**
   * device_find_child - device iterator for locating a particular device.
   * @parent: parent struct device
   * @data: Data to pass to match function
   * @match: Callback function to check device
   *
   * 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.
   */
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1252
1253
  struct device *device_find_child(struct device *parent, void *data,
  				 int (*match)(struct device *dev, void *data))
5ab699810   Cornelia Huck   driver core: Intr...
1254
1255
1256
1257
1258
1259
  {
  	struct klist_iter i;
  	struct device *child;
  
  	if (!parent)
  		return NULL;
f791b8c83   Greg Kroah-Hartman   driver core: move...
1260
  	klist_iter_init(&parent->p->klist_children, &i);
5ab699810   Cornelia Huck   driver core: Intr...
1261
1262
1263
1264
1265
1266
  	while ((child = next_device(&i)))
  		if (match(child, data) && get_device(child))
  			break;
  	klist_iter_exit(&i);
  	return child;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1267
1268
  int __init devices_init(void)
  {
881c6cfd7   Greg Kroah-Hartman   kset: convert /sy...
1269
1270
1271
  	devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  	if (!devices_kset)
  		return -ENOMEM;
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1272
1273
1274
1275
1276
1277
1278
1279
1280
  	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...
1281
  	return 0;
e105b8bfc   Dan Williams   sysfs: add /sys/d...
1282
1283
1284
1285
1286
1287
1288
1289
  
   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
1290
1291
1292
  }
  
  EXPORT_SYMBOL_GPL(device_for_each_child);
5ab699810   Cornelia Huck   driver core: Intr...
1293
  EXPORT_SYMBOL_GPL(device_find_child);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1294
1295
1296
1297
1298
1299
1300
1301
1302
  
  EXPORT_SYMBOL_GPL(device_initialize);
  EXPORT_SYMBOL_GPL(device_add);
  EXPORT_SYMBOL_GPL(device_register);
  
  EXPORT_SYMBOL_GPL(device_del);
  EXPORT_SYMBOL_GPL(device_unregister);
  EXPORT_SYMBOL_GPL(get_device);
  EXPORT_SYMBOL_GPL(put_device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1303
1304
1305
  
  EXPORT_SYMBOL_GPL(device_create_file);
  EXPORT_SYMBOL_GPL(device_remove_file);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1306

7f100d156   Karthigan Srinivasan   drivers/base/core...
1307
  struct root_device {
0aa0dc41b   Mark McLoughlin   driver core: add ...
1308
1309
1310
  	struct device dev;
  	struct module *owner;
  };
481e20799   Ferenc Wagner   driver core: Repl...
1311
1312
1313
1314
  inline struct root_device *to_root_device(struct device *d)
  {
  	return container_of(d, struct root_device, dev);
  }
0aa0dc41b   Mark McLoughlin   driver core: add ...
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
  
  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...
1339
1340
   * Returns &struct device pointer on success, or ERR_PTR() on error.
   *
0aa0dc41b   Mark McLoughlin   driver core: add ...
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
   * 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 ...
1351
  	err = dev_set_name(&root->dev, "%s", name);
0aa0dc41b   Mark McLoughlin   driver core: add ...
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
  	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 ...
1364
  #ifdef CONFIG_MODULES	/* gotta find a "cleaner" way to do this */
0aa0dc41b   Mark McLoughlin   driver core: add ...
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
  	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 ...
1383
   * @dev: device going away
0aa0dc41b   Mark McLoughlin   driver core: add ...
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
   *
   * 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...
1398
1399
1400
  
  static void device_create_release(struct device *dev)
  {
1e0b2cf93   Kay Sievers   driver core: stru...
1401
1402
  	pr_debug("device: '%s': %s
  ", dev_name(dev), __func__);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1403
1404
1405
1406
  	kfree(dev);
  }
  
  /**
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1407
   * device_create_vargs - creates a device and registers it with sysfs
42734dafa   Henrik Kretzschmar   [PATCH] Driver co...
1408
1409
1410
   * @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
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1411
   * @drvdata: the data to be added to the device for callbacks
42734dafa   Henrik Kretzschmar   [PATCH] Driver co...
1412
   * @fmt: string for the device's name
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1413
   * @args: va_list for the device's name
42734dafa   Henrik Kretzschmar   [PATCH] Driver co...
1414
1415
1416
   *
   * This function can be used by char device classes.  A struct device
   * will be created in sysfs, registered to the specified class.
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1417
   *
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1418
1419
   * A "dev" file will be created, showing the dev_t for the device, if
   * the dev_t is not 0,0.
42734dafa   Henrik Kretzschmar   [PATCH] Driver co...
1420
1421
1422
1423
   * 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
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1424
1425
   * pointer.
   *
f0eae0ed3   Jani Nikula   driver-core: docu...
1426
1427
   * Returns &struct device pointer on success, or ERR_PTR() on error.
   *
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1428
1429
1430
   * Note: the struct class passed to this function must have previously
   * been created with a call to class_create().
   */
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1431
1432
1433
  struct device *device_create_vargs(struct class *class, struct device *parent,
  				   dev_t devt, void *drvdata, const char *fmt,
  				   va_list args)
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1434
  {
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1435
1436
1437
1438
1439
  	struct device *dev = NULL;
  	int retval = -ENODEV;
  
  	if (class == NULL || IS_ERR(class))
  		goto error;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
  
  	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  	if (!dev) {
  		retval = -ENOMEM;
  		goto error;
  	}
  
  	dev->devt = devt;
  	dev->class = class;
  	dev->parent = parent;
  	dev->release = device_create_release;
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1451
  	dev_set_drvdata(dev, drvdata);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1452

1fa5ae857   Kay Sievers   driver core: get ...
1453
1454
1455
  	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  	if (retval)
  		goto error;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1456
1457
1458
  	retval = device_register(dev);
  	if (retval)
  		goto error;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1459
1460
1461
  	return dev;
  
  error:
286661b37   Cornelia Huck   Driver core: Fix ...
1462
  	put_device(dev);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1463
1464
  	return ERR_PTR(retval);
  }
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1465
1466
1467
  EXPORT_SYMBOL_GPL(device_create_vargs);
  
  /**
4e1067394   Greg Kroah-Hartman   device create: co...
1468
   * device_create - creates a device and registers it with sysfs
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
   * @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...
1486
1487
   * Returns &struct device pointer on success, or ERR_PTR() on error.
   *
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1488
1489
1490
   * 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...
1491
1492
  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 ...
1493
1494
1495
1496
1497
1498
1499
1500
1501
  {
  	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...
1502
  EXPORT_SYMBOL_GPL(device_create);
8882b3942   Greg Kroah-Hartman   Driver core: add ...
1503

cd35449b9   Dave Young   driver core: conv...
1504
  static int __match_devt(struct device *dev, void *data)
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1505
  {
cd35449b9   Dave Young   driver core: conv...
1506
  	dev_t *devt = data;
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1507

cd35449b9   Dave Young   driver core: conv...
1508
  	return dev->devt == *devt;
775b64d2b   Rafael J. Wysocki   PM: Acquire devic...
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
  }
  
  /**
   * 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...
1522

695794ae0   Greg Kroah-Hartman   Driver Core: add ...
1523
  	dev = class_find_device(class, NULL, &devt, __match_devt);
cd35449b9   Dave Young   driver core: conv...
1524
1525
  	if (dev) {
  		put_device(dev);
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1526
  		device_unregister(dev);
cd35449b9   Dave Young   driver core: conv...
1527
  	}
23681e479   Greg Kroah-Hartman   [PATCH] Driver co...
1528
1529
  }
  EXPORT_SYMBOL_GPL(device_destroy);
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1530
1531
1532
1533
1534
  
  /**
   * 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...
1535
1536
1537
1538
1539
   *
   * 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...
1540
   *
a5462516a   Timur Tabi   driver-core: docu...
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
   * 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 ...
1569
   */
6937e8f8c   Johannes Berg   driver core: devi...
1570
  int device_rename(struct device *dev, const char *new_name)
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1571
1572
1573
  {
  	char *old_class_name = NULL;
  	char *new_class_name = NULL;
2ee97caf0   Cornelia Huck   Driver core: chec...
1574
  	char *old_device_name = NULL;
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1575
1576
1577
1578
1579
  	int error;
  
  	dev = get_device(dev);
  	if (!dev)
  		return -EINVAL;
1e0b2cf93   Kay Sievers   driver core: stru...
1580
1581
  	pr_debug("device: '%s': %s: renaming to '%s'
  ", dev_name(dev),
2b3a302a0   Harvey Harrison   driver core: repl...
1582
  		 __func__, new_name);
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1583

1fa5ae857   Kay Sievers   driver core: get ...
1584
  	old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
2ee97caf0   Cornelia Huck   Driver core: chec...
1585
1586
1587
  	if (!old_device_name) {
  		error = -ENOMEM;
  		goto out;
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1588
  	}
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1589

f349cf347   Eric W. Biederman   driver core: Impl...
1590
  	if (dev->class) {
6b6e39a6a   Kay Sievers   driver-core: merg...
1591
  		error = sysfs_rename_link(&dev->class->p->subsys.kobj,
f349cf347   Eric W. Biederman   driver core: Impl...
1592
1593
1594
1595
  			&dev->kobj, old_device_name, new_name);
  		if (error)
  			goto out;
  	}
39aba963d   Kay Sievers   driver core: remo...
1596

a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1597
  	error = kobject_rename(&dev->kobj, new_name);
1fa5ae857   Kay Sievers   driver core: get ...
1598
  	if (error)
2ee97caf0   Cornelia Huck   Driver core: chec...
1599
  		goto out;
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1600

2ee97caf0   Cornelia Huck   Driver core: chec...
1601
  out:
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1602
  	put_device(dev);
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1603
  	kfree(new_class_name);
952ab431c   Jesper Juhl   Driver core: Don'...
1604
  	kfree(old_class_name);
2ee97caf0   Cornelia Huck   Driver core: chec...
1605
  	kfree(old_device_name);
a2de48cac   Greg Kroah-Hartman   Driver core: add ...
1606
1607
1608
  
  	return error;
  }
a2807dbcb   Johannes Berg   driver core: expo...
1609
  EXPORT_SYMBOL_GPL(device_rename);
8a82472f8   Cornelia Huck   driver core: Intr...
1610
1611
1612
1613
1614
  
  static int device_move_class_links(struct device *dev,
  				   struct device *old_parent,
  				   struct device *new_parent)
  {
f7f3461d8   Greg Kroah-Hartman   Driver core: add ...
1615
  	int error = 0;
8a82472f8   Cornelia Huck   driver core: Intr...
1616

f7f3461d8   Greg Kroah-Hartman   Driver core: add ...
1617
1618
1619
1620
1621
1622
  	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...
1623
1624
1625
1626
1627
  }
  
  /**
   * 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...
1628
   * @new_parent: the new parent of the device (can by NULL)
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1629
   * @dpm_order: how to reorder the dpm_list
8a82472f8   Cornelia Huck   driver core: Intr...
1630
   */
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1631
1632
  int device_move(struct device *dev, struct device *new_parent,
  		enum dpm_order dpm_order)
8a82472f8   Cornelia Huck   driver core: Intr...
1633
1634
1635
  {
  	int error;
  	struct device *old_parent;
c744aeae9   Cornelia Huck   driver core: Allo...
1636
  	struct kobject *new_parent_kobj;
8a82472f8   Cornelia Huck   driver core: Intr...
1637
1638
1639
1640
  
  	dev = get_device(dev);
  	if (!dev)
  		return -EINVAL;
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1641
  	device_pm_lock();
8a82472f8   Cornelia Huck   driver core: Intr...
1642
  	new_parent = get_device(new_parent);
4a3ad20cc   Greg Kroah-Hartman   Driver core: codi...
1643
  	new_parent_kobj = get_device_parent(dev, new_parent);
63b6971a0   Cornelia Huck   Driver core: Clea...
1644

1e0b2cf93   Kay Sievers   driver core: stru...
1645
1646
1647
  	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...
1648
  	error = kobject_move(&dev->kobj, new_parent_kobj);
8a82472f8   Cornelia Huck   driver core: Intr...
1649
  	if (error) {
63b6971a0   Cornelia Huck   Driver core: Clea...
1650
  		cleanup_glue_dir(dev, new_parent_kobj);
8a82472f8   Cornelia Huck   driver core: Intr...
1651
1652
1653
1654
1655
1656
  		put_device(new_parent);
  		goto out;
  	}
  	old_parent = dev->parent;
  	dev->parent = new_parent;
  	if (old_parent)
f791b8c83   Greg Kroah-Hartman   driver core: move...
1657
  		klist_remove(&dev->p->knode_parent);
0d358f22f   Yinghai Lu   driver core: try ...
1658
  	if (new_parent) {
f791b8c83   Greg Kroah-Hartman   driver core: move...
1659
1660
  		klist_add_tail(&dev->p->knode_parent,
  			       &new_parent->p->klist_children);
0d358f22f   Yinghai Lu   driver core: try ...
1661
1662
  		set_dev_node(dev, dev_to_node(new_parent));
  	}
8a82472f8   Cornelia Huck   driver core: Intr...
1663
1664
1665
1666
1667
1668
1669
  	if (!dev->class)
  		goto out_put;
  	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)) {
c744aeae9   Cornelia Huck   driver core: Allo...
1670
  			if (new_parent)
f791b8c83   Greg Kroah-Hartman   driver core: move...
1671
  				klist_remove(&dev->p->knode_parent);
0d358f22f   Yinghai Lu   driver core: try ...
1672
1673
  			dev->parent = old_parent;
  			if (old_parent) {
f791b8c83   Greg Kroah-Hartman   driver core: move...
1674
1675
  				klist_add_tail(&dev->p->knode_parent,
  					       &old_parent->p->klist_children);
0d358f22f   Yinghai Lu   driver core: try ...
1676
1677
  				set_dev_node(dev, dev_to_node(old_parent));
  			}
8a82472f8   Cornelia Huck   driver core: Intr...
1678
  		}
63b6971a0   Cornelia Huck   Driver core: Clea...
1679
  		cleanup_glue_dir(dev, new_parent_kobj);
8a82472f8   Cornelia Huck   driver core: Intr...
1680
1681
1682
  		put_device(new_parent);
  		goto out;
  	}
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
  	switch (dpm_order) {
  	case DPM_ORDER_NONE:
  		break;
  	case DPM_ORDER_DEV_AFTER_PARENT:
  		device_pm_move_after(dev, new_parent);
  		break;
  	case DPM_ORDER_PARENT_BEFORE_DEV:
  		device_pm_move_before(new_parent, dev);
  		break;
  	case DPM_ORDER_DEV_LAST:
  		device_pm_move_last(dev);
  		break;
  	}
8a82472f8   Cornelia Huck   driver core: Intr...
1696
1697
1698
  out_put:
  	put_device(old_parent);
  out:
ffa6a7054   Cornelia Huck   Driver core: Fix ...
1699
  	device_pm_unlock();
8a82472f8   Cornelia Huck   driver core: Intr...
1700
1701
1702
  	put_device(dev);
  	return error;
  }
8a82472f8   Cornelia Huck   driver core: Intr...
1703
  EXPORT_SYMBOL_GPL(device_move);
37b0c0203   Greg Kroah-Hartman   driver core: clea...
1704
1705
1706
1707
1708
1709
  
  /**
   * device_shutdown - call ->shutdown() on each device to shutdown.
   */
  void device_shutdown(void)
  {
6245838fe   Hugh Daschbach   Driver core: Prot...
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
  	struct device *dev;
  
  	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);
  		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:...
1728
1729
1730
1731
  
  		/* Don't allow any more runtime suspends */
  		pm_runtime_get_noresume(dev);
  		pm_runtime_barrier(dev);
37b0c0203   Greg Kroah-Hartman   driver core: clea...
1732

37b0c0203   Greg Kroah-Hartman   driver core: clea...
1733
1734
1735
1736
1737
1738
1739
1740
1741
  		if (dev->bus && dev->bus->shutdown) {
  			dev_dbg(dev, "shutdown
  ");
  			dev->bus->shutdown(dev);
  		} else if (dev->driver && dev->driver->shutdown) {
  			dev_dbg(dev, "shutdown
  ");
  			dev->driver->shutdown(dev);
  		}
6245838fe   Hugh Daschbach   Driver core: Prot...
1742
1743
1744
  		put_device(dev);
  
  		spin_lock(&devices_kset->list_lock);
37b0c0203   Greg Kroah-Hartman   driver core: clea...
1745
  	}
6245838fe   Hugh Daschbach   Driver core: Prot...
1746
  	spin_unlock(&devices_kset->list_lock);
401097ea4   Shaohua Li   driver core: sync...
1747
  	async_synchronize_full();
37b0c0203   Greg Kroah-Hartman   driver core: clea...
1748
  }
99bcf2171   Joe Perches   device.h drivers/...
1749
1750
1751
1752
1753
1754
  
  /*
   * Device logging functions
   */
  
  #ifdef CONFIG_PRINTK
cbc466355   Joe Perches   dynamic_debug: Ad...
1755
1756
  int __dev_printk(const char *level, const struct device *dev,
  		 struct va_format *vaf)
99bcf2171   Joe Perches   device.h drivers/...
1757
1758
1759
1760
1761
1762
1763
  {
  	if (!dev)
  		return printk("%s(NULL device *): %pV", level, vaf);
  
  	return printk("%s%s %s: %pV",
  		      level, dev_driver_string(dev), dev_name(dev), vaf);
  }
cbc466355   Joe Perches   dynamic_debug: Ad...
1764
  EXPORT_SYMBOL(__dev_printk);
99bcf2171   Joe Perches   device.h drivers/...
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
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
  
  int dev_printk(const char *level, const struct device *dev,
  	       const char *fmt, ...)
  {
  	struct va_format vaf;
  	va_list args;
  	int r;
  
  	va_start(args, fmt);
  
  	vaf.fmt = fmt;
  	vaf.va = &args;
  
  	r = __dev_printk(level, dev, &vaf);
  	va_end(args);
  
  	return r;
  }
  EXPORT_SYMBOL(dev_printk);
  
  #define define_dev_printk_level(func, kern_level)		\
  int func(const struct device *dev, const char *fmt, ...)	\
  {								\
  	struct va_format vaf;					\
  	va_list args;						\
  	int r;							\
  								\
  	va_start(args, fmt);					\
  								\
  	vaf.fmt = fmt;						\
  	vaf.va = &args;						\
  								\
  	r = __dev_printk(kern_level, dev, &vaf);		\
  	va_end(args);						\
  								\
  	return r;						\
  }								\
  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