34 #include <linux/module.h>
35 #include <linux/slab.h>
37 #include <scsi/scsi.h>
53 #define ALAUDA_STATUS_ERROR 0x01
54 #define ALAUDA_STATUS_READY 0x40
59 #define ALAUDA_GET_XD_MEDIA_STATUS 0x08
60 #define ALAUDA_GET_SM_MEDIA_STATUS 0x98
61 #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
62 #define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a
63 #define ALAUDA_GET_XD_MEDIA_SIG 0x86
64 #define ALAUDA_GET_SM_MEDIA_SIG 0x96
69 #define ALAUDA_BULK_CMD 0x40
74 #define ALAUDA_BULK_GET_REDU_DATA 0x85
75 #define ALAUDA_BULK_READ_BLOCK 0x94
76 #define ALAUDA_BULK_ERASE_BLOCK 0xa3
77 #define ALAUDA_BULK_WRITE_BLOCK 0xb4
78 #define ALAUDA_BULK_GET_STATUS2 0xb7
79 #define ALAUDA_BULK_RESET_MEDIA 0xe0
84 #define ALAUDA_PORT_XD 0x00
85 #define ALAUDA_PORT_SM 0x01
92 #define UNUSABLE 0xfffd
119 #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
120 #define LSB_of(s) ((s)&0xFF)
121 #define MSB_of(s) ((s)>>8)
123 #define MEDIA_PORT(us) us->srb->device->lun
124 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
126 #define PBA_LO(pba) ((pba & 0xF) << 5)
127 #define PBA_HI(pba) (pba >> 3)
128 #define PBA_ZONE(pba) (pba >> 11)
130 static int init_alauda(
struct us_data *us);
136 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
137 vendorName, productName, useProtocol, useTransport, \
138 initFunction, flags) \
139 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
140 .driver_info = (flags) }
153 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
154 vendor_name, product_name, use_protocol, use_transport, \
155 init_function, Flags) \
157 .vendorName = vendor_name, \
158 .productName = product_name, \
159 .useProtocol = use_protocol, \
160 .useTransport = use_transport, \
161 .initFunction = init_function, \
186 { 0x6e, 20, 8, 4, 8},
187 { 0xe8, 20, 8, 4, 8},
188 { 0xec, 20, 8, 4, 8},
189 { 0x64, 21, 8, 4, 9},
190 { 0xea, 21, 8, 4, 9},
191 { 0x6b, 22, 9, 4, 9},
192 { 0xe3, 22, 9, 4, 9},
193 { 0xe5, 22, 9, 4, 9},
194 { 0xe6, 23, 9, 4, 10},
195 { 0x73, 24, 9, 5, 10},
196 { 0x75, 25, 9, 5, 10},
197 { 0x76, 26, 9, 5, 10},
198 { 0x79, 27, 9, 5, 10},
199 { 0x71, 28, 9, 5, 10},
202 { 0x5d, 21, 9, 4, 8},
203 { 0xd5, 22, 9, 4, 9},
204 { 0xd6, 23, 9, 4, 10},
205 { 0x57, 24, 9, 4, 11},
206 { 0x58, 25, 9, 4, 12},
213 for (i = 0; alauda_card_ids[
i].
id != 0; i++)
214 if (alauda_card_ids[i].
id ==
id)
215 return &(alauda_card_ids[
i]);
223 static unsigned char parity[256];
224 static unsigned char ecc2[256];
226 static void nand_init_ecc(
void) {
230 for (i = 1; i < 256; i++)
231 parity[i] = (parity[i&(i-1)] ^ 1);
233 for (i = 0; i < 256; i++) {
235 for (j = 0; j < 8; j++) {
245 ecc2[
i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
250 static void nand_compute_ecc(
unsigned char *
data,
unsigned char *
ecc) {
252 unsigned char par,
bit,
bits[8];
255 for (j = 0; j < 8; j++)
259 for (i = 0; i < 256; i++) {
261 bit = parity[data[
i]];
262 for (j = 0; j < 8; j++)
263 if ((i & (1<<j)) == 0)
268 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
269 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
271 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
272 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
277 static int nand_compare_ecc(
unsigned char *data,
unsigned char *ecc) {
278 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
281 static void nand_store_ecc(
unsigned char *data,
unsigned char *ecc) {
294 unsigned int shift = media_info->
zoneshift
296 unsigned int num_zones = media_info->
capacity >> shift;
300 for (i = 0; i < num_zones; i++) {
306 for (i = 0; i < num_zones; i++) {
316 static int alauda_get_media_status(
struct us_data *us,
unsigned char *data)
327 command, 0xc0, 0, 1, data, 2);
329 US_DEBUGP(
"alauda_get_media_status: Media status %02X %02X\n",
339 static int alauda_ack_media(
struct us_data *us)
349 command, 0x40, 0, 1,
NULL, 0);
356 static int alauda_get_media_signature(
struct us_data *us,
unsigned char *data)
366 command, 0xc0, 0, 0, data, 4);
372 static int alauda_reset_media(
struct us_data *us)
374 unsigned char *command = us->
iobuf;
388 static int alauda_init_media(
struct us_data *us)
390 unsigned char *data = us->
iobuf;
393 unsigned int num_zones;
405 US_DEBUGP(
"alauda_init_media: We are ready for action!\n");
415 if (data[0] != 0x14) {
416 US_DEBUGP(
"alauda_init_media: Media not ready after ack\n");
423 US_DEBUGP(
"alauda_init_media: Media signature: %02X %02X %02X %02X\n",
424 data[0], data[1], data[2], data[3]);
425 media_info = alauda_card_find_id(data[1]);
426 if (media_info ==
NULL) {
428 "alauda_init_media: Unrecognised media signature: "
429 "%02X %02X %02X %02X\n",
430 data[0], data[1], data[2], data[3]);
435 US_DEBUGP(
"Found media with capacity: %ldMB\n",
464 static int alauda_check_media(
struct us_data *us)
470 rc = alauda_get_media_status(us,
status);
474 || ((
status[1] & 0x01) == 0)) {
475 US_DEBUGP(
"alauda_check_media: No media, or door open\n");
485 US_DEBUGP(
"alauda_check_media: Media change detected\n");
487 alauda_init_media(us);
502 static int alauda_check_status2(
struct us_data *us)
505 unsigned char command[] = {
509 unsigned char data[3];
521 US_DEBUGP(
"alauda_check_status2: %02X %02X %02X\n", data[0], data[1], data[2]);
532 static int alauda_get_redu_data(
struct us_data *us,
u16 pba,
unsigned char *data)
535 unsigned char command[] = {
559 for (i = 0; i < info->
zonesize; i++)
560 if (pba_to_lba[i] ==
UNDEF)
570 static int alauda_read_map(
struct us_data *us,
unsigned int zone)
572 unsigned char *data = us->
iobuf;
575 unsigned int zonesize =
MEDIA_INFO(us).zonesize;
576 unsigned int uzonesize =
MEDIA_INFO(us).uzonesize;
577 unsigned int lba_offset, lba_real, blocknum;
578 unsigned int zone_base_lba = zone * uzonesize;
579 unsigned int zone_base_pba = zone * zonesize;
582 if (lba_to_pba ==
NULL || pba_to_lba ==
NULL) {
587 US_DEBUGP(
"alauda_read_map: Mapping blocks for zone %d\n", zone);
590 for (i = 0; i < zonesize; i++)
591 lba_to_pba[i] = pba_to_lba[i] =
UNDEF;
593 for (i = 0; i < zonesize; i++) {
594 blocknum = zone_base_pba +
i;
596 result = alauda_get_redu_data(us, blocknum, data);
603 for (j = 0; j < 16; j++)
607 US_DEBUGP(
"alauda_read_map: PBA %d has no logical mapping\n", blocknum);
612 for (j = 0; j < 16; j++)
620 US_DEBUGP(
"alauda_read_map: PBA %d has no logical mapping: "
621 "reserved area = %02X%02X%02X%02X "
622 "data status %02X block status %02X\n",
623 blocknum, data[0], data[1], data[2], data[3],
629 if ((data[6] >> 4) != 0x01) {
630 US_DEBUGP(
"alauda_read_map: PBA %d has invalid address "
631 "field %02X%02X/%02X%02X\n",
632 blocknum, data[6], data[7], data[11], data[12]);
638 if (parity[data[6] ^ data[7]]) {
640 "alauda_read_map: Bad parity in LBA for block %d"
641 " (%02X %02X)\n", i, data[6], data[7]);
647 lba_offset = (lba_offset & 0x07FF) >> 1;
648 lba_real = lba_offset + zone_base_lba;
659 if (lba_offset >= uzonesize) {
661 "alauda_read_map: Bad low LBA %d for block %d\n",
666 if (lba_to_pba[lba_offset] !=
UNDEF) {
669 "LBA %d seen for PBA %d and %d\n",
670 lba_real, lba_to_pba[lba_offset], blocknum);
674 pba_to_lba[
i] = lba_real;
675 lba_to_pba[lba_offset] = blocknum;
695 static void alauda_ensure_map_for_zone(
struct us_data *us,
unsigned int zone)
699 alauda_read_map(us, zone);
705 static int alauda_erase_block(
struct us_data *us,
u16 pba)
708 unsigned char command[] = {
712 unsigned char buf[2];
714 US_DEBUGP(
"alauda_erase_block: Erasing PBA %d\n", pba);
726 US_DEBUGP(
"alauda_erase_block: Erase result: %02X %02X\n",
735 static int alauda_read_block_raw(
struct us_data *us,
u16 pba,
736 unsigned int page,
unsigned int pages,
unsigned char *data)
739 unsigned char command[] = {
744 US_DEBUGP(
"alauda_read_block: pba %d page %d count %d\n",
762 static int alauda_read_block(
struct us_data *us,
u16 pba,
763 unsigned int page,
unsigned int pages,
unsigned char *data)
766 unsigned int pagesize =
MEDIA_INFO(us).pagesize;
768 rc = alauda_read_block_raw(us, pba, page, pages, data);
773 for (i = 0; i <
pages; i++) {
774 int dest_offset = i * pagesize;
775 int src_offset = i * (pagesize + 64);
776 memmove(data + dest_offset, data + src_offset, pagesize);
787 static int alauda_write_block(
struct us_data *us,
u16 pba,
unsigned char *data)
791 unsigned char command[] = {
796 US_DEBUGP(
"alauda_write_block: pba %d\n", pba);
809 return alauda_check_status2(us);
816 unsigned int page,
unsigned int pages,
817 unsigned char *
ptr,
unsigned char *blockbuffer)
819 u16 pba, lbap, new_pba;
820 unsigned char *bptr, *cptr, *xptr;
821 unsigned char ecc[3];
823 unsigned int uzonesize =
MEDIA_INFO(us).uzonesize;
824 unsigned int zonesize =
MEDIA_INFO(us).zonesize;
825 unsigned int pagesize =
MEDIA_INFO(us).pagesize;
826 unsigned int blocksize =
MEDIA_INFO(us).blocksize;
827 unsigned int lba_offset = lba % uzonesize;
828 unsigned int new_pba_offset;
829 unsigned int zone = lba / uzonesize;
831 alauda_ensure_map_for_zone(us, zone);
833 pba =
MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
838 "alauda_write_lba: avoid writing to pba 1\n");
842 new_pba = alauda_find_unused_pba(&
MEDIA_INFO(us), zone);
845 "alauda_write_lba: Out of unused blocks\n");
851 result = alauda_read_block_raw(us, pba, 0,
852 blocksize, blockbuffer);
856 memset(blockbuffer, 0, blocksize * (pagesize + 64));
859 lbap = (lba_offset << 1) | 0x1000;
864 for (i = 0; i < blocksize; i++) {
865 bptr = blockbuffer + (i * (pagesize + 64));
866 cptr = bptr + pagesize;
867 nand_compute_ecc(bptr, ecc);
868 if (!nand_compare_ecc(cptr+13, ecc)) {
869 US_DEBUGP(
"Warning: bad ecc in page %d- of pba %d\n",
871 nand_store_ecc(cptr+13, ecc);
873 nand_compute_ecc(bptr + (pagesize / 2), ecc);
874 if (!nand_compare_ecc(cptr+8, ecc)) {
875 US_DEBUGP(
"Warning: bad ecc in page %d+ of pba %d\n",
877 nand_store_ecc(cptr+8, ecc);
879 cptr[6] = cptr[11] =
MSB_of(lbap);
880 cptr[7] = cptr[12] =
LSB_of(lbap);
885 for (i = page; i < page+
pages; i++) {
886 bptr = blockbuffer + (i * (pagesize + 64));
887 cptr = bptr + pagesize;
888 memcpy(bptr, xptr, pagesize);
890 nand_compute_ecc(bptr, ecc);
891 nand_store_ecc(cptr+13, ecc);
892 nand_compute_ecc(bptr + (pagesize / 2), ecc);
893 nand_store_ecc(cptr+8, ecc);
896 result = alauda_write_block(us, new_pba, blockbuffer);
900 new_pba_offset = new_pba - (zone * zonesize);
902 MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
903 US_DEBUGP(
"alauda_write_lba: Remapped LBA %d to PBA %d\n",
907 unsigned int pba_offset = pba - (zone * zonesize);
908 result = alauda_erase_block(us, pba);
920 static int alauda_read_data(
struct us_data *us,
unsigned long address,
926 unsigned int blockshift =
MEDIA_INFO(us).blockshift;
927 unsigned int pageshift =
MEDIA_INFO(us).pageshift;
928 unsigned int blocksize =
MEDIA_INFO(us).blocksize;
929 unsigned int pagesize =
MEDIA_INFO(us).pagesize;
930 unsigned int uzonesize =
MEDIA_INFO(us).uzonesize;
942 len =
min(sectors, blocksize) * (pagesize + 64);
944 if (buffer ==
NULL) {
950 lba = address >> blockshift;
952 max_lba =
MEDIA_INFO(us).capacity >> (blockshift + pageshift);
958 while (sectors > 0) {
959 unsigned int zone = lba / uzonesize;
960 unsigned int lba_offset = lba - (zone * uzonesize);
963 alauda_ensure_map_for_zone(us, zone);
966 if (lba >= max_lba) {
967 US_DEBUGP(
"Error: Requested lba %u exceeds "
968 "maximum %u\n", lba, max_lba);
974 pages =
min(sectors, blocksize - page);
975 len = pages << pageshift;
978 pba =
MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
981 US_DEBUGP(
"Read %d zero pages (LBA %d) page %d\n",
992 " (LBA %d) page %d\n",
993 pages, pba, lba, page);
995 result = alauda_read_block(us, pba, page, pages, buffer);
1016 static int alauda_write_data(
struct us_data *us,
unsigned long address,
1017 unsigned int sectors)
1019 unsigned char *
buffer, *blockbuffer;
1021 unsigned int blockshift =
MEDIA_INFO(us).blockshift;
1022 unsigned int pageshift =
MEDIA_INFO(us).pageshift;
1023 unsigned int blocksize =
MEDIA_INFO(us).blocksize;
1024 unsigned int pagesize =
MEDIA_INFO(us).pagesize;
1035 len =
min(sectors, blocksize) * pagesize;
1037 if (buffer ==
NULL) {
1047 if (blockbuffer ==
NULL) {
1054 lba = address >> blockshift;
1056 max_lba =
MEDIA_INFO(us).capacity >> (pageshift + blockshift);
1062 while (sectors > 0) {
1064 unsigned int pages =
min(sectors, blocksize - page);
1065 len = pages << pageshift;
1068 if (lba >= max_lba) {
1069 US_DEBUGP(
"alauda_write_data: Requested lba %u exceeds "
1070 "maximum %u\n", lba, max_lba);
1079 result = alauda_write_lba(us, lba, page, pages, buffer,
1098 static void alauda_info_destructor(
void *
extra)
1106 for (port = 0; port < 2; port++) {
1109 alauda_free_maps(media_info);
1118 static int init_alauda(
struct us_data *us)
1121 struct usb_host_interface *altsetting = us->
pusb_intf->cur_altsetting;
1126 US_DEBUGP(
"init_alauda: Gah! Can't allocate storage for"
1127 "alauda info struct!\n");
1134 altsetting->endpoint[0].desc.bEndpointAddress
1144 unsigned char *ptr = us->
iobuf;
1145 static unsigned char inquiry_response[36] = {
1146 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
1151 "Returning bogus response.\n");
1152 memcpy(ptr, inquiry_response,
sizeof(inquiry_response));
1158 US_DEBUGP(
"alauda_transport: TEST_UNIT_READY.\n");
1159 return alauda_check_media(us);
1163 unsigned int num_zones;
1166 rc = alauda_check_media(us);
1173 capacity = num_zones *
MEDIA_INFO(us).uzonesize
1187 rc = alauda_check_media(us);
1196 US_DEBUGP(
"alauda_transport: READ_10: page %d pagect %d\n",
1199 return alauda_read_data(us, page, pages);
1205 rc = alauda_check_media(us);
1214 US_DEBUGP(
"alauda_transport: WRITE_10: page %d pagect %d\n",
1217 return alauda_write_data(us, page, pages);
1221 US_DEBUGP(
"alauda_transport: REQUEST_SENSE.\n");
1240 US_DEBUGP(
"alauda_transport: Gah! Unknown command: %d (0x%x)\n",
1241 srb->cmnd[0],
srb->cmnd[0]);
1255 (
id - alauda_usb_ids) + alauda_unusual_dev_list);
1268 static struct usb_driver alauda_driver = {
1269 .name =
"ums-alauda",
1270 .probe = alauda_probe,
1277 .id_table = alauda_usb_ids,