core.c 37 KB

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