core.c 48 KB

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