28 #include <linux/sched.h>
29 #include <linux/slab.h>
31 #include <linux/export.h>
33 #include <scsi/scsi.h>
50 int i, unknown_cmd = 0;
52 switch (srb->
cmnd[0]) {
54 what = (
char *)
"TEST_UNIT_READY";
57 what = (
char *)
"REZERO_UNIT";
60 what = (
char *)
"REQUEST_SENSE";
63 what = (
char *)
"FORMAT_UNIT";
66 what = (
char *)
"READ_BLOCK_LIMITS";
69 what = (
char *)
"REASSIGN_BLOCKS";
72 what = (
char *)
"READ_6";
75 what = (
char *)
"WRITE_6";
78 what = (
char *)
"SEEK_6";
81 what = (
char *)
"READ_REVERSE";
84 what = (
char *)
"WRITE_FILEMARKS";
87 what = (
char *)
"SPACE";
90 what = (
char *)
"INQUIRY";
93 what = (
char *)
"RECOVER_BUFFERED_DATA";
96 what = (
char *)
"MODE_SELECT";
99 what = (
char *)
"RESERVE";
102 what = (
char *)
"RELEASE";
105 what = (
char *)
"COPY";
108 what = (
char *)
"ERASE";
111 what = (
char *)
"MODE_SENSE";
114 what = (
char *)
"START_STOP";
117 what = (
char *)
"RECEIVE_DIAGNOSTIC";
120 what = (
char *)
"SEND_DIAGNOSTIC";
123 what = (
char *)
"ALLOW_MEDIUM_REMOVAL";
126 what = (
char *)
"SET_WINDOW";
129 what = (
char *)
"READ_CAPACITY";
132 what = (
char *)
"READ_10";
135 what = (
char *)
"WRITE_10";
138 what = (
char *)
"SEEK_10";
141 what = (
char *)
"WRITE_VERIFY";
144 what = (
char *)
"VERIFY";
147 what = (
char *)
"SEARCH_HIGH";
150 what = (
char *)
"SEARCH_EQUAL";
153 what = (
char *)
"SEARCH_LOW";
156 what = (
char *)
"SET_LIMITS";
159 what = (
char *)
"READ_POSITION";
162 what = (
char *)
"SYNCHRONIZE_CACHE";
165 what = (
char *)
"LOCK_UNLOCK_CACHE";
168 what = (
char *)
"READ_DEFECT_DATA";
171 what = (
char *)
"MEDIUM_SCAN";
174 what = (
char *)
"COMPARE";
177 what = (
char *)
"COPY_VERIFY";
180 what = (
char *)
"WRITE_BUFFER";
183 what = (
char *)
"READ_BUFFER";
186 what = (
char *)
"UPDATE_BLOCK";
189 what = (
char *)
"READ_LONG";
192 what = (
char *)
"WRITE_LONG";
195 what = (
char *)
"CHANGE_DEFINITION";
198 what = (
char *)
"WRITE_SAME";
201 what = (
char *)
"READ SUBCHANNEL";
204 what = (
char *)
"READ_TOC";
207 what = (
char *)
"READ HEADER";
210 what = (
char *)
"PLAY AUDIO (10)";
213 what = (
char *)
"PLAY AUDIO MSF";
216 what = (
char *)
"GET EVENT/STATUS NOTIFICATION";
219 what = (
char *)
"PAUSE/RESUME";
222 what = (
char *)
"LOG_SELECT";
225 what = (
char *)
"LOG_SENSE";
228 what = (
char *)
"STOP PLAY/SCAN";
231 what = (
char *)
"READ DISC INFORMATION";
234 what = (
char *)
"READ TRACK INFORMATION";
237 what = (
char *)
"RESERVE TRACK";
240 what = (
char *)
"SEND OPC";
243 what = (
char *)
"MODE_SELECT_10";
246 what = (
char *)
"REPAIR TRACK";
249 what = (
char *)
"READ MASTER CUE";
252 what = (
char *)
"MODE_SENSE_10";
255 what = (
char *)
"CLOSE TRACK/SESSION";
258 what = (
char *)
"READ BUFFER CAPACITY";
261 what = (
char *)
"SEND CUE SHEET";
264 what = (
char *)
"BLANK";
267 what = (
char *)
"REPORT LUNS";
270 what = (
char *)
"MOVE_MEDIUM or PLAY AUDIO (12)";
273 what = (
char *)
"READ_12";
276 what = (
char *)
"WRITE_12";
279 what = (
char *)
"WRITE_VERIFY_12";
282 what = (
char *)
"SEARCH_HIGH_12";
285 what = (
char *)
"SEARCH_EQUAL_12";
288 what = (
char *)
"SEARCH_LOW_12";
291 what = (
char *)
"SEND_VOLUME_TAG";
294 what = (
char *)
"READ_ELEMENT_STATUS";
297 what = (
char *)
"READ CD MSF";
300 what = (
char *)
"SCAN";
303 what = (
char *)
"SET CD SPEED";
306 what = (
char *)
"MECHANISM STATUS";
309 what = (
char *)
"READ CD";
312 what = (
char *)
"WRITE CONTINUE";
315 what = (
char *)
"WRITE_LONG_2";
318 what = (
char *)
"Realtek's vendor command";
321 what = (
char *)
"(unknown command)";
330 for (i = 0; i < srb->
cmd_len && i < 16; i++)
338 switch (sense_type) {
376 #ifdef SUPPORT_MAGIC_GATE
409 sense->
info[0] = (
u8) (info >> 24);
410 sense->
info[1] = (
u8) (info >> 16);
411 sense->
info[2] = (
u8) (info >> 8);
412 sense->
info[3] = (
u8) info;
417 if (sns_key_info0 != 0) {
435 if (!check_lun_mc(chip, lun)) {
436 set_lun_mc(chip, lun);
445 'M',
'E',
'M',
'O',
'R',
'Y',
'S',
'T',
'I',
'C',
'K',
456 char *inquiry_default = (
char *)
"Generic-xD/SD/M.S. 1.00 ";
457 char *inquiry_string;
458 unsigned char sendbytes;
461 int pro_formatter_flag = 0;
462 unsigned char inquiry_buf[] = {
473 inquiry_string = inquiry_default;
475 buf =
vmalloc(scsi_bufflen(srb));
480 if (!card || (card == MS_CARD))
481 pro_formatter_flag = 1;
484 if (pro_formatter_flag) {
485 if (scsi_bufflen(srb) < 56)
486 sendbytes = (
unsigned char)(scsi_bufflen(srb));
490 if (scsi_bufflen(srb) < 36)
491 sendbytes = (
unsigned char)(scsi_bufflen(srb));
497 memcpy(buf, inquiry_buf, 8);
498 memcpy(buf + 8, inquiry_string, sendbytes - 8);
499 if (pro_formatter_flag)
502 memcpy(buf, inquiry_buf, sendbytes);
505 if (pro_formatter_flag) {
507 memcpy(buf + 36, formatter_inquiry_str, sendbytes - 36);
510 scsi_set_resid(srb, 0);
522 scsi_set_resid(srb, scsi_bufflen(srb));
524 if (srb->
cmnd[1] == 1)
527 switch (srb->
cmnd[0x4]) {
557 prevent = srb->
cmnd[4] & 0x1;
559 scsi_set_resid(srb, 0);
571 int lun,
u8 *buf,
int buf_len)
576 int support_format = 0;
581 if (data_size > 0x68)
585 sys_info_offset = 12;
586 if (data_size > 0x6C)
628 if (data_size >= 12) {
642 if (data_size >= 8) {
650 if (data_size > sys_info_offset) {
652 int len = data_size - sys_info_offset;
653 len = (len < 96) ? len : 96;
662 unsigned int dataSize;
664 int pro_formatter_flag;
665 unsigned char pageCode, *
buf;
670 scsi_set_resid(srb, scsi_bufflen(srb));
674 pro_formatter_flag = 0;
678 if ((get_lun2card(chip, lun) & MS_CARD)) {
679 if (!card || (card == MS_CARD)) {
681 if (chip->
option.mspro_formatter_enable)
682 pro_formatter_flag = 1;
690 pageCode = srb->
cmnd[2] & 0x3f;
692 if ((pageCode == 0x3F) || (pageCode == 0x1C) ||
693 (pageCode == 0x00) || (pro_formatter_flag && (pageCode == 0x20))) {
695 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
696 ms_mode_sense(chip, srb->
cmnd[0], lun, buf,
708 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
709 ms_mode_sense(chip, srb->
cmnd[0], lun, buf,
729 scsi_set_resid(srb, scsi_bufflen(srb));
734 unsigned int len =
min(scsi_bufflen(srb), dataSize);
736 scsi_set_resid(srb, scsi_bufflen(srb) - len);
757 buf =
vmalloc(scsi_bufflen(srb));
761 tmp = (
unsigned char *)sense;
762 memcpy(buf, tmp, scsi_bufflen(srb));
767 scsi_set_resid(srb, 0);
785 if (!check_lun_mc(chip, lun)) {
786 set_lun_mc(chip, lun);
800 sec_cnt = ((
u16) (srb->
cmnd[7]) << 8) | srb->
cmnd[8];
802 start_sec = ((
u32) (srb->
cmnd[1] & 0x1F) << 16) |
804 sec_cnt = srb->
cmnd[4];
809 start_sec = ((
u32) srb->
cmnd[4] << 24) |
813 sec_cnt = ((
u16) (srb->
cmnd[9]) << 8) | srb->
cmnd[10];
819 if ((start_sec > chip->
capacity[lun]) ||
820 ((start_sec + sec_cnt) > chip->
capacity[lun])) {
826 scsi_set_resid(srb, 0);
837 retval =
card_rw(srb, chip, start_sec, sec_cnt);
848 scsi_set_resid(srb, 0);
857 unsigned int buf_len;
863 if (!chip->
option.mspro_formatter_enable) {
869 buf_len = (scsi_bufflen(srb) > 12) ? 0x14 : 12;
880 if ((buf_len > 12) && chip->
option.mspro_formatter_enable &&
881 (chip->
lun2card[lun] & MS_CARD) && (!card || (card == MS_CARD))) {
891 buf[i++] = (
unsigned char)((chip->
capacity[lun]) >> 24);
892 buf[i++] = (
unsigned char)((chip->
capacity[lun]) >> 16);
921 buf_len =
min(scsi_bufflen(srb), buf_len);
925 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
940 if (!check_lun_mc(chip, lun)) {
941 set_lun_mc(chip, lun);
950 buf[0] = (
unsigned char)((chip->
capacity[lun] - 1) >> 24);
951 buf[1] = (
unsigned char)((chip->
capacity[lun] - 1) >> 16);
952 buf[2] = (
unsigned char)((chip->
capacity[lun] - 1) >> 8);
963 scsi_set_resid(srb, 0);
971 unsigned int buf_len;
972 u8 status[32] = { 0 };
976 buf_len =
min(scsi_bufflen(srb), (
unsigned int)
sizeof(status));
978 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
986 unsigned int buf_len;
991 buf_len =
min(scsi_bufflen(srb), (
unsigned int)
sizeof(rts51x_status));
993 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
1001 unsigned short addr, len,
i;
1011 if (addr < 0xe000) {
1020 for (i = 0; i < len; i++) {
1030 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1032 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1042 unsigned short addr, len,
i;
1052 if (addr < 0xe000) {
1057 len = (
unsigned short)
min(scsi_bufflen(srb), (
unsigned int)len);
1064 for (i = 0; i < len; i++) {
1075 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1095 scsi_set_resid(srb, 0);
1110 addr = srb->
cmnd[5];
1118 for (i = 0; i < len; i++) {
1129 len =
min(scsi_bufflen(srb), (
unsigned int)len);
1131 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1148 addr = srb->
cmnd[5];
1152 len =
min(scsi_bufflen(srb), (
unsigned int)len);
1159 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1161 for (i = 0; i < len; i++) {
1189 if ((card ==
SD_CARD) || (card == MS_CARD)) {
1196 scsi_set_resid(srb, 0);
1205 unsigned char *buf =
NULL;
1207 unsigned int buf_len;
1213 if ((scsi_bufflen(srb) < buf_len) || (scsi_sglist(srb) ==
NULL)) {
1219 clear = srb->
cmnd[2];
1221 buf =
vmalloc(scsi_bufflen(srb));
1230 scsi_set_resid(srb, 0);
1246 switch (srb->
cmnd[3]) {
1248 rts51x_init_cmd(chip);
1252 cmd_type = srb->
cmnd[4];
1258 addr = (srb->
cmnd[5] << 8) | srb->
cmnd[6];
1259 mask = srb->
cmnd[7];
1260 value = srb->
cmnd[8];
1265 mode = srb->
cmnd[4];
1269 cmnd[7] << 16) | ((
u32) srb->
1293 if (scsi_bufflen(srb) < 1) {
1299 scsi_set_resid(srb, 0);
1319 switch (srb->
cmnd[3]) {
1324 result = rw_mem_cmd_buf(srb, chip);
1337 switch (srb->
cmnd[2]) {
1340 result = read_write(srb, chip);
1344 result = suit_cmd(srb, chip);
1348 result = read_phy_register(srb, chip);
1352 result = write_phy_register(srb, chip);
1356 result = get_dev_status(srb, chip);
1372 switch (srb->
cmnd[1]) {
1374 result = read_status(srb, chip);
1378 result = read_mem(srb, chip);
1382 result = write_mem(srb, chip);
1386 result = get_card_bus_width(srb, chip);
1390 result = get_sd_csd(srb, chip);
1395 result = trace_msg_cmd(srb, chip);
1400 result = app_cmd(srb, chip);
1416 int retval, quick_format;
1423 if ((srb->
cmnd[3] != 0x4D) || (srb->
cmnd[4] != 0x47)
1424 || (srb->
cmnd[5] != 0x66) || (srb->
cmnd[6] != 0x6D)
1425 || (srb->
cmnd[7] != 0x74)) {
1430 if (srb->
cmnd[8] & 0x01)
1440 if (chip->
card_wp & MS_CARD) {
1459 scsi_set_resid(srb, 0);
1463 #ifdef SUPPORT_PCGL_1P18
1470 unsigned int buf_len;
1482 if ((srb->
cmnd[2] != 0xB0) || (srb->
cmnd[4] != 0x4D) ||
1483 (srb->
cmnd[5] != 0x53) || (srb->
cmnd[6] != 0x49) ||
1484 (srb->
cmnd[7] != 0x44)) {
1489 dev_info_id = srb->
cmnd[3];
1490 if ((
CHK_MSXC(ms_card) && (dev_info_id == 0x10)) ||
1491 (!
CHK_MSXC(ms_card) && (dev_info_id == 0x13)) ||
1497 if (dev_info_id == 0x15)
1498 buf_len = data_len = 0x3A;
1500 buf_len = data_len = 0x6A;
1526 buf[i++] = dev_info_id;
1528 if (dev_info_id == 0x15)
1536 if ((dev_info_id == 0x10) || (dev_info_id == 0x13)) {
1546 if (dev_info_id == 0x15)
1547 scsi_set_resid(srb, scsi_bufflen(srb) - 0x3C);
1549 scsi_set_resid(srb, scsi_bufflen(srb) - 0x6C);
1561 retval = ms_format_cmnd(srb, chip);
1562 #ifdef SUPPORT_PCGL_1P18
1564 retval = get_ms_information(srb, chip);
1590 switch (srb->
cmnd[0]) {
1624 #ifdef SUPPORT_MAGIC_GATE
1656 key_format = srb->
cmnd[10] & 0x3F;
1658 switch (key_format) {
1660 if ((scsi_bufflen(srb) == 0x41C) &&
1661 (srb->
cmnd[8] == 0x04) && (srb->
cmnd[9] == 0x1C)) {
1673 if ((scsi_bufflen(srb) == 0x24) &&
1674 (srb->
cmnd[8] == 0x00) && (srb->
cmnd[9] == 0x24)) {
1687 if ((scsi_bufflen(srb) == 0x404) &&
1688 (srb->
cmnd[8] == 0x04) &&
1689 (srb->
cmnd[9] == 0x04) &&
1690 (srb->
cmnd[2] == 0x00) &&
1691 (srb->
cmnd[3] == 0x00) &&
1692 (srb->
cmnd[4] == 0x00) && (srb->
cmnd[5] < 32)) {
1708 scsi_set_resid(srb, 0);
1747 key_format = srb->
cmnd[10] & 0x3F;
1749 switch (key_format) {
1751 if ((scsi_bufflen(srb) == 0x0C) &&
1752 (srb->
cmnd[8] == 0x00) && (srb->
cmnd[9] == 0x0C)) {
1764 if ((scsi_bufflen(srb) == 0x0C) &&
1765 (srb->
cmnd[8] == 0x00) && (srb->
cmnd[9] == 0x0C)) {
1766 retval =
mg_chg(srb, chip);
1777 if ((scsi_bufflen(srb) == 0x0C) &&
1778 (srb->
cmnd[8] == 0x00) && (srb->
cmnd[9] == 0x0C)) {
1779 retval =
mg_rsp(srb, chip);
1791 if ((scsi_bufflen(srb) == 0x404) &&
1792 (srb->
cmnd[8] == 0x04) &&
1793 (srb->
cmnd[9] == 0x04) &&
1794 (srb->
cmnd[2] == 0x00) &&
1795 (srb->
cmnd[3] == 0x00) &&
1796 (srb->
cmnd[4] == 0x00) && (srb->
cmnd[5] < 32)) {
1812 scsi_set_resid(srb, 0);
1834 switch (srb->
cmnd[0]) {
1839 result = read_write(srb, chip);
1843 result = test_unit_ready(srb, chip);
1851 result = read_capacity(srb, chip);
1855 result = start_stop_unit(srb, chip);
1859 result = allow_medium_removal(srb, chip);
1868 result = mode_sense(srb, chip);
1872 result = read_format_capacity(srb, chip);
1876 result = vendor_cmnd(srb, chip);
1880 result = ms_sp_cmnd(srb, chip);
1890 result = sd_extention_cmnd(srb, chip);
1894 #ifdef SUPPORT_MAGIC_GATE
1896 result = mg_report_key(srb, chip);
1900 result = mg_send_key(srb, chip);
1971 #define SPRINTF(args...) \
1972 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
1987 SPRINTF(
" Vendor: Realtek Corp.\n");
1988 SPRINTF(
" Product: RTS51xx USB Card Reader\n");
1990 SPRINTF(
" Build: %s\n", __TIME__);
1995 *start = buffer +
offset;
1997 if ((pos - buffer) < offset)
1999 else if ((pos - buffer - offset) < length)
2000 return pos - buffer -
offset;
2014 __func__, chip->
srb);
2041 struct rts51x_chip *chip = host_to_rts51x(srb->device->host);
2050 if (chip->
srb != srb) {
2096 return "SCSI emulation for RTS5139 USB card reader";
2104 .info = rts5139_info,
2134 .use_clustering = 1,
2140 .skip_settle_delay = 1,