]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/base/core.c
sysfs.h: remove attr_name() macro
[karo-tx-linux.git] / drivers / base / core.c
1 /*
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
6  * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7  * Copyright (c) 2006 Novell, Inc.
8  *
9  * This file is released under the GPLv2
10  *
11  */
12
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/kallsyms.h>
25 #include <linux/mutex.h>
26 #include <linux/async.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
29
30 #include "base.h"
31 #include "power/power.h"
32
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated = 1;
36 #else
37 long sysfs_deprecated = 0;
38 #endif
39 static int __init sysfs_deprecated_setup(char *arg)
40 {
41         return kstrtol(arg, 10, &sysfs_deprecated);
42 }
43 early_param("sysfs.deprecated", sysfs_deprecated_setup);
44 #endif
45
46 int (*platform_notify)(struct device *dev) = NULL;
47 int (*platform_notify_remove)(struct device *dev) = NULL;
48 static struct kobject *dev_kobj;
49 struct kobject *sysfs_dev_char_kobj;
50 struct kobject *sysfs_dev_block_kobj;
51
52 #ifdef CONFIG_BLOCK
53 static inline int device_is_not_partition(struct device *dev)
54 {
55         return !(dev->type == &part_type);
56 }
57 #else
58 static inline int device_is_not_partition(struct device *dev)
59 {
60         return 1;
61 }
62 #endif
63
64 /**
65  * dev_driver_string - Return a device's driver name, if at all possible
66  * @dev: struct device to get the name of
67  *
68  * Will return the device's driver's name if it is bound to a device.  If
69  * the device is not bound to a driver, it will return the name of the bus
70  * it is attached to.  If it is not attached to a bus either, an empty
71  * string will be returned.
72  */
73 const char *dev_driver_string(const struct device *dev)
74 {
75         struct device_driver *drv;
76
77         /* dev->driver can change to NULL underneath us because of unbinding,
78          * so be careful about accessing it.  dev->bus and dev->class should
79          * never change once they are set, so they don't need special care.
80          */
81         drv = ACCESS_ONCE(dev->driver);
82         return drv ? drv->name :
83                         (dev->bus ? dev->bus->name :
84                         (dev->class ? dev->class->name : ""));
85 }
86 EXPORT_SYMBOL(dev_driver_string);
87
88 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
89
90 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
91                              char *buf)
92 {
93         struct device_attribute *dev_attr = to_dev_attr(attr);
94         struct device *dev = kobj_to_dev(kobj);
95         ssize_t ret = -EIO;
96
97         if (dev_attr->show)
98                 ret = dev_attr->show(dev, dev_attr, buf);
99         if (ret >= (ssize_t)PAGE_SIZE) {
100                 print_symbol("dev_attr_show: %s returned bad count\n",
101                                 (unsigned long)dev_attr->show);
102         }
103         return ret;
104 }
105
106 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
107                               const char *buf, size_t count)
108 {
109         struct device_attribute *dev_attr = to_dev_attr(attr);
110         struct device *dev = kobj_to_dev(kobj);
111         ssize_t ret = -EIO;
112
113         if (dev_attr->store)
114                 ret = dev_attr->store(dev, dev_attr, buf, count);
115         return ret;
116 }
117
118 static const struct sysfs_ops dev_sysfs_ops = {
119         .show   = dev_attr_show,
120         .store  = dev_attr_store,
121 };
122
123 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
124
125 ssize_t device_store_ulong(struct device *dev,
126                            struct device_attribute *attr,
127                            const char *buf, size_t size)
128 {
129         struct dev_ext_attribute *ea = to_ext_attr(attr);
130         char *end;
131         unsigned long new = simple_strtoul(buf, &end, 0);
132         if (end == buf)
133                 return -EINVAL;
134         *(unsigned long *)(ea->var) = new;
135         /* Always return full write size even if we didn't consume all */
136         return size;
137 }
138 EXPORT_SYMBOL_GPL(device_store_ulong);
139
140 ssize_t device_show_ulong(struct device *dev,
141                           struct device_attribute *attr,
142                           char *buf)
143 {
144         struct dev_ext_attribute *ea = to_ext_attr(attr);
145         return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
146 }
147 EXPORT_SYMBOL_GPL(device_show_ulong);
148
149 ssize_t device_store_int(struct device *dev,
150                          struct device_attribute *attr,
151                          const char *buf, size_t size)
152 {
153         struct dev_ext_attribute *ea = to_ext_attr(attr);
154         char *end;
155         long new = simple_strtol(buf, &end, 0);
156         if (end == buf || new > INT_MAX || new < INT_MIN)
157                 return -EINVAL;
158         *(int *)(ea->var) = new;
159         /* Always return full write size even if we didn't consume all */
160         return size;
161 }
162 EXPORT_SYMBOL_GPL(device_store_int);
163
164 ssize_t device_show_int(struct device *dev,
165                         struct device_attribute *attr,
166                         char *buf)
167 {
168         struct dev_ext_attribute *ea = to_ext_attr(attr);
169
170         return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
171 }
172 EXPORT_SYMBOL_GPL(device_show_int);
173
174 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
175                           const char *buf, size_t size)
176 {
177         struct dev_ext_attribute *ea = to_ext_attr(attr);
178
179         if (strtobool(buf, ea->var) < 0)
180                 return -EINVAL;
181
182         return size;
183 }
184 EXPORT_SYMBOL_GPL(device_store_bool);
185
186 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
187                          char *buf)
188 {
189         struct dev_ext_attribute *ea = to_ext_attr(attr);
190
191         return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
192 }
193 EXPORT_SYMBOL_GPL(device_show_bool);
194
195 /**
196  * device_release - free device structure.
197  * @kobj: device's kobject.
198  *
199  * This is called once the reference count for the object
200  * reaches 0. We forward the call to the device's release
201  * method, which should handle actually freeing the structure.
202  */
203 static void device_release(struct kobject *kobj)
204 {
205         struct device *dev = kobj_to_dev(kobj);
206         struct device_private *p = dev->p;
207
208         /*
209          * Some platform devices are driven without driver attached
210          * and managed resources may have been acquired.  Make sure
211          * all resources are released.
212          *
213          * Drivers still can add resources into device after device
214          * is deleted but alive, so release devres here to avoid
215          * possible memory leak.
216          */
217         devres_release_all(dev);
218
219         if (dev->release)
220                 dev->release(dev);
221         else if (dev->type && dev->type->release)
222                 dev->type->release(dev);
223         else if (dev->class && dev->class->dev_release)
224                 dev->class->dev_release(dev);
225         else
226                 WARN(1, KERN_ERR "Device '%s' does not have a release() "
227                         "function, it is broken and must be fixed.\n",
228                         dev_name(dev));
229         kfree(p);
230 }
231
232 static const void *device_namespace(struct kobject *kobj)
233 {
234         struct device *dev = kobj_to_dev(kobj);
235         const void *ns = NULL;
236
237         if (dev->class && dev->class->ns_type)
238                 ns = dev->class->namespace(dev);
239
240         return ns;
241 }
242
243 static struct kobj_type device_ktype = {
244         .release        = device_release,
245         .sysfs_ops      = &dev_sysfs_ops,
246         .namespace      = device_namespace,
247 };
248
249
250 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
251 {
252         struct kobj_type *ktype = get_ktype(kobj);
253
254         if (ktype == &device_ktype) {
255                 struct device *dev = kobj_to_dev(kobj);
256                 if (dev->bus)
257                         return 1;
258                 if (dev->class)
259                         return 1;
260         }
261         return 0;
262 }
263
264 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
265 {
266         struct device *dev = kobj_to_dev(kobj);
267
268         if (dev->bus)
269                 return dev->bus->name;
270         if (dev->class)
271                 return dev->class->name;
272         return NULL;
273 }
274
275 static int dev_uevent(struct kset *kset, struct kobject *kobj,
276                       struct kobj_uevent_env *env)
277 {
278         struct device *dev = kobj_to_dev(kobj);
279         int retval = 0;
280
281         /* add device node properties if present */
282         if (MAJOR(dev->devt)) {
283                 const char *tmp;
284                 const char *name;
285                 umode_t mode = 0;
286                 kuid_t uid = GLOBAL_ROOT_UID;
287                 kgid_t gid = GLOBAL_ROOT_GID;
288
289                 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
290                 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
291                 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
292                 if (name) {
293                         add_uevent_var(env, "DEVNAME=%s", name);
294                         if (mode)
295                                 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
296                         if (!uid_eq(uid, GLOBAL_ROOT_UID))
297                                 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
298                         if (!gid_eq(gid, GLOBAL_ROOT_GID))
299                                 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
300                         kfree(tmp);
301                 }
302         }
303
304         if (dev->type && dev->type->name)
305                 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
306
307         if (dev->driver)
308                 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
309
310         /* Add common DT information about the device */
311         of_device_uevent(dev, env);
312
313         /* have the bus specific function add its stuff */
314         if (dev->bus && dev->bus->uevent) {
315                 retval = dev->bus->uevent(dev, env);
316                 if (retval)
317                         pr_debug("device: '%s': %s: bus uevent() returned %d\n",
318                                  dev_name(dev), __func__, retval);
319         }
320
321         /* have the class specific function add its stuff */
322         if (dev->class && dev->class->dev_uevent) {
323                 retval = dev->class->dev_uevent(dev, env);
324                 if (retval)
325                         pr_debug("device: '%s': %s: class uevent() "
326                                  "returned %d\n", dev_name(dev),
327                                  __func__, retval);
328         }
329
330         /* have the device type specific function add its stuff */
331         if (dev->type && dev->type->uevent) {
332                 retval = dev->type->uevent(dev, env);
333                 if (retval)
334                         pr_debug("device: '%s': %s: dev_type uevent() "
335                                  "returned %d\n", dev_name(dev),
336                                  __func__, retval);
337         }
338
339         return retval;
340 }
341
342 static const struct kset_uevent_ops device_uevent_ops = {
343         .filter =       dev_uevent_filter,
344         .name =         dev_uevent_name,
345         .uevent =       dev_uevent,
346 };
347
348 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
349                            char *buf)
350 {
351         struct kobject *top_kobj;
352         struct kset *kset;
353         struct kobj_uevent_env *env = NULL;
354         int i;
355         size_t count = 0;
356         int retval;
357
358         /* search the kset, the device belongs to */
359         top_kobj = &dev->kobj;
360         while (!top_kobj->kset && top_kobj->parent)
361                 top_kobj = top_kobj->parent;
362         if (!top_kobj->kset)
363                 goto out;
364
365         kset = top_kobj->kset;
366         if (!kset->uevent_ops || !kset->uevent_ops->uevent)
367                 goto out;
368
369         /* respect filter */
370         if (kset->uevent_ops && kset->uevent_ops->filter)
371                 if (!kset->uevent_ops->filter(kset, &dev->kobj))
372                         goto out;
373
374         env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
375         if (!env)
376                 return -ENOMEM;
377
378         /* let the kset specific function add its keys */
379         retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
380         if (retval)
381                 goto out;
382
383         /* copy keys to file */
384         for (i = 0; i < env->envp_idx; i++)
385                 count += sprintf(&buf[count], "%s\n", env->envp[i]);
386 out:
387         kfree(env);
388         return count;
389 }
390
391 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
392                             const char *buf, size_t count)
393 {
394         enum kobject_action action;
395
396         if (kobject_action_type(buf, count, &action) == 0)
397                 kobject_uevent(&dev->kobj, action);
398         else
399                 dev_err(dev, "uevent: unknown action-string\n");
400         return count;
401 }
402
403 static struct device_attribute uevent_attr =
404         __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
405
406 static ssize_t show_online(struct device *dev, struct device_attribute *attr,
407                            char *buf)
408 {
409         bool val;
410
411         lock_device_hotplug();
412         val = !dev->offline;
413         unlock_device_hotplug();
414         return sprintf(buf, "%u\n", val);
415 }
416
417 static ssize_t store_online(struct device *dev, struct device_attribute *attr,
418                             const char *buf, size_t count)
419 {
420         bool val;
421         int ret;
422
423         ret = strtobool(buf, &val);
424         if (ret < 0)
425                 return ret;
426
427         lock_device_hotplug();
428         ret = val ? device_online(dev) : device_offline(dev);
429         unlock_device_hotplug();
430         return ret < 0 ? ret : count;
431 }
432
433 static struct device_attribute online_attr =
434         __ATTR(online, S_IRUGO | S_IWUSR, show_online, store_online);
435
436 static int device_add_attributes(struct device *dev,
437                                  struct device_attribute *attrs)
438 {
439         int error = 0;
440         int i;
441
442         if (attrs) {
443                 for (i = 0; attrs[i].attr.name; i++) {
444                         error = device_create_file(dev, &attrs[i]);
445                         if (error)
446                                 break;
447                 }
448                 if (error)
449                         while (--i >= 0)
450                                 device_remove_file(dev, &attrs[i]);
451         }
452         return error;
453 }
454
455 static void device_remove_attributes(struct device *dev,
456                                      struct device_attribute *attrs)
457 {
458         int i;
459
460         if (attrs)
461                 for (i = 0; attrs[i].attr.name; i++)
462                         device_remove_file(dev, &attrs[i]);
463 }
464
465 static int device_add_bin_attributes(struct device *dev,
466                                      struct bin_attribute *attrs)
467 {
468         int error = 0;
469         int i;
470
471         if (attrs) {
472                 for (i = 0; attrs[i].attr.name; i++) {
473                         error = device_create_bin_file(dev, &attrs[i]);
474                         if (error)
475                                 break;
476                 }
477                 if (error)
478                         while (--i >= 0)
479                                 device_remove_bin_file(dev, &attrs[i]);
480         }
481         return error;
482 }
483
484 static void device_remove_bin_attributes(struct device *dev,
485                                          struct bin_attribute *attrs)
486 {
487         int i;
488
489         if (attrs)
490                 for (i = 0; attrs[i].attr.name; i++)
491                         device_remove_bin_file(dev, &attrs[i]);
492 }
493
494 int device_add_groups(struct device *dev, const struct attribute_group **groups)
495 {
496         return sysfs_create_groups(&dev->kobj, groups);
497 }
498
499 void device_remove_groups(struct device *dev,
500                           const struct attribute_group **groups)
501 {
502         sysfs_remove_groups(&dev->kobj, groups);
503 }
504
505 static int device_add_attrs(struct device *dev)
506 {
507         struct class *class = dev->class;
508         const struct device_type *type = dev->type;
509         int error;
510
511         if (class) {
512                 error = device_add_groups(dev, class->dev_groups);
513                 if (error)
514                         return error;
515                 error = device_add_attributes(dev, class->dev_attrs);
516                 if (error)
517                         goto err_remove_class_groups;
518                 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
519                 if (error)
520                         goto err_remove_class_attrs;
521         }
522
523         if (type) {
524                 error = device_add_groups(dev, type->groups);
525                 if (error)
526                         goto err_remove_class_bin_attrs;
527         }
528
529         error = device_add_groups(dev, dev->groups);
530         if (error)
531                 goto err_remove_type_groups;
532
533         if (device_supports_offline(dev) && !dev->offline_disabled) {
534                 error = device_create_file(dev, &online_attr);
535                 if (error)
536                         goto err_remove_type_groups;
537         }
538
539         return 0;
540
541  err_remove_type_groups:
542         if (type)
543                 device_remove_groups(dev, type->groups);
544  err_remove_class_bin_attrs:
545         if (class)
546                 device_remove_bin_attributes(dev, class->dev_bin_attrs);
547  err_remove_class_attrs:
548         if (class)
549                 device_remove_attributes(dev, class->dev_attrs);
550  err_remove_class_groups:
551         if (class)
552                 device_remove_groups(dev, class->dev_groups);
553
554         return error;
555 }
556
557 static void device_remove_attrs(struct device *dev)
558 {
559         struct class *class = dev->class;
560         const struct device_type *type = dev->type;
561
562         device_remove_file(dev, &online_attr);
563         device_remove_groups(dev, dev->groups);
564
565         if (type)
566                 device_remove_groups(dev, type->groups);
567
568         if (class) {
569                 device_remove_attributes(dev, class->dev_attrs);
570                 device_remove_bin_attributes(dev, class->dev_bin_attrs);
571                 device_remove_groups(dev, class->dev_groups);
572         }
573 }
574
575
576 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
577                         char *buf)
578 {
579         return print_dev_t(buf, dev->devt);
580 }
581
582 static struct device_attribute devt_attr =
583         __ATTR(dev, S_IRUGO, show_dev, NULL);
584
585 /* /sys/devices/ */
586 struct kset *devices_kset;
587
588 /**
589  * device_create_file - create sysfs attribute file for device.
590  * @dev: device.
591  * @attr: device attribute descriptor.
592  */
593 int device_create_file(struct device *dev,
594                        const struct device_attribute *attr)
595 {
596         int error = 0;
597
598         if (dev) {
599                 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
600                         "Attribute %s: write permission without 'store'\n",
601                         attr->attr.name);
602                 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
603                         "Attribute %s: read permission without 'show'\n",
604                         attr->attr.name);
605                 error = sysfs_create_file(&dev->kobj, &attr->attr);
606         }
607
608         return error;
609 }
610 EXPORT_SYMBOL_GPL(device_create_file);
611
612 /**
613  * device_remove_file - remove sysfs attribute file.
614  * @dev: device.
615  * @attr: device attribute descriptor.
616  */
617 void device_remove_file(struct device *dev,
618                         const struct device_attribute *attr)
619 {
620         if (dev)
621                 sysfs_remove_file(&dev->kobj, &attr->attr);
622 }
623 EXPORT_SYMBOL_GPL(device_remove_file);
624
625 /**
626  * device_create_bin_file - create sysfs binary attribute file for device.
627  * @dev: device.
628  * @attr: device binary attribute descriptor.
629  */
630 int device_create_bin_file(struct device *dev,
631                            const struct bin_attribute *attr)
632 {
633         int error = -EINVAL;
634         if (dev)
635                 error = sysfs_create_bin_file(&dev->kobj, attr);
636         return error;
637 }
638 EXPORT_SYMBOL_GPL(device_create_bin_file);
639
640 /**
641  * device_remove_bin_file - remove sysfs binary attribute file
642  * @dev: device.
643  * @attr: device binary attribute descriptor.
644  */
645 void device_remove_bin_file(struct device *dev,
646                             const struct bin_attribute *attr)
647 {
648         if (dev)
649                 sysfs_remove_bin_file(&dev->kobj, attr);
650 }
651 EXPORT_SYMBOL_GPL(device_remove_bin_file);
652
653 /**
654  * device_schedule_callback_owner - helper to schedule a callback for a device
655  * @dev: device.
656  * @func: callback function to invoke later.
657  * @owner: module owning the callback routine
658  *
659  * Attribute methods must not unregister themselves or their parent device
660  * (which would amount to the same thing).  Attempts to do so will deadlock,
661  * since unregistration is mutually exclusive with driver callbacks.
662  *
663  * Instead methods can call this routine, which will attempt to allocate
664  * and schedule a workqueue request to call back @func with @dev as its
665  * argument in the workqueue's process context.  @dev will be pinned until
666  * @func returns.
667  *
668  * This routine is usually called via the inline device_schedule_callback(),
669  * which automatically sets @owner to THIS_MODULE.
670  *
671  * Returns 0 if the request was submitted, -ENOMEM if storage could not
672  * be allocated, -ENODEV if a reference to @owner isn't available.
673  *
674  * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
675  * underlying sysfs routine (since it is intended for use by attribute
676  * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
677  */
678 int device_schedule_callback_owner(struct device *dev,
679                 void (*func)(struct device *), struct module *owner)
680 {
681         return sysfs_schedule_callback(&dev->kobj,
682                         (void (*)(void *)) func, dev, owner);
683 }
684 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
685
686 static void klist_children_get(struct klist_node *n)
687 {
688         struct device_private *p = to_device_private_parent(n);
689         struct device *dev = p->device;
690
691         get_device(dev);
692 }
693
694 static void klist_children_put(struct klist_node *n)
695 {
696         struct device_private *p = to_device_private_parent(n);
697         struct device *dev = p->device;
698
699         put_device(dev);
700 }
701
702 /**
703  * device_initialize - init device structure.
704  * @dev: device.
705  *
706  * This prepares the device for use by other layers by initializing
707  * its fields.
708  * It is the first half of device_register(), if called by
709  * that function, though it can also be called separately, so one
710  * may use @dev's fields. In particular, get_device()/put_device()
711  * may be used for reference counting of @dev after calling this
712  * function.
713  *
714  * All fields in @dev must be initialized by the caller to 0, except
715  * for those explicitly set to some other value.  The simplest
716  * approach is to use kzalloc() to allocate the structure containing
717  * @dev.
718  *
719  * NOTE: Use put_device() to give up your reference instead of freeing
720  * @dev directly once you have called this function.
721  */
722 void device_initialize(struct device *dev)
723 {
724         dev->kobj.kset = devices_kset;
725         kobject_init(&dev->kobj, &device_ktype);
726         INIT_LIST_HEAD(&dev->dma_pools);
727         mutex_init(&dev->mutex);
728         lockdep_set_novalidate_class(&dev->mutex);
729         spin_lock_init(&dev->devres_lock);
730         INIT_LIST_HEAD(&dev->devres_head);
731         device_pm_init(dev);
732         set_dev_node(dev, -1);
733 }
734 EXPORT_SYMBOL_GPL(device_initialize);
735
736 struct kobject *virtual_device_parent(struct device *dev)
737 {
738         static struct kobject *virtual_dir = NULL;
739
740         if (!virtual_dir)
741                 virtual_dir = kobject_create_and_add("virtual",
742                                                      &devices_kset->kobj);
743
744         return virtual_dir;
745 }
746
747 struct class_dir {
748         struct kobject kobj;
749         struct class *class;
750 };
751
752 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
753
754 static void class_dir_release(struct kobject *kobj)
755 {
756         struct class_dir *dir = to_class_dir(kobj);
757         kfree(dir);
758 }
759
760 static const
761 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
762 {
763         struct class_dir *dir = to_class_dir(kobj);
764         return dir->class->ns_type;
765 }
766
767 static struct kobj_type class_dir_ktype = {
768         .release        = class_dir_release,
769         .sysfs_ops      = &kobj_sysfs_ops,
770         .child_ns_type  = class_dir_child_ns_type
771 };
772
773 static struct kobject *
774 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
775 {
776         struct class_dir *dir;
777         int retval;
778
779         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
780         if (!dir)
781                 return NULL;
782
783         dir->class = class;
784         kobject_init(&dir->kobj, &class_dir_ktype);
785
786         dir->kobj.kset = &class->p->glue_dirs;
787
788         retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
789         if (retval < 0) {
790                 kobject_put(&dir->kobj);
791                 return NULL;
792         }
793         return &dir->kobj;
794 }
795
796
797 static struct kobject *get_device_parent(struct device *dev,
798                                          struct device *parent)
799 {
800         if (dev->class) {
801                 static DEFINE_MUTEX(gdp_mutex);
802                 struct kobject *kobj = NULL;
803                 struct kobject *parent_kobj;
804                 struct kobject *k;
805
806 #ifdef CONFIG_BLOCK
807                 /* block disks show up in /sys/block */
808                 if (sysfs_deprecated && dev->class == &block_class) {
809                         if (parent && parent->class == &block_class)
810                                 return &parent->kobj;
811                         return &block_class.p->subsys.kobj;
812                 }
813 #endif
814
815                 /*
816                  * If we have no parent, we live in "virtual".
817                  * Class-devices with a non class-device as parent, live
818                  * in a "glue" directory to prevent namespace collisions.
819                  */
820                 if (parent == NULL)
821                         parent_kobj = virtual_device_parent(dev);
822                 else if (parent->class && !dev->class->ns_type)
823                         return &parent->kobj;
824                 else
825                         parent_kobj = &parent->kobj;
826
827                 mutex_lock(&gdp_mutex);
828
829                 /* find our class-directory at the parent and reference it */
830                 spin_lock(&dev->class->p->glue_dirs.list_lock);
831                 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
832                         if (k->parent == parent_kobj) {
833                                 kobj = kobject_get(k);
834                                 break;
835                         }
836                 spin_unlock(&dev->class->p->glue_dirs.list_lock);
837                 if (kobj) {
838                         mutex_unlock(&gdp_mutex);
839                         return kobj;
840                 }
841
842                 /* or create a new class-directory at the parent device */
843                 k = class_dir_create_and_add(dev->class, parent_kobj);
844                 /* do not emit an uevent for this simple "glue" directory */
845                 mutex_unlock(&gdp_mutex);
846                 return k;
847         }
848
849         /* subsystems can specify a default root directory for their devices */
850         if (!parent && dev->bus && dev->bus->dev_root)
851                 return &dev->bus->dev_root->kobj;
852
853         if (parent)
854                 return &parent->kobj;
855         return NULL;
856 }
857
858 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
859 {
860         /* see if we live in a "glue" directory */
861         if (!glue_dir || !dev->class ||
862             glue_dir->kset != &dev->class->p->glue_dirs)
863                 return;
864
865         kobject_put(glue_dir);
866 }
867
868 static void cleanup_device_parent(struct device *dev)
869 {
870         cleanup_glue_dir(dev, dev->kobj.parent);
871 }
872
873 static int device_add_class_symlinks(struct device *dev)
874 {
875         int error;
876
877         if (!dev->class)
878                 return 0;
879
880         error = sysfs_create_link(&dev->kobj,
881                                   &dev->class->p->subsys.kobj,
882                                   "subsystem");
883         if (error)
884                 goto out;
885
886         if (dev->parent && device_is_not_partition(dev)) {
887                 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
888                                           "device");
889                 if (error)
890                         goto out_subsys;
891         }
892
893 #ifdef CONFIG_BLOCK
894         /* /sys/block has directories and does not need symlinks */
895         if (sysfs_deprecated && dev->class == &block_class)
896                 return 0;
897 #endif
898
899         /* link in the class directory pointing to the device */
900         error = sysfs_create_link(&dev->class->p->subsys.kobj,
901                                   &dev->kobj, dev_name(dev));
902         if (error)
903                 goto out_device;
904
905         return 0;
906
907 out_device:
908         sysfs_remove_link(&dev->kobj, "device");
909
910 out_subsys:
911         sysfs_remove_link(&dev->kobj, "subsystem");
912 out:
913         return error;
914 }
915
916 static void device_remove_class_symlinks(struct device *dev)
917 {
918         if (!dev->class)
919                 return;
920
921         if (dev->parent && device_is_not_partition(dev))
922                 sysfs_remove_link(&dev->kobj, "device");
923         sysfs_remove_link(&dev->kobj, "subsystem");
924 #ifdef CONFIG_BLOCK
925         if (sysfs_deprecated && dev->class == &block_class)
926                 return;
927 #endif
928         sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
929 }
930
931 /**
932  * dev_set_name - set a device name
933  * @dev: device
934  * @fmt: format string for the device's name
935  */
936 int dev_set_name(struct device *dev, const char *fmt, ...)
937 {
938         va_list vargs;
939         int err;
940
941         va_start(vargs, fmt);
942         err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
943         va_end(vargs);
944         return err;
945 }
946 EXPORT_SYMBOL_GPL(dev_set_name);
947
948 /**
949  * device_to_dev_kobj - select a /sys/dev/ directory for the device
950  * @dev: device
951  *
952  * By default we select char/ for new entries.  Setting class->dev_obj
953  * to NULL prevents an entry from being created.  class->dev_kobj must
954  * be set (or cleared) before any devices are registered to the class
955  * otherwise device_create_sys_dev_entry() and
956  * device_remove_sys_dev_entry() will disagree about the presence of
957  * the link.
958  */
959 static struct kobject *device_to_dev_kobj(struct device *dev)
960 {
961         struct kobject *kobj;
962
963         if (dev->class)
964                 kobj = dev->class->dev_kobj;
965         else
966                 kobj = sysfs_dev_char_kobj;
967
968         return kobj;
969 }
970
971 static int device_create_sys_dev_entry(struct device *dev)
972 {
973         struct kobject *kobj = device_to_dev_kobj(dev);
974         int error = 0;
975         char devt_str[15];
976
977         if (kobj) {
978                 format_dev_t(devt_str, dev->devt);
979                 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
980         }
981
982         return error;
983 }
984
985 static void device_remove_sys_dev_entry(struct device *dev)
986 {
987         struct kobject *kobj = device_to_dev_kobj(dev);
988         char devt_str[15];
989
990         if (kobj) {
991                 format_dev_t(devt_str, dev->devt);
992                 sysfs_remove_link(kobj, devt_str);
993         }
994 }
995
996 int device_private_init(struct device *dev)
997 {
998         dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
999         if (!dev->p)
1000                 return -ENOMEM;
1001         dev->p->device = dev;
1002         klist_init(&dev->p->klist_children, klist_children_get,
1003                    klist_children_put);
1004         INIT_LIST_HEAD(&dev->p->deferred_probe);
1005         return 0;
1006 }
1007
1008 /**
1009  * device_add - add device to device hierarchy.
1010  * @dev: device.
1011  *
1012  * This is part 2 of device_register(), though may be called
1013  * separately _iff_ device_initialize() has been called separately.
1014  *
1015  * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1016  * to the global and sibling lists for the device, then
1017  * adds it to the other relevant subsystems of the driver model.
1018  *
1019  * Do not call this routine or device_register() more than once for
1020  * any device structure.  The driver model core is not designed to work
1021  * with devices that get unregistered and then spring back to life.
1022  * (Among other things, it's very hard to guarantee that all references
1023  * to the previous incarnation of @dev have been dropped.)  Allocate
1024  * and register a fresh new struct device instead.
1025  *
1026  * NOTE: _Never_ directly free @dev after calling this function, even
1027  * if it returned an error! Always use put_device() to give up your
1028  * reference instead.
1029  */
1030 int device_add(struct device *dev)
1031 {
1032         struct device *parent = NULL;
1033         struct kobject *kobj;
1034         struct class_interface *class_intf;
1035         int error = -EINVAL;
1036
1037         dev = get_device(dev);
1038         if (!dev)
1039                 goto done;
1040
1041         if (!dev->p) {
1042                 error = device_private_init(dev);
1043                 if (error)
1044                         goto done;
1045         }
1046
1047         /*
1048          * for statically allocated devices, which should all be converted
1049          * some day, we need to initialize the name. We prevent reading back
1050          * the name, and force the use of dev_name()
1051          */
1052         if (dev->init_name) {
1053                 dev_set_name(dev, "%s", dev->init_name);
1054                 dev->init_name = NULL;
1055         }
1056
1057         /* subsystems can specify simple device enumeration */
1058         if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1059                 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1060
1061         if (!dev_name(dev)) {
1062                 error = -EINVAL;
1063                 goto name_error;
1064         }
1065
1066         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1067
1068         parent = get_device(dev->parent);
1069         kobj = get_device_parent(dev, parent);
1070         if (kobj)
1071                 dev->kobj.parent = kobj;
1072
1073         /* use parent numa_node */
1074         if (parent)
1075                 set_dev_node(dev, dev_to_node(parent));
1076
1077         /* first, register with generic layer. */
1078         /* we require the name to be set before, and pass NULL */
1079         error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1080         if (error)
1081                 goto Error;
1082
1083         /* notify platform of device entry */
1084         if (platform_notify)
1085                 platform_notify(dev);
1086
1087         error = device_create_file(dev, &uevent_attr);
1088         if (error)
1089                 goto attrError;
1090
1091         if (MAJOR(dev->devt)) {
1092                 error = device_create_file(dev, &devt_attr);
1093                 if (error)
1094                         goto ueventattrError;
1095
1096                 error = device_create_sys_dev_entry(dev);
1097                 if (error)
1098                         goto devtattrError;
1099
1100                 devtmpfs_create_node(dev);
1101         }
1102
1103         error = device_add_class_symlinks(dev);
1104         if (error)
1105                 goto SymlinkError;
1106         error = device_add_attrs(dev);
1107         if (error)
1108                 goto AttrsError;
1109         error = bus_add_device(dev);
1110         if (error)
1111                 goto BusError;
1112         error = dpm_sysfs_add(dev);
1113         if (error)
1114                 goto DPMError;
1115         device_pm_add(dev);
1116
1117         /* Notify clients of device addition.  This call must come
1118          * after dpm_sysfs_add() and before kobject_uevent().
1119          */
1120         if (dev->bus)
1121                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1122                                              BUS_NOTIFY_ADD_DEVICE, dev);
1123
1124         kobject_uevent(&dev->kobj, KOBJ_ADD);
1125         bus_probe_device(dev);
1126         if (parent)
1127                 klist_add_tail(&dev->p->knode_parent,
1128                                &parent->p->klist_children);
1129
1130         if (dev->class) {
1131                 mutex_lock(&dev->class->p->mutex);
1132                 /* tie the class to the device */
1133                 klist_add_tail(&dev->knode_class,
1134                                &dev->class->p->klist_devices);
1135
1136                 /* notify any interfaces that the device is here */
1137                 list_for_each_entry(class_intf,
1138                                     &dev->class->p->interfaces, node)
1139                         if (class_intf->add_dev)
1140                                 class_intf->add_dev(dev, class_intf);
1141                 mutex_unlock(&dev->class->p->mutex);
1142         }
1143 done:
1144         put_device(dev);
1145         return error;
1146  DPMError:
1147         bus_remove_device(dev);
1148  BusError:
1149         device_remove_attrs(dev);
1150  AttrsError:
1151         device_remove_class_symlinks(dev);
1152  SymlinkError:
1153         if (MAJOR(dev->devt))
1154                 devtmpfs_delete_node(dev);
1155         if (MAJOR(dev->devt))
1156                 device_remove_sys_dev_entry(dev);
1157  devtattrError:
1158         if (MAJOR(dev->devt))
1159                 device_remove_file(dev, &devt_attr);
1160  ueventattrError:
1161         device_remove_file(dev, &uevent_attr);
1162  attrError:
1163         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1164         kobject_del(&dev->kobj);
1165  Error:
1166         cleanup_device_parent(dev);
1167         if (parent)
1168                 put_device(parent);
1169 name_error:
1170         kfree(dev->p);
1171         dev->p = NULL;
1172         goto done;
1173 }
1174 EXPORT_SYMBOL_GPL(device_add);
1175
1176 /**
1177  * device_register - register a device with the system.
1178  * @dev: pointer to the device structure
1179  *
1180  * This happens in two clean steps - initialize the device
1181  * and add it to the system. The two steps can be called
1182  * separately, but this is the easiest and most common.
1183  * I.e. you should only call the two helpers separately if
1184  * have a clearly defined need to use and refcount the device
1185  * before it is added to the hierarchy.
1186  *
1187  * For more information, see the kerneldoc for device_initialize()
1188  * and device_add().
1189  *
1190  * NOTE: _Never_ directly free @dev after calling this function, even
1191  * if it returned an error! Always use put_device() to give up the
1192  * reference initialized in this function instead.
1193  */
1194 int device_register(struct device *dev)
1195 {
1196         device_initialize(dev);
1197         return device_add(dev);
1198 }
1199 EXPORT_SYMBOL_GPL(device_register);
1200
1201 /**
1202  * get_device - increment reference count for device.
1203  * @dev: device.
1204  *
1205  * This simply forwards the call to kobject_get(), though
1206  * we do take care to provide for the case that we get a NULL
1207  * pointer passed in.
1208  */
1209 struct device *get_device(struct device *dev)
1210 {
1211         return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1212 }
1213 EXPORT_SYMBOL_GPL(get_device);
1214
1215 /**
1216  * put_device - decrement reference count.
1217  * @dev: device in question.
1218  */
1219 void put_device(struct device *dev)
1220 {
1221         /* might_sleep(); */
1222         if (dev)
1223                 kobject_put(&dev->kobj);
1224 }
1225 EXPORT_SYMBOL_GPL(put_device);
1226
1227 /**
1228  * device_del - delete device from system.
1229  * @dev: device.
1230  *
1231  * This is the first part of the device unregistration
1232  * sequence. This removes the device from the lists we control
1233  * from here, has it removed from the other driver model
1234  * subsystems it was added to in device_add(), and removes it
1235  * from the kobject hierarchy.
1236  *
1237  * NOTE: this should be called manually _iff_ device_add() was
1238  * also called manually.
1239  */
1240 void device_del(struct device *dev)
1241 {
1242         struct device *parent = dev->parent;
1243         struct class_interface *class_intf;
1244
1245         /* Notify clients of device removal.  This call must come
1246          * before dpm_sysfs_remove().
1247          */
1248         if (dev->bus)
1249                 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1250                                              BUS_NOTIFY_DEL_DEVICE, dev);
1251         dpm_sysfs_remove(dev);
1252         if (parent)
1253                 klist_del(&dev->p->knode_parent);
1254         if (MAJOR(dev->devt)) {
1255                 devtmpfs_delete_node(dev);
1256                 device_remove_sys_dev_entry(dev);
1257                 device_remove_file(dev, &devt_attr);
1258         }
1259         if (dev->class) {
1260                 device_remove_class_symlinks(dev);
1261
1262                 mutex_lock(&dev->class->p->mutex);
1263                 /* notify any interfaces that the device is now gone */
1264                 list_for_each_entry(class_intf,
1265                                     &dev->class->p->interfaces, node)
1266                         if (class_intf->remove_dev)
1267                                 class_intf->remove_dev(dev, class_intf);
1268                 /* remove the device from the class list */
1269                 klist_del(&dev->knode_class);
1270                 mutex_unlock(&dev->class->p->mutex);
1271         }
1272         device_remove_file(dev, &uevent_attr);
1273         device_remove_attrs(dev);
1274         bus_remove_device(dev);
1275         device_pm_remove(dev);
1276         driver_deferred_probe_del(dev);
1277
1278         /* Notify the platform of the removal, in case they
1279          * need to do anything...
1280          */
1281         if (platform_notify_remove)
1282                 platform_notify_remove(dev);
1283         kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1284         cleanup_device_parent(dev);
1285         kobject_del(&dev->kobj);
1286         put_device(parent);
1287 }
1288 EXPORT_SYMBOL_GPL(device_del);
1289
1290 /**
1291  * device_unregister - unregister device from system.
1292  * @dev: device going away.
1293  *
1294  * We do this in two parts, like we do device_register(). First,
1295  * we remove it from all the subsystems with device_del(), then
1296  * we decrement the reference count via put_device(). If that
1297  * is the final reference count, the device will be cleaned up
1298  * via device_release() above. Otherwise, the structure will
1299  * stick around until the final reference to the device is dropped.
1300  */
1301 void device_unregister(struct device *dev)
1302 {
1303         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1304         device_del(dev);
1305         put_device(dev);
1306 }
1307 EXPORT_SYMBOL_GPL(device_unregister);
1308
1309 static struct device *next_device(struct klist_iter *i)
1310 {
1311         struct klist_node *n = klist_next(i);
1312         struct device *dev = NULL;
1313         struct device_private *p;
1314
1315         if (n) {
1316                 p = to_device_private_parent(n);
1317                 dev = p->device;
1318         }
1319         return dev;
1320 }
1321
1322 /**
1323  * device_get_devnode - path of device node file
1324  * @dev: device
1325  * @mode: returned file access mode
1326  * @uid: returned file owner
1327  * @gid: returned file group
1328  * @tmp: possibly allocated string
1329  *
1330  * Return the relative path of a possible device node.
1331  * Non-default names may need to allocate a memory to compose
1332  * a name. This memory is returned in tmp and needs to be
1333  * freed by the caller.
1334  */
1335 const char *device_get_devnode(struct device *dev,
1336                                umode_t *mode, kuid_t *uid, kgid_t *gid,
1337                                const char **tmp)
1338 {
1339         char *s;
1340
1341         *tmp = NULL;
1342
1343         /* the device type may provide a specific name */
1344         if (dev->type && dev->type->devnode)
1345                 *tmp = dev->type->devnode(dev, mode, uid, gid);
1346         if (*tmp)
1347                 return *tmp;
1348
1349         /* the class may provide a specific name */
1350         if (dev->class && dev->class->devnode)
1351                 *tmp = dev->class->devnode(dev, mode);
1352         if (*tmp)
1353                 return *tmp;
1354
1355         /* return name without allocation, tmp == NULL */
1356         if (strchr(dev_name(dev), '!') == NULL)
1357                 return dev_name(dev);
1358
1359         /* replace '!' in the name with '/' */
1360         *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1361         if (!*tmp)
1362                 return NULL;
1363         while ((s = strchr(*tmp, '!')))
1364                 s[0] = '/';
1365         return *tmp;
1366 }
1367
1368 /**
1369  * device_for_each_child - device child iterator.
1370  * @parent: parent struct device.
1371  * @fn: function to be called for each device.
1372  * @data: data for the callback.
1373  *
1374  * Iterate over @parent's child devices, and call @fn for each,
1375  * passing it @data.
1376  *
1377  * We check the return of @fn each time. If it returns anything
1378  * other than 0, we break out and return that value.
1379  */
1380 int device_for_each_child(struct device *parent, void *data,
1381                           int (*fn)(struct device *dev, void *data))
1382 {
1383         struct klist_iter i;
1384         struct device *child;
1385         int error = 0;
1386
1387         if (!parent->p)
1388                 return 0;
1389
1390         klist_iter_init(&parent->p->klist_children, &i);
1391         while ((child = next_device(&i)) && !error)
1392                 error = fn(child, data);
1393         klist_iter_exit(&i);
1394         return error;
1395 }
1396 EXPORT_SYMBOL_GPL(device_for_each_child);
1397
1398 /**
1399  * device_find_child - device iterator for locating a particular device.
1400  * @parent: parent struct device
1401  * @match: Callback function to check device
1402  * @data: Data to pass to match function
1403  *
1404  * This is similar to the device_for_each_child() function above, but it
1405  * returns a reference to a device that is 'found' for later use, as
1406  * determined by the @match callback.
1407  *
1408  * The callback should return 0 if the device doesn't match and non-zero
1409  * if it does.  If the callback returns non-zero and a reference to the
1410  * current device can be obtained, this function will return to the caller
1411  * and not iterate over any more devices.
1412  *
1413  * NOTE: you will need to drop the reference with put_device() after use.
1414  */
1415 struct device *device_find_child(struct device *parent, void *data,
1416                                  int (*match)(struct device *dev, void *data))
1417 {
1418         struct klist_iter i;
1419         struct device *child;
1420
1421         if (!parent)
1422                 return NULL;
1423
1424         klist_iter_init(&parent->p->klist_children, &i);
1425         while ((child = next_device(&i)))
1426                 if (match(child, data) && get_device(child))
1427                         break;
1428         klist_iter_exit(&i);
1429         return child;
1430 }
1431 EXPORT_SYMBOL_GPL(device_find_child);
1432
1433 int __init devices_init(void)
1434 {
1435         devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1436         if (!devices_kset)
1437                 return -ENOMEM;
1438         dev_kobj = kobject_create_and_add("dev", NULL);
1439         if (!dev_kobj)
1440                 goto dev_kobj_err;
1441         sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1442         if (!sysfs_dev_block_kobj)
1443                 goto block_kobj_err;
1444         sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1445         if (!sysfs_dev_char_kobj)
1446                 goto char_kobj_err;
1447
1448         return 0;
1449
1450  char_kobj_err:
1451         kobject_put(sysfs_dev_block_kobj);
1452  block_kobj_err:
1453         kobject_put(dev_kobj);
1454  dev_kobj_err:
1455         kset_unregister(devices_kset);
1456         return -ENOMEM;
1457 }
1458
1459 static DEFINE_MUTEX(device_hotplug_lock);
1460
1461 void lock_device_hotplug(void)
1462 {
1463         mutex_lock(&device_hotplug_lock);
1464 }
1465
1466 void unlock_device_hotplug(void)
1467 {
1468         mutex_unlock(&device_hotplug_lock);
1469 }
1470
1471 static int device_check_offline(struct device *dev, void *not_used)
1472 {
1473         int ret;
1474
1475         ret = device_for_each_child(dev, NULL, device_check_offline);
1476         if (ret)
1477                 return ret;
1478
1479         return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1480 }
1481
1482 /**
1483  * device_offline - Prepare the device for hot-removal.
1484  * @dev: Device to be put offline.
1485  *
1486  * Execute the device bus type's .offline() callback, if present, to prepare
1487  * the device for a subsequent hot-removal.  If that succeeds, the device must
1488  * not be used until either it is removed or its bus type's .online() callback
1489  * is executed.
1490  *
1491  * Call under device_hotplug_lock.
1492  */
1493 int device_offline(struct device *dev)
1494 {
1495         int ret;
1496
1497         if (dev->offline_disabled)
1498                 return -EPERM;
1499
1500         ret = device_for_each_child(dev, NULL, device_check_offline);
1501         if (ret)
1502                 return ret;
1503
1504         device_lock(dev);
1505         if (device_supports_offline(dev)) {
1506                 if (dev->offline) {
1507                         ret = 1;
1508                 } else {
1509                         ret = dev->bus->offline(dev);
1510                         if (!ret) {
1511                                 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1512                                 dev->offline = true;
1513                         }
1514                 }
1515         }
1516         device_unlock(dev);
1517
1518         return ret;
1519 }
1520
1521 /**
1522  * device_online - Put the device back online after successful device_offline().
1523  * @dev: Device to be put back online.
1524  *
1525  * If device_offline() has been successfully executed for @dev, but the device
1526  * has not been removed subsequently, execute its bus type's .online() callback
1527  * to indicate that the device can be used again.
1528  *
1529  * Call under device_hotplug_lock.
1530  */
1531 int device_online(struct device *dev)
1532 {
1533         int ret = 0;
1534
1535         device_lock(dev);
1536         if (device_supports_offline(dev)) {
1537                 if (dev->offline) {
1538                         ret = dev->bus->online(dev);
1539                         if (!ret) {
1540                                 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1541                                 dev->offline = false;
1542                         }
1543                 } else {
1544                         ret = 1;
1545                 }
1546         }
1547         device_unlock(dev);
1548
1549         return ret;
1550 }
1551
1552 struct root_device {
1553         struct device dev;
1554         struct module *owner;
1555 };
1556
1557 static inline struct root_device *to_root_device(struct device *d)
1558 {
1559         return container_of(d, struct root_device, dev);
1560 }
1561
1562 static void root_device_release(struct device *dev)
1563 {
1564         kfree(to_root_device(dev));
1565 }
1566
1567 /**
1568  * __root_device_register - allocate and register a root device
1569  * @name: root device name
1570  * @owner: owner module of the root device, usually THIS_MODULE
1571  *
1572  * This function allocates a root device and registers it
1573  * using device_register(). In order to free the returned
1574  * device, use root_device_unregister().
1575  *
1576  * Root devices are dummy devices which allow other devices
1577  * to be grouped under /sys/devices. Use this function to
1578  * allocate a root device and then use it as the parent of
1579  * any device which should appear under /sys/devices/{name}
1580  *
1581  * The /sys/devices/{name} directory will also contain a
1582  * 'module' symlink which points to the @owner directory
1583  * in sysfs.
1584  *
1585  * Returns &struct device pointer on success, or ERR_PTR() on error.
1586  *
1587  * Note: You probably want to use root_device_register().
1588  */
1589 struct device *__root_device_register(const char *name, struct module *owner)
1590 {
1591         struct root_device *root;
1592         int err = -ENOMEM;
1593
1594         root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1595         if (!root)
1596                 return ERR_PTR(err);
1597
1598         err = dev_set_name(&root->dev, "%s", name);
1599         if (err) {
1600                 kfree(root);
1601                 return ERR_PTR(err);
1602         }
1603
1604         root->dev.release = root_device_release;
1605
1606         err = device_register(&root->dev);
1607         if (err) {
1608                 put_device(&root->dev);
1609                 return ERR_PTR(err);
1610         }
1611
1612 #ifdef CONFIG_MODULES   /* gotta find a "cleaner" way to do this */
1613         if (owner) {
1614                 struct module_kobject *mk = &owner->mkobj;
1615
1616                 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1617                 if (err) {
1618                         device_unregister(&root->dev);
1619                         return ERR_PTR(err);
1620                 }
1621                 root->owner = owner;
1622         }
1623 #endif
1624
1625         return &root->dev;
1626 }
1627 EXPORT_SYMBOL_GPL(__root_device_register);
1628
1629 /**
1630  * root_device_unregister - unregister and free a root device
1631  * @dev: device going away
1632  *
1633  * This function unregisters and cleans up a device that was created by
1634  * root_device_register().
1635  */
1636 void root_device_unregister(struct device *dev)
1637 {
1638         struct root_device *root = to_root_device(dev);
1639
1640         if (root->owner)
1641                 sysfs_remove_link(&root->dev.kobj, "module");
1642
1643         device_unregister(dev);
1644 }
1645 EXPORT_SYMBOL_GPL(root_device_unregister);
1646
1647
1648 static void device_create_release(struct device *dev)
1649 {
1650         pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1651         kfree(dev);
1652 }
1653
1654 static struct device *
1655 device_create_groups_vargs(struct class *class, struct device *parent,
1656                            dev_t devt, void *drvdata,
1657                            const struct attribute_group **groups,
1658                            const char *fmt, va_list args)
1659 {
1660         struct device *dev = NULL;
1661         int retval = -ENODEV;
1662
1663         if (class == NULL || IS_ERR(class))
1664                 goto error;
1665
1666         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1667         if (!dev) {
1668                 retval = -ENOMEM;
1669                 goto error;
1670         }
1671
1672         dev->devt = devt;
1673         dev->class = class;
1674         dev->parent = parent;
1675         dev->groups = groups;
1676         dev->release = device_create_release;
1677         dev_set_drvdata(dev, drvdata);
1678
1679         retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1680         if (retval)
1681                 goto error;
1682
1683         retval = device_register(dev);
1684         if (retval)
1685                 goto error;
1686
1687         return dev;
1688
1689 error:
1690         put_device(dev);
1691         return ERR_PTR(retval);
1692 }
1693
1694 /**
1695  * device_create_vargs - creates a device and registers it with sysfs
1696  * @class: pointer to the struct class that this device should be registered to
1697  * @parent: pointer to the parent struct device of this new device, if any
1698  * @devt: the dev_t for the char device to be added
1699  * @drvdata: the data to be added to the device for callbacks
1700  * @fmt: string for the device's name
1701  * @args: va_list for the device's name
1702  *
1703  * This function can be used by char device classes.  A struct device
1704  * will be created in sysfs, registered to the specified class.
1705  *
1706  * A "dev" file will be created, showing the dev_t for the device, if
1707  * the dev_t is not 0,0.
1708  * If a pointer to a parent struct device is passed in, the newly created
1709  * struct device will be a child of that device in sysfs.
1710  * The pointer to the struct device will be returned from the call.
1711  * Any further sysfs files that might be required can be created using this
1712  * pointer.
1713  *
1714  * Returns &struct device pointer on success, or ERR_PTR() on error.
1715  *
1716  * Note: the struct class passed to this function must have previously
1717  * been created with a call to class_create().
1718  */
1719 struct device *device_create_vargs(struct class *class, struct device *parent,
1720                                    dev_t devt, void *drvdata, const char *fmt,
1721                                    va_list args)
1722 {
1723         return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1724                                           fmt, args);
1725 }
1726 EXPORT_SYMBOL_GPL(device_create_vargs);
1727
1728 /**
1729  * device_create - creates a device and registers it with sysfs
1730  * @class: pointer to the struct class that this device should be registered to
1731  * @parent: pointer to the parent struct device of this new device, if any
1732  * @devt: the dev_t for the char device to be added
1733  * @drvdata: the data to be added to the device for callbacks
1734  * @fmt: string for the device's name
1735  *
1736  * This function can be used by char device classes.  A struct device
1737  * will be created in sysfs, registered to the specified class.
1738  *
1739  * A "dev" file will be created, showing the dev_t for the device, if
1740  * the dev_t is not 0,0.
1741  * If a pointer to a parent struct device is passed in, the newly created
1742  * struct device will be a child of that device in sysfs.
1743  * The pointer to the struct device will be returned from the call.
1744  * Any further sysfs files that might be required can be created using this
1745  * pointer.
1746  *
1747  * Returns &struct device pointer on success, or ERR_PTR() on error.
1748  *
1749  * Note: the struct class passed to this function must have previously
1750  * been created with a call to class_create().
1751  */
1752 struct device *device_create(struct class *class, struct device *parent,
1753                              dev_t devt, void *drvdata, const char *fmt, ...)
1754 {
1755         va_list vargs;
1756         struct device *dev;
1757
1758         va_start(vargs, fmt);
1759         dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1760         va_end(vargs);
1761         return dev;
1762 }
1763 EXPORT_SYMBOL_GPL(device_create);
1764
1765 /**
1766  * device_create_with_groups - creates a device and registers it with sysfs
1767  * @class: pointer to the struct class that this device should be registered to
1768  * @parent: pointer to the parent struct device of this new device, if any
1769  * @devt: the dev_t for the char device to be added
1770  * @drvdata: the data to be added to the device for callbacks
1771  * @groups: NULL-terminated list of attribute groups to be created
1772  * @fmt: string for the device's name
1773  *
1774  * This function can be used by char device classes.  A struct device
1775  * will be created in sysfs, registered to the specified class.
1776  * Additional attributes specified in the groups parameter will also
1777  * be created automatically.
1778  *
1779  * A "dev" file will be created, showing the dev_t for the device, if
1780  * the dev_t is not 0,0.
1781  * If a pointer to a parent struct device is passed in, the newly created
1782  * struct device will be a child of that device in sysfs.
1783  * The pointer to the struct device will be returned from the call.
1784  * Any further sysfs files that might be required can be created using this
1785  * pointer.
1786  *
1787  * Returns &struct device pointer on success, or ERR_PTR() on error.
1788  *
1789  * Note: the struct class passed to this function must have previously
1790  * been created with a call to class_create().
1791  */
1792 struct device *device_create_with_groups(struct class *class,
1793                                          struct device *parent, dev_t devt,
1794                                          void *drvdata,
1795                                          const struct attribute_group **groups,
1796                                          const char *fmt, ...)
1797 {
1798         va_list vargs;
1799         struct device *dev;
1800
1801         va_start(vargs, fmt);
1802         dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1803                                          fmt, vargs);
1804         va_end(vargs);
1805         return dev;
1806 }
1807 EXPORT_SYMBOL_GPL(device_create_with_groups);
1808
1809 static int __match_devt(struct device *dev, const void *data)
1810 {
1811         const dev_t *devt = data;
1812
1813         return dev->devt == *devt;
1814 }
1815
1816 /**
1817  * device_destroy - removes a device that was created with device_create()
1818  * @class: pointer to the struct class that this device was registered with
1819  * @devt: the dev_t of the device that was previously registered
1820  *
1821  * This call unregisters and cleans up a device that was created with a
1822  * call to device_create().
1823  */
1824 void device_destroy(struct class *class, dev_t devt)
1825 {
1826         struct device *dev;
1827
1828         dev = class_find_device(class, NULL, &devt, __match_devt);
1829         if (dev) {
1830                 put_device(dev);
1831                 device_unregister(dev);
1832         }
1833 }
1834 EXPORT_SYMBOL_GPL(device_destroy);
1835
1836 /**
1837  * device_rename - renames a device
1838  * @dev: the pointer to the struct device to be renamed
1839  * @new_name: the new name of the device
1840  *
1841  * It is the responsibility of the caller to provide mutual
1842  * exclusion between two different calls of device_rename
1843  * on the same device to ensure that new_name is valid and
1844  * won't conflict with other devices.
1845  *
1846  * Note: Don't call this function.  Currently, the networking layer calls this
1847  * function, but that will change.  The following text from Kay Sievers offers
1848  * some insight:
1849  *
1850  * Renaming devices is racy at many levels, symlinks and other stuff are not
1851  * replaced atomically, and you get a "move" uevent, but it's not easy to
1852  * connect the event to the old and new device. Device nodes are not renamed at
1853  * all, there isn't even support for that in the kernel now.
1854  *
1855  * In the meantime, during renaming, your target name might be taken by another
1856  * driver, creating conflicts. Or the old name is taken directly after you
1857  * renamed it -- then you get events for the same DEVPATH, before you even see
1858  * the "move" event. It's just a mess, and nothing new should ever rely on
1859  * kernel device renaming. Besides that, it's not even implemented now for
1860  * other things than (driver-core wise very simple) network devices.
1861  *
1862  * We are currently about to change network renaming in udev to completely
1863  * disallow renaming of devices in the same namespace as the kernel uses,
1864  * because we can't solve the problems properly, that arise with swapping names
1865  * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1866  * be allowed to some other name than eth[0-9]*, for the aforementioned
1867  * reasons.
1868  *
1869  * Make up a "real" name in the driver before you register anything, or add
1870  * some other attributes for userspace to find the device, or use udev to add
1871  * symlinks -- but never rename kernel devices later, it's a complete mess. We
1872  * don't even want to get into that and try to implement the missing pieces in
1873  * the core. We really have other pieces to fix in the driver core mess. :)
1874  */
1875 int device_rename(struct device *dev, const char *new_name)
1876 {
1877         char *old_device_name = NULL;
1878         int error;
1879
1880         dev = get_device(dev);
1881         if (!dev)
1882                 return -EINVAL;
1883
1884         pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1885                  __func__, new_name);
1886
1887         old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1888         if (!old_device_name) {
1889                 error = -ENOMEM;
1890                 goto out;
1891         }
1892
1893         if (dev->class) {
1894                 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
1895                         &dev->kobj, old_device_name, new_name);
1896                 if (error)
1897                         goto out;
1898         }
1899
1900         error = kobject_rename(&dev->kobj, new_name);
1901         if (error)
1902                 goto out;
1903
1904 out:
1905         put_device(dev);
1906
1907         kfree(old_device_name);
1908
1909         return error;
1910 }
1911 EXPORT_SYMBOL_GPL(device_rename);
1912
1913 static int device_move_class_links(struct device *dev,
1914                                    struct device *old_parent,
1915                                    struct device *new_parent)
1916 {
1917         int error = 0;
1918
1919         if (old_parent)
1920                 sysfs_remove_link(&dev->kobj, "device");
1921         if (new_parent)
1922                 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1923                                           "device");
1924         return error;
1925 }
1926
1927 /**
1928  * device_move - moves a device to a new parent
1929  * @dev: the pointer to the struct device to be moved
1930  * @new_parent: the new parent of the device (can by NULL)
1931  * @dpm_order: how to reorder the dpm_list
1932  */
1933 int device_move(struct device *dev, struct device *new_parent,
1934                 enum dpm_order dpm_order)
1935 {
1936         int error;
1937         struct device *old_parent;
1938         struct kobject *new_parent_kobj;
1939
1940         dev = get_device(dev);
1941         if (!dev)
1942                 return -EINVAL;
1943
1944         device_pm_lock();
1945         new_parent = get_device(new_parent);
1946         new_parent_kobj = get_device_parent(dev, new_parent);
1947
1948         pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1949                  __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1950         error = kobject_move(&dev->kobj, new_parent_kobj);
1951         if (error) {
1952                 cleanup_glue_dir(dev, new_parent_kobj);
1953                 put_device(new_parent);
1954                 goto out;
1955         }
1956         old_parent = dev->parent;
1957         dev->parent = new_parent;
1958         if (old_parent)
1959                 klist_remove(&dev->p->knode_parent);
1960         if (new_parent) {
1961                 klist_add_tail(&dev->p->knode_parent,
1962                                &new_parent->p->klist_children);
1963                 set_dev_node(dev, dev_to_node(new_parent));
1964         }
1965
1966         if (dev->class) {
1967                 error = device_move_class_links(dev, old_parent, new_parent);
1968                 if (error) {
1969                         /* We ignore errors on cleanup since we're hosed anyway... */
1970                         device_move_class_links(dev, new_parent, old_parent);
1971                         if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1972                                 if (new_parent)
1973                                         klist_remove(&dev->p->knode_parent);
1974                                 dev->parent = old_parent;
1975                                 if (old_parent) {
1976                                         klist_add_tail(&dev->p->knode_parent,
1977                                                        &old_parent->p->klist_children);
1978                                         set_dev_node(dev, dev_to_node(old_parent));
1979                                 }
1980                         }
1981                         cleanup_glue_dir(dev, new_parent_kobj);
1982                         put_device(new_parent);
1983                         goto out;
1984                 }
1985         }
1986         switch (dpm_order) {
1987         case DPM_ORDER_NONE:
1988                 break;
1989         case DPM_ORDER_DEV_AFTER_PARENT:
1990                 device_pm_move_after(dev, new_parent);
1991                 break;
1992         case DPM_ORDER_PARENT_BEFORE_DEV:
1993                 device_pm_move_before(new_parent, dev);
1994                 break;
1995         case DPM_ORDER_DEV_LAST:
1996                 device_pm_move_last(dev);
1997                 break;
1998         }
1999
2000         put_device(old_parent);
2001 out:
2002         device_pm_unlock();
2003         put_device(dev);
2004         return error;
2005 }
2006 EXPORT_SYMBOL_GPL(device_move);
2007
2008 /**
2009  * device_shutdown - call ->shutdown() on each device to shutdown.
2010  */
2011 void device_shutdown(void)
2012 {
2013         struct device *dev;
2014
2015         spin_lock(&devices_kset->list_lock);
2016         /*
2017          * Walk the devices list backward, shutting down each in turn.
2018          * Beware that device unplug events may also start pulling
2019          * devices offline, even as the system is shutting down.
2020          */
2021         while (!list_empty(&devices_kset->list)) {
2022                 dev = list_entry(devices_kset->list.prev, struct device,
2023                                 kobj.entry);
2024
2025                 /*
2026                  * hold reference count of device's parent to
2027                  * prevent it from being freed because parent's
2028                  * lock is to be held
2029                  */
2030                 get_device(dev->parent);
2031                 get_device(dev);
2032                 /*
2033                  * Make sure the device is off the kset list, in the
2034                  * event that dev->*->shutdown() doesn't remove it.
2035                  */
2036                 list_del_init(&dev->kobj.entry);
2037                 spin_unlock(&devices_kset->list_lock);
2038
2039                 /* hold lock to avoid race with probe/release */
2040                 if (dev->parent)
2041                         device_lock(dev->parent);
2042                 device_lock(dev);
2043
2044                 /* Don't allow any more runtime suspends */
2045                 pm_runtime_get_noresume(dev);
2046                 pm_runtime_barrier(dev);
2047
2048                 if (dev->bus && dev->bus->shutdown) {
2049                         if (initcall_debug)
2050                                 dev_info(dev, "shutdown\n");
2051                         dev->bus->shutdown(dev);
2052                 } else if (dev->driver && dev->driver->shutdown) {
2053                         if (initcall_debug)
2054                                 dev_info(dev, "shutdown\n");
2055                         dev->driver->shutdown(dev);
2056                 }
2057
2058                 device_unlock(dev);
2059                 if (dev->parent)
2060                         device_unlock(dev->parent);
2061
2062                 put_device(dev);
2063                 put_device(dev->parent);
2064
2065                 spin_lock(&devices_kset->list_lock);
2066         }
2067         spin_unlock(&devices_kset->list_lock);
2068         async_synchronize_full();
2069 }
2070
2071 /*
2072  * Device logging functions
2073  */
2074
2075 #ifdef CONFIG_PRINTK
2076 static int
2077 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2078 {
2079         const char *subsys;
2080         size_t pos = 0;
2081
2082         if (dev->class)
2083                 subsys = dev->class->name;
2084         else if (dev->bus)
2085                 subsys = dev->bus->name;
2086         else
2087                 return 0;
2088
2089         pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2090
2091         /*
2092          * Add device identifier DEVICE=:
2093          *   b12:8         block dev_t
2094          *   c127:3        char dev_t
2095          *   n8            netdev ifindex
2096          *   +sound:card0  subsystem:devname
2097          */
2098         if (MAJOR(dev->devt)) {
2099                 char c;
2100
2101                 if (strcmp(subsys, "block") == 0)
2102                         c = 'b';
2103                 else
2104                         c = 'c';
2105                 pos++;
2106                 pos += snprintf(hdr + pos, hdrlen - pos,
2107                                 "DEVICE=%c%u:%u",
2108                                 c, MAJOR(dev->devt), MINOR(dev->devt));
2109         } else if (strcmp(subsys, "net") == 0) {
2110                 struct net_device *net = to_net_dev(dev);
2111
2112                 pos++;
2113                 pos += snprintf(hdr + pos, hdrlen - pos,
2114                                 "DEVICE=n%u", net->ifindex);
2115         } else {
2116                 pos++;
2117                 pos += snprintf(hdr + pos, hdrlen - pos,
2118                                 "DEVICE=+%s:%s", subsys, dev_name(dev));
2119         }
2120
2121         return pos;
2122 }
2123 EXPORT_SYMBOL(create_syslog_header);
2124
2125 int dev_vprintk_emit(int level, const struct device *dev,
2126                      const char *fmt, va_list args)
2127 {
2128         char hdr[128];
2129         size_t hdrlen;
2130
2131         hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2132
2133         return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2134 }
2135 EXPORT_SYMBOL(dev_vprintk_emit);
2136
2137 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2138 {
2139         va_list args;
2140         int r;
2141
2142         va_start(args, fmt);
2143
2144         r = dev_vprintk_emit(level, dev, fmt, args);
2145
2146         va_end(args);
2147
2148         return r;
2149 }
2150 EXPORT_SYMBOL(dev_printk_emit);
2151
2152 static int __dev_printk(const char *level, const struct device *dev,
2153                         struct va_format *vaf)
2154 {
2155         if (!dev)
2156                 return printk("%s(NULL device *): %pV", level, vaf);
2157
2158         return dev_printk_emit(level[1] - '0', dev,
2159                                "%s %s: %pV",
2160                                dev_driver_string(dev), dev_name(dev), vaf);
2161 }
2162
2163 int dev_printk(const char *level, const struct device *dev,
2164                const char *fmt, ...)
2165 {
2166         struct va_format vaf;
2167         va_list args;
2168         int r;
2169
2170         va_start(args, fmt);
2171
2172         vaf.fmt = fmt;
2173         vaf.va = &args;
2174
2175         r = __dev_printk(level, dev, &vaf);
2176
2177         va_end(args);
2178
2179         return r;
2180 }
2181 EXPORT_SYMBOL(dev_printk);
2182
2183 #define define_dev_printk_level(func, kern_level)               \
2184 int func(const struct device *dev, const char *fmt, ...)        \
2185 {                                                               \
2186         struct va_format vaf;                                   \
2187         va_list args;                                           \
2188         int r;                                                  \
2189                                                                 \
2190         va_start(args, fmt);                                    \
2191                                                                 \
2192         vaf.fmt = fmt;                                          \
2193         vaf.va = &args;                                         \
2194                                                                 \
2195         r = __dev_printk(kern_level, dev, &vaf);                \
2196                                                                 \
2197         va_end(args);                                           \
2198                                                                 \
2199         return r;                                               \
2200 }                                                               \
2201 EXPORT_SYMBOL(func);
2202
2203 define_dev_printk_level(dev_emerg, KERN_EMERG);
2204 define_dev_printk_level(dev_alert, KERN_ALERT);
2205 define_dev_printk_level(dev_crit, KERN_CRIT);
2206 define_dev_printk_level(dev_err, KERN_ERR);
2207 define_dev_printk_level(dev_warn, KERN_WARNING);
2208 define_dev_printk_level(dev_notice, KERN_NOTICE);
2209 define_dev_printk_level(_dev_info, KERN_INFO);
2210
2211 #endif