core.c 57 KB

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