mtdcore.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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/proc_fs.h>
  35. #include <linux/idr.h>
  36. #include <linux/backing-dev.h>
  37. #include <linux/gfp.h>
  38. #include <linux/slab.h>
  39. #include <linux/mtd/mtd.h>
  40. #include <linux/mtd/partitions.h>
  41. #include "mtdcore.h"
  42. /*
  43. * backing device capabilities for non-mappable devices (such as NAND flash)
  44. * - permits private mappings, copies are taken of the data
  45. */
  46. static struct backing_dev_info mtd_bdi_unmappable = {
  47. .capabilities = BDI_CAP_MAP_COPY,
  48. };
  49. /*
  50. * backing device capabilities for R/O mappable devices (such as ROM)
  51. * - permits private mappings, copies are taken of the data
  52. * - permits non-writable shared mappings
  53. */
  54. static struct backing_dev_info mtd_bdi_ro_mappable = {
  55. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  56. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
  57. };
  58. /*
  59. * backing device capabilities for writable mappable devices (such as RAM)
  60. * - permits private mappings, copies are taken of the data
  61. * - permits non-writable shared mappings
  62. */
  63. static struct backing_dev_info mtd_bdi_rw_mappable = {
  64. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  65. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
  66. BDI_CAP_WRITE_MAP),
  67. };
  68. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  69. static int mtd_cls_resume(struct device *dev);
  70. static struct class mtd_class = {
  71. .name = "mtd",
  72. .owner = THIS_MODULE,
  73. .suspend = mtd_cls_suspend,
  74. .resume = mtd_cls_resume,
  75. };
  76. static DEFINE_IDR(mtd_idr);
  77. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  78. should not use them for _anything_ else */
  79. DEFINE_MUTEX(mtd_table_mutex);
  80. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  81. struct mtd_info *__mtd_next_device(int i)
  82. {
  83. return idr_get_next(&mtd_idr, &i);
  84. }
  85. EXPORT_SYMBOL_GPL(__mtd_next_device);
  86. static LIST_HEAD(mtd_notifiers);
  87. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  88. /* REVISIT once MTD uses the driver model better, whoever allocates
  89. * the mtd_info will probably want to use the release() hook...
  90. */
  91. static void mtd_release(struct device *dev)
  92. {
  93. struct mtd_info *mtd = dev_get_drvdata(dev);
  94. dev_t index = MTD_DEVT(mtd->index);
  95. /* remove /dev/mtdXro node */
  96. device_destroy(&mtd_class, index + 1);
  97. }
  98. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  99. {
  100. struct mtd_info *mtd = dev_get_drvdata(dev);
  101. return mtd ? mtd_suspend(mtd) : 0;
  102. }
  103. static int mtd_cls_resume(struct device *dev)
  104. {
  105. struct mtd_info *mtd = dev_get_drvdata(dev);
  106. if (mtd)
  107. mtd_resume(mtd);
  108. return 0;
  109. }
  110. static ssize_t mtd_type_show(struct device *dev,
  111. struct device_attribute *attr, char *buf)
  112. {
  113. struct mtd_info *mtd = dev_get_drvdata(dev);
  114. char *type;
  115. switch (mtd->type) {
  116. case MTD_ABSENT:
  117. type = "absent";
  118. break;
  119. case MTD_RAM:
  120. type = "ram";
  121. break;
  122. case MTD_ROM:
  123. type = "rom";
  124. break;
  125. case MTD_NORFLASH:
  126. type = "nor";
  127. break;
  128. case MTD_NANDFLASH:
  129. type = "nand";
  130. break;
  131. case MTD_DATAFLASH:
  132. type = "dataflash";
  133. break;
  134. case MTD_UBIVOLUME:
  135. type = "ubi";
  136. break;
  137. case MTD_MLCNANDFLASH:
  138. type = "mlc-nand";
  139. break;
  140. default:
  141. type = "unknown";
  142. }
  143. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  144. }
  145. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  146. static ssize_t mtd_flags_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct mtd_info *mtd = dev_get_drvdata(dev);
  150. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  151. }
  152. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  153. static ssize_t mtd_size_show(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct mtd_info *mtd = dev_get_drvdata(dev);
  157. return snprintf(buf, PAGE_SIZE, "%llu\n",
  158. (unsigned long long)mtd->size);
  159. }
  160. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  161. static ssize_t mtd_erasesize_show(struct device *dev,
  162. struct device_attribute *attr, char *buf)
  163. {
  164. struct mtd_info *mtd = dev_get_drvdata(dev);
  165. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  166. }
  167. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  168. static ssize_t mtd_writesize_show(struct device *dev,
  169. struct device_attribute *attr, char *buf)
  170. {
  171. struct mtd_info *mtd = dev_get_drvdata(dev);
  172. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  173. }
  174. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  175. static ssize_t mtd_subpagesize_show(struct device *dev,
  176. struct device_attribute *attr, char *buf)
  177. {
  178. struct mtd_info *mtd = dev_get_drvdata(dev);
  179. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  180. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  181. }
  182. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  183. static ssize_t mtd_oobsize_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. struct mtd_info *mtd = dev_get_drvdata(dev);
  187. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  188. }
  189. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  190. static ssize_t mtd_numeraseregions_show(struct device *dev,
  191. struct device_attribute *attr, char *buf)
  192. {
  193. struct mtd_info *mtd = dev_get_drvdata(dev);
  194. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  195. }
  196. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  197. NULL);
  198. static ssize_t mtd_name_show(struct device *dev,
  199. struct device_attribute *attr, char *buf)
  200. {
  201. struct mtd_info *mtd = dev_get_drvdata(dev);
  202. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  203. }
  204. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  205. static ssize_t mtd_ecc_strength_show(struct device *dev,
  206. struct device_attribute *attr, char *buf)
  207. {
  208. struct mtd_info *mtd = dev_get_drvdata(dev);
  209. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
  210. }
  211. static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
  212. static ssize_t mtd_bitflip_threshold_show(struct device *dev,
  213. struct device_attribute *attr,
  214. char *buf)
  215. {
  216. struct mtd_info *mtd = dev_get_drvdata(dev);
  217. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
  218. }
  219. static ssize_t mtd_bitflip_threshold_store(struct device *dev,
  220. struct device_attribute *attr,
  221. const char *buf, size_t count)
  222. {
  223. struct mtd_info *mtd = dev_get_drvdata(dev);
  224. unsigned int bitflip_threshold;
  225. int retval;
  226. retval = kstrtouint(buf, 0, &bitflip_threshold);
  227. if (retval)
  228. return retval;
  229. mtd->bitflip_threshold = bitflip_threshold;
  230. return count;
  231. }
  232. static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
  233. mtd_bitflip_threshold_show,
  234. mtd_bitflip_threshold_store);
  235. static ssize_t mtd_ecc_step_size_show(struct device *dev,
  236. struct device_attribute *attr, char *buf)
  237. {
  238. struct mtd_info *mtd = dev_get_drvdata(dev);
  239. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
  240. }
  241. static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
  242. static ssize_t mtd_ecc_stats_corrected_show(struct device *dev,
  243. struct device_attribute *attr, char *buf)
  244. {
  245. struct mtd_info *mtd = dev_get_drvdata(dev);
  246. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  247. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected);
  248. }
  249. static DEVICE_ATTR(corrected_bits, S_IRUGO,
  250. mtd_ecc_stats_corrected_show, NULL);
  251. static ssize_t mtd_ecc_stats_errors_show(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. struct mtd_info *mtd = dev_get_drvdata(dev);
  255. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  256. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed);
  257. }
  258. static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL);
  259. static ssize_t mtd_badblocks_show(struct device *dev,
  260. struct device_attribute *attr, char *buf)
  261. {
  262. struct mtd_info *mtd = dev_get_drvdata(dev);
  263. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  264. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks);
  265. }
  266. static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL);
  267. static ssize_t mtd_bbtblocks_show(struct device *dev,
  268. struct device_attribute *attr, char *buf)
  269. {
  270. struct mtd_info *mtd = dev_get_drvdata(dev);
  271. struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats;
  272. return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks);
  273. }
  274. static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL);
  275. static struct attribute *mtd_attrs[] = {
  276. &dev_attr_type.attr,
  277. &dev_attr_flags.attr,
  278. &dev_attr_size.attr,
  279. &dev_attr_erasesize.attr,
  280. &dev_attr_writesize.attr,
  281. &dev_attr_subpagesize.attr,
  282. &dev_attr_oobsize.attr,
  283. &dev_attr_numeraseregions.attr,
  284. &dev_attr_name.attr,
  285. &dev_attr_ecc_strength.attr,
  286. &dev_attr_ecc_step_size.attr,
  287. &dev_attr_corrected_bits.attr,
  288. &dev_attr_ecc_failures.attr,
  289. &dev_attr_bad_blocks.attr,
  290. &dev_attr_bbt_blocks.attr,
  291. &dev_attr_bitflip_threshold.attr,
  292. NULL,
  293. };
  294. ATTRIBUTE_GROUPS(mtd);
  295. static struct device_type mtd_devtype = {
  296. .name = "mtd",
  297. .groups = mtd_groups,
  298. .release = mtd_release,
  299. };
  300. /**
  301. * add_mtd_device - register an MTD device
  302. * @mtd: pointer to new MTD device info structure
  303. *
  304. * Add a device to the list of MTD devices present in the system, and
  305. * notify each currently active MTD 'user' of its arrival. Returns
  306. * zero on success or 1 on failure, which currently will only happen
  307. * if there is insufficient memory or a sysfs error.
  308. */
  309. int add_mtd_device(struct mtd_info *mtd)
  310. {
  311. struct mtd_notifier *not;
  312. int i, error;
  313. if (!mtd->backing_dev_info) {
  314. switch (mtd->type) {
  315. case MTD_RAM:
  316. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  317. break;
  318. case MTD_ROM:
  319. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  320. break;
  321. default:
  322. mtd->backing_dev_info = &mtd_bdi_unmappable;
  323. break;
  324. }
  325. }
  326. BUG_ON(mtd->writesize == 0);
  327. mutex_lock(&mtd_table_mutex);
  328. i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
  329. if (i < 0)
  330. goto fail_locked;
  331. mtd->index = i;
  332. mtd->usecount = 0;
  333. /* default value if not set by driver */
  334. if (mtd->bitflip_threshold == 0)
  335. mtd->bitflip_threshold = mtd->ecc_strength;
  336. if (is_power_of_2(mtd->erasesize))
  337. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  338. else
  339. mtd->erasesize_shift = 0;
  340. if (is_power_of_2(mtd->writesize))
  341. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  342. else
  343. mtd->writesize_shift = 0;
  344. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  345. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  346. /* Some chips always power up locked. Unlock them now */
  347. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  348. error = mtd_unlock(mtd, 0, mtd->size);
  349. if (error && error != -EOPNOTSUPP)
  350. printk(KERN_WARNING
  351. "%s: unlock failed, writes may not work\n",
  352. mtd->name);
  353. }
  354. /* Caller should have set dev.parent to match the
  355. * physical device.
  356. */
  357. mtd->dev.type = &mtd_devtype;
  358. mtd->dev.class = &mtd_class;
  359. mtd->dev.devt = MTD_DEVT(i);
  360. dev_set_name(&mtd->dev, "mtd%d", i);
  361. dev_set_drvdata(&mtd->dev, mtd);
  362. if (device_register(&mtd->dev) != 0)
  363. goto fail_added;
  364. device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
  365. "mtd%dro", i);
  366. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  367. /* No need to get a refcount on the module containing
  368. the notifier, since we hold the mtd_table_mutex */
  369. list_for_each_entry(not, &mtd_notifiers, list)
  370. not->add(mtd);
  371. mutex_unlock(&mtd_table_mutex);
  372. /* We _know_ we aren't being removed, because
  373. our caller is still holding us here. So none
  374. of this try_ nonsense, and no bitching about it
  375. either. :) */
  376. __module_get(THIS_MODULE);
  377. return 0;
  378. fail_added:
  379. idr_remove(&mtd_idr, i);
  380. fail_locked:
  381. mutex_unlock(&mtd_table_mutex);
  382. return 1;
  383. }
  384. /**
  385. * del_mtd_device - unregister an MTD device
  386. * @mtd: pointer to MTD device info structure
  387. *
  388. * Remove a device from the list of MTD devices present in the system,
  389. * and notify each currently active MTD 'user' of its departure.
  390. * Returns zero on success or 1 on failure, which currently will happen
  391. * if the requested device does not appear to be present in the list.
  392. */
  393. int del_mtd_device(struct mtd_info *mtd)
  394. {
  395. int ret;
  396. struct mtd_notifier *not;
  397. mutex_lock(&mtd_table_mutex);
  398. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  399. ret = -ENODEV;
  400. goto out_error;
  401. }
  402. /* No need to get a refcount on the module containing
  403. the notifier, since we hold the mtd_table_mutex */
  404. list_for_each_entry(not, &mtd_notifiers, list)
  405. not->remove(mtd);
  406. if (mtd->usecount) {
  407. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  408. mtd->index, mtd->name, mtd->usecount);
  409. ret = -EBUSY;
  410. } else {
  411. device_unregister(&mtd->dev);
  412. idr_remove(&mtd_idr, mtd->index);
  413. module_put(THIS_MODULE);
  414. ret = 0;
  415. }
  416. out_error:
  417. mutex_unlock(&mtd_table_mutex);
  418. return ret;
  419. }
  420. /**
  421. * mtd_device_parse_register - parse partitions and register an MTD device.
  422. *
  423. * @mtd: the MTD device to register
  424. * @types: the list of MTD partition probes to try, see
  425. * 'parse_mtd_partitions()' for more information
  426. * @parser_data: MTD partition parser-specific data
  427. * @parts: fallback partition information to register, if parsing fails;
  428. * only valid if %nr_parts > %0
  429. * @nr_parts: the number of partitions in parts, if zero then the full
  430. * MTD device is registered if no partition info is found
  431. *
  432. * This function aggregates MTD partitions parsing (done by
  433. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  434. * basically follows the most common pattern found in many MTD drivers:
  435. *
  436. * * It first tries to probe partitions on MTD device @mtd using parsers
  437. * specified in @types (if @types is %NULL, then the default list of parsers
  438. * is used, see 'parse_mtd_partitions()' for more information). If none are
  439. * found this functions tries to fallback to information specified in
  440. * @parts/@nr_parts.
  441. * * If any partitioning info was found, this function registers the found
  442. * partitions.
  443. * * If no partitions were found this function just registers the MTD device
  444. * @mtd and exits.
  445. *
  446. * Returns zero in case of success and a negative error code in case of failure.
  447. */
  448. int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
  449. struct mtd_part_parser_data *parser_data,
  450. const struct mtd_partition *parts,
  451. int nr_parts)
  452. {
  453. int err;
  454. struct mtd_partition *real_parts;
  455. err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
  456. if (err <= 0 && nr_parts && parts) {
  457. real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
  458. GFP_KERNEL);
  459. if (!real_parts)
  460. err = -ENOMEM;
  461. else
  462. err = nr_parts;
  463. }
  464. if (err > 0) {
  465. err = add_mtd_partitions(mtd, real_parts, err);
  466. kfree(real_parts);
  467. } else if (err == 0) {
  468. err = add_mtd_device(mtd);
  469. if (err == 1)
  470. err = -ENODEV;
  471. }
  472. return err;
  473. }
  474. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  475. /**
  476. * mtd_device_unregister - unregister an existing MTD device.
  477. *
  478. * @master: the MTD device to unregister. This will unregister both the master
  479. * and any partitions if registered.
  480. */
  481. int mtd_device_unregister(struct mtd_info *master)
  482. {
  483. int err;
  484. err = del_mtd_partitions(master);
  485. if (err)
  486. return err;
  487. if (!device_is_registered(&master->dev))
  488. return 0;
  489. return del_mtd_device(master);
  490. }
  491. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  492. /**
  493. * register_mtd_user - register a 'user' of MTD devices.
  494. * @new: pointer to notifier info structure
  495. *
  496. * Registers a pair of callbacks function to be called upon addition
  497. * or removal of MTD devices. Causes the 'add' callback to be immediately
  498. * invoked for each MTD device currently present in the system.
  499. */
  500. void register_mtd_user (struct mtd_notifier *new)
  501. {
  502. struct mtd_info *mtd;
  503. mutex_lock(&mtd_table_mutex);
  504. list_add(&new->list, &mtd_notifiers);
  505. __module_get(THIS_MODULE);
  506. mtd_for_each_device(mtd)
  507. new->add(mtd);
  508. mutex_unlock(&mtd_table_mutex);
  509. }
  510. EXPORT_SYMBOL_GPL(register_mtd_user);
  511. /**
  512. * unregister_mtd_user - unregister a 'user' of MTD devices.
  513. * @old: pointer to notifier info structure
  514. *
  515. * Removes a callback function pair from the list of 'users' to be
  516. * notified upon addition or removal of MTD devices. Causes the
  517. * 'remove' callback to be immediately invoked for each MTD device
  518. * currently present in the system.
  519. */
  520. int unregister_mtd_user (struct mtd_notifier *old)
  521. {
  522. struct mtd_info *mtd;
  523. mutex_lock(&mtd_table_mutex);
  524. module_put(THIS_MODULE);
  525. mtd_for_each_device(mtd)
  526. old->remove(mtd);
  527. list_del(&old->list);
  528. mutex_unlock(&mtd_table_mutex);
  529. return 0;
  530. }
  531. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  532. /**
  533. * get_mtd_device - obtain a validated handle for an MTD device
  534. * @mtd: last known address of the required MTD device
  535. * @num: internal device number of the required MTD device
  536. *
  537. * Given a number and NULL address, return the num'th entry in the device
  538. * table, if any. Given an address and num == -1, search the device table
  539. * for a device with that address and return if it's still present. Given
  540. * both, return the num'th driver only if its address matches. Return
  541. * error code if not.
  542. */
  543. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  544. {
  545. struct mtd_info *ret = NULL, *other;
  546. int err = -ENODEV;
  547. mutex_lock(&mtd_table_mutex);
  548. if (num == -1) {
  549. mtd_for_each_device(other) {
  550. if (other == mtd) {
  551. ret = mtd;
  552. break;
  553. }
  554. }
  555. } else if (num >= 0) {
  556. ret = idr_find(&mtd_idr, num);
  557. if (mtd && mtd != ret)
  558. ret = NULL;
  559. }
  560. if (!ret) {
  561. ret = ERR_PTR(err);
  562. goto out;
  563. }
  564. err = __get_mtd_device(ret);
  565. if (err)
  566. ret = ERR_PTR(err);
  567. out:
  568. mutex_unlock(&mtd_table_mutex);
  569. return ret;
  570. }
  571. EXPORT_SYMBOL_GPL(get_mtd_device);
  572. int __get_mtd_device(struct mtd_info *mtd)
  573. {
  574. int err;
  575. if (!try_module_get(mtd->owner))
  576. return -ENODEV;
  577. if (mtd->_get_device) {
  578. err = mtd->_get_device(mtd);
  579. if (err) {
  580. module_put(mtd->owner);
  581. return err;
  582. }
  583. }
  584. mtd->usecount++;
  585. return 0;
  586. }
  587. EXPORT_SYMBOL_GPL(__get_mtd_device);
  588. /**
  589. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  590. * device name
  591. * @name: MTD device name to open
  592. *
  593. * This function returns MTD device description structure in case of
  594. * success and an error code in case of failure.
  595. */
  596. struct mtd_info *get_mtd_device_nm(const char *name)
  597. {
  598. int err = -ENODEV;
  599. struct mtd_info *mtd = NULL, *other;
  600. mutex_lock(&mtd_table_mutex);
  601. mtd_for_each_device(other) {
  602. if (!strcmp(name, other->name)) {
  603. mtd = other;
  604. break;
  605. }
  606. }
  607. if (!mtd)
  608. goto out_unlock;
  609. err = __get_mtd_device(mtd);
  610. if (err)
  611. goto out_unlock;
  612. mutex_unlock(&mtd_table_mutex);
  613. return mtd;
  614. out_unlock:
  615. mutex_unlock(&mtd_table_mutex);
  616. return ERR_PTR(err);
  617. }
  618. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  619. void put_mtd_device(struct mtd_info *mtd)
  620. {
  621. mutex_lock(&mtd_table_mutex);
  622. __put_mtd_device(mtd);
  623. mutex_unlock(&mtd_table_mutex);
  624. }
  625. EXPORT_SYMBOL_GPL(put_mtd_device);
  626. void __put_mtd_device(struct mtd_info *mtd)
  627. {
  628. --mtd->usecount;
  629. BUG_ON(mtd->usecount < 0);
  630. if (mtd->_put_device)
  631. mtd->_put_device(mtd);
  632. module_put(mtd->owner);
  633. }
  634. EXPORT_SYMBOL_GPL(__put_mtd_device);
  635. /*
  636. * Erase is an asynchronous operation. Device drivers are supposed
  637. * to call instr->callback() whenever the operation completes, even
  638. * if it completes with a failure.
  639. * Callers are supposed to pass a callback function and wait for it
  640. * to be called before writing to the block.
  641. */
  642. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  643. {
  644. if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
  645. return -EINVAL;
  646. if (!(mtd->flags & MTD_WRITEABLE))
  647. return -EROFS;
  648. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  649. if (!instr->len) {
  650. instr->state = MTD_ERASE_DONE;
  651. mtd_erase_callback(instr);
  652. return 0;
  653. }
  654. return mtd->_erase(mtd, instr);
  655. }
  656. EXPORT_SYMBOL_GPL(mtd_erase);
  657. /*
  658. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  659. */
  660. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  661. void **virt, resource_size_t *phys)
  662. {
  663. *retlen = 0;
  664. *virt = NULL;
  665. if (phys)
  666. *phys = 0;
  667. if (!mtd->_point)
  668. return -EOPNOTSUPP;
  669. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  670. return -EINVAL;
  671. if (!len)
  672. return 0;
  673. return mtd->_point(mtd, from, len, retlen, virt, phys);
  674. }
  675. EXPORT_SYMBOL_GPL(mtd_point);
  676. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  677. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  678. {
  679. if (!mtd->_point)
  680. return -EOPNOTSUPP;
  681. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  682. return -EINVAL;
  683. if (!len)
  684. return 0;
  685. return mtd->_unpoint(mtd, from, len);
  686. }
  687. EXPORT_SYMBOL_GPL(mtd_unpoint);
  688. /*
  689. * Allow NOMMU mmap() to directly map the device (if not NULL)
  690. * - return the address to which the offset maps
  691. * - return -ENOSYS to indicate refusal to do the mapping
  692. */
  693. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  694. unsigned long offset, unsigned long flags)
  695. {
  696. if (!mtd->_get_unmapped_area)
  697. return -EOPNOTSUPP;
  698. if (offset >= mtd->size || len > mtd->size - offset)
  699. return -EINVAL;
  700. return mtd->_get_unmapped_area(mtd, len, offset, flags);
  701. }
  702. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  703. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  704. u_char *buf)
  705. {
  706. int ret_code;
  707. *retlen = 0;
  708. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  709. return -EINVAL;
  710. if (!len)
  711. return 0;
  712. /*
  713. * In the absence of an error, drivers return a non-negative integer
  714. * representing the maximum number of bitflips that were corrected on
  715. * any one ecc region (if applicable; zero otherwise).
  716. */
  717. ret_code = mtd->_read(mtd, from, len, retlen, buf);
  718. if (unlikely(ret_code < 0))
  719. return ret_code;
  720. if (mtd->ecc_strength == 0)
  721. return 0; /* device lacks ecc */
  722. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  723. }
  724. EXPORT_SYMBOL_GPL(mtd_read);
  725. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  726. const u_char *buf)
  727. {
  728. *retlen = 0;
  729. if (to < 0 || to >= mtd->size || len > mtd->size - to)
  730. return -EINVAL;
  731. if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
  732. return -EROFS;
  733. if (!len)
  734. return 0;
  735. return mtd->_write(mtd, to, len, retlen, buf);
  736. }
  737. EXPORT_SYMBOL_GPL(mtd_write);
  738. /*
  739. * In blackbox flight recorder like scenarios we want to make successful writes
  740. * in interrupt context. panic_write() is only intended to be called when its
  741. * known the kernel is about to panic and we need the write to succeed. Since
  742. * the kernel is not going to be running for much longer, this function can
  743. * break locks and delay to ensure the write succeeds (but not sleep).
  744. */
  745. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  746. const u_char *buf)
  747. {
  748. *retlen = 0;
  749. if (!mtd->_panic_write)
  750. return -EOPNOTSUPP;
  751. if (to < 0 || to >= mtd->size || len > mtd->size - to)
  752. return -EINVAL;
  753. if (!(mtd->flags & MTD_WRITEABLE))
  754. return -EROFS;
  755. if (!len)
  756. return 0;
  757. return mtd->_panic_write(mtd, to, len, retlen, buf);
  758. }
  759. EXPORT_SYMBOL_GPL(mtd_panic_write);
  760. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  761. {
  762. int ret_code;
  763. ops->retlen = ops->oobretlen = 0;
  764. if (!mtd->_read_oob)
  765. return -EOPNOTSUPP;
  766. /*
  767. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  768. * similar to mtd->_read(), returning a non-negative integer
  769. * representing max bitflips. In other cases, mtd->_read_oob() may
  770. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  771. */
  772. ret_code = mtd->_read_oob(mtd, from, ops);
  773. if (unlikely(ret_code < 0))
  774. return ret_code;
  775. if (mtd->ecc_strength == 0)
  776. return 0; /* device lacks ecc */
  777. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  778. }
  779. EXPORT_SYMBOL_GPL(mtd_read_oob);
  780. /*
  781. * Method to access the protection register area, present in some flash
  782. * devices. The user data is one time programmable but the factory data is read
  783. * only.
  784. */
  785. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  786. struct otp_info *buf)
  787. {
  788. if (!mtd->_get_fact_prot_info)
  789. return -EOPNOTSUPP;
  790. if (!len)
  791. return 0;
  792. return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
  793. }
  794. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  795. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  796. size_t *retlen, u_char *buf)
  797. {
  798. *retlen = 0;
  799. if (!mtd->_read_fact_prot_reg)
  800. return -EOPNOTSUPP;
  801. if (!len)
  802. return 0;
  803. return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
  804. }
  805. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  806. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  807. struct otp_info *buf)
  808. {
  809. if (!mtd->_get_user_prot_info)
  810. return -EOPNOTSUPP;
  811. if (!len)
  812. return 0;
  813. return mtd->_get_user_prot_info(mtd, len, retlen, buf);
  814. }
  815. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  816. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  817. size_t *retlen, u_char *buf)
  818. {
  819. *retlen = 0;
  820. if (!mtd->_read_user_prot_reg)
  821. return -EOPNOTSUPP;
  822. if (!len)
  823. return 0;
  824. return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
  825. }
  826. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  827. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  828. size_t *retlen, u_char *buf)
  829. {
  830. int ret;
  831. *retlen = 0;
  832. if (!mtd->_write_user_prot_reg)
  833. return -EOPNOTSUPP;
  834. if (!len)
  835. return 0;
  836. ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
  837. if (ret)
  838. return ret;
  839. /*
  840. * If no data could be written at all, we are out of memory and
  841. * must return -ENOSPC.
  842. */
  843. return (*retlen) ? 0 : -ENOSPC;
  844. }
  845. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  846. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  847. {
  848. if (!mtd->_lock_user_prot_reg)
  849. return -EOPNOTSUPP;
  850. if (!len)
  851. return 0;
  852. return mtd->_lock_user_prot_reg(mtd, from, len);
  853. }
  854. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  855. /* Chip-supported device locking */
  856. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  857. {
  858. if (!mtd->_lock)
  859. return -EOPNOTSUPP;
  860. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  861. return -EINVAL;
  862. if (!len)
  863. return 0;
  864. return mtd->_lock(mtd, ofs, len);
  865. }
  866. EXPORT_SYMBOL_GPL(mtd_lock);
  867. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  868. {
  869. if (!mtd->_unlock)
  870. return -EOPNOTSUPP;
  871. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  872. return -EINVAL;
  873. if (!len)
  874. return 0;
  875. return mtd->_unlock(mtd, ofs, len);
  876. }
  877. EXPORT_SYMBOL_GPL(mtd_unlock);
  878. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  879. {
  880. if (!mtd->_is_locked)
  881. return -EOPNOTSUPP;
  882. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  883. return -EINVAL;
  884. if (!len)
  885. return 0;
  886. return mtd->_is_locked(mtd, ofs, len);
  887. }
  888. EXPORT_SYMBOL_GPL(mtd_is_locked);
  889. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  890. {
  891. if (ofs < 0 || ofs >= mtd->size)
  892. return -EINVAL;
  893. if (!mtd->_block_isreserved)
  894. return 0;
  895. return mtd->_block_isreserved(mtd, ofs);
  896. }
  897. EXPORT_SYMBOL_GPL(mtd_block_isreserved);
  898. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  899. {
  900. if (ofs < 0 || ofs >= mtd->size)
  901. return -EINVAL;
  902. if (!mtd->_block_isbad)
  903. return 0;
  904. return mtd->_block_isbad(mtd, ofs);
  905. }
  906. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  907. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  908. {
  909. if (!mtd->_block_markbad)
  910. return -EOPNOTSUPP;
  911. if (ofs < 0 || ofs >= mtd->size)
  912. return -EINVAL;
  913. if (!(mtd->flags & MTD_WRITEABLE))
  914. return -EROFS;
  915. return mtd->_block_markbad(mtd, ofs, NULL);
  916. }
  917. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  918. int mtd_block_markbad_hw(struct mtd_info *mtd, loff_t ofs, const uint8_t *buf)
  919. {
  920. if (!mtd->_block_markbad)
  921. return -EOPNOTSUPP;
  922. if (ofs < 0 || ofs >= mtd->size)
  923. return -EINVAL;
  924. if (!(mtd->flags & MTD_WRITEABLE))
  925. return -EROFS;
  926. return mtd->_block_markbad(mtd, ofs, buf);
  927. }
  928. EXPORT_SYMBOL_GPL(mtd_block_markbad_hw);
  929. /*
  930. * default_mtd_writev - the default writev method
  931. * @mtd: mtd device description object pointer
  932. * @vecs: the vectors to write
  933. * @count: count of vectors in @vecs
  934. * @to: the MTD device offset to write to
  935. * @retlen: on exit contains the count of bytes written to the MTD device.
  936. *
  937. * This function returns zero in case of success and a negative error code in
  938. * case of failure.
  939. */
  940. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  941. unsigned long count, loff_t to, size_t *retlen)
  942. {
  943. unsigned long i;
  944. size_t totlen = 0, thislen;
  945. int ret = 0;
  946. for (i = 0; i < count; i++) {
  947. if (!vecs[i].iov_len)
  948. continue;
  949. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  950. vecs[i].iov_base);
  951. totlen += thislen;
  952. if (ret || thislen != vecs[i].iov_len)
  953. break;
  954. to += vecs[i].iov_len;
  955. }
  956. *retlen = totlen;
  957. return ret;
  958. }
  959. /*
  960. * mtd_writev - the vector-based MTD write method
  961. * @mtd: mtd device description object pointer
  962. * @vecs: the vectors to write
  963. * @count: count of vectors in @vecs
  964. * @to: the MTD device offset to write to
  965. * @retlen: on exit contains the count of bytes written to the MTD device.
  966. *
  967. * This function returns zero in case of success and a negative error code in
  968. * case of failure.
  969. */
  970. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  971. unsigned long count, loff_t to, size_t *retlen)
  972. {
  973. *retlen = 0;
  974. if (!(mtd->flags & MTD_WRITEABLE))
  975. return -EROFS;
  976. if (!mtd->_writev)
  977. return default_mtd_writev(mtd, vecs, count, to, retlen);
  978. return mtd->_writev(mtd, vecs, count, to, retlen);
  979. }
  980. EXPORT_SYMBOL_GPL(mtd_writev);
  981. /**
  982. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  983. * @mtd: mtd device description object pointer
  984. * @size: a pointer to the ideal or maximum size of the allocation, points
  985. * to the actual allocation size on success.
  986. *
  987. * This routine attempts to allocate a contiguous kernel buffer up to
  988. * the specified size, backing off the size of the request exponentially
  989. * until the request succeeds or until the allocation size falls below
  990. * the system page size. This attempts to make sure it does not adversely
  991. * impact system performance, so when allocating more than one page, we
  992. * ask the memory allocator to avoid re-trying, swapping, writing back
  993. * or performing I/O.
  994. *
  995. * Note, this function also makes sure that the allocated buffer is aligned to
  996. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  997. *
  998. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  999. * to handle smaller (i.e. degraded) buffer allocations under low- or
  1000. * fragmented-memory situations where such reduced allocations, from a
  1001. * requested ideal, are allowed.
  1002. *
  1003. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  1004. */
  1005. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  1006. {
  1007. gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
  1008. __GFP_NORETRY | __GFP_NO_KSWAPD;
  1009. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  1010. void *kbuf;
  1011. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  1012. while (*size > min_alloc) {
  1013. kbuf = kmalloc(*size, flags);
  1014. if (kbuf)
  1015. return kbuf;
  1016. *size >>= 1;
  1017. *size = ALIGN(*size, mtd->writesize);
  1018. }
  1019. /*
  1020. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  1021. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  1022. */
  1023. return kmalloc(*size, GFP_KERNEL);
  1024. }
  1025. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  1026. #ifdef CONFIG_PROC_FS
  1027. /*====================================================================*/
  1028. /* Support for /proc/mtd */
  1029. static int mtd_proc_show(struct seq_file *m, void *v)
  1030. {
  1031. struct mtd_info *mtd;
  1032. seq_puts(m, "dev: size erasesize name\n");
  1033. mutex_lock(&mtd_table_mutex);
  1034. mtd_for_each_device(mtd) {
  1035. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  1036. mtd->index, (unsigned long long)mtd->size,
  1037. mtd->erasesize, mtd->name);
  1038. }
  1039. mutex_unlock(&mtd_table_mutex);
  1040. return 0;
  1041. }
  1042. static int mtd_proc_open(struct inode *inode, struct file *file)
  1043. {
  1044. return single_open(file, mtd_proc_show, NULL);
  1045. }
  1046. static const struct file_operations mtd_proc_ops = {
  1047. .open = mtd_proc_open,
  1048. .read = seq_read,
  1049. .llseek = seq_lseek,
  1050. .release = single_release,
  1051. };
  1052. #endif /* CONFIG_PROC_FS */
  1053. /*====================================================================*/
  1054. /* Init code */
  1055. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  1056. {
  1057. int ret;
  1058. ret = bdi_init(bdi);
  1059. if (!ret)
  1060. ret = bdi_register(bdi, NULL, "%s", name);
  1061. if (ret)
  1062. bdi_destroy(bdi);
  1063. return ret;
  1064. }
  1065. static struct proc_dir_entry *proc_mtd;
  1066. /* tonykuo 2013-11-05 */
  1067. static const struct file_operations mtd_write_proc_fops = {
  1068. .owner = THIS_MODULE,
  1069. .write = mtd_writeable_proc_write,
  1070. };
  1071. static const struct file_operations mtd_change_proc_fops = {
  1072. .owner = THIS_MODULE,
  1073. .write = mtd_change_proc_write,
  1074. };
  1075. static int __init init_mtd(void)
  1076. {
  1077. int ret;
  1078. ret = class_register(&mtd_class);
  1079. if (ret)
  1080. goto err_reg;
  1081. ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
  1082. if (ret)
  1083. goto err_bdi1;
  1084. ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
  1085. if (ret)
  1086. goto err_bdi2;
  1087. ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
  1088. if (ret)
  1089. goto err_bdi3;
  1090. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  1091. #ifdef DYNAMIC_CHANGE_MTD_WRITEABLE /* tonykuo 2013-11-05 */
  1092. entry = proc_create("driver/mtd_writeable", 0600, NULL, &mtd_write_proc_fops);
  1093. if (entry != NULL)
  1094. pr_debug("mtd_writeable success\n");
  1095. entry = proc_create("driver/mtd_change", 0600, NULL, &mtd_change_proc_fops);
  1096. if (entry != NULL)
  1097. pr_debug("mtd_change success\n");
  1098. #endif
  1099. ret = init_mtdchar();
  1100. if (ret)
  1101. goto out_procfs;
  1102. return 0;
  1103. out_procfs:
  1104. if (proc_mtd)
  1105. remove_proc_entry("mtd", NULL);
  1106. err_bdi3:
  1107. bdi_destroy(&mtd_bdi_ro_mappable);
  1108. err_bdi2:
  1109. bdi_destroy(&mtd_bdi_unmappable);
  1110. err_bdi1:
  1111. class_unregister(&mtd_class);
  1112. err_reg:
  1113. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1114. return ret;
  1115. }
  1116. static void __exit cleanup_mtd(void)
  1117. {
  1118. cleanup_mtdchar();
  1119. if (proc_mtd)
  1120. remove_proc_entry("mtd", NULL);
  1121. #ifdef DYNAMIC_CHANGE_MTD_WRITEABLE /* tonykuo 2013-11-05 */
  1122. if (entry) {
  1123. remove_proc_entry("driver/mtd_writeable", NULL);
  1124. remove_proc_entry("driver/mtd_change", NULL);
  1125. }
  1126. #endif
  1127. class_unregister(&mtd_class);
  1128. bdi_destroy(&mtd_bdi_unmappable);
  1129. bdi_destroy(&mtd_bdi_ro_mappable);
  1130. bdi_destroy(&mtd_bdi_rw_mappable);
  1131. }
  1132. module_init(init_mtd);
  1133. module_exit(cleanup_mtd);
  1134. MODULE_LICENSE("GPL");
  1135. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1136. MODULE_DESCRIPTION("Core MTD registration and access routines");