core.c 52 KB

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