mtdcore.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 *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. if (mtd && mtd->suspend)
  106. return mtd_suspend(mtd);
  107. else
  108. return 0;
  109. }
  110. static int mtd_cls_resume(struct device *dev)
  111. {
  112. struct mtd_info *mtd = dev_get_drvdata(dev);
  113. if (mtd && mtd->resume)
  114. mtd_resume(mtd);
  115. return 0;
  116. }
  117. static ssize_t mtd_type_show(struct device *dev,
  118. struct device_attribute *attr, char *buf)
  119. {
  120. struct mtd_info *mtd = dev_get_drvdata(dev);
  121. char *type;
  122. switch (mtd->type) {
  123. case MTD_ABSENT:
  124. type = "absent";
  125. break;
  126. case MTD_RAM:
  127. type = "ram";
  128. break;
  129. case MTD_ROM:
  130. type = "rom";
  131. break;
  132. case MTD_NORFLASH:
  133. type = "nor";
  134. break;
  135. case MTD_NANDFLASH:
  136. type = "nand";
  137. break;
  138. case MTD_DATAFLASH:
  139. type = "dataflash";
  140. break;
  141. case MTD_UBIVOLUME:
  142. type = "ubi";
  143. break;
  144. default:
  145. type = "unknown";
  146. }
  147. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  148. }
  149. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  150. static ssize_t mtd_flags_show(struct device *dev,
  151. struct device_attribute *attr, char *buf)
  152. {
  153. struct mtd_info *mtd = dev_get_drvdata(dev);
  154. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  155. }
  156. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  157. static ssize_t mtd_size_show(struct device *dev,
  158. struct device_attribute *attr, char *buf)
  159. {
  160. struct mtd_info *mtd = dev_get_drvdata(dev);
  161. return snprintf(buf, PAGE_SIZE, "%llu\n",
  162. (unsigned long long)mtd->size);
  163. }
  164. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  165. static ssize_t mtd_erasesize_show(struct device *dev,
  166. struct device_attribute *attr, char *buf)
  167. {
  168. struct mtd_info *mtd = dev_get_drvdata(dev);
  169. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  170. }
  171. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  172. static ssize_t mtd_writesize_show(struct device *dev,
  173. struct device_attribute *attr, char *buf)
  174. {
  175. struct mtd_info *mtd = dev_get_drvdata(dev);
  176. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  177. }
  178. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  179. static ssize_t mtd_subpagesize_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct mtd_info *mtd = dev_get_drvdata(dev);
  183. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  184. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  185. }
  186. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  187. static ssize_t mtd_oobsize_show(struct device *dev,
  188. struct device_attribute *attr, char *buf)
  189. {
  190. struct mtd_info *mtd = dev_get_drvdata(dev);
  191. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  192. }
  193. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  194. static ssize_t mtd_numeraseregions_show(struct device *dev,
  195. struct device_attribute *attr, char *buf)
  196. {
  197. struct mtd_info *mtd = dev_get_drvdata(dev);
  198. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  199. }
  200. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  201. NULL);
  202. static ssize_t mtd_name_show(struct device *dev,
  203. struct device_attribute *attr, char *buf)
  204. {
  205. struct mtd_info *mtd = dev_get_drvdata(dev);
  206. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  207. }
  208. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  209. static struct attribute *mtd_attrs[] = {
  210. &dev_attr_type.attr,
  211. &dev_attr_flags.attr,
  212. &dev_attr_size.attr,
  213. &dev_attr_erasesize.attr,
  214. &dev_attr_writesize.attr,
  215. &dev_attr_subpagesize.attr,
  216. &dev_attr_oobsize.attr,
  217. &dev_attr_numeraseregions.attr,
  218. &dev_attr_name.attr,
  219. NULL,
  220. };
  221. static struct attribute_group mtd_group = {
  222. .attrs = mtd_attrs,
  223. };
  224. static const struct attribute_group *mtd_groups[] = {
  225. &mtd_group,
  226. NULL,
  227. };
  228. static struct device_type mtd_devtype = {
  229. .name = "mtd",
  230. .groups = mtd_groups,
  231. .release = mtd_release,
  232. };
  233. /**
  234. * add_mtd_device - register an MTD device
  235. * @mtd: pointer to new MTD device info structure
  236. *
  237. * Add a device to the list of MTD devices present in the system, and
  238. * notify each currently active MTD 'user' of its arrival. Returns
  239. * zero on success or 1 on failure, which currently will only happen
  240. * if there is insufficient memory or a sysfs error.
  241. */
  242. int add_mtd_device(struct mtd_info *mtd)
  243. {
  244. struct mtd_notifier *not;
  245. int i, error;
  246. if (!mtd->backing_dev_info) {
  247. switch (mtd->type) {
  248. case MTD_RAM:
  249. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  250. break;
  251. case MTD_ROM:
  252. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  253. break;
  254. default:
  255. mtd->backing_dev_info = &mtd_bdi_unmappable;
  256. break;
  257. }
  258. }
  259. BUG_ON(mtd->writesize == 0);
  260. mutex_lock(&mtd_table_mutex);
  261. do {
  262. if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
  263. goto fail_locked;
  264. error = idr_get_new(&mtd_idr, mtd, &i);
  265. } while (error == -EAGAIN);
  266. if (error)
  267. goto fail_locked;
  268. mtd->index = i;
  269. mtd->usecount = 0;
  270. if (is_power_of_2(mtd->erasesize))
  271. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  272. else
  273. mtd->erasesize_shift = 0;
  274. if (is_power_of_2(mtd->writesize))
  275. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  276. else
  277. mtd->writesize_shift = 0;
  278. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  279. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  280. /* Some chips always power up locked. Unlock them now */
  281. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  282. error = mtd_unlock(mtd, 0, mtd->size);
  283. if (error && error != -EOPNOTSUPP)
  284. printk(KERN_WARNING
  285. "%s: unlock failed, writes may not work\n",
  286. mtd->name);
  287. }
  288. /* Caller should have set dev.parent to match the
  289. * physical device.
  290. */
  291. mtd->dev.type = &mtd_devtype;
  292. mtd->dev.class = &mtd_class;
  293. mtd->dev.devt = MTD_DEVT(i);
  294. dev_set_name(&mtd->dev, "mtd%d", i);
  295. dev_set_drvdata(&mtd->dev, mtd);
  296. if (device_register(&mtd->dev) != 0)
  297. goto fail_added;
  298. if (MTD_DEVT(i))
  299. device_create(&mtd_class, mtd->dev.parent,
  300. MTD_DEVT(i) + 1,
  301. NULL, "mtd%dro", i);
  302. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  303. /* No need to get a refcount on the module containing
  304. the notifier, since we hold the mtd_table_mutex */
  305. list_for_each_entry(not, &mtd_notifiers, list)
  306. not->add(mtd);
  307. mutex_unlock(&mtd_table_mutex);
  308. /* We _know_ we aren't being removed, because
  309. our caller is still holding us here. So none
  310. of this try_ nonsense, and no bitching about it
  311. either. :) */
  312. __module_get(THIS_MODULE);
  313. return 0;
  314. fail_added:
  315. idr_remove(&mtd_idr, i);
  316. fail_locked:
  317. mutex_unlock(&mtd_table_mutex);
  318. return 1;
  319. }
  320. /**
  321. * del_mtd_device - unregister an MTD device
  322. * @mtd: pointer to MTD device info structure
  323. *
  324. * Remove a device from the list of MTD devices present in the system,
  325. * and notify each currently active MTD 'user' of its departure.
  326. * Returns zero on success or 1 on failure, which currently will happen
  327. * if the requested device does not appear to be present in the list.
  328. */
  329. int del_mtd_device(struct mtd_info *mtd)
  330. {
  331. int ret;
  332. struct mtd_notifier *not;
  333. mutex_lock(&mtd_table_mutex);
  334. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  335. ret = -ENODEV;
  336. goto out_error;
  337. }
  338. /* No need to get a refcount on the module containing
  339. the notifier, since we hold the mtd_table_mutex */
  340. list_for_each_entry(not, &mtd_notifiers, list)
  341. not->remove(mtd);
  342. if (mtd->usecount) {
  343. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  344. mtd->index, mtd->name, mtd->usecount);
  345. ret = -EBUSY;
  346. } else {
  347. device_unregister(&mtd->dev);
  348. idr_remove(&mtd_idr, mtd->index);
  349. module_put(THIS_MODULE);
  350. ret = 0;
  351. }
  352. out_error:
  353. mutex_unlock(&mtd_table_mutex);
  354. return ret;
  355. }
  356. /**
  357. * mtd_device_parse_register - parse partitions and register an MTD device.
  358. *
  359. * @mtd: the MTD device to register
  360. * @types: the list of MTD partition probes to try, see
  361. * 'parse_mtd_partitions()' for more information
  362. * @parser_data: MTD partition parser-specific data
  363. * @parts: fallback partition information to register, if parsing fails;
  364. * only valid if %nr_parts > %0
  365. * @nr_parts: the number of partitions in parts, if zero then the full
  366. * MTD device is registered if no partition info is found
  367. *
  368. * This function aggregates MTD partitions parsing (done by
  369. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  370. * basically follows the most common pattern found in many MTD drivers:
  371. *
  372. * * It first tries to probe partitions on MTD device @mtd using parsers
  373. * specified in @types (if @types is %NULL, then the default list of parsers
  374. * is used, see 'parse_mtd_partitions()' for more information). If none are
  375. * found this functions tries to fallback to information specified in
  376. * @parts/@nr_parts.
  377. * * If any partitioning info was found, this function registers the found
  378. * partitions.
  379. * * If no partitions were found this function just registers the MTD device
  380. * @mtd and exits.
  381. *
  382. * Returns zero in case of success and a negative error code in case of failure.
  383. */
  384. int mtd_device_parse_register(struct mtd_info *mtd, const char **types,
  385. struct mtd_part_parser_data *parser_data,
  386. const struct mtd_partition *parts,
  387. int nr_parts)
  388. {
  389. int err;
  390. struct mtd_partition *real_parts;
  391. err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
  392. if (err <= 0 && nr_parts && parts) {
  393. real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
  394. GFP_KERNEL);
  395. if (!real_parts)
  396. err = -ENOMEM;
  397. else
  398. err = nr_parts;
  399. }
  400. if (err > 0) {
  401. err = add_mtd_partitions(mtd, real_parts, err);
  402. kfree(real_parts);
  403. } else if (err == 0) {
  404. err = add_mtd_device(mtd);
  405. if (err == 1)
  406. err = -ENODEV;
  407. }
  408. return err;
  409. }
  410. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  411. /**
  412. * mtd_device_unregister - unregister an existing MTD device.
  413. *
  414. * @master: the MTD device to unregister. This will unregister both the master
  415. * and any partitions if registered.
  416. */
  417. int mtd_device_unregister(struct mtd_info *master)
  418. {
  419. int err;
  420. err = del_mtd_partitions(master);
  421. if (err)
  422. return err;
  423. if (!device_is_registered(&master->dev))
  424. return 0;
  425. return del_mtd_device(master);
  426. }
  427. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  428. /**
  429. * register_mtd_user - register a 'user' of MTD devices.
  430. * @new: pointer to notifier info structure
  431. *
  432. * Registers a pair of callbacks function to be called upon addition
  433. * or removal of MTD devices. Causes the 'add' callback to be immediately
  434. * invoked for each MTD device currently present in the system.
  435. */
  436. void register_mtd_user (struct mtd_notifier *new)
  437. {
  438. struct mtd_info *mtd;
  439. mutex_lock(&mtd_table_mutex);
  440. list_add(&new->list, &mtd_notifiers);
  441. __module_get(THIS_MODULE);
  442. mtd_for_each_device(mtd)
  443. new->add(mtd);
  444. mutex_unlock(&mtd_table_mutex);
  445. }
  446. EXPORT_SYMBOL_GPL(register_mtd_user);
  447. /**
  448. * unregister_mtd_user - unregister a 'user' of MTD devices.
  449. * @old: pointer to notifier info structure
  450. *
  451. * Removes a callback function pair from the list of 'users' to be
  452. * notified upon addition or removal of MTD devices. Causes the
  453. * 'remove' callback to be immediately invoked for each MTD device
  454. * currently present in the system.
  455. */
  456. int unregister_mtd_user (struct mtd_notifier *old)
  457. {
  458. struct mtd_info *mtd;
  459. mutex_lock(&mtd_table_mutex);
  460. module_put(THIS_MODULE);
  461. mtd_for_each_device(mtd)
  462. old->remove(mtd);
  463. list_del(&old->list);
  464. mutex_unlock(&mtd_table_mutex);
  465. return 0;
  466. }
  467. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  468. /**
  469. * get_mtd_device - obtain a validated handle for an MTD device
  470. * @mtd: last known address of the required MTD device
  471. * @num: internal device number of the required MTD device
  472. *
  473. * Given a number and NULL address, return the num'th entry in the device
  474. * table, if any. Given an address and num == -1, search the device table
  475. * for a device with that address and return if it's still present. Given
  476. * both, return the num'th driver only if its address matches. Return
  477. * error code if not.
  478. */
  479. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  480. {
  481. struct mtd_info *ret = NULL, *other;
  482. int err = -ENODEV;
  483. mutex_lock(&mtd_table_mutex);
  484. if (num == -1) {
  485. mtd_for_each_device(other) {
  486. if (other == mtd) {
  487. ret = mtd;
  488. break;
  489. }
  490. }
  491. } else if (num >= 0) {
  492. ret = idr_find(&mtd_idr, num);
  493. if (mtd && mtd != ret)
  494. ret = NULL;
  495. }
  496. if (!ret) {
  497. ret = ERR_PTR(err);
  498. goto out;
  499. }
  500. err = __get_mtd_device(ret);
  501. if (err)
  502. ret = ERR_PTR(err);
  503. out:
  504. mutex_unlock(&mtd_table_mutex);
  505. return ret;
  506. }
  507. EXPORT_SYMBOL_GPL(get_mtd_device);
  508. int __get_mtd_device(struct mtd_info *mtd)
  509. {
  510. int err;
  511. if (!try_module_get(mtd->owner))
  512. return -ENODEV;
  513. if (mtd->get_device) {
  514. err = mtd->get_device(mtd);
  515. if (err) {
  516. module_put(mtd->owner);
  517. return err;
  518. }
  519. }
  520. mtd->usecount++;
  521. return 0;
  522. }
  523. EXPORT_SYMBOL_GPL(__get_mtd_device);
  524. /**
  525. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  526. * device name
  527. * @name: MTD device name to open
  528. *
  529. * This function returns MTD device description structure in case of
  530. * success and an error code in case of failure.
  531. */
  532. struct mtd_info *get_mtd_device_nm(const char *name)
  533. {
  534. int err = -ENODEV;
  535. struct mtd_info *mtd = NULL, *other;
  536. mutex_lock(&mtd_table_mutex);
  537. mtd_for_each_device(other) {
  538. if (!strcmp(name, other->name)) {
  539. mtd = other;
  540. break;
  541. }
  542. }
  543. if (!mtd)
  544. goto out_unlock;
  545. err = __get_mtd_device(mtd);
  546. if (err)
  547. goto out_unlock;
  548. mutex_unlock(&mtd_table_mutex);
  549. return mtd;
  550. out_unlock:
  551. mutex_unlock(&mtd_table_mutex);
  552. return ERR_PTR(err);
  553. }
  554. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  555. void put_mtd_device(struct mtd_info *mtd)
  556. {
  557. mutex_lock(&mtd_table_mutex);
  558. __put_mtd_device(mtd);
  559. mutex_unlock(&mtd_table_mutex);
  560. }
  561. EXPORT_SYMBOL_GPL(put_mtd_device);
  562. void __put_mtd_device(struct mtd_info *mtd)
  563. {
  564. --mtd->usecount;
  565. BUG_ON(mtd->usecount < 0);
  566. if (mtd->put_device)
  567. mtd->put_device(mtd);
  568. module_put(mtd->owner);
  569. }
  570. EXPORT_SYMBOL_GPL(__put_mtd_device);
  571. /*
  572. * default_mtd_writev - the default writev method
  573. * @mtd: mtd device description object pointer
  574. * @vecs: the vectors to write
  575. * @count: count of vectors in @vecs
  576. * @to: the MTD device offset to write to
  577. * @retlen: on exit contains the count of bytes written to the MTD device.
  578. *
  579. * This function returns zero in case of success and a negative error code in
  580. * case of failure.
  581. */
  582. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  583. unsigned long count, loff_t to, size_t *retlen)
  584. {
  585. unsigned long i;
  586. size_t totlen = 0, thislen;
  587. int ret = 0;
  588. for (i = 0; i < count; i++) {
  589. if (!vecs[i].iov_len)
  590. continue;
  591. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  592. vecs[i].iov_base);
  593. totlen += thislen;
  594. if (ret || thislen != vecs[i].iov_len)
  595. break;
  596. to += vecs[i].iov_len;
  597. }
  598. *retlen = totlen;
  599. return ret;
  600. }
  601. /*
  602. * mtd_writev - the vector-based MTD write method
  603. * @mtd: mtd device description object pointer
  604. * @vecs: the vectors to write
  605. * @count: count of vectors in @vecs
  606. * @to: the MTD device offset to write to
  607. * @retlen: on exit contains the count of bytes written to the MTD device.
  608. *
  609. * This function returns zero in case of success and a negative error code in
  610. * case of failure.
  611. */
  612. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  613. unsigned long count, loff_t to, size_t *retlen)
  614. {
  615. *retlen = 0;
  616. if (!mtd->writev)
  617. return default_mtd_writev(mtd, vecs, count, to, retlen);
  618. return mtd->writev(mtd, vecs, count, to, retlen);
  619. }
  620. EXPORT_SYMBOL_GPL(mtd_writev);
  621. /**
  622. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  623. * @mtd: mtd device description object pointer
  624. * @size: a pointer to the ideal or maximum size of the allocation, points
  625. * to the actual allocation size on success.
  626. *
  627. * This routine attempts to allocate a contiguous kernel buffer up to
  628. * the specified size, backing off the size of the request exponentially
  629. * until the request succeeds or until the allocation size falls below
  630. * the system page size. This attempts to make sure it does not adversely
  631. * impact system performance, so when allocating more than one page, we
  632. * ask the memory allocator to avoid re-trying, swapping, writing back
  633. * or performing I/O.
  634. *
  635. * Note, this function also makes sure that the allocated buffer is aligned to
  636. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  637. *
  638. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  639. * to handle smaller (i.e. degraded) buffer allocations under low- or
  640. * fragmented-memory situations where such reduced allocations, from a
  641. * requested ideal, are allowed.
  642. *
  643. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  644. */
  645. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  646. {
  647. gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
  648. __GFP_NORETRY | __GFP_NO_KSWAPD;
  649. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  650. void *kbuf;
  651. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  652. while (*size > min_alloc) {
  653. kbuf = kmalloc(*size, flags);
  654. if (kbuf)
  655. return kbuf;
  656. *size >>= 1;
  657. *size = ALIGN(*size, mtd->writesize);
  658. }
  659. /*
  660. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  661. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  662. */
  663. return kmalloc(*size, GFP_KERNEL);
  664. }
  665. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  666. #ifdef CONFIG_PROC_FS
  667. /*====================================================================*/
  668. /* Support for /proc/mtd */
  669. static struct proc_dir_entry *proc_mtd;
  670. static int mtd_proc_show(struct seq_file *m, void *v)
  671. {
  672. struct mtd_info *mtd;
  673. seq_puts(m, "dev: size erasesize name\n");
  674. mutex_lock(&mtd_table_mutex);
  675. mtd_for_each_device(mtd) {
  676. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  677. mtd->index, (unsigned long long)mtd->size,
  678. mtd->erasesize, mtd->name);
  679. }
  680. mutex_unlock(&mtd_table_mutex);
  681. return 0;
  682. }
  683. static int mtd_proc_open(struct inode *inode, struct file *file)
  684. {
  685. return single_open(file, mtd_proc_show, NULL);
  686. }
  687. static const struct file_operations mtd_proc_ops = {
  688. .open = mtd_proc_open,
  689. .read = seq_read,
  690. .llseek = seq_lseek,
  691. .release = single_release,
  692. };
  693. #endif /* CONFIG_PROC_FS */
  694. /*====================================================================*/
  695. /* Init code */
  696. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  697. {
  698. int ret;
  699. ret = bdi_init(bdi);
  700. if (!ret)
  701. ret = bdi_register(bdi, NULL, name);
  702. if (ret)
  703. bdi_destroy(bdi);
  704. return ret;
  705. }
  706. static int __init init_mtd(void)
  707. {
  708. int ret;
  709. ret = class_register(&mtd_class);
  710. if (ret)
  711. goto err_reg;
  712. ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
  713. if (ret)
  714. goto err_bdi1;
  715. ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
  716. if (ret)
  717. goto err_bdi2;
  718. ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
  719. if (ret)
  720. goto err_bdi3;
  721. #ifdef CONFIG_PROC_FS
  722. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  723. #endif /* CONFIG_PROC_FS */
  724. return 0;
  725. err_bdi3:
  726. bdi_destroy(&mtd_bdi_ro_mappable);
  727. err_bdi2:
  728. bdi_destroy(&mtd_bdi_unmappable);
  729. err_bdi1:
  730. class_unregister(&mtd_class);
  731. err_reg:
  732. pr_err("Error registering mtd class or bdi: %d\n", ret);
  733. return ret;
  734. }
  735. static void __exit cleanup_mtd(void)
  736. {
  737. #ifdef CONFIG_PROC_FS
  738. if (proc_mtd)
  739. remove_proc_entry( "mtd", NULL);
  740. #endif /* CONFIG_PROC_FS */
  741. class_unregister(&mtd_class);
  742. bdi_destroy(&mtd_bdi_unmappable);
  743. bdi_destroy(&mtd_bdi_ro_mappable);
  744. bdi_destroy(&mtd_bdi_rw_mappable);
  745. }
  746. module_init(init_mtd);
  747. module_exit(cleanup_mtd);
  748. MODULE_LICENSE("GPL");
  749. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  750. MODULE_DESCRIPTION("Core MTD registration and access routines");