core.c 51 KB

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