mtdcore.c 46 KB

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