mtdconcat.c 21 KB

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