core.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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 <asm/semaphore.h>
  21. #include "base.h"
  22. #include "power/power.h"
  23. int (*platform_notify)(struct device * dev) = NULL;
  24. int (*platform_notify_remove)(struct device * dev) = NULL;
  25. /*
  26. * sysfs bindings for devices.
  27. */
  28. /**
  29. * dev_driver_string - Return a device's driver name, if at all possible
  30. * @dev: struct device to get the name of
  31. *
  32. * Will return the device's driver's name if it is bound to a device. If
  33. * the device is not bound to a device, it will return the name of the bus
  34. * it is attached to. If it is not attached to a bus either, an empty
  35. * string will be returned.
  36. */
  37. const char *dev_driver_string(struct device *dev)
  38. {
  39. return dev->driver ? dev->driver->name :
  40. (dev->bus ? dev->bus->name :
  41. (dev->class ? dev->class->name : ""));
  42. }
  43. EXPORT_SYMBOL(dev_driver_string);
  44. #define to_dev(obj) container_of(obj, struct device, kobj)
  45. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  46. static ssize_t
  47. dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  48. {
  49. struct device_attribute * dev_attr = to_dev_attr(attr);
  50. struct device * dev = to_dev(kobj);
  51. ssize_t ret = -EIO;
  52. if (dev_attr->show)
  53. ret = dev_attr->show(dev, dev_attr, buf);
  54. return ret;
  55. }
  56. static ssize_t
  57. dev_attr_store(struct kobject * kobj, struct attribute * attr,
  58. const char * buf, size_t count)
  59. {
  60. struct device_attribute * dev_attr = to_dev_attr(attr);
  61. struct device * dev = to_dev(kobj);
  62. ssize_t ret = -EIO;
  63. if (dev_attr->store)
  64. ret = dev_attr->store(dev, dev_attr, buf, count);
  65. return ret;
  66. }
  67. static struct sysfs_ops dev_sysfs_ops = {
  68. .show = dev_attr_show,
  69. .store = dev_attr_store,
  70. };
  71. /**
  72. * device_release - free device structure.
  73. * @kobj: device's kobject.
  74. *
  75. * This is called once the reference count for the object
  76. * reaches 0. We forward the call to the device's release
  77. * method, which should handle actually freeing the structure.
  78. */
  79. static void device_release(struct kobject * kobj)
  80. {
  81. struct device * dev = to_dev(kobj);
  82. if (dev->release)
  83. dev->release(dev);
  84. else if (dev->type && dev->type->release)
  85. dev->type->release(dev);
  86. else if (dev->class && dev->class->dev_release)
  87. dev->class->dev_release(dev);
  88. else {
  89. printk(KERN_ERR "Device '%s' does not have a release() function, "
  90. "it is broken and must be fixed.\n",
  91. dev->bus_id);
  92. WARN_ON(1);
  93. }
  94. }
  95. static struct kobj_type ktype_device = {
  96. .release = device_release,
  97. .sysfs_ops = &dev_sysfs_ops,
  98. };
  99. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  100. {
  101. struct kobj_type *ktype = get_ktype(kobj);
  102. if (ktype == &ktype_device) {
  103. struct device *dev = to_dev(kobj);
  104. if (dev->uevent_suppress)
  105. return 0;
  106. if (dev->bus)
  107. return 1;
  108. if (dev->class)
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  114. {
  115. struct device *dev = to_dev(kobj);
  116. if (dev->bus)
  117. return dev->bus->name;
  118. if (dev->class)
  119. return dev->class->name;
  120. return NULL;
  121. }
  122. static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
  123. int num_envp, char *buffer, int buffer_size)
  124. {
  125. struct device *dev = to_dev(kobj);
  126. int i = 0;
  127. int length = 0;
  128. int retval = 0;
  129. /* add the major/minor if present */
  130. if (MAJOR(dev->devt)) {
  131. add_uevent_var(envp, num_envp, &i,
  132. buffer, buffer_size, &length,
  133. "MAJOR=%u", MAJOR(dev->devt));
  134. add_uevent_var(envp, num_envp, &i,
  135. buffer, buffer_size, &length,
  136. "MINOR=%u", MINOR(dev->devt));
  137. }
  138. if (dev->type && dev->type->name)
  139. add_uevent_var(envp, num_envp, &i,
  140. buffer, buffer_size, &length,
  141. "DEVTYPE=%s", dev->type->name);
  142. if (dev->driver)
  143. add_uevent_var(envp, num_envp, &i,
  144. buffer, buffer_size, &length,
  145. "DRIVER=%s", dev->driver->name);
  146. #ifdef CONFIG_SYSFS_DEPRECATED
  147. if (dev->class) {
  148. struct device *parent = dev->parent;
  149. /* find first bus device in parent chain */
  150. while (parent && !parent->bus)
  151. parent = parent->parent;
  152. if (parent && parent->bus) {
  153. const char *path;
  154. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  155. if (path) {
  156. add_uevent_var(envp, num_envp, &i,
  157. buffer, buffer_size, &length,
  158. "PHYSDEVPATH=%s", path);
  159. kfree(path);
  160. }
  161. add_uevent_var(envp, num_envp, &i,
  162. buffer, buffer_size, &length,
  163. "PHYSDEVBUS=%s", parent->bus->name);
  164. if (parent->driver)
  165. add_uevent_var(envp, num_envp, &i,
  166. buffer, buffer_size, &length,
  167. "PHYSDEVDRIVER=%s", parent->driver->name);
  168. }
  169. } else if (dev->bus) {
  170. add_uevent_var(envp, num_envp, &i,
  171. buffer, buffer_size, &length,
  172. "PHYSDEVBUS=%s", dev->bus->name);
  173. if (dev->driver)
  174. add_uevent_var(envp, num_envp, &i,
  175. buffer, buffer_size, &length,
  176. "PHYSDEVDRIVER=%s", dev->driver->name);
  177. }
  178. #endif
  179. /* terminate, set to next free slot, shrink available space */
  180. envp[i] = NULL;
  181. envp = &envp[i];
  182. num_envp -= i;
  183. buffer = &buffer[length];
  184. buffer_size -= length;
  185. if (dev->bus && dev->bus->uevent) {
  186. /* have the bus specific function add its stuff */
  187. retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
  188. if (retval)
  189. pr_debug ("%s: bus uevent() returned %d\n",
  190. __FUNCTION__, retval);
  191. }
  192. if (dev->class && dev->class->dev_uevent) {
  193. /* have the class specific function add its stuff */
  194. retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
  195. if (retval)
  196. pr_debug("%s: class uevent() returned %d\n",
  197. __FUNCTION__, retval);
  198. }
  199. if (dev->type && dev->type->uevent) {
  200. /* have the device type specific fuction add its stuff */
  201. retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
  202. if (retval)
  203. pr_debug("%s: dev_type uevent() returned %d\n",
  204. __FUNCTION__, retval);
  205. }
  206. return retval;
  207. }
  208. static struct kset_uevent_ops device_uevent_ops = {
  209. .filter = dev_uevent_filter,
  210. .name = dev_uevent_name,
  211. .uevent = dev_uevent,
  212. };
  213. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  214. char *buf)
  215. {
  216. struct kobject *top_kobj;
  217. struct kset *kset;
  218. char *envp[32];
  219. char *data = NULL;
  220. char *pos;
  221. int i;
  222. size_t count = 0;
  223. int retval;
  224. /* search the kset, the device belongs to */
  225. top_kobj = &dev->kobj;
  226. if (!top_kobj->kset && top_kobj->parent) {
  227. do {
  228. top_kobj = top_kobj->parent;
  229. } while (!top_kobj->kset && top_kobj->parent);
  230. }
  231. if (!top_kobj->kset)
  232. goto out;
  233. kset = top_kobj->kset;
  234. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  235. goto out;
  236. /* respect filter */
  237. if (kset->uevent_ops && kset->uevent_ops->filter)
  238. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  239. goto out;
  240. data = (char *)get_zeroed_page(GFP_KERNEL);
  241. if (!data)
  242. return -ENOMEM;
  243. /* let the kset specific function add its keys */
  244. pos = data;
  245. retval = kset->uevent_ops->uevent(kset, &dev->kobj,
  246. envp, ARRAY_SIZE(envp),
  247. pos, PAGE_SIZE);
  248. if (retval)
  249. goto out;
  250. /* copy keys to file */
  251. for (i = 0; envp[i]; i++) {
  252. pos = &buf[count];
  253. count += sprintf(pos, "%s\n", envp[i]);
  254. }
  255. out:
  256. free_page((unsigned long)data);
  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. size_t len = count;
  263. enum kobject_action action;
  264. if (len && buf[len-1] == '\n')
  265. len--;
  266. for (action = 0; action < KOBJ_MAX; action++) {
  267. if (strncmp(kobject_actions[action], buf, len) != 0)
  268. continue;
  269. if (kobject_actions[action][len] != '\0')
  270. continue;
  271. kobject_uevent(&dev->kobj, action);
  272. goto out;
  273. }
  274. dev_err(dev, "uevent: unsupported action-string; this will "
  275. "be ignored in a future kernel version\n");
  276. kobject_uevent(&dev->kobj, KOBJ_ADD);
  277. out:
  278. return count;
  279. }
  280. static struct device_attribute uevent_attr =
  281. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
  282. static int device_add_attributes(struct device *dev,
  283. struct device_attribute *attrs)
  284. {
  285. int error = 0;
  286. int i;
  287. if (attrs) {
  288. for (i = 0; attr_name(attrs[i]); i++) {
  289. error = device_create_file(dev, &attrs[i]);
  290. if (error)
  291. break;
  292. }
  293. if (error)
  294. while (--i >= 0)
  295. device_remove_file(dev, &attrs[i]);
  296. }
  297. return error;
  298. }
  299. static void device_remove_attributes(struct device *dev,
  300. struct device_attribute *attrs)
  301. {
  302. int i;
  303. if (attrs)
  304. for (i = 0; attr_name(attrs[i]); i++)
  305. device_remove_file(dev, &attrs[i]);
  306. }
  307. static int device_add_groups(struct device *dev,
  308. struct attribute_group **groups)
  309. {
  310. int error = 0;
  311. int i;
  312. if (groups) {
  313. for (i = 0; groups[i]; i++) {
  314. error = sysfs_create_group(&dev->kobj, groups[i]);
  315. if (error) {
  316. while (--i >= 0)
  317. sysfs_remove_group(&dev->kobj, groups[i]);
  318. break;
  319. }
  320. }
  321. }
  322. return error;
  323. }
  324. static void device_remove_groups(struct device *dev,
  325. struct attribute_group **groups)
  326. {
  327. int i;
  328. if (groups)
  329. for (i = 0; groups[i]; i++)
  330. sysfs_remove_group(&dev->kobj, groups[i]);
  331. }
  332. static int device_add_attrs(struct device *dev)
  333. {
  334. struct class *class = dev->class;
  335. struct device_type *type = dev->type;
  336. int error;
  337. if (class) {
  338. error = device_add_attributes(dev, class->dev_attrs);
  339. if (error)
  340. return error;
  341. }
  342. if (type) {
  343. error = device_add_groups(dev, type->groups);
  344. if (error)
  345. goto err_remove_class_attrs;
  346. }
  347. error = device_add_groups(dev, dev->groups);
  348. if (error)
  349. goto err_remove_type_groups;
  350. return 0;
  351. err_remove_type_groups:
  352. if (type)
  353. device_remove_groups(dev, type->groups);
  354. err_remove_class_attrs:
  355. if (class)
  356. device_remove_attributes(dev, class->dev_attrs);
  357. return error;
  358. }
  359. static void device_remove_attrs(struct device *dev)
  360. {
  361. struct class *class = dev->class;
  362. struct device_type *type = dev->type;
  363. device_remove_groups(dev, dev->groups);
  364. if (type)
  365. device_remove_groups(dev, type->groups);
  366. if (class)
  367. device_remove_attributes(dev, class->dev_attrs);
  368. }
  369. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  370. char *buf)
  371. {
  372. return print_dev_t(buf, dev->devt);
  373. }
  374. static struct device_attribute devt_attr =
  375. __ATTR(dev, S_IRUGO, show_dev, NULL);
  376. /*
  377. * devices_subsys - structure to be registered with kobject core.
  378. */
  379. decl_subsys(devices, &ktype_device, &device_uevent_ops);
  380. /**
  381. * device_create_file - create sysfs attribute file for device.
  382. * @dev: device.
  383. * @attr: device attribute descriptor.
  384. */
  385. int device_create_file(struct device * dev, struct device_attribute * attr)
  386. {
  387. int error = 0;
  388. if (get_device(dev)) {
  389. error = sysfs_create_file(&dev->kobj, &attr->attr);
  390. put_device(dev);
  391. }
  392. return error;
  393. }
  394. /**
  395. * device_remove_file - remove sysfs attribute file.
  396. * @dev: device.
  397. * @attr: device attribute descriptor.
  398. */
  399. void device_remove_file(struct device * dev, struct device_attribute * attr)
  400. {
  401. if (get_device(dev)) {
  402. sysfs_remove_file(&dev->kobj, &attr->attr);
  403. put_device(dev);
  404. }
  405. }
  406. /**
  407. * device_create_bin_file - create sysfs binary attribute file for device.
  408. * @dev: device.
  409. * @attr: device binary attribute descriptor.
  410. */
  411. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  412. {
  413. int error = -EINVAL;
  414. if (dev)
  415. error = sysfs_create_bin_file(&dev->kobj, attr);
  416. return error;
  417. }
  418. EXPORT_SYMBOL_GPL(device_create_bin_file);
  419. /**
  420. * device_remove_bin_file - remove sysfs binary attribute file
  421. * @dev: device.
  422. * @attr: device binary attribute descriptor.
  423. */
  424. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  425. {
  426. if (dev)
  427. sysfs_remove_bin_file(&dev->kobj, attr);
  428. }
  429. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  430. /**
  431. * device_schedule_callback_owner - helper to schedule a callback for a device
  432. * @dev: device.
  433. * @func: callback function to invoke later.
  434. * @owner: module owning the callback routine
  435. *
  436. * Attribute methods must not unregister themselves or their parent device
  437. * (which would amount to the same thing). Attempts to do so will deadlock,
  438. * since unregistration is mutually exclusive with driver callbacks.
  439. *
  440. * Instead methods can call this routine, which will attempt to allocate
  441. * and schedule a workqueue request to call back @func with @dev as its
  442. * argument in the workqueue's process context. @dev will be pinned until
  443. * @func returns.
  444. *
  445. * This routine is usually called via the inline device_schedule_callback(),
  446. * which automatically sets @owner to THIS_MODULE.
  447. *
  448. * Returns 0 if the request was submitted, -ENOMEM if storage could not
  449. * be allocated, -ENODEV if a reference to @owner isn't available.
  450. *
  451. * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
  452. * underlying sysfs routine (since it is intended for use by attribute
  453. * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
  454. */
  455. int device_schedule_callback_owner(struct device *dev,
  456. void (*func)(struct device *), struct module *owner)
  457. {
  458. return sysfs_schedule_callback(&dev->kobj,
  459. (void (*)(void *)) func, dev, owner);
  460. }
  461. EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
  462. static void klist_children_get(struct klist_node *n)
  463. {
  464. struct device *dev = container_of(n, struct device, knode_parent);
  465. get_device(dev);
  466. }
  467. static void klist_children_put(struct klist_node *n)
  468. {
  469. struct device *dev = container_of(n, struct device, knode_parent);
  470. put_device(dev);
  471. }
  472. /**
  473. * device_initialize - init device structure.
  474. * @dev: device.
  475. *
  476. * This prepares the device for use by other layers,
  477. * including adding it to the device hierarchy.
  478. * It is the first half of device_register(), if called by
  479. * that, though it can also be called separately, so one
  480. * may use @dev's fields (e.g. the refcount).
  481. */
  482. void device_initialize(struct device *dev)
  483. {
  484. kobj_set_kset_s(dev, devices_subsys);
  485. kobject_init(&dev->kobj);
  486. klist_init(&dev->klist_children, klist_children_get,
  487. klist_children_put);
  488. INIT_LIST_HEAD(&dev->dma_pools);
  489. INIT_LIST_HEAD(&dev->node);
  490. init_MUTEX(&dev->sem);
  491. spin_lock_init(&dev->devres_lock);
  492. INIT_LIST_HEAD(&dev->devres_head);
  493. device_init_wakeup(dev, 0);
  494. set_dev_node(dev, -1);
  495. }
  496. #ifdef CONFIG_SYSFS_DEPRECATED
  497. static struct kobject * get_device_parent(struct device *dev,
  498. struct device *parent)
  499. {
  500. /* Set the parent to the class, not the parent device */
  501. /* this keeps sysfs from having a symlink to make old udevs happy */
  502. if (dev->class)
  503. return &dev->class->subsys.kobj;
  504. else if (parent)
  505. return &parent->kobj;
  506. return NULL;
  507. }
  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_add_dir(&devices_subsys.kobj, "virtual");
  514. return virtual_dir;
  515. }
  516. static struct kobject * get_device_parent(struct device *dev,
  517. struct device *parent)
  518. {
  519. if (dev->class) {
  520. struct kobject *kobj = NULL;
  521. struct kobject *parent_kobj;
  522. struct kobject *k;
  523. /*
  524. * If we have no parent, we live in "virtual".
  525. * Class-devices with a bus-device as parent, live
  526. * in a class-directory to prevent namespace collisions.
  527. */
  528. if (parent == NULL)
  529. parent_kobj = virtual_device_parent(dev);
  530. else if (parent->class)
  531. return &parent->kobj;
  532. else
  533. parent_kobj = &parent->kobj;
  534. /* find our class-directory at the parent and reference it */
  535. spin_lock(&dev->class->class_dirs.list_lock);
  536. list_for_each_entry(k, &dev->class->class_dirs.list, entry)
  537. if (k->parent == parent_kobj) {
  538. kobj = kobject_get(k);
  539. break;
  540. }
  541. spin_unlock(&dev->class->class_dirs.list_lock);
  542. if (kobj)
  543. return kobj;
  544. /* or create a new class-directory at the parent device */
  545. return kobject_kset_add_dir(&dev->class->class_dirs,
  546. parent_kobj, dev->class->name);
  547. }
  548. if (parent)
  549. return &parent->kobj;
  550. return NULL;
  551. }
  552. #endif
  553. static int setup_parent(struct device *dev, struct device *parent)
  554. {
  555. struct kobject *kobj;
  556. kobj = get_device_parent(dev, parent);
  557. if (IS_ERR(kobj))
  558. return PTR_ERR(kobj);
  559. if (kobj)
  560. dev->kobj.parent = kobj;
  561. return 0;
  562. }
  563. static int device_add_class_symlinks(struct device *dev)
  564. {
  565. int error;
  566. if (!dev->class)
  567. return 0;
  568. error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
  569. "subsystem");
  570. if (error)
  571. goto out;
  572. /*
  573. * If this is not a "fake" compatible device, then create the
  574. * symlink from the class to the device.
  575. */
  576. if (dev->kobj.parent != &dev->class->subsys.kobj) {
  577. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  578. dev->bus_id);
  579. if (error)
  580. goto out_subsys;
  581. }
  582. if (dev->parent) {
  583. #ifdef CONFIG_SYSFS_DEPRECATED
  584. {
  585. struct device *parent = dev->parent;
  586. char *class_name;
  587. /*
  588. * In old sysfs stacked class devices had 'device'
  589. * link pointing to real device instead of parent
  590. */
  591. while (parent->class && !parent->bus && parent->parent)
  592. parent = parent->parent;
  593. error = sysfs_create_link(&dev->kobj,
  594. &parent->kobj,
  595. "device");
  596. if (error)
  597. goto out_busid;
  598. class_name = make_class_name(dev->class->name,
  599. &dev->kobj);
  600. if (class_name)
  601. error = sysfs_create_link(&dev->parent->kobj,
  602. &dev->kobj, class_name);
  603. kfree(class_name);
  604. if (error)
  605. goto out_device;
  606. }
  607. #else
  608. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  609. "device");
  610. if (error)
  611. goto out_busid;
  612. #endif
  613. }
  614. return 0;
  615. #ifdef CONFIG_SYSFS_DEPRECATED
  616. out_device:
  617. if (dev->parent)
  618. sysfs_remove_link(&dev->kobj, "device");
  619. #endif
  620. out_busid:
  621. if (dev->kobj.parent != &dev->class->subsys.kobj)
  622. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  623. out_subsys:
  624. sysfs_remove_link(&dev->kobj, "subsystem");
  625. out:
  626. return error;
  627. }
  628. static void device_remove_class_symlinks(struct device *dev)
  629. {
  630. if (!dev->class)
  631. return;
  632. if (dev->parent) {
  633. #ifdef CONFIG_SYSFS_DEPRECATED
  634. char *class_name;
  635. class_name = make_class_name(dev->class->name, &dev->kobj);
  636. if (class_name) {
  637. sysfs_remove_link(&dev->parent->kobj, class_name);
  638. kfree(class_name);
  639. }
  640. #endif
  641. sysfs_remove_link(&dev->kobj, "device");
  642. }
  643. if (dev->kobj.parent != &dev->class->subsys.kobj)
  644. sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
  645. sysfs_remove_link(&dev->kobj, "subsystem");
  646. }
  647. /**
  648. * device_add - add device to device hierarchy.
  649. * @dev: device.
  650. *
  651. * This is part 2 of device_register(), though may be called
  652. * separately _iff_ device_initialize() has been called separately.
  653. *
  654. * This adds it to the kobject hierarchy via kobject_add(), adds it
  655. * to the global and sibling lists for the device, then
  656. * adds it to the other relevant subsystems of the driver model.
  657. */
  658. int device_add(struct device *dev)
  659. {
  660. struct device *parent = NULL;
  661. struct class_interface *class_intf;
  662. int error = -EINVAL;
  663. dev = get_device(dev);
  664. if (!dev || !strlen(dev->bus_id))
  665. goto Error;
  666. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  667. parent = get_device(dev->parent);
  668. error = setup_parent(dev, parent);
  669. if (error)
  670. goto Error;
  671. /* first, register with generic layer. */
  672. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  673. error = kobject_add(&dev->kobj);
  674. if (error)
  675. goto Error;
  676. /* notify platform of device entry */
  677. if (platform_notify)
  678. platform_notify(dev);
  679. /* notify clients of device entry (new way) */
  680. if (dev->bus)
  681. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  682. BUS_NOTIFY_ADD_DEVICE, dev);
  683. error = device_create_file(dev, &uevent_attr);
  684. if (error)
  685. goto attrError;
  686. if (MAJOR(dev->devt)) {
  687. error = device_create_file(dev, &devt_attr);
  688. if (error)
  689. goto ueventattrError;
  690. }
  691. error = device_add_class_symlinks(dev);
  692. if (error)
  693. goto SymlinkError;
  694. error = device_add_attrs(dev);
  695. if (error)
  696. goto AttrsError;
  697. error = device_pm_add(dev);
  698. if (error)
  699. goto PMError;
  700. error = bus_add_device(dev);
  701. if (error)
  702. goto BusError;
  703. kobject_uevent(&dev->kobj, KOBJ_ADD);
  704. bus_attach_device(dev);
  705. if (parent)
  706. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  707. if (dev->class) {
  708. down(&dev->class->sem);
  709. /* tie the class to the device */
  710. list_add_tail(&dev->node, &dev->class->devices);
  711. /* notify any interfaces that the device is here */
  712. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  713. if (class_intf->add_dev)
  714. class_intf->add_dev(dev, class_intf);
  715. up(&dev->class->sem);
  716. }
  717. Done:
  718. put_device(dev);
  719. return error;
  720. BusError:
  721. device_pm_remove(dev);
  722. PMError:
  723. if (dev->bus)
  724. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  725. BUS_NOTIFY_DEL_DEVICE, dev);
  726. device_remove_attrs(dev);
  727. AttrsError:
  728. device_remove_class_symlinks(dev);
  729. SymlinkError:
  730. if (MAJOR(dev->devt))
  731. device_remove_file(dev, &devt_attr);
  732. if (dev->class) {
  733. sysfs_remove_link(&dev->kobj, "subsystem");
  734. /* If this is not a "fake" compatible device, remove the
  735. * symlink from the class to the device. */
  736. if (dev->kobj.parent != &dev->class->subsys.kobj)
  737. sysfs_remove_link(&dev->class->subsys.kobj,
  738. dev->bus_id);
  739. if (parent) {
  740. #ifdef CONFIG_SYSFS_DEPRECATED
  741. char *class_name = make_class_name(dev->class->name,
  742. &dev->kobj);
  743. if (class_name)
  744. sysfs_remove_link(&dev->parent->kobj,
  745. class_name);
  746. kfree(class_name);
  747. #endif
  748. sysfs_remove_link(&dev->kobj, "device");
  749. }
  750. }
  751. ueventattrError:
  752. device_remove_file(dev, &uevent_attr);
  753. attrError:
  754. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  755. kobject_del(&dev->kobj);
  756. Error:
  757. if (parent)
  758. put_device(parent);
  759. goto Done;
  760. }
  761. /**
  762. * device_register - register a device with the system.
  763. * @dev: pointer to the device structure
  764. *
  765. * This happens in two clean steps - initialize the device
  766. * and add it to the system. The two steps can be called
  767. * separately, but this is the easiest and most common.
  768. * I.e. you should only call the two helpers separately if
  769. * have a clearly defined need to use and refcount the device
  770. * before it is added to the hierarchy.
  771. */
  772. int device_register(struct device *dev)
  773. {
  774. device_initialize(dev);
  775. return device_add(dev);
  776. }
  777. /**
  778. * get_device - increment reference count for device.
  779. * @dev: device.
  780. *
  781. * This simply forwards the call to kobject_get(), though
  782. * we do take care to provide for the case that we get a NULL
  783. * pointer passed in.
  784. */
  785. struct device * get_device(struct device * dev)
  786. {
  787. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  788. }
  789. /**
  790. * put_device - decrement reference count.
  791. * @dev: device in question.
  792. */
  793. void put_device(struct device * dev)
  794. {
  795. if (dev)
  796. kobject_put(&dev->kobj);
  797. }
  798. /**
  799. * device_del - delete device from system.
  800. * @dev: device.
  801. *
  802. * This is the first part of the device unregistration
  803. * sequence. This removes the device from the lists we control
  804. * from here, has it removed from the other driver model
  805. * subsystems it was added to in device_add(), and removes it
  806. * from the kobject hierarchy.
  807. *
  808. * NOTE: this should be called manually _iff_ device_add() was
  809. * also called manually.
  810. */
  811. void device_del(struct device * dev)
  812. {
  813. struct device * parent = dev->parent;
  814. struct class_interface *class_intf;
  815. if (parent)
  816. klist_del(&dev->knode_parent);
  817. if (MAJOR(dev->devt))
  818. device_remove_file(dev, &devt_attr);
  819. if (dev->class) {
  820. sysfs_remove_link(&dev->kobj, "subsystem");
  821. /* If this is not a "fake" compatible device, remove the
  822. * symlink from the class to the device. */
  823. if (dev->kobj.parent != &dev->class->subsys.kobj)
  824. sysfs_remove_link(&dev->class->subsys.kobj,
  825. dev->bus_id);
  826. if (parent) {
  827. #ifdef CONFIG_SYSFS_DEPRECATED
  828. char *class_name = make_class_name(dev->class->name,
  829. &dev->kobj);
  830. if (class_name)
  831. sysfs_remove_link(&dev->parent->kobj,
  832. class_name);
  833. kfree(class_name);
  834. #endif
  835. sysfs_remove_link(&dev->kobj, "device");
  836. }
  837. down(&dev->class->sem);
  838. /* notify any interfaces that the device is now gone */
  839. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  840. if (class_intf->remove_dev)
  841. class_intf->remove_dev(dev, class_intf);
  842. /* remove the device from the class list */
  843. list_del_init(&dev->node);
  844. up(&dev->class->sem);
  845. /* If we live in a parent class-directory, unreference it */
  846. if (dev->kobj.parent->kset == &dev->class->class_dirs) {
  847. struct device *d;
  848. int other = 0;
  849. /*
  850. * if we are the last child of our class, delete
  851. * our class-directory at this parent
  852. */
  853. down(&dev->class->sem);
  854. list_for_each_entry(d, &dev->class->devices, node) {
  855. if (d == dev)
  856. continue;
  857. if (d->kobj.parent == dev->kobj.parent) {
  858. other = 1;
  859. break;
  860. }
  861. }
  862. if (!other)
  863. kobject_del(dev->kobj.parent);
  864. kobject_put(dev->kobj.parent);
  865. up(&dev->class->sem);
  866. }
  867. }
  868. device_remove_file(dev, &uevent_attr);
  869. device_remove_attrs(dev);
  870. bus_remove_device(dev);
  871. /*
  872. * Some platform devices are driven without driver attached
  873. * and managed resources may have been acquired. Make sure
  874. * all resources are released.
  875. */
  876. devres_release_all(dev);
  877. /* Notify the platform of the removal, in case they
  878. * need to do anything...
  879. */
  880. if (platform_notify_remove)
  881. platform_notify_remove(dev);
  882. if (dev->bus)
  883. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  884. BUS_NOTIFY_DEL_DEVICE, dev);
  885. device_pm_remove(dev);
  886. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  887. kobject_del(&dev->kobj);
  888. if (parent)
  889. put_device(parent);
  890. }
  891. /**
  892. * device_unregister - unregister device from system.
  893. * @dev: device going away.
  894. *
  895. * We do this in two parts, like we do device_register(). First,
  896. * we remove it from all the subsystems with device_del(), then
  897. * we decrement the reference count via put_device(). If that
  898. * is the final reference count, the device will be cleaned up
  899. * via device_release() above. Otherwise, the structure will
  900. * stick around until the final reference to the device is dropped.
  901. */
  902. void device_unregister(struct device * dev)
  903. {
  904. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  905. device_del(dev);
  906. put_device(dev);
  907. }
  908. static struct device * next_device(struct klist_iter * i)
  909. {
  910. struct klist_node * n = klist_next(i);
  911. return n ? container_of(n, struct device, knode_parent) : NULL;
  912. }
  913. /**
  914. * device_for_each_child - device child iterator.
  915. * @parent: parent struct device.
  916. * @data: data for the callback.
  917. * @fn: function to be called for each device.
  918. *
  919. * Iterate over @parent's child devices, and call @fn for each,
  920. * passing it @data.
  921. *
  922. * We check the return of @fn each time. If it returns anything
  923. * other than 0, we break out and return that value.
  924. */
  925. int device_for_each_child(struct device * parent, void * data,
  926. int (*fn)(struct device *, void *))
  927. {
  928. struct klist_iter i;
  929. struct device * child;
  930. int error = 0;
  931. klist_iter_init(&parent->klist_children, &i);
  932. while ((child = next_device(&i)) && !error)
  933. error = fn(child, data);
  934. klist_iter_exit(&i);
  935. return error;
  936. }
  937. /**
  938. * device_find_child - device iterator for locating a particular device.
  939. * @parent: parent struct device
  940. * @data: Data to pass to match function
  941. * @match: Callback function to check device
  942. *
  943. * This is similar to the device_for_each_child() function above, but it
  944. * returns a reference to a device that is 'found' for later use, as
  945. * determined by the @match callback.
  946. *
  947. * The callback should return 0 if the device doesn't match and non-zero
  948. * if it does. If the callback returns non-zero and a reference to the
  949. * current device can be obtained, this function will return to the caller
  950. * and not iterate over any more devices.
  951. */
  952. struct device * device_find_child(struct device *parent, void *data,
  953. int (*match)(struct device *, void *))
  954. {
  955. struct klist_iter i;
  956. struct device *child;
  957. if (!parent)
  958. return NULL;
  959. klist_iter_init(&parent->klist_children, &i);
  960. while ((child = next_device(&i)))
  961. if (match(child, data) && get_device(child))
  962. break;
  963. klist_iter_exit(&i);
  964. return child;
  965. }
  966. int __init devices_init(void)
  967. {
  968. return subsystem_register(&devices_subsys);
  969. }
  970. EXPORT_SYMBOL_GPL(device_for_each_child);
  971. EXPORT_SYMBOL_GPL(device_find_child);
  972. EXPORT_SYMBOL_GPL(device_initialize);
  973. EXPORT_SYMBOL_GPL(device_add);
  974. EXPORT_SYMBOL_GPL(device_register);
  975. EXPORT_SYMBOL_GPL(device_del);
  976. EXPORT_SYMBOL_GPL(device_unregister);
  977. EXPORT_SYMBOL_GPL(get_device);
  978. EXPORT_SYMBOL_GPL(put_device);
  979. EXPORT_SYMBOL_GPL(device_create_file);
  980. EXPORT_SYMBOL_GPL(device_remove_file);
  981. static void device_create_release(struct device *dev)
  982. {
  983. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  984. kfree(dev);
  985. }
  986. /**
  987. * device_create - creates a device and registers it with sysfs
  988. * @class: pointer to the struct class that this device should be registered to
  989. * @parent: pointer to the parent struct device of this new device, if any
  990. * @devt: the dev_t for the char device to be added
  991. * @fmt: string for the device's name
  992. *
  993. * This function can be used by char device classes. A struct device
  994. * will be created in sysfs, registered to the specified class.
  995. *
  996. * A "dev" file will be created, showing the dev_t for the device, if
  997. * the dev_t is not 0,0.
  998. * If a pointer to a parent struct device is passed in, the newly created
  999. * struct device will be a child of that device in sysfs.
  1000. * The pointer to the struct device will be returned from the call.
  1001. * Any further sysfs files that might be required can be created using this
  1002. * pointer.
  1003. *
  1004. * Note: the struct class passed to this function must have previously
  1005. * been created with a call to class_create().
  1006. */
  1007. struct device *device_create(struct class *class, struct device *parent,
  1008. dev_t devt, const char *fmt, ...)
  1009. {
  1010. va_list args;
  1011. struct device *dev = NULL;
  1012. int retval = -ENODEV;
  1013. if (class == NULL || IS_ERR(class))
  1014. goto error;
  1015. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1016. if (!dev) {
  1017. retval = -ENOMEM;
  1018. goto error;
  1019. }
  1020. dev->devt = devt;
  1021. dev->class = class;
  1022. dev->parent = parent;
  1023. dev->release = device_create_release;
  1024. va_start(args, fmt);
  1025. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  1026. va_end(args);
  1027. retval = device_register(dev);
  1028. if (retval)
  1029. goto error;
  1030. return dev;
  1031. error:
  1032. kfree(dev);
  1033. return ERR_PTR(retval);
  1034. }
  1035. EXPORT_SYMBOL_GPL(device_create);
  1036. /**
  1037. * device_destroy - removes a device that was created with device_create()
  1038. * @class: pointer to the struct class that this device was registered with
  1039. * @devt: the dev_t of the device that was previously registered
  1040. *
  1041. * This call unregisters and cleans up a device that was created with a
  1042. * call to device_create().
  1043. */
  1044. void device_destroy(struct class *class, dev_t devt)
  1045. {
  1046. struct device *dev = NULL;
  1047. struct device *dev_tmp;
  1048. down(&class->sem);
  1049. list_for_each_entry(dev_tmp, &class->devices, node) {
  1050. if (dev_tmp->devt == devt) {
  1051. dev = dev_tmp;
  1052. break;
  1053. }
  1054. }
  1055. up(&class->sem);
  1056. if (dev)
  1057. device_unregister(dev);
  1058. }
  1059. EXPORT_SYMBOL_GPL(device_destroy);
  1060. /**
  1061. * device_rename - renames a device
  1062. * @dev: the pointer to the struct device to be renamed
  1063. * @new_name: the new name of the device
  1064. */
  1065. int device_rename(struct device *dev, char *new_name)
  1066. {
  1067. char *old_class_name = NULL;
  1068. char *new_class_name = NULL;
  1069. char *old_device_name = NULL;
  1070. int error;
  1071. dev = get_device(dev);
  1072. if (!dev)
  1073. return -EINVAL;
  1074. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  1075. #ifdef CONFIG_SYSFS_DEPRECATED
  1076. if ((dev->class) && (dev->parent))
  1077. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  1078. #endif
  1079. old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  1080. if (!old_device_name) {
  1081. error = -ENOMEM;
  1082. goto out;
  1083. }
  1084. strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
  1085. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  1086. error = kobject_rename(&dev->kobj, new_name);
  1087. if (error) {
  1088. strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
  1089. goto out;
  1090. }
  1091. #ifdef CONFIG_SYSFS_DEPRECATED
  1092. if (old_class_name) {
  1093. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  1094. if (new_class_name) {
  1095. error = sysfs_create_link(&dev->parent->kobj,
  1096. &dev->kobj, new_class_name);
  1097. if (error)
  1098. goto out;
  1099. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  1100. }
  1101. }
  1102. #endif
  1103. if (dev->class) {
  1104. sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
  1105. error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
  1106. dev->bus_id);
  1107. if (error) {
  1108. /* Uh... how to unravel this if restoring can fail? */
  1109. dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
  1110. __FUNCTION__, error);
  1111. }
  1112. }
  1113. out:
  1114. put_device(dev);
  1115. kfree(new_class_name);
  1116. kfree(old_class_name);
  1117. kfree(old_device_name);
  1118. return error;
  1119. }
  1120. EXPORT_SYMBOL_GPL(device_rename);
  1121. static int device_move_class_links(struct device *dev,
  1122. struct device *old_parent,
  1123. struct device *new_parent)
  1124. {
  1125. int error = 0;
  1126. #ifdef CONFIG_SYSFS_DEPRECATED
  1127. char *class_name;
  1128. class_name = make_class_name(dev->class->name, &dev->kobj);
  1129. if (!class_name) {
  1130. error = -ENOMEM;
  1131. goto out;
  1132. }
  1133. if (old_parent) {
  1134. sysfs_remove_link(&dev->kobj, "device");
  1135. sysfs_remove_link(&old_parent->kobj, class_name);
  1136. }
  1137. if (new_parent) {
  1138. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1139. "device");
  1140. if (error)
  1141. goto out;
  1142. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  1143. class_name);
  1144. if (error)
  1145. sysfs_remove_link(&dev->kobj, "device");
  1146. }
  1147. else
  1148. error = 0;
  1149. out:
  1150. kfree(class_name);
  1151. return error;
  1152. #else
  1153. if (old_parent)
  1154. sysfs_remove_link(&dev->kobj, "device");
  1155. if (new_parent)
  1156. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  1157. "device");
  1158. return error;
  1159. #endif
  1160. }
  1161. /**
  1162. * device_move - moves a device to a new parent
  1163. * @dev: the pointer to the struct device to be moved
  1164. * @new_parent: the new parent of the device (can by NULL)
  1165. */
  1166. int device_move(struct device *dev, struct device *new_parent)
  1167. {
  1168. int error;
  1169. struct device *old_parent;
  1170. struct kobject *new_parent_kobj;
  1171. dev = get_device(dev);
  1172. if (!dev)
  1173. return -EINVAL;
  1174. new_parent = get_device(new_parent);
  1175. new_parent_kobj = get_device_parent (dev, new_parent);
  1176. if (IS_ERR(new_parent_kobj)) {
  1177. error = PTR_ERR(new_parent_kobj);
  1178. put_device(new_parent);
  1179. goto out;
  1180. }
  1181. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  1182. new_parent ? new_parent->bus_id : "<NULL>");
  1183. error = kobject_move(&dev->kobj, new_parent_kobj);
  1184. if (error) {
  1185. put_device(new_parent);
  1186. goto out;
  1187. }
  1188. old_parent = dev->parent;
  1189. dev->parent = new_parent;
  1190. if (old_parent)
  1191. klist_remove(&dev->knode_parent);
  1192. if (new_parent)
  1193. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  1194. if (!dev->class)
  1195. goto out_put;
  1196. error = device_move_class_links(dev, old_parent, new_parent);
  1197. if (error) {
  1198. /* We ignore errors on cleanup since we're hosed anyway... */
  1199. device_move_class_links(dev, new_parent, old_parent);
  1200. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  1201. if (new_parent)
  1202. klist_remove(&dev->knode_parent);
  1203. if (old_parent)
  1204. klist_add_tail(&dev->knode_parent,
  1205. &old_parent->klist_children);
  1206. }
  1207. put_device(new_parent);
  1208. goto out;
  1209. }
  1210. out_put:
  1211. put_device(old_parent);
  1212. out:
  1213. put_device(dev);
  1214. return error;
  1215. }
  1216. EXPORT_SYMBOL_GPL(device_move);