mtdcore.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Core registration and callback routines for MTD
  3. * drivers and users.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/mtd/mtd.h>
  10. #include <linux/mtd/compat.h>
  11. #include <ubi_uboot.h>
  12. struct mtd_info *mtd_table[MAX_MTD_DEVICES];
  13. int add_mtd_device(struct mtd_info *mtd)
  14. {
  15. int i;
  16. BUG_ON(mtd->writesize == 0);
  17. for (i = 0; i < MAX_MTD_DEVICES; i++)
  18. if (!mtd_table[i]) {
  19. mtd_table[i] = mtd;
  20. mtd->index = i;
  21. mtd->usecount = 0;
  22. /* No need to get a refcount on the module containing
  23. the notifier, since we hold the mtd_table_mutex */
  24. /* We _know_ we aren't being removed, because
  25. our caller is still holding us here. So none
  26. of this try_ nonsense, and no bitching about it
  27. either. :) */
  28. return 0;
  29. }
  30. return 1;
  31. }
  32. /**
  33. * del_mtd_device - unregister an MTD device
  34. * @mtd: pointer to MTD device info structure
  35. *
  36. * Remove a device from the list of MTD devices present in the system,
  37. * and notify each currently active MTD 'user' of its departure.
  38. * Returns zero on success or 1 on failure, which currently will happen
  39. * if the requested device does not appear to be present in the list.
  40. */
  41. int del_mtd_device(struct mtd_info *mtd)
  42. {
  43. int ret;
  44. if (mtd_table[mtd->index] != mtd) {
  45. ret = -ENODEV;
  46. } else if (mtd->usecount) {
  47. printk(KERN_NOTICE "Removing MTD device #%d (%s)"
  48. " with use count %d\n",
  49. mtd->index, mtd->name, mtd->usecount);
  50. ret = -EBUSY;
  51. } else {
  52. /* No need to get a refcount on the module containing
  53. * the notifier, since we hold the mtd_table_mutex */
  54. mtd_table[mtd->index] = NULL;
  55. ret = 0;
  56. }
  57. return ret;
  58. }
  59. /**
  60. * get_mtd_device - obtain a validated handle for an MTD device
  61. * @mtd: last known address of the required MTD device
  62. * @num: internal device number of the required MTD device
  63. *
  64. * Given a number and NULL address, return the num'th entry in the device
  65. * table, if any. Given an address and num == -1, search the device table
  66. * for a device with that address and return if it's still present. Given
  67. * both, return the num'th driver only if its address matches. Return
  68. * error code if not.
  69. */
  70. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  71. {
  72. struct mtd_info *ret = NULL;
  73. int i, err = -ENODEV;
  74. if (num == -1) {
  75. for (i = 0; i < MAX_MTD_DEVICES; i++)
  76. if (mtd_table[i] == mtd)
  77. ret = mtd_table[i];
  78. } else if (num < MAX_MTD_DEVICES) {
  79. ret = mtd_table[num];
  80. if (mtd && mtd != ret)
  81. ret = NULL;
  82. }
  83. if (!ret)
  84. goto out_unlock;
  85. ret->usecount++;
  86. return ret;
  87. out_unlock:
  88. return ERR_PTR(err);
  89. }
  90. /**
  91. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  92. * device name
  93. * @name: MTD device name to open
  94. *
  95. * This function returns MTD device description structure in case of
  96. * success and an error code in case of failure.
  97. */
  98. struct mtd_info *get_mtd_device_nm(const char *name)
  99. {
  100. int i, err = -ENODEV;
  101. struct mtd_info *mtd = NULL;
  102. for (i = 0; i < MAX_MTD_DEVICES; i++) {
  103. if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
  104. mtd = mtd_table[i];
  105. break;
  106. }
  107. }
  108. if (!mtd)
  109. goto out_unlock;
  110. mtd->usecount++;
  111. return mtd;
  112. out_unlock:
  113. return ERR_PTR(err);
  114. }
  115. void put_mtd_device(struct mtd_info *mtd)
  116. {
  117. int c;
  118. c = --mtd->usecount;
  119. BUG_ON(c < 0);
  120. }