mtdcore.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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/sched.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <linux/timer.h>
  15. #include <linux/major.h>
  16. #include <linux/fs.h>
  17. #include <linux/err.h>
  18. #include <linux/ioctl.h>
  19. #include <linux/init.h>
  20. #include <linux/mtd/compatmac.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/mtd/mtd.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 NULL
  160. * 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;
  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. ret = NULL;
  180. goto out_unlock;
  181. }
  182. if (ret->get_device && ret->get_device(ret)) {
  183. module_put(ret->owner);
  184. ret = NULL;
  185. goto out_unlock;
  186. }
  187. ret->usecount++;
  188. out_unlock:
  189. mutex_unlock(&mtd_table_mutex);
  190. return ret;
  191. }
  192. /**
  193. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  194. * device name
  195. * @name: MTD device name to open
  196. *
  197. * This function returns MTD device description structure in case of
  198. * success and an error code in case of failure.
  199. */
  200. struct mtd_info *get_mtd_device_nm(const char *name)
  201. {
  202. int i, err = -ENODEV;
  203. struct mtd_info *mtd = NULL;
  204. mutex_lock(&mtd_table_mutex);
  205. for (i = 0; i < MAX_MTD_DEVICES; i++) {
  206. if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
  207. mtd = mtd_table[i];
  208. break;
  209. }
  210. }
  211. if (!mtd)
  212. goto out_unlock;
  213. if (!try_module_get(mtd->owner))
  214. goto out_unlock;
  215. if (mtd->get_device) {
  216. err = mtd->get_device(mtd);
  217. if (err)
  218. goto out_put;
  219. }
  220. mtd->usecount++;
  221. mutex_unlock(&mtd_table_mutex);
  222. return mtd;
  223. out_put:
  224. module_put(mtd->owner);
  225. out_unlock:
  226. mutex_unlock(&mtd_table_mutex);
  227. return ERR_PTR(err);
  228. }
  229. void put_mtd_device(struct mtd_info *mtd)
  230. {
  231. int c;
  232. mutex_lock(&mtd_table_mutex);
  233. c = --mtd->usecount;
  234. if (mtd->put_device)
  235. mtd->put_device(mtd);
  236. mutex_unlock(&mtd_table_mutex);
  237. BUG_ON(c < 0);
  238. module_put(mtd->owner);
  239. }
  240. /* default_mtd_writev - default mtd writev method for MTD devices that
  241. * dont implement their own
  242. */
  243. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  244. unsigned long count, loff_t to, size_t *retlen)
  245. {
  246. unsigned long i;
  247. size_t totlen = 0, thislen;
  248. int ret = 0;
  249. if(!mtd->write) {
  250. ret = -EROFS;
  251. } else {
  252. for (i=0; i<count; i++) {
  253. if (!vecs[i].iov_len)
  254. continue;
  255. ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  256. totlen += thislen;
  257. if (ret || thislen != vecs[i].iov_len)
  258. break;
  259. to += vecs[i].iov_len;
  260. }
  261. }
  262. if (retlen)
  263. *retlen = totlen;
  264. return ret;
  265. }
  266. EXPORT_SYMBOL(add_mtd_device);
  267. EXPORT_SYMBOL(del_mtd_device);
  268. EXPORT_SYMBOL(get_mtd_device);
  269. EXPORT_SYMBOL(get_mtd_device_nm);
  270. EXPORT_SYMBOL(put_mtd_device);
  271. EXPORT_SYMBOL(register_mtd_user);
  272. EXPORT_SYMBOL(unregister_mtd_user);
  273. EXPORT_SYMBOL(default_mtd_writev);
  274. #ifdef CONFIG_PROC_FS
  275. /*====================================================================*/
  276. /* Support for /proc/mtd */
  277. static struct proc_dir_entry *proc_mtd;
  278. static inline int mtd_proc_info (char *buf, int i)
  279. {
  280. struct mtd_info *this = mtd_table[i];
  281. if (!this)
  282. return 0;
  283. return sprintf(buf, "mtd%d: %8.8x %8.8x \"%s\"\n", i, this->size,
  284. this->erasesize, this->name);
  285. }
  286. static int mtd_read_proc (char *page, char **start, off_t off, int count,
  287. int *eof, void *data_unused)
  288. {
  289. int len, l, i;
  290. off_t begin = 0;
  291. mutex_lock(&mtd_table_mutex);
  292. len = sprintf(page, "dev: size erasesize name\n");
  293. for (i=0; i< MAX_MTD_DEVICES; i++) {
  294. l = mtd_proc_info(page + len, i);
  295. len += l;
  296. if (len+begin > off+count)
  297. goto done;
  298. if (len+begin < off) {
  299. begin += len;
  300. len = 0;
  301. }
  302. }
  303. *eof = 1;
  304. done:
  305. mutex_unlock(&mtd_table_mutex);
  306. if (off >= len+begin)
  307. return 0;
  308. *start = page + (off-begin);
  309. return ((count < begin+len-off) ? count : begin+len-off);
  310. }
  311. /*====================================================================*/
  312. /* Init code */
  313. static int __init init_mtd(void)
  314. {
  315. if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
  316. proc_mtd->read_proc = mtd_read_proc;
  317. return 0;
  318. }
  319. static void __exit cleanup_mtd(void)
  320. {
  321. if (proc_mtd)
  322. remove_proc_entry( "mtd", NULL);
  323. }
  324. module_init(init_mtd);
  325. module_exit(cleanup_mtd);
  326. #endif /* CONFIG_PROC_FS */
  327. MODULE_LICENSE("GPL");
  328. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  329. MODULE_DESCRIPTION("Core MTD registration and access routines");