25 #include <linux/sched.h>
41 int i, unknown_cmd = 0;
43 switch (srb->
cmnd[0]) {
50 case READ_6: what =
"READ_6";
break;
51 case WRITE_6: what =
"WRITE_6";
break;
52 case SEEK_6: what =
"SEEK_6";
break;
55 case SPACE: what =
"SPACE";
break;
56 case INQUIRY: what =
"INQUIRY";
break;
59 case RESERVE: what =
"RESERVE";
break;
60 case RELEASE: what =
"RELEASE";
break;
61 case COPY: what =
"COPY";
break;
62 case ERASE: what =
"ERASE";
break;
70 case READ_10: what =
"READ_10";
break;
71 case WRITE_10: what =
"WRITE_10";
break;
72 case SEEK_10: what =
"SEEK_10";
break;
74 case VERIFY: what =
"VERIFY";
break;
84 case COMPARE: what =
"COMPARE";
break;
89 case READ_LONG: what =
"READ_LONG";
break;
94 case READ_TOC: what =
"READ_TOC";
break;
99 what =
"GET EVENT/STATUS NOTIFICATION";
break;
102 case LOG_SENSE: what =
"LOG_SENSE";
break;
106 what =
"READ TRACK INFORMATION";
break;
111 case 0x59: what =
"READ MASTER CUE";
break;
114 case 0x5C: what =
"READ BUFFER CAPACITY";
break;
115 case 0x5D: what =
"SEND CUE SHEET";
break;
118 case MOVE_MEDIUM: what =
"MOVE_MEDIUM or PLAY AUDIO (12)";
break;
119 case READ_12: what =
"READ_12";
break;
120 case WRITE_12: what =
"WRITE_12";
break;
132 case 0xE1: what =
"WRITE CONTINUE";
break;
134 case VENDOR_CMND: what =
"Realtek's vendor command";
break;
135 default: what =
"(unknown command)"; unknown_cmd = 1;
break;
143 for (i = 0; i < srb->
cmd_len && i < 16; i++)
151 switch (sense_type) {
193 #ifdef SUPPORT_MAGIC_GATE
211 #ifdef SUPPORT_SD_LOCK
231 sense->
info[0] = (
u8)(info >> 24);
232 sense->
info[1] = (
u8)(info >> 16);
233 sense->
info[2] = (
u8)(info >> 8);
234 sense->
info[3] = (
u8)info;
239 if (sns_key_info0 != 0) {
261 #ifdef SUPPORT_SD_LOCK
279 'M',
'E',
'M',
'O',
'R',
'Y',
'S',
'T',
'I',
'C',
'K',
280 #ifdef SUPPORT_MAGIC_GATE
286 #ifdef SUPPORT_MAGIC_GATE
299 char *inquiry_default = (
char *)
"Generic-xD/SD/M.S. 1.00 ";
300 char *inquiry_sdms = (
char *)
"Generic-SD/MemoryStick 1.00 ";
301 char *inquiry_sd = (
char *)
"Generic-SD/MMC 1.00 ";
302 char *inquiry_ms = (
char *)
"Generic-MemoryStick 1.00 ";
303 char *inquiry_string;
304 unsigned char sendbytes;
307 int pro_formatter_flag = 0;
308 unsigned char inquiry_buf[] = {
321 inquiry_string = inquiry_sd;
323 inquiry_string = inquiry_ms;
326 inquiry_string = inquiry_sdms;
328 inquiry_string = inquiry_default;
331 buf =
vmalloc(scsi_bufflen(srb));
335 #ifdef SUPPORT_MAGIC_GATE
342 if (!card || (card == MS_CARD))
343 pro_formatter_flag = 1;
346 if (pro_formatter_flag) {
347 if (scsi_bufflen(srb) < 56)
348 sendbytes = (
unsigned char)(scsi_bufflen(srb));
353 if (scsi_bufflen(srb) < 36)
354 sendbytes = (
unsigned char)(scsi_bufflen(srb));
360 memcpy(buf, inquiry_buf, 8);
361 memcpy(buf + 8, inquiry_string, sendbytes - 8);
362 if (pro_formatter_flag) {
367 memcpy(buf, inquiry_buf, sendbytes);
370 if (pro_formatter_flag) {
372 memcpy(buf + 36, formatter_inquiry_str, sendbytes - 36);
375 scsi_set_resid(srb, 0);
388 scsi_set_resid(srb, scsi_bufflen(srb));
390 if (srb->
cmnd[1] == 1)
393 switch (srb->
cmnd[0x4]) {
424 prevent = srb->
cmnd[4] & 0x1;
426 scsi_set_resid(srb, 0);
465 buf =
vmalloc(scsi_bufflen(srb));
469 tmp = (
unsigned char *)sense;
470 memcpy(buf, tmp, scsi_bufflen(srb));
475 scsi_set_resid(srb, 0);
482 int lun,
u8 *buf,
int buf_len)
487 int support_format = 0;
492 if (data_size > 0x68)
497 sys_info_offset = 12;
498 if (data_size > 0x6C)
542 if (data_size >= 12) {
556 if (data_size >= 8) {
564 if (data_size > sys_info_offset) {
566 int len = data_size - sys_info_offset;
567 len = (len < 96) ? len : 96;
576 unsigned int dataSize;
578 int pro_formatter_flag;
579 unsigned char pageCode, *
buf;
582 #ifndef SUPPORT_MAGIC_GATE
585 scsi_set_resid(srb, scsi_bufflen(srb));
590 pro_formatter_flag = 0;
592 #ifdef SUPPORT_MAGIC_GATE
593 if ((chip->
lun2card[lun] & MS_CARD)) {
594 if (!card || (card == MS_CARD)) {
597 pro_formatter_flag = 1;
601 if (card == MS_CARD) {
603 pro_formatter_flag = 1;
613 pageCode = srb->
cmnd[2] & 0x3f;
615 if ((pageCode == 0x3F) || (pageCode == 0x1C) ||
616 (pageCode == 0x00) ||
617 (pro_formatter_flag && (pageCode == 0x20))) {
619 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
620 ms_mode_sense(chip, srb->
cmnd[0],
634 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
635 ms_mode_sense(chip, srb->
cmnd[0],
655 scsi_set_resid(srb, scsi_bufflen(srb));
660 unsigned int len =
min(scsi_bufflen(srb), dataSize);
662 scsi_set_resid(srb, scsi_bufflen(srb) - len);
671 #ifdef SUPPORT_SD_LOCK
698 #ifdef SUPPORT_SD_LOCK
718 start_sec = ((
u32)srb->
cmnd[2] << 24) | ((
u32)srb->
cmnd[3] << 16) |
720 sec_cnt = ((
u16)(srb->
cmnd[7]) << 8) | srb->
cmnd[8];
722 start_sec = ((
u32)(srb->
cmnd[1] & 0x1F) << 16) |
724 sec_cnt = srb->
cmnd[4];
727 start_sec = ((
u32)srb->
cmnd[4] << 24) | ((
u32)srb->
cmnd[5] << 16) |
729 sec_cnt = ((
u16)(srb->
cmnd[9]) << 8) | srb->
cmnd[10];
739 if ((start_sec > get_card_size(chip, lun)) ||
740 ((start_sec + sec_cnt) > get_card_size(chip, lun))) {
746 scsi_set_resid(srb, 0);
751 RTSX_DEBUGP(
"read/write fail three times in succession\n");
774 retval =
card_rw(srb, chip, start_sec, sec_cnt);
793 scsi_set_resid(srb, 0);
811 unsigned int buf_len;
824 buf_len = (scsi_bufflen(srb) > 12) ? 0x14 : 12;
837 (!card || (card == MS_CARD))) {
847 card_size = get_card_size(chip, lun);
848 buf[i++] = (
unsigned char)(card_size >> 24);
849 buf[i++] = (
unsigned char)(card_size >> 16);
850 buf[i++] = (
unsigned char)(card_size >> 8);
851 buf[i++] = (
unsigned char)card_size;
876 buf_len =
min(scsi_bufflen(srb), buf_len);
880 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
906 card_size = get_card_size(chip, lun);
907 buf[0] = (
unsigned char)((card_size - 1) >> 24);
908 buf[1] = (
unsigned char)((card_size - 1) >> 16);
909 buf[2] = (
unsigned char)((card_size - 1) >> 8);
910 buf[3] = (
unsigned char)(card_size - 1);
920 scsi_set_resid(srb, 0);
927 unsigned short len,
i;
952 for (i = 0; i < len; i++) {
961 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
963 scsi_set_resid(srb, scsi_bufflen(srb) - len);
972 unsigned short len,
i;
999 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1005 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1007 for (i = 0; i < len; i++) {
1024 unsigned short addr, len,
i;
1039 if (addr < 0xFC00) {
1055 for (i = 0; i < len; i++) {
1064 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1066 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1075 unsigned short addr, len,
i;
1090 if (addr < 0xFC00) {
1095 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1101 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1110 for (i = 0; i < len; i++) {
1139 scsi_set_resid(srb, 0);
1167 unsigned char *
ptr, *buf =
NULL;
1170 unsigned int buf_len;
1174 if ((scsi_bufflen(srb) < buf_len) || (scsi_sglist(srb) ==
NULL)) {
1179 clear = srb->
cmnd[2];
1181 buf = (
unsigned char *)
vmalloc(scsi_bufflen(srb));
1191 *(ptr++) = (
u8)(msg_cnt >> 24);
1192 *(ptr++) = (
u8)(msg_cnt >> 16);
1193 *(ptr++) = (
u8)(msg_cnt >> 8);
1194 *(ptr++) = (
u8)msg_cnt;
1195 RTSX_DEBUGP(
"Trace message count is %d\n", msg_cnt);
1197 for (i = 1; i <= msg_cnt; i++) {
1225 scsi_set_resid(srb, 0);
1244 addr = srb->
cmnd[4];
1247 RTSX_DEBUGP(
"Host register (0x%x): 0x%x\n", addr, val);
1249 buf[0] = (
u8)(val >> 24);
1250 buf[1] = (
u8)(val >> 16);
1251 buf[2] = (
u8)(val >> 8);
1254 len =
min(scsi_bufflen(srb), (
unsigned int)4);
1256 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1275 addr = srb->
cmnd[4];
1277 len =
min(scsi_bufflen(srb), (
unsigned int)4);
1279 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1281 val = ((
u32)buf[0] << 24) | ((
u32)buf[1] << 16) | ((
u32)buf[2] << 8) | buf[3];
1292 if (srb->
cmnd[3] == 1) {
1298 switch (srb->
cmnd[4]) {
1315 }
else if (srb->
cmnd[3] == 2) {
1351 if (srb->
cmnd[3] == 1) {
1357 switch (srb->
cmnd[4]) {
1376 }
else if (srb->
cmnd[3] == 2) {
1402 len =
min(len, (
u16)scsi_bufflen(srb));
1419 scsi_set_resid(srb, 0);
1433 u8 oc_now_mask = 0, oc_ever_mask = 0;
1477 status[0x0E] = 0x02;
1479 status[0x0E] = 0x01;
1481 status[0x0E] = 0x00;
1485 status[0x0F] = 0x03;
1487 status[0x0F] = 0x04;
1489 status[0x0F] = 0x02;
1491 status[0x0F] = 0x01;
1493 status[0x0F] = 0x00;
1496 status[0x0E] = 0x01;
1498 status[0x0E] = 0x00;
1501 status[0x0F] = 0x03;
1503 status[0x0F] = 0x02;
1505 status[0x0F] = 0x01;
1507 status[0x0F] = 0x00;
1509 }
else if (card == MS_CARD) {
1512 status[0x0E] = 0x01;
1514 status[0x0E] = 0x00;
1517 status[0x0F] = 0x01;
1519 status[0x0F] = 0x00;
1523 #ifdef SUPPORT_SD_LOCK
1525 status[0x17] = 0x80;
1527 status[0x17] |= 0x01;
1529 status[0x17] |= 0x02;
1530 status[0x07] |= 0x40;
1533 status[0x17] |= 0x04;
1535 status[0x17] = 0x00;
1538 RTSX_DEBUGP(
"status[0x17] = 0x%x\n", status[0x17]);
1541 status[0x18] = 0x8A;
1542 status[0x1A] = 0x28;
1543 #ifdef SUPPORT_SD_LOCK
1544 status[0x1F] = 0x01;
1547 buf_len =
min(scsi_bufflen(srb), (
unsigned int)
sizeof(status));
1549 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
1565 phy_debug_mode = (
int)(srb->
cmnd[3]);
1567 if (phy_debug_mode) {
1619 switch (srb->
cmnd[3]) {
1625 cmd_type = srb->
cmnd[4];
1630 addr = (srb->
cmnd[5] << 8) | srb->
cmnd[6];
1631 mask = srb->
cmnd[7];
1632 value = srb->
cmnd[8];
1643 if (scsi_bufflen(srb) < 1) {
1648 scsi_set_resid(srb, 0);
1668 switch (srb->
cmnd[3]) {
1673 result = rw_mem_cmd_buf(srb, chip);
1684 unsigned short addr, len,
i;
1715 for (i = 0; i < len / 2; i++) {
1723 buf[2*
i] = (
u8)(val >> 8);
1724 buf[2*i+1] = (
u8)val;
1727 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1729 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1739 unsigned short addr, len,
i;
1759 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1766 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1775 for (i = 0; i < len / 2; i++) {
1776 val = ((
u16)buf[2*i] << 8) | buf[2*i+1];
1793 unsigned short addr;
1811 mode = srb->
cmnd[3];
1820 }
else if (mode == 1) {
1836 unsigned short addr, len,
i;
1862 for (i = 0; i < len; i++) {
1871 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1873 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1882 unsigned short addr, len,
i;
1897 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1903 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1912 for (i = 0; i < len; i++) {
1940 addr = srb->
cmnd[4];
1954 for (i = 0; i < len; i++) {
1963 len = (
u8)
min(scsi_bufflen(srb), (
unsigned int)len);
1965 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1987 addr = srb->
cmnd[4];
1990 len = (
u8)
min(scsi_bufflen(srb), (
unsigned int)len);
1996 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2042 for (i = 0; i < len; i++) {
2092 func = srb->
cmnd[3];
2096 RTSX_DEBUGP(
"%s: func = %d, addr = 0x%x, len = %d\n", __func__, func, addr, len);
2103 if (func > func_max) {
2119 len = (
u16)
min(scsi_bufflen(srb), (
unsigned int)len);
2121 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2143 func = srb->
cmnd[3];
2147 RTSX_DEBUGP(
"%s: func = %d, addr = 0x%x\n", __func__, func, addr);
2154 if (func > func_max) {
2159 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
2165 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2183 switch (srb->
cmnd[2]) {
2186 result = read_write(srb, chip);
2190 result = read_host_reg(srb, chip);
2194 result = write_host_reg(srb, chip);
2198 result = get_variable(srb, chip);
2202 result = set_variable(srb, chip);
2207 result = dma_access_ring_buffer(srb, chip);
2211 result = read_phy_register(srb, chip);
2215 result = write_phy_register(srb, chip);
2219 result = erase_eeprom2(srb, chip);
2223 result = read_eeprom2(srb, chip);
2227 result = write_eeprom2(srb, chip);
2235 result = write_efuse(srb, chip);
2239 result = read_cfg_byte(srb, chip);
2243 result = write_cfg_byte(srb, chip);
2247 result = set_chip_mode(srb, chip);
2251 result = suit_cmd(srb, chip);
2255 result = get_dev_status(srb, chip);
2279 rtsx_status[4] = (
u8)lun;
2315 rtsx_status[10] = 0;
2317 rtsx_status[10] = 1;
2329 rtsx_status[12] = 1;
2331 rtsx_status[12] = 0;
2334 rtsx_status[13] = 0x40;
2338 rtsx_status[13] = 0x20;
2341 rtsx_status[13] |= 0x04;
2343 rtsx_status[13] |= 0x02;
2345 rtsx_status[13] |= 0x08;
2347 rtsx_status[13] |= 0x02;
2349 rtsx_status[13] |= 0x04;
2355 rtsx_status[13] = 0x38;
2357 rtsx_status[13] |= 0x04;
2360 rtsx_status[13] |= 0x01;
2363 rtsx_status[13] = 0x30;
2369 rtsx_status[13] = 0x60;
2371 rtsx_status[13] = 0x70;
2373 rtsx_status[13] = 0x70;
2377 rtsx_status[13] = 0x20;
2379 rtsx_status[13] = 0x30;
2383 rtsx_status[14] = 0x78;
2385 rtsx_status[15] = 0x83;
2387 rtsx_status[15] = 0x82;
2389 buf_len =
min(scsi_bufflen(srb), (
unsigned int)
sizeof(rtsx_status));
2391 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
2407 if ((card ==
SD_CARD) || (card == MS_CARD)) {
2414 scsi_set_resid(srb, 0);
2444 switch (srb->
cmnd[2]) {
2492 switch (srb->
cmnd[1]) {
2494 result = read_status(srb, chip);
2498 result = read_mem(srb, chip);
2502 result = write_mem(srb, chip);
2510 result = write_eeprom(srb, chip);
2514 result = toggle_gpio_cmd(srb, chip);
2518 result = get_sd_csd(srb, chip);
2522 result = get_card_bus_width(srb, chip);
2527 result = trace_msg_cmd(srb, chip);
2532 result = app_cmd(srb, chip);
2536 result = spi_vendor_cmd(srb, chip);
2547 #if !defined(LED_AUTO_BLINK) && !defined(REGULAR_BLINK)
2554 sec_cnt = ((
u16)(srb->
cmnd[7]) << 8) | srb->
cmnd[8];
2556 sec_cnt = srb->
cmnd[4];
2573 int retval, quick_format;
2580 if ((srb->
cmnd[3] != 0x4D) || (srb->
cmnd[4] != 0x47) ||
2581 (srb->
cmnd[5] != 0x66) || (srb->
cmnd[6] != 0x6D) ||
2582 (srb->
cmnd[7] != 0x74)) {
2594 (get_card_size(chip, lun) == 0)) {
2601 if (srb->
cmnd[8] & 0x01)
2611 if (chip->
card_wp & MS_CARD) {
2627 scsi_set_resid(srb, 0);
2631 #ifdef SUPPORT_PCGL_1P18
2638 unsigned int buf_len;
2650 if ((srb->
cmnd[2] != 0xB0) || (srb->
cmnd[4] != 0x4D) ||
2651 (srb->
cmnd[5] != 0x53) || (srb->
cmnd[6] != 0x49) ||
2652 (srb->
cmnd[7] != 0x44)) {
2657 dev_info_id = srb->
cmnd[3];
2658 if ((
CHK_MSXC(ms_card) && (dev_info_id == 0x10)) ||
2659 (!
CHK_MSXC(ms_card) && (dev_info_id == 0x13)) ||
2665 if (dev_info_id == 0x15)
2666 buf_len = data_len = 0x3A;
2668 buf_len = data_len = 0x6A;
2696 buf[i++] = dev_info_id;
2698 if (dev_info_id == 0x15)
2707 if ((dev_info_id == 0x10) || (dev_info_id == 0x13)) {
2717 if (dev_info_id == 0x15)
2718 scsi_set_resid(srb, scsi_bufflen(srb)-0x3C);
2720 scsi_set_resid(srb, scsi_bufflen(srb)-0x6C);
2732 retval = ms_format_cmnd(srb, chip);
2733 #ifdef SUPPORT_PCGL_1P18
2735 retval = get_ms_information(srb, chip);
2766 switch (srb->
cmnd[0]) {
2800 #ifdef SUPPORT_MAGIC_GATE
2839 key_format = srb->
cmnd[10] & 0x3F;
2842 switch (key_format) {
2844 if ((scsi_bufflen(srb) == 0x41C) &&
2845 (srb->
cmnd[8] == 0x04) &&
2846 (srb->
cmnd[9] == 0x1C)) {
2858 if ((scsi_bufflen(srb) == 0x24) &&
2859 (srb->
cmnd[8] == 0x00) &&
2860 (srb->
cmnd[9] == 0x24)) {
2873 if ((scsi_bufflen(srb) == 0x404) &&
2874 (srb->
cmnd[8] == 0x04) &&
2875 (srb->
cmnd[9] == 0x04) &&
2876 (srb->
cmnd[2] == 0x00) &&
2877 (srb->
cmnd[3] == 0x00) &&
2878 (srb->
cmnd[4] == 0x00) &&
2879 (srb->
cmnd[5] < 32)) {
2895 scsi_set_resid(srb, 0);
2941 key_format = srb->
cmnd[10] & 0x3F;
2944 switch (key_format) {
2946 if ((scsi_bufflen(srb) == 0x0C) &&
2947 (srb->
cmnd[8] == 0x00) &&
2948 (srb->
cmnd[9] == 0x0C)) {
2960 if ((scsi_bufflen(srb) == 0x0C) &&
2961 (srb->
cmnd[8] == 0x00) &&
2962 (srb->
cmnd[9] == 0x0C)) {
2963 retval =
mg_chg(srb, chip);
2974 if ((scsi_bufflen(srb) == 0x0C) &&
2975 (srb->
cmnd[8] == 0x00) &&
2976 (srb->
cmnd[9] == 0x0C)) {
2977 retval =
mg_rsp(srb, chip);
2989 if ((scsi_bufflen(srb) == 0x404) &&
2990 (srb->
cmnd[8] == 0x04) &&
2991 (srb->
cmnd[9] == 0x04) &&
2992 (srb->
cmnd[2] == 0x00) &&
2993 (srb->
cmnd[3] == 0x00) &&
2994 (srb->
cmnd[4] == 0x00) &&
2995 (srb->
cmnd[5] < 32)) {
3011 scsi_set_resid(srb, 0);
3018 #ifdef SUPPORT_SD_LOCK
3025 #ifdef SUPPORT_SD_LOCK
3036 0x02, 0, 0x04, 0x04, 0, 0);
3052 switch (srb->
cmnd[0]) {
3057 result = read_write(srb, chip);
3058 #if !defined(LED_AUTO_BLINK) && !defined(REGULAR_BLINK)
3059 led_shine(srb, chip);
3064 result = test_unit_ready(srb, chip);
3072 result = read_capacity(srb, chip);
3076 result = start_stop_unit(srb, chip);
3080 result = allow_medium_removal(srb, chip);
3089 result = mode_sense(srb, chip);
3093 result = read_format_capacity(srb, chip);
3097 result = vendor_cmnd(srb, chip);
3101 result = ms_sp_cmnd(srb, chip);
3111 result = sd_extention_cmnd(srb, chip);
3115 #ifdef SUPPORT_MAGIC_GATE
3117 result = mg_report_key(srb, chip);
3121 result = mg_send_key(srb, chip);