mtdcore.c 34 KB

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