core.c 44 KB

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