mtdcore.c 31 KB

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