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