core.c 54 KB

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