mtdcore.c 51 KB

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