core.c 21 KB

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