mtdcore.c 50 KB

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