mtdcore.c 9.7 KB

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