mtdcore.c 32 KB

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