core.c 47 KB

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