core.c 52 KB

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