mtdcore.c 49 KB

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