mtdcore.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * $Id: mtdcore.c,v 1.45 2005/02/18 14:34:50 dedekind 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. #ifdef CONFIG_PROC_FS
  22. #include <linux/proc_fs.h>
  23. #endif
  24. #include <linux/mtd/mtd.h>
  25. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  26. should not use them for _anything_ else */
  27. DECLARE_MUTEX(mtd_table_mutex);
  28. struct mtd_info *mtd_table[MAX_MTD_DEVICES];
  29. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  30. EXPORT_SYMBOL_GPL(mtd_table);
  31. static LIST_HEAD(mtd_notifiers);
  32. /**
  33. * add_mtd_device - register an MTD device
  34. * @mtd: pointer to new MTD device info structure
  35. *
  36. * Add a device to the list of MTD devices present in the system, and
  37. * notify each currently active MTD 'user' of its arrival. Returns
  38. * zero on success or 1 on failure, which currently will only happen
  39. * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
  40. */
  41. int add_mtd_device(struct mtd_info *mtd)
  42. {
  43. int i;
  44. down(&mtd_table_mutex);
  45. for (i=0; i < MAX_MTD_DEVICES; i++)
  46. if (!mtd_table[i]) {
  47. struct list_head *this;
  48. mtd_table[i] = mtd;
  49. mtd->index = i;
  50. mtd->usecount = 0;
  51. DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
  52. /* No need to get a refcount on the module containing
  53. the notifier, since we hold the mtd_table_mutex */
  54. list_for_each(this, &mtd_notifiers) {
  55. struct mtd_notifier *not = list_entry(this, struct mtd_notifier, list);
  56. not->add(mtd);
  57. }
  58. up(&mtd_table_mutex);
  59. /* We _know_ we aren't being removed, because
  60. our caller is still holding us here. So none
  61. of this try_ nonsense, and no bitching about it
  62. either. :) */
  63. __module_get(THIS_MODULE);
  64. return 0;
  65. }
  66. up(&mtd_table_mutex);
  67. return 1;
  68. }
  69. /**
  70. * del_mtd_device - unregister an MTD device
  71. * @mtd: pointer to MTD device info structure
  72. *
  73. * Remove a device from the list of MTD devices present in the system,
  74. * and notify each currently active MTD 'user' of its departure.
  75. * Returns zero on success or 1 on failure, which currently will happen
  76. * if the requested device does not appear to be present in the list.
  77. */
  78. int del_mtd_device (struct mtd_info *mtd)
  79. {
  80. int ret;
  81. down(&mtd_table_mutex);
  82. if (mtd_table[mtd->index] != mtd) {
  83. ret = -ENODEV;
  84. } else if (mtd->usecount) {
  85. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  86. mtd->index, mtd->name, mtd->usecount);
  87. ret = -EBUSY;
  88. } else {
  89. struct list_head *this;
  90. /* No need to get a refcount on the module containing
  91. the notifier, since we hold the mtd_table_mutex */
  92. list_for_each(this, &mtd_notifiers) {
  93. struct mtd_notifier *not = list_entry(this, struct mtd_notifier, list);
  94. not->remove(mtd);
  95. }
  96. mtd_table[mtd->index] = NULL;
  97. module_put(THIS_MODULE);
  98. ret = 0;
  99. }
  100. up(&mtd_table_mutex);
  101. return ret;
  102. }
  103. /**
  104. * register_mtd_user - register a 'user' of MTD devices.
  105. * @new: pointer to notifier info structure
  106. *
  107. * Registers a pair of callbacks function to be called upon addition
  108. * or removal of MTD devices. Causes the 'add' callback to be immediately
  109. * invoked for each MTD device currently present in the system.
  110. */
  111. void register_mtd_user (struct mtd_notifier *new)
  112. {
  113. int i;
  114. down(&mtd_table_mutex);
  115. list_add(&new->list, &mtd_notifiers);
  116. __module_get(THIS_MODULE);
  117. for (i=0; i< MAX_MTD_DEVICES; i++)
  118. if (mtd_table[i])
  119. new->add(mtd_table[i]);
  120. up(&mtd_table_mutex);
  121. }
  122. /**
  123. * unregister_mtd_user - unregister a 'user' of MTD devices.
  124. * @old: pointer to notifier info structure
  125. *
  126. * Removes a callback function pair from the list of 'users' to be
  127. * notified upon addition or removal of MTD devices. Causes the
  128. * 'remove' callback to be immediately invoked for each MTD device
  129. * currently present in the system.
  130. */
  131. int unregister_mtd_user (struct mtd_notifier *old)
  132. {
  133. int i;
  134. down(&mtd_table_mutex);
  135. module_put(THIS_MODULE);
  136. for (i=0; i< MAX_MTD_DEVICES; i++)
  137. if (mtd_table[i])
  138. old->remove(mtd_table[i]);
  139. list_del(&old->list);
  140. up(&mtd_table_mutex);
  141. return 0;
  142. }
  143. /**
  144. * get_mtd_device - obtain a validated handle for an MTD device
  145. * @mtd: last known address of the required MTD device
  146. * @num: internal device number of the required MTD device
  147. *
  148. * Given a number and NULL address, return the num'th entry in the device
  149. * table, if any. Given an address and num == -1, search the device table
  150. * for a device with that address and return if it's still present. Given
  151. * both, return the num'th driver only if its address matches. Return NULL
  152. * if not.
  153. */
  154. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  155. {
  156. struct mtd_info *ret = NULL;
  157. int i;
  158. down(&mtd_table_mutex);
  159. if (num == -1) {
  160. for (i=0; i< MAX_MTD_DEVICES; i++)
  161. if (mtd_table[i] == mtd)
  162. ret = mtd_table[i];
  163. } else if (num < MAX_MTD_DEVICES) {
  164. ret = mtd_table[num];
  165. if (mtd && mtd != ret)
  166. ret = NULL;
  167. }
  168. if (ret && !try_module_get(ret->owner))
  169. ret = NULL;
  170. if (ret)
  171. ret->usecount++;
  172. up(&mtd_table_mutex);
  173. return ret;
  174. }
  175. void put_mtd_device(struct mtd_info *mtd)
  176. {
  177. int c;
  178. down(&mtd_table_mutex);
  179. c = --mtd->usecount;
  180. up(&mtd_table_mutex);
  181. BUG_ON(c < 0);
  182. module_put(mtd->owner);
  183. }
  184. /* default_mtd_writev - default mtd writev method for MTD devices that
  185. * dont implement their own
  186. */
  187. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  188. unsigned long count, loff_t to, size_t *retlen)
  189. {
  190. unsigned long i;
  191. size_t totlen = 0, thislen;
  192. int ret = 0;
  193. if(!mtd->write) {
  194. ret = -EROFS;
  195. } else {
  196. for (i=0; i<count; i++) {
  197. if (!vecs[i].iov_len)
  198. continue;
  199. ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  200. totlen += thislen;
  201. if (ret || thislen != vecs[i].iov_len)
  202. break;
  203. to += vecs[i].iov_len;
  204. }
  205. }
  206. if (retlen)
  207. *retlen = totlen;
  208. return ret;
  209. }
  210. /* default_mtd_readv - default mtd readv method for MTD devices that dont
  211. * implement their own
  212. */
  213. int default_mtd_readv(struct mtd_info *mtd, struct kvec *vecs,
  214. unsigned long count, loff_t from, size_t *retlen)
  215. {
  216. unsigned long i;
  217. size_t totlen = 0, thislen;
  218. int ret = 0;
  219. if(!mtd->read) {
  220. ret = -EIO;
  221. } else {
  222. for (i=0; i<count; i++) {
  223. if (!vecs[i].iov_len)
  224. continue;
  225. ret = mtd->read(mtd, from, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  226. totlen += thislen;
  227. if (ret || thislen != vecs[i].iov_len)
  228. break;
  229. from += vecs[i].iov_len;
  230. }
  231. }
  232. if (retlen)
  233. *retlen = totlen;
  234. return ret;
  235. }
  236. EXPORT_SYMBOL(add_mtd_device);
  237. EXPORT_SYMBOL(del_mtd_device);
  238. EXPORT_SYMBOL(get_mtd_device);
  239. EXPORT_SYMBOL(put_mtd_device);
  240. EXPORT_SYMBOL(register_mtd_user);
  241. EXPORT_SYMBOL(unregister_mtd_user);
  242. EXPORT_SYMBOL(default_mtd_writev);
  243. EXPORT_SYMBOL(default_mtd_readv);
  244. /*====================================================================*/
  245. /* Power management code */
  246. #ifdef CONFIG_PM
  247. #include <linux/pm.h>
  248. static struct pm_dev *mtd_pm_dev = NULL;
  249. static int mtd_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
  250. {
  251. int ret = 0, i;
  252. if (down_trylock(&mtd_table_mutex))
  253. return -EAGAIN;
  254. if (rqst == PM_SUSPEND) {
  255. for (i = 0; ret == 0 && i < MAX_MTD_DEVICES; i++) {
  256. if (mtd_table[i] && mtd_table[i]->suspend)
  257. ret = mtd_table[i]->suspend(mtd_table[i]);
  258. }
  259. } else i = MAX_MTD_DEVICES-1;
  260. if (rqst == PM_RESUME || ret) {
  261. for ( ; i >= 0; i--) {
  262. if (mtd_table[i] && mtd_table[i]->resume)
  263. mtd_table[i]->resume(mtd_table[i]);
  264. }
  265. }
  266. up(&mtd_table_mutex);
  267. return ret;
  268. }
  269. #endif
  270. /*====================================================================*/
  271. /* Support for /proc/mtd */
  272. #ifdef CONFIG_PROC_FS
  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. down(&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. up(&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. #endif /* CONFIG_PROC_FS */
  308. /*====================================================================*/
  309. /* Init code */
  310. static int __init init_mtd(void)
  311. {
  312. #ifdef CONFIG_PROC_FS
  313. if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
  314. proc_mtd->read_proc = mtd_read_proc;
  315. #endif
  316. #ifdef CONFIG_PM
  317. mtd_pm_dev = pm_register(PM_UNKNOWN_DEV, 0, mtd_pm_callback);
  318. #endif
  319. return 0;
  320. }
  321. static void __exit cleanup_mtd(void)
  322. {
  323. #ifdef CONFIG_PM
  324. if (mtd_pm_dev) {
  325. pm_unregister(mtd_pm_dev);
  326. mtd_pm_dev = NULL;
  327. }
  328. #endif
  329. #ifdef CONFIG_PROC_FS
  330. if (proc_mtd)
  331. remove_proc_entry( "mtd", NULL);
  332. #endif
  333. }
  334. module_init(init_mtd);
  335. module_exit(cleanup_mtd);
  336. MODULE_LICENSE("GPL");
  337. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  338. MODULE_DESCRIPTION("Core MTD registration and access routines");