mtdcore.c 50 KB

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