mtdcore.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 "internal.h"
  21. #include "mtdcore.h"
  22. static struct class *mtd_class;
  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. #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
  31. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  32. #else
  33. #define MTD_DEVT(index) 0
  34. #endif
  35. /* REVISIT once MTD uses the driver model better, whoever allocates
  36. * the mtd_info will probably want to use the release() hook...
  37. */
  38. static void mtd_release(struct device *dev)
  39. {
  40. struct mtd_info *mtd = dev_to_mtd(dev);
  41. /* remove /dev/mtdXro node if needed */
  42. if (MTD_DEVT(mtd->index))
  43. device_destroy(mtd_class, MTD_DEVT(mtd->index) + 1);
  44. }
  45. static ssize_t mtd_type_show(struct device *dev,
  46. struct device_attribute *attr, char *buf)
  47. {
  48. struct mtd_info *mtd = dev_to_mtd(dev);
  49. char *type;
  50. switch (mtd->type) {
  51. case MTD_ABSENT:
  52. type = "absent";
  53. break;
  54. case MTD_RAM:
  55. type = "ram";
  56. break;
  57. case MTD_ROM:
  58. type = "rom";
  59. break;
  60. case MTD_NORFLASH:
  61. type = "nor";
  62. break;
  63. case MTD_NANDFLASH:
  64. type = "nand";
  65. break;
  66. case MTD_DATAFLASH:
  67. type = "dataflash";
  68. break;
  69. case MTD_UBIVOLUME:
  70. type = "ubi";
  71. break;
  72. default:
  73. type = "unknown";
  74. }
  75. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  76. }
  77. static DEVICE_ATTR(mtd_type, S_IRUGO, mtd_type_show, NULL);
  78. static struct attribute *mtd_attrs[] = {
  79. &dev_attr_mtd_type.attr,
  80. /* FIXME provide a /proc/mtd superset */
  81. NULL,
  82. };
  83. struct attribute_group mtd_group = {
  84. .attrs = mtd_attrs,
  85. };
  86. struct attribute_group *mtd_groups[] = {
  87. &mtd_group,
  88. NULL,
  89. };
  90. static struct device_type mtd_devtype = {
  91. .name = "mtd",
  92. .groups = mtd_groups,
  93. .release = mtd_release,
  94. };
  95. /**
  96. * add_mtd_device - register an MTD device
  97. * @mtd: pointer to new MTD device info structure
  98. *
  99. * Add a device to the list of MTD devices present in the system, and
  100. * notify each currently active MTD 'user' of its arrival. Returns
  101. * zero on success or 1 on failure, which currently will only happen
  102. * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
  103. * or there's a sysfs error.
  104. */
  105. int add_mtd_device(struct mtd_info *mtd)
  106. {
  107. int i;
  108. if (!mtd->backing_dev_info) {
  109. switch (mtd->type) {
  110. case MTD_RAM:
  111. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  112. break;
  113. case MTD_ROM:
  114. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  115. break;
  116. default:
  117. mtd->backing_dev_info = &mtd_bdi_unmappable;
  118. break;
  119. }
  120. }
  121. BUG_ON(mtd->writesize == 0);
  122. mutex_lock(&mtd_table_mutex);
  123. for (i=0; i < MAX_MTD_DEVICES; i++)
  124. if (!mtd_table[i]) {
  125. struct mtd_notifier *not;
  126. mtd_table[i] = mtd;
  127. mtd->index = i;
  128. mtd->usecount = 0;
  129. if (is_power_of_2(mtd->erasesize))
  130. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  131. else
  132. mtd->erasesize_shift = 0;
  133. if (is_power_of_2(mtd->writesize))
  134. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  135. else
  136. mtd->writesize_shift = 0;
  137. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  138. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  139. /* Some chips always power up locked. Unlock them now */
  140. if ((mtd->flags & MTD_WRITEABLE)
  141. && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
  142. if (mtd->unlock(mtd, 0, mtd->size))
  143. printk(KERN_WARNING
  144. "%s: unlock failed, "
  145. "writes may not work\n",
  146. mtd->name);
  147. }
  148. /* Caller should have set dev.parent to match the
  149. * physical device.
  150. */
  151. mtd->dev.type = &mtd_devtype;
  152. mtd->dev.class = mtd_class;
  153. mtd->dev.devt = MTD_DEVT(i);
  154. dev_set_name(&mtd->dev, "mtd%d", i);
  155. if (device_register(&mtd->dev) != 0) {
  156. mtd_table[i] = NULL;
  157. break;
  158. }
  159. if (MTD_DEVT(i))
  160. device_create(mtd_class, mtd->dev.parent,
  161. MTD_DEVT(i) + 1,
  162. NULL, "mtd%dro", i);
  163. DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
  164. /* No need to get a refcount on the module containing
  165. the notifier, since we hold the mtd_table_mutex */
  166. list_for_each_entry(not, &mtd_notifiers, list)
  167. not->add(mtd);
  168. mutex_unlock(&mtd_table_mutex);
  169. /* We _know_ we aren't being removed, because
  170. our caller is still holding us here. So none
  171. of this try_ nonsense, and no bitching about it
  172. either. :) */
  173. __module_get(THIS_MODULE);
  174. return 0;
  175. }
  176. mutex_unlock(&mtd_table_mutex);
  177. return 1;
  178. }
  179. /**
  180. * del_mtd_device - unregister an MTD device
  181. * @mtd: pointer to MTD device info structure
  182. *
  183. * Remove a device from the list of MTD devices present in the system,
  184. * and notify each currently active MTD 'user' of its departure.
  185. * Returns zero on success or 1 on failure, which currently will happen
  186. * if the requested device does not appear to be present in the list.
  187. */
  188. int del_mtd_device (struct mtd_info *mtd)
  189. {
  190. int ret;
  191. mutex_lock(&mtd_table_mutex);
  192. if (mtd_table[mtd->index] != mtd) {
  193. ret = -ENODEV;
  194. } else if (mtd->usecount) {
  195. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  196. mtd->index, mtd->name, mtd->usecount);
  197. ret = -EBUSY;
  198. } else {
  199. struct mtd_notifier *not;
  200. /* No need to get a refcount on the module containing
  201. the notifier, since we hold the mtd_table_mutex */
  202. list_for_each_entry(not, &mtd_notifiers, list)
  203. not->remove(mtd);
  204. mtd_table[mtd->index] = NULL;
  205. module_put(THIS_MODULE);
  206. ret = 0;
  207. }
  208. mutex_unlock(&mtd_table_mutex);
  209. return ret;
  210. }
  211. /**
  212. * register_mtd_user - register a 'user' of MTD devices.
  213. * @new: pointer to notifier info structure
  214. *
  215. * Registers a pair of callbacks function to be called upon addition
  216. * or removal of MTD devices. Causes the 'add' callback to be immediately
  217. * invoked for each MTD device currently present in the system.
  218. */
  219. void register_mtd_user (struct mtd_notifier *new)
  220. {
  221. int i;
  222. mutex_lock(&mtd_table_mutex);
  223. list_add(&new->list, &mtd_notifiers);
  224. __module_get(THIS_MODULE);
  225. for (i=0; i< MAX_MTD_DEVICES; i++)
  226. if (mtd_table[i])
  227. new->add(mtd_table[i]);
  228. mutex_unlock(&mtd_table_mutex);
  229. }
  230. /**
  231. * unregister_mtd_user - unregister a 'user' of MTD devices.
  232. * @old: pointer to notifier info structure
  233. *
  234. * Removes a callback function pair from the list of 'users' to be
  235. * notified upon addition or removal of MTD devices. Causes the
  236. * 'remove' callback to be immediately invoked for each MTD device
  237. * currently present in the system.
  238. */
  239. int unregister_mtd_user (struct mtd_notifier *old)
  240. {
  241. int i;
  242. mutex_lock(&mtd_table_mutex);
  243. module_put(THIS_MODULE);
  244. for (i=0; i< MAX_MTD_DEVICES; i++)
  245. if (mtd_table[i])
  246. old->remove(mtd_table[i]);
  247. list_del(&old->list);
  248. mutex_unlock(&mtd_table_mutex);
  249. return 0;
  250. }
  251. /**
  252. * get_mtd_device - obtain a validated handle for an MTD device
  253. * @mtd: last known address of the required MTD device
  254. * @num: internal device number of the required MTD device
  255. *
  256. * Given a number and NULL address, return the num'th entry in the device
  257. * table, if any. Given an address and num == -1, search the device table
  258. * for a device with that address and return if it's still present. Given
  259. * both, return the num'th driver only if its address matches. Return
  260. * error code if not.
  261. */
  262. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  263. {
  264. struct mtd_info *ret = NULL;
  265. int i, err = -ENODEV;
  266. mutex_lock(&mtd_table_mutex);
  267. if (num == -1) {
  268. for (i=0; i< MAX_MTD_DEVICES; i++)
  269. if (mtd_table[i] == mtd)
  270. ret = mtd_table[i];
  271. } else if (num < MAX_MTD_DEVICES) {
  272. ret = mtd_table[num];
  273. if (mtd && mtd != ret)
  274. ret = NULL;
  275. }
  276. if (!ret)
  277. goto out_unlock;
  278. if (!try_module_get(ret->owner))
  279. goto out_unlock;
  280. if (ret->get_device) {
  281. err = ret->get_device(ret);
  282. if (err)
  283. goto out_put;
  284. }
  285. ret->usecount++;
  286. mutex_unlock(&mtd_table_mutex);
  287. return ret;
  288. out_put:
  289. module_put(ret->owner);
  290. out_unlock:
  291. mutex_unlock(&mtd_table_mutex);
  292. return ERR_PTR(err);
  293. }
  294. /**
  295. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  296. * device name
  297. * @name: MTD device name to open
  298. *
  299. * This function returns MTD device description structure in case of
  300. * success and an error code in case of failure.
  301. */
  302. struct mtd_info *get_mtd_device_nm(const char *name)
  303. {
  304. int i, err = -ENODEV;
  305. struct mtd_info *mtd = NULL;
  306. mutex_lock(&mtd_table_mutex);
  307. for (i = 0; i < MAX_MTD_DEVICES; i++) {
  308. if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
  309. mtd = mtd_table[i];
  310. break;
  311. }
  312. }
  313. if (!mtd)
  314. goto out_unlock;
  315. if (!try_module_get(mtd->owner))
  316. goto out_unlock;
  317. if (mtd->get_device) {
  318. err = mtd->get_device(mtd);
  319. if (err)
  320. goto out_put;
  321. }
  322. mtd->usecount++;
  323. mutex_unlock(&mtd_table_mutex);
  324. return mtd;
  325. out_put:
  326. module_put(mtd->owner);
  327. out_unlock:
  328. mutex_unlock(&mtd_table_mutex);
  329. return ERR_PTR(err);
  330. }
  331. void put_mtd_device(struct mtd_info *mtd)
  332. {
  333. int c;
  334. mutex_lock(&mtd_table_mutex);
  335. c = --mtd->usecount;
  336. if (mtd->put_device)
  337. mtd->put_device(mtd);
  338. mutex_unlock(&mtd_table_mutex);
  339. BUG_ON(c < 0);
  340. module_put(mtd->owner);
  341. }
  342. /* default_mtd_writev - default mtd writev method for MTD devices that
  343. * don't implement their own
  344. */
  345. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  346. unsigned long count, loff_t to, size_t *retlen)
  347. {
  348. unsigned long i;
  349. size_t totlen = 0, thislen;
  350. int ret = 0;
  351. if(!mtd->write) {
  352. ret = -EROFS;
  353. } else {
  354. for (i=0; i<count; i++) {
  355. if (!vecs[i].iov_len)
  356. continue;
  357. ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  358. totlen += thislen;
  359. if (ret || thislen != vecs[i].iov_len)
  360. break;
  361. to += vecs[i].iov_len;
  362. }
  363. }
  364. if (retlen)
  365. *retlen = totlen;
  366. return ret;
  367. }
  368. EXPORT_SYMBOL_GPL(add_mtd_device);
  369. EXPORT_SYMBOL_GPL(del_mtd_device);
  370. EXPORT_SYMBOL_GPL(get_mtd_device);
  371. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  372. EXPORT_SYMBOL_GPL(put_mtd_device);
  373. EXPORT_SYMBOL_GPL(register_mtd_user);
  374. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  375. EXPORT_SYMBOL_GPL(default_mtd_writev);
  376. static int __init mtd_setup(void)
  377. {
  378. mtd_class = class_create(THIS_MODULE, "mtd");
  379. if (IS_ERR(mtd_class)) {
  380. pr_err("Error creating mtd class.\n");
  381. return PTR_ERR(mtd_class);
  382. }
  383. return 0;
  384. }
  385. core_initcall(mtd_setup);
  386. static void __exit mtd_teardown(void)
  387. {
  388. class_destroy(mtd_class);
  389. }
  390. __exitcall(mtd_teardown);
  391. #ifdef CONFIG_PROC_FS
  392. /*====================================================================*/
  393. /* Support for /proc/mtd */
  394. static struct proc_dir_entry *proc_mtd;
  395. static inline int mtd_proc_info (char *buf, int i)
  396. {
  397. struct mtd_info *this = mtd_table[i];
  398. if (!this)
  399. return 0;
  400. return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
  401. (unsigned long long)this->size,
  402. this->erasesize, this->name);
  403. }
  404. static int mtd_read_proc (char *page, char **start, off_t off, int count,
  405. int *eof, void *data_unused)
  406. {
  407. int len, l, i;
  408. off_t begin = 0;
  409. mutex_lock(&mtd_table_mutex);
  410. len = sprintf(page, "dev: size erasesize name\n");
  411. for (i=0; i< MAX_MTD_DEVICES; i++) {
  412. l = mtd_proc_info(page + len, i);
  413. len += l;
  414. if (len+begin > off+count)
  415. goto done;
  416. if (len+begin < off) {
  417. begin += len;
  418. len = 0;
  419. }
  420. }
  421. *eof = 1;
  422. done:
  423. mutex_unlock(&mtd_table_mutex);
  424. if (off >= len+begin)
  425. return 0;
  426. *start = page + (off-begin);
  427. return ((count < begin+len-off) ? count : begin+len-off);
  428. }
  429. /*====================================================================*/
  430. /* Init code */
  431. static int __init init_mtd(void)
  432. {
  433. if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
  434. proc_mtd->read_proc = mtd_read_proc;
  435. return 0;
  436. }
  437. static void __exit cleanup_mtd(void)
  438. {
  439. if (proc_mtd)
  440. remove_proc_entry( "mtd", NULL);
  441. }
  442. module_init(init_mtd);
  443. module_exit(cleanup_mtd);
  444. #endif /* CONFIG_PROC_FS */
  445. MODULE_LICENSE("GPL");
  446. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  447. MODULE_DESCRIPTION("Core MTD registration and access routines");