mtdcore.c 32 KB

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