core.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  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. * devices_kset_move_before - Move device in the devices_kset's list.
  454. * @deva: Device to move.
  455. * @devb: Device @deva should come before.
  456. */
  457. static void devices_kset_move_before(struct device *deva, struct device *devb)
  458. {
  459. if (!devices_kset)
  460. return;
  461. pr_debug("devices_kset: Moving %s before %s\n",
  462. dev_name(deva), dev_name(devb));
  463. spin_lock(&devices_kset->list_lock);
  464. list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
  465. spin_unlock(&devices_kset->list_lock);
  466. }
  467. /**
  468. * devices_kset_move_after - Move device in the devices_kset's list.
  469. * @deva: Device to move
  470. * @devb: Device @deva should come after.
  471. */
  472. static void devices_kset_move_after(struct device *deva, struct device *devb)
  473. {
  474. if (!devices_kset)
  475. return;
  476. pr_debug("devices_kset: Moving %s after %s\n",
  477. dev_name(deva), dev_name(devb));
  478. spin_lock(&devices_kset->list_lock);
  479. list_move(&deva->kobj.entry, &devb->kobj.entry);
  480. spin_unlock(&devices_kset->list_lock);
  481. }
  482. /**
  483. * devices_kset_move_last - move the device to the end of devices_kset's list.
  484. * @dev: device to move
  485. */
  486. void devices_kset_move_last(struct device *dev)
  487. {
  488. if (!devices_kset)
  489. return;
  490. pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
  491. spin_lock(&devices_kset->list_lock);
  492. list_move_tail(&dev->kobj.entry, &devices_kset->list);
  493. spin_unlock(&devices_kset->list_lock);
  494. }
  495. /**
  496. * device_create_file - create sysfs attribute file for device.
  497. * @dev: device.
  498. * @attr: device attribute descriptor.
  499. */
  500. int device_create_file(struct device *dev,
  501. const struct device_attribute *attr)
  502. {
  503. int error = 0;
  504. if (dev) {
  505. WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
  506. "Attribute %s: write permission without 'store'\n",
  507. attr->attr.name);
  508. WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
  509. "Attribute %s: read permission without 'show'\n",
  510. attr->attr.name);
  511. error = sysfs_create_file(&dev->kobj, &attr->attr);
  512. }
  513. return error;
  514. }
  515. EXPORT_SYMBOL_GPL(device_create_file);
  516. /**
  517. * device_remove_file - remove sysfs attribute file.
  518. * @dev: device.
  519. * @attr: device attribute descriptor.
  520. */
  521. void device_remove_file(struct device *dev,
  522. const struct device_attribute *attr)
  523. {
  524. if (dev)
  525. sysfs_remove_file(&dev->kobj, &attr->attr);
  526. }
  527. EXPORT_SYMBOL_GPL(device_remove_file);
  528. /**
  529. * device_remove_file_self - remove sysfs attribute file from its own method.
  530. * @dev: device.
  531. * @attr: device attribute descriptor.
  532. *
  533. * See kernfs_remove_self() for details.
  534. */
  535. bool device_remove_file_self(struct device *dev,
  536. const struct device_attribute *attr)
  537. {
  538. if (dev)
  539. return sysfs_remove_file_self(&dev->kobj, &attr->attr);
  540. else
  541. return false;
  542. }
  543. EXPORT_SYMBOL_GPL(device_remove_file_self);
  544. /**
  545. * device_create_bin_file - create sysfs binary attribute file for device.
  546. * @dev: device.
  547. * @attr: device binary attribute descriptor.
  548. */
  549. int device_create_bin_file(struct device *dev,
  550. const struct bin_attribute *attr)
  551. {
  552. int error = -EINVAL;
  553. if (dev)
  554. error = sysfs_create_bin_file(&dev->kobj, attr);
  555. return error;
  556. }
  557. EXPORT_SYMBOL_GPL(device_create_bin_file);
  558. /**
  559. * device_remove_bin_file - remove sysfs binary attribute file
  560. * @dev: device.
  561. * @attr: device binary attribute descriptor.
  562. */
  563. void device_remove_bin_file(struct device *dev,
  564. const struct bin_attribute *attr)
  565. {
  566. if (dev)
  567. sysfs_remove_bin_file(&dev->kobj, attr);
  568. }
  569. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  570. static void klist_children_get(struct klist_node *n)
  571. {
  572. struct device_private *p = to_device_private_parent(n);
  573. struct device *dev = p->device;
  574. get_device(dev);
  575. }
  576. static void klist_children_put(struct klist_node *n)
  577. {
  578. struct device_private *p = to_device_private_parent(n);
  579. struct device *dev = p->device;
  580. put_device(dev);
  581. }
  582. /**
  583. * device_initialize - init device structure.
  584. * @dev: device.
  585. *
  586. * This prepares the device for use by other layers by initializing
  587. * its fields.
  588. * It is the first half of device_register(), if called by
  589. * that function, though it can also be called separately, so one
  590. * may use @dev's fields. In particular, get_device()/put_device()
  591. * may be used for reference counting of @dev after calling this
  592. * function.
  593. *
  594. * All fields in @dev must be initialized by the caller to 0, except
  595. * for those explicitly set to some other value. The simplest
  596. * approach is to use kzalloc() to allocate the structure containing
  597. * @dev.
  598. *
  599. * NOTE: Use put_device() to give up your reference instead of freeing
  600. * @dev directly once you have called this function.
  601. */
  602. void device_initialize(struct device *dev)
  603. {
  604. dev->kobj.kset = devices_kset;
  605. kobject_init(&dev->kobj, &device_ktype);
  606. INIT_LIST_HEAD(&dev->dma_pools);
  607. mutex_init(&dev->mutex);
  608. lockdep_set_novalidate_class(&dev->mutex);
  609. spin_lock_init(&dev->devres_lock);
  610. INIT_LIST_HEAD(&dev->devres_head);
  611. device_pm_init(dev);
  612. set_dev_node(dev, -1);
  613. #ifdef CONFIG_GENERIC_MSI_IRQ
  614. INIT_LIST_HEAD(&dev->msi_list);
  615. #endif
  616. }
  617. EXPORT_SYMBOL_GPL(device_initialize);
  618. struct kobject *virtual_device_parent(struct device *dev)
  619. {
  620. static struct kobject *virtual_dir = NULL;
  621. if (!virtual_dir)
  622. virtual_dir = kobject_create_and_add("virtual",
  623. &devices_kset->kobj);
  624. return virtual_dir;
  625. }
  626. struct class_dir {
  627. struct kobject kobj;
  628. struct class *class;
  629. };
  630. #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
  631. static void class_dir_release(struct kobject *kobj)
  632. {
  633. struct class_dir *dir = to_class_dir(kobj);
  634. kfree(dir);
  635. }
  636. static const
  637. struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
  638. {
  639. struct class_dir *dir = to_class_dir(kobj);
  640. return dir->class->ns_type;
  641. }
  642. static struct kobj_type class_dir_ktype = {
  643. .release = class_dir_release,
  644. .sysfs_ops = &kobj_sysfs_ops,
  645. .child_ns_type = class_dir_child_ns_type
  646. };
  647. static struct kobject *
  648. class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
  649. {
  650. struct class_dir *dir;
  651. int retval;
  652. dir = kzalloc(sizeof(*dir), GFP_KERNEL);
  653. if (!dir)
  654. return ERR_PTR(-ENOMEM);
  655. dir->class = class;
  656. kobject_init(&dir->kobj, &class_dir_ktype);
  657. dir->kobj.kset = &class->p->glue_dirs;
  658. retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
  659. if (retval < 0) {
  660. kobject_put(&dir->kobj);
  661. return ERR_PTR(retval);
  662. }
  663. return &dir->kobj;
  664. }
  665. static DEFINE_MUTEX(gdp_mutex);
  666. static struct kobject *get_device_parent(struct device *dev,
  667. struct device *parent)
  668. {
  669. if (dev->class) {
  670. struct kobject *kobj = NULL;
  671. struct kobject *parent_kobj;
  672. struct kobject *k;
  673. #ifdef CONFIG_BLOCK
  674. /* block disks show up in /sys/block */
  675. if (sysfs_deprecated && dev->class == &block_class) {
  676. if (parent && parent->class == &block_class)
  677. return &parent->kobj;
  678. return &block_class.p->subsys.kobj;
  679. }
  680. #endif
  681. /*
  682. * If we have no parent, we live in "virtual".
  683. * Class-devices with a non class-device as parent, live
  684. * in a "glue" directory to prevent namespace collisions.
  685. */
  686. if (parent == NULL)
  687. parent_kobj = virtual_device_parent(dev);
  688. else if (parent->class && !dev->class->ns_type)
  689. return &parent->kobj;
  690. else
  691. parent_kobj = &parent->kobj;
  692. mutex_lock(&gdp_mutex);
  693. /* find our class-directory at the parent and reference it */
  694. spin_lock(&dev->class->p->glue_dirs.list_lock);
  695. list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
  696. if (k->parent == parent_kobj) {
  697. kobj = kobject_get(k);
  698. break;
  699. }
  700. spin_unlock(&dev->class->p->glue_dirs.list_lock);
  701. if (kobj) {
  702. mutex_unlock(&gdp_mutex);
  703. return kobj;
  704. }
  705. /* or create a new class-directory at the parent device */
  706. k = class_dir_create_and_add(dev->class, parent_kobj);
  707. /* do not emit an uevent for this simple "glue" directory */
  708. mutex_unlock(&gdp_mutex);
  709. return k;
  710. }
  711. /* subsystems can specify a default root directory for their devices */
  712. if (!parent && dev->bus && dev->bus->dev_root)
  713. return &dev->bus->dev_root->kobj;
  714. if (parent)
  715. return &parent->kobj;
  716. return NULL;
  717. }
  718. static inline bool live_in_glue_dir(struct kobject *kobj,
  719. struct device *dev)
  720. {
  721. if (!kobj || !dev->class ||
  722. kobj->kset != &dev->class->p->glue_dirs)
  723. return false;
  724. return true;
  725. }
  726. static inline struct kobject *get_glue_dir(struct device *dev)
  727. {
  728. return dev->kobj.parent;
  729. }
  730. /*
  731. * make sure cleaning up dir as the last step, we need to make
  732. * sure .release handler of kobject is run with holding the
  733. * global lock
  734. */
  735. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  736. {
  737. /* see if we live in a "glue" directory */
  738. if (!live_in_glue_dir(glue_dir, dev))
  739. return;
  740. mutex_lock(&gdp_mutex);
  741. if (!kobject_has_children(glue_dir))
  742. kobject_del(glue_dir);
  743. kobject_put(glue_dir);
  744. mutex_unlock(&gdp_mutex);
  745. }
  746. static int device_add_class_symlinks(struct device *dev)
  747. {
  748. struct device_node *of_node = dev_of_node(dev);
  749. int error;
  750. if (of_node) {
  751. error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
  752. if (error)
  753. dev_warn(dev, "Error %d creating of_node link\n",error);
  754. /* An error here doesn't warrant bringing down the device */
  755. }
  756. if (!dev->class)
  757. return 0;
  758. error = sysfs_create_link(&dev->kobj,
  759. &dev->class->p->subsys.kobj,
  760. "subsystem");
  761. if (error)
  762. goto out_devnode;
  763. if (dev->parent && device_is_not_partition(dev)) {
  764. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  765. "device");
  766. if (error)
  767. goto out_subsys;
  768. }
  769. #ifdef CONFIG_BLOCK
  770. /* /sys/block has directories and does not need symlinks */
  771. if (sysfs_deprecated && dev->class == &block_class)
  772. return 0;
  773. #endif
  774. /* link in the class directory pointing to the device */
  775. error = sysfs_create_link(&dev->class->p->subsys.kobj,
  776. &dev->kobj, dev_name(dev));
  777. if (error)
  778. goto out_device;
  779. return 0;
  780. out_device:
  781. sysfs_remove_link(&dev->kobj, "device");
  782. out_subsys:
  783. sysfs_remove_link(&dev->kobj, "subsystem");
  784. out_devnode:
  785. sysfs_remove_link(&dev->kobj, "of_node");
  786. return error;
  787. }
  788. static void device_remove_class_symlinks(struct device *dev)
  789. {
  790. if (dev_of_node(dev))
  791. sysfs_remove_link(&dev->kobj, "of_node");
  792. if (!dev->class)
  793. return;
  794. if (dev->parent && device_is_not_partition(dev))
  795. sysfs_remove_link(&dev->kobj, "device");
  796. sysfs_remove_link(&dev->kobj, "subsystem");
  797. #ifdef CONFIG_BLOCK
  798. if (sysfs_deprecated && dev->class == &block_class)
  799. return;
  800. #endif
  801. sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
  802. }
  803. /**
  804. * dev_set_name - set a device name
  805. * @dev: device
  806. * @fmt: format string for the device's name
  807. */
  808. int dev_set_name(struct device *dev, const char *fmt, ...)
  809. {
  810. va_list vargs;
  811. int err;
  812. va_start(vargs, fmt);
  813. err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  814. va_end(vargs);
  815. return err;
  816. }
  817. EXPORT_SYMBOL_GPL(dev_set_name);
  818. /**
  819. * device_to_dev_kobj - select a /sys/dev/ directory for the device
  820. * @dev: device
  821. *
  822. * By default we select char/ for new entries. Setting class->dev_obj
  823. * to NULL prevents an entry from being created. class->dev_kobj must
  824. * be set (or cleared) before any devices are registered to the class
  825. * otherwise device_create_sys_dev_entry() and
  826. * device_remove_sys_dev_entry() will disagree about the presence of
  827. * the link.
  828. */
  829. static struct kobject *device_to_dev_kobj(struct device *dev)
  830. {
  831. struct kobject *kobj;
  832. if (dev->class)
  833. kobj = dev->class->dev_kobj;
  834. else
  835. kobj = sysfs_dev_char_kobj;
  836. return kobj;
  837. }
  838. static int device_create_sys_dev_entry(struct device *dev)
  839. {
  840. struct kobject *kobj = device_to_dev_kobj(dev);
  841. int error = 0;
  842. char devt_str[15];
  843. if (kobj) {
  844. format_dev_t(devt_str, dev->devt);
  845. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  846. }
  847. return error;
  848. }
  849. static void device_remove_sys_dev_entry(struct device *dev)
  850. {
  851. struct kobject *kobj = device_to_dev_kobj(dev);
  852. char devt_str[15];
  853. if (kobj) {
  854. format_dev_t(devt_str, dev->devt);
  855. sysfs_remove_link(kobj, devt_str);
  856. }
  857. }
  858. int device_private_init(struct device *dev)
  859. {
  860. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  861. if (!dev->p)
  862. return -ENOMEM;
  863. dev->p->device = dev;
  864. klist_init(&dev->p->klist_children, klist_children_get,
  865. klist_children_put);
  866. INIT_LIST_HEAD(&dev->p->deferred_probe);
  867. return 0;
  868. }
  869. /**
  870. * device_add - add device to device hierarchy.
  871. * @dev: device.
  872. *
  873. * This is part 2 of device_register(), though may be called
  874. * separately _iff_ device_initialize() has been called separately.
  875. *
  876. * This adds @dev to the kobject hierarchy via kobject_add(), adds it
  877. * to the global and sibling lists for the device, then
  878. * adds it to the other relevant subsystems of the driver model.
  879. *
  880. * Do not call this routine or device_register() more than once for
  881. * any device structure. The driver model core is not designed to work
  882. * with devices that get unregistered and then spring back to life.
  883. * (Among other things, it's very hard to guarantee that all references
  884. * to the previous incarnation of @dev have been dropped.) Allocate
  885. * and register a fresh new struct device instead.
  886. *
  887. * NOTE: _Never_ directly free @dev after calling this function, even
  888. * if it returned an error! Always use put_device() to give up your
  889. * reference instead.
  890. */
  891. int device_add(struct device *dev)
  892. {
  893. struct device *parent = NULL;
  894. struct kobject *kobj;
  895. struct class_interface *class_intf;
  896. int error = -EINVAL;
  897. struct kobject *glue_dir = NULL;
  898. dev = get_device(dev);
  899. if (!dev)
  900. goto done;
  901. if (!dev->p) {
  902. error = device_private_init(dev);
  903. if (error)
  904. goto done;
  905. }
  906. /*
  907. * for statically allocated devices, which should all be converted
  908. * some day, we need to initialize the name. We prevent reading back
  909. * the name, and force the use of dev_name()
  910. */
  911. if (dev->init_name) {
  912. dev_set_name(dev, "%s", dev->init_name);
  913. dev->init_name = NULL;
  914. }
  915. /* subsystems can specify simple device enumeration */
  916. if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
  917. dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
  918. if (!dev_name(dev)) {
  919. error = -EINVAL;
  920. goto name_error;
  921. }
  922. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  923. parent = get_device(dev->parent);
  924. kobj = get_device_parent(dev, parent);
  925. if (IS_ERR(kobj)) {
  926. error = PTR_ERR(kobj);
  927. goto parent_error;
  928. }
  929. if (kobj)
  930. dev->kobj.parent = kobj;
  931. /* use parent numa_node */
  932. if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
  933. set_dev_node(dev, dev_to_node(parent));
  934. /* first, register with generic layer. */
  935. /* we require the name to be set before, and pass NULL */
  936. error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  937. if (error) {
  938. glue_dir = get_glue_dir(dev);
  939. goto Error;
  940. }
  941. /* notify platform of device entry */
  942. if (platform_notify)
  943. platform_notify(dev);
  944. error = device_create_file(dev, &dev_attr_uevent);
  945. if (error)
  946. goto attrError;
  947. error = device_add_class_symlinks(dev);
  948. if (error)
  949. goto SymlinkError;
  950. error = device_add_attrs(dev);
  951. if (error)
  952. goto AttrsError;
  953. error = bus_add_device(dev);
  954. if (error)
  955. goto BusError;
  956. error = dpm_sysfs_add(dev);
  957. if (error)
  958. goto DPMError;
  959. device_pm_add(dev);
  960. if (MAJOR(dev->devt)) {
  961. error = device_create_file(dev, &dev_attr_dev);
  962. if (error)
  963. goto DevAttrError;
  964. error = device_create_sys_dev_entry(dev);
  965. if (error)
  966. goto SysEntryError;
  967. devtmpfs_create_node(dev);
  968. }
  969. /* Notify clients of device addition. This call must come
  970. * after dpm_sysfs_add() and before kobject_uevent().
  971. */
  972. if (dev->bus)
  973. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  974. BUS_NOTIFY_ADD_DEVICE, dev);
  975. kobject_uevent(&dev->kobj, KOBJ_ADD);
  976. bus_probe_device(dev);
  977. if (parent)
  978. klist_add_tail(&dev->p->knode_parent,
  979. &parent->p->klist_children);
  980. if (dev->class) {
  981. mutex_lock(&dev->class->p->mutex);
  982. /* tie the class to the device */
  983. klist_add_tail(&dev->knode_class,
  984. &dev->class->p->klist_devices);
  985. /* notify any interfaces that the device is here */
  986. list_for_each_entry(class_intf,
  987. &dev->class->p->interfaces, node)
  988. if (class_intf->add_dev)
  989. class_intf->add_dev(dev, class_intf);
  990. mutex_unlock(&dev->class->p->mutex);
  991. }
  992. done:
  993. put_device(dev);
  994. return error;
  995. SysEntryError:
  996. if (MAJOR(dev->devt))
  997. device_remove_file(dev, &dev_attr_dev);
  998. DevAttrError:
  999. device_pm_remove(dev);
  1000. dpm_sysfs_remove(dev);
  1001. DPMError:
  1002. bus_remove_device(dev);
  1003. BusError:
  1004. device_remove_attrs(dev);
  1005. AttrsError:
  1006. device_remove_class_symlinks(dev);
  1007. SymlinkError:
  1008. device_remove_file(dev, &dev_attr_uevent);
  1009. attrError:
  1010. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  1011. glue_dir = get_glue_dir(dev);
  1012. kobject_del(&dev->kobj);
  1013. Error:
  1014. cleanup_glue_dir(dev, glue_dir);
  1015. parent_error:
  1016. put_device(parent);
  1017. name_error:
  1018. kfree(dev->p);
  1019. dev->p = NULL;
  1020. goto done;
  1021. }
  1022. EXPORT_SYMBOL_GPL(device_add);
  1023. /**
  1024. * device_register - register a device with the system.
  1025. * @dev: pointer to the device structure
  1026. *
  1027. * This happens in two clean steps - initialize the device
  1028. * and add it to the system. The two steps can be called
  1029. * separately, but this is the easiest and most common.
  1030. * I.e. you should only call the two helpers separately if
  1031. * have a clearly defined need to use and refcount the device
  1032. * before it is added to the hierarchy.
  1033. *
  1034. * For more information, see the kerneldoc for device_initialize()
  1035. * and device_add().
  1036. *
  1037. * NOTE: _Never_ directly free @dev after calling this function, even
  1038. * if it returned an error! Always use put_device() to give up the
  1039. * reference initialized in this function instead.
  1040. */
  1041. int device_register(struct device *dev)
  1042. {
  1043. device_initialize(dev);
  1044. return device_add(dev);
  1045. }
  1046. EXPORT_SYMBOL_GPL(device_register);
  1047. /**
  1048. * get_device - increment reference count for device.
  1049. * @dev: device.
  1050. *
  1051. * This simply forwards the call to kobject_get(), though
  1052. * we do take care to provide for the case that we get a NULL
  1053. * pointer passed in.
  1054. */
  1055. struct device *get_device(struct device *dev)
  1056. {
  1057. return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
  1058. }
  1059. EXPORT_SYMBOL_GPL(get_device);
  1060. /**
  1061. * put_device - decrement reference count.
  1062. * @dev: device in question.
  1063. */
  1064. void put_device(struct device *dev)
  1065. {
  1066. /* might_sleep(); */
  1067. if (dev)
  1068. kobject_put(&dev->kobj);
  1069. }
  1070. EXPORT_SYMBOL_GPL(put_device);
  1071. /**
  1072. * device_del - delete device from system.
  1073. * @dev: device.
  1074. *
  1075. * This is the first part of the device unregistration
  1076. * sequence. This removes the device from the lists we control
  1077. * from here, has it removed from the other driver model
  1078. * subsystems it was added to in device_add(), and removes it
  1079. * from the kobject hierarchy.
  1080. *
  1081. * NOTE: this should be called manually _iff_ device_add() was
  1082. * also called manually.
  1083. */
  1084. void device_del(struct device *dev)
  1085. {
  1086. struct device *parent = dev->parent;
  1087. struct kobject *glue_dir = NULL;
  1088. struct class_interface *class_intf;
  1089. /* Notify clients of device removal. This call must come
  1090. * before dpm_sysfs_remove().
  1091. */
  1092. if (dev->bus)
  1093. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1094. BUS_NOTIFY_DEL_DEVICE, dev);
  1095. dpm_sysfs_remove(dev);
  1096. if (parent)
  1097. klist_del(&dev->p->knode_parent);
  1098. if (MAJOR(dev->devt)) {
  1099. devtmpfs_delete_node(dev);
  1100. device_remove_sys_dev_entry(dev);
  1101. device_remove_file(dev, &dev_attr_dev);
  1102. }
  1103. if (dev->class) {
  1104. device_remove_class_symlinks(dev);
  1105. mutex_lock(&dev->class->p->mutex);
  1106. /* notify any interfaces that the device is now gone */
  1107. list_for_each_entry(class_intf,
  1108. &dev->class->p->interfaces, node)
  1109. if (class_intf->remove_dev)
  1110. class_intf->remove_dev(dev, class_intf);
  1111. /* remove the device from the class list */
  1112. klist_del(&dev->knode_class);
  1113. mutex_unlock(&dev->class->p->mutex);
  1114. }
  1115. device_remove_file(dev, &dev_attr_uevent);
  1116. device_remove_attrs(dev);
  1117. bus_remove_device(dev);
  1118. device_pm_remove(dev);
  1119. driver_deferred_probe_del(dev);
  1120. /* Notify the platform of the removal, in case they
  1121. * need to do anything...
  1122. */
  1123. if (platform_notify_remove)
  1124. platform_notify_remove(dev);
  1125. if (dev->bus)
  1126. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1127. BUS_NOTIFY_REMOVED_DEVICE, dev);
  1128. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  1129. glue_dir = get_glue_dir(dev);
  1130. kobject_del(&dev->kobj);
  1131. cleanup_glue_dir(dev, glue_dir);
  1132. put_device(parent);
  1133. }
  1134. EXPORT_SYMBOL_GPL(device_del);
  1135. /**
  1136. * device_unregister - unregister device from system.
  1137. * @dev: device going away.
  1138. *
  1139. * We do this in two parts, like we do device_register(). First,
  1140. * we remove it from all the subsystems with device_del(), then
  1141. * we decrement the reference count via put_device(). If that
  1142. * is the final reference count, the device will be cleaned up
  1143. * via device_release() above. Otherwise, the structure will
  1144. * stick around until the final reference to the device is dropped.
  1145. */
  1146. void device_unregister(struct device *dev)
  1147. {
  1148. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1149. device_del(dev);
  1150. put_device(dev);
  1151. }
  1152. EXPORT_SYMBOL_GPL(device_unregister);
  1153. static struct device *prev_device(struct klist_iter *i)
  1154. {
  1155. struct klist_node *n = klist_prev(i);
  1156. struct device *dev = NULL;
  1157. struct device_private *p;
  1158. if (n) {
  1159. p = to_device_private_parent(n);
  1160. dev = p->device;
  1161. }
  1162. return dev;
  1163. }
  1164. static struct device *next_device(struct klist_iter *i)
  1165. {
  1166. struct klist_node *n = klist_next(i);
  1167. struct device *dev = NULL;
  1168. struct device_private *p;
  1169. if (n) {
  1170. p = to_device_private_parent(n);
  1171. dev = p->device;
  1172. }
  1173. return dev;
  1174. }
  1175. /**
  1176. * device_get_devnode - path of device node file
  1177. * @dev: device
  1178. * @mode: returned file access mode
  1179. * @uid: returned file owner
  1180. * @gid: returned file group
  1181. * @tmp: possibly allocated string
  1182. *
  1183. * Return the relative path of a possible device node.
  1184. * Non-default names may need to allocate a memory to compose
  1185. * a name. This memory is returned in tmp and needs to be
  1186. * freed by the caller.
  1187. */
  1188. const char *device_get_devnode(struct device *dev,
  1189. umode_t *mode, kuid_t *uid, kgid_t *gid,
  1190. const char **tmp)
  1191. {
  1192. char *s;
  1193. *tmp = NULL;
  1194. /* the device type may provide a specific name */
  1195. if (dev->type && dev->type->devnode)
  1196. *tmp = dev->type->devnode(dev, mode, uid, gid);
  1197. if (*tmp)
  1198. return *tmp;
  1199. /* the class may provide a specific name */
  1200. if (dev->class && dev->class->devnode)
  1201. *tmp = dev->class->devnode(dev, mode);
  1202. if (*tmp)
  1203. return *tmp;
  1204. /* return name without allocation, tmp == NULL */
  1205. if (strchr(dev_name(dev), '!') == NULL)
  1206. return dev_name(dev);
  1207. /* replace '!' in the name with '/' */
  1208. s = kstrdup(dev_name(dev), GFP_KERNEL);
  1209. if (!s)
  1210. return NULL;
  1211. strreplace(s, '!', '/');
  1212. return *tmp = s;
  1213. }
  1214. /**
  1215. * device_for_each_child - device child iterator.
  1216. * @parent: parent struct device.
  1217. * @fn: function to be called for each device.
  1218. * @data: data for the callback.
  1219. *
  1220. * Iterate over @parent's child devices, and call @fn for each,
  1221. * passing it @data.
  1222. *
  1223. * We check the return of @fn each time. If it returns anything
  1224. * other than 0, we break out and return that value.
  1225. */
  1226. int device_for_each_child(struct device *parent, void *data,
  1227. int (*fn)(struct device *dev, void *data))
  1228. {
  1229. struct klist_iter i;
  1230. struct device *child;
  1231. int error = 0;
  1232. if (!parent->p)
  1233. return 0;
  1234. klist_iter_init(&parent->p->klist_children, &i);
  1235. while ((child = next_device(&i)) && !error)
  1236. error = fn(child, data);
  1237. klist_iter_exit(&i);
  1238. return error;
  1239. }
  1240. EXPORT_SYMBOL_GPL(device_for_each_child);
  1241. /**
  1242. * device_for_each_child_reverse - device child iterator in reversed order.
  1243. * @parent: parent struct device.
  1244. * @fn: function to be called for each device.
  1245. * @data: data for the callback.
  1246. *
  1247. * Iterate over @parent's child devices, and call @fn for each,
  1248. * passing it @data.
  1249. *
  1250. * We check the return of @fn each time. If it returns anything
  1251. * other than 0, we break out and return that value.
  1252. */
  1253. int device_for_each_child_reverse(struct device *parent, void *data,
  1254. int (*fn)(struct device *dev, void *data))
  1255. {
  1256. struct klist_iter i;
  1257. struct device *child;
  1258. int error = 0;
  1259. if (!parent->p)
  1260. return 0;
  1261. klist_iter_init(&parent->p->klist_children, &i);
  1262. while ((child = prev_device(&i)) && !error)
  1263. error = fn(child, data);
  1264. klist_iter_exit(&i);
  1265. return error;
  1266. }
  1267. EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
  1268. /**
  1269. * device_find_child - device iterator for locating a particular device.
  1270. * @parent: parent struct device
  1271. * @match: Callback function to check device
  1272. * @data: Data to pass to match function
  1273. *
  1274. * This is similar to the device_for_each_child() function above, but it
  1275. * returns a reference to a device that is 'found' for later use, as
  1276. * determined by the @match callback.
  1277. *
  1278. * The callback should return 0 if the device doesn't match and non-zero
  1279. * if it does. If the callback returns non-zero and a reference to the
  1280. * current device can be obtained, this function will return to the caller
  1281. * and not iterate over any more devices.
  1282. *
  1283. * NOTE: you will need to drop the reference with put_device() after use.
  1284. */
  1285. struct device *device_find_child(struct device *parent, void *data,
  1286. int (*match)(struct device *dev, void *data))
  1287. {
  1288. struct klist_iter i;
  1289. struct device *child;
  1290. if (!parent)
  1291. return NULL;
  1292. klist_iter_init(&parent->p->klist_children, &i);
  1293. while ((child = next_device(&i)))
  1294. if (match(child, data) && get_device(child))
  1295. break;
  1296. klist_iter_exit(&i);
  1297. return child;
  1298. }
  1299. EXPORT_SYMBOL_GPL(device_find_child);
  1300. int __init devices_init(void)
  1301. {
  1302. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  1303. if (!devices_kset)
  1304. return -ENOMEM;
  1305. dev_kobj = kobject_create_and_add("dev", NULL);
  1306. if (!dev_kobj)
  1307. goto dev_kobj_err;
  1308. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  1309. if (!sysfs_dev_block_kobj)
  1310. goto block_kobj_err;
  1311. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  1312. if (!sysfs_dev_char_kobj)
  1313. goto char_kobj_err;
  1314. return 0;
  1315. char_kobj_err:
  1316. kobject_put(sysfs_dev_block_kobj);
  1317. block_kobj_err:
  1318. kobject_put(dev_kobj);
  1319. dev_kobj_err:
  1320. kset_unregister(devices_kset);
  1321. return -ENOMEM;
  1322. }
  1323. static int device_check_offline(struct device *dev, void *not_used)
  1324. {
  1325. int ret;
  1326. ret = device_for_each_child(dev, NULL, device_check_offline);
  1327. if (ret)
  1328. return ret;
  1329. return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
  1330. }
  1331. /**
  1332. * device_offline - Prepare the device for hot-removal.
  1333. * @dev: Device to be put offline.
  1334. *
  1335. * Execute the device bus type's .offline() callback, if present, to prepare
  1336. * the device for a subsequent hot-removal. If that succeeds, the device must
  1337. * not be used until either it is removed or its bus type's .online() callback
  1338. * is executed.
  1339. *
  1340. * Call under device_hotplug_lock.
  1341. */
  1342. int device_offline(struct device *dev)
  1343. {
  1344. int ret;
  1345. if (dev->offline_disabled)
  1346. return -EPERM;
  1347. ret = device_for_each_child(dev, NULL, device_check_offline);
  1348. if (ret)
  1349. return ret;
  1350. device_lock(dev);
  1351. if (device_supports_offline(dev)) {
  1352. if (dev->offline) {
  1353. ret = 1;
  1354. } else {
  1355. ret = dev->bus->offline(dev);
  1356. if (!ret) {
  1357. kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
  1358. dev->offline = true;
  1359. }
  1360. }
  1361. }
  1362. device_unlock(dev);
  1363. return ret;
  1364. }
  1365. /**
  1366. * device_online - Put the device back online after successful device_offline().
  1367. * @dev: Device to be put back online.
  1368. *
  1369. * If device_offline() has been successfully executed for @dev, but the device
  1370. * has not been removed subsequently, execute its bus type's .online() callback
  1371. * to indicate that the device can be used again.
  1372. *
  1373. * Call under device_hotplug_lock.
  1374. */
  1375. int device_online(struct device *dev)
  1376. {
  1377. int ret = 0;
  1378. device_lock(dev);
  1379. if (device_supports_offline(dev)) {
  1380. if (dev->offline) {
  1381. ret = dev->bus->online(dev);
  1382. if (!ret) {
  1383. kobject_uevent(&dev->kobj, KOBJ_ONLINE);
  1384. dev->offline = false;
  1385. }
  1386. } else {
  1387. ret = 1;
  1388. }
  1389. }
  1390. device_unlock(dev);
  1391. return ret;
  1392. }
  1393. struct root_device {
  1394. struct device dev;
  1395. struct module *owner;
  1396. };
  1397. static inline struct root_device *to_root_device(struct device *d)
  1398. {
  1399. return container_of(d, struct root_device, dev);
  1400. }
  1401. static void root_device_release(struct device *dev)
  1402. {
  1403. kfree(to_root_device(dev));
  1404. }
  1405. /**
  1406. * __root_device_register - allocate and register a root device
  1407. * @name: root device name
  1408. * @owner: owner module of the root device, usually THIS_MODULE
  1409. *
  1410. * This function allocates a root device and registers it
  1411. * using device_register(). In order to free the returned
  1412. * device, use root_device_unregister().
  1413. *
  1414. * Root devices are dummy devices which allow other devices
  1415. * to be grouped under /sys/devices. Use this function to
  1416. * allocate a root device and then use it as the parent of
  1417. * any device which should appear under /sys/devices/{name}
  1418. *
  1419. * The /sys/devices/{name} directory will also contain a
  1420. * 'module' symlink which points to the @owner directory
  1421. * in sysfs.
  1422. *
  1423. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1424. *
  1425. * Note: You probably want to use root_device_register().
  1426. */
  1427. struct device *__root_device_register(const char *name, struct module *owner)
  1428. {
  1429. struct root_device *root;
  1430. int err = -ENOMEM;
  1431. root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
  1432. if (!root)
  1433. return ERR_PTR(err);
  1434. err = dev_set_name(&root->dev, "%s", name);
  1435. if (err) {
  1436. kfree(root);
  1437. return ERR_PTR(err);
  1438. }
  1439. root->dev.release = root_device_release;
  1440. err = device_register(&root->dev);
  1441. if (err) {
  1442. put_device(&root->dev);
  1443. return ERR_PTR(err);
  1444. }
  1445. #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
  1446. if (owner) {
  1447. struct module_kobject *mk = &owner->mkobj;
  1448. err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
  1449. if (err) {
  1450. device_unregister(&root->dev);
  1451. return ERR_PTR(err);
  1452. }
  1453. root->owner = owner;
  1454. }
  1455. #endif
  1456. return &root->dev;
  1457. }
  1458. EXPORT_SYMBOL_GPL(__root_device_register);
  1459. /**
  1460. * root_device_unregister - unregister and free a root device
  1461. * @dev: device going away
  1462. *
  1463. * This function unregisters and cleans up a device that was created by
  1464. * root_device_register().
  1465. */
  1466. void root_device_unregister(struct device *dev)
  1467. {
  1468. struct root_device *root = to_root_device(dev);
  1469. if (root->owner)
  1470. sysfs_remove_link(&root->dev.kobj, "module");
  1471. device_unregister(dev);
  1472. }
  1473. EXPORT_SYMBOL_GPL(root_device_unregister);
  1474. static void device_create_release(struct device *dev)
  1475. {
  1476. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  1477. kfree(dev);
  1478. }
  1479. static struct device *
  1480. device_create_groups_vargs(struct class *class, struct device *parent,
  1481. dev_t devt, void *drvdata,
  1482. const struct attribute_group **groups,
  1483. const char *fmt, va_list args)
  1484. {
  1485. struct device *dev = NULL;
  1486. int retval = -ENODEV;
  1487. if (class == NULL || IS_ERR(class))
  1488. goto error;
  1489. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1490. if (!dev) {
  1491. retval = -ENOMEM;
  1492. goto error;
  1493. }
  1494. device_initialize(dev);
  1495. dev->devt = devt;
  1496. dev->class = class;
  1497. dev->parent = parent;
  1498. dev->groups = groups;
  1499. dev->release = device_create_release;
  1500. dev_set_drvdata(dev, drvdata);
  1501. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  1502. if (retval)
  1503. goto error;
  1504. retval = device_add(dev);
  1505. if (retval)
  1506. goto error;
  1507. return dev;
  1508. error:
  1509. put_device(dev);
  1510. return ERR_PTR(retval);
  1511. }
  1512. /**
  1513. * device_create_vargs - creates a device and registers it with sysfs
  1514. * @class: pointer to the struct class that this device should be registered to
  1515. * @parent: pointer to the parent struct device of this new device, if any
  1516. * @devt: the dev_t for the char device to be added
  1517. * @drvdata: the data to be added to the device for callbacks
  1518. * @fmt: string for the device's name
  1519. * @args: va_list for the device's name
  1520. *
  1521. * This function can be used by char device classes. A struct device
  1522. * will be created in sysfs, registered to the specified class.
  1523. *
  1524. * A "dev" file will be created, showing the dev_t for the device, if
  1525. * the dev_t is not 0,0.
  1526. * If a pointer to a parent struct device is passed in, the newly created
  1527. * struct device will be a child of that device in sysfs.
  1528. * The pointer to the struct device will be returned from the call.
  1529. * Any further sysfs files that might be required can be created using this
  1530. * pointer.
  1531. *
  1532. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1533. *
  1534. * Note: the struct class passed to this function must have previously
  1535. * been created with a call to class_create().
  1536. */
  1537. struct device *device_create_vargs(struct class *class, struct device *parent,
  1538. dev_t devt, void *drvdata, const char *fmt,
  1539. va_list args)
  1540. {
  1541. return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
  1542. fmt, args);
  1543. }
  1544. EXPORT_SYMBOL_GPL(device_create_vargs);
  1545. /**
  1546. * device_create - creates a device and registers it with sysfs
  1547. * @class: pointer to the struct class that this device should be registered to
  1548. * @parent: pointer to the parent struct device of this new device, if any
  1549. * @devt: the dev_t for the char device to be added
  1550. * @drvdata: the data to be added to the device for callbacks
  1551. * @fmt: string for the device's name
  1552. *
  1553. * This function can be used by char device classes. A struct device
  1554. * will be created in sysfs, registered to the specified class.
  1555. *
  1556. * A "dev" file will be created, showing the dev_t for the device, if
  1557. * the dev_t is not 0,0.
  1558. * If a pointer to a parent struct device is passed in, the newly created
  1559. * struct device will be a child of that device in sysfs.
  1560. * The pointer to the struct device will be returned from the call.
  1561. * Any further sysfs files that might be required can be created using this
  1562. * pointer.
  1563. *
  1564. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1565. *
  1566. * Note: the struct class passed to this function must have previously
  1567. * been created with a call to class_create().
  1568. */
  1569. struct device *device_create(struct class *class, struct device *parent,
  1570. dev_t devt, void *drvdata, const char *fmt, ...)
  1571. {
  1572. va_list vargs;
  1573. struct device *dev;
  1574. va_start(vargs, fmt);
  1575. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  1576. va_end(vargs);
  1577. return dev;
  1578. }
  1579. EXPORT_SYMBOL_GPL(device_create);
  1580. /**
  1581. * device_create_with_groups - creates a device and registers it with sysfs
  1582. * @class: pointer to the struct class that this device should be registered to
  1583. * @parent: pointer to the parent struct device of this new device, if any
  1584. * @devt: the dev_t for the char device to be added
  1585. * @drvdata: the data to be added to the device for callbacks
  1586. * @groups: NULL-terminated list of attribute groups to be created
  1587. * @fmt: string for the device's name
  1588. *
  1589. * This function can be used by char device classes. A struct device
  1590. * will be created in sysfs, registered to the specified class.
  1591. * Additional attributes specified in the groups parameter will also
  1592. * be created automatically.
  1593. *
  1594. * A "dev" file will be created, showing the dev_t for the device, if
  1595. * the dev_t is not 0,0.
  1596. * If a pointer to a parent struct device is passed in, the newly created
  1597. * struct device will be a child of that device in sysfs.
  1598. * The pointer to the struct device will be returned from the call.
  1599. * Any further sysfs files that might be required can be created using this
  1600. * pointer.
  1601. *
  1602. * Returns &struct device pointer on success, or ERR_PTR() on error.
  1603. *
  1604. * Note: the struct class passed to this function must have previously
  1605. * been created with a call to class_create().
  1606. */
  1607. struct device *device_create_with_groups(struct class *class,
  1608. struct device *parent, dev_t devt,
  1609. void *drvdata,
  1610. const struct attribute_group **groups,
  1611. const char *fmt, ...)
  1612. {
  1613. va_list vargs;
  1614. struct device *dev;
  1615. va_start(vargs, fmt);
  1616. dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
  1617. fmt, vargs);
  1618. va_end(vargs);
  1619. return dev;
  1620. }
  1621. EXPORT_SYMBOL_GPL(device_create_with_groups);
  1622. static int __match_devt(struct device *dev, const void *data)
  1623. {
  1624. const dev_t *devt = data;
  1625. return dev->devt == *devt;
  1626. }
  1627. /**
  1628. * device_destroy - removes a device that was created with device_create()
  1629. * @class: pointer to the struct class that this device was registered with
  1630. * @devt: the dev_t of the device that was previously registered
  1631. *
  1632. * This call unregisters and cleans up a device that was created with a
  1633. * call to device_create().
  1634. */
  1635. void device_destroy(struct class *class, dev_t devt)
  1636. {
  1637. struct device *dev;
  1638. dev = class_find_device(class, NULL, &devt, __match_devt);
  1639. if (dev) {
  1640. put_device(dev);
  1641. device_unregister(dev);
  1642. }
  1643. }
  1644. EXPORT_SYMBOL_GPL(device_destroy);
  1645. /**
  1646. * device_rename - renames a device
  1647. * @dev: the pointer to the struct device to be renamed
  1648. * @new_name: the new name of the device
  1649. *
  1650. * It is the responsibility of the caller to provide mutual
  1651. * exclusion between two different calls of device_rename
  1652. * on the same device to ensure that new_name is valid and
  1653. * won't conflict with other devices.
  1654. *
  1655. * Note: Don't call this function. Currently, the networking layer calls this
  1656. * function, but that will change. The following text from Kay Sievers offers
  1657. * some insight:
  1658. *
  1659. * Renaming devices is racy at many levels, symlinks and other stuff are not
  1660. * replaced atomically, and you get a "move" uevent, but it's not easy to
  1661. * connect the event to the old and new device. Device nodes are not renamed at
  1662. * all, there isn't even support for that in the kernel now.
  1663. *
  1664. * In the meantime, during renaming, your target name might be taken by another
  1665. * driver, creating conflicts. Or the old name is taken directly after you
  1666. * renamed it -- then you get events for the same DEVPATH, before you even see
  1667. * the "move" event. It's just a mess, and nothing new should ever rely on
  1668. * kernel device renaming. Besides that, it's not even implemented now for
  1669. * other things than (driver-core wise very simple) network devices.
  1670. *
  1671. * We are currently about to change network renaming in udev to completely
  1672. * disallow renaming of devices in the same namespace as the kernel uses,
  1673. * because we can't solve the problems properly, that arise with swapping names
  1674. * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
  1675. * be allowed to some other name than eth[0-9]*, for the aforementioned
  1676. * reasons.
  1677. *
  1678. * Make up a "real" name in the driver before you register anything, or add
  1679. * some other attributes for userspace to find the device, or use udev to add
  1680. * symlinks -- but never rename kernel devices later, it's a complete mess. We
  1681. * don't even want to get into that and try to implement the missing pieces in
  1682. * the core. We really have other pieces to fix in the driver core mess. :)
  1683. */
  1684. int device_rename(struct device *dev, const char *new_name)
  1685. {
  1686. struct kobject *kobj = &dev->kobj;
  1687. char *old_device_name = NULL;
  1688. int error;
  1689. dev = get_device(dev);
  1690. if (!dev)
  1691. return -EINVAL;
  1692. dev_dbg(dev, "renaming to %s\n", new_name);
  1693. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  1694. if (!old_device_name) {
  1695. error = -ENOMEM;
  1696. goto out;
  1697. }
  1698. if (dev->class) {
  1699. error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
  1700. kobj, old_device_name,
  1701. new_name, kobject_namespace(kobj));
  1702. if (error)
  1703. goto out;
  1704. }
  1705. error = kobject_rename(kobj, new_name);
  1706. if (error)
  1707. goto out;
  1708. out:
  1709. put_device(dev);
  1710. kfree(old_device_name);
  1711. return error;
  1712. }
  1713. EXPORT_SYMBOL_GPL(device_rename);
  1714. static int device_move_class_links(struct device *dev,
  1715. struct device *old_parent,
  1716. struct device *new_parent)
  1717. {
  1718. int error = 0;
  1719. if (old_parent)
  1720. sysfs_remove_link(&dev->kobj, "device");
  1721. if (new_parent)
  1722. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1723. "device");
  1724. return error;
  1725. }
  1726. /**
  1727. * device_move - moves a device to a new parent
  1728. * @dev: the pointer to the struct device to be moved
  1729. * @new_parent: the new parent of the device (can by NULL)
  1730. * @dpm_order: how to reorder the dpm_list
  1731. */
  1732. int device_move(struct device *dev, struct device *new_parent,
  1733. enum dpm_order dpm_order)
  1734. {
  1735. int error;
  1736. struct device *old_parent;
  1737. struct kobject *new_parent_kobj;
  1738. dev = get_device(dev);
  1739. if (!dev)
  1740. return -EINVAL;
  1741. device_pm_lock();
  1742. new_parent = get_device(new_parent);
  1743. new_parent_kobj = get_device_parent(dev, new_parent);
  1744. if (IS_ERR(new_parent_kobj)) {
  1745. error = PTR_ERR(new_parent_kobj);
  1746. put_device(new_parent);
  1747. goto out;
  1748. }
  1749. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  1750. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  1751. error = kobject_move(&dev->kobj, new_parent_kobj);
  1752. if (error) {
  1753. cleanup_glue_dir(dev, new_parent_kobj);
  1754. put_device(new_parent);
  1755. goto out;
  1756. }
  1757. old_parent = dev->parent;
  1758. dev->parent = new_parent;
  1759. if (old_parent)
  1760. klist_remove(&dev->p->knode_parent);
  1761. if (new_parent) {
  1762. klist_add_tail(&dev->p->knode_parent,
  1763. &new_parent->p->klist_children);
  1764. set_dev_node(dev, dev_to_node(new_parent));
  1765. }
  1766. if (dev->class) {
  1767. error = device_move_class_links(dev, old_parent, new_parent);
  1768. if (error) {
  1769. /* We ignore errors on cleanup since we're hosed anyway... */
  1770. device_move_class_links(dev, new_parent, old_parent);
  1771. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1772. if (new_parent)
  1773. klist_remove(&dev->p->knode_parent);
  1774. dev->parent = old_parent;
  1775. if (old_parent) {
  1776. klist_add_tail(&dev->p->knode_parent,
  1777. &old_parent->p->klist_children);
  1778. set_dev_node(dev, dev_to_node(old_parent));
  1779. }
  1780. }
  1781. cleanup_glue_dir(dev, new_parent_kobj);
  1782. put_device(new_parent);
  1783. goto out;
  1784. }
  1785. }
  1786. switch (dpm_order) {
  1787. case DPM_ORDER_NONE:
  1788. break;
  1789. case DPM_ORDER_DEV_AFTER_PARENT:
  1790. device_pm_move_after(dev, new_parent);
  1791. devices_kset_move_after(dev, new_parent);
  1792. break;
  1793. case DPM_ORDER_PARENT_BEFORE_DEV:
  1794. device_pm_move_before(new_parent, dev);
  1795. devices_kset_move_before(new_parent, dev);
  1796. break;
  1797. case DPM_ORDER_DEV_LAST:
  1798. device_pm_move_last(dev);
  1799. devices_kset_move_last(dev);
  1800. break;
  1801. }
  1802. put_device(old_parent);
  1803. out:
  1804. device_pm_unlock();
  1805. put_device(dev);
  1806. return error;
  1807. }
  1808. EXPORT_SYMBOL_GPL(device_move);
  1809. /**
  1810. * device_shutdown - call ->shutdown() on each device to shutdown.
  1811. */
  1812. void device_shutdown(void)
  1813. {
  1814. struct device *dev, *parent;
  1815. spin_lock(&devices_kset->list_lock);
  1816. /*
  1817. * Walk the devices list backward, shutting down each in turn.
  1818. * Beware that device unplug events may also start pulling
  1819. * devices offline, even as the system is shutting down.
  1820. */
  1821. while (!list_empty(&devices_kset->list)) {
  1822. dev = list_entry(devices_kset->list.prev, struct device,
  1823. kobj.entry);
  1824. /*
  1825. * hold reference count of device's parent to
  1826. * prevent it from being freed because parent's
  1827. * lock is to be held
  1828. */
  1829. parent = get_device(dev->parent);
  1830. get_device(dev);
  1831. /*
  1832. * Make sure the device is off the kset list, in the
  1833. * event that dev->*->shutdown() doesn't remove it.
  1834. */
  1835. list_del_init(&dev->kobj.entry);
  1836. spin_unlock(&devices_kset->list_lock);
  1837. /* hold lock to avoid race with probe/release */
  1838. if (parent)
  1839. device_lock(parent);
  1840. device_lock(dev);
  1841. /* Don't allow any more runtime suspends */
  1842. pm_runtime_get_noresume(dev);
  1843. pm_runtime_barrier(dev);
  1844. if (dev->class && dev->class->shutdown) {
  1845. if (initcall_debug)
  1846. dev_info(dev, "shutdown\n");
  1847. dev->class->shutdown(dev);
  1848. } else if (dev->bus && dev->bus->shutdown) {
  1849. if (initcall_debug)
  1850. dev_info(dev, "shutdown\n");
  1851. dev->bus->shutdown(dev);
  1852. } else if (dev->driver && dev->driver->shutdown) {
  1853. if (initcall_debug)
  1854. dev_info(dev, "shutdown\n");
  1855. dev->driver->shutdown(dev);
  1856. }
  1857. device_unlock(dev);
  1858. if (parent)
  1859. device_unlock(parent);
  1860. put_device(dev);
  1861. put_device(parent);
  1862. spin_lock(&devices_kset->list_lock);
  1863. }
  1864. spin_unlock(&devices_kset->list_lock);
  1865. }
  1866. /*
  1867. * Device logging functions
  1868. */
  1869. #ifdef CONFIG_PRINTK
  1870. static int
  1871. create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
  1872. {
  1873. const char *subsys;
  1874. size_t pos = 0;
  1875. if (dev->class)
  1876. subsys = dev->class->name;
  1877. else if (dev->bus)
  1878. subsys = dev->bus->name;
  1879. else
  1880. return 0;
  1881. pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
  1882. if (pos >= hdrlen)
  1883. goto overflow;
  1884. /*
  1885. * Add device identifier DEVICE=:
  1886. * b12:8 block dev_t
  1887. * c127:3 char dev_t
  1888. * n8 netdev ifindex
  1889. * +sound:card0 subsystem:devname
  1890. */
  1891. if (MAJOR(dev->devt)) {
  1892. char c;
  1893. if (strcmp(subsys, "block") == 0)
  1894. c = 'b';
  1895. else
  1896. c = 'c';
  1897. pos++;
  1898. pos += snprintf(hdr + pos, hdrlen - pos,
  1899. "DEVICE=%c%u:%u",
  1900. c, MAJOR(dev->devt), MINOR(dev->devt));
  1901. } else if (strcmp(subsys, "net") == 0) {
  1902. struct net_device *net = to_net_dev(dev);
  1903. pos++;
  1904. pos += snprintf(hdr + pos, hdrlen - pos,
  1905. "DEVICE=n%u", net->ifindex);
  1906. } else {
  1907. pos++;
  1908. pos += snprintf(hdr + pos, hdrlen - pos,
  1909. "DEVICE=+%s:%s", subsys, dev_name(dev));
  1910. }
  1911. if (pos >= hdrlen)
  1912. goto overflow;
  1913. return pos;
  1914. overflow:
  1915. dev_WARN(dev, "device/subsystem name too long");
  1916. return 0;
  1917. }
  1918. int dev_vprintk_emit(int level, const struct device *dev,
  1919. const char *fmt, va_list args)
  1920. {
  1921. char hdr[128];
  1922. size_t hdrlen;
  1923. hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
  1924. return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
  1925. }
  1926. EXPORT_SYMBOL(dev_vprintk_emit);
  1927. int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
  1928. {
  1929. va_list args;
  1930. int r;
  1931. va_start(args, fmt);
  1932. r = dev_vprintk_emit(level, dev, fmt, args);
  1933. va_end(args);
  1934. return r;
  1935. }
  1936. EXPORT_SYMBOL(dev_printk_emit);
  1937. static void __dev_printk(const char *level, const struct device *dev,
  1938. struct va_format *vaf)
  1939. {
  1940. if (dev)
  1941. dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
  1942. dev_driver_string(dev), dev_name(dev), vaf);
  1943. else
  1944. printk("%s(NULL device *): %pV", level, vaf);
  1945. }
  1946. void dev_printk(const char *level, const struct device *dev,
  1947. const char *fmt, ...)
  1948. {
  1949. struct va_format vaf;
  1950. va_list args;
  1951. va_start(args, fmt);
  1952. vaf.fmt = fmt;
  1953. vaf.va = &args;
  1954. __dev_printk(level, dev, &vaf);
  1955. va_end(args);
  1956. }
  1957. EXPORT_SYMBOL(dev_printk);
  1958. #define define_dev_printk_level(func, kern_level) \
  1959. void func(const struct device *dev, const char *fmt, ...) \
  1960. { \
  1961. struct va_format vaf; \
  1962. va_list args; \
  1963. \
  1964. va_start(args, fmt); \
  1965. \
  1966. vaf.fmt = fmt; \
  1967. vaf.va = &args; \
  1968. \
  1969. __dev_printk(kern_level, dev, &vaf); \
  1970. \
  1971. va_end(args); \
  1972. } \
  1973. EXPORT_SYMBOL(func);
  1974. define_dev_printk_level(dev_emerg, KERN_EMERG);
  1975. define_dev_printk_level(dev_alert, KERN_ALERT);
  1976. define_dev_printk_level(dev_crit, KERN_CRIT);
  1977. define_dev_printk_level(dev_err, KERN_ERR);
  1978. define_dev_printk_level(dev_warn, KERN_WARNING);
  1979. define_dev_printk_level(dev_notice, KERN_NOTICE);
  1980. define_dev_printk_level(_dev_info, KERN_INFO);
  1981. #endif
  1982. static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
  1983. {
  1984. return fwnode && !IS_ERR(fwnode->secondary);
  1985. }
  1986. /**
  1987. * set_primary_fwnode - Change the primary firmware node of a given device.
  1988. * @dev: Device to handle.
  1989. * @fwnode: New primary firmware node of the device.
  1990. *
  1991. * Set the device's firmware node pointer to @fwnode, but if a secondary
  1992. * firmware node of the device is present, preserve it.
  1993. */
  1994. void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  1995. {
  1996. if (fwnode) {
  1997. struct fwnode_handle *fn = dev->fwnode;
  1998. if (fwnode_is_primary(fn))
  1999. fn = fn->secondary;
  2000. fwnode->secondary = fn;
  2001. dev->fwnode = fwnode;
  2002. } else {
  2003. dev->fwnode = fwnode_is_primary(dev->fwnode) ?
  2004. dev->fwnode->secondary : NULL;
  2005. }
  2006. }
  2007. EXPORT_SYMBOL_GPL(set_primary_fwnode);
  2008. /**
  2009. * set_secondary_fwnode - Change the secondary firmware node of a given device.
  2010. * @dev: Device to handle.
  2011. * @fwnode: New secondary firmware node of the device.
  2012. *
  2013. * If a primary firmware node of the device is present, set its secondary
  2014. * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
  2015. * @fwnode.
  2016. */
  2017. void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
  2018. {
  2019. if (fwnode)
  2020. fwnode->secondary = ERR_PTR(-ENODEV);
  2021. if (fwnode_is_primary(dev->fwnode))
  2022. dev->fwnode->secondary = fwnode;
  2023. else
  2024. dev->fwnode = fwnode;
  2025. }