mtdcore.c 9.5 KB

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