core.c 44 KB

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