core.c 57 KB

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