core.c 43 KB

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