mtdcore.c 46 KB

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