mtdcore.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  758. {
  759. if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
  760. return -EINVAL;
  761. if (!(mtd->flags & MTD_WRITEABLE))
  762. return -EROFS;
  763. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  764. if (!instr->len) {
  765. instr->state = MTD_ERASE_DONE;
  766. return 0;
  767. }
  768. return mtd->_erase(mtd, instr);
  769. }
  770. EXPORT_SYMBOL_GPL(mtd_erase);
  771. #ifndef __UBOOT__
  772. /*
  773. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  774. */
  775. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  776. void **virt, resource_size_t *phys)
  777. {
  778. *retlen = 0;
  779. *virt = NULL;
  780. if (phys)
  781. *phys = 0;
  782. if (!mtd->_point)
  783. return -EOPNOTSUPP;
  784. if (from < 0 || from > mtd->size || len > mtd->size - from)
  785. return -EINVAL;
  786. if (!len)
  787. return 0;
  788. return mtd->_point(mtd, from, len, retlen, virt, phys);
  789. }
  790. EXPORT_SYMBOL_GPL(mtd_point);
  791. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  792. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  793. {
  794. if (!mtd->_point)
  795. return -EOPNOTSUPP;
  796. if (from < 0 || from > mtd->size || len > mtd->size - from)
  797. return -EINVAL;
  798. if (!len)
  799. return 0;
  800. return mtd->_unpoint(mtd, from, len);
  801. }
  802. EXPORT_SYMBOL_GPL(mtd_unpoint);
  803. #endif
  804. /*
  805. * Allow NOMMU mmap() to directly map the device (if not NULL)
  806. * - return the address to which the offset maps
  807. * - return -ENOSYS to indicate refusal to do the mapping
  808. */
  809. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  810. unsigned long offset, unsigned long flags)
  811. {
  812. if (!mtd->_get_unmapped_area)
  813. return -EOPNOTSUPP;
  814. if (offset > mtd->size || len > mtd->size - offset)
  815. return -EINVAL;
  816. return mtd->_get_unmapped_area(mtd, len, offset, flags);
  817. }
  818. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  819. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  820. u_char *buf)
  821. {
  822. int ret_code;
  823. *retlen = 0;
  824. if (from < 0 || from > mtd->size || len > mtd->size - from)
  825. return -EINVAL;
  826. if (!len)
  827. return 0;
  828. /*
  829. * In the absence of an error, drivers return a non-negative integer
  830. * representing the maximum number of bitflips that were corrected on
  831. * any one ecc region (if applicable; zero otherwise).
  832. */
  833. if (mtd->_read) {
  834. ret_code = mtd->_read(mtd, from, len, retlen, buf);
  835. } else if (mtd->_read_oob) {
  836. struct mtd_oob_ops ops = {
  837. .len = len,
  838. .datbuf = buf,
  839. };
  840. ret_code = mtd->_read_oob(mtd, from, &ops);
  841. *retlen = ops.retlen;
  842. } else {
  843. return -ENOTSUPP;
  844. }
  845. if (unlikely(ret_code < 0))
  846. return ret_code;
  847. if (mtd->ecc_strength == 0)
  848. return 0; /* device lacks ecc */
  849. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  850. }
  851. EXPORT_SYMBOL_GPL(mtd_read);
  852. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  853. const u_char *buf)
  854. {
  855. *retlen = 0;
  856. if (to < 0 || to > mtd->size || len > mtd->size - to)
  857. return -EINVAL;
  858. if ((!mtd->_write && !mtd->_write_oob) ||
  859. !(mtd->flags & MTD_WRITEABLE))
  860. return -EROFS;
  861. if (!len)
  862. return 0;
  863. if (!mtd->_write) {
  864. struct mtd_oob_ops ops = {
  865. .len = len,
  866. .datbuf = (u8 *)buf,
  867. };
  868. int ret;
  869. ret = mtd->_write_oob(mtd, to, &ops);
  870. *retlen = ops.retlen;
  871. return ret;
  872. }
  873. return mtd->_write(mtd, to, len, retlen, buf);
  874. }
  875. EXPORT_SYMBOL_GPL(mtd_write);
  876. /*
  877. * In blackbox flight recorder like scenarios we want to make successful writes
  878. * in interrupt context. panic_write() is only intended to be called when its
  879. * known the kernel is about to panic and we need the write to succeed. Since
  880. * the kernel is not going to be running for much longer, this function can
  881. * break locks and delay to ensure the write succeeds (but not sleep).
  882. */
  883. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  884. const u_char *buf)
  885. {
  886. *retlen = 0;
  887. if (!mtd->_panic_write)
  888. return -EOPNOTSUPP;
  889. if (to < 0 || to > mtd->size || len > mtd->size - to)
  890. return -EINVAL;
  891. if (!(mtd->flags & MTD_WRITEABLE))
  892. return -EROFS;
  893. if (!len)
  894. return 0;
  895. return mtd->_panic_write(mtd, to, len, retlen, buf);
  896. }
  897. EXPORT_SYMBOL_GPL(mtd_panic_write);
  898. static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
  899. struct mtd_oob_ops *ops)
  900. {
  901. /*
  902. * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
  903. * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
  904. * this case.
  905. */
  906. if (!ops->datbuf)
  907. ops->len = 0;
  908. if (!ops->oobbuf)
  909. ops->ooblen = 0;
  910. if (offs < 0 || offs + ops->len > mtd->size)
  911. return -EINVAL;
  912. if (ops->ooblen) {
  913. size_t maxooblen;
  914. if (ops->ooboffs >= mtd_oobavail(mtd, ops))
  915. return -EINVAL;
  916. maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
  917. mtd_div_by_ws(offs, mtd)) *
  918. mtd_oobavail(mtd, ops)) - ops->ooboffs;
  919. if (ops->ooblen > maxooblen)
  920. return -EINVAL;
  921. }
  922. return 0;
  923. }
  924. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  925. {
  926. int ret_code;
  927. ops->retlen = ops->oobretlen = 0;
  928. ret_code = mtd_check_oob_ops(mtd, from, ops);
  929. if (ret_code)
  930. return ret_code;
  931. /* Check the validity of a potential fallback on mtd->_read */
  932. if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
  933. return -EOPNOTSUPP;
  934. if (mtd->_read_oob)
  935. ret_code = mtd->_read_oob(mtd, from, ops);
  936. else
  937. ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
  938. ops->datbuf);
  939. /*
  940. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  941. * similar to mtd->_read(), returning a non-negative integer
  942. * representing max bitflips. In other cases, mtd->_read_oob() may
  943. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  944. */
  945. if (unlikely(ret_code < 0))
  946. return ret_code;
  947. if (mtd->ecc_strength == 0)
  948. return 0; /* device lacks ecc */
  949. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  950. }
  951. EXPORT_SYMBOL_GPL(mtd_read_oob);
  952. int mtd_write_oob(struct mtd_info *mtd, loff_t to,
  953. struct mtd_oob_ops *ops)
  954. {
  955. int ret;
  956. ops->retlen = ops->oobretlen = 0;
  957. if (!(mtd->flags & MTD_WRITEABLE))
  958. return -EROFS;
  959. ret = mtd_check_oob_ops(mtd, to, ops);
  960. if (ret)
  961. return ret;
  962. /* Check the validity of a potential fallback on mtd->_write */
  963. if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
  964. return -EOPNOTSUPP;
  965. if (mtd->_write_oob)
  966. return mtd->_write_oob(mtd, to, ops);
  967. else
  968. return mtd->_write(mtd, to, ops->len, &ops->retlen,
  969. ops->datbuf);
  970. }
  971. EXPORT_SYMBOL_GPL(mtd_write_oob);
  972. /**
  973. * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
  974. * @mtd: MTD device structure
  975. * @section: ECC section. Depending on the layout you may have all the ECC
  976. * bytes stored in a single contiguous section, or one section
  977. * per ECC chunk (and sometime several sections for a single ECC
  978. * ECC chunk)
  979. * @oobecc: OOB region struct filled with the appropriate ECC position
  980. * information
  981. *
  982. * This function returns ECC section information in the OOB area. If you want
  983. * to get all the ECC bytes information, then you should call
  984. * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
  985. *
  986. * Returns zero on success, a negative error code otherwise.
  987. */
  988. int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  989. struct mtd_oob_region *oobecc)
  990. {
  991. memset(oobecc, 0, sizeof(*oobecc));
  992. if (!mtd || section < 0)
  993. return -EINVAL;
  994. if (!mtd->ooblayout || !mtd->ooblayout->ecc)
  995. return -ENOTSUPP;
  996. return mtd->ooblayout->ecc(mtd, section, oobecc);
  997. }
  998. EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
  999. /**
  1000. * mtd_ooblayout_free - Get the OOB region definition of a specific free
  1001. * section
  1002. * @mtd: MTD device structure
  1003. * @section: Free section you are interested in. Depending on the layout
  1004. * you may have all the free bytes stored in a single contiguous
  1005. * section, or one section per ECC chunk plus an extra section
  1006. * for the remaining bytes (or other funky layout).
  1007. * @oobfree: OOB region struct filled with the appropriate free position
  1008. * information
  1009. *
  1010. * This function returns free bytes position in the OOB area. If you want
  1011. * to get all the free bytes information, then you should call
  1012. * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
  1013. *
  1014. * Returns zero on success, a negative error code otherwise.
  1015. */
  1016. int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  1017. struct mtd_oob_region *oobfree)
  1018. {
  1019. memset(oobfree, 0, sizeof(*oobfree));
  1020. if (!mtd || section < 0)
  1021. return -EINVAL;
  1022. if (!mtd->ooblayout || !mtd->ooblayout->rfree)
  1023. return -ENOTSUPP;
  1024. return mtd->ooblayout->rfree(mtd, section, oobfree);
  1025. }
  1026. EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
  1027. /**
  1028. * mtd_ooblayout_find_region - Find the region attached to a specific byte
  1029. * @mtd: mtd info structure
  1030. * @byte: the byte we are searching for
  1031. * @sectionp: pointer where the section id will be stored
  1032. * @oobregion: used to retrieve the ECC position
  1033. * @iter: iterator function. Should be either mtd_ooblayout_free or
  1034. * mtd_ooblayout_ecc depending on the region type you're searching for
  1035. *
  1036. * This function returns the section id and oobregion information of a
  1037. * specific byte. For example, say you want to know where the 4th ECC byte is
  1038. * stored, you'll use:
  1039. *
  1040. * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
  1041. *
  1042. * Returns zero on success, a negative error code otherwise.
  1043. */
  1044. static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
  1045. int *sectionp, struct mtd_oob_region *oobregion,
  1046. int (*iter)(struct mtd_info *,
  1047. int section,
  1048. struct mtd_oob_region *oobregion))
  1049. {
  1050. int pos = 0, ret, section = 0;
  1051. memset(oobregion, 0, sizeof(*oobregion));
  1052. while (1) {
  1053. ret = iter(mtd, section, oobregion);
  1054. if (ret)
  1055. return ret;
  1056. if (pos + oobregion->length > byte)
  1057. break;
  1058. pos += oobregion->length;
  1059. section++;
  1060. }
  1061. /*
  1062. * Adjust region info to make it start at the beginning at the
  1063. * 'start' ECC byte.
  1064. */
  1065. oobregion->offset += byte - pos;
  1066. oobregion->length -= byte - pos;
  1067. *sectionp = section;
  1068. return 0;
  1069. }
  1070. /**
  1071. * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
  1072. * ECC byte
  1073. * @mtd: mtd info structure
  1074. * @eccbyte: the byte we are searching for
  1075. * @sectionp: pointer where the section id will be stored
  1076. * @oobregion: OOB region information
  1077. *
  1078. * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
  1079. * byte.
  1080. *
  1081. * Returns zero on success, a negative error code otherwise.
  1082. */
  1083. int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  1084. int *section,
  1085. struct mtd_oob_region *oobregion)
  1086. {
  1087. return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
  1088. mtd_ooblayout_ecc);
  1089. }
  1090. EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
  1091. /**
  1092. * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
  1093. * @mtd: mtd info structure
  1094. * @buf: destination buffer to store OOB bytes
  1095. * @oobbuf: OOB buffer
  1096. * @start: first byte to retrieve
  1097. * @nbytes: number of bytes to retrieve
  1098. * @iter: section iterator
  1099. *
  1100. * Extract bytes attached to a specific category (ECC or free)
  1101. * from the OOB buffer and copy them into buf.
  1102. *
  1103. * Returns zero on success, a negative error code otherwise.
  1104. */
  1105. static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
  1106. const u8 *oobbuf, int start, int nbytes,
  1107. int (*iter)(struct mtd_info *,
  1108. int section,
  1109. struct mtd_oob_region *oobregion))
  1110. {
  1111. struct mtd_oob_region oobregion;
  1112. int section, ret;
  1113. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1114. &oobregion, iter);
  1115. while (!ret) {
  1116. int cnt;
  1117. cnt = min_t(int, nbytes, oobregion.length);
  1118. memcpy(buf, oobbuf + oobregion.offset, cnt);
  1119. buf += cnt;
  1120. nbytes -= cnt;
  1121. if (!nbytes)
  1122. break;
  1123. ret = iter(mtd, ++section, &oobregion);
  1124. }
  1125. return ret;
  1126. }
  1127. /**
  1128. * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
  1129. * @mtd: mtd info structure
  1130. * @buf: source buffer to get OOB bytes from
  1131. * @oobbuf: OOB buffer
  1132. * @start: first OOB byte to set
  1133. * @nbytes: number of OOB bytes to set
  1134. * @iter: section iterator
  1135. *
  1136. * Fill the OOB buffer with data provided in buf. The category (ECC or free)
  1137. * is selected by passing the appropriate iterator.
  1138. *
  1139. * Returns zero on success, a negative error code otherwise.
  1140. */
  1141. static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
  1142. u8 *oobbuf, int start, int nbytes,
  1143. int (*iter)(struct mtd_info *,
  1144. int section,
  1145. struct mtd_oob_region *oobregion))
  1146. {
  1147. struct mtd_oob_region oobregion;
  1148. int section, ret;
  1149. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1150. &oobregion, iter);
  1151. while (!ret) {
  1152. int cnt;
  1153. cnt = min_t(int, nbytes, oobregion.length);
  1154. memcpy(oobbuf + oobregion.offset, buf, cnt);
  1155. buf += cnt;
  1156. nbytes -= cnt;
  1157. if (!nbytes)
  1158. break;
  1159. ret = iter(mtd, ++section, &oobregion);
  1160. }
  1161. return ret;
  1162. }
  1163. /**
  1164. * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
  1165. * @mtd: mtd info structure
  1166. * @iter: category iterator
  1167. *
  1168. * Count the number of bytes in a given category.
  1169. *
  1170. * Returns a positive value on success, a negative error code otherwise.
  1171. */
  1172. static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
  1173. int (*iter)(struct mtd_info *,
  1174. int section,
  1175. struct mtd_oob_region *oobregion))
  1176. {
  1177. struct mtd_oob_region oobregion;
  1178. int section = 0, ret, nbytes = 0;
  1179. while (1) {
  1180. ret = iter(mtd, section++, &oobregion);
  1181. if (ret) {
  1182. if (ret == -ERANGE)
  1183. ret = nbytes;
  1184. break;
  1185. }
  1186. nbytes += oobregion.length;
  1187. }
  1188. return ret;
  1189. }
  1190. /**
  1191. * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
  1192. * @mtd: mtd info structure
  1193. * @eccbuf: destination buffer to store ECC bytes
  1194. * @oobbuf: OOB buffer
  1195. * @start: first ECC byte to retrieve
  1196. * @nbytes: number of ECC bytes to retrieve
  1197. *
  1198. * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
  1199. *
  1200. * Returns zero on success, a negative error code otherwise.
  1201. */
  1202. int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  1203. const u8 *oobbuf, int start, int nbytes)
  1204. {
  1205. return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1206. mtd_ooblayout_ecc);
  1207. }
  1208. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
  1209. /**
  1210. * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
  1211. * @mtd: mtd info structure
  1212. * @eccbuf: source buffer to get ECC bytes from
  1213. * @oobbuf: OOB buffer
  1214. * @start: first ECC byte to set
  1215. * @nbytes: number of ECC bytes to set
  1216. *
  1217. * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
  1218. *
  1219. * Returns zero on success, a negative error code otherwise.
  1220. */
  1221. int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  1222. u8 *oobbuf, int start, int nbytes)
  1223. {
  1224. return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1225. mtd_ooblayout_ecc);
  1226. }
  1227. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
  1228. /**
  1229. * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
  1230. * @mtd: mtd info structure
  1231. * @databuf: destination buffer to store ECC bytes
  1232. * @oobbuf: OOB buffer
  1233. * @start: first ECC byte to retrieve
  1234. * @nbytes: number of ECC bytes to retrieve
  1235. *
  1236. * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  1237. *
  1238. * Returns zero on success, a negative error code otherwise.
  1239. */
  1240. int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  1241. const u8 *oobbuf, int start, int nbytes)
  1242. {
  1243. return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
  1244. mtd_ooblayout_free);
  1245. }
  1246. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
  1247. /**
  1248. * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
  1249. * @mtd: mtd info structure
  1250. * @eccbuf: source buffer to get data bytes from
  1251. * @oobbuf: OOB buffer
  1252. * @start: first ECC byte to set
  1253. * @nbytes: number of ECC bytes to set
  1254. *
  1255. * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  1256. *
  1257. * Returns zero on success, a negative error code otherwise.
  1258. */
  1259. int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  1260. u8 *oobbuf, int start, int nbytes)
  1261. {
  1262. return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
  1263. mtd_ooblayout_free);
  1264. }
  1265. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
  1266. /**
  1267. * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
  1268. * @mtd: mtd info structure
  1269. *
  1270. * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
  1271. *
  1272. * Returns zero on success, a negative error code otherwise.
  1273. */
  1274. int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
  1275. {
  1276. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
  1277. }
  1278. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
  1279. /**
  1280. * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
  1281. * @mtd: mtd info structure
  1282. *
  1283. * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
  1284. *
  1285. * Returns zero on success, a negative error code otherwise.
  1286. */
  1287. int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
  1288. {
  1289. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
  1290. }
  1291. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
  1292. /*
  1293. * Method to access the protection register area, present in some flash
  1294. * devices. The user data is one time programmable but the factory data is read
  1295. * only.
  1296. */
  1297. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1298. struct otp_info *buf)
  1299. {
  1300. if (!mtd->_get_fact_prot_info)
  1301. return -EOPNOTSUPP;
  1302. if (!len)
  1303. return 0;
  1304. return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
  1305. }
  1306. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  1307. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1308. size_t *retlen, u_char *buf)
  1309. {
  1310. *retlen = 0;
  1311. if (!mtd->_read_fact_prot_reg)
  1312. return -EOPNOTSUPP;
  1313. if (!len)
  1314. return 0;
  1315. return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
  1316. }
  1317. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  1318. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1319. struct otp_info *buf)
  1320. {
  1321. if (!mtd->_get_user_prot_info)
  1322. return -EOPNOTSUPP;
  1323. if (!len)
  1324. return 0;
  1325. return mtd->_get_user_prot_info(mtd, len, retlen, buf);
  1326. }
  1327. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  1328. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1329. size_t *retlen, u_char *buf)
  1330. {
  1331. *retlen = 0;
  1332. if (!mtd->_read_user_prot_reg)
  1333. return -EOPNOTSUPP;
  1334. if (!len)
  1335. return 0;
  1336. return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
  1337. }
  1338. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  1339. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  1340. size_t *retlen, u_char *buf)
  1341. {
  1342. int ret;
  1343. *retlen = 0;
  1344. if (!mtd->_write_user_prot_reg)
  1345. return -EOPNOTSUPP;
  1346. if (!len)
  1347. return 0;
  1348. ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
  1349. if (ret)
  1350. return ret;
  1351. /*
  1352. * If no data could be written at all, we are out of memory and
  1353. * must return -ENOSPC.
  1354. */
  1355. return (*retlen) ? 0 : -ENOSPC;
  1356. }
  1357. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  1358. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  1359. {
  1360. if (!mtd->_lock_user_prot_reg)
  1361. return -EOPNOTSUPP;
  1362. if (!len)
  1363. return 0;
  1364. return mtd->_lock_user_prot_reg(mtd, from, len);
  1365. }
  1366. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  1367. /* Chip-supported device locking */
  1368. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1369. {
  1370. if (!mtd->_lock)
  1371. return -EOPNOTSUPP;
  1372. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1373. return -EINVAL;
  1374. if (!len)
  1375. return 0;
  1376. return mtd->_lock(mtd, ofs, len);
  1377. }
  1378. EXPORT_SYMBOL_GPL(mtd_lock);
  1379. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1380. {
  1381. if (!mtd->_unlock)
  1382. return -EOPNOTSUPP;
  1383. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1384. return -EINVAL;
  1385. if (!len)
  1386. return 0;
  1387. return mtd->_unlock(mtd, ofs, len);
  1388. }
  1389. EXPORT_SYMBOL_GPL(mtd_unlock);
  1390. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1391. {
  1392. if (!mtd->_is_locked)
  1393. return -EOPNOTSUPP;
  1394. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1395. return -EINVAL;
  1396. if (!len)
  1397. return 0;
  1398. return mtd->_is_locked(mtd, ofs, len);
  1399. }
  1400. EXPORT_SYMBOL_GPL(mtd_is_locked);
  1401. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  1402. {
  1403. if (ofs < 0 || ofs > mtd->size)
  1404. return -EINVAL;
  1405. if (!mtd->_block_isreserved)
  1406. return 0;
  1407. return mtd->_block_isreserved(mtd, ofs);
  1408. }
  1409. EXPORT_SYMBOL_GPL(mtd_block_isreserved);
  1410. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1411. {
  1412. if (ofs < 0 || ofs > mtd->size)
  1413. return -EINVAL;
  1414. if (!mtd->_block_isbad)
  1415. return 0;
  1416. return mtd->_block_isbad(mtd, ofs);
  1417. }
  1418. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  1419. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1420. {
  1421. if (!mtd->_block_markbad)
  1422. return -EOPNOTSUPP;
  1423. if (ofs < 0 || ofs > mtd->size)
  1424. return -EINVAL;
  1425. if (!(mtd->flags & MTD_WRITEABLE))
  1426. return -EROFS;
  1427. return mtd->_block_markbad(mtd, ofs);
  1428. }
  1429. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  1430. #ifndef __UBOOT__
  1431. /*
  1432. * default_mtd_writev - the default writev method
  1433. * @mtd: mtd device description object pointer
  1434. * @vecs: the vectors to write
  1435. * @count: count of vectors in @vecs
  1436. * @to: the MTD device offset to write to
  1437. * @retlen: on exit contains the count of bytes written to the MTD device.
  1438. *
  1439. * This function returns zero in case of success and a negative error code in
  1440. * case of failure.
  1441. */
  1442. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1443. unsigned long count, loff_t to, size_t *retlen)
  1444. {
  1445. unsigned long i;
  1446. size_t totlen = 0, thislen;
  1447. int ret = 0;
  1448. for (i = 0; i < count; i++) {
  1449. if (!vecs[i].iov_len)
  1450. continue;
  1451. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  1452. vecs[i].iov_base);
  1453. totlen += thislen;
  1454. if (ret || thislen != vecs[i].iov_len)
  1455. break;
  1456. to += vecs[i].iov_len;
  1457. }
  1458. *retlen = totlen;
  1459. return ret;
  1460. }
  1461. /*
  1462. * mtd_writev - the vector-based MTD write method
  1463. * @mtd: mtd device description object pointer
  1464. * @vecs: the vectors to write
  1465. * @count: count of vectors in @vecs
  1466. * @to: the MTD device offset to write to
  1467. * @retlen: on exit contains the count of bytes written to the MTD device.
  1468. *
  1469. * This function returns zero in case of success and a negative error code in
  1470. * case of failure.
  1471. */
  1472. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1473. unsigned long count, loff_t to, size_t *retlen)
  1474. {
  1475. *retlen = 0;
  1476. if (!(mtd->flags & MTD_WRITEABLE))
  1477. return -EROFS;
  1478. if (!mtd->_writev)
  1479. return default_mtd_writev(mtd, vecs, count, to, retlen);
  1480. return mtd->_writev(mtd, vecs, count, to, retlen);
  1481. }
  1482. EXPORT_SYMBOL_GPL(mtd_writev);
  1483. /**
  1484. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  1485. * @mtd: mtd device description object pointer
  1486. * @size: a pointer to the ideal or maximum size of the allocation, points
  1487. * to the actual allocation size on success.
  1488. *
  1489. * This routine attempts to allocate a contiguous kernel buffer up to
  1490. * the specified size, backing off the size of the request exponentially
  1491. * until the request succeeds or until the allocation size falls below
  1492. * the system page size. This attempts to make sure it does not adversely
  1493. * impact system performance, so when allocating more than one page, we
  1494. * ask the memory allocator to avoid re-trying, swapping, writing back
  1495. * or performing I/O.
  1496. *
  1497. * Note, this function also makes sure that the allocated buffer is aligned to
  1498. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  1499. *
  1500. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  1501. * to handle smaller (i.e. degraded) buffer allocations under low- or
  1502. * fragmented-memory situations where such reduced allocations, from a
  1503. * requested ideal, are allowed.
  1504. *
  1505. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  1506. */
  1507. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  1508. {
  1509. gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
  1510. __GFP_NORETRY | __GFP_NO_KSWAPD;
  1511. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  1512. void *kbuf;
  1513. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  1514. while (*size > min_alloc) {
  1515. kbuf = kmalloc(*size, flags);
  1516. if (kbuf)
  1517. return kbuf;
  1518. *size >>= 1;
  1519. *size = ALIGN(*size, mtd->writesize);
  1520. }
  1521. /*
  1522. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  1523. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  1524. */
  1525. return kmalloc(*size, GFP_KERNEL);
  1526. }
  1527. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  1528. #endif
  1529. #ifdef CONFIG_PROC_FS
  1530. /*====================================================================*/
  1531. /* Support for /proc/mtd */
  1532. static int mtd_proc_show(struct seq_file *m, void *v)
  1533. {
  1534. struct mtd_info *mtd;
  1535. seq_puts(m, "dev: size erasesize name\n");
  1536. mutex_lock(&mtd_table_mutex);
  1537. mtd_for_each_device(mtd) {
  1538. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  1539. mtd->index, (unsigned long long)mtd->size,
  1540. mtd->erasesize, mtd->name);
  1541. }
  1542. mutex_unlock(&mtd_table_mutex);
  1543. return 0;
  1544. }
  1545. static int mtd_proc_open(struct inode *inode, struct file *file)
  1546. {
  1547. return single_open(file, mtd_proc_show, NULL);
  1548. }
  1549. static const struct file_operations mtd_proc_ops = {
  1550. .open = mtd_proc_open,
  1551. .read = seq_read,
  1552. .llseek = seq_lseek,
  1553. .release = single_release,
  1554. };
  1555. #endif /* CONFIG_PROC_FS */
  1556. /*====================================================================*/
  1557. /* Init code */
  1558. #ifndef __UBOOT__
  1559. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  1560. {
  1561. int ret;
  1562. ret = bdi_init(bdi);
  1563. if (!ret)
  1564. ret = bdi_register(bdi, NULL, "%s", name);
  1565. if (ret)
  1566. bdi_destroy(bdi);
  1567. return ret;
  1568. }
  1569. static struct proc_dir_entry *proc_mtd;
  1570. static int __init init_mtd(void)
  1571. {
  1572. int ret;
  1573. ret = class_register(&mtd_class);
  1574. if (ret)
  1575. goto err_reg;
  1576. ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
  1577. if (ret)
  1578. goto err_bdi1;
  1579. ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
  1580. if (ret)
  1581. goto err_bdi2;
  1582. ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
  1583. if (ret)
  1584. goto err_bdi3;
  1585. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  1586. ret = init_mtdchar();
  1587. if (ret)
  1588. goto out_procfs;
  1589. return 0;
  1590. out_procfs:
  1591. if (proc_mtd)
  1592. remove_proc_entry("mtd", NULL);
  1593. err_bdi3:
  1594. bdi_destroy(&mtd_bdi_ro_mappable);
  1595. err_bdi2:
  1596. bdi_destroy(&mtd_bdi_unmappable);
  1597. err_bdi1:
  1598. class_unregister(&mtd_class);
  1599. err_reg:
  1600. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1601. return ret;
  1602. }
  1603. static void __exit cleanup_mtd(void)
  1604. {
  1605. cleanup_mtdchar();
  1606. if (proc_mtd)
  1607. remove_proc_entry("mtd", NULL);
  1608. class_unregister(&mtd_class);
  1609. bdi_destroy(&mtd_bdi_unmappable);
  1610. bdi_destroy(&mtd_bdi_ro_mappable);
  1611. bdi_destroy(&mtd_bdi_rw_mappable);
  1612. }
  1613. module_init(init_mtd);
  1614. module_exit(cleanup_mtd);
  1615. #endif
  1616. MODULE_LICENSE("GPL");
  1617. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1618. MODULE_DESCRIPTION("Core MTD registration and access routines");