blob: 3c5ff17f578fe58c83307b2c92086a22cd55136a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/base/core.c - core driver model code (device registration, etc)
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartman64bb5d22006-06-28 16:19:58 -07006 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is released under the GPLv2
10 *
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/device.h>
14#include <linux/err.h>
Rafael J. Wysocki97badf82015-04-03 23:23:37 +020015#include <linux/fwnode.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/string.h>
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -070020#include <linux/kdev_t.h>
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +100021#include <linux/notifier.h>
Grant Likely07d57a32012-02-01 11:22:22 -070022#include <linux/of.h>
23#include <linux/of_device.h>
Kay Sieversda231fd2007-11-21 17:29:15 +010024#include <linux/genhd.h>
Andrew Morton815d2d52008-03-04 15:09:07 -080025#include <linux/kallsyms.h>
Dave Youngf75b1c62008-05-28 09:28:39 -070026#include <linux/mutex.h>
Peter Chenaf8db152011-11-15 21:52:29 +010027#include <linux/pm_runtime.h>
Kay Sieversc4e00da2012-05-03 02:29:59 +020028#include <linux/netdevice.h>
Greg Kroah-Hartman63967682013-08-27 10:24:15 -070029#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "base.h"
32#include "power/power.h"
33
Andi Kleene52eec12010-09-08 16:54:17 +020034#ifdef CONFIG_SYSFS_DEPRECATED
35#ifdef CONFIG_SYSFS_DEPRECATED_V2
36long sysfs_deprecated = 1;
37#else
38long sysfs_deprecated = 0;
39#endif
Hanjun Guo3454bf92013-08-17 20:42:24 +080040static int __init sysfs_deprecated_setup(char *arg)
Andi Kleene52eec12010-09-08 16:54:17 +020041{
Jingoo Han34da5e62013-07-26 13:10:22 +090042 return kstrtol(arg, 10, &sysfs_deprecated);
Andi Kleene52eec12010-09-08 16:54:17 +020043}
44early_param("sysfs.deprecated", sysfs_deprecated_setup);
45#endif
46
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +010047/* Device links support. */
48
49#ifdef CONFIG_SRCU
50static DEFINE_MUTEX(device_links_lock);
51DEFINE_STATIC_SRCU(device_links_srcu);
52
53static inline void device_links_write_lock(void)
54{
55 mutex_lock(&device_links_lock);
56}
57
58static inline void device_links_write_unlock(void)
59{
60 mutex_unlock(&device_links_lock);
61}
62
63int device_links_read_lock(void)
64{
65 return srcu_read_lock(&device_links_srcu);
66}
67
68void device_links_read_unlock(int idx)
69{
70 srcu_read_unlock(&device_links_srcu, idx);
71}
72#else /* !CONFIG_SRCU */
73static DECLARE_RWSEM(device_links_lock);
74
75static inline void device_links_write_lock(void)
76{
77 down_write(&device_links_lock);
78}
79
80static inline void device_links_write_unlock(void)
81{
82 up_write(&device_links_lock);
83}
84
85int device_links_read_lock(void)
86{
87 down_read(&device_links_lock);
88 return 0;
89}
90
91void device_links_read_unlock(int not_used)
92{
93 up_read(&device_links_lock);
94}
95#endif /* !CONFIG_SRCU */
96
97/**
98 * device_is_dependent - Check if one device depends on another one
99 * @dev: Device to check dependencies for.
100 * @target: Device to check against.
101 *
102 * Check if @target depends on @dev or any device dependent on it (its child or
103 * its consumer etc). Return 1 if that is the case or 0 otherwise.
104 */
105static int device_is_dependent(struct device *dev, void *target)
106{
107 struct device_link *link;
108 int ret;
109
110 if (WARN_ON(dev == target))
111 return 1;
112
113 ret = device_for_each_child(dev, target, device_is_dependent);
114 if (ret)
115 return ret;
116
117 list_for_each_entry(link, &dev->links.consumers, s_node) {
118 if (WARN_ON(link->consumer == target))
119 return 1;
120
121 ret = device_is_dependent(link->consumer, target);
122 if (ret)
123 break;
124 }
125 return ret;
126}
127
128static int device_reorder_to_tail(struct device *dev, void *not_used)
129{
130 struct device_link *link;
131
132 /*
133 * Devices that have not been registered yet will be put to the ends
134 * of the lists during the registration, so skip them here.
135 */
136 if (device_is_registered(dev))
137 devices_kset_move_last(dev);
138
139 if (device_pm_initialized(dev))
140 device_pm_move_last(dev);
141
142 device_for_each_child(dev, NULL, device_reorder_to_tail);
143 list_for_each_entry(link, &dev->links.consumers, s_node)
144 device_reorder_to_tail(link->consumer, NULL);
145
146 return 0;
147}
148
149/**
150 * device_link_add - Create a link between two devices.
151 * @consumer: Consumer end of the link.
152 * @supplier: Supplier end of the link.
153 * @flags: Link flags.
154 *
155 * If the DL_FLAG_AUTOREMOVE is set, the link will be removed automatically
156 * when the consumer device driver unbinds from it. The combination of both
157 * DL_FLAG_AUTOREMOVE and DL_FLAG_STATELESS set is invalid and will cause NULL
158 * to be returned.
159 *
160 * A side effect of the link creation is re-ordering of dpm_list and the
161 * devices_kset list by moving the consumer device and all devices depending
162 * on it to the ends of these lists (that does not happen to devices that have
163 * not been registered when this function is called).
164 *
165 * The supplier device is required to be registered when this function is called
166 * and NULL will be returned if that is not the case. The consumer device need
167 * not be registerd, however.
168 */
169struct device_link *device_link_add(struct device *consumer,
170 struct device *supplier, u32 flags)
171{
172 struct device_link *link;
173
174 if (!consumer || !supplier ||
175 ((flags & DL_FLAG_STATELESS) && (flags & DL_FLAG_AUTOREMOVE)))
176 return NULL;
177
178 device_links_write_lock();
179 device_pm_lock();
180
181 /*
182 * If the supplier has not been fully registered yet or there is a
183 * reverse dependency between the consumer and the supplier already in
184 * the graph, return NULL.
185 */
186 if (!device_pm_initialized(supplier)
187 || device_is_dependent(consumer, supplier)) {
188 link = NULL;
189 goto out;
190 }
191
192 list_for_each_entry(link, &supplier->links.consumers, s_node)
193 if (link->consumer == consumer)
194 goto out;
195
196 link = kmalloc(sizeof(*link), GFP_KERNEL);
197 if (!link)
198 goto out;
199
200 get_device(supplier);
201 link->supplier = supplier;
202 INIT_LIST_HEAD(&link->s_node);
203 get_device(consumer);
204 link->consumer = consumer;
205 INIT_LIST_HEAD(&link->c_node);
206 link->flags = flags;
207
208 /* Deterine the initial link state. */
209 if (flags & DL_FLAG_STATELESS) {
210 link->status = DL_STATE_NONE;
211 } else {
212 switch (supplier->links.status) {
213 case DL_DEV_DRIVER_BOUND:
214 switch (consumer->links.status) {
215 case DL_DEV_PROBING:
216 link->status = DL_STATE_CONSUMER_PROBE;
217 break;
218 case DL_DEV_DRIVER_BOUND:
219 link->status = DL_STATE_ACTIVE;
220 break;
221 default:
222 link->status = DL_STATE_AVAILABLE;
223 break;
224 }
225 break;
226 case DL_DEV_UNBINDING:
227 link->status = DL_STATE_SUPPLIER_UNBIND;
228 break;
229 default:
230 link->status = DL_STATE_DORMANT;
231 break;
232 }
233 }
234
235 /*
236 * Move the consumer and all of the devices depending on it to the end
237 * of dpm_list and the devices_kset list.
238 *
239 * It is necessary to hold dpm_list locked throughout all that or else
240 * we may end up suspending with a wrong ordering of it.
241 */
242 device_reorder_to_tail(consumer, NULL);
243
244 list_add_tail_rcu(&link->s_node, &supplier->links.consumers);
245 list_add_tail_rcu(&link->c_node, &consumer->links.suppliers);
246
247 dev_info(consumer, "Linked as a consumer to %s\n", dev_name(supplier));
248
249 out:
250 device_pm_unlock();
251 device_links_write_unlock();
252 return link;
253}
254EXPORT_SYMBOL_GPL(device_link_add);
255
256static void device_link_free(struct device_link *link)
257{
258 put_device(link->consumer);
259 put_device(link->supplier);
260 kfree(link);
261}
262
263#ifdef CONFIG_SRCU
264static void __device_link_free_srcu(struct rcu_head *rhead)
265{
266 device_link_free(container_of(rhead, struct device_link, rcu_head));
267}
268
269static void __device_link_del(struct device_link *link)
270{
271 dev_info(link->consumer, "Dropping the link to %s\n",
272 dev_name(link->supplier));
273
274 list_del_rcu(&link->s_node);
275 list_del_rcu(&link->c_node);
276 call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);
277}
278#else /* !CONFIG_SRCU */
279static void __device_link_del(struct device_link *link)
280{
281 dev_info(link->consumer, "Dropping the link to %s\n",
282 dev_name(link->supplier));
283
284 list_del(&link->s_node);
285 list_del(&link->c_node);
286 device_link_free(link);
287}
288#endif /* !CONFIG_SRCU */
289
290/**
291 * device_link_del - Delete a link between two devices.
292 * @link: Device link to delete.
293 *
294 * The caller must ensure proper synchronization of this function with runtime
295 * PM.
296 */
297void device_link_del(struct device_link *link)
298{
299 device_links_write_lock();
300 device_pm_lock();
301 __device_link_del(link);
302 device_pm_unlock();
303 device_links_write_unlock();
304}
305EXPORT_SYMBOL_GPL(device_link_del);
306
307static void device_links_missing_supplier(struct device *dev)
308{
309 struct device_link *link;
310
311 list_for_each_entry(link, &dev->links.suppliers, c_node)
312 if (link->status == DL_STATE_CONSUMER_PROBE)
313 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
314}
315
316/**
317 * device_links_check_suppliers - Check presence of supplier drivers.
318 * @dev: Consumer device.
319 *
320 * Check links from this device to any suppliers. Walk the list of the device's
321 * links to suppliers and see if all of them are available. If not, simply
322 * return -EPROBE_DEFER.
323 *
324 * We need to guarantee that the supplier will not go away after the check has
325 * been positive here. It only can go away in __device_release_driver() and
326 * that function checks the device's links to consumers. This means we need to
327 * mark the link as "consumer probe in progress" to make the supplier removal
328 * wait for us to complete (or bad things may happen).
329 *
330 * Links with the DL_FLAG_STATELESS flag set are ignored.
331 */
332int device_links_check_suppliers(struct device *dev)
333{
334 struct device_link *link;
335 int ret = 0;
336
337 device_links_write_lock();
338
339 list_for_each_entry(link, &dev->links.suppliers, c_node) {
340 if (link->flags & DL_FLAG_STATELESS)
341 continue;
342
343 if (link->status != DL_STATE_AVAILABLE) {
344 device_links_missing_supplier(dev);
345 ret = -EPROBE_DEFER;
346 break;
347 }
348 WRITE_ONCE(link->status, DL_STATE_CONSUMER_PROBE);
349 }
350 dev->links.status = DL_DEV_PROBING;
351
352 device_links_write_unlock();
353 return ret;
354}
355
356/**
357 * device_links_driver_bound - Update device links after probing its driver.
358 * @dev: Device to update the links for.
359 *
360 * The probe has been successful, so update links from this device to any
361 * consumers by changing their status to "available".
362 *
363 * Also change the status of @dev's links to suppliers to "active".
364 *
365 * Links with the DL_FLAG_STATELESS flag set are ignored.
366 */
367void device_links_driver_bound(struct device *dev)
368{
369 struct device_link *link;
370
371 device_links_write_lock();
372
373 list_for_each_entry(link, &dev->links.consumers, s_node) {
374 if (link->flags & DL_FLAG_STATELESS)
375 continue;
376
377 WARN_ON(link->status != DL_STATE_DORMANT);
378 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
379 }
380
381 list_for_each_entry(link, &dev->links.suppliers, c_node) {
382 if (link->flags & DL_FLAG_STATELESS)
383 continue;
384
385 WARN_ON(link->status != DL_STATE_CONSUMER_PROBE);
386 WRITE_ONCE(link->status, DL_STATE_ACTIVE);
387 }
388
389 dev->links.status = DL_DEV_DRIVER_BOUND;
390
391 device_links_write_unlock();
392}
393
394/**
395 * __device_links_no_driver - Update links of a device without a driver.
396 * @dev: Device without a drvier.
397 *
398 * Delete all non-persistent links from this device to any suppliers.
399 *
400 * Persistent links stay around, but their status is changed to "available",
401 * unless they already are in the "supplier unbind in progress" state in which
402 * case they need not be updated.
403 *
404 * Links with the DL_FLAG_STATELESS flag set are ignored.
405 */
406static void __device_links_no_driver(struct device *dev)
407{
408 struct device_link *link, *ln;
409
410 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
411 if (link->flags & DL_FLAG_STATELESS)
412 continue;
413
414 if (link->flags & DL_FLAG_AUTOREMOVE)
415 __device_link_del(link);
416 else if (link->status != DL_STATE_SUPPLIER_UNBIND)
417 WRITE_ONCE(link->status, DL_STATE_AVAILABLE);
418 }
419
420 dev->links.status = DL_DEV_NO_DRIVER;
421}
422
423void device_links_no_driver(struct device *dev)
424{
425 device_links_write_lock();
426 __device_links_no_driver(dev);
427 device_links_write_unlock();
428}
429
430/**
431 * device_links_driver_cleanup - Update links after driver removal.
432 * @dev: Device whose driver has just gone away.
433 *
434 * Update links to consumers for @dev by changing their status to "dormant" and
435 * invoke %__device_links_no_driver() to update links to suppliers for it as
436 * appropriate.
437 *
438 * Links with the DL_FLAG_STATELESS flag set are ignored.
439 */
440void device_links_driver_cleanup(struct device *dev)
441{
442 struct device_link *link;
443
444 device_links_write_lock();
445
446 list_for_each_entry(link, &dev->links.consumers, s_node) {
447 if (link->flags & DL_FLAG_STATELESS)
448 continue;
449
450 WARN_ON(link->flags & DL_FLAG_AUTOREMOVE);
451 WARN_ON(link->status != DL_STATE_SUPPLIER_UNBIND);
452 WRITE_ONCE(link->status, DL_STATE_DORMANT);
453 }
454
455 __device_links_no_driver(dev);
456
457 device_links_write_unlock();
458}
459
460/**
461 * device_links_busy - Check if there are any busy links to consumers.
462 * @dev: Device to check.
463 *
464 * Check each consumer of the device and return 'true' if its link's status
465 * is one of "consumer probe" or "active" (meaning that the given consumer is
466 * probing right now or its driver is present). Otherwise, change the link
467 * state to "supplier unbind" to prevent the consumer from being probed
468 * successfully going forward.
469 *
470 * Return 'false' if there are no probing or active consumers.
471 *
472 * Links with the DL_FLAG_STATELESS flag set are ignored.
473 */
474bool device_links_busy(struct device *dev)
475{
476 struct device_link *link;
477 bool ret = false;
478
479 device_links_write_lock();
480
481 list_for_each_entry(link, &dev->links.consumers, s_node) {
482 if (link->flags & DL_FLAG_STATELESS)
483 continue;
484
485 if (link->status == DL_STATE_CONSUMER_PROBE
486 || link->status == DL_STATE_ACTIVE) {
487 ret = true;
488 break;
489 }
490 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
491 }
492
493 dev->links.status = DL_DEV_UNBINDING;
494
495 device_links_write_unlock();
496 return ret;
497}
498
499/**
500 * device_links_unbind_consumers - Force unbind consumers of the given device.
501 * @dev: Device to unbind the consumers of.
502 *
503 * Walk the list of links to consumers for @dev and if any of them is in the
504 * "consumer probe" state, wait for all device probes in progress to complete
505 * and start over.
506 *
507 * If that's not the case, change the status of the link to "supplier unbind"
508 * and check if the link was in the "active" state. If so, force the consumer
509 * driver to unbind and start over (the consumer will not re-probe as we have
510 * changed the state of the link already).
511 *
512 * Links with the DL_FLAG_STATELESS flag set are ignored.
513 */
514void device_links_unbind_consumers(struct device *dev)
515{
516 struct device_link *link;
517
518 start:
519 device_links_write_lock();
520
521 list_for_each_entry(link, &dev->links.consumers, s_node) {
522 enum device_link_state status;
523
524 if (link->flags & DL_FLAG_STATELESS)
525 continue;
526
527 status = link->status;
528 if (status == DL_STATE_CONSUMER_PROBE) {
529 device_links_write_unlock();
530
531 wait_for_device_probe();
532 goto start;
533 }
534 WRITE_ONCE(link->status, DL_STATE_SUPPLIER_UNBIND);
535 if (status == DL_STATE_ACTIVE) {
536 struct device *consumer = link->consumer;
537
538 get_device(consumer);
539
540 device_links_write_unlock();
541
542 device_release_driver_internal(consumer, NULL,
543 consumer->parent);
544 put_device(consumer);
545 goto start;
546 }
547 }
548
549 device_links_write_unlock();
550}
551
552/**
553 * device_links_purge - Delete existing links to other devices.
554 * @dev: Target device.
555 */
556static void device_links_purge(struct device *dev)
557{
558 struct device_link *link, *ln;
559
560 /*
561 * Delete all of the remaining links from this device to any other
562 * devices (either consumers or suppliers).
563 */
564 device_links_write_lock();
565
566 list_for_each_entry_safe_reverse(link, ln, &dev->links.suppliers, c_node) {
567 WARN_ON(link->status == DL_STATE_ACTIVE);
568 __device_link_del(link);
569 }
570
571 list_for_each_entry_safe_reverse(link, ln, &dev->links.consumers, s_node) {
572 WARN_ON(link->status != DL_STATE_DORMANT &&
573 link->status != DL_STATE_NONE);
574 __device_link_del(link);
575 }
576
577 device_links_write_unlock();
578}
579
580/* Device links support end. */
581
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800582int (*platform_notify)(struct device *dev) = NULL;
583int (*platform_notify_remove)(struct device *dev) = NULL;
Dan Williamse105b8b2008-04-21 10:51:07 -0700584static struct kobject *dev_kobj;
585struct kobject *sysfs_dev_char_kobj;
586struct kobject *sysfs_dev_block_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200588static DEFINE_MUTEX(device_hotplug_lock);
589
590void lock_device_hotplug(void)
591{
592 mutex_lock(&device_hotplug_lock);
593}
594
595void unlock_device_hotplug(void)
596{
597 mutex_unlock(&device_hotplug_lock);
598}
599
600int lock_device_hotplug_sysfs(void)
601{
602 if (mutex_trylock(&device_hotplug_lock))
603 return 0;
604
605 /* Avoid busy looping (5 ms of sleep should do). */
606 msleep(5);
607 return restart_syscall();
608}
609
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -0800610#ifdef CONFIG_BLOCK
611static inline int device_is_not_partition(struct device *dev)
612{
613 return !(dev->type == &part_type);
614}
615#else
616static inline int device_is_not_partition(struct device *dev)
617{
618 return 1;
619}
620#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Alan Stern3e956372006-06-16 17:10:48 -0400622/**
623 * dev_driver_string - Return a device's driver name, if at all possible
624 * @dev: struct device to get the name of
625 *
626 * Will return the device's driver's name if it is bound to a device. If
yan9169c012012-04-20 21:08:45 +0800627 * the device is not bound to a driver, it will return the name of the bus
Alan Stern3e956372006-06-16 17:10:48 -0400628 * it is attached to. If it is not attached to a bus either, an empty
629 * string will be returned.
630 */
Jean Delvarebf9ca692008-07-30 12:29:21 -0700631const char *dev_driver_string(const struct device *dev)
Alan Stern3e956372006-06-16 17:10:48 -0400632{
Alan Stern35899722009-12-04 11:06:57 -0500633 struct device_driver *drv;
634
635 /* dev->driver can change to NULL underneath us because of unbinding,
636 * so be careful about accessing it. dev->bus and dev->class should
637 * never change once they are set, so they don't need special care.
638 */
639 drv = ACCESS_ONCE(dev->driver);
640 return drv ? drv->name :
Jean Delvarea456b702007-03-09 16:33:10 +0100641 (dev->bus ? dev->bus->name :
642 (dev->class ? dev->class->name : ""));
Alan Stern3e956372006-06-16 17:10:48 -0400643}
Matthew Wilcox310a9222006-09-23 23:35:04 -0600644EXPORT_SYMBOL(dev_driver_string);
Alan Stern3e956372006-06-16 17:10:48 -0400645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
647
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800648static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
649 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800651 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200652 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500653 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (dev_attr->show)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400656 ret = dev_attr->show(dev, dev_attr, buf);
Andrew Morton815d2d52008-03-04 15:09:07 -0800657 if (ret >= (ssize_t)PAGE_SIZE) {
Greg Kroah-Hartman53a9c872013-01-17 13:10:23 -0800658 print_symbol("dev_attr_show: %s returned bad count\n",
659 (unsigned long)dev_attr->show);
Andrew Morton815d2d52008-03-04 15:09:07 -0800660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return ret;
662}
663
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800664static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
665 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800667 struct device_attribute *dev_attr = to_dev_attr(attr);
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200668 struct device *dev = kobj_to_dev(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -0500669 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (dev_attr->store)
Yani Ioannou54b6f352005-05-17 06:39:34 -0400672 ret = dev_attr->store(dev, dev_attr, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return ret;
674}
675
Emese Revfy52cf25d2010-01-19 02:58:23 +0100676static const struct sysfs_ops dev_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .show = dev_attr_show,
678 .store = dev_attr_store,
679};
680
Kay Sieversca22e562011-12-14 14:29:38 -0800681#define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
682
683ssize_t device_store_ulong(struct device *dev,
684 struct device_attribute *attr,
685 const char *buf, size_t size)
686{
687 struct dev_ext_attribute *ea = to_ext_attr(attr);
688 char *end;
689 unsigned long new = simple_strtoul(buf, &end, 0);
690 if (end == buf)
691 return -EINVAL;
692 *(unsigned long *)(ea->var) = new;
693 /* Always return full write size even if we didn't consume all */
694 return size;
695}
696EXPORT_SYMBOL_GPL(device_store_ulong);
697
698ssize_t device_show_ulong(struct device *dev,
699 struct device_attribute *attr,
700 char *buf)
701{
702 struct dev_ext_attribute *ea = to_ext_attr(attr);
703 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
704}
705EXPORT_SYMBOL_GPL(device_show_ulong);
706
707ssize_t device_store_int(struct device *dev,
708 struct device_attribute *attr,
709 const char *buf, size_t size)
710{
711 struct dev_ext_attribute *ea = to_ext_attr(attr);
712 char *end;
713 long new = simple_strtol(buf, &end, 0);
714 if (end == buf || new > INT_MAX || new < INT_MIN)
715 return -EINVAL;
716 *(int *)(ea->var) = new;
717 /* Always return full write size even if we didn't consume all */
718 return size;
719}
720EXPORT_SYMBOL_GPL(device_store_int);
721
722ssize_t device_show_int(struct device *dev,
723 struct device_attribute *attr,
724 char *buf)
725{
726 struct dev_ext_attribute *ea = to_ext_attr(attr);
727
728 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
729}
730EXPORT_SYMBOL_GPL(device_show_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Borislav Petkov91872392012-10-09 19:52:05 +0200732ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
733 const char *buf, size_t size)
734{
735 struct dev_ext_attribute *ea = to_ext_attr(attr);
736
737 if (strtobool(buf, ea->var) < 0)
738 return -EINVAL;
739
740 return size;
741}
742EXPORT_SYMBOL_GPL(device_store_bool);
743
744ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
745 char *buf)
746{
747 struct dev_ext_attribute *ea = to_ext_attr(attr);
748
749 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
750}
751EXPORT_SYMBOL_GPL(device_show_bool);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/**
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400754 * device_release - free device structure.
755 * @kobj: device's kobject.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -0400757 * This is called once the reference count for the object
758 * reaches 0. We forward the call to the device's release
759 * method, which should handle actually freeing the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800761static void device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200763 struct device *dev = kobj_to_dev(kobj);
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800764 struct device_private *p = dev->p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Ming Leia525a3d2012-07-25 01:42:29 +0800766 /*
767 * Some platform devices are driven without driver attached
768 * and managed resources may have been acquired. Make sure
769 * all resources are released.
770 *
771 * Drivers still can add resources into device after device
772 * is deleted but alive, so release devres here to avoid
773 * possible memory leak.
774 */
775 devres_release_all(dev);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (dev->release)
778 dev->release(dev);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200779 else if (dev->type && dev->type->release)
780 dev->type->release(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700781 else if (dev->class && dev->class->dev_release)
782 dev->class->dev_release(dev);
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700783 else
784 WARN(1, KERN_ERR "Device '%s' does not have a release() "
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800785 "function, it is broken and must be fixed.\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100786 dev_name(dev));
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -0800787 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700790static const void *device_namespace(struct kobject *kobj)
791{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200792 struct device *dev = kobj_to_dev(kobj);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700793 const void *ns = NULL;
794
795 if (dev->class && dev->class->ns_type)
796 ns = dev->class->namespace(dev);
797
798 return ns;
799}
800
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600801static struct kobj_type device_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 .release = device_release,
803 .sysfs_ops = &dev_sysfs_ops,
Eric W. Biedermanbc451f22010-03-30 11:31:25 -0700804 .namespace = device_namespace,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805};
806
807
Kay Sievers312c0042005-11-16 09:00:00 +0100808static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 struct kobj_type *ktype = get_ktype(kobj);
811
Greg Kroah-Hartman8f4afc42007-10-11 10:47:49 -0600812 if (ktype == &device_ktype) {
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200813 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (dev->bus)
815 return 1;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700816 if (dev->class)
817 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 return 0;
820}
821
Kay Sievers312c0042005-11-16 09:00:00 +0100822static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200824 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700826 if (dev->bus)
827 return dev->bus->name;
828 if (dev->class)
829 return dev->class->name;
830 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Kay Sievers7eff2e72007-08-14 15:15:12 +0200833static int dev_uevent(struct kset *kset, struct kobject *kobj,
834 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +0200836 struct device *dev = kobj_to_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int retval = 0;
838
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200839 /* add device node properties if present */
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700840 if (MAJOR(dev->devt)) {
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200841 const char *tmp;
842 const char *name;
Al Viro2c9ede52011-07-23 20:24:48 -0400843 umode_t mode = 0;
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700844 kuid_t uid = GLOBAL_ROOT_UID;
845 kgid_t gid = GLOBAL_ROOT_GID;
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200846
Kay Sievers7eff2e72007-08-14 15:15:12 +0200847 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
848 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700849 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200850 if (name) {
851 add_uevent_var(env, "DEVNAME=%s", name);
Kay Sieverse454cea2009-09-18 23:01:12 +0200852 if (mode)
853 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -0700854 if (!uid_eq(uid, GLOBAL_ROOT_UID))
855 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
856 if (!gid_eq(gid, GLOBAL_ROOT_GID))
857 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
Kay Sievers3c2670e2013-04-06 09:56:00 -0700858 kfree(tmp);
Kay Sievers6fcf53a2009-04-30 15:23:42 +0200859 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -0700860 }
861
Kay Sievers414264f2007-03-12 21:08:57 +0100862 if (dev->type && dev->type->name)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200863 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
Kay Sievers414264f2007-03-12 21:08:57 +0100864
Kay Sievers239378f2006-10-07 21:54:55 +0200865 if (dev->driver)
Kay Sievers7eff2e72007-08-14 15:15:12 +0200866 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
Kay Sievers239378f2006-10-07 21:54:55 +0200867
Grant Likely07d57a32012-02-01 11:22:22 -0700868 /* Add common DT information about the device */
869 of_device_uevent(dev, env);
870
Kay Sievers7eff2e72007-08-14 15:15:12 +0200871 /* have the bus specific function add its stuff */
Kay Sievers312c0042005-11-16 09:00:00 +0100872 if (dev->bus && dev->bus->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200873 retval = dev->bus->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200874 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800875 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100876 dev_name(dev), __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
Kay Sievers7eff2e72007-08-14 15:15:12 +0200879 /* have the class specific function add its stuff */
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700880 if (dev->class && dev->class->dev_uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200881 retval = dev->class->dev_uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200882 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800883 pr_debug("device: '%s': %s: class uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100884 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800885 __func__, retval);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200886 }
887
Stefan Weileef35c22010-08-06 21:11:15 +0200888 /* have the device type specific function add its stuff */
Kay Sieversf9f852d2006-10-07 21:54:55 +0200889 if (dev->type && dev->type->uevent) {
Kay Sievers7eff2e72007-08-14 15:15:12 +0200890 retval = dev->type->uevent(dev, env);
Kay Sieversf9f852d2006-10-07 21:54:55 +0200891 if (retval)
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800892 pr_debug("device: '%s': %s: dev_type uevent() "
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100893 "returned %d\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800894 __func__, retval);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -0700895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return retval;
898}
899
Emese Revfy9cd43612009-12-31 14:52:51 +0100900static const struct kset_uevent_ops device_uevent_ops = {
Kay Sievers312c0042005-11-16 09:00:00 +0100901 .filter = dev_uevent_filter,
902 .name = dev_uevent_name,
903 .uevent = dev_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904};
905
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700906static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
Kay Sievers16574dc2007-04-06 01:40:38 +0200907 char *buf)
908{
909 struct kobject *top_kobj;
910 struct kset *kset;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200911 struct kobj_uevent_env *env = NULL;
Kay Sievers16574dc2007-04-06 01:40:38 +0200912 int i;
913 size_t count = 0;
914 int retval;
915
916 /* search the kset, the device belongs to */
917 top_kobj = &dev->kobj;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200918 while (!top_kobj->kset && top_kobj->parent)
919 top_kobj = top_kobj->parent;
Kay Sievers16574dc2007-04-06 01:40:38 +0200920 if (!top_kobj->kset)
921 goto out;
Kay Sievers5c5daf62007-08-12 20:43:55 +0200922
Kay Sievers16574dc2007-04-06 01:40:38 +0200923 kset = top_kobj->kset;
924 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
925 goto out;
926
927 /* respect filter */
928 if (kset->uevent_ops && kset->uevent_ops->filter)
929 if (!kset->uevent_ops->filter(kset, &dev->kobj))
930 goto out;
931
Kay Sievers7eff2e72007-08-14 15:15:12 +0200932 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
933 if (!env)
Greg Kroah-Hartmanc7308c82007-05-02 14:14:11 +0200934 return -ENOMEM;
935
Kay Sievers16574dc2007-04-06 01:40:38 +0200936 /* let the kset specific function add its keys */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200937 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200938 if (retval)
939 goto out;
940
941 /* copy keys to file */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200942 for (i = 0; i < env->envp_idx; i++)
943 count += sprintf(&buf[count], "%s\n", env->envp[i]);
Kay Sievers16574dc2007-04-06 01:40:38 +0200944out:
Kay Sievers7eff2e72007-08-14 15:15:12 +0200945 kfree(env);
Kay Sievers16574dc2007-04-06 01:40:38 +0200946 return count;
947}
948
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700949static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
Kay Sieversa7fd6702005-10-01 14:49:43 +0200950 const char *buf, size_t count)
951{
Kay Sievers60a96a52007-07-08 22:29:26 +0200952 enum kobject_action action;
953
Kay Sievers3f5468c2010-01-14 22:54:37 +0100954 if (kobject_action_type(buf, count, &action) == 0)
Kay Sievers60a96a52007-07-08 22:29:26 +0200955 kobject_uevent(&dev->kobj, action);
Kay Sievers3f5468c2010-01-14 22:54:37 +0100956 else
957 dev_err(dev, "uevent: unknown action-string\n");
Kay Sieversa7fd6702005-10-01 14:49:43 +0200958 return count;
959}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700960static DEVICE_ATTR_RW(uevent);
Kay Sieversa7fd6702005-10-01 14:49:43 +0200961
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700962static ssize_t online_show(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200963 char *buf)
964{
965 bool val;
966
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200967 device_lock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200968 val = !dev->offline;
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200969 device_unlock(dev);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200970 return sprintf(buf, "%u\n", val);
971}
972
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700973static ssize_t online_store(struct device *dev, struct device_attribute *attr,
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200974 const char *buf, size_t count)
975{
976 bool val;
977 int ret;
978
979 ret = strtobool(buf, &val);
980 if (ret < 0)
981 return ret;
982
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200983 ret = lock_device_hotplug_sysfs();
984 if (ret)
985 return ret;
986
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200987 ret = val ? device_online(dev) : device_offline(dev);
988 unlock_device_hotplug();
989 return ret < 0 ? ret : count;
990}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -0700991static DEVICE_ATTR_RW(online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +0200992
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700993int device_add_groups(struct device *dev, const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500994{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -0700995 return sysfs_create_groups(&dev->kobj, groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -0500996}
997
Greg Kroah-Hartmanfa6fdb32013-08-08 15:22:55 -0700998void device_remove_groups(struct device *dev,
999 const struct attribute_group **groups)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001000{
Greg Kroah-Hartman3e9b2ba2013-08-21 13:47:50 -07001001 sysfs_remove_groups(&dev->kobj, groups);
Greg Kroah-Hartmande0ff002006-06-27 00:06:09 -07001002}
1003
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001004static int device_add_attrs(struct device *dev)
1005{
1006 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001007 const struct device_type *type = dev->type;
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001008 int error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001009
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001010 if (class) {
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001011 error = device_add_groups(dev, class->dev_groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001012 if (error)
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001013 return error;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001014 }
Kay Sieversf9f852d2006-10-07 21:54:55 +02001015
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001016 if (type) {
1017 error = device_add_groups(dev, type->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001018 if (error)
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001019 goto err_remove_class_groups;
Kay Sieversf9f852d2006-10-07 21:54:55 +02001020 }
1021
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001022 error = device_add_groups(dev, dev->groups);
1023 if (error)
1024 goto err_remove_type_groups;
1025
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001026 if (device_supports_offline(dev) && !dev->offline_disabled) {
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001027 error = device_create_file(dev, &dev_attr_online);
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001028 if (error)
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001029 goto err_remove_dev_groups;
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02001030 }
1031
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001032 return 0;
1033
Rafael J. Wysockiecfbf6f2013-12-12 06:11:02 +01001034 err_remove_dev_groups:
1035 device_remove_groups(dev, dev->groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001036 err_remove_type_groups:
1037 if (type)
1038 device_remove_groups(dev, type->groups);
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001039 err_remove_class_groups:
1040 if (class)
1041 device_remove_groups(dev, class->dev_groups);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001042
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001043 return error;
1044}
1045
1046static void device_remove_attrs(struct device *dev)
1047{
1048 struct class *class = dev->class;
Stephen Hemmingeraed65af2011-03-28 09:12:52 -07001049 const struct device_type *type = dev->type;
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001050
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001051 device_remove_file(dev, &dev_attr_online);
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001052 device_remove_groups(dev, dev->groups);
Kay Sieversf9f852d2006-10-07 21:54:55 +02001053
Dmitry Torokhov621a1672007-03-10 01:37:34 -05001054 if (type)
1055 device_remove_groups(dev, type->groups);
1056
Greg Kroah-Hartmana6b01de2013-10-05 18:19:30 -07001057 if (class)
Greg Kroah-Hartmand05a6f92013-07-14 16:05:58 -07001058 device_remove_groups(dev, class->dev_groups);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001059}
1060
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001061static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001062 char *buf)
1063{
1064 return print_dev_t(buf, dev->devt);
1065}
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001066static DEVICE_ATTR_RO(dev);
Tejun Heoad6a1e12007-06-14 03:45:17 +09001067
Kay Sieversca22e562011-12-14 14:29:38 -08001068/* /sys/devices/ */
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001069struct kset *devices_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/**
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03001072 * devices_kset_move_before - Move device in the devices_kset's list.
1073 * @deva: Device to move.
1074 * @devb: Device @deva should come before.
1075 */
1076static void devices_kset_move_before(struct device *deva, struct device *devb)
1077{
1078 if (!devices_kset)
1079 return;
1080 pr_debug("devices_kset: Moving %s before %s\n",
1081 dev_name(deva), dev_name(devb));
1082 spin_lock(&devices_kset->list_lock);
1083 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
1084 spin_unlock(&devices_kset->list_lock);
1085}
1086
1087/**
1088 * devices_kset_move_after - Move device in the devices_kset's list.
1089 * @deva: Device to move
1090 * @devb: Device @deva should come after.
1091 */
1092static void devices_kset_move_after(struct device *deva, struct device *devb)
1093{
1094 if (!devices_kset)
1095 return;
1096 pr_debug("devices_kset: Moving %s after %s\n",
1097 dev_name(deva), dev_name(devb));
1098 spin_lock(&devices_kset->list_lock);
1099 list_move(&deva->kobj.entry, &devb->kobj.entry);
1100 spin_unlock(&devices_kset->list_lock);
1101}
1102
1103/**
1104 * devices_kset_move_last - move the device to the end of devices_kset's list.
1105 * @dev: device to move
1106 */
1107void devices_kset_move_last(struct device *dev)
1108{
1109 if (!devices_kset)
1110 return;
1111 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
1112 spin_lock(&devices_kset->list_lock);
1113 list_move_tail(&dev->kobj.entry, &devices_kset->list);
1114 spin_unlock(&devices_kset->list_lock);
1115}
1116
1117/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001118 * device_create_file - create sysfs attribute file for device.
1119 * @dev: device.
1120 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001122int device_create_file(struct device *dev,
1123 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 int error = 0;
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001126
1127 if (dev) {
1128 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001129 "Attribute %s: write permission without 'store'\n",
1130 attr->attr.name);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001131 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
dyoung@redhat.com97521972013-05-16 14:31:30 +08001132 "Attribute %s: read permission without 'show'\n",
1133 attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 error = sysfs_create_file(&dev->kobj, &attr->attr);
Felipe Balbi8f46baa2013-02-20 10:31:42 +02001135 }
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return error;
1138}
David Graham White86df2682013-07-21 20:41:14 -04001139EXPORT_SYMBOL_GPL(device_create_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001142 * device_remove_file - remove sysfs attribute file.
1143 * @dev: device.
1144 * @attr: device attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 */
Phil Carmody26579ab2009-12-18 15:34:19 +02001146void device_remove_file(struct device *dev,
1147 const struct device_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Cornelia Huck0c98b192008-01-31 10:39:38 +01001149 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 sysfs_remove_file(&dev->kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
David Graham White86df2682013-07-21 20:41:14 -04001152EXPORT_SYMBOL_GPL(device_remove_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001154/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001155 * device_remove_file_self - remove sysfs attribute file from its own method.
1156 * @dev: device.
1157 * @attr: device attribute descriptor.
1158 *
1159 * See kernfs_remove_self() for details.
1160 */
1161bool device_remove_file_self(struct device *dev,
1162 const struct device_attribute *attr)
1163{
1164 if (dev)
1165 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
1166 else
1167 return false;
1168}
1169EXPORT_SYMBOL_GPL(device_remove_file_self);
1170
1171/**
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001172 * device_create_bin_file - create sysfs binary attribute file for device.
1173 * @dev: device.
1174 * @attr: device binary attribute descriptor.
1175 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001176int device_create_bin_file(struct device *dev,
1177 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001178{
1179 int error = -EINVAL;
1180 if (dev)
1181 error = sysfs_create_bin_file(&dev->kobj, attr);
1182 return error;
1183}
1184EXPORT_SYMBOL_GPL(device_create_bin_file);
1185
1186/**
1187 * device_remove_bin_file - remove sysfs binary attribute file
1188 * @dev: device.
1189 * @attr: device binary attribute descriptor.
1190 */
Phil Carmody66ecb922009-12-18 15:34:20 +02001191void device_remove_bin_file(struct device *dev,
1192 const struct bin_attribute *attr)
Greg Kroah-Hartman2589f1882006-09-19 09:39:19 -07001193{
1194 if (dev)
1195 sysfs_remove_bin_file(&dev->kobj, attr);
1196}
1197EXPORT_SYMBOL_GPL(device_remove_bin_file);
1198
James Bottomley34bb61f2005-09-06 16:56:51 -07001199static void klist_children_get(struct klist_node *n)
1200{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001201 struct device_private *p = to_device_private_parent(n);
1202 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001203
1204 get_device(dev);
1205}
1206
1207static void klist_children_put(struct klist_node *n)
1208{
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001209 struct device_private *p = to_device_private_parent(n);
1210 struct device *dev = p->device;
James Bottomley34bb61f2005-09-06 16:56:51 -07001211
1212 put_device(dev);
1213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001216 * device_initialize - init device structure.
1217 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 *
Cornelia Huck57394112008-09-03 18:26:40 +02001219 * This prepares the device for use by other layers by initializing
1220 * its fields.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001221 * It is the first half of device_register(), if called by
Cornelia Huck57394112008-09-03 18:26:40 +02001222 * that function, though it can also be called separately, so one
1223 * may use @dev's fields. In particular, get_device()/put_device()
1224 * may be used for reference counting of @dev after calling this
1225 * function.
1226 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001227 * All fields in @dev must be initialized by the caller to 0, except
1228 * for those explicitly set to some other value. The simplest
1229 * approach is to use kzalloc() to allocate the structure containing
1230 * @dev.
1231 *
Cornelia Huck57394112008-09-03 18:26:40 +02001232 * NOTE: Use put_device() to give up your reference instead of freeing
1233 * @dev directly once you have called this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235void device_initialize(struct device *dev)
1236{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001237 dev->kobj.kset = devices_kset;
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07001238 kobject_init(&dev->kobj, &device_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 INIT_LIST_HEAD(&dev->dma_pools);
Thomas Gleixner31427882010-01-29 20:39:02 +00001240 mutex_init(&dev->mutex);
Peter Zijlstra1704f472010-03-19 01:37:42 +01001241 lockdep_set_novalidate_class(&dev->mutex);
Tejun Heo9ac78492007-01-20 16:00:26 +09001242 spin_lock_init(&dev->devres_lock);
1243 INIT_LIST_HEAD(&dev->devres_head);
Alan Stern3b98aea2008-08-07 13:06:12 -04001244 device_pm_init(dev);
Christoph Hellwig87348132006-12-06 20:32:33 -08001245 set_dev_node(dev, -1);
Jiang Liu4a7cc832015-07-09 16:00:44 +08001246#ifdef CONFIG_GENERIC_MSI_IRQ
1247 INIT_LIST_HEAD(&dev->msi_list);
1248#endif
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001249 INIT_LIST_HEAD(&dev->links.consumers);
1250 INIT_LIST_HEAD(&dev->links.suppliers);
1251 dev->links.status = DL_DEV_NO_DRIVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
David Graham White86df2682013-07-21 20:41:14 -04001253EXPORT_SYMBOL_GPL(device_initialize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Tejun Heod73ce002013-03-12 11:30:05 -07001255struct kobject *virtual_device_parent(struct device *dev)
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001256{
Kay Sievers86406242007-03-14 03:25:56 +01001257 static struct kobject *virtual_dir = NULL;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001258
Kay Sievers86406242007-03-14 03:25:56 +01001259 if (!virtual_dir)
Greg Kroah-Hartman4ff6abf2007-11-05 22:24:43 -08001260 virtual_dir = kobject_create_and_add("virtual",
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06001261 &devices_kset->kobj);
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001262
Kay Sievers86406242007-03-14 03:25:56 +01001263 return virtual_dir;
Greg Kroah-Hartmanf0ee61a2006-10-23 10:40:54 -07001264}
1265
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001266struct class_dir {
1267 struct kobject kobj;
1268 struct class *class;
1269};
1270
1271#define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
1272
1273static void class_dir_release(struct kobject *kobj)
1274{
1275 struct class_dir *dir = to_class_dir(kobj);
1276 kfree(dir);
1277}
1278
1279static const
1280struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
1281{
1282 struct class_dir *dir = to_class_dir(kobj);
1283 return dir->class->ns_type;
1284}
1285
1286static struct kobj_type class_dir_ktype = {
1287 .release = class_dir_release,
1288 .sysfs_ops = &kobj_sysfs_ops,
1289 .child_ns_type = class_dir_child_ns_type
1290};
1291
1292static struct kobject *
1293class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
1294{
1295 struct class_dir *dir;
1296 int retval;
1297
1298 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1299 if (!dir)
1300 return NULL;
1301
1302 dir->class = class;
1303 kobject_init(&dir->kobj, &class_dir_ktype);
1304
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001305 dir->kobj.kset = &class->p->glue_dirs;
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001306
1307 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
1308 if (retval < 0) {
1309 kobject_put(&dir->kobj);
1310 return NULL;
1311 }
1312 return &dir->kobj;
1313}
1314
Yijing Wange4a60d12014-11-07 12:05:49 +08001315static DEFINE_MUTEX(gdp_mutex);
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001316
Kay Sieversda231fd2007-11-21 17:29:15 +01001317static struct kobject *get_device_parent(struct device *dev,
1318 struct device *parent)
Greg Kroah-Hartman40fa5422006-10-24 00:37:58 +01001319{
Kay Sievers86406242007-03-14 03:25:56 +01001320 if (dev->class) {
1321 struct kobject *kobj = NULL;
1322 struct kobject *parent_kobj;
1323 struct kobject *k;
1324
Randy Dunlapead454f2010-09-24 14:36:49 -07001325#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001326 /* block disks show up in /sys/block */
Andi Kleene52eec12010-09-08 16:54:17 +02001327 if (sysfs_deprecated && dev->class == &block_class) {
Kay Sievers39aba962010-09-04 22:33:14 -07001328 if (parent && parent->class == &block_class)
1329 return &parent->kobj;
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001330 return &block_class.p->subsys.kobj;
Kay Sievers39aba962010-09-04 22:33:14 -07001331 }
Randy Dunlapead454f2010-09-24 14:36:49 -07001332#endif
Andi Kleene52eec12010-09-08 16:54:17 +02001333
Kay Sievers86406242007-03-14 03:25:56 +01001334 /*
1335 * If we have no parent, we live in "virtual".
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001336 * Class-devices with a non class-device as parent, live
1337 * in a "glue" directory to prevent namespace collisions.
Kay Sievers86406242007-03-14 03:25:56 +01001338 */
1339 if (parent == NULL)
1340 parent_kobj = virtual_device_parent(dev);
Eric W. Biederman24b14422010-07-24 22:43:35 -07001341 else if (parent->class && !dev->class->ns_type)
Kay Sievers86406242007-03-14 03:25:56 +01001342 return &parent->kobj;
1343 else
1344 parent_kobj = &parent->kobj;
1345
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001346 mutex_lock(&gdp_mutex);
1347
Kay Sievers86406242007-03-14 03:25:56 +01001348 /* find our class-directory at the parent and reference it */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001349 spin_lock(&dev->class->p->glue_dirs.list_lock);
1350 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
Kay Sievers86406242007-03-14 03:25:56 +01001351 if (k->parent == parent_kobj) {
1352 kobj = kobject_get(k);
1353 break;
1354 }
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001355 spin_unlock(&dev->class->p->glue_dirs.list_lock);
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001356 if (kobj) {
1357 mutex_unlock(&gdp_mutex);
Kay Sievers86406242007-03-14 03:25:56 +01001358 return kobj;
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001359 }
Kay Sievers86406242007-03-14 03:25:56 +01001360
1361 /* or create a new class-directory at the parent device */
Eric W. Biedermanbc451f22010-03-30 11:31:25 -07001362 k = class_dir_create_and_add(dev->class, parent_kobj);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001363 /* do not emit an uevent for this simple "glue" directory */
Tejun Heo77d3d7c2010-02-05 17:57:02 +09001364 mutex_unlock(&gdp_mutex);
Greg Kroah-Hartman43968d22007-11-05 22:24:43 -08001365 return k;
Kay Sievers86406242007-03-14 03:25:56 +01001366 }
1367
Kay Sieversca22e562011-12-14 14:29:38 -08001368 /* subsystems can specify a default root directory for their devices */
1369 if (!parent && dev->bus && dev->bus->dev_root)
1370 return &dev->bus->dev_root->kobj;
1371
Kay Sievers86406242007-03-14 03:25:56 +01001372 if (parent)
Cornelia Huckc744aeae2007-01-08 20:16:44 +01001373 return &parent->kobj;
1374 return NULL;
1375}
Kay Sieversda231fd2007-11-21 17:29:15 +01001376
Ming Leicebf8fd2016-07-10 19:27:36 +08001377static inline bool live_in_glue_dir(struct kobject *kobj,
1378 struct device *dev)
1379{
1380 if (!kobj || !dev->class ||
1381 kobj->kset != &dev->class->p->glue_dirs)
1382 return false;
1383 return true;
1384}
1385
1386static inline struct kobject *get_glue_dir(struct device *dev)
1387{
1388 return dev->kobj.parent;
1389}
1390
1391/*
1392 * make sure cleaning up dir as the last step, we need to make
1393 * sure .release handler of kobject is run with holding the
1394 * global lock
1395 */
Cornelia Huck63b69712008-01-21 16:09:44 +01001396static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
Kay Sieversda231fd2007-11-21 17:29:15 +01001397{
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001398 /* see if we live in a "glue" directory */
Ming Leicebf8fd2016-07-10 19:27:36 +08001399 if (!live_in_glue_dir(glue_dir, dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001400 return;
1401
Yijing Wange4a60d12014-11-07 12:05:49 +08001402 mutex_lock(&gdp_mutex);
Kay Sievers0f4dafc2007-12-19 01:40:42 +01001403 kobject_put(glue_dir);
Yijing Wange4a60d12014-11-07 12:05:49 +08001404 mutex_unlock(&gdp_mutex);
Kay Sieversda231fd2007-11-21 17:29:15 +01001405}
Cornelia Huck63b69712008-01-21 16:09:44 +01001406
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001407static int device_add_class_symlinks(struct device *dev)
1408{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001409 struct device_node *of_node = dev_of_node(dev);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001410 int error;
1411
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001412 if (of_node) {
1413 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
1414 if (error)
1415 dev_warn(dev, "Error %d creating of_node link\n",error);
1416 /* An error here doesn't warrant bringing down the device */
1417 }
1418
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001419 if (!dev->class)
1420 return 0;
Kay Sieversda231fd2007-11-21 17:29:15 +01001421
Greg Kroah-Hartman1fbfee62008-05-28 09:28:39 -07001422 error = sysfs_create_link(&dev->kobj,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001423 &dev->class->p->subsys.kobj,
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001424 "subsystem");
1425 if (error)
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001426 goto out_devnode;
Kay Sieversda231fd2007-11-21 17:29:15 +01001427
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001428 if (dev->parent && device_is_not_partition(dev)) {
Dmitry Torokhov4f01a752007-09-18 22:46:50 -07001429 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
1430 "device");
1431 if (error)
Kay Sievers39aba962010-09-04 22:33:14 -07001432 goto out_subsys;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001433 }
Kay Sievers39aba962010-09-04 22:33:14 -07001434
Randy Dunlapead454f2010-09-24 14:36:49 -07001435#ifdef CONFIG_BLOCK
Kay Sievers39aba962010-09-04 22:33:14 -07001436 /* /sys/block has directories and does not need symlinks */
Andi Kleene52eec12010-09-08 16:54:17 +02001437 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001438 return 0;
Randy Dunlapead454f2010-09-24 14:36:49 -07001439#endif
Kay Sievers39aba962010-09-04 22:33:14 -07001440
1441 /* link in the class directory pointing to the device */
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001442 error = sysfs_create_link(&dev->class->p->subsys.kobj,
Kay Sievers39aba962010-09-04 22:33:14 -07001443 &dev->kobj, dev_name(dev));
1444 if (error)
1445 goto out_device;
1446
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001447 return 0;
1448
Kay Sievers39aba962010-09-04 22:33:14 -07001449out_device:
1450 sysfs_remove_link(&dev->kobj, "device");
Kay Sieversda231fd2007-11-21 17:29:15 +01001451
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001452out_subsys:
1453 sysfs_remove_link(&dev->kobj, "subsystem");
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001454out_devnode:
1455 sysfs_remove_link(&dev->kobj, "of_node");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001456 return error;
1457}
1458
1459static void device_remove_class_symlinks(struct device *dev)
1460{
Benjamin Herrenschmidt5590f312015-02-18 11:25:18 +11001461 if (dev_of_node(dev))
1462 sysfs_remove_link(&dev->kobj, "of_node");
1463
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001464 if (!dev->class)
1465 return;
Kay Sieversda231fd2007-11-21 17:29:15 +01001466
Greg Kroah-Hartman4e886c22008-01-27 12:12:43 -08001467 if (dev->parent && device_is_not_partition(dev))
Kay Sieversda231fd2007-11-21 17:29:15 +01001468 sysfs_remove_link(&dev->kobj, "device");
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001469 sysfs_remove_link(&dev->kobj, "subsystem");
Randy Dunlapead454f2010-09-24 14:36:49 -07001470#ifdef CONFIG_BLOCK
Andi Kleene52eec12010-09-08 16:54:17 +02001471 if (sysfs_deprecated && dev->class == &block_class)
Kay Sievers39aba962010-09-04 22:33:14 -07001472 return;
Randy Dunlapead454f2010-09-24 14:36:49 -07001473#endif
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001474 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477/**
Stephen Rothwell413c2392008-05-30 10:16:40 +10001478 * dev_set_name - set a device name
1479 * @dev: device
Randy Dunlap46232362008-06-04 21:40:43 -07001480 * @fmt: format string for the device's name
Stephen Rothwell413c2392008-05-30 10:16:40 +10001481 */
1482int dev_set_name(struct device *dev, const char *fmt, ...)
1483{
1484 va_list vargs;
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001485 int err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001486
1487 va_start(vargs, fmt);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001488 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
Stephen Rothwell413c2392008-05-30 10:16:40 +10001489 va_end(vargs);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001490 return err;
Stephen Rothwell413c2392008-05-30 10:16:40 +10001491}
1492EXPORT_SYMBOL_GPL(dev_set_name);
1493
1494/**
Dan Williamse105b8b2008-04-21 10:51:07 -07001495 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1496 * @dev: device
1497 *
1498 * By default we select char/ for new entries. Setting class->dev_obj
1499 * to NULL prevents an entry from being created. class->dev_kobj must
1500 * be set (or cleared) before any devices are registered to the class
1501 * otherwise device_create_sys_dev_entry() and
Peter Korsgaard0d4e293c2012-04-17 12:12:57 +02001502 * device_remove_sys_dev_entry() will disagree about the presence of
1503 * the link.
Dan Williamse105b8b2008-04-21 10:51:07 -07001504 */
1505static struct kobject *device_to_dev_kobj(struct device *dev)
1506{
1507 struct kobject *kobj;
1508
1509 if (dev->class)
1510 kobj = dev->class->dev_kobj;
1511 else
1512 kobj = sysfs_dev_char_kobj;
1513
1514 return kobj;
1515}
1516
1517static int device_create_sys_dev_entry(struct device *dev)
1518{
1519 struct kobject *kobj = device_to_dev_kobj(dev);
1520 int error = 0;
1521 char devt_str[15];
1522
1523 if (kobj) {
1524 format_dev_t(devt_str, dev->devt);
1525 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1526 }
1527
1528 return error;
1529}
1530
1531static void device_remove_sys_dev_entry(struct device *dev)
1532{
1533 struct kobject *kobj = device_to_dev_kobj(dev);
1534 char devt_str[15];
1535
1536 if (kobj) {
1537 format_dev_t(devt_str, dev->devt);
1538 sysfs_remove_link(kobj, devt_str);
1539 }
1540}
1541
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001542int device_private_init(struct device *dev)
1543{
1544 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1545 if (!dev->p)
1546 return -ENOMEM;
1547 dev->p->device = dev;
1548 klist_init(&dev->p->klist_children, klist_children_get,
1549 klist_children_put);
Greg Kroah-Hartmanef8a3fd2012-03-08 12:17:22 -08001550 INIT_LIST_HEAD(&dev->p->deferred_probe);
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001551 return 0;
1552}
1553
Dan Williamse105b8b2008-04-21 10:51:07 -07001554/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001555 * device_add - add device to device hierarchy.
1556 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001558 * This is part 2 of device_register(), though may be called
1559 * separately _iff_ device_initialize() has been called separately.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 *
Cornelia Huck57394112008-09-03 18:26:40 +02001561 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001562 * to the global and sibling lists for the device, then
1563 * adds it to the other relevant subsystems of the driver model.
Cornelia Huck57394112008-09-03 18:26:40 +02001564 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001565 * Do not call this routine or device_register() more than once for
1566 * any device structure. The driver model core is not designed to work
1567 * with devices that get unregistered and then spring back to life.
1568 * (Among other things, it's very hard to guarantee that all references
1569 * to the previous incarnation of @dev have been dropped.) Allocate
1570 * and register a fresh new struct device instead.
1571 *
Cornelia Huck57394112008-09-03 18:26:40 +02001572 * NOTE: _Never_ directly free @dev after calling this function, even
1573 * if it returned an error! Always use put_device() to give up your
1574 * reference instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
1576int device_add(struct device *dev)
1577{
1578 struct device *parent = NULL;
Kay Sieversca22e562011-12-14 14:29:38 -08001579 struct kobject *kobj;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001580 struct class_interface *class_intf;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001581 int error = -EINVAL;
Ming Leicebf8fd2016-07-10 19:27:36 +08001582 struct kobject *glue_dir = NULL;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 dev = get_device(dev);
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001585 if (!dev)
1586 goto done;
1587
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001588 if (!dev->p) {
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -07001589 error = device_private_init(dev);
1590 if (error)
1591 goto done;
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001592 }
Greg Kroah-Hartmanfb069a52008-12-16 12:23:36 -08001593
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001594 /*
1595 * for statically allocated devices, which should all be converted
1596 * some day, we need to initialize the name. We prevent reading back
1597 * the name, and force the use of dev_name()
1598 */
1599 if (dev->init_name) {
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07001600 dev_set_name(dev, "%s", dev->init_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01001601 dev->init_name = NULL;
1602 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001603
Kay Sieversca22e562011-12-14 14:29:38 -08001604 /* subsystems can specify simple device enumeration */
1605 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1606 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1607
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001608 if (!dev_name(dev)) {
1609 error = -EINVAL;
Kay Sievers5c8563d2009-05-28 14:24:07 -07001610 goto name_error;
Thomas Gleixnere6309e72009-12-10 19:32:49 +00001611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001613 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartmanc205ef42006-08-07 22:19:37 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 parent = get_device(dev->parent);
Kay Sieversca22e562011-12-14 14:29:38 -08001616 kobj = get_device_parent(dev, parent);
1617 if (kobj)
1618 dev->kobj.parent = kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Yinghai Lu0d358f22008-02-19 03:20:41 -08001620 /* use parent numa_node */
Zhen Lei56f2de82015-08-25 12:08:22 +08001621 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
Yinghai Lu0d358f22008-02-19 03:20:41 -08001622 set_dev_node(dev, dev_to_node(parent));
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* first, register with generic layer. */
Kay Sievers8a577ff2009-04-18 15:05:45 -07001625 /* we require the name to be set before, and pass NULL */
1626 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
Ming Leicebf8fd2016-07-10 19:27:36 +08001627 if (error) {
1628 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 goto Error;
Ming Leicebf8fd2016-07-10 19:27:36 +08001630 }
Kay Sieversa7fd6702005-10-01 14:49:43 +02001631
Brian Walsh37022642006-08-14 22:43:19 -07001632 /* notify platform of device entry */
1633 if (platform_notify)
1634 platform_notify(dev);
1635
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001636 error = device_create_file(dev, &dev_attr_uevent);
Cornelia Hucka306eea2006-09-22 11:37:13 +02001637 if (error)
1638 goto attrError;
Kay Sieversa7fd6702005-10-01 14:49:43 +02001639
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001640 error = device_add_class_symlinks(dev);
1641 if (error)
1642 goto SymlinkError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001643 error = device_add_attrs(dev);
1644 if (error)
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001645 goto AttrsError;
Cornelia Huckdc0afa82007-07-09 11:39:18 -07001646 error = bus_add_device(dev);
1647 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto BusError;
Alan Stern3b98aea2008-08-07 13:06:12 -04001649 error = dpm_sysfs_add(dev);
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001650 if (error)
Alan Stern3b98aea2008-08-07 13:06:12 -04001651 goto DPMError;
1652 device_pm_add(dev);
Alan Sternec0676ee2008-12-05 14:10:31 -05001653
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001654 if (MAJOR(dev->devt)) {
1655 error = device_create_file(dev, &dev_attr_dev);
1656 if (error)
1657 goto DevAttrError;
1658
1659 error = device_create_sys_dev_entry(dev);
1660 if (error)
1661 goto SysEntryError;
1662
1663 devtmpfs_create_node(dev);
1664 }
1665
Alan Sternec0676ee2008-12-05 14:10:31 -05001666 /* Notify clients of device addition. This call must come
majianpeng268863f2012-01-11 15:12:06 +00001667 * after dpm_sysfs_add() and before kobject_uevent().
Alan Sternec0676ee2008-12-05 14:10:31 -05001668 */
1669 if (dev->bus)
1670 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1671 BUS_NOTIFY_ADD_DEVICE, dev);
1672
Cornelia Huck83b5fb4c2007-03-29 11:12:11 +02001673 kobject_uevent(&dev->kobj, KOBJ_ADD);
Alan Stern2023c612009-07-30 15:27:18 -04001674 bus_probe_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001676 klist_add_tail(&dev->p->knode_parent,
1677 &parent->p->klist_children);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001679 if (dev->class) {
Kay Sieversca22e562011-12-14 14:29:38 -08001680 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001681 /* tie the class to the device */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001682 klist_add_tail(&dev->knode_class,
Kay Sievers6b6e39a2010-11-15 23:13:18 +01001683 &dev->class->p->klist_devices);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001684
1685 /* notify any interfaces that the device is here */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001686 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001687 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001688 if (class_intf->add_dev)
1689 class_intf->add_dev(dev, class_intf);
Kay Sieversca22e562011-12-14 14:29:38 -08001690 mutex_unlock(&dev->class->p->mutex);
Greg Kroah-Hartman5d9fd162006-06-22 17:17:32 -07001691 }
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001692done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 put_device(dev);
1694 return error;
Sergey Klyaus0cd75042014-10-08 11:31:54 +04001695 SysEntryError:
1696 if (MAJOR(dev->devt))
1697 device_remove_file(dev, &dev_attr_dev);
1698 DevAttrError:
1699 device_pm_remove(dev);
1700 dpm_sysfs_remove(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001701 DPMError:
Rafael J. Wysocki57eee3d2008-03-12 00:59:38 +01001702 bus_remove_device(dev);
1703 BusError:
James Simmons82f0cf92007-02-21 17:44:51 +00001704 device_remove_attrs(dev);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001705 AttrsError:
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07001706 device_remove_class_symlinks(dev);
1707 SymlinkError:
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001708 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07001709 attrError:
Kay Sievers312c0042005-11-16 09:00:00 +01001710 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001711 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 kobject_del(&dev->kobj);
1713 Error:
Ming Leicebf8fd2016-07-10 19:27:36 +08001714 cleanup_glue_dir(dev, glue_dir);
Markus Elfring5f0163a2015-02-05 11:48:26 +01001715 put_device(parent);
Kay Sievers5c8563d2009-05-28 14:24:07 -07001716name_error:
1717 kfree(dev->p);
1718 dev->p = NULL;
Greg Kroah-Hartmanc906a482008-05-30 10:45:12 -07001719 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
David Graham White86df2682013-07-21 20:41:14 -04001721EXPORT_SYMBOL_GPL(device_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001724 * device_register - register a device with the system.
1725 * @dev: pointer to the device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001727 * This happens in two clean steps - initialize the device
1728 * and add it to the system. The two steps can be called
1729 * separately, but this is the easiest and most common.
1730 * I.e. you should only call the two helpers separately if
1731 * have a clearly defined need to use and refcount the device
1732 * before it is added to the hierarchy.
Cornelia Huck57394112008-09-03 18:26:40 +02001733 *
Alan Sternb10d5ef2012-01-17 11:39:00 -05001734 * For more information, see the kerneldoc for device_initialize()
1735 * and device_add().
1736 *
Cornelia Huck57394112008-09-03 18:26:40 +02001737 * NOTE: _Never_ directly free @dev after calling this function, even
1738 * if it returned an error! Always use put_device() to give up the
1739 * reference initialized in this function instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741int device_register(struct device *dev)
1742{
1743 device_initialize(dev);
1744 return device_add(dev);
1745}
David Graham White86df2682013-07-21 20:41:14 -04001746EXPORT_SYMBOL_GPL(device_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001749 * get_device - increment reference count for device.
1750 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001752 * This simply forwards the call to kobject_get(), though
1753 * we do take care to provide for the case that we get a NULL
1754 * pointer passed in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001756struct device *get_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Lars-Peter Clausenb0d1f802012-07-03 18:49:36 +02001758 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
David Graham White86df2682013-07-21 20:41:14 -04001760EXPORT_SYMBOL_GPL(get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001763 * put_device - decrement reference count.
1764 * @dev: device in question.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001766void put_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001768 /* might_sleep(); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (dev)
1770 kobject_put(&dev->kobj);
1771}
David Graham White86df2682013-07-21 20:41:14 -04001772EXPORT_SYMBOL_GPL(put_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001775 * device_del - delete device from system.
1776 * @dev: device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001778 * This is the first part of the device unregistration
1779 * sequence. This removes the device from the lists we control
1780 * from here, has it removed from the other driver model
1781 * subsystems it was added to in device_add(), and removes it
1782 * from the kobject hierarchy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001784 * NOTE: this should be called manually _iff_ device_add() was
1785 * also called manually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001787void device_del(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001789 struct device *parent = dev->parent;
Ming Leicebf8fd2016-07-10 19:27:36 +08001790 struct kobject *glue_dir = NULL;
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001791 struct class_interface *class_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Alan Sternec0676ee2008-12-05 14:10:31 -05001793 /* Notify clients of device removal. This call must come
1794 * before dpm_sysfs_remove().
1795 */
1796 if (dev->bus)
1797 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1798 BUS_NOTIFY_DEL_DEVICE, dev);
Rafael J. Wysocki9ed98952016-10-30 17:32:16 +01001799
1800 device_links_purge(dev);
Alan Stern3b98aea2008-08-07 13:06:12 -04001801 dpm_sysfs_remove(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001803 klist_del(&dev->p->knode_parent);
Dan Williamse105b8b2008-04-21 10:51:07 -07001804 if (MAJOR(dev->devt)) {
Kay Sievers2b2af542009-04-30 15:23:42 +02001805 devtmpfs_delete_node(dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001806 device_remove_sys_dev_entry(dev);
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001807 device_remove_file(dev, &dev_attr_dev);
Dan Williamse105b8b2008-04-21 10:51:07 -07001808 }
Kay Sieversb9d9c822006-06-15 15:31:56 +02001809 if (dev->class) {
Kay Sieversda231fd2007-11-21 17:29:15 +01001810 device_remove_class_symlinks(dev);
Kay Sievers99ef3ef2006-09-14 11:23:28 +02001811
Kay Sieversca22e562011-12-14 14:29:38 -08001812 mutex_lock(&dev->class->p->mutex);
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001813 /* notify any interfaces that the device is now gone */
Greg Kroah-Hartman184f1f72008-05-28 09:28:39 -07001814 list_for_each_entry(class_intf,
Kay Sieversca22e562011-12-14 14:29:38 -08001815 &dev->class->p->interfaces, node)
Greg Kroah-Hartmanc47ed212006-09-13 15:34:05 +02001816 if (class_intf->remove_dev)
1817 class_intf->remove_dev(dev, class_intf);
1818 /* remove the device from the class list */
Tejun Heo5a3ceb82008-08-25 19:50:19 +02001819 klist_del(&dev->knode_class);
Kay Sieversca22e562011-12-14 14:29:38 -08001820 mutex_unlock(&dev->class->p->mutex);
Kay Sieversb9d9c822006-06-15 15:31:56 +02001821 }
Greg Kroah-Hartmanc5e064a2013-08-23 17:07:26 -07001822 device_remove_file(dev, &dev_attr_uevent);
Greg Kroah-Hartman2620efe2006-06-28 16:19:58 -07001823 device_remove_attrs(dev);
Benjamin Herrenschmidt28953532006-11-08 19:46:14 -08001824 bus_remove_device(dev);
LongX Zhang4b6d1f122012-10-25 00:21:28 +02001825 device_pm_remove(dev);
Grant Likelyd1c34142012-03-05 08:47:41 -07001826 driver_deferred_probe_del(dev);
Lukas Wunner478573c2016-07-28 02:25:41 +02001827 device_remove_properties(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 /* Notify the platform of the removal, in case they
1830 * need to do anything...
1831 */
1832 if (platform_notify_remove)
1833 platform_notify_remove(dev);
Joerg Roedel599bad32014-09-30 13:02:02 +02001834 if (dev->bus)
1835 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1836 BUS_NOTIFY_REMOVED_DEVICE, dev);
Kay Sievers312c0042005-11-16 09:00:00 +01001837 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
Ming Leicebf8fd2016-07-10 19:27:36 +08001838 glue_dir = get_glue_dir(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 kobject_del(&dev->kobj);
Ming Leicebf8fd2016-07-10 19:27:36 +08001840 cleanup_glue_dir(dev, glue_dir);
Kay Sieversda231fd2007-11-21 17:29:15 +01001841 put_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
David Graham White86df2682013-07-21 20:41:14 -04001843EXPORT_SYMBOL_GPL(device_del);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001846 * device_unregister - unregister device from system.
1847 * @dev: device going away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001849 * We do this in two parts, like we do device_register(). First,
1850 * we remove it from all the subsystems with device_del(), then
1851 * we decrement the reference count via put_device(). If that
1852 * is the final reference count, the device will be cleaned up
1853 * via device_release() above. Otherwise, the structure will
1854 * stick around until the final reference to the device is dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001856void device_unregister(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01001858 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 device_del(dev);
1860 put_device(dev);
1861}
David Graham White86df2682013-07-21 20:41:14 -04001862EXPORT_SYMBOL_GPL(device_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03001864static struct device *prev_device(struct klist_iter *i)
1865{
1866 struct klist_node *n = klist_prev(i);
1867 struct device *dev = NULL;
1868 struct device_private *p;
1869
1870 if (n) {
1871 p = to_device_private_parent(n);
1872 dev = p->device;
1873 }
1874 return dev;
1875}
1876
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001877static struct device *next_device(struct klist_iter *i)
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001878{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001879 struct klist_node *n = klist_next(i);
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001880 struct device *dev = NULL;
1881 struct device_private *p;
1882
1883 if (n) {
1884 p = to_device_private_parent(n);
1885 dev = p->device;
1886 }
1887 return dev;
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001888}
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890/**
Kay Sieverse454cea2009-09-18 23:01:12 +02001891 * device_get_devnode - path of device node file
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001892 * @dev: device
Kay Sieverse454cea2009-09-18 23:01:12 +02001893 * @mode: returned file access mode
Kay Sievers3c2670e2013-04-06 09:56:00 -07001894 * @uid: returned file owner
1895 * @gid: returned file group
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001896 * @tmp: possibly allocated string
1897 *
1898 * Return the relative path of a possible device node.
1899 * Non-default names may need to allocate a memory to compose
1900 * a name. This memory is returned in tmp and needs to be
1901 * freed by the caller.
1902 */
Kay Sieverse454cea2009-09-18 23:01:12 +02001903const char *device_get_devnode(struct device *dev,
Greg Kroah-Hartman4e4098a2013-04-11 11:43:29 -07001904 umode_t *mode, kuid_t *uid, kgid_t *gid,
Kay Sievers3c2670e2013-04-06 09:56:00 -07001905 const char **tmp)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001906{
1907 char *s;
1908
1909 *tmp = NULL;
1910
1911 /* the device type may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001912 if (dev->type && dev->type->devnode)
Kay Sievers3c2670e2013-04-06 09:56:00 -07001913 *tmp = dev->type->devnode(dev, mode, uid, gid);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001914 if (*tmp)
1915 return *tmp;
1916
1917 /* the class may provide a specific name */
Kay Sieverse454cea2009-09-18 23:01:12 +02001918 if (dev->class && dev->class->devnode)
1919 *tmp = dev->class->devnode(dev, mode);
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001920 if (*tmp)
1921 return *tmp;
1922
1923 /* return name without allocation, tmp == NULL */
1924 if (strchr(dev_name(dev), '!') == NULL)
1925 return dev_name(dev);
1926
1927 /* replace '!' in the name with '/' */
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07001928 s = kstrdup(dev_name(dev), GFP_KERNEL);
1929 if (!s)
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001930 return NULL;
Rasmus Villemoesa29fd612015-06-25 15:02:33 -07001931 strreplace(s, '!', '/');
1932 return *tmp = s;
Kay Sievers6fcf53a2009-04-30 15:23:42 +02001933}
1934
1935/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001936 * device_for_each_child - device child iterator.
1937 * @parent: parent struct device.
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001938 * @fn: function to be called for each device.
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001939 * @data: data for the callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001941 * Iterate over @parent's child devices, and call @fn for each,
1942 * passing it @data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001944 * We check the return of @fn each time. If it returns anything
1945 * other than 0, we break out and return that value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001947int device_for_each_child(struct device *parent, void *data,
1948 int (*fn)(struct device *dev, void *data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001950 struct klist_iter i;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08001951 struct device *child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int error = 0;
1953
Greg Kroah-Hartman014c90db2009-04-15 16:00:12 -07001954 if (!parent->p)
1955 return 0;
1956
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08001957 klist_iter_init(&parent->p->klist_children, &i);
mochel@digitalimplant.org36239572005-03-24 19:08:30 -08001958 while ((child = next_device(&i)) && !error)
1959 error = fn(child, data);
1960 klist_iter_exit(&i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return error;
1962}
David Graham White86df2682013-07-21 20:41:14 -04001963EXPORT_SYMBOL_GPL(device_for_each_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Cornelia Huck5ab69982006-11-16 15:42:07 +01001965/**
Andy Shevchenko3d060ae2015-07-27 18:04:00 +03001966 * device_for_each_child_reverse - device child iterator in reversed order.
1967 * @parent: parent struct device.
1968 * @fn: function to be called for each device.
1969 * @data: data for the callback.
1970 *
1971 * Iterate over @parent's child devices, and call @fn for each,
1972 * passing it @data.
1973 *
1974 * We check the return of @fn each time. If it returns anything
1975 * other than 0, we break out and return that value.
1976 */
1977int device_for_each_child_reverse(struct device *parent, void *data,
1978 int (*fn)(struct device *dev, void *data))
1979{
1980 struct klist_iter i;
1981 struct device *child;
1982 int error = 0;
1983
1984 if (!parent->p)
1985 return 0;
1986
1987 klist_iter_init(&parent->p->klist_children, &i);
1988 while ((child = prev_device(&i)) && !error)
1989 error = fn(child, data);
1990 klist_iter_exit(&i);
1991 return error;
1992}
1993EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
1994
1995/**
Cornelia Huck5ab69982006-11-16 15:42:07 +01001996 * device_find_child - device iterator for locating a particular device.
1997 * @parent: parent struct device
Cornelia Huck5ab69982006-11-16 15:42:07 +01001998 * @match: Callback function to check device
Robert P. J. Dayf8878dc2013-06-01 20:17:34 -04001999 * @data: Data to pass to match function
Cornelia Huck5ab69982006-11-16 15:42:07 +01002000 *
2001 * This is similar to the device_for_each_child() function above, but it
2002 * returns a reference to a device that is 'found' for later use, as
2003 * determined by the @match callback.
2004 *
2005 * The callback should return 0 if the device doesn't match and non-zero
2006 * if it does. If the callback returns non-zero and a reference to the
2007 * current device can be obtained, this function will return to the caller
2008 * and not iterate over any more devices.
Federico Vagaa4e24002013-04-15 11:18:11 +02002009 *
2010 * NOTE: you will need to drop the reference with put_device() after use.
Cornelia Huck5ab69982006-11-16 15:42:07 +01002011 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002012struct device *device_find_child(struct device *parent, void *data,
2013 int (*match)(struct device *dev, void *data))
Cornelia Huck5ab69982006-11-16 15:42:07 +01002014{
2015 struct klist_iter i;
2016 struct device *child;
2017
2018 if (!parent)
2019 return NULL;
2020
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002021 klist_iter_init(&parent->p->klist_children, &i);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002022 while ((child = next_device(&i)))
2023 if (match(child, data) && get_device(child))
2024 break;
2025 klist_iter_exit(&i);
2026 return child;
2027}
David Graham White86df2682013-07-21 20:41:14 -04002028EXPORT_SYMBOL_GPL(device_find_child);
Cornelia Huck5ab69982006-11-16 15:42:07 +01002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030int __init devices_init(void)
2031{
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002032 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
2033 if (!devices_kset)
2034 return -ENOMEM;
Dan Williamse105b8b2008-04-21 10:51:07 -07002035 dev_kobj = kobject_create_and_add("dev", NULL);
2036 if (!dev_kobj)
2037 goto dev_kobj_err;
2038 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
2039 if (!sysfs_dev_block_kobj)
2040 goto block_kobj_err;
2041 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
2042 if (!sysfs_dev_char_kobj)
2043 goto char_kobj_err;
2044
Greg Kroah-Hartman881c6cfd2007-11-01 09:29:06 -06002045 return 0;
Dan Williamse105b8b2008-04-21 10:51:07 -07002046
2047 char_kobj_err:
2048 kobject_put(sysfs_dev_block_kobj);
2049 block_kobj_err:
2050 kobject_put(dev_kobj);
2051 dev_kobj_err:
2052 kset_unregister(devices_kset);
2053 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Rafael J. Wysocki4f3549d2013-05-02 22:15:29 +02002056static int device_check_offline(struct device *dev, void *not_used)
2057{
2058 int ret;
2059
2060 ret = device_for_each_child(dev, NULL, device_check_offline);
2061 if (ret)
2062 return ret;
2063
2064 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
2065}
2066
2067/**
2068 * device_offline - Prepare the device for hot-removal.
2069 * @dev: Device to be put offline.
2070 *
2071 * Execute the device bus type's .offline() callback, if present, to prepare
2072 * the device for a subsequent hot-removal. If that succeeds, the device must
2073 * not be used until either it is removed or its bus type's .online() callback
2074 * is executed.
2075 *
2076 * Call under device_hotplug_lock.
2077 */
2078int device_offline(struct device *dev)
2079{
2080 int ret;
2081
2082 if (dev->offline_disabled)
2083 return -EPERM;
2084
2085 ret = device_for_each_child(dev, NULL, device_check_offline);
2086 if (ret)
2087 return ret;
2088
2089 device_lock(dev);
2090 if (device_supports_offline(dev)) {
2091 if (dev->offline) {
2092 ret = 1;
2093 } else {
2094 ret = dev->bus->offline(dev);
2095 if (!ret) {
2096 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2097 dev->offline = true;
2098 }
2099 }
2100 }
2101 device_unlock(dev);
2102
2103 return ret;
2104}
2105
2106/**
2107 * device_online - Put the device back online after successful device_offline().
2108 * @dev: Device to be put back online.
2109 *
2110 * If device_offline() has been successfully executed for @dev, but the device
2111 * has not been removed subsequently, execute its bus type's .online() callback
2112 * to indicate that the device can be used again.
2113 *
2114 * Call under device_hotplug_lock.
2115 */
2116int device_online(struct device *dev)
2117{
2118 int ret = 0;
2119
2120 device_lock(dev);
2121 if (device_supports_offline(dev)) {
2122 if (dev->offline) {
2123 ret = dev->bus->online(dev);
2124 if (!ret) {
2125 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2126 dev->offline = false;
2127 }
2128 } else {
2129 ret = 1;
2130 }
2131 }
2132 device_unlock(dev);
2133
2134 return ret;
2135}
2136
Karthigan Srinivasan7f100d12011-04-18 16:16:52 -05002137struct root_device {
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002138 struct device dev;
2139 struct module *owner;
2140};
2141
Josh Triplett93058422012-11-18 21:27:55 -08002142static inline struct root_device *to_root_device(struct device *d)
Ferenc Wagner481e2072011-01-07 15:17:47 +01002143{
2144 return container_of(d, struct root_device, dev);
2145}
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002146
2147static void root_device_release(struct device *dev)
2148{
2149 kfree(to_root_device(dev));
2150}
2151
2152/**
2153 * __root_device_register - allocate and register a root device
2154 * @name: root device name
2155 * @owner: owner module of the root device, usually THIS_MODULE
2156 *
2157 * This function allocates a root device and registers it
2158 * using device_register(). In order to free the returned
2159 * device, use root_device_unregister().
2160 *
2161 * Root devices are dummy devices which allow other devices
2162 * to be grouped under /sys/devices. Use this function to
2163 * allocate a root device and then use it as the parent of
2164 * any device which should appear under /sys/devices/{name}
2165 *
2166 * The /sys/devices/{name} directory will also contain a
2167 * 'module' symlink which points to the @owner directory
2168 * in sysfs.
2169 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002170 * Returns &struct device pointer on success, or ERR_PTR() on error.
2171 *
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002172 * Note: You probably want to use root_device_register().
2173 */
2174struct device *__root_device_register(const char *name, struct module *owner)
2175{
2176 struct root_device *root;
2177 int err = -ENOMEM;
2178
2179 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
2180 if (!root)
2181 return ERR_PTR(err);
2182
Greg Kroah-Hartmanacc0e902009-06-02 15:39:55 -07002183 err = dev_set_name(&root->dev, "%s", name);
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002184 if (err) {
2185 kfree(root);
2186 return ERR_PTR(err);
2187 }
2188
2189 root->dev.release = root_device_release;
2190
2191 err = device_register(&root->dev);
2192 if (err) {
2193 put_device(&root->dev);
2194 return ERR_PTR(err);
2195 }
2196
Christoph Egger1d9e8822010-05-17 16:57:58 +02002197#ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002198 if (owner) {
2199 struct module_kobject *mk = &owner->mkobj;
2200
2201 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
2202 if (err) {
2203 device_unregister(&root->dev);
2204 return ERR_PTR(err);
2205 }
2206 root->owner = owner;
2207 }
2208#endif
2209
2210 return &root->dev;
2211}
2212EXPORT_SYMBOL_GPL(__root_device_register);
2213
2214/**
2215 * root_device_unregister - unregister and free a root device
Randy Dunlap7cbcf222009-01-20 16:29:13 -08002216 * @dev: device going away
Mark McLoughlin0aa0dc42008-12-15 12:58:26 +00002217 *
2218 * This function unregisters and cleans up a device that was created by
2219 * root_device_register().
2220 */
2221void root_device_unregister(struct device *dev)
2222{
2223 struct root_device *root = to_root_device(dev);
2224
2225 if (root->owner)
2226 sysfs_remove_link(&root->dev.kobj, "module");
2227
2228 device_unregister(dev);
2229}
2230EXPORT_SYMBOL_GPL(root_device_unregister);
2231
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002232
2233static void device_create_release(struct device *dev)
2234{
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002235 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002236 kfree(dev);
2237}
2238
Guenter Roeck39ef3112013-07-14 16:05:57 -07002239static struct device *
2240device_create_groups_vargs(struct class *class, struct device *parent,
2241 dev_t devt, void *drvdata,
2242 const struct attribute_group **groups,
2243 const char *fmt, va_list args)
2244{
2245 struct device *dev = NULL;
2246 int retval = -ENODEV;
2247
2248 if (class == NULL || IS_ERR(class))
2249 goto error;
2250
2251 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2252 if (!dev) {
2253 retval = -ENOMEM;
2254 goto error;
2255 }
2256
David Herrmannbbc780f2013-11-21 20:15:48 +01002257 device_initialize(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002258 dev->devt = devt;
2259 dev->class = class;
2260 dev->parent = parent;
2261 dev->groups = groups;
2262 dev->release = device_create_release;
2263 dev_set_drvdata(dev, drvdata);
2264
2265 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
2266 if (retval)
2267 goto error;
2268
David Herrmannbbc780f2013-11-21 20:15:48 +01002269 retval = device_add(dev);
Guenter Roeck39ef3112013-07-14 16:05:57 -07002270 if (retval)
2271 goto error;
2272
2273 return dev;
2274
2275error:
2276 put_device(dev);
2277 return ERR_PTR(retval);
2278}
2279
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002280/**
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002281 * device_create_vargs - creates a device and registers it with sysfs
2282 * @class: pointer to the struct class that this device should be registered to
2283 * @parent: pointer to the parent struct device of this new device, if any
2284 * @devt: the dev_t for the char device to be added
2285 * @drvdata: the data to be added to the device for callbacks
2286 * @fmt: string for the device's name
2287 * @args: va_list for the device's name
2288 *
2289 * This function can be used by char device classes. A struct device
2290 * will be created in sysfs, registered to the specified class.
2291 *
2292 * A "dev" file will be created, showing the dev_t for the device, if
2293 * the dev_t is not 0,0.
2294 * If a pointer to a parent struct device is passed in, the newly created
2295 * struct device will be a child of that device in sysfs.
2296 * The pointer to the struct device will be returned from the call.
2297 * Any further sysfs files that might be required can be created using this
2298 * pointer.
2299 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002300 * Returns &struct device pointer on success, or ERR_PTR() on error.
2301 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002302 * Note: the struct class passed to this function must have previously
2303 * been created with a call to class_create().
2304 */
2305struct device *device_create_vargs(struct class *class, struct device *parent,
2306 dev_t devt, void *drvdata, const char *fmt,
2307 va_list args)
2308{
Guenter Roeck39ef3112013-07-14 16:05:57 -07002309 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
2310 fmt, args);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002311}
2312EXPORT_SYMBOL_GPL(device_create_vargs);
2313
2314/**
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002315 * device_create - creates a device and registers it with sysfs
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002316 * @class: pointer to the struct class that this device should be registered to
2317 * @parent: pointer to the parent struct device of this new device, if any
2318 * @devt: the dev_t for the char device to be added
2319 * @drvdata: the data to be added to the device for callbacks
2320 * @fmt: string for the device's name
2321 *
2322 * This function can be used by char device classes. A struct device
2323 * will be created in sysfs, registered to the specified class.
2324 *
2325 * A "dev" file will be created, showing the dev_t for the device, if
2326 * the dev_t is not 0,0.
2327 * If a pointer to a parent struct device is passed in, the newly created
2328 * struct device will be a child of that device in sysfs.
2329 * The pointer to the struct device will be returned from the call.
2330 * Any further sysfs files that might be required can be created using this
2331 * pointer.
2332 *
Jani Nikulaf0eae0e2010-03-11 18:11:45 +02002333 * Returns &struct device pointer on success, or ERR_PTR() on error.
2334 *
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002335 * Note: the struct class passed to this function must have previously
2336 * been created with a call to class_create().
2337 */
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002338struct device *device_create(struct class *class, struct device *parent,
2339 dev_t devt, void *drvdata, const char *fmt, ...)
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002340{
2341 va_list vargs;
2342 struct device *dev;
2343
2344 va_start(vargs, fmt);
2345 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
2346 va_end(vargs);
2347 return dev;
2348}
Greg Kroah-Hartman4e106732008-07-21 20:03:34 -07002349EXPORT_SYMBOL_GPL(device_create);
Greg Kroah-Hartman8882b392008-05-15 13:44:08 -07002350
Guenter Roeck39ef3112013-07-14 16:05:57 -07002351/**
2352 * device_create_with_groups - creates a device and registers it with sysfs
2353 * @class: pointer to the struct class that this device should be registered to
2354 * @parent: pointer to the parent struct device of this new device, if any
2355 * @devt: the dev_t for the char device to be added
2356 * @drvdata: the data to be added to the device for callbacks
2357 * @groups: NULL-terminated list of attribute groups to be created
2358 * @fmt: string for the device's name
2359 *
2360 * This function can be used by char device classes. A struct device
2361 * will be created in sysfs, registered to the specified class.
2362 * Additional attributes specified in the groups parameter will also
2363 * be created automatically.
2364 *
2365 * A "dev" file will be created, showing the dev_t for the device, if
2366 * the dev_t is not 0,0.
2367 * If a pointer to a parent struct device is passed in, the newly created
2368 * struct device will be a child of that device in sysfs.
2369 * The pointer to the struct device will be returned from the call.
2370 * Any further sysfs files that might be required can be created using this
2371 * pointer.
2372 *
2373 * Returns &struct device pointer on success, or ERR_PTR() on error.
2374 *
2375 * Note: the struct class passed to this function must have previously
2376 * been created with a call to class_create().
2377 */
2378struct device *device_create_with_groups(struct class *class,
2379 struct device *parent, dev_t devt,
2380 void *drvdata,
2381 const struct attribute_group **groups,
2382 const char *fmt, ...)
2383{
2384 va_list vargs;
2385 struct device *dev;
2386
2387 va_start(vargs, fmt);
2388 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
2389 fmt, vargs);
2390 va_end(vargs);
2391 return dev;
2392}
2393EXPORT_SYMBOL_GPL(device_create_with_groups);
2394
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002395static int __match_devt(struct device *dev, const void *data)
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002396{
Michał Mirosław9f3b7952013-02-01 20:40:17 +01002397 const dev_t *devt = data;
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002398
Dave Youngcd354492008-01-28 16:56:11 +08002399 return dev->devt == *devt;
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002400}
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002401
Rafael J. Wysocki775b64d2008-01-12 20:40:46 +01002402/**
2403 * device_destroy - removes a device that was created with device_create()
2404 * @class: pointer to the struct class that this device was registered with
2405 * @devt: the dev_t of the device that was previously registered
2406 *
2407 * This call unregisters and cleans up a device that was created with a
2408 * call to device_create().
2409 */
2410void device_destroy(struct class *class, dev_t devt)
2411{
2412 struct device *dev;
2413
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -04002414 dev = class_find_device(class, NULL, &devt, __match_devt);
Dave Youngcd354492008-01-28 16:56:11 +08002415 if (dev) {
2416 put_device(dev);
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002417 device_unregister(dev);
Dave Youngcd354492008-01-28 16:56:11 +08002418 }
Greg Kroah-Hartman23681e42006-06-14 12:14:34 -07002419}
2420EXPORT_SYMBOL_GPL(device_destroy);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002421
2422/**
2423 * device_rename - renames a device
2424 * @dev: the pointer to the struct device to be renamed
2425 * @new_name: the new name of the device
Eric W. Biederman030c1d22008-05-08 14:41:00 -07002426 *
2427 * It is the responsibility of the caller to provide mutual
2428 * exclusion between two different calls of device_rename
2429 * on the same device to ensure that new_name is valid and
2430 * won't conflict with other devices.
Michael Ellermanc6c0ac62010-11-25 09:44:07 +11002431 *
Timur Tabia5462512010-12-13 14:08:52 -06002432 * Note: Don't call this function. Currently, the networking layer calls this
2433 * function, but that will change. The following text from Kay Sievers offers
2434 * some insight:
2435 *
2436 * Renaming devices is racy at many levels, symlinks and other stuff are not
2437 * replaced atomically, and you get a "move" uevent, but it's not easy to
2438 * connect the event to the old and new device. Device nodes are not renamed at
2439 * all, there isn't even support for that in the kernel now.
2440 *
2441 * In the meantime, during renaming, your target name might be taken by another
2442 * driver, creating conflicts. Or the old name is taken directly after you
2443 * renamed it -- then you get events for the same DEVPATH, before you even see
2444 * the "move" event. It's just a mess, and nothing new should ever rely on
2445 * kernel device renaming. Besides that, it's not even implemented now for
2446 * other things than (driver-core wise very simple) network devices.
2447 *
2448 * We are currently about to change network renaming in udev to completely
2449 * disallow renaming of devices in the same namespace as the kernel uses,
2450 * because we can't solve the problems properly, that arise with swapping names
2451 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
2452 * be allowed to some other name than eth[0-9]*, for the aforementioned
2453 * reasons.
2454 *
2455 * Make up a "real" name in the driver before you register anything, or add
2456 * some other attributes for userspace to find the device, or use udev to add
2457 * symlinks -- but never rename kernel devices later, it's a complete mess. We
2458 * don't even want to get into that and try to implement the missing pieces in
2459 * the core. We really have other pieces to fix in the driver core mess. :)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002460 */
Johannes Berg6937e8f2010-08-05 17:38:18 +02002461int device_rename(struct device *dev, const char *new_name)
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002462{
Tejun Heo4b30ee52013-09-11 22:29:06 -04002463 struct kobject *kobj = &dev->kobj;
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002464 char *old_device_name = NULL;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002465 int error;
2466
2467 dev = get_device(dev);
2468 if (!dev)
2469 return -EINVAL;
2470
ethan.zhao69df7532013-10-13 22:12:35 +08002471 dev_dbg(dev, "renaming to %s\n", new_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002472
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002473 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002474 if (!old_device_name) {
2475 error = -ENOMEM;
2476 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002477 }
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002478
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002479 if (dev->class) {
Tejun Heo4b30ee52013-09-11 22:29:06 -04002480 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2481 kobj, old_device_name,
2482 new_name, kobject_namespace(kobj));
Eric W. Biedermanf349cf32010-03-30 11:31:29 -07002483 if (error)
2484 goto out;
2485 }
Kay Sievers39aba962010-09-04 22:33:14 -07002486
Tejun Heo4b30ee52013-09-11 22:29:06 -04002487 error = kobject_rename(kobj, new_name);
Kay Sievers1fa5ae82009-01-25 15:17:37 +01002488 if (error)
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002489 goto out;
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002490
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002491out:
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002492 put_device(dev);
2493
Cornelia Huck2ee97ca2007-07-18 01:43:47 -07002494 kfree(old_device_name);
Greg Kroah-Hartmana2de48c2006-07-03 14:31:12 -07002495
2496 return error;
2497}
Johannes Berga2807db2007-02-28 12:38:31 +01002498EXPORT_SYMBOL_GPL(device_rename);
Cornelia Huck8a824722006-11-20 17:07:51 +01002499
2500static int device_move_class_links(struct device *dev,
2501 struct device *old_parent,
2502 struct device *new_parent)
2503{
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002504 int error = 0;
Cornelia Huck8a824722006-11-20 17:07:51 +01002505
Greg Kroah-Hartmanf7f34612007-03-06 12:55:53 -08002506 if (old_parent)
2507 sysfs_remove_link(&dev->kobj, "device");
2508 if (new_parent)
2509 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2510 "device");
2511 return error;
Cornelia Huck8a824722006-11-20 17:07:51 +01002512}
2513
2514/**
2515 * device_move - moves a device to a new parent
2516 * @dev: the pointer to the struct device to be moved
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002517 * @new_parent: the new parent of the device (can by NULL)
Cornelia Huckffa6a702009-03-04 12:44:00 +01002518 * @dpm_order: how to reorder the dpm_list
Cornelia Huck8a824722006-11-20 17:07:51 +01002519 */
Cornelia Huckffa6a702009-03-04 12:44:00 +01002520int device_move(struct device *dev, struct device *new_parent,
2521 enum dpm_order dpm_order)
Cornelia Huck8a824722006-11-20 17:07:51 +01002522{
2523 int error;
2524 struct device *old_parent;
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002525 struct kobject *new_parent_kobj;
Cornelia Huck8a824722006-11-20 17:07:51 +01002526
2527 dev = get_device(dev);
2528 if (!dev)
2529 return -EINVAL;
2530
Cornelia Huckffa6a702009-03-04 12:44:00 +01002531 device_pm_lock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002532 new_parent = get_device(new_parent);
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002533 new_parent_kobj = get_device_parent(dev, new_parent);
Cornelia Huck63b69712008-01-21 16:09:44 +01002534
Kay Sievers1e0b2cf2008-10-30 01:36:48 +01002535 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2536 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
Cornelia Huckc744aeae2007-01-08 20:16:44 +01002537 error = kobject_move(&dev->kobj, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002538 if (error) {
Cornelia Huck63b69712008-01-21 16:09:44 +01002539 cleanup_glue_dir(dev, new_parent_kobj);
Cornelia Huck8a824722006-11-20 17:07:51 +01002540 put_device(new_parent);
2541 goto out;
2542 }
2543 old_parent = dev->parent;
2544 dev->parent = new_parent;
2545 if (old_parent)
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002546 klist_remove(&dev->p->knode_parent);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002547 if (new_parent) {
Greg Kroah-Hartmanf791b8c2008-12-16 12:24:56 -08002548 klist_add_tail(&dev->p->knode_parent,
2549 &new_parent->p->klist_children);
Yinghai Lu0d358f22008-02-19 03:20:41 -08002550 set_dev_node(dev, dev_to_node(new_parent));
2551 }
2552
Rabin Vincentbdd40342012-04-23 09:16:36 +02002553 if (dev->class) {
2554 error = device_move_class_links(dev, old_parent, new_parent);
2555 if (error) {
2556 /* We ignore errors on cleanup since we're hosed anyway... */
2557 device_move_class_links(dev, new_parent, old_parent);
2558 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2559 if (new_parent)
2560 klist_remove(&dev->p->knode_parent);
2561 dev->parent = old_parent;
2562 if (old_parent) {
2563 klist_add_tail(&dev->p->knode_parent,
2564 &old_parent->p->klist_children);
2565 set_dev_node(dev, dev_to_node(old_parent));
2566 }
Yinghai Lu0d358f22008-02-19 03:20:41 -08002567 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002568 cleanup_glue_dir(dev, new_parent_kobj);
2569 put_device(new_parent);
2570 goto out;
Cornelia Huck8a824722006-11-20 17:07:51 +01002571 }
Cornelia Huck8a824722006-11-20 17:07:51 +01002572 }
Cornelia Huckffa6a702009-03-04 12:44:00 +01002573 switch (dpm_order) {
2574 case DPM_ORDER_NONE:
2575 break;
2576 case DPM_ORDER_DEV_AFTER_PARENT:
2577 device_pm_move_after(dev, new_parent);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002578 devices_kset_move_after(dev, new_parent);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002579 break;
2580 case DPM_ORDER_PARENT_BEFORE_DEV:
2581 device_pm_move_before(new_parent, dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002582 devices_kset_move_before(new_parent, dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002583 break;
2584 case DPM_ORDER_DEV_LAST:
2585 device_pm_move_last(dev);
Grygorii Strashko52cdbdd2015-07-27 20:43:01 +03002586 devices_kset_move_last(dev);
Cornelia Huckffa6a702009-03-04 12:44:00 +01002587 break;
2588 }
Rabin Vincentbdd40342012-04-23 09:16:36 +02002589
Cornelia Huck8a824722006-11-20 17:07:51 +01002590 put_device(old_parent);
2591out:
Cornelia Huckffa6a702009-03-04 12:44:00 +01002592 device_pm_unlock();
Cornelia Huck8a824722006-11-20 17:07:51 +01002593 put_device(dev);
2594 return error;
2595}
Cornelia Huck8a824722006-11-20 17:07:51 +01002596EXPORT_SYMBOL_GPL(device_move);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002597
2598/**
2599 * device_shutdown - call ->shutdown() on each device to shutdown.
2600 */
2601void device_shutdown(void)
2602{
Benson Leungf123db82013-09-24 20:05:11 -07002603 struct device *dev, *parent;
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002604
Hugh Daschbach62458382010-03-22 10:36:37 -07002605 spin_lock(&devices_kset->list_lock);
2606 /*
2607 * Walk the devices list backward, shutting down each in turn.
2608 * Beware that device unplug events may also start pulling
2609 * devices offline, even as the system is shutting down.
2610 */
2611 while (!list_empty(&devices_kset->list)) {
2612 dev = list_entry(devices_kset->list.prev, struct device,
2613 kobj.entry);
Ming Leid1c6c032012-06-22 18:01:40 +08002614
2615 /*
2616 * hold reference count of device's parent to
2617 * prevent it from being freed because parent's
2618 * lock is to be held
2619 */
Benson Leungf123db82013-09-24 20:05:11 -07002620 parent = get_device(dev->parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002621 get_device(dev);
2622 /*
2623 * Make sure the device is off the kset list, in the
2624 * event that dev->*->shutdown() doesn't remove it.
2625 */
2626 list_del_init(&dev->kobj.entry);
2627 spin_unlock(&devices_kset->list_lock);
Alan Sternfe6b91f2011-12-06 23:24:52 +01002628
Ming Leid1c6c032012-06-22 18:01:40 +08002629 /* hold lock to avoid race with probe/release */
Benson Leungf123db82013-09-24 20:05:11 -07002630 if (parent)
2631 device_lock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002632 device_lock(dev);
2633
Alan Sternfe6b91f2011-12-06 23:24:52 +01002634 /* Don't allow any more runtime suspends */
2635 pm_runtime_get_noresume(dev);
2636 pm_runtime_barrier(dev);
Hugh Daschbach62458382010-03-22 10:36:37 -07002637
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002638 if (dev->bus && dev->bus->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002639 if (initcall_debug)
2640 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002641 dev->bus->shutdown(dev);
2642 } else if (dev->driver && dev->driver->shutdown) {
ShuoX Liu0246c4f2012-11-23 15:14:12 +08002643 if (initcall_debug)
2644 dev_info(dev, "shutdown\n");
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002645 dev->driver->shutdown(dev);
2646 }
Ming Leid1c6c032012-06-22 18:01:40 +08002647
2648 device_unlock(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002649 if (parent)
2650 device_unlock(parent);
Ming Leid1c6c032012-06-22 18:01:40 +08002651
Hugh Daschbach62458382010-03-22 10:36:37 -07002652 put_device(dev);
Benson Leungf123db82013-09-24 20:05:11 -07002653 put_device(parent);
Hugh Daschbach62458382010-03-22 10:36:37 -07002654
2655 spin_lock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002656 }
Hugh Daschbach62458382010-03-22 10:36:37 -07002657 spin_unlock(&devices_kset->list_lock);
Greg Kroah-Hartman37b0c022007-11-26 22:11:55 -08002658}
Joe Perches99bcf212010-06-27 01:02:34 +00002659
2660/*
2661 * Device logging functions
2662 */
2663
2664#ifdef CONFIG_PRINTK
Joe Perches666f3552012-09-12 20:14:11 -07002665static int
2666create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
Joe Perches99bcf212010-06-27 01:02:34 +00002667{
Kay Sieversc4e00da2012-05-03 02:29:59 +02002668 const char *subsys;
Joe Perches798efc62012-09-12 20:11:29 -07002669 size_t pos = 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002670
Kay Sieversc4e00da2012-05-03 02:29:59 +02002671 if (dev->class)
2672 subsys = dev->class->name;
2673 else if (dev->bus)
2674 subsys = dev->bus->name;
2675 else
Joe Perches798efc62012-09-12 20:11:29 -07002676 return 0;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002677
Joe Perches798efc62012-09-12 20:11:29 -07002678 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
Ben Hutchings655e5b72014-08-26 00:34:44 -07002679 if (pos >= hdrlen)
2680 goto overflow;
Kay Sieversc4e00da2012-05-03 02:29:59 +02002681
2682 /*
2683 * Add device identifier DEVICE=:
2684 * b12:8 block dev_t
2685 * c127:3 char dev_t
2686 * n8 netdev ifindex
2687 * +sound:card0 subsystem:devname
2688 */
2689 if (MAJOR(dev->devt)) {
2690 char c;
2691
2692 if (strcmp(subsys, "block") == 0)
2693 c = 'b';
2694 else
2695 c = 'c';
Joe Perches798efc62012-09-12 20:11:29 -07002696 pos++;
2697 pos += snprintf(hdr + pos, hdrlen - pos,
2698 "DEVICE=%c%u:%u",
2699 c, MAJOR(dev->devt), MINOR(dev->devt));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002700 } else if (strcmp(subsys, "net") == 0) {
2701 struct net_device *net = to_net_dev(dev);
2702
Joe Perches798efc62012-09-12 20:11:29 -07002703 pos++;
2704 pos += snprintf(hdr + pos, hdrlen - pos,
2705 "DEVICE=n%u", net->ifindex);
Kay Sieversc4e00da2012-05-03 02:29:59 +02002706 } else {
Joe Perches798efc62012-09-12 20:11:29 -07002707 pos++;
2708 pos += snprintf(hdr + pos, hdrlen - pos,
2709 "DEVICE=+%s:%s", subsys, dev_name(dev));
Kay Sieversc4e00da2012-05-03 02:29:59 +02002710 }
Jim Cromieaf7f2152012-07-19 13:46:21 -06002711
Ben Hutchings655e5b72014-08-26 00:34:44 -07002712 if (pos >= hdrlen)
2713 goto overflow;
2714
Joe Perches798efc62012-09-12 20:11:29 -07002715 return pos;
Ben Hutchings655e5b72014-08-26 00:34:44 -07002716
2717overflow:
2718 dev_WARN(dev, "device/subsystem name too long");
2719 return 0;
Joe Perches99bcf212010-06-27 01:02:34 +00002720}
Joe Perches798efc62012-09-12 20:11:29 -07002721
Joe Perches05e4e5b2012-09-12 20:13:37 -07002722int dev_vprintk_emit(int level, const struct device *dev,
2723 const char *fmt, va_list args)
2724{
2725 char hdr[128];
2726 size_t hdrlen;
2727
2728 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2729
2730 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2731}
2732EXPORT_SYMBOL(dev_vprintk_emit);
2733
2734int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2735{
2736 va_list args;
2737 int r;
2738
2739 va_start(args, fmt);
2740
2741 r = dev_vprintk_emit(level, dev, fmt, args);
2742
2743 va_end(args);
2744
2745 return r;
2746}
2747EXPORT_SYMBOL(dev_printk_emit);
2748
Joe Perchesd1f10522014-12-25 15:07:04 -08002749static void __dev_printk(const char *level, const struct device *dev,
Joe Perches798efc62012-09-12 20:11:29 -07002750 struct va_format *vaf)
2751{
Joe Perchesd1f10522014-12-25 15:07:04 -08002752 if (dev)
2753 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2754 dev_driver_string(dev), dev_name(dev), vaf);
2755 else
2756 printk("%s(NULL device *): %pV", level, vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002757}
Joe Perches99bcf212010-06-27 01:02:34 +00002758
Joe Perchesd1f10522014-12-25 15:07:04 -08002759void dev_printk(const char *level, const struct device *dev,
2760 const char *fmt, ...)
Joe Perches99bcf212010-06-27 01:02:34 +00002761{
2762 struct va_format vaf;
2763 va_list args;
Joe Perches99bcf212010-06-27 01:02:34 +00002764
2765 va_start(args, fmt);
2766
2767 vaf.fmt = fmt;
2768 vaf.va = &args;
2769
Joe Perchesd1f10522014-12-25 15:07:04 -08002770 __dev_printk(level, dev, &vaf);
Joe Perches798efc62012-09-12 20:11:29 -07002771
Joe Perches99bcf212010-06-27 01:02:34 +00002772 va_end(args);
Joe Perches99bcf212010-06-27 01:02:34 +00002773}
2774EXPORT_SYMBOL(dev_printk);
2775
2776#define define_dev_printk_level(func, kern_level) \
Joe Perchesd1f10522014-12-25 15:07:04 -08002777void func(const struct device *dev, const char *fmt, ...) \
Joe Perches99bcf212010-06-27 01:02:34 +00002778{ \
2779 struct va_format vaf; \
2780 va_list args; \
Joe Perches99bcf212010-06-27 01:02:34 +00002781 \
2782 va_start(args, fmt); \
2783 \
2784 vaf.fmt = fmt; \
2785 vaf.va = &args; \
2786 \
Joe Perchesd1f10522014-12-25 15:07:04 -08002787 __dev_printk(kern_level, dev, &vaf); \
Joe Perches798efc62012-09-12 20:11:29 -07002788 \
Joe Perches99bcf212010-06-27 01:02:34 +00002789 va_end(args); \
Joe Perches99bcf212010-06-27 01:02:34 +00002790} \
2791EXPORT_SYMBOL(func);
2792
2793define_dev_printk_level(dev_emerg, KERN_EMERG);
2794define_dev_printk_level(dev_alert, KERN_ALERT);
2795define_dev_printk_level(dev_crit, KERN_CRIT);
2796define_dev_printk_level(dev_err, KERN_ERR);
2797define_dev_printk_level(dev_warn, KERN_WARNING);
2798define_dev_printk_level(dev_notice, KERN_NOTICE);
2799define_dev_printk_level(_dev_info, KERN_INFO);
2800
2801#endif
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002802
2803static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2804{
2805 return fwnode && !IS_ERR(fwnode->secondary);
2806}
2807
2808/**
2809 * set_primary_fwnode - Change the primary firmware node of a given device.
2810 * @dev: Device to handle.
2811 * @fwnode: New primary firmware node of the device.
2812 *
2813 * Set the device's firmware node pointer to @fwnode, but if a secondary
2814 * firmware node of the device is present, preserve it.
2815 */
2816void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2817{
2818 if (fwnode) {
2819 struct fwnode_handle *fn = dev->fwnode;
2820
2821 if (fwnode_is_primary(fn))
2822 fn = fn->secondary;
2823
Mika Westerberg55f89a82015-11-30 17:11:39 +02002824 if (fn) {
2825 WARN_ON(fwnode->secondary);
2826 fwnode->secondary = fn;
2827 }
Rafael J. Wysocki97badf82015-04-03 23:23:37 +02002828 dev->fwnode = fwnode;
2829 } else {
2830 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2831 dev->fwnode->secondary : NULL;
2832 }
2833}
2834EXPORT_SYMBOL_GPL(set_primary_fwnode);
2835
2836/**
2837 * set_secondary_fwnode - Change the secondary firmware node of a given device.
2838 * @dev: Device to handle.
2839 * @fwnode: New secondary firmware node of the device.
2840 *
2841 * If a primary firmware node of the device is present, set its secondary
2842 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
2843 * @fwnode.
2844 */
2845void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2846{
2847 if (fwnode)
2848 fwnode->secondary = ERR_PTR(-ENODEV);
2849
2850 if (fwnode_is_primary(dev->fwnode))
2851 dev->fwnode->secondary = fwnode;
2852 else
2853 dev->fwnode = fwnode;
2854}