mtdcore.c 35 KB

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