mtdcore.c 51 KB

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