core.c 54 KB

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