core.c 16 KB

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