core.c 58 KB

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