mtdcore.c 51 KB

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