core.c 38 KB

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