mtdcore.c 19 KB

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