mtdcore.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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. /* Some chips always power up locked. Unlock them now */
  348. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  349. error = mtd_unlock(mtd, 0, mtd->size);
  350. if (error && error != -EOPNOTSUPP)
  351. printk(KERN_WARNING
  352. "%s: unlock failed, writes may not work\n",
  353. mtd->name);
  354. /* Ignore unlock failures? */
  355. error = 0;
  356. }
  357. /* Caller should have set dev.parent to match the
  358. * physical device, if appropriate.
  359. */
  360. mtd->dev.type = &mtd_devtype;
  361. mtd->dev.class = &mtd_class;
  362. mtd->dev.devt = MTD_DEVT(i);
  363. dev_set_name(&mtd->dev, "mtd%d", i);
  364. dev_set_drvdata(&mtd->dev, mtd);
  365. error = device_register(&mtd->dev);
  366. if (error)
  367. goto fail_added;
  368. device_create(&mtd_class, mtd->dev.parent, MTD_DEVT(i) + 1, NULL,
  369. "mtd%dro", i);
  370. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  371. /* No need to get a refcount on the module containing
  372. the notifier, since we hold the mtd_table_mutex */
  373. list_for_each_entry(not, &mtd_notifiers, list)
  374. not->add(mtd);
  375. mutex_unlock(&mtd_table_mutex);
  376. /* We _know_ we aren't being removed, because
  377. our caller is still holding us here. So none
  378. of this try_ nonsense, and no bitching about it
  379. either. :) */
  380. __module_get(THIS_MODULE);
  381. return 0;
  382. fail_added:
  383. idr_remove(&mtd_idr, i);
  384. fail_locked:
  385. mutex_unlock(&mtd_table_mutex);
  386. return error;
  387. }
  388. /**
  389. * del_mtd_device - unregister an MTD device
  390. * @mtd: pointer to MTD device info structure
  391. *
  392. * Remove a device from the list of MTD devices present in the system,
  393. * and notify each currently active MTD 'user' of its departure.
  394. * Returns zero on success or 1 on failure, which currently will happen
  395. * if the requested device does not appear to be present in the list.
  396. */
  397. int del_mtd_device(struct mtd_info *mtd)
  398. {
  399. int ret;
  400. struct mtd_notifier *not;
  401. mutex_lock(&mtd_table_mutex);
  402. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  403. ret = -ENODEV;
  404. goto out_error;
  405. }
  406. /* No need to get a refcount on the module containing
  407. the notifier, since we hold the mtd_table_mutex */
  408. list_for_each_entry(not, &mtd_notifiers, list)
  409. not->remove(mtd);
  410. if (mtd->usecount) {
  411. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  412. mtd->index, mtd->name, mtd->usecount);
  413. ret = -EBUSY;
  414. } else {
  415. device_unregister(&mtd->dev);
  416. idr_remove(&mtd_idr, mtd->index);
  417. module_put(THIS_MODULE);
  418. ret = 0;
  419. }
  420. out_error:
  421. mutex_unlock(&mtd_table_mutex);
  422. return ret;
  423. }
  424. static int mtd_add_device_partitions(struct mtd_info *mtd,
  425. struct mtd_partition *real_parts,
  426. int nbparts)
  427. {
  428. int ret;
  429. if (nbparts == 0 || IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER)) {
  430. ret = add_mtd_device(mtd);
  431. if (ret)
  432. return ret;
  433. }
  434. if (nbparts > 0) {
  435. ret = add_mtd_partitions(mtd, real_parts, nbparts);
  436. if (ret && IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
  437. del_mtd_device(mtd);
  438. return ret;
  439. }
  440. return 0;
  441. }
  442. /*
  443. * Set a few defaults based on the parent devices, if not provided by the
  444. * driver
  445. */
  446. static void mtd_set_dev_defaults(struct mtd_info *mtd)
  447. {
  448. if (mtd->dev.parent) {
  449. if (!mtd->owner && mtd->dev.parent->driver)
  450. mtd->owner = mtd->dev.parent->driver->owner;
  451. if (!mtd->name)
  452. mtd->name = dev_name(mtd->dev.parent);
  453. } else {
  454. pr_debug("mtd device won't show a device symlink in sysfs\n");
  455. }
  456. }
  457. /**
  458. * mtd_device_parse_register - parse partitions and register an MTD device.
  459. *
  460. * @mtd: the MTD device to register
  461. * @types: the list of MTD partition probes to try, see
  462. * 'parse_mtd_partitions()' for more information
  463. * @parser_data: MTD partition parser-specific data
  464. * @parts: fallback partition information to register, if parsing fails;
  465. * only valid if %nr_parts > %0
  466. * @nr_parts: the number of partitions in parts, if zero then the full
  467. * MTD device is registered if no partition info is found
  468. *
  469. * This function aggregates MTD partitions parsing (done by
  470. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  471. * basically follows the most common pattern found in many MTD drivers:
  472. *
  473. * * It first tries to probe partitions on MTD device @mtd using parsers
  474. * specified in @types (if @types is %NULL, then the default list of parsers
  475. * is used, see 'parse_mtd_partitions()' for more information). If none are
  476. * found this functions tries to fallback to information specified in
  477. * @parts/@nr_parts.
  478. * * If any partitioning info was found, this function registers the found
  479. * partitions. If the MTD_PARTITIONED_MASTER option is set, then the device
  480. * as a whole is registered first.
  481. * * If no partitions were found this function just registers the MTD device
  482. * @mtd and exits.
  483. *
  484. * Returns zero in case of success and a negative error code in case of failure.
  485. */
  486. int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
  487. struct mtd_part_parser_data *parser_data,
  488. const struct mtd_partition *parts,
  489. int nr_parts)
  490. {
  491. int ret;
  492. struct mtd_partition *real_parts = NULL;
  493. mtd_set_dev_defaults(mtd);
  494. ret = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
  495. if (ret <= 0 && nr_parts && parts) {
  496. real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
  497. GFP_KERNEL);
  498. if (!real_parts)
  499. ret = -ENOMEM;
  500. else
  501. ret = nr_parts;
  502. }
  503. /* Didn't come up with either parsed OR fallback partitions */
  504. if (ret < 0) {
  505. pr_info("mtd: failed to find partitions; one or more parsers reports errors (%d)\n",
  506. ret);
  507. /* Don't abort on errors; we can still use unpartitioned MTD */
  508. ret = 0;
  509. }
  510. ret = mtd_add_device_partitions(mtd, real_parts, ret);
  511. if (ret)
  512. goto out;
  513. /*
  514. * FIXME: some drivers unfortunately call this function more than once.
  515. * So we have to check if we've already assigned the reboot notifier.
  516. *
  517. * Generally, we can make multiple calls work for most cases, but it
  518. * does cause problems with parse_mtd_partitions() above (e.g.,
  519. * cmdlineparts will register partitions more than once).
  520. */
  521. WARN_ONCE(mtd->_reboot && mtd->reboot_notifier.notifier_call,
  522. "MTD already registered\n");
  523. if (mtd->_reboot && !mtd->reboot_notifier.notifier_call) {
  524. mtd->reboot_notifier.notifier_call = mtd_reboot_notifier;
  525. register_reboot_notifier(&mtd->reboot_notifier);
  526. }
  527. out:
  528. kfree(real_parts);
  529. return ret;
  530. }
  531. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  532. /**
  533. * mtd_device_unregister - unregister an existing MTD device.
  534. *
  535. * @master: the MTD device to unregister. This will unregister both the master
  536. * and any partitions if registered.
  537. */
  538. int mtd_device_unregister(struct mtd_info *master)
  539. {
  540. int err;
  541. if (master->_reboot)
  542. unregister_reboot_notifier(&master->reboot_notifier);
  543. err = del_mtd_partitions(master);
  544. if (err)
  545. return err;
  546. if (!device_is_registered(&master->dev))
  547. return 0;
  548. return del_mtd_device(master);
  549. }
  550. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  551. /**
  552. * register_mtd_user - register a 'user' of MTD devices.
  553. * @new: pointer to notifier info structure
  554. *
  555. * Registers a pair of callbacks function to be called upon addition
  556. * or removal of MTD devices. Causes the 'add' callback to be immediately
  557. * invoked for each MTD device currently present in the system.
  558. */
  559. void register_mtd_user (struct mtd_notifier *new)
  560. {
  561. struct mtd_info *mtd;
  562. mutex_lock(&mtd_table_mutex);
  563. list_add(&new->list, &mtd_notifiers);
  564. __module_get(THIS_MODULE);
  565. mtd_for_each_device(mtd)
  566. new->add(mtd);
  567. mutex_unlock(&mtd_table_mutex);
  568. }
  569. EXPORT_SYMBOL_GPL(register_mtd_user);
  570. /**
  571. * unregister_mtd_user - unregister a 'user' of MTD devices.
  572. * @old: pointer to notifier info structure
  573. *
  574. * Removes a callback function pair from the list of 'users' to be
  575. * notified upon addition or removal of MTD devices. Causes the
  576. * 'remove' callback to be immediately invoked for each MTD device
  577. * currently present in the system.
  578. */
  579. int unregister_mtd_user (struct mtd_notifier *old)
  580. {
  581. struct mtd_info *mtd;
  582. mutex_lock(&mtd_table_mutex);
  583. module_put(THIS_MODULE);
  584. mtd_for_each_device(mtd)
  585. old->remove(mtd);
  586. list_del(&old->list);
  587. mutex_unlock(&mtd_table_mutex);
  588. return 0;
  589. }
  590. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  591. /**
  592. * get_mtd_device - obtain a validated handle for an MTD device
  593. * @mtd: last known address of the required MTD device
  594. * @num: internal device number of the required MTD device
  595. *
  596. * Given a number and NULL address, return the num'th entry in the device
  597. * table, if any. Given an address and num == -1, search the device table
  598. * for a device with that address and return if it's still present. Given
  599. * both, return the num'th driver only if its address matches. Return
  600. * error code if not.
  601. */
  602. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  603. {
  604. struct mtd_info *ret = NULL, *other;
  605. int err = -ENODEV;
  606. mutex_lock(&mtd_table_mutex);
  607. if (num == -1) {
  608. mtd_for_each_device(other) {
  609. if (other == mtd) {
  610. ret = mtd;
  611. break;
  612. }
  613. }
  614. } else if (num >= 0) {
  615. ret = idr_find(&mtd_idr, num);
  616. if (mtd && mtd != ret)
  617. ret = NULL;
  618. }
  619. if (!ret) {
  620. ret = ERR_PTR(err);
  621. goto out;
  622. }
  623. err = __get_mtd_device(ret);
  624. if (err)
  625. ret = ERR_PTR(err);
  626. out:
  627. mutex_unlock(&mtd_table_mutex);
  628. return ret;
  629. }
  630. EXPORT_SYMBOL_GPL(get_mtd_device);
  631. int __get_mtd_device(struct mtd_info *mtd)
  632. {
  633. int err;
  634. if (!try_module_get(mtd->owner))
  635. return -ENODEV;
  636. if (mtd->_get_device) {
  637. err = mtd->_get_device(mtd);
  638. if (err) {
  639. module_put(mtd->owner);
  640. return err;
  641. }
  642. }
  643. mtd->usecount++;
  644. return 0;
  645. }
  646. EXPORT_SYMBOL_GPL(__get_mtd_device);
  647. /**
  648. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  649. * device name
  650. * @name: MTD device name to open
  651. *
  652. * This function returns MTD device description structure in case of
  653. * success and an error code in case of failure.
  654. */
  655. struct mtd_info *get_mtd_device_nm(const char *name)
  656. {
  657. int err = -ENODEV;
  658. struct mtd_info *mtd = NULL, *other;
  659. mutex_lock(&mtd_table_mutex);
  660. mtd_for_each_device(other) {
  661. if (!strcmp(name, other->name)) {
  662. mtd = other;
  663. break;
  664. }
  665. }
  666. if (!mtd)
  667. goto out_unlock;
  668. err = __get_mtd_device(mtd);
  669. if (err)
  670. goto out_unlock;
  671. mutex_unlock(&mtd_table_mutex);
  672. return mtd;
  673. out_unlock:
  674. mutex_unlock(&mtd_table_mutex);
  675. return ERR_PTR(err);
  676. }
  677. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  678. void put_mtd_device(struct mtd_info *mtd)
  679. {
  680. mutex_lock(&mtd_table_mutex);
  681. __put_mtd_device(mtd);
  682. mutex_unlock(&mtd_table_mutex);
  683. }
  684. EXPORT_SYMBOL_GPL(put_mtd_device);
  685. void __put_mtd_device(struct mtd_info *mtd)
  686. {
  687. --mtd->usecount;
  688. BUG_ON(mtd->usecount < 0);
  689. if (mtd->_put_device)
  690. mtd->_put_device(mtd);
  691. module_put(mtd->owner);
  692. }
  693. EXPORT_SYMBOL_GPL(__put_mtd_device);
  694. /*
  695. * Erase is an asynchronous operation. Device drivers are supposed
  696. * to call instr->callback() whenever the operation completes, even
  697. * if it completes with a failure.
  698. * Callers are supposed to pass a callback function and wait for it
  699. * to be called before writing to the block.
  700. */
  701. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  702. {
  703. if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
  704. return -EINVAL;
  705. if (!(mtd->flags & MTD_WRITEABLE))
  706. return -EROFS;
  707. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  708. if (!instr->len) {
  709. instr->state = MTD_ERASE_DONE;
  710. mtd_erase_callback(instr);
  711. return 0;
  712. }
  713. return mtd->_erase(mtd, instr);
  714. }
  715. EXPORT_SYMBOL_GPL(mtd_erase);
  716. /*
  717. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  718. */
  719. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  720. void **virt, resource_size_t *phys)
  721. {
  722. *retlen = 0;
  723. *virt = NULL;
  724. if (phys)
  725. *phys = 0;
  726. if (!mtd->_point)
  727. return -EOPNOTSUPP;
  728. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  729. return -EINVAL;
  730. if (!len)
  731. return 0;
  732. return mtd->_point(mtd, from, len, retlen, virt, phys);
  733. }
  734. EXPORT_SYMBOL_GPL(mtd_point);
  735. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  736. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  737. {
  738. if (!mtd->_point)
  739. return -EOPNOTSUPP;
  740. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  741. return -EINVAL;
  742. if (!len)
  743. return 0;
  744. return mtd->_unpoint(mtd, from, len);
  745. }
  746. EXPORT_SYMBOL_GPL(mtd_unpoint);
  747. /*
  748. * Allow NOMMU mmap() to directly map the device (if not NULL)
  749. * - return the address to which the offset maps
  750. * - return -ENOSYS to indicate refusal to do the mapping
  751. */
  752. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  753. unsigned long offset, unsigned long flags)
  754. {
  755. if (!mtd->_get_unmapped_area)
  756. return -EOPNOTSUPP;
  757. if (offset >= mtd->size || len > mtd->size - offset)
  758. return -EINVAL;
  759. return mtd->_get_unmapped_area(mtd, len, offset, flags);
  760. }
  761. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  762. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  763. u_char *buf)
  764. {
  765. int ret_code;
  766. *retlen = 0;
  767. if (from < 0 || from >= mtd->size || len > mtd->size - from)
  768. return -EINVAL;
  769. if (!len)
  770. return 0;
  771. /*
  772. * In the absence of an error, drivers return a non-negative integer
  773. * representing the maximum number of bitflips that were corrected on
  774. * any one ecc region (if applicable; zero otherwise).
  775. */
  776. ret_code = mtd->_read(mtd, from, len, retlen, buf);
  777. if (unlikely(ret_code < 0))
  778. return ret_code;
  779. if (mtd->ecc_strength == 0)
  780. return 0; /* device lacks ecc */
  781. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  782. }
  783. EXPORT_SYMBOL_GPL(mtd_read);
  784. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  785. const u_char *buf)
  786. {
  787. *retlen = 0;
  788. if (to < 0 || to >= mtd->size || len > mtd->size - to)
  789. return -EINVAL;
  790. if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
  791. return -EROFS;
  792. if (!len)
  793. return 0;
  794. return mtd->_write(mtd, to, len, retlen, buf);
  795. }
  796. EXPORT_SYMBOL_GPL(mtd_write);
  797. /*
  798. * In blackbox flight recorder like scenarios we want to make successful writes
  799. * in interrupt context. panic_write() is only intended to be called when its
  800. * known the kernel is about to panic and we need the write to succeed. Since
  801. * the kernel is not going to be running for much longer, this function can
  802. * break locks and delay to ensure the write succeeds (but not sleep).
  803. */
  804. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  805. const u_char *buf)
  806. {
  807. *retlen = 0;
  808. if (!mtd->_panic_write)
  809. return -EOPNOTSUPP;
  810. if (to < 0 || to >= mtd->size || len > mtd->size - to)
  811. return -EINVAL;
  812. if (!(mtd->flags & MTD_WRITEABLE))
  813. return -EROFS;
  814. if (!len)
  815. return 0;
  816. return mtd->_panic_write(mtd, to, len, retlen, buf);
  817. }
  818. EXPORT_SYMBOL_GPL(mtd_panic_write);
  819. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  820. {
  821. int ret_code;
  822. ops->retlen = ops->oobretlen = 0;
  823. if (!mtd->_read_oob)
  824. return -EOPNOTSUPP;
  825. /*
  826. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  827. * similar to mtd->_read(), returning a non-negative integer
  828. * representing max bitflips. In other cases, mtd->_read_oob() may
  829. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  830. */
  831. ret_code = mtd->_read_oob(mtd, from, ops);
  832. if (unlikely(ret_code < 0))
  833. return ret_code;
  834. if (mtd->ecc_strength == 0)
  835. return 0; /* device lacks ecc */
  836. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  837. }
  838. EXPORT_SYMBOL_GPL(mtd_read_oob);
  839. /*
  840. * Method to access the protection register area, present in some flash
  841. * devices. The user data is one time programmable but the factory data is read
  842. * only.
  843. */
  844. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  845. struct otp_info *buf)
  846. {
  847. if (!mtd->_get_fact_prot_info)
  848. return -EOPNOTSUPP;
  849. if (!len)
  850. return 0;
  851. return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
  852. }
  853. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  854. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  855. size_t *retlen, u_char *buf)
  856. {
  857. *retlen = 0;
  858. if (!mtd->_read_fact_prot_reg)
  859. return -EOPNOTSUPP;
  860. if (!len)
  861. return 0;
  862. return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
  863. }
  864. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  865. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  866. struct otp_info *buf)
  867. {
  868. if (!mtd->_get_user_prot_info)
  869. return -EOPNOTSUPP;
  870. if (!len)
  871. return 0;
  872. return mtd->_get_user_prot_info(mtd, len, retlen, buf);
  873. }
  874. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  875. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  876. size_t *retlen, u_char *buf)
  877. {
  878. *retlen = 0;
  879. if (!mtd->_read_user_prot_reg)
  880. return -EOPNOTSUPP;
  881. if (!len)
  882. return 0;
  883. return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
  884. }
  885. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  886. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  887. size_t *retlen, u_char *buf)
  888. {
  889. int ret;
  890. *retlen = 0;
  891. if (!mtd->_write_user_prot_reg)
  892. return -EOPNOTSUPP;
  893. if (!len)
  894. return 0;
  895. ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
  896. if (ret)
  897. return ret;
  898. /*
  899. * If no data could be written at all, we are out of memory and
  900. * must return -ENOSPC.
  901. */
  902. return (*retlen) ? 0 : -ENOSPC;
  903. }
  904. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  905. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  906. {
  907. if (!mtd->_lock_user_prot_reg)
  908. return -EOPNOTSUPP;
  909. if (!len)
  910. return 0;
  911. return mtd->_lock_user_prot_reg(mtd, from, len);
  912. }
  913. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  914. /* Chip-supported device locking */
  915. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  916. {
  917. if (!mtd->_lock)
  918. return -EOPNOTSUPP;
  919. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  920. return -EINVAL;
  921. if (!len)
  922. return 0;
  923. return mtd->_lock(mtd, ofs, len);
  924. }
  925. EXPORT_SYMBOL_GPL(mtd_lock);
  926. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  927. {
  928. if (!mtd->_unlock)
  929. return -EOPNOTSUPP;
  930. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  931. return -EINVAL;
  932. if (!len)
  933. return 0;
  934. return mtd->_unlock(mtd, ofs, len);
  935. }
  936. EXPORT_SYMBOL_GPL(mtd_unlock);
  937. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  938. {
  939. if (!mtd->_is_locked)
  940. return -EOPNOTSUPP;
  941. if (ofs < 0 || ofs >= mtd->size || len > mtd->size - ofs)
  942. return -EINVAL;
  943. if (!len)
  944. return 0;
  945. return mtd->_is_locked(mtd, ofs, len);
  946. }
  947. EXPORT_SYMBOL_GPL(mtd_is_locked);
  948. int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  949. {
  950. if (ofs < 0 || ofs >= mtd->size)
  951. return -EINVAL;
  952. if (!mtd->_block_isreserved)
  953. return 0;
  954. return mtd->_block_isreserved(mtd, ofs);
  955. }
  956. EXPORT_SYMBOL_GPL(mtd_block_isreserved);
  957. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  958. {
  959. if (ofs < 0 || ofs >= mtd->size)
  960. return -EINVAL;
  961. if (!mtd->_block_isbad)
  962. return 0;
  963. return mtd->_block_isbad(mtd, ofs);
  964. }
  965. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  966. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  967. {
  968. if (!mtd->_block_markbad)
  969. return -EOPNOTSUPP;
  970. if (ofs < 0 || ofs >= mtd->size)
  971. return -EINVAL;
  972. if (!(mtd->flags & MTD_WRITEABLE))
  973. return -EROFS;
  974. return mtd->_block_markbad(mtd, ofs);
  975. }
  976. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  977. /*
  978. * default_mtd_writev - the default writev method
  979. * @mtd: mtd device description object pointer
  980. * @vecs: the vectors to write
  981. * @count: count of vectors in @vecs
  982. * @to: the MTD device offset to write to
  983. * @retlen: on exit contains the count of bytes written to the MTD device.
  984. *
  985. * This function returns zero in case of success and a negative error code in
  986. * case of failure.
  987. */
  988. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  989. unsigned long count, loff_t to, size_t *retlen)
  990. {
  991. unsigned long i;
  992. size_t totlen = 0, thislen;
  993. int ret = 0;
  994. for (i = 0; i < count; i++) {
  995. if (!vecs[i].iov_len)
  996. continue;
  997. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  998. vecs[i].iov_base);
  999. totlen += thislen;
  1000. if (ret || thislen != vecs[i].iov_len)
  1001. break;
  1002. to += vecs[i].iov_len;
  1003. }
  1004. *retlen = totlen;
  1005. return ret;
  1006. }
  1007. /*
  1008. * mtd_writev - the vector-based MTD write method
  1009. * @mtd: mtd device description object pointer
  1010. * @vecs: the vectors to write
  1011. * @count: count of vectors in @vecs
  1012. * @to: the MTD device offset to write to
  1013. * @retlen: on exit contains the count of bytes written to the MTD device.
  1014. *
  1015. * This function returns zero in case of success and a negative error code in
  1016. * case of failure.
  1017. */
  1018. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  1019. unsigned long count, loff_t to, size_t *retlen)
  1020. {
  1021. *retlen = 0;
  1022. if (!(mtd->flags & MTD_WRITEABLE))
  1023. return -EROFS;
  1024. if (!mtd->_writev)
  1025. return default_mtd_writev(mtd, vecs, count, to, retlen);
  1026. return mtd->_writev(mtd, vecs, count, to, retlen);
  1027. }
  1028. EXPORT_SYMBOL_GPL(mtd_writev);
  1029. /**
  1030. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  1031. * @mtd: mtd device description object pointer
  1032. * @size: a pointer to the ideal or maximum size of the allocation, points
  1033. * to the actual allocation size on success.
  1034. *
  1035. * This routine attempts to allocate a contiguous kernel buffer up to
  1036. * the specified size, backing off the size of the request exponentially
  1037. * until the request succeeds or until the allocation size falls below
  1038. * the system page size. This attempts to make sure it does not adversely
  1039. * impact system performance, so when allocating more than one page, we
  1040. * ask the memory allocator to avoid re-trying, swapping, writing back
  1041. * or performing I/O.
  1042. *
  1043. * Note, this function also makes sure that the allocated buffer is aligned to
  1044. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  1045. *
  1046. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  1047. * to handle smaller (i.e. degraded) buffer allocations under low- or
  1048. * fragmented-memory situations where such reduced allocations, from a
  1049. * requested ideal, are allowed.
  1050. *
  1051. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  1052. */
  1053. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  1054. {
  1055. gfp_t flags = __GFP_NOWARN | __GFP_DIRECT_RECLAIM | __GFP_NORETRY;
  1056. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  1057. void *kbuf;
  1058. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  1059. while (*size > min_alloc) {
  1060. kbuf = kmalloc(*size, flags);
  1061. if (kbuf)
  1062. return kbuf;
  1063. *size >>= 1;
  1064. *size = ALIGN(*size, mtd->writesize);
  1065. }
  1066. /*
  1067. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  1068. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  1069. */
  1070. return kmalloc(*size, GFP_KERNEL);
  1071. }
  1072. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  1073. #ifdef CONFIG_PROC_FS
  1074. /*====================================================================*/
  1075. /* Support for /proc/mtd */
  1076. static int mtd_proc_show(struct seq_file *m, void *v)
  1077. {
  1078. struct mtd_info *mtd;
  1079. seq_puts(m, "dev: size erasesize name\n");
  1080. mutex_lock(&mtd_table_mutex);
  1081. mtd_for_each_device(mtd) {
  1082. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  1083. mtd->index, (unsigned long long)mtd->size,
  1084. mtd->erasesize, mtd->name);
  1085. }
  1086. mutex_unlock(&mtd_table_mutex);
  1087. return 0;
  1088. }
  1089. static int mtd_proc_open(struct inode *inode, struct file *file)
  1090. {
  1091. return single_open(file, mtd_proc_show, NULL);
  1092. }
  1093. static const struct file_operations mtd_proc_ops = {
  1094. .open = mtd_proc_open,
  1095. .read = seq_read,
  1096. .llseek = seq_lseek,
  1097. .release = single_release,
  1098. };
  1099. #endif /* CONFIG_PROC_FS */
  1100. /*====================================================================*/
  1101. /* Init code */
  1102. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  1103. {
  1104. int ret;
  1105. ret = bdi_init(bdi);
  1106. if (!ret)
  1107. ret = bdi_register(bdi, NULL, "%s", name);
  1108. if (ret)
  1109. bdi_destroy(bdi);
  1110. return ret;
  1111. }
  1112. static struct proc_dir_entry *proc_mtd;
  1113. static int __init init_mtd(void)
  1114. {
  1115. int ret;
  1116. ret = class_register(&mtd_class);
  1117. if (ret)
  1118. goto err_reg;
  1119. ret = mtd_bdi_init(&mtd_bdi, "mtd");
  1120. if (ret)
  1121. goto err_bdi;
  1122. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  1123. ret = init_mtdchar();
  1124. if (ret)
  1125. goto out_procfs;
  1126. return 0;
  1127. out_procfs:
  1128. if (proc_mtd)
  1129. remove_proc_entry("mtd", NULL);
  1130. err_bdi:
  1131. class_unregister(&mtd_class);
  1132. err_reg:
  1133. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1134. return ret;
  1135. }
  1136. static void __exit cleanup_mtd(void)
  1137. {
  1138. cleanup_mtdchar();
  1139. if (proc_mtd)
  1140. remove_proc_entry("mtd", NULL);
  1141. class_unregister(&mtd_class);
  1142. bdi_destroy(&mtd_bdi);
  1143. idr_destroy(&mtd_idr);
  1144. }
  1145. module_init(init_mtd);
  1146. module_exit(cleanup_mtd);
  1147. MODULE_LICENSE("GPL");
  1148. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1149. MODULE_DESCRIPTION("Core MTD registration and access routines");