mtdcore.c 9.3 KB

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