core.c 39 KB

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