mtdcore.c 49 KB

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