core.c 50 KB

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