mtdcore.c 35 KB

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