core.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/kdev_t.h>
  19. #include <linux/notifier.h>
  20. #include <linux/genhd.h>
  21. #include <asm/semaphore.h>
  22. #include "base.h"
  23. #include "power/power.h"
  24. int (*platform_notify)(struct device * dev) = NULL;
  25. int (*platform_notify_remove)(struct device * dev) = NULL;
  26. /*
  27. * sysfs bindings for devices.
  28. */
  29. /**
  30. * dev_driver_string - Return a device's driver name, if at all possible
  31. * @dev: struct device to get the name of
  32. *
  33. * Will return the device's driver's name if it is bound to a device. If
  34. * the device is not bound to a device, it will return the name of the bus
  35. * it is attached to. If it is not attached to a bus either, an empty
  36. * string will be returned.
  37. */
  38. const char *dev_driver_string(struct device *dev)
  39. {
  40. return dev->driver ? dev->driver->name :
  41. (dev->bus ? dev->bus->name :
  42. (dev->class ? dev->class->name : ""));
  43. }
  44. EXPORT_SYMBOL(dev_driver_string);
  45. #define to_dev(obj) container_of(obj, struct device, kobj)
  46. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  47. static ssize_t
  48. dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  49. {
  50. struct device_attribute * dev_attr = to_dev_attr(attr);
  51. struct device * dev = to_dev(kobj);
  52. ssize_t ret = -EIO;
  53. if (dev_attr->show)
  54. ret = dev_attr->show(dev, dev_attr, buf);
  55. return ret;
  56. }
  57. static ssize_t
  58. dev_attr_store(struct kobject * kobj, struct attribute * attr,
  59. const char * buf, size_t count)
  60. {
  61. struct device_attribute * dev_attr = to_dev_attr(attr);
  62. struct device * dev = to_dev(kobj);
  63. ssize_t ret = -EIO;
  64. if (dev_attr->store)
  65. ret = dev_attr->store(dev, dev_attr, buf, count);
  66. return ret;
  67. }
  68. static struct sysfs_ops dev_sysfs_ops = {
  69. .show = dev_attr_show,
  70. .store = dev_attr_store,
  71. };
  72. /**
  73. * device_release - free device structure.
  74. * @kobj: device's kobject.
  75. *
  76. * This is called once the reference count for the object
  77. * reaches 0. We forward the call to the device's release
  78. * method, which should handle actually freeing the structure.
  79. */
  80. static void device_release(struct kobject * kobj)
  81. {
  82. struct device * dev = to_dev(kobj);
  83. if (dev->release)
  84. dev->release(dev);
  85. else if (dev->type && dev->type->release)
  86. dev->type->release(dev);
  87. else if (dev->class && dev->class->dev_release)
  88. dev->class->dev_release(dev);
  89. else {
  90. printk(KERN_ERR "Device '%s' does not have a release() function, "
  91. "it is broken and must be fixed.\n",
  92. dev->bus_id);
  93. WARN_ON(1);
  94. }
  95. }
  96. static struct kobj_type device_ktype = {
  97. .release = device_release,
  98. .sysfs_ops = &dev_sysfs_ops,
  99. };
  100. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  101. {
  102. struct kobj_type *ktype = get_ktype(kobj);
  103. if (ktype == &device_ktype) {
  104. struct device *dev = to_dev(kobj);
  105. if (dev->uevent_suppress)
  106. return 0;
  107. if (dev->bus)
  108. return 1;
  109. if (dev->class)
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  115. {
  116. struct device *dev = to_dev(kobj);
  117. if (dev->bus)
  118. return dev->bus->name;
  119. if (dev->class)
  120. return dev->class->name;
  121. return NULL;
  122. }
  123. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  124. struct kobj_uevent_env *env)
  125. {
  126. struct device *dev = to_dev(kobj);
  127. int retval = 0;
  128. /* add the major/minor if present */
  129. if (MAJOR(dev->devt)) {
  130. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  131. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  132. }
  133. if (dev->type && dev->type->name)
  134. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  135. if (dev->driver)
  136. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  137. #ifdef CONFIG_SYSFS_DEPRECATED
  138. if (dev->class) {
  139. struct device *parent = dev->parent;
  140. /* find first bus device in parent chain */
  141. while (parent && !parent->bus)
  142. parent = parent->parent;
  143. if (parent && parent->bus) {
  144. const char *path;
  145. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  146. if (path) {
  147. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  148. kfree(path);
  149. }
  150. add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
  151. if (parent->driver)
  152. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  153. parent->driver->name);
  154. }
  155. } else if (dev->bus) {
  156. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  157. if (dev->driver)
  158. add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
  159. }
  160. #endif
  161. /* have the bus specific function add its stuff */
  162. if (dev->bus && dev->bus->uevent) {
  163. retval = dev->bus->uevent(dev, env);
  164. if (retval)
  165. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  166. dev->bus_id, __FUNCTION__, retval);
  167. }
  168. /* have the class specific function add its stuff */
  169. if (dev->class && dev->class->dev_uevent) {
  170. retval = dev->class->dev_uevent(dev, env);
  171. if (retval)
  172. pr_debug("device: '%s': %s: class uevent() "
  173. "returned %d\n", dev->bus_id,
  174. __FUNCTION__, retval);
  175. }
  176. /* have the device type specific fuction add its stuff */
  177. if (dev->type && dev->type->uevent) {
  178. retval = dev->type->uevent(dev, env);
  179. if (retval)
  180. pr_debug("device: '%s': %s: dev_type uevent() "
  181. "returned %d\n", dev->bus_id,
  182. __FUNCTION__, retval);
  183. }
  184. return retval;
  185. }
  186. static struct kset_uevent_ops device_uevent_ops = {
  187. .filter = dev_uevent_filter,
  188. .name = dev_uevent_name,
  189. .uevent = dev_uevent,
  190. };
  191. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  192. char *buf)
  193. {
  194. struct kobject *top_kobj;
  195. struct kset *kset;
  196. struct kobj_uevent_env *env = NULL;
  197. int i;
  198. size_t count = 0;
  199. int retval;
  200. /* search the kset, the device belongs to */
  201. top_kobj = &dev->kobj;
  202. while (!top_kobj->kset && top_kobj->parent)
  203. top_kobj = top_kobj->parent;
  204. if (!top_kobj->kset)
  205. goto out;
  206. kset = top_kobj->kset;
  207. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  208. goto out;
  209. /* respect filter */
  210. if (kset->uevent_ops && kset->uevent_ops->filter)
  211. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  212. goto out;
  213. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  214. if (!env)
  215. return -ENOMEM;
  216. /* let the kset specific function add its keys */
  217. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  218. if (retval)
  219. goto out;
  220. /* copy keys to file */
  221. for (i = 0; i < env->envp_idx; i++)
  222. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  223. out:
  224. kfree(env);
  225. return count;
  226. }
  227. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  228. const char *buf, size_t count)
  229. {
  230. enum kobject_action action;
  231. if (kobject_action_type(buf, count, &action) == 0) {
  232. kobject_uevent(&dev->kobj, action);
  233. goto out;
  234. }
  235. dev_err(dev, "uevent: unsupported action-string; this will "
  236. "be ignored in a future kernel version\n");
  237. kobject_uevent(&dev->kobj, KOBJ_ADD);
  238. out:
  239. return count;
  240. }
  241. static struct device_attribute uevent_attr =
  242. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  243. static int device_add_attributes(struct device *dev,
  244. struct device_attribute *attrs)
  245. {
  246. int error = 0;
  247. int i;
  248. if (attrs) {
  249. for (i = 0; attr_name(attrs[i]); i++) {
  250. error = device_create_file(dev, &attrs[i]);
  251. if (error)
  252. break;
  253. }
  254. if (error)
  255. while (--i >= 0)
  256. device_remove_file(dev, &attrs[i]);
  257. }
  258. return error;
  259. }
  260. static void device_remove_attributes(struct device *dev,
  261. struct device_attribute *attrs)
  262. {
  263. int i;
  264. if (attrs)
  265. for (i = 0; attr_name(attrs[i]); i++)
  266. device_remove_file(dev, &attrs[i]);
  267. }
  268. static int device_add_groups(struct device *dev,
  269. struct attribute_group **groups)
  270. {
  271. int error = 0;
  272. int i;
  273. if (groups) {
  274. for (i = 0; groups[i]; i++) {
  275. error = sysfs_create_group(&dev->kobj, groups[i]);
  276. if (error) {
  277. while (--i >= 0)
  278. sysfs_remove_group(&dev->kobj, groups[i]);
  279. break;
  280. }
  281. }
  282. }
  283. return error;
  284. }
  285. static void device_remove_groups(struct device *dev,
  286. struct attribute_group **groups)
  287. {
  288. int i;
  289. if (groups)
  290. for (i = 0; groups[i]; i++)
  291. sysfs_remove_group(&dev->kobj, groups[i]);
  292. }
  293. static int device_add_attrs(struct device *dev)
  294. {
  295. struct class *class = dev->class;
  296. struct device_type *type = dev->type;
  297. int error;
  298. if (class) {
  299. error = device_add_attributes(dev, class->dev_attrs);
  300. if (error)
  301. return error;
  302. }
  303. if (type) {
  304. error = device_add_groups(dev, type->groups);
  305. if (error)
  306. goto err_remove_class_attrs;
  307. }
  308. error = device_add_groups(dev, dev->groups);
  309. if (error)
  310. goto err_remove_type_groups;
  311. return 0;
  312. err_remove_type_groups:
  313. if (type)
  314. device_remove_groups(dev, type->groups);
  315. err_remove_class_attrs:
  316. if (class)
  317. device_remove_attributes(dev, class->dev_attrs);
  318. return error;
  319. }
  320. static void device_remove_attrs(struct device *dev)
  321. {
  322. struct class *class = dev->class;
  323. struct device_type *type = dev->type;
  324. device_remove_groups(dev, dev->groups);
  325. if (type)
  326. device_remove_groups(dev, type->groups);
  327. if (class)
  328. device_remove_attributes(dev, class->dev_attrs);
  329. }
  330. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  331. char *buf)
  332. {
  333. return print_dev_t(buf, dev->devt);
  334. }
  335. static struct device_attribute devt_attr =
  336. __ATTR(dev, S_IRUGO, show_dev, NULL);
  337. /* kset to create /sys/devices/ */
  338. struct kset *devices_kset;
  339. /**
  340. * device_create_file - create sysfs attribute file for device.
  341. * @dev: device.
  342. * @attr: device attribute descriptor.
  343. */
  344. int device_create_file(struct device * dev, struct device_attribute * attr)
  345. {
  346. int error = 0;
  347. if (get_device(dev)) {
  348. error = sysfs_create_file(&dev->kobj, &attr->attr);
  349. put_device(dev);
  350. }
  351. return error;
  352. }
  353. /**
  354. * device_remove_file - remove sysfs attribute file.
  355. * @dev: device.
  356. * @attr: device attribute descriptor.
  357. */
  358. void device_remove_file(struct device * dev, struct device_attribute * attr)
  359. {
  360. if (get_device(dev)) {
  361. sysfs_remove_file(&dev->kobj, &attr->attr);
  362. put_device(dev);
  363. }
  364. }
  365. /**
  366. * device_create_bin_file - create sysfs binary attribute file for device.
  367. * @dev: device.
  368. * @attr: device binary attribute descriptor.
  369. */
  370. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  371. {
  372. int error = -EINVAL;
  373. if (dev)
  374. error = sysfs_create_bin_file(&dev->kobj, attr);
  375. return error;
  376. }
  377. EXPORT_SYMBOL_GPL(device_create_bin_file);
  378. /**
  379. * device_remove_bin_file - remove sysfs binary attribute file
  380. * @dev: device.
  381. * @attr: device binary attribute descriptor.
  382. */
  383. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  384. {
  385. if (dev)
  386. sysfs_remove_bin_file(&dev->kobj, attr);
  387. }
  388. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  389. /**
  390. * device_schedule_callback_owner - helper to schedule a callback for a device
  391. * @dev: device.
  392. * @func: callback function to invoke later.
  393. * @owner: module owning the callback routine
  394. *
  395. * Attribute methods must not unregister themselves or their parent device
  396. * (which would amount to the same thing). Attempts to do so will deadlock,
  397. * since unregistration is mutually exclusive with driver callbacks.
  398. *
  399. * Instead methods can call this routine, which will attempt to allocate
  400. * and schedule a workqueue request to call back @func with @dev as its
  401. * argument in the workqueue's process context. @dev will be pinned until
  402. * @func returns.
  403. *
  404. * This routine is usually called via the inline device_schedule_callback(),
  405. * which automatically sets @owner to THIS_MODULE.
  406. *
  407. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  408. * be allocated, -ENODEV if a reference to @owner isn't available.
  409. *
  410. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  411. * underlying sysfs routine (since it is intended for use by attribute
  412. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  413. */
  414. int device_schedule_callback_owner(struct device *dev,
  415. void (*func)(struct device *), struct module *owner)
  416. {
  417. return sysfs_schedule_callback(&dev->kobj,
  418. (void (*)(void *)) func, dev, owner);
  419. }
  420. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  421. static void klist_children_get(struct klist_node *n)
  422. {
  423. struct device *dev = container_of(n, struct device, knode_parent);
  424. get_device(dev);
  425. }
  426. static void klist_children_put(struct klist_node *n)
  427. {
  428. struct device *dev = container_of(n, struct device, knode_parent);
  429. put_device(dev);
  430. }
  431. /**
  432. * device_initialize - init device structure.
  433. * @dev: device.
  434. *
  435. * This prepares the device for use by other layers,
  436. * including adding it to the device hierarchy.
  437. * It is the first half of device_register(), if called by
  438. * that, though it can also be called separately, so one
  439. * may use @dev's fields (e.g. the refcount).
  440. */
  441. void device_initialize(struct device *dev)
  442. {
  443. dev->kobj.kset = devices_kset;
  444. kobject_init_ng(&dev->kobj, &device_ktype);
  445. klist_init(&dev->klist_children, klist_children_get,
  446. klist_children_put);
  447. INIT_LIST_HEAD(&dev->dma_pools);
  448. INIT_LIST_HEAD(&dev->node);
  449. init_MUTEX(&dev->sem);
  450. spin_lock_init(&dev->devres_lock);
  451. INIT_LIST_HEAD(&dev->devres_head);
  452. device_init_wakeup(dev, 0);
  453. set_dev_node(dev, -1);
  454. }
  455. #ifdef CONFIG_SYSFS_DEPRECATED
  456. static struct kobject *get_device_parent(struct device *dev,
  457. struct device *parent)
  458. {
  459. /* class devices without a parent live in /sys/class/<classname>/ */
  460. if (dev->class && (!parent || parent->class != dev->class))
  461. return &dev->class->subsys.kobj;
  462. /* all other devices keep their parent */
  463. else if (parent)
  464. return &parent->kobj;
  465. return NULL;
  466. }
  467. static inline void cleanup_device_parent(struct device *dev) {}
  468. #else
  469. static struct kobject *virtual_device_parent(struct device *dev)
  470. {
  471. static struct kobject *virtual_dir = NULL;
  472. if (!virtual_dir)
  473. virtual_dir = kobject_create_and_add("virtual",
  474. &devices_kset->kobj);
  475. return virtual_dir;
  476. }
  477. static struct kobject *get_device_parent(struct device *dev,
  478. struct device *parent)
  479. {
  480. int retval;
  481. if (dev->class) {
  482. struct kobject *kobj = NULL;
  483. struct kobject *parent_kobj;
  484. struct kobject *k;
  485. /*
  486. * If we have no parent, we live in "virtual".
  487. * Class-devices with a bus-device as parent, live
  488. * in a class-directory to prevent namespace collisions.
  489. */
  490. if (parent == NULL)
  491. parent_kobj = virtual_device_parent(dev);
  492. else if (parent->class)
  493. return &parent->kobj;
  494. else
  495. parent_kobj = &parent->kobj;
  496. /* find our class-directory at the parent and reference it */
  497. spin_lock(&dev->class->class_dirs.list_lock);
  498. list_for_each_entry(k, &dev->class->class_dirs.list, entry)
  499. if (k->parent == parent_kobj) {
  500. kobj = kobject_get(k);
  501. break;
  502. }
  503. spin_unlock(&dev->class->class_dirs.list_lock);
  504. if (kobj)
  505. return kobj;
  506. /* or create a new class-directory at the parent device */
  507. k = kobject_create();
  508. if (!k)
  509. return NULL;
  510. k->kset = &dev->class->class_dirs;
  511. retval = kobject_add_ng(k, parent_kobj, "%s", dev->class->name);
  512. if (retval < 0) {
  513. kobject_put(k);
  514. return NULL;
  515. }
  516. /* Do not emit a uevent, as it's not needed for this
  517. * "class glue" directory. */
  518. return k;
  519. }
  520. if (parent)
  521. return &parent->kobj;
  522. return NULL;
  523. }
  524. static void cleanup_device_parent(struct device *dev)
  525. {
  526. struct device *d;
  527. int other = 0;
  528. if (!dev->class)
  529. return;
  530. /* see if we live in a parent class directory */
  531. if (dev->kobj.parent->kset != &dev->class->class_dirs)
  532. return;
  533. /* if we are the last child of our class, delete the directory */
  534. down(&dev->class->sem);
  535. list_for_each_entry(d, &dev->class->devices, node) {
  536. if (d == dev)
  537. continue;
  538. if (d->kobj.parent == dev->kobj.parent) {
  539. other = 1;
  540. break;
  541. }
  542. }
  543. if (!other)
  544. kobject_del(dev->kobj.parent);
  545. kobject_put(dev->kobj.parent);
  546. up(&dev->class->sem);
  547. }
  548. #endif
  549. static int setup_parent(struct device *dev, struct device *parent)
  550. {
  551. struct kobject *kobj;
  552. kobj = get_device_parent(dev, parent);
  553. if (IS_ERR(kobj))
  554. return PTR_ERR(kobj);
  555. if (kobj)
  556. dev->kobj.parent = kobj;
  557. return 0;
  558. }
  559. static int device_add_class_symlinks(struct device *dev)
  560. {
  561. int error;
  562. if (!dev->class)
  563. return 0;
  564. error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
  565. "subsystem");
  566. if (error)
  567. goto out;
  568. #ifdef CONFIG_SYSFS_DEPRECATED
  569. /* stacked class devices need a symlink in the class directory */
  570. if (dev->kobj.parent != &dev->class->subsys.kobj) {
  571. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  572. dev->bus_id);
  573. if (error)
  574. goto out_subsys;
  575. }
  576. if (dev->parent) {
  577. struct device *parent = dev->parent;
  578. char *class_name;
  579. /*
  580. * stacked class devices have the 'device' link
  581. * pointing to the bus device instead of the parent
  582. */
  583. while (parent->class && !parent->bus && parent->parent)
  584. parent = parent->parent;
  585. error = sysfs_create_link(&dev->kobj,
  586. &parent->kobj,
  587. "device");
  588. if (error)
  589. goto out_busid;
  590. class_name = make_class_name(dev->class->name,
  591. &dev->kobj);
  592. if (class_name)
  593. error = sysfs_create_link(&dev->parent->kobj,
  594. &dev->kobj, class_name);
  595. kfree(class_name);
  596. if (error)
  597. goto out_device;
  598. }
  599. return 0;
  600. out_device:
  601. if (dev->parent)
  602. sysfs_remove_link(&dev->kobj, "device");
  603. out_busid:
  604. if (dev->kobj.parent != &dev->class->subsys.kobj)
  605. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  606. #else
  607. /* link in the class directory pointing to the device */
  608. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  609. dev->bus_id);
  610. if (error)
  611. goto out_subsys;
  612. if (dev->parent) {
  613. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  614. "device");
  615. if (error)
  616. goto out_busid;
  617. }
  618. return 0;
  619. out_busid:
  620. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  621. #endif
  622. out_subsys:
  623. sysfs_remove_link(&dev->kobj, "subsystem");
  624. out:
  625. return error;
  626. }
  627. static void device_remove_class_symlinks(struct device *dev)
  628. {
  629. if (!dev->class)
  630. return;
  631. #ifdef CONFIG_SYSFS_DEPRECATED
  632. if (dev->parent) {
  633. char *class_name;
  634. class_name = make_class_name(dev->class->name, &dev->kobj);
  635. if (class_name) {
  636. sysfs_remove_link(&dev->parent->kobj, class_name);
  637. kfree(class_name);
  638. }
  639. sysfs_remove_link(&dev->kobj, "device");
  640. }
  641. if (dev->kobj.parent != &dev->class->subsys.kobj)
  642. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  643. #else
  644. if (dev->parent)
  645. sysfs_remove_link(&dev->kobj, "device");
  646. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  647. #endif
  648. sysfs_remove_link(&dev->kobj, "subsystem");
  649. }
  650. /**
  651. * device_add - add device to device hierarchy.
  652. * @dev: device.
  653. *
  654. * This is part 2 of device_register(), though may be called
  655. * separately _iff_ device_initialize() has been called separately.
  656. *
  657. * This adds it to the kobject hierarchy via kobject_add_ng(), adds it
  658. * to the global and sibling lists for the device, then
  659. * adds it to the other relevant subsystems of the driver model.
  660. */
  661. int device_add(struct device *dev)
  662. {
  663. struct device *parent = NULL;
  664. struct class_interface *class_intf;
  665. int error;
  666. error = pm_sleep_lock();
  667. if (error) {
  668. dev_warn(dev, "Suspicious %s during suspend\n", __FUNCTION__);
  669. dump_stack();
  670. return error;
  671. }
  672. dev = get_device(dev);
  673. if (!dev || !strlen(dev->bus_id)) {
  674. error = -EINVAL;
  675. goto Error;
  676. }
  677. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  678. parent = get_device(dev->parent);
  679. error = setup_parent(dev, parent);
  680. if (error)
  681. goto Error;
  682. /* first, register with generic layer. */
  683. error = kobject_add_ng(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
  684. if (error)
  685. goto Error;
  686. /* notify platform of device entry */
  687. if (platform_notify)
  688. platform_notify(dev);
  689. /* notify clients of device entry (new way) */
  690. if (dev->bus)
  691. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  692. BUS_NOTIFY_ADD_DEVICE, dev);
  693. error = device_create_file(dev, &uevent_attr);
  694. if (error)
  695. goto attrError;
  696. if (MAJOR(dev->devt)) {
  697. error = device_create_file(dev, &devt_attr);
  698. if (error)
  699. goto ueventattrError;
  700. }
  701. error = device_add_class_symlinks(dev);
  702. if (error)
  703. goto SymlinkError;
  704. error = device_add_attrs(dev);
  705. if (error)
  706. goto AttrsError;
  707. error = dpm_sysfs_add(dev);
  708. if (error)
  709. goto PMError;
  710. device_pm_add(dev);
  711. error = bus_add_device(dev);
  712. if (error)
  713. goto BusError;
  714. kobject_uevent(&dev->kobj, KOBJ_ADD);
  715. bus_attach_device(dev);
  716. if (parent)
  717. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  718. if (dev->class) {
  719. down(&dev->class->sem);
  720. /* tie the class to the device */
  721. list_add_tail(&dev->node, &dev->class->devices);
  722. /* notify any interfaces that the device is here */
  723. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  724. if (class_intf->add_dev)
  725. class_intf->add_dev(dev, class_intf);
  726. up(&dev->class->sem);
  727. }
  728. Done:
  729. put_device(dev);
  730. pm_sleep_unlock();
  731. return error;
  732. BusError:
  733. device_pm_remove(dev);
  734. dpm_sysfs_remove(dev);
  735. PMError:
  736. if (dev->bus)
  737. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  738. BUS_NOTIFY_DEL_DEVICE, dev);
  739. device_remove_attrs(dev);
  740. AttrsError:
  741. device_remove_class_symlinks(dev);
  742. SymlinkError:
  743. if (MAJOR(dev->devt))
  744. device_remove_file(dev, &devt_attr);
  745. ueventattrError:
  746. device_remove_file(dev, &uevent_attr);
  747. attrError:
  748. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  749. kobject_del(&dev->kobj);
  750. Error:
  751. if (parent)
  752. put_device(parent);
  753. goto Done;
  754. }
  755. /**
  756. * device_register - register a device with the system.
  757. * @dev: pointer to the device structure
  758. *
  759. * This happens in two clean steps - initialize the device
  760. * and add it to the system. The two steps can be called
  761. * separately, but this is the easiest and most common.
  762. * I.e. you should only call the two helpers separately if
  763. * have a clearly defined need to use and refcount the device
  764. * before it is added to the hierarchy.
  765. */
  766. int device_register(struct device *dev)
  767. {
  768. device_initialize(dev);
  769. return device_add(dev);
  770. }
  771. /**
  772. * get_device - increment reference count for device.
  773. * @dev: device.
  774. *
  775. * This simply forwards the call to kobject_get(), though
  776. * we do take care to provide for the case that we get a NULL
  777. * pointer passed in.
  778. */
  779. struct device * get_device(struct device * dev)
  780. {
  781. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  782. }
  783. /**
  784. * put_device - decrement reference count.
  785. * @dev: device in question.
  786. */
  787. void put_device(struct device * dev)
  788. {
  789. if (dev)
  790. kobject_put(&dev->kobj);
  791. }
  792. /**
  793. * device_del - delete device from system.
  794. * @dev: device.
  795. *
  796. * This is the first part of the device unregistration
  797. * sequence. This removes the device from the lists we control
  798. * from here, has it removed from the other driver model
  799. * subsystems it was added to in device_add(), and removes it
  800. * from the kobject hierarchy.
  801. *
  802. * NOTE: this should be called manually _iff_ device_add() was
  803. * also called manually.
  804. */
  805. void device_del(struct device * dev)
  806. {
  807. struct device * parent = dev->parent;
  808. struct class_interface *class_intf;
  809. device_pm_remove(dev);
  810. if (parent)
  811. klist_del(&dev->knode_parent);
  812. if (MAJOR(dev->devt))
  813. device_remove_file(dev, &devt_attr);
  814. if (dev->class) {
  815. device_remove_class_symlinks(dev);
  816. down(&dev->class->sem);
  817. /* notify any interfaces that the device is now gone */
  818. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  819. if (class_intf->remove_dev)
  820. class_intf->remove_dev(dev, class_intf);
  821. /* remove the device from the class list */
  822. list_del_init(&dev->node);
  823. up(&dev->class->sem);
  824. }
  825. device_remove_file(dev, &uevent_attr);
  826. device_remove_attrs(dev);
  827. bus_remove_device(dev);
  828. /*
  829. * Some platform devices are driven without driver attached
  830. * and managed resources may have been acquired. Make sure
  831. * all resources are released.
  832. */
  833. devres_release_all(dev);
  834. /* Notify the platform of the removal, in case they
  835. * need to do anything...
  836. */
  837. if (platform_notify_remove)
  838. platform_notify_remove(dev);
  839. if (dev->bus)
  840. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  841. BUS_NOTIFY_DEL_DEVICE, dev);
  842. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  843. cleanup_device_parent(dev);
  844. kobject_del(&dev->kobj);
  845. put_device(parent);
  846. }
  847. /**
  848. * device_unregister - unregister device from system.
  849. * @dev: device going away.
  850. *
  851. * We do this in two parts, like we do device_register(). First,
  852. * we remove it from all the subsystems with device_del(), then
  853. * we decrement the reference count via put_device(). If that
  854. * is the final reference count, the device will be cleaned up
  855. * via device_release() above. Otherwise, the structure will
  856. * stick around until the final reference to the device is dropped.
  857. */
  858. void device_unregister(struct device * dev)
  859. {
  860. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  861. device_del(dev);
  862. put_device(dev);
  863. }
  864. static struct device * next_device(struct klist_iter * i)
  865. {
  866. struct klist_node * n = klist_next(i);
  867. return n ? container_of(n, struct device, knode_parent) : NULL;
  868. }
  869. /**
  870. * device_for_each_child - device child iterator.
  871. * @parent: parent struct device.
  872. * @data: data for the callback.
  873. * @fn: function to be called for each device.
  874. *
  875. * Iterate over @parent's child devices, and call @fn for each,
  876. * passing it @data.
  877. *
  878. * We check the return of @fn each time. If it returns anything
  879. * other than 0, we break out and return that value.
  880. */
  881. int device_for_each_child(struct device * parent, void * data,
  882. int (*fn)(struct device *, void *))
  883. {
  884. struct klist_iter i;
  885. struct device * child;
  886. int error = 0;
  887. klist_iter_init(&parent->klist_children, &i);
  888. while ((child = next_device(&i)) && !error)
  889. error = fn(child, data);
  890. klist_iter_exit(&i);
  891. return error;
  892. }
  893. /**
  894. * device_find_child - device iterator for locating a particular device.
  895. * @parent: parent struct device
  896. * @data: Data to pass to match function
  897. * @match: Callback function to check device
  898. *
  899. * This is similar to the device_for_each_child() function above, but it
  900. * returns a reference to a device that is 'found' for later use, as
  901. * determined by the @match callback.
  902. *
  903. * The callback should return 0 if the device doesn't match and non-zero
  904. * if it does. If the callback returns non-zero and a reference to the
  905. * current device can be obtained, this function will return to the caller
  906. * and not iterate over any more devices.
  907. */
  908. struct device * device_find_child(struct device *parent, void *data,
  909. int (*match)(struct device *, void *))
  910. {
  911. struct klist_iter i;
  912. struct device *child;
  913. if (!parent)
  914. return NULL;
  915. klist_iter_init(&parent->klist_children, &i);
  916. while ((child = next_device(&i)))
  917. if (match(child, data) && get_device(child))
  918. break;
  919. klist_iter_exit(&i);
  920. return child;
  921. }
  922. int __init devices_init(void)
  923. {
  924. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  925. if (!devices_kset)
  926. return -ENOMEM;
  927. return 0;
  928. }
  929. EXPORT_SYMBOL_GPL(device_for_each_child);
  930. EXPORT_SYMBOL_GPL(device_find_child);
  931. EXPORT_SYMBOL_GPL(device_initialize);
  932. EXPORT_SYMBOL_GPL(device_add);
  933. EXPORT_SYMBOL_GPL(device_register);
  934. EXPORT_SYMBOL_GPL(device_del);
  935. EXPORT_SYMBOL_GPL(device_unregister);
  936. EXPORT_SYMBOL_GPL(get_device);
  937. EXPORT_SYMBOL_GPL(put_device);
  938. EXPORT_SYMBOL_GPL(device_create_file);
  939. EXPORT_SYMBOL_GPL(device_remove_file);
  940. static void device_create_release(struct device *dev)
  941. {
  942. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  943. kfree(dev);
  944. }
  945. /**
  946. * device_create - creates a device and registers it with sysfs
  947. * @class: pointer to the struct class that this device should be registered to
  948. * @parent: pointer to the parent struct device of this new device, if any
  949. * @devt: the dev_t for the char device to be added
  950. * @fmt: string for the device's name
  951. *
  952. * This function can be used by char device classes. A struct device
  953. * will be created in sysfs, registered to the specified class.
  954. *
  955. * A "dev" file will be created, showing the dev_t for the device, if
  956. * the dev_t is not 0,0.
  957. * If a pointer to a parent struct device is passed in, the newly created
  958. * struct device will be a child of that device in sysfs.
  959. * The pointer to the struct device will be returned from the call.
  960. * Any further sysfs files that might be required can be created using this
  961. * pointer.
  962. *
  963. * Note: the struct class passed to this function must have previously
  964. * been created with a call to class_create().
  965. */
  966. struct device *device_create(struct class *class, struct device *parent,
  967. dev_t devt, const char *fmt, ...)
  968. {
  969. va_list args;
  970. struct device *dev = NULL;
  971. int retval = -ENODEV;
  972. if (class == NULL || IS_ERR(class))
  973. goto error;
  974. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  975. if (!dev) {
  976. retval = -ENOMEM;
  977. goto error;
  978. }
  979. dev->devt = devt;
  980. dev->class = class;
  981. dev->parent = parent;
  982. dev->release = device_create_release;
  983. va_start(args, fmt);
  984. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  985. va_end(args);
  986. retval = device_register(dev);
  987. if (retval)
  988. goto error;
  989. return dev;
  990. error:
  991. kfree(dev);
  992. return ERR_PTR(retval);
  993. }
  994. EXPORT_SYMBOL_GPL(device_create);
  995. /**
  996. * find_device - finds a device that was created with device_create()
  997. * @class: pointer to the struct class that this device was registered with
  998. * @devt: the dev_t of the device that was previously registered
  999. */
  1000. static struct device *find_device(struct class *class, dev_t devt)
  1001. {
  1002. struct device *dev = NULL;
  1003. struct device *dev_tmp;
  1004. down(&class->sem);
  1005. list_for_each_entry(dev_tmp, &class->devices, node) {
  1006. if (dev_tmp->devt == devt) {
  1007. dev = dev_tmp;
  1008. break;
  1009. }
  1010. }
  1011. up(&class->sem);
  1012. return dev;
  1013. }
  1014. /**
  1015. * device_destroy - removes a device that was created with device_create()
  1016. * @class: pointer to the struct class that this device was registered with
  1017. * @devt: the dev_t of the device that was previously registered
  1018. *
  1019. * This call unregisters and cleans up a device that was created with a
  1020. * call to device_create().
  1021. */
  1022. void device_destroy(struct class *class, dev_t devt)
  1023. {
  1024. struct device *dev;
  1025. dev = find_device(class, devt);
  1026. if (dev)
  1027. device_unregister(dev);
  1028. }
  1029. EXPORT_SYMBOL_GPL(device_destroy);
  1030. #ifdef CONFIG_PM_SLEEP
  1031. /**
  1032. * destroy_suspended_device - asks the PM core to remove a suspended device
  1033. * @class: pointer to the struct class that this device was registered with
  1034. * @devt: the dev_t of the device that was previously registered
  1035. *
  1036. * This call notifies the PM core of the necessity to unregister a suspended
  1037. * device created with a call to device_create() (devices cannot be
  1038. * unregistered directly while suspended, since the PM core holds their
  1039. * semaphores at that time).
  1040. *
  1041. * It can only be called within the scope of a system sleep transition. In
  1042. * practice this means it has to be directly or indirectly invoked either by
  1043. * a suspend or resume method, or by the PM core (e.g. via
  1044. * disable_nonboot_cpus() or enable_nonboot_cpus()).
  1045. */
  1046. void destroy_suspended_device(struct class *class, dev_t devt)
  1047. {
  1048. struct device *dev;
  1049. dev = find_device(class, devt);
  1050. if (dev)
  1051. device_pm_schedule_removal(dev);
  1052. }
  1053. EXPORT_SYMBOL_GPL(destroy_suspended_device);
  1054. #endif /* CONFIG_PM_SLEEP */
  1055. /**
  1056. * device_rename - renames a device
  1057. * @dev: the pointer to the struct device to be renamed
  1058. * @new_name: the new name of the device
  1059. */
  1060. int device_rename(struct device *dev, char *new_name)
  1061. {
  1062. char *old_class_name = NULL;
  1063. char *new_class_name = NULL;
  1064. char *old_device_name = NULL;
  1065. int error;
  1066. dev = get_device(dev);
  1067. if (!dev)
  1068. return -EINVAL;
  1069. pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
  1070. __FUNCTION__, new_name);
  1071. #ifdef CONFIG_SYSFS_DEPRECATED
  1072. if ((dev->class) && (dev->parent))
  1073. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1074. #endif
  1075. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1076. if (!old_device_name) {
  1077. error = -ENOMEM;
  1078. goto out;
  1079. }
  1080. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1081. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1082. error = kobject_rename(&dev->kobj, new_name);
  1083. if (error) {
  1084. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1085. goto out;
  1086. }
  1087. #ifdef CONFIG_SYSFS_DEPRECATED
  1088. if (old_class_name) {
  1089. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1090. if (new_class_name) {
  1091. error = sysfs_create_link(&dev->parent->kobj,
  1092. &dev->kobj, new_class_name);
  1093. if (error)
  1094. goto out;
  1095. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1096. }
  1097. }
  1098. #else
  1099. if (dev->class) {
  1100. sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
  1101. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  1102. dev->bus_id);
  1103. if (error) {
  1104. dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
  1105. __FUNCTION__, error);
  1106. }
  1107. }
  1108. #endif
  1109. out:
  1110. put_device(dev);
  1111. kfree(new_class_name);
  1112. kfree(old_class_name);
  1113. kfree(old_device_name);
  1114. return error;
  1115. }
  1116. EXPORT_SYMBOL_GPL(device_rename);
  1117. static int device_move_class_links(struct device *dev,
  1118. struct device *old_parent,
  1119. struct device *new_parent)
  1120. {
  1121. int error = 0;
  1122. #ifdef CONFIG_SYSFS_DEPRECATED
  1123. char *class_name;
  1124. class_name = make_class_name(dev->class->name, &dev->kobj);
  1125. if (!class_name) {
  1126. error = -ENOMEM;
  1127. goto out;
  1128. }
  1129. if (old_parent) {
  1130. sysfs_remove_link(&dev->kobj, "device");
  1131. sysfs_remove_link(&old_parent->kobj, class_name);
  1132. }
  1133. if (new_parent) {
  1134. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1135. "device");
  1136. if (error)
  1137. goto out;
  1138. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1139. class_name);
  1140. if (error)
  1141. sysfs_remove_link(&dev->kobj, "device");
  1142. }
  1143. else
  1144. error = 0;
  1145. out:
  1146. kfree(class_name);
  1147. return error;
  1148. #else
  1149. if (old_parent)
  1150. sysfs_remove_link(&dev->kobj, "device");
  1151. if (new_parent)
  1152. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1153. "device");
  1154. return error;
  1155. #endif
  1156. }
  1157. /**
  1158. * device_move - moves a device to a new parent
  1159. * @dev: the pointer to the struct device to be moved
  1160. * @new_parent: the new parent of the device (can by NULL)
  1161. */
  1162. int device_move(struct device *dev, struct device *new_parent)
  1163. {
  1164. int error;
  1165. struct device *old_parent;
  1166. struct kobject *new_parent_kobj;
  1167. dev = get_device(dev);
  1168. if (!dev)
  1169. return -EINVAL;
  1170. new_parent = get_device(new_parent);
  1171. new_parent_kobj = get_device_parent (dev, new_parent);
  1172. if (IS_ERR(new_parent_kobj)) {
  1173. error = PTR_ERR(new_parent_kobj);
  1174. put_device(new_parent);
  1175. goto out;
  1176. }
  1177. pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
  1178. __FUNCTION__, new_parent ? new_parent->bus_id : "<NULL>");
  1179. error = kobject_move(&dev->kobj, new_parent_kobj);
  1180. if (error) {
  1181. put_device(new_parent);
  1182. goto out;
  1183. }
  1184. old_parent = dev->parent;
  1185. dev->parent = new_parent;
  1186. if (old_parent)
  1187. klist_remove(&dev->knode_parent);
  1188. if (new_parent)
  1189. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1190. if (!dev->class)
  1191. goto out_put;
  1192. error = device_move_class_links(dev, old_parent, new_parent);
  1193. if (error) {
  1194. /* We ignore errors on cleanup since we're hosed anyway... */
  1195. device_move_class_links(dev, new_parent, old_parent);
  1196. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1197. if (new_parent)
  1198. klist_remove(&dev->knode_parent);
  1199. if (old_parent)
  1200. klist_add_tail(&dev->knode_parent,
  1201. &old_parent->klist_children);
  1202. }
  1203. put_device(new_parent);
  1204. goto out;
  1205. }
  1206. out_put:
  1207. put_device(old_parent);
  1208. out:
  1209. put_device(dev);
  1210. return error;
  1211. }
  1212. EXPORT_SYMBOL_GPL(device_move);
  1213. /**
  1214. * device_shutdown - call ->shutdown() on each device to shutdown.
  1215. */
  1216. void device_shutdown(void)
  1217. {
  1218. struct device * dev, *devn;
  1219. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1220. kobj.entry) {
  1221. if (dev->bus && dev->bus->shutdown) {
  1222. dev_dbg(dev, "shutdown\n");
  1223. dev->bus->shutdown(dev);
  1224. } else if (dev->driver && dev->driver->shutdown) {
  1225. dev_dbg(dev, "shutdown\n");
  1226. dev->driver->shutdown(dev);
  1227. }
  1228. }
  1229. }