mtdcore.c 46 KB

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