mtdcore.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Core registration and callback routines for MTD
  3. * drivers and users.
  4. *
  5. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  6. * Copyright © 2006 Red Hat UK Limited
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/string.h>
  27. #include <linux/timer.h>
  28. #include <linux/major.h>
  29. #include <linux/fs.h>
  30. #include <linux/err.h>
  31. #include <linux/ioctl.h>
  32. #include <linux/init.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/idr.h>
  35. #include <linux/backing-dev.h>
  36. #include <linux/gfp.h>
  37. #include <linux/mtd/mtd.h>
  38. #include "mtdcore.h"
  39. /*
  40. * backing device capabilities for non-mappable devices (such as NAND flash)
  41. * - permits private mappings, copies are taken of the data
  42. */
  43. struct backing_dev_info mtd_bdi_unmappable = {
  44. .capabilities = BDI_CAP_MAP_COPY,
  45. };
  46. /*
  47. * backing device capabilities for R/O mappable devices (such as ROM)
  48. * - permits private mappings, copies are taken of the data
  49. * - permits non-writable shared mappings
  50. */
  51. struct backing_dev_info mtd_bdi_ro_mappable = {
  52. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  53. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
  54. };
  55. /*
  56. * backing device capabilities for writable mappable devices (such as RAM)
  57. * - permits private mappings, copies are taken of the data
  58. * - permits non-writable shared mappings
  59. */
  60. struct backing_dev_info mtd_bdi_rw_mappable = {
  61. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  62. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
  63. BDI_CAP_WRITE_MAP),
  64. };
  65. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  66. static int mtd_cls_resume(struct device *dev);
  67. static struct class mtd_class = {
  68. .name = "mtd",
  69. .owner = THIS_MODULE,
  70. .suspend = mtd_cls_suspend,
  71. .resume = mtd_cls_resume,
  72. };
  73. static DEFINE_IDR(mtd_idr);
  74. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  75. should not use them for _anything_ else */
  76. DEFINE_MUTEX(mtd_table_mutex);
  77. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  78. struct mtd_info *__mtd_next_device(int i)
  79. {
  80. return idr_get_next(&mtd_idr, &i);
  81. }
  82. EXPORT_SYMBOL_GPL(__mtd_next_device);
  83. static LIST_HEAD(mtd_notifiers);
  84. #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
  85. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  86. #else
  87. #define MTD_DEVT(index) 0
  88. #endif
  89. /* REVISIT once MTD uses the driver model better, whoever allocates
  90. * the mtd_info will probably want to use the release() hook...
  91. */
  92. static void mtd_release(struct device *dev)
  93. {
  94. dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
  95. /* remove /dev/mtdXro node if needed */
  96. if (index)
  97. device_destroy(&mtd_class, index + 1);
  98. }
  99. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  100. {
  101. struct mtd_info *mtd = dev_to_mtd(dev);
  102. if (mtd && mtd->suspend)
  103. return mtd->suspend(mtd);
  104. else
  105. return 0;
  106. }
  107. static int mtd_cls_resume(struct device *dev)
  108. {
  109. struct mtd_info *mtd = dev_to_mtd(dev);
  110. if (mtd && mtd->resume)
  111. mtd->resume(mtd);
  112. return 0;
  113. }
  114. static ssize_t mtd_type_show(struct device *dev,
  115. struct device_attribute *attr, char *buf)
  116. {
  117. struct mtd_info *mtd = dev_to_mtd(dev);
  118. char *type;
  119. switch (mtd->type) {
  120. case MTD_ABSENT:
  121. type = "absent";
  122. break;
  123. case MTD_RAM:
  124. type = "ram";
  125. break;
  126. case MTD_ROM:
  127. type = "rom";
  128. break;
  129. case MTD_NORFLASH:
  130. type = "nor";
  131. break;
  132. case MTD_NANDFLASH:
  133. type = "nand";
  134. break;
  135. case MTD_DATAFLASH:
  136. type = "dataflash";
  137. break;
  138. case MTD_UBIVOLUME:
  139. type = "ubi";
  140. break;
  141. default:
  142. type = "unknown";
  143. }
  144. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  145. }
  146. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  147. static ssize_t mtd_flags_show(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct mtd_info *mtd = dev_to_mtd(dev);
  151. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  152. }
  153. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  154. static ssize_t mtd_size_show(struct device *dev,
  155. struct device_attribute *attr, char *buf)
  156. {
  157. struct mtd_info *mtd = dev_to_mtd(dev);
  158. return snprintf(buf, PAGE_SIZE, "%llu\n",
  159. (unsigned long long)mtd->size);
  160. }
  161. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  162. static ssize_t mtd_erasesize_show(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. struct mtd_info *mtd = dev_to_mtd(dev);
  166. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  167. }
  168. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  169. static ssize_t mtd_writesize_show(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. struct mtd_info *mtd = dev_to_mtd(dev);
  173. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  174. }
  175. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  176. static ssize_t mtd_subpagesize_show(struct device *dev,
  177. struct device_attribute *attr, char *buf)
  178. {
  179. struct mtd_info *mtd = dev_to_mtd(dev);
  180. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  181. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  182. }
  183. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  184. static ssize_t mtd_oobsize_show(struct device *dev,
  185. struct device_attribute *attr, char *buf)
  186. {
  187. struct mtd_info *mtd = dev_to_mtd(dev);
  188. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  189. }
  190. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  191. static ssize_t mtd_numeraseregions_show(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct mtd_info *mtd = dev_to_mtd(dev);
  195. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  196. }
  197. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  198. NULL);
  199. static ssize_t mtd_name_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct mtd_info *mtd = dev_to_mtd(dev);
  203. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  204. }
  205. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  206. static struct attribute *mtd_attrs[] = {
  207. &dev_attr_type.attr,
  208. &dev_attr_flags.attr,
  209. &dev_attr_size.attr,
  210. &dev_attr_erasesize.attr,
  211. &dev_attr_writesize.attr,
  212. &dev_attr_subpagesize.attr,
  213. &dev_attr_oobsize.attr,
  214. &dev_attr_numeraseregions.attr,
  215. &dev_attr_name.attr,
  216. NULL,
  217. };
  218. static struct attribute_group mtd_group = {
  219. .attrs = mtd_attrs,
  220. };
  221. static const struct attribute_group *mtd_groups[] = {
  222. &mtd_group,
  223. NULL,
  224. };
  225. static struct device_type mtd_devtype = {
  226. .name = "mtd",
  227. .groups = mtd_groups,
  228. .release = mtd_release,
  229. };
  230. /**
  231. * add_mtd_device - register an MTD device
  232. * @mtd: pointer to new MTD device info structure
  233. *
  234. * Add a device to the list of MTD devices present in the system, and
  235. * notify each currently active MTD 'user' of its arrival. Returns
  236. * zero on success or 1 on failure, which currently will only happen
  237. * if there is insufficient memory or a sysfs error.
  238. */
  239. int add_mtd_device(struct mtd_info *mtd)
  240. {
  241. struct mtd_notifier *not;
  242. int i, error;
  243. if (!mtd->backing_dev_info) {
  244. switch (mtd->type) {
  245. case MTD_RAM:
  246. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  247. break;
  248. case MTD_ROM:
  249. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  250. break;
  251. default:
  252. mtd->backing_dev_info = &mtd_bdi_unmappable;
  253. break;
  254. }
  255. }
  256. BUG_ON(mtd->writesize == 0);
  257. mutex_lock(&mtd_table_mutex);
  258. do {
  259. if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
  260. goto fail_locked;
  261. error = idr_get_new(&mtd_idr, mtd, &i);
  262. } while (error == -EAGAIN);
  263. if (error)
  264. goto fail_locked;
  265. mtd->index = i;
  266. mtd->usecount = 0;
  267. if (is_power_of_2(mtd->erasesize))
  268. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  269. else
  270. mtd->erasesize_shift = 0;
  271. if (is_power_of_2(mtd->writesize))
  272. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  273. else
  274. mtd->writesize_shift = 0;
  275. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  276. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  277. /* Some chips always power up locked. Unlock them now */
  278. if ((mtd->flags & MTD_WRITEABLE)
  279. && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
  280. if (mtd->unlock(mtd, 0, mtd->size))
  281. printk(KERN_WARNING
  282. "%s: unlock failed, writes may not work\n",
  283. mtd->name);
  284. }
  285. /* Caller should have set dev.parent to match the
  286. * physical device.
  287. */
  288. mtd->dev.type = &mtd_devtype;
  289. mtd->dev.class = &mtd_class;
  290. mtd->dev.devt = MTD_DEVT(i);
  291. dev_set_name(&mtd->dev, "mtd%d", i);
  292. dev_set_drvdata(&mtd->dev, mtd);
  293. if (device_register(&mtd->dev) != 0)
  294. goto fail_added;
  295. if (MTD_DEVT(i))
  296. device_create(&mtd_class, mtd->dev.parent,
  297. MTD_DEVT(i) + 1,
  298. NULL, "mtd%dro", i);
  299. DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
  300. /* No need to get a refcount on the module containing
  301. the notifier, since we hold the mtd_table_mutex */
  302. list_for_each_entry(not, &mtd_notifiers, list)
  303. not->add(mtd);
  304. mutex_unlock(&mtd_table_mutex);
  305. /* We _know_ we aren't being removed, because
  306. our caller is still holding us here. So none
  307. of this try_ nonsense, and no bitching about it
  308. either. :) */
  309. __module_get(THIS_MODULE);
  310. return 0;
  311. fail_added:
  312. idr_remove(&mtd_idr, i);
  313. fail_locked:
  314. mutex_unlock(&mtd_table_mutex);
  315. return 1;
  316. }
  317. /**
  318. * del_mtd_device - unregister an MTD device
  319. * @mtd: pointer to MTD device info structure
  320. *
  321. * Remove a device from the list of MTD devices present in the system,
  322. * and notify each currently active MTD 'user' of its departure.
  323. * Returns zero on success or 1 on failure, which currently will happen
  324. * if the requested device does not appear to be present in the list.
  325. */
  326. int del_mtd_device (struct mtd_info *mtd)
  327. {
  328. int ret;
  329. struct mtd_notifier *not;
  330. mutex_lock(&mtd_table_mutex);
  331. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  332. ret = -ENODEV;
  333. goto out_error;
  334. }
  335. /* No need to get a refcount on the module containing
  336. the notifier, since we hold the mtd_table_mutex */
  337. list_for_each_entry(not, &mtd_notifiers, list)
  338. not->remove(mtd);
  339. if (mtd->usecount) {
  340. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  341. mtd->index, mtd->name, mtd->usecount);
  342. ret = -EBUSY;
  343. } else {
  344. device_unregister(&mtd->dev);
  345. idr_remove(&mtd_idr, mtd->index);
  346. module_put(THIS_MODULE);
  347. ret = 0;
  348. }
  349. out_error:
  350. mutex_unlock(&mtd_table_mutex);
  351. return ret;
  352. }
  353. /**
  354. * register_mtd_user - register a 'user' of MTD devices.
  355. * @new: pointer to notifier info structure
  356. *
  357. * Registers a pair of callbacks function to be called upon addition
  358. * or removal of MTD devices. Causes the 'add' callback to be immediately
  359. * invoked for each MTD device currently present in the system.
  360. */
  361. void register_mtd_user (struct mtd_notifier *new)
  362. {
  363. struct mtd_info *mtd;
  364. mutex_lock(&mtd_table_mutex);
  365. list_add(&new->list, &mtd_notifiers);
  366. __module_get(THIS_MODULE);
  367. mtd_for_each_device(mtd)
  368. new->add(mtd);
  369. mutex_unlock(&mtd_table_mutex);
  370. }
  371. /**
  372. * unregister_mtd_user - unregister a 'user' of MTD devices.
  373. * @old: pointer to notifier info structure
  374. *
  375. * Removes a callback function pair from the list of 'users' to be
  376. * notified upon addition or removal of MTD devices. Causes the
  377. * 'remove' callback to be immediately invoked for each MTD device
  378. * currently present in the system.
  379. */
  380. int unregister_mtd_user (struct mtd_notifier *old)
  381. {
  382. struct mtd_info *mtd;
  383. mutex_lock(&mtd_table_mutex);
  384. module_put(THIS_MODULE);
  385. mtd_for_each_device(mtd)
  386. old->remove(mtd);
  387. list_del(&old->list);
  388. mutex_unlock(&mtd_table_mutex);
  389. return 0;
  390. }
  391. /**
  392. * get_mtd_device - obtain a validated handle for an MTD device
  393. * @mtd: last known address of the required MTD device
  394. * @num: internal device number of the required MTD device
  395. *
  396. * Given a number and NULL address, return the num'th entry in the device
  397. * table, if any. Given an address and num == -1, search the device table
  398. * for a device with that address and return if it's still present. Given
  399. * both, return the num'th driver only if its address matches. Return
  400. * error code if not.
  401. */
  402. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  403. {
  404. struct mtd_info *ret = NULL, *other;
  405. int err = -ENODEV;
  406. mutex_lock(&mtd_table_mutex);
  407. if (num == -1) {
  408. mtd_for_each_device(other) {
  409. if (other == mtd) {
  410. ret = mtd;
  411. break;
  412. }
  413. }
  414. } else if (num >= 0) {
  415. ret = idr_find(&mtd_idr, num);
  416. if (mtd && mtd != ret)
  417. ret = NULL;
  418. }
  419. if (!ret) {
  420. ret = ERR_PTR(err);
  421. goto out;
  422. }
  423. err = __get_mtd_device(ret);
  424. if (err)
  425. ret = ERR_PTR(err);
  426. out:
  427. mutex_unlock(&mtd_table_mutex);
  428. return ret;
  429. }
  430. int __get_mtd_device(struct mtd_info *mtd)
  431. {
  432. int err;
  433. if (!try_module_get(mtd->owner))
  434. return -ENODEV;
  435. if (mtd->get_device) {
  436. err = mtd->get_device(mtd);
  437. if (err) {
  438. module_put(mtd->owner);
  439. return err;
  440. }
  441. }
  442. mtd->usecount++;
  443. return 0;
  444. }
  445. /**
  446. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  447. * device name
  448. * @name: MTD device name to open
  449. *
  450. * This function returns MTD device description structure in case of
  451. * success and an error code in case of failure.
  452. */
  453. struct mtd_info *get_mtd_device_nm(const char *name)
  454. {
  455. int err = -ENODEV;
  456. struct mtd_info *mtd = NULL, *other;
  457. mutex_lock(&mtd_table_mutex);
  458. mtd_for_each_device(other) {
  459. if (!strcmp(name, other->name)) {
  460. mtd = other;
  461. break;
  462. }
  463. }
  464. if (!mtd)
  465. goto out_unlock;
  466. if (!try_module_get(mtd->owner))
  467. goto out_unlock;
  468. if (mtd->get_device) {
  469. err = mtd->get_device(mtd);
  470. if (err)
  471. goto out_put;
  472. }
  473. mtd->usecount++;
  474. mutex_unlock(&mtd_table_mutex);
  475. return mtd;
  476. out_put:
  477. module_put(mtd->owner);
  478. out_unlock:
  479. mutex_unlock(&mtd_table_mutex);
  480. return ERR_PTR(err);
  481. }
  482. void put_mtd_device(struct mtd_info *mtd)
  483. {
  484. mutex_lock(&mtd_table_mutex);
  485. __put_mtd_device(mtd);
  486. mutex_unlock(&mtd_table_mutex);
  487. }
  488. void __put_mtd_device(struct mtd_info *mtd)
  489. {
  490. --mtd->usecount;
  491. BUG_ON(mtd->usecount < 0);
  492. if (mtd->put_device)
  493. mtd->put_device(mtd);
  494. module_put(mtd->owner);
  495. }
  496. /* default_mtd_writev - default mtd writev method for MTD devices that
  497. * don't implement their own
  498. */
  499. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  500. unsigned long count, loff_t to, size_t *retlen)
  501. {
  502. unsigned long i;
  503. size_t totlen = 0, thislen;
  504. int ret = 0;
  505. if(!mtd->write) {
  506. ret = -EROFS;
  507. } else {
  508. for (i=0; i<count; i++) {
  509. if (!vecs[i].iov_len)
  510. continue;
  511. ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  512. totlen += thislen;
  513. if (ret || thislen != vecs[i].iov_len)
  514. break;
  515. to += vecs[i].iov_len;
  516. }
  517. }
  518. if (retlen)
  519. *retlen = totlen;
  520. return ret;
  521. }
  522. EXPORT_SYMBOL_GPL(add_mtd_device);
  523. EXPORT_SYMBOL_GPL(del_mtd_device);
  524. EXPORT_SYMBOL_GPL(get_mtd_device);
  525. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  526. EXPORT_SYMBOL_GPL(__get_mtd_device);
  527. EXPORT_SYMBOL_GPL(put_mtd_device);
  528. EXPORT_SYMBOL_GPL(__put_mtd_device);
  529. EXPORT_SYMBOL_GPL(register_mtd_user);
  530. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  531. EXPORT_SYMBOL_GPL(default_mtd_writev);
  532. #ifdef CONFIG_PROC_FS
  533. /*====================================================================*/
  534. /* Support for /proc/mtd */
  535. static struct proc_dir_entry *proc_mtd;
  536. static inline int mtd_proc_info(char *buf, struct mtd_info *this)
  537. {
  538. return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
  539. (unsigned long long)this->size,
  540. this->erasesize, this->name);
  541. }
  542. static int mtd_read_proc (char *page, char **start, off_t off, int count,
  543. int *eof, void *data_unused)
  544. {
  545. struct mtd_info *mtd;
  546. int len, l;
  547. off_t begin = 0;
  548. mutex_lock(&mtd_table_mutex);
  549. len = sprintf(page, "dev: size erasesize name\n");
  550. mtd_for_each_device(mtd) {
  551. l = mtd_proc_info(page + len, mtd);
  552. len += l;
  553. if (len+begin > off+count)
  554. goto done;
  555. if (len+begin < off) {
  556. begin += len;
  557. len = 0;
  558. }
  559. }
  560. *eof = 1;
  561. done:
  562. mutex_unlock(&mtd_table_mutex);
  563. if (off >= len+begin)
  564. return 0;
  565. *start = page + (off-begin);
  566. return ((count < begin+len-off) ? count : begin+len-off);
  567. }
  568. #endif /* CONFIG_PROC_FS */
  569. /*====================================================================*/
  570. /* Init code */
  571. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  572. {
  573. int ret;
  574. ret = bdi_init(bdi);
  575. if (!ret)
  576. ret = bdi_register(bdi, NULL, name);
  577. if (ret)
  578. bdi_destroy(bdi);
  579. return ret;
  580. }
  581. static int __init init_mtd(void)
  582. {
  583. int ret;
  584. ret = class_register(&mtd_class);
  585. if (ret)
  586. goto err_reg;
  587. ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
  588. if (ret)
  589. goto err_bdi1;
  590. ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
  591. if (ret)
  592. goto err_bdi2;
  593. ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
  594. if (ret)
  595. goto err_bdi3;
  596. #ifdef CONFIG_PROC_FS
  597. if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
  598. proc_mtd->read_proc = mtd_read_proc;
  599. #endif /* CONFIG_PROC_FS */
  600. return 0;
  601. err_bdi3:
  602. bdi_destroy(&mtd_bdi_ro_mappable);
  603. err_bdi2:
  604. bdi_destroy(&mtd_bdi_unmappable);
  605. err_bdi1:
  606. class_unregister(&mtd_class);
  607. err_reg:
  608. pr_err("Error registering mtd class or bdi: %d\n", ret);
  609. return ret;
  610. }
  611. static void __exit cleanup_mtd(void)
  612. {
  613. #ifdef CONFIG_PROC_FS
  614. if (proc_mtd)
  615. remove_proc_entry( "mtd", NULL);
  616. #endif /* CONFIG_PROC_FS */
  617. class_unregister(&mtd_class);
  618. bdi_destroy(&mtd_bdi_unmappable);
  619. bdi_destroy(&mtd_bdi_ro_mappable);
  620. bdi_destroy(&mtd_bdi_rw_mappable);
  621. }
  622. module_init(init_mtd);
  623. module_exit(cleanup_mtd);
  624. MODULE_LICENSE("GPL");
  625. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  626. MODULE_DESCRIPTION("Core MTD registration and access routines");