mtdcore.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Core registration and callback routines for MTD
  4. * drivers and users.
  5. *
  6. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  7. * Copyright © 2006 Red Hat UK Limited
  8. *
  9. */
  10. #ifndef __UBOOT__
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/string.h>
  16. #include <linux/timer.h>
  17. #include <linux/major.h>
  18. #include <linux/fs.h>
  19. #include <linux/err.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/init.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/idr.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/gfp.h>
  26. #include <linux/slab.h>
  27. #else
  28. #include <linux/bitops.h>
  29. #include <linux/bug.h>
  30. #include <linux/err.h>
  31. #include <ubi_uboot.h>
  32. #endif
  33. #include <linux/log2.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/partitions.h>
  36. #include "mtdcore.h"
  37. #ifndef __UBOOT__
  38. /*
  39. * backing device capabilities for non-mappable devices (such as NAND flash)
  40. * - permits private mappings, copies are taken of the data
  41. */
  42. static struct backing_dev_info mtd_bdi_unmappable = {
  43. .capabilities = BDI_CAP_MAP_COPY,
  44. };
  45. /*
  46. * backing device capabilities for R/O mappable devices (such as ROM)
  47. * - permits private mappings, copies are taken of the data
  48. * - permits non-writable shared mappings
  49. */
  50. static struct backing_dev_info mtd_bdi_ro_mappable = {
  51. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  52. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
  53. };
  54. /*
  55. * backing device capabilities for writable mappable devices (such as RAM)
  56. * - permits private mappings, copies are taken of the data
  57. * - permits non-writable shared mappings
  58. */
  59. static struct backing_dev_info mtd_bdi_rw_mappable = {
  60. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  61. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
  62. BDI_CAP_WRITE_MAP),
  63. };
  64. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  65. static int mtd_cls_resume(struct device *dev);
  66. static struct class mtd_class = {
  67. .name = "mtd",
  68. .owner = THIS_MODULE,
  69. .suspend = mtd_cls_suspend,
  70. .resume = mtd_cls_resume,
  71. };
  72. #else
  73. #define MAX_IDR_ID 64
  74. struct idr_layer {
  75. int used;
  76. void *ptr;
  77. };
  78. struct idr {
  79. struct idr_layer id[MAX_IDR_ID];
  80. bool updated;
  81. };
  82. #define DEFINE_IDR(name) struct idr name;
  83. void idr_remove(struct idr *idp, int id)
  84. {
  85. if (idp->id[id].used) {
  86. idp->id[id].used = 0;
  87. idp->updated = true;
  88. }
  89. return;
  90. }
  91. void *idr_find(struct idr *idp, int id)
  92. {
  93. if (idp->id[id].used)
  94. return idp->id[id].ptr;
  95. return NULL;
  96. }
  97. void *idr_get_next(struct idr *idp, int *next)
  98. {
  99. void *ret;
  100. int id = *next;
  101. ret = idr_find(idp, id);
  102. if (ret) {
  103. id ++;
  104. if (!idp->id[id].used)
  105. id = 0;
  106. *next = id;
  107. } else {
  108. *next = 0;
  109. }
  110. return ret;
  111. }
  112. int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
  113. {
  114. struct idr_layer *idl;
  115. int i = 0;
  116. while (i < MAX_IDR_ID) {
  117. idl = &idp->id[i];
  118. if (idl->used == 0) {
  119. idl->used = 1;
  120. idl->ptr = ptr;
  121. idp->updated = true;
  122. return i;
  123. }
  124. i++;
  125. }
  126. return -ENOSPC;
  127. }
  128. #endif
  129. static DEFINE_IDR(mtd_idr);
  130. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  131. should not use them for _anything_ else */
  132. DEFINE_MUTEX(mtd_table_mutex);
  133. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  134. struct mtd_info *__mtd_next_device(int i)
  135. {
  136. return idr_get_next(&mtd_idr, &i);
  137. }
  138. EXPORT_SYMBOL_GPL(__mtd_next_device);
  139. bool mtd_dev_list_updated(void)
  140. {
  141. if (mtd_idr.updated) {
  142. mtd_idr.updated = false;
  143. return true;
  144. }
  145. return false;
  146. }
  147. #ifndef __UBOOT__
  148. static LIST_HEAD(mtd_notifiers);
  149. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  150. /* REVISIT once MTD uses the driver model better, whoever allocates
  151. * the mtd_info will probably want to use the release() hook...
  152. */
  153. static void mtd_release(struct device *dev)
  154. {
  155. struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
  156. dev_t index = MTD_DEVT(mtd->index);
  157. /* remove /dev/mtdXro node if needed */
  158. if (index)
  159. device_destroy(&mtd_class, index + 1);
  160. }
  161. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  162. {
  163. struct mtd_info *mtd = dev_get_drvdata(dev);
  164. return mtd ? mtd_suspend(mtd) : 0;
  165. }
  166. static int mtd_cls_resume(struct device *dev)
  167. {
  168. struct mtd_info *mtd = dev_get_drvdata(dev);
  169. if (mtd)
  170. mtd_resume(mtd);
  171. return 0;
  172. }
  173. static ssize_t mtd_type_show(struct device *dev,
  174. struct device_attribute *attr, char *buf)
  175. {
  176. struct mtd_info *mtd = dev_get_drvdata(dev);
  177. char *type;
  178. switch (mtd->type) {
  179. case MTD_ABSENT:
  180. type = "absent";
  181. break;
  182. case MTD_RAM:
  183. type = "ram";
  184. break;
  185. case MTD_ROM:
  186. type = "rom";
  187. break;
  188. case MTD_NORFLASH:
  189. type = "nor";
  190. break;
  191. case MTD_NANDFLASH:
  192. type = "nand";
  193. break;
  194. case MTD_DATAFLASH:
  195. type = "dataflash";
  196. break;
  197. case MTD_UBIVOLUME:
  198. type = "ubi";
  199. break;
  200. case MTD_MLCNANDFLASH:
  201. type = "mlc-nand";
  202. break;
  203. default:
  204. type = "unknown";
  205. }
  206. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  207. }
  208. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  209. static ssize_t mtd_flags_show(struct device *dev,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct mtd_info *mtd = dev_get_drvdata(dev);
  213. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  214. }
  215. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  216. static ssize_t mtd_size_show(struct device *dev,
  217. struct device_attribute *attr, char *buf)
  218. {
  219. struct mtd_info *mtd = dev_get_drvdata(dev);
  220. return snprintf(buf, PAGE_SIZE, "%llu\n",
  221. (unsigned long long)mtd->size);
  222. }
  223. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  224. static ssize_t mtd_erasesize_show(struct device *dev,
  225. struct device_attribute *attr, char *buf)
  226. {
  227. struct mtd_info *mtd = dev_get_drvdata(dev);
  228. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  229. }
  230. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  231. static ssize_t mtd_writesize_show(struct device *dev,
  232. struct device_attribute *attr, char *buf)
  233. {
  234. struct mtd_info *mtd = dev_get_drvdata(dev);
  235. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  236. }
  237. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  238. static ssize_t mtd_subpagesize_show(struct device *dev,
  239. struct device_attribute *attr, char *buf)
  240. {
  241. struct mtd_info *mtd = dev_get_drvdata(dev);
  242. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  243. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  244. }
  245. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  246. static ssize_t mtd_oobsize_show(struct device *dev,
  247. struct device_attribute *attr, char *buf)
  248. {
  249. struct mtd_info *mtd = dev_get_drvdata(dev);
  250. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  251. }
  252. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  253. static ssize_t mtd_numeraseregions_show(struct device *dev,
  254. struct device_attribute *attr, char *buf)
  255. {
  256. struct mtd_info *mtd = dev_get_drvdata(dev);
  257. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  258. }
  259. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  260. NULL);
  261. static ssize_t mtd_name_show(struct device *dev,
  262. struct device_attribute *attr, char *buf)
  263. {
  264. struct mtd_info *mtd = dev_get_drvdata(dev);
  265. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  266. }
  267. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  268. static ssize_t mtd_ecc_strength_show(struct device *dev,
  269. struct device_attribute *attr, char *buf)
  270. {
  271. struct mtd_info *mtd = dev_get_drvdata(dev);
  272. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
  273. }
  274. static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
  275. static ssize_t mtd_bitflip_threshold_show(struct device *dev,
  276. struct device_attribute *attr,
  277. char *buf)
  278. {
  279. struct mtd_info *mtd = dev_get_drvdata(dev);
  280. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
  281. }
  282. static ssize_t mtd_bitflip_threshold_store(struct device *dev,
  283. struct device_attribute *attr,
  284. const char *buf, size_t count)
  285. {
  286. struct mtd_info *mtd = dev_get_drvdata(dev);
  287. unsigned int bitflip_threshold;
  288. int retval;
  289. retval = kstrtouint(buf, 0, &bitflip_threshold);
  290. if (retval)
  291. return retval;
  292. mtd->bitflip_threshold = bitflip_threshold;
  293. return count;
  294. }
  295. static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
  296. mtd_bitflip_threshold_show,
  297. mtd_bitflip_threshold_store);
  298. static ssize_t mtd_ecc_step_size_show(struct device *dev,
  299. struct device_attribute *attr, char *buf)
  300. {
  301. struct mtd_info *mtd = dev_get_drvdata(dev);
  302. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
  303. }
  304. static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
  305. static struct attribute *mtd_attrs[] = {
  306. &dev_attr_type.attr,
  307. &dev_attr_flags.attr,
  308. &dev_attr_size.attr,
  309. &dev_attr_erasesize.attr,
  310. &dev_attr_writesize.attr,
  311. &dev_attr_subpagesize.attr,
  312. &dev_attr_oobsize.attr,
  313. &dev_attr_numeraseregions.attr,
  314. &dev_attr_name.attr,
  315. &dev_attr_ecc_strength.attr,
  316. &dev_attr_ecc_step_size.attr,
  317. &dev_attr_bitflip_threshold.attr,
  318. NULL,
  319. };
  320. ATTRIBUTE_GROUPS(mtd);
  321. static struct device_type mtd_devtype = {
  322. .name = "mtd",
  323. .groups = mtd_groups,
  324. .release = mtd_release,
  325. };
  326. #endif
  327. /**
  328. * add_mtd_device - register an MTD device
  329. * @mtd: pointer to new MTD device info structure
  330. *
  331. * Add a device to the list of MTD devices present in the system, and
  332. * notify each currently active MTD 'user' of its arrival. Returns
  333. * zero on success or 1 on failure, which currently will only happen
  334. * if there is insufficient memory or a sysfs error.
  335. */
  336. int add_mtd_device(struct mtd_info *mtd)
  337. {
  338. #ifndef __UBOOT__
  339. struct mtd_notifier *not;
  340. #endif
  341. int i, error;
  342. #ifndef __UBOOT__
  343. if (!mtd->backing_dev_info) {
  344. switch (mtd->type) {
  345. case MTD_RAM:
  346. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  347. break;
  348. case MTD_ROM:
  349. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  350. break;
  351. default:
  352. mtd->backing_dev_info = &mtd_bdi_unmappable;
  353. break;
  354. }
  355. }
  356. #endif
  357. BUG_ON(mtd->writesize == 0);
  358. mutex_lock(&mtd_table_mutex);
  359. i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
  360. if (i < 0)
  361. goto fail_locked;
  362. mtd->index = i;
  363. mtd->usecount = 0;
  364. INIT_LIST_HEAD(&mtd->partitions);
  365. /* default value if not set by driver */
  366. if (mtd->bitflip_threshold == 0)
  367. mtd->bitflip_threshold = mtd->ecc_strength;
  368. if (is_power_of_2(mtd->erasesize))
  369. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  370. else
  371. mtd->erasesize_shift = 0;
  372. if (is_power_of_2(mtd->writesize))
  373. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  374. else
  375. mtd->writesize_shift = 0;
  376. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  377. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  378. /* Some chips always power up locked. Unlock them now */
  379. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  380. error = mtd_unlock(mtd, 0, mtd->size);
  381. if (error && error != -EOPNOTSUPP)
  382. printk(KERN_WARNING
  383. "%s: unlock failed, writes may not work\n",
  384. mtd->name);
  385. }
  386. #ifndef __UBOOT__
  387. /* Caller should have set dev.parent to match the
  388. * physical device.
  389. */
  390. mtd->dev.type = &mtd_devtype;
  391. mtd->dev.class = &mtd_class;
  392. mtd->dev.devt = MTD_DEVT(i);
  393. dev_set_name(&mtd->dev, "mtd%d", i);
  394. dev_set_drvdata(&mtd->dev, mtd);
  395. if (device_register(&mtd->dev) != 0)
  396. goto fail_added;
  397. if (MTD_DEVT(i))
  398. device_create(&mtd_class, mtd->dev.parent,
  399. MTD_DEVT(i) + 1,
  400. NULL, "mtd%dro", i);
  401. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  402. /* No need to get a refcount on the module containing
  403. the notifier, since we hold the mtd_table_mutex */
  404. list_for_each_entry(not, &mtd_notifiers, list)
  405. not->add(mtd);
  406. #else
  407. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  408. #endif
  409. mutex_unlock(&mtd_table_mutex);
  410. /* We _know_ we aren't being removed, because
  411. our caller is still holding us here. So none
  412. of this try_ nonsense, and no bitching about it
  413. either. :) */
  414. __module_get(THIS_MODULE);
  415. return 0;
  416. #ifndef __UBOOT__
  417. fail_added:
  418. idr_remove(&mtd_idr, i);
  419. #endif
  420. fail_locked:
  421. mutex_unlock(&mtd_table_mutex);
  422. return 1;
  423. }
  424. /**
  425. * del_mtd_device - unregister an MTD device
  426. * @mtd: pointer to MTD device info structure
  427. *
  428. * Remove a device from the list of MTD devices present in the system,
  429. * and notify each currently active MTD 'user' of its departure.
  430. * Returns zero on success or 1 on failure, which currently will happen
  431. * if the requested device does not appear to be present in the list.
  432. */
  433. int del_mtd_device(struct mtd_info *mtd)
  434. {
  435. int ret;
  436. #ifndef __UBOOT__
  437. struct mtd_notifier *not;
  438. #endif
  439. ret = del_mtd_partitions(mtd);
  440. if (ret) {
  441. debug("Failed to delete MTD partitions attached to %s (err %d)\n",
  442. mtd->name, ret);
  443. return ret;
  444. }
  445. mutex_lock(&mtd_table_mutex);
  446. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  447. ret = -ENODEV;
  448. goto out_error;
  449. }
  450. #ifndef __UBOOT__
  451. /* No need to get a refcount on the module containing
  452. the notifier, since we hold the mtd_table_mutex */
  453. list_for_each_entry(not, &mtd_notifiers, list)
  454. not->remove(mtd);
  455. #endif
  456. if (mtd->usecount) {
  457. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  458. mtd->index, mtd->name, mtd->usecount);
  459. ret = -EBUSY;
  460. } else {
  461. #ifndef __UBOOT__
  462. device_unregister(&mtd->dev);
  463. #endif
  464. idr_remove(&mtd_idr, mtd->index);
  465. module_put(THIS_MODULE);
  466. ret = 0;
  467. }
  468. out_error:
  469. mutex_unlock(&mtd_table_mutex);
  470. return ret;
  471. }
  472. #ifndef __UBOOT__
  473. /**
  474. * mtd_device_parse_register - parse partitions and register an MTD device.
  475. *
  476. * @mtd: the MTD device to register
  477. * @types: the list of MTD partition probes to try, see
  478. * 'parse_mtd_partitions()' for more information
  479. * @parser_data: MTD partition parser-specific data
  480. * @parts: fallback partition information to register, if parsing fails;
  481. * only valid if %nr_parts > %0
  482. * @nr_parts: the number of partitions in parts, if zero then the full
  483. * MTD device is registered if no partition info is found
  484. *
  485. * This function aggregates MTD partitions parsing (done by
  486. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  487. * basically follows the most common pattern found in many MTD drivers:
  488. *
  489. * * It first tries to probe partitions on MTD device @mtd using parsers
  490. * specified in @types (if @types is %NULL, then the default list of parsers
  491. * is used, see 'parse_mtd_partitions()' for more information). If none are
  492. * found this functions tries to fallback to information specified in
  493. * @parts/@nr_parts.
  494. * * If any partitioning info was found, this function registers the found
  495. * partitions.
  496. * * If no partitions were found this function just registers the MTD device
  497. * @mtd and exits.
  498. *
  499. * Returns zero in case of success and a negative error code in case of failure.
  500. */
  501. int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
  502. struct mtd_part_parser_data *parser_data,
  503. const struct mtd_partition *parts,
  504. int nr_parts)
  505. {
  506. int err;
  507. struct mtd_partition *real_parts;
  508. err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
  509. if (err <= 0 && nr_parts && parts) {
  510. real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
  511. GFP_KERNEL);
  512. if (!real_parts)
  513. err = -ENOMEM;
  514. else
  515. err = nr_parts;
  516. }
  517. if (err > 0) {
  518. err = add_mtd_partitions(mtd, real_parts, err);
  519. kfree(real_parts);
  520. } else if (err == 0) {
  521. err = add_mtd_device(mtd);
  522. if (err == 1)
  523. err = -ENODEV;
  524. }
  525. return err;
  526. }
  527. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  528. /**
  529. * mtd_device_unregister - unregister an existing MTD device.
  530. *
  531. * @master: the MTD device to unregister. This will unregister both the master
  532. * and any partitions if registered.
  533. */
  534. int mtd_device_unregister(struct mtd_info *master)
  535. {
  536. int err;
  537. err = del_mtd_partitions(master);
  538. if (err)
  539. return err;
  540. if (!device_is_registered(&master->dev))
  541. return 0;
  542. return del_mtd_device(master);
  543. }
  544. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  545. /**
  546. * register_mtd_user - register a 'user' of MTD devices.
  547. * @new: pointer to notifier info structure
  548. *
  549. * Registers a pair of callbacks function to be called upon addition
  550. * or removal of MTD devices. Causes the 'add' callback to be immediately
  551. * invoked for each MTD device currently present in the system.
  552. */
  553. void register_mtd_user (struct mtd_notifier *new)
  554. {
  555. struct mtd_info *mtd;
  556. mutex_lock(&mtd_table_mutex);
  557. list_add(&new->list, &mtd_notifiers);
  558. __module_get(THIS_MODULE);
  559. mtd_for_each_device(mtd)
  560. new->add(mtd);
  561. mutex_unlock(&mtd_table_mutex);
  562. }
  563. EXPORT_SYMBOL_GPL(register_mtd_user);
  564. /**
  565. * unregister_mtd_user - unregister a 'user' of MTD devices.
  566. * @old: pointer to notifier info structure
  567. *
  568. * Removes a callback function pair from the list of 'users' to be
  569. * notified upon addition or removal of MTD devices. Causes the
  570. * 'remove' callback to be immediately invoked for each MTD device
  571. * currently present in the system.
  572. */
  573. int unregister_mtd_user (struct mtd_notifier *old)
  574. {
  575. struct mtd_info *mtd;
  576. mutex_lock(&mtd_table_mutex);
  577. module_put(THIS_MODULE);
  578. mtd_for_each_device(mtd)
  579. old->remove(mtd);
  580. list_del(&old->list);
  581. mutex_unlock(&mtd_table_mutex);
  582. return 0;
  583. }
  584. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  585. #endif
  586. /**
  587. * get_mtd_device - obtain a validated handle for an MTD device
  588. * @mtd: last known address of the required MTD device
  589. * @num: internal device number of the required MTD device
  590. *
  591. * Given a number and NULL address, return the num'th entry in the device
  592. * table, if any. Given an address and num == -1, search the device table
  593. * for a device with that address and return if it's still present. Given
  594. * both, return the num'th driver only if its address matches. Return
  595. * error code if not.
  596. */
  597. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  598. {
  599. struct mtd_info *ret = NULL, *other;
  600. int err = -ENODEV;
  601. mutex_lock(&mtd_table_mutex);
  602. if (num == -1) {
  603. mtd_for_each_device(other) {
  604. if (other == mtd) {
  605. ret = mtd;
  606. break;
  607. }
  608. }
  609. } else if (num >= 0) {
  610. ret = idr_find(&mtd_idr, num);
  611. if (mtd && mtd != ret)
  612. ret = NULL;
  613. }
  614. if (!ret) {
  615. ret = ERR_PTR(err);
  616. goto out;
  617. }
  618. err = __get_mtd_device(ret);
  619. if (err)
  620. ret = ERR_PTR(err);
  621. out:
  622. mutex_unlock(&mtd_table_mutex);
  623. return ret;
  624. }
  625. EXPORT_SYMBOL_GPL(get_mtd_device);
  626. int __get_mtd_device(struct mtd_info *mtd)
  627. {
  628. int err;
  629. if (!try_module_get(mtd->owner))
  630. return -ENODEV;
  631. if (mtd->_get_device) {
  632. err = mtd->_get_device(mtd);
  633. if (err) {
  634. module_put(mtd->owner);
  635. return err;
  636. }
  637. }
  638. mtd->usecount++;
  639. return 0;
  640. }
  641. EXPORT_SYMBOL_GPL(__get_mtd_device);
  642. #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL)
  643. static bool mtd_device_matches_name(struct mtd_info *mtd, const char *name)
  644. {
  645. struct udevice *dev = NULL;
  646. bool is_part;
  647. /*
  648. * If the first character of mtd name is '/', try interpreting as OF
  649. * path. Otherwise try comparing by mtd->name and mtd->dev->name.
  650. */
  651. if (*name == '/')
  652. device_get_global_by_ofnode(ofnode_path(name), &dev);
  653. is_part = mtd_is_partition(mtd);
  654. return (!is_part && dev && mtd->dev == dev) ||
  655. !strcmp(name, mtd->name) ||
  656. (is_part && mtd->dev && !strcmp(name, mtd->dev->name));
  657. }
  658. #else
  659. static bool mtd_device_matches_name(struct mtd_info *mtd, const char *name)
  660. {
  661. return !strcmp(name, mtd->name);
  662. }
  663. #endif
  664. /**
  665. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  666. * device name
  667. * @name: MTD device name to open
  668. *
  669. * This function returns MTD device description structure in case of
  670. * success and an error code in case of failure.
  671. */
  672. struct mtd_info *get_mtd_device_nm(const char *name)
  673. {
  674. int err = -ENODEV;
  675. struct mtd_info *mtd = NULL, *other;
  676. mutex_lock(&mtd_table_mutex);
  677. mtd_for_each_device(other) {
  678. #ifdef __UBOOT__
  679. if (mtd_device_matches_name(other, name)) {
  680. if (mtd)
  681. printf("\nWarning: MTD name \"%s\" is not unique!\n\n",
  682. name);
  683. mtd = other;
  684. }
  685. #else /* !__UBOOT__ */
  686. if (!strcmp(name, other->name)) {
  687. mtd = other;
  688. break;
  689. }
  690. #endif /* !__UBOOT__ */
  691. }
  692. if (!mtd)
  693. goto out_unlock;
  694. err = __get_mtd_device(mtd);
  695. if (err)
  696. goto out_unlock;
  697. mutex_unlock(&mtd_table_mutex);
  698. return mtd;
  699. out_unlock:
  700. mutex_unlock(&mtd_table_mutex);
  701. return ERR_PTR(err);
  702. }
  703. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  704. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  705. /**
  706. * mtd_get_len_incl_bad
  707. *
  708. * Check if length including bad blocks fits into device.
  709. *
  710. * @param mtd an MTD device
  711. * @param offset offset in flash
  712. * @param length image length
  713. * @return image length including bad blocks in *len_incl_bad and whether or not
  714. * the length returned was truncated in *truncated
  715. */
  716. void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
  717. const uint64_t length, uint64_t *len_incl_bad,
  718. int *truncated)
  719. {
  720. *truncated = 0;
  721. *len_incl_bad = 0;
  722. if (!mtd->_block_isbad) {
  723. *len_incl_bad = length;
  724. return;
  725. }
  726. uint64_t len_excl_bad = 0;
  727. uint64_t block_len;
  728. while (len_excl_bad < length) {
  729. if (offset >= mtd->size) {
  730. *truncated = 1;
  731. return;
  732. }
  733. block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
  734. if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
  735. len_excl_bad += block_len;
  736. *len_incl_bad += block_len;
  737. offset += block_len;
  738. }
  739. }
  740. #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
  741. void put_mtd_device(struct mtd_info *mtd)
  742. {
  743. mutex_lock(&mtd_table_mutex);
  744. __put_mtd_device(mtd);
  745. mutex_unlock(&mtd_table_mutex);
  746. }
  747. EXPORT_SYMBOL_GPL(put_mtd_device);
  748. void __put_mtd_device(struct mtd_info *mtd)
  749. {
  750. --mtd->usecount;
  751. BUG_ON(mtd->usecount < 0);
  752. if (mtd->_put_device)
  753. mtd->_put_device(mtd);
  754. module_put(mtd->owner);
  755. }
  756. EXPORT_SYMBOL_GPL(__put_mtd_device);
  757. /*
  758. * Erase is an asynchronous operation. Device drivers are supposed
  759. * to call instr->callback() whenever the operation completes, even
  760. * if it completes with a failure.
  761. * Callers are supposed to pass a callback function and wait for it
  762. * to be called before writing to the block.
  763. */
  764. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  765. {
  766. if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
  767. return -EINVAL;
  768. if (!(mtd->flags & MTD_WRITEABLE))
  769. return -EROFS;
  770. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  771. if (!instr->len) {
  772. instr->state = MTD_ERASE_DONE;
  773. mtd_erase_callback(instr);
  774. return 0;
  775. }
  776. return mtd->_erase(mtd, instr);
  777. }
  778. EXPORT_SYMBOL_GPL(mtd_erase);
  779. #ifndef __UBOOT__
  780. /*
  781. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  782. */
  783. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  784. void **virt, resource_size_t *phys)
  785. {
  786. *retlen = 0;
  787. *virt = NULL;
  788. if (phys)
  789. *phys = 0;
  790. if (!mtd->_point)
  791. return -EOPNOTSUPP;
  792. if (from < 0 || from > mtd->size || len > mtd->size - from)
  793. return -EINVAL;
  794. if (!len)
  795. return 0;
  796. return mtd->_point(mtd, from, len, retlen, virt, phys);
  797. }
  798. EXPORT_SYMBOL_GPL(mtd_point);
  799. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  800. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  801. {
  802. if (!mtd->_point)
  803. return -EOPNOTSUPP;
  804. if (from < 0 || from > mtd->size || len > mtd->size - from)
  805. return -EINVAL;
  806. if (!len)
  807. return 0;
  808. return mtd->_unpoint(mtd, from, len);
  809. }
  810. EXPORT_SYMBOL_GPL(mtd_unpoint);
  811. #endif
  812. /*
  813. * Allow NOMMU mmap() to directly map the device (if not NULL)
  814. * - return the address to which the offset maps
  815. * - return -ENOSYS to indicate refusal to do the mapping
  816. */
  817. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  818. unsigned long offset, unsigned long flags)
  819. {
  820. if (!mtd->_get_unmapped_area)
  821. return -EOPNOTSUPP;
  822. if (offset > mtd->size || len > mtd->size - offset)
  823. return -EINVAL;
  824. return mtd->_get_unmapped_area(mtd, len, offset, flags);
  825. }
  826. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  827. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  828. u_char *buf)
  829. {
  830. int ret_code;
  831. *retlen = 0;
  832. if (from < 0 || from > mtd->size || len > mtd->size - from)
  833. return -EINVAL;
  834. if (!len)
  835. return 0;
  836. /*
  837. * In the absence of an error, drivers return a non-negative integer
  838. * representing the maximum number of bitflips that were corrected on
  839. * any one ecc region (if applicable; zero otherwise).
  840. */
  841. if (mtd->_read) {
  842. ret_code = mtd->_read(mtd, from, len, retlen, buf);
  843. } else if (mtd->_read_oob) {
  844. struct mtd_oob_ops ops = {
  845. .len = len,
  846. .datbuf = buf,
  847. };
  848. ret_code = mtd->_read_oob(mtd, from, &ops);
  849. *retlen = ops.retlen;
  850. } else {
  851. return -ENOTSUPP;
  852. }
  853. if (unlikely(ret_code < 0))
  854. return ret_code;
  855. if (mtd->ecc_strength == 0)
  856. return 0; /* device lacks ecc */
  857. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  858. }
  859. EXPORT_SYMBOL_GPL(mtd_read);
  860. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  861. const u_char *buf)
  862. {
  863. *retlen = 0;
  864. if (to < 0 || to > mtd->size || len > mtd->size - to)
  865. return -EINVAL;
  866. if ((!mtd->_write && !mtd->_write_oob) ||
  867. !(mtd->flags & MTD_WRITEABLE))
  868. return -EROFS;
  869. if (!len)
  870. return 0;
  871. if (!mtd->_write) {
  872. struct mtd_oob_ops ops = {
  873. .len = len,
  874. .datbuf = (u8 *)buf,
  875. };
  876. int ret;
  877. ret = mtd->_write_oob(mtd, to, &ops);
  878. *retlen = ops.retlen;
  879. return ret;
  880. }
  881. return mtd->_write(mtd, to, len, retlen, buf);
  882. }
  883. EXPORT_SYMBOL_GPL(mtd_write);
  884. /*
  885. * In blackbox flight recorder like scenarios we want to make successful writes
  886. * in interrupt context. panic_write() is only intended to be called when its
  887. * known the kernel is about to panic and we need the write to succeed. Since
  888. * the kernel is not going to be running for much longer, this function can
  889. * break locks and delay to ensure the write succeeds (but not sleep).
  890. */
  891. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  892. const u_char *buf)
  893. {
  894. *retlen = 0;
  895. if (!mtd->_panic_write)
  896. return -EOPNOTSUPP;
  897. if (to < 0 || to > mtd->size || len > mtd->size - to)
  898. return -EINVAL;
  899. if (!(mtd->flags & MTD_WRITEABLE))
  900. return -EROFS;
  901. if (!len)
  902. return 0;
  903. return mtd->_panic_write(mtd, to, len, retlen, buf);
  904. }
  905. EXPORT_SYMBOL_GPL(mtd_panic_write);
  906. static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
  907. struct mtd_oob_ops *ops)
  908. {
  909. /*
  910. * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
  911. * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
  912. * this case.
  913. */
  914. if (!ops->datbuf)
  915. ops->len = 0;
  916. if (!ops->oobbuf)
  917. ops->ooblen = 0;
  918. if (offs < 0 || offs + ops->len > mtd->size)
  919. return -EINVAL;
  920. if (ops->ooblen) {
  921. size_t maxooblen;
  922. if (ops->ooboffs >= mtd_oobavail(mtd, ops))
  923. return -EINVAL;
  924. maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
  925. mtd_div_by_ws(offs, mtd)) *
  926. mtd_oobavail(mtd, ops)) - ops->ooboffs;
  927. if (ops->ooblen > maxooblen)
  928. return -EINVAL;
  929. }
  930. return 0;
  931. }
  932. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  933. {
  934. int ret_code;
  935. ops->retlen = ops->oobretlen = 0;
  936. ret_code = mtd_check_oob_ops(mtd, from, ops);
  937. if (ret_code)
  938. return ret_code;
  939. /* Check the validity of a potential fallback on mtd->_read */
  940. if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
  941. return -EOPNOTSUPP;
  942. if (mtd->_read_oob)
  943. ret_code = mtd->_read_oob(mtd, from, ops);
  944. else
  945. ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
  946. ops->datbuf);
  947. /*
  948. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  949. * similar to mtd->_read(), returning a non-negative integer
  950. * representing max bitflips. In other cases, mtd->_read_oob() may
  951. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  952. */
  953. if (unlikely(ret_code < 0))
  954. return ret_code;
  955. if (mtd->ecc_strength == 0)
  956. return 0; /* device lacks ecc */
  957. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  958. }
  959. EXPORT_SYMBOL_GPL(mtd_read_oob);
  960. int mtd_write_oob(struct mtd_info *mtd, loff_t to,
  961. struct mtd_oob_ops *ops)
  962. {
  963. int ret;
  964. ops->retlen = ops->oobretlen = 0;
  965. if (!(mtd->flags & MTD_WRITEABLE))
  966. return -EROFS;
  967. ret = mtd_check_oob_ops(mtd, to, ops);
  968. if (ret)
  969. return ret;
  970. /* Check the validity of a potential fallback on mtd->_write */
  971. if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
  972. return -EOPNOTSUPP;
  973. if (mtd->_write_oob)
  974. return mtd->_write_oob(mtd, to, ops);
  975. else
  976. return mtd->_write(mtd, to, ops->len, &ops->retlen,
  977. ops->datbuf);
  978. }
  979. EXPORT_SYMBOL_GPL(mtd_write_oob);
  980. /**
  981. * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
  982. * @mtd: MTD device structure
  983. * @section: ECC section. Depending on the layout you may have all the ECC
  984. * bytes stored in a single contiguous section, or one section
  985. * per ECC chunk (and sometime several sections for a single ECC
  986. * ECC chunk)
  987. * @oobecc: OOB region struct filled with the appropriate ECC position
  988. * information
  989. *
  990. * This function returns ECC section information in the OOB area. If you want
  991. * to get all the ECC bytes information, then you should call
  992. * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
  993. *
  994. * Returns zero on success, a negative error code otherwise.
  995. */
  996. int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  997. struct mtd_oob_region *oobecc)
  998. {
  999. memset(oobecc, 0, sizeof(*oobecc));
  1000. if (!mtd || section < 0)
  1001. return -EINVAL;
  1002. if (!mtd->ooblayout || !mtd->ooblayout->ecc)
  1003. return -ENOTSUPP;
  1004. return mtd->ooblayout->ecc(mtd, section, oobecc);
  1005. }
  1006. EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
  1007. /**
  1008. * mtd_ooblayout_free - Get the OOB region definition of a specific free
  1009. * section
  1010. * @mtd: MTD device structure
  1011. * @section: Free section you are interested in. Depending on the layout
  1012. * you may have all the free bytes stored in a single contiguous
  1013. * section, or one section per ECC chunk plus an extra section
  1014. * for the remaining bytes (or other funky layout).
  1015. * @oobfree: OOB region struct filled with the appropriate free position
  1016. * information
  1017. *
  1018. * This function returns free bytes position in the OOB area. If you want
  1019. * to get all the free bytes information, then you should call
  1020. * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
  1021. *
  1022. * Returns zero on success, a negative error code otherwise.
  1023. */
  1024. int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  1025. struct mtd_oob_region *oobfree)
  1026. {
  1027. memset(oobfree, 0, sizeof(*oobfree));
  1028. if (!mtd || section < 0)
  1029. return -EINVAL;
  1030. if (!mtd->ooblayout || !mtd->ooblayout->rfree)
  1031. return -ENOTSUPP;
  1032. return mtd->ooblayout->rfree(mtd, section, oobfree);
  1033. }
  1034. EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
  1035. /**
  1036. * mtd_ooblayout_find_region - Find the region attached to a specific byte
  1037. * @mtd: mtd info structure
  1038. * @byte: the byte we are searching for
  1039. * @sectionp: pointer where the section id will be stored
  1040. * @oobregion: used to retrieve the ECC position
  1041. * @iter: iterator function. Should be either mtd_ooblayout_free or
  1042. * mtd_ooblayout_ecc depending on the region type you're searching for
  1043. *
  1044. * This function returns the section id and oobregion information of a
  1045. * specific byte. For example, say you want to know where the 4th ECC byte is
  1046. * stored, you'll use:
  1047. *
  1048. * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
  1049. *
  1050. * Returns zero on success, a negative error code otherwise.
  1051. */
  1052. static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
  1053. int *sectionp, struct mtd_oob_region *oobregion,
  1054. int (*iter)(struct mtd_info *,
  1055. int section,
  1056. struct mtd_oob_region *oobregion))
  1057. {
  1058. int pos = 0, ret, section = 0;
  1059. memset(oobregion, 0, sizeof(*oobregion));
  1060. while (1) {
  1061. ret = iter(mtd, section, oobregion);
  1062. if (ret)
  1063. return ret;
  1064. if (pos + oobregion->length > byte)
  1065. break;
  1066. pos += oobregion->length;
  1067. section++;
  1068. }
  1069. /*
  1070. * Adjust region info to make it start at the beginning at the
  1071. * 'start' ECC byte.
  1072. */
  1073. oobregion->offset += byte - pos;
  1074. oobregion->length -= byte - pos;
  1075. *sectionp = section;
  1076. return 0;
  1077. }
  1078. /**
  1079. * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
  1080. * ECC byte
  1081. * @mtd: mtd info structure
  1082. * @eccbyte: the byte we are searching for
  1083. * @sectionp: pointer where the section id will be stored
  1084. * @oobregion: OOB region information
  1085. *
  1086. * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
  1087. * byte.
  1088. *
  1089. * Returns zero on success, a negative error code otherwise.
  1090. */
  1091. int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  1092. int *section,
  1093. struct mtd_oob_region *oobregion)
  1094. {
  1095. return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
  1096. mtd_ooblayout_ecc);
  1097. }
  1098. EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
  1099. /**
  1100. * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
  1101. * @mtd: mtd info structure
  1102. * @buf: destination buffer to store OOB bytes
  1103. * @oobbuf: OOB buffer
  1104. * @start: first byte to retrieve
  1105. * @nbytes: number of bytes to retrieve
  1106. * @iter: section iterator
  1107. *
  1108. * Extract bytes attached to a specific category (ECC or free)
  1109. * from the OOB buffer and copy them into buf.
  1110. *
  1111. * Returns zero on success, a negative error code otherwise.
  1112. */
  1113. static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
  1114. const u8 *oobbuf, int start, int nbytes,
  1115. int (*iter)(struct mtd_info *,
  1116. int section,
  1117. struct mtd_oob_region *oobregion))
  1118. {
  1119. struct mtd_oob_region oobregion;
  1120. int section, ret;
  1121. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1122. &oobregion, iter);
  1123. while (!ret) {
  1124. int cnt;
  1125. cnt = min_t(int, nbytes, oobregion.length);
  1126. memcpy(buf, oobbuf + oobregion.offset, cnt);
  1127. buf += cnt;
  1128. nbytes -= cnt;
  1129. if (!nbytes)
  1130. break;
  1131. ret = iter(mtd, ++section, &oobregion);
  1132. }
  1133. return ret;
  1134. }
  1135. /**
  1136. * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
  1137. * @mtd: mtd info structure
  1138. * @buf: source buffer to get OOB bytes from
  1139. * @oobbuf: OOB buffer
  1140. * @start: first OOB byte to set
  1141. * @nbytes: number of OOB bytes to set
  1142. * @iter: section iterator
  1143. *
  1144. * Fill the OOB buffer with data provided in buf. The category (ECC or free)
  1145. * is selected by passing the appropriate iterator.
  1146. *
  1147. * Returns zero on success, a negative error code otherwise.
  1148. */
  1149. static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
  1150. u8 *oobbuf, int start, int nbytes,
  1151. int (*iter)(struct mtd_info *,
  1152. int section,
  1153. struct mtd_oob_region *oobregion))
  1154. {
  1155. struct mtd_oob_region oobregion;
  1156. int section, ret;
  1157. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1158. &oobregion, iter);
  1159. while (!ret) {
  1160. int cnt;
  1161. cnt = min_t(int, nbytes, oobregion.length);
  1162. memcpy(oobbuf + oobregion.offset, buf, cnt);
  1163. buf += cnt;
  1164. nbytes -= cnt;
  1165. if (!nbytes)
  1166. break;
  1167. ret = iter(mtd, ++section, &oobregion);
  1168. }
  1169. return ret;
  1170. }
  1171. /**
  1172. * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
  1173. * @mtd: mtd info structure
  1174. * @iter: category iterator
  1175. *
  1176. * Count the number of bytes in a given category.
  1177. *
  1178. * Returns a positive value on success, a negative error code otherwise.
  1179. */
  1180. static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
  1181. int (*iter)(struct mtd_info *,
  1182. int section,
  1183. struct mtd_oob_region *oobregion))
  1184. {
  1185. struct mtd_oob_region oobregion;
  1186. int section = 0, ret, nbytes = 0;
  1187. while (1) {
  1188. ret = iter(mtd, section++, &oobregion);
  1189. if (ret) {
  1190. if (ret == -ERANGE)
  1191. ret = nbytes;
  1192. break;
  1193. }
  1194. nbytes += oobregion.length;
  1195. }
  1196. return ret;
  1197. }
  1198. /**
  1199. * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
  1200. * @mtd: mtd info structure
  1201. * @eccbuf: destination buffer to store ECC bytes
  1202. * @oobbuf: OOB buffer
  1203. * @start: first ECC byte to retrieve
  1204. * @nbytes: number of ECC bytes to retrieve
  1205. *
  1206. * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
  1207. *
  1208. * Returns zero on success, a negative error code otherwise.
  1209. */
  1210. int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  1211. const u8 *oobbuf, int start, int nbytes)
  1212. {
  1213. return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1214. mtd_ooblayout_ecc);
  1215. }
  1216. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
  1217. /**
  1218. * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
  1219. * @mtd: mtd info structure
  1220. * @eccbuf: source buffer to get ECC bytes from
  1221. * @oobbuf: OOB buffer
  1222. * @start: first ECC byte to set
  1223. * @nbytes: number of ECC bytes to set
  1224. *
  1225. * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
  1226. *
  1227. * Returns zero on success, a negative error code otherwise.
  1228. */
  1229. int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  1230. u8 *oobbuf, int start, int nbytes)
  1231. {
  1232. return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1233. mtd_ooblayout_ecc);
  1234. }
  1235. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
  1236. /**
  1237. * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
  1238. * @mtd: mtd info structure
  1239. * @databuf: destination buffer to store ECC bytes
  1240. * @oobbuf: OOB buffer
  1241. * @start: first ECC byte to retrieve
  1242. * @nbytes: number of ECC bytes to retrieve
  1243. *
  1244. * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  1245. *
  1246. * Returns zero on success, a negative error code otherwise.
  1247. */
  1248. int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  1249. const u8 *oobbuf, int start, int nbytes)
  1250. {
  1251. return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
  1252. mtd_ooblayout_free);
  1253. }
  1254. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
  1255. /**
  1256. * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
  1257. * @mtd: mtd info structure
  1258. * @eccbuf: source buffer to get data bytes from
  1259. * @oobbuf: OOB buffer
  1260. * @start: first ECC byte to set
  1261. * @nbytes: number of ECC bytes to set
  1262. *
  1263. * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  1264. *
  1265. * Returns zero on success, a negative error code otherwise.
  1266. */
  1267. int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  1268. u8 *oobbuf, int start, int nbytes)
  1269. {
  1270. return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
  1271. mtd_ooblayout_free);
  1272. }
  1273. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
  1274. /**
  1275. * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
  1276. * @mtd: mtd info structure
  1277. *
  1278. * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
  1279. *
  1280. * Returns zero on success, a negative error code otherwise.
  1281. */
  1282. int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
  1283. {
  1284. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
  1285. }
  1286. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
  1287. /**
  1288. * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
  1289. * @mtd: mtd info structure
  1290. *
  1291. * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
  1292. *
  1293. * Returns zero on success, a negative error code otherwise.
  1294. */
  1295. int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
  1296. {
  1297. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
  1298. }
  1299. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
  1300. /*
  1301. * Method to access the protection register area, present in some flash
  1302. * devices. The user data is one time programmable but the factory data is read
  1303. * only.
  1304. */
  1305. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1306. struct otp_info *buf)
  1307. {
  1308. if (!mtd->_get_fact_prot_info)
  1309. return -EOPNOTSUPP;
  1310. if (!len)
  1311. return 0;
  1312. return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
  1313. }
  1314. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  1315. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1316. size_t *retlen, u_char *buf)
  1317. {
  1318. *retlen = 0;
  1319. if (!mtd->_read_fact_prot_reg)
  1320. return -EOPNOTSUPP;
  1321. if (!len)
  1322. return 0;
  1323. return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
  1324. }
  1325. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  1326. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1327. struct otp_info *buf)
  1328. {
  1329. if (!mtd->_get_user_prot_info)
  1330. return -EOPNOTSUPP;
  1331. if (!len)
  1332. return 0;
  1333. return mtd->_get_user_prot_info(mtd, len, retlen, buf);
  1334. }
  1335. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  1336. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1337. size_t *retlen, u_char *buf)
  1338. {
  1339. *retlen = 0;
  1340. if (!mtd->_read_user_prot_reg)
  1341. return -EOPNOTSUPP;
  1342. if (!len)
  1343. return 0;
  1344. return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
  1345. }
  1346. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  1347. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  1348. size_t *retlen, u_char *buf)
  1349. {
  1350. int ret;
  1351. *retlen = 0;
  1352. if (!mtd->_write_user_prot_reg)
  1353. return -EOPNOTSUPP;
  1354. if (!len)
  1355. return 0;
  1356. ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
  1357. if (ret)
  1358. return ret;
  1359. /*
  1360. * If no data could be written at all, we are out of memory and
  1361. * must return -ENOSPC.
  1362. */
  1363. return (*retlen) ? 0 : -ENOSPC;
  1364. }
  1365. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  1366. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  1367. {
  1368. if (!mtd->_lock_user_prot_reg)
  1369. return -EOPNOTSUPP;
  1370. if (!len)
  1371. return 0;
  1372. return mtd->_lock_user_prot_reg(mtd, from, len);
  1373. }
  1374. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  1375. /* Chip-supported device locking */
  1376. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1377. {
  1378. if (!mtd->_lock)
  1379. return -EOPNOTSUPP;
  1380. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1381. return -EINVAL;
  1382. if (!len)
  1383. return 0;
  1384. return mtd->_lock(mtd, ofs, len);
  1385. }
  1386. EXPORT_SYMBOL_GPL(mtd_lock);
  1387. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1388. {
  1389. if (!mtd->_unlock)
  1390. return -EOPNOTSUPP;
  1391. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1392. return -EINVAL;
  1393. if (!len)
  1394. return 0;
  1395. return mtd->_unlock(mtd, ofs, len);
  1396. }
  1397. EXPORT_SYMBOL_GPL(mtd_unlock);
  1398. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1399. {
  1400. if (!mtd->_is_locked)
  1401. return -EOPNOTSUPP;
  1402. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1403. return -EINVAL;
  1404. if (!len)
  1405. return 0;
  1406. return mtd->_is_locked(mtd, ofs, len);
  1407. }
  1408. EXPORT_SYMBOL_GPL(mtd_is_locked);
  1409. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  1410. {
  1411. if (ofs < 0 || ofs > mtd->size)
  1412. return -EINVAL;
  1413. if (!mtd->_block_isreserved)
  1414. return 0;
  1415. return mtd->_block_isreserved(mtd, ofs);
  1416. }
  1417. EXPORT_SYMBOL_GPL(mtd_block_isreserved);
  1418. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1419. {
  1420. if (ofs < 0 || ofs > mtd->size)
  1421. return -EINVAL;
  1422. if (!mtd->_block_isbad)
  1423. return 0;
  1424. return mtd->_block_isbad(mtd, ofs);
  1425. }
  1426. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  1427. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1428. {
  1429. if (!mtd->_block_markbad)
  1430. return -EOPNOTSUPP;
  1431. if (ofs < 0 || ofs > mtd->size)
  1432. return -EINVAL;
  1433. if (!(mtd->flags & MTD_WRITEABLE))
  1434. return -EROFS;
  1435. return mtd->_block_markbad(mtd, ofs);
  1436. }
  1437. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  1438. #ifndef __UBOOT__
  1439. /*
  1440. * default_mtd_writev - the default writev method
  1441. * @mtd: mtd device description object pointer
  1442. * @vecs: the vectors to write
  1443. * @count: count of vectors in @vecs
  1444. * @to: the MTD device offset to write to
  1445. * @retlen: on exit contains the count of bytes written to the MTD device.
  1446. *
  1447. * This function returns zero in case of success and a negative error code in
  1448. * case of failure.
  1449. */
  1450. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1451. unsigned long count, loff_t to, size_t *retlen)
  1452. {
  1453. unsigned long i;
  1454. size_t totlen = 0, thislen;
  1455. int ret = 0;
  1456. for (i = 0; i < count; i++) {
  1457. if (!vecs[i].iov_len)
  1458. continue;
  1459. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  1460. vecs[i].iov_base);
  1461. totlen += thislen;
  1462. if (ret || thislen != vecs[i].iov_len)
  1463. break;
  1464. to += vecs[i].iov_len;
  1465. }
  1466. *retlen = totlen;
  1467. return ret;
  1468. }
  1469. /*
  1470. * mtd_writev - the vector-based MTD write method
  1471. * @mtd: mtd device description object pointer
  1472. * @vecs: the vectors to write
  1473. * @count: count of vectors in @vecs
  1474. * @to: the MTD device offset to write to
  1475. * @retlen: on exit contains the count of bytes written to the MTD device.
  1476. *
  1477. * This function returns zero in case of success and a negative error code in
  1478. * case of failure.
  1479. */
  1480. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1481. unsigned long count, loff_t to, size_t *retlen)
  1482. {
  1483. *retlen = 0;
  1484. if (!(mtd->flags & MTD_WRITEABLE))
  1485. return -EROFS;
  1486. if (!mtd->_writev)
  1487. return default_mtd_writev(mtd, vecs, count, to, retlen);
  1488. return mtd->_writev(mtd, vecs, count, to, retlen);
  1489. }
  1490. EXPORT_SYMBOL_GPL(mtd_writev);
  1491. /**
  1492. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  1493. * @mtd: mtd device description object pointer
  1494. * @size: a pointer to the ideal or maximum size of the allocation, points
  1495. * to the actual allocation size on success.
  1496. *
  1497. * This routine attempts to allocate a contiguous kernel buffer up to
  1498. * the specified size, backing off the size of the request exponentially
  1499. * until the request succeeds or until the allocation size falls below
  1500. * the system page size. This attempts to make sure it does not adversely
  1501. * impact system performance, so when allocating more than one page, we
  1502. * ask the memory allocator to avoid re-trying, swapping, writing back
  1503. * or performing I/O.
  1504. *
  1505. * Note, this function also makes sure that the allocated buffer is aligned to
  1506. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  1507. *
  1508. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  1509. * to handle smaller (i.e. degraded) buffer allocations under low- or
  1510. * fragmented-memory situations where such reduced allocations, from a
  1511. * requested ideal, are allowed.
  1512. *
  1513. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  1514. */
  1515. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  1516. {
  1517. gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
  1518. __GFP_NORETRY | __GFP_NO_KSWAPD;
  1519. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  1520. void *kbuf;
  1521. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  1522. while (*size > min_alloc) {
  1523. kbuf = kmalloc(*size, flags);
  1524. if (kbuf)
  1525. return kbuf;
  1526. *size >>= 1;
  1527. *size = ALIGN(*size, mtd->writesize);
  1528. }
  1529. /*
  1530. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  1531. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  1532. */
  1533. return kmalloc(*size, GFP_KERNEL);
  1534. }
  1535. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  1536. #endif
  1537. #ifdef CONFIG_PROC_FS
  1538. /*====================================================================*/
  1539. /* Support for /proc/mtd */
  1540. static int mtd_proc_show(struct seq_file *m, void *v)
  1541. {
  1542. struct mtd_info *mtd;
  1543. seq_puts(m, "dev: size erasesize name\n");
  1544. mutex_lock(&mtd_table_mutex);
  1545. mtd_for_each_device(mtd) {
  1546. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  1547. mtd->index, (unsigned long long)mtd->size,
  1548. mtd->erasesize, mtd->name);
  1549. }
  1550. mutex_unlock(&mtd_table_mutex);
  1551. return 0;
  1552. }
  1553. static int mtd_proc_open(struct inode *inode, struct file *file)
  1554. {
  1555. return single_open(file, mtd_proc_show, NULL);
  1556. }
  1557. static const struct file_operations mtd_proc_ops = {
  1558. .open = mtd_proc_open,
  1559. .read = seq_read,
  1560. .llseek = seq_lseek,
  1561. .release = single_release,
  1562. };
  1563. #endif /* CONFIG_PROC_FS */
  1564. /*====================================================================*/
  1565. /* Init code */
  1566. #ifndef __UBOOT__
  1567. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  1568. {
  1569. int ret;
  1570. ret = bdi_init(bdi);
  1571. if (!ret)
  1572. ret = bdi_register(bdi, NULL, "%s", name);
  1573. if (ret)
  1574. bdi_destroy(bdi);
  1575. return ret;
  1576. }
  1577. static struct proc_dir_entry *proc_mtd;
  1578. static int __init init_mtd(void)
  1579. {
  1580. int ret;
  1581. ret = class_register(&mtd_class);
  1582. if (ret)
  1583. goto err_reg;
  1584. ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
  1585. if (ret)
  1586. goto err_bdi1;
  1587. ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
  1588. if (ret)
  1589. goto err_bdi2;
  1590. ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
  1591. if (ret)
  1592. goto err_bdi3;
  1593. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  1594. ret = init_mtdchar();
  1595. if (ret)
  1596. goto out_procfs;
  1597. return 0;
  1598. out_procfs:
  1599. if (proc_mtd)
  1600. remove_proc_entry("mtd", NULL);
  1601. err_bdi3:
  1602. bdi_destroy(&mtd_bdi_ro_mappable);
  1603. err_bdi2:
  1604. bdi_destroy(&mtd_bdi_unmappable);
  1605. err_bdi1:
  1606. class_unregister(&mtd_class);
  1607. err_reg:
  1608. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1609. return ret;
  1610. }
  1611. static void __exit cleanup_mtd(void)
  1612. {
  1613. cleanup_mtdchar();
  1614. if (proc_mtd)
  1615. remove_proc_entry("mtd", NULL);
  1616. class_unregister(&mtd_class);
  1617. bdi_destroy(&mtd_bdi_unmappable);
  1618. bdi_destroy(&mtd_bdi_ro_mappable);
  1619. bdi_destroy(&mtd_bdi_rw_mappable);
  1620. }
  1621. module_init(init_mtd);
  1622. module_exit(cleanup_mtd);
  1623. #endif
  1624. MODULE_LICENSE("GPL");
  1625. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1626. MODULE_DESCRIPTION("Core MTD registration and access routines");