mtdcore.c 3.5 KB

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