mtdcore.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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/seq_file.h>
  27. #include <linux/string.h>
  28. #include <linux/timer.h>
  29. #include <linux/major.h>
  30. #include <linux/fs.h>
  31. #include <linux/err.h>
  32. #include <linux/ioctl.h>
  33. #include <linux/init.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/idr.h>
  36. #include <linux/backing-dev.h>
  37. #include <linux/gfp.h>
  38. #include <linux/slab.h>
  39. #include <linux/mtd/mtd.h>
  40. #include <linux/mtd/partitions.h>
  41. #include "mtdcore.h"
  42. static struct backing_dev_info mtd_bdi = {
  43. };
  44. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  45. static int mtd_cls_resume(struct device *dev);
  46. static struct class mtd_class = {
  47. .name = "mtd",
  48. .owner = THIS_MODULE,
  49. .suspend = mtd_cls_suspend,
  50. .resume = mtd_cls_resume,
  51. };
  52. static DEFINE_IDR(mtd_idr);
  53. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  54. should not use them for _anything_ else */
  55. DEFINE_MUTEX(mtd_table_mutex);
  56. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  57. struct mtd_info *__mtd_next_device(int i)
  58. {
  59. return idr_get_next(&mtd_idr, &i);
  60. }
  61. EXPORT_SYMBOL_GPL(__mtd_next_device);
  62. static LIST_HEAD(mtd_notifiers);
  63. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  64. /* REVISIT once MTD uses the driver model better, whoever allocates
  65. * the mtd_info will probably want to use the release() hook...
  66. */
  67. static void mtd_release(struct device *dev)
  68. {
  69. struct mtd_info *mtd = dev_get_drvdata(dev);
  70. dev_t index = MTD_DEVT(mtd->index);
  71. /* remove /dev/mtdXro node */
  72. device_destroy(&mtd_class, index + 1);
  73. }
  74. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  75. {
  76. struct mtd_info *mtd = dev_get_drvdata(dev);
  77. return mtd ? mtd_suspend(mtd) : 0;
  78. }
  79. static int mtd_cls_resume(struct device *dev)
  80. {
  81. struct mtd_info *mtd = dev_get_drvdata(dev);
  82. if (mtd)
  83. mtd_resume(mtd);
  84. return 0;
  85. }
  86. static ssize_t mtd_type_show(struct device *dev,
  87. struct device_attribute *attr, char *buf)
  88. {
  89. struct mtd_info *mtd = dev_get_drvdata(dev);
  90. char *type;
  91. switch (mtd->type) {
  92. case MTD_ABSENT:
  93. type = "absent";
  94. break;
  95. case MTD_RAM:
  96. type = "ram";
  97. break;
  98. case MTD_ROM:
  99. type = "rom";
  100. break;
  101. case MTD_NORFLASH:
  102. type = "nor";
  103. break;
  104. case MTD_NANDFLASH:
  105. type = "nand";
  106. break;
  107. case MTD_DATAFLASH:
  108. type = "dataflash";
  109. break;
  110. case MTD_UBIVOLUME:
  111. type = "ubi";
  112. break;
  113. case MTD_MLCNANDFLASH:
  114. type = "mlc-nand";
  115. break;
  116. default:
  117. type = "unknown";
  118. }
  119. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  120. }
  121. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  122. static ssize_t mtd_flags_show(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. struct mtd_info *mtd = dev_get_drvdata(dev);
  126. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  127. }
  128. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  129. static ssize_t mtd_size_show(struct device *dev,
  130. struct device_attribute *attr, char *buf)
  131. {
  132. struct mtd_info *mtd = dev_get_drvdata(dev);
  133. return snprintf(buf, PAGE_SIZE, "%llu\n",
  134. (unsigned long long)mtd->size);
  135. }
  136. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  137. static ssize_t mtd_erasesize_show(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. struct mtd_info *mtd = dev_get_drvdata(dev);
  141. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  142. }
  143. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  144. static ssize_t mtd_writesize_show(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct mtd_info *mtd = dev_get_drvdata(dev);
  148. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  149. }
  150. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  151. static ssize_t mtd_subpagesize_show(struct device *dev,
  152. struct device_attribute *attr, char *buf)
  153. {
  154. struct mtd_info *mtd = dev_get_drvdata(dev);
  155. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  156. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  157. }
  158. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  159. static ssize_t mtd_oobsize_show(struct device *dev,
  160. struct device_attribute *attr, char *buf)
  161. {
  162. struct mtd_info *mtd = dev_get_drvdata(dev);
  163. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  164. }
  165. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  166. static ssize_t mtd_numeraseregions_show(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. struct mtd_info *mtd = dev_get_drvdata(dev);
  170. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  171. }
  172. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  173. NULL);
  174. static ssize_t mtd_name_show(struct device *dev,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. struct mtd_info *mtd = dev_get_drvdata(dev);
  178. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  179. }
  180. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  181. static ssize_t mtd_ecc_strength_show(struct device *dev,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. struct mtd_info *mtd = dev_get_drvdata(dev);
  185. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
  186. }
  187. static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
  188. static ssize_t mtd_bitflip_threshold_show(struct device *dev,
  189. struct device_attribute *attr,
  190. char *buf)
  191. {
  192. struct mtd_info *mtd = dev_get_drvdata(dev);
  193. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
  194. }
  195. static ssize_t mtd_bitflip_threshold_store(struct device *dev,
  196. struct device_attribute *attr,
  197. const char *buf, size_t count)
  198. {
  199. struct mtd_info *mtd = dev_get_drvdata(dev);
  200. unsigned int bitflip_threshold;
  201. int retval;
  202. retval = kstrtouint(buf, 0, &bitflip_threshold);
  203. if (retval)
  204. return retval;
  205. mtd->bitflip_threshold = bitflip_threshold;
  206. return count;
  207. }
  208. static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
  209. mtd_bitflip_threshold_show,
  210. mtd_bitflip_threshold_store);
  211. static ssize_t mtd_ecc_step_size_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct mtd_info *mtd = dev_get_drvdata(dev);
  215. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
  216. }
  217. static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
  218. static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
  219. struct device_attribute *attr, char *buf)
  220. {
  221. struct mtd_info *mtd = dev_get_drvdata(dev);
  222. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  223. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected);
  224. }
  225. static DEVICE_ATTR(corrected_bits, S_IRUGO,
  226. mtd_ecc_stats_corrected_show, NULL);
  227. static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. struct mtd_info *mtd = dev_get_drvdata(dev);
  231. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  232. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed);
  233. }
  234. static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
  235. static ssize_t mtd_badblocks_show(struct device *dev,
  236. struct device_attribute *attr, char *buf)
  237. {
  238. struct mtd_info *mtd = dev_get_drvdata(dev);
  239. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  240. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks);
  241. }
  242. static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
  243. static ssize_t mtd_bbtblocks_show(struct device *dev,
  244. struct device_attribute *attr, char *buf)
  245. {
  246. struct mtd_info *mtd = dev_get_drvdata(dev);
  247. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  248. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks);
  249. }
  250. static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
  251. static struct attribute *mtd_attrs[] = {
  252. &dev_attr_type.attr,
  253. &dev_attr_flags.attr,
  254. &dev_attr_size.attr,
  255. &dev_attr_erasesize.attr,
  256. &dev_attr_writesize.attr,
  257. &dev_attr_subpagesize.attr,
  258. &dev_attr_oobsize.attr,
  259. &dev_attr_numeraseregions.attr,
  260. &dev_attr_name.attr,
  261. &dev_attr_ecc_strength.attr,
  262. &dev_attr_ecc_step_size.attr,
  263. &dev_attr_corrected_bits.attr,
  264. &dev_attr_ecc_failures.attr,
  265. &dev_attr_bad_blocks.attr,
  266. &dev_attr_bbt_blocks.attr,
  267. &dev_attr_bitflip_threshold.attr,
  268. NULL,
  269. };
  270. ATTRIBUTE_GROUPS(mtd);
  271. static struct device_type mtd_devtype = {
  272. .name = "mtd",
  273. .groups = mtd_groups,
  274. .release = mtd_release,
  275. };
  276. #ifndef CONFIG_MMU
  277. unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
  278. {
  279. switch (mtd->type) {
  280. case MTD_RAM:
  281. return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
  282. NOMMU_MAP_READ | NOMMU_MAP_WRITE;
  283. case MTD_ROM:
  284. return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
  285. NOMMU_MAP_READ;
  286. default:
  287. return NOMMU_MAP_COPY;
  288. }
  289. }
  290. EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
  291. #endif
  292. /**
  293. * add_mtd_device - register an MTD device
  294. * @mtd: pointer to new MTD device info structure
  295. *
  296. * Add a device to the list of MTD devices present in the system, and
  297. * notify each currently active MTD 'user' of its arrival. Returns
  298. * zero on success or 1 on failure, which currently will only happen
  299. * if there is insufficient memory or a sysfs error.
  300. */
  301. int add_mtd_device(struct mtd_info *mtd)
  302. {
  303. struct mtd_notifier *not;
  304. int i, error;
  305. mtd->backing_dev_info = &mtd_bdi;
  306. BUG_ON(mtd->writesize == 0);
  307. mutex_lock(&mtd_table_mutex);
  308. i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
  309. if (i < 0)
  310. goto fail_locked;
  311. mtd->index = i;
  312. mtd->usecount = 0;
  313. /* default value if not set by driver */
  314. if (mtd->bitflip_threshold == 0)
  315. mtd->bitflip_threshold = mtd->ecc_strength;
  316. if (is_power_of_2(mtd->erasesize))
  317. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  318. else
  319. mtd->erasesize_shift = 0;
  320. if (is_power_of_2(mtd->writesize))
  321. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  322. else
  323. mtd->writesize_shift = 0;
  324. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  325. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  326. /* Some chips always power up locked. Unlock them now */
  327. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  328. error = mtd_unlock(mtd, 0, mtd->size);
  329. if (error && error != -EOPNOTSUPP)
  330. printk(KERN_WARNING
  331. "%s: unlock failed, writes may not work\n",
  332. mtd->name);
  333. }
  334. /* Caller should have set dev.parent to match the
  335. * physical device.
  336. */
  337. mtd->dev.type = &mtd_devtype;
  338. mtd->dev.class = &mtd_class;
  339. mtd->dev.devt = MTD_DEVT(i);
  340. dev_set_name(&mtd->dev, "mtd%d", i);
  341. dev_set_drvdata(&mtd->dev, mtd);
  342. if (device_register(&mtd->dev) != 0)
  343. goto fail_added;
  344. device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
  345. "mtd%dro", i);
  346. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  347. /* No need to get a refcount on the module containing
  348. the notifier, since we hold the mtd_table_mutex */
  349. list_for_each_entry(not, &mtd_notifiers, list)
  350. not->add(mtd);
  351. mutex_unlock(&mtd_table_mutex);
  352. /* We _know_ we aren't being removed, because
  353. our caller is still holding us here. So none
  354. of this try_ nonsense, and no bitching about it
  355. either. :) */
  356. __module_get(THIS_MODULE);
  357. return 0;
  358. fail_added:
  359. idr_remove(&mtd_idr, i);
  360. fail_locked:
  361. mutex_unlock(&mtd_table_mutex);
  362. return 1;
  363. }
  364. /**
  365. * del_mtd_device - unregister an MTD device
  366. * @mtd: pointer to MTD device info structure
  367. *
  368. * Remove a device from the list of MTD devices present in the system,
  369. * and notify each currently active MTD 'user' of its departure.
  370. * Returns zero on success or 1 on failure, which currently will happen
  371. * if the requested device does not appear to be present in the list.
  372. */
  373. int del_mtd_device(struct mtd_info *mtd)
  374. {
  375. int ret;
  376. struct mtd_notifier *not;
  377. mutex_lock(&mtd_table_mutex);
  378. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  379. ret = -ENODEV;
  380. goto out_error;
  381. }
  382. /* No need to get a refcount on the module containing
  383. the notifier, since we hold the mtd_table_mutex */
  384. list_for_each_entry(not, &mtd_notifiers, list)
  385. not->remove(mtd);
  386. if (mtd->usecount) {
  387. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  388. mtd->index, mtd->name, mtd->usecount);
  389. ret = -EBUSY;
  390. } else {
  391. device_unregister(&mtd->dev);
  392. idr_remove(&mtd_idr, mtd->index);
  393. module_put(THIS_MODULE);
  394. ret = 0;
  395. }
  396. out_error:
  397. mutex_unlock(&mtd_table_mutex);
  398. return ret;
  399. }
  400. /**
  401. * mtd_device_parse_register - parse partitions and register an MTD device.
  402. *
  403. * @mtd: the MTD device to register
  404. * @types: the list of MTD partition probes to try, see
  405. * 'parse_mtd_partitions()' for more information
  406. * @parser_data: MTD partition parser-specific data
  407. * @parts: fallback partition information to register, if parsing fails;
  408. * only valid if %nr_parts > %0
  409. * @nr_parts: the number of partitions in parts, if zero then the full
  410. * MTD device is registered if no partition info is found
  411. *
  412. * This function aggregates MTD partitions parsing (done by
  413. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  414. * basically follows the most common pattern found in many MTD drivers:
  415. *
  416. * * It first tries to probe partitions on MTD device @mtd using parsers
  417. * specified in @types (if @types is %NULL, then the default list of parsers
  418. * is used, see 'parse_mtd_partitions()' for more information). If none are
  419. * found this functions tries to fallback to information specified in
  420. * @parts/@nr_parts.
  421. * * If any partitioning info was found, this function registers the found
  422. * partitions.
  423. * * If no partitions were found this function just registers the MTD device
  424. * @mtd and exits.
  425. *
  426. * Returns zero in case of success and a negative error code in case of failure.
  427. */
  428. int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
  429. struct mtd_part_parser_data *parser_data,
  430. const struct mtd_partition *parts,
  431. int nr_parts)
  432. {
  433. int err;
  434. struct mtd_partition *real_parts;
  435. err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
  436. if (err <= 0 && nr_parts && parts) {
  437. real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
  438. GFP_KERNEL);
  439. if (!real_parts)
  440. err = -ENOMEM;
  441. else
  442. err = nr_parts;
  443. }
  444. if (err > 0) {
  445. err = add_mtd_partitions(mtd, real_parts, err);
  446. kfree(real_parts);
  447. } else if (err == 0) {
  448. err = add_mtd_device(mtd);
  449. if (err == 1)
  450. err = -ENODEV;
  451. }
  452. return err;
  453. }
  454. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  455. /**
  456. * mtd_device_unregister - unregister an existing MTD device.
  457. *
  458. * @master: the MTD device to unregister. This will unregister both the master
  459. * and any partitions if registered.
  460. */
  461. int mtd_device_unregister(struct mtd_info *master)
  462. {
  463. int err;
  464. err = del_mtd_partitions(master);
  465. if (err)
  466. return err;
  467. if (!device_is_registered(&master->dev))
  468. return 0;
  469. return del_mtd_device(master);
  470. }
  471. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  472. /**
  473. * register_mtd_user - register a 'user' of MTD devices.
  474. * @new: pointer to notifier info structure
  475. *
  476. * Registers a pair of callbacks function to be called upon addition
  477. * or removal of MTD devices. Causes the 'add' callback to be immediately
  478. * invoked for each MTD device currently present in the system.
  479. */
  480. void register_mtd_user (struct mtd_notifier *new)
  481. {
  482. struct mtd_info *mtd;
  483. mutex_lock(&mtd_table_mutex);
  484. list_add(&new->list, &mtd_notifiers);
  485. __module_get(THIS_MODULE);
  486. mtd_for_each_device(mtd)
  487. new->add(mtd);
  488. mutex_unlock(&mtd_table_mutex);
  489. }
  490. EXPORT_SYMBOL_GPL(register_mtd_user);
  491. /**
  492. * unregister_mtd_user - unregister a 'user' of MTD devices.
  493. * @old: pointer to notifier info structure
  494. *
  495. * Removes a callback function pair from the list of 'users' to be
  496. * notified upon addition or removal of MTD devices. Causes the
  497. * 'remove' callback to be immediately invoked for each MTD device
  498. * currently present in the system.
  499. */
  500. int unregister_mtd_user (struct mtd_notifier *old)
  501. {
  502. struct mtd_info *mtd;
  503. mutex_lock(&mtd_table_mutex);
  504. module_put(THIS_MODULE);
  505. mtd_for_each_device(mtd)
  506. old->remove(mtd);
  507. list_del(&old->list);
  508. mutex_unlock(&mtd_table_mutex);
  509. return 0;
  510. }
  511. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  512. /**
  513. * get_mtd_device - obtain a validated handle for an MTD device
  514. * @mtd: last known address of the required MTD device
  515. * @num: internal device number of the required MTD device
  516. *
  517. * Given a number and NULL address, return the num'th entry in the device
  518. * table, if any. Given an address and num == -1, search the device table
  519. * for a device with that address and return if it's still present. Given
  520. * both, return the num'th driver only if its address matches. Return
  521. * error code if not.
  522. */
  523. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  524. {
  525. struct mtd_info *ret = NULL, *other;
  526. int err = -ENODEV;
  527. mutex_lock(&mtd_table_mutex);
  528. if (num == -1) {
  529. mtd_for_each_device(other) {
  530. if (other == mtd) {
  531. ret = mtd;
  532. break;
  533. }
  534. }
  535. } else if (num >= 0) {
  536. ret = idr_find(&mtd_idr, num);
  537. if (mtd && mtd != ret)
  538. ret = NULL;
  539. }
  540. if (!ret) {
  541. ret = ERR_PTR(err);
  542. goto out;
  543. }
  544. err = __get_mtd_device(ret);
  545. if (err)
  546. ret = ERR_PTR(err);
  547. out:
  548. mutex_unlock(&mtd_table_mutex);
  549. return ret;
  550. }
  551. EXPORT_SYMBOL_GPL(get_mtd_device);
  552. int __get_mtd_device(struct mtd_info *mtd)
  553. {
  554. int err;
  555. if (!try_module_get(mtd->owner))
  556. return -ENODEV;
  557. if (mtd->_get_device) {
  558. err = mtd->_get_device(mtd);
  559. if (err) {
  560. module_put(mtd->owner);
  561. return err;
  562. }
  563. }
  564. mtd->usecount++;
  565. return 0;
  566. }
  567. EXPORT_SYMBOL_GPL(__get_mtd_device);
  568. /**
  569. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  570. * device name
  571. * @name: MTD device name to open
  572. *
  573. * This function returns MTD device description structure in case of
  574. * success and an error code in case of failure.
  575. */
  576. struct mtd_info *get_mtd_device_nm(const char *name)
  577. {
  578. int err = -ENODEV;
  579. struct mtd_info *mtd = NULL, *other;
  580. mutex_lock(&mtd_table_mutex);
  581. mtd_for_each_device(other) {
  582. if (!strcmp(name, other->name)) {
  583. mtd = other;
  584. break;
  585. }
  586. }
  587. if (!mtd)
  588. goto out_unlock;
  589. err = __get_mtd_device(mtd);
  590. if (err)
  591. goto out_unlock;
  592. mutex_unlock(&mtd_table_mutex);
  593. return mtd;
  594. out_unlock:
  595. mutex_unlock(&mtd_table_mutex);
  596. return ERR_PTR(err);
  597. }
  598. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  599. void put_mtd_device(struct mtd_info *mtd)
  600. {
  601. mutex_lock(&mtd_table_mutex);
  602. __put_mtd_device(mtd);
  603. mutex_unlock(&mtd_table_mutex);
  604. }
  605. EXPORT_SYMBOL_GPL(put_mtd_device);
  606. void __put_mtd_device(struct mtd_info *mtd)
  607. {
  608. --mtd->usecount;
  609. BUG_ON(mtd->usecount < 0);
  610. if (mtd->_put_device)
  611. mtd->_put_device(mtd);
  612. module_put(mtd->owner);
  613. }
  614. EXPORT_SYMBOL_GPL(__put_mtd_device);
  615. /*
  616. * Erase is an asynchronous operation. Device drivers are supposed
  617. * to call instr->callback() whenever the operation completes, even
  618. * if it completes with a failure.
  619. * Callers are supposed to pass a callback function and wait for it
  620. * to be called before writing to the block.
  621. */
  622. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  623. {
  624. if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
  625. return -EINVAL;
  626. if (!(mtd->flags & MTD_WRITEABLE))
  627. return -EROFS;
  628. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  629. if (!instr->len) {
  630. instr->state = MTD_ERASE_DONE;
  631. mtd_erase_callback(instr);
  632. return 0;
  633. }
  634. return mtd->_erase(mtd, instr);
  635. }
  636. EXPORT_SYMBOL_GPL(mtd_erase);
  637. /*
  638. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  639. */
  640. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  641. void **virt, resource_size_t *phys)
  642. {
  643. *retlen = 0;
  644. *virt = NULL;
  645. if (phys)
  646. *phys = 0;
  647. if (!mtd->_point)
  648. return -EOPNOTSUPP;
  649. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  650. return -EINVAL;
  651. if (!len)
  652. return 0;
  653. return mtd->_point(mtd, from, len, retlen, virt, phys);
  654. }
  655. EXPORT_SYMBOL_GPL(mtd_point);
  656. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  657. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  658. {
  659. if (!mtd->_point)
  660. return -EOPNOTSUPP;
  661. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  662. return -EINVAL;
  663. if (!len)
  664. return 0;
  665. return mtd->_unpoint(mtd, from, len);
  666. }
  667. EXPORT_SYMBOL_GPL(mtd_unpoint);
  668. /*
  669. * Allow NOMMU mmap() to directly map the device (if not NULL)
  670. * - return the address to which the offset maps
  671. * - return -ENOSYS to indicate refusal to do the mapping
  672. */
  673. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  674. unsigned long offset, unsigned long flags)
  675. {
  676. if (!mtd->_get_unmapped_area)
  677. return -EOPNOTSUPP;
  678. if (offset >= mtd->size || len > mtd->size - offset)
  679. return -EINVAL;
  680. return mtd->_get_unmapped_area(mtd, len, offset, flags);
  681. }
  682. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  683. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  684. u_char *buf)
  685. {
  686. int ret_code;
  687. *retlen = 0;
  688. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  689. return -EINVAL;
  690. if (!len)
  691. return 0;
  692. /*
  693. * In the absence of an error, drivers return a non-negative integer
  694. * representing the maximum number of bitflips that were corrected on
  695. * any one ecc region (if applicable; zero otherwise).
  696. */
  697. ret_code = mtd->_read(mtd, from, len, retlen, buf);
  698. if (unlikely(ret_code < 0))
  699. return ret_code;
  700. if (mtd->ecc_strength == 0)
  701. return 0; /* device lacks ecc */
  702. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  703. }
  704. EXPORT_SYMBOL_GPL(mtd_read);
  705. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  706. const u_char *buf)
  707. {
  708. *retlen = 0;
  709. if (to < 0 || to >= mtd->size || len > mtd->size - to)
  710. return -EINVAL;
  711. if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
  712. return -EROFS;
  713. if (!len)
  714. return 0;
  715. return mtd->_write(mtd, to, len, retlen, buf);
  716. }
  717. EXPORT_SYMBOL_GPL(mtd_write);
  718. /*
  719. * In blackbox flight recorder like scenarios we want to make successful writes
  720. * in interrupt context. panic_write() is only intended to be called when its
  721. * known the kernel is about to panic and we need the write to succeed. Since
  722. * the kernel is not going to be running for much longer, this function can
  723. * break locks and delay to ensure the write succeeds (but not sleep).
  724. */
  725. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  726. const u_char *buf)
  727. {
  728. *retlen = 0;
  729. if (!mtd->_panic_write)
  730. return -EOPNOTSUPP;
  731. if (to < 0 || to >= mtd->size || len > mtd->size - to)
  732. return -EINVAL;
  733. if (!(mtd->flags & MTD_WRITEABLE))
  734. return -EROFS;
  735. if (!len)
  736. return 0;
  737. return mtd->_panic_write(mtd, to, len, retlen, buf);
  738. }
  739. EXPORT_SYMBOL_GPL(mtd_panic_write);
  740. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  741. {
  742. int ret_code;
  743. ops->retlen = ops->oobretlen = 0;
  744. if (!mtd->_read_oob)
  745. return -EOPNOTSUPP;
  746. /*
  747. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  748. * similar to mtd->_read(), returning a non-negative integer
  749. * representing max bitflips. In other cases, mtd->_read_oob() may
  750. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  751. */
  752. ret_code = mtd->_read_oob(mtd, from, ops);
  753. if (unlikely(ret_code < 0))
  754. return ret_code;
  755. if (mtd->ecc_strength == 0)
  756. return 0; /* device lacks ecc */
  757. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  758. }
  759. EXPORT_SYMBOL_GPL(mtd_read_oob);
  760. /*
  761. * Method to access the protection register area, present in some flash
  762. * devices. The user data is one time programmable but the factory data is read
  763. * only.
  764. */
  765. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  766. struct otp_info *buf)
  767. {
  768. if (!mtd->_get_fact_prot_info)
  769. return -EOPNOTSUPP;
  770. if (!len)
  771. return 0;
  772. return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
  773. }
  774. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  775. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  776. size_t *retlen, u_char *buf)
  777. {
  778. *retlen = 0;
  779. if (!mtd->_read_fact_prot_reg)
  780. return -EOPNOTSUPP;
  781. if (!len)
  782. return 0;
  783. return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
  784. }
  785. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  786. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  787. struct otp_info *buf)
  788. {
  789. if (!mtd->_get_user_prot_info)
  790. return -EOPNOTSUPP;
  791. if (!len)
  792. return 0;
  793. return mtd->_get_user_prot_info(mtd, len, retlen, buf);
  794. }
  795. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  796. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  797. size_t *retlen, u_char *buf)
  798. {
  799. *retlen = 0;
  800. if (!mtd->_read_user_prot_reg)
  801. return -EOPNOTSUPP;
  802. if (!len)
  803. return 0;
  804. return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
  805. }
  806. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  807. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  808. size_t *retlen, u_char *buf)
  809. {
  810. int ret;
  811. *retlen = 0;
  812. if (!mtd->_write_user_prot_reg)
  813. return -EOPNOTSUPP;
  814. if (!len)
  815. return 0;
  816. ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
  817. if (ret)
  818. return ret;
  819. /*
  820. * If no data could be written at all, we are out of memory and
  821. * must return -ENOSPC.
  822. */
  823. return (*retlen) ? 0 : -ENOSPC;
  824. }
  825. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  826. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  827. {
  828. if (!mtd->_lock_user_prot_reg)
  829. return -EOPNOTSUPP;
  830. if (!len)
  831. return 0;
  832. return mtd->_lock_user_prot_reg(mtd, from, len);
  833. }
  834. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  835. /* Chip-supported device locking */
  836. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  837. {
  838. if (!mtd->_lock)
  839. return -EOPNOTSUPP;
  840. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  841. return -EINVAL;
  842. if (!len)
  843. return 0;
  844. return mtd->_lock(mtd, ofs, len);
  845. }
  846. EXPORT_SYMBOL_GPL(mtd_lock);
  847. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  848. {
  849. if (!mtd->_unlock)
  850. return -EOPNOTSUPP;
  851. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  852. return -EINVAL;
  853. if (!len)
  854. return 0;
  855. return mtd->_unlock(mtd, ofs, len);
  856. }
  857. EXPORT_SYMBOL_GPL(mtd_unlock);
  858. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  859. {
  860. if (!mtd->_is_locked)
  861. return -EOPNOTSUPP;
  862. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  863. return -EINVAL;
  864. if (!len)
  865. return 0;
  866. return mtd->_is_locked(mtd, ofs, len);
  867. }
  868. EXPORT_SYMBOL_GPL(mtd_is_locked);
  869. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  870. {
  871. if (ofs < 0 || ofs >= mtd->size)
  872. return -EINVAL;
  873. if (!mtd->_block_isreserved)
  874. return 0;
  875. return mtd->_block_isreserved(mtd, ofs);
  876. }
  877. EXPORT_SYMBOL_GPL(mtd_block_isreserved);
  878. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  879. {
  880. if (ofs < 0 || ofs >= mtd->size)
  881. return -EINVAL;
  882. if (!mtd->_block_isbad)
  883. return 0;
  884. return mtd->_block_isbad(mtd, ofs);
  885. }
  886. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  887. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  888. {
  889. if (!mtd->_block_markbad)
  890. return -EOPNOTSUPP;
  891. if (ofs < 0 || ofs >= mtd->size)
  892. return -EINVAL;
  893. if (!(mtd->flags & MTD_WRITEABLE))
  894. return -EROFS;
  895. return mtd->_block_markbad(mtd, ofs);
  896. }
  897. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  898. /*
  899. * default_mtd_writev - the default writev method
  900. * @mtd: mtd device description object pointer
  901. * @vecs: the vectors to write
  902. * @count: count of vectors in @vecs
  903. * @to: the MTD device offset to write to
  904. * @retlen: on exit contains the count of bytes written to the MTD device.
  905. *
  906. * This function returns zero in case of success and a negative error code in
  907. * case of failure.
  908. */
  909. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  910. unsigned long count, loff_t to, size_t *retlen)
  911. {
  912. unsigned long i;
  913. size_t totlen = 0, thislen;
  914. int ret = 0;
  915. for (i = 0; i < count; i++) {
  916. if (!vecs[i].iov_len)
  917. continue;
  918. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  919. vecs[i].iov_base);
  920. totlen += thislen;
  921. if (ret || thislen != vecs[i].iov_len)
  922. break;
  923. to += vecs[i].iov_len;
  924. }
  925. *retlen = totlen;
  926. return ret;
  927. }
  928. /*
  929. * mtd_writev - the vector-based MTD write method
  930. * @mtd: mtd device description object pointer
  931. * @vecs: the vectors to write
  932. * @count: count of vectors in @vecs
  933. * @to: the MTD device offset to write to
  934. * @retlen: on exit contains the count of bytes written to the MTD device.
  935. *
  936. * This function returns zero in case of success and a negative error code in
  937. * case of failure.
  938. */
  939. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  940. unsigned long count, loff_t to, size_t *retlen)
  941. {
  942. *retlen = 0;
  943. if (!(mtd->flags & MTD_WRITEABLE))
  944. return -EROFS;
  945. if (!mtd->_writev)
  946. return default_mtd_writev(mtd, vecs, count, to, retlen);
  947. return mtd->_writev(mtd, vecs, count, to, retlen);
  948. }
  949. EXPORT_SYMBOL_GPL(mtd_writev);
  950. /**
  951. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  952. * @mtd: mtd device description object pointer
  953. * @size: a pointer to the ideal or maximum size of the allocation, points
  954. * to the actual allocation size on success.
  955. *
  956. * This routine attempts to allocate a contiguous kernel buffer up to
  957. * the specified size, backing off the size of the request exponentially
  958. * until the request succeeds or until the allocation size falls below
  959. * the system page size. This attempts to make sure it does not adversely
  960. * impact system performance, so when allocating more than one page, we
  961. * ask the memory allocator to avoid re-trying, swapping, writing back
  962. * or performing I/O.
  963. *
  964. * Note, this function also makes sure that the allocated buffer is aligned to
  965. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  966. *
  967. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  968. * to handle smaller (i.e. degraded) buffer allocations under low- or
  969. * fragmented-memory situations where such reduced allocations, from a
  970. * requested ideal, are allowed.
  971. *
  972. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  973. */
  974. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  975. {
  976. gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
  977. __GFP_NORETRY | __GFP_NO_KSWAPD;
  978. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  979. void *kbuf;
  980. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  981. while (*size > min_alloc) {
  982. kbuf = kmalloc(*size, flags);
  983. if (kbuf)
  984. return kbuf;
  985. *size >>= 1;
  986. *size = ALIGN(*size, mtd->writesize);
  987. }
  988. /*
  989. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  990. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  991. */
  992. return kmalloc(*size, GFP_KERNEL);
  993. }
  994. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  995. #ifdef CONFIG_PROC_FS
  996. /*====================================================================*/
  997. /* Support for /proc/mtd */
  998. static int mtd_proc_show(struct seq_file *m, void *v)
  999. {
  1000. struct mtd_info *mtd;
  1001. seq_puts(m, "dev: size erasesize name\n");
  1002. mutex_lock(&mtd_table_mutex);
  1003. mtd_for_each_device(mtd) {
  1004. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  1005. mtd->index, (unsigned long long)mtd->size,
  1006. mtd->erasesize, mtd->name);
  1007. }
  1008. mutex_unlock(&mtd_table_mutex);
  1009. return 0;
  1010. }
  1011. static int mtd_proc_open(struct inode *inode, struct file *file)
  1012. {
  1013. return single_open(file, mtd_proc_show, NULL);
  1014. }
  1015. static const struct file_operations mtd_proc_ops = {
  1016. .open = mtd_proc_open,
  1017. .read = seq_read,
  1018. .llseek = seq_lseek,
  1019. .release = single_release,
  1020. };
  1021. #endif /* CONFIG_PROC_FS */
  1022. /*====================================================================*/
  1023. /* Init code */
  1024. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  1025. {
  1026. int ret;
  1027. ret = bdi_init(bdi);
  1028. if (!ret)
  1029. ret = bdi_register(bdi, NULL, "%s", name);
  1030. if (ret)
  1031. bdi_destroy(bdi);
  1032. return ret;
  1033. }
  1034. static struct proc_dir_entry *proc_mtd;
  1035. static int __init init_mtd(void)
  1036. {
  1037. int ret;
  1038. ret = class_register(&mtd_class);
  1039. if (ret)
  1040. goto err_reg;
  1041. ret = mtd_bdi_init(&mtd_bdi, "mtd");
  1042. if (ret)
  1043. goto err_bdi;
  1044. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  1045. ret = init_mtdchar();
  1046. if (ret)
  1047. goto out_procfs;
  1048. return 0;
  1049. out_procfs:
  1050. if (proc_mtd)
  1051. remove_proc_entry("mtd", NULL);
  1052. err_bdi:
  1053. class_unregister(&mtd_class);
  1054. err_reg:
  1055. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1056. return ret;
  1057. }
  1058. static void __exit cleanup_mtd(void)
  1059. {
  1060. cleanup_mtdchar();
  1061. if (proc_mtd)
  1062. remove_proc_entry("mtd", NULL);
  1063. class_unregister(&mtd_class);
  1064. bdi_destroy(&mtd_bdi);
  1065. }
  1066. module_init(init_mtd);
  1067. module_exit(cleanup_mtd);
  1068. MODULE_LICENSE("GPL");
  1069. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1070. MODULE_DESCRIPTION("Core MTD registration and access routines");