mtdcore.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * $Id: mtdcore.c,v 1.47 2005/11/07 11:14:20 gleixner Exp $
  3. *
  4. * Core registration and callback routines for MTD
  5. * drivers and users.
  6. *
  7. */
  8. #include <linux/config.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <linux/timer.h>
  16. #include <linux/major.h>
  17. #include <linux/fs.h>
  18. #include <linux/ioctl.h>
  19. #include <linux/init.h>
  20. #include <linux/mtd/compatmac.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/mtd/mtd.h>
  23. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  24. should not use them for _anything_ else */
  25. DEFINE_MUTEX(mtd_table_mutex);
  26. struct mtd_info *mtd_table[MAX_MTD_DEVICES];
  27. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  28. EXPORT_SYMBOL_GPL(mtd_table);
  29. static LIST_HEAD(mtd_notifiers);
  30. /**
  31. * add_mtd_device - register an MTD device
  32. * @mtd: pointer to new MTD device info structure
  33. *
  34. * Add a device to the list of MTD devices present in the system, and
  35. * notify each currently active MTD 'user' of its arrival. Returns
  36. * zero on success or 1 on failure, which currently will only happen
  37. * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
  38. */
  39. int add_mtd_device(struct mtd_info *mtd)
  40. {
  41. int i;
  42. BUG_ON(mtd->writesize == 0);
  43. mutex_lock(&mtd_table_mutex);
  44. for (i=0; i < MAX_MTD_DEVICES; i++)
  45. if (!mtd_table[i]) {
  46. struct list_head *this;
  47. mtd_table[i] = mtd;
  48. mtd->index = i;
  49. mtd->usecount = 0;
  50. DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
  51. /* No need to get a refcount on the module containing
  52. the notifier, since we hold the mtd_table_mutex */
  53. list_for_each(this, &mtd_notifiers) {
  54. struct mtd_notifier *not = list_entry(this, struct mtd_notifier, list);
  55. not->add(mtd);
  56. }
  57. mutex_unlock(&mtd_table_mutex);
  58. /* We _know_ we aren't being removed, because
  59. our caller is still holding us here. So none
  60. of this try_ nonsense, and no bitching about it
  61. either. :) */
  62. __module_get(THIS_MODULE);
  63. return 0;
  64. }
  65. mutex_unlock(&mtd_table_mutex);
  66. return 1;
  67. }
  68. /**
  69. * del_mtd_device - unregister an MTD device
  70. * @mtd: pointer to MTD device info structure
  71. *
  72. * Remove a device from the list of MTD devices present in the system,
  73. * and notify each currently active MTD 'user' of its departure.
  74. * Returns zero on success or 1 on failure, which currently will happen
  75. * if the requested device does not appear to be present in the list.
  76. */
  77. int del_mtd_device (struct mtd_info *mtd)
  78. {
  79. int ret;
  80. mutex_lock(&mtd_table_mutex);
  81. if (mtd_table[mtd->index] != mtd) {
  82. ret = -ENODEV;
  83. } else if (mtd->usecount) {
  84. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  85. mtd->index, mtd->name, mtd->usecount);
  86. ret = -EBUSY;
  87. } else {
  88. struct list_head *this;
  89. /* No need to get a refcount on the module containing
  90. the notifier, since we hold the mtd_table_mutex */
  91. list_for_each(this, &mtd_notifiers) {
  92. struct mtd_notifier *not = list_entry(this, struct mtd_notifier, list);
  93. not->remove(mtd);
  94. }
  95. mtd_table[mtd->index] = NULL;
  96. module_put(THIS_MODULE);
  97. ret = 0;
  98. }
  99. mutex_unlock(&mtd_table_mutex);
  100. return ret;
  101. }
  102. /**
  103. * register_mtd_user - register a 'user' of MTD devices.
  104. * @new: pointer to notifier info structure
  105. *
  106. * Registers a pair of callbacks function to be called upon addition
  107. * or removal of MTD devices. Causes the 'add' callback to be immediately
  108. * invoked for each MTD device currently present in the system.
  109. */
  110. void register_mtd_user (struct mtd_notifier *new)
  111. {
  112. int i;
  113. mutex_lock(&mtd_table_mutex);
  114. list_add(&new->list, &mtd_notifiers);
  115. __module_get(THIS_MODULE);
  116. for (i=0; i< MAX_MTD_DEVICES; i++)
  117. if (mtd_table[i])
  118. new->add(mtd_table[i]);
  119. mutex_unlock(&mtd_table_mutex);
  120. }
  121. /**
  122. * unregister_mtd_user - unregister a 'user' of MTD devices.
  123. * @old: pointer to notifier info structure
  124. *
  125. * Removes a callback function pair from the list of 'users' to be
  126. * notified upon addition or removal of MTD devices. Causes the
  127. * 'remove' callback to be immediately invoked for each MTD device
  128. * currently present in the system.
  129. */
  130. int unregister_mtd_user (struct mtd_notifier *old)
  131. {
  132. int i;
  133. mutex_lock(&mtd_table_mutex);
  134. module_put(THIS_MODULE);
  135. for (i=0; i< MAX_MTD_DEVICES; i++)
  136. if (mtd_table[i])
  137. old->remove(mtd_table[i]);
  138. list_del(&old->list);
  139. mutex_unlock(&mtd_table_mutex);
  140. return 0;
  141. }
  142. /**
  143. * get_mtd_device - obtain a validated handle for an MTD device
  144. * @mtd: last known address of the required MTD device
  145. * @num: internal device number of the required MTD device
  146. *
  147. * Given a number and NULL address, return the num'th entry in the device
  148. * table, if any. Given an address and num == -1, search the device table
  149. * for a device with that address and return if it's still present. Given
  150. * both, return the num'th driver only if its address matches. Return NULL
  151. * if not.
  152. */
  153. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  154. {
  155. struct mtd_info *ret = NULL;
  156. int i;
  157. mutex_lock(&mtd_table_mutex);
  158. if (num == -1) {
  159. for (i=0; i< MAX_MTD_DEVICES; i++)
  160. if (mtd_table[i] == mtd)
  161. ret = mtd_table[i];
  162. } else if (num < MAX_MTD_DEVICES) {
  163. ret = mtd_table[num];
  164. if (mtd && mtd != ret)
  165. ret = NULL;
  166. }
  167. if (ret && !try_module_get(ret->owner))
  168. ret = NULL;
  169. if (ret)
  170. ret->usecount++;
  171. mutex_unlock(&mtd_table_mutex);
  172. return ret;
  173. }
  174. void put_mtd_device(struct mtd_info *mtd)
  175. {
  176. int c;
  177. mutex_lock(&mtd_table_mutex);
  178. c = --mtd->usecount;
  179. mutex_unlock(&mtd_table_mutex);
  180. BUG_ON(c < 0);
  181. module_put(mtd->owner);
  182. }
  183. /* default_mtd_writev - default mtd writev method for MTD devices that
  184. * dont implement their own
  185. */
  186. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  187. unsigned long count, loff_t to, size_t *retlen)
  188. {
  189. unsigned long i;
  190. size_t totlen = 0, thislen;
  191. int ret = 0;
  192. if(!mtd->write) {
  193. ret = -EROFS;
  194. } else {
  195. for (i=0; i<count; i++) {
  196. if (!vecs[i].iov_len)
  197. continue;
  198. ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  199. totlen += thislen;
  200. if (ret || thislen != vecs[i].iov_len)
  201. break;
  202. to += vecs[i].iov_len;
  203. }
  204. }
  205. if (retlen)
  206. *retlen = totlen;
  207. return ret;
  208. }
  209. EXPORT_SYMBOL(add_mtd_device);
  210. EXPORT_SYMBOL(del_mtd_device);
  211. EXPORT_SYMBOL(get_mtd_device);
  212. EXPORT_SYMBOL(put_mtd_device);
  213. EXPORT_SYMBOL(register_mtd_user);
  214. EXPORT_SYMBOL(unregister_mtd_user);
  215. EXPORT_SYMBOL(default_mtd_writev);
  216. #ifdef CONFIG_PROC_FS
  217. /*====================================================================*/
  218. /* Support for /proc/mtd */
  219. static struct proc_dir_entry *proc_mtd;
  220. static inline int mtd_proc_info (char *buf, int i)
  221. {
  222. struct mtd_info *this = mtd_table[i];
  223. if (!this)
  224. return 0;
  225. return sprintf(buf, "mtd%d: %8.8x %8.8x \"%s\"\n", i, this->size,
  226. this->erasesize, this->name);
  227. }
  228. static int mtd_read_proc (char *page, char **start, off_t off, int count,
  229. int *eof, void *data_unused)
  230. {
  231. int len, l, i;
  232. off_t begin = 0;
  233. mutex_lock(&mtd_table_mutex);
  234. len = sprintf(page, "dev: size erasesize name\n");
  235. for (i=0; i< MAX_MTD_DEVICES; i++) {
  236. l = mtd_proc_info(page + len, i);
  237. len += l;
  238. if (len+begin > off+count)
  239. goto done;
  240. if (len+begin < off) {
  241. begin += len;
  242. len = 0;
  243. }
  244. }
  245. *eof = 1;
  246. done:
  247. mutex_unlock(&mtd_table_mutex);
  248. if (off >= len+begin)
  249. return 0;
  250. *start = page + (off-begin);
  251. return ((count < begin+len-off) ? count : begin+len-off);
  252. }
  253. /*====================================================================*/
  254. /* Init code */
  255. static int __init init_mtd(void)
  256. {
  257. if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
  258. proc_mtd->read_proc = mtd_read_proc;
  259. return 0;
  260. }
  261. static void __exit cleanup_mtd(void)
  262. {
  263. if (proc_mtd)
  264. remove_proc_entry( "mtd", NULL);
  265. }
  266. module_init(init_mtd);
  267. module_exit(cleanup_mtd);
  268. #endif /* CONFIG_PROC_FS */
  269. MODULE_LICENSE("GPL");
  270. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  271. MODULE_DESCRIPTION("Core MTD registration and access routines");