core.c 44 KB

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