core.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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. * All fields in @dev must be initialized by the caller to 0, except
  549. * for those explicitly set to some other value. The simplest
  550. * approach is to use kzalloc() to allocate the structure containing
  551. * @dev.
  552. *
  553. * NOTE: Use put_device() to give up your reference instead of freeing
  554. * @dev directly once you have called this function.
  555. */
  556. void device_initialize(struct device *dev)
  557. {
  558. dev->kobj.kset = devices_kset;
  559. kobject_init(&dev->kobj, &device_ktype);
  560. INIT_LIST_HEAD(&dev->dma_pools);
  561. mutex_init(&dev->mutex);
  562. lockdep_set_novalidate_class(&dev->mutex);
  563. spin_lock_init(&dev->devres_lock);
  564. INIT_LIST_HEAD(&dev->devres_head);
  565. device_pm_init(dev);
  566. set_dev_node(dev, -1);
  567. }
  568. static struct kobject *virtual_device_parent(struct device *dev)
  569. {
  570. static struct kobject *virtual_dir = NULL;
  571. if (!virtual_dir)
  572. virtual_dir = kobject_create_and_add("virtual",
  573. &devices_kset->kobj);
  574. return virtual_dir;
  575. }
  576. struct class_dir {
  577. struct kobject kobj;
  578. struct class *class;
  579. };
  580. #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
  581. static void class_dir_release(struct kobject *kobj)
  582. {
  583. struct class_dir *dir = to_class_dir(kobj);
  584. kfree(dir);
  585. }
  586. static const
  587. struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
  588. {
  589. struct class_dir *dir = to_class_dir(kobj);
  590. return dir->class->ns_type;
  591. }
  592. static struct kobj_type class_dir_ktype = {
  593. .release = class_dir_release,
  594. .sysfs_ops = &kobj_sysfs_ops,
  595. .child_ns_type = class_dir_child_ns_type
  596. };
  597. static struct kobject *
  598. class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
  599. {
  600. struct class_dir *dir;
  601. int retval;
  602. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  603. if (!dir)
  604. return NULL;
  605. dir->class = class;
  606. kobject_init(&dir->kobj, &class_dir_ktype);
  607. dir->kobj.kset = &class->p->glue_dirs;
  608. retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
  609. if (retval < 0) {
  610. kobject_put(&dir->kobj);
  611. return NULL;
  612. }
  613. return &dir->kobj;
  614. }
  615. static struct kobject *get_device_parent(struct device *dev,
  616. struct device *parent)
  617. {
  618. if (dev->class) {
  619. static DEFINE_MUTEX(gdp_mutex);
  620. struct kobject *kobj = NULL;
  621. struct kobject *parent_kobj;
  622. struct kobject *k;
  623. #ifdef CONFIG_BLOCK
  624. /* block disks show up in /sys/block */
  625. if (sysfs_deprecated && dev->class == &block_class) {
  626. if (parent && parent->class == &block_class)
  627. return &parent->kobj;
  628. return &block_class.p->subsys.kobj;
  629. }
  630. #endif
  631. /*
  632. * If we have no parent, we live in "virtual".
  633. * Class-devices with a non class-device as parent, live
  634. * in a "glue" directory to prevent namespace collisions.
  635. */
  636. if (parent == NULL)
  637. parent_kobj = virtual_device_parent(dev);
  638. else if (parent->class && !dev->class->ns_type)
  639. return &parent->kobj;
  640. else
  641. parent_kobj = &parent->kobj;
  642. mutex_lock(&gdp_mutex);
  643. /* find our class-directory at the parent and reference it */
  644. spin_lock(&dev->class->p->glue_dirs.list_lock);
  645. list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
  646. if (k->parent == parent_kobj) {
  647. kobj = kobject_get(k);
  648. break;
  649. }
  650. spin_unlock(&dev->class->p->glue_dirs.list_lock);
  651. if (kobj) {
  652. mutex_unlock(&gdp_mutex);
  653. return kobj;
  654. }
  655. /* or create a new class-directory at the parent device */
  656. k = class_dir_create_and_add(dev->class, parent_kobj);
  657. /* do not emit an uevent for this simple "glue" directory */
  658. mutex_unlock(&gdp_mutex);
  659. return k;
  660. }
  661. /* subsystems can specify a default root directory for their devices */
  662. if (!parent && dev->bus && dev->bus->dev_root)
  663. return &dev->bus->dev_root->kobj;
  664. if (parent)
  665. return &parent->kobj;
  666. return NULL;
  667. }
  668. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  669. {
  670. /* see if we live in a "glue" directory */
  671. if (!glue_dir || !dev->class ||
  672. glue_dir->kset != &dev->class->p->glue_dirs)
  673. return;
  674. kobject_put(glue_dir);
  675. }
  676. static void cleanup_device_parent(struct device *dev)
  677. {
  678. cleanup_glue_dir(dev, dev->kobj.parent);
  679. }
  680. static int device_add_class_symlinks(struct device *dev)
  681. {
  682. int error;
  683. if (!dev->class)
  684. return 0;
  685. error = sysfs_create_link(&dev->kobj,
  686. &dev->class->p->subsys.kobj,
  687. "subsystem");
  688. if (error)
  689. goto out;
  690. if (dev->parent && device_is_not_partition(dev)) {
  691. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  692. "device");
  693. if (error)
  694. goto out_subsys;
  695. }
  696. #ifdef CONFIG_BLOCK
  697. /* /sys/block has directories and does not need symlinks */
  698. if (sysfs_deprecated && dev->class == &block_class)
  699. return 0;
  700. #endif
  701. /* link in the class directory pointing to the device */
  702. error = sysfs_create_link(&dev->class->p->subsys.kobj,
  703. &dev->kobj, dev_name(dev));
  704. if (error)
  705. goto out_device;
  706. return 0;
  707. out_device:
  708. sysfs_remove_link(&dev->kobj, "device");
  709. out_subsys:
  710. sysfs_remove_link(&dev->kobj, "subsystem");
  711. out:
  712. return error;
  713. }
  714. static void device_remove_class_symlinks(struct device *dev)
  715. {
  716. if (!dev->class)
  717. return;
  718. if (dev->parent && device_is_not_partition(dev))
  719. sysfs_remove_link(&dev->kobj, "device");
  720. sysfs_remove_link(&dev->kobj, "subsystem");
  721. #ifdef CONFIG_BLOCK
  722. if (sysfs_deprecated && dev->class == &block_class)
  723. return;
  724. #endif
  725. sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
  726. }
  727. /**
  728. * dev_set_name - set a device name
  729. * @dev: device
  730. * @fmt: format string for the device's name
  731. */
  732. int dev_set_name(struct device *dev, const char *fmt, ...)
  733. {
  734. va_list vargs;
  735. int err;
  736. va_start(vargs, fmt);
  737. err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  738. va_end(vargs);
  739. return err;
  740. }
  741. EXPORT_SYMBOL_GPL(dev_set_name);
  742. /**
  743. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  744. * @dev: device
  745. *
  746. * By default we select char/ for new entries. Setting class->dev_obj
  747. * to NULL prevents an entry from being created. class->dev_kobj must
  748. * be set (or cleared) before any devices are registered to the class
  749. * otherwise device_create_sys_dev_entry() and
  750. * device_remove_sys_dev_entry() will disagree about the the presence
  751. * of the link.
  752. */
  753. static struct kobject *device_to_dev_kobj(struct device *dev)
  754. {
  755. struct kobject *kobj;
  756. if (dev->class)
  757. kobj = dev->class->dev_kobj;
  758. else
  759. kobj = sysfs_dev_char_kobj;
  760. return kobj;
  761. }
  762. static int device_create_sys_dev_entry(struct device *dev)
  763. {
  764. struct kobject *kobj = device_to_dev_kobj(dev);
  765. int error = 0;
  766. char devt_str[15];
  767. if (kobj) {
  768. format_dev_t(devt_str, dev->devt);
  769. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  770. }
  771. return error;
  772. }
  773. static void device_remove_sys_dev_entry(struct device *dev)
  774. {
  775. struct kobject *kobj = device_to_dev_kobj(dev);
  776. char devt_str[15];
  777. if (kobj) {
  778. format_dev_t(devt_str, dev->devt);
  779. sysfs_remove_link(kobj, devt_str);
  780. }
  781. }
  782. int device_private_init(struct device *dev)
  783. {
  784. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  785. if (!dev->p)
  786. return -ENOMEM;
  787. dev->p->device = dev;
  788. klist_init(&dev->p->klist_children, klist_children_get,
  789. klist_children_put);
  790. return 0;
  791. }
  792. /**
  793. * device_add - add device to device hierarchy.
  794. * @dev: device.
  795. *
  796. * This is part 2 of device_register(), though may be called
  797. * separately _iff_ device_initialize() has been called separately.
  798. *
  799. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  800. * to the global and sibling lists for the device, then
  801. * adds it to the other relevant subsystems of the driver model.
  802. *
  803. * Do not call this routine or device_register() more than once for
  804. * any device structure. The driver model core is not designed to work
  805. * with devices that get unregistered and then spring back to life.
  806. * (Among other things, it's very hard to guarantee that all references
  807. * to the previous incarnation of @dev have been dropped.) Allocate
  808. * and register a fresh new struct device instead.
  809. *
  810. * NOTE: _Never_ directly free @dev after calling this function, even
  811. * if it returned an error! Always use put_device() to give up your
  812. * reference instead.
  813. */
  814. int device_add(struct device *dev)
  815. {
  816. struct device *parent = NULL;
  817. struct kobject *kobj;
  818. struct class_interface *class_intf;
  819. int error = -EINVAL;
  820. dev = get_device(dev);
  821. if (!dev)
  822. goto done;
  823. if (!dev->p) {
  824. error = device_private_init(dev);
  825. if (error)
  826. goto done;
  827. }
  828. /*
  829. * for statically allocated devices, which should all be converted
  830. * some day, we need to initialize the name. We prevent reading back
  831. * the name, and force the use of dev_name()
  832. */
  833. if (dev->init_name) {
  834. dev_set_name(dev, "%s", dev->init_name);
  835. dev->init_name = NULL;
  836. }
  837. /* subsystems can specify simple device enumeration */
  838. if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
  839. dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
  840. if (!dev_name(dev)) {
  841. error = -EINVAL;
  842. goto name_error;
  843. }
  844. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  845. parent = get_device(dev->parent);
  846. kobj = get_device_parent(dev, parent);
  847. if (kobj)
  848. dev->kobj.parent = kobj;
  849. /* use parent numa_node */
  850. if (parent)
  851. set_dev_node(dev, dev_to_node(parent));
  852. /* first, register with generic layer. */
  853. /* we require the name to be set before, and pass NULL */
  854. error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  855. if (error)
  856. goto Error;
  857. /* notify platform of device entry */
  858. if (platform_notify)
  859. platform_notify(dev);
  860. error = device_create_file(dev, &uevent_attr);
  861. if (error)
  862. goto attrError;
  863. if (MAJOR(dev->devt)) {
  864. error = device_create_file(dev, &devt_attr);
  865. if (error)
  866. goto ueventattrError;
  867. error = device_create_sys_dev_entry(dev);
  868. if (error)
  869. goto devtattrError;
  870. devtmpfs_create_node(dev);
  871. }
  872. error = device_add_class_symlinks(dev);
  873. if (error)
  874. goto SymlinkError;
  875. error = device_add_attrs(dev);
  876. if (error)
  877. goto AttrsError;
  878. error = bus_add_device(dev);
  879. if (error)
  880. goto BusError;
  881. error = dpm_sysfs_add(dev);
  882. if (error)
  883. goto DPMError;
  884. device_pm_add(dev);
  885. /* Notify clients of device addition. This call must come
  886. * after dpm_sysfs_add() and before kobject_uevent().
  887. */
  888. if (dev->bus)
  889. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  890. BUS_NOTIFY_ADD_DEVICE, dev);
  891. kobject_uevent(&dev->kobj, KOBJ_ADD);
  892. bus_probe_device(dev);
  893. if (parent)
  894. klist_add_tail(&dev->p->knode_parent,
  895. &parent->p->klist_children);
  896. if (dev->class) {
  897. mutex_lock(&dev->class->p->mutex);
  898. /* tie the class to the device */
  899. klist_add_tail(&dev->knode_class,
  900. &dev->class->p->klist_devices);
  901. /* notify any interfaces that the device is here */
  902. list_for_each_entry(class_intf,
  903. &dev->class->p->interfaces, node)
  904. if (class_intf->add_dev)
  905. class_intf->add_dev(dev, class_intf);
  906. mutex_unlock(&dev->class->p->mutex);
  907. }
  908. done:
  909. put_device(dev);
  910. return error;
  911. DPMError:
  912. bus_remove_device(dev);
  913. BusError:
  914. device_remove_attrs(dev);
  915. AttrsError:
  916. device_remove_class_symlinks(dev);
  917. SymlinkError:
  918. if (MAJOR(dev->devt))
  919. devtmpfs_delete_node(dev);
  920. if (MAJOR(dev->devt))
  921. device_remove_sys_dev_entry(dev);
  922. devtattrError:
  923. if (MAJOR(dev->devt))
  924. device_remove_file(dev, &devt_attr);
  925. ueventattrError:
  926. device_remove_file(dev, &uevent_attr);
  927. attrError:
  928. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  929. kobject_del(&dev->kobj);
  930. Error:
  931. cleanup_device_parent(dev);
  932. if (parent)
  933. put_device(parent);
  934. name_error:
  935. kfree(dev->p);
  936. dev->p = NULL;
  937. goto done;
  938. }
  939. /**
  940. * device_register - register a device with the system.
  941. * @dev: pointer to the device structure
  942. *
  943. * This happens in two clean steps - initialize the device
  944. * and add it to the system. The two steps can be called
  945. * separately, but this is the easiest and most common.
  946. * I.e. you should only call the two helpers separately if
  947. * have a clearly defined need to use and refcount the device
  948. * before it is added to the hierarchy.
  949. *
  950. * For more information, see the kerneldoc for device_initialize()
  951. * and device_add().
  952. *
  953. * NOTE: _Never_ directly free @dev after calling this function, even
  954. * if it returned an error! Always use put_device() to give up the
  955. * reference initialized in this function instead.
  956. */
  957. int device_register(struct device *dev)
  958. {
  959. device_initialize(dev);
  960. return device_add(dev);
  961. }
  962. /**
  963. * get_device - increment reference count for device.
  964. * @dev: device.
  965. *
  966. * This simply forwards the call to kobject_get(), though
  967. * we do take care to provide for the case that we get a NULL
  968. * pointer passed in.
  969. */
  970. struct device *get_device(struct device *dev)
  971. {
  972. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  973. }
  974. /**
  975. * put_device - decrement reference count.
  976. * @dev: device in question.
  977. */
  978. void put_device(struct device *dev)
  979. {
  980. /* might_sleep(); */
  981. if (dev)
  982. kobject_put(&dev->kobj);
  983. }
  984. /**
  985. * device_del - delete device from system.
  986. * @dev: device.
  987. *
  988. * This is the first part of the device unregistration
  989. * sequence. This removes the device from the lists we control
  990. * from here, has it removed from the other driver model
  991. * subsystems it was added to in device_add(), and removes it
  992. * from the kobject hierarchy.
  993. *
  994. * NOTE: this should be called manually _iff_ device_add() was
  995. * also called manually.
  996. */
  997. void device_del(struct device *dev)
  998. {
  999. struct device *parent = dev->parent;
  1000. struct class_interface *class_intf;
  1001. /* Notify clients of device removal. This call must come
  1002. * before dpm_sysfs_remove().
  1003. */
  1004. if (dev->bus)
  1005. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1006. BUS_NOTIFY_DEL_DEVICE, dev);
  1007. device_pm_remove(dev);
  1008. dpm_sysfs_remove(dev);
  1009. if (parent)
  1010. klist_del(&dev->p->knode_parent);
  1011. if (MAJOR(dev->devt)) {
  1012. devtmpfs_delete_node(dev);
  1013. device_remove_sys_dev_entry(dev);
  1014. device_remove_file(dev, &devt_attr);
  1015. }
  1016. if (dev->class) {
  1017. device_remove_class_symlinks(dev);
  1018. mutex_lock(&dev->class->p->mutex);
  1019. /* notify any interfaces that the device is now gone */
  1020. list_for_each_entry(class_intf,
  1021. &dev->class->p->interfaces, node)
  1022. if (class_intf->remove_dev)
  1023. class_intf->remove_dev(dev, class_intf);
  1024. /* remove the device from the class list */
  1025. klist_del(&dev->knode_class);
  1026. mutex_unlock(&dev->class->p->mutex);
  1027. }
  1028. device_remove_file(dev, &uevent_attr);
  1029. device_remove_attrs(dev);
  1030. bus_remove_device(dev);
  1031. /*
  1032. * Some platform devices are driven without driver attached
  1033. * and managed resources may have been acquired. Make sure
  1034. * all resources are released.
  1035. */
  1036. devres_release_all(dev);
  1037. /* Notify the platform of the removal, in case they
  1038. * need to do anything...
  1039. */
  1040. if (platform_notify_remove)
  1041. platform_notify_remove(dev);
  1042. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  1043. cleanup_device_parent(dev);
  1044. kobject_del(&dev->kobj);
  1045. put_device(parent);
  1046. }
  1047. /**
  1048. * device_unregister - unregister device from system.
  1049. * @dev: device going away.
  1050. *
  1051. * We do this in two parts, like we do device_register(). First,
  1052. * we remove it from all the subsystems with device_del(), then
  1053. * we decrement the reference count via put_device(). If that
  1054. * is the final reference count, the device will be cleaned up
  1055. * via device_release() above. Otherwise, the structure will
  1056. * stick around until the final reference to the device is dropped.
  1057. */
  1058. void device_unregister(struct device *dev)
  1059. {
  1060. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1061. device_del(dev);
  1062. put_device(dev);
  1063. }
  1064. static struct device *next_device(struct klist_iter *i)
  1065. {
  1066. struct klist_node *n = klist_next(i);
  1067. struct device *dev = NULL;
  1068. struct device_private *p;
  1069. if (n) {
  1070. p = to_device_private_parent(n);
  1071. dev = p->device;
  1072. }
  1073. return dev;
  1074. }
  1075. /**
  1076. * device_get_devnode - path of device node file
  1077. * @dev: device
  1078. * @mode: returned file access mode
  1079. * @tmp: possibly allocated string
  1080. *
  1081. * Return the relative path of a possible device node.
  1082. * Non-default names may need to allocate a memory to compose
  1083. * a name. This memory is returned in tmp and needs to be
  1084. * freed by the caller.
  1085. */
  1086. const char *device_get_devnode(struct device *dev,
  1087. umode_t *mode, const char **tmp)
  1088. {
  1089. char *s;
  1090. *tmp = NULL;
  1091. /* the device type may provide a specific name */
  1092. if (dev->type && dev->type->devnode)
  1093. *tmp = dev->type->devnode(dev, mode);
  1094. if (*tmp)
  1095. return *tmp;
  1096. /* the class may provide a specific name */
  1097. if (dev->class && dev->class->devnode)
  1098. *tmp = dev->class->devnode(dev, mode);
  1099. if (*tmp)
  1100. return *tmp;
  1101. /* return name without allocation, tmp == NULL */
  1102. if (strchr(dev_name(dev), '!') == NULL)
  1103. return dev_name(dev);
  1104. /* replace '!' in the name with '/' */
  1105. *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
  1106. if (!*tmp)
  1107. return NULL;
  1108. while ((s = strchr(*tmp, '!')))
  1109. s[0] = '/';
  1110. return *tmp;
  1111. }
  1112. /**
  1113. * device_for_each_child - device child iterator.
  1114. * @parent: parent struct device.
  1115. * @data: data for the callback.
  1116. * @fn: function to be called for each device.
  1117. *
  1118. * Iterate over @parent's child devices, and call @fn for each,
  1119. * passing it @data.
  1120. *
  1121. * We check the return of @fn each time. If it returns anything
  1122. * other than 0, we break out and return that value.
  1123. */
  1124. int device_for_each_child(struct device *parent, void *data,
  1125. int (*fn)(struct device *dev, void *data))
  1126. {
  1127. struct klist_iter i;
  1128. struct device *child;
  1129. int error = 0;
  1130. if (!parent->p)
  1131. return 0;
  1132. klist_iter_init(&parent->p->klist_children, &i);
  1133. while ((child = next_device(&i)) && !error)
  1134. error = fn(child, data);
  1135. klist_iter_exit(&i);
  1136. return error;
  1137. }
  1138. /**
  1139. * device_find_child - device iterator for locating a particular device.
  1140. * @parent: parent struct device
  1141. * @data: Data to pass to match function
  1142. * @match: Callback function to check device
  1143. *
  1144. * This is similar to the device_for_each_child() function above, but it
  1145. * returns a reference to a device that is 'found' for later use, as
  1146. * determined by the @match callback.
  1147. *
  1148. * The callback should return 0 if the device doesn't match and non-zero
  1149. * if it does. If the callback returns non-zero and a reference to the
  1150. * current device can be obtained, this function will return to the caller
  1151. * and not iterate over any more devices.
  1152. */
  1153. struct device *device_find_child(struct device *parent, void *data,
  1154. int (*match)(struct device *dev, void *data))
  1155. {
  1156. struct klist_iter i;
  1157. struct device *child;
  1158. if (!parent)
  1159. return NULL;
  1160. klist_iter_init(&parent->p->klist_children, &i);
  1161. while ((child = next_device(&i)))
  1162. if (match(child, data) && get_device(child))
  1163. break;
  1164. klist_iter_exit(&i);
  1165. return child;
  1166. }
  1167. int __init devices_init(void)
  1168. {
  1169. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1170. if (!devices_kset)
  1171. return -ENOMEM;
  1172. dev_kobj = kobject_create_and_add("dev", NULL);
  1173. if (!dev_kobj)
  1174. goto dev_kobj_err;
  1175. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1176. if (!sysfs_dev_block_kobj)
  1177. goto block_kobj_err;
  1178. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1179. if (!sysfs_dev_char_kobj)
  1180. goto char_kobj_err;
  1181. return 0;
  1182. char_kobj_err:
  1183. kobject_put(sysfs_dev_block_kobj);
  1184. block_kobj_err:
  1185. kobject_put(dev_kobj);
  1186. dev_kobj_err:
  1187. kset_unregister(devices_kset);
  1188. return -ENOMEM;
  1189. }
  1190. EXPORT_SYMBOL_GPL(device_for_each_child);
  1191. EXPORT_SYMBOL_GPL(device_find_child);
  1192. EXPORT_SYMBOL_GPL(device_initialize);
  1193. EXPORT_SYMBOL_GPL(device_add);
  1194. EXPORT_SYMBOL_GPL(device_register);
  1195. EXPORT_SYMBOL_GPL(device_del);
  1196. EXPORT_SYMBOL_GPL(device_unregister);
  1197. EXPORT_SYMBOL_GPL(get_device);
  1198. EXPORT_SYMBOL_GPL(put_device);
  1199. EXPORT_SYMBOL_GPL(device_create_file);
  1200. EXPORT_SYMBOL_GPL(device_remove_file);
  1201. struct root_device {
  1202. struct device dev;
  1203. struct module *owner;
  1204. };
  1205. inline struct root_device *to_root_device(struct device *d)
  1206. {
  1207. return container_of(d, struct root_device, dev);
  1208. }
  1209. static void root_device_release(struct device *dev)
  1210. {
  1211. kfree(to_root_device(dev));
  1212. }
  1213. /**
  1214. * __root_device_register - allocate and register a root device
  1215. * @name: root device name
  1216. * @owner: owner module of the root device, usually THIS_MODULE
  1217. *
  1218. * This function allocates a root device and registers it
  1219. * using device_register(). In order to free the returned
  1220. * device, use root_device_unregister().
  1221. *
  1222. * Root devices are dummy devices which allow other devices
  1223. * to be grouped under /sys/devices. Use this function to
  1224. * allocate a root device and then use it as the parent of
  1225. * any device which should appear under /sys/devices/{name}
  1226. *
  1227. * The /sys/devices/{name} directory will also contain a
  1228. * 'module' symlink which points to the @owner directory
  1229. * in sysfs.
  1230. *
  1231. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1232. *
  1233. * Note: You probably want to use root_device_register().
  1234. */
  1235. struct device *__root_device_register(const char *name, struct module *owner)
  1236. {
  1237. struct root_device *root;
  1238. int err = -ENOMEM;
  1239. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  1240. if (!root)
  1241. return ERR_PTR(err);
  1242. err = dev_set_name(&root->dev, "%s", name);
  1243. if (err) {
  1244. kfree(root);
  1245. return ERR_PTR(err);
  1246. }
  1247. root->dev.release = root_device_release;
  1248. err = device_register(&root->dev);
  1249. if (err) {
  1250. put_device(&root->dev);
  1251. return ERR_PTR(err);
  1252. }
  1253. #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
  1254. if (owner) {
  1255. struct module_kobject *mk = &owner->mkobj;
  1256. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  1257. if (err) {
  1258. device_unregister(&root->dev);
  1259. return ERR_PTR(err);
  1260. }
  1261. root->owner = owner;
  1262. }
  1263. #endif
  1264. return &root->dev;
  1265. }
  1266. EXPORT_SYMBOL_GPL(__root_device_register);
  1267. /**
  1268. * root_device_unregister - unregister and free a root device
  1269. * @dev: device going away
  1270. *
  1271. * This function unregisters and cleans up a device that was created by
  1272. * root_device_register().
  1273. */
  1274. void root_device_unregister(struct device *dev)
  1275. {
  1276. struct root_device *root = to_root_device(dev);
  1277. if (root->owner)
  1278. sysfs_remove_link(&root->dev.kobj, "module");
  1279. device_unregister(dev);
  1280. }
  1281. EXPORT_SYMBOL_GPL(root_device_unregister);
  1282. static void device_create_release(struct device *dev)
  1283. {
  1284. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1285. kfree(dev);
  1286. }
  1287. /**
  1288. * device_create_vargs - creates a device and registers it with sysfs
  1289. * @class: pointer to the struct class that this device should be registered to
  1290. * @parent: pointer to the parent struct device of this new device, if any
  1291. * @devt: the dev_t for the char device to be added
  1292. * @drvdata: the data to be added to the device for callbacks
  1293. * @fmt: string for the device's name
  1294. * @args: va_list for the device's name
  1295. *
  1296. * This function can be used by char device classes. A struct device
  1297. * will be created in sysfs, registered to the specified class.
  1298. *
  1299. * A "dev" file will be created, showing the dev_t for the device, if
  1300. * the dev_t is not 0,0.
  1301. * If a pointer to a parent struct device is passed in, the newly created
  1302. * struct device will be a child of that device in sysfs.
  1303. * The pointer to the struct device will be returned from the call.
  1304. * Any further sysfs files that might be required can be created using this
  1305. * pointer.
  1306. *
  1307. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1308. *
  1309. * Note: the struct class passed to this function must have previously
  1310. * been created with a call to class_create().
  1311. */
  1312. struct device *device_create_vargs(struct class *class, struct device *parent,
  1313. dev_t devt, void *drvdata, const char *fmt,
  1314. va_list args)
  1315. {
  1316. struct device *dev = NULL;
  1317. int retval = -ENODEV;
  1318. if (class == NULL || IS_ERR(class))
  1319. goto error;
  1320. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1321. if (!dev) {
  1322. retval = -ENOMEM;
  1323. goto error;
  1324. }
  1325. dev->devt = devt;
  1326. dev->class = class;
  1327. dev->parent = parent;
  1328. dev->release = device_create_release;
  1329. dev_set_drvdata(dev, drvdata);
  1330. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  1331. if (retval)
  1332. goto error;
  1333. retval = device_register(dev);
  1334. if (retval)
  1335. goto error;
  1336. return dev;
  1337. error:
  1338. put_device(dev);
  1339. return ERR_PTR(retval);
  1340. }
  1341. EXPORT_SYMBOL_GPL(device_create_vargs);
  1342. /**
  1343. * device_create - creates a device and registers it with sysfs
  1344. * @class: pointer to the struct class that this device should be registered to
  1345. * @parent: pointer to the parent struct device of this new device, if any
  1346. * @devt: the dev_t for the char device to be added
  1347. * @drvdata: the data to be added to the device for callbacks
  1348. * @fmt: string for the device's name
  1349. *
  1350. * This function can be used by char device classes. A struct device
  1351. * will be created in sysfs, registered to the specified class.
  1352. *
  1353. * A "dev" file will be created, showing the dev_t for the device, if
  1354. * the dev_t is not 0,0.
  1355. * If a pointer to a parent struct device is passed in, the newly created
  1356. * struct device will be a child of that device in sysfs.
  1357. * The pointer to the struct device will be returned from the call.
  1358. * Any further sysfs files that might be required can be created using this
  1359. * pointer.
  1360. *
  1361. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1362. *
  1363. * Note: the struct class passed to this function must have previously
  1364. * been created with a call to class_create().
  1365. */
  1366. struct device *device_create(struct class *class, struct device *parent,
  1367. dev_t devt, void *drvdata, const char *fmt, ...)
  1368. {
  1369. va_list vargs;
  1370. struct device *dev;
  1371. va_start(vargs, fmt);
  1372. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1373. va_end(vargs);
  1374. return dev;
  1375. }
  1376. EXPORT_SYMBOL_GPL(device_create);
  1377. static int __match_devt(struct device *dev, void *data)
  1378. {
  1379. dev_t *devt = data;
  1380. return dev->devt == *devt;
  1381. }
  1382. /**
  1383. * device_destroy - removes a device that was created with device_create()
  1384. * @class: pointer to the struct class that this device was registered with
  1385. * @devt: the dev_t of the device that was previously registered
  1386. *
  1387. * This call unregisters and cleans up a device that was created with a
  1388. * call to device_create().
  1389. */
  1390. void device_destroy(struct class *class, dev_t devt)
  1391. {
  1392. struct device *dev;
  1393. dev = class_find_device(class, NULL, &devt, __match_devt);
  1394. if (dev) {
  1395. put_device(dev);
  1396. device_unregister(dev);
  1397. }
  1398. }
  1399. EXPORT_SYMBOL_GPL(device_destroy);
  1400. /**
  1401. * device_rename - renames a device
  1402. * @dev: the pointer to the struct device to be renamed
  1403. * @new_name: the new name of the device
  1404. *
  1405. * It is the responsibility of the caller to provide mutual
  1406. * exclusion between two different calls of device_rename
  1407. * on the same device to ensure that new_name is valid and
  1408. * won't conflict with other devices.
  1409. *
  1410. * Note: Don't call this function. Currently, the networking layer calls this
  1411. * function, but that will change. The following text from Kay Sievers offers
  1412. * some insight:
  1413. *
  1414. * Renaming devices is racy at many levels, symlinks and other stuff are not
  1415. * replaced atomically, and you get a "move" uevent, but it's not easy to
  1416. * connect the event to the old and new device. Device nodes are not renamed at
  1417. * all, there isn't even support for that in the kernel now.
  1418. *
  1419. * In the meantime, during renaming, your target name might be taken by another
  1420. * driver, creating conflicts. Or the old name is taken directly after you
  1421. * renamed it -- then you get events for the same DEVPATH, before you even see
  1422. * the "move" event. It's just a mess, and nothing new should ever rely on
  1423. * kernel device renaming. Besides that, it's not even implemented now for
  1424. * other things than (driver-core wise very simple) network devices.
  1425. *
  1426. * We are currently about to change network renaming in udev to completely
  1427. * disallow renaming of devices in the same namespace as the kernel uses,
  1428. * because we can't solve the problems properly, that arise with swapping names
  1429. * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
  1430. * be allowed to some other name than eth[0-9]*, for the aforementioned
  1431. * reasons.
  1432. *
  1433. * Make up a "real" name in the driver before you register anything, or add
  1434. * some other attributes for userspace to find the device, or use udev to add
  1435. * symlinks -- but never rename kernel devices later, it's a complete mess. We
  1436. * don't even want to get into that and try to implement the missing pieces in
  1437. * the core. We really have other pieces to fix in the driver core mess. :)
  1438. */
  1439. int device_rename(struct device *dev, const char *new_name)
  1440. {
  1441. char *old_class_name = NULL;
  1442. char *new_class_name = NULL;
  1443. char *old_device_name = NULL;
  1444. int error;
  1445. dev = get_device(dev);
  1446. if (!dev)
  1447. return -EINVAL;
  1448. pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
  1449. __func__, new_name);
  1450. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  1451. if (!old_device_name) {
  1452. error = -ENOMEM;
  1453. goto out;
  1454. }
  1455. if (dev->class) {
  1456. error = sysfs_rename_link(&dev->class->p->subsys.kobj,
  1457. &dev->kobj, old_device_name, new_name);
  1458. if (error)
  1459. goto out;
  1460. }
  1461. error = kobject_rename(&dev->kobj, new_name);
  1462. if (error)
  1463. goto out;
  1464. out:
  1465. put_device(dev);
  1466. kfree(new_class_name);
  1467. kfree(old_class_name);
  1468. kfree(old_device_name);
  1469. return error;
  1470. }
  1471. EXPORT_SYMBOL_GPL(device_rename);
  1472. static int device_move_class_links(struct device *dev,
  1473. struct device *old_parent,
  1474. struct device *new_parent)
  1475. {
  1476. int error = 0;
  1477. if (old_parent)
  1478. sysfs_remove_link(&dev->kobj, "device");
  1479. if (new_parent)
  1480. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1481. "device");
  1482. return error;
  1483. }
  1484. /**
  1485. * device_move - moves a device to a new parent
  1486. * @dev: the pointer to the struct device to be moved
  1487. * @new_parent: the new parent of the device (can by NULL)
  1488. * @dpm_order: how to reorder the dpm_list
  1489. */
  1490. int device_move(struct device *dev, struct device *new_parent,
  1491. enum dpm_order dpm_order)
  1492. {
  1493. int error;
  1494. struct device *old_parent;
  1495. struct kobject *new_parent_kobj;
  1496. dev = get_device(dev);
  1497. if (!dev)
  1498. return -EINVAL;
  1499. device_pm_lock();
  1500. new_parent = get_device(new_parent);
  1501. new_parent_kobj = get_device_parent(dev, new_parent);
  1502. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  1503. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  1504. error = kobject_move(&dev->kobj, new_parent_kobj);
  1505. if (error) {
  1506. cleanup_glue_dir(dev, new_parent_kobj);
  1507. put_device(new_parent);
  1508. goto out;
  1509. }
  1510. old_parent = dev->parent;
  1511. dev->parent = new_parent;
  1512. if (old_parent)
  1513. klist_remove(&dev->p->knode_parent);
  1514. if (new_parent) {
  1515. klist_add_tail(&dev->p->knode_parent,
  1516. &new_parent->p->klist_children);
  1517. set_dev_node(dev, dev_to_node(new_parent));
  1518. }
  1519. if (!dev->class)
  1520. goto out_put;
  1521. error = device_move_class_links(dev, old_parent, new_parent);
  1522. if (error) {
  1523. /* We ignore errors on cleanup since we're hosed anyway... */
  1524. device_move_class_links(dev, new_parent, old_parent);
  1525. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1526. if (new_parent)
  1527. klist_remove(&dev->p->knode_parent);
  1528. dev->parent = old_parent;
  1529. if (old_parent) {
  1530. klist_add_tail(&dev->p->knode_parent,
  1531. &old_parent->p->klist_children);
  1532. set_dev_node(dev, dev_to_node(old_parent));
  1533. }
  1534. }
  1535. cleanup_glue_dir(dev, new_parent_kobj);
  1536. put_device(new_parent);
  1537. goto out;
  1538. }
  1539. switch (dpm_order) {
  1540. case DPM_ORDER_NONE:
  1541. break;
  1542. case DPM_ORDER_DEV_AFTER_PARENT:
  1543. device_pm_move_after(dev, new_parent);
  1544. break;
  1545. case DPM_ORDER_PARENT_BEFORE_DEV:
  1546. device_pm_move_before(new_parent, dev);
  1547. break;
  1548. case DPM_ORDER_DEV_LAST:
  1549. device_pm_move_last(dev);
  1550. break;
  1551. }
  1552. out_put:
  1553. put_device(old_parent);
  1554. out:
  1555. device_pm_unlock();
  1556. put_device(dev);
  1557. return error;
  1558. }
  1559. EXPORT_SYMBOL_GPL(device_move);
  1560. /**
  1561. * device_shutdown - call ->shutdown() on each device to shutdown.
  1562. */
  1563. void device_shutdown(void)
  1564. {
  1565. struct device *dev;
  1566. spin_lock(&devices_kset->list_lock);
  1567. /*
  1568. * Walk the devices list backward, shutting down each in turn.
  1569. * Beware that device unplug events may also start pulling
  1570. * devices offline, even as the system is shutting down.
  1571. */
  1572. while (!list_empty(&devices_kset->list)) {
  1573. dev = list_entry(devices_kset->list.prev, struct device,
  1574. kobj.entry);
  1575. get_device(dev);
  1576. /*
  1577. * Make sure the device is off the kset list, in the
  1578. * event that dev->*->shutdown() doesn't remove it.
  1579. */
  1580. list_del_init(&dev->kobj.entry);
  1581. spin_unlock(&devices_kset->list_lock);
  1582. /* Don't allow any more runtime suspends */
  1583. pm_runtime_get_noresume(dev);
  1584. pm_runtime_barrier(dev);
  1585. if (dev->bus && dev->bus->shutdown) {
  1586. dev_dbg(dev, "shutdown\n");
  1587. dev->bus->shutdown(dev);
  1588. } else if (dev->driver && dev->driver->shutdown) {
  1589. dev_dbg(dev, "shutdown\n");
  1590. dev->driver->shutdown(dev);
  1591. }
  1592. put_device(dev);
  1593. spin_lock(&devices_kset->list_lock);
  1594. }
  1595. spin_unlock(&devices_kset->list_lock);
  1596. async_synchronize_full();
  1597. }
  1598. /*
  1599. * Device logging functions
  1600. */
  1601. #ifdef CONFIG_PRINTK
  1602. int __dev_printk(const char *level, const struct device *dev,
  1603. struct va_format *vaf)
  1604. {
  1605. if (!dev)
  1606. return printk("%s(NULL device *): %pV", level, vaf);
  1607. return printk("%s%s %s: %pV",
  1608. level, dev_driver_string(dev), dev_name(dev), vaf);
  1609. }
  1610. EXPORT_SYMBOL(__dev_printk);
  1611. int dev_printk(const char *level, const struct device *dev,
  1612. const char *fmt, ...)
  1613. {
  1614. struct va_format vaf;
  1615. va_list args;
  1616. int r;
  1617. va_start(args, fmt);
  1618. vaf.fmt = fmt;
  1619. vaf.va = &args;
  1620. r = __dev_printk(level, dev, &vaf);
  1621. va_end(args);
  1622. return r;
  1623. }
  1624. EXPORT_SYMBOL(dev_printk);
  1625. #define define_dev_printk_level(func, kern_level) \
  1626. int func(const struct device *dev, const char *fmt, ...) \
  1627. { \
  1628. struct va_format vaf; \
  1629. va_list args; \
  1630. int r; \
  1631. \
  1632. va_start(args, fmt); \
  1633. \
  1634. vaf.fmt = fmt; \
  1635. vaf.va = &args; \
  1636. \
  1637. r = __dev_printk(kern_level, dev, &vaf); \
  1638. va_end(args); \
  1639. \
  1640. return r; \
  1641. } \
  1642. EXPORT_SYMBOL(func);
  1643. define_dev_printk_level(dev_emerg, KERN_EMERG);
  1644. define_dev_printk_level(dev_alert, KERN_ALERT);
  1645. define_dev_printk_level(dev_crit, KERN_CRIT);
  1646. define_dev_printk_level(dev_err, KERN_ERR);
  1647. define_dev_printk_level(dev_warn, KERN_WARNING);
  1648. define_dev_printk_level(dev_notice, KERN_NOTICE);
  1649. define_dev_printk_level(_dev_info, KERN_INFO);
  1650. #endif