mtdcore.c 31 KB

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