core.c 39 KB

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