spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / staging / rts5139 / rts51x_scsi.c
blob87c9cdc8bd29866446783f85e09d4c923ed9c7f5
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
8 * later version.
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/>.
18 * Author:
19 * wwang (wei_wang@realsil.com.cn)
20 * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
21 * Maintainer:
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>
37 #include "debug.h"
38 #include "rts51x.h"
39 #include "rts51x_chip.h"
40 #include "rts51x_scsi.h"
41 #include "rts51x_card.h"
42 #include "rts51x_transport.h"
43 #include "rts51x_sys.h"
44 #include "sd_cprm.h"
45 #include "ms_mg.h"
46 #include "trace.h"
48 void scsi_show_command(struct scsi_cmnd *srb)
50 char *what = NULL;
51 int i, unknown_cmd = 0;
53 switch (srb->cmnd[0]) {
54 case TEST_UNIT_READY:
55 what = (char *)"TEST_UNIT_READY";
56 break;
57 case REZERO_UNIT:
58 what = (char *)"REZERO_UNIT";
59 break;
60 case REQUEST_SENSE:
61 what = (char *)"REQUEST_SENSE";
62 break;
63 case FORMAT_UNIT:
64 what = (char *)"FORMAT_UNIT";
65 break;
66 case READ_BLOCK_LIMITS:
67 what = (char *)"READ_BLOCK_LIMITS";
68 break;
69 case 0x07:
70 what = (char *)"REASSIGN_BLOCKS";
71 break;
72 case READ_6:
73 what = (char *)"READ_6";
74 break;
75 case WRITE_6:
76 what = (char *)"WRITE_6";
77 break;
78 case SEEK_6:
79 what = (char *)"SEEK_6";
80 break;
81 case READ_REVERSE:
82 what = (char *)"READ_REVERSE";
83 break;
84 case WRITE_FILEMARKS:
85 what = (char *)"WRITE_FILEMARKS";
86 break;
87 case SPACE:
88 what = (char *)"SPACE";
89 break;
90 case INQUIRY:
91 what = (char *)"INQUIRY";
92 break;
93 case RECOVER_BUFFERED_DATA:
94 what = (char *)"RECOVER_BUFFERED_DATA";
95 break;
96 case MODE_SELECT:
97 what = (char *)"MODE_SELECT";
98 break;
99 case RESERVE:
100 what = (char *)"RESERVE";
101 break;
102 case RELEASE:
103 what = (char *)"RELEASE";
104 break;
105 case COPY:
106 what = (char *)"COPY";
107 break;
108 case ERASE:
109 what = (char *)"ERASE";
110 break;
111 case MODE_SENSE:
112 what = (char *)"MODE_SENSE";
113 break;
114 case START_STOP:
115 what = (char *)"START_STOP";
116 break;
117 case RECEIVE_DIAGNOSTIC:
118 what = (char *)"RECEIVE_DIAGNOSTIC";
119 break;
120 case SEND_DIAGNOSTIC:
121 what = (char *)"SEND_DIAGNOSTIC";
122 break;
123 case ALLOW_MEDIUM_REMOVAL:
124 what = (char *)"ALLOW_MEDIUM_REMOVAL";
125 break;
126 case SET_WINDOW:
127 what = (char *)"SET_WINDOW";
128 break;
129 case READ_CAPACITY:
130 what = (char *)"READ_CAPACITY";
131 break;
132 case READ_10:
133 what = (char *)"READ_10";
134 break;
135 case WRITE_10:
136 what = (char *)"WRITE_10";
137 break;
138 case SEEK_10:
139 what = (char *)"SEEK_10";
140 break;
141 case WRITE_VERIFY:
142 what = (char *)"WRITE_VERIFY";
143 break;
144 case VERIFY:
145 what = (char *)"VERIFY";
146 break;
147 case SEARCH_HIGH:
148 what = (char *)"SEARCH_HIGH";
149 break;
150 case SEARCH_EQUAL:
151 what = (char *)"SEARCH_EQUAL";
152 break;
153 case SEARCH_LOW:
154 what = (char *)"SEARCH_LOW";
155 break;
156 case SET_LIMITS:
157 what = (char *)"SET_LIMITS";
158 break;
159 case READ_POSITION:
160 what = (char *)"READ_POSITION";
161 break;
162 case SYNCHRONIZE_CACHE:
163 what = (char *)"SYNCHRONIZE_CACHE";
164 break;
165 case LOCK_UNLOCK_CACHE:
166 what = (char *)"LOCK_UNLOCK_CACHE";
167 break;
168 case READ_DEFECT_DATA:
169 what = (char *)"READ_DEFECT_DATA";
170 break;
171 case MEDIUM_SCAN:
172 what = (char *)"MEDIUM_SCAN";
173 break;
174 case COMPARE:
175 what = (char *)"COMPARE";
176 break;
177 case COPY_VERIFY:
178 what = (char *)"COPY_VERIFY";
179 break;
180 case WRITE_BUFFER:
181 what = (char *)"WRITE_BUFFER";
182 break;
183 case READ_BUFFER:
184 what = (char *)"READ_BUFFER";
185 break;
186 case UPDATE_BLOCK:
187 what = (char *)"UPDATE_BLOCK";
188 break;
189 case READ_LONG:
190 what = (char *)"READ_LONG";
191 break;
192 case WRITE_LONG:
193 what = (char *)"WRITE_LONG";
194 break;
195 case CHANGE_DEFINITION:
196 what = (char *)"CHANGE_DEFINITION";
197 break;
198 case WRITE_SAME:
199 what = (char *)"WRITE_SAME";
200 break;
201 case GPCMD_READ_SUBCHANNEL:
202 what = (char *)"READ SUBCHANNEL";
203 break;
204 case READ_TOC:
205 what = (char *)"READ_TOC";
206 break;
207 case GPCMD_READ_HEADER:
208 what = (char *)"READ HEADER";
209 break;
210 case GPCMD_PLAY_AUDIO_10:
211 what = (char *)"PLAY AUDIO (10)";
212 break;
213 case GPCMD_PLAY_AUDIO_MSF:
214 what = (char *)"PLAY AUDIO MSF";
215 break;
216 case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
217 what = (char *)"GET EVENT/STATUS NOTIFICATION";
218 break;
219 case GPCMD_PAUSE_RESUME:
220 what = (char *)"PAUSE/RESUME";
221 break;
222 case LOG_SELECT:
223 what = (char *)"LOG_SELECT";
224 break;
225 case LOG_SENSE:
226 what = (char *)"LOG_SENSE";
227 break;
228 case GPCMD_STOP_PLAY_SCAN:
229 what = (char *)"STOP PLAY/SCAN";
230 break;
231 case GPCMD_READ_DISC_INFO:
232 what = (char *)"READ DISC INFORMATION";
233 break;
234 case GPCMD_READ_TRACK_RZONE_INFO:
235 what = (char *)"READ TRACK INFORMATION";
236 break;
237 case GPCMD_RESERVE_RZONE_TRACK:
238 what = (char *)"RESERVE TRACK";
239 break;
240 case GPCMD_SEND_OPC:
241 what = (char *)"SEND OPC";
242 break;
243 case MODE_SELECT_10:
244 what = (char *)"MODE_SELECT_10";
245 break;
246 case GPCMD_REPAIR_RZONE_TRACK:
247 what = (char *)"REPAIR TRACK";
248 break;
249 case 0x59:
250 what = (char *)"READ MASTER CUE";
251 break;
252 case MODE_SENSE_10:
253 what = (char *)"MODE_SENSE_10";
254 break;
255 case GPCMD_CLOSE_TRACK:
256 what = (char *)"CLOSE TRACK/SESSION";
257 break;
258 case 0x5C:
259 what = (char *)"READ BUFFER CAPACITY";
260 break;
261 case 0x5D:
262 what = (char *)"SEND CUE SHEET";
263 break;
264 case GPCMD_BLANK:
265 what = (char *)"BLANK";
266 break;
267 case REPORT_LUNS:
268 what = (char *)"REPORT LUNS";
269 break;
270 case MOVE_MEDIUM:
271 what = (char *)"MOVE_MEDIUM or PLAY AUDIO (12)";
272 break;
273 case READ_12:
274 what = (char *)"READ_12";
275 break;
276 case WRITE_12:
277 what = (char *)"WRITE_12";
278 break;
279 case WRITE_VERIFY_12:
280 what = (char *)"WRITE_VERIFY_12";
281 break;
282 case SEARCH_HIGH_12:
283 what = (char *)"SEARCH_HIGH_12";
284 break;
285 case SEARCH_EQUAL_12:
286 what = (char *)"SEARCH_EQUAL_12";
287 break;
288 case SEARCH_LOW_12:
289 what = (char *)"SEARCH_LOW_12";
290 break;
291 case SEND_VOLUME_TAG:
292 what = (char *)"SEND_VOLUME_TAG";
293 break;
294 case READ_ELEMENT_STATUS:
295 what = (char *)"READ_ELEMENT_STATUS";
296 break;
297 case GPCMD_READ_CD_MSF:
298 what = (char *)"READ CD MSF";
299 break;
300 case GPCMD_SCAN:
301 what = (char *)"SCAN";
302 break;
303 case GPCMD_SET_SPEED:
304 what = (char *)"SET CD SPEED";
305 break;
306 case GPCMD_MECHANISM_STATUS:
307 what = (char *)"MECHANISM STATUS";
308 break;
309 case GPCMD_READ_CD:
310 what = (char *)"READ CD";
311 break;
312 case 0xE1:
313 what = (char *)"WRITE CONTINUE";
314 break;
315 case WRITE_LONG_2:
316 what = (char *)"WRITE_LONG_2";
317 break;
318 case VENDOR_CMND:
319 what = (char *)"Realtek's vendor command";
320 break;
321 default:
322 what = (char *)"(unknown command)";
323 unknown_cmd = 1;
324 break;
327 if (srb->cmnd[0] != TEST_UNIT_READY)
328 RTS51X_DEBUGP("Command %s (%d bytes)\n", what, srb->cmd_len);
329 if (unknown_cmd) {
330 RTS51X_DEBUGP("");
331 for (i = 0; i < srb->cmd_len && i < 16; i++)
332 RTS51X_DEBUGPN(" %02x", srb->cmnd[i]);
333 RTS51X_DEBUGPN("\n");
337 void set_sense_type(struct rts51x_chip *chip, unsigned int lun, int sense_type)
339 switch (sense_type) {
340 case SENSE_TYPE_MEDIA_CHANGE:
341 set_sense_data(chip, lun, CUR_ERR, 0x06, 0, 0x28, 0, 0, 0);
342 break;
344 case SENSE_TYPE_MEDIA_NOT_PRESENT:
345 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x3A, 0, 0, 0);
346 break;
348 case SENSE_TYPE_MEDIA_LBA_OVER_RANGE:
349 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x21, 0, 0, 0);
350 break;
352 case SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT:
353 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x25, 0, 0, 0);
354 break;
356 case SENSE_TYPE_MEDIA_WRITE_PROTECT:
357 set_sense_data(chip, lun, CUR_ERR, 0x07, 0, 0x27, 0, 0, 0);
358 break;
360 case SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR:
361 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x11, 0, 0, 0);
362 break;
364 case SENSE_TYPE_MEDIA_WRITE_ERR:
365 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x0C, 0x02, 0, 0);
366 break;
368 case SENSE_TYPE_MEDIA_INVALID_CMD_FIELD:
369 set_sense_data(chip, lun, CUR_ERR, ILGAL_REQ, 0,
370 ASC_INVLD_CDB, ASCQ_INVLD_CDB, CDB_ILLEGAL, 1);
371 break;
373 case SENSE_TYPE_FORMAT_IN_PROGRESS:
374 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04, 0, 0);
375 break;
377 case SENSE_TYPE_FORMAT_CMD_FAILED:
378 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x31, 0x01, 0, 0);
379 break;
381 #ifdef SUPPORT_MAGIC_GATE
382 case SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB:
383 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x6F, 0x02, 0, 0);
384 break;
386 case SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN:
387 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x6F, 0x00, 0, 0);
388 break;
390 case SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM:
391 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x30, 0x00, 0, 0);
392 break;
394 case SENSE_TYPE_MG_WRITE_ERR:
395 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x0C, 0x00, 0, 0);
396 break;
397 #endif
399 #ifdef SUPPORT_SD_LOCK
400 case SENSE_TYPE_MEDIA_READ_FORBIDDEN:
401 set_sense_data(chip, lun, CUR_ERR, 0x07, 0, 0x11, 0x13, 0, 0);
402 break;
403 #endif
405 case SENSE_TYPE_NO_SENSE:
406 default:
407 set_sense_data(chip, lun, CUR_ERR, 0, 0, 0, 0, 0, 0);
408 break;
412 void set_sense_data(struct rts51x_chip *chip, unsigned int lun, u8 err_code,
413 u8 sense_key, u32 info, u8 asc, u8 ascq, u8 sns_key_info0,
414 u16 sns_key_info1)
416 struct sense_data_t *sense = &(chip->sense_buffer[lun]);
418 sense->err_code = err_code;
419 sense->sense_key = sense_key;
420 sense->info[0] = (u8) (info >> 24);
421 sense->info[1] = (u8) (info >> 16);
422 sense->info[2] = (u8) (info >> 8);
423 sense->info[3] = (u8) info;
425 sense->ad_sense_len = sizeof(struct sense_data_t) - 8;
426 sense->asc = asc;
427 sense->ascq = ascq;
428 if (sns_key_info0 != 0) {
429 sense->sns_key_info[0] = SKSV | sns_key_info0;
430 sense->sns_key_info[1] = (sns_key_info1 & 0xf0) >> 8;
431 sense->sns_key_info[2] = sns_key_info1 & 0x0f;
435 static int test_unit_ready(struct scsi_cmnd *srb, struct rts51x_chip *chip)
437 unsigned int lun = SCSI_LUN(srb);
439 rts51x_init_cards(chip);
441 if (!check_card_ready(chip, lun)) {
442 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
443 return TRANSPORT_FAILED;
446 if (!check_lun_mc(chip, lun)) {
447 set_lun_mc(chip, lun);
448 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
449 return TRANSPORT_FAILED;
451 #ifdef SUPPORT_SD_LOCK
452 if (get_lun_card(chip, SCSI_LUN(srb)) == SD_CARD) {
453 struct sd_info *sd_card = &(chip->sd_card);
454 if (sd_card->sd_lock_notify) {
455 sd_card->sd_lock_notify = 0;
456 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
457 return TRANSPORT_FAILED;
458 } else if (sd_card->sd_lock_status & SD_LOCKED) {
459 set_sense_type(chip, lun,
460 SENSE_TYPE_MEDIA_READ_FORBIDDEN);
461 return TRANSPORT_FAILED;
464 #endif
466 return TRANSPORT_GOOD;
469 unsigned char formatter_inquiry_str[20] = {
470 'M', 'E', 'M', 'O', 'R', 'Y', 'S', 'T', 'I', 'C', 'K',
471 '-', 'M', 'G', /* Byte[47:49] */
472 0x0B, /* Byte[50]: MG, MS, MSPro, MSXC */
473 0x00, /* Byte[51]: Category Specific Commands */
474 0x00, /* Byte[52]: Access Control and feature */
475 0x20, 0x20, 0x20, /* Byte[53:55] */
478 static int inquiry(struct scsi_cmnd *srb, struct rts51x_chip *chip)
480 unsigned int lun = SCSI_LUN(srb);
481 char *inquiry_default = (char *)"Generic-xD/SD/M.S. 1.00 ";
482 char *inquiry_string;
483 unsigned char sendbytes;
484 unsigned char *buf;
485 u8 card = get_lun_card(chip, lun);
486 int pro_formatter_flag = 0;
487 unsigned char inquiry_buf[] = {
488 QULIFIRE | DRCT_ACCESS_DEV,
489 RMB_DISC | 0x0D,
490 0x00,
491 0x01,
492 0x1f,
493 0x02,
495 REL_ADR | WBUS_32 | WBUS_16 | SYNC | LINKED | CMD_QUE | SFT_RE,
498 inquiry_string = inquiry_default;
500 buf = vmalloc(scsi_bufflen(srb));
501 if (buf == NULL)
502 TRACE_RET(chip, TRANSPORT_ERROR);
504 if (MS_FORMATTER_ENABLED(chip) && (get_lun2card(chip, lun) & MS_CARD)) {
505 if (!card || (card == MS_CARD))
506 pro_formatter_flag = 1;
509 if (pro_formatter_flag) {
510 if (scsi_bufflen(srb) < 56)
511 sendbytes = (unsigned char)(scsi_bufflen(srb));
512 else
513 sendbytes = 56;
514 } else {
515 if (scsi_bufflen(srb) < 36)
516 sendbytes = (unsigned char)(scsi_bufflen(srb));
517 else
518 sendbytes = 36;
521 if (sendbytes > 8) {
522 memcpy(buf, inquiry_buf, 8);
523 memcpy(buf + 8, inquiry_string, sendbytes - 8);
524 if (pro_formatter_flag)
525 buf[4] = 0x33; /* Additional Length */
526 } else {
527 memcpy(buf, inquiry_buf, sendbytes);
530 if (pro_formatter_flag) {
531 if (sendbytes > 36)
532 memcpy(buf + 36, formatter_inquiry_str, sendbytes - 36);
535 scsi_set_resid(srb, 0);
537 rts51x_set_xfer_buf(buf, scsi_bufflen(srb), srb);
538 vfree(buf);
540 return TRANSPORT_GOOD;
543 static int start_stop_unit(struct scsi_cmnd *srb, struct rts51x_chip *chip)
545 unsigned int lun = SCSI_LUN(srb);
547 scsi_set_resid(srb, scsi_bufflen(srb));
549 if (srb->cmnd[1] == 1)
550 return TRANSPORT_GOOD;
552 switch (srb->cmnd[0x4]) {
553 case STOP_MEDIUM:
554 /* Media disabled */
555 return TRANSPORT_GOOD;
557 case UNLOAD_MEDIUM:
558 /* Media shall be unload */
559 if (check_card_ready(chip, lun))
560 eject_card(chip, lun);
561 return TRANSPORT_GOOD;
563 case MAKE_MEDIUM_READY:
564 case LOAD_MEDIUM:
565 if (check_card_ready(chip, lun)) {
566 return TRANSPORT_GOOD;
567 } else {
568 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
569 TRACE_RET(chip, TRANSPORT_FAILED);
572 break;
575 TRACE_RET(chip, TRANSPORT_ERROR);
578 static int allow_medium_removal(struct scsi_cmnd *srb, struct rts51x_chip *chip)
580 int prevent;
582 prevent = srb->cmnd[4] & 0x1;
584 scsi_set_resid(srb, 0);
586 if (prevent) {
587 set_sense_type(chip, SCSI_LUN(srb),
588 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
589 TRACE_RET(chip, TRANSPORT_FAILED);
592 return TRANSPORT_GOOD;
595 static void ms_mode_sense(struct rts51x_chip *chip, u8 cmd,
596 int lun, u8 *buf, int buf_len)
598 struct ms_info *ms_card = &(chip->ms_card);
599 int sys_info_offset;
600 int data_size = buf_len;
601 int support_format = 0;
602 int i = 0;
604 if (cmd == MODE_SENSE) {
605 sys_info_offset = 8;
606 if (data_size > 0x68)
607 data_size = 0x68;
608 buf[i++] = 0x67; /* Mode Data Length */
609 } else {
610 sys_info_offset = 12;
611 if (data_size > 0x6C)
612 data_size = 0x6C;
613 buf[i++] = 0x00; /* Mode Data Length (MSB) */
614 buf[i++] = 0x6A; /* Mode Data Length (LSB) */
617 /* Medium Type Code */
618 if (check_card_ready(chip, lun)) {
619 if (CHK_MSXC(ms_card)) {
620 support_format = 1;
621 buf[i++] = 0x40;
622 } else if (CHK_MSPRO(ms_card)) {
623 support_format = 1;
624 buf[i++] = 0x20;
625 } else {
626 buf[i++] = 0x10;
629 /* WP */
630 if (check_card_wp(chip, lun))
631 buf[i++] = 0x80;
632 else
633 buf[i++] = 0x00;
634 } else {
635 buf[i++] = 0x00; /* MediaType */
636 buf[i++] = 0x00; /* WP */
639 buf[i++] = 0x00; /* Reserved */
641 if (cmd == MODE_SENSE_10) {
642 buf[i++] = 0x00; /* Reserved */
643 buf[i++] = 0x00; /* Block descriptor length(MSB) */
644 buf[i++] = 0x00; /* Block descriptor length(LSB) */
646 /* The Following Data is the content of "Page 0x20" */
647 if (data_size >= 9)
648 buf[i++] = 0x20; /* Page Code */
649 if (data_size >= 10)
650 buf[i++] = 0x62; /* Page Length */
651 if (data_size >= 11)
652 buf[i++] = 0x00; /* No Access Control */
653 if (data_size >= 12) {
654 if (support_format)
655 buf[i++] = 0xC0; /* SF, SGM */
656 else
657 buf[i++] = 0x00;
659 } else {
660 /* The Following Data is the content of "Page 0x20" */
661 if (data_size >= 5)
662 buf[i++] = 0x20; /* Page Code */
663 if (data_size >= 6)
664 buf[i++] = 0x62; /* Page Length */
665 if (data_size >= 7)
666 buf[i++] = 0x00; /* No Access Control */
667 if (data_size >= 8) {
668 if (support_format)
669 buf[i++] = 0xC0; /* SF, SGM */
670 else
671 buf[i++] = 0x00;
675 if (data_size > sys_info_offset) {
676 /* 96 Bytes Attribute Data */
677 int len = data_size - sys_info_offset;
678 len = (len < 96) ? len : 96;
680 memcpy(buf + sys_info_offset, ms_card->raw_sys_info, len);
684 static int mode_sense(struct scsi_cmnd *srb, struct rts51x_chip *chip)
686 unsigned int lun = SCSI_LUN(srb);
687 unsigned int dataSize;
688 int status;
689 int pro_formatter_flag;
690 unsigned char pageCode, *buf;
691 u8 card = get_lun_card(chip, lun);
693 if (!check_card_ready(chip, lun)) {
694 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
695 scsi_set_resid(srb, scsi_bufflen(srb));
696 TRACE_RET(chip, TRANSPORT_FAILED);
699 pro_formatter_flag = 0;
700 dataSize = 8;
701 /* In Combo mode, device responses ModeSense command as a MS LUN
702 * when no card is inserted */
703 if ((get_lun2card(chip, lun) & MS_CARD)) {
704 if (!card || (card == MS_CARD)) {
705 dataSize = 108;
706 if (chip->option.mspro_formatter_enable)
707 pro_formatter_flag = 1;
711 buf = kmalloc(dataSize, GFP_KERNEL);
712 if (buf == NULL)
713 TRACE_RET(chip, TRANSPORT_ERROR);
715 pageCode = srb->cmnd[2] & 0x3f;
717 if ((pageCode == 0x3F) || (pageCode == 0x1C) ||
718 (pageCode == 0x00) || (pro_formatter_flag && (pageCode == 0x20))) {
719 if (srb->cmnd[0] == MODE_SENSE) {
720 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
721 ms_mode_sense(chip, srb->cmnd[0], lun, buf,
722 dataSize);
723 } else {
724 dataSize = 4;
725 buf[0] = 0x03;
726 buf[1] = 0x00;
727 if (check_card_wp(chip, lun))
728 buf[2] = 0x80;
729 else
730 buf[3] = 0x00;
732 } else {
733 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
734 ms_mode_sense(chip, srb->cmnd[0], lun, buf,
735 dataSize);
736 } else {
737 dataSize = 8;
738 buf[0] = 0x00;
739 buf[1] = 0x06;
740 buf[2] = 0x00;
741 if (check_card_wp(chip, lun))
742 buf[3] = 0x80;
743 else
744 buf[3] = 0x00;
745 buf[4] = 0x00;
746 buf[5] = 0x00;
747 buf[6] = 0x00;
748 buf[7] = 0x00;
751 status = TRANSPORT_GOOD;
752 } else {
753 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
754 scsi_set_resid(srb, scsi_bufflen(srb));
755 status = TRANSPORT_FAILED;
758 if (status == TRANSPORT_GOOD) {
759 unsigned int len = min(scsi_bufflen(srb), dataSize);
760 rts51x_set_xfer_buf(buf, len, srb);
761 scsi_set_resid(srb, scsi_bufflen(srb) - len);
763 kfree(buf);
765 return status;
768 static int request_sense(struct scsi_cmnd *srb, struct rts51x_chip *chip)
770 struct sense_data_t *sense;
771 unsigned int lun = SCSI_LUN(srb);
772 struct ms_info *ms_card = &(chip->ms_card);
773 unsigned char *tmp, *buf;
775 sense = &(chip->sense_buffer[lun]);
777 if ((get_lun_card(chip, lun) == MS_CARD)
778 && PRO_UNDER_FORMATTING(ms_card)) {
779 mspro_format_sense(chip, lun);
782 buf = vmalloc(scsi_bufflen(srb));
783 if (buf == NULL)
784 TRACE_RET(chip, TRANSPORT_ERROR);
786 tmp = (unsigned char *)sense;
787 memcpy(buf, tmp, scsi_bufflen(srb));
789 rts51x_set_xfer_buf(buf, scsi_bufflen(srb), srb);
790 vfree(buf);
792 scsi_set_resid(srb, 0);
793 /* Reset Sense Data */
794 set_sense_type(chip, lun, SENSE_TYPE_NO_SENSE);
795 return TRANSPORT_GOOD;
798 static int read_write(struct scsi_cmnd *srb, struct rts51x_chip *chip)
800 #ifdef SUPPORT_SD_LOCK
801 struct sd_info *sd_card = &(chip->sd_card);
802 #endif
803 unsigned int lun = SCSI_LUN(srb);
804 int retval;
805 u32 start_sec;
806 u16 sec_cnt;
808 if (!check_card_ready(chip, lun) || (chip->capacity[lun] == 0)) {
809 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
810 TRACE_RET(chip, TRANSPORT_FAILED);
813 if (!check_lun_mc(chip, lun)) {
814 set_lun_mc(chip, lun);
815 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
816 return TRANSPORT_FAILED;
819 rts51x_prepare_run(chip);
820 RTS51X_SET_STAT(chip, STAT_RUN);
822 #ifdef SUPPORT_SD_LOCK
823 if (sd_card->sd_erase_status) {
824 /* Accessing to any card is forbidden
825 * until the erase procedure of SD is completed */
826 RTS51X_DEBUGP("SD card being erased!\n");
827 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_READ_FORBIDDEN);
828 TRACE_RET(chip, TRANSPORT_FAILED);
831 if (get_lun_card(chip, lun) == SD_CARD) {
832 if (sd_card->sd_lock_status & SD_LOCKED) {
833 RTS51X_DEBUGP("SD card locked!\n");
834 set_sense_type(chip, lun,
835 SENSE_TYPE_MEDIA_READ_FORBIDDEN);
836 TRACE_RET(chip, TRANSPORT_FAILED);
839 #endif
841 if ((srb->cmnd[0] == READ_10) || (srb->cmnd[0] == WRITE_10)) {
842 start_sec =
843 ((u32) srb->cmnd[2] << 24) |
844 ((u32) srb->cmnd[3] << 16) |
845 ((u32) srb->cmnd[4] << 8) |
846 ((u32) srb->cmnd[5]);
847 sec_cnt = ((u16) (srb->cmnd[7]) << 8) | srb->cmnd[8];
848 } else if ((srb->cmnd[0] == READ_6) || (srb->cmnd[0] == WRITE_6)) {
849 start_sec = ((u32) (srb->cmnd[1] & 0x1F) << 16) |
850 ((u32) srb->cmnd[2] << 8) | ((u32) srb->cmnd[3]);
851 sec_cnt = srb->cmnd[4];
852 } else if ((srb->cmnd[0] == VENDOR_CMND) &&
853 (srb->cmnd[1] == SCSI_APP_CMD) &&
854 ((srb->cmnd[2] == PP_READ10) ||
855 (srb->cmnd[2] == PP_WRITE10))) {
856 start_sec = ((u32) srb->cmnd[4] << 24) |
857 ((u32) srb->cmnd[5] << 16) |
858 ((u32) srb->cmnd[6] << 8) |
859 ((u32) srb->cmnd[7]);
860 sec_cnt = ((u16) (srb->cmnd[9]) << 8) | srb->cmnd[10];
861 } else {
862 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
863 TRACE_RET(chip, TRANSPORT_FAILED);
866 if ((start_sec > chip->capacity[lun]) ||
867 ((start_sec + sec_cnt) > chip->capacity[lun])) {
868 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LBA_OVER_RANGE);
869 TRACE_RET(chip, TRANSPORT_FAILED);
872 if (sec_cnt == 0) {
873 scsi_set_resid(srb, 0);
874 return TRANSPORT_GOOD;
877 if ((srb->sc_data_direction == DMA_TO_DEVICE)
878 && check_card_wp(chip, lun)) {
879 RTS51X_DEBUGP("Write protected card!\n");
880 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_PROTECT);
881 TRACE_RET(chip, TRANSPORT_FAILED);
884 retval = card_rw(srb, chip, start_sec, sec_cnt);
885 if (retval != STATUS_SUCCESS) {
886 #if 0
887 if (chip->need_release & chip->lun2card[lun]) {
888 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
889 } else {
890 #endif
891 if (srb->sc_data_direction == DMA_FROM_DEVICE) {
892 set_sense_type(chip, lun,
893 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
894 } else {
895 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
897 #if 0
899 #endif
900 TRACE_RET(chip, TRANSPORT_FAILED);
903 scsi_set_resid(srb, 0);
905 return TRANSPORT_GOOD;
908 static int read_format_capacity(struct scsi_cmnd *srb, struct rts51x_chip *chip)
910 unsigned char *buf;
911 unsigned int lun = SCSI_LUN(srb);
912 unsigned int buf_len;
913 u8 card = get_lun_card(chip, lun);
914 int desc_cnt;
915 int i = 0;
917 if (!check_card_ready(chip, lun)) {
918 if (!chip->option.mspro_formatter_enable) {
919 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
920 TRACE_RET(chip, TRANSPORT_FAILED);
924 buf_len = (scsi_bufflen(srb) > 12) ? 0x14 : 12;
926 buf = kmalloc(buf_len, GFP_KERNEL);
927 if (buf == NULL)
928 TRACE_RET(chip, TRANSPORT_ERROR);
930 buf[i++] = 0;
931 buf[i++] = 0;
932 buf[i++] = 0;
934 /* Capacity List Length */
935 if ((buf_len > 12) && chip->option.mspro_formatter_enable &&
936 (chip->lun2card[lun] & MS_CARD) && (!card || (card == MS_CARD))) {
937 buf[i++] = 0x10;
938 desc_cnt = 2;
939 } else {
940 buf[i++] = 0x08;
941 desc_cnt = 1;
944 while (desc_cnt) {
945 if (check_card_ready(chip, lun)) {
946 buf[i++] = (unsigned char)((chip->capacity[lun]) >> 24);
947 buf[i++] = (unsigned char)((chip->capacity[lun]) >> 16);
948 buf[i++] = (unsigned char)((chip->capacity[lun]) >> 8);
949 buf[i++] = (unsigned char)(chip->capacity[lun]);
951 if (desc_cnt == 2)
952 /* Byte[8]: Descriptor Type: Formatted medium */
953 buf[i++] = 2;
954 else
955 buf[i++] = 0; /* Byte[16] */
956 } else {
957 buf[i++] = 0xFF;
958 buf[i++] = 0xFF;
959 buf[i++] = 0xFF;
960 buf[i++] = 0xFF;
962 if (desc_cnt == 2)
963 /* Byte[8]: Descriptor Type: No medium */
964 buf[i++] = 3;
965 else
966 buf[i++] = 0; /*Byte[16] */
969 buf[i++] = 0x00;
970 buf[i++] = 0x02;
971 buf[i++] = 0x00;
973 desc_cnt--;
976 buf_len = min(scsi_bufflen(srb), buf_len);
977 rts51x_set_xfer_buf(buf, buf_len, srb);
978 kfree(buf);
980 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
982 return TRANSPORT_GOOD;
985 static int read_capacity(struct scsi_cmnd *srb, struct rts51x_chip *chip)
987 unsigned char *buf;
988 unsigned int lun = SCSI_LUN(srb);
990 if (!check_card_ready(chip, lun)) {
991 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
992 TRACE_RET(chip, TRANSPORT_FAILED);
995 if (!check_lun_mc(chip, lun)) {
996 set_lun_mc(chip, lun);
997 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
998 return TRANSPORT_FAILED;
1001 buf = kmalloc(8, GFP_KERNEL);
1002 if (buf == NULL)
1003 TRACE_RET(chip, TRANSPORT_ERROR);
1005 buf[0] = (unsigned char)((chip->capacity[lun] - 1) >> 24);
1006 buf[1] = (unsigned char)((chip->capacity[lun] - 1) >> 16);
1007 buf[2] = (unsigned char)((chip->capacity[lun] - 1) >> 8);
1008 buf[3] = (unsigned char)(chip->capacity[lun] - 1);
1010 buf[4] = 0x00;
1011 buf[5] = 0x00;
1012 buf[6] = 0x02;
1013 buf[7] = 0x00;
1015 rts51x_set_xfer_buf(buf, scsi_bufflen(srb), srb);
1016 kfree(buf);
1018 scsi_set_resid(srb, 0);
1020 return TRANSPORT_GOOD;
1023 static int get_dev_status(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1025 unsigned int lun = SCSI_LUN(srb);
1026 unsigned int buf_len;
1027 u8 status[32] = { 0 };
1029 rts51x_pp_status(chip, lun, status, 32);
1031 buf_len = min(scsi_bufflen(srb), (unsigned int)sizeof(status));
1032 rts51x_set_xfer_buf(status, buf_len, srb);
1033 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
1035 return TRANSPORT_GOOD;
1038 static int read_status(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1040 u8 rts51x_status[16];
1041 unsigned int buf_len;
1042 unsigned int lun = SCSI_LUN(srb);
1044 rts51x_read_status(chip, lun, rts51x_status, 16);
1046 buf_len = min(scsi_bufflen(srb), (unsigned int)sizeof(rts51x_status));
1047 rts51x_set_xfer_buf(rts51x_status, buf_len, srb);
1048 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
1050 return TRANSPORT_GOOD;
1053 static int read_mem(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1055 unsigned int lun = SCSI_LUN(srb);
1056 unsigned short addr, len, i;
1057 int retval;
1058 u8 *buf;
1060 rts51x_prepare_run(chip);
1061 RTS51X_SET_STAT(chip, STAT_RUN);
1063 addr = ((u16) srb->cmnd[2] << 8) | srb->cmnd[3];
1064 len = ((u16) srb->cmnd[4] << 8) | srb->cmnd[5];
1066 if (addr < 0xe000) {
1067 RTS51X_DEBUGP("filter!addr=0x%x\n", addr);
1068 return TRANSPORT_GOOD;
1071 buf = vmalloc(len);
1072 if (!buf)
1073 TRACE_RET(chip, TRANSPORT_ERROR);
1075 for (i = 0; i < len; i++) {
1076 retval = rts51x_ep0_read_register(chip, addr + i, buf + i);
1077 if (retval != STATUS_SUCCESS) {
1078 vfree(buf);
1079 set_sense_type(chip, lun,
1080 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1081 TRACE_RET(chip, TRANSPORT_FAILED);
1085 len = (unsigned short)min(scsi_bufflen(srb), (unsigned int)len);
1086 rts51x_set_xfer_buf(buf, len, srb);
1087 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1089 vfree(buf);
1091 return TRANSPORT_GOOD;
1094 static int write_mem(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1096 unsigned int lun = SCSI_LUN(srb);
1097 unsigned short addr, len, i;
1098 int retval;
1099 u8 *buf;
1101 rts51x_prepare_run(chip);
1102 RTS51X_SET_STAT(chip, STAT_RUN);
1104 addr = ((u16) srb->cmnd[2] << 8) | srb->cmnd[3];
1105 len = ((u16) srb->cmnd[4] << 8) | srb->cmnd[5];
1107 if (addr < 0xe000) {
1108 RTS51X_DEBUGP("filter!addr=0x%x\n", addr);
1109 return TRANSPORT_GOOD;
1112 len = (unsigned short)min(scsi_bufflen(srb), (unsigned int)len);
1113 buf = vmalloc(len);
1114 if (!buf)
1115 TRACE_RET(chip, TRANSPORT_ERROR);
1117 rts51x_get_xfer_buf(buf, len, srb);
1119 for (i = 0; i < len; i++) {
1120 retval =
1121 rts51x_ep0_write_register(chip, addr + i, 0xFF, buf[i]);
1122 if (retval != STATUS_SUCCESS) {
1123 vfree(buf);
1124 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
1125 TRACE_RET(chip, TRANSPORT_FAILED);
1129 vfree(buf);
1130 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1132 return TRANSPORT_GOOD;
1135 static int get_sd_csd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1137 struct sd_info *sd_card = &(chip->sd_card);
1138 unsigned int lun = SCSI_LUN(srb);
1140 if (!check_card_ready(chip, lun)) {
1141 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1142 TRACE_RET(chip, TRANSPORT_FAILED);
1145 if (get_lun_card(chip, lun) != SD_CARD) {
1146 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1147 TRACE_RET(chip, TRANSPORT_FAILED);
1150 scsi_set_resid(srb, 0);
1151 rts51x_set_xfer_buf(sd_card->raw_csd, scsi_bufflen(srb), srb);
1153 return TRANSPORT_GOOD;
1156 static int read_phy_register(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1158 int retval;
1159 u8 addr, len, i;
1160 u8 *buf;
1162 rts51x_prepare_run(chip);
1163 RTS51X_SET_STAT(chip, STAT_RUN);
1165 addr = srb->cmnd[5];
1166 len = srb->cmnd[7];
1168 if (len) {
1169 buf = vmalloc(len);
1170 if (!buf)
1171 TRACE_RET(chip, TRANSPORT_ERROR);
1173 for (i = 0; i < len; i++) {
1174 retval =
1175 rts51x_read_phy_register(chip, addr + i, buf + i);
1176 if (retval != STATUS_SUCCESS) {
1177 vfree(buf);
1178 set_sense_type(chip, SCSI_LUN(srb),
1179 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1180 TRACE_RET(chip, TRANSPORT_FAILED);
1184 len = min(scsi_bufflen(srb), (unsigned int)len);
1185 rts51x_set_xfer_buf(buf, len, srb);
1186 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1188 vfree(buf);
1191 return TRANSPORT_GOOD;
1194 static int write_phy_register(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1196 int retval;
1197 u8 addr, len, i;
1198 u8 *buf;
1200 rts51x_prepare_run(chip);
1201 RTS51X_SET_STAT(chip, STAT_RUN);
1203 addr = srb->cmnd[5];
1204 len = srb->cmnd[7];
1206 if (len) {
1207 len = min(scsi_bufflen(srb), (unsigned int)len);
1209 buf = vmalloc(len);
1210 if (buf == NULL)
1211 TRACE_RET(chip, TRANSPORT_ERROR);
1213 rts51x_get_xfer_buf(buf, len, srb);
1214 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1216 for (i = 0; i < len; i++) {
1217 retval =
1218 rts51x_write_phy_register(chip, addr + i, buf[i]);
1219 if (retval != STATUS_SUCCESS) {
1220 vfree(buf);
1221 set_sense_type(chip, SCSI_LUN(srb),
1222 SENSE_TYPE_MEDIA_WRITE_ERR);
1223 TRACE_RET(chip, TRANSPORT_FAILED);
1227 vfree(buf);
1230 return TRANSPORT_GOOD;
1233 static int get_card_bus_width(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1235 unsigned int lun = SCSI_LUN(srb);
1236 u8 card, bus_width;
1238 if (!check_card_ready(chip, lun)) {
1239 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1240 TRACE_RET(chip, TRANSPORT_FAILED);
1243 card = get_lun_card(chip, lun);
1244 if ((card == SD_CARD) || (card == MS_CARD)) {
1245 bus_width = chip->card_bus_width[lun];
1246 } else {
1247 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1248 TRACE_RET(chip, TRANSPORT_FAILED);
1251 scsi_set_resid(srb, 0);
1252 rts51x_set_xfer_buf(&bus_width, scsi_bufflen(srb), srb);
1254 return TRANSPORT_GOOD;
1257 #ifdef _MSG_TRACE
1258 static int trace_msg_cmd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1260 unsigned char *buf = NULL;
1261 u8 clear;
1262 unsigned int buf_len;
1264 buf_len =
1266 ((2 + MSG_FUNC_LEN + MSG_FILE_LEN + TIME_VAL_LEN) * TRACE_ITEM_CNT);
1268 if ((scsi_bufflen(srb) < buf_len) || (scsi_sglist(srb) == NULL)) {
1269 set_sense_type(chip, SCSI_LUN(srb),
1270 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1271 TRACE_RET(chip, TRANSPORT_FAILED);
1274 clear = srb->cmnd[2];
1276 buf = vmalloc(scsi_bufflen(srb));
1277 if (buf == NULL)
1278 TRACE_RET(chip, TRANSPORT_ERROR);
1280 rts51x_trace_msg(chip, buf, clear);
1282 rts51x_set_xfer_buf(buf, scsi_bufflen(srb), srb);
1283 vfree(buf);
1285 scsi_set_resid(srb, 0);
1286 return TRANSPORT_GOOD;
1288 #endif
1290 static int rw_mem_cmd_buf(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1292 int retval = STATUS_SUCCESS;
1293 unsigned int lun = SCSI_LUN(srb);
1294 u8 cmd_type, mask, value, idx, mode, len;
1295 u16 addr;
1296 u32 timeout;
1298 rts51x_prepare_run(chip);
1299 RTS51X_SET_STAT(chip, STAT_RUN);
1301 switch (srb->cmnd[3]) {
1302 case INIT_BATCHCMD:
1303 rts51x_init_cmd(chip);
1304 break;
1306 case ADD_BATCHCMD:
1307 cmd_type = srb->cmnd[4];
1308 if (cmd_type > 2) {
1309 set_sense_type(chip, lun,
1310 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1311 TRACE_RET(chip, TRANSPORT_FAILED);
1313 addr = (srb->cmnd[5] << 8) | srb->cmnd[6];
1314 mask = srb->cmnd[7];
1315 value = srb->cmnd[8];
1316 rts51x_add_cmd(chip, cmd_type, addr, mask, value);
1317 break;
1319 case SEND_BATCHCMD:
1320 mode = srb->cmnd[4];
1321 len = srb->cmnd[5];
1322 timeout =
1323 ((u32) srb->cmnd[6] << 24) | ((u32) srb->
1324 cmnd[7] << 16) | ((u32) srb->
1325 cmnd[8] <<
1326 8) | ((u32)
1327 srb->
1328 cmnd
1329 [9]);
1330 retval = rts51x_send_cmd(chip, mode, 1000);
1331 if (retval != STATUS_SUCCESS) {
1332 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
1333 TRACE_RET(chip, TRANSPORT_FAILED);
1335 if (mode & STAGE_R) {
1336 retval = rts51x_get_rsp(chip, len, timeout);
1337 if (retval != STATUS_SUCCESS) {
1338 set_sense_type(chip, lun,
1339 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1340 TRACE_RET(chip, TRANSPORT_FAILED);
1343 break;
1345 case GET_BATCHRSP:
1346 idx = srb->cmnd[4];
1347 value = chip->rsp_buf[idx];
1348 if (scsi_bufflen(srb) < 1) {
1349 set_sense_type(chip, lun,
1350 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1351 TRACE_RET(chip, TRANSPORT_FAILED);
1353 rts51x_set_xfer_buf(&value, 1, srb);
1354 scsi_set_resid(srb, 0);
1355 break;
1357 default:
1358 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1359 TRACE_RET(chip, TRANSPORT_FAILED);
1362 if (retval != STATUS_SUCCESS) {
1363 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
1364 TRACE_RET(chip, TRANSPORT_FAILED);
1367 return TRANSPORT_GOOD;
1370 static int suit_cmd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1372 int result;
1374 switch (srb->cmnd[3]) {
1375 case INIT_BATCHCMD:
1376 case ADD_BATCHCMD:
1377 case SEND_BATCHCMD:
1378 case GET_BATCHRSP:
1379 result = rw_mem_cmd_buf(srb, chip);
1380 break;
1381 default:
1382 result = TRANSPORT_ERROR;
1385 return result;
1388 static int app_cmd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1390 int result;
1392 switch (srb->cmnd[2]) {
1393 case PP_READ10:
1394 case PP_WRITE10:
1395 result = read_write(srb, chip);
1396 break;
1398 case SUIT_CMD:
1399 result = suit_cmd(srb, chip);
1400 break;
1402 case READ_PHY:
1403 result = read_phy_register(srb, chip);
1404 break;
1406 case WRITE_PHY:
1407 result = write_phy_register(srb, chip);
1408 break;
1410 case GET_DEV_STATUS:
1411 result = get_dev_status(srb, chip);
1412 break;
1414 default:
1415 set_sense_type(chip, SCSI_LUN(srb),
1416 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1417 TRACE_RET(chip, TRANSPORT_FAILED);
1420 return result;
1423 static int vendor_cmnd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1425 int result = TRANSPORT_GOOD;
1427 switch (srb->cmnd[1]) {
1428 case READ_STATUS:
1429 result = read_status(srb, chip);
1430 break;
1432 case READ_MEM:
1433 result = read_mem(srb, chip);
1434 break;
1436 case WRITE_MEM:
1437 result = write_mem(srb, chip);
1438 break;
1440 case GET_BUS_WIDTH:
1441 result = get_card_bus_width(srb, chip);
1442 break;
1444 case GET_SD_CSD:
1445 result = get_sd_csd(srb, chip);
1446 break;
1448 #ifdef _MSG_TRACE
1449 case TRACE_MSG:
1450 result = trace_msg_cmd(srb, chip);
1451 break;
1452 #endif
1454 case SCSI_APP_CMD:
1455 result = app_cmd(srb, chip);
1456 break;
1458 default:
1459 set_sense_type(chip, SCSI_LUN(srb),
1460 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1461 TRACE_RET(chip, TRANSPORT_FAILED);
1464 return result;
1467 static int ms_format_cmnd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1469 struct ms_info *ms_card = &(chip->ms_card);
1470 unsigned int lun = SCSI_LUN(srb);
1471 int retval, quick_format;
1473 if (get_lun_card(chip, lun) != MS_CARD) {
1474 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
1475 TRACE_RET(chip, TRANSPORT_FAILED);
1478 if ((srb->cmnd[3] != 0x4D) || (srb->cmnd[4] != 0x47)
1479 || (srb->cmnd[5] != 0x66) || (srb->cmnd[6] != 0x6D)
1480 || (srb->cmnd[7] != 0x74)) {
1481 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1482 TRACE_RET(chip, TRANSPORT_FAILED);
1485 if (srb->cmnd[8] & 0x01)
1486 quick_format = 0;
1487 else
1488 quick_format = 1;
1490 if (!(chip->card_ready & MS_CARD)) {
1491 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1492 TRACE_RET(chip, TRANSPORT_FAILED);
1495 if (chip->card_wp & MS_CARD) {
1496 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_PROTECT);
1497 TRACE_RET(chip, TRANSPORT_FAILED);
1500 if (!CHK_MSPRO(ms_card)) {
1501 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
1502 TRACE_RET(chip, TRANSPORT_FAILED);
1505 rts51x_prepare_run(chip);
1506 RTS51X_SET_STAT(chip, STAT_RUN);
1508 retval = mspro_format(srb, chip, MS_SHORT_DATA_LEN, quick_format);
1509 if (retval != STATUS_SUCCESS) {
1510 set_sense_type(chip, lun, SENSE_TYPE_FORMAT_CMD_FAILED);
1511 TRACE_RET(chip, TRANSPORT_FAILED);
1514 scsi_set_resid(srb, 0);
1515 return TRANSPORT_GOOD;
1518 #ifdef SUPPORT_PCGL_1P18
1519 int get_ms_information(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1521 struct ms_info *ms_card = &(chip->ms_card);
1522 unsigned int lun = SCSI_LUN(srb);
1523 u8 dev_info_id, data_len;
1524 u8 *buf;
1525 unsigned int buf_len;
1526 int i;
1528 if (!check_card_ready(chip, lun)) {
1529 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1530 TRACE_RET(chip, TRANSPORT_FAILED);
1532 if ((get_lun_card(chip, lun) != MS_CARD)) {
1533 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
1534 TRACE_RET(chip, TRANSPORT_FAILED);
1537 if ((srb->cmnd[2] != 0xB0) || (srb->cmnd[4] != 0x4D) ||
1538 (srb->cmnd[5] != 0x53) || (srb->cmnd[6] != 0x49) ||
1539 (srb->cmnd[7] != 0x44)) {
1540 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1541 TRACE_RET(chip, TRANSPORT_FAILED);
1544 dev_info_id = srb->cmnd[3];
1545 if ((CHK_MSXC(ms_card) && (dev_info_id == 0x10)) ||
1546 (!CHK_MSXC(ms_card) && (dev_info_id == 0x13)) ||
1547 !CHK_MSPRO(ms_card)) {
1548 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1549 TRACE_RET(chip, TRANSPORT_FAILED);
1552 if (dev_info_id == 0x15)
1553 buf_len = data_len = 0x3A;
1554 else
1555 buf_len = data_len = 0x6A;
1557 buf = kmalloc(buf_len, GFP_KERNEL);
1558 if (!buf)
1559 TRACE_RET(chip, TRANSPORT_ERROR);
1561 i = 0;
1562 /* GET Memory Stick Media Information Response Header */
1563 buf[i++] = 0x00; /* Data length MSB */
1564 buf[i++] = data_len; /* Data length LSB */
1565 /* Device Information Type Code */
1566 if (CHK_MSXC(ms_card))
1567 buf[i++] = 0x03;
1568 else
1569 buf[i++] = 0x02;
1570 /* SGM bit */
1571 buf[i++] = 0x01;
1572 /* Reserved */
1573 buf[i++] = 0x00;
1574 buf[i++] = 0x00;
1575 buf[i++] = 0x00;
1576 /* Number of Device Information */
1577 buf[i++] = 0x01;
1579 /* Device Information Body
1580 * Device Information ID Number */
1581 buf[i++] = dev_info_id;
1582 /* Device Information Length */
1583 if (dev_info_id == 0x15)
1584 data_len = 0x31;
1585 else
1586 data_len = 0x61;
1587 buf[i++] = 0x00; /* Data length MSB */
1588 buf[i++] = data_len; /* Data length LSB */
1589 /* Valid Bit */
1590 buf[i++] = 0x80;
1591 if ((dev_info_id == 0x10) || (dev_info_id == 0x13)) {
1592 /* System Information */
1593 memcpy(buf + i, ms_card->raw_sys_info, 96);
1594 } else {
1595 /* Model Name */
1596 memcpy(buf + i, ms_card->raw_model_name, 48);
1599 rts51x_set_xfer_buf(buf, buf_len, srb);
1601 if (dev_info_id == 0x15)
1602 scsi_set_resid(srb, scsi_bufflen(srb) - 0x3C);
1603 else
1604 scsi_set_resid(srb, scsi_bufflen(srb) - 0x6C);
1606 kfree(buf);
1607 return STATUS_SUCCESS;
1609 #endif
1611 static int ms_sp_cmnd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1613 int retval = TRANSPORT_ERROR;
1615 if (srb->cmnd[2] == MS_FORMAT)
1616 retval = ms_format_cmnd(srb, chip);
1617 #ifdef SUPPORT_PCGL_1P18
1618 else if (srb->cmnd[2] == GET_MS_INFORMATION)
1619 retval = get_ms_information(srb, chip);
1620 #endif
1622 return retval;
1625 #ifdef SUPPORT_CPRM
1626 static int sd_extention_cmnd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1628 unsigned int lun = SCSI_LUN(srb);
1629 int result;
1631 rts51x_prepare_run(chip);
1632 RTS51X_SET_STAT(chip, STAT_RUN);
1634 sd_cleanup_work(chip);
1636 if (!check_card_ready(chip, lun)) {
1637 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1638 TRACE_RET(chip, TRANSPORT_FAILED);
1640 if ((get_lun_card(chip, lun) != SD_CARD)) {
1641 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
1642 TRACE_RET(chip, TRANSPORT_FAILED);
1645 switch (srb->cmnd[0]) {
1646 case SD_PASS_THRU_MODE:
1647 result = sd_pass_thru_mode(srb, chip);
1648 break;
1650 case SD_EXECUTE_NO_DATA:
1651 result = sd_execute_no_data(srb, chip);
1652 break;
1654 case SD_EXECUTE_READ:
1655 result = sd_execute_read_data(srb, chip);
1656 break;
1658 case SD_EXECUTE_WRITE:
1659 result = sd_execute_write_data(srb, chip);
1660 break;
1662 case SD_GET_RSP:
1663 result = sd_get_cmd_rsp(srb, chip);
1664 break;
1666 case SD_HW_RST:
1667 result = sd_hw_rst(srb, chip);
1668 break;
1670 default:
1671 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1672 TRACE_RET(chip, TRANSPORT_FAILED);
1675 return result;
1677 #endif
1679 #ifdef SUPPORT_MAGIC_GATE
1680 int mg_report_key(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1682 struct ms_info *ms_card = &(chip->ms_card);
1683 unsigned int lun = SCSI_LUN(srb);
1684 int retval;
1685 u8 key_format;
1687 rts51x_prepare_run(chip);
1688 RTS51X_SET_STAT(chip, STAT_RUN);
1690 ms_cleanup_work(chip);
1692 if (!check_card_ready(chip, lun)) {
1693 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1694 TRACE_RET(chip, TRANSPORT_FAILED);
1696 if ((get_lun_card(chip, lun) != MS_CARD)) {
1697 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
1698 TRACE_RET(chip, TRANSPORT_FAILED);
1701 if (srb->cmnd[7] != KC_MG_R_PRO) {
1702 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1703 TRACE_RET(chip, TRANSPORT_FAILED);
1706 if (!CHK_MSPRO(ms_card)) {
1707 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
1708 TRACE_RET(chip, TRANSPORT_FAILED);
1711 key_format = srb->cmnd[10] & 0x3F;
1713 switch (key_format) {
1714 case KF_GET_LOC_EKB:
1715 if ((scsi_bufflen(srb) == 0x41C) &&
1716 (srb->cmnd[8] == 0x04) && (srb->cmnd[9] == 0x1C)) {
1717 retval = mg_get_local_EKB(srb, chip);
1718 if (retval != STATUS_SUCCESS)
1719 TRACE_RET(chip, TRANSPORT_FAILED);
1720 } else {
1721 set_sense_type(chip, lun,
1722 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1723 TRACE_RET(chip, TRANSPORT_FAILED);
1725 break;
1727 case KF_RSP_CHG:
1728 if ((scsi_bufflen(srb) == 0x24) &&
1729 (srb->cmnd[8] == 0x00) && (srb->cmnd[9] == 0x24)) {
1730 retval = mg_get_rsp_chg(srb, chip);
1731 if (retval != STATUS_SUCCESS)
1732 TRACE_RET(chip, TRANSPORT_FAILED);
1733 } else {
1734 set_sense_type(chip, lun,
1735 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1736 TRACE_RET(chip, TRANSPORT_FAILED);
1738 break;
1740 case KF_GET_ICV:
1741 ms_card->mg_entry_num = srb->cmnd[5];
1742 if ((scsi_bufflen(srb) == 0x404) &&
1743 (srb->cmnd[8] == 0x04) &&
1744 (srb->cmnd[9] == 0x04) &&
1745 (srb->cmnd[2] == 0x00) &&
1746 (srb->cmnd[3] == 0x00) &&
1747 (srb->cmnd[4] == 0x00) && (srb->cmnd[5] < 32)) {
1748 retval = mg_get_ICV(srb, chip);
1749 if (retval != STATUS_SUCCESS)
1750 TRACE_RET(chip, TRANSPORT_FAILED);
1751 } else {
1752 set_sense_type(chip, lun,
1753 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1754 TRACE_RET(chip, TRANSPORT_FAILED);
1756 break;
1758 default:
1759 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1760 TRACE_RET(chip, TRANSPORT_FAILED);
1763 scsi_set_resid(srb, 0);
1764 return TRANSPORT_GOOD;
1767 int mg_send_key(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1769 struct ms_info *ms_card = &(chip->ms_card);
1770 unsigned int lun = SCSI_LUN(srb);
1771 int retval;
1772 u8 key_format;
1774 rts51x_prepare_run(chip);
1775 RTS51X_SET_STAT(chip, STAT_RUN);
1777 ms_cleanup_work(chip);
1779 if (!check_card_ready(chip, lun)) {
1780 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1781 TRACE_RET(chip, TRANSPORT_FAILED);
1783 if (check_card_wp(chip, lun)) {
1784 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_PROTECT);
1785 TRACE_RET(chip, TRANSPORT_FAILED);
1787 if ((get_lun_card(chip, lun) != MS_CARD)) {
1788 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
1789 TRACE_RET(chip, TRANSPORT_FAILED);
1792 if (srb->cmnd[7] != KC_MG_R_PRO) {
1793 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1794 TRACE_RET(chip, TRANSPORT_FAILED);
1797 if (!CHK_MSPRO(ms_card)) {
1798 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
1799 TRACE_RET(chip, TRANSPORT_FAILED);
1802 key_format = srb->cmnd[10] & 0x3F;
1804 switch (key_format) {
1805 case KF_SET_LEAF_ID:
1806 if ((scsi_bufflen(srb) == 0x0C) &&
1807 (srb->cmnd[8] == 0x00) && (srb->cmnd[9] == 0x0C)) {
1808 retval = mg_set_leaf_id(srb, chip);
1809 if (retval != STATUS_SUCCESS)
1810 TRACE_RET(chip, TRANSPORT_FAILED);
1811 } else {
1812 set_sense_type(chip, lun,
1813 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1814 TRACE_RET(chip, TRANSPORT_FAILED);
1816 break;
1818 case KF_CHG_HOST:
1819 if ((scsi_bufflen(srb) == 0x0C) &&
1820 (srb->cmnd[8] == 0x00) && (srb->cmnd[9] == 0x0C)) {
1821 retval = mg_chg(srb, chip);
1822 if (retval != STATUS_SUCCESS)
1823 TRACE_RET(chip, TRANSPORT_FAILED);
1824 } else {
1825 set_sense_type(chip, lun,
1826 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1827 TRACE_RET(chip, TRANSPORT_FAILED);
1829 break;
1831 case KF_RSP_HOST:
1832 if ((scsi_bufflen(srb) == 0x0C) &&
1833 (srb->cmnd[8] == 0x00) && (srb->cmnd[9] == 0x0C)) {
1834 retval = mg_rsp(srb, chip);
1835 if (retval != STATUS_SUCCESS)
1836 TRACE_RET(chip, TRANSPORT_FAILED);
1837 } else {
1838 set_sense_type(chip, lun,
1839 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1840 TRACE_RET(chip, TRANSPORT_FAILED);
1842 break;
1844 case KF_SET_ICV:
1845 ms_card->mg_entry_num = srb->cmnd[5];
1846 if ((scsi_bufflen(srb) == 0x404) &&
1847 (srb->cmnd[8] == 0x04) &&
1848 (srb->cmnd[9] == 0x04) &&
1849 (srb->cmnd[2] == 0x00) &&
1850 (srb->cmnd[3] == 0x00) &&
1851 (srb->cmnd[4] == 0x00) && (srb->cmnd[5] < 32)) {
1852 retval = mg_set_ICV(srb, chip);
1853 if (retval != STATUS_SUCCESS)
1854 TRACE_RET(chip, TRANSPORT_FAILED);
1855 } else {
1856 set_sense_type(chip, lun,
1857 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1858 TRACE_RET(chip, TRANSPORT_FAILED);
1860 break;
1862 default:
1863 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1864 TRACE_RET(chip, TRANSPORT_FAILED);
1867 scsi_set_resid(srb, 0);
1868 return TRANSPORT_GOOD;
1870 #endif
1872 int rts51x_scsi_handler(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1874 #ifdef SUPPORT_SD_LOCK
1875 struct sd_info *sd_card = &(chip->sd_card);
1876 #endif
1877 struct ms_info *ms_card = &(chip->ms_card);
1878 unsigned int lun = SCSI_LUN(srb);
1879 int result = TRANSPORT_GOOD;
1881 #ifdef SUPPORT_SD_LOCK
1882 if (sd_card->sd_erase_status) {
1883 /* Block all SCSI command except for REQUEST_SENSE
1884 * and rs_ppstatus */
1885 if (!
1886 ((srb->cmnd[0] == VENDOR_CMND)
1887 && (srb->cmnd[1] == SCSI_APP_CMD)
1888 && (srb->cmnd[2] == GET_DEV_STATUS))
1889 && (srb->cmnd[0] != REQUEST_SENSE)) {
1890 /* Logical Unit Not Ready Format in Progress */
1891 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04,
1892 0, 0);
1893 TRACE_RET(chip, TRANSPORT_FAILED);
1896 #endif
1898 if ((get_lun_card(chip, lun) == MS_CARD) &&
1899 (ms_card->format_status == FORMAT_IN_PROGRESS)) {
1900 if ((srb->cmnd[0] != REQUEST_SENSE)
1901 && (srb->cmnd[0] != INQUIRY)) {
1902 /* Logical Unit Not Ready Format in Progress */
1903 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04,
1904 0, (u16) (ms_card->progress));
1905 TRACE_RET(chip, TRANSPORT_FAILED);
1909 switch (srb->cmnd[0]) {
1910 case READ_10:
1911 case WRITE_10:
1912 case READ_6:
1913 case WRITE_6:
1914 result = read_write(srb, chip);
1915 break;
1917 case TEST_UNIT_READY:
1918 result = test_unit_ready(srb, chip);
1919 break;
1921 case INQUIRY:
1922 result = inquiry(srb, chip);
1923 break;
1925 case READ_CAPACITY:
1926 result = read_capacity(srb, chip);
1927 break;
1929 case START_STOP:
1930 result = start_stop_unit(srb, chip);
1931 break;
1933 case ALLOW_MEDIUM_REMOVAL:
1934 result = allow_medium_removal(srb, chip);
1935 break;
1937 case REQUEST_SENSE:
1938 result = request_sense(srb, chip);
1939 break;
1941 case MODE_SENSE:
1942 case MODE_SENSE_10:
1943 result = mode_sense(srb, chip);
1944 break;
1946 case 0x23:
1947 result = read_format_capacity(srb, chip);
1948 break;
1950 case VENDOR_CMND:
1951 result = vendor_cmnd(srb, chip);
1952 break;
1954 case MS_SP_CMND:
1955 result = ms_sp_cmnd(srb, chip);
1956 break;
1958 #ifdef SUPPORT_CPRM
1959 case SD_PASS_THRU_MODE:
1960 case SD_EXECUTE_NO_DATA:
1961 case SD_EXECUTE_READ:
1962 case SD_EXECUTE_WRITE:
1963 case SD_GET_RSP:
1964 case SD_HW_RST:
1965 result = sd_extention_cmnd(srb, chip);
1966 break;
1967 #endif
1969 #ifdef SUPPORT_MAGIC_GATE
1970 case CMD_MSPRO_MG_RKEY:
1971 result = mg_report_key(srb, chip);
1972 break;
1974 case CMD_MSPRO_MG_SKEY:
1975 result = mg_send_key(srb, chip);
1976 break;
1977 #endif
1979 case FORMAT_UNIT:
1980 case MODE_SELECT:
1981 case VERIFY:
1982 result = TRANSPORT_GOOD;
1983 break;
1985 default:
1986 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1987 result = TRANSPORT_FAILED;
1990 return result;
1993 /***********************************************************************
1994 * Host functions
1995 ***********************************************************************/
1997 const char *host_info(struct Scsi_Host *host)
1999 return "SCSI emulation for RTS51xx USB driver-based card reader";
2002 int slave_alloc(struct scsi_device *sdev)
2005 * Set the INQUIRY transfer length to 36. We don't use any of
2006 * the extra data and many devices choke if asked for more or
2007 * less than 36 bytes.
2009 sdev->inquiry_len = 36;
2010 return 0;
2013 int slave_configure(struct scsi_device *sdev)
2015 /* Scatter-gather buffers (all but the last) must have a length
2016 * divisible by the bulk maxpacket size. Otherwise a data packet
2017 * would end up being short, causing a premature end to the data
2018 * transfer. Since high-speed bulk pipes have a maxpacket size
2019 * of 512, we'll use that as the scsi device queue's DMA alignment
2020 * mask. Guaranteeing proper alignment of the first buffer will
2021 * have the desired effect because, except at the beginning and
2022 * the end, scatter-gather buffers follow page boundaries. */
2023 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
2025 /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
2026 * what is originally reported. We need this to avoid confusing
2027 * the SCSI layer with devices that report 0 or 1, but need 10-byte
2028 * commands (ala ATAPI devices behind certain bridges, or devices
2029 * which simply have broken INQUIRY data).
2031 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
2032 * actual information. This seems to be the preference for
2033 * programs like that.
2035 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
2036 * the actual value or the modified one, depending on where the
2037 * data comes from.
2039 if (sdev->scsi_level < SCSI_2)
2040 sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
2042 return 0;
2045 /***********************************************************************
2046 * /proc/scsi/ functions
2047 ***********************************************************************/
2049 /* we use this macro to help us write into the buffer */
2050 #undef SPRINTF
2051 #define SPRINTF(args...) \
2052 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
2054 int proc_info(struct Scsi_Host *host, char *buffer,
2055 char **start, off_t offset, int length, int inout)
2057 char *pos = buffer;
2059 /* if someone is sending us data, just throw it away */
2060 if (inout)
2061 return length;
2063 /* print the controller name */
2064 SPRINTF(" Host scsi%d: %s\n", host->host_no, RTS51X_NAME);
2066 /* print product, vendor, and driver version strings */
2067 SPRINTF(" Vendor: Realtek Corp.\n");
2068 SPRINTF(" Product: RTS51xx USB Card Reader\n");
2069 SPRINTF(" Version: %s\n", DRIVER_VERSION);
2070 SPRINTF(" Build: %s\n", __TIME__);
2073 * Calculate start of next buffer, and return value.
2075 *start = buffer + offset;
2077 if ((pos - buffer) < offset)
2078 return 0;
2079 else if ((pos - buffer - offset) < length)
2080 return pos - buffer - offset;
2081 else
2082 return length;
2085 /* queue a command */
2086 /* This is always called with scsi_lock(host) held */
2087 int queuecommand_lck(struct scsi_cmnd *srb, void (*done) (struct scsi_cmnd *))
2089 struct rts51x_chip *chip = host_to_rts51x(srb->device->host);
2091 /* check for state-transition errors */
2092 if (chip->srb != NULL) {
2093 RTS51X_DEBUGP("Error in %s: chip->srb = %p\n",
2094 __func__, chip->srb);
2095 return SCSI_MLQUEUE_HOST_BUSY;
2098 /* fail the command if we are disconnecting */
2099 if (test_bit(FLIDX_DISCONNECTING, &chip->usb->dflags)) {
2100 RTS51X_DEBUGP("Fail command during disconnect\n");
2101 srb->result = DID_NO_CONNECT << 16;
2102 done(srb);
2103 return 0;
2106 /* enqueue the command and wake up the control thread */
2107 srb->scsi_done = done;
2108 chip->srb = srb;
2109 complete(&chip->usb->cmnd_ready);
2111 return 0;
2114 #if 0 /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 37) */
2115 int queuecommand(struct scsi_cmnd *srb, void (*done) (struct scsi_cmnd *))
2117 return queuecommand_lck(srb, done);
2119 #else
2120 DEF_SCSI_QCMD(queuecommand)
2121 #endif
2122 /***********************************************************************
2123 * Error handling functions
2124 ***********************************************************************/
2125 /* Command timeout and abort */
2126 int command_abort(struct scsi_cmnd *srb)
2128 struct rts51x_chip *chip = host_to_rts51x(srb->device->host);
2130 RTS51X_DEBUGP("%s called\n", __func__);
2132 /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
2133 * bits are protected by the host lock. */
2134 scsi_lock(rts51x_to_host(chip));
2136 /* Is this command still active? */
2137 if (chip->srb != srb) {
2138 scsi_unlock(rts51x_to_host(chip));
2139 RTS51X_DEBUGP("-- nothing to abort\n");
2140 return FAILED;
2143 /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
2144 * a device reset isn't already in progress (to avoid interfering
2145 * with the reset). Note that we must retain the host lock while
2146 * calling usb_stor_stop_transport(); otherwise it might interfere
2147 * with an auto-reset that begins as soon as we release the lock. */
2148 set_bit(FLIDX_TIMED_OUT, &chip->usb->dflags);
2149 if (!test_bit(FLIDX_RESETTING, &chip->usb->dflags)) {
2150 set_bit(FLIDX_ABORTING, &chip->usb->dflags);
2151 /* rts51x_stop_transport(us); */
2153 scsi_unlock(rts51x_to_host(chip));
2155 /* Wait for the aborted command to finish */
2156 wait_for_completion(&chip->usb->notify);
2157 return SUCCESS;
2160 /* This invokes the transport reset mechanism to reset the state of the
2161 * device */
2162 int device_reset(struct scsi_cmnd *srb)
2164 int result = 0;
2166 RTS51X_DEBUGP("%s called\n", __func__);
2168 return result < 0 ? FAILED : SUCCESS;
2171 /* Simulate a SCSI bus reset by resetting the device's USB port. */
2172 int bus_reset(struct scsi_cmnd *srb)
2174 int result = 0;
2176 RTS51X_DEBUGP("%s called\n", __func__);
2178 return result < 0 ? FAILED : SUCCESS;
2181 static const char *rts5139_info(struct Scsi_Host *host)
2183 return "SCSI emulation for RTS5139 USB card reader";
2186 struct scsi_host_template rts51x_host_template = {
2187 /* basic userland interface stuff */
2188 .name = RTS51X_NAME,
2189 .proc_name = RTS51X_NAME,
2190 .proc_info = proc_info,
2191 .info = rts5139_info,
2193 /* command interface -- queued only */
2194 .queuecommand = queuecommand,
2196 /* error and abort handlers */
2197 .eh_abort_handler = command_abort,
2198 .eh_device_reset_handler = device_reset,
2199 .eh_bus_reset_handler = bus_reset,
2201 /* queue commands only, only one command per LUN */
2202 .can_queue = 1,
2203 .cmd_per_lun = 1,
2205 /* unknown initiator id */
2206 .this_id = -1,
2208 .slave_alloc = slave_alloc,
2209 .slave_configure = slave_configure,
2211 /* lots of sg segments can be handled */
2212 .sg_tablesize = SG_ALL,
2214 /* limit the total size of a transfer to 120 KB */
2215 .max_sectors = 240,
2217 /* merge commands... this seems to help performance, but
2218 * periodically someone should test to see which setting is more
2219 * optimal.
2221 .use_clustering = 1,
2223 /* emulated HBA */
2224 .emulated = 1,
2226 /* we do our own delay after a device or bus reset */
2227 .skip_settle_delay = 1,
2229 /* sysfs device attributes */
2230 /* .sdev_attrs = sysfs_device_attr_list, */
2232 /* module management */
2233 .module = THIS_MODULE