core.c 55 KB

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