core.c 56 KB

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