core.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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(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_LIST_HEAD(&dev->node);
  461. init_MUTEX(&dev->sem);
  462. spin_lock_init(&dev->devres_lock);
  463. INIT_LIST_HEAD(&dev->devres_head);
  464. device_init_wakeup(dev, 0);
  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;
  724. dev = get_device(dev);
  725. if (!dev || !strlen(dev->bus_id)) {
  726. error = -EINVAL;
  727. goto Done;
  728. }
  729. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  730. parent = get_device(dev->parent);
  731. setup_parent(dev, parent);
  732. /* use parent numa_node */
  733. if (parent)
  734. set_dev_node(dev, dev_to_node(parent));
  735. /* first, register with generic layer. */
  736. error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
  737. if (error)
  738. goto Error;
  739. /* notify platform of device entry */
  740. if (platform_notify)
  741. platform_notify(dev);
  742. /* notify clients of device entry (new way) */
  743. if (dev->bus)
  744. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  745. BUS_NOTIFY_ADD_DEVICE, dev);
  746. error = device_create_file(dev, &uevent_attr);
  747. if (error)
  748. goto attrError;
  749. if (MAJOR(dev->devt)) {
  750. error = device_create_file(dev, &devt_attr);
  751. if (error)
  752. goto ueventattrError;
  753. error = device_create_sys_dev_entry(dev);
  754. if (error)
  755. goto devtattrError;
  756. }
  757. error = device_add_class_symlinks(dev);
  758. if (error)
  759. goto SymlinkError;
  760. error = device_add_attrs(dev);
  761. if (error)
  762. goto AttrsError;
  763. error = bus_add_device(dev);
  764. if (error)
  765. goto BusError;
  766. error = device_pm_add(dev);
  767. if (error)
  768. goto PMError;
  769. kobject_uevent(&dev->kobj, KOBJ_ADD);
  770. bus_attach_device(dev);
  771. if (parent)
  772. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  773. if (dev->class) {
  774. mutex_lock(&dev->class->p->class_mutex);
  775. /* tie the class to the device */
  776. list_add_tail(&dev->node, &dev->class->p->class_devices);
  777. /* notify any interfaces that the device is here */
  778. list_for_each_entry(class_intf,
  779. &dev->class->p->class_interfaces, node)
  780. if (class_intf->add_dev)
  781. class_intf->add_dev(dev, class_intf);
  782. mutex_unlock(&dev->class->p->class_mutex);
  783. }
  784. Done:
  785. put_device(dev);
  786. return error;
  787. PMError:
  788. bus_remove_device(dev);
  789. BusError:
  790. if (dev->bus)
  791. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  792. BUS_NOTIFY_DEL_DEVICE, dev);
  793. device_remove_attrs(dev);
  794. AttrsError:
  795. device_remove_class_symlinks(dev);
  796. SymlinkError:
  797. if (MAJOR(dev->devt))
  798. device_remove_sys_dev_entry(dev);
  799. devtattrError:
  800. if (MAJOR(dev->devt))
  801. device_remove_file(dev, &devt_attr);
  802. ueventattrError:
  803. device_remove_file(dev, &uevent_attr);
  804. attrError:
  805. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  806. kobject_del(&dev->kobj);
  807. Error:
  808. cleanup_device_parent(dev);
  809. if (parent)
  810. put_device(parent);
  811. goto Done;
  812. }
  813. /**
  814. * device_register - register a device with the system.
  815. * @dev: pointer to the device structure
  816. *
  817. * This happens in two clean steps - initialize the device
  818. * and add it to the system. The two steps can be called
  819. * separately, but this is the easiest and most common.
  820. * I.e. you should only call the two helpers separately if
  821. * have a clearly defined need to use and refcount the device
  822. * before it is added to the hierarchy.
  823. */
  824. int device_register(struct device *dev)
  825. {
  826. device_initialize(dev);
  827. return device_add(dev);
  828. }
  829. /**
  830. * get_device - increment reference count for device.
  831. * @dev: device.
  832. *
  833. * This simply forwards the call to kobject_get(), though
  834. * we do take care to provide for the case that we get a NULL
  835. * pointer passed in.
  836. */
  837. struct device *get_device(struct device *dev)
  838. {
  839. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  840. }
  841. /**
  842. * put_device - decrement reference count.
  843. * @dev: device in question.
  844. */
  845. void put_device(struct device *dev)
  846. {
  847. /* might_sleep(); */
  848. if (dev)
  849. kobject_put(&dev->kobj);
  850. }
  851. /**
  852. * device_del - delete device from system.
  853. * @dev: device.
  854. *
  855. * This is the first part of the device unregistration
  856. * sequence. This removes the device from the lists we control
  857. * from here, has it removed from the other driver model
  858. * subsystems it was added to in device_add(), and removes it
  859. * from the kobject hierarchy.
  860. *
  861. * NOTE: this should be called manually _iff_ device_add() was
  862. * also called manually.
  863. */
  864. void device_del(struct device *dev)
  865. {
  866. struct device *parent = dev->parent;
  867. struct class_interface *class_intf;
  868. device_pm_remove(dev);
  869. if (parent)
  870. klist_del(&dev->knode_parent);
  871. if (MAJOR(dev->devt)) {
  872. device_remove_sys_dev_entry(dev);
  873. device_remove_file(dev, &devt_attr);
  874. }
  875. if (dev->class) {
  876. device_remove_class_symlinks(dev);
  877. mutex_lock(&dev->class->p->class_mutex);
  878. /* notify any interfaces that the device is now gone */
  879. list_for_each_entry(class_intf,
  880. &dev->class->p->class_interfaces, node)
  881. if (class_intf->remove_dev)
  882. class_intf->remove_dev(dev, class_intf);
  883. /* remove the device from the class list */
  884. list_del_init(&dev->node);
  885. mutex_unlock(&dev->class->p->class_mutex);
  886. }
  887. device_remove_file(dev, &uevent_attr);
  888. device_remove_attrs(dev);
  889. bus_remove_device(dev);
  890. /*
  891. * Some platform devices are driven without driver attached
  892. * and managed resources may have been acquired. Make sure
  893. * all resources are released.
  894. */
  895. devres_release_all(dev);
  896. /* Notify the platform of the removal, in case they
  897. * need to do anything...
  898. */
  899. if (platform_notify_remove)
  900. platform_notify_remove(dev);
  901. if (dev->bus)
  902. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  903. BUS_NOTIFY_DEL_DEVICE, dev);
  904. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  905. cleanup_device_parent(dev);
  906. kobject_del(&dev->kobj);
  907. put_device(parent);
  908. }
  909. /**
  910. * device_unregister - unregister device from system.
  911. * @dev: device going away.
  912. *
  913. * We do this in two parts, like we do device_register(). First,
  914. * we remove it from all the subsystems with device_del(), then
  915. * we decrement the reference count via put_device(). If that
  916. * is the final reference count, the device will be cleaned up
  917. * via device_release() above. Otherwise, the structure will
  918. * stick around until the final reference to the device is dropped.
  919. */
  920. void device_unregister(struct device *dev)
  921. {
  922. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  923. device_del(dev);
  924. put_device(dev);
  925. }
  926. static struct device *next_device(struct klist_iter *i)
  927. {
  928. struct klist_node *n = klist_next(i);
  929. return n ? container_of(n, struct device, knode_parent) : NULL;
  930. }
  931. /**
  932. * device_for_each_child - device child iterator.
  933. * @parent: parent struct device.
  934. * @data: data for the callback.
  935. * @fn: function to be called for each device.
  936. *
  937. * Iterate over @parent's child devices, and call @fn for each,
  938. * passing it @data.
  939. *
  940. * We check the return of @fn each time. If it returns anything
  941. * other than 0, we break out and return that value.
  942. */
  943. int device_for_each_child(struct device *parent, void *data,
  944. int (*fn)(struct device *dev, void *data))
  945. {
  946. struct klist_iter i;
  947. struct device *child;
  948. int error = 0;
  949. klist_iter_init(&parent->klist_children, &i);
  950. while ((child = next_device(&i)) && !error)
  951. error = fn(child, data);
  952. klist_iter_exit(&i);
  953. return error;
  954. }
  955. /**
  956. * device_find_child - device iterator for locating a particular device.
  957. * @parent: parent struct device
  958. * @data: Data to pass to match function
  959. * @match: Callback function to check device
  960. *
  961. * This is similar to the device_for_each_child() function above, but it
  962. * returns a reference to a device that is 'found' for later use, as
  963. * determined by the @match callback.
  964. *
  965. * The callback should return 0 if the device doesn't match and non-zero
  966. * if it does. If the callback returns non-zero and a reference to the
  967. * current device can be obtained, this function will return to the caller
  968. * and not iterate over any more devices.
  969. */
  970. struct device *device_find_child(struct device *parent, void *data,
  971. int (*match)(struct device *dev, void *data))
  972. {
  973. struct klist_iter i;
  974. struct device *child;
  975. if (!parent)
  976. return NULL;
  977. klist_iter_init(&parent->klist_children, &i);
  978. while ((child = next_device(&i)))
  979. if (match(child, data) && get_device(child))
  980. break;
  981. klist_iter_exit(&i);
  982. return child;
  983. }
  984. int __init devices_init(void)
  985. {
  986. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  987. if (!devices_kset)
  988. return -ENOMEM;
  989. dev_kobj = kobject_create_and_add("dev", NULL);
  990. if (!dev_kobj)
  991. goto dev_kobj_err;
  992. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  993. if (!sysfs_dev_block_kobj)
  994. goto block_kobj_err;
  995. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  996. if (!sysfs_dev_char_kobj)
  997. goto char_kobj_err;
  998. return 0;
  999. char_kobj_err:
  1000. kobject_put(sysfs_dev_block_kobj);
  1001. block_kobj_err:
  1002. kobject_put(dev_kobj);
  1003. dev_kobj_err:
  1004. kset_unregister(devices_kset);
  1005. return -ENOMEM;
  1006. }
  1007. EXPORT_SYMBOL_GPL(device_for_each_child);
  1008. EXPORT_SYMBOL_GPL(device_find_child);
  1009. EXPORT_SYMBOL_GPL(device_initialize);
  1010. EXPORT_SYMBOL_GPL(device_add);
  1011. EXPORT_SYMBOL_GPL(device_register);
  1012. EXPORT_SYMBOL_GPL(device_del);
  1013. EXPORT_SYMBOL_GPL(device_unregister);
  1014. EXPORT_SYMBOL_GPL(get_device);
  1015. EXPORT_SYMBOL_GPL(put_device);
  1016. EXPORT_SYMBOL_GPL(device_create_file);
  1017. EXPORT_SYMBOL_GPL(device_remove_file);
  1018. static void device_create_release(struct device *dev)
  1019. {
  1020. pr_debug("device: '%s': %s\n", dev->bus_id, __func__);
  1021. kfree(dev);
  1022. }
  1023. /**
  1024. * device_create_vargs - creates a device and registers it with sysfs
  1025. * @class: pointer to the struct class that this device should be registered to
  1026. * @parent: pointer to the parent struct device of this new device, if any
  1027. * @devt: the dev_t for the char device to be added
  1028. * @drvdata: the data to be added to the device for callbacks
  1029. * @fmt: string for the device's name
  1030. * @args: va_list for the device's name
  1031. *
  1032. * This function can be used by char device classes. A struct device
  1033. * will be created in sysfs, registered to the specified class.
  1034. *
  1035. * A "dev" file will be created, showing the dev_t for the device, if
  1036. * the dev_t is not 0,0.
  1037. * If a pointer to a parent struct device is passed in, the newly created
  1038. * struct device will be a child of that device in sysfs.
  1039. * The pointer to the struct device will be returned from the call.
  1040. * Any further sysfs files that might be required can be created using this
  1041. * pointer.
  1042. *
  1043. * Note: the struct class passed to this function must have previously
  1044. * been created with a call to class_create().
  1045. */
  1046. struct device *device_create_vargs(struct class *class, struct device *parent,
  1047. dev_t devt, void *drvdata, const char *fmt,
  1048. va_list args)
  1049. {
  1050. struct device *dev = NULL;
  1051. int retval = -ENODEV;
  1052. if (class == NULL || IS_ERR(class))
  1053. goto error;
  1054. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1055. if (!dev) {
  1056. retval = -ENOMEM;
  1057. goto error;
  1058. }
  1059. dev->devt = devt;
  1060. dev->class = class;
  1061. dev->parent = parent;
  1062. dev->release = device_create_release;
  1063. dev_set_drvdata(dev, drvdata);
  1064. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  1065. retval = device_register(dev);
  1066. if (retval)
  1067. goto error;
  1068. return dev;
  1069. error:
  1070. kfree(dev);
  1071. return ERR_PTR(retval);
  1072. }
  1073. EXPORT_SYMBOL_GPL(device_create_vargs);
  1074. /**
  1075. * device_create - creates a device and registers it with sysfs
  1076. * @class: pointer to the struct class that this device should be registered to
  1077. * @parent: pointer to the parent struct device of this new device, if any
  1078. * @devt: the dev_t for the char device to be added
  1079. * @drvdata: the data to be added to the device for callbacks
  1080. * @fmt: string for the device's name
  1081. *
  1082. * This function can be used by char device classes. A struct device
  1083. * will be created in sysfs, registered to the specified class.
  1084. *
  1085. * A "dev" file will be created, showing the dev_t for the device, if
  1086. * the dev_t is not 0,0.
  1087. * If a pointer to a parent struct device is passed in, the newly created
  1088. * struct device will be a child of that device in sysfs.
  1089. * The pointer to the struct device will be returned from the call.
  1090. * Any further sysfs files that might be required can be created using this
  1091. * pointer.
  1092. *
  1093. * Note: the struct class passed to this function must have previously
  1094. * been created with a call to class_create().
  1095. */
  1096. struct device *device_create(struct class *class, struct device *parent,
  1097. dev_t devt, void *drvdata, const char *fmt, ...)
  1098. {
  1099. va_list vargs;
  1100. struct device *dev;
  1101. va_start(vargs, fmt);
  1102. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1103. va_end(vargs);
  1104. return dev;
  1105. }
  1106. EXPORT_SYMBOL_GPL(device_create);
  1107. static int __match_devt(struct device *dev, void *data)
  1108. {
  1109. dev_t *devt = data;
  1110. return dev->devt == *devt;
  1111. }
  1112. /**
  1113. * device_destroy - removes a device that was created with device_create()
  1114. * @class: pointer to the struct class that this device was registered with
  1115. * @devt: the dev_t of the device that was previously registered
  1116. *
  1117. * This call unregisters and cleans up a device that was created with a
  1118. * call to device_create().
  1119. */
  1120. void device_destroy(struct class *class, dev_t devt)
  1121. {
  1122. struct device *dev;
  1123. dev = class_find_device(class, NULL, &devt, __match_devt);
  1124. if (dev) {
  1125. put_device(dev);
  1126. device_unregister(dev);
  1127. }
  1128. }
  1129. EXPORT_SYMBOL_GPL(device_destroy);
  1130. /**
  1131. * device_rename - renames a device
  1132. * @dev: the pointer to the struct device to be renamed
  1133. * @new_name: the new name of the device
  1134. */
  1135. int device_rename(struct device *dev, char *new_name)
  1136. {
  1137. char *old_class_name = NULL;
  1138. char *new_class_name = NULL;
  1139. char *old_device_name = NULL;
  1140. int error;
  1141. dev = get_device(dev);
  1142. if (!dev)
  1143. return -EINVAL;
  1144. pr_debug("device: '%s': %s: renaming to '%s'\n", dev->bus_id,
  1145. __func__, new_name);
  1146. #ifdef CONFIG_SYSFS_DEPRECATED
  1147. if ((dev->class) && (dev->parent))
  1148. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1149. #endif
  1150. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1151. if (!old_device_name) {
  1152. error = -ENOMEM;
  1153. goto out;
  1154. }
  1155. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1156. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1157. error = kobject_rename(&dev->kobj, new_name);
  1158. if (error) {
  1159. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1160. goto out;
  1161. }
  1162. #ifdef CONFIG_SYSFS_DEPRECATED
  1163. if (old_class_name) {
  1164. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1165. if (new_class_name) {
  1166. error = sysfs_create_link_nowarn(&dev->parent->kobj,
  1167. &dev->kobj,
  1168. new_class_name);
  1169. if (error)
  1170. goto out;
  1171. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1172. }
  1173. }
  1174. #else
  1175. if (dev->class) {
  1176. error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
  1177. &dev->kobj, dev->bus_id);
  1178. if (error)
  1179. goto out;
  1180. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  1181. old_device_name);
  1182. }
  1183. #endif
  1184. out:
  1185. put_device(dev);
  1186. kfree(new_class_name);
  1187. kfree(old_class_name);
  1188. kfree(old_device_name);
  1189. return error;
  1190. }
  1191. EXPORT_SYMBOL_GPL(device_rename);
  1192. static int device_move_class_links(struct device *dev,
  1193. struct device *old_parent,
  1194. struct device *new_parent)
  1195. {
  1196. int error = 0;
  1197. #ifdef CONFIG_SYSFS_DEPRECATED
  1198. char *class_name;
  1199. class_name = make_class_name(dev->class->name, &dev->kobj);
  1200. if (!class_name) {
  1201. error = -ENOMEM;
  1202. goto out;
  1203. }
  1204. if (old_parent) {
  1205. sysfs_remove_link(&dev->kobj, "device");
  1206. sysfs_remove_link(&old_parent->kobj, class_name);
  1207. }
  1208. if (new_parent) {
  1209. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1210. "device");
  1211. if (error)
  1212. goto out;
  1213. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1214. class_name);
  1215. if (error)
  1216. sysfs_remove_link(&dev->kobj, "device");
  1217. } else
  1218. error = 0;
  1219. out:
  1220. kfree(class_name);
  1221. return error;
  1222. #else
  1223. if (old_parent)
  1224. sysfs_remove_link(&dev->kobj, "device");
  1225. if (new_parent)
  1226. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1227. "device");
  1228. return error;
  1229. #endif
  1230. }
  1231. /**
  1232. * device_move - moves a device to a new parent
  1233. * @dev: the pointer to the struct device to be moved
  1234. * @new_parent: the new parent of the device (can by NULL)
  1235. */
  1236. int device_move(struct device *dev, struct device *new_parent)
  1237. {
  1238. int error;
  1239. struct device *old_parent;
  1240. struct kobject *new_parent_kobj;
  1241. dev = get_device(dev);
  1242. if (!dev)
  1243. return -EINVAL;
  1244. new_parent = get_device(new_parent);
  1245. new_parent_kobj = get_device_parent(dev, new_parent);
  1246. pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
  1247. __func__, new_parent ? new_parent->bus_id : "<NULL>");
  1248. error = kobject_move(&dev->kobj, new_parent_kobj);
  1249. if (error) {
  1250. cleanup_glue_dir(dev, new_parent_kobj);
  1251. put_device(new_parent);
  1252. goto out;
  1253. }
  1254. old_parent = dev->parent;
  1255. dev->parent = new_parent;
  1256. if (old_parent)
  1257. klist_remove(&dev->knode_parent);
  1258. if (new_parent) {
  1259. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1260. set_dev_node(dev, dev_to_node(new_parent));
  1261. }
  1262. if (!dev->class)
  1263. goto out_put;
  1264. error = device_move_class_links(dev, old_parent, new_parent);
  1265. if (error) {
  1266. /* We ignore errors on cleanup since we're hosed anyway... */
  1267. device_move_class_links(dev, new_parent, old_parent);
  1268. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1269. if (new_parent)
  1270. klist_remove(&dev->knode_parent);
  1271. dev->parent = old_parent;
  1272. if (old_parent) {
  1273. klist_add_tail(&dev->knode_parent,
  1274. &old_parent->klist_children);
  1275. set_dev_node(dev, dev_to_node(old_parent));
  1276. }
  1277. }
  1278. cleanup_glue_dir(dev, new_parent_kobj);
  1279. put_device(new_parent);
  1280. goto out;
  1281. }
  1282. out_put:
  1283. put_device(old_parent);
  1284. out:
  1285. put_device(dev);
  1286. return error;
  1287. }
  1288. EXPORT_SYMBOL_GPL(device_move);
  1289. /**
  1290. * device_shutdown - call ->shutdown() on each device to shutdown.
  1291. */
  1292. void device_shutdown(void)
  1293. {
  1294. struct device *dev, *devn;
  1295. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1296. kobj.entry) {
  1297. if (dev->bus && dev->bus->shutdown) {
  1298. dev_dbg(dev, "shutdown\n");
  1299. dev->bus->shutdown(dev);
  1300. } else if (dev->driver && dev->driver->shutdown) {
  1301. dev_dbg(dev, "shutdown\n");
  1302. dev->driver->shutdown(dev);
  1303. }
  1304. }
  1305. kobject_put(sysfs_dev_char_kobj);
  1306. kobject_put(sysfs_dev_block_kobj);
  1307. kobject_put(dev_kobj);
  1308. }