core.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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/mutex.h>
  23. #include <linux/async.h>
  24. #include <linux/pm_runtime.h>
  25. #include "base.h"
  26. #include "power/power.h"
  27. #ifdef CONFIG_SYSFS_DEPRECATED
  28. #ifdef CONFIG_SYSFS_DEPRECATED_V2
  29. long sysfs_deprecated = 1;
  30. #else
  31. long sysfs_deprecated = 0;
  32. #endif
  33. static __init int sysfs_deprecated_setup(char *arg)
  34. {
  35. return strict_strtol(arg, 10, &sysfs_deprecated);
  36. }
  37. early_param("sysfs.deprecated", sysfs_deprecated_setup);
  38. #endif
  39. int (*platform_notify)(struct device *dev) = NULL;
  40. int (*platform_notify_remove)(struct device *dev) = NULL;
  41. static struct kobject *dev_kobj;
  42. struct kobject *sysfs_dev_char_kobj;
  43. struct kobject *sysfs_dev_block_kobj;
  44. #ifdef CONFIG_BLOCK
  45. static inline int device_is_not_partition(struct device *dev)
  46. {
  47. return !(dev->type == &part_type);
  48. }
  49. #else
  50. static inline int device_is_not_partition(struct device *dev)
  51. {
  52. return 1;
  53. }
  54. #endif
  55. /**
  56. * dev_driver_string - Return a device's driver name, if at all possible
  57. * @dev: struct device to get the name of
  58. *
  59. * Will return the device's driver's name if it is bound to a device. If
  60. * the device is not bound to a device, it will return the name of the bus
  61. * it is attached to. If it is not attached to a bus either, an empty
  62. * string will be returned.
  63. */
  64. const char *dev_driver_string(const struct device *dev)
  65. {
  66. struct device_driver *drv;
  67. /* dev->driver can change to NULL underneath us because of unbinding,
  68. * so be careful about accessing it. dev->bus and dev->class should
  69. * never change once they are set, so they don't need special care.
  70. */
  71. drv = ACCESS_ONCE(dev->driver);
  72. return drv ? drv->name :
  73. (dev->bus ? dev->bus->name :
  74. (dev->class ? dev->class->name : ""));
  75. }
  76. EXPORT_SYMBOL(dev_driver_string);
  77. #define to_dev(obj) container_of(obj, struct device, kobj)
  78. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  79. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  80. char *buf)
  81. {
  82. struct device_attribute *dev_attr = to_dev_attr(attr);
  83. struct device *dev = to_dev(kobj);
  84. ssize_t ret = -EIO;
  85. if (dev_attr->show)
  86. ret = dev_attr->show(dev, dev_attr, buf);
  87. if (ret >= (ssize_t)PAGE_SIZE) {
  88. print_symbol("dev_attr_show: %s returned bad count\n",
  89. (unsigned long)dev_attr->show);
  90. }
  91. return ret;
  92. }
  93. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  94. const char *buf, size_t count)
  95. {
  96. struct device_attribute *dev_attr = to_dev_attr(attr);
  97. struct device *dev = to_dev(kobj);
  98. ssize_t ret = -EIO;
  99. if (dev_attr->store)
  100. ret = dev_attr->store(dev, dev_attr, buf, count);
  101. return ret;
  102. }
  103. static const struct sysfs_ops dev_sysfs_ops = {
  104. .show = dev_attr_show,
  105. .store = dev_attr_store,
  106. };
  107. #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
  108. ssize_t device_store_ulong(struct device *dev,
  109. struct device_attribute *attr,
  110. const char *buf, size_t size)
  111. {
  112. struct dev_ext_attribute *ea = to_ext_attr(attr);
  113. char *end;
  114. unsigned long new = simple_strtoul(buf, &end, 0);
  115. if (end == buf)
  116. return -EINVAL;
  117. *(unsigned long *)(ea->var) = new;
  118. /* Always return full write size even if we didn't consume all */
  119. return size;
  120. }
  121. EXPORT_SYMBOL_GPL(device_store_ulong);
  122. ssize_t device_show_ulong(struct device *dev,
  123. struct device_attribute *attr,
  124. char *buf)
  125. {
  126. struct dev_ext_attribute *ea = to_ext_attr(attr);
  127. return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
  128. }
  129. EXPORT_SYMBOL_GPL(device_show_ulong);
  130. ssize_t device_store_int(struct device *dev,
  131. struct device_attribute *attr,
  132. const char *buf, size_t size)
  133. {
  134. struct dev_ext_attribute *ea = to_ext_attr(attr);
  135. char *end;
  136. long new = simple_strtol(buf, &end, 0);
  137. if (end == buf || new > INT_MAX || new < INT_MIN)
  138. return -EINVAL;
  139. *(int *)(ea->var) = new;
  140. /* Always return full write size even if we didn't consume all */
  141. return size;
  142. }
  143. EXPORT_SYMBOL_GPL(device_store_int);
  144. ssize_t device_show_int(struct device *dev,
  145. struct device_attribute *attr,
  146. char *buf)
  147. {
  148. struct dev_ext_attribute *ea = to_ext_attr(attr);
  149. return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
  150. }
  151. EXPORT_SYMBOL_GPL(device_show_int);
  152. /**
  153. * device_release - free device structure.
  154. * @kobj: device's kobject.
  155. *
  156. * This is called once the reference count for the object
  157. * reaches 0. We forward the call to the device's release
  158. * method, which should handle actually freeing the structure.
  159. */
  160. static void device_release(struct kobject *kobj)
  161. {
  162. struct device *dev = to_dev(kobj);
  163. struct device_private *p = dev->p;
  164. if (dev->release)
  165. dev->release(dev);
  166. else if (dev->type && dev->type->release)
  167. dev->type->release(dev);
  168. else if (dev->class && dev->class->dev_release)
  169. dev->class->dev_release(dev);
  170. else
  171. WARN(1, KERN_ERR "Device '%s' does not have a release() "
  172. "function, it is broken and must be fixed.\n",
  173. dev_name(dev));
  174. kfree(p);
  175. }
  176. static const void *device_namespace(struct kobject *kobj)
  177. {
  178. struct device *dev = to_dev(kobj);
  179. const void *ns = NULL;
  180. if (dev->class && dev->class->ns_type)
  181. ns = dev->class->namespace(dev);
  182. return ns;
  183. }
  184. static struct kobj_type device_ktype = {
  185. .release = device_release,
  186. .sysfs_ops = &dev_sysfs_ops,
  187. .namespace = device_namespace,
  188. };
  189. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  190. {
  191. struct kobj_type *ktype = get_ktype(kobj);
  192. if (ktype == &device_ktype) {
  193. struct device *dev = to_dev(kobj);
  194. if (dev->bus)
  195. return 1;
  196. if (dev->class)
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  202. {
  203. struct device *dev = to_dev(kobj);
  204. if (dev->bus)
  205. return dev->bus->name;
  206. if (dev->class)
  207. return dev->class->name;
  208. return NULL;
  209. }
  210. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  211. struct kobj_uevent_env *env)
  212. {
  213. struct device *dev = to_dev(kobj);
  214. int retval = 0;
  215. /* add device node properties if present */
  216. if (MAJOR(dev->devt)) {
  217. const char *tmp;
  218. const char *name;
  219. umode_t mode = 0;
  220. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  221. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  222. name = device_get_devnode(dev, &mode, &tmp);
  223. if (name) {
  224. add_uevent_var(env, "DEVNAME=%s", name);
  225. kfree(tmp);
  226. if (mode)
  227. add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
  228. }
  229. }
  230. if (dev->type && dev->type->name)
  231. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  232. if (dev->driver)
  233. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  234. /* have the bus specific function add its stuff */
  235. if (dev->bus && dev->bus->uevent) {
  236. retval = dev->bus->uevent(dev, env);
  237. if (retval)
  238. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  239. dev_name(dev), __func__, retval);
  240. }
  241. /* have the class specific function add its stuff */
  242. if (dev->class && dev->class->dev_uevent) {
  243. retval = dev->class->dev_uevent(dev, env);
  244. if (retval)
  245. pr_debug("device: '%s': %s: class uevent() "
  246. "returned %d\n", dev_name(dev),
  247. __func__, retval);
  248. }
  249. /* have the device type specific function add its stuff */
  250. if (dev->type && dev->type->uevent) {
  251. retval = dev->type->uevent(dev, env);
  252. if (retval)
  253. pr_debug("device: '%s': %s: dev_type uevent() "
  254. "returned %d\n", dev_name(dev),
  255. __func__, retval);
  256. }
  257. return retval;
  258. }
  259. static const struct kset_uevent_ops device_uevent_ops = {
  260. .filter = dev_uevent_filter,
  261. .name = dev_uevent_name,
  262. .uevent = dev_uevent,
  263. };
  264. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  265. char *buf)
  266. {
  267. struct kobject *top_kobj;
  268. struct kset *kset;
  269. struct kobj_uevent_env *env = NULL;
  270. int i;
  271. size_t count = 0;
  272. int retval;
  273. /* search the kset, the device belongs to */
  274. top_kobj = &dev->kobj;
  275. while (!top_kobj->kset && top_kobj->parent)
  276. top_kobj = top_kobj->parent;
  277. if (!top_kobj->kset)
  278. goto out;
  279. kset = top_kobj->kset;
  280. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  281. goto out;
  282. /* respect filter */
  283. if (kset->uevent_ops && kset->uevent_ops->filter)
  284. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  285. goto out;
  286. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  287. if (!env)
  288. return -ENOMEM;
  289. /* let the kset specific function add its keys */
  290. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  291. if (retval)
  292. goto out;
  293. /* copy keys to file */
  294. for (i = 0; i < env->envp_idx; i++)
  295. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  296. out:
  297. kfree(env);
  298. return count;
  299. }
  300. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  301. const char *buf, size_t count)
  302. {
  303. enum kobject_action action;
  304. if (kobject_action_type(buf, count, &action) == 0)
  305. kobject_uevent(&dev->kobj, action);
  306. else
  307. dev_err(dev, "uevent: unknown action-string\n");
  308. return count;
  309. }
  310. static struct device_attribute uevent_attr =
  311. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  312. static int device_add_attributes(struct device *dev,
  313. struct device_attribute *attrs)
  314. {
  315. int error = 0;
  316. int i;
  317. if (attrs) {
  318. for (i = 0; attr_name(attrs[i]); i++) {
  319. error = device_create_file(dev, &attrs[i]);
  320. if (error)
  321. break;
  322. }
  323. if (error)
  324. while (--i >= 0)
  325. device_remove_file(dev, &attrs[i]);
  326. }
  327. return error;
  328. }
  329. static void device_remove_attributes(struct device *dev,
  330. struct device_attribute *attrs)
  331. {
  332. int i;
  333. if (attrs)
  334. for (i = 0; attr_name(attrs[i]); i++)
  335. device_remove_file(dev, &attrs[i]);
  336. }
  337. static int device_add_bin_attributes(struct device *dev,
  338. struct bin_attribute *attrs)
  339. {
  340. int error = 0;
  341. int i;
  342. if (attrs) {
  343. for (i = 0; attr_name(attrs[i]); i++) {
  344. error = device_create_bin_file(dev, &attrs[i]);
  345. if (error)
  346. break;
  347. }
  348. if (error)
  349. while (--i >= 0)
  350. device_remove_bin_file(dev, &attrs[i]);
  351. }
  352. return error;
  353. }
  354. static void device_remove_bin_attributes(struct device *dev,
  355. struct bin_attribute *attrs)
  356. {
  357. int i;
  358. if (attrs)
  359. for (i = 0; attr_name(attrs[i]); i++)
  360. device_remove_bin_file(dev, &attrs[i]);
  361. }
  362. static int device_add_groups(struct device *dev,
  363. const struct attribute_group **groups)
  364. {
  365. int error = 0;
  366. int i;
  367. if (groups) {
  368. for (i = 0; groups[i]; i++) {
  369. error = sysfs_create_group(&dev->kobj, groups[i]);
  370. if (error) {
  371. while (--i >= 0)
  372. sysfs_remove_group(&dev->kobj,
  373. groups[i]);
  374. break;
  375. }
  376. }
  377. }
  378. return error;
  379. }
  380. static void device_remove_groups(struct device *dev,
  381. const struct attribute_group **groups)
  382. {
  383. int i;
  384. if (groups)
  385. for (i = 0; groups[i]; i++)
  386. sysfs_remove_group(&dev->kobj, groups[i]);
  387. }
  388. static int device_add_attrs(struct device *dev)
  389. {
  390. struct class *class = dev->class;
  391. const struct device_type *type = dev->type;
  392. int error;
  393. if (class) {
  394. error = device_add_attributes(dev, class->dev_attrs);
  395. if (error)
  396. return error;
  397. error = device_add_bin_attributes(dev, class->dev_bin_attrs);
  398. if (error)
  399. goto err_remove_class_attrs;
  400. }
  401. if (type) {
  402. error = device_add_groups(dev, type->groups);
  403. if (error)
  404. goto err_remove_class_bin_attrs;
  405. }
  406. error = device_add_groups(dev, dev->groups);
  407. if (error)
  408. goto err_remove_type_groups;
  409. return 0;
  410. err_remove_type_groups:
  411. if (type)
  412. device_remove_groups(dev, type->groups);
  413. err_remove_class_bin_attrs:
  414. if (class)
  415. device_remove_bin_attributes(dev, class->dev_bin_attrs);
  416. err_remove_class_attrs:
  417. if (class)
  418. device_remove_attributes(dev, class->dev_attrs);
  419. return error;
  420. }
  421. static void device_remove_attrs(struct device *dev)
  422. {
  423. struct class *class = dev->class;
  424. const struct device_type *type = dev->type;
  425. device_remove_groups(dev, dev->groups);
  426. if (type)
  427. device_remove_groups(dev, type->groups);
  428. if (class) {
  429. device_remove_attributes(dev, class->dev_attrs);
  430. device_remove_bin_attributes(dev, class->dev_bin_attrs);
  431. }
  432. }
  433. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  434. char *buf)
  435. {
  436. return print_dev_t(buf, dev->devt);
  437. }
  438. static struct device_attribute devt_attr =
  439. __ATTR(dev, S_IRUGO, show_dev, NULL);
  440. /* /sys/devices/ */
  441. struct kset *devices_kset;
  442. /**
  443. * device_create_file - create sysfs attribute file for device.
  444. * @dev: device.
  445. * @attr: device attribute descriptor.
  446. */
  447. int device_create_file(struct device *dev,
  448. const struct device_attribute *attr)
  449. {
  450. int error = 0;
  451. if (dev)
  452. error = sysfs_create_file(&dev->kobj, &attr->attr);
  453. return error;
  454. }
  455. /**
  456. * device_remove_file - remove sysfs attribute file.
  457. * @dev: device.
  458. * @attr: device attribute descriptor.
  459. */
  460. void device_remove_file(struct device *dev,
  461. const struct device_attribute *attr)
  462. {
  463. if (dev)
  464. sysfs_remove_file(&dev->kobj, &attr->attr);
  465. }
  466. /**
  467. * device_create_bin_file - create sysfs binary attribute file for device.
  468. * @dev: device.
  469. * @attr: device binary attribute descriptor.
  470. */
  471. int device_create_bin_file(struct device *dev,
  472. const struct bin_attribute *attr)
  473. {
  474. int error = -EINVAL;
  475. if (dev)
  476. error = sysfs_create_bin_file(&dev->kobj, attr);
  477. return error;
  478. }
  479. EXPORT_SYMBOL_GPL(device_create_bin_file);
  480. /**
  481. * device_remove_bin_file - remove sysfs binary attribute file
  482. * @dev: device.
  483. * @attr: device binary attribute descriptor.
  484. */
  485. void device_remove_bin_file(struct device *dev,
  486. const struct bin_attribute *attr)
  487. {
  488. if (dev)
  489. sysfs_remove_bin_file(&dev->kobj, attr);
  490. }
  491. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  492. /**
  493. * device_schedule_callback_owner - helper to schedule a callback for a device
  494. * @dev: device.
  495. * @func: callback function to invoke later.
  496. * @owner: module owning the callback routine
  497. *
  498. * Attribute methods must not unregister themselves or their parent device
  499. * (which would amount to the same thing). Attempts to do so will deadlock,
  500. * since unregistration is mutually exclusive with driver callbacks.
  501. *
  502. * Instead methods can call this routine, which will attempt to allocate
  503. * and schedule a workqueue request to call back @func with @dev as its
  504. * argument in the workqueue's process context. @dev will be pinned until
  505. * @func returns.
  506. *
  507. * This routine is usually called via the inline device_schedule_callback(),
  508. * which automatically sets @owner to THIS_MODULE.
  509. *
  510. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  511. * be allocated, -ENODEV if a reference to @owner isn't available.
  512. *
  513. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  514. * underlying sysfs routine (since it is intended for use by attribute
  515. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  516. */
  517. int device_schedule_callback_owner(struct device *dev,
  518. void (*func)(struct device *), struct module *owner)
  519. {
  520. return sysfs_schedule_callback(&dev->kobj,
  521. (void (*)(void *)) func, dev, owner);
  522. }
  523. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  524. static void klist_children_get(struct klist_node *n)
  525. {
  526. struct device_private *p = to_device_private_parent(n);
  527. struct device *dev = p->device;
  528. get_device(dev);
  529. }
  530. static void klist_children_put(struct klist_node *n)
  531. {
  532. struct device_private *p = to_device_private_parent(n);
  533. struct device *dev = p->device;
  534. put_device(dev);
  535. }
  536. /**
  537. * device_initialize - init device structure.
  538. * @dev: device.
  539. *
  540. * This prepares the device for use by other layers by initializing
  541. * its fields.
  542. * It is the first half of device_register(), if called by
  543. * that function, though it can also be called separately, so one
  544. * may use @dev's fields. In particular, get_device()/put_device()
  545. * may be used for reference counting of @dev after calling this
  546. * function.
  547. *
  548. * NOTE: Use put_device() to give up your reference instead of freeing
  549. * @dev directly once you have called this function.
  550. */
  551. void device_initialize(struct device *dev)
  552. {
  553. dev->kobj.kset = devices_kset;
  554. kobject_init(&dev->kobj, &device_ktype);
  555. INIT_LIST_HEAD(&dev->dma_pools);
  556. mutex_init(&dev->mutex);
  557. lockdep_set_novalidate_class(&dev->mutex);
  558. spin_lock_init(&dev->devres_lock);
  559. INIT_LIST_HEAD(&dev->devres_head);
  560. device_pm_init(dev);
  561. set_dev_node(dev, -1);
  562. }
  563. static struct kobject *virtual_device_parent(struct device *dev)
  564. {
  565. static struct kobject *virtual_dir = NULL;
  566. if (!virtual_dir)
  567. virtual_dir = kobject_create_and_add("virtual",
  568. &devices_kset->kobj);
  569. return virtual_dir;
  570. }
  571. struct class_dir {
  572. struct kobject kobj;
  573. struct class *class;
  574. };
  575. #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
  576. static void class_dir_release(struct kobject *kobj)
  577. {
  578. struct class_dir *dir = to_class_dir(kobj);
  579. kfree(dir);
  580. }
  581. static const
  582. struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
  583. {
  584. struct class_dir *dir = to_class_dir(kobj);
  585. return dir->class->ns_type;
  586. }
  587. static struct kobj_type class_dir_ktype = {
  588. .release = class_dir_release,
  589. .sysfs_ops = &kobj_sysfs_ops,
  590. .child_ns_type = class_dir_child_ns_type
  591. };
  592. static struct kobject *
  593. class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
  594. {
  595. struct class_dir *dir;
  596. int retval;
  597. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  598. if (!dir)
  599. return NULL;
  600. dir->class = class;
  601. kobject_init(&dir->kobj, &class_dir_ktype);
  602. dir->kobj.kset = &class->p->glue_dirs;
  603. retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
  604. if (retval < 0) {
  605. kobject_put(&dir->kobj);
  606. return NULL;
  607. }
  608. return &dir->kobj;
  609. }
  610. static struct kobject *get_device_parent(struct device *dev,
  611. struct device *parent)
  612. {
  613. if (dev->class) {
  614. static DEFINE_MUTEX(gdp_mutex);
  615. struct kobject *kobj = NULL;
  616. struct kobject *parent_kobj;
  617. struct kobject *k;
  618. #ifdef CONFIG_BLOCK
  619. /* block disks show up in /sys/block */
  620. if (sysfs_deprecated && dev->class == &block_class) {
  621. if (parent && parent->class == &block_class)
  622. return &parent->kobj;
  623. return &block_class.p->subsys.kobj;
  624. }
  625. #endif
  626. /*
  627. * If we have no parent, we live in "virtual".
  628. * Class-devices with a non class-device as parent, live
  629. * in a "glue" directory to prevent namespace collisions.
  630. */
  631. if (parent == NULL)
  632. parent_kobj = virtual_device_parent(dev);
  633. else if (parent->class && !dev->class->ns_type)
  634. return &parent->kobj;
  635. else
  636. parent_kobj = &parent->kobj;
  637. mutex_lock(&gdp_mutex);
  638. /* find our class-directory at the parent and reference it */
  639. spin_lock(&dev->class->p->glue_dirs.list_lock);
  640. list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
  641. if (k->parent == parent_kobj) {
  642. kobj = kobject_get(k);
  643. break;
  644. }
  645. spin_unlock(&dev->class->p->glue_dirs.list_lock);
  646. if (kobj) {
  647. mutex_unlock(&gdp_mutex);
  648. return kobj;
  649. }
  650. /* or create a new class-directory at the parent device */
  651. k = class_dir_create_and_add(dev->class, parent_kobj);
  652. /* do not emit an uevent for this simple "glue" directory */
  653. mutex_unlock(&gdp_mutex);
  654. return k;
  655. }
  656. /* subsystems can specify a default root directory for their devices */
  657. if (!parent && dev->bus && dev->bus->dev_root)
  658. return &dev->bus->dev_root->kobj;
  659. if (parent)
  660. return &parent->kobj;
  661. return NULL;
  662. }
  663. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  664. {
  665. /* see if we live in a "glue" directory */
  666. if (!glue_dir || !dev->class ||
  667. glue_dir->kset != &dev->class->p->glue_dirs)
  668. return;
  669. kobject_put(glue_dir);
  670. }
  671. static void cleanup_device_parent(struct device *dev)
  672. {
  673. cleanup_glue_dir(dev, dev->kobj.parent);
  674. }
  675. static int device_add_class_symlinks(struct device *dev)
  676. {
  677. int error;
  678. if (!dev->class)
  679. return 0;
  680. error = sysfs_create_link(&dev->kobj,
  681. &dev->class->p->subsys.kobj,
  682. "subsystem");
  683. if (error)
  684. goto out;
  685. if (dev->parent && device_is_not_partition(dev)) {
  686. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  687. "device");
  688. if (error)
  689. goto out_subsys;
  690. }
  691. #ifdef CONFIG_BLOCK
  692. /* /sys/block has directories and does not need symlinks */
  693. if (sysfs_deprecated && dev->class == &block_class)
  694. return 0;
  695. #endif
  696. /* link in the class directory pointing to the device */
  697. error = sysfs_create_link(&dev->class->p->subsys.kobj,
  698. &dev->kobj, dev_name(dev));
  699. if (error)
  700. goto out_device;
  701. return 0;
  702. out_device:
  703. sysfs_remove_link(&dev->kobj, "device");
  704. out_subsys:
  705. sysfs_remove_link(&dev->kobj, "subsystem");
  706. out:
  707. return error;
  708. }
  709. static void device_remove_class_symlinks(struct device *dev)
  710. {
  711. if (!dev->class)
  712. return;
  713. if (dev->parent && device_is_not_partition(dev))
  714. sysfs_remove_link(&dev->kobj, "device");
  715. sysfs_remove_link(&dev->kobj, "subsystem");
  716. #ifdef CONFIG_BLOCK
  717. if (sysfs_deprecated && dev->class == &block_class)
  718. return;
  719. #endif
  720. sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
  721. }
  722. /**
  723. * dev_set_name - set a device name
  724. * @dev: device
  725. * @fmt: format string for the device's name
  726. */
  727. int dev_set_name(struct device *dev, const char *fmt, ...)
  728. {
  729. va_list vargs;
  730. int err;
  731. va_start(vargs, fmt);
  732. err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  733. va_end(vargs);
  734. return err;
  735. }
  736. EXPORT_SYMBOL_GPL(dev_set_name);
  737. /**
  738. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  739. * @dev: device
  740. *
  741. * By default we select char/ for new entries. Setting class->dev_obj
  742. * to NULL prevents an entry from being created. class->dev_kobj must
  743. * be set (or cleared) before any devices are registered to the class
  744. * otherwise device_create_sys_dev_entry() and
  745. * device_remove_sys_dev_entry() will disagree about the the presence
  746. * of the link.
  747. */
  748. static struct kobject *device_to_dev_kobj(struct device *dev)
  749. {
  750. struct kobject *kobj;
  751. if (dev->class)
  752. kobj = dev->class->dev_kobj;
  753. else
  754. kobj = sysfs_dev_char_kobj;
  755. return kobj;
  756. }
  757. static int device_create_sys_dev_entry(struct device *dev)
  758. {
  759. struct kobject *kobj = device_to_dev_kobj(dev);
  760. int error = 0;
  761. char devt_str[15];
  762. if (kobj) {
  763. format_dev_t(devt_str, dev->devt);
  764. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  765. }
  766. return error;
  767. }
  768. static void device_remove_sys_dev_entry(struct device *dev)
  769. {
  770. struct kobject *kobj = device_to_dev_kobj(dev);
  771. char devt_str[15];
  772. if (kobj) {
  773. format_dev_t(devt_str, dev->devt);
  774. sysfs_remove_link(kobj, devt_str);
  775. }
  776. }
  777. int device_private_init(struct device *dev)
  778. {
  779. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  780. if (!dev->p)
  781. return -ENOMEM;
  782. dev->p->device = dev;
  783. klist_init(&dev->p->klist_children, klist_children_get,
  784. klist_children_put);
  785. return 0;
  786. }
  787. /**
  788. * device_add - add device to device hierarchy.
  789. * @dev: device.
  790. *
  791. * This is part 2 of device_register(), though may be called
  792. * separately _iff_ device_initialize() has been called separately.
  793. *
  794. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  795. * to the global and sibling lists for the device, then
  796. * adds it to the other relevant subsystems of the driver model.
  797. *
  798. * NOTE: _Never_ directly free @dev after calling this function, even
  799. * if it returned an error! Always use put_device() to give up your
  800. * reference instead.
  801. */
  802. int device_add(struct device *dev)
  803. {
  804. struct device *parent = NULL;
  805. struct kobject *kobj;
  806. struct class_interface *class_intf;
  807. int error = -EINVAL;
  808. dev = get_device(dev);
  809. if (!dev)
  810. goto done;
  811. if (!dev->p) {
  812. error = device_private_init(dev);
  813. if (error)
  814. goto done;
  815. }
  816. /*
  817. * for statically allocated devices, which should all be converted
  818. * some day, we need to initialize the name. We prevent reading back
  819. * the name, and force the use of dev_name()
  820. */
  821. if (dev->init_name) {
  822. dev_set_name(dev, "%s", dev->init_name);
  823. dev->init_name = NULL;
  824. }
  825. /* subsystems can specify simple device enumeration */
  826. if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
  827. dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
  828. if (!dev_name(dev)) {
  829. error = -EINVAL;
  830. goto name_error;
  831. }
  832. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  833. parent = get_device(dev->parent);
  834. kobj = get_device_parent(dev, parent);
  835. if (kobj)
  836. dev->kobj.parent = kobj;
  837. /* use parent numa_node */
  838. if (parent)
  839. set_dev_node(dev, dev_to_node(parent));
  840. /* first, register with generic layer. */
  841. /* we require the name to be set before, and pass NULL */
  842. error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  843. if (error)
  844. goto Error;
  845. /* notify platform of device entry */
  846. if (platform_notify)
  847. platform_notify(dev);
  848. error = device_create_file(dev, &uevent_attr);
  849. if (error)
  850. goto attrError;
  851. if (MAJOR(dev->devt)) {
  852. error = device_create_file(dev, &devt_attr);
  853. if (error)
  854. goto ueventattrError;
  855. error = device_create_sys_dev_entry(dev);
  856. if (error)
  857. goto devtattrError;
  858. devtmpfs_create_node(dev);
  859. }
  860. error = device_add_class_symlinks(dev);
  861. if (error)
  862. goto SymlinkError;
  863. error = device_add_attrs(dev);
  864. if (error)
  865. goto AttrsError;
  866. error = bus_add_device(dev);
  867. if (error)
  868. goto BusError;
  869. error = dpm_sysfs_add(dev);
  870. if (error)
  871. goto DPMError;
  872. device_pm_add(dev);
  873. /* Notify clients of device addition. This call must come
  874. * after dpm_sysf_add() and before kobject_uevent().
  875. */
  876. if (dev->bus)
  877. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  878. BUS_NOTIFY_ADD_DEVICE, dev);
  879. kobject_uevent(&dev->kobj, KOBJ_ADD);
  880. bus_probe_device(dev);
  881. if (parent)
  882. klist_add_tail(&dev->p->knode_parent,
  883. &parent->p->klist_children);
  884. if (dev->class) {
  885. mutex_lock(&dev->class->p->mutex);
  886. /* tie the class to the device */
  887. klist_add_tail(&dev->knode_class,
  888. &dev->class->p->klist_devices);
  889. /* notify any interfaces that the device is here */
  890. list_for_each_entry(class_intf,
  891. &dev->class->p->interfaces, node)
  892. if (class_intf->add_dev)
  893. class_intf->add_dev(dev, class_intf);
  894. mutex_unlock(&dev->class->p->mutex);
  895. }
  896. done:
  897. put_device(dev);
  898. return error;
  899. DPMError:
  900. bus_remove_device(dev);
  901. BusError:
  902. device_remove_attrs(dev);
  903. AttrsError:
  904. device_remove_class_symlinks(dev);
  905. SymlinkError:
  906. if (MAJOR(dev->devt))
  907. devtmpfs_delete_node(dev);
  908. if (MAJOR(dev->devt))
  909. device_remove_sys_dev_entry(dev);
  910. devtattrError:
  911. if (MAJOR(dev->devt))
  912. device_remove_file(dev, &devt_attr);
  913. ueventattrError:
  914. device_remove_file(dev, &uevent_attr);
  915. attrError:
  916. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  917. kobject_del(&dev->kobj);
  918. Error:
  919. cleanup_device_parent(dev);
  920. if (parent)
  921. put_device(parent);
  922. name_error:
  923. kfree(dev->p);
  924. dev->p = NULL;
  925. goto done;
  926. }
  927. /**
  928. * device_register - register a device with the system.
  929. * @dev: pointer to the device structure
  930. *
  931. * This happens in two clean steps - initialize the device
  932. * and add it to the system. The two steps can be called
  933. * separately, but this is the easiest and most common.
  934. * I.e. you should only call the two helpers separately if
  935. * have a clearly defined need to use and refcount the device
  936. * before it is added to the hierarchy.
  937. *
  938. * NOTE: _Never_ directly free @dev after calling this function, even
  939. * if it returned an error! Always use put_device() to give up the
  940. * reference initialized in this function instead.
  941. */
  942. int device_register(struct device *dev)
  943. {
  944. device_initialize(dev);
  945. return device_add(dev);
  946. }
  947. /**
  948. * get_device - increment reference count for device.
  949. * @dev: device.
  950. *
  951. * This simply forwards the call to kobject_get(), though
  952. * we do take care to provide for the case that we get a NULL
  953. * pointer passed in.
  954. */
  955. struct device *get_device(struct device *dev)
  956. {
  957. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  958. }
  959. /**
  960. * put_device - decrement reference count.
  961. * @dev: device in question.
  962. */
  963. void put_device(struct device *dev)
  964. {
  965. /* might_sleep(); */
  966. if (dev)
  967. kobject_put(&dev->kobj);
  968. }
  969. /**
  970. * device_del - delete device from system.
  971. * @dev: device.
  972. *
  973. * This is the first part of the device unregistration
  974. * sequence. This removes the device from the lists we control
  975. * from here, has it removed from the other driver model
  976. * subsystems it was added to in device_add(), and removes it
  977. * from the kobject hierarchy.
  978. *
  979. * NOTE: this should be called manually _iff_ device_add() was
  980. * also called manually.
  981. */
  982. void device_del(struct device *dev)
  983. {
  984. struct device *parent = dev->parent;
  985. struct class_interface *class_intf;
  986. /* Notify clients of device removal. This call must come
  987. * before dpm_sysfs_remove().
  988. */
  989. if (dev->bus)
  990. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  991. BUS_NOTIFY_DEL_DEVICE, dev);
  992. device_pm_remove(dev);
  993. dpm_sysfs_remove(dev);
  994. if (parent)
  995. klist_del(&dev->p->knode_parent);
  996. if (MAJOR(dev->devt)) {
  997. devtmpfs_delete_node(dev);
  998. device_remove_sys_dev_entry(dev);
  999. device_remove_file(dev, &devt_attr);
  1000. }
  1001. if (dev->class) {
  1002. device_remove_class_symlinks(dev);
  1003. mutex_lock(&dev->class->p->mutex);
  1004. /* notify any interfaces that the device is now gone */
  1005. list_for_each_entry(class_intf,
  1006. &dev->class->p->interfaces, node)
  1007. if (class_intf->remove_dev)
  1008. class_intf->remove_dev(dev, class_intf);
  1009. /* remove the device from the class list */
  1010. klist_del(&dev->knode_class);
  1011. mutex_unlock(&dev->class->p->mutex);
  1012. }
  1013. device_remove_file(dev, &uevent_attr);
  1014. device_remove_attrs(dev);
  1015. bus_remove_device(dev);
  1016. /*
  1017. * Some platform devices are driven without driver attached
  1018. * and managed resources may have been acquired. Make sure
  1019. * all resources are released.
  1020. */
  1021. devres_release_all(dev);
  1022. /* Notify the platform of the removal, in case they
  1023. * need to do anything...
  1024. */
  1025. if (platform_notify_remove)
  1026. platform_notify_remove(dev);
  1027. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  1028. cleanup_device_parent(dev);
  1029. kobject_del(&dev->kobj);
  1030. put_device(parent);
  1031. }
  1032. /**
  1033. * device_unregister - unregister device from system.
  1034. * @dev: device going away.
  1035. *
  1036. * We do this in two parts, like we do device_register(). First,
  1037. * we remove it from all the subsystems with device_del(), then
  1038. * we decrement the reference count via put_device(). If that
  1039. * is the final reference count, the device will be cleaned up
  1040. * via device_release() above. Otherwise, the structure will
  1041. * stick around until the final reference to the device is dropped.
  1042. */
  1043. void device_unregister(struct device *dev)
  1044. {
  1045. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1046. device_del(dev);
  1047. put_device(dev);
  1048. }
  1049. static struct device *next_device(struct klist_iter *i)
  1050. {
  1051. struct klist_node *n = klist_next(i);
  1052. struct device *dev = NULL;
  1053. struct device_private *p;
  1054. if (n) {
  1055. p = to_device_private_parent(n);
  1056. dev = p->device;
  1057. }
  1058. return dev;
  1059. }
  1060. /**
  1061. * device_get_devnode - path of device node file
  1062. * @dev: device
  1063. * @mode: returned file access mode
  1064. * @tmp: possibly allocated string
  1065. *
  1066. * Return the relative path of a possible device node.
  1067. * Non-default names may need to allocate a memory to compose
  1068. * a name. This memory is returned in tmp and needs to be
  1069. * freed by the caller.
  1070. */
  1071. const char *device_get_devnode(struct device *dev,
  1072. umode_t *mode, const char **tmp)
  1073. {
  1074. char *s;
  1075. *tmp = NULL;
  1076. /* the device type may provide a specific name */
  1077. if (dev->type && dev->type->devnode)
  1078. *tmp = dev->type->devnode(dev, mode);
  1079. if (*tmp)
  1080. return *tmp;
  1081. /* the class may provide a specific name */
  1082. if (dev->class && dev->class->devnode)
  1083. *tmp = dev->class->devnode(dev, mode);
  1084. if (*tmp)
  1085. return *tmp;
  1086. /* return name without allocation, tmp == NULL */
  1087. if (strchr(dev_name(dev), '!') == NULL)
  1088. return dev_name(dev);
  1089. /* replace '!' in the name with '/' */
  1090. *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
  1091. if (!*tmp)
  1092. return NULL;
  1093. while ((s = strchr(*tmp, '!')))
  1094. s[0] = '/';
  1095. return *tmp;
  1096. }
  1097. /**
  1098. * device_for_each_child - device child iterator.
  1099. * @parent: parent struct device.
  1100. * @data: data for the callback.
  1101. * @fn: function to be called for each device.
  1102. *
  1103. * Iterate over @parent's child devices, and call @fn for each,
  1104. * passing it @data.
  1105. *
  1106. * We check the return of @fn each time. If it returns anything
  1107. * other than 0, we break out and return that value.
  1108. */
  1109. int device_for_each_child(struct device *parent, void *data,
  1110. int (*fn)(struct device *dev, void *data))
  1111. {
  1112. struct klist_iter i;
  1113. struct device *child;
  1114. int error = 0;
  1115. if (!parent->p)
  1116. return 0;
  1117. klist_iter_init(&parent->p->klist_children, &i);
  1118. while ((child = next_device(&i)) && !error)
  1119. error = fn(child, data);
  1120. klist_iter_exit(&i);
  1121. return error;
  1122. }
  1123. /**
  1124. * device_find_child - device iterator for locating a particular device.
  1125. * @parent: parent struct device
  1126. * @data: Data to pass to match function
  1127. * @match: Callback function to check device
  1128. *
  1129. * This is similar to the device_for_each_child() function above, but it
  1130. * returns a reference to a device that is 'found' for later use, as
  1131. * determined by the @match callback.
  1132. *
  1133. * The callback should return 0 if the device doesn't match and non-zero
  1134. * if it does. If the callback returns non-zero and a reference to the
  1135. * current device can be obtained, this function will return to the caller
  1136. * and not iterate over any more devices.
  1137. */
  1138. struct device *device_find_child(struct device *parent, void *data,
  1139. int (*match)(struct device *dev, void *data))
  1140. {
  1141. struct klist_iter i;
  1142. struct device *child;
  1143. if (!parent)
  1144. return NULL;
  1145. klist_iter_init(&parent->p->klist_children, &i);
  1146. while ((child = next_device(&i)))
  1147. if (match(child, data) && get_device(child))
  1148. break;
  1149. klist_iter_exit(&i);
  1150. return child;
  1151. }
  1152. int __init devices_init(void)
  1153. {
  1154. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1155. if (!devices_kset)
  1156. return -ENOMEM;
  1157. dev_kobj = kobject_create_and_add("dev", NULL);
  1158. if (!dev_kobj)
  1159. goto dev_kobj_err;
  1160. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1161. if (!sysfs_dev_block_kobj)
  1162. goto block_kobj_err;
  1163. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1164. if (!sysfs_dev_char_kobj)
  1165. goto char_kobj_err;
  1166. return 0;
  1167. char_kobj_err:
  1168. kobject_put(sysfs_dev_block_kobj);
  1169. block_kobj_err:
  1170. kobject_put(dev_kobj);
  1171. dev_kobj_err:
  1172. kset_unregister(devices_kset);
  1173. return -ENOMEM;
  1174. }
  1175. EXPORT_SYMBOL_GPL(device_for_each_child);
  1176. EXPORT_SYMBOL_GPL(device_find_child);
  1177. EXPORT_SYMBOL_GPL(device_initialize);
  1178. EXPORT_SYMBOL_GPL(device_add);
  1179. EXPORT_SYMBOL_GPL(device_register);
  1180. EXPORT_SYMBOL_GPL(device_del);
  1181. EXPORT_SYMBOL_GPL(device_unregister);
  1182. EXPORT_SYMBOL_GPL(get_device);
  1183. EXPORT_SYMBOL_GPL(put_device);
  1184. EXPORT_SYMBOL_GPL(device_create_file);
  1185. EXPORT_SYMBOL_GPL(device_remove_file);
  1186. struct root_device {
  1187. struct device dev;
  1188. struct module *owner;
  1189. };
  1190. inline struct root_device *to_root_device(struct device *d)
  1191. {
  1192. return container_of(d, struct root_device, dev);
  1193. }
  1194. static void root_device_release(struct device *dev)
  1195. {
  1196. kfree(to_root_device(dev));
  1197. }
  1198. /**
  1199. * __root_device_register - allocate and register a root device
  1200. * @name: root device name
  1201. * @owner: owner module of the root device, usually THIS_MODULE
  1202. *
  1203. * This function allocates a root device and registers it
  1204. * using device_register(). In order to free the returned
  1205. * device, use root_device_unregister().
  1206. *
  1207. * Root devices are dummy devices which allow other devices
  1208. * to be grouped under /sys/devices. Use this function to
  1209. * allocate a root device and then use it as the parent of
  1210. * any device which should appear under /sys/devices/{name}
  1211. *
  1212. * The /sys/devices/{name} directory will also contain a
  1213. * 'module' symlink which points to the @owner directory
  1214. * in sysfs.
  1215. *
  1216. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1217. *
  1218. * Note: You probably want to use root_device_register().
  1219. */
  1220. struct device *__root_device_register(const char *name, struct module *owner)
  1221. {
  1222. struct root_device *root;
  1223. int err = -ENOMEM;
  1224. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  1225. if (!root)
  1226. return ERR_PTR(err);
  1227. err = dev_set_name(&root->dev, "%s", name);
  1228. if (err) {
  1229. kfree(root);
  1230. return ERR_PTR(err);
  1231. }
  1232. root->dev.release = root_device_release;
  1233. err = device_register(&root->dev);
  1234. if (err) {
  1235. put_device(&root->dev);
  1236. return ERR_PTR(err);
  1237. }
  1238. #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
  1239. if (owner) {
  1240. struct module_kobject *mk = &owner->mkobj;
  1241. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  1242. if (err) {
  1243. device_unregister(&root->dev);
  1244. return ERR_PTR(err);
  1245. }
  1246. root->owner = owner;
  1247. }
  1248. #endif
  1249. return &root->dev;
  1250. }
  1251. EXPORT_SYMBOL_GPL(__root_device_register);
  1252. /**
  1253. * root_device_unregister - unregister and free a root device
  1254. * @dev: device going away
  1255. *
  1256. * This function unregisters and cleans up a device that was created by
  1257. * root_device_register().
  1258. */
  1259. void root_device_unregister(struct device *dev)
  1260. {
  1261. struct root_device *root = to_root_device(dev);
  1262. if (root->owner)
  1263. sysfs_remove_link(&root->dev.kobj, "module");
  1264. device_unregister(dev);
  1265. }
  1266. EXPORT_SYMBOL_GPL(root_device_unregister);
  1267. static void device_create_release(struct device *dev)
  1268. {
  1269. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1270. kfree(dev);
  1271. }
  1272. /**
  1273. * device_create_vargs - creates a device and registers it with sysfs
  1274. * @class: pointer to the struct class that this device should be registered to
  1275. * @parent: pointer to the parent struct device of this new device, if any
  1276. * @devt: the dev_t for the char device to be added
  1277. * @drvdata: the data to be added to the device for callbacks
  1278. * @fmt: string for the device's name
  1279. * @args: va_list for the device's name
  1280. *
  1281. * This function can be used by char device classes. A struct device
  1282. * will be created in sysfs, registered to the specified class.
  1283. *
  1284. * A "dev" file will be created, showing the dev_t for the device, if
  1285. * the dev_t is not 0,0.
  1286. * If a pointer to a parent struct device is passed in, the newly created
  1287. * struct device will be a child of that device in sysfs.
  1288. * The pointer to the struct device will be returned from the call.
  1289. * Any further sysfs files that might be required can be created using this
  1290. * pointer.
  1291. *
  1292. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1293. *
  1294. * Note: the struct class passed to this function must have previously
  1295. * been created with a call to class_create().
  1296. */
  1297. struct device *device_create_vargs(struct class *class, struct device *parent,
  1298. dev_t devt, void *drvdata, const char *fmt,
  1299. va_list args)
  1300. {
  1301. struct device *dev = NULL;
  1302. int retval = -ENODEV;
  1303. if (class == NULL || IS_ERR(class))
  1304. goto error;
  1305. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1306. if (!dev) {
  1307. retval = -ENOMEM;
  1308. goto error;
  1309. }
  1310. dev->devt = devt;
  1311. dev->class = class;
  1312. dev->parent = parent;
  1313. dev->release = device_create_release;
  1314. dev_set_drvdata(dev, drvdata);
  1315. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  1316. if (retval)
  1317. goto error;
  1318. retval = device_register(dev);
  1319. if (retval)
  1320. goto error;
  1321. return dev;
  1322. error:
  1323. put_device(dev);
  1324. return ERR_PTR(retval);
  1325. }
  1326. EXPORT_SYMBOL_GPL(device_create_vargs);
  1327. /**
  1328. * device_create - creates a device and registers it with sysfs
  1329. * @class: pointer to the struct class that this device should be registered to
  1330. * @parent: pointer to the parent struct device of this new device, if any
  1331. * @devt: the dev_t for the char device to be added
  1332. * @drvdata: the data to be added to the device for callbacks
  1333. * @fmt: string for the device's name
  1334. *
  1335. * This function can be used by char device classes. A struct device
  1336. * will be created in sysfs, registered to the specified class.
  1337. *
  1338. * A "dev" file will be created, showing the dev_t for the device, if
  1339. * the dev_t is not 0,0.
  1340. * If a pointer to a parent struct device is passed in, the newly created
  1341. * struct device will be a child of that device in sysfs.
  1342. * The pointer to the struct device will be returned from the call.
  1343. * Any further sysfs files that might be required can be created using this
  1344. * pointer.
  1345. *
  1346. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1347. *
  1348. * Note: the struct class passed to this function must have previously
  1349. * been created with a call to class_create().
  1350. */
  1351. struct device *device_create(struct class *class, struct device *parent,
  1352. dev_t devt, void *drvdata, const char *fmt, ...)
  1353. {
  1354. va_list vargs;
  1355. struct device *dev;
  1356. va_start(vargs, fmt);
  1357. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1358. va_end(vargs);
  1359. return dev;
  1360. }
  1361. EXPORT_SYMBOL_GPL(device_create);
  1362. static int __match_devt(struct device *dev, void *data)
  1363. {
  1364. dev_t *devt = data;
  1365. return dev->devt == *devt;
  1366. }
  1367. /**
  1368. * device_destroy - removes a device that was created with device_create()
  1369. * @class: pointer to the struct class that this device was registered with
  1370. * @devt: the dev_t of the device that was previously registered
  1371. *
  1372. * This call unregisters and cleans up a device that was created with a
  1373. * call to device_create().
  1374. */
  1375. void device_destroy(struct class *class, dev_t devt)
  1376. {
  1377. struct device *dev;
  1378. dev = class_find_device(class, NULL, &devt, __match_devt);
  1379. if (dev) {
  1380. put_device(dev);
  1381. device_unregister(dev);
  1382. }
  1383. }
  1384. EXPORT_SYMBOL_GPL(device_destroy);
  1385. /**
  1386. * device_rename - renames a device
  1387. * @dev: the pointer to the struct device to be renamed
  1388. * @new_name: the new name of the device
  1389. *
  1390. * It is the responsibility of the caller to provide mutual
  1391. * exclusion between two different calls of device_rename
  1392. * on the same device to ensure that new_name is valid and
  1393. * won't conflict with other devices.
  1394. *
  1395. * Note: Don't call this function. Currently, the networking layer calls this
  1396. * function, but that will change. The following text from Kay Sievers offers
  1397. * some insight:
  1398. *
  1399. * Renaming devices is racy at many levels, symlinks and other stuff are not
  1400. * replaced atomically, and you get a "move" uevent, but it's not easy to
  1401. * connect the event to the old and new device. Device nodes are not renamed at
  1402. * all, there isn't even support for that in the kernel now.
  1403. *
  1404. * In the meantime, during renaming, your target name might be taken by another
  1405. * driver, creating conflicts. Or the old name is taken directly after you
  1406. * renamed it -- then you get events for the same DEVPATH, before you even see
  1407. * the "move" event. It's just a mess, and nothing new should ever rely on
  1408. * kernel device renaming. Besides that, it's not even implemented now for
  1409. * other things than (driver-core wise very simple) network devices.
  1410. *
  1411. * We are currently about to change network renaming in udev to completely
  1412. * disallow renaming of devices in the same namespace as the kernel uses,
  1413. * because we can't solve the problems properly, that arise with swapping names
  1414. * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
  1415. * be allowed to some other name than eth[0-9]*, for the aforementioned
  1416. * reasons.
  1417. *
  1418. * Make up a "real" name in the driver before you register anything, or add
  1419. * some other attributes for userspace to find the device, or use udev to add
  1420. * symlinks -- but never rename kernel devices later, it's a complete mess. We
  1421. * don't even want to get into that and try to implement the missing pieces in
  1422. * the core. We really have other pieces to fix in the driver core mess. :)
  1423. */
  1424. int device_rename(struct device *dev, const char *new_name)
  1425. {
  1426. char *old_class_name = NULL;
  1427. char *new_class_name = NULL;
  1428. char *old_device_name = NULL;
  1429. int error;
  1430. dev = get_device(dev);
  1431. if (!dev)
  1432. return -EINVAL;
  1433. pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
  1434. __func__, new_name);
  1435. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  1436. if (!old_device_name) {
  1437. error = -ENOMEM;
  1438. goto out;
  1439. }
  1440. if (dev->class) {
  1441. error = sysfs_rename_link(&dev->class->p->subsys.kobj,
  1442. &dev->kobj, old_device_name, new_name);
  1443. if (error)
  1444. goto out;
  1445. }
  1446. error = kobject_rename(&dev->kobj, new_name);
  1447. if (error)
  1448. goto out;
  1449. out:
  1450. put_device(dev);
  1451. kfree(new_class_name);
  1452. kfree(old_class_name);
  1453. kfree(old_device_name);
  1454. return error;
  1455. }
  1456. EXPORT_SYMBOL_GPL(device_rename);
  1457. static int device_move_class_links(struct device *dev,
  1458. struct device *old_parent,
  1459. struct device *new_parent)
  1460. {
  1461. int error = 0;
  1462. if (old_parent)
  1463. sysfs_remove_link(&dev->kobj, "device");
  1464. if (new_parent)
  1465. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1466. "device");
  1467. return error;
  1468. }
  1469. /**
  1470. * device_move - moves a device to a new parent
  1471. * @dev: the pointer to the struct device to be moved
  1472. * @new_parent: the new parent of the device (can by NULL)
  1473. * @dpm_order: how to reorder the dpm_list
  1474. */
  1475. int device_move(struct device *dev, struct device *new_parent,
  1476. enum dpm_order dpm_order)
  1477. {
  1478. int error;
  1479. struct device *old_parent;
  1480. struct kobject *new_parent_kobj;
  1481. dev = get_device(dev);
  1482. if (!dev)
  1483. return -EINVAL;
  1484. device_pm_lock();
  1485. new_parent = get_device(new_parent);
  1486. new_parent_kobj = get_device_parent(dev, new_parent);
  1487. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  1488. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  1489. error = kobject_move(&dev->kobj, new_parent_kobj);
  1490. if (error) {
  1491. cleanup_glue_dir(dev, new_parent_kobj);
  1492. put_device(new_parent);
  1493. goto out;
  1494. }
  1495. old_parent = dev->parent;
  1496. dev->parent = new_parent;
  1497. if (old_parent)
  1498. klist_remove(&dev->p->knode_parent);
  1499. if (new_parent) {
  1500. klist_add_tail(&dev->p->knode_parent,
  1501. &new_parent->p->klist_children);
  1502. set_dev_node(dev, dev_to_node(new_parent));
  1503. }
  1504. if (!dev->class)
  1505. goto out_put;
  1506. error = device_move_class_links(dev, old_parent, new_parent);
  1507. if (error) {
  1508. /* We ignore errors on cleanup since we're hosed anyway... */
  1509. device_move_class_links(dev, new_parent, old_parent);
  1510. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1511. if (new_parent)
  1512. klist_remove(&dev->p->knode_parent);
  1513. dev->parent = old_parent;
  1514. if (old_parent) {
  1515. klist_add_tail(&dev->p->knode_parent,
  1516. &old_parent->p->klist_children);
  1517. set_dev_node(dev, dev_to_node(old_parent));
  1518. }
  1519. }
  1520. cleanup_glue_dir(dev, new_parent_kobj);
  1521. put_device(new_parent);
  1522. goto out;
  1523. }
  1524. switch (dpm_order) {
  1525. case DPM_ORDER_NONE:
  1526. break;
  1527. case DPM_ORDER_DEV_AFTER_PARENT:
  1528. device_pm_move_after(dev, new_parent);
  1529. break;
  1530. case DPM_ORDER_PARENT_BEFORE_DEV:
  1531. device_pm_move_before(new_parent, dev);
  1532. break;
  1533. case DPM_ORDER_DEV_LAST:
  1534. device_pm_move_last(dev);
  1535. break;
  1536. }
  1537. out_put:
  1538. put_device(old_parent);
  1539. out:
  1540. device_pm_unlock();
  1541. put_device(dev);
  1542. return error;
  1543. }
  1544. EXPORT_SYMBOL_GPL(device_move);
  1545. /**
  1546. * device_shutdown - call ->shutdown() on each device to shutdown.
  1547. */
  1548. void device_shutdown(void)
  1549. {
  1550. struct device *dev;
  1551. spin_lock(&devices_kset->list_lock);
  1552. /*
  1553. * Walk the devices list backward, shutting down each in turn.
  1554. * Beware that device unplug events may also start pulling
  1555. * devices offline, even as the system is shutting down.
  1556. */
  1557. while (!list_empty(&devices_kset->list)) {
  1558. dev = list_entry(devices_kset->list.prev, struct device,
  1559. kobj.entry);
  1560. get_device(dev);
  1561. /*
  1562. * Make sure the device is off the kset list, in the
  1563. * event that dev->*->shutdown() doesn't remove it.
  1564. */
  1565. list_del_init(&dev->kobj.entry);
  1566. spin_unlock(&devices_kset->list_lock);
  1567. /* Don't allow any more runtime suspends */
  1568. pm_runtime_get_noresume(dev);
  1569. pm_runtime_barrier(dev);
  1570. if (dev->bus && dev->bus->shutdown) {
  1571. dev_dbg(dev, "shutdown\n");
  1572. dev->bus->shutdown(dev);
  1573. } else if (dev->driver && dev->driver->shutdown) {
  1574. dev_dbg(dev, "shutdown\n");
  1575. dev->driver->shutdown(dev);
  1576. }
  1577. put_device(dev);
  1578. spin_lock(&devices_kset->list_lock);
  1579. }
  1580. spin_unlock(&devices_kset->list_lock);
  1581. async_synchronize_full();
  1582. }
  1583. /*
  1584. * Device logging functions
  1585. */
  1586. #ifdef CONFIG_PRINTK
  1587. int __dev_printk(const char *level, const struct device *dev,
  1588. struct va_format *vaf)
  1589. {
  1590. if (!dev)
  1591. return printk("%s(NULL device *): %pV", level, vaf);
  1592. return printk("%s%s %s: %pV",
  1593. level, dev_driver_string(dev), dev_name(dev), vaf);
  1594. }
  1595. EXPORT_SYMBOL(__dev_printk);
  1596. int dev_printk(const char *level, const struct device *dev,
  1597. const char *fmt, ...)
  1598. {
  1599. struct va_format vaf;
  1600. va_list args;
  1601. int r;
  1602. va_start(args, fmt);
  1603. vaf.fmt = fmt;
  1604. vaf.va = &args;
  1605. r = __dev_printk(level, dev, &vaf);
  1606. va_end(args);
  1607. return r;
  1608. }
  1609. EXPORT_SYMBOL(dev_printk);
  1610. #define define_dev_printk_level(func, kern_level) \
  1611. int func(const struct device *dev, const char *fmt, ...) \
  1612. { \
  1613. struct va_format vaf; \
  1614. va_list args; \
  1615. int r; \
  1616. \
  1617. va_start(args, fmt); \
  1618. \
  1619. vaf.fmt = fmt; \
  1620. vaf.va = &args; \
  1621. \
  1622. r = __dev_printk(kern_level, dev, &vaf); \
  1623. va_end(args); \
  1624. \
  1625. return r; \
  1626. } \
  1627. EXPORT_SYMBOL(func);
  1628. define_dev_printk_level(dev_emerg, KERN_EMERG);
  1629. define_dev_printk_level(dev_alert, KERN_ALERT);
  1630. define_dev_printk_level(dev_crit, KERN_CRIT);
  1631. define_dev_printk_level(dev_err, KERN_ERR);
  1632. define_dev_printk_level(dev_warn, KERN_WARNING);
  1633. define_dev_printk_level(dev_notice, KERN_NOTICE);
  1634. define_dev_printk_level(_dev_info, KERN_INFO);
  1635. #endif