core.c 41 KB

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