alauda.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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, 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. struct scatterlist *sg;
  682. int result;
  683. /*
  684. * Since we only read in one block at a time, we have to create
  685. * a bounce buffer and move the data a piece at a time between the
  686. * bounce buffer and the actual transfer buffer.
  687. * We make this buffer big enough to hold temporary redundancy data,
  688. * which we use when reading the data blocks.
  689. */
  690. len = min(sectors, blocksize) * (pagesize + 64);
  691. buffer = kmalloc(len, GFP_NOIO);
  692. if (buffer == NULL) {
  693. printk("alauda_read_data: Out of memory\n");
  694. return USB_STOR_TRANSPORT_ERROR;
  695. }
  696. /* Figure out the initial LBA and page */
  697. lba = address >> blockshift;
  698. page = (address & MEDIA_INFO(us).blockmask);
  699. max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
  700. result = USB_STOR_TRANSPORT_GOOD;
  701. offset = 0;
  702. sg = NULL;
  703. while (sectors > 0) {
  704. unsigned int zone = lba / uzonesize; /* integer division */
  705. unsigned int lba_offset = lba - (zone * uzonesize);
  706. unsigned int pages;
  707. u16 pba;
  708. alauda_ensure_map_for_zone(us, zone);
  709. /* Not overflowing capacity? */
  710. if (lba >= max_lba) {
  711. US_DEBUGP("Error: Requested lba %u exceeds "
  712. "maximum %u\n", lba, max_lba);
  713. result = USB_STOR_TRANSPORT_ERROR;
  714. break;
  715. }
  716. /* Find number of pages we can read in this block */
  717. pages = min(sectors, blocksize - page);
  718. len = pages << pageshift;
  719. /* Find where this lba lives on disk */
  720. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  721. if (pba == UNDEF) { /* this lba was never written */
  722. US_DEBUGP("Read %d zero pages (LBA %d) page %d\n",
  723. pages, lba, page);
  724. /* This is not really an error. It just means
  725. that the block has never been written.
  726. Instead of returning USB_STOR_TRANSPORT_ERROR
  727. it is better to return all zero data. */
  728. memset(buffer, 0, len);
  729. } else {
  730. US_DEBUGP("Read %d pages, from PBA %d"
  731. " (LBA %d) page %d\n",
  732. pages, pba, lba, page);
  733. result = alauda_read_block(us, pba, page, pages, buffer);
  734. if (result != USB_STOR_TRANSPORT_GOOD)
  735. break;
  736. }
  737. /* Store the data in the transfer buffer */
  738. usb_stor_access_xfer_buf(buffer, len, us->srb,
  739. &sg, &offset, TO_XFER_BUF);
  740. page = 0;
  741. lba++;
  742. sectors -= pages;
  743. }
  744. kfree(buffer);
  745. return result;
  746. }
  747. /*
  748. * Write data to a specific sector address
  749. */
  750. static int alauda_write_data(struct us_data *us, unsigned long address,
  751. unsigned int sectors)
  752. {
  753. unsigned char *buffer, *blockbuffer;
  754. unsigned int page, len, offset;
  755. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  756. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  757. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  758. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  759. struct scatterlist *sg;
  760. u16 lba, max_lba;
  761. int result;
  762. /*
  763. * Since we don't write the user data directly to the device,
  764. * we have to create a bounce buffer and move the data a piece
  765. * at a time between the bounce buffer and the actual transfer buffer.
  766. */
  767. len = min(sectors, blocksize) * pagesize;
  768. buffer = kmalloc(len, GFP_NOIO);
  769. if (buffer == NULL) {
  770. printk("alauda_write_data: Out of memory\n");
  771. return USB_STOR_TRANSPORT_ERROR;
  772. }
  773. /*
  774. * We also need a temporary block buffer, where we read in the old data,
  775. * overwrite parts with the new data, and manipulate the redundancy data
  776. */
  777. blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
  778. if (blockbuffer == NULL) {
  779. printk("alauda_write_data: Out of memory\n");
  780. kfree(buffer);
  781. return USB_STOR_TRANSPORT_ERROR;
  782. }
  783. /* Figure out the initial LBA and page */
  784. lba = address >> blockshift;
  785. page = (address & MEDIA_INFO(us).blockmask);
  786. max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
  787. result = USB_STOR_TRANSPORT_GOOD;
  788. offset = 0;
  789. sg = NULL;
  790. while (sectors > 0) {
  791. /* Write as many sectors as possible in this block */
  792. unsigned int pages = min(sectors, blocksize - page);
  793. len = pages << pageshift;
  794. /* Not overflowing capacity? */
  795. if (lba >= max_lba) {
  796. US_DEBUGP("alauda_write_data: Requested lba %u exceeds "
  797. "maximum %u\n", lba, max_lba);
  798. result = USB_STOR_TRANSPORT_ERROR;
  799. break;
  800. }
  801. /* Get the data from the transfer buffer */
  802. usb_stor_access_xfer_buf(buffer, len, us->srb,
  803. &sg, &offset, FROM_XFER_BUF);
  804. result = alauda_write_lba(us, lba, page, pages, buffer,
  805. blockbuffer);
  806. if (result != USB_STOR_TRANSPORT_GOOD)
  807. break;
  808. page = 0;
  809. lba++;
  810. sectors -= pages;
  811. }
  812. kfree(buffer);
  813. kfree(blockbuffer);
  814. return result;
  815. }
  816. /*
  817. * Our interface with the rest of the world
  818. */
  819. static void alauda_info_destructor(void *extra)
  820. {
  821. struct alauda_info *info = (struct alauda_info *) extra;
  822. int port;
  823. if (!info)
  824. return;
  825. for (port = 0; port < 2; port++) {
  826. struct alauda_media_info *media_info = &info->port[port];
  827. alauda_free_maps(media_info);
  828. kfree(media_info->lba_to_pba);
  829. kfree(media_info->pba_to_lba);
  830. }
  831. }
  832. /*
  833. * Initialize alauda_info struct and find the data-write endpoint
  834. */
  835. int init_alauda(struct us_data *us)
  836. {
  837. struct alauda_info *info;
  838. struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
  839. nand_init_ecc();
  840. us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
  841. if (!us->extra) {
  842. US_DEBUGP("init_alauda: Gah! Can't allocate storage for"
  843. "alauda info struct!\n");
  844. return USB_STOR_TRANSPORT_ERROR;
  845. }
  846. info = (struct alauda_info *) us->extra;
  847. us->extra_destructor = alauda_info_destructor;
  848. info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
  849. altsetting->endpoint[0].desc.bEndpointAddress
  850. & USB_ENDPOINT_NUMBER_MASK);
  851. return USB_STOR_TRANSPORT_GOOD;
  852. }
  853. int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
  854. {
  855. int rc;
  856. struct alauda_info *info = (struct alauda_info *) us->extra;
  857. unsigned char *ptr = us->iobuf;
  858. static unsigned char inquiry_response[36] = {
  859. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  860. };
  861. if (srb->cmnd[0] == INQUIRY) {
  862. US_DEBUGP("alauda_transport: INQUIRY. "
  863. "Returning bogus response.\n");
  864. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  865. fill_inquiry_response(us, ptr, 36);
  866. return USB_STOR_TRANSPORT_GOOD;
  867. }
  868. if (srb->cmnd[0] == TEST_UNIT_READY) {
  869. US_DEBUGP("alauda_transport: TEST_UNIT_READY.\n");
  870. return alauda_check_media(us);
  871. }
  872. if (srb->cmnd[0] == READ_CAPACITY) {
  873. unsigned int num_zones;
  874. unsigned long capacity;
  875. rc = alauda_check_media(us);
  876. if (rc != USB_STOR_TRANSPORT_GOOD)
  877. return rc;
  878. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  879. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  880. capacity = num_zones * MEDIA_INFO(us).uzonesize
  881. * MEDIA_INFO(us).blocksize;
  882. /* Report capacity and page size */
  883. ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
  884. ((__be32 *) ptr)[1] = cpu_to_be32(512);
  885. usb_stor_set_xfer_buf(ptr, 8, srb);
  886. return USB_STOR_TRANSPORT_GOOD;
  887. }
  888. if (srb->cmnd[0] == READ_10) {
  889. unsigned int page, pages;
  890. rc = alauda_check_media(us);
  891. if (rc != USB_STOR_TRANSPORT_GOOD)
  892. return rc;
  893. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  894. page <<= 16;
  895. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  896. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  897. US_DEBUGP("alauda_transport: READ_10: page %d pagect %d\n",
  898. page, pages);
  899. return alauda_read_data(us, page, pages);
  900. }
  901. if (srb->cmnd[0] == WRITE_10) {
  902. unsigned int page, pages;
  903. rc = alauda_check_media(us);
  904. if (rc != USB_STOR_TRANSPORT_GOOD)
  905. return rc;
  906. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  907. page <<= 16;
  908. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  909. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  910. US_DEBUGP("alauda_transport: WRITE_10: page %d pagect %d\n",
  911. page, pages);
  912. return alauda_write_data(us, page, pages);
  913. }
  914. if (srb->cmnd[0] == REQUEST_SENSE) {
  915. US_DEBUGP("alauda_transport: REQUEST_SENSE.\n");
  916. memset(ptr, 0, 18);
  917. ptr[0] = 0xF0;
  918. ptr[2] = info->sense_key;
  919. ptr[7] = 11;
  920. ptr[12] = info->sense_asc;
  921. ptr[13] = info->sense_ascq;
  922. usb_stor_set_xfer_buf(ptr, 18, srb);
  923. return USB_STOR_TRANSPORT_GOOD;
  924. }
  925. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  926. /* sure. whatever. not like we can stop the user from popping
  927. the media out of the device (no locking doors, etc) */
  928. return USB_STOR_TRANSPORT_GOOD;
  929. }
  930. US_DEBUGP("alauda_transport: Gah! Unknown command: %d (0x%x)\n",
  931. srb->cmnd[0], srb->cmnd[0]);
  932. info->sense_key = 0x05;
  933. info->sense_asc = 0x20;
  934. info->sense_ascq = 0x00;
  935. return USB_STOR_TRANSPORT_FAILED;
  936. }