mtdconcat.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * MTD device concatenation layer
  3. *
  4. * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
  5. *
  6. * NAND support by Christian Gan <cgan@iders.ca>
  7. *
  8. * This code is GPL
  9. *
  10. * $Id: mtdconcat.c,v 1.11 2005/11/07 11:14:20 gleixner Exp $
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/types.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/concat.h>
  19. #include <asm/div64.h>
  20. /*
  21. * Our storage structure:
  22. * Subdev points to an array of pointers to struct mtd_info objects
  23. * which is allocated along with this structure
  24. *
  25. */
  26. struct mtd_concat {
  27. struct mtd_info mtd;
  28. int num_subdev;
  29. struct mtd_info **subdev;
  30. };
  31. /*
  32. * how to calculate the size required for the above structure,
  33. * including the pointer array subdev points to:
  34. */
  35. #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
  36. ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
  37. /*
  38. * Given a pointer to the MTD object in the mtd_concat structure,
  39. * we can retrieve the pointer to that structure with this macro.
  40. */
  41. #define CONCAT(x) ((struct mtd_concat *)(x))
  42. /*
  43. * MTD methods which look up the relevant subdevice, translate the
  44. * effective address and pass through to the subdevice.
  45. */
  46. static int
  47. concat_read(struct mtd_info *mtd, loff_t from, size_t len,
  48. size_t * retlen, u_char * buf)
  49. {
  50. struct mtd_concat *concat = CONCAT(mtd);
  51. int err = -EINVAL;
  52. int i;
  53. *retlen = 0;
  54. for (i = 0; i < concat->num_subdev; i++) {
  55. struct mtd_info *subdev = concat->subdev[i];
  56. size_t size, retsize;
  57. if (from >= subdev->size) {
  58. /* Not destined for this subdev */
  59. size = 0;
  60. from -= subdev->size;
  61. continue;
  62. }
  63. if (from + len > subdev->size)
  64. /* First part goes into this subdev */
  65. size = subdev->size - from;
  66. else
  67. /* Entire transaction goes into this subdev */
  68. size = len;
  69. err = subdev->read(subdev, from, size, &retsize, buf);
  70. if (err)
  71. break;
  72. *retlen += retsize;
  73. len -= size;
  74. if (len == 0)
  75. break;
  76. err = -EINVAL;
  77. buf += size;
  78. from = 0;
  79. }
  80. return err;
  81. }
  82. static int
  83. concat_write(struct mtd_info *mtd, loff_t to, size_t len,
  84. size_t * retlen, const u_char * buf)
  85. {
  86. struct mtd_concat *concat = CONCAT(mtd);
  87. int err = -EINVAL;
  88. int i;
  89. if (!(mtd->flags & MTD_WRITEABLE))
  90. return -EROFS;
  91. *retlen = 0;
  92. for (i = 0; i < concat->num_subdev; i++) {
  93. struct mtd_info *subdev = concat->subdev[i];
  94. size_t size, retsize;
  95. if (to >= subdev->size) {
  96. size = 0;
  97. to -= subdev->size;
  98. continue;
  99. }
  100. if (to + len > subdev->size)
  101. size = subdev->size - to;
  102. else
  103. size = len;
  104. if (!(subdev->flags & MTD_WRITEABLE))
  105. err = -EROFS;
  106. else
  107. err = subdev->write(subdev, to, size, &retsize, buf);
  108. if (err)
  109. break;
  110. *retlen += retsize;
  111. len -= size;
  112. if (len == 0)
  113. break;
  114. err = -EINVAL;
  115. buf += size;
  116. to = 0;
  117. }
  118. return err;
  119. }
  120. static int
  121. concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
  122. unsigned long count, loff_t to, size_t * retlen)
  123. {
  124. struct mtd_concat *concat = CONCAT(mtd);
  125. struct kvec *vecs_copy;
  126. unsigned long entry_low, entry_high;
  127. size_t total_len = 0;
  128. int i;
  129. int err = -EINVAL;
  130. if (!(mtd->flags & MTD_WRITEABLE))
  131. return -EROFS;
  132. *retlen = 0;
  133. /* Calculate total length of data */
  134. for (i = 0; i < count; i++)
  135. total_len += vecs[i].iov_len;
  136. /* Do not allow write past end of device */
  137. if ((to + total_len) > mtd->size)
  138. return -EINVAL;
  139. /* Check alignment */
  140. if (mtd->writesize > 1) {
  141. loff_t __to = to;
  142. if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
  143. return -EINVAL;
  144. }
  145. /* make a copy of vecs */
  146. vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL);
  147. if (!vecs_copy)
  148. return -ENOMEM;
  149. memcpy(vecs_copy, vecs, sizeof(struct kvec) * count);
  150. entry_low = 0;
  151. for (i = 0; i < concat->num_subdev; i++) {
  152. struct mtd_info *subdev = concat->subdev[i];
  153. size_t size, wsize, retsize, old_iov_len;
  154. if (to >= subdev->size) {
  155. to -= subdev->size;
  156. continue;
  157. }
  158. size = min(total_len, (size_t)(subdev->size - to));
  159. wsize = size; /* store for future use */
  160. entry_high = entry_low;
  161. while (entry_high < count) {
  162. if (size <= vecs_copy[entry_high].iov_len)
  163. break;
  164. size -= vecs_copy[entry_high++].iov_len;
  165. }
  166. old_iov_len = vecs_copy[entry_high].iov_len;
  167. vecs_copy[entry_high].iov_len = size;
  168. if (!(subdev->flags & MTD_WRITEABLE))
  169. err = -EROFS;
  170. else
  171. err = subdev->writev(subdev, &vecs_copy[entry_low],
  172. entry_high - entry_low + 1, to, &retsize);
  173. vecs_copy[entry_high].iov_len = old_iov_len - size;
  174. vecs_copy[entry_high].iov_base += size;
  175. entry_low = entry_high;
  176. if (err)
  177. break;
  178. *retlen += retsize;
  179. total_len -= wsize;
  180. if (total_len == 0)
  181. break;
  182. err = -EINVAL;
  183. to = 0;
  184. }
  185. kfree(vecs_copy);
  186. return err;
  187. }
  188. static int
  189. concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  190. {
  191. struct mtd_concat *concat = CONCAT(mtd);
  192. struct mtd_oob_ops devops = *ops;
  193. int i, err;
  194. ops->retlen = 0;
  195. for (i = 0; i < concat->num_subdev; i++) {
  196. struct mtd_info *subdev = concat->subdev[i];
  197. if (from >= subdev->size) {
  198. from -= subdev->size;
  199. continue;
  200. }
  201. /* partial read ? */
  202. if (from + devops.len > subdev->size)
  203. devops.len = subdev->size - from;
  204. err = subdev->read_oob(subdev, from, &devops);
  205. ops->retlen += devops.retlen;
  206. if (err)
  207. return err;
  208. devops.len = ops->len - ops->retlen;
  209. if (!devops.len)
  210. return 0;
  211. if (devops.datbuf)
  212. devops.datbuf += devops.retlen;
  213. if (devops.oobbuf)
  214. devops.oobbuf += devops.ooblen;
  215. from = 0;
  216. }
  217. return -EINVAL;
  218. }
  219. static int
  220. concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
  221. {
  222. struct mtd_concat *concat = CONCAT(mtd);
  223. struct mtd_oob_ops devops = *ops;
  224. int i, err;
  225. if (!(mtd->flags & MTD_WRITEABLE))
  226. return -EROFS;
  227. ops->retlen = 0;
  228. for (i = 0; i < concat->num_subdev; i++) {
  229. struct mtd_info *subdev = concat->subdev[i];
  230. if (to >= subdev->size) {
  231. to -= subdev->size;
  232. continue;
  233. }
  234. /* partial write ? */
  235. if (to + devops.len > subdev->size)
  236. devops.len = subdev->size - to;
  237. err = subdev->write_oob(subdev, to, &devops);
  238. ops->retlen += devops.retlen;
  239. if (err)
  240. return err;
  241. devops.len = ops->len - ops->retlen;
  242. if (!devops.len)
  243. return 0;
  244. if (devops.datbuf)
  245. devops.datbuf += devops.retlen;
  246. if (devops.oobbuf)
  247. devops.oobbuf += devops.ooblen;
  248. to = 0;
  249. }
  250. return -EINVAL;
  251. }
  252. static void concat_erase_callback(struct erase_info *instr)
  253. {
  254. wake_up((wait_queue_head_t *) instr->priv);
  255. }
  256. static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
  257. {
  258. int err;
  259. wait_queue_head_t waitq;
  260. DECLARE_WAITQUEUE(wait, current);
  261. /*
  262. * This code was stol^H^H^H^Hinspired by mtdchar.c
  263. */
  264. init_waitqueue_head(&waitq);
  265. erase->mtd = mtd;
  266. erase->callback = concat_erase_callback;
  267. erase->priv = (unsigned long) &waitq;
  268. /*
  269. * FIXME: Allow INTERRUPTIBLE. Which means
  270. * not having the wait_queue head on the stack.
  271. */
  272. err = mtd->erase(mtd, erase);
  273. if (!err) {
  274. set_current_state(TASK_UNINTERRUPTIBLE);
  275. add_wait_queue(&waitq, &wait);
  276. if (erase->state != MTD_ERASE_DONE
  277. && erase->state != MTD_ERASE_FAILED)
  278. schedule();
  279. remove_wait_queue(&waitq, &wait);
  280. set_current_state(TASK_RUNNING);
  281. err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
  282. }
  283. return err;
  284. }
  285. static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
  286. {
  287. struct mtd_concat *concat = CONCAT(mtd);
  288. struct mtd_info *subdev;
  289. int i, err;
  290. u_int32_t length, offset = 0;
  291. struct erase_info *erase;
  292. if (!(mtd->flags & MTD_WRITEABLE))
  293. return -EROFS;
  294. if (instr->addr > concat->mtd.size)
  295. return -EINVAL;
  296. if (instr->len + instr->addr > concat->mtd.size)
  297. return -EINVAL;
  298. /*
  299. * Check for proper erase block alignment of the to-be-erased area.
  300. * It is easier to do this based on the super device's erase
  301. * region info rather than looking at each particular sub-device
  302. * in turn.
  303. */
  304. if (!concat->mtd.numeraseregions) {
  305. /* the easy case: device has uniform erase block size */
  306. if (instr->addr & (concat->mtd.erasesize - 1))
  307. return -EINVAL;
  308. if (instr->len & (concat->mtd.erasesize - 1))
  309. return -EINVAL;
  310. } else {
  311. /* device has variable erase size */
  312. struct mtd_erase_region_info *erase_regions =
  313. concat->mtd.eraseregions;
  314. /*
  315. * Find the erase region where the to-be-erased area begins:
  316. */
  317. for (i = 0; i < concat->mtd.numeraseregions &&
  318. instr->addr >= erase_regions[i].offset; i++) ;
  319. --i;
  320. /*
  321. * Now erase_regions[i] is the region in which the
  322. * to-be-erased area begins. Verify that the starting
  323. * offset is aligned to this region's erase size:
  324. */
  325. if (instr->addr & (erase_regions[i].erasesize - 1))
  326. return -EINVAL;
  327. /*
  328. * now find the erase region where the to-be-erased area ends:
  329. */
  330. for (; i < concat->mtd.numeraseregions &&
  331. (instr->addr + instr->len) >= erase_regions[i].offset;
  332. ++i) ;
  333. --i;
  334. /*
  335. * check if the ending offset is aligned to this region's erase size
  336. */
  337. if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
  338. 1))
  339. return -EINVAL;
  340. }
  341. instr->fail_addr = 0xffffffff;
  342. /* make a local copy of instr to avoid modifying the caller's struct */
  343. erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
  344. if (!erase)
  345. return -ENOMEM;
  346. *erase = *instr;
  347. length = instr->len;
  348. /*
  349. * find the subdevice where the to-be-erased area begins, adjust
  350. * starting offset to be relative to the subdevice start
  351. */
  352. for (i = 0; i < concat->num_subdev; i++) {
  353. subdev = concat->subdev[i];
  354. if (subdev->size <= erase->addr) {
  355. erase->addr -= subdev->size;
  356. offset += subdev->size;
  357. } else {
  358. break;
  359. }
  360. }
  361. /* must never happen since size limit has been verified above */
  362. BUG_ON(i >= concat->num_subdev);
  363. /* now do the erase: */
  364. err = 0;
  365. for (; length > 0; i++) {
  366. /* loop for all subdevices affected by this request */
  367. subdev = concat->subdev[i]; /* get current subdevice */
  368. /* limit length to subdevice's size: */
  369. if (erase->addr + length > subdev->size)
  370. erase->len = subdev->size - erase->addr;
  371. else
  372. erase->len = length;
  373. if (!(subdev->flags & MTD_WRITEABLE)) {
  374. err = -EROFS;
  375. break;
  376. }
  377. length -= erase->len;
  378. if ((err = concat_dev_erase(subdev, erase))) {
  379. /* sanity check: should never happen since
  380. * block alignment has been checked above */
  381. BUG_ON(err == -EINVAL);
  382. if (erase->fail_addr != 0xffffffff)
  383. instr->fail_addr = erase->fail_addr + offset;
  384. break;
  385. }
  386. /*
  387. * erase->addr specifies the offset of the area to be
  388. * erased *within the current subdevice*. It can be
  389. * non-zero only the first time through this loop, i.e.
  390. * for the first subdevice where blocks need to be erased.
  391. * All the following erases must begin at the start of the
  392. * current subdevice, i.e. at offset zero.
  393. */
  394. erase->addr = 0;
  395. offset += subdev->size;
  396. }
  397. instr->state = erase->state;
  398. kfree(erase);
  399. if (err)
  400. return err;
  401. if (instr->callback)
  402. instr->callback(instr);
  403. return 0;
  404. }
  405. static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  406. {
  407. struct mtd_concat *concat = CONCAT(mtd);
  408. int i, err = -EINVAL;
  409. if ((len + ofs) > mtd->size)
  410. return -EINVAL;
  411. for (i = 0; i < concat->num_subdev; i++) {
  412. struct mtd_info *subdev = concat->subdev[i];
  413. size_t size;
  414. if (ofs >= subdev->size) {
  415. size = 0;
  416. ofs -= subdev->size;
  417. continue;
  418. }
  419. if (ofs + len > subdev->size)
  420. size = subdev->size - ofs;
  421. else
  422. size = len;
  423. err = subdev->lock(subdev, ofs, size);
  424. if (err)
  425. break;
  426. len -= size;
  427. if (len == 0)
  428. break;
  429. err = -EINVAL;
  430. ofs = 0;
  431. }
  432. return err;
  433. }
  434. static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  435. {
  436. struct mtd_concat *concat = CONCAT(mtd);
  437. int i, err = 0;
  438. if ((len + ofs) > mtd->size)
  439. return -EINVAL;
  440. for (i = 0; i < concat->num_subdev; i++) {
  441. struct mtd_info *subdev = concat->subdev[i];
  442. size_t size;
  443. if (ofs >= subdev->size) {
  444. size = 0;
  445. ofs -= subdev->size;
  446. continue;
  447. }
  448. if (ofs + len > subdev->size)
  449. size = subdev->size - ofs;
  450. else
  451. size = len;
  452. err = subdev->unlock(subdev, ofs, size);
  453. if (err)
  454. break;
  455. len -= size;
  456. if (len == 0)
  457. break;
  458. err = -EINVAL;
  459. ofs = 0;
  460. }
  461. return err;
  462. }
  463. static void concat_sync(struct mtd_info *mtd)
  464. {
  465. struct mtd_concat *concat = CONCAT(mtd);
  466. int i;
  467. for (i = 0; i < concat->num_subdev; i++) {
  468. struct mtd_info *subdev = concat->subdev[i];
  469. subdev->sync(subdev);
  470. }
  471. }
  472. static int concat_suspend(struct mtd_info *mtd)
  473. {
  474. struct mtd_concat *concat = CONCAT(mtd);
  475. int i, rc = 0;
  476. for (i = 0; i < concat->num_subdev; i++) {
  477. struct mtd_info *subdev = concat->subdev[i];
  478. if ((rc = subdev->suspend(subdev)) < 0)
  479. return rc;
  480. }
  481. return rc;
  482. }
  483. static void concat_resume(struct mtd_info *mtd)
  484. {
  485. struct mtd_concat *concat = CONCAT(mtd);
  486. int i;
  487. for (i = 0; i < concat->num_subdev; i++) {
  488. struct mtd_info *subdev = concat->subdev[i];
  489. subdev->resume(subdev);
  490. }
  491. }
  492. static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
  493. {
  494. struct mtd_concat *concat = CONCAT(mtd);
  495. int i, res = 0;
  496. if (!concat->subdev[0]->block_isbad)
  497. return res;
  498. if (ofs > mtd->size)
  499. return -EINVAL;
  500. for (i = 0; i < concat->num_subdev; i++) {
  501. struct mtd_info *subdev = concat->subdev[i];
  502. if (ofs >= subdev->size) {
  503. ofs -= subdev->size;
  504. continue;
  505. }
  506. res = subdev->block_isbad(subdev, ofs);
  507. break;
  508. }
  509. return res;
  510. }
  511. static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
  512. {
  513. struct mtd_concat *concat = CONCAT(mtd);
  514. int i, err = -EINVAL;
  515. if (!concat->subdev[0]->block_markbad)
  516. return 0;
  517. if (ofs > mtd->size)
  518. return -EINVAL;
  519. for (i = 0; i < concat->num_subdev; i++) {
  520. struct mtd_info *subdev = concat->subdev[i];
  521. if (ofs >= subdev->size) {
  522. ofs -= subdev->size;
  523. continue;
  524. }
  525. err = subdev->block_markbad(subdev, ofs);
  526. break;
  527. }
  528. return err;
  529. }
  530. /*
  531. * This function constructs a virtual MTD device by concatenating
  532. * num_devs MTD devices. A pointer to the new device object is
  533. * stored to *new_dev upon success. This function does _not_
  534. * register any devices: this is the caller's responsibility.
  535. */
  536. struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
  537. int num_devs, /* number of subdevices */
  538. char *name)
  539. { /* name for the new device */
  540. int i;
  541. size_t size;
  542. struct mtd_concat *concat;
  543. u_int32_t max_erasesize, curr_erasesize;
  544. int num_erase_region;
  545. printk(KERN_NOTICE "Concatenating MTD devices:\n");
  546. for (i = 0; i < num_devs; i++)
  547. printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
  548. printk(KERN_NOTICE "into device \"%s\"\n", name);
  549. /* allocate the device structure */
  550. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  551. concat = kmalloc(size, GFP_KERNEL);
  552. if (!concat) {
  553. printk
  554. ("memory allocation error while creating concatenated device \"%s\"\n",
  555. name);
  556. return NULL;
  557. }
  558. memset(concat, 0, size);
  559. concat->subdev = (struct mtd_info **) (concat + 1);
  560. /*
  561. * Set up the new "super" device's MTD object structure, check for
  562. * incompatibilites between the subdevices.
  563. */
  564. concat->mtd.type = subdev[0]->type;
  565. concat->mtd.flags = subdev[0]->flags;
  566. concat->mtd.size = subdev[0]->size;
  567. concat->mtd.erasesize = subdev[0]->erasesize;
  568. concat->mtd.writesize = subdev[0]->writesize;
  569. concat->mtd.oobsize = subdev[0]->oobsize;
  570. concat->mtd.ecctype = subdev[0]->ecctype;
  571. concat->mtd.eccsize = subdev[0]->eccsize;
  572. if (subdev[0]->writev)
  573. concat->mtd.writev = concat_writev;
  574. if (subdev[0]->read_oob)
  575. concat->mtd.read_oob = concat_read_oob;
  576. if (subdev[0]->write_oob)
  577. concat->mtd.write_oob = concat_write_oob;
  578. if (subdev[0]->block_isbad)
  579. concat->mtd.block_isbad = concat_block_isbad;
  580. if (subdev[0]->block_markbad)
  581. concat->mtd.block_markbad = concat_block_markbad;
  582. concat->subdev[0] = subdev[0];
  583. for (i = 1; i < num_devs; i++) {
  584. if (concat->mtd.type != subdev[i]->type) {
  585. kfree(concat);
  586. printk("Incompatible device type on \"%s\"\n",
  587. subdev[i]->name);
  588. return NULL;
  589. }
  590. if (concat->mtd.flags != subdev[i]->flags) {
  591. /*
  592. * Expect all flags except MTD_WRITEABLE to be
  593. * equal on all subdevices.
  594. */
  595. if ((concat->mtd.flags ^ subdev[i]->
  596. flags) & ~MTD_WRITEABLE) {
  597. kfree(concat);
  598. printk("Incompatible device flags on \"%s\"\n",
  599. subdev[i]->name);
  600. return NULL;
  601. } else
  602. /* if writeable attribute differs,
  603. make super device writeable */
  604. concat->mtd.flags |=
  605. subdev[i]->flags & MTD_WRITEABLE;
  606. }
  607. concat->mtd.size += subdev[i]->size;
  608. if (concat->mtd.writesize != subdev[i]->writesize ||
  609. concat->mtd.oobsize != subdev[i]->oobsize ||
  610. concat->mtd.ecctype != subdev[i]->ecctype ||
  611. concat->mtd.eccsize != subdev[i]->eccsize ||
  612. !concat->mtd.read_oob != !subdev[i]->read_oob ||
  613. !concat->mtd.write_oob != !subdev[i]->write_oob) {
  614. kfree(concat);
  615. printk("Incompatible OOB or ECC data on \"%s\"\n",
  616. subdev[i]->name);
  617. return NULL;
  618. }
  619. concat->subdev[i] = subdev[i];
  620. }
  621. concat->mtd.ecclayout = subdev[0]->ecclayout;
  622. concat->num_subdev = num_devs;
  623. concat->mtd.name = name;
  624. concat->mtd.erase = concat_erase;
  625. concat->mtd.read = concat_read;
  626. concat->mtd.write = concat_write;
  627. concat->mtd.sync = concat_sync;
  628. concat->mtd.lock = concat_lock;
  629. concat->mtd.unlock = concat_unlock;
  630. concat->mtd.suspend = concat_suspend;
  631. concat->mtd.resume = concat_resume;
  632. /*
  633. * Combine the erase block size info of the subdevices:
  634. *
  635. * first, walk the map of the new device and see how
  636. * many changes in erase size we have
  637. */
  638. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  639. num_erase_region = 1;
  640. for (i = 0; i < num_devs; i++) {
  641. if (subdev[i]->numeraseregions == 0) {
  642. /* current subdevice has uniform erase size */
  643. if (subdev[i]->erasesize != curr_erasesize) {
  644. /* if it differs from the last subdevice's erase size, count it */
  645. ++num_erase_region;
  646. curr_erasesize = subdev[i]->erasesize;
  647. if (curr_erasesize > max_erasesize)
  648. max_erasesize = curr_erasesize;
  649. }
  650. } else {
  651. /* current subdevice has variable erase size */
  652. int j;
  653. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  654. /* walk the list of erase regions, count any changes */
  655. if (subdev[i]->eraseregions[j].erasesize !=
  656. curr_erasesize) {
  657. ++num_erase_region;
  658. curr_erasesize =
  659. subdev[i]->eraseregions[j].
  660. erasesize;
  661. if (curr_erasesize > max_erasesize)
  662. max_erasesize = curr_erasesize;
  663. }
  664. }
  665. }
  666. }
  667. if (num_erase_region == 1) {
  668. /*
  669. * All subdevices have the same uniform erase size.
  670. * This is easy:
  671. */
  672. concat->mtd.erasesize = curr_erasesize;
  673. concat->mtd.numeraseregions = 0;
  674. } else {
  675. /*
  676. * erase block size varies across the subdevices: allocate
  677. * space to store the data describing the variable erase regions
  678. */
  679. struct mtd_erase_region_info *erase_region_p;
  680. u_int32_t begin, position;
  681. concat->mtd.erasesize = max_erasesize;
  682. concat->mtd.numeraseregions = num_erase_region;
  683. concat->mtd.eraseregions = erase_region_p =
  684. kmalloc(num_erase_region *
  685. sizeof (struct mtd_erase_region_info), GFP_KERNEL);
  686. if (!erase_region_p) {
  687. kfree(concat);
  688. printk
  689. ("memory allocation error while creating erase region list"
  690. " for device \"%s\"\n", name);
  691. return NULL;
  692. }
  693. /*
  694. * walk the map of the new device once more and fill in
  695. * in erase region info:
  696. */
  697. curr_erasesize = subdev[0]->erasesize;
  698. begin = position = 0;
  699. for (i = 0; i < num_devs; i++) {
  700. if (subdev[i]->numeraseregions == 0) {
  701. /* current subdevice has uniform erase size */
  702. if (subdev[i]->erasesize != curr_erasesize) {
  703. /*
  704. * fill in an mtd_erase_region_info structure for the area
  705. * we have walked so far:
  706. */
  707. erase_region_p->offset = begin;
  708. erase_region_p->erasesize =
  709. curr_erasesize;
  710. erase_region_p->numblocks =
  711. (position - begin) / curr_erasesize;
  712. begin = position;
  713. curr_erasesize = subdev[i]->erasesize;
  714. ++erase_region_p;
  715. }
  716. position += subdev[i]->size;
  717. } else {
  718. /* current subdevice has variable erase size */
  719. int j;
  720. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  721. /* walk the list of erase regions, count any changes */
  722. if (subdev[i]->eraseregions[j].
  723. erasesize != curr_erasesize) {
  724. erase_region_p->offset = begin;
  725. erase_region_p->erasesize =
  726. curr_erasesize;
  727. erase_region_p->numblocks =
  728. (position -
  729. begin) / curr_erasesize;
  730. begin = position;
  731. curr_erasesize =
  732. subdev[i]->eraseregions[j].
  733. erasesize;
  734. ++erase_region_p;
  735. }
  736. position +=
  737. subdev[i]->eraseregions[j].
  738. numblocks * curr_erasesize;
  739. }
  740. }
  741. }
  742. /* Now write the final entry */
  743. erase_region_p->offset = begin;
  744. erase_region_p->erasesize = curr_erasesize;
  745. erase_region_p->numblocks = (position - begin) / curr_erasesize;
  746. }
  747. return &concat->mtd;
  748. }
  749. /*
  750. * This function destroys an MTD object obtained from concat_mtd_devs()
  751. */
  752. void mtd_concat_destroy(struct mtd_info *mtd)
  753. {
  754. struct mtd_concat *concat = CONCAT(mtd);
  755. if (concat->mtd.numeraseregions)
  756. kfree(concat->mtd.eraseregions);
  757. kfree(concat);
  758. }
  759. EXPORT_SYMBOL(mtd_concat_create);
  760. EXPORT_SYMBOL(mtd_concat_destroy);
  761. MODULE_LICENSE("GPL");
  762. MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
  763. MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");