core.c 36 KB

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