core.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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(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. dev->type != &part_type) {
  572. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  573. dev->bus_id);
  574. if (error)
  575. goto out_subsys;
  576. }
  577. if (dev->parent && dev->type != &part_type) {
  578. struct device *parent = dev->parent;
  579. char *class_name;
  580. /*
  581. * stacked class devices have the 'device' link
  582. * pointing to the bus device instead of the parent
  583. */
  584. while (parent->class && !parent->bus && parent->parent)
  585. parent = parent->parent;
  586. error = sysfs_create_link(&dev->kobj,
  587. &parent->kobj,
  588. "device");
  589. if (error)
  590. goto out_busid;
  591. class_name = make_class_name(dev->class->name,
  592. &dev->kobj);
  593. if (class_name)
  594. error = sysfs_create_link(&dev->parent->kobj,
  595. &dev->kobj, class_name);
  596. kfree(class_name);
  597. if (error)
  598. goto out_device;
  599. }
  600. return 0;
  601. out_device:
  602. if (dev->parent && dev->type != &part_type)
  603. sysfs_remove_link(&dev->kobj, "device");
  604. out_busid:
  605. if (dev->kobj.parent != &dev->class->subsys.kobj &&
  606. dev->type != &part_type)
  607. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  608. #else
  609. /* link in the class directory pointing to the device */
  610. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  611. dev->bus_id);
  612. if (error)
  613. goto out_subsys;
  614. if (dev->parent && dev->type != &part_type) {
  615. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  616. "device");
  617. if (error)
  618. goto out_busid;
  619. }
  620. return 0;
  621. out_busid:
  622. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  623. #endif
  624. out_subsys:
  625. sysfs_remove_link(&dev->kobj, "subsystem");
  626. out:
  627. return error;
  628. }
  629. static void device_remove_class_symlinks(struct device *dev)
  630. {
  631. if (!dev->class)
  632. return;
  633. #ifdef CONFIG_SYSFS_DEPRECATED
  634. if (dev->parent && dev->type != &part_type) {
  635. char *class_name;
  636. class_name = make_class_name(dev->class->name, &dev->kobj);
  637. if (class_name) {
  638. sysfs_remove_link(&dev->parent->kobj, class_name);
  639. kfree(class_name);
  640. }
  641. sysfs_remove_link(&dev->kobj, "device");
  642. }
  643. if (dev->kobj.parent != &dev->class->subsys.kobj &&
  644. dev->type != &part_type)
  645. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  646. #else
  647. if (dev->parent && dev->type != &part_type)
  648. sysfs_remove_link(&dev->kobj, "device");
  649. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  650. #endif
  651. sysfs_remove_link(&dev->kobj, "subsystem");
  652. }
  653. /**
  654. * device_add - add device to device hierarchy.
  655. * @dev: device.
  656. *
  657. * This is part 2 of device_register(), though may be called
  658. * separately _iff_ device_initialize() has been called separately.
  659. *
  660. * This adds it to the kobject hierarchy via kobject_add(), adds it
  661. * to the global and sibling lists for the device, then
  662. * adds it to the other relevant subsystems of the driver model.
  663. */
  664. int device_add(struct device *dev)
  665. {
  666. struct device *parent = NULL;
  667. struct class_interface *class_intf;
  668. int error;
  669. error = pm_sleep_lock();
  670. if (error) {
  671. dev_warn(dev, "Suspicious %s during suspend\n", __FUNCTION__);
  672. dump_stack();
  673. return error;
  674. }
  675. dev = get_device(dev);
  676. if (!dev || !strlen(dev->bus_id)) {
  677. error = -EINVAL;
  678. goto Error;
  679. }
  680. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  681. parent = get_device(dev->parent);
  682. error = setup_parent(dev, parent);
  683. if (error)
  684. goto Error;
  685. /* first, register with generic layer. */
  686. error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
  687. if (error)
  688. goto Error;
  689. /* notify platform of device entry */
  690. if (platform_notify)
  691. platform_notify(dev);
  692. /* notify clients of device entry (new way) */
  693. if (dev->bus)
  694. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  695. BUS_NOTIFY_ADD_DEVICE, dev);
  696. error = device_create_file(dev, &uevent_attr);
  697. if (error)
  698. goto attrError;
  699. if (MAJOR(dev->devt)) {
  700. error = device_create_file(dev, &devt_attr);
  701. if (error)
  702. goto ueventattrError;
  703. }
  704. error = device_add_class_symlinks(dev);
  705. if (error)
  706. goto SymlinkError;
  707. error = device_add_attrs(dev);
  708. if (error)
  709. goto AttrsError;
  710. error = dpm_sysfs_add(dev);
  711. if (error)
  712. goto PMError;
  713. device_pm_add(dev);
  714. error = bus_add_device(dev);
  715. if (error)
  716. goto BusError;
  717. kobject_uevent(&dev->kobj, KOBJ_ADD);
  718. bus_attach_device(dev);
  719. if (parent)
  720. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  721. if (dev->class) {
  722. down(&dev->class->sem);
  723. /* tie the class to the device */
  724. list_add_tail(&dev->node, &dev->class->devices);
  725. /* notify any interfaces that the device is here */
  726. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  727. if (class_intf->add_dev)
  728. class_intf->add_dev(dev, class_intf);
  729. up(&dev->class->sem);
  730. }
  731. Done:
  732. put_device(dev);
  733. pm_sleep_unlock();
  734. return error;
  735. BusError:
  736. device_pm_remove(dev);
  737. dpm_sysfs_remove(dev);
  738. PMError:
  739. if (dev->bus)
  740. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  741. BUS_NOTIFY_DEL_DEVICE, dev);
  742. device_remove_attrs(dev);
  743. AttrsError:
  744. device_remove_class_symlinks(dev);
  745. SymlinkError:
  746. if (MAJOR(dev->devt))
  747. device_remove_file(dev, &devt_attr);
  748. ueventattrError:
  749. device_remove_file(dev, &uevent_attr);
  750. attrError:
  751. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  752. kobject_del(&dev->kobj);
  753. Error:
  754. if (parent)
  755. put_device(parent);
  756. goto Done;
  757. }
  758. /**
  759. * device_register - register a device with the system.
  760. * @dev: pointer to the device structure
  761. *
  762. * This happens in two clean steps - initialize the device
  763. * and add it to the system. The two steps can be called
  764. * separately, but this is the easiest and most common.
  765. * I.e. you should only call the two helpers separately if
  766. * have a clearly defined need to use and refcount the device
  767. * before it is added to the hierarchy.
  768. */
  769. int device_register(struct device *dev)
  770. {
  771. device_initialize(dev);
  772. return device_add(dev);
  773. }
  774. /**
  775. * get_device - increment reference count for device.
  776. * @dev: device.
  777. *
  778. * This simply forwards the call to kobject_get(), though
  779. * we do take care to provide for the case that we get a NULL
  780. * pointer passed in.
  781. */
  782. struct device * get_device(struct device * dev)
  783. {
  784. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  785. }
  786. /**
  787. * put_device - decrement reference count.
  788. * @dev: device in question.
  789. */
  790. void put_device(struct device * dev)
  791. {
  792. /* might_sleep(); */
  793. if (dev)
  794. kobject_put(&dev->kobj);
  795. }
  796. /**
  797. * device_del - delete device from system.
  798. * @dev: device.
  799. *
  800. * This is the first part of the device unregistration
  801. * sequence. This removes the device from the lists we control
  802. * from here, has it removed from the other driver model
  803. * subsystems it was added to in device_add(), and removes it
  804. * from the kobject hierarchy.
  805. *
  806. * NOTE: this should be called manually _iff_ device_add() was
  807. * also called manually.
  808. */
  809. void device_del(struct device * dev)
  810. {
  811. struct device * parent = dev->parent;
  812. struct class_interface *class_intf;
  813. device_pm_remove(dev);
  814. if (parent)
  815. klist_del(&dev->knode_parent);
  816. if (MAJOR(dev->devt))
  817. device_remove_file(dev, &devt_attr);
  818. if (dev->class) {
  819. device_remove_class_symlinks(dev);
  820. down(&dev->class->sem);
  821. /* notify any interfaces that the device is now gone */
  822. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  823. if (class_intf->remove_dev)
  824. class_intf->remove_dev(dev, class_intf);
  825. /* remove the device from the class list */
  826. list_del_init(&dev->node);
  827. up(&dev->class->sem);
  828. }
  829. device_remove_file(dev, &uevent_attr);
  830. device_remove_attrs(dev);
  831. bus_remove_device(dev);
  832. /*
  833. * Some platform devices are driven without driver attached
  834. * and managed resources may have been acquired. Make sure
  835. * all resources are released.
  836. */
  837. devres_release_all(dev);
  838. /* Notify the platform of the removal, in case they
  839. * need to do anything...
  840. */
  841. if (platform_notify_remove)
  842. platform_notify_remove(dev);
  843. if (dev->bus)
  844. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  845. BUS_NOTIFY_DEL_DEVICE, dev);
  846. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  847. cleanup_device_parent(dev);
  848. kobject_del(&dev->kobj);
  849. put_device(parent);
  850. }
  851. /**
  852. * device_unregister - unregister device from system.
  853. * @dev: device going away.
  854. *
  855. * We do this in two parts, like we do device_register(). First,
  856. * we remove it from all the subsystems with device_del(), then
  857. * we decrement the reference count via put_device(). If that
  858. * is the final reference count, the device will be cleaned up
  859. * via device_release() above. Otherwise, the structure will
  860. * stick around until the final reference to the device is dropped.
  861. */
  862. void device_unregister(struct device * dev)
  863. {
  864. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  865. device_del(dev);
  866. put_device(dev);
  867. }
  868. static struct device * next_device(struct klist_iter * i)
  869. {
  870. struct klist_node * n = klist_next(i);
  871. return n ? container_of(n, struct device, knode_parent) : NULL;
  872. }
  873. /**
  874. * device_for_each_child - device child iterator.
  875. * @parent: parent struct device.
  876. * @data: data for the callback.
  877. * @fn: function to be called for each device.
  878. *
  879. * Iterate over @parent's child devices, and call @fn for each,
  880. * passing it @data.
  881. *
  882. * We check the return of @fn each time. If it returns anything
  883. * other than 0, we break out and return that value.
  884. */
  885. int device_for_each_child(struct device * parent, void * data,
  886. int (*fn)(struct device *, void *))
  887. {
  888. struct klist_iter i;
  889. struct device * child;
  890. int error = 0;
  891. klist_iter_init(&parent->klist_children, &i);
  892. while ((child = next_device(&i)) && !error)
  893. error = fn(child, data);
  894. klist_iter_exit(&i);
  895. return error;
  896. }
  897. /**
  898. * device_find_child - device iterator for locating a particular device.
  899. * @parent: parent struct device
  900. * @data: Data to pass to match function
  901. * @match: Callback function to check device
  902. *
  903. * This is similar to the device_for_each_child() function above, but it
  904. * returns a reference to a device that is 'found' for later use, as
  905. * determined by the @match callback.
  906. *
  907. * The callback should return 0 if the device doesn't match and non-zero
  908. * if it does. If the callback returns non-zero and a reference to the
  909. * current device can be obtained, this function will return to the caller
  910. * and not iterate over any more devices.
  911. */
  912. struct device * device_find_child(struct device *parent, void *data,
  913. int (*match)(struct device *, void *))
  914. {
  915. struct klist_iter i;
  916. struct device *child;
  917. if (!parent)
  918. return NULL;
  919. klist_iter_init(&parent->klist_children, &i);
  920. while ((child = next_device(&i)))
  921. if (match(child, data) && get_device(child))
  922. break;
  923. klist_iter_exit(&i);
  924. return child;
  925. }
  926. int __init devices_init(void)
  927. {
  928. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  929. if (!devices_kset)
  930. return -ENOMEM;
  931. return 0;
  932. }
  933. EXPORT_SYMBOL_GPL(device_for_each_child);
  934. EXPORT_SYMBOL_GPL(device_find_child);
  935. EXPORT_SYMBOL_GPL(device_initialize);
  936. EXPORT_SYMBOL_GPL(device_add);
  937. EXPORT_SYMBOL_GPL(device_register);
  938. EXPORT_SYMBOL_GPL(device_del);
  939. EXPORT_SYMBOL_GPL(device_unregister);
  940. EXPORT_SYMBOL_GPL(get_device);
  941. EXPORT_SYMBOL_GPL(put_device);
  942. EXPORT_SYMBOL_GPL(device_create_file);
  943. EXPORT_SYMBOL_GPL(device_remove_file);
  944. static void device_create_release(struct device *dev)
  945. {
  946. pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
  947. kfree(dev);
  948. }
  949. /**
  950. * device_create - creates a device and registers it with sysfs
  951. * @class: pointer to the struct class that this device should be registered to
  952. * @parent: pointer to the parent struct device of this new device, if any
  953. * @devt: the dev_t for the char device to be added
  954. * @fmt: string for the device's name
  955. *
  956. * This function can be used by char device classes. A struct device
  957. * will be created in sysfs, registered to the specified class.
  958. *
  959. * A "dev" file will be created, showing the dev_t for the device, if
  960. * the dev_t is not 0,0.
  961. * If a pointer to a parent struct device is passed in, the newly created
  962. * struct device will be a child of that device in sysfs.
  963. * The pointer to the struct device will be returned from the call.
  964. * Any further sysfs files that might be required can be created using this
  965. * pointer.
  966. *
  967. * Note: the struct class passed to this function must have previously
  968. * been created with a call to class_create().
  969. */
  970. struct device *device_create(struct class *class, struct device *parent,
  971. dev_t devt, const char *fmt, ...)
  972. {
  973. va_list args;
  974. struct device *dev = NULL;
  975. int retval = -ENODEV;
  976. if (class == NULL || IS_ERR(class))
  977. goto error;
  978. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  979. if (!dev) {
  980. retval = -ENOMEM;
  981. goto error;
  982. }
  983. dev->devt = devt;
  984. dev->class = class;
  985. dev->parent = parent;
  986. dev->release = device_create_release;
  987. va_start(args, fmt);
  988. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  989. va_end(args);
  990. retval = device_register(dev);
  991. if (retval)
  992. goto error;
  993. return dev;
  994. error:
  995. kfree(dev);
  996. return ERR_PTR(retval);
  997. }
  998. EXPORT_SYMBOL_GPL(device_create);
  999. /**
  1000. * find_device - finds a device that was created with device_create()
  1001. * @class: pointer to the struct class that this device was registered with
  1002. * @devt: the dev_t of the device that was previously registered
  1003. */
  1004. static struct device *find_device(struct class *class, dev_t devt)
  1005. {
  1006. struct device *dev = NULL;
  1007. struct device *dev_tmp;
  1008. down(&class->sem);
  1009. list_for_each_entry(dev_tmp, &class->devices, node) {
  1010. if (dev_tmp->devt == devt) {
  1011. dev = dev_tmp;
  1012. break;
  1013. }
  1014. }
  1015. up(&class->sem);
  1016. return dev;
  1017. }
  1018. /**
  1019. * device_destroy - removes a device that was created with device_create()
  1020. * @class: pointer to the struct class that this device was registered with
  1021. * @devt: the dev_t of the device that was previously registered
  1022. *
  1023. * This call unregisters and cleans up a device that was created with a
  1024. * call to device_create().
  1025. */
  1026. void device_destroy(struct class *class, dev_t devt)
  1027. {
  1028. struct device *dev;
  1029. dev = find_device(class, devt);
  1030. if (dev)
  1031. device_unregister(dev);
  1032. }
  1033. EXPORT_SYMBOL_GPL(device_destroy);
  1034. #ifdef CONFIG_PM_SLEEP
  1035. /**
  1036. * destroy_suspended_device - asks the PM core to remove a suspended device
  1037. * @class: pointer to the struct class that this device was registered with
  1038. * @devt: the dev_t of the device that was previously registered
  1039. *
  1040. * This call notifies the PM core of the necessity to unregister a suspended
  1041. * device created with a call to device_create() (devices cannot be
  1042. * unregistered directly while suspended, since the PM core holds their
  1043. * semaphores at that time).
  1044. *
  1045. * It can only be called within the scope of a system sleep transition. In
  1046. * practice this means it has to be directly or indirectly invoked either by
  1047. * a suspend or resume method, or by the PM core (e.g. via
  1048. * disable_nonboot_cpus() or enable_nonboot_cpus()).
  1049. */
  1050. void destroy_suspended_device(struct class *class, dev_t devt)
  1051. {
  1052. struct device *dev;
  1053. dev = find_device(class, devt);
  1054. if (dev)
  1055. device_pm_schedule_removal(dev);
  1056. }
  1057. EXPORT_SYMBOL_GPL(destroy_suspended_device);
  1058. #endif /* CONFIG_PM_SLEEP */
  1059. /**
  1060. * device_rename - renames a device
  1061. * @dev: the pointer to the struct device to be renamed
  1062. * @new_name: the new name of the device
  1063. */
  1064. int device_rename(struct device *dev, char *new_name)
  1065. {
  1066. char *old_class_name = NULL;
  1067. char *new_class_name = NULL;
  1068. char *old_device_name = NULL;
  1069. int error;
  1070. dev = get_device(dev);
  1071. if (!dev)
  1072. return -EINVAL;
  1073. pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
  1074. __FUNCTION__, new_name);
  1075. #ifdef CONFIG_SYSFS_DEPRECATED
  1076. if ((dev->class) && (dev->parent))
  1077. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1078. #endif
  1079. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1080. if (!old_device_name) {
  1081. error = -ENOMEM;
  1082. goto out;
  1083. }
  1084. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1085. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1086. error = kobject_rename(&dev->kobj, new_name);
  1087. if (error) {
  1088. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1089. goto out;
  1090. }
  1091. #ifdef CONFIG_SYSFS_DEPRECATED
  1092. if (old_class_name) {
  1093. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1094. if (new_class_name) {
  1095. error = sysfs_create_link(&dev->parent->kobj,
  1096. &dev->kobj, new_class_name);
  1097. if (error)
  1098. goto out;
  1099. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1100. }
  1101. }
  1102. #else
  1103. if (dev->class) {
  1104. sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
  1105. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  1106. dev->bus_id);
  1107. if (error) {
  1108. dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
  1109. __FUNCTION__, error);
  1110. }
  1111. }
  1112. #endif
  1113. out:
  1114. put_device(dev);
  1115. kfree(new_class_name);
  1116. kfree(old_class_name);
  1117. kfree(old_device_name);
  1118. return error;
  1119. }
  1120. EXPORT_SYMBOL_GPL(device_rename);
  1121. static int device_move_class_links(struct device *dev,
  1122. struct device *old_parent,
  1123. struct device *new_parent)
  1124. {
  1125. int error = 0;
  1126. #ifdef CONFIG_SYSFS_DEPRECATED
  1127. char *class_name;
  1128. class_name = make_class_name(dev->class->name, &dev->kobj);
  1129. if (!class_name) {
  1130. error = -ENOMEM;
  1131. goto out;
  1132. }
  1133. if (old_parent) {
  1134. sysfs_remove_link(&dev->kobj, "device");
  1135. sysfs_remove_link(&old_parent->kobj, class_name);
  1136. }
  1137. if (new_parent) {
  1138. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1139. "device");
  1140. if (error)
  1141. goto out;
  1142. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1143. class_name);
  1144. if (error)
  1145. sysfs_remove_link(&dev->kobj, "device");
  1146. }
  1147. else
  1148. error = 0;
  1149. out:
  1150. kfree(class_name);
  1151. return error;
  1152. #else
  1153. if (old_parent)
  1154. sysfs_remove_link(&dev->kobj, "device");
  1155. if (new_parent)
  1156. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1157. "device");
  1158. return error;
  1159. #endif
  1160. }
  1161. /**
  1162. * device_move - moves a device to a new parent
  1163. * @dev: the pointer to the struct device to be moved
  1164. * @new_parent: the new parent of the device (can by NULL)
  1165. */
  1166. int device_move(struct device *dev, struct device *new_parent)
  1167. {
  1168. int error;
  1169. struct device *old_parent;
  1170. struct kobject *new_parent_kobj;
  1171. dev = get_device(dev);
  1172. if (!dev)
  1173. return -EINVAL;
  1174. new_parent = get_device(new_parent);
  1175. new_parent_kobj = get_device_parent (dev, new_parent);
  1176. if (IS_ERR(new_parent_kobj)) {
  1177. error = PTR_ERR(new_parent_kobj);
  1178. put_device(new_parent);
  1179. goto out;
  1180. }
  1181. pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
  1182. __FUNCTION__, new_parent ? new_parent->bus_id : "<NULL>");
  1183. error = kobject_move(&dev->kobj, new_parent_kobj);
  1184. if (error) {
  1185. put_device(new_parent);
  1186. goto out;
  1187. }
  1188. old_parent = dev->parent;
  1189. dev->parent = new_parent;
  1190. if (old_parent)
  1191. klist_remove(&dev->knode_parent);
  1192. if (new_parent)
  1193. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1194. if (!dev->class)
  1195. goto out_put;
  1196. error = device_move_class_links(dev, old_parent, new_parent);
  1197. if (error) {
  1198. /* We ignore errors on cleanup since we're hosed anyway... */
  1199. device_move_class_links(dev, new_parent, old_parent);
  1200. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1201. if (new_parent)
  1202. klist_remove(&dev->knode_parent);
  1203. if (old_parent)
  1204. klist_add_tail(&dev->knode_parent,
  1205. &old_parent->klist_children);
  1206. }
  1207. put_device(new_parent);
  1208. goto out;
  1209. }
  1210. out_put:
  1211. put_device(old_parent);
  1212. out:
  1213. put_device(dev);
  1214. return error;
  1215. }
  1216. EXPORT_SYMBOL_GPL(device_move);
  1217. /**
  1218. * device_shutdown - call ->shutdown() on each device to shutdown.
  1219. */
  1220. void device_shutdown(void)
  1221. {
  1222. struct device * dev, *devn;
  1223. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1224. kobj.entry) {
  1225. if (dev->bus && dev->bus->shutdown) {
  1226. dev_dbg(dev, "shutdown\n");
  1227. dev->bus->shutdown(dev);
  1228. } else if (dev->driver && dev->driver->shutdown) {
  1229. dev_dbg(dev, "shutdown\n");
  1230. dev->driver->shutdown(dev);
  1231. }
  1232. }
  1233. }