mtdcore.c 9.3 KB

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