mtdcore.c 31 KB

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