core.c 51 KB

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