core.c 35 KB

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