core.c 39 KB

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