mtdcore.c 50 KB

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