mtdcore.c 33 KB

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