mtdcore.c 49 KB

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