2 * Driver for Alauda-based card readers
4 * Current development and maintenance by:
5 * (c) 2005 Daniel Drake <dsd@gentoo.org>
7 * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
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.
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.
17 * For protocol info, see http://alauda.sourceforge.net
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
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.
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.
34 #include <scsi/scsi.h>
35 #include <scsi/scsi_cmnd.h>
36 #include <scsi/scsi_device.h>
39 #include "transport.h"
44 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
45 #define LSB_of(s) ((s)&0xFF)
46 #define MSB_of(s) ((s)>>8)
48 #define MEDIA_PORT(us) us->srb->device->lun
49 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
51 #define PBA_LO(pba) ((pba & 0xF) << 5)
52 #define PBA_HI(pba) (pba >> 3)
53 #define PBA_ZONE(pba) (pba >> 11)
59 struct alauda_card_info {
60 unsigned char id; /* id byte */
61 unsigned char chipshift; /* 1<<cs bytes total capacity */
62 unsigned char pageshift; /* 1<<ps bytes in a page */
63 unsigned char blockshift; /* 1<<bs pages per block */
64 unsigned char zoneshift; /* 1<<zs blocks per zone */
67 static struct alauda_card_info alauda_card_ids[] = {
69 { 0x6e, 20, 8, 4, 8}, /* 1 MB */
70 { 0xe8, 20, 8, 4, 8}, /* 1 MB */
71 { 0xec, 20, 8, 4, 8}, /* 1 MB */
72 { 0x64, 21, 8, 4, 9}, /* 2 MB */
73 { 0xea, 21, 8, 4, 9}, /* 2 MB */
74 { 0x6b, 22, 9, 4, 9}, /* 4 MB */
75 { 0xe3, 22, 9, 4, 9}, /* 4 MB */
76 { 0xe5, 22, 9, 4, 9}, /* 4 MB */
77 { 0xe6, 23, 9, 4, 10}, /* 8 MB */
78 { 0x73, 24, 9, 5, 10}, /* 16 MB */
79 { 0x75, 25, 9, 5, 10}, /* 32 MB */
80 { 0x76, 26, 9, 5, 10}, /* 64 MB */
81 { 0x79, 27, 9, 5, 10}, /* 128 MB */
82 { 0x71, 28, 9, 5, 10}, /* 256 MB */
85 { 0x5d, 21, 9, 4, 8}, /* 2 MB */
86 { 0xd5, 22, 9, 4, 9}, /* 4 MB */
87 { 0xd6, 23, 9, 4, 10}, /* 8 MB */
88 { 0x57, 24, 9, 4, 11}, /* 16 MB */
89 { 0x58, 25, 9, 4, 12}, /* 32 MB */
93 static struct alauda_card_info *alauda_card_find_id(unsigned char id) {
96 for (i = 0; alauda_card_ids[i].id != 0; i++)
97 if (alauda_card_ids[i].id == id)
98 return &(alauda_card_ids[i]);
106 static unsigned char parity[256];
107 static unsigned char ecc2[256];
109 static void nand_init_ecc(void) {
113 for (i = 1; i < 256; i++)
114 parity[i] = (parity[i&(i-1)] ^ 1);
116 for (i = 0; i < 256; i++) {
118 for (j = 0; j < 8; j++) {
128 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
132 /* compute 3-byte ecc on 256 bytes */
133 static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
135 unsigned char par, bit, bits[8];
138 for (j = 0; j < 8; j++)
141 /* collect 16 checksum bits */
142 for (i = 0; i < 256; i++) {
144 bit = parity[data[i]];
145 for (j = 0; j < 8; j++)
146 if ((i & (1<<j)) == 0)
150 /* put 4+4+4 = 12 bits in the ecc */
151 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
152 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
154 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
155 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
160 static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
161 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
164 static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
165 memcpy(data, ecc, 3);
173 * Forget our PBA <---> LBA mappings for a particular port
175 static void alauda_free_maps (struct alauda_media_info *media_info)
177 unsigned int shift = media_info->zoneshift
178 + media_info->blockshift + media_info->pageshift;
179 unsigned int num_zones = media_info->capacity >> shift;
182 if (media_info->lba_to_pba != NULL)
183 for (i = 0; i < num_zones; i++) {
184 kfree(media_info->lba_to_pba[i]);
185 media_info->lba_to_pba[i] = NULL;
188 if (media_info->pba_to_lba != NULL)
189 for (i = 0; i < num_zones; i++) {
190 kfree(media_info->pba_to_lba[i]);
191 media_info->pba_to_lba[i] = NULL;
196 * Returns 2 bytes of status data
197 * The first byte describes media status, and second byte describes door status
199 static int alauda_get_media_status(struct us_data *us, unsigned char *data)
202 unsigned char command;
204 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
205 command = ALAUDA_GET_XD_MEDIA_STATUS;
207 command = ALAUDA_GET_SM_MEDIA_STATUS;
209 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
210 command, 0xc0, 0, 1, data, 2);
212 US_DEBUGP("alauda_get_media_status: Media status %02X %02X\n",
219 * Clears the "media was changed" bit so that we know when it changes again
222 static int alauda_ack_media(struct us_data *us)
224 unsigned char command;
226 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
227 command = ALAUDA_ACK_XD_MEDIA_CHANGE;
229 command = ALAUDA_ACK_SM_MEDIA_CHANGE;
231 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
232 command, 0x40, 0, 1, NULL, 0);
236 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
237 * and some other details.
239 static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
241 unsigned char command;
243 if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
244 command = ALAUDA_GET_XD_MEDIA_SIG;
246 command = ALAUDA_GET_SM_MEDIA_SIG;
248 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
249 command, 0xc0, 0, 0, data, 4);
253 * Resets the media status (but not the whole device?)
255 static int alauda_reset_media(struct us_data *us)
257 unsigned char *command = us->iobuf;
259 memset(command, 0, 9);
260 command[0] = ALAUDA_BULK_CMD;
261 command[1] = ALAUDA_BULK_RESET_MEDIA;
262 command[8] = MEDIA_PORT(us);
264 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
269 * Examines the media and deduces capacity, etc.
271 static int alauda_init_media(struct us_data *us)
273 unsigned char *data = us->iobuf;
275 struct alauda_card_info *media_info;
276 unsigned int num_zones;
281 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
282 return USB_STOR_TRANSPORT_ERROR;
288 US_DEBUGP("alauda_init_media: We are ready for action!\n");
290 if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
291 return USB_STOR_TRANSPORT_ERROR;
295 if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
296 return USB_STOR_TRANSPORT_ERROR;
298 if (data[0] != 0x14) {
299 US_DEBUGP("alauda_init_media: Media not ready after ack\n");
300 return USB_STOR_TRANSPORT_ERROR;
303 if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
304 return USB_STOR_TRANSPORT_ERROR;
306 US_DEBUGP("alauda_init_media: Media signature: %02X %02X %02X %02X\n",
307 data[0], data[1], data[2], data[3]);
308 media_info = alauda_card_find_id(data[1]);
309 if (media_info == NULL) {
311 "alauda_init_media: Unrecognised media signature: "
312 "%02X %02X %02X %02X\n",
313 data[0], data[1], data[2], data[3]);
314 return USB_STOR_TRANSPORT_ERROR;
317 MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
318 US_DEBUGP("Found media with capacity: %ldMB\n",
319 MEDIA_INFO(us).capacity >> 20);
321 MEDIA_INFO(us).pageshift = media_info->pageshift;
322 MEDIA_INFO(us).blockshift = media_info->blockshift;
323 MEDIA_INFO(us).zoneshift = media_info->zoneshift;
325 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
326 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
327 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
329 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
330 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
332 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
333 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
334 MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
335 MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
337 if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
338 return USB_STOR_TRANSPORT_ERROR;
340 return USB_STOR_TRANSPORT_GOOD;
344 * Examines the media status and does the right thing when the media has gone,
345 * appeared, or changed.
347 static int alauda_check_media(struct us_data *us)
349 struct alauda_info *info = (struct alauda_info *) us->extra;
350 unsigned char status[2];
353 rc = alauda_get_media_status(us, status);
355 /* Check for no media or door open */
356 if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
357 || ((status[1] & 0x01) == 0)) {
358 US_DEBUGP("alauda_check_media: No media, or door open\n");
359 alauda_free_maps(&MEDIA_INFO(us));
360 info->sense_key = 0x02;
361 info->sense_asc = 0x3A;
362 info->sense_ascq = 0x00;
363 return USB_STOR_TRANSPORT_FAILED;
366 /* Check for media change */
367 if (status[0] & 0x08) {
368 US_DEBUGP("alauda_check_media: Media change detected\n");
369 alauda_free_maps(&MEDIA_INFO(us));
370 alauda_init_media(us);
372 info->sense_key = UNIT_ATTENTION;
373 info->sense_asc = 0x28;
374 info->sense_ascq = 0x00;
375 return USB_STOR_TRANSPORT_FAILED;
378 return USB_STOR_TRANSPORT_GOOD;
382 * Checks the status from the 2nd status register
383 * Returns 3 bytes of status data, only the first is known
385 static int alauda_check_status2(struct us_data *us)
388 unsigned char command[] = {
389 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
390 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
392 unsigned char data[3];
394 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
396 if (rc != USB_STOR_XFER_GOOD)
399 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
401 if (rc != USB_STOR_XFER_GOOD)
404 US_DEBUGP("alauda_check_status2: %02X %02X %02X\n", data[0], data[1], data[2]);
405 if (data[0] & ALAUDA_STATUS_ERROR)
406 return USB_STOR_XFER_ERROR;
408 return USB_STOR_XFER_GOOD;
412 * Gets the redundancy data for the first page of a PBA
415 static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
418 unsigned char command[] = {
419 ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
420 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
423 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
425 if (rc != USB_STOR_XFER_GOOD)
428 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
433 * Finds the first unused PBA in a zone
434 * Returns the absolute PBA of an unused PBA, or 0 if none found.
436 static u16 alauda_find_unused_pba(struct alauda_media_info *info,
439 u16 *pba_to_lba = info->pba_to_lba[zone];
442 for (i = 0; i < info->zonesize; i++)
443 if (pba_to_lba[i] == UNDEF)
444 return (zone << info->zoneshift) + i;
450 * Reads the redundancy data for all PBA's in a zone
451 * Produces lba <--> pba mappings
453 static int alauda_read_map(struct us_data *us, unsigned int zone)
455 unsigned char *data = us->iobuf;
458 unsigned int zonesize = MEDIA_INFO(us).zonesize;
459 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
460 unsigned int lba_offset, lba_real, blocknum;
461 unsigned int zone_base_lba = zone * uzonesize;
462 unsigned int zone_base_pba = zone * zonesize;
463 u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
464 u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
465 if (lba_to_pba == NULL || pba_to_lba == NULL) {
466 result = USB_STOR_TRANSPORT_ERROR;
470 US_DEBUGP("alauda_read_map: Mapping blocks for zone %d\n", zone);
472 /* 1024 PBA's per zone */
473 for (i = 0; i < zonesize; i++)
474 lba_to_pba[i] = pba_to_lba[i] = UNDEF;
476 for (i = 0; i < zonesize; i++) {
477 blocknum = zone_base_pba + i;
479 result = alauda_get_redu_data(us, blocknum, data);
480 if (result != USB_STOR_XFER_GOOD) {
481 result = USB_STOR_TRANSPORT_ERROR;
485 /* special PBAs have control field 0^16 */
486 for (j = 0; j < 16; j++)
489 pba_to_lba[i] = UNUSABLE;
490 US_DEBUGP("alauda_read_map: PBA %d has no logical mapping\n", blocknum);
494 /* unwritten PBAs have control field FF^16 */
495 for (j = 0; j < 16; j++)
501 /* normal PBAs start with six FFs */
503 US_DEBUGP("alauda_read_map: PBA %d has no logical mapping: "
504 "reserved area = %02X%02X%02X%02X "
505 "data status %02X block status %02X\n",
506 blocknum, data[0], data[1], data[2], data[3],
508 pba_to_lba[i] = UNUSABLE;
512 if ((data[6] >> 4) != 0x01) {
513 US_DEBUGP("alauda_read_map: PBA %d has invalid address "
514 "field %02X%02X/%02X%02X\n",
515 blocknum, data[6], data[7], data[11], data[12]);
516 pba_to_lba[i] = UNUSABLE;
520 /* check even parity */
521 if (parity[data[6] ^ data[7]]) {
523 "alauda_read_map: Bad parity in LBA for block %d"
524 " (%02X %02X)\n", i, data[6], data[7]);
525 pba_to_lba[i] = UNUSABLE;
529 lba_offset = short_pack(data[7], data[6]);
530 lba_offset = (lba_offset & 0x07FF) >> 1;
531 lba_real = lba_offset + zone_base_lba;
534 * Every 1024 physical blocks ("zone"), the LBA numbers
535 * go back to zero, but are within a higher block of LBA's.
536 * Also, there is a maximum of 1000 LBA's per zone.
537 * In other words, in PBA 1024-2047 you will find LBA 0-999
538 * which are really LBA 1000-1999. This allows for 24 bad
539 * or special physical blocks per zone.
542 if (lba_offset >= uzonesize) {
544 "alauda_read_map: Bad low LBA %d for block %d\n",
549 if (lba_to_pba[lba_offset] != UNDEF) {
552 "LBA %d seen for PBA %d and %d\n",
553 lba_real, lba_to_pba[lba_offset], blocknum);
557 pba_to_lba[i] = lba_real;
558 lba_to_pba[lba_offset] = blocknum;
562 MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
563 MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
575 * Checks to see whether we have already mapped a certain zone
576 * If we haven't, the map is generated
578 static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
580 if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
581 || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
582 alauda_read_map(us, zone);
586 * Erases an entire block
588 static int alauda_erase_block(struct us_data *us, u16 pba)
591 unsigned char command[] = {
592 ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
593 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
595 unsigned char buf[2];
597 US_DEBUGP("alauda_erase_block: Erasing PBA %d\n", pba);
599 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
601 if (rc != USB_STOR_XFER_GOOD)
604 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
606 if (rc != USB_STOR_XFER_GOOD)
609 US_DEBUGP("alauda_erase_block: Erase result: %02X %02X\n",
615 * Reads data from a certain offset page inside a PBA, including interleaved
616 * redundancy data. Returns (pagesize+64)*pages bytes in data.
618 static int alauda_read_block_raw(struct us_data *us, u16 pba,
619 unsigned int page, unsigned int pages, unsigned char *data)
622 unsigned char command[] = {
623 ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
624 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
627 US_DEBUGP("alauda_read_block: pba %d page %d count %d\n",
630 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
632 if (rc != USB_STOR_XFER_GOOD)
635 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
636 data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
640 * Reads data from a certain offset page inside a PBA, excluding redundancy
641 * data. Returns pagesize*pages bytes in data. Note that data must be big enough
642 * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
643 * trailing bytes outside this function.
645 static int alauda_read_block(struct us_data *us, u16 pba,
646 unsigned int page, unsigned int pages, unsigned char *data)
649 unsigned int pagesize = MEDIA_INFO(us).pagesize;
651 rc = alauda_read_block_raw(us, pba, page, pages, data);
652 if (rc != USB_STOR_XFER_GOOD)
655 /* Cut out the redundancy data */
656 for (i = 0; i < pages; i++) {
657 int dest_offset = i * pagesize;
658 int src_offset = i * (pagesize + 64);
659 memmove(data + dest_offset, data + src_offset, pagesize);
666 * Writes an entire block of data and checks status after write.
667 * Redundancy data must be already included in data. Data should be
668 * (pagesize+64)*blocksize bytes in length.
670 static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
673 struct alauda_info *info = (struct alauda_info *) us->extra;
674 unsigned char command[] = {
675 ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
676 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
679 US_DEBUGP("alauda_write_block: pba %d\n", pba);
681 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
683 if (rc != USB_STOR_XFER_GOOD)
686 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
687 (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
689 if (rc != USB_STOR_XFER_GOOD)
692 return alauda_check_status2(us);
696 * Write some data to a specific LBA.
698 static int alauda_write_lba(struct us_data *us, u16 lba,
699 unsigned int page, unsigned int pages,
700 unsigned char *ptr, unsigned char *blockbuffer)
702 u16 pba, lbap, new_pba;
703 unsigned char *bptr, *cptr, *xptr;
704 unsigned char ecc[3];
706 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
707 unsigned int zonesize = MEDIA_INFO(us).zonesize;
708 unsigned int pagesize = MEDIA_INFO(us).pagesize;
709 unsigned int blocksize = MEDIA_INFO(us).blocksize;
710 unsigned int lba_offset = lba % uzonesize;
711 unsigned int new_pba_offset;
712 unsigned int zone = lba / uzonesize;
714 alauda_ensure_map_for_zone(us, zone);
716 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
718 /* Maybe it is impossible to write to PBA 1.
719 Fake success, but don't do anything. */
721 "alauda_write_lba: avoid writing to pba 1\n");
722 return USB_STOR_TRANSPORT_GOOD;
725 new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
728 "alauda_write_lba: Out of unused blocks\n");
729 return USB_STOR_TRANSPORT_ERROR;
732 /* read old contents */
734 result = alauda_read_block_raw(us, pba, 0,
735 blocksize, blockbuffer);
736 if (result != USB_STOR_XFER_GOOD)
739 memset(blockbuffer, 0, blocksize * (pagesize + 64));
742 lbap = (lba_offset << 1) | 0x1000;
743 if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
746 /* check old contents and fill lba */
747 for (i = 0; i < blocksize; i++) {
748 bptr = blockbuffer + (i * (pagesize + 64));
749 cptr = bptr + pagesize;
750 nand_compute_ecc(bptr, ecc);
751 if (!nand_compare_ecc(cptr+13, ecc)) {
752 US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n",
754 nand_store_ecc(cptr+13, ecc);
756 nand_compute_ecc(bptr + (pagesize / 2), ecc);
757 if (!nand_compare_ecc(cptr+8, ecc)) {
758 US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n",
760 nand_store_ecc(cptr+8, ecc);
762 cptr[6] = cptr[11] = MSB_of(lbap);
763 cptr[7] = cptr[12] = LSB_of(lbap);
766 /* copy in new stuff and compute ECC */
768 for (i = page; i < page+pages; i++) {
769 bptr = blockbuffer + (i * (pagesize + 64));
770 cptr = bptr + pagesize;
771 memcpy(bptr, xptr, pagesize);
773 nand_compute_ecc(bptr, ecc);
774 nand_store_ecc(cptr+13, ecc);
775 nand_compute_ecc(bptr + (pagesize / 2), ecc);
776 nand_store_ecc(cptr+8, ecc);
779 result = alauda_write_block(us, new_pba, blockbuffer);
780 if (result != USB_STOR_XFER_GOOD)
783 new_pba_offset = new_pba - (zone * zonesize);
784 MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
785 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
786 US_DEBUGP("alauda_write_lba: Remapped LBA %d to PBA %d\n",
790 unsigned int pba_offset = pba - (zone * zonesize);
791 result = alauda_erase_block(us, pba);
792 if (result != USB_STOR_XFER_GOOD)
794 MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
797 return USB_STOR_TRANSPORT_GOOD;
801 * Read data from a specific sector address
803 static int alauda_read_data(struct us_data *us, unsigned long address,
804 unsigned int sectors)
806 unsigned char *buffer;
808 unsigned int page, len, offset;
809 unsigned int blockshift = MEDIA_INFO(us).blockshift;
810 unsigned int pageshift = MEDIA_INFO(us).pageshift;
811 unsigned int blocksize = MEDIA_INFO(us).blocksize;
812 unsigned int pagesize = MEDIA_INFO(us).pagesize;
813 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
814 struct scatterlist *sg;
818 * Since we only read in one block at a time, we have to create
819 * a bounce buffer and move the data a piece at a time between the
820 * bounce buffer and the actual transfer buffer.
821 * We make this buffer big enough to hold temporary redundancy data,
822 * which we use when reading the data blocks.
825 len = min(sectors, blocksize) * (pagesize + 64);
826 buffer = kmalloc(len, GFP_NOIO);
827 if (buffer == NULL) {
828 printk(KERN_WARNING "alauda_read_data: Out of memory\n");
829 return USB_STOR_TRANSPORT_ERROR;
832 /* Figure out the initial LBA and page */
833 lba = address >> blockshift;
834 page = (address & MEDIA_INFO(us).blockmask);
835 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
837 result = USB_STOR_TRANSPORT_GOOD;
841 while (sectors > 0) {
842 unsigned int zone = lba / uzonesize; /* integer division */
843 unsigned int lba_offset = lba - (zone * uzonesize);
846 alauda_ensure_map_for_zone(us, zone);
848 /* Not overflowing capacity? */
849 if (lba >= max_lba) {
850 US_DEBUGP("Error: Requested lba %u exceeds "
851 "maximum %u\n", lba, max_lba);
852 result = USB_STOR_TRANSPORT_ERROR;
856 /* Find number of pages we can read in this block */
857 pages = min(sectors, blocksize - page);
858 len = pages << pageshift;
860 /* Find where this lba lives on disk */
861 pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
863 if (pba == UNDEF) { /* this lba was never written */
864 US_DEBUGP("Read %d zero pages (LBA %d) page %d\n",
867 /* This is not really an error. It just means
868 that the block has never been written.
869 Instead of returning USB_STOR_TRANSPORT_ERROR
870 it is better to return all zero data. */
872 memset(buffer, 0, len);
874 US_DEBUGP("Read %d pages, from PBA %d"
875 " (LBA %d) page %d\n",
876 pages, pba, lba, page);
878 result = alauda_read_block(us, pba, page, pages, buffer);
879 if (result != USB_STOR_TRANSPORT_GOOD)
883 /* Store the data in the transfer buffer */
884 usb_stor_access_xfer_buf(buffer, len, us->srb,
885 &sg, &offset, TO_XFER_BUF);
897 * Write data to a specific sector address
899 static int alauda_write_data(struct us_data *us, unsigned long address,
900 unsigned int sectors)
902 unsigned char *buffer, *blockbuffer;
903 unsigned int page, len, offset;
904 unsigned int blockshift = MEDIA_INFO(us).blockshift;
905 unsigned int pageshift = MEDIA_INFO(us).pageshift;
906 unsigned int blocksize = MEDIA_INFO(us).blocksize;
907 unsigned int pagesize = MEDIA_INFO(us).pagesize;
908 struct scatterlist *sg;
913 * Since we don't write the user data directly to the device,
914 * we have to create a bounce buffer and move the data a piece
915 * at a time between the bounce buffer and the actual transfer buffer.
918 len = min(sectors, blocksize) * pagesize;
919 buffer = kmalloc(len, GFP_NOIO);
920 if (buffer == NULL) {
921 printk(KERN_WARNING "alauda_write_data: Out of memory\n");
922 return USB_STOR_TRANSPORT_ERROR;
926 * We also need a temporary block buffer, where we read in the old data,
927 * overwrite parts with the new data, and manipulate the redundancy data
929 blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
930 if (blockbuffer == NULL) {
931 printk(KERN_WARNING "alauda_write_data: Out of memory\n");
933 return USB_STOR_TRANSPORT_ERROR;
936 /* Figure out the initial LBA and page */
937 lba = address >> blockshift;
938 page = (address & MEDIA_INFO(us).blockmask);
939 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
941 result = USB_STOR_TRANSPORT_GOOD;
945 while (sectors > 0) {
946 /* Write as many sectors as possible in this block */
947 unsigned int pages = min(sectors, blocksize - page);
948 len = pages << pageshift;
950 /* Not overflowing capacity? */
951 if (lba >= max_lba) {
952 US_DEBUGP("alauda_write_data: Requested lba %u exceeds "
953 "maximum %u\n", lba, max_lba);
954 result = USB_STOR_TRANSPORT_ERROR;
958 /* Get the data from the transfer buffer */
959 usb_stor_access_xfer_buf(buffer, len, us->srb,
960 &sg, &offset, FROM_XFER_BUF);
962 result = alauda_write_lba(us, lba, page, pages, buffer,
964 if (result != USB_STOR_TRANSPORT_GOOD)
978 * Our interface with the rest of the world
981 static void alauda_info_destructor(void *extra)
983 struct alauda_info *info = (struct alauda_info *) extra;
989 for (port = 0; port < 2; port++) {
990 struct alauda_media_info *media_info = &info->port[port];
992 alauda_free_maps(media_info);
993 kfree(media_info->lba_to_pba);
994 kfree(media_info->pba_to_lba);
999 * Initialize alauda_info struct and find the data-write endpoint
1001 int init_alauda(struct us_data *us)
1003 struct alauda_info *info;
1004 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
1007 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
1009 US_DEBUGP("init_alauda: Gah! Can't allocate storage for"
1010 "alauda info struct!\n");
1011 return USB_STOR_TRANSPORT_ERROR;
1013 info = (struct alauda_info *) us->extra;
1014 us->extra_destructor = alauda_info_destructor;
1016 info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
1017 altsetting->endpoint[0].desc.bEndpointAddress
1018 & USB_ENDPOINT_NUMBER_MASK);
1020 return USB_STOR_TRANSPORT_GOOD;
1023 int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
1026 struct alauda_info *info = (struct alauda_info *) us->extra;
1027 unsigned char *ptr = us->iobuf;
1028 static unsigned char inquiry_response[36] = {
1029 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1032 if (srb->cmnd[0] == INQUIRY) {
1033 US_DEBUGP("alauda_transport: INQUIRY. "
1034 "Returning bogus response.\n");
1035 memcpy(ptr, inquiry_response, sizeof(inquiry_response));
1036 fill_inquiry_response(us, ptr, 36);
1037 return USB_STOR_TRANSPORT_GOOD;
1040 if (srb->cmnd[0] == TEST_UNIT_READY) {
1041 US_DEBUGP("alauda_transport: TEST_UNIT_READY.\n");
1042 return alauda_check_media(us);
1045 if (srb->cmnd[0] == READ_CAPACITY) {
1046 unsigned int num_zones;
1047 unsigned long capacity;
1049 rc = alauda_check_media(us);
1050 if (rc != USB_STOR_TRANSPORT_GOOD)
1053 num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
1054 + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
1056 capacity = num_zones * MEDIA_INFO(us).uzonesize
1057 * MEDIA_INFO(us).blocksize;
1059 /* Report capacity and page size */
1060 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
1061 ((__be32 *) ptr)[1] = cpu_to_be32(512);
1063 usb_stor_set_xfer_buf(ptr, 8, srb);
1064 return USB_STOR_TRANSPORT_GOOD;
1067 if (srb->cmnd[0] == READ_10) {
1068 unsigned int page, pages;
1070 rc = alauda_check_media(us);
1071 if (rc != USB_STOR_TRANSPORT_GOOD)
1074 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1076 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1077 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1079 US_DEBUGP("alauda_transport: READ_10: page %d pagect %d\n",
1082 return alauda_read_data(us, page, pages);
1085 if (srb->cmnd[0] == WRITE_10) {
1086 unsigned int page, pages;
1088 rc = alauda_check_media(us);
1089 if (rc != USB_STOR_TRANSPORT_GOOD)
1092 page = short_pack(srb->cmnd[3], srb->cmnd[2]);
1094 page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
1095 pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
1097 US_DEBUGP("alauda_transport: WRITE_10: page %d pagect %d\n",
1100 return alauda_write_data(us, page, pages);
1103 if (srb->cmnd[0] == REQUEST_SENSE) {
1104 US_DEBUGP("alauda_transport: REQUEST_SENSE.\n");
1108 ptr[2] = info->sense_key;
1110 ptr[12] = info->sense_asc;
1111 ptr[13] = info->sense_ascq;
1112 usb_stor_set_xfer_buf(ptr, 18, srb);
1114 return USB_STOR_TRANSPORT_GOOD;
1117 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
1118 /* sure. whatever. not like we can stop the user from popping
1119 the media out of the device (no locking doors, etc) */
1120 return USB_STOR_TRANSPORT_GOOD;
1123 US_DEBUGP("alauda_transport: Gah! Unknown command: %d (0x%x)\n",
1124 srb->cmnd[0], srb->cmnd[0]);
1125 info->sense_key = 0x05;
1126 info->sense_asc = 0x20;
1127 info->sense_ascq = 0x00;
1128 return USB_STOR_TRANSPORT_FAILED;