Lines Matching +full:1 +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");
63 * Bulk opcodes (byte 1)
114 #define MEDIA_PORT(us) us->srb->device->lun
115 #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
169 unsigned char chipshift; /* 1<<cs bytes total capacity */
170 unsigned char pageshift; /* 1<<ps bytes in a page */
171 unsigned char blockshift; /* 1<<bs pages per block */
172 unsigned char zoneshift; /* 1<<zs blocks per zone */
177 { 0x6e, 20, 8, 4, 8}, /* 1 MB */
178 { 0xe8, 20, 8, 4, 8}, /* 1 MB */
179 { 0xec, 20, 8, 4, 8}, /* 1 MB */
180 { 0x64, 21, 8, 4, 9}, /* 2 MB */
181 { 0xea, 21, 8, 4, 9}, /* 2 MB */
182 { 0x6b, 22, 9, 4, 9}, /* 4 MB */
183 { 0xe3, 22, 9, 4, 9}, /* 4 MB */
184 { 0xe5, 22, 9, 4, 9}, /* 4 MB */
185 { 0xe6, 23, 9, 4, 10}, /* 8 MB */
186 { 0x73, 24, 9, 5, 10}, /* 16 MB */
187 { 0x75, 25, 9, 5, 10}, /* 32 MB */
188 { 0x76, 26, 9, 5, 10}, /* 64 MB */
189 { 0x79, 27, 9, 5, 10}, /* 128 MB */
190 { 0x71, 28, 9, 5, 10}, /* 256 MB */
193 { 0x5d, 21, 9, 4, 8}, /* 2 MB */
194 { 0xd5, 22, 9, 4, 9}, /* 4 MB */
195 { 0xd6, 23, 9, 4, 10}, /* 8 MB */
196 { 0x57, 24, 9, 4, 11}, /* 16 MB */
197 { 0x58, 25, 9, 4, 12}, /* 32 MB */
220 int i, j, a; in nand_init_ecc() local
223 for (i = 1; i < 256; i++) in nand_init_ecc()
224 parity[i] = (parity[i&(i-1)] ^ 1); in nand_init_ecc()
227 a = 0; in nand_init_ecc()
229 if (i & (1<<j)) { in nand_init_ecc()
230 if ((j & 1) == 0) in nand_init_ecc()
231 a ^= 0x04; in nand_init_ecc()
233 a ^= 0x10; in nand_init_ecc()
235 a ^= 0x40; in nand_init_ecc()
238 ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0)); in nand_init_ecc()
242 /* compute 3-byte ecc on 256 bytes */
245 int i, j, a; in nand_compute_ecc() local
253 if ((i & (1<<j)) == 0) in nand_compute_ecc()
258 a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0]; in nand_compute_ecc()
259 ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); in nand_compute_ecc()
261 a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4]; in nand_compute_ecc()
262 ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0)); in nand_compute_ecc()
269 return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]); in nand_compare_ecc()
282 * Forget our PBA <---> LBA mappings for a particular port
286 unsigned int shift = media_info->zoneshift in alauda_free_maps()
287 + media_info->blockshift + media_info->pageshift; in alauda_free_maps()
288 unsigned int num_zones = media_info->capacity >> shift; in alauda_free_maps()
291 if (media_info->lba_to_pba != NULL) in alauda_free_maps()
293 kfree(media_info->lba_to_pba[i]); in alauda_free_maps()
294 media_info->lba_to_pba[i] = NULL; in alauda_free_maps()
297 if (media_info->pba_to_lba != NULL) in alauda_free_maps()
299 kfree(media_info->pba_to_lba[i]); in alauda_free_maps()
300 media_info->pba_to_lba[i] = NULL; in alauda_free_maps()
318 rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, in alauda_get_media_status()
319 command, 0xc0, 0, 1, data, 2); in alauda_get_media_status()
322 usb_stor_dbg(us, "Media status %02X %02X\n", data[0], data[1]); in alauda_get_media_status()
340 return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe, in alauda_ack_media()
341 command, 0x40, 0, 1, NULL, 0); in alauda_ack_media()
345 * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
357 return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe, in alauda_get_media_signature()
366 unsigned char *command = us->iobuf; in alauda_reset_media()
368 memset(command, 0, 9); in alauda_reset_media()
370 command[1] = ALAUDA_BULK_RESET_MEDIA; in alauda_reset_media()
373 return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_reset_media()
374 command, 9, NULL); in alauda_reset_media()
382 unsigned char *data = us->iobuf; in alauda_init_media()
394 ready = 1; in alauda_init_media()
416 media_info = alauda_card_find_id(data[1]); in alauda_init_media()
423 MEDIA_INFO(us).capacity = 1 << media_info->chipshift; in alauda_init_media()
427 MEDIA_INFO(us).pageshift = media_info->pageshift; in alauda_init_media()
428 MEDIA_INFO(us).blockshift = media_info->blockshift; in alauda_init_media()
429 MEDIA_INFO(us).zoneshift = media_info->zoneshift; in alauda_init_media()
431 MEDIA_INFO(us).pagesize = 1 << media_info->pageshift; in alauda_init_media()
432 MEDIA_INFO(us).blocksize = 1 << media_info->blockshift; in alauda_init_media()
433 MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift; in alauda_init_media()
435 MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125; in alauda_init_media()
436 MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1; in alauda_init_media()
457 struct alauda_info *info = (struct alauda_info *) us->extra; in alauda_check_media()
458 unsigned char *status = us->iobuf; in alauda_check_media()
464 status[1] = 0; in alauda_check_media()
469 || ((status[1] & 0x01) == 0)) { in alauda_check_media()
472 info->sense_key = 0x02; in alauda_check_media()
473 info->sense_asc = 0x3A; in alauda_check_media()
474 info->sense_ascq = 0x00; in alauda_check_media()
484 info->sense_key = UNIT_ATTENTION; in alauda_check_media()
485 info->sense_asc = 0x28; in alauda_check_media()
486 info->sense_ascq = 0x00; in alauda_check_media()
506 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_check_status2()
507 command, 9, NULL); in alauda_check_status2()
511 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, in alauda_check_status2()
524 * Gets the redundancy data for the first page of a PBA
535 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_get_redu_data()
536 command, 9, NULL); in alauda_get_redu_data()
540 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, in alauda_get_redu_data()
545 * Finds the first unused PBA in a zone
551 u16 *pba_to_lba = info->pba_to_lba[zone]; in alauda_find_unused_pba()
554 for (i = 0; i < info->zonesize; i++) in alauda_find_unused_pba()
556 return (zone << info->zoneshift) + i; in alauda_find_unused_pba()
562 * Reads the redundancy data for all PBA's in a zone
563 * Produces lba <--> pba mappings
567 unsigned char *data = us->iobuf; in alauda_read_map()
582 usb_stor_dbg(us, "Mapping blocks for zone %d\n", zone); in alauda_read_map()
602 usb_stor_dbg(us, "PBA %d has no logical mapping\n", blocknum); in alauda_read_map()
615 …usb_stor_dbg(us, "PBA %d has no logical mapping: reserved area = %02X%02X%02X%02X data status %02X… in alauda_read_map()
617 data[0], data[1], data[2], data[3], in alauda_read_map()
624 usb_stor_dbg(us, "PBA %d has invalid address field %02X%02X/%02X%02X\n", in alauda_read_map()
634 "alauda_read_map: Bad parity in LBA for block %d" in alauda_read_map()
641 lba_offset = (lba_offset & 0x07FF) >> 1; in alauda_read_map()
646 * go back to zero, but are within a higher block of LBA's. in alauda_read_map()
647 * Also, there is a maximum of 1000 LBA's per zone. in alauda_read_map()
648 * In other words, in PBA 1024-2047 you will find LBA 0-999 in alauda_read_map()
649 * which are really LBA 1000-1999. This allows for 24 bad in alauda_read_map()
655 "alauda_read_map: Bad low LBA %d for block %d\n", in alauda_read_map()
663 "LBA %d seen for PBA %d and %d\n", in alauda_read_map()
686 * Checks to see whether we have already mapped a certain zone
708 usb_stor_dbg(us, "Erasing PBA %d\n", pba); in alauda_erase_block()
710 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_erase_block()
711 command, 9, NULL); in alauda_erase_block()
715 rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, in alauda_erase_block()
720 usb_stor_dbg(us, "Erase result: %02X %02X\n", buf[0], buf[1]); in alauda_erase_block()
725 * Reads data from a certain offset page inside a PBA, including interleaved
737 usb_stor_dbg(us, "pba %d page %d count %d\n", pba, page, pages); in alauda_read_block_raw()
739 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_read_block_raw()
740 command, 9, NULL); in alauda_read_block_raw()
744 return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe, in alauda_read_block_raw()
749 * Reads data from a certain offset page inside a PBA, excluding redundancy
782 struct alauda_info *info = (struct alauda_info *) us->extra; in alauda_write_block()
788 usb_stor_dbg(us, "pba %d\n", pba); in alauda_write_block()
790 rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe, in alauda_write_block()
791 command, 9, NULL); in alauda_write_block()
795 rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data, in alauda_write_block()
805 * Write some data to a specific LBA.
826 if (pba == 1) { in alauda_write_lba()
828 * Maybe it is impossible to write to PBA 1. in alauda_write_lba()
832 "alauda_write_lba: avoid writing to pba 1\n"); in alauda_write_lba()
853 lbap = (lba_offset << 1) | 0x1000; in alauda_write_lba()
855 lbap ^= 1; in alauda_write_lba()
863 usb_stor_dbg(us, "Warning: bad ecc in page %d- of pba %d\n", in alauda_write_lba()
869 usb_stor_dbg(us, "Warning: bad ecc in page %d+ of pba %d\n", in alauda_write_lba()
894 new_pba_offset = new_pba - (zone * zonesize); in alauda_write_lba()
897 usb_stor_dbg(us, "Remapped LBA %d to PBA %d\n", lba, new_pba); in alauda_write_lba()
900 unsigned int pba_offset = pba - (zone * zonesize); in alauda_write_lba()
911 * Read data from a specific sector address
928 * Since we only read in one block at a time, we have to create in alauda_read_data()
929 * a bounce buffer and move the data a piece at a time between the in alauda_read_data()
951 unsigned int lba_offset = lba - (zone * uzonesize); in alauda_read_data()
965 pages = min(sectors, blocksize - page); in alauda_read_data()
972 usb_stor_dbg(us, "Read %d zero pages (LBA %d) page %d\n", in alauda_read_data()
984 usb_stor_dbg(us, "Read %d pages, from PBA %d (LBA %d) page %d\n", in alauda_read_data()
993 usb_stor_access_xfer_buf(buffer, len, us->srb, in alauda_read_data()
998 sectors -= pages; in alauda_read_data()
1006 * Write data to a specific sector address
1023 * we have to create a bounce buffer and move the data a piece in alauda_write_data()
1024 * at a time between the bounce buffer and the actual transfer buffer. in alauda_write_data()
1033 * We also need a temporary block buffer, where we read in the old data, in alauda_write_data()
1053 unsigned int pages = min(sectors, blocksize - page); in alauda_write_data()
1065 usb_stor_access_xfer_buf(buffer, len, us->srb, in alauda_write_data()
1075 sectors -= pages; in alauda_write_data()
1096 struct alauda_media_info *media_info = &info->port[port]; in alauda_info_destructor()
1099 kfree(media_info->lba_to_pba); in alauda_info_destructor()
1100 kfree(media_info->pba_to_lba); in alauda_info_destructor()
1105 * Initialize alauda_info struct and find the data-write endpoint
1110 struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting; in init_alauda()
1113 us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO); in init_alauda()
1114 if (!us->extra) in init_alauda()
1115 return -ENOMEM; in init_alauda()
1117 info = (struct alauda_info *) us->extra; in init_alauda()
1118 us->extra_destructor = alauda_info_destructor; in init_alauda()
1120 info->wr_ep = usb_sndbulkpipe(us->pusb_dev, in init_alauda()
1121 altsetting->endpoint[0].desc.bEndpointAddress in init_alauda()
1130 struct alauda_info *info = (struct alauda_info *) us->extra; in alauda_transport()
1131 unsigned char *ptr = us->iobuf; in alauda_transport()
1136 if (srb->cmnd[0] == INQUIRY) { in alauda_transport()
1137 usb_stor_dbg(us, "INQUIRY - Returning bogus response\n"); in alauda_transport()
1143 if (srb->cmnd[0] == TEST_UNIT_READY) { in alauda_transport()
1148 if (srb->cmnd[0] == READ_CAPACITY) { in alauda_transport()
1163 ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1); in alauda_transport()
1164 ((__be32 *) ptr)[1] = cpu_to_be32(512); in alauda_transport()
1170 if (srb->cmnd[0] == READ_10) { in alauda_transport()
1177 page = short_pack(srb->cmnd[3], srb->cmnd[2]); in alauda_transport()
1179 page |= short_pack(srb->cmnd[5], srb->cmnd[4]); in alauda_transport()
1180 pages = short_pack(srb->cmnd[8], srb->cmnd[7]); in alauda_transport()
1182 usb_stor_dbg(us, "READ_10: page %d pagect %d\n", page, pages); in alauda_transport()
1187 if (srb->cmnd[0] == WRITE_10) { in alauda_transport()
1194 page = short_pack(srb->cmnd[3], srb->cmnd[2]); in alauda_transport()
1196 page |= short_pack(srb->cmnd[5], srb->cmnd[4]); in alauda_transport()
1197 pages = short_pack(srb->cmnd[8], srb->cmnd[7]); in alauda_transport()
1199 usb_stor_dbg(us, "WRITE_10: page %d pagect %d\n", page, pages); in alauda_transport()
1204 if (srb->cmnd[0] == REQUEST_SENSE) { in alauda_transport()
1209 ptr[2] = info->sense_key; in alauda_transport()
1211 ptr[12] = info->sense_asc; in alauda_transport()
1212 ptr[13] = info->sense_ascq; in alauda_transport()
1218 if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) { in alauda_transport()
1226 usb_stor_dbg(us, "Gah! Unknown command: %d (0x%x)\n", in alauda_transport()
1227 srb->cmnd[0], srb->cmnd[0]); in alauda_transport()
1228 info->sense_key = 0x05; in alauda_transport()
1229 info->sense_asc = 0x20; in alauda_transport()
1230 info->sense_ascq = 0x00; in alauda_transport()
1243 (id - alauda_usb_ids) + alauda_unusual_dev_list, in alauda_probe()
1248 us->transport_name = "Alauda Control/Bulk"; in alauda_probe()
1249 us->transport = alauda_transport; in alauda_probe()
1250 us->transport_reset = usb_stor_Bulk_reset; in alauda_probe()
1251 us->max_lun = 1; in alauda_probe()
1267 .soft_unbind = 1,
1268 .no_dynamic_id = 1,