mtdcore.c 8.7 KB

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