core.c 55 KB

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