mtdcore.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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/bitops.h>
  31. #include <linux/bug.h>
  32. #include <linux/err.h>
  33. #include <ubi_uboot.h>
  34. #endif
  35. #include <linux/log2.h>
  36. #include <linux/mtd/mtd.h>
  37. #include <linux/mtd/partitions.h>
  38. #include "mtdcore.h"
  39. #ifndef __UBOOT__
  40. /*
  41. * backing device capabilities for non-mappable devices (such as NAND flash)
  42. * - permits private mappings, copies are taken of the data
  43. */
  44. static struct backing_dev_info mtd_bdi_unmappable = {
  45. .capabilities = BDI_CAP_MAP_COPY,
  46. };
  47. /*
  48. * backing device capabilities for R/O mappable devices (such as ROM)
  49. * - permits private mappings, copies are taken of the data
  50. * - permits non-writable shared mappings
  51. */
  52. static struct backing_dev_info mtd_bdi_ro_mappable = {
  53. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  54. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
  55. };
  56. /*
  57. * backing device capabilities for writable mappable devices (such as RAM)
  58. * - permits private mappings, copies are taken of the data
  59. * - permits non-writable shared mappings
  60. */
  61. static struct backing_dev_info mtd_bdi_rw_mappable = {
  62. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  63. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
  64. BDI_CAP_WRITE_MAP),
  65. };
  66. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  67. static int mtd_cls_resume(struct device *dev);
  68. static struct class mtd_class = {
  69. .name = "mtd",
  70. .owner = THIS_MODULE,
  71. .suspend = mtd_cls_suspend,
  72. .resume = mtd_cls_resume,
  73. };
  74. #else
  75. #define MAX_IDR_ID 64
  76. struct idr_layer {
  77. int used;
  78. void *ptr;
  79. };
  80. struct idr {
  81. struct idr_layer id[MAX_IDR_ID];
  82. bool updated;
  83. };
  84. #define DEFINE_IDR(name) struct idr name;
  85. void idr_remove(struct idr *idp, int id)
  86. {
  87. if (idp->id[id].used) {
  88. idp->id[id].used = 0;
  89. idp->updated = true;
  90. }
  91. return;
  92. }
  93. void *idr_find(struct idr *idp, int id)
  94. {
  95. if (idp->id[id].used)
  96. return idp->id[id].ptr;
  97. return NULL;
  98. }
  99. void *idr_get_next(struct idr *idp, int *next)
  100. {
  101. void *ret;
  102. int id = *next;
  103. ret = idr_find(idp, id);
  104. if (ret) {
  105. id ++;
  106. if (!idp->id[id].used)
  107. id = 0;
  108. *next = id;
  109. } else {
  110. *next = 0;
  111. }
  112. return ret;
  113. }
  114. int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
  115. {
  116. struct idr_layer *idl;
  117. int i = 0;
  118. while (i < MAX_IDR_ID) {
  119. idl = &idp->id[i];
  120. if (idl->used == 0) {
  121. idl->used = 1;
  122. idl->ptr = ptr;
  123. idp->updated = true;
  124. return i;
  125. }
  126. i++;
  127. }
  128. return -ENOSPC;
  129. }
  130. #endif
  131. static DEFINE_IDR(mtd_idr);
  132. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  133. should not use them for _anything_ else */
  134. DEFINE_MUTEX(mtd_table_mutex);
  135. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  136. struct mtd_info *__mtd_next_device(int i)
  137. {
  138. return idr_get_next(&mtd_idr, &i);
  139. }
  140. EXPORT_SYMBOL_GPL(__mtd_next_device);
  141. bool mtd_dev_list_updated(void)
  142. {
  143. if (mtd_idr.updated) {
  144. mtd_idr.updated = false;
  145. return true;
  146. }
  147. return false;
  148. }
  149. #ifndef __UBOOT__
  150. static LIST_HEAD(mtd_notifiers);
  151. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  152. /* REVISIT once MTD uses the driver model better, whoever allocates
  153. * the mtd_info will probably want to use the release() hook...
  154. */
  155. static void mtd_release(struct device *dev)
  156. {
  157. struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
  158. dev_t index = MTD_DEVT(mtd->index);
  159. /* remove /dev/mtdXro node if needed */
  160. if (index)
  161. device_destroy(&mtd_class, index + 1);
  162. }
  163. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  164. {
  165. struct mtd_info *mtd = dev_get_drvdata(dev);
  166. return mtd ? mtd_suspend(mtd) : 0;
  167. }
  168. static int mtd_cls_resume(struct device *dev)
  169. {
  170. struct mtd_info *mtd = dev_get_drvdata(dev);
  171. if (mtd)
  172. mtd_resume(mtd);
  173. return 0;
  174. }
  175. static ssize_t mtd_type_show(struct device *dev,
  176. struct device_attribute *attr, char *buf)
  177. {
  178. struct mtd_info *mtd = dev_get_drvdata(dev);
  179. char *type;
  180. switch (mtd->type) {
  181. case MTD_ABSENT:
  182. type = "absent";
  183. break;
  184. case MTD_RAM:
  185. type = "ram";
  186. break;
  187. case MTD_ROM:
  188. type = "rom";
  189. break;
  190. case MTD_NORFLASH:
  191. type = "nor";
  192. break;
  193. case MTD_NANDFLASH:
  194. type = "nand";
  195. break;
  196. case MTD_DATAFLASH:
  197. type = "dataflash";
  198. break;
  199. case MTD_UBIVOLUME:
  200. type = "ubi";
  201. break;
  202. case MTD_MLCNANDFLASH:
  203. type = "mlc-nand";
  204. break;
  205. default:
  206. type = "unknown";
  207. }
  208. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  209. }
  210. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  211. static ssize_t mtd_flags_show(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct mtd_info *mtd = dev_get_drvdata(dev);
  215. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  216. }
  217. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  218. static ssize_t mtd_size_show(struct device *dev,
  219. struct device_attribute *attr, char *buf)
  220. {
  221. struct mtd_info *mtd = dev_get_drvdata(dev);
  222. return snprintf(buf, PAGE_SIZE, "%llu\n",
  223. (unsigned long long)mtd->size);
  224. }
  225. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  226. static ssize_t mtd_erasesize_show(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct mtd_info *mtd = dev_get_drvdata(dev);
  230. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  231. }
  232. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  233. static ssize_t mtd_writesize_show(struct device *dev,
  234. struct device_attribute *attr, char *buf)
  235. {
  236. struct mtd_info *mtd = dev_get_drvdata(dev);
  237. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  238. }
  239. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  240. static ssize_t mtd_subpagesize_show(struct device *dev,
  241. struct device_attribute *attr, char *buf)
  242. {
  243. struct mtd_info *mtd = dev_get_drvdata(dev);
  244. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  245. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  246. }
  247. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  248. static ssize_t mtd_oobsize_show(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. struct mtd_info *mtd = dev_get_drvdata(dev);
  252. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  253. }
  254. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  255. static ssize_t mtd_numeraseregions_show(struct device *dev,
  256. struct device_attribute *attr, char *buf)
  257. {
  258. struct mtd_info *mtd = dev_get_drvdata(dev);
  259. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  260. }
  261. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  262. NULL);
  263. static ssize_t mtd_name_show(struct device *dev,
  264. struct device_attribute *attr, char *buf)
  265. {
  266. struct mtd_info *mtd = dev_get_drvdata(dev);
  267. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  268. }
  269. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  270. static ssize_t mtd_ecc_strength_show(struct device *dev,
  271. struct device_attribute *attr, char *buf)
  272. {
  273. struct mtd_info *mtd = dev_get_drvdata(dev);
  274. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
  275. }
  276. static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
  277. static ssize_t mtd_bitflip_threshold_show(struct device *dev,
  278. struct device_attribute *attr,
  279. char *buf)
  280. {
  281. struct mtd_info *mtd = dev_get_drvdata(dev);
  282. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
  283. }
  284. static ssize_t mtd_bitflip_threshold_store(struct device *dev,
  285. struct device_attribute *attr,
  286. const char *buf, size_t count)
  287. {
  288. struct mtd_info *mtd = dev_get_drvdata(dev);
  289. unsigned int bitflip_threshold;
  290. int retval;
  291. retval = kstrtouint(buf, 0, &bitflip_threshold);
  292. if (retval)
  293. return retval;
  294. mtd->bitflip_threshold = bitflip_threshold;
  295. return count;
  296. }
  297. static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
  298. mtd_bitflip_threshold_show,
  299. mtd_bitflip_threshold_store);
  300. static ssize_t mtd_ecc_step_size_show(struct device *dev,
  301. struct device_attribute *attr, char *buf)
  302. {
  303. struct mtd_info *mtd = dev_get_drvdata(dev);
  304. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
  305. }
  306. static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
  307. static struct attribute *mtd_attrs[] = {
  308. &dev_attr_type.attr,
  309. &dev_attr_flags.attr,
  310. &dev_attr_size.attr,
  311. &dev_attr_erasesize.attr,
  312. &dev_attr_writesize.attr,
  313. &dev_attr_subpagesize.attr,
  314. &dev_attr_oobsize.attr,
  315. &dev_attr_numeraseregions.attr,
  316. &dev_attr_name.attr,
  317. &dev_attr_ecc_strength.attr,
  318. &dev_attr_ecc_step_size.attr,
  319. &dev_attr_bitflip_threshold.attr,
  320. NULL,
  321. };
  322. ATTRIBUTE_GROUPS(mtd);
  323. static struct device_type mtd_devtype = {
  324. .name = "mtd",
  325. .groups = mtd_groups,
  326. .release = mtd_release,
  327. };
  328. #endif
  329. /**
  330. * add_mtd_device - register an MTD device
  331. * @mtd: pointer to new MTD device info structure
  332. *
  333. * Add a device to the list of MTD devices present in the system, and
  334. * notify each currently active MTD 'user' of its arrival. Returns
  335. * zero on success or 1 on failure, which currently will only happen
  336. * if there is insufficient memory or a sysfs error.
  337. */
  338. int add_mtd_device(struct mtd_info *mtd)
  339. {
  340. #ifndef __UBOOT__
  341. struct mtd_notifier *not;
  342. #endif
  343. int i, error;
  344. #ifndef __UBOOT__
  345. if (!mtd->backing_dev_info) {
  346. switch (mtd->type) {
  347. case MTD_RAM:
  348. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  349. break;
  350. case MTD_ROM:
  351. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  352. break;
  353. default:
  354. mtd->backing_dev_info = &mtd_bdi_unmappable;
  355. break;
  356. }
  357. }
  358. #endif
  359. BUG_ON(mtd->writesize == 0);
  360. mutex_lock(&mtd_table_mutex);
  361. i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
  362. if (i < 0)
  363. goto fail_locked;
  364. mtd->index = i;
  365. mtd->usecount = 0;
  366. INIT_LIST_HEAD(&mtd->partitions);
  367. /* default value if not set by driver */
  368. if (mtd->bitflip_threshold == 0)
  369. mtd->bitflip_threshold = mtd->ecc_strength;
  370. if (is_power_of_2(mtd->erasesize))
  371. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  372. else
  373. mtd->erasesize_shift = 0;
  374. if (is_power_of_2(mtd->writesize))
  375. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  376. else
  377. mtd->writesize_shift = 0;
  378. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  379. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  380. /* Some chips always power up locked. Unlock them now */
  381. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  382. error = mtd_unlock(mtd, 0, mtd->size);
  383. if (error && error != -EOPNOTSUPP)
  384. printk(KERN_WARNING
  385. "%s: unlock failed, writes may not work\n",
  386. mtd->name);
  387. }
  388. #ifndef __UBOOT__
  389. /* Caller should have set dev.parent to match the
  390. * physical device.
  391. */
  392. mtd->dev.type = &mtd_devtype;
  393. mtd->dev.class = &mtd_class;
  394. mtd->dev.devt = MTD_DEVT(i);
  395. dev_set_name(&mtd->dev, "mtd%d", i);
  396. dev_set_drvdata(&mtd->dev, mtd);
  397. if (device_register(&mtd->dev) != 0)
  398. goto fail_added;
  399. if (MTD_DEVT(i))
  400. device_create(&mtd_class, mtd->dev.parent,
  401. MTD_DEVT(i) + 1,
  402. NULL, "mtd%dro", i);
  403. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  404. /* No need to get a refcount on the module containing
  405. the notifier, since we hold the mtd_table_mutex */
  406. list_for_each_entry(not, &mtd_notifiers, list)
  407. not->add(mtd);
  408. #else
  409. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  410. #endif
  411. mutex_unlock(&mtd_table_mutex);
  412. /* We _know_ we aren't being removed, because
  413. our caller is still holding us here. So none
  414. of this try_ nonsense, and no bitching about it
  415. either. :) */
  416. __module_get(THIS_MODULE);
  417. return 0;
  418. #ifndef __UBOOT__
  419. fail_added:
  420. idr_remove(&mtd_idr, i);
  421. #endif
  422. fail_locked:
  423. mutex_unlock(&mtd_table_mutex);
  424. return 1;
  425. }
  426. /**
  427. * del_mtd_device - unregister an MTD device
  428. * @mtd: pointer to MTD device info structure
  429. *
  430. * Remove a device from the list of MTD devices present in the system,
  431. * and notify each currently active MTD 'user' of its departure.
  432. * Returns zero on success or 1 on failure, which currently will happen
  433. * if the requested device does not appear to be present in the list.
  434. */
  435. int del_mtd_device(struct mtd_info *mtd)
  436. {
  437. int ret;
  438. #ifndef __UBOOT__
  439. struct mtd_notifier *not;
  440. #endif
  441. ret = del_mtd_partitions(mtd);
  442. if (ret) {
  443. debug("Failed to delete MTD partitions attached to %s (err %d)\n",
  444. mtd->name, ret);
  445. return ret;
  446. }
  447. mutex_lock(&mtd_table_mutex);
  448. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  449. ret = -ENODEV;
  450. goto out_error;
  451. }
  452. #ifndef __UBOOT__
  453. /* No need to get a refcount on the module containing
  454. the notifier, since we hold the mtd_table_mutex */
  455. list_for_each_entry(not, &mtd_notifiers, list)
  456. not->remove(mtd);
  457. #endif
  458. if (mtd->usecount) {
  459. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  460. mtd->index, mtd->name, mtd->usecount);
  461. ret = -EBUSY;
  462. } else {
  463. #ifndef __UBOOT__
  464. device_unregister(&mtd->dev);
  465. #endif
  466. idr_remove(&mtd_idr, mtd->index);
  467. module_put(THIS_MODULE);
  468. ret = 0;
  469. }
  470. out_error:
  471. mutex_unlock(&mtd_table_mutex);
  472. return ret;
  473. }
  474. #ifndef __UBOOT__
  475. /**
  476. * mtd_device_parse_register - parse partitions and register an MTD device.
  477. *
  478. * @mtd: the MTD device to register
  479. * @types: the list of MTD partition probes to try, see
  480. * 'parse_mtd_partitions()' for more information
  481. * @parser_data: MTD partition parser-specific data
  482. * @parts: fallback partition information to register, if parsing fails;
  483. * only valid if %nr_parts > %0
  484. * @nr_parts: the number of partitions in parts, if zero then the full
  485. * MTD device is registered if no partition info is found
  486. *
  487. * This function aggregates MTD partitions parsing (done by
  488. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  489. * basically follows the most common pattern found in many MTD drivers:
  490. *
  491. * * It first tries to probe partitions on MTD device @mtd using parsers
  492. * specified in @types (if @types is %NULL, then the default list of parsers
  493. * is used, see 'parse_mtd_partitions()' for more information). If none are
  494. * found this functions tries to fallback to information specified in
  495. * @parts/@nr_parts.
  496. * * If any partitioning info was found, this function registers the found
  497. * partitions.
  498. * * If no partitions were found this function just registers the MTD device
  499. * @mtd and exits.
  500. *
  501. * Returns zero in case of success and a negative error code in case of failure.
  502. */
  503. int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
  504. struct mtd_part_parser_data *parser_data,
  505. const struct mtd_partition *parts,
  506. int nr_parts)
  507. {
  508. int err;
  509. struct mtd_partition *real_parts;
  510. err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
  511. if (err <= 0 && nr_parts && parts) {
  512. real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
  513. GFP_KERNEL);
  514. if (!real_parts)
  515. err = -ENOMEM;
  516. else
  517. err = nr_parts;
  518. }
  519. if (err > 0) {
  520. err = add_mtd_partitions(mtd, real_parts, err);
  521. kfree(real_parts);
  522. } else if (err == 0) {
  523. err = add_mtd_device(mtd);
  524. if (err == 1)
  525. err = -ENODEV;
  526. }
  527. return err;
  528. }
  529. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  530. /**
  531. * mtd_device_unregister - unregister an existing MTD device.
  532. *
  533. * @master: the MTD device to unregister. This will unregister both the master
  534. * and any partitions if registered.
  535. */
  536. int mtd_device_unregister(struct mtd_info *master)
  537. {
  538. int err;
  539. err = del_mtd_partitions(master);
  540. if (err)
  541. return err;
  542. if (!device_is_registered(&master->dev))
  543. return 0;
  544. return del_mtd_device(master);
  545. }
  546. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  547. /**
  548. * register_mtd_user - register a 'user' of MTD devices.
  549. * @new: pointer to notifier info structure
  550. *
  551. * Registers a pair of callbacks function to be called upon addition
  552. * or removal of MTD devices. Causes the 'add' callback to be immediately
  553. * invoked for each MTD device currently present in the system.
  554. */
  555. void register_mtd_user (struct mtd_notifier *new)
  556. {
  557. struct mtd_info *mtd;
  558. mutex_lock(&mtd_table_mutex);
  559. list_add(&new->list, &mtd_notifiers);
  560. __module_get(THIS_MODULE);
  561. mtd_for_each_device(mtd)
  562. new->add(mtd);
  563. mutex_unlock(&mtd_table_mutex);
  564. }
  565. EXPORT_SYMBOL_GPL(register_mtd_user);
  566. /**
  567. * unregister_mtd_user - unregister a 'user' of MTD devices.
  568. * @old: pointer to notifier info structure
  569. *
  570. * Removes a callback function pair from the list of 'users' to be
  571. * notified upon addition or removal of MTD devices. Causes the
  572. * 'remove' callback to be immediately invoked for each MTD device
  573. * currently present in the system.
  574. */
  575. int unregister_mtd_user (struct mtd_notifier *old)
  576. {
  577. struct mtd_info *mtd;
  578. mutex_lock(&mtd_table_mutex);
  579. module_put(THIS_MODULE);
  580. mtd_for_each_device(mtd)
  581. old->remove(mtd);
  582. list_del(&old->list);
  583. mutex_unlock(&mtd_table_mutex);
  584. return 0;
  585. }
  586. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  587. #endif
  588. /**
  589. * get_mtd_device - obtain a validated handle for an MTD device
  590. * @mtd: last known address of the required MTD device
  591. * @num: internal device number of the required MTD device
  592. *
  593. * Given a number and NULL address, return the num'th entry in the device
  594. * table, if any. Given an address and num == -1, search the device table
  595. * for a device with that address and return if it's still present. Given
  596. * both, return the num'th driver only if its address matches. Return
  597. * error code if not.
  598. */
  599. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  600. {
  601. struct mtd_info *ret = NULL, *other;
  602. int err = -ENODEV;
  603. mutex_lock(&mtd_table_mutex);
  604. if (num == -1) {
  605. mtd_for_each_device(other) {
  606. if (other == mtd) {
  607. ret = mtd;
  608. break;
  609. }
  610. }
  611. } else if (num >= 0) {
  612. ret = idr_find(&mtd_idr, num);
  613. if (mtd && mtd != ret)
  614. ret = NULL;
  615. }
  616. if (!ret) {
  617. ret = ERR_PTR(err);
  618. goto out;
  619. }
  620. err = __get_mtd_device(ret);
  621. if (err)
  622. ret = ERR_PTR(err);
  623. out:
  624. mutex_unlock(&mtd_table_mutex);
  625. return ret;
  626. }
  627. EXPORT_SYMBOL_GPL(get_mtd_device);
  628. int __get_mtd_device(struct mtd_info *mtd)
  629. {
  630. int err;
  631. if (!try_module_get(mtd->owner))
  632. return -ENODEV;
  633. if (mtd->_get_device) {
  634. err = mtd->_get_device(mtd);
  635. if (err) {
  636. module_put(mtd->owner);
  637. return err;
  638. }
  639. }
  640. mtd->usecount++;
  641. return 0;
  642. }
  643. EXPORT_SYMBOL_GPL(__get_mtd_device);
  644. /**
  645. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  646. * device name
  647. * @name: MTD device name to open
  648. *
  649. * This function returns MTD device description structure in case of
  650. * success and an error code in case of failure.
  651. */
  652. struct mtd_info *get_mtd_device_nm(const char *name)
  653. {
  654. int err = -ENODEV;
  655. struct mtd_info *mtd = NULL, *other;
  656. mutex_lock(&mtd_table_mutex);
  657. mtd_for_each_device(other) {
  658. if (!strcmp(name, other->name)) {
  659. mtd = other;
  660. break;
  661. }
  662. }
  663. if (!mtd)
  664. goto out_unlock;
  665. err = __get_mtd_device(mtd);
  666. if (err)
  667. goto out_unlock;
  668. mutex_unlock(&mtd_table_mutex);
  669. return mtd;
  670. out_unlock:
  671. mutex_unlock(&mtd_table_mutex);
  672. return ERR_PTR(err);
  673. }
  674. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  675. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  676. /**
  677. * mtd_get_len_incl_bad
  678. *
  679. * Check if length including bad blocks fits into device.
  680. *
  681. * @param mtd an MTD device
  682. * @param offset offset in flash
  683. * @param length image length
  684. * @return image length including bad blocks in *len_incl_bad and whether or not
  685. * the length returned was truncated in *truncated
  686. */
  687. void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
  688. const uint64_t length, uint64_t *len_incl_bad,
  689. int *truncated)
  690. {
  691. *truncated = 0;
  692. *len_incl_bad = 0;
  693. if (!mtd->_block_isbad) {
  694. *len_incl_bad = length;
  695. return;
  696. }
  697. uint64_t len_excl_bad = 0;
  698. uint64_t block_len;
  699. while (len_excl_bad < length) {
  700. if (offset >= mtd->size) {
  701. *truncated = 1;
  702. return;
  703. }
  704. block_len = mtd->erasesize - (offset & (mtd->erasesize - 1));
  705. if (!mtd->_block_isbad(mtd, offset & ~(mtd->erasesize - 1)))
  706. len_excl_bad += block_len;
  707. *len_incl_bad += block_len;
  708. offset += block_len;
  709. }
  710. }
  711. #endif /* defined(CONFIG_CMD_MTDPARTS_SPREAD) */
  712. void put_mtd_device(struct mtd_info *mtd)
  713. {
  714. mutex_lock(&mtd_table_mutex);
  715. __put_mtd_device(mtd);
  716. mutex_unlock(&mtd_table_mutex);
  717. }
  718. EXPORT_SYMBOL_GPL(put_mtd_device);
  719. void __put_mtd_device(struct mtd_info *mtd)
  720. {
  721. --mtd->usecount;
  722. BUG_ON(mtd->usecount < 0);
  723. if (mtd->_put_device)
  724. mtd->_put_device(mtd);
  725. module_put(mtd->owner);
  726. }
  727. EXPORT_SYMBOL_GPL(__put_mtd_device);
  728. /*
  729. * Erase is an asynchronous operation. Device drivers are supposed
  730. * to call instr->callback() whenever the operation completes, even
  731. * if it completes with a failure.
  732. * Callers are supposed to pass a callback function and wait for it
  733. * to be called before writing to the block.
  734. */
  735. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  736. {
  737. if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
  738. return -EINVAL;
  739. if (!(mtd->flags & MTD_WRITEABLE))
  740. return -EROFS;
  741. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  742. if (!instr->len) {
  743. instr->state = MTD_ERASE_DONE;
  744. mtd_erase_callback(instr);
  745. return 0;
  746. }
  747. return mtd->_erase(mtd, instr);
  748. }
  749. EXPORT_SYMBOL_GPL(mtd_erase);
  750. #ifndef __UBOOT__
  751. /*
  752. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  753. */
  754. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  755. void **virt, resource_size_t *phys)
  756. {
  757. *retlen = 0;
  758. *virt = NULL;
  759. if (phys)
  760. *phys = 0;
  761. if (!mtd->_point)
  762. return -EOPNOTSUPP;
  763. if (from < 0 || from > mtd->size || len > mtd->size - from)
  764. return -EINVAL;
  765. if (!len)
  766. return 0;
  767. return mtd->_point(mtd, from, len, retlen, virt, phys);
  768. }
  769. EXPORT_SYMBOL_GPL(mtd_point);
  770. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  771. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  772. {
  773. if (!mtd->_point)
  774. return -EOPNOTSUPP;
  775. if (from < 0 || from > mtd->size || len > mtd->size - from)
  776. return -EINVAL;
  777. if (!len)
  778. return 0;
  779. return mtd->_unpoint(mtd, from, len);
  780. }
  781. EXPORT_SYMBOL_GPL(mtd_unpoint);
  782. #endif
  783. /*
  784. * Allow NOMMU mmap() to directly map the device (if not NULL)
  785. * - return the address to which the offset maps
  786. * - return -ENOSYS to indicate refusal to do the mapping
  787. */
  788. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  789. unsigned long offset, unsigned long flags)
  790. {
  791. if (!mtd->_get_unmapped_area)
  792. return -EOPNOTSUPP;
  793. if (offset > mtd->size || len > mtd->size - offset)
  794. return -EINVAL;
  795. return mtd->_get_unmapped_area(mtd, len, offset, flags);
  796. }
  797. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  798. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  799. u_char *buf)
  800. {
  801. int ret_code;
  802. *retlen = 0;
  803. if (from < 0 || from > mtd->size || len > mtd->size - from)
  804. return -EINVAL;
  805. if (!len)
  806. return 0;
  807. /*
  808. * In the absence of an error, drivers return a non-negative integer
  809. * representing the maximum number of bitflips that were corrected on
  810. * any one ecc region (if applicable; zero otherwise).
  811. */
  812. if (mtd->_read) {
  813. ret_code = mtd->_read(mtd, from, len, retlen, buf);
  814. } else if (mtd->_read_oob) {
  815. struct mtd_oob_ops ops = {
  816. .len = len,
  817. .datbuf = buf,
  818. };
  819. ret_code = mtd->_read_oob(mtd, from, &ops);
  820. *retlen = ops.retlen;
  821. } else {
  822. return -ENOTSUPP;
  823. }
  824. if (unlikely(ret_code < 0))
  825. return ret_code;
  826. if (mtd->ecc_strength == 0)
  827. return 0; /* device lacks ecc */
  828. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  829. }
  830. EXPORT_SYMBOL_GPL(mtd_read);
  831. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  832. const u_char *buf)
  833. {
  834. *retlen = 0;
  835. if (to < 0 || to > mtd->size || len > mtd->size - to)
  836. return -EINVAL;
  837. if ((!mtd->_write && !mtd->_write_oob) ||
  838. !(mtd->flags & MTD_WRITEABLE))
  839. return -EROFS;
  840. if (!len)
  841. return 0;
  842. if (!mtd->_write) {
  843. struct mtd_oob_ops ops = {
  844. .len = len,
  845. .datbuf = (u8 *)buf,
  846. };
  847. int ret;
  848. ret = mtd->_write_oob(mtd, to, &ops);
  849. *retlen = ops.retlen;
  850. return ret;
  851. }
  852. return mtd->_write(mtd, to, len, retlen, buf);
  853. }
  854. EXPORT_SYMBOL_GPL(mtd_write);
  855. /*
  856. * In blackbox flight recorder like scenarios we want to make successful writes
  857. * in interrupt context. panic_write() is only intended to be called when its
  858. * known the kernel is about to panic and we need the write to succeed. Since
  859. * the kernel is not going to be running for much longer, this function can
  860. * break locks and delay to ensure the write succeeds (but not sleep).
  861. */
  862. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  863. const u_char *buf)
  864. {
  865. *retlen = 0;
  866. if (!mtd->_panic_write)
  867. return -EOPNOTSUPP;
  868. if (to < 0 || to > mtd->size || len > mtd->size - to)
  869. return -EINVAL;
  870. if (!(mtd->flags & MTD_WRITEABLE))
  871. return -EROFS;
  872. if (!len)
  873. return 0;
  874. return mtd->_panic_write(mtd, to, len, retlen, buf);
  875. }
  876. EXPORT_SYMBOL_GPL(mtd_panic_write);
  877. static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
  878. struct mtd_oob_ops *ops)
  879. {
  880. /*
  881. * Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
  882. * ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
  883. * this case.
  884. */
  885. if (!ops->datbuf)
  886. ops->len = 0;
  887. if (!ops->oobbuf)
  888. ops->ooblen = 0;
  889. if (offs < 0 || offs + ops->len > mtd->size)
  890. return -EINVAL;
  891. if (ops->ooblen) {
  892. size_t maxooblen;
  893. if (ops->ooboffs >= mtd_oobavail(mtd, ops))
  894. return -EINVAL;
  895. maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
  896. mtd_div_by_ws(offs, mtd)) *
  897. mtd_oobavail(mtd, ops)) - ops->ooboffs;
  898. if (ops->ooblen > maxooblen)
  899. return -EINVAL;
  900. }
  901. return 0;
  902. }
  903. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  904. {
  905. int ret_code;
  906. ops->retlen = ops->oobretlen = 0;
  907. ret_code = mtd_check_oob_ops(mtd, from, ops);
  908. if (ret_code)
  909. return ret_code;
  910. /* Check the validity of a potential fallback on mtd->_read */
  911. if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))
  912. return -EOPNOTSUPP;
  913. if (mtd->_read_oob)
  914. ret_code = mtd->_read_oob(mtd, from, ops);
  915. else
  916. ret_code = mtd->_read(mtd, from, ops->len, &ops->retlen,
  917. ops->datbuf);
  918. /*
  919. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  920. * similar to mtd->_read(), returning a non-negative integer
  921. * representing max bitflips. In other cases, mtd->_read_oob() may
  922. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  923. */
  924. if (unlikely(ret_code < 0))
  925. return ret_code;
  926. if (mtd->ecc_strength == 0)
  927. return 0; /* device lacks ecc */
  928. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  929. }
  930. EXPORT_SYMBOL_GPL(mtd_read_oob);
  931. int mtd_write_oob(struct mtd_info *mtd, loff_t to,
  932. struct mtd_oob_ops *ops)
  933. {
  934. int ret;
  935. ops->retlen = ops->oobretlen = 0;
  936. if (!(mtd->flags & MTD_WRITEABLE))
  937. return -EROFS;
  938. ret = mtd_check_oob_ops(mtd, to, ops);
  939. if (ret)
  940. return ret;
  941. /* Check the validity of a potential fallback on mtd->_write */
  942. if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))
  943. return -EOPNOTSUPP;
  944. if (mtd->_write_oob)
  945. return mtd->_write_oob(mtd, to, ops);
  946. else
  947. return mtd->_write(mtd, to, ops->len, &ops->retlen,
  948. ops->datbuf);
  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->rfree)
  1002. return -ENOTSUPP;
  1003. return mtd->ooblayout->rfree(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. #ifndef __UBOOT__
  1410. /*
  1411. * default_mtd_writev - the default writev method
  1412. * @mtd: mtd device description object pointer
  1413. * @vecs: the vectors to write
  1414. * @count: count of vectors in @vecs
  1415. * @to: the MTD device offset to write to
  1416. * @retlen: on exit contains the count of bytes written to the MTD device.
  1417. *
  1418. * This function returns zero in case of success and a negative error code in
  1419. * case of failure.
  1420. */
  1421. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1422. unsigned long count, loff_t to, size_t *retlen)
  1423. {
  1424. unsigned long i;
  1425. size_t totlen = 0, thislen;
  1426. int ret = 0;
  1427. for (i = 0; i < count; i++) {
  1428. if (!vecs[i].iov_len)
  1429. continue;
  1430. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  1431. vecs[i].iov_base);
  1432. totlen += thislen;
  1433. if (ret || thislen != vecs[i].iov_len)
  1434. break;
  1435. to += vecs[i].iov_len;
  1436. }
  1437. *retlen = totlen;
  1438. return ret;
  1439. }
  1440. /*
  1441. * mtd_writev - the vector-based MTD write method
  1442. * @mtd: mtd device description object pointer
  1443. * @vecs: the vectors to write
  1444. * @count: count of vectors in @vecs
  1445. * @to: the MTD device offset to write to
  1446. * @retlen: on exit contains the count of bytes written to the MTD device.
  1447. *
  1448. * This function returns zero in case of success and a negative error code in
  1449. * case of failure.
  1450. */
  1451. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1452. unsigned long count, loff_t to, size_t *retlen)
  1453. {
  1454. *retlen = 0;
  1455. if (!(mtd->flags & MTD_WRITEABLE))
  1456. return -EROFS;
  1457. if (!mtd->_writev)
  1458. return default_mtd_writev(mtd, vecs, count, to, retlen);
  1459. return mtd->_writev(mtd, vecs, count, to, retlen);
  1460. }
  1461. EXPORT_SYMBOL_GPL(mtd_writev);
  1462. /**
  1463. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  1464. * @mtd: mtd device description object pointer
  1465. * @size: a pointer to the ideal or maximum size of the allocation, points
  1466. * to the actual allocation size on success.
  1467. *
  1468. * This routine attempts to allocate a contiguous kernel buffer up to
  1469. * the specified size, backing off the size of the request exponentially
  1470. * until the request succeeds or until the allocation size falls below
  1471. * the system page size. This attempts to make sure it does not adversely
  1472. * impact system performance, so when allocating more than one page, we
  1473. * ask the memory allocator to avoid re-trying, swapping, writing back
  1474. * or performing I/O.
  1475. *
  1476. * Note, this function also makes sure that the allocated buffer is aligned to
  1477. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  1478. *
  1479. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  1480. * to handle smaller (i.e. degraded) buffer allocations under low- or
  1481. * fragmented-memory situations where such reduced allocations, from a
  1482. * requested ideal, are allowed.
  1483. *
  1484. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  1485. */
  1486. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  1487. {
  1488. gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
  1489. __GFP_NORETRY | __GFP_NO_KSWAPD;
  1490. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  1491. void *kbuf;
  1492. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  1493. while (*size > min_alloc) {
  1494. kbuf = kmalloc(*size, flags);
  1495. if (kbuf)
  1496. return kbuf;
  1497. *size >>= 1;
  1498. *size = ALIGN(*size, mtd->writesize);
  1499. }
  1500. /*
  1501. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  1502. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  1503. */
  1504. return kmalloc(*size, GFP_KERNEL);
  1505. }
  1506. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  1507. #endif
  1508. #ifdef CONFIG_PROC_FS
  1509. /*====================================================================*/
  1510. /* Support for /proc/mtd */
  1511. static int mtd_proc_show(struct seq_file *m, void *v)
  1512. {
  1513. struct mtd_info *mtd;
  1514. seq_puts(m, "dev: size erasesize name\n");
  1515. mutex_lock(&mtd_table_mutex);
  1516. mtd_for_each_device(mtd) {
  1517. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  1518. mtd->index, (unsigned long long)mtd->size,
  1519. mtd->erasesize, mtd->name);
  1520. }
  1521. mutex_unlock(&mtd_table_mutex);
  1522. return 0;
  1523. }
  1524. static int mtd_proc_open(struct inode *inode, struct file *file)
  1525. {
  1526. return single_open(file, mtd_proc_show, NULL);
  1527. }
  1528. static const struct file_operations mtd_proc_ops = {
  1529. .open = mtd_proc_open,
  1530. .read = seq_read,
  1531. .llseek = seq_lseek,
  1532. .release = single_release,
  1533. };
  1534. #endif /* CONFIG_PROC_FS */
  1535. /*====================================================================*/
  1536. /* Init code */
  1537. #ifndef __UBOOT__
  1538. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  1539. {
  1540. int ret;
  1541. ret = bdi_init(bdi);
  1542. if (!ret)
  1543. ret = bdi_register(bdi, NULL, "%s", name);
  1544. if (ret)
  1545. bdi_destroy(bdi);
  1546. return ret;
  1547. }
  1548. static struct proc_dir_entry *proc_mtd;
  1549. static int __init init_mtd(void)
  1550. {
  1551. int ret;
  1552. ret = class_register(&mtd_class);
  1553. if (ret)
  1554. goto err_reg;
  1555. ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
  1556. if (ret)
  1557. goto err_bdi1;
  1558. ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
  1559. if (ret)
  1560. goto err_bdi2;
  1561. ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
  1562. if (ret)
  1563. goto err_bdi3;
  1564. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  1565. ret = init_mtdchar();
  1566. if (ret)
  1567. goto out_procfs;
  1568. return 0;
  1569. out_procfs:
  1570. if (proc_mtd)
  1571. remove_proc_entry("mtd", NULL);
  1572. err_bdi3:
  1573. bdi_destroy(&mtd_bdi_ro_mappable);
  1574. err_bdi2:
  1575. bdi_destroy(&mtd_bdi_unmappable);
  1576. err_bdi1:
  1577. class_unregister(&mtd_class);
  1578. err_reg:
  1579. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1580. return ret;
  1581. }
  1582. static void __exit cleanup_mtd(void)
  1583. {
  1584. cleanup_mtdchar();
  1585. if (proc_mtd)
  1586. remove_proc_entry("mtd", NULL);
  1587. class_unregister(&mtd_class);
  1588. bdi_destroy(&mtd_bdi_unmappable);
  1589. bdi_destroy(&mtd_bdi_ro_mappable);
  1590. bdi_destroy(&mtd_bdi_rw_mappable);
  1591. }
  1592. module_init(init_mtd);
  1593. module_exit(cleanup_mtd);
  1594. #endif
  1595. MODULE_LICENSE("GPL");
  1596. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1597. MODULE_DESCRIPTION("Core MTD registration and access routines");