core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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(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. /**
  273. * device_create_bin_file - create sysfs binary attribute file for device.
  274. * @dev: device.
  275. * @attr: device binary attribute descriptor.
  276. */
  277. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  278. {
  279. int error = -EINVAL;
  280. if (dev)
  281. error = sysfs_create_bin_file(&dev->kobj, attr);
  282. return error;
  283. }
  284. EXPORT_SYMBOL_GPL(device_create_bin_file);
  285. /**
  286. * device_remove_bin_file - remove sysfs binary attribute file
  287. * @dev: device.
  288. * @attr: device binary attribute descriptor.
  289. */
  290. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  291. {
  292. if (dev)
  293. sysfs_remove_bin_file(&dev->kobj, attr);
  294. }
  295. EXPORT_SYMBOL_GPL(device_remove_bin_file);
  296. static void klist_children_get(struct klist_node *n)
  297. {
  298. struct device *dev = container_of(n, struct device, knode_parent);
  299. get_device(dev);
  300. }
  301. static void klist_children_put(struct klist_node *n)
  302. {
  303. struct device *dev = container_of(n, struct device, knode_parent);
  304. put_device(dev);
  305. }
  306. /**
  307. * device_initialize - init device structure.
  308. * @dev: device.
  309. *
  310. * This prepares the device for use by other layers,
  311. * including adding it to the device hierarchy.
  312. * It is the first half of device_register(), if called by
  313. * that, though it can also be called separately, so one
  314. * may use @dev's fields (e.g. the refcount).
  315. */
  316. void device_initialize(struct device *dev)
  317. {
  318. kobj_set_kset_s(dev, devices_subsys);
  319. kobject_init(&dev->kobj);
  320. klist_init(&dev->klist_children, klist_children_get,
  321. klist_children_put);
  322. INIT_LIST_HEAD(&dev->dma_pools);
  323. INIT_LIST_HEAD(&dev->node);
  324. init_MUTEX(&dev->sem);
  325. device_init_wakeup(dev, 0);
  326. }
  327. /**
  328. * device_add - add device to device hierarchy.
  329. * @dev: device.
  330. *
  331. * This is part 2 of device_register(), though may be called
  332. * separately _iff_ device_initialize() has been called separately.
  333. *
  334. * This adds it to the kobject hierarchy via kobject_add(), adds it
  335. * to the global and sibling lists for the device, then
  336. * adds it to the other relevant subsystems of the driver model.
  337. */
  338. int device_add(struct device *dev)
  339. {
  340. struct device *parent = NULL;
  341. char *class_name = NULL;
  342. struct class_interface *class_intf;
  343. int error = -EINVAL;
  344. dev = get_device(dev);
  345. if (!dev || !strlen(dev->bus_id))
  346. goto Error;
  347. /* if this is a class device, and has no parent, create one */
  348. if ((dev->class) && (dev->parent == NULL)) {
  349. error = virtual_device_parent(dev);
  350. if (error)
  351. goto Error;
  352. }
  353. parent = get_device(dev->parent);
  354. pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
  355. /* first, register with generic layer. */
  356. kobject_set_name(&dev->kobj, "%s", dev->bus_id);
  357. if (parent)
  358. dev->kobj.parent = &parent->kobj;
  359. if ((error = kobject_add(&dev->kobj)))
  360. goto Error;
  361. /* notify platform of device entry */
  362. if (platform_notify)
  363. platform_notify(dev);
  364. dev->uevent_attr.attr.name = "uevent";
  365. dev->uevent_attr.attr.mode = S_IWUSR;
  366. if (dev->driver)
  367. dev->uevent_attr.attr.owner = dev->driver->owner;
  368. dev->uevent_attr.store = store_uevent;
  369. error = device_create_file(dev, &dev->uevent_attr);
  370. if (error)
  371. goto attrError;
  372. if (MAJOR(dev->devt)) {
  373. struct device_attribute *attr;
  374. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  375. if (!attr) {
  376. error = -ENOMEM;
  377. goto ueventattrError;
  378. }
  379. attr->attr.name = "dev";
  380. attr->attr.mode = S_IRUGO;
  381. if (dev->driver)
  382. attr->attr.owner = dev->driver->owner;
  383. attr->show = show_dev;
  384. error = device_create_file(dev, attr);
  385. if (error) {
  386. kfree(attr);
  387. goto ueventattrError;
  388. }
  389. dev->devt_attr = attr;
  390. }
  391. if (dev->class) {
  392. sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
  393. "subsystem");
  394. sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
  395. dev->bus_id);
  396. if (parent) {
  397. sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
  398. class_name = make_class_name(dev->class->name, &dev->kobj);
  399. sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
  400. }
  401. }
  402. if ((error = device_add_attrs(dev)))
  403. goto AttrsError;
  404. if ((error = device_add_groups(dev)))
  405. goto GroupError;
  406. if ((error = device_pm_add(dev)))
  407. goto PMError;
  408. if ((error = bus_add_device(dev)))
  409. goto BusError;
  410. kobject_uevent(&dev->kobj, KOBJ_ADD);
  411. if ((error = bus_attach_device(dev)))
  412. goto AttachError;
  413. if (parent)
  414. klist_add_tail(&dev->knode_parent, &parent->klist_children);
  415. if (dev->class) {
  416. down(&dev->class->sem);
  417. /* tie the class to the device */
  418. list_add_tail(&dev->node, &dev->class->devices);
  419. /* notify any interfaces that the device is here */
  420. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  421. if (class_intf->add_dev)
  422. class_intf->add_dev(dev, class_intf);
  423. up(&dev->class->sem);
  424. }
  425. Done:
  426. kfree(class_name);
  427. put_device(dev);
  428. return error;
  429. AttachError:
  430. bus_remove_device(dev);
  431. BusError:
  432. device_pm_remove(dev);
  433. PMError:
  434. device_remove_groups(dev);
  435. GroupError:
  436. device_remove_attrs(dev);
  437. AttrsError:
  438. if (dev->devt_attr) {
  439. device_remove_file(dev, dev->devt_attr);
  440. kfree(dev->devt_attr);
  441. }
  442. ueventattrError:
  443. device_remove_file(dev, &dev->uevent_attr);
  444. attrError:
  445. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  446. kobject_del(&dev->kobj);
  447. Error:
  448. if (parent)
  449. put_device(parent);
  450. goto Done;
  451. }
  452. /**
  453. * device_register - register a device with the system.
  454. * @dev: pointer to the device structure
  455. *
  456. * This happens in two clean steps - initialize the device
  457. * and add it to the system. The two steps can be called
  458. * separately, but this is the easiest and most common.
  459. * I.e. you should only call the two helpers separately if
  460. * have a clearly defined need to use and refcount the device
  461. * before it is added to the hierarchy.
  462. */
  463. int device_register(struct device *dev)
  464. {
  465. device_initialize(dev);
  466. return device_add(dev);
  467. }
  468. /**
  469. * get_device - increment reference count for device.
  470. * @dev: device.
  471. *
  472. * This simply forwards the call to kobject_get(), though
  473. * we do take care to provide for the case that we get a NULL
  474. * pointer passed in.
  475. */
  476. struct device * get_device(struct device * dev)
  477. {
  478. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  479. }
  480. /**
  481. * put_device - decrement reference count.
  482. * @dev: device in question.
  483. */
  484. void put_device(struct device * dev)
  485. {
  486. if (dev)
  487. kobject_put(&dev->kobj);
  488. }
  489. /**
  490. * device_del - delete device from system.
  491. * @dev: device.
  492. *
  493. * This is the first part of the device unregistration
  494. * sequence. This removes the device from the lists we control
  495. * from here, has it removed from the other driver model
  496. * subsystems it was added to in device_add(), and removes it
  497. * from the kobject hierarchy.
  498. *
  499. * NOTE: this should be called manually _iff_ device_add() was
  500. * also called manually.
  501. */
  502. void device_del(struct device * dev)
  503. {
  504. struct device * parent = dev->parent;
  505. char *class_name = NULL;
  506. struct class_interface *class_intf;
  507. if (parent)
  508. klist_del(&dev->knode_parent);
  509. if (dev->devt_attr)
  510. device_remove_file(dev, dev->devt_attr);
  511. if (dev->class) {
  512. sysfs_remove_link(&dev->kobj, "subsystem");
  513. sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
  514. class_name = make_class_name(dev->class->name, &dev->kobj);
  515. if (parent) {
  516. sysfs_remove_link(&dev->kobj, "device");
  517. sysfs_remove_link(&dev->parent->kobj, class_name);
  518. }
  519. kfree(class_name);
  520. down(&dev->class->sem);
  521. /* notify any interfaces that the device is now gone */
  522. list_for_each_entry(class_intf, &dev->class->interfaces, node)
  523. if (class_intf->remove_dev)
  524. class_intf->remove_dev(dev, class_intf);
  525. /* remove the device from the class list */
  526. list_del_init(&dev->node);
  527. up(&dev->class->sem);
  528. }
  529. device_remove_file(dev, &dev->uevent_attr);
  530. device_remove_groups(dev);
  531. device_remove_attrs(dev);
  532. /* Notify the platform of the removal, in case they
  533. * need to do anything...
  534. */
  535. if (platform_notify_remove)
  536. platform_notify_remove(dev);
  537. bus_remove_device(dev);
  538. device_pm_remove(dev);
  539. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  540. kobject_del(&dev->kobj);
  541. if (parent)
  542. put_device(parent);
  543. }
  544. /**
  545. * device_unregister - unregister device from system.
  546. * @dev: device going away.
  547. *
  548. * We do this in two parts, like we do device_register(). First,
  549. * we remove it from all the subsystems with device_del(), then
  550. * we decrement the reference count via put_device(). If that
  551. * is the final reference count, the device will be cleaned up
  552. * via device_release() above. Otherwise, the structure will
  553. * stick around until the final reference to the device is dropped.
  554. */
  555. void device_unregister(struct device * dev)
  556. {
  557. pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
  558. device_del(dev);
  559. put_device(dev);
  560. }
  561. static struct device * next_device(struct klist_iter * i)
  562. {
  563. struct klist_node * n = klist_next(i);
  564. return n ? container_of(n, struct device, knode_parent) : NULL;
  565. }
  566. /**
  567. * device_for_each_child - device child iterator.
  568. * @parent: parent struct device.
  569. * @data: data for the callback.
  570. * @fn: function to be called for each device.
  571. *
  572. * Iterate over @parent's child devices, and call @fn for each,
  573. * passing it @data.
  574. *
  575. * We check the return of @fn each time. If it returns anything
  576. * other than 0, we break out and return that value.
  577. */
  578. int device_for_each_child(struct device * parent, void * data,
  579. int (*fn)(struct device *, void *))
  580. {
  581. struct klist_iter i;
  582. struct device * child;
  583. int error = 0;
  584. klist_iter_init(&parent->klist_children, &i);
  585. while ((child = next_device(&i)) && !error)
  586. error = fn(child, data);
  587. klist_iter_exit(&i);
  588. return error;
  589. }
  590. int __init devices_init(void)
  591. {
  592. return subsystem_register(&devices_subsys);
  593. }
  594. EXPORT_SYMBOL_GPL(device_for_each_child);
  595. EXPORT_SYMBOL_GPL(device_initialize);
  596. EXPORT_SYMBOL_GPL(device_add);
  597. EXPORT_SYMBOL_GPL(device_register);
  598. EXPORT_SYMBOL_GPL(device_del);
  599. EXPORT_SYMBOL_GPL(device_unregister);
  600. EXPORT_SYMBOL_GPL(get_device);
  601. EXPORT_SYMBOL_GPL(put_device);
  602. EXPORT_SYMBOL_GPL(device_create_file);
  603. EXPORT_SYMBOL_GPL(device_remove_file);
  604. static void device_create_release(struct device *dev)
  605. {
  606. pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
  607. kfree(dev);
  608. }
  609. /**
  610. * device_create - creates a device and registers it with sysfs
  611. * @class: pointer to the struct class that this device should be registered to
  612. * @parent: pointer to the parent struct device of this new device, if any
  613. * @devt: the dev_t for the char device to be added
  614. * @fmt: string for the device's name
  615. *
  616. * This function can be used by char device classes. A struct device
  617. * will be created in sysfs, registered to the specified class.
  618. *
  619. * A "dev" file will be created, showing the dev_t for the device, if
  620. * the dev_t is not 0,0.
  621. * If a pointer to a parent struct device is passed in, the newly created
  622. * struct device will be a child of that device in sysfs.
  623. * The pointer to the struct device will be returned from the call.
  624. * Any further sysfs files that might be required can be created using this
  625. * pointer.
  626. *
  627. * Note: the struct class passed to this function must have previously
  628. * been created with a call to class_create().
  629. */
  630. struct device *device_create(struct class *class, struct device *parent,
  631. dev_t devt, const char *fmt, ...)
  632. {
  633. va_list args;
  634. struct device *dev = NULL;
  635. int retval = -ENODEV;
  636. if (class == NULL || IS_ERR(class))
  637. goto error;
  638. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  639. if (!dev) {
  640. retval = -ENOMEM;
  641. goto error;
  642. }
  643. dev->devt = devt;
  644. dev->class = class;
  645. dev->parent = parent;
  646. dev->release = device_create_release;
  647. va_start(args, fmt);
  648. vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
  649. va_end(args);
  650. retval = device_register(dev);
  651. if (retval)
  652. goto error;
  653. return dev;
  654. error:
  655. kfree(dev);
  656. return ERR_PTR(retval);
  657. }
  658. EXPORT_SYMBOL_GPL(device_create);
  659. /**
  660. * device_destroy - removes a device that was created with device_create()
  661. * @class: pointer to the struct class that this device was registered with
  662. * @devt: the dev_t of the device that was previously registered
  663. *
  664. * This call unregisters and cleans up a device that was created with a
  665. * call to device_create().
  666. */
  667. void device_destroy(struct class *class, dev_t devt)
  668. {
  669. struct device *dev = NULL;
  670. struct device *dev_tmp;
  671. down(&class->sem);
  672. list_for_each_entry(dev_tmp, &class->devices, node) {
  673. if (dev_tmp->devt == devt) {
  674. dev = dev_tmp;
  675. break;
  676. }
  677. }
  678. up(&class->sem);
  679. if (dev)
  680. device_unregister(dev);
  681. }
  682. EXPORT_SYMBOL_GPL(device_destroy);
  683. /**
  684. * device_rename - renames a device
  685. * @dev: the pointer to the struct device to be renamed
  686. * @new_name: the new name of the device
  687. */
  688. int device_rename(struct device *dev, char *new_name)
  689. {
  690. char *old_class_name = NULL;
  691. char *new_class_name = NULL;
  692. char *old_symlink_name = NULL;
  693. int error;
  694. dev = get_device(dev);
  695. if (!dev)
  696. return -EINVAL;
  697. pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
  698. if ((dev->class) && (dev->parent))
  699. old_class_name = make_class_name(dev->class->name, &dev->kobj);
  700. if (dev->class) {
  701. old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  702. if (!old_symlink_name) {
  703. error = -ENOMEM;
  704. goto out_free_old_class;
  705. }
  706. strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
  707. }
  708. strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
  709. error = kobject_rename(&dev->kobj, new_name);
  710. if (old_class_name) {
  711. new_class_name = make_class_name(dev->class->name, &dev->kobj);
  712. if (new_class_name) {
  713. sysfs_create_link(&dev->parent->kobj, &dev->kobj,
  714. new_class_name);
  715. sysfs_remove_link(&dev->parent->kobj, old_class_name);
  716. }
  717. }
  718. if (dev->class) {
  719. sysfs_remove_link(&dev->class->subsys.kset.kobj,
  720. old_symlink_name);
  721. sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
  722. dev->bus_id);
  723. }
  724. put_device(dev);
  725. kfree(new_class_name);
  726. kfree(old_symlink_name);
  727. out_free_old_class:
  728. kfree(old_class_name);
  729. return error;
  730. }