core.c 35 KB

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