core.c 51 KB

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