mtdcore.c 9.5 KB

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