core.c 57 KB

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