mtdcore.c 8.3 KB

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