core.c 23 KB

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