alauda.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /*
  2. * Driver for Alauda-based card readers
  3. *
  4. * Current development and maintenance by:
  5. * (c) 2005 Daniel Drake <dsd@gentoo.org>
  6. *
  7. * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
  8. *
  9. * Alauda implements a vendor-specific command set to access two media reader
  10. * ports (XD, SmartMedia). This driver converts SCSI commands to the commands
  11. * which are accepted by these devices.
  12. *
  13. * The driver was developed through reverse-engineering, with the help of the
  14. * sddr09 driver which has many similarities, and with some help from the
  15. * (very old) vendor-supplied GPL sma03 driver.
  16. *
  17. * For protocol info, see http://alauda.sourceforge.net
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2, or (at your option) any
  22. * later version.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <scsi/scsi_device.h>
  36. #include "usb.h"
  37. #include "transport.h"
  38. #include "protocol.h"
  39. #include "debug.h"
  40. #include "alauda.h"
  41. #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
  42. #define LSB_of(s) ((s)&0xFF)
  43. #define MSB_of(s) ((s)>>8)
  44. #define MEDIA_PORT(us) us->srb->device->lun
  45. #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
  46. #define PBA_LO(pba) ((pba & 0xF) << 5)
  47. #define PBA_HI(pba) (pba >> 3)
  48. #define PBA_ZONE(pba) (pba >> 11)
  49. /*
  50. * Media handling
  51. */
  52. struct alauda_card_info {
  53. unsigned char id; /* id byte */
  54. unsigned char chipshift; /* 1<<cs bytes total capacity */
  55. unsigned char pageshift; /* 1<<ps bytes in a page */
  56. unsigned char blockshift; /* 1<<bs pages per block */
  57. unsigned char zoneshift; /* 1<<zs blocks per zone */
  58. };
  59. static struct alauda_card_info alauda_card_ids[] = {
  60. /* NAND flash */
  61. { 0x6e, 20, 8, 4, 8}, /* 1 MB */
  62. { 0xe8, 20, 8, 4, 8}, /* 1 MB */
  63. { 0xec, 20, 8, 4, 8}, /* 1 MB */
  64. { 0x64, 21, 8, 4, 9}, /* 2 MB */
  65. { 0xea, 21, 8, 4, 9}, /* 2 MB */
  66. { 0x6b, 22, 9, 4, 9}, /* 4 MB */
  67. { 0xe3, 22, 9, 4, 9}, /* 4 MB */
  68. { 0xe5, 22, 9, 4, 9}, /* 4 MB */
  69. { 0xe6, 23, 9, 4, 10}, /* 8 MB */
  70. { 0x73, 24, 9, 5, 10}, /* 16 MB */
  71. { 0x75, 25, 9, 5, 10}, /* 32 MB */
  72. { 0x76, 26, 9, 5, 10}, /* 64 MB */
  73. { 0x79, 27, 9, 5, 10}, /* 128 MB */
  74. { 0x71, 28, 9, 5, 10}, /* 256 MB */
  75. /* MASK ROM */
  76. { 0x5d, 21, 9, 4, 8}, /* 2 MB */
  77. { 0xd5, 22, 9, 4, 9}, /* 4 MB */
  78. { 0xd6, 23, 9, 4, 10}, /* 8 MB */
  79. { 0x57, 24, 9, 4, 11}, /* 16 MB */
  80. { 0x58, 25, 9, 4, 12}, /* 32 MB */
  81. { 0,}
  82. };
  83. static struct alauda_card_info *alauda_card_find_id(unsigned char id) {
  84. int i;
  85. for (i = 0; alauda_card_ids[i].id != 0; i++)
  86. if (alauda_card_ids[i].id == id)
  87. return &(alauda_card_ids[i]);
  88. return NULL;
  89. }
  90. /*
  91. * ECC computation.
  92. */
  93. static unsigned char parity[256];
  94. static unsigned char ecc2[256];
  95. static void nand_init_ecc(void) {
  96. int i, j, a;
  97. parity[0] = 0;
  98. for (i = 1; i < 256; i++)
  99. parity[i] = (parity[i&(i-1)] ^ 1);
  100. for (i = 0; i < 256; i++) {
  101. a = 0;
  102. for (j = 0; j < 8; j++) {
  103. if (i & (1<<j)) {
  104. if ((j & 1) == 0)
  105. a ^= 0x04;
  106. if ((j & 2) == 0)
  107. a ^= 0x10;
  108. if ((j & 4) == 0)
  109. a ^= 0x40;
  110. }
  111. }
  112. ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
  113. }
  114. }
  115. /* compute 3-byte ecc on 256 bytes */
  116. static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
  117. int i, j, a;
  118. unsigned char par, bit, bits[8];
  119. par = 0;
  120. for (j = 0; j < 8; j++)
  121. bits[j] = 0;
  122. /* collect 16 checksum bits */
  123. for (i = 0; i < 256; i++) {
  124. par ^= data[i];
  125. bit = parity[data[i]];
  126. for (j = 0; j < 8; j++)
  127. if ((i & (1<<j)) == 0)
  128. bits[j] ^= bit;
  129. }
  130. /* put 4+4+4 = 12 bits in the ecc */
  131. a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
  132. ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  133. a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
  134. ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  135. ecc[2] = ecc2[par];
  136. }
  137. static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
  138. return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
  139. }
  140. static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
  141. memcpy(data, ecc, 3);
  142. }
  143. /*
  144. * Alauda driver
  145. */
  146. /*
  147. * Forget our PBA <---> LBA mappings for a particular port
  148. */
  149. static void alauda_free_maps (struct alauda_media_info *media_info)
  150. {
  151. unsigned int shift = media_info->zoneshift
  152. + media_info->blockshift + media_info->pageshift;
  153. unsigned int num_zones = media_info->capacity >> shift;
  154. unsigned int i;
  155. if (media_info->lba_to_pba != NULL)
  156. for (i = 0; i < num_zones; i++) {
  157. kfree(media_info->lba_to_pba[i]);
  158. media_info->lba_to_pba[i] = NULL;
  159. }
  160. if (media_info->pba_to_lba != NULL)
  161. for (i = 0; i < num_zones; i++) {
  162. kfree(media_info->pba_to_lba[i]);
  163. media_info->pba_to_lba[i] = NULL;
  164. }
  165. }
  166. /*
  167. * Returns 2 bytes of status data
  168. * The first byte describes media status, and second byte describes door status
  169. */
  170. static int alauda_get_media_status(struct us_data *us, unsigned char *data)
  171. {
  172. int rc;
  173. unsigned char command;
  174. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  175. command = ALAUDA_GET_XD_MEDIA_STATUS;
  176. else
  177. command = ALAUDA_GET_SM_MEDIA_STATUS;
  178. rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  179. command, 0xc0, 0, 1, data, 2);
  180. US_DEBUGP("alauda_get_media_status: Media status %02X %02X\n",
  181. data[0], data[1]);
  182. return rc;
  183. }
  184. /*
  185. * Clears the "media was changed" bit so that we know when it changes again
  186. * in the future.
  187. */
  188. static int alauda_ack_media(struct us_data *us)
  189. {
  190. unsigned char command;
  191. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  192. command = ALAUDA_ACK_XD_MEDIA_CHANGE;
  193. else
  194. command = ALAUDA_ACK_SM_MEDIA_CHANGE;
  195. return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  196. command, 0x40, 0, 1, NULL, 0);
  197. }
  198. /*
  199. * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
  200. * and some other details.
  201. */
  202. static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
  203. {
  204. unsigned char command;
  205. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  206. command = ALAUDA_GET_XD_MEDIA_SIG;
  207. else
  208. command = ALAUDA_GET_SM_MEDIA_SIG;
  209. return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  210. command, 0xc0, 0, 0, data, 4);
  211. }
  212. /*
  213. * Resets the media status (but not the whole device?)
  214. */
  215. static int alauda_reset_media(struct us_data *us)
  216. {
  217. unsigned char *command = us->iobuf;
  218. memset(command, 0, 9);
  219. command[0] = ALAUDA_BULK_CMD;
  220. command[1] = ALAUDA_BULK_RESET_MEDIA;
  221. command[8] = MEDIA_PORT(us);
  222. return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  223. command, 9, NULL);
  224. }
  225. /*
  226. * Examines the media and deduces capacity, etc.
  227. */
  228. static int alauda_init_media(struct us_data *us)
  229. {
  230. unsigned char *data = us->iobuf;
  231. int ready = 0;
  232. struct alauda_card_info *media_info;
  233. unsigned int num_zones;
  234. while (ready == 0) {
  235. msleep(20);
  236. if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
  237. return USB_STOR_TRANSPORT_ERROR;
  238. if (data[0] & 0x10)
  239. ready = 1;
  240. }
  241. US_DEBUGP("alauda_init_media: We are ready for action!\n");
  242. if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
  243. return USB_STOR_TRANSPORT_ERROR;
  244. msleep(10);
  245. if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
  246. return USB_STOR_TRANSPORT_ERROR;
  247. if (data[0] != 0x14) {
  248. US_DEBUGP("alauda_init_media: Media not ready after ack\n");
  249. return USB_STOR_TRANSPORT_ERROR;
  250. }
  251. if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
  252. return USB_STOR_TRANSPORT_ERROR;
  253. US_DEBUGP("alauda_init_media: Media signature: %02X %02X %02X %02X\n",
  254. data[0], data[1], data[2], data[3]);
  255. media_info = alauda_card_find_id(data[1]);
  256. if (media_info == NULL) {
  257. printk("alauda_init_media: Unrecognised media signature: "
  258. "%02X %02X %02X %02X\n",
  259. data[0], data[1], data[2], data[3]);
  260. return USB_STOR_TRANSPORT_ERROR;
  261. }
  262. MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
  263. US_DEBUGP("Found media with capacity: %ldMB\n",
  264. MEDIA_INFO(us).capacity >> 20);
  265. MEDIA_INFO(us).pageshift = media_info->pageshift;
  266. MEDIA_INFO(us).blockshift = media_info->blockshift;
  267. MEDIA_INFO(us).zoneshift = media_info->zoneshift;
  268. MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
  269. MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
  270. MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
  271. MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
  272. MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
  273. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  274. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  275. MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
  276. MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
  277. if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
  278. return USB_STOR_TRANSPORT_ERROR;
  279. return USB_STOR_TRANSPORT_GOOD;
  280. }
  281. /*
  282. * Examines the media status and does the right thing when the media has gone,
  283. * appeared, or changed.
  284. */
  285. static int alauda_check_media(struct us_data *us)
  286. {
  287. struct alauda_info *info = (struct alauda_info *) us->extra;
  288. unsigned char status[2];
  289. int rc;
  290. rc = alauda_get_media_status(us, status);
  291. /* Check for no media or door open */
  292. if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
  293. || ((status[1] & 0x01) == 0)) {
  294. US_DEBUGP("alauda_check_media: No media, or door open\n");
  295. alauda_free_maps(&MEDIA_INFO(us));
  296. info->sense_key = 0x02;
  297. info->sense_asc = 0x3A;
  298. info->sense_ascq = 0x00;
  299. return USB_STOR_TRANSPORT_FAILED;
  300. }
  301. /* Check for media change */
  302. if (status[0] & 0x08) {
  303. US_DEBUGP("alauda_check_media: Media change detected\n");
  304. alauda_free_maps(&MEDIA_INFO(us));
  305. alauda_init_media(us);
  306. info->sense_key = UNIT_ATTENTION;
  307. info->sense_asc = 0x28;
  308. info->sense_ascq = 0x00;
  309. return USB_STOR_TRANSPORT_FAILED;
  310. }
  311. return USB_STOR_TRANSPORT_GOOD;
  312. }
  313. /*
  314. * Checks the status from the 2nd status register
  315. * Returns 3 bytes of status data, only the first is known
  316. */
  317. static int alauda_check_status2(struct us_data *us)
  318. {
  319. int rc;
  320. unsigned char command[] = {
  321. ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
  322. 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
  323. };
  324. unsigned char data[3];
  325. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  326. command, 9, NULL);
  327. if (rc != USB_STOR_XFER_GOOD)
  328. return rc;
  329. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  330. data, 3, NULL);
  331. if (rc != USB_STOR_XFER_GOOD)
  332. return rc;
  333. US_DEBUGP("alauda_check_status2: %02X %02X %02X\n", data[0], data[1], data[2]);
  334. if (data[0] & ALAUDA_STATUS_ERROR)
  335. return USB_STOR_XFER_ERROR;
  336. return USB_STOR_XFER_GOOD;
  337. }
  338. /*
  339. * Gets the redundancy data for the first page of a PBA
  340. * Returns 16 bytes.
  341. */
  342. static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
  343. {
  344. int rc;
  345. unsigned char command[] = {
  346. ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
  347. PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
  348. };
  349. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  350. command, 9, NULL);
  351. if (rc != USB_STOR_XFER_GOOD)
  352. return rc;
  353. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  354. data, 16, NULL);
  355. }
  356. /*
  357. * Finds the first unused PBA in a zone
  358. * Returns the absolute PBA of an unused PBA, or 0 if none found.
  359. */
  360. static u16 alauda_find_unused_pba(struct alauda_media_info *info,
  361. unsigned int zone)
  362. {
  363. u16 *pba_to_lba = info->pba_to_lba[zone];
  364. unsigned int i;
  365. for (i = 0; i < info->zonesize; i++)
  366. if (pba_to_lba[i] == UNDEF)
  367. return (zone << info->zoneshift) + i;
  368. return 0;
  369. }
  370. /*
  371. * Reads the redundancy data for all PBA's in a zone
  372. * Produces lba <--> pba mappings
  373. */
  374. static int alauda_read_map(struct us_data *us, unsigned int zone)
  375. {
  376. unsigned char *data = us->iobuf;
  377. int result;
  378. int i, j;
  379. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  380. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  381. unsigned int lba_offset, lba_real, blocknum;
  382. unsigned int zone_base_lba = zone * uzonesize;
  383. unsigned int zone_base_pba = zone * zonesize;
  384. u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
  385. u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
  386. if (lba_to_pba == NULL || pba_to_lba == NULL) {
  387. result = USB_STOR_TRANSPORT_ERROR;
  388. goto error;
  389. }
  390. US_DEBUGP("alauda_read_map: Mapping blocks for zone %d\n", zone);
  391. /* 1024 PBA's per zone */
  392. for (i = 0; i < zonesize; i++)
  393. lba_to_pba[i] = pba_to_lba[i] = UNDEF;
  394. for (i = 0; i < zonesize; i++) {
  395. blocknum = zone_base_pba + i;
  396. result = alauda_get_redu_data(us, blocknum, data);
  397. if (result != USB_STOR_XFER_GOOD) {
  398. result = USB_STOR_TRANSPORT_ERROR;
  399. goto error;
  400. }
  401. /* special PBAs have control field 0^16 */
  402. for (j = 0; j < 16; j++)
  403. if (data[j] != 0)
  404. goto nonz;
  405. pba_to_lba[i] = UNUSABLE;
  406. US_DEBUGP("alauda_read_map: PBA %d has no logical mapping\n", blocknum);
  407. continue;
  408. nonz:
  409. /* unwritten PBAs have control field FF^16 */
  410. for (j = 0; j < 16; j++)
  411. if (data[j] != 0xff)
  412. goto nonff;
  413. continue;
  414. nonff:
  415. /* normal PBAs start with six FFs */
  416. if (j < 6) {
  417. US_DEBUGP("alauda_read_map: PBA %d has no logical mapping: "
  418. "reserved area = %02X%02X%02X%02X "
  419. "data status %02X block status %02X\n",
  420. blocknum, data[0], data[1], data[2], data[3],
  421. data[4], data[5]);
  422. pba_to_lba[i] = UNUSABLE;
  423. continue;
  424. }
  425. if ((data[6] >> 4) != 0x01) {
  426. US_DEBUGP("alauda_read_map: PBA %d has invalid address "
  427. "field %02X%02X/%02X%02X\n",
  428. blocknum, data[6], data[7], data[11], data[12]);
  429. pba_to_lba[i] = UNUSABLE;
  430. continue;
  431. }
  432. /* check even parity */
  433. if (parity[data[6] ^ data[7]]) {
  434. printk("alauda_read_map: Bad parity in LBA for block %d"
  435. " (%02X %02X)\n", i, data[6], data[7]);
  436. pba_to_lba[i] = UNUSABLE;
  437. continue;
  438. }
  439. lba_offset = short_pack(data[7], data[6]);
  440. lba_offset = (lba_offset & 0x07FF) >> 1;
  441. lba_real = lba_offset + zone_base_lba;
  442. /*
  443. * Every 1024 physical blocks ("zone"), the LBA numbers
  444. * go back to zero, but are within a higher block of LBA's.
  445. * Also, there is a maximum of 1000 LBA's per zone.
  446. * In other words, in PBA 1024-2047 you will find LBA 0-999
  447. * which are really LBA 1000-1999. This allows for 24 bad
  448. * or special physical blocks per zone.
  449. */
  450. if (lba_offset >= uzonesize) {
  451. printk("alauda_read_map: Bad low LBA %d for block %d\n",
  452. lba_real, blocknum);
  453. continue;
  454. }
  455. if (lba_to_pba[lba_offset] != UNDEF) {
  456. printk("alauda_read_map: LBA %d seen for PBA %d and %d\n",
  457. lba_real, lba_to_pba[lba_offset], blocknum);
  458. continue;
  459. }
  460. pba_to_lba[i] = lba_real;
  461. lba_to_pba[lba_offset] = blocknum;
  462. continue;
  463. }
  464. MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
  465. MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
  466. result = 0;
  467. goto out;
  468. error:
  469. kfree(lba_to_pba);
  470. kfree(pba_to_lba);
  471. out:
  472. return result;
  473. }
  474. /*
  475. * Checks to see whether we have already mapped a certain zone
  476. * If we haven't, the map is generated
  477. */
  478. static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
  479. {
  480. if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
  481. || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
  482. alauda_read_map(us, zone);
  483. }
  484. /*
  485. * Erases an entire block
  486. */
  487. static int alauda_erase_block(struct us_data *us, u16 pba)
  488. {
  489. int rc;
  490. unsigned char command[] = {
  491. ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
  492. PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
  493. };
  494. unsigned char buf[2];
  495. US_DEBUGP("alauda_erase_block: Erasing PBA %d\n", pba);
  496. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  497. command, 9, NULL);
  498. if (rc != USB_STOR_XFER_GOOD)
  499. return rc;
  500. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  501. buf, 2, NULL);
  502. if (rc != USB_STOR_XFER_GOOD)
  503. return rc;
  504. US_DEBUGP("alauda_erase_block: Erase result: %02X %02X\n",
  505. buf[0], buf[1]);
  506. return rc;
  507. }
  508. /*
  509. * Reads data from a certain offset page inside a PBA, including interleaved
  510. * redundancy data. Returns (pagesize+64)*pages bytes in data.
  511. */
  512. static int alauda_read_block_raw(struct us_data *us, u16 pba,
  513. unsigned int page, unsigned int pages, unsigned char *data)
  514. {
  515. int rc;
  516. unsigned char command[] = {
  517. ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
  518. PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
  519. };
  520. US_DEBUGP("alauda_read_block: pba %d page %d count %d\n",
  521. pba, page, pages);
  522. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  523. command, 9, NULL);
  524. if (rc != USB_STOR_XFER_GOOD)
  525. return rc;
  526. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  527. data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
  528. }
  529. /*
  530. * Reads data from a certain offset page inside a PBA, excluding redundancy
  531. * data. Returns pagesize*pages bytes in data. Note that data must be big enough
  532. * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
  533. * trailing bytes outside this function.
  534. */
  535. static int alauda_read_block(struct us_data *us, u16 pba,
  536. unsigned int page, unsigned int pages, unsigned char *data)
  537. {
  538. int i, rc;
  539. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  540. rc = alauda_read_block_raw(us, pba, page, pages, data);
  541. if (rc != USB_STOR_XFER_GOOD)
  542. return rc;
  543. /* Cut out the redundancy data */
  544. for (i = 0; i < pages; i++) {
  545. int dest_offset = i * pagesize;
  546. int src_offset = i * (pagesize + 64);
  547. memmove(data + dest_offset, data + src_offset, pagesize);
  548. }
  549. return rc;
  550. }
  551. /*
  552. * Writes an entire block of data and checks status after write.
  553. * Redundancy data must be already included in data. Data should be
  554. * (pagesize+64)*blocksize bytes in length.
  555. */
  556. static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
  557. {
  558. int rc;
  559. struct alauda_info *info = (struct alauda_info *) us->extra;
  560. unsigned char command[] = {
  561. ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
  562. PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
  563. };
  564. US_DEBUGP("alauda_write_block: pba %d\n", pba);
  565. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  566. command, 9, NULL);
  567. if (rc != USB_STOR_XFER_GOOD)
  568. return rc;
  569. rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
  570. (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
  571. NULL);
  572. if (rc != USB_STOR_XFER_GOOD)
  573. return rc;
  574. return alauda_check_status2(us);
  575. }
  576. /*
  577. * Write some data to a specific LBA.
  578. */
  579. static int alauda_write_lba(struct us_data *us, u16 lba,
  580. unsigned int page, unsigned int pages,
  581. unsigned char *ptr, unsigned char *blockbuffer)
  582. {
  583. u16 pba, lbap, new_pba;
  584. unsigned char *bptr, *cptr, *xptr;
  585. unsigned char ecc[3];
  586. int i, result;
  587. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  588. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  589. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  590. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  591. unsigned int lba_offset = lba % uzonesize;
  592. unsigned int new_pba_offset;
  593. unsigned int zone = lba / uzonesize;
  594. alauda_ensure_map_for_zone(us, zone);
  595. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  596. if (pba == 1) {
  597. /* Maybe it is impossible to write to PBA 1.
  598. Fake success, but don't do anything. */
  599. printk("alauda_write_lba: avoid writing to pba 1\n");
  600. return USB_STOR_TRANSPORT_GOOD;
  601. }
  602. new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
  603. if (!new_pba) {
  604. printk("alauda_write_lba: Out of unused blocks\n");
  605. return USB_STOR_TRANSPORT_ERROR;
  606. }
  607. /* read old contents */
  608. if (pba != UNDEF) {
  609. result = alauda_read_block_raw(us, pba, 0,
  610. blocksize, blockbuffer);
  611. if (result != USB_STOR_XFER_GOOD)
  612. return result;
  613. } else {
  614. memset(blockbuffer, 0, blocksize * (pagesize + 64));
  615. }
  616. lbap = (lba_offset << 1) | 0x1000;
  617. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  618. lbap ^= 1;
  619. /* check old contents and fill lba */
  620. for (i = 0; i < blocksize; i++) {
  621. bptr = blockbuffer + (i * (pagesize + 64));
  622. cptr = bptr + pagesize;
  623. nand_compute_ecc(bptr, ecc);
  624. if (!nand_compare_ecc(cptr+13, ecc)) {
  625. US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n",
  626. i, pba);
  627. nand_store_ecc(cptr+13, ecc);
  628. }
  629. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  630. if (!nand_compare_ecc(cptr+8, ecc)) {
  631. US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n",
  632. i, pba);
  633. nand_store_ecc(cptr+8, ecc);
  634. }
  635. cptr[6] = cptr[11] = MSB_of(lbap);
  636. cptr[7] = cptr[12] = LSB_of(lbap);
  637. }
  638. /* copy in new stuff and compute ECC */
  639. xptr = ptr;
  640. for (i = page; i < page+pages; i++) {
  641. bptr = blockbuffer + (i * (pagesize + 64));
  642. cptr = bptr + pagesize;
  643. memcpy(bptr, xptr, pagesize);
  644. xptr += pagesize;
  645. nand_compute_ecc(bptr, ecc);
  646. nand_store_ecc(cptr+13, ecc);
  647. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  648. nand_store_ecc(cptr+8, ecc);
  649. }
  650. result = alauda_write_block(us, new_pba, blockbuffer);
  651. if (result != USB_STOR_XFER_GOOD)
  652. return result;
  653. new_pba_offset = new_pba - (zone * zonesize);
  654. MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
  655. MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
  656. US_DEBUGP("alauda_write_lba: Remapped LBA %d to PBA %d\n",
  657. lba, new_pba);
  658. if (pba != UNDEF) {
  659. unsigned int pba_offset = pba - (zone * zonesize);
  660. result = alauda_erase_block(us, pba);
  661. if (result != USB_STOR_XFER_GOOD)
  662. return result;
  663. MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
  664. }
  665. return USB_STOR_TRANSPORT_GOOD;
  666. }
  667. /*
  668. * Read data from a specific sector address
  669. */
  670. static int alauda_read_data(struct us_data *us, unsigned long address,
  671. unsigned int sectors)
  672. {
  673. unsigned char *buffer;
  674. u16 lba, max_lba;
  675. unsigned int page, len, index, offset;
  676. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  677. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  678. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  679. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  680. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  681. int result;
  682. /*
  683. * Since we only read in one block at a time, we have to create
  684. * a bounce buffer and move the data a piece at a time between the
  685. * bounce buffer and the actual transfer buffer.
  686. * We make this buffer big enough to hold temporary redundancy data,
  687. * which we use when reading the data blocks.
  688. */
  689. len = min(sectors, blocksize) * (pagesize + 64);
  690. buffer = kmalloc(len, GFP_NOIO);
  691. if (buffer == NULL) {
  692. printk("alauda_read_data: Out of memory\n");
  693. return USB_STOR_TRANSPORT_ERROR;
  694. }
  695. /* Figure out the initial LBA and page */
  696. lba = address >> blockshift;
  697. page = (address & MEDIA_INFO(us).blockmask);
  698. max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
  699. result = USB_STOR_TRANSPORT_GOOD;
  700. index = offset = 0;
  701. while (sectors > 0) {
  702. unsigned int zone = lba / uzonesize; /* integer division */
  703. unsigned int lba_offset = lba - (zone * uzonesize);
  704. unsigned int pages;
  705. u16 pba;
  706. alauda_ensure_map_for_zone(us, zone);
  707. /* Not overflowing capacity? */
  708. if (lba >= max_lba) {
  709. US_DEBUGP("Error: Requested lba %u exceeds "
  710. "maximum %u\n", lba, max_lba);
  711. result = USB_STOR_TRANSPORT_ERROR;
  712. break;
  713. }
  714. /* Find number of pages we can read in this block */
  715. pages = min(sectors, blocksize - page);
  716. len = pages << pageshift;
  717. /* Find where this lba lives on disk */
  718. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  719. if (pba == UNDEF) { /* this lba was never written */
  720. US_DEBUGP("Read %d zero pages (LBA %d) page %d\n",
  721. pages, lba, page);
  722. /* This is not really an error. It just means
  723. that the block has never been written.
  724. Instead of returning USB_STOR_TRANSPORT_ERROR
  725. it is better to return all zero data. */
  726. memset(buffer, 0, len);
  727. } else {
  728. US_DEBUGP("Read %d pages, from PBA %d"
  729. " (LBA %d) page %d\n",
  730. pages, pba, lba, page);
  731. result = alauda_read_block(us, pba, page, pages, buffer);
  732. if (result != USB_STOR_TRANSPORT_GOOD)
  733. break;
  734. }
  735. /* Store the data in the transfer buffer */
  736. usb_stor_access_xfer_buf(buffer, len, us->srb,
  737. &index, &offset, TO_XFER_BUF);
  738. page = 0;
  739. lba++;
  740. sectors -= pages;
  741. }
  742. kfree(buffer);
  743. return result;
  744. }
  745. /*
  746. * Write data to a specific sector address
  747. */
  748. static int alauda_write_data(struct us_data *us, unsigned long address,
  749. unsigned int sectors)
  750. {
  751. unsigned char *buffer, *blockbuffer;
  752. unsigned int page, len, index, offset;
  753. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  754. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  755. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  756. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  757. u16 lba, max_lba;
  758. int result;
  759. /*
  760. * Since we don't write the user data directly to the device,
  761. * we have to create a bounce buffer and move the data a piece
  762. * at a time between the bounce buffer and the actual transfer buffer.
  763. */
  764. len = min(sectors, blocksize) * pagesize;
  765. buffer = kmalloc(len, GFP_NOIO);
  766. if (buffer == NULL) {
  767. printk("alauda_write_data: Out of memory\n");
  768. return USB_STOR_TRANSPORT_ERROR;
  769. }
  770. /*
  771. * We also need a temporary block buffer, where we read in the old data,
  772. * overwrite parts with the new data, and manipulate the redundancy data
  773. */
  774. blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
  775. if (blockbuffer == NULL) {
  776. printk("alauda_write_data: Out of memory\n");
  777. kfree(buffer);
  778. return USB_STOR_TRANSPORT_ERROR;
  779. }
  780. /* Figure out the initial LBA and page */
  781. lba = address >> blockshift;
  782. page = (address & MEDIA_INFO(us).blockmask);
  783. max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
  784. result = USB_STOR_TRANSPORT_GOOD;
  785. index = offset = 0;
  786. while (sectors > 0) {
  787. /* Write as many sectors as possible in this block */
  788. unsigned int pages = min(sectors, blocksize - page);
  789. len = pages << pageshift;
  790. /* Not overflowing capacity? */
  791. if (lba >= max_lba) {
  792. US_DEBUGP("alauda_write_data: Requested lba %u exceeds "
  793. "maximum %u\n", lba, max_lba);
  794. result = USB_STOR_TRANSPORT_ERROR;
  795. break;
  796. }
  797. /* Get the data from the transfer buffer */
  798. usb_stor_access_xfer_buf(buffer, len, us->srb,
  799. &index, &offset, FROM_XFER_BUF);
  800. result = alauda_write_lba(us, lba, page, pages, buffer,
  801. blockbuffer);
  802. if (result != USB_STOR_TRANSPORT_GOOD)
  803. break;
  804. page = 0;
  805. lba++;
  806. sectors -= pages;
  807. }
  808. kfree(buffer);
  809. kfree(blockbuffer);
  810. return result;
  811. }
  812. /*
  813. * Our interface with the rest of the world
  814. */
  815. static void alauda_info_destructor(void *extra)
  816. {
  817. struct alauda_info *info = (struct alauda_info *) extra;
  818. int port;
  819. if (!info)
  820. return;
  821. for (port = 0; port < 2; port++) {
  822. struct alauda_media_info *media_info = &info->port[port];
  823. alauda_free_maps(media_info);
  824. kfree(media_info->lba_to_pba);
  825. kfree(media_info->pba_to_lba);
  826. }
  827. }
  828. /*
  829. * Initialize alauda_info struct and find the data-write endpoint
  830. */
  831. int init_alauda(struct us_data *us)
  832. {
  833. struct alauda_info *info;
  834. struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
  835. nand_init_ecc();
  836. us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
  837. if (!us->extra) {
  838. US_DEBUGP("init_alauda: Gah! Can't allocate storage for"
  839. "alauda info struct!\n");
  840. return USB_STOR_TRANSPORT_ERROR;
  841. }
  842. info = (struct alauda_info *) us->extra;
  843. us->extra_destructor = alauda_info_destructor;
  844. info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
  845. altsetting->endpoint[0].desc.bEndpointAddress
  846. & USB_ENDPOINT_NUMBER_MASK);
  847. return USB_STOR_TRANSPORT_GOOD;
  848. }
  849. int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
  850. {
  851. int rc;
  852. struct alauda_info *info = (struct alauda_info *) us->extra;
  853. unsigned char *ptr = us->iobuf;
  854. static unsigned char inquiry_response[36] = {
  855. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  856. };
  857. if (srb->cmnd[0] == INQUIRY) {
  858. US_DEBUGP("alauda_transport: INQUIRY. "
  859. "Returning bogus response.\n");
  860. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  861. fill_inquiry_response(us, ptr, 36);
  862. return USB_STOR_TRANSPORT_GOOD;
  863. }
  864. if (srb->cmnd[0] == TEST_UNIT_READY) {
  865. US_DEBUGP("alauda_transport: TEST_UNIT_READY.\n");
  866. return alauda_check_media(us);
  867. }
  868. if (srb->cmnd[0] == READ_CAPACITY) {
  869. unsigned int num_zones;
  870. unsigned long capacity;
  871. rc = alauda_check_media(us);
  872. if (rc != USB_STOR_TRANSPORT_GOOD)
  873. return rc;
  874. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  875. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  876. capacity = num_zones * MEDIA_INFO(us).uzonesize
  877. * MEDIA_INFO(us).blocksize;
  878. /* Report capacity and page size */
  879. ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
  880. ((__be32 *) ptr)[1] = cpu_to_be32(512);
  881. usb_stor_set_xfer_buf(ptr, 8, srb);
  882. return USB_STOR_TRANSPORT_GOOD;
  883. }
  884. if (srb->cmnd[0] == READ_10) {
  885. unsigned int page, pages;
  886. rc = alauda_check_media(us);
  887. if (rc != USB_STOR_TRANSPORT_GOOD)
  888. return rc;
  889. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  890. page <<= 16;
  891. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  892. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  893. US_DEBUGP("alauda_transport: READ_10: page %d pagect %d\n",
  894. page, pages);
  895. return alauda_read_data(us, page, pages);
  896. }
  897. if (srb->cmnd[0] == WRITE_10) {
  898. unsigned int page, pages;
  899. rc = alauda_check_media(us);
  900. if (rc != USB_STOR_TRANSPORT_GOOD)
  901. return rc;
  902. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  903. page <<= 16;
  904. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  905. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  906. US_DEBUGP("alauda_transport: WRITE_10: page %d pagect %d\n",
  907. page, pages);
  908. return alauda_write_data(us, page, pages);
  909. }
  910. if (srb->cmnd[0] == REQUEST_SENSE) {
  911. US_DEBUGP("alauda_transport: REQUEST_SENSE.\n");
  912. memset(ptr, 0, 18);
  913. ptr[0] = 0xF0;
  914. ptr[2] = info->sense_key;
  915. ptr[7] = 11;
  916. ptr[12] = info->sense_asc;
  917. ptr[13] = info->sense_ascq;
  918. usb_stor_set_xfer_buf(ptr, 18, srb);
  919. return USB_STOR_TRANSPORT_GOOD;
  920. }
  921. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  922. /* sure. whatever. not like we can stop the user from popping
  923. the media out of the device (no locking doors, etc) */
  924. return USB_STOR_TRANSPORT_GOOD;
  925. }
  926. US_DEBUGP("alauda_transport: Gah! Unknown command: %d (0x%x)\n",
  927. srb->cmnd[0], srb->cmnd[0]);
  928. info->sense_key = 0x05;
  929. info->sense_asc = 0x20;
  930. info->sense_ascq = 0x00;
  931. return USB_STOR_TRANSPORT_FAILED;
  932. }