core.c 54 KB

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