Lines Matching +full:0 +full:- +full:9 +full:a +full:- +full:d

1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Alauda-based card readers
8 * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
10 * Alauda implements a vendor-specific command set to access two media reader
14 * The driver was developed through reverse-engineering, with the help of the
16 * (very old) vendor-supplied GPL sma03 driver.
34 #define DRV_NAME "ums-alauda"
36 MODULE_DESCRIPTION("Driver for Alauda-based card readers");
44 #define ALAUDA_STATUS_ERROR 0x01
45 #define ALAUDA_STATUS_READY 0x40
50 #define ALAUDA_GET_XD_MEDIA_STATUS 0x08
51 #define ALAUDA_GET_SM_MEDIA_STATUS 0x98
52 #define ALAUDA_ACK_XD_MEDIA_CHANGE 0x0a
53 #define ALAUDA_ACK_SM_MEDIA_CHANGE 0x9a
54 #define ALAUDA_GET_XD_MEDIA_SIG 0x86
55 #define ALAUDA_GET_SM_MEDIA_SIG 0x96
58 * Bulk command identity (byte 0)
60 #define ALAUDA_BULK_CMD 0x40
65 #define ALAUDA_BULK_GET_REDU_DATA 0x85
66 #define ALAUDA_BULK_READ_BLOCK 0x94
67 #define ALAUDA_BULK_ERASE_BLOCK 0xa3
68 #define ALAUDA_BULK_WRITE_BLOCK 0xb4
69 #define ALAUDA_BULK_GET_STATUS2 0xb7
70 #define ALAUDA_BULK_RESET_MEDIA 0xe0
75 #define ALAUDA_PORT_XD 0x00
76 #define ALAUDA_PORT_SM 0x01
81 #define UNDEF 0xffff
82 #define SPARE 0xfffe
83 #define UNUSABLE 0xfffd
113 #define LSB_of(s) ((s)&0xFF)
116 #define MEDIA_PORT(us) us->srb->device->lun
117 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
119 #define PBA_LO(pba) ((pba & 0xF) << 5)
172 unsigned char pageshift; /* 1<<ps bytes in a page */
179 { 0x6e, 20, 8, 4, 8}, /* 1 MB */
180 { 0xe8, 20, 8, 4, 8}, /* 1 MB */
181 { 0xec, 20, 8, 4, 8}, /* 1 MB */
182 { 0x64, 21, 8, 4, 9}, /* 2 MB */
183 { 0xea, 21, 8, 4, 9}, /* 2 MB */
184 { 0x6b, 22, 9, 4, 9}, /* 4 MB */
185 { 0xe3, 22, 9, 4, 9}, /* 4 MB */
186 { 0xe5, 22, 9, 4, 9}, /* 4 MB */
187 { 0xe6, 23, 9, 4, 10}, /* 8 MB */
188 { 0x73, 24, 9, 5, 10}, /* 16 MB */
189 { 0x75, 25, 9, 5, 10}, /* 32 MB */
190 { 0x76, 26, 9, 5, 10}, /* 64 MB */
191 { 0x79, 27, 9, 5, 10}, /* 128 MB */
192 { 0x71, 28, 9, 5, 10}, /* 256 MB */
195 { 0x5d, 21, 9, 4, 8}, /* 2 MB */
196 { 0xd5, 22, 9, 4, 9}, /* 4 MB */
197 { 0xd6, 23, 9, 4, 10}, /* 8 MB */
198 { 0x57, 24, 9, 4, 11}, /* 16 MB */
199 { 0x58, 25, 9, 4, 12}, /* 32 MB */
200 { 0,}
207 for (i = 0; alauda_card_ids[i].id != 0; i++) in alauda_card_find_id()
222 int i, j, a; in nand_init_ecc() local
224 parity[0] = 0; in nand_init_ecc()
226 parity[i] = (parity[i&(i-1)] ^ 1); in nand_init_ecc()
228 for (i = 0; i < 256; i++) { in nand_init_ecc()
229 a = 0; in nand_init_ecc()
230 for (j = 0; j < 8; j++) { in nand_init_ecc()
232 if ((j & 1) == 0) in nand_init_ecc()
233 a ^= 0x04; in nand_init_ecc()
234 if ((j & 2) == 0) in nand_init_ecc()
235 a ^= 0x10; in nand_init_ecc()
236 if ((j & 4) == 0) in nand_init_ecc()
237 a ^= 0x40; in nand_init_ecc()
240 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0)); in nand_init_ecc()
244 /* compute 3-byte ecc on 256 bytes */
247 int i, j, a; in nand_compute_ecc() local
248 unsigned char par = 0, bit, bits[8] = {0}; in nand_compute_ecc()
251 for (i = 0; i < 256; i++) { in nand_compute_ecc()
254 for (j = 0; j < 8; j++) in nand_compute_ecc()
255 if ((i & (1<<j)) == 0) in nand_compute_ecc()
260 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0]; in nand_compute_ecc()
261 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); in nand_compute_ecc()
263 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4]; in nand_compute_ecc()
264 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); in nand_compute_ecc()
271 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]); in nand_compare_ecc()
284 * Forget our PBA <---> LBA mappings for a particular port
288 unsigned int shift = media_info->zoneshift in alauda_free_maps()
289 + media_info->blockshift + media_info->pageshift; in alauda_free_maps()
290 unsigned int num_zones = media_info->capacity >> shift; in alauda_free_maps()
293 if (media_info->lba_to_pba != NULL) in alauda_free_maps()
294 for (i = 0; i < num_zones; i++) { in alauda_free_maps()
295 kfree(media_info->lba_to_pba[i]); in alauda_free_maps()
296 media_info->lba_to_pba[i] = NULL; in alauda_free_maps()
299 if (media_info->pba_to_lba != NULL) in alauda_free_maps()
300 for (i = 0; i < num_zones; i++) { in alauda_free_maps()
301 kfree(media_info->pba_to_lba[i]); in alauda_free_maps()
302 media_info->pba_to_lba[i] = NULL; in alauda_free_maps()
320 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, in alauda_get_media_status()
321 command, 0xc0, 0, 1, data, 2); in alauda_get_media_status()
324 usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]); in alauda_get_media_status()
342 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, in alauda_ack_media()
343 command, 0x40, 0, 1, NULL, 0); in alauda_ack_media()
347 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
359 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, in alauda_get_media_signature()
360 command, 0xc0, 0, 0, data, 4); in alauda_get_media_signature()
368 unsigned char *command = us->iobuf; in alauda_reset_media()
370 memset(command, 0, 9); in alauda_reset_media()
371 command[0] = ALAUDA_BULK_CMD; in alauda_reset_media()
375 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_reset_media()
376 command, 9, NULL); in alauda_reset_media()
384 unsigned char *data = us->iobuf; in alauda_init_media()
385 int ready = 0; in alauda_init_media()
389 while (ready == 0) { in alauda_init_media()
395 if (data[0] & 0x10) in alauda_init_media()
409 if (data[0] != 0x14) { in alauda_init_media()
425 MEDIA_INFO(us).capacity = 1 << media_info->chipshift; in alauda_init_media()
429 MEDIA_INFO(us).pageshift = media_info->pageshift; in alauda_init_media()
430 MEDIA_INFO(us).blockshift = media_info->blockshift; in alauda_init_media()
431 MEDIA_INFO(us).zoneshift = media_info->zoneshift; in alauda_init_media()
433 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift; in alauda_init_media()
434 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift; in alauda_init_media()
435 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift; in alauda_init_media()
437 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125; in alauda_init_media()
438 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1; in alauda_init_media()
459 struct alauda_info *info = (struct alauda_info *) us->extra; in alauda_check_media()
460 unsigned char *status = us->iobuf; in alauda_check_media()
465 status[0] = 0xF0; /* Pretend there's no media */ in alauda_check_media()
466 status[1] = 0; in alauda_check_media()
470 if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10) in alauda_check_media()
471 || ((status[1] & 0x01) == 0)) { in alauda_check_media()
474 info->sense_key = 0x02; in alauda_check_media()
475 info->sense_asc = 0x3A; in alauda_check_media()
476 info->sense_ascq = 0x00; in alauda_check_media()
481 if (status[0] & 0x08 || !info->media_initialized) { in alauda_check_media()
486 info->media_initialized = true; in alauda_check_media()
487 info->sense_key = UNIT_ATTENTION; in alauda_check_media()
488 info->sense_asc = 0x28; in alauda_check_media()
489 info->sense_ascq = 0x00; in alauda_check_media()
505 0, 0, 0, 0, 3, 0, MEDIA_PORT(us) in alauda_check_status2()
509 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_check_status2()
510 command, 9, NULL); in alauda_check_status2()
514 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, in alauda_check_status2()
520 if (data[0] & ALAUDA_STATUS_ERROR) in alauda_check_status2()
527 * Gets the redundancy data for the first page of a PBA
535 PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us) in alauda_get_redu_data()
538 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_get_redu_data()
539 command, 9, NULL); in alauda_get_redu_data()
543 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, in alauda_get_redu_data()
548 * Finds the first unused PBA in a zone
549 * Returns the absolute PBA of an unused PBA, or 0 if none found.
554 u16 *pba_to_lba = info->pba_to_lba[zone]; in alauda_find_unused_pba()
557 for (i = 0; i < info->zonesize; i++) in alauda_find_unused_pba()
559 return (zone << info->zoneshift) + i; in alauda_find_unused_pba()
561 return 0; in alauda_find_unused_pba()
565 * Reads the redundancy data for all PBA's in a zone
566 * Produces lba <--> pba mappings
570 unsigned char *data = us->iobuf; in alauda_read_map()
585 usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone); in alauda_read_map()
588 for (i = 0; i < zonesize; i++) in alauda_read_map()
591 for (i = 0; i < zonesize; i++) { in alauda_read_map()
600 /* special PBAs have control field 0^16 */ in alauda_read_map()
601 for (j = 0; j < 16; j++) in alauda_read_map()
602 if (data[j] != 0) in alauda_read_map()
605 usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum); in alauda_read_map()
610 for (j = 0; j < 16; j++) in alauda_read_map()
611 if (data[j] != 0xff) in alauda_read_map()
618 …usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X… in alauda_read_map()
620 data[0], data[1], data[2], data[3], in alauda_read_map()
626 if ((data[6] >> 4) != 0x01) { in alauda_read_map()
627 usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n", in alauda_read_map()
637 "alauda_read_map: Bad parity in LBA for block %d" in alauda_read_map()
644 lba_offset = (lba_offset & 0x07FF) >> 1; in alauda_read_map()
649 * go back to zero, but are within a higher block of LBA's. in alauda_read_map()
650 * Also, there is a maximum of 1000 LBA's per zone. in alauda_read_map()
651 * In other words, in PBA 1024-2047 you will find LBA 0-999 in alauda_read_map()
652 * which are really LBA 1000-1999. This allows for 24 bad in alauda_read_map()
658 "alauda_read_map: Bad low LBA %d for block %d\n", in alauda_read_map()
666 "LBA %d seen for PBA %d and %d\n", in alauda_read_map()
678 result = 0; in alauda_read_map()
689 * Checks to see whether we have already mapped a certain zone
707 PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us) in alauda_erase_block()
711 usb_stor_dbg(us, "Erasing PBA %d\n", pba); in alauda_erase_block()
713 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_erase_block()
714 command, 9, NULL); in alauda_erase_block()
718 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, in alauda_erase_block()
723 usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]); in alauda_erase_block()
728 * Reads data from a certain offset page inside a PBA, including interleaved
737 PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us) in alauda_read_block_raw()
740 usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages); in alauda_read_block_raw()
742 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_read_block_raw()
743 command, 9, NULL); in alauda_read_block_raw()
747 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, in alauda_read_block_raw()
752 * Reads data from a certain offset page inside a PBA, excluding redundancy
768 for (i = 0; i < pages; i++) { in alauda_read_block()
785 struct alauda_info *info = (struct alauda_info *) us->extra; in alauda_write_block()
788 PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us) in alauda_write_block()
791 usb_stor_dbg(us, "pba %d\n", pba); in alauda_write_block()
793 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_write_block()
794 command, 9, NULL); in alauda_write_block()
798 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data, in alauda_write_block()
808 * Write some data to a specific LBA.
848 result = alauda_read_block_raw(us, pba, 0, in alauda_write_lba()
853 memset(blockbuffer, 0, blocksize * (pagesize + 64)); in alauda_write_lba()
856 lbap = (lba_offset << 1) | 0x1000; in alauda_write_lba()
861 for (i = 0; i < blocksize; i++) { in alauda_write_lba()
866 usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n", in alauda_write_lba()
872 usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n", in alauda_write_lba()
897 new_pba_offset = new_pba - (zone * zonesize); in alauda_write_lba()
900 usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba); in alauda_write_lba()
903 unsigned int pba_offset = pba - (zone * zonesize); in alauda_write_lba()
914 * Read data from a specific sector address
931 * Since we only read in one block at a time, we have to create in alauda_read_data()
932 * a bounce buffer and move the data a piece at a time between the in alauda_read_data()
949 offset = 0; in alauda_read_data()
952 while (sectors > 0) { in alauda_read_data()
954 unsigned int lba_offset = lba - (zone * uzonesize); in alauda_read_data()
968 pages = min(sectors, blocksize - page); in alauda_read_data()
975 usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n", in alauda_read_data()
985 memset(buffer, 0, len); in alauda_read_data()
987 usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n", in alauda_read_data()
996 usb_stor_access_xfer_buf(buffer, len, us->srb, in alauda_read_data()
999 page = 0; in alauda_read_data()
1001 sectors -= pages; in alauda_read_data()
1009 * Write data to a specific sector address
1026 * we have to create a bounce buffer and move the data a piece in alauda_write_data()
1027 * at a time between the bounce buffer and the actual transfer buffer. in alauda_write_data()
1036 * We also need a temporary block buffer, where we read in the old data, in alauda_write_data()
1051 offset = 0; in alauda_write_data()
1054 while (sectors > 0) { in alauda_write_data()
1056 unsigned int pages = min(sectors, blocksize - page); in alauda_write_data()
1068 usb_stor_access_xfer_buf(buffer, len, us->srb, in alauda_write_data()
1076 page = 0; in alauda_write_data()
1078 sectors -= pages; in alauda_write_data()
1098 for (port = 0; port < 2; port++) { in alauda_info_destructor()
1099 struct alauda_media_info *media_info = &info->port[port]; in alauda_info_destructor()
1102 kfree(media_info->lba_to_pba); in alauda_info_destructor()
1103 kfree(media_info->pba_to_lba); in alauda_info_destructor()
1108 * Initialize alauda_info struct and find the data-write endpoint
1113 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting; in init_alauda()
1116 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO); in init_alauda()
1117 if (!us->extra) in init_alauda()
1118 return -ENOMEM; in init_alauda()
1120 info = (struct alauda_info *) us->extra; in init_alauda()
1121 us->extra_destructor = alauda_info_destructor; in init_alauda()
1123 info->wr_ep = usb_sndbulkpipe(us->pusb_dev, in init_alauda()
1124 altsetting->endpoint[0].desc.bEndpointAddress in init_alauda()
1127 return 0; in init_alauda()
1133 struct alauda_info *info = (struct alauda_info *) us->extra; in alauda_transport()
1134 unsigned char *ptr = us->iobuf; in alauda_transport()
1136 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00 in alauda_transport()
1139 if (srb->cmnd[0] == INQUIRY) { in alauda_transport()
1140 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n"); in alauda_transport()
1146 if (srb->cmnd[0] == TEST_UNIT_READY) { in alauda_transport()
1151 if (srb->cmnd[0] == READ_CAPACITY) { in alauda_transport()
1166 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1); in alauda_transport()
1173 if (srb->cmnd[0] == READ_10) { in alauda_transport()
1180 page = short_pack(srb->cmnd[3], srb->cmnd[2]); in alauda_transport()
1182 page |= short_pack(srb->cmnd[5], srb->cmnd[4]); in alauda_transport()
1183 pages = short_pack(srb->cmnd[8], srb->cmnd[7]); in alauda_transport()
1185 usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages); in alauda_transport()
1190 if (srb->cmnd[0] == WRITE_10) { in alauda_transport()
1197 page = short_pack(srb->cmnd[3], srb->cmnd[2]); in alauda_transport()
1199 page |= short_pack(srb->cmnd[5], srb->cmnd[4]); in alauda_transport()
1200 pages = short_pack(srb->cmnd[8], srb->cmnd[7]); in alauda_transport()
1202 usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages); in alauda_transport()
1207 if (srb->cmnd[0] == REQUEST_SENSE) { in alauda_transport()
1210 memset(ptr, 0, 18); in alauda_transport()
1211 ptr[0] = 0xF0; in alauda_transport()
1212 ptr[2] = info->sense_key; in alauda_transport()
1214 ptr[12] = info->sense_asc; in alauda_transport()
1215 ptr[13] = info->sense_ascq; in alauda_transport()
1221 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { in alauda_transport()
1229 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n", in alauda_transport()
1230 srb->cmnd[0], srb->cmnd[0]); in alauda_transport()
1231 info->sense_key = 0x05; in alauda_transport()
1232 info->sense_asc = 0x20; in alauda_transport()
1233 info->sense_ascq = 0x00; in alauda_transport()
1246 (id - alauda_usb_ids) + alauda_unusual_dev_list, in alauda_probe()
1251 us->transport_name = "Alauda Control/Bulk"; in alauda_probe()
1252 us->transport = alauda_transport; in alauda_probe()
1253 us->transport_reset = usb_stor_Bulk_reset; in alauda_probe()
1254 us->max_lun = 1; in alauda_probe()