core.c 55 KB

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