1 /* Driver for Realtek RTS51xx USB card reader
3 * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * wwang (wei_wang@realsil.com.cn)
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
22 * Edwin Rong (edwin_rong@realsil.com.cn)
23 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
26 #include <linux/blkdev.h>
27 #include <linux/kthread.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/export.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_device.h>
39 #include "rts51x_chip.h"
40 #include "rts51x_scsi.h"
41 #include "rts51x_card.h"
42 #include "rts51x_transport.h"
47 void scsi_show_command(struct scsi_cmnd
*srb
)
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";
65 case READ_BLOCK_LIMITS
:
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";
92 case RECOVER_BUFFERED_DATA
:
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";
116 case RECEIVE_DIAGNOSTIC
:
117 what
= (char *)"RECEIVE_DIAGNOSTIC";
119 case SEND_DIAGNOSTIC
:
120 what
= (char *)"SEND_DIAGNOSTIC";
122 case ALLOW_MEDIUM_REMOVAL
:
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";
161 case SYNCHRONIZE_CACHE
:
162 what
= (char *)"SYNCHRONIZE_CACHE";
164 case LOCK_UNLOCK_CACHE
:
165 what
= (char *)"LOCK_UNLOCK_CACHE";
167 case READ_DEFECT_DATA
:
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";
194 case CHANGE_DEFINITION
:
195 what
= (char *)"CHANGE_DEFINITION";
198 what
= (char *)"WRITE_SAME";
200 case GPCMD_READ_SUBCHANNEL
:
201 what
= (char *)"READ SUBCHANNEL";
204 what
= (char *)"READ_TOC";
206 case GPCMD_READ_HEADER
:
207 what
= (char *)"READ HEADER";
209 case GPCMD_PLAY_AUDIO_10
:
210 what
= (char *)"PLAY AUDIO (10)";
212 case GPCMD_PLAY_AUDIO_MSF
:
213 what
= (char *)"PLAY AUDIO MSF";
215 case GPCMD_GET_EVENT_STATUS_NOTIFICATION
:
216 what
= (char *)"GET EVENT/STATUS NOTIFICATION";
218 case GPCMD_PAUSE_RESUME
:
219 what
= (char *)"PAUSE/RESUME";
222 what
= (char *)"LOG_SELECT";
225 what
= (char *)"LOG_SENSE";
227 case GPCMD_STOP_PLAY_SCAN
:
228 what
= (char *)"STOP PLAY/SCAN";
230 case GPCMD_READ_DISC_INFO
:
231 what
= (char *)"READ DISC INFORMATION";
233 case GPCMD_READ_TRACK_RZONE_INFO
:
234 what
= (char *)"READ TRACK INFORMATION";
236 case GPCMD_RESERVE_RZONE_TRACK
:
237 what
= (char *)"RESERVE TRACK";
240 what
= (char *)"SEND OPC";
243 what
= (char *)"MODE_SELECT_10";
245 case GPCMD_REPAIR_RZONE_TRACK
:
246 what
= (char *)"REPAIR TRACK";
249 what
= (char *)"READ MASTER CUE";
252 what
= (char *)"MODE_SENSE_10";
254 case GPCMD_CLOSE_TRACK
:
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";
278 case WRITE_VERIFY_12
:
279 what
= (char *)"WRITE_VERIFY_12";
282 what
= (char *)"SEARCH_HIGH_12";
284 case SEARCH_EQUAL_12
:
285 what
= (char *)"SEARCH_EQUAL_12";
288 what
= (char *)"SEARCH_LOW_12";
290 case SEND_VOLUME_TAG
:
291 what
= (char *)"SEND_VOLUME_TAG";
293 case READ_ELEMENT_STATUS
:
294 what
= (char *)"READ_ELEMENT_STATUS";
296 case GPCMD_READ_CD_MSF
:
297 what
= (char *)"READ CD MSF";
300 what
= (char *)"SCAN";
302 case GPCMD_SET_SPEED
:
303 what
= (char *)"SET CD SPEED";
305 case GPCMD_MECHANISM_STATUS
:
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)";
326 if (srb
->cmnd
[0] != TEST_UNIT_READY
)
327 RTS51X_DEBUGP("Command %s (%d bytes)\n", what
, srb
->cmd_len
);
330 for (i
= 0; i
< srb
->cmd_len
&& i
< 16; i
++)
331 RTS51X_DEBUGPN(" %02x", srb
->cmnd
[i
]);
332 RTS51X_DEBUGPN("\n");
336 void set_sense_type(struct rts51x_chip
*chip
, unsigned int lun
, int sense_type
)
338 switch (sense_type
) {
339 case SENSE_TYPE_MEDIA_CHANGE
:
340 set_sense_data(chip
, lun
, CUR_ERR
, 0x06, 0, 0x28, 0, 0, 0);
343 case SENSE_TYPE_MEDIA_NOT_PRESENT
:
344 set_sense_data(chip
, lun
, CUR_ERR
, 0x02, 0, 0x3A, 0, 0, 0);
347 case SENSE_TYPE_MEDIA_LBA_OVER_RANGE
:
348 set_sense_data(chip
, lun
, CUR_ERR
, 0x05, 0, 0x21, 0, 0, 0);
351 case SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT
:
352 set_sense_data(chip
, lun
, CUR_ERR
, 0x05, 0, 0x25, 0, 0, 0);
355 case SENSE_TYPE_MEDIA_WRITE_PROTECT
:
356 set_sense_data(chip
, lun
, CUR_ERR
, 0x07, 0, 0x27, 0, 0, 0);
359 case SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
:
360 set_sense_data(chip
, lun
, CUR_ERR
, 0x03, 0, 0x11, 0, 0, 0);
363 case SENSE_TYPE_MEDIA_WRITE_ERR
:
364 set_sense_data(chip
, lun
, CUR_ERR
, 0x03, 0, 0x0C, 0x02, 0, 0);
367 case SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
:
368 set_sense_data(chip
, lun
, CUR_ERR
, ILGAL_REQ
, 0,
369 ASC_INVLD_CDB
, ASCQ_INVLD_CDB
, CDB_ILLEGAL
, 1);
372 case SENSE_TYPE_FORMAT_CMD_FAILED
:
373 set_sense_data(chip
, lun
, CUR_ERR
, 0x03, 0, 0x31, 0x01, 0, 0);
376 #ifdef SUPPORT_MAGIC_GATE
377 case SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB
:
378 set_sense_data(chip
, lun
, CUR_ERR
, 0x05, 0, 0x6F, 0x02, 0, 0);
381 case SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN
:
382 set_sense_data(chip
, lun
, CUR_ERR
, 0x05, 0, 0x6F, 0x00, 0, 0);
385 case SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
:
386 set_sense_data(chip
, lun
, CUR_ERR
, 0x02, 0, 0x30, 0x00, 0, 0);
389 case SENSE_TYPE_MG_WRITE_ERR
:
390 set_sense_data(chip
, lun
, CUR_ERR
, 0x03, 0, 0x0C, 0x00, 0, 0);
394 case SENSE_TYPE_NO_SENSE
:
396 set_sense_data(chip
, lun
, CUR_ERR
, 0, 0, 0, 0, 0, 0);
401 void set_sense_data(struct rts51x_chip
*chip
, unsigned int lun
, u8 err_code
,
402 u8 sense_key
, u32 info
, u8 asc
, u8 ascq
, u8 sns_key_info0
,
405 struct sense_data_t
*sense
= &(chip
->sense_buffer
[lun
]);
407 sense
->err_code
= err_code
;
408 sense
->sense_key
= sense_key
;
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
;
414 sense
->ad_sense_len
= sizeof(struct sense_data_t
) - 8;
417 if (sns_key_info0
!= 0) {
418 sense
->sns_key_info
[0] = SKSV
| sns_key_info0
;
419 sense
->sns_key_info
[1] = (sns_key_info1
& 0xf0) >> 8;
420 sense
->sns_key_info
[2] = sns_key_info1
& 0x0f;
424 static int test_unit_ready(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
426 unsigned int lun
= SCSI_LUN(srb
);
428 rts51x_init_cards(chip
);
430 if (!check_card_ready(chip
, lun
)) {
431 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
432 return TRANSPORT_FAILED
;
435 if (!check_lun_mc(chip
, lun
)) {
436 set_lun_mc(chip
, lun
);
437 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
438 return TRANSPORT_FAILED
;
441 return TRANSPORT_GOOD
;
444 unsigned char formatter_inquiry_str
[20] = {
445 'M', 'E', 'M', 'O', 'R', 'Y', 'S', 'T', 'I', 'C', 'K',
446 '-', 'M', 'G', /* Byte[47:49] */
447 0x0B, /* Byte[50]: MG, MS, MSPro, MSXC */
448 0x00, /* Byte[51]: Category Specific Commands */
449 0x00, /* Byte[52]: Access Control and feature */
450 0x20, 0x20, 0x20, /* Byte[53:55] */
453 static int inquiry(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
455 unsigned int lun
= SCSI_LUN(srb
);
456 char *inquiry_default
= (char *)"Generic-xD/SD/M.S. 1.00 ";
457 char *inquiry_string
;
458 unsigned char sendbytes
;
460 u8 card
= get_lun_card(chip
, lun
);
461 int pro_formatter_flag
= 0;
462 unsigned char inquiry_buf
[] = {
463 QULIFIRE
| DRCT_ACCESS_DEV
,
470 REL_ADR
| WBUS_32
| WBUS_16
| SYNC
| LINKED
| CMD_QUE
| SFT_RE
,
473 inquiry_string
= inquiry_default
;
475 buf
= vmalloc(scsi_bufflen(srb
));
477 TRACE_RET(chip
, TRANSPORT_ERROR
);
479 if (MS_FORMATTER_ENABLED(chip
) && (get_lun2card(chip
, lun
) & MS_CARD
)) {
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
)
500 buf
[4] = 0x33; /* Additional Length */
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);
512 rts51x_set_xfer_buf(buf
, scsi_bufflen(srb
), srb
);
515 return TRANSPORT_GOOD
;
518 static int start_stop_unit(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
520 unsigned int lun
= SCSI_LUN(srb
);
522 scsi_set_resid(srb
, scsi_bufflen(srb
));
524 if (srb
->cmnd
[1] == 1)
525 return TRANSPORT_GOOD
;
527 switch (srb
->cmnd
[0x4]) {
530 return TRANSPORT_GOOD
;
533 /* Media shall be unload */
534 if (check_card_ready(chip
, lun
))
535 eject_card(chip
, lun
);
536 return TRANSPORT_GOOD
;
538 case MAKE_MEDIUM_READY
:
540 if (check_card_ready(chip
, lun
)) {
541 return TRANSPORT_GOOD
;
543 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
544 TRACE_RET(chip
, TRANSPORT_FAILED
);
550 TRACE_RET(chip
, TRANSPORT_ERROR
);
553 static int allow_medium_removal(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
557 prevent
= srb
->cmnd
[4] & 0x1;
559 scsi_set_resid(srb
, 0);
562 set_sense_type(chip
, SCSI_LUN(srb
),
563 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
564 TRACE_RET(chip
, TRANSPORT_FAILED
);
567 return TRANSPORT_GOOD
;
570 static void ms_mode_sense(struct rts51x_chip
*chip
, u8 cmd
,
571 int lun
, u8
*buf
, int buf_len
)
573 struct ms_info
*ms_card
= &(chip
->ms_card
);
575 int data_size
= buf_len
;
576 int support_format
= 0;
579 if (cmd
== MODE_SENSE
) {
581 if (data_size
> 0x68)
583 buf
[i
++] = 0x67; /* Mode Data Length */
585 sys_info_offset
= 12;
586 if (data_size
> 0x6C)
588 buf
[i
++] = 0x00; /* Mode Data Length (MSB) */
589 buf
[i
++] = 0x6A; /* Mode Data Length (LSB) */
592 /* Medium Type Code */
593 if (check_card_ready(chip
, lun
)) {
594 if (CHK_MSXC(ms_card
)) {
597 } else if (CHK_MSPRO(ms_card
)) {
605 if (check_card_wp(chip
, lun
))
610 buf
[i
++] = 0x00; /* MediaType */
611 buf
[i
++] = 0x00; /* WP */
614 buf
[i
++] = 0x00; /* Reserved */
616 if (cmd
== MODE_SENSE_10
) {
617 buf
[i
++] = 0x00; /* Reserved */
618 buf
[i
++] = 0x00; /* Block descriptor length(MSB) */
619 buf
[i
++] = 0x00; /* Block descriptor length(LSB) */
621 /* The Following Data is the content of "Page 0x20" */
623 buf
[i
++] = 0x20; /* Page Code */
625 buf
[i
++] = 0x62; /* Page Length */
627 buf
[i
++] = 0x00; /* No Access Control */
628 if (data_size
>= 12) {
630 buf
[i
++] = 0xC0; /* SF, SGM */
635 /* The Following Data is the content of "Page 0x20" */
637 buf
[i
++] = 0x20; /* Page Code */
639 buf
[i
++] = 0x62; /* Page Length */
641 buf
[i
++] = 0x00; /* No Access Control */
642 if (data_size
>= 8) {
644 buf
[i
++] = 0xC0; /* SF, SGM */
650 if (data_size
> sys_info_offset
) {
651 /* 96 Bytes Attribute Data */
652 int len
= data_size
- sys_info_offset
;
653 len
= (len
< 96) ? len
: 96;
655 memcpy(buf
+ sys_info_offset
, ms_card
->raw_sys_info
, len
);
659 static int mode_sense(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
661 unsigned int lun
= SCSI_LUN(srb
);
662 unsigned int dataSize
;
664 int pro_formatter_flag
;
665 unsigned char pageCode
, *buf
;
666 u8 card
= get_lun_card(chip
, lun
);
668 if (!check_card_ready(chip
, lun
)) {
669 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
670 scsi_set_resid(srb
, scsi_bufflen(srb
));
671 TRACE_RET(chip
, TRANSPORT_FAILED
);
674 pro_formatter_flag
= 0;
676 /* In Combo mode, device responses ModeSense command as a MS LUN
677 * when no card is inserted */
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;
686 buf
= kmalloc(dataSize
, GFP_KERNEL
);
688 TRACE_RET(chip
, TRANSPORT_ERROR
);
690 pageCode
= srb
->cmnd
[2] & 0x3f;
692 if ((pageCode
== 0x3F) || (pageCode
== 0x1C) ||
693 (pageCode
== 0x00) || (pro_formatter_flag
&& (pageCode
== 0x20))) {
694 if (srb
->cmnd
[0] == MODE_SENSE
) {
695 if ((pageCode
== 0x3F) || (pageCode
== 0x20)) {
696 ms_mode_sense(chip
, srb
->cmnd
[0], lun
, buf
,
702 if (check_card_wp(chip
, lun
))
708 if ((pageCode
== 0x3F) || (pageCode
== 0x20)) {
709 ms_mode_sense(chip
, srb
->cmnd
[0], lun
, buf
,
716 if (check_card_wp(chip
, lun
))
726 status
= TRANSPORT_GOOD
;
728 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
729 scsi_set_resid(srb
, scsi_bufflen(srb
));
730 status
= TRANSPORT_FAILED
;
733 if (status
== TRANSPORT_GOOD
) {
734 unsigned int len
= min(scsi_bufflen(srb
), dataSize
);
735 rts51x_set_xfer_buf(buf
, len
, srb
);
736 scsi_set_resid(srb
, scsi_bufflen(srb
) - len
);
743 static int request_sense(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
745 struct sense_data_t
*sense
;
746 unsigned int lun
= SCSI_LUN(srb
);
747 struct ms_info
*ms_card
= &(chip
->ms_card
);
748 unsigned char *tmp
, *buf
;
750 sense
= &(chip
->sense_buffer
[lun
]);
752 if ((get_lun_card(chip
, lun
) == MS_CARD
)
753 && PRO_UNDER_FORMATTING(ms_card
)) {
754 mspro_format_sense(chip
, lun
);
757 buf
= vmalloc(scsi_bufflen(srb
));
759 TRACE_RET(chip
, TRANSPORT_ERROR
);
761 tmp
= (unsigned char *)sense
;
762 memcpy(buf
, tmp
, scsi_bufflen(srb
));
764 rts51x_set_xfer_buf(buf
, scsi_bufflen(srb
), srb
);
767 scsi_set_resid(srb
, 0);
768 /* Reset Sense Data */
769 set_sense_type(chip
, lun
, SENSE_TYPE_NO_SENSE
);
770 return TRANSPORT_GOOD
;
773 static int read_write(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
775 unsigned int lun
= SCSI_LUN(srb
);
780 if (!check_card_ready(chip
, lun
) || (chip
->capacity
[lun
] == 0)) {
781 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
782 TRACE_RET(chip
, TRANSPORT_FAILED
);
785 if (!check_lun_mc(chip
, lun
)) {
786 set_lun_mc(chip
, lun
);
787 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
788 return TRANSPORT_FAILED
;
791 rts51x_prepare_run(chip
);
792 RTS51X_SET_STAT(chip
, STAT_RUN
);
794 if ((srb
->cmnd
[0] == READ_10
) || (srb
->cmnd
[0] == WRITE_10
)) {
796 ((u32
) srb
->cmnd
[2] << 24) |
797 ((u32
) srb
->cmnd
[3] << 16) |
798 ((u32
) srb
->cmnd
[4] << 8) |
799 ((u32
) srb
->cmnd
[5]);
800 sec_cnt
= ((u16
) (srb
->cmnd
[7]) << 8) | srb
->cmnd
[8];
801 } else if ((srb
->cmnd
[0] == READ_6
) || (srb
->cmnd
[0] == WRITE_6
)) {
802 start_sec
= ((u32
) (srb
->cmnd
[1] & 0x1F) << 16) |
803 ((u32
) srb
->cmnd
[2] << 8) | ((u32
) srb
->cmnd
[3]);
804 sec_cnt
= srb
->cmnd
[4];
805 } else if ((srb
->cmnd
[0] == VENDOR_CMND
) &&
806 (srb
->cmnd
[1] == SCSI_APP_CMD
) &&
807 ((srb
->cmnd
[2] == PP_READ10
) ||
808 (srb
->cmnd
[2] == PP_WRITE10
))) {
809 start_sec
= ((u32
) srb
->cmnd
[4] << 24) |
810 ((u32
) srb
->cmnd
[5] << 16) |
811 ((u32
) srb
->cmnd
[6] << 8) |
812 ((u32
) srb
->cmnd
[7]);
813 sec_cnt
= ((u16
) (srb
->cmnd
[9]) << 8) | srb
->cmnd
[10];
815 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
816 TRACE_RET(chip
, TRANSPORT_FAILED
);
819 if ((start_sec
> chip
->capacity
[lun
]) ||
820 ((start_sec
+ sec_cnt
) > chip
->capacity
[lun
])) {
821 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_LBA_OVER_RANGE
);
822 TRACE_RET(chip
, TRANSPORT_FAILED
);
826 scsi_set_resid(srb
, 0);
827 return TRANSPORT_GOOD
;
830 if ((srb
->sc_data_direction
== DMA_TO_DEVICE
)
831 && check_card_wp(chip
, lun
)) {
832 RTS51X_DEBUGP("Write protected card!\n");
833 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_PROTECT
);
834 TRACE_RET(chip
, TRANSPORT_FAILED
);
837 retval
= card_rw(srb
, chip
, start_sec
, sec_cnt
);
838 if (retval
!= STATUS_SUCCESS
) {
839 if (srb
->sc_data_direction
== DMA_FROM_DEVICE
) {
840 set_sense_type(chip
, lun
,
841 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
843 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
845 TRACE_RET(chip
, TRANSPORT_FAILED
);
848 scsi_set_resid(srb
, 0);
850 return TRANSPORT_GOOD
;
853 static int read_format_capacity(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
856 unsigned int lun
= SCSI_LUN(srb
);
857 unsigned int buf_len
;
858 u8 card
= get_lun_card(chip
, lun
);
862 if (!check_card_ready(chip
, lun
)) {
863 if (!chip
->option
.mspro_formatter_enable
) {
864 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
865 TRACE_RET(chip
, TRANSPORT_FAILED
);
869 buf_len
= (scsi_bufflen(srb
) > 12) ? 0x14 : 12;
871 buf
= kmalloc(buf_len
, GFP_KERNEL
);
873 TRACE_RET(chip
, TRANSPORT_ERROR
);
879 /* Capacity List Length */
880 if ((buf_len
> 12) && chip
->option
.mspro_formatter_enable
&&
881 (chip
->lun2card
[lun
] & MS_CARD
) && (!card
|| (card
== MS_CARD
))) {
890 if (check_card_ready(chip
, lun
)) {
891 buf
[i
++] = (unsigned char)((chip
->capacity
[lun
]) >> 24);
892 buf
[i
++] = (unsigned char)((chip
->capacity
[lun
]) >> 16);
893 buf
[i
++] = (unsigned char)((chip
->capacity
[lun
]) >> 8);
894 buf
[i
++] = (unsigned char)(chip
->capacity
[lun
]);
897 /* Byte[8]: Descriptor Type: Formatted medium */
900 buf
[i
++] = 0; /* Byte[16] */
908 /* Byte[8]: Descriptor Type: No medium */
911 buf
[i
++] = 0; /*Byte[16] */
921 buf_len
= min(scsi_bufflen(srb
), buf_len
);
922 rts51x_set_xfer_buf(buf
, buf_len
, srb
);
925 scsi_set_resid(srb
, scsi_bufflen(srb
) - buf_len
);
927 return TRANSPORT_GOOD
;
930 static int read_capacity(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
933 unsigned int lun
= SCSI_LUN(srb
);
935 if (!check_card_ready(chip
, lun
)) {
936 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
937 TRACE_RET(chip
, TRANSPORT_FAILED
);
940 if (!check_lun_mc(chip
, lun
)) {
941 set_lun_mc(chip
, lun
);
942 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_CHANGE
);
943 return TRANSPORT_FAILED
;
946 buf
= kmalloc(8, GFP_KERNEL
);
948 TRACE_RET(chip
, TRANSPORT_ERROR
);
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);
953 buf
[3] = (unsigned char)(chip
->capacity
[lun
] - 1);
960 rts51x_set_xfer_buf(buf
, scsi_bufflen(srb
), srb
);
963 scsi_set_resid(srb
, 0);
965 return TRANSPORT_GOOD
;
968 static int get_dev_status(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
970 unsigned int lun
= SCSI_LUN(srb
);
971 unsigned int buf_len
;
972 u8 status
[32] = { 0 };
974 rts51x_pp_status(chip
, lun
, status
, 32);
976 buf_len
= min(scsi_bufflen(srb
), (unsigned int)sizeof(status
));
977 rts51x_set_xfer_buf(status
, buf_len
, srb
);
978 scsi_set_resid(srb
, scsi_bufflen(srb
) - buf_len
);
980 return TRANSPORT_GOOD
;
983 static int read_status(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
985 u8 rts51x_status
[16];
986 unsigned int buf_len
;
987 unsigned int lun
= SCSI_LUN(srb
);
989 rts51x_read_status(chip
, lun
, rts51x_status
, 16);
991 buf_len
= min(scsi_bufflen(srb
), (unsigned int)sizeof(rts51x_status
));
992 rts51x_set_xfer_buf(rts51x_status
, buf_len
, srb
);
993 scsi_set_resid(srb
, scsi_bufflen(srb
) - buf_len
);
995 return TRANSPORT_GOOD
;
998 static int read_mem(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1000 unsigned int lun
= SCSI_LUN(srb
);
1001 unsigned short addr
, len
, i
;
1005 rts51x_prepare_run(chip
);
1006 RTS51X_SET_STAT(chip
, STAT_RUN
);
1008 addr
= ((u16
) srb
->cmnd
[2] << 8) | srb
->cmnd
[3];
1009 len
= ((u16
) srb
->cmnd
[4] << 8) | srb
->cmnd
[5];
1011 if (addr
< 0xe000) {
1012 RTS51X_DEBUGP("filter!addr=0x%x\n", addr
);
1013 return TRANSPORT_GOOD
;
1018 TRACE_RET(chip
, TRANSPORT_ERROR
);
1020 for (i
= 0; i
< len
; i
++) {
1021 retval
= rts51x_ep0_read_register(chip
, addr
+ i
, buf
+ i
);
1022 if (retval
!= STATUS_SUCCESS
) {
1024 set_sense_type(chip
, lun
,
1025 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
1026 TRACE_RET(chip
, TRANSPORT_FAILED
);
1030 len
= (unsigned short)min(scsi_bufflen(srb
), (unsigned int)len
);
1031 rts51x_set_xfer_buf(buf
, len
, srb
);
1032 scsi_set_resid(srb
, scsi_bufflen(srb
) - len
);
1036 return TRANSPORT_GOOD
;
1039 static int write_mem(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1041 unsigned int lun
= SCSI_LUN(srb
);
1042 unsigned short addr
, len
, i
;
1046 rts51x_prepare_run(chip
);
1047 RTS51X_SET_STAT(chip
, STAT_RUN
);
1049 addr
= ((u16
) srb
->cmnd
[2] << 8) | srb
->cmnd
[3];
1050 len
= ((u16
) srb
->cmnd
[4] << 8) | srb
->cmnd
[5];
1052 if (addr
< 0xe000) {
1053 RTS51X_DEBUGP("filter!addr=0x%x\n", addr
);
1054 return TRANSPORT_GOOD
;
1057 len
= (unsigned short)min(scsi_bufflen(srb
), (unsigned int)len
);
1060 TRACE_RET(chip
, TRANSPORT_ERROR
);
1062 rts51x_get_xfer_buf(buf
, len
, srb
);
1064 for (i
= 0; i
< len
; i
++) {
1066 rts51x_ep0_write_register(chip
, addr
+ i
, 0xFF, buf
[i
]);
1067 if (retval
!= STATUS_SUCCESS
) {
1069 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
1070 TRACE_RET(chip
, TRANSPORT_FAILED
);
1075 scsi_set_resid(srb
, scsi_bufflen(srb
) - len
);
1077 return TRANSPORT_GOOD
;
1080 static int get_sd_csd(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1082 struct sd_info
*sd_card
= &(chip
->sd_card
);
1083 unsigned int lun
= SCSI_LUN(srb
);
1085 if (!check_card_ready(chip
, lun
)) {
1086 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1087 TRACE_RET(chip
, TRANSPORT_FAILED
);
1090 if (get_lun_card(chip
, lun
) != SD_CARD
) {
1091 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
1092 TRACE_RET(chip
, TRANSPORT_FAILED
);
1095 scsi_set_resid(srb
, 0);
1096 rts51x_set_xfer_buf(sd_card
->raw_csd
, scsi_bufflen(srb
), srb
);
1098 return TRANSPORT_GOOD
;
1101 static int read_phy_register(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1107 rts51x_prepare_run(chip
);
1108 RTS51X_SET_STAT(chip
, STAT_RUN
);
1110 addr
= srb
->cmnd
[5];
1116 TRACE_RET(chip
, TRANSPORT_ERROR
);
1118 for (i
= 0; i
< len
; i
++) {
1120 rts51x_read_phy_register(chip
, addr
+ i
, buf
+ i
);
1121 if (retval
!= STATUS_SUCCESS
) {
1123 set_sense_type(chip
, SCSI_LUN(srb
),
1124 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
1125 TRACE_RET(chip
, TRANSPORT_FAILED
);
1129 len
= min(scsi_bufflen(srb
), (unsigned int)len
);
1130 rts51x_set_xfer_buf(buf
, len
, srb
);
1131 scsi_set_resid(srb
, scsi_bufflen(srb
) - len
);
1136 return TRANSPORT_GOOD
;
1139 static int write_phy_register(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1145 rts51x_prepare_run(chip
);
1146 RTS51X_SET_STAT(chip
, STAT_RUN
);
1148 addr
= srb
->cmnd
[5];
1152 len
= min(scsi_bufflen(srb
), (unsigned int)len
);
1156 TRACE_RET(chip
, TRANSPORT_ERROR
);
1158 rts51x_get_xfer_buf(buf
, len
, srb
);
1159 scsi_set_resid(srb
, scsi_bufflen(srb
) - len
);
1161 for (i
= 0; i
< len
; i
++) {
1163 rts51x_write_phy_register(chip
, addr
+ i
, buf
[i
]);
1164 if (retval
!= STATUS_SUCCESS
) {
1166 set_sense_type(chip
, SCSI_LUN(srb
),
1167 SENSE_TYPE_MEDIA_WRITE_ERR
);
1168 TRACE_RET(chip
, TRANSPORT_FAILED
);
1175 return TRANSPORT_GOOD
;
1178 static int get_card_bus_width(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1180 unsigned int lun
= SCSI_LUN(srb
);
1183 if (!check_card_ready(chip
, lun
)) {
1184 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1185 TRACE_RET(chip
, TRANSPORT_FAILED
);
1188 card
= get_lun_card(chip
, lun
);
1189 if ((card
== SD_CARD
) || (card
== MS_CARD
)) {
1190 bus_width
= chip
->card_bus_width
[lun
];
1192 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
1193 TRACE_RET(chip
, TRANSPORT_FAILED
);
1196 scsi_set_resid(srb
, 0);
1197 rts51x_set_xfer_buf(&bus_width
, scsi_bufflen(srb
), srb
);
1199 return TRANSPORT_GOOD
;
1203 static int trace_msg_cmd(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1205 unsigned char *buf
= NULL
;
1207 unsigned int buf_len
;
1211 ((2 + MSG_FUNC_LEN
+ MSG_FILE_LEN
+ TIME_VAL_LEN
) * TRACE_ITEM_CNT
);
1213 if ((scsi_bufflen(srb
) < buf_len
) || (scsi_sglist(srb
) == NULL
)) {
1214 set_sense_type(chip
, SCSI_LUN(srb
),
1215 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
1216 TRACE_RET(chip
, TRANSPORT_FAILED
);
1219 clear
= srb
->cmnd
[2];
1221 buf
= vmalloc(scsi_bufflen(srb
));
1223 TRACE_RET(chip
, TRANSPORT_ERROR
);
1225 rts51x_trace_msg(chip
, buf
, clear
);
1227 rts51x_set_xfer_buf(buf
, scsi_bufflen(srb
), srb
);
1230 scsi_set_resid(srb
, 0);
1231 return TRANSPORT_GOOD
;
1235 static int rw_mem_cmd_buf(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1237 int retval
= STATUS_SUCCESS
;
1238 unsigned int lun
= SCSI_LUN(srb
);
1239 u8 cmd_type
, mask
, value
, idx
, mode
, len
;
1243 rts51x_prepare_run(chip
);
1244 RTS51X_SET_STAT(chip
, STAT_RUN
);
1246 switch (srb
->cmnd
[3]) {
1248 rts51x_init_cmd(chip
);
1252 cmd_type
= srb
->cmnd
[4];
1254 set_sense_type(chip
, lun
,
1255 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1256 TRACE_RET(chip
, TRANSPORT_FAILED
);
1258 addr
= (srb
->cmnd
[5] << 8) | srb
->cmnd
[6];
1259 mask
= srb
->cmnd
[7];
1260 value
= srb
->cmnd
[8];
1261 rts51x_add_cmd(chip
, cmd_type
, addr
, mask
, value
);
1265 mode
= srb
->cmnd
[4];
1268 ((u32
) srb
->cmnd
[6] << 24) | ((u32
) srb
->
1269 cmnd
[7] << 16) | ((u32
) srb
->
1275 retval
= rts51x_send_cmd(chip
, mode
, 1000);
1276 if (retval
!= STATUS_SUCCESS
) {
1277 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
1278 TRACE_RET(chip
, TRANSPORT_FAILED
);
1280 if (mode
& STAGE_R
) {
1281 retval
= rts51x_get_rsp(chip
, len
, timeout
);
1282 if (retval
!= STATUS_SUCCESS
) {
1283 set_sense_type(chip
, lun
,
1284 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR
);
1285 TRACE_RET(chip
, TRANSPORT_FAILED
);
1292 value
= chip
->rsp_buf
[idx
];
1293 if (scsi_bufflen(srb
) < 1) {
1294 set_sense_type(chip
, lun
,
1295 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1296 TRACE_RET(chip
, TRANSPORT_FAILED
);
1298 rts51x_set_xfer_buf(&value
, 1, srb
);
1299 scsi_set_resid(srb
, 0);
1303 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1304 TRACE_RET(chip
, TRANSPORT_FAILED
);
1307 if (retval
!= STATUS_SUCCESS
) {
1308 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_ERR
);
1309 TRACE_RET(chip
, TRANSPORT_FAILED
);
1312 return TRANSPORT_GOOD
;
1315 static int suit_cmd(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1319 switch (srb
->cmnd
[3]) {
1324 result
= rw_mem_cmd_buf(srb
, chip
);
1327 result
= TRANSPORT_ERROR
;
1333 static int app_cmd(struct scsi_cmnd
*srb
, struct rts51x_chip
*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
);
1355 case GET_DEV_STATUS
:
1356 result
= get_dev_status(srb
, chip
);
1360 set_sense_type(chip
, SCSI_LUN(srb
),
1361 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1362 TRACE_RET(chip
, TRANSPORT_FAILED
);
1368 static int vendor_cmnd(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1370 int result
= TRANSPORT_GOOD
;
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
);
1404 set_sense_type(chip
, SCSI_LUN(srb
),
1405 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1406 TRACE_RET(chip
, TRANSPORT_FAILED
);
1412 static int ms_format_cmnd(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1414 struct ms_info
*ms_card
= &(chip
->ms_card
);
1415 unsigned int lun
= SCSI_LUN(srb
);
1416 int retval
, quick_format
;
1418 if (get_lun_card(chip
, lun
) != MS_CARD
) {
1419 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT
);
1420 TRACE_RET(chip
, TRANSPORT_FAILED
);
1423 if ((srb
->cmnd
[3] != 0x4D) || (srb
->cmnd
[4] != 0x47)
1424 || (srb
->cmnd
[5] != 0x66) || (srb
->cmnd
[6] != 0x6D)
1425 || (srb
->cmnd
[7] != 0x74)) {
1426 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1427 TRACE_RET(chip
, TRANSPORT_FAILED
);
1430 if (srb
->cmnd
[8] & 0x01)
1435 if (!(chip
->card_ready
& MS_CARD
)) {
1436 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1437 TRACE_RET(chip
, TRANSPORT_FAILED
);
1440 if (chip
->card_wp
& MS_CARD
) {
1441 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_PROTECT
);
1442 TRACE_RET(chip
, TRANSPORT_FAILED
);
1445 if (!CHK_MSPRO(ms_card
)) {
1446 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT
);
1447 TRACE_RET(chip
, TRANSPORT_FAILED
);
1450 rts51x_prepare_run(chip
);
1451 RTS51X_SET_STAT(chip
, STAT_RUN
);
1453 retval
= mspro_format(srb
, chip
, MS_SHORT_DATA_LEN
, quick_format
);
1454 if (retval
!= STATUS_SUCCESS
) {
1455 set_sense_type(chip
, lun
, SENSE_TYPE_FORMAT_CMD_FAILED
);
1456 TRACE_RET(chip
, TRANSPORT_FAILED
);
1459 scsi_set_resid(srb
, 0);
1460 return TRANSPORT_GOOD
;
1463 #ifdef SUPPORT_PCGL_1P18
1464 static int get_ms_information(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1466 struct ms_info
*ms_card
= &(chip
->ms_card
);
1467 unsigned int lun
= SCSI_LUN(srb
);
1468 u8 dev_info_id
, data_len
;
1470 unsigned int buf_len
;
1473 if (!check_card_ready(chip
, lun
)) {
1474 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1475 TRACE_RET(chip
, TRANSPORT_FAILED
);
1477 if ((get_lun_card(chip
, lun
) != MS_CARD
)) {
1478 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT
);
1479 TRACE_RET(chip
, TRANSPORT_FAILED
);
1482 if ((srb
->cmnd
[2] != 0xB0) || (srb
->cmnd
[4] != 0x4D) ||
1483 (srb
->cmnd
[5] != 0x53) || (srb
->cmnd
[6] != 0x49) ||
1484 (srb
->cmnd
[7] != 0x44)) {
1485 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1486 TRACE_RET(chip
, TRANSPORT_FAILED
);
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)) ||
1492 !CHK_MSPRO(ms_card
)) {
1493 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1494 TRACE_RET(chip
, TRANSPORT_FAILED
);
1497 if (dev_info_id
== 0x15)
1498 buf_len
= data_len
= 0x3A;
1500 buf_len
= data_len
= 0x6A;
1502 buf
= kmalloc(buf_len
, GFP_KERNEL
);
1504 TRACE_RET(chip
, TRANSPORT_ERROR
);
1507 /* GET Memory Stick Media Information Response Header */
1508 buf
[i
++] = 0x00; /* Data length MSB */
1509 buf
[i
++] = data_len
; /* Data length LSB */
1510 /* Device Information Type Code */
1511 if (CHK_MSXC(ms_card
))
1521 /* Number of Device Information */
1524 /* Device Information Body
1525 * Device Information ID Number */
1526 buf
[i
++] = dev_info_id
;
1527 /* Device Information Length */
1528 if (dev_info_id
== 0x15)
1532 buf
[i
++] = 0x00; /* Data length MSB */
1533 buf
[i
++] = data_len
; /* Data length LSB */
1536 if ((dev_info_id
== 0x10) || (dev_info_id
== 0x13)) {
1537 /* System Information */
1538 memcpy(buf
+ i
, ms_card
->raw_sys_info
, 96);
1541 memcpy(buf
+ i
, ms_card
->raw_model_name
, 48);
1544 rts51x_set_xfer_buf(buf
, buf_len
, srb
);
1546 if (dev_info_id
== 0x15)
1547 scsi_set_resid(srb
, scsi_bufflen(srb
) - 0x3C);
1549 scsi_set_resid(srb
, scsi_bufflen(srb
) - 0x6C);
1552 return STATUS_SUCCESS
;
1556 static int ms_sp_cmnd(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1558 int retval
= TRANSPORT_ERROR
;
1560 if (srb
->cmnd
[2] == MS_FORMAT
)
1561 retval
= ms_format_cmnd(srb
, chip
);
1562 #ifdef SUPPORT_PCGL_1P18
1563 else if (srb
->cmnd
[2] == GET_MS_INFORMATION
)
1564 retval
= get_ms_information(srb
, chip
);
1571 static int sd_extention_cmnd(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1573 unsigned int lun
= SCSI_LUN(srb
);
1576 rts51x_prepare_run(chip
);
1577 RTS51X_SET_STAT(chip
, STAT_RUN
);
1579 sd_cleanup_work(chip
);
1581 if (!check_card_ready(chip
, lun
)) {
1582 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1583 TRACE_RET(chip
, TRANSPORT_FAILED
);
1585 if ((get_lun_card(chip
, lun
) != SD_CARD
)) {
1586 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT
);
1587 TRACE_RET(chip
, TRANSPORT_FAILED
);
1590 switch (srb
->cmnd
[0]) {
1591 case SD_PASS_THRU_MODE
:
1592 result
= sd_pass_thru_mode(srb
, chip
);
1595 case SD_EXECUTE_NO_DATA
:
1596 result
= sd_execute_no_data(srb
, chip
);
1599 case SD_EXECUTE_READ
:
1600 result
= sd_execute_read_data(srb
, chip
);
1603 case SD_EXECUTE_WRITE
:
1604 result
= sd_execute_write_data(srb
, chip
);
1608 result
= sd_get_cmd_rsp(srb
, chip
);
1612 result
= sd_hw_rst(srb
, chip
);
1616 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1617 TRACE_RET(chip
, TRANSPORT_FAILED
);
1624 #ifdef SUPPORT_MAGIC_GATE
1625 static int mg_report_key(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1627 struct ms_info
*ms_card
= &(chip
->ms_card
);
1628 unsigned int lun
= SCSI_LUN(srb
);
1632 rts51x_prepare_run(chip
);
1633 RTS51X_SET_STAT(chip
, STAT_RUN
);
1635 ms_cleanup_work(chip
);
1637 if (!check_card_ready(chip
, lun
)) {
1638 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1639 TRACE_RET(chip
, TRANSPORT_FAILED
);
1641 if ((get_lun_card(chip
, lun
) != MS_CARD
)) {
1642 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT
);
1643 TRACE_RET(chip
, TRANSPORT_FAILED
);
1646 if (srb
->cmnd
[7] != KC_MG_R_PRO
) {
1647 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1648 TRACE_RET(chip
, TRANSPORT_FAILED
);
1651 if (!CHK_MSPRO(ms_card
)) {
1652 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
1653 TRACE_RET(chip
, TRANSPORT_FAILED
);
1656 key_format
= srb
->cmnd
[10] & 0x3F;
1658 switch (key_format
) {
1659 case KF_GET_LOC_EKB
:
1660 if ((scsi_bufflen(srb
) == 0x41C) &&
1661 (srb
->cmnd
[8] == 0x04) && (srb
->cmnd
[9] == 0x1C)) {
1662 retval
= mg_get_local_EKB(srb
, chip
);
1663 if (retval
!= STATUS_SUCCESS
)
1664 TRACE_RET(chip
, TRANSPORT_FAILED
);
1666 set_sense_type(chip
, lun
,
1667 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1668 TRACE_RET(chip
, TRANSPORT_FAILED
);
1673 if ((scsi_bufflen(srb
) == 0x24) &&
1674 (srb
->cmnd
[8] == 0x00) && (srb
->cmnd
[9] == 0x24)) {
1675 retval
= mg_get_rsp_chg(srb
, chip
);
1676 if (retval
!= STATUS_SUCCESS
)
1677 TRACE_RET(chip
, TRANSPORT_FAILED
);
1679 set_sense_type(chip
, lun
,
1680 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1681 TRACE_RET(chip
, TRANSPORT_FAILED
);
1686 ms_card
->mg_entry_num
= srb
->cmnd
[5];
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)) {
1693 retval
= mg_get_ICV(srb
, chip
);
1694 if (retval
!= STATUS_SUCCESS
)
1695 TRACE_RET(chip
, TRANSPORT_FAILED
);
1697 set_sense_type(chip
, lun
,
1698 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1699 TRACE_RET(chip
, TRANSPORT_FAILED
);
1704 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1705 TRACE_RET(chip
, TRANSPORT_FAILED
);
1708 scsi_set_resid(srb
, 0);
1709 return TRANSPORT_GOOD
;
1712 static int mg_send_key(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1714 struct ms_info
*ms_card
= &(chip
->ms_card
);
1715 unsigned int lun
= SCSI_LUN(srb
);
1719 rts51x_prepare_run(chip
);
1720 RTS51X_SET_STAT(chip
, STAT_RUN
);
1722 ms_cleanup_work(chip
);
1724 if (!check_card_ready(chip
, lun
)) {
1725 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_NOT_PRESENT
);
1726 TRACE_RET(chip
, TRANSPORT_FAILED
);
1728 if (check_card_wp(chip
, lun
)) {
1729 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_WRITE_PROTECT
);
1730 TRACE_RET(chip
, TRANSPORT_FAILED
);
1732 if ((get_lun_card(chip
, lun
) != MS_CARD
)) {
1733 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT
);
1734 TRACE_RET(chip
, TRANSPORT_FAILED
);
1737 if (srb
->cmnd
[7] != KC_MG_R_PRO
) {
1738 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1739 TRACE_RET(chip
, TRANSPORT_FAILED
);
1742 if (!CHK_MSPRO(ms_card
)) {
1743 set_sense_type(chip
, lun
, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM
);
1744 TRACE_RET(chip
, TRANSPORT_FAILED
);
1747 key_format
= srb
->cmnd
[10] & 0x3F;
1749 switch (key_format
) {
1750 case KF_SET_LEAF_ID
:
1751 if ((scsi_bufflen(srb
) == 0x0C) &&
1752 (srb
->cmnd
[8] == 0x00) && (srb
->cmnd
[9] == 0x0C)) {
1753 retval
= mg_set_leaf_id(srb
, chip
);
1754 if (retval
!= STATUS_SUCCESS
)
1755 TRACE_RET(chip
, TRANSPORT_FAILED
);
1757 set_sense_type(chip
, lun
,
1758 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1759 TRACE_RET(chip
, TRANSPORT_FAILED
);
1764 if ((scsi_bufflen(srb
) == 0x0C) &&
1765 (srb
->cmnd
[8] == 0x00) && (srb
->cmnd
[9] == 0x0C)) {
1766 retval
= mg_chg(srb
, chip
);
1767 if (retval
!= STATUS_SUCCESS
)
1768 TRACE_RET(chip
, TRANSPORT_FAILED
);
1770 set_sense_type(chip
, lun
,
1771 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1772 TRACE_RET(chip
, TRANSPORT_FAILED
);
1777 if ((scsi_bufflen(srb
) == 0x0C) &&
1778 (srb
->cmnd
[8] == 0x00) && (srb
->cmnd
[9] == 0x0C)) {
1779 retval
= mg_rsp(srb
, chip
);
1780 if (retval
!= STATUS_SUCCESS
)
1781 TRACE_RET(chip
, TRANSPORT_FAILED
);
1783 set_sense_type(chip
, lun
,
1784 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1785 TRACE_RET(chip
, TRANSPORT_FAILED
);
1790 ms_card
->mg_entry_num
= srb
->cmnd
[5];
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)) {
1797 retval
= mg_set_ICV(srb
, chip
);
1798 if (retval
!= STATUS_SUCCESS
)
1799 TRACE_RET(chip
, TRANSPORT_FAILED
);
1801 set_sense_type(chip
, lun
,
1802 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1803 TRACE_RET(chip
, TRANSPORT_FAILED
);
1808 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1809 TRACE_RET(chip
, TRANSPORT_FAILED
);
1812 scsi_set_resid(srb
, 0);
1813 return TRANSPORT_GOOD
;
1817 int rts51x_scsi_handler(struct scsi_cmnd
*srb
, struct rts51x_chip
*chip
)
1819 struct ms_info
*ms_card
= &(chip
->ms_card
);
1820 unsigned int lun
= SCSI_LUN(srb
);
1821 int result
= TRANSPORT_GOOD
;
1823 if ((get_lun_card(chip
, lun
) == MS_CARD
) &&
1824 (ms_card
->format_status
== FORMAT_IN_PROGRESS
)) {
1825 if ((srb
->cmnd
[0] != REQUEST_SENSE
)
1826 && (srb
->cmnd
[0] != INQUIRY
)) {
1827 /* Logical Unit Not Ready Format in Progress */
1828 set_sense_data(chip
, lun
, CUR_ERR
, 0x02, 0, 0x04, 0x04,
1829 0, (u16
) (ms_card
->progress
));
1830 TRACE_RET(chip
, TRANSPORT_FAILED
);
1834 switch (srb
->cmnd
[0]) {
1839 result
= read_write(srb
, chip
);
1842 case TEST_UNIT_READY
:
1843 result
= test_unit_ready(srb
, chip
);
1847 result
= inquiry(srb
, chip
);
1851 result
= read_capacity(srb
, chip
);
1855 result
= start_stop_unit(srb
, chip
);
1858 case ALLOW_MEDIUM_REMOVAL
:
1859 result
= allow_medium_removal(srb
, chip
);
1863 result
= request_sense(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
);
1884 case SD_PASS_THRU_MODE
:
1885 case SD_EXECUTE_NO_DATA
:
1886 case SD_EXECUTE_READ
:
1887 case SD_EXECUTE_WRITE
:
1890 result
= sd_extention_cmnd(srb
, chip
);
1894 #ifdef SUPPORT_MAGIC_GATE
1895 case CMD_MSPRO_MG_RKEY
:
1896 result
= mg_report_key(srb
, chip
);
1899 case CMD_MSPRO_MG_SKEY
:
1900 result
= mg_send_key(srb
, chip
);
1907 result
= TRANSPORT_GOOD
;
1911 set_sense_type(chip
, lun
, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD
);
1912 result
= TRANSPORT_FAILED
;
1918 /***********************************************************************
1920 ***********************************************************************/
1922 int slave_alloc(struct scsi_device
*sdev
)
1925 * Set the INQUIRY transfer length to 36. We don't use any of
1926 * the extra data and many devices choke if asked for more or
1927 * less than 36 bytes.
1929 sdev
->inquiry_len
= 36;
1933 int slave_configure(struct scsi_device
*sdev
)
1935 /* Scatter-gather buffers (all but the last) must have a length
1936 * divisible by the bulk maxpacket size. Otherwise a data packet
1937 * would end up being short, causing a premature end to the data
1938 * transfer. Since high-speed bulk pipes have a maxpacket size
1939 * of 512, we'll use that as the scsi device queue's DMA alignment
1940 * mask. Guaranteeing proper alignment of the first buffer will
1941 * have the desired effect because, except at the beginning and
1942 * the end, scatter-gather buffers follow page boundaries. */
1943 blk_queue_dma_alignment(sdev
->request_queue
, (512 - 1));
1945 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
1946 * what is originally reported. We need this to avoid confusing
1947 * the SCSI layer with devices that report 0 or 1, but need 10-byte
1948 * commands (ala ATAPI devices behind certain bridges, or devices
1949 * which simply have broken INQUIRY data).
1951 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
1952 * actual information. This seems to be the preference for
1953 * programs like that.
1955 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
1956 * the actual value or the modified one, depending on where the
1959 if (sdev
->scsi_level
< SCSI_2
)
1960 sdev
->scsi_level
= sdev
->sdev_target
->scsi_level
= SCSI_2
;
1965 /***********************************************************************
1966 * /proc/scsi/ functions
1967 ***********************************************************************/
1969 /* we use this macro to help us write into the buffer */
1971 #define SPRINTF(args...) \
1972 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
1974 int proc_info(struct Scsi_Host
*host
, char *buffer
,
1975 char **start
, off_t offset
, int length
, int inout
)
1979 /* if someone is sending us data, just throw it away */
1983 /* print the controller name */
1984 SPRINTF(" Host scsi%d: %s\n", host
->host_no
, RTS51X_NAME
);
1986 /* print product, vendor, and driver version strings */
1987 SPRINTF(" Vendor: Realtek Corp.\n");
1988 SPRINTF(" Product: RTS51xx USB Card Reader\n");
1989 SPRINTF(" Version: %s\n", DRIVER_VERSION
);
1990 SPRINTF(" Build: %s\n", __TIME__
);
1993 * Calculate start of next buffer, and return value.
1995 *start
= buffer
+ offset
;
1997 if ((pos
- buffer
) < offset
)
1999 else if ((pos
- buffer
- offset
) < length
)
2000 return pos
- buffer
- offset
;
2005 /* queue a command */
2006 /* This is always called with scsi_lock(host) held */
2007 int queuecommand_lck(struct scsi_cmnd
*srb
, void (*done
) (struct scsi_cmnd
*))
2009 struct rts51x_chip
*chip
= host_to_rts51x(srb
->device
->host
);
2011 /* check for state-transition errors */
2012 if (chip
->srb
!= NULL
) {
2013 RTS51X_DEBUGP("Error in %s: chip->srb = %p\n",
2014 __func__
, chip
->srb
);
2015 return SCSI_MLQUEUE_HOST_BUSY
;
2018 /* fail the command if we are disconnecting */
2019 if (test_bit(FLIDX_DISCONNECTING
, &chip
->usb
->dflags
)) {
2020 RTS51X_DEBUGP("Fail command during disconnect\n");
2021 srb
->result
= DID_NO_CONNECT
<< 16;
2026 /* enqueue the command and wake up the control thread */
2027 srb
->scsi_done
= done
;
2029 complete(&chip
->usb
->cmnd_ready
);
2034 DEF_SCSI_QCMD(queuecommand
)
2035 /***********************************************************************
2036 * Error handling functions
2037 ***********************************************************************/
2038 /* Command timeout and abort */
2039 int command_abort(struct scsi_cmnd
*srb
)
2041 struct rts51x_chip
*chip
= host_to_rts51x(srb
->device
->host
);
2043 RTS51X_DEBUGP("%s called\n", __func__
);
2045 /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
2046 * bits are protected by the host lock. */
2047 scsi_lock(rts51x_to_host(chip
));
2049 /* Is this command still active? */
2050 if (chip
->srb
!= srb
) {
2051 scsi_unlock(rts51x_to_host(chip
));
2052 RTS51X_DEBUGP("-- nothing to abort\n");
2056 /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
2057 * a device reset isn't already in progress (to avoid interfering
2058 * with the reset). Note that we must retain the host lock while
2059 * calling usb_stor_stop_transport(); otherwise it might interfere
2060 * with an auto-reset that begins as soon as we release the lock. */
2061 set_bit(FLIDX_TIMED_OUT
, &chip
->usb
->dflags
);
2062 if (!test_bit(FLIDX_RESETTING
, &chip
->usb
->dflags
)) {
2063 set_bit(FLIDX_ABORTING
, &chip
->usb
->dflags
);
2064 /* rts51x_stop_transport(us); */
2066 scsi_unlock(rts51x_to_host(chip
));
2068 /* Wait for the aborted command to finish */
2069 wait_for_completion(&chip
->usb
->notify
);
2073 /* This invokes the transport reset mechanism to reset the state of the
2075 int device_reset(struct scsi_cmnd
*srb
)
2079 RTS51X_DEBUGP("%s called\n", __func__
);
2081 return result
< 0 ? FAILED
: SUCCESS
;
2084 /* Simulate a SCSI bus reset by resetting the device's USB port. */
2085 int bus_reset(struct scsi_cmnd
*srb
)
2089 RTS51X_DEBUGP("%s called\n", __func__
);
2091 return result
< 0 ? FAILED
: SUCCESS
;
2094 static const char *rts5139_info(struct Scsi_Host
*host
)
2096 return "SCSI emulation for RTS5139 USB card reader";
2099 struct scsi_host_template rts51x_host_template
= {
2100 /* basic userland interface stuff */
2101 .name
= RTS51X_NAME
,
2102 .proc_name
= RTS51X_NAME
,
2103 .proc_info
= proc_info
,
2104 .info
= rts5139_info
,
2106 /* command interface -- queued only */
2107 .queuecommand
= queuecommand
,
2109 /* error and abort handlers */
2110 .eh_abort_handler
= command_abort
,
2111 .eh_device_reset_handler
= device_reset
,
2112 .eh_bus_reset_handler
= bus_reset
,
2114 /* queue commands only, only one command per LUN */
2118 /* unknown initiator id */
2121 .slave_alloc
= slave_alloc
,
2122 .slave_configure
= slave_configure
,
2124 /* lots of sg segments can be handled */
2125 .sg_tablesize
= SG_ALL
,
2127 /* limit the total size of a transfer to 120 KB */
2130 /* merge commands... this seems to help performance, but
2131 * periodically someone should test to see which setting is more
2134 .use_clustering
= 1,
2139 /* we do our own delay after a device or bus reset */
2140 .skip_settle_delay
= 1,
2142 /* sysfs device attributes */
2143 /* .sdev_attrs = sysfs_device_attr_list, */
2145 /* module management */
2146 .module
= THIS_MODULE