mtdcore.c 20 KB

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