mtdcore.c 46 KB

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