mtdcore.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Core registration and callback routines for MTD
  4. * drivers and users.
  5. *
  6. * Copyright © 1999-2010 David Woodhouse <[email protected]>
  7. * Copyright © 2006 Red Hat UK Limited
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/string.h>
  14. #include <linux/timer.h>
  15. #include <linux/major.h>
  16. #include <linux/fs.h>
  17. #include <linux/err.h>
  18. #include <linux/ioctl.h>
  19. #include <linux/init.h>
  20. #include <linux/of.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/idr.h>
  23. #include <linux/backing-dev.h>
  24. #include <linux/gfp.h>
  25. #include <linux/slab.h>
  26. #include <linux/reboot.h>
  27. #include <linux/leds.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/nvmem-provider.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/partitions.h>
  32. #include "mtdcore.h"
  33. struct backing_dev_info *mtd_bdi;
  34. #ifdef CONFIG_PM_SLEEP
  35. static int mtd_cls_suspend(struct device *dev)
  36. {
  37. struct mtd_info *mtd = dev_get_drvdata(dev);
  38. return mtd ? mtd_suspend(mtd) : 0;
  39. }
  40. static int mtd_cls_resume(struct device *dev)
  41. {
  42. struct mtd_info *mtd = dev_get_drvdata(dev);
  43. if (mtd)
  44. mtd_resume(mtd);
  45. return 0;
  46. }
  47. static SIMPLE_DEV_PM_OPS(mtd_cls_pm_ops, mtd_cls_suspend, mtd_cls_resume);
  48. #define MTD_CLS_PM_OPS (&mtd_cls_pm_ops)
  49. #else
  50. #define MTD_CLS_PM_OPS NULL
  51. #endif
  52. static struct class mtd_class = {
  53. .name = "mtd",
  54. .owner = THIS_MODULE,
  55. .pm = MTD_CLS_PM_OPS,
  56. };
  57. static DEFINE_IDR(mtd_idr);
  58. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  59. should not use them for _anything_ else */
  60. DEFINE_MUTEX(mtd_table_mutex);
  61. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  62. struct mtd_info *__mtd_next_device(int i)
  63. {
  64. return idr_get_next(&mtd_idr, &i);
  65. }
  66. EXPORT_SYMBOL_GPL(__mtd_next_device);
  67. static LIST_HEAD(mtd_notifiers);
  68. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  69. /* REVISIT once MTD uses the driver model better, whoever allocates
  70. * the mtd_info will probably want to use the release() hook...
  71. */
  72. static void mtd_release(struct device *dev)
  73. {
  74. struct mtd_info *mtd = dev_get_drvdata(dev);
  75. dev_t index = MTD_DEVT(mtd->index);
  76. /* remove /dev/mtdXro node */
  77. device_destroy(&mtd_class, index + 1);
  78. }
  79. #define MTD_DEVICE_ATTR_RO(name) \
  80. static DEVICE_ATTR(name, 0444, mtd_##name##_show, NULL)
  81. #define MTD_DEVICE_ATTR_RW(name) \
  82. static DEVICE_ATTR(name, 0644, mtd_##name##_show, mtd_##name##_store)
  83. static ssize_t mtd_type_show(struct device *dev,
  84. struct device_attribute *attr, char *buf)
  85. {
  86. struct mtd_info *mtd = dev_get_drvdata(dev);
  87. char *type;
  88. switch (mtd->type) {
  89. case MTD_ABSENT:
  90. type = "absent";
  91. break;
  92. case MTD_RAM:
  93. type = "ram";
  94. break;
  95. case MTD_ROM:
  96. type = "rom";
  97. break;
  98. case MTD_NORFLASH:
  99. type = "nor";
  100. break;
  101. case MTD_NANDFLASH:
  102. type = "nand";
  103. break;
  104. case MTD_DATAFLASH:
  105. type = "dataflash";
  106. break;
  107. case MTD_UBIVOLUME:
  108. type = "ubi";
  109. break;
  110. case MTD_MLCNANDFLASH:
  111. type = "mlc-nand";
  112. break;
  113. default:
  114. type = "unknown";
  115. }
  116. return sysfs_emit(buf, "%s\n", type);
  117. }
  118. MTD_DEVICE_ATTR_RO(type);
  119. static ssize_t mtd_flags_show(struct device *dev,
  120. struct device_attribute *attr, char *buf)
  121. {
  122. struct mtd_info *mtd = dev_get_drvdata(dev);
  123. return sysfs_emit(buf, "0x%lx\n", (unsigned long)mtd->flags);
  124. }
  125. MTD_DEVICE_ATTR_RO(flags);
  126. static ssize_t mtd_size_show(struct device *dev,
  127. struct device_attribute *attr, char *buf)
  128. {
  129. struct mtd_info *mtd = dev_get_drvdata(dev);
  130. return sysfs_emit(buf, "%llu\n", (unsigned long long)mtd->size);
  131. }
  132. MTD_DEVICE_ATTR_RO(size);
  133. static ssize_t mtd_erasesize_show(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. struct mtd_info *mtd = dev_get_drvdata(dev);
  137. return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->erasesize);
  138. }
  139. MTD_DEVICE_ATTR_RO(erasesize);
  140. static ssize_t mtd_writesize_show(struct device *dev,
  141. struct device_attribute *attr, char *buf)
  142. {
  143. struct mtd_info *mtd = dev_get_drvdata(dev);
  144. return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->writesize);
  145. }
  146. MTD_DEVICE_ATTR_RO(writesize);
  147. static ssize_t mtd_subpagesize_show(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct mtd_info *mtd = dev_get_drvdata(dev);
  151. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  152. return sysfs_emit(buf, "%u\n", subpagesize);
  153. }
  154. MTD_DEVICE_ATTR_RO(subpagesize);
  155. static ssize_t mtd_oobsize_show(struct device *dev,
  156. struct device_attribute *attr, char *buf)
  157. {
  158. struct mtd_info *mtd = dev_get_drvdata(dev);
  159. return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->oobsize);
  160. }
  161. MTD_DEVICE_ATTR_RO(oobsize);
  162. static ssize_t mtd_oobavail_show(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. struct mtd_info *mtd = dev_get_drvdata(dev);
  166. return sysfs_emit(buf, "%u\n", mtd->oobavail);
  167. }
  168. MTD_DEVICE_ATTR_RO(oobavail);
  169. static ssize_t mtd_numeraseregions_show(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. struct mtd_info *mtd = dev_get_drvdata(dev);
  173. return sysfs_emit(buf, "%u\n", mtd->numeraseregions);
  174. }
  175. MTD_DEVICE_ATTR_RO(numeraseregions);
  176. static ssize_t mtd_name_show(struct device *dev,
  177. struct device_attribute *attr, char *buf)
  178. {
  179. struct mtd_info *mtd = dev_get_drvdata(dev);
  180. return sysfs_emit(buf, "%s\n", mtd->name);
  181. }
  182. MTD_DEVICE_ATTR_RO(name);
  183. static ssize_t mtd_ecc_strength_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct mtd_info *mtd = dev_get_drvdata(dev);
  187. return sysfs_emit(buf, "%u\n", mtd->ecc_strength);
  188. }
  189. MTD_DEVICE_ATTR_RO(ecc_strength);
  190. static ssize_t mtd_bitflip_threshold_show(struct device *dev,
  191. struct device_attribute *attr,
  192. char *buf)
  193. {
  194. struct mtd_info *mtd = dev_get_drvdata(dev);
  195. return sysfs_emit(buf, "%u\n", mtd->bitflip_threshold);
  196. }
  197. static ssize_t mtd_bitflip_threshold_store(struct device *dev,
  198. struct device_attribute *attr,
  199. const char *buf, size_t count)
  200. {
  201. struct mtd_info *mtd = dev_get_drvdata(dev);
  202. unsigned int bitflip_threshold;
  203. int retval;
  204. retval = kstrtouint(buf, 0, &bitflip_threshold);
  205. if (retval)
  206. return retval;
  207. mtd->bitflip_threshold = bitflip_threshold;
  208. return count;
  209. }
  210. MTD_DEVICE_ATTR_RW(bitflip_threshold);
  211. static ssize_t mtd_ecc_step_size_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct mtd_info *mtd = dev_get_drvdata(dev);
  215. return sysfs_emit(buf, "%u\n", mtd->ecc_step_size);
  216. }
  217. MTD_DEVICE_ATTR_RO(ecc_step_size);
  218. static ssize_t mtd_corrected_bits_show(struct device *dev,
  219. struct device_attribute *attr, char *buf)
  220. {
  221. struct mtd_info *mtd = dev_get_drvdata(dev);
  222. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  223. return sysfs_emit(buf, "%u\n", ecc_stats->corrected);
  224. }
  225. MTD_DEVICE_ATTR_RO(corrected_bits); /* ecc stats corrected */
  226. static ssize_t mtd_ecc_failures_show(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct mtd_info *mtd = dev_get_drvdata(dev);
  230. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  231. return sysfs_emit(buf, "%u\n", ecc_stats->failed);
  232. }
  233. MTD_DEVICE_ATTR_RO(ecc_failures); /* ecc stats errors */
  234. static ssize_t mtd_bad_blocks_show(struct device *dev,
  235. struct device_attribute *attr, char *buf)
  236. {
  237. struct mtd_info *mtd = dev_get_drvdata(dev);
  238. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  239. return sysfs_emit(buf, "%u\n", ecc_stats->badblocks);
  240. }
  241. MTD_DEVICE_ATTR_RO(bad_blocks);
  242. static ssize_t mtd_bbt_blocks_show(struct device *dev,
  243. struct device_attribute *attr, char *buf)
  244. {
  245. struct mtd_info *mtd = dev_get_drvdata(dev);
  246. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  247. return sysfs_emit(buf, "%u\n", ecc_stats->bbtblocks);
  248. }
  249. MTD_DEVICE_ATTR_RO(bbt_blocks);
  250. static struct attribute *mtd_attrs[] = {
  251. &dev_attr_type.attr,
  252. &dev_attr_flags.attr,
  253. &dev_attr_size.attr,
  254. &dev_attr_erasesize.attr,
  255. &dev_attr_writesize.attr,
  256. &dev_attr_subpagesize.attr,
  257. &dev_attr_oobsize.attr,
  258. &dev_attr_oobavail.attr,
  259. &dev_attr_numeraseregions.attr,
  260. &dev_attr_name.attr,
  261. &dev_attr_ecc_strength.attr,
  262. &dev_attr_ecc_step_size.attr,
  263. &dev_attr_corrected_bits.attr,
  264. &dev_attr_ecc_failures.attr,
  265. &dev_attr_bad_blocks.attr,
  266. &dev_attr_bbt_blocks.attr,
  267. &dev_attr_bitflip_threshold.attr,
  268. NULL,
  269. };
  270. ATTRIBUTE_GROUPS(mtd);
  271. static const struct device_type mtd_devtype = {
  272. .name = "mtd",
  273. .groups = mtd_groups,
  274. .release = mtd_release,
  275. };
  276. static bool mtd_expert_analysis_mode;
  277. #ifdef CONFIG_DEBUG_FS
  278. bool mtd_check_expert_analysis_mode(void)
  279. {
  280. const char *mtd_expert_analysis_warning =
  281. "Bad block checks have been entirely disabled.\n"
  282. "This is only reserved for post-mortem forensics and debug purposes.\n"
  283. "Never enable this mode if you do not know what you are doing!\n";
  284. return WARN_ONCE(mtd_expert_analysis_mode, mtd_expert_analysis_warning);
  285. }
  286. EXPORT_SYMBOL_GPL(mtd_check_expert_analysis_mode);
  287. #endif
  288. static struct dentry *dfs_dir_mtd;
  289. static void mtd_debugfs_populate(struct mtd_info *mtd)
  290. {
  291. struct device *dev = &mtd->dev;
  292. if (IS_ERR_OR_NULL(dfs_dir_mtd))
  293. return;
  294. mtd->dbg.dfs_dir = debugfs_create_dir(dev_name(dev), dfs_dir_mtd);
  295. }
  296. #ifndef CONFIG_MMU
  297. unsigned mtd_mmap_capabilities(struct mtd_info *mtd)
  298. {
  299. switch (mtd->type) {
  300. case MTD_RAM:
  301. return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
  302. NOMMU_MAP_READ | NOMMU_MAP_WRITE;
  303. case MTD_ROM:
  304. return NOMMU_MAP_COPY | NOMMU_MAP_DIRECT | NOMMU_MAP_EXEC |
  305. NOMMU_MAP_READ;
  306. default:
  307. return NOMMU_MAP_COPY;
  308. }
  309. }
  310. EXPORT_SYMBOL_GPL(mtd_mmap_capabilities);
  311. #endif
  312. static int mtd_reboot_notifier(struct notifier_block *n, unsigned long state,
  313. void *cmd)
  314. {
  315. struct mtd_info *mtd;
  316. mtd = container_of(n, struct mtd_info, reboot_notifier);
  317. mtd->_reboot(mtd);
  318. return NOTIFY_DONE;
  319. }
  320. /**
  321. * mtd_wunit_to_pairing_info - get pairing information of a wunit
  322. * @mtd: pointer to new MTD device info structure
  323. * @wunit: write unit we are interested in
  324. * @info: returned pairing information
  325. *
  326. * Retrieve pairing information associated to the wunit.
  327. * This is mainly useful when dealing with MLC/TLC NANDs where pages can be
  328. * paired together, and where programming a page may influence the page it is
  329. * paired with.
  330. * The notion of page is replaced by the term wunit (write-unit) to stay
  331. * consistent with the ->writesize field.
  332. *
  333. * The @wunit argument can be extracted from an absolute offset using
  334. * mtd_offset_to_wunit(). @info is filled with the pairing information attached
  335. * to @wunit.
  336. *
  337. * From the pairing info the MTD user can find all the wunits paired with
  338. * @wunit using the following loop:
  339. *
  340. * for (i = 0; i < mtd_pairing_groups(mtd); i++) {
  341. * info.pair = i;
  342. * mtd_pairing_info_to_wunit(mtd, &info);
  343. * ...
  344. * }
  345. */
  346. int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
  347. struct mtd_pairing_info *info)
  348. {
  349. struct mtd_info *master = mtd_get_master(mtd);
  350. int npairs = mtd_wunit_per_eb(master) / mtd_pairing_groups(master);
  351. if (wunit < 0 || wunit >= npairs)
  352. return -EINVAL;
  353. if (master->pairing && master->pairing->get_info)
  354. return master->pairing->get_info(master, wunit, info);
  355. info->group = 0;
  356. info->pair = wunit;
  357. return 0;
  358. }
  359. EXPORT_SYMBOL_GPL(mtd_wunit_to_pairing_info);
  360. /**
  361. * mtd_pairing_info_to_wunit - get wunit from pairing information
  362. * @mtd: pointer to new MTD device info structure
  363. * @info: pairing information struct
  364. *
  365. * Returns a positive number representing the wunit associated to the info
  366. * struct, or a negative error code.
  367. *
  368. * This is the reverse of mtd_wunit_to_pairing_info(), and can help one to
  369. * iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info()
  370. * doc).
  371. *
  372. * It can also be used to only program the first page of each pair (i.e.
  373. * page attached to group 0), which allows one to use an MLC NAND in
  374. * software-emulated SLC mode:
  375. *
  376. * info.group = 0;
  377. * npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd);
  378. * for (info.pair = 0; info.pair < npairs; info.pair++) {
  379. * wunit = mtd_pairing_info_to_wunit(mtd, &info);
  380. * mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit),
  381. * mtd->writesize, &retlen, buf + (i * mtd->writesize));
  382. * }
  383. */
  384. int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
  385. const struct mtd_pairing_info *info)
  386. {
  387. struct mtd_info *master = mtd_get_master(mtd);
  388. int ngroups = mtd_pairing_groups(master);
  389. int npairs = mtd_wunit_per_eb(master) / ngroups;
  390. if (!info || info->pair < 0 || info->pair >= npairs ||
  391. info->group < 0 || info->group >= ngroups)
  392. return -EINVAL;
  393. if (master->pairing && master->pairing->get_wunit)
  394. return mtd->pairing->get_wunit(master, info);
  395. return info->pair;
  396. }
  397. EXPORT_SYMBOL_GPL(mtd_pairing_info_to_wunit);
  398. /**
  399. * mtd_pairing_groups - get the number of pairing groups
  400. * @mtd: pointer to new MTD device info structure
  401. *
  402. * Returns the number of pairing groups.
  403. *
  404. * This number is usually equal to the number of bits exposed by a single
  405. * cell, and can be used in conjunction with mtd_pairing_info_to_wunit()
  406. * to iterate over all pages of a given pair.
  407. */
  408. int mtd_pairing_groups(struct mtd_info *mtd)
  409. {
  410. struct mtd_info *master = mtd_get_master(mtd);
  411. if (!master->pairing || !master->pairing->ngroups)
  412. return 1;
  413. return master->pairing->ngroups;
  414. }
  415. EXPORT_SYMBOL_GPL(mtd_pairing_groups);
  416. static int mtd_nvmem_reg_read(void *priv, unsigned int offset,
  417. void *val, size_t bytes)
  418. {
  419. struct mtd_info *mtd = priv;
  420. size_t retlen;
  421. int err;
  422. err = mtd_read(mtd, offset, bytes, &retlen, val);
  423. if (err && err != -EUCLEAN)
  424. return err;
  425. return retlen == bytes ? 0 : -EIO;
  426. }
  427. static int mtd_nvmem_add(struct mtd_info *mtd)
  428. {
  429. struct device_node *node = mtd_get_of_node(mtd);
  430. struct nvmem_config config = {};
  431. config.id = -1;
  432. config.dev = &mtd->dev;
  433. config.name = dev_name(&mtd->dev);
  434. config.owner = THIS_MODULE;
  435. config.reg_read = mtd_nvmem_reg_read;
  436. config.size = mtd->size;
  437. config.word_size = 1;
  438. config.stride = 1;
  439. config.read_only = true;
  440. config.root_only = true;
  441. config.ignore_wp = true;
  442. config.no_of_node = !of_device_is_compatible(node, "nvmem-cells");
  443. config.priv = mtd;
  444. mtd->nvmem = nvmem_register(&config);
  445. if (IS_ERR(mtd->nvmem)) {
  446. /* Just ignore if there is no NVMEM support in the kernel */
  447. if (PTR_ERR(mtd->nvmem) == -EOPNOTSUPP) {
  448. mtd->nvmem = NULL;
  449. } else {
  450. dev_err(&mtd->dev, "Failed to register NVMEM device\n");
  451. return PTR_ERR(mtd->nvmem);
  452. }
  453. }
  454. return 0;
  455. }
  456. static void mtd_check_of_node(struct mtd_info *mtd)
  457. {
  458. struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
  459. const char *pname, *prefix = "partition-";
  460. int plen, mtd_name_len, offset, prefix_len;
  461. struct mtd_info *parent;
  462. bool found = false;
  463. /* Check if MTD already has a device node */
  464. if (dev_of_node(&mtd->dev))
  465. return;
  466. /* Check if a partitions node exist */
  467. if (!mtd_is_partition(mtd))
  468. return;
  469. parent = mtd->parent;
  470. parent_dn = of_node_get(dev_of_node(&parent->dev));
  471. if (!parent_dn)
  472. return;
  473. partitions = of_get_child_by_name(parent_dn, "partitions");
  474. if (!partitions)
  475. goto exit_parent;
  476. prefix_len = strlen(prefix);
  477. mtd_name_len = strlen(mtd->name);
  478. /* Search if a partition is defined with the same name */
  479. for_each_child_of_node(partitions, mtd_dn) {
  480. offset = 0;
  481. /* Skip partition with no/wrong prefix */
  482. if (!of_node_name_prefix(mtd_dn, "partition-"))
  483. continue;
  484. /* Label have priority. Check that first */
  485. if (of_property_read_string(mtd_dn, "label", &pname)) {
  486. of_property_read_string(mtd_dn, "name", &pname);
  487. offset = prefix_len;
  488. }
  489. plen = strlen(pname) - offset;
  490. if (plen == mtd_name_len &&
  491. !strncmp(mtd->name, pname + offset, plen)) {
  492. found = true;
  493. break;
  494. }
  495. }
  496. if (!found)
  497. goto exit_partitions;
  498. /* Set of_node only for nvmem */
  499. if (of_device_is_compatible(mtd_dn, "nvmem-cells"))
  500. mtd_set_of_node(mtd, mtd_dn);
  501. exit_partitions:
  502. of_node_put(partitions);
  503. exit_parent:
  504. of_node_put(parent_dn);
  505. }
  506. /**
  507. * add_mtd_device - register an MTD device
  508. * @mtd: pointer to new MTD device info structure
  509. *
  510. * Add a device to the list of MTD devices present in the system, and
  511. * notify each currently active MTD 'user' of its arrival. Returns
  512. * zero on success or non-zero on failure.
  513. */
  514. int add_mtd_device(struct mtd_info *mtd)
  515. {
  516. struct device_node *np = mtd_get_of_node(mtd);
  517. struct mtd_info *master = mtd_get_master(mtd);
  518. struct mtd_notifier *not;
  519. int i, error, ofidx;
  520. /*
  521. * May occur, for instance, on buggy drivers which call
  522. * mtd_device_parse_register() multiple times on the same master MTD,
  523. * especially with CONFIG_MTD_PARTITIONED_MASTER=y.
  524. */
  525. if (WARN_ONCE(mtd->dev.type, "MTD already registered\n"))
  526. return -EEXIST;
  527. BUG_ON(mtd->writesize == 0);
  528. /*
  529. * MTD drivers should implement ->_{write,read}() or
  530. * ->_{write,read}_oob(), but not both.
  531. */
  532. if (WARN_ON((mtd->_write && mtd->_write_oob) ||
  533. (mtd->_read && mtd->_read_oob)))
  534. return -EINVAL;
  535. if (WARN_ON((!mtd->erasesize || !master->_erase) &&
  536. !(mtd->flags & MTD_NO_ERASE)))
  537. return -EINVAL;
  538. /*
  539. * MTD_SLC_ON_MLC_EMULATION can only be set on partitions, when the
  540. * master is an MLC NAND and has a proper pairing scheme defined.
  541. * We also reject masters that implement ->_writev() for now, because
  542. * NAND controller drivers don't implement this hook, and adding the
  543. * SLC -> MLC address/length conversion to this path is useless if we
  544. * don't have a user.
  545. */
  546. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION &&
  547. (!mtd_is_partition(mtd) || master->type != MTD_MLCNANDFLASH ||
  548. !master->pairing || master->_writev))
  549. return -EINVAL;
  550. mutex_lock(&mtd_table_mutex);
  551. ofidx = -1;
  552. if (np)
  553. ofidx = of_alias_get_id(np, "mtd");
  554. if (ofidx >= 0)
  555. i = idr_alloc(&mtd_idr, mtd, ofidx, ofidx + 1, GFP_KERNEL);
  556. else
  557. i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
  558. if (i < 0) {
  559. error = i;
  560. goto fail_locked;
  561. }
  562. mtd->index = i;
  563. mtd->usecount = 0;
  564. /* default value if not set by driver */
  565. if (mtd->bitflip_threshold == 0)
  566. mtd->bitflip_threshold = mtd->ecc_strength;
  567. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  568. int ngroups = mtd_pairing_groups(master);
  569. mtd->erasesize /= ngroups;
  570. mtd->size = (u64)mtd_div_by_eb(mtd->size, master) *
  571. mtd->erasesize;
  572. }
  573. if (is_power_of_2(mtd->erasesize))
  574. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  575. else
  576. mtd->erasesize_shift = 0;
  577. if (is_power_of_2(mtd->writesize))
  578. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  579. else
  580. mtd->writesize_shift = 0;
  581. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  582. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  583. /* Some chips always power up locked. Unlock them now */
  584. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  585. error = mtd_unlock(mtd, 0, mtd->size);
  586. if (error && error != -EOPNOTSUPP)
  587. printk(KERN_WARNING
  588. "%s: unlock failed, writes may not work\n",
  589. mtd->name);
  590. /* Ignore unlock failures? */
  591. error = 0;
  592. }
  593. /* Caller should have set dev.parent to match the
  594. * physical device, if appropriate.
  595. */
  596. mtd->dev.type = &mtd_devtype;
  597. mtd->dev.class = &mtd_class;
  598. mtd->dev.devt = MTD_DEVT(i);
  599. dev_set_name(&mtd->dev, "mtd%d", i);
  600. dev_set_drvdata(&mtd->dev, mtd);
  601. mtd_check_of_node(mtd);
  602. of_node_get(mtd_get_of_node(mtd));
  603. error = device_register(&mtd->dev);
  604. if (error) {
  605. put_device(&mtd->dev);
  606. goto fail_added;
  607. }
  608. /* Add the nvmem provider */
  609. error = mtd_nvmem_add(mtd);
  610. if (error)
  611. goto fail_nvmem_add;
  612. mtd_debugfs_populate(mtd);
  613. device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
  614. "mtd%dro", i);
  615. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  616. /* No need to get a refcount on the module containing
  617. the notifier, since we hold the mtd_table_mutex */
  618. list_for_each_entry(not, &mtd_notifiers, list)
  619. not->add(mtd);
  620. mutex_unlock(&mtd_table_mutex);
  621. /* We _know_ we aren't being removed, because
  622. our caller is still holding us here. So none
  623. of this try_ nonsense, and no bitching about it
  624. either. :) */
  625. __module_get(THIS_MODULE);
  626. return 0;
  627. fail_nvmem_add:
  628. device_unregister(&mtd->dev);
  629. fail_added:
  630. of_node_put(mtd_get_of_node(mtd));
  631. idr_remove(&mtd_idr, i);
  632. fail_locked:
  633. mutex_unlock(&mtd_table_mutex);
  634. return error;
  635. }
  636. /**
  637. * del_mtd_device - unregister an MTD device
  638. * @mtd: pointer to MTD device info structure
  639. *
  640. * Remove a device from the list of MTD devices present in the system,
  641. * and notify each currently active MTD 'user' of its departure.
  642. * Returns zero on success or 1 on failure, which currently will happen
  643. * if the requested device does not appear to be present in the list.
  644. */
  645. int del_mtd_device(struct mtd_info *mtd)
  646. {
  647. int ret;
  648. struct mtd_notifier *not;
  649. struct device_node *mtd_of_node;
  650. mutex_lock(&mtd_table_mutex);
  651. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  652. ret = -ENODEV;
  653. goto out_error;
  654. }
  655. /* No need to get a refcount on the module containing
  656. the notifier, since we hold the mtd_table_mutex */
  657. list_for_each_entry(not, &mtd_notifiers, list)
  658. not->remove(mtd);
  659. if (mtd->usecount) {
  660. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  661. mtd->index, mtd->name, mtd->usecount);
  662. ret = -EBUSY;
  663. } else {
  664. mtd_of_node = mtd_get_of_node(mtd);
  665. debugfs_remove_recursive(mtd->dbg.dfs_dir);
  666. /* Try to remove the NVMEM provider */
  667. nvmem_unregister(mtd->nvmem);
  668. device_unregister(&mtd->dev);
  669. /* Clear dev so mtd can be safely re-registered later if desired */
  670. memset(&mtd->dev, 0, sizeof(mtd->dev));
  671. idr_remove(&mtd_idr, mtd->index);
  672. of_node_put(mtd_of_node);
  673. module_put(THIS_MODULE);
  674. ret = 0;
  675. }
  676. out_error:
  677. mutex_unlock(&mtd_table_mutex);
  678. return ret;
  679. }
  680. /*
  681. * Set a few defaults based on the parent devices, if not provided by the
  682. * driver
  683. */
  684. static void mtd_set_dev_defaults(struct mtd_info *mtd)
  685. {
  686. if (mtd->dev.parent) {
  687. if (!mtd->owner && mtd->dev.parent->driver)
  688. mtd->owner = mtd->dev.parent->driver->owner;
  689. if (!mtd->name)
  690. mtd->name = dev_name(mtd->dev.parent);
  691. } else {
  692. pr_debug("mtd device won't show a device symlink in sysfs\n");
  693. }
  694. INIT_LIST_HEAD(&mtd->partitions);
  695. mutex_init(&mtd->master.partitions_lock);
  696. mutex_init(&mtd->master.chrdev_lock);
  697. }
  698. static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user)
  699. {
  700. struct otp_info *info;
  701. ssize_t size = 0;
  702. unsigned int i;
  703. size_t retlen;
  704. int ret;
  705. info = kmalloc(PAGE_SIZE, GFP_KERNEL);
  706. if (!info)
  707. return -ENOMEM;
  708. if (is_user)
  709. ret = mtd_get_user_prot_info(mtd, PAGE_SIZE, &retlen, info);
  710. else
  711. ret = mtd_get_fact_prot_info(mtd, PAGE_SIZE, &retlen, info);
  712. if (ret)
  713. goto err;
  714. for (i = 0; i < retlen / sizeof(*info); i++)
  715. size += info[i].length;
  716. kfree(info);
  717. return size;
  718. err:
  719. kfree(info);
  720. /* ENODATA means there is no OTP region. */
  721. return ret == -ENODATA ? 0 : ret;
  722. }
  723. static struct nvmem_device *mtd_otp_nvmem_register(struct mtd_info *mtd,
  724. const char *compatible,
  725. int size,
  726. nvmem_reg_read_t reg_read)
  727. {
  728. struct nvmem_device *nvmem = NULL;
  729. struct nvmem_config config = {};
  730. struct device_node *np;
  731. /* DT binding is optional */
  732. np = of_get_compatible_child(mtd->dev.of_node, compatible);
  733. /* OTP nvmem will be registered on the physical device */
  734. config.dev = mtd->dev.parent;
  735. config.name = compatible;
  736. config.id = NVMEM_DEVID_AUTO;
  737. config.owner = THIS_MODULE;
  738. config.type = NVMEM_TYPE_OTP;
  739. config.root_only = true;
  740. config.ignore_wp = true;
  741. config.reg_read = reg_read;
  742. config.size = size;
  743. config.of_node = np;
  744. config.priv = mtd;
  745. nvmem = nvmem_register(&config);
  746. /* Just ignore if there is no NVMEM support in the kernel */
  747. if (IS_ERR(nvmem) && PTR_ERR(nvmem) == -EOPNOTSUPP)
  748. nvmem = NULL;
  749. of_node_put(np);
  750. return nvmem;
  751. }
  752. static int mtd_nvmem_user_otp_reg_read(void *priv, unsigned int offset,
  753. void *val, size_t bytes)
  754. {
  755. struct mtd_info *mtd = priv;
  756. size_t retlen;
  757. int ret;
  758. ret = mtd_read_user_prot_reg(mtd, offset, bytes, &retlen, val);
  759. if (ret)
  760. return ret;
  761. return retlen == bytes ? 0 : -EIO;
  762. }
  763. static int mtd_nvmem_fact_otp_reg_read(void *priv, unsigned int offset,
  764. void *val, size_t bytes)
  765. {
  766. struct mtd_info *mtd = priv;
  767. size_t retlen;
  768. int ret;
  769. ret = mtd_read_fact_prot_reg(mtd, offset, bytes, &retlen, val);
  770. if (ret)
  771. return ret;
  772. return retlen == bytes ? 0 : -EIO;
  773. }
  774. static int mtd_otp_nvmem_add(struct mtd_info *mtd)
  775. {
  776. struct device *dev = mtd->dev.parent;
  777. struct nvmem_device *nvmem;
  778. ssize_t size;
  779. int err;
  780. if (mtd->_get_user_prot_info && mtd->_read_user_prot_reg) {
  781. size = mtd_otp_size(mtd, true);
  782. if (size < 0)
  783. return size;
  784. if (size > 0) {
  785. nvmem = mtd_otp_nvmem_register(mtd, "user-otp", size,
  786. mtd_nvmem_user_otp_reg_read);
  787. if (IS_ERR(nvmem)) {
  788. dev_err(dev, "Failed to register OTP NVMEM device\n");
  789. return PTR_ERR(nvmem);
  790. }
  791. mtd->otp_user_nvmem = nvmem;
  792. }
  793. }
  794. if (mtd->_get_fact_prot_info && mtd->_read_fact_prot_reg) {
  795. size = mtd_otp_size(mtd, false);
  796. if (size < 0) {
  797. err = size;
  798. goto err;
  799. }
  800. if (size > 0) {
  801. nvmem = mtd_otp_nvmem_register(mtd, "factory-otp", size,
  802. mtd_nvmem_fact_otp_reg_read);
  803. if (IS_ERR(nvmem)) {
  804. dev_err(dev, "Failed to register OTP NVMEM device\n");
  805. err = PTR_ERR(nvmem);
  806. goto err;
  807. }
  808. mtd->otp_factory_nvmem = nvmem;
  809. }
  810. }
  811. return 0;
  812. err:
  813. nvmem_unregister(mtd->otp_user_nvmem);
  814. return err;
  815. }
  816. /**
  817. * mtd_device_parse_register - parse partitions and register an MTD device.
  818. *
  819. * @mtd: the MTD device to register
  820. * @types: the list of MTD partition probes to try, see
  821. * 'parse_mtd_partitions()' for more information
  822. * @parser_data: MTD partition parser-specific data
  823. * @parts: fallback partition information to register, if parsing fails;
  824. * only valid if %nr_parts > %0
  825. * @nr_parts: the number of partitions in parts, if zero then the full
  826. * MTD device is registered if no partition info is found
  827. *
  828. * This function aggregates MTD partitions parsing (done by
  829. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  830. * basically follows the most common pattern found in many MTD drivers:
  831. *
  832. * * If the MTD_PARTITIONED_MASTER option is set, then the device as a whole is
  833. * registered first.
  834. * * Then It tries to probe partitions on MTD device @mtd using parsers
  835. * specified in @types (if @types is %NULL, then the default list of parsers
  836. * is used, see 'parse_mtd_partitions()' for more information). If none are
  837. * found this functions tries to fallback to information specified in
  838. * @parts/@nr_parts.
  839. * * If no partitions were found this function just registers the MTD device
  840. * @mtd and exits.
  841. *
  842. * Returns zero in case of success and a negative error code in case of failure.
  843. */
  844. int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
  845. struct mtd_part_parser_data *parser_data,
  846. const struct mtd_partition *parts,
  847. int nr_parts)
  848. {
  849. int ret;
  850. mtd_set_dev_defaults(mtd);
  851. ret = mtd_otp_nvmem_add(mtd);
  852. if (ret)
  853. goto out;
  854. if (IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
  855. ret = add_mtd_device(mtd);
  856. if (ret)
  857. goto out;
  858. }
  859. /* Prefer parsed partitions over driver-provided fallback */
  860. ret = parse_mtd_partitions(mtd, types, parser_data);
  861. if (ret == -EPROBE_DEFER)
  862. goto out;
  863. if (ret > 0)
  864. ret = 0;
  865. else if (nr_parts)
  866. ret = add_mtd_partitions(mtd, parts, nr_parts);
  867. else if (!device_is_registered(&mtd->dev))
  868. ret = add_mtd_device(mtd);
  869. else
  870. ret = 0;
  871. if (ret)
  872. goto out;
  873. /*
  874. * FIXME: some drivers unfortunately call this function more than once.
  875. * So we have to check if we've already assigned the reboot notifier.
  876. *
  877. * Generally, we can make multiple calls work for most cases, but it
  878. * does cause problems with parse_mtd_partitions() above (e.g.,
  879. * cmdlineparts will register partitions more than once).
  880. */
  881. WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call,
  882. "MTD already registered\n");
  883. if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
  884. mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
  885. register_reboot_notifier(&mtd->reboot_notifier);
  886. }
  887. out:
  888. if (ret) {
  889. nvmem_unregister(mtd->otp_user_nvmem);
  890. nvmem_unregister(mtd->otp_factory_nvmem);
  891. }
  892. if (ret && device_is_registered(&mtd->dev))
  893. del_mtd_device(mtd);
  894. return ret;
  895. }
  896. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  897. /**
  898. * mtd_device_unregister - unregister an existing MTD device.
  899. *
  900. * @master: the MTD device to unregister. This will unregister both the master
  901. * and any partitions if registered.
  902. */
  903. int mtd_device_unregister(struct mtd_info *master)
  904. {
  905. int err;
  906. if (master->_reboot) {
  907. unregister_reboot_notifier(&master->reboot_notifier);
  908. memset(&master->reboot_notifier, 0, sizeof(master->reboot_notifier));
  909. }
  910. nvmem_unregister(master->otp_user_nvmem);
  911. nvmem_unregister(master->otp_factory_nvmem);
  912. err = del_mtd_partitions(master);
  913. if (err)
  914. return err;
  915. if (!device_is_registered(&master->dev))
  916. return 0;
  917. return del_mtd_device(master);
  918. }
  919. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  920. /**
  921. * register_mtd_user - register a 'user' of MTD devices.
  922. * @new: pointer to notifier info structure
  923. *
  924. * Registers a pair of callbacks function to be called upon addition
  925. * or removal of MTD devices. Causes the 'add' callback to be immediately
  926. * invoked for each MTD device currently present in the system.
  927. */
  928. void register_mtd_user (struct mtd_notifier *new)
  929. {
  930. struct mtd_info *mtd;
  931. mutex_lock(&mtd_table_mutex);
  932. list_add(&new->list, &mtd_notifiers);
  933. __module_get(THIS_MODULE);
  934. mtd_for_each_device(mtd)
  935. new->add(mtd);
  936. mutex_unlock(&mtd_table_mutex);
  937. }
  938. EXPORT_SYMBOL_GPL(register_mtd_user);
  939. /**
  940. * unregister_mtd_user - unregister a 'user' of MTD devices.
  941. * @old: pointer to notifier info structure
  942. *
  943. * Removes a callback function pair from the list of 'users' to be
  944. * notified upon addition or removal of MTD devices. Causes the
  945. * 'remove' callback to be immediately invoked for each MTD device
  946. * currently present in the system.
  947. */
  948. int unregister_mtd_user (struct mtd_notifier *old)
  949. {
  950. struct mtd_info *mtd;
  951. mutex_lock(&mtd_table_mutex);
  952. module_put(THIS_MODULE);
  953. mtd_for_each_device(mtd)
  954. old->remove(mtd);
  955. list_del(&old->list);
  956. mutex_unlock(&mtd_table_mutex);
  957. return 0;
  958. }
  959. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  960. /**
  961. * get_mtd_device - obtain a validated handle for an MTD device
  962. * @mtd: last known address of the required MTD device
  963. * @num: internal device number of the required MTD device
  964. *
  965. * Given a number and NULL address, return the num'th entry in the device
  966. * table, if any. Given an address and num == -1, search the device table
  967. * for a device with that address and return if it's still present. Given
  968. * both, return the num'th driver only if its address matches. Return
  969. * error code if not.
  970. */
  971. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  972. {
  973. struct mtd_info *ret = NULL, *other;
  974. int err = -ENODEV;
  975. mutex_lock(&mtd_table_mutex);
  976. if (num == -1) {
  977. mtd_for_each_device(other) {
  978. if (other == mtd) {
  979. ret = mtd;
  980. break;
  981. }
  982. }
  983. } else if (num >= 0) {
  984. ret = idr_find(&mtd_idr, num);
  985. if (mtd && mtd != ret)
  986. ret = NULL;
  987. }
  988. if (!ret) {
  989. ret = ERR_PTR(err);
  990. goto out;
  991. }
  992. err = __get_mtd_device(ret);
  993. if (err)
  994. ret = ERR_PTR(err);
  995. out:
  996. mutex_unlock(&mtd_table_mutex);
  997. return ret;
  998. }
  999. EXPORT_SYMBOL_GPL(get_mtd_device);
  1000. int __get_mtd_device(struct mtd_info *mtd)
  1001. {
  1002. struct mtd_info *master = mtd_get_master(mtd);
  1003. int err;
  1004. if (!try_module_get(master->owner))
  1005. return -ENODEV;
  1006. if (master->_get_device) {
  1007. err = master->_get_device(mtd);
  1008. if (err) {
  1009. module_put(master->owner);
  1010. return err;
  1011. }
  1012. }
  1013. master->usecount++;
  1014. while (mtd->parent) {
  1015. mtd->usecount++;
  1016. mtd = mtd->parent;
  1017. }
  1018. return 0;
  1019. }
  1020. EXPORT_SYMBOL_GPL(__get_mtd_device);
  1021. /**
  1022. * of_get_mtd_device_by_node - obtain an MTD device associated with a given node
  1023. *
  1024. * @np: device tree node
  1025. */
  1026. struct mtd_info *of_get_mtd_device_by_node(struct device_node *np)
  1027. {
  1028. struct mtd_info *mtd = NULL;
  1029. struct mtd_info *tmp;
  1030. int err;
  1031. mutex_lock(&mtd_table_mutex);
  1032. err = -EPROBE_DEFER;
  1033. mtd_for_each_device(tmp) {
  1034. if (mtd_get_of_node(tmp) == np) {
  1035. mtd = tmp;
  1036. err = __get_mtd_device(mtd);
  1037. break;
  1038. }
  1039. }
  1040. mutex_unlock(&mtd_table_mutex);
  1041. return err ? ERR_PTR(err) : mtd;
  1042. }
  1043. EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node);
  1044. /**
  1045. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  1046. * device name
  1047. * @name: MTD device name to open
  1048. *
  1049. * This function returns MTD device description structure in case of
  1050. * success and an error code in case of failure.
  1051. */
  1052. struct mtd_info *get_mtd_device_nm(const char *name)
  1053. {
  1054. int err = -ENODEV;
  1055. struct mtd_info *mtd = NULL, *other;
  1056. mutex_lock(&mtd_table_mutex);
  1057. mtd_for_each_device(other) {
  1058. if (!strcmp(name, other->name)) {
  1059. mtd = other;
  1060. break;
  1061. }
  1062. }
  1063. if (!mtd)
  1064. goto out_unlock;
  1065. err = __get_mtd_device(mtd);
  1066. if (err)
  1067. goto out_unlock;
  1068. mutex_unlock(&mtd_table_mutex);
  1069. return mtd;
  1070. out_unlock:
  1071. mutex_unlock(&mtd_table_mutex);
  1072. return ERR_PTR(err);
  1073. }
  1074. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  1075. void put_mtd_device(struct mtd_info *mtd)
  1076. {
  1077. mutex_lock(&mtd_table_mutex);
  1078. __put_mtd_device(mtd);
  1079. mutex_unlock(&mtd_table_mutex);
  1080. }
  1081. EXPORT_SYMBOL_GPL(put_mtd_device);
  1082. void __put_mtd_device(struct mtd_info *mtd)
  1083. {
  1084. struct mtd_info *master = mtd_get_master(mtd);
  1085. while (mtd->parent) {
  1086. --mtd->usecount;
  1087. BUG_ON(mtd->usecount < 0);
  1088. mtd = mtd->parent;
  1089. }
  1090. master->usecount--;
  1091. if (master->_put_device)
  1092. master->_put_device(master);
  1093. module_put(master->owner);
  1094. }
  1095. EXPORT_SYMBOL_GPL(__put_mtd_device);
  1096. /*
  1097. * Erase is an synchronous operation. Device drivers are epected to return a
  1098. * negative error code if the operation failed and update instr->fail_addr
  1099. * to point the portion that was not properly erased.
  1100. */
  1101. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  1102. {
  1103. struct mtd_info *master = mtd_get_master(mtd);
  1104. u64 mst_ofs = mtd_get_master_ofs(mtd, 0);
  1105. struct erase_info adjinstr;
  1106. int ret;
  1107. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  1108. adjinstr = *instr;
  1109. if (!mtd->erasesize || !master->_erase)
  1110. return -ENOTSUPP;
  1111. if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
  1112. return -EINVAL;
  1113. if (!(mtd->flags & MTD_WRITEABLE))
  1114. return -EROFS;
  1115. if (!instr->len)
  1116. return 0;
  1117. ledtrig_mtd_activity();
  1118. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  1119. adjinstr.addr = (loff_t)mtd_div_by_eb(instr->addr, mtd) *
  1120. master->erasesize;
  1121. adjinstr.len = ((u64)mtd_div_by_eb(instr->addr + instr->len, mtd) *
  1122. master->erasesize) -
  1123. adjinstr.addr;
  1124. }
  1125. adjinstr.addr += mst_ofs;
  1126. ret = master->_erase(master, &adjinstr);
  1127. if (adjinstr.fail_addr != MTD_FAIL_ADDR_UNKNOWN) {
  1128. instr->fail_addr = adjinstr.fail_addr - mst_ofs;
  1129. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  1130. instr->fail_addr = mtd_div_by_eb(instr->fail_addr,
  1131. master);
  1132. instr->fail_addr *= mtd->erasesize;
  1133. }
  1134. }
  1135. return ret;
  1136. }
  1137. EXPORT_SYMBOL_GPL(mtd_erase);
  1138. /*
  1139. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  1140. */
  1141. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  1142. void **virt, resource_size_t *phys)
  1143. {
  1144. struct mtd_info *master = mtd_get_master(mtd);
  1145. *retlen = 0;
  1146. *virt = NULL;
  1147. if (phys)
  1148. *phys = 0;
  1149. if (!master->_point)
  1150. return -EOPNOTSUPP;
  1151. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  1152. return -EINVAL;
  1153. if (!len)
  1154. return 0;
  1155. from = mtd_get_master_ofs(mtd, from);
  1156. return master->_point(master, from, len, retlen, virt, phys);
  1157. }
  1158. EXPORT_SYMBOL_GPL(mtd_point);
  1159. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  1160. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  1161. {
  1162. struct mtd_info *master = mtd_get_master(mtd);
  1163. if (!master->_unpoint)
  1164. return -EOPNOTSUPP;
  1165. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  1166. return -EINVAL;
  1167. if (!len)
  1168. return 0;
  1169. return master->_unpoint(master, mtd_get_master_ofs(mtd, from), len);
  1170. }
  1171. EXPORT_SYMBOL_GPL(mtd_unpoint);
  1172. /*
  1173. * Allow NOMMU mmap() to directly map the device (if not NULL)
  1174. * - return the address to which the offset maps
  1175. * - return -ENOSYS to indicate refusal to do the mapping
  1176. */
  1177. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  1178. unsigned long offset, unsigned long flags)
  1179. {
  1180. size_t retlen;
  1181. void *virt;
  1182. int ret;
  1183. ret = mtd_point(mtd, offset, len, &retlen, &virt, NULL);
  1184. if (ret)
  1185. return ret;
  1186. if (retlen != len) {
  1187. mtd_unpoint(mtd, offset, retlen);
  1188. return -ENOSYS;
  1189. }
  1190. return (unsigned long)virt;
  1191. }
  1192. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  1193. static void mtd_update_ecc_stats(struct mtd_info *mtd, struct mtd_info *master,
  1194. const struct mtd_ecc_stats *old_stats)
  1195. {
  1196. struct mtd_ecc_stats diff;
  1197. if (master == mtd)
  1198. return;
  1199. diff = master->ecc_stats;
  1200. diff.failed -= old_stats->failed;
  1201. diff.corrected -= old_stats->corrected;
  1202. while (mtd->parent) {
  1203. mtd->ecc_stats.failed += diff.failed;
  1204. mtd->ecc_stats.corrected += diff.corrected;
  1205. mtd = mtd->parent;
  1206. }
  1207. }
  1208. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  1209. u_char *buf)
  1210. {
  1211. struct mtd_oob_ops ops = {
  1212. .len = len,
  1213. .datbuf = buf,
  1214. };
  1215. int ret;
  1216. ret = mtd_read_oob(mtd, from, &ops);
  1217. *retlen = ops.retlen;
  1218. return ret;
  1219. }
  1220. EXPORT_SYMBOL_GPL(mtd_read);
  1221. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  1222. const u_char *buf)
  1223. {
  1224. struct mtd_oob_ops ops = {
  1225. .len = len,
  1226. .datbuf = (u8 *)buf,
  1227. };
  1228. int ret;
  1229. ret = mtd_write_oob(mtd, to, &ops);
  1230. *retlen = ops.retlen;
  1231. return ret;
  1232. }
  1233. EXPORT_SYMBOL_GPL(mtd_write);
  1234. /*
  1235. * In blackbox flight recorder like scenarios we want to make successful writes
  1236. * in interrupt context. panic_write() is only intended to be called when its
  1237. * known the kernel is about to panic and we need the write to succeed. Since
  1238. * the kernel is not going to be running for much longer, this function can
  1239. * break locks and delay to ensure the write succeeds (but not sleep).
  1240. */
  1241. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  1242. const u_char *buf)
  1243. {
  1244. struct mtd_info *master = mtd_get_master(mtd);
  1245. *retlen = 0;
  1246. if (!master->_panic_write)
  1247. return -EOPNOTSUPP;
  1248. if (to < 0 || to >= mtd->size || len > mtd->size - to)
  1249. return -EINVAL;
  1250. if (!(mtd->flags & MTD_WRITEABLE))
  1251. return -EROFS;
  1252. if (!len)
  1253. return 0;
  1254. if (!master->oops_panic_write)
  1255. master->oops_panic_write = true;
  1256. return master->_panic_write(master, mtd_get_master_ofs(mtd, to), len,
  1257. retlen, buf);
  1258. }
  1259. EXPORT_SYMBOL_GPL(mtd_panic_write);
  1260. static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
  1261. struct mtd_oob_ops *ops)
  1262. {
  1263. /*
  1264. * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
  1265. * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
  1266. * this case.
  1267. */
  1268. if (!ops->datbuf)
  1269. ops->len = 0;
  1270. if (!ops->oobbuf)
  1271. ops->ooblen = 0;
  1272. if (offs < 0 || offs + ops->len > mtd->size)
  1273. return -EINVAL;
  1274. if (ops->ooblen) {
  1275. size_t maxooblen;
  1276. if (ops->ooboffs >= mtd_oobavail(mtd, ops))
  1277. return -EINVAL;
  1278. maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
  1279. mtd_div_by_ws(offs, mtd)) *
  1280. mtd_oobavail(mtd, ops)) - ops->ooboffs;
  1281. if (ops->ooblen > maxooblen)
  1282. return -EINVAL;
  1283. }
  1284. return 0;
  1285. }
  1286. static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from,
  1287. struct mtd_oob_ops *ops)
  1288. {
  1289. struct mtd_info *master = mtd_get_master(mtd);
  1290. int ret;
  1291. from = mtd_get_master_ofs(mtd, from);
  1292. if (master->_read_oob)
  1293. ret = master->_read_oob(master, from, ops);
  1294. else
  1295. ret = master->_read(master, from, ops->len, &ops->retlen,
  1296. ops->datbuf);
  1297. return ret;
  1298. }
  1299. static int mtd_write_oob_std(struct mtd_info *mtd, loff_t to,
  1300. struct mtd_oob_ops *ops)
  1301. {
  1302. struct mtd_info *master = mtd_get_master(mtd);
  1303. int ret;
  1304. to = mtd_get_master_ofs(mtd, to);
  1305. if (master->_write_oob)
  1306. ret = master->_write_oob(master, to, ops);
  1307. else
  1308. ret = master->_write(master, to, ops->len, &ops->retlen,
  1309. ops->datbuf);
  1310. return ret;
  1311. }
  1312. static int mtd_io_emulated_slc(struct mtd_info *mtd, loff_t start, bool read,
  1313. struct mtd_oob_ops *ops)
  1314. {
  1315. struct mtd_info *master = mtd_get_master(mtd);
  1316. int ngroups = mtd_pairing_groups(master);
  1317. int npairs = mtd_wunit_per_eb(master) / ngroups;
  1318. struct mtd_oob_ops adjops = *ops;
  1319. unsigned int wunit, oobavail;
  1320. struct mtd_pairing_info info;
  1321. int max_bitflips = 0;
  1322. u32 ebofs, pageofs;
  1323. loff_t base, pos;
  1324. ebofs = mtd_mod_by_eb(start, mtd);
  1325. base = (loff_t)mtd_div_by_eb(start, mtd) * master->erasesize;
  1326. info.group = 0;
  1327. info.pair = mtd_div_by_ws(ebofs, mtd);
  1328. pageofs = mtd_mod_by_ws(ebofs, mtd);
  1329. oobavail = mtd_oobavail(mtd, ops);
  1330. while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) {
  1331. int ret;
  1332. if (info.pair >= npairs) {
  1333. info.pair = 0;
  1334. base += master->erasesize;
  1335. }
  1336. wunit = mtd_pairing_info_to_wunit(master, &info);
  1337. pos = mtd_wunit_to_offset(mtd, base, wunit);
  1338. adjops.len = ops->len - ops->retlen;
  1339. if (adjops.len > mtd->writesize - pageofs)
  1340. adjops.len = mtd->writesize - pageofs;
  1341. adjops.ooblen = ops->ooblen - ops->oobretlen;
  1342. if (adjops.ooblen > oobavail - adjops.ooboffs)
  1343. adjops.ooblen = oobavail - adjops.ooboffs;
  1344. if (read) {
  1345. ret = mtd_read_oob_std(mtd, pos + pageofs, &adjops);
  1346. if (ret > 0)
  1347. max_bitflips = max(max_bitflips, ret);
  1348. } else {
  1349. ret = mtd_write_oob_std(mtd, pos + pageofs, &adjops);
  1350. }
  1351. if (ret < 0)
  1352. return ret;
  1353. max_bitflips = max(max_bitflips, ret);
  1354. ops->retlen += adjops.retlen;
  1355. ops->oobretlen += adjops.oobretlen;
  1356. adjops.datbuf += adjops.retlen;
  1357. adjops.oobbuf += adjops.oobretlen;
  1358. adjops.ooboffs = 0;
  1359. pageofs = 0;
  1360. info.pair++;
  1361. }
  1362. return max_bitflips;
  1363. }
  1364. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  1365. {
  1366. struct mtd_info *master = mtd_get_master(mtd);
  1367. struct mtd_ecc_stats old_stats = master->ecc_stats;
  1368. int ret_code;
  1369. ops->retlen = ops->oobretlen = 0;
  1370. ret_code = mtd_check_oob_ops(mtd, from, ops);
  1371. if (ret_code)
  1372. return ret_code;
  1373. ledtrig_mtd_activity();
  1374. /* Check the validity of a potential fallback on mtd->_read */
  1375. if (!master->_read_oob && (!master->_read || ops->oobbuf))
  1376. return -EOPNOTSUPP;
  1377. if (ops->stats)
  1378. memset(ops->stats, 0, sizeof(*ops->stats));
  1379. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1380. ret_code = mtd_io_emulated_slc(mtd, from, true, ops);
  1381. else
  1382. ret_code = mtd_read_oob_std(mtd, from, ops);
  1383. mtd_update_ecc_stats(mtd, master, &old_stats);
  1384. /*
  1385. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  1386. * similar to mtd->_read(), returning a non-negative integer
  1387. * representing max bitflips. In other cases, mtd->_read_oob() may
  1388. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  1389. */
  1390. if (unlikely(ret_code < 0))
  1391. return ret_code;
  1392. if (mtd->ecc_strength == 0)
  1393. return 0; /* device lacks ecc */
  1394. if (ops->stats)
  1395. ops->stats->max_bitflips = ret_code;
  1396. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  1397. }
  1398. EXPORT_SYMBOL_GPL(mtd_read_oob);
  1399. int mtd_write_oob(struct mtd_info *mtd, loff_t to,
  1400. struct mtd_oob_ops *ops)
  1401. {
  1402. struct mtd_info *master = mtd_get_master(mtd);
  1403. int ret;
  1404. ops->retlen = ops->oobretlen = 0;
  1405. if (!(mtd->flags & MTD_WRITEABLE))
  1406. return -EROFS;
  1407. ret = mtd_check_oob_ops(mtd, to, ops);
  1408. if (ret)
  1409. return ret;
  1410. ledtrig_mtd_activity();
  1411. /* Check the validity of a potential fallback on mtd->_write */
  1412. if (!master->_write_oob && (!master->_write || ops->oobbuf))
  1413. return -EOPNOTSUPP;
  1414. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1415. return mtd_io_emulated_slc(mtd, to, false, ops);
  1416. return mtd_write_oob_std(mtd, to, ops);
  1417. }
  1418. EXPORT_SYMBOL_GPL(mtd_write_oob);
  1419. /**
  1420. * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
  1421. * @mtd: MTD device structure
  1422. * @section: ECC section. Depending on the layout you may have all the ECC
  1423. * bytes stored in a single contiguous section, or one section
  1424. * per ECC chunk (and sometime several sections for a single ECC
  1425. * ECC chunk)
  1426. * @oobecc: OOB region struct filled with the appropriate ECC position
  1427. * information
  1428. *
  1429. * This function returns ECC section information in the OOB area. If you want
  1430. * to get all the ECC bytes information, then you should call
  1431. * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
  1432. *
  1433. * Returns zero on success, a negative error code otherwise.
  1434. */
  1435. int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  1436. struct mtd_oob_region *oobecc)
  1437. {
  1438. struct mtd_info *master = mtd_get_master(mtd);
  1439. memset(oobecc, 0, sizeof(*oobecc));
  1440. if (!master || section < 0)
  1441. return -EINVAL;
  1442. if (!master->ooblayout || !master->ooblayout->ecc)
  1443. return -ENOTSUPP;
  1444. return master->ooblayout->ecc(master, section, oobecc);
  1445. }
  1446. EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
  1447. /**
  1448. * mtd_ooblayout_free - Get the OOB region definition of a specific free
  1449. * section
  1450. * @mtd: MTD device structure
  1451. * @section: Free section you are interested in. Depending on the layout
  1452. * you may have all the free bytes stored in a single contiguous
  1453. * section, or one section per ECC chunk plus an extra section
  1454. * for the remaining bytes (or other funky layout).
  1455. * @oobfree: OOB region struct filled with the appropriate free position
  1456. * information
  1457. *
  1458. * This function returns free bytes position in the OOB area. If you want
  1459. * to get all the free bytes information, then you should call
  1460. * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
  1461. *
  1462. * Returns zero on success, a negative error code otherwise.
  1463. */
  1464. int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  1465. struct mtd_oob_region *oobfree)
  1466. {
  1467. struct mtd_info *master = mtd_get_master(mtd);
  1468. memset(oobfree, 0, sizeof(*oobfree));
  1469. if (!master || section < 0)
  1470. return -EINVAL;
  1471. if (!master->ooblayout || !master->ooblayout->free)
  1472. return -ENOTSUPP;
  1473. return master->ooblayout->free(master, section, oobfree);
  1474. }
  1475. EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
  1476. /**
  1477. * mtd_ooblayout_find_region - Find the region attached to a specific byte
  1478. * @mtd: mtd info structure
  1479. * @byte: the byte we are searching for
  1480. * @sectionp: pointer where the section id will be stored
  1481. * @oobregion: used to retrieve the ECC position
  1482. * @iter: iterator function. Should be either mtd_ooblayout_free or
  1483. * mtd_ooblayout_ecc depending on the region type you're searching for
  1484. *
  1485. * This function returns the section id and oobregion information of a
  1486. * specific byte. For example, say you want to know where the 4th ECC byte is
  1487. * stored, you'll use:
  1488. *
  1489. * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
  1490. *
  1491. * Returns zero on success, a negative error code otherwise.
  1492. */
  1493. static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
  1494. int *sectionp, struct mtd_oob_region *oobregion,
  1495. int (*iter)(struct mtd_info *,
  1496. int section,
  1497. struct mtd_oob_region *oobregion))
  1498. {
  1499. int pos = 0, ret, section = 0;
  1500. memset(oobregion, 0, sizeof(*oobregion));
  1501. while (1) {
  1502. ret = iter(mtd, section, oobregion);
  1503. if (ret)
  1504. return ret;
  1505. if (pos + oobregion->length > byte)
  1506. break;
  1507. pos += oobregion->length;
  1508. section++;
  1509. }
  1510. /*
  1511. * Adjust region info to make it start at the beginning at the
  1512. * 'start' ECC byte.
  1513. */
  1514. oobregion->offset += byte - pos;
  1515. oobregion->length -= byte - pos;
  1516. *sectionp = section;
  1517. return 0;
  1518. }
  1519. /**
  1520. * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
  1521. * ECC byte
  1522. * @mtd: mtd info structure
  1523. * @eccbyte: the byte we are searching for
  1524. * @section: pointer where the section id will be stored
  1525. * @oobregion: OOB region information
  1526. *
  1527. * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
  1528. * byte.
  1529. *
  1530. * Returns zero on success, a negative error code otherwise.
  1531. */
  1532. int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  1533. int *section,
  1534. struct mtd_oob_region *oobregion)
  1535. {
  1536. return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
  1537. mtd_ooblayout_ecc);
  1538. }
  1539. EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
  1540. /**
  1541. * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
  1542. * @mtd: mtd info structure
  1543. * @buf: destination buffer to store OOB bytes
  1544. * @oobbuf: OOB buffer
  1545. * @start: first byte to retrieve
  1546. * @nbytes: number of bytes to retrieve
  1547. * @iter: section iterator
  1548. *
  1549. * Extract bytes attached to a specific category (ECC or free)
  1550. * from the OOB buffer and copy them into buf.
  1551. *
  1552. * Returns zero on success, a negative error code otherwise.
  1553. */
  1554. static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
  1555. const u8 *oobbuf, int start, int nbytes,
  1556. int (*iter)(struct mtd_info *,
  1557. int section,
  1558. struct mtd_oob_region *oobregion))
  1559. {
  1560. struct mtd_oob_region oobregion;
  1561. int section, ret;
  1562. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1563. &oobregion, iter);
  1564. while (!ret) {
  1565. int cnt;
  1566. cnt = min_t(int, nbytes, oobregion.length);
  1567. memcpy(buf, oobbuf + oobregion.offset, cnt);
  1568. buf += cnt;
  1569. nbytes -= cnt;
  1570. if (!nbytes)
  1571. break;
  1572. ret = iter(mtd, ++section, &oobregion);
  1573. }
  1574. return ret;
  1575. }
  1576. /**
  1577. * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
  1578. * @mtd: mtd info structure
  1579. * @buf: source buffer to get OOB bytes from
  1580. * @oobbuf: OOB buffer
  1581. * @start: first OOB byte to set
  1582. * @nbytes: number of OOB bytes to set
  1583. * @iter: section iterator
  1584. *
  1585. * Fill the OOB buffer with data provided in buf. The category (ECC or free)
  1586. * is selected by passing the appropriate iterator.
  1587. *
  1588. * Returns zero on success, a negative error code otherwise.
  1589. */
  1590. static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
  1591. u8 *oobbuf, int start, int nbytes,
  1592. int (*iter)(struct mtd_info *,
  1593. int section,
  1594. struct mtd_oob_region *oobregion))
  1595. {
  1596. struct mtd_oob_region oobregion;
  1597. int section, ret;
  1598. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1599. &oobregion, iter);
  1600. while (!ret) {
  1601. int cnt;
  1602. cnt = min_t(int, nbytes, oobregion.length);
  1603. memcpy(oobbuf + oobregion.offset, buf, cnt);
  1604. buf += cnt;
  1605. nbytes -= cnt;
  1606. if (!nbytes)
  1607. break;
  1608. ret = iter(mtd, ++section, &oobregion);
  1609. }
  1610. return ret;
  1611. }
  1612. /**
  1613. * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
  1614. * @mtd: mtd info structure
  1615. * @iter: category iterator
  1616. *
  1617. * Count the number of bytes in a given category.
  1618. *
  1619. * Returns a positive value on success, a negative error code otherwise.
  1620. */
  1621. static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
  1622. int (*iter)(struct mtd_info *,
  1623. int section,
  1624. struct mtd_oob_region *oobregion))
  1625. {
  1626. struct mtd_oob_region oobregion;
  1627. int section = 0, ret, nbytes = 0;
  1628. while (1) {
  1629. ret = iter(mtd, section++, &oobregion);
  1630. if (ret) {
  1631. if (ret == -ERANGE)
  1632. ret = nbytes;
  1633. break;
  1634. }
  1635. nbytes += oobregion.length;
  1636. }
  1637. return ret;
  1638. }
  1639. /**
  1640. * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
  1641. * @mtd: mtd info structure
  1642. * @eccbuf: destination buffer to store ECC bytes
  1643. * @oobbuf: OOB buffer
  1644. * @start: first ECC byte to retrieve
  1645. * @nbytes: number of ECC bytes to retrieve
  1646. *
  1647. * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
  1648. *
  1649. * Returns zero on success, a negative error code otherwise.
  1650. */
  1651. int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  1652. const u8 *oobbuf, int start, int nbytes)
  1653. {
  1654. return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1655. mtd_ooblayout_ecc);
  1656. }
  1657. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
  1658. /**
  1659. * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
  1660. * @mtd: mtd info structure
  1661. * @eccbuf: source buffer to get ECC bytes from
  1662. * @oobbuf: OOB buffer
  1663. * @start: first ECC byte to set
  1664. * @nbytes: number of ECC bytes to set
  1665. *
  1666. * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
  1667. *
  1668. * Returns zero on success, a negative error code otherwise.
  1669. */
  1670. int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  1671. u8 *oobbuf, int start, int nbytes)
  1672. {
  1673. return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1674. mtd_ooblayout_ecc);
  1675. }
  1676. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
  1677. /**
  1678. * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
  1679. * @mtd: mtd info structure
  1680. * @databuf: destination buffer to store ECC bytes
  1681. * @oobbuf: OOB buffer
  1682. * @start: first ECC byte to retrieve
  1683. * @nbytes: number of ECC bytes to retrieve
  1684. *
  1685. * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  1686. *
  1687. * Returns zero on success, a negative error code otherwise.
  1688. */
  1689. int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  1690. const u8 *oobbuf, int start, int nbytes)
  1691. {
  1692. return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
  1693. mtd_ooblayout_free);
  1694. }
  1695. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
  1696. /**
  1697. * mtd_ooblayout_set_databytes - set data bytes into the oob buffer
  1698. * @mtd: mtd info structure
  1699. * @databuf: source buffer to get data bytes from
  1700. * @oobbuf: OOB buffer
  1701. * @start: first ECC byte to set
  1702. * @nbytes: number of ECC bytes to set
  1703. *
  1704. * Works like mtd_ooblayout_set_bytes(), except it acts on free bytes.
  1705. *
  1706. * Returns zero on success, a negative error code otherwise.
  1707. */
  1708. int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  1709. u8 *oobbuf, int start, int nbytes)
  1710. {
  1711. return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
  1712. mtd_ooblayout_free);
  1713. }
  1714. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
  1715. /**
  1716. * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
  1717. * @mtd: mtd info structure
  1718. *
  1719. * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
  1720. *
  1721. * Returns zero on success, a negative error code otherwise.
  1722. */
  1723. int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
  1724. {
  1725. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
  1726. }
  1727. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
  1728. /**
  1729. * mtd_ooblayout_count_eccbytes - count the number of ECC bytes in OOB
  1730. * @mtd: mtd info structure
  1731. *
  1732. * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
  1733. *
  1734. * Returns zero on success, a negative error code otherwise.
  1735. */
  1736. int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
  1737. {
  1738. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
  1739. }
  1740. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
  1741. /*
  1742. * Method to access the protection register area, present in some flash
  1743. * devices. The user data is one time programmable but the factory data is read
  1744. * only.
  1745. */
  1746. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1747. struct otp_info *buf)
  1748. {
  1749. struct mtd_info *master = mtd_get_master(mtd);
  1750. if (!master->_get_fact_prot_info)
  1751. return -EOPNOTSUPP;
  1752. if (!len)
  1753. return 0;
  1754. return master->_get_fact_prot_info(master, len, retlen, buf);
  1755. }
  1756. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  1757. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1758. size_t *retlen, u_char *buf)
  1759. {
  1760. struct mtd_info *master = mtd_get_master(mtd);
  1761. *retlen = 0;
  1762. if (!master->_read_fact_prot_reg)
  1763. return -EOPNOTSUPP;
  1764. if (!len)
  1765. return 0;
  1766. return master->_read_fact_prot_reg(master, from, len, retlen, buf);
  1767. }
  1768. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  1769. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1770. struct otp_info *buf)
  1771. {
  1772. struct mtd_info *master = mtd_get_master(mtd);
  1773. if (!master->_get_user_prot_info)
  1774. return -EOPNOTSUPP;
  1775. if (!len)
  1776. return 0;
  1777. return master->_get_user_prot_info(master, len, retlen, buf);
  1778. }
  1779. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  1780. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1781. size_t *retlen, u_char *buf)
  1782. {
  1783. struct mtd_info *master = mtd_get_master(mtd);
  1784. *retlen = 0;
  1785. if (!master->_read_user_prot_reg)
  1786. return -EOPNOTSUPP;
  1787. if (!len)
  1788. return 0;
  1789. return master->_read_user_prot_reg(master, from, len, retlen, buf);
  1790. }
  1791. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  1792. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  1793. size_t *retlen, const u_char *buf)
  1794. {
  1795. struct mtd_info *master = mtd_get_master(mtd);
  1796. int ret;
  1797. *retlen = 0;
  1798. if (!master->_write_user_prot_reg)
  1799. return -EOPNOTSUPP;
  1800. if (!len)
  1801. return 0;
  1802. ret = master->_write_user_prot_reg(master, to, len, retlen, buf);
  1803. if (ret)
  1804. return ret;
  1805. /*
  1806. * If no data could be written at all, we are out of memory and
  1807. * must return -ENOSPC.
  1808. */
  1809. return (*retlen) ? 0 : -ENOSPC;
  1810. }
  1811. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  1812. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  1813. {
  1814. struct mtd_info *master = mtd_get_master(mtd);
  1815. if (!master->_lock_user_prot_reg)
  1816. return -EOPNOTSUPP;
  1817. if (!len)
  1818. return 0;
  1819. return master->_lock_user_prot_reg(master, from, len);
  1820. }
  1821. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  1822. int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  1823. {
  1824. struct mtd_info *master = mtd_get_master(mtd);
  1825. if (!master->_erase_user_prot_reg)
  1826. return -EOPNOTSUPP;
  1827. if (!len)
  1828. return 0;
  1829. return master->_erase_user_prot_reg(master, from, len);
  1830. }
  1831. EXPORT_SYMBOL_GPL(mtd_erase_user_prot_reg);
  1832. /* Chip-supported device locking */
  1833. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1834. {
  1835. struct mtd_info *master = mtd_get_master(mtd);
  1836. if (!master->_lock)
  1837. return -EOPNOTSUPP;
  1838. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  1839. return -EINVAL;
  1840. if (!len)
  1841. return 0;
  1842. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  1843. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1844. len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
  1845. }
  1846. return master->_lock(master, mtd_get_master_ofs(mtd, ofs), len);
  1847. }
  1848. EXPORT_SYMBOL_GPL(mtd_lock);
  1849. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1850. {
  1851. struct mtd_info *master = mtd_get_master(mtd);
  1852. if (!master->_unlock)
  1853. return -EOPNOTSUPP;
  1854. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  1855. return -EINVAL;
  1856. if (!len)
  1857. return 0;
  1858. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  1859. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1860. len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
  1861. }
  1862. return master->_unlock(master, mtd_get_master_ofs(mtd, ofs), len);
  1863. }
  1864. EXPORT_SYMBOL_GPL(mtd_unlock);
  1865. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1866. {
  1867. struct mtd_info *master = mtd_get_master(mtd);
  1868. if (!master->_is_locked)
  1869. return -EOPNOTSUPP;
  1870. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  1871. return -EINVAL;
  1872. if (!len)
  1873. return 0;
  1874. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION) {
  1875. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1876. len = (u64)mtd_div_by_eb(len, mtd) * master->erasesize;
  1877. }
  1878. return master->_is_locked(master, mtd_get_master_ofs(mtd, ofs), len);
  1879. }
  1880. EXPORT_SYMBOL_GPL(mtd_is_locked);
  1881. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  1882. {
  1883. struct mtd_info *master = mtd_get_master(mtd);
  1884. if (ofs < 0 || ofs >= mtd->size)
  1885. return -EINVAL;
  1886. if (!master->_block_isreserved)
  1887. return 0;
  1888. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1889. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1890. return master->_block_isreserved(master, mtd_get_master_ofs(mtd, ofs));
  1891. }
  1892. EXPORT_SYMBOL_GPL(mtd_block_isreserved);
  1893. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1894. {
  1895. struct mtd_info *master = mtd_get_master(mtd);
  1896. if (ofs < 0 || ofs >= mtd->size)
  1897. return -EINVAL;
  1898. if (!master->_block_isbad)
  1899. return 0;
  1900. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1901. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1902. return master->_block_isbad(master, mtd_get_master_ofs(mtd, ofs));
  1903. }
  1904. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  1905. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1906. {
  1907. struct mtd_info *master = mtd_get_master(mtd);
  1908. int ret;
  1909. if (!master->_block_markbad)
  1910. return -EOPNOTSUPP;
  1911. if (ofs < 0 || ofs >= mtd->size)
  1912. return -EINVAL;
  1913. if (!(mtd->flags & MTD_WRITEABLE))
  1914. return -EROFS;
  1915. if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
  1916. ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
  1917. ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs));
  1918. if (ret)
  1919. return ret;
  1920. while (mtd->parent) {
  1921. mtd->ecc_stats.badblocks++;
  1922. mtd = mtd->parent;
  1923. }
  1924. return 0;
  1925. }
  1926. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  1927. /*
  1928. * default_mtd_writev - the default writev method
  1929. * @mtd: mtd device description object pointer
  1930. * @vecs: the vectors to write
  1931. * @count: count of vectors in @vecs
  1932. * @to: the MTD device offset to write to
  1933. * @retlen: on exit contains the count of bytes written to the MTD device.
  1934. *
  1935. * This function returns zero in case of success and a negative error code in
  1936. * case of failure.
  1937. */
  1938. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1939. unsigned long count, loff_t to, size_t *retlen)
  1940. {
  1941. unsigned long i;
  1942. size_t totlen = 0, thislen;
  1943. int ret = 0;
  1944. for (i = 0; i < count; i++) {
  1945. if (!vecs[i].iov_len)
  1946. continue;
  1947. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  1948. vecs[i].iov_base);
  1949. totlen += thislen;
  1950. if (ret || thislen != vecs[i].iov_len)
  1951. break;
  1952. to += vecs[i].iov_len;
  1953. }
  1954. *retlen = totlen;
  1955. return ret;
  1956. }
  1957. /*
  1958. * mtd_writev - the vector-based MTD write method
  1959. * @mtd: mtd device description object pointer
  1960. * @vecs: the vectors to write
  1961. * @count: count of vectors in @vecs
  1962. * @to: the MTD device offset to write to
  1963. * @retlen: on exit contains the count of bytes written to the MTD device.
  1964. *
  1965. * This function returns zero in case of success and a negative error code in
  1966. * case of failure.
  1967. */
  1968. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1969. unsigned long count, loff_t to, size_t *retlen)
  1970. {
  1971. struct mtd_info *master = mtd_get_master(mtd);
  1972. *retlen = 0;
  1973. if (!(mtd->flags & MTD_WRITEABLE))
  1974. return -EROFS;
  1975. if (!master->_writev)
  1976. return default_mtd_writev(mtd, vecs, count, to, retlen);
  1977. return master->_writev(master, vecs, count,
  1978. mtd_get_master_ofs(mtd, to), retlen);
  1979. }
  1980. EXPORT_SYMBOL_GPL(mtd_writev);
  1981. /**
  1982. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  1983. * @mtd: mtd device description object pointer
  1984. * @size: a pointer to the ideal or maximum size of the allocation, points
  1985. * to the actual allocation size on success.
  1986. *
  1987. * This routine attempts to allocate a contiguous kernel buffer up to
  1988. * the specified size, backing off the size of the request exponentially
  1989. * until the request succeeds or until the allocation size falls below
  1990. * the system page size. This attempts to make sure it does not adversely
  1991. * impact system performance, so when allocating more than one page, we
  1992. * ask the memory allocator to avoid re-trying, swapping, writing back
  1993. * or performing I/O.
  1994. *
  1995. * Note, this function also makes sure that the allocated buffer is aligned to
  1996. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  1997. *
  1998. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  1999. * to handle smaller (i.e. degraded) buffer allocations under low- or
  2000. * fragmented-memory situations where such reduced allocations, from a
  2001. * requested ideal, are allowed.
  2002. *
  2003. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  2004. */
  2005. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  2006. {
  2007. gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY;
  2008. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  2009. void *kbuf;
  2010. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  2011. while (*size > min_alloc) {
  2012. kbuf = kmalloc(*size, flags);
  2013. if (kbuf)
  2014. return kbuf;
  2015. *size >>= 1;
  2016. *size = ALIGN(*size, mtd->writesize);
  2017. }
  2018. /*
  2019. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  2020. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  2021. */
  2022. return kmalloc(*size, GFP_KERNEL);
  2023. }
  2024. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  2025. #ifdef CONFIG_PROC_FS
  2026. /*====================================================================*/
  2027. /* Support for /proc/mtd */
  2028. static int mtd_proc_show(struct seq_file *m, void *v)
  2029. {
  2030. struct mtd_info *mtd;
  2031. seq_puts(m, "dev: size erasesize name\n");
  2032. mutex_lock(&mtd_table_mutex);
  2033. mtd_for_each_device(mtd) {
  2034. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  2035. mtd->index, (unsigned long long)mtd->size,
  2036. mtd->erasesize, mtd->name);
  2037. }
  2038. mutex_unlock(&mtd_table_mutex);
  2039. return 0;
  2040. }
  2041. #endif /* CONFIG_PROC_FS */
  2042. /*====================================================================*/
  2043. /* Init code */
  2044. static struct backing_dev_info * __init mtd_bdi_init(const char *name)
  2045. {
  2046. struct backing_dev_info *bdi;
  2047. int ret;
  2048. bdi = bdi_alloc(NUMA_NO_NODE);
  2049. if (!bdi)
  2050. return ERR_PTR(-ENOMEM);
  2051. bdi->ra_pages = 0;
  2052. bdi->io_pages = 0;
  2053. /*
  2054. * We put '-0' suffix to the name to get the same name format as we
  2055. * used to get. Since this is called only once, we get a unique name.
  2056. */
  2057. ret = bdi_register(bdi, "%.28s-0", name);
  2058. if (ret)
  2059. bdi_put(bdi);
  2060. return ret ? ERR_PTR(ret) : bdi;
  2061. }
  2062. static struct proc_dir_entry *proc_mtd;
  2063. static int __init init_mtd(void)
  2064. {
  2065. int ret;
  2066. ret = class_register(&mtd_class);
  2067. if (ret)
  2068. goto err_reg;
  2069. mtd_bdi = mtd_bdi_init("mtd");
  2070. if (IS_ERR(mtd_bdi)) {
  2071. ret = PTR_ERR(mtd_bdi);
  2072. goto err_bdi;
  2073. }
  2074. proc_mtd = proc_create_single("mtd", 0, NULL, mtd_proc_show);
  2075. ret = init_mtdchar();
  2076. if (ret)
  2077. goto out_procfs;
  2078. dfs_dir_mtd = debugfs_create_dir("mtd", NULL);
  2079. debugfs_create_bool("expert_analysis_mode", 0600, dfs_dir_mtd,
  2080. &mtd_expert_analysis_mode);
  2081. return 0;
  2082. out_procfs:
  2083. if (proc_mtd)
  2084. remove_proc_entry("mtd", NULL);
  2085. bdi_unregister(mtd_bdi);
  2086. bdi_put(mtd_bdi);
  2087. err_bdi:
  2088. class_unregister(&mtd_class);
  2089. err_reg:
  2090. pr_err("Error registering mtd class or bdi: %d\n", ret);
  2091. return ret;
  2092. }
  2093. static void __exit cleanup_mtd(void)
  2094. {
  2095. debugfs_remove_recursive(dfs_dir_mtd);
  2096. cleanup_mtdchar();
  2097. if (proc_mtd)
  2098. remove_proc_entry("mtd", NULL);
  2099. class_unregister(&mtd_class);
  2100. bdi_unregister(mtd_bdi);
  2101. bdi_put(mtd_bdi);
  2102. idr_destroy(&mtd_idr);
  2103. }
  2104. module_init(init_mtd);
  2105. module_exit(cleanup_mtd);
  2106. MODULE_LICENSE("GPL");
  2107. MODULE_AUTHOR("David Woodhouse <[email protected]>");
  2108. MODULE_DESCRIPTION("Core MTD registration and access routines");