mtdcore.c 50 KB

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