mtdcore.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  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. * SPDX-License-Identifier: GPL-2.0+
  9. *
  10. */
  11. #ifndef __UBOOT__
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/string.h>
  17. #include <linux/timer.h>
  18. #include <linux/major.h>
  19. #include <linux/fs.h>
  20. #include <linux/err.h>
  21. #include <linux/ioctl.h>
  22. #include <linux/init.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/idr.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/gfp.h>
  27. #include <linux/slab.h>
  28. #else
  29. #include <linux/err.h>
  30. #include <ubi_uboot.h>
  31. #endif
  32. #include <linux/log2.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/partitions.h>
  35. #include "mtdcore.h"
  36. #ifndef __UBOOT__
  37. /*
  38. * backing device capabilities for non-mappable devices (such as NAND flash)
  39. * - permits private mappings, copies are taken of the data
  40. */
  41. static struct backing_dev_info mtd_bdi_unmappable = {
  42. .capabilities = BDI_CAP_MAP_COPY,
  43. };
  44. /*
  45. * backing device capabilities for R/O mappable devices (such as ROM)
  46. * - permits private mappings, copies are taken of the data
  47. * - permits non-writable shared mappings
  48. */
  49. static struct backing_dev_info mtd_bdi_ro_mappable = {
  50. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  51. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
  52. };
  53. /*
  54. * backing device capabilities for writable mappable devices (such as RAM)
  55. * - permits private mappings, copies are taken of the data
  56. * - permits non-writable shared mappings
  57. */
  58. static struct backing_dev_info mtd_bdi_rw_mappable = {
  59. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  60. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
  61. BDI_CAP_WRITE_MAP),
  62. };
  63. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  64. static int mtd_cls_resume(struct device *dev);
  65. static struct class mtd_class = {
  66. .name = "mtd",
  67. .owner = THIS_MODULE,
  68. .suspend = mtd_cls_suspend,
  69. .resume = mtd_cls_resume,
  70. };
  71. #else
  72. struct mtd_info *mtd_table[MAX_MTD_DEVICES];
  73. #define MAX_IDR_ID 64
  74. struct idr_layer {
  75. int used;
  76. void *ptr;
  77. };
  78. struct idr {
  79. struct idr_layer id[MAX_IDR_ID];
  80. };
  81. #define DEFINE_IDR(name) struct idr name;
  82. void idr_remove(struct idr *idp, int id)
  83. {
  84. if (idp->id[id].used)
  85. idp->id[id].used = 0;
  86. return;
  87. }
  88. void *idr_find(struct idr *idp, int id)
  89. {
  90. if (idp->id[id].used)
  91. return idp->id[id].ptr;
  92. return NULL;
  93. }
  94. void *idr_get_next(struct idr *idp, int *next)
  95. {
  96. void *ret;
  97. int id = *next;
  98. ret = idr_find(idp, id);
  99. if (ret) {
  100. id ++;
  101. if (!idp->id[id].used)
  102. id = 0;
  103. *next = id;
  104. } else {
  105. *next = 0;
  106. }
  107. return ret;
  108. }
  109. int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask)
  110. {
  111. struct idr_layer *idl;
  112. int i = 0;
  113. while (i < MAX_IDR_ID) {
  114. idl = &idp->id[i];
  115. if (idl->used == 0) {
  116. idl->used = 1;
  117. idl->ptr = ptr;
  118. return i;
  119. }
  120. i++;
  121. }
  122. return -ENOSPC;
  123. }
  124. #endif
  125. static DEFINE_IDR(mtd_idr);
  126. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  127. should not use them for _anything_ else */
  128. DEFINE_MUTEX(mtd_table_mutex);
  129. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  130. struct mtd_info *__mtd_next_device(int i)
  131. {
  132. return idr_get_next(&mtd_idr, &i);
  133. }
  134. EXPORT_SYMBOL_GPL(__mtd_next_device);
  135. #ifndef __UBOOT__
  136. static LIST_HEAD(mtd_notifiers);
  137. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  138. /* REVISIT once MTD uses the driver model better, whoever allocates
  139. * the mtd_info will probably want to use the release() hook...
  140. */
  141. static void mtd_release(struct device *dev)
  142. {
  143. struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
  144. dev_t index = MTD_DEVT(mtd->index);
  145. /* remove /dev/mtdXro node if needed */
  146. if (index)
  147. device_destroy(&mtd_class, index + 1);
  148. }
  149. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  150. {
  151. struct mtd_info *mtd = dev_get_drvdata(dev);
  152. return mtd ? mtd_suspend(mtd) : 0;
  153. }
  154. static int mtd_cls_resume(struct device *dev)
  155. {
  156. struct mtd_info *mtd = dev_get_drvdata(dev);
  157. if (mtd)
  158. mtd_resume(mtd);
  159. return 0;
  160. }
  161. static ssize_t mtd_type_show(struct device *dev,
  162. struct device_attribute *attr, char *buf)
  163. {
  164. struct mtd_info *mtd = dev_get_drvdata(dev);
  165. char *type;
  166. switch (mtd->type) {
  167. case MTD_ABSENT:
  168. type = "absent";
  169. break;
  170. case MTD_RAM:
  171. type = "ram";
  172. break;
  173. case MTD_ROM:
  174. type = "rom";
  175. break;
  176. case MTD_NORFLASH:
  177. type = "nor";
  178. break;
  179. case MTD_NANDFLASH:
  180. type = "nand";
  181. break;
  182. case MTD_DATAFLASH:
  183. type = "dataflash";
  184. break;
  185. case MTD_UBIVOLUME:
  186. type = "ubi";
  187. break;
  188. case MTD_MLCNANDFLASH:
  189. type = "mlc-nand";
  190. break;
  191. default:
  192. type = "unknown";
  193. }
  194. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  195. }
  196. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  197. static ssize_t mtd_flags_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct mtd_info *mtd = dev_get_drvdata(dev);
  201. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  202. }
  203. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  204. static ssize_t mtd_size_show(struct device *dev,
  205. struct device_attribute *attr, char *buf)
  206. {
  207. struct mtd_info *mtd = dev_get_drvdata(dev);
  208. return snprintf(buf, PAGE_SIZE, "%llu\n",
  209. (unsigned long long)mtd->size);
  210. }
  211. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  212. static ssize_t mtd_erasesize_show(struct device *dev,
  213. struct device_attribute *attr, char *buf)
  214. {
  215. struct mtd_info *mtd = dev_get_drvdata(dev);
  216. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  217. }
  218. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  219. static ssize_t mtd_writesize_show(struct device *dev,
  220. struct device_attribute *attr, char *buf)
  221. {
  222. struct mtd_info *mtd = dev_get_drvdata(dev);
  223. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  224. }
  225. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  226. static ssize_t mtd_subpagesize_show(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct mtd_info *mtd = dev_get_drvdata(dev);
  230. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  231. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  232. }
  233. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  234. static ssize_t mtd_oobsize_show(struct device *dev,
  235. struct device_attribute *attr, char *buf)
  236. {
  237. struct mtd_info *mtd = dev_get_drvdata(dev);
  238. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  239. }
  240. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  241. static ssize_t mtd_numeraseregions_show(struct device *dev,
  242. struct device_attribute *attr, char *buf)
  243. {
  244. struct mtd_info *mtd = dev_get_drvdata(dev);
  245. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  246. }
  247. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  248. NULL);
  249. static ssize_t mtd_name_show(struct device *dev,
  250. struct device_attribute *attr, char *buf)
  251. {
  252. struct mtd_info *mtd = dev_get_drvdata(dev);
  253. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  254. }
  255. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  256. static ssize_t mtd_ecc_strength_show(struct device *dev,
  257. struct device_attribute *attr, char *buf)
  258. {
  259. struct mtd_info *mtd = dev_get_drvdata(dev);
  260. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
  261. }
  262. static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
  263. static ssize_t mtd_bitflip_threshold_show(struct device *dev,
  264. struct device_attribute *attr,
  265. char *buf)
  266. {
  267. struct mtd_info *mtd = dev_get_drvdata(dev);
  268. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
  269. }
  270. static ssize_t mtd_bitflip_threshold_store(struct device *dev,
  271. struct device_attribute *attr,
  272. const char *buf, size_t count)
  273. {
  274. struct mtd_info *mtd = dev_get_drvdata(dev);
  275. unsigned int bitflip_threshold;
  276. int retval;
  277. retval = kstrtouint(buf, 0, &bitflip_threshold);
  278. if (retval)
  279. return retval;
  280. mtd->bitflip_threshold = bitflip_threshold;
  281. return count;
  282. }
  283. static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
  284. mtd_bitflip_threshold_show,
  285. mtd_bitflip_threshold_store);
  286. static ssize_t mtd_ecc_step_size_show(struct device *dev,
  287. struct device_attribute *attr, char *buf)
  288. {
  289. struct mtd_info *mtd = dev_get_drvdata(dev);
  290. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
  291. }
  292. static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
  293. static struct attribute *mtd_attrs[] = {
  294. &dev_attr_type.attr,
  295. &dev_attr_flags.attr,
  296. &dev_attr_size.attr,
  297. &dev_attr_erasesize.attr,
  298. &dev_attr_writesize.attr,
  299. &dev_attr_subpagesize.attr,
  300. &dev_attr_oobsize.attr,
  301. &dev_attr_numeraseregions.attr,
  302. &dev_attr_name.attr,
  303. &dev_attr_ecc_strength.attr,
  304. &dev_attr_ecc_step_size.attr,
  305. &dev_attr_bitflip_threshold.attr,
  306. NULL,
  307. };
  308. ATTRIBUTE_GROUPS(mtd);
  309. static struct device_type mtd_devtype = {
  310. .name = "mtd",
  311. .groups = mtd_groups,
  312. .release = mtd_release,
  313. };
  314. #endif
  315. /**
  316. * add_mtd_device - register an MTD device
  317. * @mtd: pointer to new MTD device info structure
  318. *
  319. * Add a device to the list of MTD devices present in the system, and
  320. * notify each currently active MTD 'user' of its arrival. Returns
  321. * zero on success or 1 on failure, which currently will only happen
  322. * if there is insufficient memory or a sysfs error.
  323. */
  324. int add_mtd_device(struct mtd_info *mtd)
  325. {
  326. #ifndef __UBOOT__
  327. struct mtd_notifier *not;
  328. #endif
  329. int i, error;
  330. #ifndef __UBOOT__
  331. if (!mtd->backing_dev_info) {
  332. switch (mtd->type) {
  333. case MTD_RAM:
  334. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  335. break;
  336. case MTD_ROM:
  337. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  338. break;
  339. default:
  340. mtd->backing_dev_info = &mtd_bdi_unmappable;
  341. break;
  342. }
  343. }
  344. #endif
  345. BUG_ON(mtd->writesize == 0);
  346. mutex_lock(&mtd_table_mutex);
  347. i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
  348. if (i < 0)
  349. goto fail_locked;
  350. mtd->index = i;
  351. mtd->usecount = 0;
  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. ret_code = mtd->_read(mtd, from, len, retlen, buf);
  792. if (unlikely(ret_code < 0))
  793. return ret_code;
  794. if (mtd->ecc_strength == 0)
  795. return 0; /* device lacks ecc */
  796. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  797. }
  798. EXPORT_SYMBOL_GPL(mtd_read);
  799. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  800. const u_char *buf)
  801. {
  802. *retlen = 0;
  803. if (to < 0 || to > mtd->size || len > mtd->size - to)
  804. return -EINVAL;
  805. if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
  806. return -EROFS;
  807. if (!len)
  808. return 0;
  809. return mtd->_write(mtd, to, len, retlen, buf);
  810. }
  811. EXPORT_SYMBOL_GPL(mtd_write);
  812. /*
  813. * In blackbox flight recorder like scenarios we want to make successful writes
  814. * in interrupt context. panic_write() is only intended to be called when its
  815. * known the kernel is about to panic and we need the write to succeed. Since
  816. * the kernel is not going to be running for much longer, this function can
  817. * break locks and delay to ensure the write succeeds (but not sleep).
  818. */
  819. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  820. const u_char *buf)
  821. {
  822. *retlen = 0;
  823. if (!mtd->_panic_write)
  824. return -EOPNOTSUPP;
  825. if (to < 0 || to > mtd->size || len > mtd->size - to)
  826. return -EINVAL;
  827. if (!(mtd->flags & MTD_WRITEABLE))
  828. return -EROFS;
  829. if (!len)
  830. return 0;
  831. return mtd->_panic_write(mtd, to, len, retlen, buf);
  832. }
  833. EXPORT_SYMBOL_GPL(mtd_panic_write);
  834. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  835. {
  836. int ret_code;
  837. ops->retlen = ops->oobretlen = 0;
  838. if (!mtd->_read_oob)
  839. return -EOPNOTSUPP;
  840. /*
  841. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  842. * similar to mtd->_read(), returning a non-negative integer
  843. * representing max bitflips. In other cases, mtd->_read_oob() may
  844. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  845. */
  846. ret_code = mtd->_read_oob(mtd, from, ops);
  847. if (unlikely(ret_code < 0))
  848. return ret_code;
  849. if (mtd->ecc_strength == 0)
  850. return 0; /* device lacks ecc */
  851. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  852. }
  853. EXPORT_SYMBOL_GPL(mtd_read_oob);
  854. /**
  855. * mtd_ooblayout_ecc - Get the OOB region definition of a specific ECC section
  856. * @mtd: MTD device structure
  857. * @section: ECC section. Depending on the layout you may have all the ECC
  858. * bytes stored in a single contiguous section, or one section
  859. * per ECC chunk (and sometime several sections for a single ECC
  860. * ECC chunk)
  861. * @oobecc: OOB region struct filled with the appropriate ECC position
  862. * information
  863. *
  864. * This function returns ECC section information in the OOB area. If you want
  865. * to get all the ECC bytes information, then you should call
  866. * mtd_ooblayout_ecc(mtd, section++, oobecc) until it returns -ERANGE.
  867. *
  868. * Returns zero on success, a negative error code otherwise.
  869. */
  870. int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
  871. struct mtd_oob_region *oobecc)
  872. {
  873. memset(oobecc, 0, sizeof(*oobecc));
  874. if (!mtd || section < 0)
  875. return -EINVAL;
  876. if (!mtd->ooblayout || !mtd->ooblayout->ecc)
  877. return -ENOTSUPP;
  878. return mtd->ooblayout->ecc(mtd, section, oobecc);
  879. }
  880. EXPORT_SYMBOL_GPL(mtd_ooblayout_ecc);
  881. /**
  882. * mtd_ooblayout_free - Get the OOB region definition of a specific free
  883. * section
  884. * @mtd: MTD device structure
  885. * @section: Free section you are interested in. Depending on the layout
  886. * you may have all the free bytes stored in a single contiguous
  887. * section, or one section per ECC chunk plus an extra section
  888. * for the remaining bytes (or other funky layout).
  889. * @oobfree: OOB region struct filled with the appropriate free position
  890. * information
  891. *
  892. * This function returns free bytes position in the OOB area. If you want
  893. * to get all the free bytes information, then you should call
  894. * mtd_ooblayout_free(mtd, section++, oobfree) until it returns -ERANGE.
  895. *
  896. * Returns zero on success, a negative error code otherwise.
  897. */
  898. int mtd_ooblayout_free(struct mtd_info *mtd, int section,
  899. struct mtd_oob_region *oobfree)
  900. {
  901. memset(oobfree, 0, sizeof(*oobfree));
  902. if (!mtd || section < 0)
  903. return -EINVAL;
  904. if (!mtd->ooblayout || !mtd->ooblayout->free)
  905. return -ENOTSUPP;
  906. return mtd->ooblayout->free(mtd, section, oobfree);
  907. }
  908. EXPORT_SYMBOL_GPL(mtd_ooblayout_free);
  909. /**
  910. * mtd_ooblayout_find_region - Find the region attached to a specific byte
  911. * @mtd: mtd info structure
  912. * @byte: the byte we are searching for
  913. * @sectionp: pointer where the section id will be stored
  914. * @oobregion: used to retrieve the ECC position
  915. * @iter: iterator function. Should be either mtd_ooblayout_free or
  916. * mtd_ooblayout_ecc depending on the region type you're searching for
  917. *
  918. * This function returns the section id and oobregion information of a
  919. * specific byte. For example, say you want to know where the 4th ECC byte is
  920. * stored, you'll use:
  921. *
  922. * mtd_ooblayout_find_region(mtd, 3, &section, &oobregion, mtd_ooblayout_ecc);
  923. *
  924. * Returns zero on success, a negative error code otherwise.
  925. */
  926. static int mtd_ooblayout_find_region(struct mtd_info *mtd, int byte,
  927. int *sectionp, struct mtd_oob_region *oobregion,
  928. int (*iter)(struct mtd_info *,
  929. int section,
  930. struct mtd_oob_region *oobregion))
  931. {
  932. int pos = 0, ret, section = 0;
  933. memset(oobregion, 0, sizeof(*oobregion));
  934. while (1) {
  935. ret = iter(mtd, section, oobregion);
  936. if (ret)
  937. return ret;
  938. if (pos + oobregion->length > byte)
  939. break;
  940. pos += oobregion->length;
  941. section++;
  942. }
  943. /*
  944. * Adjust region info to make it start at the beginning at the
  945. * 'start' ECC byte.
  946. */
  947. oobregion->offset += byte - pos;
  948. oobregion->length -= byte - pos;
  949. *sectionp = section;
  950. return 0;
  951. }
  952. /**
  953. * mtd_ooblayout_find_eccregion - Find the ECC region attached to a specific
  954. * ECC byte
  955. * @mtd: mtd info structure
  956. * @eccbyte: the byte we are searching for
  957. * @sectionp: pointer where the section id will be stored
  958. * @oobregion: OOB region information
  959. *
  960. * Works like mtd_ooblayout_find_region() except it searches for a specific ECC
  961. * byte.
  962. *
  963. * Returns zero on success, a negative error code otherwise.
  964. */
  965. int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
  966. int *section,
  967. struct mtd_oob_region *oobregion)
  968. {
  969. return mtd_ooblayout_find_region(mtd, eccbyte, section, oobregion,
  970. mtd_ooblayout_ecc);
  971. }
  972. EXPORT_SYMBOL_GPL(mtd_ooblayout_find_eccregion);
  973. /**
  974. * mtd_ooblayout_get_bytes - Extract OOB bytes from the oob buffer
  975. * @mtd: mtd info structure
  976. * @buf: destination buffer to store OOB bytes
  977. * @oobbuf: OOB buffer
  978. * @start: first byte to retrieve
  979. * @nbytes: number of bytes to retrieve
  980. * @iter: section iterator
  981. *
  982. * Extract bytes attached to a specific category (ECC or free)
  983. * from the OOB buffer and copy them into buf.
  984. *
  985. * Returns zero on success, a negative error code otherwise.
  986. */
  987. static int mtd_ooblayout_get_bytes(struct mtd_info *mtd, u8 *buf,
  988. const u8 *oobbuf, int start, int nbytes,
  989. int (*iter)(struct mtd_info *,
  990. int section,
  991. struct mtd_oob_region *oobregion))
  992. {
  993. struct mtd_oob_region oobregion;
  994. int section, ret;
  995. ret = mtd_ooblayout_find_region(mtd, start, &section,
  996. &oobregion, iter);
  997. while (!ret) {
  998. int cnt;
  999. cnt = min_t(int, nbytes, oobregion.length);
  1000. memcpy(buf, oobbuf + oobregion.offset, cnt);
  1001. buf += cnt;
  1002. nbytes -= cnt;
  1003. if (!nbytes)
  1004. break;
  1005. ret = iter(mtd, ++section, &oobregion);
  1006. }
  1007. return ret;
  1008. }
  1009. /**
  1010. * mtd_ooblayout_set_bytes - put OOB bytes into the oob buffer
  1011. * @mtd: mtd info structure
  1012. * @buf: source buffer to get OOB bytes from
  1013. * @oobbuf: OOB buffer
  1014. * @start: first OOB byte to set
  1015. * @nbytes: number of OOB bytes to set
  1016. * @iter: section iterator
  1017. *
  1018. * Fill the OOB buffer with data provided in buf. The category (ECC or free)
  1019. * is selected by passing the appropriate iterator.
  1020. *
  1021. * Returns zero on success, a negative error code otherwise.
  1022. */
  1023. static int mtd_ooblayout_set_bytes(struct mtd_info *mtd, const u8 *buf,
  1024. u8 *oobbuf, int start, int nbytes,
  1025. int (*iter)(struct mtd_info *,
  1026. int section,
  1027. struct mtd_oob_region *oobregion))
  1028. {
  1029. struct mtd_oob_region oobregion;
  1030. int section, ret;
  1031. ret = mtd_ooblayout_find_region(mtd, start, &section,
  1032. &oobregion, iter);
  1033. while (!ret) {
  1034. int cnt;
  1035. cnt = min_t(int, nbytes, oobregion.length);
  1036. memcpy(oobbuf + oobregion.offset, buf, cnt);
  1037. buf += cnt;
  1038. nbytes -= cnt;
  1039. if (!nbytes)
  1040. break;
  1041. ret = iter(mtd, ++section, &oobregion);
  1042. }
  1043. return ret;
  1044. }
  1045. /**
  1046. * mtd_ooblayout_count_bytes - count the number of bytes in a OOB category
  1047. * @mtd: mtd info structure
  1048. * @iter: category iterator
  1049. *
  1050. * Count the number of bytes in a given category.
  1051. *
  1052. * Returns a positive value on success, a negative error code otherwise.
  1053. */
  1054. static int mtd_ooblayout_count_bytes(struct mtd_info *mtd,
  1055. int (*iter)(struct mtd_info *,
  1056. int section,
  1057. struct mtd_oob_region *oobregion))
  1058. {
  1059. struct mtd_oob_region oobregion;
  1060. int section = 0, ret, nbytes = 0;
  1061. while (1) {
  1062. ret = iter(mtd, section++, &oobregion);
  1063. if (ret) {
  1064. if (ret == -ERANGE)
  1065. ret = nbytes;
  1066. break;
  1067. }
  1068. nbytes += oobregion.length;
  1069. }
  1070. return ret;
  1071. }
  1072. /**
  1073. * mtd_ooblayout_get_eccbytes - extract ECC bytes from the oob buffer
  1074. * @mtd: mtd info structure
  1075. * @eccbuf: destination buffer to store ECC bytes
  1076. * @oobbuf: OOB buffer
  1077. * @start: first ECC byte to retrieve
  1078. * @nbytes: number of ECC bytes to retrieve
  1079. *
  1080. * Works like mtd_ooblayout_get_bytes(), except it acts on ECC bytes.
  1081. *
  1082. * Returns zero on success, a negative error code otherwise.
  1083. */
  1084. int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
  1085. const u8 *oobbuf, int start, int nbytes)
  1086. {
  1087. return mtd_ooblayout_get_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1088. mtd_ooblayout_ecc);
  1089. }
  1090. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_eccbytes);
  1091. /**
  1092. * mtd_ooblayout_set_eccbytes - set ECC bytes into the oob buffer
  1093. * @mtd: mtd info structure
  1094. * @eccbuf: source buffer to get ECC bytes from
  1095. * @oobbuf: OOB buffer
  1096. * @start: first ECC byte to set
  1097. * @nbytes: number of ECC bytes to set
  1098. *
  1099. * Works like mtd_ooblayout_set_bytes(), except it acts on ECC bytes.
  1100. *
  1101. * Returns zero on success, a negative error code otherwise.
  1102. */
  1103. int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
  1104. u8 *oobbuf, int start, int nbytes)
  1105. {
  1106. return mtd_ooblayout_set_bytes(mtd, eccbuf, oobbuf, start, nbytes,
  1107. mtd_ooblayout_ecc);
  1108. }
  1109. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_eccbytes);
  1110. /**
  1111. * mtd_ooblayout_get_databytes - extract data bytes from the oob buffer
  1112. * @mtd: mtd info structure
  1113. * @databuf: destination buffer to store ECC bytes
  1114. * @oobbuf: OOB buffer
  1115. * @start: first ECC byte to retrieve
  1116. * @nbytes: number of ECC bytes to retrieve
  1117. *
  1118. * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  1119. *
  1120. * Returns zero on success, a negative error code otherwise.
  1121. */
  1122. int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
  1123. const u8 *oobbuf, int start, int nbytes)
  1124. {
  1125. return mtd_ooblayout_get_bytes(mtd, databuf, oobbuf, start, nbytes,
  1126. mtd_ooblayout_free);
  1127. }
  1128. EXPORT_SYMBOL_GPL(mtd_ooblayout_get_databytes);
  1129. /**
  1130. * mtd_ooblayout_get_eccbytes - set data bytes into the oob buffer
  1131. * @mtd: mtd info structure
  1132. * @eccbuf: source buffer to get data bytes from
  1133. * @oobbuf: OOB buffer
  1134. * @start: first ECC byte to set
  1135. * @nbytes: number of ECC bytes to set
  1136. *
  1137. * Works like mtd_ooblayout_get_bytes(), except it acts on free bytes.
  1138. *
  1139. * Returns zero on success, a negative error code otherwise.
  1140. */
  1141. int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
  1142. u8 *oobbuf, int start, int nbytes)
  1143. {
  1144. return mtd_ooblayout_set_bytes(mtd, databuf, oobbuf, start, nbytes,
  1145. mtd_ooblayout_free);
  1146. }
  1147. EXPORT_SYMBOL_GPL(mtd_ooblayout_set_databytes);
  1148. /**
  1149. * mtd_ooblayout_count_freebytes - count the number of free bytes in OOB
  1150. * @mtd: mtd info structure
  1151. *
  1152. * Works like mtd_ooblayout_count_bytes(), except it count free bytes.
  1153. *
  1154. * Returns zero on success, a negative error code otherwise.
  1155. */
  1156. int mtd_ooblayout_count_freebytes(struct mtd_info *mtd)
  1157. {
  1158. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_free);
  1159. }
  1160. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_freebytes);
  1161. /**
  1162. * mtd_ooblayout_count_freebytes - count the number of ECC bytes in OOB
  1163. * @mtd: mtd info structure
  1164. *
  1165. * Works like mtd_ooblayout_count_bytes(), except it count ECC bytes.
  1166. *
  1167. * Returns zero on success, a negative error code otherwise.
  1168. */
  1169. int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd)
  1170. {
  1171. return mtd_ooblayout_count_bytes(mtd, mtd_ooblayout_ecc);
  1172. }
  1173. EXPORT_SYMBOL_GPL(mtd_ooblayout_count_eccbytes);
  1174. /*
  1175. * Method to access the protection register area, present in some flash
  1176. * devices. The user data is one time programmable but the factory data is read
  1177. * only.
  1178. */
  1179. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1180. struct otp_info *buf)
  1181. {
  1182. if (!mtd->_get_fact_prot_info)
  1183. return -EOPNOTSUPP;
  1184. if (!len)
  1185. return 0;
  1186. return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
  1187. }
  1188. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  1189. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1190. size_t *retlen, u_char *buf)
  1191. {
  1192. *retlen = 0;
  1193. if (!mtd->_read_fact_prot_reg)
  1194. return -EOPNOTSUPP;
  1195. if (!len)
  1196. return 0;
  1197. return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
  1198. }
  1199. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  1200. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  1201. struct otp_info *buf)
  1202. {
  1203. if (!mtd->_get_user_prot_info)
  1204. return -EOPNOTSUPP;
  1205. if (!len)
  1206. return 0;
  1207. return mtd->_get_user_prot_info(mtd, len, retlen, buf);
  1208. }
  1209. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  1210. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  1211. size_t *retlen, u_char *buf)
  1212. {
  1213. *retlen = 0;
  1214. if (!mtd->_read_user_prot_reg)
  1215. return -EOPNOTSUPP;
  1216. if (!len)
  1217. return 0;
  1218. return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
  1219. }
  1220. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  1221. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  1222. size_t *retlen, u_char *buf)
  1223. {
  1224. int ret;
  1225. *retlen = 0;
  1226. if (!mtd->_write_user_prot_reg)
  1227. return -EOPNOTSUPP;
  1228. if (!len)
  1229. return 0;
  1230. ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
  1231. if (ret)
  1232. return ret;
  1233. /*
  1234. * If no data could be written at all, we are out of memory and
  1235. * must return -ENOSPC.
  1236. */
  1237. return (*retlen) ? 0 : -ENOSPC;
  1238. }
  1239. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  1240. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  1241. {
  1242. if (!mtd->_lock_user_prot_reg)
  1243. return -EOPNOTSUPP;
  1244. if (!len)
  1245. return 0;
  1246. return mtd->_lock_user_prot_reg(mtd, from, len);
  1247. }
  1248. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  1249. /* Chip-supported device locking */
  1250. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1251. {
  1252. if (!mtd->_lock)
  1253. return -EOPNOTSUPP;
  1254. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1255. return -EINVAL;
  1256. if (!len)
  1257. return 0;
  1258. return mtd->_lock(mtd, ofs, len);
  1259. }
  1260. EXPORT_SYMBOL_GPL(mtd_lock);
  1261. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1262. {
  1263. if (!mtd->_unlock)
  1264. return -EOPNOTSUPP;
  1265. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1266. return -EINVAL;
  1267. if (!len)
  1268. return 0;
  1269. return mtd->_unlock(mtd, ofs, len);
  1270. }
  1271. EXPORT_SYMBOL_GPL(mtd_unlock);
  1272. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1273. {
  1274. if (!mtd->_is_locked)
  1275. return -EOPNOTSUPP;
  1276. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  1277. return -EINVAL;
  1278. if (!len)
  1279. return 0;
  1280. return mtd->_is_locked(mtd, ofs, len);
  1281. }
  1282. EXPORT_SYMBOL_GPL(mtd_is_locked);
  1283. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  1284. {
  1285. if (ofs < 0 || ofs > mtd->size)
  1286. return -EINVAL;
  1287. if (!mtd->_block_isreserved)
  1288. return 0;
  1289. return mtd->_block_isreserved(mtd, ofs);
  1290. }
  1291. EXPORT_SYMBOL_GPL(mtd_block_isreserved);
  1292. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  1293. {
  1294. if (ofs < 0 || ofs > mtd->size)
  1295. return -EINVAL;
  1296. if (!mtd->_block_isbad)
  1297. return 0;
  1298. return mtd->_block_isbad(mtd, ofs);
  1299. }
  1300. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  1301. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1302. {
  1303. if (!mtd->_block_markbad)
  1304. return -EOPNOTSUPP;
  1305. if (ofs < 0 || ofs > mtd->size)
  1306. return -EINVAL;
  1307. if (!(mtd->flags & MTD_WRITEABLE))
  1308. return -EROFS;
  1309. return mtd->_block_markbad(mtd, ofs);
  1310. }
  1311. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  1312. #ifndef __UBOOT__
  1313. /*
  1314. * default_mtd_writev - the default writev method
  1315. * @mtd: mtd device description object pointer
  1316. * @vecs: the vectors to write
  1317. * @count: count of vectors in @vecs
  1318. * @to: the MTD device offset to write to
  1319. * @retlen: on exit contains the count of bytes written to the MTD device.
  1320. *
  1321. * This function returns zero in case of success and a negative error code in
  1322. * case of failure.
  1323. */
  1324. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1325. unsigned long count, loff_t to, size_t *retlen)
  1326. {
  1327. unsigned long i;
  1328. size_t totlen = 0, thislen;
  1329. int ret = 0;
  1330. for (i = 0; i < count; i++) {
  1331. if (!vecs[i].iov_len)
  1332. continue;
  1333. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  1334. vecs[i].iov_base);
  1335. totlen += thislen;
  1336. if (ret || thislen != vecs[i].iov_len)
  1337. break;
  1338. to += vecs[i].iov_len;
  1339. }
  1340. *retlen = totlen;
  1341. return ret;
  1342. }
  1343. /*
  1344. * mtd_writev - the vector-based MTD write method
  1345. * @mtd: mtd device description object pointer
  1346. * @vecs: the vectors to write
  1347. * @count: count of vectors in @vecs
  1348. * @to: the MTD device offset to write to
  1349. * @retlen: on exit contains the count of bytes written to the MTD device.
  1350. *
  1351. * This function returns zero in case of success and a negative error code in
  1352. * case of failure.
  1353. */
  1354. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1355. unsigned long count, loff_t to, size_t *retlen)
  1356. {
  1357. *retlen = 0;
  1358. if (!(mtd->flags & MTD_WRITEABLE))
  1359. return -EROFS;
  1360. if (!mtd->_writev)
  1361. return default_mtd_writev(mtd, vecs, count, to, retlen);
  1362. return mtd->_writev(mtd, vecs, count, to, retlen);
  1363. }
  1364. EXPORT_SYMBOL_GPL(mtd_writev);
  1365. /**
  1366. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  1367. * @mtd: mtd device description object pointer
  1368. * @size: a pointer to the ideal or maximum size of the allocation, points
  1369. * to the actual allocation size on success.
  1370. *
  1371. * This routine attempts to allocate a contiguous kernel buffer up to
  1372. * the specified size, backing off the size of the request exponentially
  1373. * until the request succeeds or until the allocation size falls below
  1374. * the system page size. This attempts to make sure it does not adversely
  1375. * impact system performance, so when allocating more than one page, we
  1376. * ask the memory allocator to avoid re-trying, swapping, writing back
  1377. * or performing I/O.
  1378. *
  1379. * Note, this function also makes sure that the allocated buffer is aligned to
  1380. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  1381. *
  1382. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  1383. * to handle smaller (i.e. degraded) buffer allocations under low- or
  1384. * fragmented-memory situations where such reduced allocations, from a
  1385. * requested ideal, are allowed.
  1386. *
  1387. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  1388. */
  1389. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  1390. {
  1391. gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
  1392. __GFP_NORETRY | __GFP_NO_KSWAPD;
  1393. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  1394. void *kbuf;
  1395. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  1396. while (*size > min_alloc) {
  1397. kbuf = kmalloc(*size, flags);
  1398. if (kbuf)
  1399. return kbuf;
  1400. *size >>= 1;
  1401. *size = ALIGN(*size, mtd->writesize);
  1402. }
  1403. /*
  1404. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  1405. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  1406. */
  1407. return kmalloc(*size, GFP_KERNEL);
  1408. }
  1409. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  1410. #endif
  1411. #ifdef CONFIG_PROC_FS
  1412. /*====================================================================*/
  1413. /* Support for /proc/mtd */
  1414. static int mtd_proc_show(struct seq_file *m, void *v)
  1415. {
  1416. struct mtd_info *mtd;
  1417. seq_puts(m, "dev: size erasesize name\n");
  1418. mutex_lock(&mtd_table_mutex);
  1419. mtd_for_each_device(mtd) {
  1420. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  1421. mtd->index, (unsigned long long)mtd->size,
  1422. mtd->erasesize, mtd->name);
  1423. }
  1424. mutex_unlock(&mtd_table_mutex);
  1425. return 0;
  1426. }
  1427. static int mtd_proc_open(struct inode *inode, struct file *file)
  1428. {
  1429. return single_open(file, mtd_proc_show, NULL);
  1430. }
  1431. static const struct file_operations mtd_proc_ops = {
  1432. .open = mtd_proc_open,
  1433. .read = seq_read,
  1434. .llseek = seq_lseek,
  1435. .release = single_release,
  1436. };
  1437. #endif /* CONFIG_PROC_FS */
  1438. /*====================================================================*/
  1439. /* Init code */
  1440. #ifndef __UBOOT__
  1441. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  1442. {
  1443. int ret;
  1444. ret = bdi_init(bdi);
  1445. if (!ret)
  1446. ret = bdi_register(bdi, NULL, "%s", name);
  1447. if (ret)
  1448. bdi_destroy(bdi);
  1449. return ret;
  1450. }
  1451. static struct proc_dir_entry *proc_mtd;
  1452. static int __init init_mtd(void)
  1453. {
  1454. int ret;
  1455. ret = class_register(&mtd_class);
  1456. if (ret)
  1457. goto err_reg;
  1458. ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
  1459. if (ret)
  1460. goto err_bdi1;
  1461. ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
  1462. if (ret)
  1463. goto err_bdi2;
  1464. ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
  1465. if (ret)
  1466. goto err_bdi3;
  1467. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  1468. ret = init_mtdchar();
  1469. if (ret)
  1470. goto out_procfs;
  1471. return 0;
  1472. out_procfs:
  1473. if (proc_mtd)
  1474. remove_proc_entry("mtd", NULL);
  1475. err_bdi3:
  1476. bdi_destroy(&mtd_bdi_ro_mappable);
  1477. err_bdi2:
  1478. bdi_destroy(&mtd_bdi_unmappable);
  1479. err_bdi1:
  1480. class_unregister(&mtd_class);
  1481. err_reg:
  1482. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1483. return ret;
  1484. }
  1485. static void __exit cleanup_mtd(void)
  1486. {
  1487. cleanup_mtdchar();
  1488. if (proc_mtd)
  1489. remove_proc_entry("mtd", NULL);
  1490. class_unregister(&mtd_class);
  1491. bdi_destroy(&mtd_bdi_unmappable);
  1492. bdi_destroy(&mtd_bdi_ro_mappable);
  1493. bdi_destroy(&mtd_bdi_rw_mappable);
  1494. }
  1495. module_init(init_mtd);
  1496. module_exit(cleanup_mtd);
  1497. #endif
  1498. MODULE_LICENSE("GPL");
  1499. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1500. MODULE_DESCRIPTION("Core MTD registration and access routines");