core.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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. char *s;
  668. va_start(vargs, fmt);
  669. vsnprintf(dev->bus_id, sizeof(dev->bus_id), fmt, vargs);
  670. va_end(vargs);
  671. /* ewww... some of these buggers have / in the name... */
  672. while ((s = strchr(dev->bus_id, '/')))
  673. *s = '!';
  674. return 0;
  675. }
  676. EXPORT_SYMBOL_GPL(dev_set_name);
  677. /**
  678. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  679. * @dev: device
  680. *
  681. * By default we select char/ for new entries. Setting class->dev_obj
  682. * to NULL prevents an entry from being created. class->dev_kobj must
  683. * be set (or cleared) before any devices are registered to the class
  684. * otherwise device_create_sys_dev_entry() and
  685. * device_remove_sys_dev_entry() will disagree about the the presence
  686. * of the link.
  687. */
  688. static struct kobject *device_to_dev_kobj(struct device *dev)
  689. {
  690. struct kobject *kobj;
  691. if (dev->class)
  692. kobj = dev->class->dev_kobj;
  693. else
  694. kobj = sysfs_dev_char_kobj;
  695. return kobj;
  696. }
  697. static int device_create_sys_dev_entry(struct device *dev)
  698. {
  699. struct kobject *kobj = device_to_dev_kobj(dev);
  700. int error = 0;
  701. char devt_str[15];
  702. if (kobj) {
  703. format_dev_t(devt_str, dev->devt);
  704. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  705. }
  706. return error;
  707. }
  708. static void device_remove_sys_dev_entry(struct device *dev)
  709. {
  710. struct kobject *kobj = device_to_dev_kobj(dev);
  711. char devt_str[15];
  712. if (kobj) {
  713. format_dev_t(devt_str, dev->devt);
  714. sysfs_remove_link(kobj, devt_str);
  715. }
  716. }
  717. /**
  718. * device_add - add device to device hierarchy.
  719. * @dev: device.
  720. *
  721. * This is part 2 of device_register(), though may be called
  722. * separately _iff_ device_initialize() has been called separately.
  723. *
  724. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  725. * to the global and sibling lists for the device, then
  726. * adds it to the other relevant subsystems of the driver model.
  727. *
  728. * NOTE: _Never_ directly free @dev after calling this function, even
  729. * if it returned an error! Always use put_device() to give up your
  730. * reference instead.
  731. */
  732. int device_add(struct device *dev)
  733. {
  734. struct device *parent = NULL;
  735. struct class_interface *class_intf;
  736. int error = -EINVAL;
  737. dev = get_device(dev);
  738. if (!dev)
  739. goto done;
  740. /* Temporarily support init_name if it is set.
  741. * It will override bus_id for now */
  742. if (dev->init_name)
  743. dev_set_name(dev, "%s", dev->init_name);
  744. if (!strlen(dev->bus_id))
  745. goto done;
  746. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  747. parent = get_device(dev->parent);
  748. setup_parent(dev, parent);
  749. /* use parent numa_node */
  750. if (parent)
  751. set_dev_node(dev, dev_to_node(parent));
  752. /* first, register with generic layer. */
  753. error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
  754. if (error)
  755. goto Error;
  756. /* notify platform of device entry */
  757. if (platform_notify)
  758. platform_notify(dev);
  759. error = device_create_file(dev, &uevent_attr);
  760. if (error)
  761. goto attrError;
  762. if (MAJOR(dev->devt)) {
  763. error = device_create_file(dev, &devt_attr);
  764. if (error)
  765. goto ueventattrError;
  766. error = device_create_sys_dev_entry(dev);
  767. if (error)
  768. goto devtattrError;
  769. }
  770. error = device_add_class_symlinks(dev);
  771. if (error)
  772. goto SymlinkError;
  773. error = device_add_attrs(dev);
  774. if (error)
  775. goto AttrsError;
  776. error = bus_add_device(dev);
  777. if (error)
  778. goto BusError;
  779. error = dpm_sysfs_add(dev);
  780. if (error)
  781. goto DPMError;
  782. device_pm_add(dev);
  783. /* Notify clients of device addition. This call must come
  784. * after dpm_sysf_add() and before kobject_uevent().
  785. */
  786. if (dev->bus)
  787. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  788. BUS_NOTIFY_ADD_DEVICE, dev);
  789. kobject_uevent(&dev->kobj, KOBJ_ADD);
  790. bus_attach_device(dev);
  791. if (parent)
  792. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  793. if (dev->class) {
  794. mutex_lock(&dev->class->p->class_mutex);
  795. /* tie the class to the device */
  796. klist_add_tail(&dev->knode_class,
  797. &dev->class->p->class_devices);
  798. /* notify any interfaces that the device is here */
  799. list_for_each_entry(class_intf,
  800. &dev->class->p->class_interfaces, node)
  801. if (class_intf->add_dev)
  802. class_intf->add_dev(dev, class_intf);
  803. mutex_unlock(&dev->class->p->class_mutex);
  804. }
  805. done:
  806. put_device(dev);
  807. return error;
  808. DPMError:
  809. bus_remove_device(dev);
  810. BusError:
  811. device_remove_attrs(dev);
  812. AttrsError:
  813. device_remove_class_symlinks(dev);
  814. SymlinkError:
  815. if (MAJOR(dev->devt))
  816. device_remove_sys_dev_entry(dev);
  817. devtattrError:
  818. if (MAJOR(dev->devt))
  819. device_remove_file(dev, &devt_attr);
  820. ueventattrError:
  821. device_remove_file(dev, &uevent_attr);
  822. attrError:
  823. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  824. kobject_del(&dev->kobj);
  825. Error:
  826. cleanup_device_parent(dev);
  827. if (parent)
  828. put_device(parent);
  829. goto done;
  830. }
  831. /**
  832. * device_register - register a device with the system.
  833. * @dev: pointer to the device structure
  834. *
  835. * This happens in two clean steps - initialize the device
  836. * and add it to the system. The two steps can be called
  837. * separately, but this is the easiest and most common.
  838. * I.e. you should only call the two helpers separately if
  839. * have a clearly defined need to use and refcount the device
  840. * before it is added to the hierarchy.
  841. *
  842. * NOTE: _Never_ directly free @dev after calling this function, even
  843. * if it returned an error! Always use put_device() to give up the
  844. * reference initialized in this function instead.
  845. */
  846. int device_register(struct device *dev)
  847. {
  848. device_initialize(dev);
  849. return device_add(dev);
  850. }
  851. /**
  852. * get_device - increment reference count for device.
  853. * @dev: device.
  854. *
  855. * This simply forwards the call to kobject_get(), though
  856. * we do take care to provide for the case that we get a NULL
  857. * pointer passed in.
  858. */
  859. struct device *get_device(struct device *dev)
  860. {
  861. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  862. }
  863. /**
  864. * put_device - decrement reference count.
  865. * @dev: device in question.
  866. */
  867. void put_device(struct device *dev)
  868. {
  869. /* might_sleep(); */
  870. if (dev)
  871. kobject_put(&dev->kobj);
  872. }
  873. /**
  874. * device_del - delete device from system.
  875. * @dev: device.
  876. *
  877. * This is the first part of the device unregistration
  878. * sequence. This removes the device from the lists we control
  879. * from here, has it removed from the other driver model
  880. * subsystems it was added to in device_add(), and removes it
  881. * from the kobject hierarchy.
  882. *
  883. * NOTE: this should be called manually _iff_ device_add() was
  884. * also called manually.
  885. */
  886. void device_del(struct device *dev)
  887. {
  888. struct device *parent = dev->parent;
  889. struct class_interface *class_intf;
  890. /* Notify clients of device removal. This call must come
  891. * before dpm_sysfs_remove().
  892. */
  893. if (dev->bus)
  894. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  895. BUS_NOTIFY_DEL_DEVICE, dev);
  896. device_pm_remove(dev);
  897. dpm_sysfs_remove(dev);
  898. if (parent)
  899. klist_del(&dev->knode_parent);
  900. if (MAJOR(dev->devt)) {
  901. device_remove_sys_dev_entry(dev);
  902. device_remove_file(dev, &devt_attr);
  903. }
  904. if (dev->class) {
  905. device_remove_class_symlinks(dev);
  906. mutex_lock(&dev->class->p->class_mutex);
  907. /* notify any interfaces that the device is now gone */
  908. list_for_each_entry(class_intf,
  909. &dev->class->p->class_interfaces, node)
  910. if (class_intf->remove_dev)
  911. class_intf->remove_dev(dev, class_intf);
  912. /* remove the device from the class list */
  913. klist_del(&dev->knode_class);
  914. mutex_unlock(&dev->class->p->class_mutex);
  915. }
  916. device_remove_file(dev, &uevent_attr);
  917. device_remove_attrs(dev);
  918. bus_remove_device(dev);
  919. /*
  920. * Some platform devices are driven without driver attached
  921. * and managed resources may have been acquired. Make sure
  922. * all resources are released.
  923. */
  924. devres_release_all(dev);
  925. /* Notify the platform of the removal, in case they
  926. * need to do anything...
  927. */
  928. if (platform_notify_remove)
  929. platform_notify_remove(dev);
  930. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  931. cleanup_device_parent(dev);
  932. kobject_del(&dev->kobj);
  933. put_device(parent);
  934. }
  935. /**
  936. * device_unregister - unregister device from system.
  937. * @dev: device going away.
  938. *
  939. * We do this in two parts, like we do device_register(). First,
  940. * we remove it from all the subsystems with device_del(), then
  941. * we decrement the reference count via put_device(). If that
  942. * is the final reference count, the device will be cleaned up
  943. * via device_release() above. Otherwise, the structure will
  944. * stick around until the final reference to the device is dropped.
  945. */
  946. void device_unregister(struct device *dev)
  947. {
  948. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  949. device_del(dev);
  950. put_device(dev);
  951. }
  952. static struct device *next_device(struct klist_iter *i)
  953. {
  954. struct klist_node *n = klist_next(i);
  955. return n ? container_of(n, struct device, knode_parent) : NULL;
  956. }
  957. /**
  958. * device_for_each_child - device child iterator.
  959. * @parent: parent struct device.
  960. * @data: data for the callback.
  961. * @fn: function to be called for each device.
  962. *
  963. * Iterate over @parent's child devices, and call @fn for each,
  964. * passing it @data.
  965. *
  966. * We check the return of @fn each time. If it returns anything
  967. * other than 0, we break out and return that value.
  968. */
  969. int device_for_each_child(struct device *parent, void *data,
  970. int (*fn)(struct device *dev, void *data))
  971. {
  972. struct klist_iter i;
  973. struct device *child;
  974. int error = 0;
  975. klist_iter_init(&parent->klist_children, &i);
  976. while ((child = next_device(&i)) && !error)
  977. error = fn(child, data);
  978. klist_iter_exit(&i);
  979. return error;
  980. }
  981. /**
  982. * device_find_child - device iterator for locating a particular device.
  983. * @parent: parent struct device
  984. * @data: Data to pass to match function
  985. * @match: Callback function to check device
  986. *
  987. * This is similar to the device_for_each_child() function above, but it
  988. * returns a reference to a device that is 'found' for later use, as
  989. * determined by the @match callback.
  990. *
  991. * The callback should return 0 if the device doesn't match and non-zero
  992. * if it does. If the callback returns non-zero and a reference to the
  993. * current device can be obtained, this function will return to the caller
  994. * and not iterate over any more devices.
  995. */
  996. struct device *device_find_child(struct device *parent, void *data,
  997. int (*match)(struct device *dev, void *data))
  998. {
  999. struct klist_iter i;
  1000. struct device *child;
  1001. if (!parent)
  1002. return NULL;
  1003. klist_iter_init(&parent->klist_children, &i);
  1004. while ((child = next_device(&i)))
  1005. if (match(child, data) && get_device(child))
  1006. break;
  1007. klist_iter_exit(&i);
  1008. return child;
  1009. }
  1010. int __init devices_init(void)
  1011. {
  1012. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1013. if (!devices_kset)
  1014. return -ENOMEM;
  1015. dev_kobj = kobject_create_and_add("dev", NULL);
  1016. if (!dev_kobj)
  1017. goto dev_kobj_err;
  1018. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1019. if (!sysfs_dev_block_kobj)
  1020. goto block_kobj_err;
  1021. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1022. if (!sysfs_dev_char_kobj)
  1023. goto char_kobj_err;
  1024. return 0;
  1025. char_kobj_err:
  1026. kobject_put(sysfs_dev_block_kobj);
  1027. block_kobj_err:
  1028. kobject_put(dev_kobj);
  1029. dev_kobj_err:
  1030. kset_unregister(devices_kset);
  1031. return -ENOMEM;
  1032. }
  1033. EXPORT_SYMBOL_GPL(device_for_each_child);
  1034. EXPORT_SYMBOL_GPL(device_find_child);
  1035. EXPORT_SYMBOL_GPL(device_initialize);
  1036. EXPORT_SYMBOL_GPL(device_add);
  1037. EXPORT_SYMBOL_GPL(device_register);
  1038. EXPORT_SYMBOL_GPL(device_del);
  1039. EXPORT_SYMBOL_GPL(device_unregister);
  1040. EXPORT_SYMBOL_GPL(get_device);
  1041. EXPORT_SYMBOL_GPL(put_device);
  1042. EXPORT_SYMBOL_GPL(device_create_file);
  1043. EXPORT_SYMBOL_GPL(device_remove_file);
  1044. struct root_device
  1045. {
  1046. struct device dev;
  1047. struct module *owner;
  1048. };
  1049. #define to_root_device(dev) container_of(dev, struct root_device, dev)
  1050. static void root_device_release(struct device *dev)
  1051. {
  1052. kfree(to_root_device(dev));
  1053. }
  1054. /**
  1055. * __root_device_register - allocate and register a root device
  1056. * @name: root device name
  1057. * @owner: owner module of the root device, usually THIS_MODULE
  1058. *
  1059. * This function allocates a root device and registers it
  1060. * using device_register(). In order to free the returned
  1061. * device, use root_device_unregister().
  1062. *
  1063. * Root devices are dummy devices which allow other devices
  1064. * to be grouped under /sys/devices. Use this function to
  1065. * allocate a root device and then use it as the parent of
  1066. * any device which should appear under /sys/devices/{name}
  1067. *
  1068. * The /sys/devices/{name} directory will also contain a
  1069. * 'module' symlink which points to the @owner directory
  1070. * in sysfs.
  1071. *
  1072. * Note: You probably want to use root_device_register().
  1073. */
  1074. struct device *__root_device_register(const char *name, struct module *owner)
  1075. {
  1076. struct root_device *root;
  1077. int err = -ENOMEM;
  1078. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  1079. if (!root)
  1080. return ERR_PTR(err);
  1081. err = dev_set_name(&root->dev, name);
  1082. if (err) {
  1083. kfree(root);
  1084. return ERR_PTR(err);
  1085. }
  1086. root->dev.release = root_device_release;
  1087. err = device_register(&root->dev);
  1088. if (err) {
  1089. put_device(&root->dev);
  1090. return ERR_PTR(err);
  1091. }
  1092. #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
  1093. if (owner) {
  1094. struct module_kobject *mk = &owner->mkobj;
  1095. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  1096. if (err) {
  1097. device_unregister(&root->dev);
  1098. return ERR_PTR(err);
  1099. }
  1100. root->owner = owner;
  1101. }
  1102. #endif
  1103. return &root->dev;
  1104. }
  1105. EXPORT_SYMBOL_GPL(__root_device_register);
  1106. /**
  1107. * root_device_unregister - unregister and free a root device
  1108. * @dev: device going away
  1109. *
  1110. * This function unregisters and cleans up a device that was created by
  1111. * root_device_register().
  1112. */
  1113. void root_device_unregister(struct device *dev)
  1114. {
  1115. struct root_device *root = to_root_device(dev);
  1116. if (root->owner)
  1117. sysfs_remove_link(&root->dev.kobj, "module");
  1118. device_unregister(dev);
  1119. }
  1120. EXPORT_SYMBOL_GPL(root_device_unregister);
  1121. static void device_create_release(struct device *dev)
  1122. {
  1123. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1124. kfree(dev);
  1125. }
  1126. /**
  1127. * device_create_vargs - creates a device and registers it with sysfs
  1128. * @class: pointer to the struct class that this device should be registered to
  1129. * @parent: pointer to the parent struct device of this new device, if any
  1130. * @devt: the dev_t for the char device to be added
  1131. * @drvdata: the data to be added to the device for callbacks
  1132. * @fmt: string for the device's name
  1133. * @args: va_list for the device's name
  1134. *
  1135. * This function can be used by char device classes. A struct device
  1136. * will be created in sysfs, registered to the specified class.
  1137. *
  1138. * A "dev" file will be created, showing the dev_t for the device, if
  1139. * the dev_t is not 0,0.
  1140. * If a pointer to a parent struct device is passed in, the newly created
  1141. * struct device will be a child of that device in sysfs.
  1142. * The pointer to the struct device will be returned from the call.
  1143. * Any further sysfs files that might be required can be created using this
  1144. * pointer.
  1145. *
  1146. * Note: the struct class passed to this function must have previously
  1147. * been created with a call to class_create().
  1148. */
  1149. struct device *device_create_vargs(struct class *class, struct device *parent,
  1150. dev_t devt, void *drvdata, const char *fmt,
  1151. va_list args)
  1152. {
  1153. struct device *dev = NULL;
  1154. int retval = -ENODEV;
  1155. if (class == NULL || IS_ERR(class))
  1156. goto error;
  1157. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1158. if (!dev) {
  1159. retval = -ENOMEM;
  1160. goto error;
  1161. }
  1162. dev->devt = devt;
  1163. dev->class = class;
  1164. dev->parent = parent;
  1165. dev->release = device_create_release;
  1166. dev_set_drvdata(dev, drvdata);
  1167. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  1168. retval = device_register(dev);
  1169. if (retval)
  1170. goto error;
  1171. return dev;
  1172. error:
  1173. put_device(dev);
  1174. return ERR_PTR(retval);
  1175. }
  1176. EXPORT_SYMBOL_GPL(device_create_vargs);
  1177. /**
  1178. * device_create - creates a device and registers it with sysfs
  1179. * @class: pointer to the struct class that this device should be registered to
  1180. * @parent: pointer to the parent struct device of this new device, if any
  1181. * @devt: the dev_t for the char device to be added
  1182. * @drvdata: the data to be added to the device for callbacks
  1183. * @fmt: string for the device's name
  1184. *
  1185. * This function can be used by char device classes. A struct device
  1186. * will be created in sysfs, registered to the specified class.
  1187. *
  1188. * A "dev" file will be created, showing the dev_t for the device, if
  1189. * the dev_t is not 0,0.
  1190. * If a pointer to a parent struct device is passed in, the newly created
  1191. * struct device will be a child of that device in sysfs.
  1192. * The pointer to the struct device will be returned from the call.
  1193. * Any further sysfs files that might be required can be created using this
  1194. * pointer.
  1195. *
  1196. * Note: the struct class passed to this function must have previously
  1197. * been created with a call to class_create().
  1198. */
  1199. struct device *device_create(struct class *class, struct device *parent,
  1200. dev_t devt, void *drvdata, const char *fmt, ...)
  1201. {
  1202. va_list vargs;
  1203. struct device *dev;
  1204. va_start(vargs, fmt);
  1205. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1206. va_end(vargs);
  1207. return dev;
  1208. }
  1209. EXPORT_SYMBOL_GPL(device_create);
  1210. static int __match_devt(struct device *dev, void *data)
  1211. {
  1212. dev_t *devt = data;
  1213. return dev->devt == *devt;
  1214. }
  1215. /**
  1216. * device_destroy - removes a device that was created with device_create()
  1217. * @class: pointer to the struct class that this device was registered with
  1218. * @devt: the dev_t of the device that was previously registered
  1219. *
  1220. * This call unregisters and cleans up a device that was created with a
  1221. * call to device_create().
  1222. */
  1223. void device_destroy(struct class *class, dev_t devt)
  1224. {
  1225. struct device *dev;
  1226. dev = class_find_device(class, NULL, &devt, __match_devt);
  1227. if (dev) {
  1228. put_device(dev);
  1229. device_unregister(dev);
  1230. }
  1231. }
  1232. EXPORT_SYMBOL_GPL(device_destroy);
  1233. /**
  1234. * device_rename - renames a device
  1235. * @dev: the pointer to the struct device to be renamed
  1236. * @new_name: the new name of the device
  1237. *
  1238. * It is the responsibility of the caller to provide mutual
  1239. * exclusion between two different calls of device_rename
  1240. * on the same device to ensure that new_name is valid and
  1241. * won't conflict with other devices.
  1242. */
  1243. int device_rename(struct device *dev, char *new_name)
  1244. {
  1245. char *old_class_name = NULL;
  1246. char *new_class_name = NULL;
  1247. char *old_device_name = NULL;
  1248. int error;
  1249. dev = get_device(dev);
  1250. if (!dev)
  1251. return -EINVAL;
  1252. pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
  1253. __func__, new_name);
  1254. #ifdef CONFIG_SYSFS_DEPRECATED
  1255. if ((dev->class) && (dev->parent))
  1256. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1257. #endif
  1258. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1259. if (!old_device_name) {
  1260. error = -ENOMEM;
  1261. goto out;
  1262. }
  1263. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1264. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1265. error = kobject_rename(&dev->kobj, new_name);
  1266. if (error) {
  1267. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1268. goto out;
  1269. }
  1270. #ifdef CONFIG_SYSFS_DEPRECATED
  1271. if (old_class_name) {
  1272. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1273. if (new_class_name) {
  1274. error = sysfs_create_link_nowarn(&dev->parent->kobj,
  1275. &dev->kobj,
  1276. new_class_name);
  1277. if (error)
  1278. goto out;
  1279. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1280. }
  1281. }
  1282. #else
  1283. if (dev->class) {
  1284. error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
  1285. &dev->kobj, dev_name(dev));
  1286. if (error)
  1287. goto out;
  1288. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  1289. old_device_name);
  1290. }
  1291. #endif
  1292. out:
  1293. put_device(dev);
  1294. kfree(new_class_name);
  1295. kfree(old_class_name);
  1296. kfree(old_device_name);
  1297. return error;
  1298. }
  1299. EXPORT_SYMBOL_GPL(device_rename);
  1300. static int device_move_class_links(struct device *dev,
  1301. struct device *old_parent,
  1302. struct device *new_parent)
  1303. {
  1304. int error = 0;
  1305. #ifdef CONFIG_SYSFS_DEPRECATED
  1306. char *class_name;
  1307. class_name = make_class_name(dev->class->name, &dev->kobj);
  1308. if (!class_name) {
  1309. error = -ENOMEM;
  1310. goto out;
  1311. }
  1312. if (old_parent) {
  1313. sysfs_remove_link(&dev->kobj, "device");
  1314. sysfs_remove_link(&old_parent->kobj, class_name);
  1315. }
  1316. if (new_parent) {
  1317. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1318. "device");
  1319. if (error)
  1320. goto out;
  1321. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1322. class_name);
  1323. if (error)
  1324. sysfs_remove_link(&dev->kobj, "device");
  1325. } else
  1326. error = 0;
  1327. out:
  1328. kfree(class_name);
  1329. return error;
  1330. #else
  1331. if (old_parent)
  1332. sysfs_remove_link(&dev->kobj, "device");
  1333. if (new_parent)
  1334. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1335. "device");
  1336. return error;
  1337. #endif
  1338. }
  1339. /**
  1340. * device_move - moves a device to a new parent
  1341. * @dev: the pointer to the struct device to be moved
  1342. * @new_parent: the new parent of the device (can by NULL)
  1343. */
  1344. int device_move(struct device *dev, struct device *new_parent)
  1345. {
  1346. int error;
  1347. struct device *old_parent;
  1348. struct kobject *new_parent_kobj;
  1349. dev = get_device(dev);
  1350. if (!dev)
  1351. return -EINVAL;
  1352. new_parent = get_device(new_parent);
  1353. new_parent_kobj = get_device_parent(dev, new_parent);
  1354. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  1355. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  1356. error = kobject_move(&dev->kobj, new_parent_kobj);
  1357. if (error) {
  1358. cleanup_glue_dir(dev, new_parent_kobj);
  1359. put_device(new_parent);
  1360. goto out;
  1361. }
  1362. old_parent = dev->parent;
  1363. dev->parent = new_parent;
  1364. if (old_parent)
  1365. klist_remove(&dev->knode_parent);
  1366. if (new_parent) {
  1367. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1368. set_dev_node(dev, dev_to_node(new_parent));
  1369. }
  1370. if (!dev->class)
  1371. goto out_put;
  1372. error = device_move_class_links(dev, old_parent, new_parent);
  1373. if (error) {
  1374. /* We ignore errors on cleanup since we're hosed anyway... */
  1375. device_move_class_links(dev, new_parent, old_parent);
  1376. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1377. if (new_parent)
  1378. klist_remove(&dev->knode_parent);
  1379. dev->parent = old_parent;
  1380. if (old_parent) {
  1381. klist_add_tail(&dev->knode_parent,
  1382. &old_parent->klist_children);
  1383. set_dev_node(dev, dev_to_node(old_parent));
  1384. }
  1385. }
  1386. cleanup_glue_dir(dev, new_parent_kobj);
  1387. put_device(new_parent);
  1388. goto out;
  1389. }
  1390. out_put:
  1391. put_device(old_parent);
  1392. out:
  1393. put_device(dev);
  1394. return error;
  1395. }
  1396. EXPORT_SYMBOL_GPL(device_move);
  1397. /**
  1398. * device_shutdown - call ->shutdown() on each device to shutdown.
  1399. */
  1400. void device_shutdown(void)
  1401. {
  1402. struct device *dev, *devn;
  1403. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  1404. kobj.entry) {
  1405. if (dev->bus && dev->bus->shutdown) {
  1406. dev_dbg(dev, "shutdown\n");
  1407. dev->bus->shutdown(dev);
  1408. } else if (dev->driver && dev->driver->shutdown) {
  1409. dev_dbg(dev, "shutdown\n");
  1410. dev->driver->shutdown(dev);
  1411. }
  1412. }
  1413. kobject_put(sysfs_dev_char_kobj);
  1414. kobject_put(sysfs_dev_block_kobj);
  1415. kobject_put(dev_kobj);
  1416. }