core.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. * Detect the LANANA-assigned LOCAL/EXPERIMENTAL majors
  27. */
  28. bool is_lanana_major(unsigned int major)
  29. {
  30. if (major >= 60 && major <= 63)
  31. return 1;
  32. if (major >= 120 && major <= 127)
  33. return 1;
  34. if (major >= 240 && major <= 254)
  35. return 1;
  36. return 0;
  37. }
  38. /*
  39. * sysfs bindings for devices.
  40. */
  41. /**
  42. * dev_driver_string - Return a device's driver name, if at all possible
  43. * @dev: struct device to get the name of
  44. *
  45. * Will return the device's driver's name if it is bound to a device. If
  46. * the device is not bound to a device, it will return the name of the bus
  47. * it is attached to. If it is not attached to a bus either, an empty
  48. * string will be returned.
  49. */
  50. const char *dev_driver_string(struct device *dev)
  51. {
  52. return dev->driver ? dev->driver->name :
  53. (dev->bus ? dev->bus->name : "");
  54. }
  55. EXPORT_SYMBOL(dev_driver_string);
  56. #define to_dev(obj) container_of(obj, struct device, kobj)
  57. #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
  58. static ssize_t
  59. dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
  60. {
  61. struct device_attribute * dev_attr = to_dev_attr(attr);
  62. struct device * dev = to_dev(kobj);
  63. ssize_t ret = -EIO;
  64. if (dev_attr->show)
  65. ret = dev_attr->show(dev, dev_attr, buf);
  66. return ret;
  67. }
  68. static ssize_t
  69. dev_attr_store(struct kobject * kobj, struct attribute * attr,
  70. const char * buf, size_t count)
  71. {
  72. struct device_attribute * dev_attr = to_dev_attr(attr);
  73. struct device * dev = to_dev(kobj);
  74. ssize_t ret = -EIO;
  75. if (dev_attr->store)
  76. ret = dev_attr->store(dev, dev_attr, buf, count);
  77. return ret;
  78. }
  79. static struct sysfs_ops dev_sysfs_ops = {
  80. .show = dev_attr_show,
  81. .store = dev_attr_store,
  82. };
  83. /**
  84. * device_release - free device structure.
  85. * @kobj: device's kobject.
  86. *
  87. * This is called once the reference count for the object
  88. * reaches 0. We forward the call to the device's release
  89. * method, which should handle actually freeing the structure.
  90. */
  91. static void device_release(struct kobject * kobj)
  92. {
  93. struct device * dev = to_dev(kobj);
  94. if (dev->release)
  95. dev->release(dev);
  96. else if (dev->type && dev->type->release)
  97. dev->type->release(dev);
  98. else if (dev->class && dev->class->dev_release)
  99. dev->class->dev_release(dev);
  100. else {
  101. printk(KERN_ERR "Device '%s' does not have a release() function, "
  102. "it is broken and must be fixed.\n",
  103. dev->bus_id);
  104. WARN_ON(1);
  105. }
  106. }
  107. static struct kobj_type ktype_device = {
  108. .release = device_release,
  109. .sysfs_ops = &dev_sysfs_ops,
  110. };
  111. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  112. {
  113. struct kobj_type *ktype = get_ktype(kobj);
  114. if (ktype == &ktype_device) {
  115. struct device *dev = to_dev(kobj);
  116. if (dev->bus)
  117. return 1;
  118. if (dev->class)
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  124. {
  125. struct device *dev = to_dev(kobj);
  126. if (dev->bus)
  127. return dev->bus->name;
  128. if (dev->class)
  129. return dev->class->name;
  130. return NULL;
  131. }
  132. static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
  133. int num_envp, char *buffer, int buffer_size)
  134. {
  135. struct device *dev = to_dev(kobj);
  136. int i = 0;
  137. int length = 0;
  138. int retval = 0;
  139. /* add the major/minor if present */
  140. if (MAJOR(dev->devt)) {
  141. add_uevent_var(envp, num_envp, &i,
  142. buffer, buffer_size, &length,
  143. "MAJOR=%u", MAJOR(dev->devt));
  144. add_uevent_var(envp, num_envp, &i,
  145. buffer, buffer_size, &length,
  146. "MINOR=%u", MINOR(dev->devt));
  147. }
  148. if (dev->driver)
  149. add_uevent_var(envp, num_envp, &i,
  150. buffer, buffer_size, &length,
  151. "DRIVER=%s", dev->driver->name);
  152. #ifdef CONFIG_SYSFS_DEPRECATED
  153. if (dev->class) {
  154. struct device *parent = dev->parent;
  155. /* find first bus device in parent chain */
  156. while (parent && !parent->bus)
  157. parent = parent->parent;
  158. if (parent && parent->bus) {
  159. const char *path;
  160. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  161. add_uevent_var(envp, num_envp, &i,
  162. buffer, buffer_size, &length,
  163. "PHYSDEVPATH=%s", path);
  164. kfree(path);
  165. add_uevent_var(envp, num_envp, &i,
  166. buffer, buffer_size, &length,
  167. "PHYSDEVBUS=%s", parent->bus->name);
  168. if (parent->driver)
  169. add_uevent_var(envp, num_envp, &i,
  170. buffer, buffer_size, &length,
  171. "PHYSDEVDRIVER=%s", parent->driver->name);
  172. }
  173. } else if (dev->bus) {
  174. add_uevent_var(envp, num_envp, &i,
  175. buffer, buffer_size, &length,
  176. "PHYSDEVBUS=%s", dev->bus->name);
  177. if (dev->driver)
  178. add_uevent_var(envp, num_envp, &i,
  179. buffer, buffer_size, &length,
  180. "PHYSDEVDRIVER=%s", dev->driver->name);
  181. }
  182. #endif
  183. /* terminate, set to next free slot, shrink available space */
  184. envp[i] = NULL;
  185. envp = &envp[i];
  186. num_envp -= i;
  187. buffer = &buffer[length];
  188. buffer_size -= length;
  189. if (dev->bus && dev->bus->uevent) {
  190. /* have the bus specific function add its stuff */
  191. retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
  192. if (retval)
  193. pr_debug ("%s: bus uevent() returned %d\n",
  194. __FUNCTION__, retval);
  195. }
  196. if (dev->class && dev->class->dev_uevent) {
  197. /* have the class specific function add its stuff */
  198. retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
  199. if (retval)
  200. pr_debug("%s: class uevent() returned %d\n",
  201. __FUNCTION__, retval);
  202. }
  203. if (dev->type && dev->type->uevent) {
  204. /* have the device type specific fuction add its stuff */
  205. retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
  206. if (retval)
  207. pr_debug("%s: dev_type uevent() returned %d\n",
  208. __FUNCTION__, retval);
  209. }
  210. return retval;
  211. }
  212. static struct kset_uevent_ops device_uevent_ops = {
  213. .filter = dev_uevent_filter,
  214. .name = dev_uevent_name,
  215. .uevent = dev_uevent,
  216. };
  217. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  218. const char *buf, size_t count)
  219. {
  220. kobject_uevent(&dev->kobj, KOBJ_ADD);
  221. return count;
  222. }
  223. static int device_add_groups(struct device *dev)
  224. {
  225. int i;
  226. int error = 0;
  227. if (dev->groups) {
  228. for (i = 0; dev->groups[i]; i++) {
  229. error = sysfs_create_group(&dev->kobj, dev->groups[i]);
  230. if (error) {
  231. while (--i >= 0)
  232. sysfs_remove_group(&dev->kobj, dev->groups[i]);
  233. goto out;
  234. }
  235. }
  236. }
  237. out:
  238. return error;
  239. }
  240. static void device_remove_groups(struct device *dev)
  241. {
  242. int i;
  243. if (dev->groups) {
  244. for (i = 0; dev->groups[i]; i++) {
  245. sysfs_remove_group(&dev->kobj, dev->groups[i]);
  246. }
  247. }
  248. }
  249. static int device_add_attrs(struct device *dev)
  250. {
  251. struct class *class = dev->class;
  252. struct device_type *type = dev->type;
  253. int error = 0;
  254. int i;
  255. if (class && class->dev_attrs) {
  256. for (i = 0; attr_name(class->dev_attrs[i]); i++) {
  257. error = device_create_file(dev, &class->dev_attrs[i]);
  258. if (error)
  259. break;
  260. }
  261. if (error)
  262. while (--i >= 0)
  263. device_remove_file(dev, &class->dev_attrs[i]);
  264. }
  265. if (type && type->attrs) {
  266. for (i = 0; attr_name(type->attrs[i]); i++) {
  267. error = device_create_file(dev, &type->attrs[i]);
  268. if (error)
  269. break;
  270. }
  271. if (error)
  272. while (--i >= 0)
  273. device_remove_file(dev, &type->attrs[i]);
  274. }
  275. return error;
  276. }
  277. static void device_remove_attrs(struct device *dev)
  278. {
  279. struct class *class = dev->class;
  280. struct device_type *type = dev->type;
  281. int i;
  282. if (class && class->dev_attrs) {
  283. for (i = 0; attr_name(class->dev_attrs[i]); i++)
  284. device_remove_file(dev, &class->dev_attrs[i]);
  285. }
  286. if (type && type->attrs) {
  287. for (i = 0; attr_name(type->attrs[i]); i++)
  288. device_remove_file(dev, &type->attrs[i]);
  289. }
  290. }
  291. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  292. char *buf)
  293. {
  294. return print_dev_t(buf, dev->devt);
  295. }
  296. /*
  297. * devices_subsys - structure to be registered with kobject core.
  298. */
  299. decl_subsys(devices, &ktype_device, &device_uevent_ops);
  300. /**
  301. * device_create_file - create sysfs attribute file for device.
  302. * @dev: device.
  303. * @attr: device attribute descriptor.
  304. */
  305. int device_create_file(struct device * dev, struct device_attribute * attr)
  306. {
  307. int error = 0;
  308. if (get_device(dev)) {
  309. error = sysfs_create_file(&dev->kobj, &attr->attr);
  310. put_device(dev);
  311. }
  312. return error;
  313. }
  314. /**
  315. * device_remove_file - remove sysfs attribute file.
  316. * @dev: device.
  317. * @attr: device attribute descriptor.
  318. */
  319. void device_remove_file(struct device * dev, struct device_attribute * attr)
  320. {
  321. if (get_device(dev)) {
  322. sysfs_remove_file(&dev->kobj, &attr->attr);
  323. put_device(dev);
  324. }
  325. }
  326. /**
  327. * device_create_bin_file - create sysfs binary attribute file for device.
  328. * @dev: device.
  329. * @attr: device binary attribute descriptor.
  330. */
  331. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  332. {
  333. int error = -EINVAL;
  334. if (dev)
  335. error = sysfs_create_bin_file(&dev->kobj, attr);
  336. return error;
  337. }
  338. EXPORT_SYMBOL_GPL(device_create_bin_file);
  339. /**
  340. * device_remove_bin_file - remove sysfs binary attribute file
  341. * @dev: device.
  342. * @attr: device binary attribute descriptor.
  343. */
  344. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  345. {
  346. if (dev)
  347. sysfs_remove_bin_file(&dev->kobj, attr);
  348. }
  349. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  350. static void klist_children_get(struct klist_node *n)
  351. {
  352. struct device *dev = container_of(n, struct device, knode_parent);
  353. get_device(dev);
  354. }
  355. static void klist_children_put(struct klist_node *n)
  356. {
  357. struct device *dev = container_of(n, struct device, knode_parent);
  358. put_device(dev);
  359. }
  360. /**
  361. * device_initialize - init device structure.
  362. * @dev: device.
  363. *
  364. * This prepares the device for use by other layers,
  365. * including adding it to the device hierarchy.
  366. * It is the first half of device_register(), if called by
  367. * that, though it can also be called separately, so one
  368. * may use @dev's fields (e.g. the refcount).
  369. */
  370. void device_initialize(struct device *dev)
  371. {
  372. kobj_set_kset_s(dev, devices_subsys);
  373. kobject_init(&dev->kobj);
  374. klist_init(&dev->klist_children, klist_children_get,
  375. klist_children_put);
  376. INIT_LIST_HEAD(&dev->dma_pools);
  377. INIT_LIST_HEAD(&dev->node);
  378. init_MUTEX(&dev->sem);
  379. spin_lock_init(&dev->devres_lock);
  380. INIT_LIST_HEAD(&dev->devres_head);
  381. device_init_wakeup(dev, 0);
  382. set_dev_node(dev, -1);
  383. }
  384. #ifdef CONFIG_SYSFS_DEPRECATED
  385. static struct kobject * get_device_parent(struct device *dev,
  386. struct device *parent)
  387. {
  388. /* Set the parent to the class, not the parent device */
  389. /* this keeps sysfs from having a symlink to make old udevs happy */
  390. if (dev->class)
  391. return &dev->class->subsys.kset.kobj;
  392. else if (parent)
  393. return &parent->kobj;
  394. return NULL;
  395. }
  396. #else
  397. static struct kobject * virtual_device_parent(struct device *dev)
  398. {
  399. if (!dev->class)
  400. return ERR_PTR(-ENODEV);
  401. if (!dev->class->virtual_dir) {
  402. static struct kobject *virtual_dir = NULL;
  403. if (!virtual_dir)
  404. virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
  405. dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
  406. }
  407. return dev->class->virtual_dir;
  408. }
  409. static struct kobject * get_device_parent(struct device *dev,
  410. struct device *parent)
  411. {
  412. /* if this is a class device, and has no parent, create one */
  413. if ((dev->class) && (parent == NULL)) {
  414. return virtual_device_parent(dev);
  415. } else if (parent)
  416. return &parent->kobj;
  417. return NULL;
  418. }
  419. #endif
  420. static int setup_parent(struct device *dev, struct device *parent)
  421. {
  422. struct kobject *kobj;
  423. kobj = get_device_parent(dev, parent);
  424. if (IS_ERR(kobj))
  425. return PTR_ERR(kobj);
  426. if (kobj)
  427. dev->kobj.parent = kobj;
  428. return 0;
  429. }
  430. /**
  431. * device_add - add device to device hierarchy.
  432. * @dev: device.
  433. *
  434. * This is part 2 of device_register(), though may be called
  435. * separately _iff_ device_initialize() has been called separately.
  436. *
  437. * This adds it to the kobject hierarchy via kobject_add(), adds it
  438. * to the global and sibling lists for the device, then
  439. * adds it to the other relevant subsystems of the driver model.
  440. */
  441. int device_add(struct device *dev)
  442. {
  443. struct device *parent = NULL;
  444. char *class_name = NULL;
  445. struct class_interface *class_intf;
  446. int error = -EINVAL;
  447. dev = get_device(dev);
  448. if (!dev || !strlen(dev->bus_id))
  449. goto Error;
  450. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  451. parent = get_device(dev->parent);
  452. error = setup_parent(dev, parent);
  453. if (error)
  454. goto Error;
  455. /* first, register with generic layer. */
  456. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  457. error = kobject_add(&dev->kobj);
  458. if (error)
  459. goto Error;
  460. /* notify platform of device entry */
  461. if (platform_notify)
  462. platform_notify(dev);
  463. /* notify clients of device entry (new way) */
  464. if (dev->bus)
  465. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  466. BUS_NOTIFY_ADD_DEVICE, dev);
  467. dev->uevent_attr.attr.name = "uevent";
  468. dev->uevent_attr.attr.mode = S_IWUSR;
  469. if (dev->driver)
  470. dev->uevent_attr.attr.owner = dev->driver->owner;
  471. dev->uevent_attr.store = store_uevent;
  472. error = device_create_file(dev, &dev->uevent_attr);
  473. if (error)
  474. goto attrError;
  475. if (MAJOR(dev->devt)) {
  476. struct device_attribute *attr;
  477. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  478. if (!attr) {
  479. error = -ENOMEM;
  480. goto ueventattrError;
  481. }
  482. attr->attr.name = "dev";
  483. attr->attr.mode = S_IRUGO;
  484. if (dev->driver)
  485. attr->attr.owner = dev->driver->owner;
  486. attr->show = show_dev;
  487. error = device_create_file(dev, attr);
  488. if (error) {
  489. kfree(attr);
  490. goto ueventattrError;
  491. }
  492. dev->devt_attr = attr;
  493. }
  494. if (dev->class) {
  495. sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
  496. "subsystem");
  497. /* If this is not a "fake" compatible device, then create the
  498. * symlink from the class to the device. */
  499. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  500. sysfs_create_link(&dev->class->subsys.kset.kobj,
  501. &dev->kobj, dev->bus_id);
  502. #ifdef CONFIG_SYSFS_DEPRECATED
  503. if (parent) {
  504. sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  505. "device");
  506. class_name = make_class_name(dev->class->name,
  507. &dev->kobj);
  508. if (class_name)
  509. sysfs_create_link(&dev->parent->kobj,
  510. &dev->kobj, class_name);
  511. }
  512. #endif
  513. }
  514. if ((error = device_add_attrs(dev)))
  515. goto AttrsError;
  516. if ((error = device_add_groups(dev)))
  517. goto GroupError;
  518. if ((error = device_pm_add(dev)))
  519. goto PMError;
  520. if ((error = bus_add_device(dev)))
  521. goto BusError;
  522. if (!dev->uevent_suppress)
  523. kobject_uevent(&dev->kobj, KOBJ_ADD);
  524. if ((error = bus_attach_device(dev)))
  525. goto AttachError;
  526. if (parent)
  527. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  528. if (dev->class) {
  529. down(&dev->class->sem);
  530. /* tie the class to the device */
  531. list_add_tail(&dev->node, &dev->class->devices);
  532. /* notify any interfaces that the device is here */
  533. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  534. if (class_intf->add_dev)
  535. class_intf->add_dev(dev, class_intf);
  536. up(&dev->class->sem);
  537. }
  538. Done:
  539. kfree(class_name);
  540. put_device(dev);
  541. return error;
  542. AttachError:
  543. bus_remove_device(dev);
  544. BusError:
  545. device_pm_remove(dev);
  546. PMError:
  547. if (dev->bus)
  548. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  549. BUS_NOTIFY_DEL_DEVICE, dev);
  550. device_remove_groups(dev);
  551. GroupError:
  552. device_remove_attrs(dev);
  553. AttrsError:
  554. if (dev->devt_attr) {
  555. device_remove_file(dev, dev->devt_attr);
  556. kfree(dev->devt_attr);
  557. }
  558. ueventattrError:
  559. device_remove_file(dev, &dev->uevent_attr);
  560. attrError:
  561. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  562. kobject_del(&dev->kobj);
  563. Error:
  564. if (parent)
  565. put_device(parent);
  566. goto Done;
  567. }
  568. /**
  569. * device_register - register a device with the system.
  570. * @dev: pointer to the device structure
  571. *
  572. * This happens in two clean steps - initialize the device
  573. * and add it to the system. The two steps can be called
  574. * separately, but this is the easiest and most common.
  575. * I.e. you should only call the two helpers separately if
  576. * have a clearly defined need to use and refcount the device
  577. * before it is added to the hierarchy.
  578. */
  579. int device_register(struct device *dev)
  580. {
  581. device_initialize(dev);
  582. return device_add(dev);
  583. }
  584. /**
  585. * get_device - increment reference count for device.
  586. * @dev: device.
  587. *
  588. * This simply forwards the call to kobject_get(), though
  589. * we do take care to provide for the case that we get a NULL
  590. * pointer passed in.
  591. */
  592. struct device * get_device(struct device * dev)
  593. {
  594. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  595. }
  596. /**
  597. * put_device - decrement reference count.
  598. * @dev: device in question.
  599. */
  600. void put_device(struct device * dev)
  601. {
  602. if (dev)
  603. kobject_put(&dev->kobj);
  604. }
  605. /**
  606. * device_del - delete device from system.
  607. * @dev: device.
  608. *
  609. * This is the first part of the device unregistration
  610. * sequence. This removes the device from the lists we control
  611. * from here, has it removed from the other driver model
  612. * subsystems it was added to in device_add(), and removes it
  613. * from the kobject hierarchy.
  614. *
  615. * NOTE: this should be called manually _iff_ device_add() was
  616. * also called manually.
  617. */
  618. void device_del(struct device * dev)
  619. {
  620. struct device * parent = dev->parent;
  621. struct class_interface *class_intf;
  622. if (parent)
  623. klist_del(&dev->knode_parent);
  624. if (dev->devt_attr) {
  625. device_remove_file(dev, dev->devt_attr);
  626. kfree(dev->devt_attr);
  627. }
  628. if (dev->class) {
  629. sysfs_remove_link(&dev->kobj, "subsystem");
  630. /* If this is not a "fake" compatible device, remove the
  631. * symlink from the class to the device. */
  632. if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
  633. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  634. dev->bus_id);
  635. #ifdef CONFIG_SYSFS_DEPRECATED
  636. if (parent) {
  637. char *class_name = make_class_name(dev->class->name,
  638. &dev->kobj);
  639. if (class_name)
  640. sysfs_remove_link(&dev->parent->kobj,
  641. class_name);
  642. kfree(class_name);
  643. sysfs_remove_link(&dev->kobj, "device");
  644. }
  645. #endif
  646. down(&dev->class->sem);
  647. /* notify any interfaces that the device is now gone */
  648. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  649. if (class_intf->remove_dev)
  650. class_intf->remove_dev(dev, class_intf);
  651. /* remove the device from the class list */
  652. list_del_init(&dev->node);
  653. up(&dev->class->sem);
  654. }
  655. device_remove_file(dev, &dev->uevent_attr);
  656. device_remove_groups(dev);
  657. device_remove_attrs(dev);
  658. bus_remove_device(dev);
  659. /* Notify the platform of the removal, in case they
  660. * need to do anything...
  661. */
  662. if (platform_notify_remove)
  663. platform_notify_remove(dev);
  664. if (dev->bus)
  665. blocking_notifier_call_chain(&dev->bus->bus_notifier,
  666. BUS_NOTIFY_DEL_DEVICE, dev);
  667. device_pm_remove(dev);
  668. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  669. kobject_del(&dev->kobj);
  670. if (parent)
  671. put_device(parent);
  672. }
  673. /**
  674. * device_unregister - unregister device from system.
  675. * @dev: device going away.
  676. *
  677. * We do this in two parts, like we do device_register(). First,
  678. * we remove it from all the subsystems with device_del(), then
  679. * we decrement the reference count via put_device(). If that
  680. * is the final reference count, the device will be cleaned up
  681. * via device_release() above. Otherwise, the structure will
  682. * stick around until the final reference to the device is dropped.
  683. */
  684. void device_unregister(struct device * dev)
  685. {
  686. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  687. device_del(dev);
  688. put_device(dev);
  689. }
  690. static struct device * next_device(struct klist_iter * i)
  691. {
  692. struct klist_node * n = klist_next(i);
  693. return n ? container_of(n, struct device, knode_parent) : NULL;
  694. }
  695. /**
  696. * device_for_each_child - device child iterator.
  697. * @parent: parent struct device.
  698. * @data: data for the callback.
  699. * @fn: function to be called for each device.
  700. *
  701. * Iterate over @parent's child devices, and call @fn for each,
  702. * passing it @data.
  703. *
  704. * We check the return of @fn each time. If it returns anything
  705. * other than 0, we break out and return that value.
  706. */
  707. int device_for_each_child(struct device * parent, void * data,
  708. int (*fn)(struct device *, void *))
  709. {
  710. struct klist_iter i;
  711. struct device * child;
  712. int error = 0;
  713. klist_iter_init(&parent->klist_children, &i);
  714. while ((child = next_device(&i)) && !error)
  715. error = fn(child, data);
  716. klist_iter_exit(&i);
  717. return error;
  718. }
  719. /**
  720. * device_find_child - device iterator for locating a particular device.
  721. * @parent: parent struct device
  722. * @data: Data to pass to match function
  723. * @match: Callback function to check device
  724. *
  725. * This is similar to the device_for_each_child() function above, but it
  726. * returns a reference to a device that is 'found' for later use, as
  727. * determined by the @match callback.
  728. *
  729. * The callback should return 0 if the device doesn't match and non-zero
  730. * if it does. If the callback returns non-zero and a reference to the
  731. * current device can be obtained, this function will return to the caller
  732. * and not iterate over any more devices.
  733. */
  734. struct device * device_find_child(struct device *parent, void *data,
  735. int (*match)(struct device *, void *))
  736. {
  737. struct klist_iter i;
  738. struct device *child;
  739. if (!parent)
  740. return NULL;
  741. klist_iter_init(&parent->klist_children, &i);
  742. while ((child = next_device(&i)))
  743. if (match(child, data) && get_device(child))
  744. break;
  745. klist_iter_exit(&i);
  746. return child;
  747. }
  748. int __init devices_init(void)
  749. {
  750. return subsystem_register(&devices_subsys);
  751. }
  752. EXPORT_SYMBOL_GPL(device_for_each_child);
  753. EXPORT_SYMBOL_GPL(device_find_child);
  754. EXPORT_SYMBOL_GPL(device_initialize);
  755. EXPORT_SYMBOL_GPL(device_add);
  756. EXPORT_SYMBOL_GPL(device_register);
  757. EXPORT_SYMBOL_GPL(device_del);
  758. EXPORT_SYMBOL_GPL(device_unregister);
  759. EXPORT_SYMBOL_GPL(get_device);
  760. EXPORT_SYMBOL_GPL(put_device);
  761. EXPORT_SYMBOL_GPL(device_create_file);
  762. EXPORT_SYMBOL_GPL(device_remove_file);
  763. static void device_create_release(struct device *dev)
  764. {
  765. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  766. kfree(dev);
  767. }
  768. /**
  769. * device_create - creates a device and registers it with sysfs
  770. * @class: pointer to the struct class that this device should be registered to
  771. * @parent: pointer to the parent struct device of this new device, if any
  772. * @devt: the dev_t for the char device to be added
  773. * @fmt: string for the device's name
  774. *
  775. * This function can be used by char device classes. A struct device
  776. * will be created in sysfs, registered to the specified class.
  777. *
  778. * A "dev" file will be created, showing the dev_t for the device, if
  779. * the dev_t is not 0,0.
  780. * If a pointer to a parent struct device is passed in, the newly created
  781. * struct device will be a child of that device in sysfs.
  782. * The pointer to the struct device will be returned from the call.
  783. * Any further sysfs files that might be required can be created using this
  784. * pointer.
  785. *
  786. * Note: the struct class passed to this function must have previously
  787. * been created with a call to class_create().
  788. */
  789. struct device *device_create(struct class *class, struct device *parent,
  790. dev_t devt, const char *fmt, ...)
  791. {
  792. va_list args;
  793. struct device *dev = NULL;
  794. int retval = -ENODEV;
  795. if (class == NULL || IS_ERR(class))
  796. goto error;
  797. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  798. if (!dev) {
  799. retval = -ENOMEM;
  800. goto error;
  801. }
  802. dev->devt = devt;
  803. dev->class = class;
  804. dev->parent = parent;
  805. dev->release = device_create_release;
  806. va_start(args, fmt);
  807. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  808. va_end(args);
  809. retval = device_register(dev);
  810. if (retval)
  811. goto error;
  812. return dev;
  813. error:
  814. kfree(dev);
  815. return ERR_PTR(retval);
  816. }
  817. EXPORT_SYMBOL_GPL(device_create);
  818. /**
  819. * device_destroy - removes a device that was created with device_create()
  820. * @class: pointer to the struct class that this device was registered with
  821. * @devt: the dev_t of the device that was previously registered
  822. *
  823. * This call unregisters and cleans up a device that was created with a
  824. * call to device_create().
  825. */
  826. void device_destroy(struct class *class, dev_t devt)
  827. {
  828. struct device *dev = NULL;
  829. struct device *dev_tmp;
  830. down(&class->sem);
  831. list_for_each_entry(dev_tmp, &class->devices, node) {
  832. if (dev_tmp->devt == devt) {
  833. dev = dev_tmp;
  834. break;
  835. }
  836. }
  837. up(&class->sem);
  838. if (dev)
  839. device_unregister(dev);
  840. }
  841. EXPORT_SYMBOL_GPL(device_destroy);
  842. /**
  843. * device_rename - renames a device
  844. * @dev: the pointer to the struct device to be renamed
  845. * @new_name: the new name of the device
  846. */
  847. int device_rename(struct device *dev, char *new_name)
  848. {
  849. char *old_class_name = NULL;
  850. char *new_class_name = NULL;
  851. char *old_symlink_name = NULL;
  852. int error;
  853. dev = get_device(dev);
  854. if (!dev)
  855. return -EINVAL;
  856. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  857. #ifdef CONFIG_SYSFS_DEPRECATED
  858. if ((dev->class) && (dev->parent))
  859. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  860. #endif
  861. if (dev->class) {
  862. old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  863. if (!old_symlink_name) {
  864. error = -ENOMEM;
  865. goto out_free_old_class;
  866. }
  867. strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
  868. }
  869. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  870. error = kobject_rename(&dev->kobj, new_name);
  871. #ifdef CONFIG_SYSFS_DEPRECATED
  872. if (old_class_name) {
  873. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  874. if (new_class_name) {
  875. sysfs_create_link(&dev->parent->kobj, &dev->kobj,
  876. new_class_name);
  877. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  878. }
  879. }
  880. #endif
  881. if (dev->class) {
  882. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  883. old_symlink_name);
  884. sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
  885. dev->bus_id);
  886. }
  887. put_device(dev);
  888. kfree(new_class_name);
  889. kfree(old_symlink_name);
  890. out_free_old_class:
  891. kfree(old_class_name);
  892. return error;
  893. }
  894. static int device_move_class_links(struct device *dev,
  895. struct device *old_parent,
  896. struct device *new_parent)
  897. {
  898. #ifdef CONFIG_SYSFS_DEPRECATED
  899. int error;
  900. char *class_name;
  901. class_name = make_class_name(dev->class->name, &dev->kobj);
  902. if (!class_name) {
  903. error = -ENOMEM;
  904. goto out;
  905. }
  906. if (old_parent) {
  907. sysfs_remove_link(&dev->kobj, "device");
  908. sysfs_remove_link(&old_parent->kobj, class_name);
  909. }
  910. if (new_parent) {
  911. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  912. "device");
  913. if (error)
  914. goto out;
  915. error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
  916. class_name);
  917. if (error)
  918. sysfs_remove_link(&dev->kobj, "device");
  919. }
  920. else
  921. error = 0;
  922. out:
  923. kfree(class_name);
  924. return error;
  925. #else
  926. return 0;
  927. #endif
  928. }
  929. /**
  930. * device_move - moves a device to a new parent
  931. * @dev: the pointer to the struct device to be moved
  932. * @new_parent: the new parent of the device (can by NULL)
  933. */
  934. int device_move(struct device *dev, struct device *new_parent)
  935. {
  936. int error;
  937. struct device *old_parent;
  938. struct kobject *new_parent_kobj;
  939. dev = get_device(dev);
  940. if (!dev)
  941. return -EINVAL;
  942. new_parent = get_device(new_parent);
  943. new_parent_kobj = get_device_parent (dev, new_parent);
  944. if (IS_ERR(new_parent_kobj)) {
  945. error = PTR_ERR(new_parent_kobj);
  946. put_device(new_parent);
  947. goto out;
  948. }
  949. pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
  950. new_parent ? new_parent->bus_id : "<NULL>");
  951. error = kobject_move(&dev->kobj, new_parent_kobj);
  952. if (error) {
  953. put_device(new_parent);
  954. goto out;
  955. }
  956. old_parent = dev->parent;
  957. dev->parent = new_parent;
  958. if (old_parent)
  959. klist_remove(&dev->knode_parent);
  960. if (new_parent)
  961. klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
  962. if (!dev->class)
  963. goto out_put;
  964. error = device_move_class_links(dev, old_parent, new_parent);
  965. if (error) {
  966. /* We ignore errors on cleanup since we're hosed anyway... */
  967. device_move_class_links(dev, new_parent, old_parent);
  968. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  969. if (new_parent)
  970. klist_remove(&dev->knode_parent);
  971. if (old_parent)
  972. klist_add_tail(&dev->knode_parent,
  973. &old_parent->klist_children);
  974. }
  975. put_device(new_parent);
  976. goto out;
  977. }
  978. out_put:
  979. put_device(old_parent);
  980. out:
  981. put_device(dev);
  982. return error;
  983. }
  984. EXPORT_SYMBOL_GPL(device_move);