mtdcore.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * Core registration and callback routines for MTD
  3. * drivers and users.
  4. *
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/slab.h>
  10. #include <linux/string.h>
  11. #include <linux/timer.h>
  12. #include <linux/major.h>
  13. #include <linux/fs.h>
  14. #include <linux/err.h>
  15. #include <linux/ioctl.h>
  16. #include <linux/init.h>
  17. #include <linux/mtd/compatmac.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/idr.h>
  20. #include <linux/mtd/mtd.h>
  21. #include "internal.h"
  22. #include "mtdcore.h"
  23. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  24. static int mtd_cls_resume(struct device *dev);
  25. static struct class mtd_class = {
  26. .name = "mtd",
  27. .owner = THIS_MODULE,
  28. .suspend = mtd_cls_suspend,
  29. .resume = mtd_cls_resume,
  30. };
  31. static DEFINE_IDR(mtd_idr);
  32. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  33. should not use them for _anything_ else */
  34. DEFINE_MUTEX(mtd_table_mutex);
  35. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  36. struct mtd_info *__mtd_next_device(int i)
  37. {
  38. return idr_get_next(&mtd_idr, &i);
  39. }
  40. EXPORT_SYMBOL_GPL(__mtd_next_device);
  41. static LIST_HEAD(mtd_notifiers);
  42. #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
  43. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  44. #else
  45. #define MTD_DEVT(index) 0
  46. #endif
  47. /* REVISIT once MTD uses the driver model better, whoever allocates
  48. * the mtd_info will probably want to use the release() hook...
  49. */
  50. static void mtd_release(struct device *dev)
  51. {
  52. dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
  53. /* remove /dev/mtdXro node if needed */
  54. if (index)
  55. device_destroy(&mtd_class, index + 1);
  56. }
  57. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  58. {
  59. struct mtd_info *mtd = dev_to_mtd(dev);
  60. if (mtd && mtd->suspend)
  61. return mtd->suspend(mtd);
  62. else
  63. return 0;
  64. }
  65. static int mtd_cls_resume(struct device *dev)
  66. {
  67. struct mtd_info *mtd = dev_to_mtd(dev);
  68. if (mtd && mtd->resume)
  69. mtd->resume(mtd);
  70. return 0;
  71. }
  72. static ssize_t mtd_type_show(struct device *dev,
  73. struct device_attribute *attr, char *buf)
  74. {
  75. struct mtd_info *mtd = dev_to_mtd(dev);
  76. char *type;
  77. switch (mtd->type) {
  78. case MTD_ABSENT:
  79. type = "absent";
  80. break;
  81. case MTD_RAM:
  82. type = "ram";
  83. break;
  84. case MTD_ROM:
  85. type = "rom";
  86. break;
  87. case MTD_NORFLASH:
  88. type = "nor";
  89. break;
  90. case MTD_NANDFLASH:
  91. type = "nand";
  92. break;
  93. case MTD_DATAFLASH:
  94. type = "dataflash";
  95. break;
  96. case MTD_UBIVOLUME:
  97. type = "ubi";
  98. break;
  99. default:
  100. type = "unknown";
  101. }
  102. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  103. }
  104. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  105. static ssize_t mtd_flags_show(struct device *dev,
  106. struct device_attribute *attr, char *buf)
  107. {
  108. struct mtd_info *mtd = dev_to_mtd(dev);
  109. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  110. }
  111. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  112. static ssize_t mtd_size_show(struct device *dev,
  113. struct device_attribute *attr, char *buf)
  114. {
  115. struct mtd_info *mtd = dev_to_mtd(dev);
  116. return snprintf(buf, PAGE_SIZE, "%llu\n",
  117. (unsigned long long)mtd->size);
  118. }
  119. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  120. static ssize_t mtd_erasesize_show(struct device *dev,
  121. struct device_attribute *attr, char *buf)
  122. {
  123. struct mtd_info *mtd = dev_to_mtd(dev);
  124. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  125. }
  126. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  127. static ssize_t mtd_writesize_show(struct device *dev,
  128. struct device_attribute *attr, char *buf)
  129. {
  130. struct mtd_info *mtd = dev_to_mtd(dev);
  131. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  132. }
  133. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  134. static ssize_t mtd_subpagesize_show(struct device *dev,
  135. struct device_attribute *attr, char *buf)
  136. {
  137. struct mtd_info *mtd = dev_to_mtd(dev);
  138. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  139. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  140. }
  141. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  142. static ssize_t mtd_oobsize_show(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. struct mtd_info *mtd = dev_to_mtd(dev);
  146. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  147. }
  148. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  149. static ssize_t mtd_numeraseregions_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, "%u\n", mtd->numeraseregions);
  154. }
  155. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  156. NULL);
  157. static ssize_t mtd_name_show(struct device *dev,
  158. struct device_attribute *attr, char *buf)
  159. {
  160. struct mtd_info *mtd = dev_to_mtd(dev);
  161. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  162. }
  163. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  164. static struct attribute *mtd_attrs[] = {
  165. &dev_attr_type.attr,
  166. &dev_attr_flags.attr,
  167. &dev_attr_size.attr,
  168. &dev_attr_erasesize.attr,
  169. &dev_attr_writesize.attr,
  170. &dev_attr_subpagesize.attr,
  171. &dev_attr_oobsize.attr,
  172. &dev_attr_numeraseregions.attr,
  173. &dev_attr_name.attr,
  174. NULL,
  175. };
  176. static struct attribute_group mtd_group = {
  177. .attrs = mtd_attrs,
  178. };
  179. static const struct attribute_group *mtd_groups[] = {
  180. &mtd_group,
  181. NULL,
  182. };
  183. static struct device_type mtd_devtype = {
  184. .name = "mtd",
  185. .groups = mtd_groups,
  186. .release = mtd_release,
  187. };
  188. /**
  189. * add_mtd_device - register an MTD device
  190. * @mtd: pointer to new MTD device info structure
  191. *
  192. * Add a device to the list of MTD devices present in the system, and
  193. * notify each currently active MTD 'user' of its arrival. Returns
  194. * zero on success or 1 on failure, which currently will only happen
  195. * if there is insufficient memory or a sysfs error.
  196. */
  197. int add_mtd_device(struct mtd_info *mtd)
  198. {
  199. struct mtd_notifier *not;
  200. int i, error;
  201. if (!mtd->backing_dev_info) {
  202. switch (mtd->type) {
  203. case MTD_RAM:
  204. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  205. break;
  206. case MTD_ROM:
  207. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  208. break;
  209. default:
  210. mtd->backing_dev_info = &mtd_bdi_unmappable;
  211. break;
  212. }
  213. }
  214. BUG_ON(mtd->writesize == 0);
  215. mutex_lock(&mtd_table_mutex);
  216. do {
  217. if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
  218. goto fail_locked;
  219. error = idr_get_new(&mtd_idr, mtd, &i);
  220. } while (error == -EAGAIN);
  221. if (error)
  222. goto fail_locked;
  223. mtd->index = i;
  224. mtd->usecount = 0;
  225. if (is_power_of_2(mtd->erasesize))
  226. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  227. else
  228. mtd->erasesize_shift = 0;
  229. if (is_power_of_2(mtd->writesize))
  230. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  231. else
  232. mtd->writesize_shift = 0;
  233. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  234. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  235. /* Some chips always power up locked. Unlock them now */
  236. if ((mtd->flags & MTD_WRITEABLE)
  237. && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
  238. if (mtd->unlock(mtd, 0, mtd->size))
  239. printk(KERN_WARNING
  240. "%s: unlock failed, writes may not work\n",
  241. mtd->name);
  242. }
  243. /* Caller should have set dev.parent to match the
  244. * physical device.
  245. */
  246. mtd->dev.type = &mtd_devtype;
  247. mtd->dev.class = &mtd_class;
  248. mtd->dev.devt = MTD_DEVT(i);
  249. dev_set_name(&mtd->dev, "mtd%d", i);
  250. dev_set_drvdata(&mtd->dev, mtd);
  251. if (device_register(&mtd->dev) != 0)
  252. goto fail_added;
  253. if (MTD_DEVT(i))
  254. device_create(&mtd_class, mtd->dev.parent,
  255. MTD_DEVT(i) + 1,
  256. NULL, "mtd%dro", i);
  257. DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
  258. /* No need to get a refcount on the module containing
  259. the notifier, since we hold the mtd_table_mutex */
  260. list_for_each_entry(not, &mtd_notifiers, list)
  261. not->add(mtd);
  262. mutex_unlock(&mtd_table_mutex);
  263. /* We _know_ we aren't being removed, because
  264. our caller is still holding us here. So none
  265. of this try_ nonsense, and no bitching about it
  266. either. :) */
  267. __module_get(THIS_MODULE);
  268. return 0;
  269. fail_added:
  270. idr_remove(&mtd_idr, i);
  271. fail_locked:
  272. mutex_unlock(&mtd_table_mutex);
  273. return 1;
  274. }
  275. /**
  276. * del_mtd_device - unregister an MTD device
  277. * @mtd: pointer to MTD device info structure
  278. *
  279. * Remove a device from the list of MTD devices present in the system,
  280. * and notify each currently active MTD 'user' of its departure.
  281. * Returns zero on success or 1 on failure, which currently will happen
  282. * if the requested device does not appear to be present in the list.
  283. */
  284. int del_mtd_device (struct mtd_info *mtd)
  285. {
  286. int ret;
  287. struct mtd_notifier *not;
  288. mutex_lock(&mtd_table_mutex);
  289. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  290. ret = -ENODEV;
  291. goto out_error;
  292. }
  293. /* No need to get a refcount on the module containing
  294. the notifier, since we hold the mtd_table_mutex */
  295. list_for_each_entry(not, &mtd_notifiers, list)
  296. not->remove(mtd);
  297. if (mtd->usecount) {
  298. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  299. mtd->index, mtd->name, mtd->usecount);
  300. ret = -EBUSY;
  301. } else {
  302. device_unregister(&mtd->dev);
  303. idr_remove(&mtd_idr, mtd->index);
  304. module_put(THIS_MODULE);
  305. ret = 0;
  306. }
  307. out_error:
  308. mutex_unlock(&mtd_table_mutex);
  309. return ret;
  310. }
  311. /**
  312. * register_mtd_user - register a 'user' of MTD devices.
  313. * @new: pointer to notifier info structure
  314. *
  315. * Registers a pair of callbacks function to be called upon addition
  316. * or removal of MTD devices. Causes the 'add' callback to be immediately
  317. * invoked for each MTD device currently present in the system.
  318. */
  319. void register_mtd_user (struct mtd_notifier *new)
  320. {
  321. struct mtd_info *mtd;
  322. mutex_lock(&mtd_table_mutex);
  323. list_add(&new->list, &mtd_notifiers);
  324. __module_get(THIS_MODULE);
  325. mtd_for_each_device(mtd)
  326. new->add(mtd);
  327. mutex_unlock(&mtd_table_mutex);
  328. }
  329. /**
  330. * unregister_mtd_user - unregister a 'user' of MTD devices.
  331. * @old: pointer to notifier info structure
  332. *
  333. * Removes a callback function pair from the list of 'users' to be
  334. * notified upon addition or removal of MTD devices. Causes the
  335. * 'remove' callback to be immediately invoked for each MTD device
  336. * currently present in the system.
  337. */
  338. int unregister_mtd_user (struct mtd_notifier *old)
  339. {
  340. struct mtd_info *mtd;
  341. mutex_lock(&mtd_table_mutex);
  342. module_put(THIS_MODULE);
  343. mtd_for_each_device(mtd)
  344. old->remove(mtd);
  345. list_del(&old->list);
  346. mutex_unlock(&mtd_table_mutex);
  347. return 0;
  348. }
  349. /**
  350. * get_mtd_device - obtain a validated handle for an MTD device
  351. * @mtd: last known address of the required MTD device
  352. * @num: internal device number of the required MTD device
  353. *
  354. * Given a number and NULL address, return the num'th entry in the device
  355. * table, if any. Given an address and num == -1, search the device table
  356. * for a device with that address and return if it's still present. Given
  357. * both, return the num'th driver only if its address matches. Return
  358. * error code if not.
  359. */
  360. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  361. {
  362. struct mtd_info *ret = NULL, *other;
  363. int err = -ENODEV;
  364. mutex_lock(&mtd_table_mutex);
  365. if (num == -1) {
  366. mtd_for_each_device(other) {
  367. if (other == mtd) {
  368. ret = mtd;
  369. break;
  370. }
  371. }
  372. } else if (num >= 0) {
  373. ret = idr_find(&mtd_idr, num);
  374. if (mtd && mtd != ret)
  375. ret = NULL;
  376. }
  377. if (!ret) {
  378. ret = ERR_PTR(err);
  379. goto out;
  380. }
  381. err = __get_mtd_device(ret);
  382. if (err)
  383. ret = ERR_PTR(err);
  384. out:
  385. mutex_unlock(&mtd_table_mutex);
  386. return ret;
  387. }
  388. int __get_mtd_device(struct mtd_info *mtd)
  389. {
  390. int err;
  391. if (!try_module_get(mtd->owner))
  392. return -ENODEV;
  393. if (mtd->get_device) {
  394. err = mtd->get_device(mtd);
  395. if (err) {
  396. module_put(mtd->owner);
  397. return err;
  398. }
  399. }
  400. mtd->usecount++;
  401. return 0;
  402. }
  403. /**
  404. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  405. * device name
  406. * @name: MTD device name to open
  407. *
  408. * This function returns MTD device description structure in case of
  409. * success and an error code in case of failure.
  410. */
  411. struct mtd_info *get_mtd_device_nm(const char *name)
  412. {
  413. int err = -ENODEV;
  414. struct mtd_info *mtd = NULL, *other;
  415. mutex_lock(&mtd_table_mutex);
  416. mtd_for_each_device(other) {
  417. if (!strcmp(name, other->name)) {
  418. mtd = other;
  419. break;
  420. }
  421. }
  422. if (!mtd)
  423. goto out_unlock;
  424. if (!try_module_get(mtd->owner))
  425. goto out_unlock;
  426. if (mtd->get_device) {
  427. err = mtd->get_device(mtd);
  428. if (err)
  429. goto out_put;
  430. }
  431. mtd->usecount++;
  432. mutex_unlock(&mtd_table_mutex);
  433. return mtd;
  434. out_put:
  435. module_put(mtd->owner);
  436. out_unlock:
  437. mutex_unlock(&mtd_table_mutex);
  438. return ERR_PTR(err);
  439. }
  440. void put_mtd_device(struct mtd_info *mtd)
  441. {
  442. mutex_lock(&mtd_table_mutex);
  443. __put_mtd_device(mtd);
  444. mutex_unlock(&mtd_table_mutex);
  445. }
  446. void __put_mtd_device(struct mtd_info *mtd)
  447. {
  448. --mtd->usecount;
  449. BUG_ON(mtd->usecount < 0);
  450. if (mtd->put_device)
  451. mtd->put_device(mtd);
  452. module_put(mtd->owner);
  453. }
  454. /* default_mtd_writev - default mtd writev method for MTD devices that
  455. * don't implement their own
  456. */
  457. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  458. unsigned long count, loff_t to, size_t *retlen)
  459. {
  460. unsigned long i;
  461. size_t totlen = 0, thislen;
  462. int ret = 0;
  463. if(!mtd->write) {
  464. ret = -EROFS;
  465. } else {
  466. for (i=0; i<count; i++) {
  467. if (!vecs[i].iov_len)
  468. continue;
  469. ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  470. totlen += thislen;
  471. if (ret || thislen != vecs[i].iov_len)
  472. break;
  473. to += vecs[i].iov_len;
  474. }
  475. }
  476. if (retlen)
  477. *retlen = totlen;
  478. return ret;
  479. }
  480. EXPORT_SYMBOL_GPL(add_mtd_device);
  481. EXPORT_SYMBOL_GPL(del_mtd_device);
  482. EXPORT_SYMBOL_GPL(get_mtd_device);
  483. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  484. EXPORT_SYMBOL_GPL(__get_mtd_device);
  485. EXPORT_SYMBOL_GPL(put_mtd_device);
  486. EXPORT_SYMBOL_GPL(__put_mtd_device);
  487. EXPORT_SYMBOL_GPL(register_mtd_user);
  488. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  489. EXPORT_SYMBOL_GPL(default_mtd_writev);
  490. #ifdef CONFIG_PROC_FS
  491. /*====================================================================*/
  492. /* Support for /proc/mtd */
  493. static struct proc_dir_entry *proc_mtd;
  494. static inline int mtd_proc_info(char *buf, struct mtd_info *this)
  495. {
  496. return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
  497. (unsigned long long)this->size,
  498. this->erasesize, this->name);
  499. }
  500. static int mtd_read_proc (char *page, char **start, off_t off, int count,
  501. int *eof, void *data_unused)
  502. {
  503. struct mtd_info *mtd;
  504. int len, l;
  505. off_t begin = 0;
  506. mutex_lock(&mtd_table_mutex);
  507. len = sprintf(page, "dev: size erasesize name\n");
  508. mtd_for_each_device(mtd) {
  509. l = mtd_proc_info(page + len, mtd);
  510. len += l;
  511. if (len+begin > off+count)
  512. goto done;
  513. if (len+begin < off) {
  514. begin += len;
  515. len = 0;
  516. }
  517. }
  518. *eof = 1;
  519. done:
  520. mutex_unlock(&mtd_table_mutex);
  521. if (off >= len+begin)
  522. return 0;
  523. *start = page + (off-begin);
  524. return ((count < begin+len-off) ? count : begin+len-off);
  525. }
  526. #endif /* CONFIG_PROC_FS */
  527. /*====================================================================*/
  528. /* Init code */
  529. static int __init init_mtd(void)
  530. {
  531. int ret;
  532. ret = class_register(&mtd_class);
  533. if (ret) {
  534. pr_err("Error registering mtd class: %d\n", ret);
  535. return ret;
  536. }
  537. #ifdef CONFIG_PROC_FS
  538. if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
  539. proc_mtd->read_proc = mtd_read_proc;
  540. #endif /* CONFIG_PROC_FS */
  541. return 0;
  542. }
  543. static void __exit cleanup_mtd(void)
  544. {
  545. #ifdef CONFIG_PROC_FS
  546. if (proc_mtd)
  547. remove_proc_entry( "mtd", NULL);
  548. #endif /* CONFIG_PROC_FS */
  549. class_unregister(&mtd_class);
  550. }
  551. module_init(init_mtd);
  552. module_exit(cleanup_mtd);
  553. MODULE_LICENSE("GPL");
  554. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  555. MODULE_DESCRIPTION("Core MTD registration and access routines");