mtdcore.c 51 KB

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