core.c 20 KB

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