core.c 57 KB

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