core.c 51 KB

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