core.c 18 KB

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