core.c 44 KB

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