core.c 38 KB

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