mtdcore.c 50 KB

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