spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / staging / rts5139 / sd_cprm.c
blob407cd43ad3b1cadcfb5c1bfa1afd529700c7ac1d
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>
31 #include "debug.h"
32 #include "trace.h"
33 #include "rts51x.h"
34 #include "rts51x_transport.h"
35 #include "rts51x_scsi.h"
36 #include "rts51x_card.h"
37 #include "rts51x_chip.h"
38 #include "sd.h"
40 #ifdef SUPPORT_CPRM
42 static inline int get_rsp_type(u8 rsp_code, u8 *rsp_type, int *rsp_len)
44 if (!rsp_type || !rsp_len)
45 return STATUS_FAIL;
47 switch (rsp_code) {
48 case 0x03:
49 *rsp_type = SD_RSP_TYPE_R0; /* no response */
50 *rsp_len = 0;
51 break;
53 case 0x04:
54 *rsp_type = SD_RSP_TYPE_R1; /* R1,R6(,R4,R5) */
55 *rsp_len = 6;
56 break;
58 case 0x05:
59 *rsp_type = SD_RSP_TYPE_R1b; /* R1b */
60 *rsp_len = 6;
61 break;
63 case 0x06:
64 *rsp_type = SD_RSP_TYPE_R2; /* R2 */
65 *rsp_len = 17;
66 break;
68 case 0x07:
69 *rsp_type = SD_RSP_TYPE_R3; /* R3 */
70 *rsp_len = 6;
71 break;
73 default:
74 return STATUS_FAIL;
77 return STATUS_SUCCESS;
80 int soft_reset_sd_card(struct rts51x_chip *chip)
82 return reset_sd(chip);
85 int ext_sd_send_cmd_get_rsp(struct rts51x_chip *chip, u8 cmd_idx,
86 u32 arg, u8 rsp_type, u8 *rsp, int rsp_len,
87 int special_check)
89 int retval;
90 int timeout = 50;
91 u16 reg_addr;
92 u8 buf[17], stat;
93 int len = 2;
94 int rty_cnt = 0;
96 RTS51X_DEBUGP("EXT SD/MMC CMD %d\n", cmd_idx);
98 if (rsp_type == SD_RSP_TYPE_R1b)
99 timeout = 3000;
101 RTY_SEND_CMD:
103 rts51x_init_cmd(chip);
105 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD0, 0xFF, 0x40 | cmd_idx);
106 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD1, 0xFF, (u8) (arg >> 24));
107 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD2, 0xFF, (u8) (arg >> 16));
108 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD3, 0xFF, (u8) (arg >> 8));
109 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD4, 0xFF, (u8) arg);
111 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CFG2, 0xFF, rsp_type);
112 rts51x_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE,
113 0x01, PINGPONG_BUFFER);
114 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_TRANSFER,
115 0xFF, SD_TM_CMD_RSP | SD_TRANSFER_START);
116 rts51x_add_cmd(chip, CHECK_REG_CMD, SD_TRANSFER, SD_TRANSFER_END,
117 SD_TRANSFER_END);
119 rts51x_add_cmd(chip, READ_REG_CMD, SD_STAT1, 0, 0);
121 if (CHECK_USB(chip, USB_20)) {
122 if (rsp_type == SD_RSP_TYPE_R2) {
123 for (reg_addr = PPBUF_BASE2;
124 reg_addr < PPBUF_BASE2 + 16; reg_addr++) {
125 rts51x_add_cmd(chip, READ_REG_CMD, reg_addr, 0,
128 len = 19;
129 } else if (rsp_type != SD_RSP_TYPE_R0) {
130 /* Read data from SD_CMDx registers */
131 for (reg_addr = SD_CMD0; reg_addr <= SD_CMD4;
132 reg_addr++) {
133 rts51x_add_cmd(chip, READ_REG_CMD, reg_addr, 0,
136 len = 8;
137 } else {
138 len = 3;
140 rts51x_add_cmd(chip, READ_REG_CMD, SD_CMD5, 0, 0);
141 } else {
142 len = 2;
145 retval = rts51x_send_cmd(chip, MODE_CR, 100);
146 if (retval != STATUS_SUCCESS)
147 TRACE_RET(chip, retval);
149 retval = rts51x_get_rsp(chip, len, timeout);
151 if (CHECK_SD_TRANS_FAIL(chip, retval)) {
152 rts51x_clear_sd_error(chip);
154 if (retval == STATUS_TIMEDOUT) {
155 if (rsp_type & SD_WAIT_BUSY_END) {
156 retval = sd_check_data0_status(chip);
157 if (retval != STATUS_SUCCESS)
158 TRACE_RET(chip, retval);
161 TRACE_RET(chip, STATUS_FAIL);
164 if (rsp_type == SD_RSP_TYPE_R0)
165 return STATUS_SUCCESS;
167 if (CHECK_USB(chip, USB_20)) {
168 rts51x_read_rsp_buf(chip, 2, buf, len - 2);
169 } else {
170 if (rsp_type == SD_RSP_TYPE_R2) {
171 reg_addr = PPBUF_BASE2;
172 len = 16;
173 } else {
174 reg_addr = SD_CMD0;
175 len = 5;
177 retval =
178 rts51x_seq_read_register(chip, reg_addr,
179 (unsigned short)len, buf);
180 if (retval != STATUS_SUCCESS)
181 TRACE_RET(chip, retval);
182 RTS51X_READ_REG(chip, SD_CMD5, buf + len);
184 stat = chip->rsp_buf[1];
186 if ((buf[0] & 0xC0) != 0)
187 TRACE_RET(chip, STATUS_FAIL);
189 if (!(rsp_type & SD_NO_CHECK_CRC7)) {
190 if (stat & SD_CRC7_ERR) {
191 if (cmd_idx == WRITE_MULTIPLE_BLOCK)
192 TRACE_RET(chip, STATUS_FAIL);
193 if (rty_cnt < SD_MAX_RETRY_COUNT) {
194 wait_timeout(20);
195 rty_cnt++;
196 goto RTY_SEND_CMD;
197 } else {
198 TRACE_RET(chip, STATUS_FAIL);
203 if ((cmd_idx == SELECT_CARD) || (cmd_idx == APP_CMD) ||
204 (cmd_idx == SEND_STATUS) || (cmd_idx == STOP_TRANSMISSION)) {
205 if ((cmd_idx != STOP_TRANSMISSION) && (special_check == 0)) {
206 if (buf[1] & 0x80)
207 TRACE_RET(chip, STATUS_FAIL);
209 #ifdef SUPPORT_SD_LOCK
210 if (buf[1] & 0x7D) {
211 #else
212 if (buf[1] & 0x7F) {
213 #endif
214 TRACE_RET(chip, STATUS_FAIL);
216 if (buf[2] & 0xF8)
217 TRACE_RET(chip, STATUS_FAIL);
219 if (cmd_idx == SELECT_CARD) {
220 if (rsp_type == SD_RSP_TYPE_R2) {
221 if ((buf[3] & 0x1E) != 0x04)
222 TRACE_RET(chip, STATUS_FAIL);
223 } else if (rsp_type == SD_RSP_TYPE_R2) {
224 if ((buf[3] & 0x1E) != 0x03)
225 TRACE_RET(chip, STATUS_FAIL);
230 if (rsp && rsp_len)
231 memcpy(rsp, buf, rsp_len);
233 return STATUS_SUCCESS;
236 int ext_sd_get_rsp(struct rts51x_chip *chip, int len, u8 * rsp, u8 rsp_type)
238 int retval, rsp_len;
239 u16 reg_addr;
241 if (rsp_type == SD_RSP_TYPE_R0)
242 return STATUS_SUCCESS;
244 rts51x_init_cmd(chip);
246 if (rsp_type == SD_RSP_TYPE_R2) {
247 for (reg_addr = PPBUF_BASE2; reg_addr < PPBUF_BASE2 + 16;
248 reg_addr++) {
249 rts51x_add_cmd(chip, READ_REG_CMD, reg_addr, 0xFF, 0);
251 rsp_len = 17;
252 } else if (rsp_type != SD_RSP_TYPE_R0) {
253 for (reg_addr = SD_CMD0; reg_addr <= SD_CMD4; reg_addr++)
254 rts51x_add_cmd(chip, READ_REG_CMD, reg_addr, 0xFF, 0);
255 rsp_len = 6;
257 rts51x_add_cmd(chip, READ_REG_CMD, SD_CMD5, 0xFF, 0);
259 retval = rts51x_send_cmd(chip, MODE_CR, 100);
260 if (retval != STATUS_SUCCESS)
261 TRACE_RET(chip, retval);
263 retval = rts51x_get_rsp(chip, rsp_len, 100);
265 if (retval != STATUS_SUCCESS)
266 TRACE_RET(chip, retval);
268 if (rsp) {
269 int min_len = (rsp_len < len) ? rsp_len : len;
271 memcpy(rsp, rts51x_get_rsp_data(chip), min_len);
273 RTS51X_DEBUGP("min_len = %d\n", min_len);
274 RTS51X_DEBUGP("Response in cmd buf: 0x%x 0x%x 0x%x 0x%x\n",
275 rsp[0], rsp[1], rsp[2], rsp[3]);
278 return STATUS_SUCCESS;
281 int ext_sd_execute_no_data(struct rts51x_chip *chip, unsigned int lun,
282 u8 cmd_idx, u8 standby, u8 acmd, u8 rsp_code,
283 u32 arg)
285 struct sd_info *sd_card = &(chip->sd_card);
286 int retval, rsp_len;
287 u8 rsp_type;
289 retval = sd_switch_clock(chip);
290 if (retval != STATUS_SUCCESS)
291 TRACE_RET(chip, TRANSPORT_FAILED);
293 if (sd_card->pre_cmd_err) {
294 sd_card->pre_cmd_err = 0;
295 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
296 TRACE_RET(chip, TRANSPORT_FAILED);
298 retval = get_rsp_type(rsp_code, &rsp_type, &rsp_len);
299 if (retval != STATUS_SUCCESS) {
300 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
301 TRACE_RET(chip, TRANSPORT_FAILED);
303 sd_card->last_rsp_type = rsp_type;
305 retval = sd_switch_clock(chip);
306 if (retval != STATUS_SUCCESS)
307 TRACE_RET(chip, TRANSPORT_FAILED);
308 #ifdef SUPPORT_SD_LOCK
309 if ((sd_card->sd_lock_status & SD_LOCK_1BIT_MODE) == 0) {
310 if (CHK_MMC_8BIT(sd_card)) {
311 retval =
312 rts51x_write_register(chip, SD_CFG1, 0x03,
313 SD_BUS_WIDTH_8);
314 if (retval != STATUS_SUCCESS)
315 TRACE_RET(chip, TRANSPORT_FAILED);
316 } else if (CHK_SD(sd_card) || CHK_MMC_4BIT(sd_card)) {
317 retval =
318 rts51x_write_register(chip, SD_CFG1, 0x03,
319 SD_BUS_WIDTH_4);
320 if (retval != STATUS_SUCCESS)
321 TRACE_RET(chip, TRANSPORT_FAILED);
324 #else
325 /* Set H/W SD/MMC Bus Width */
326 rts51x_write_register(chip, SD_CFG1, 0x03, SD_BUS_WIDTH_4);
327 #endif
329 if (standby) {
330 retval = sd_select_card(chip, 0);
331 if (retval != STATUS_SUCCESS)
332 TRACE_GOTO(chip, SD_Execute_Cmd_Failed);
335 if (acmd) {
336 retval =
337 ext_sd_send_cmd_get_rsp(chip, APP_CMD, sd_card->sd_addr,
338 SD_RSP_TYPE_R1, NULL, 0, 0);
339 if (retval != STATUS_SUCCESS)
340 TRACE_GOTO(chip, SD_Execute_Cmd_Failed);
343 retval = ext_sd_send_cmd_get_rsp(chip, cmd_idx, arg, rsp_type,
344 sd_card->rsp, rsp_len, 0);
345 if (retval != STATUS_SUCCESS)
346 TRACE_GOTO(chip, SD_Execute_Cmd_Failed);
348 if (standby) {
349 retval = sd_select_card(chip, 1);
350 if (retval != STATUS_SUCCESS)
351 TRACE_GOTO(chip, SD_Execute_Cmd_Failed);
353 #ifdef SUPPORT_SD_LOCK
354 /* Get SD lock status */
355 retval = sd_update_lock_status(chip);
356 if (retval != STATUS_SUCCESS)
357 TRACE_GOTO(chip, SD_Execute_Cmd_Failed);
358 #endif
360 return TRANSPORT_GOOD;
362 SD_Execute_Cmd_Failed:
363 sd_card->pre_cmd_err = 1;
364 set_sense_type(chip, lun, SENSE_TYPE_NO_SENSE);
365 release_sd_card(chip);
366 do_reset_sd_card(chip);
367 if (!(chip->card_ready & SD_CARD))
368 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
370 TRACE_RET(chip, TRANSPORT_FAILED);
373 int ext_sd_execute_read_data(struct rts51x_chip *chip, unsigned int lun,
374 u8 cmd_idx, u8 cmd12, u8 standby,
375 u8 acmd, u8 rsp_code, u32 arg, u32 data_len,
376 void *data_buf, unsigned int buf_len, int use_sg)
378 struct sd_info *sd_card = &(chip->sd_card);
379 int retval, rsp_len, i;
380 int cmd13_checkbit = 0, read_err = 0;
381 u8 rsp_type, bus_width;
383 if (sd_card->pre_cmd_err) {
384 sd_card->pre_cmd_err = 0;
385 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
386 TRACE_RET(chip, TRANSPORT_FAILED);
389 retval = sd_switch_clock(chip);
390 if (retval != STATUS_SUCCESS)
391 TRACE_RET(chip, STATUS_FAIL);
392 retval = get_rsp_type(rsp_code, &rsp_type, &rsp_len);
393 if (retval != STATUS_SUCCESS) {
394 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
395 TRACE_RET(chip, TRANSPORT_FAILED);
397 sd_card->last_rsp_type = rsp_type;
399 retval = sd_switch_clock(chip);
400 if (retval != STATUS_SUCCESS)
401 TRACE_RET(chip, TRANSPORT_FAILED);
402 #ifdef SUPPORT_SD_LOCK
403 if ((sd_card->sd_lock_status & SD_LOCK_1BIT_MODE) == 0) {
404 if (CHK_MMC_8BIT(sd_card))
405 bus_width = SD_BUS_WIDTH_8;
406 else if (CHK_SD(sd_card) || CHK_MMC_4BIT(sd_card))
407 bus_width = SD_BUS_WIDTH_4;
408 else
409 bus_width = SD_BUS_WIDTH_1;
410 } else {
411 bus_width = SD_BUS_WIDTH_4;
413 RTS51X_DEBUGP("bus_width = %d\n", bus_width);
414 #else
415 bus_width = SD_BUS_WIDTH_4;
416 #endif
418 if (data_len < 512) {
419 retval = ext_sd_send_cmd_get_rsp(chip, SET_BLOCKLEN, data_len,
420 SD_RSP_TYPE_R1, NULL, 0, 0);
421 if (retval != STATUS_SUCCESS)
422 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
425 if (standby) {
426 retval = sd_select_card(chip, 0);
427 if (retval != STATUS_SUCCESS)
428 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
431 if (acmd) {
432 retval =
433 ext_sd_send_cmd_get_rsp(chip, APP_CMD, sd_card->sd_addr,
434 SD_RSP_TYPE_R1, NULL, 0, 0);
435 if (retval != STATUS_SUCCESS)
436 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
439 if (data_len <= 512) {
440 int min_len;
441 u8 *buf;
442 u16 byte_cnt, blk_cnt;
443 u8 cmd[5];
444 unsigned int offset = 0;
445 void *sg = NULL;
447 byte_cnt = (u16) (data_len & 0x3FF);
448 blk_cnt = 1;
450 cmd[0] = 0x40 | cmd_idx;
451 cmd[1] = (u8) (arg >> 24);
452 cmd[2] = (u8) (arg >> 16);
453 cmd[3] = (u8) (arg >> 8);
454 cmd[4] = (u8) arg;
456 buf = kmalloc(data_len, GFP_KERNEL);
457 if (buf == NULL)
458 TRACE_RET(chip, TRANSPORT_ERROR);
460 retval = sd_read_data(chip, SD_TM_NORMAL_READ, cmd, 5, byte_cnt,
461 blk_cnt, bus_width, buf, data_len, 2000);
462 if (retval != STATUS_SUCCESS) {
463 read_err = 1;
464 kfree(buf);
465 rts51x_write_register(chip, CARD_STOP,
466 SD_STOP | SD_CLR_ERR,
467 SD_STOP | SD_CLR_ERR);
468 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
471 min_len = min(data_len, buf_len);
472 if (use_sg)
473 rts51x_access_sglist(buf, min_len, (void *)data_buf,
474 &sg, &offset, TO_XFER_BUF);
475 else
476 memcpy(data_buf, buf, min_len);
478 kfree(buf);
479 } else if (!(data_len & 0x1FF)) {
480 rts51x_init_cmd(chip);
481 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BYTE_CNT_H, 0xFF, 0x02);
482 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF, 0x00);
483 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BLOCK_CNT_H,
484 0xFF, (u8) (data_len >> 17));
485 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BLOCK_CNT_L,
486 0xFF, (u8) ((data_len & 0x0001FE00) >> 9));
487 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD0, 0xFF,
488 0x40 | cmd_idx);
489 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD1, 0xFF,
490 (u8) (arg >> 24));
491 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD2, 0xFF,
492 (u8) (arg >> 16));
493 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD3, 0xFF,
494 (u8) (arg >> 8));
495 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CMD4, 0xFF, (u8) arg);
496 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CFG1, 0x03, bus_width);
497 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_CFG2, 0xFF, rsp_type);
498 trans_dma_enable(DMA_FROM_DEVICE, chip, data_len, DMA_512);
499 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
500 SD_TM_AUTO_READ_2 | SD_TRANSFER_START);
501 rts51x_add_cmd(chip, CHECK_REG_CMD, SD_TRANSFER,
502 SD_TRANSFER_END, SD_TRANSFER_END);
503 retval = rts51x_send_cmd(chip, MODE_CDIR, 100);
504 if (retval != STATUS_SUCCESS) {
505 read_err = 1;
506 rts51x_ep0_write_register(chip, CARD_STOP,
507 SD_STOP | SD_CLR_ERR,
508 SD_STOP | SD_CLR_ERR);
509 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
512 retval =
513 rts51x_transfer_data_rcc(chip, RCV_BULK_PIPE(chip),
514 data_buf, buf_len, use_sg, NULL,
515 10000, STAGE_DI);
516 if (retval != STATUS_SUCCESS) {
517 read_err = 1;
518 rts51x_ep0_write_register(chip, CARD_STOP,
519 SD_STOP | SD_CLR_ERR,
520 SD_STOP | SD_CLR_ERR);
521 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
523 retval = rts51x_get_rsp(chip, 1, 500);
524 if (CHECK_SD_TRANS_FAIL(chip, retval)) {
525 read_err = 1;
526 rts51x_ep0_write_register(chip, CARD_STOP,
527 SD_STOP | SD_CLR_ERR,
528 SD_STOP | SD_CLR_ERR);
529 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
531 } else {
532 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
535 retval = ext_sd_get_rsp(chip, rsp_len, sd_card->rsp, rsp_type);
536 if (retval != STATUS_SUCCESS)
537 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
539 if (standby) {
540 retval = sd_select_card(chip, 1);
541 if (retval != STATUS_SUCCESS)
542 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
545 if (cmd12) {
546 retval = ext_sd_send_cmd_get_rsp(chip, STOP_TRANSMISSION,
547 0, SD_RSP_TYPE_R1b, NULL, 0,
549 if (retval != STATUS_SUCCESS)
550 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
553 if (data_len < 512) {
554 retval = ext_sd_send_cmd_get_rsp(chip, SET_BLOCKLEN, 0x200,
555 SD_RSP_TYPE_R1, NULL, 0, 0);
556 if (retval != STATUS_SUCCESS)
557 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
559 rts51x_write_register(chip, SD_BYTE_CNT_H, 0xFF, 0x02);
560 rts51x_write_register(chip, SD_BYTE_CNT_L, 0xFF, 0x00);
563 if (standby || cmd12)
564 cmd13_checkbit = 1;
566 for (i = 0; i < 3; i++) {
567 retval =
568 ext_sd_send_cmd_get_rsp(chip, SEND_STATUS, sd_card->sd_addr,
569 SD_RSP_TYPE_R1, NULL, 0,
570 cmd13_checkbit);
571 if (retval == STATUS_SUCCESS)
572 break;
574 if (retval != STATUS_SUCCESS)
575 TRACE_GOTO(chip, SD_Execute_Read_Cmd_Failed);
577 return TRANSPORT_GOOD;
579 SD_Execute_Read_Cmd_Failed:
580 sd_card->pre_cmd_err = 1;
581 set_sense_type(chip, lun, SENSE_TYPE_NO_SENSE);
582 if (read_err)
583 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
584 release_sd_card(chip);
585 do_reset_sd_card(chip);
586 if (!(chip->card_ready & SD_CARD))
587 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
589 TRACE_RET(chip, TRANSPORT_FAILED);
592 int ext_sd_execute_write_data(struct rts51x_chip *chip, unsigned int lun,
593 u8 cmd_idx, u8 cmd12, u8 standby, u8 acmd,
594 u8 rsp_code, u32 arg, u32 data_len,
595 void *data_buf, unsigned int buf_len, int use_sg)
597 struct sd_info *sd_card = &(chip->sd_card);
598 int retval, rsp_len;
599 int cmd13_checkbit = 0, write_err = 0;
600 u8 rsp_type;
601 u32 i;
602 #ifdef SUPPORT_SD_LOCK
603 int lock_cmd_fail = 0;
604 u8 sd_lock_state = 0;
605 u8 lock_cmd_type = 0;
606 #endif
608 if (sd_card->pre_cmd_err) {
609 sd_card->pre_cmd_err = 0;
610 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
611 TRACE_RET(chip, TRANSPORT_FAILED);
614 retval = sd_switch_clock(chip);
615 if (retval != STATUS_SUCCESS)
616 TRACE_RET(chip, STATUS_FAIL);
617 #ifdef SUPPORT_SD_LOCK
618 if (cmd_idx == LOCK_UNLOCK) {
619 sd_lock_state = sd_card->sd_lock_status;
620 sd_lock_state &= SD_LOCKED;
622 #endif
624 retval = get_rsp_type(rsp_code, &rsp_type, &rsp_len);
625 if (retval != STATUS_SUCCESS) {
626 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
627 TRACE_RET(chip, TRANSPORT_FAILED);
629 sd_card->last_rsp_type = rsp_type;
631 retval = sd_switch_clock(chip);
632 if (retval != STATUS_SUCCESS)
633 TRACE_RET(chip, TRANSPORT_FAILED);
634 #ifdef SUPPORT_SD_LOCK
635 if ((sd_card->sd_lock_status & SD_LOCK_1BIT_MODE) == 0) {
636 if (CHK_MMC_8BIT(sd_card)) {
637 retval =
638 rts51x_write_register(chip, SD_CFG1, 0x03,
639 SD_BUS_WIDTH_8);
640 if (retval != STATUS_SUCCESS)
641 TRACE_RET(chip, TRANSPORT_FAILED);
642 } else if (CHK_SD(sd_card) || CHK_MMC_4BIT(sd_card)) {
643 retval =
644 rts51x_write_register(chip, SD_CFG1, 0x03,
645 SD_BUS_WIDTH_4);
646 if (retval != STATUS_SUCCESS)
647 TRACE_RET(chip, TRANSPORT_FAILED);
650 #else
651 rts51x_write_register(chip, SD_CFG1, 0x03, SD_BUS_WIDTH_4);
652 #endif
654 if (data_len < 512) {
655 retval = ext_sd_send_cmd_get_rsp(chip, SET_BLOCKLEN, data_len,
656 SD_RSP_TYPE_R1, NULL, 0, 0);
657 if (retval != STATUS_SUCCESS)
658 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
661 if (standby) {
662 retval = sd_select_card(chip, 0);
663 if (retval != STATUS_SUCCESS)
664 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
667 if (acmd) {
668 retval =
669 ext_sd_send_cmd_get_rsp(chip, APP_CMD, sd_card->sd_addr,
670 SD_RSP_TYPE_R1, NULL, 0, 0);
671 if (retval != STATUS_SUCCESS)
672 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
675 retval = ext_sd_send_cmd_get_rsp(chip, cmd_idx, arg, rsp_type,
676 sd_card->rsp, rsp_len, 0);
677 if (retval != STATUS_SUCCESS)
678 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
680 if (data_len <= 512) {
681 u8 *buf;
682 unsigned int offset = 0;
683 void *sg = NULL;
685 buf = kmalloc(data_len, GFP_KERNEL);
686 if (buf == NULL)
687 TRACE_RET(chip, TRANSPORT_ERROR);
689 if (use_sg)
690 rts51x_access_sglist(buf, data_len, (void *)data_buf,
691 &sg, &offset, FROM_XFER_BUF);
692 else
693 memcpy(buf, data_buf, data_len);
695 #ifdef SUPPORT_SD_LOCK
696 if (cmd_idx == LOCK_UNLOCK)
697 lock_cmd_type = buf[0] & 0x0F;
698 #endif
700 if (data_len > 256) {
701 rts51x_init_cmd(chip);
702 for (i = 0; i < 256; i++) {
703 rts51x_add_cmd(chip, WRITE_REG_CMD,
704 (u16) (PPBUF_BASE2 + i), 0xFF,
705 buf[i]);
707 retval = rts51x_send_cmd(chip, MODE_C, 250);
708 if (retval != STATUS_SUCCESS) {
709 kfree(buf);
710 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
713 rts51x_init_cmd(chip);
714 for (i = 256; i < data_len; i++) {
715 rts51x_add_cmd(chip, WRITE_REG_CMD,
716 (u16) (PPBUF_BASE2 + i), 0xFF,
717 buf[i]);
719 retval = rts51x_send_cmd(chip, MODE_C, 250);
720 if (retval != STATUS_SUCCESS) {
721 kfree(buf);
722 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
724 } else {
725 rts51x_init_cmd(chip);
726 for (i = 0; i < data_len; i++) {
727 rts51x_add_cmd(chip, WRITE_REG_CMD,
728 (u16) (PPBUF_BASE2 + i), 0xFF,
729 buf[i]);
731 retval = rts51x_send_cmd(chip, MODE_C, 250);
732 if (retval != STATUS_SUCCESS) {
733 kfree(buf);
734 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
738 kfree(buf);
740 rts51x_init_cmd(chip);
742 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BYTE_CNT_H, 0xFF,
743 (u8) ((data_len >> 8) & 0x03));
744 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF,
745 (u8) data_len);
746 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BLOCK_CNT_H, 0xFF, 0x00);
747 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BLOCK_CNT_L, 0xFF, 0x01);
748 rts51x_add_cmd(chip, WRITE_REG_CMD, CARD_DATA_SOURCE, 0x01,
749 PINGPONG_BUFFER);
751 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
752 SD_TM_AUTO_WRITE_3 | SD_TRANSFER_START);
753 rts51x_add_cmd(chip, CHECK_REG_CMD, SD_TRANSFER,
754 SD_TRANSFER_END, SD_TRANSFER_END);
756 retval = rts51x_send_cmd(chip, MODE_CR, 100);
757 if (retval != STATUS_SUCCESS)
758 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
760 retval = rts51x_get_rsp(chip, 1, 250);
761 if (CHECK_SD_TRANS_FAIL(chip, retval))
762 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
763 } else if (!(data_len & 0x1FF)) {
764 rts51x_init_cmd(chip);
766 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BYTE_CNT_H, 0xFF, 0x02);
767 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BYTE_CNT_L, 0xFF, 0x00);
768 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BLOCK_CNT_H,
769 0xFF, (u8) (data_len >> 17));
770 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_BLOCK_CNT_L,
771 0xFF, (u8) ((data_len & 0x0001FE00) >> 9));
773 trans_dma_enable(DMA_TO_DEVICE, chip, data_len, DMA_512);
775 rts51x_add_cmd(chip, WRITE_REG_CMD, SD_TRANSFER, 0xFF,
776 SD_TM_AUTO_WRITE_3 | SD_TRANSFER_START);
777 rts51x_add_cmd(chip, CHECK_REG_CMD, SD_TRANSFER,
778 SD_TRANSFER_END, SD_TRANSFER_END);
780 retval = rts51x_send_cmd(chip, MODE_CDOR, 100);
781 if (retval != STATUS_SUCCESS)
782 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
784 retval =
785 rts51x_transfer_data_rcc(chip, SND_BULK_PIPE(chip),
786 data_buf, buf_len, use_sg, NULL,
787 10000, STAGE_DO);
788 if (retval != STATUS_SUCCESS)
789 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
791 retval = rts51x_get_rsp(chip, 1, 10000);
792 if (CHECK_SD_TRANS_FAIL(chip, retval))
793 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
795 } else {
796 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
799 if (retval < 0) {
800 write_err = 1;
801 rts51x_write_register(chip, CARD_STOP, SD_STOP | SD_CLR_ERR,
802 SD_STOP | SD_CLR_ERR);
803 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
805 #ifdef SUPPORT_SD_LOCK
806 if (cmd_idx == LOCK_UNLOCK) {
807 if (lock_cmd_type == SD_ERASE) {
808 sd_card->sd_erase_status = SD_UNDER_ERASING;
809 scsi_set_resid(srb, 0);
810 return TRANSPORT_GOOD;
813 rts51x_init_cmd(chip);
814 rts51x_add_cmd(chip, CHECK_REG_CMD, SD_BUS_STAT, SD_DAT0_STATUS,
815 SD_DAT0_STATUS);
816 retval = rts51x_send_cmd(chip, MODE_CR, 250);
817 if (retval != STATUS_SUCCESS)
818 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
819 rts51x_get_rsp(chip, 1, 200); /* Don't care return value */
821 retval = sd_update_lock_status(chip);
822 if (retval != STATUS_SUCCESS) {
823 RTS51X_DEBUGP("Lock command fail!\n");
824 lock_cmd_fail = 1;
827 #endif /* SUPPORT_SD_LOCK */
829 if (standby) {
830 retval = sd_select_card(chip, 1);
831 if (retval != STATUS_SUCCESS)
832 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
835 if (cmd12) {
836 retval = ext_sd_send_cmd_get_rsp(chip, STOP_TRANSMISSION,
837 0, SD_RSP_TYPE_R1b, NULL, 0,
839 if (retval != STATUS_SUCCESS)
840 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
843 if (data_len < 512) {
844 retval = ext_sd_send_cmd_get_rsp(chip, SET_BLOCKLEN, 0x200,
845 SD_RSP_TYPE_R1, NULL, 0, 0);
846 if (retval != STATUS_SUCCESS)
847 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
849 rts51x_write_register(chip, SD_BYTE_CNT_H, 0xFF, 0x02);
850 rts51x_write_register(chip, SD_BYTE_CNT_L, 0xFF, 0x00);
853 if (cmd12 || standby) {
854 /* There is CMD7 or CMD12 sent before CMD13 */
855 cmd13_checkbit = 1;
858 for (i = 0; i < 3; i++) {
859 retval =
860 ext_sd_send_cmd_get_rsp(chip, SEND_STATUS, sd_card->sd_addr,
861 SD_RSP_TYPE_R1, NULL, 0,
862 cmd13_checkbit);
863 if (retval == STATUS_SUCCESS)
864 break;
866 if (retval != STATUS_SUCCESS)
867 TRACE_GOTO(chip, SD_Execute_Write_Cmd_Failed);
868 #ifdef SUPPORT_SD_LOCK
869 if (cmd_idx == LOCK_UNLOCK) {
870 if (!lock_cmd_fail) {
871 RTS51X_DEBUGP("lock_cmd_type = 0x%x\n",
872 lock_cmd_type);
873 if (lock_cmd_type & SD_CLR_PWD)
874 sd_card->sd_lock_status &= ~SD_PWD_EXIST;
875 if (lock_cmd_type & SD_SET_PWD)
876 sd_card->sd_lock_status |= SD_PWD_EXIST;
879 RTS51X_DEBUGP("sd_lock_state = 0x%x,"
880 "sd_card->sd_lock_status = 0x%x\n",
881 sd_lock_state, sd_card->sd_lock_status);
882 if (sd_lock_state ^ (sd_card->sd_lock_status & SD_LOCKED)) {
883 sd_card->sd_lock_notify = 1;
884 if (sd_lock_state) {
885 if (sd_card->sd_lock_status &
886 SD_LOCK_1BIT_MODE) {
887 sd_card->sd_lock_status |=
888 (SD_UNLOCK_POW_ON | SD_SDR_RST);
889 if (CHK_SD(sd_card)) {
890 retval = reset_sd(chip);
891 if (retval != STATUS_SUCCESS) {
892 sd_card->sd_lock_status
893 &= ~(SD_UNLOCK_POW_ON |
894 SD_SDR_RST);
895 TRACE_GOTO(chip,
896 SD_Execute_Write_Cmd_Failed);
900 sd_card->sd_lock_status &=
901 ~(SD_UNLOCK_POW_ON | SD_SDR_RST);
907 if (lock_cmd_fail) {
908 scsi_set_resid(srb, 0);
909 set_sense_type(chip, lun, SENSE_TYPE_NO_SENSE);
910 TRACE_RET(chip, TRANSPORT_FAILED);
912 #endif /* SUPPORT_SD_LOCK */
914 return TRANSPORT_GOOD;
916 SD_Execute_Write_Cmd_Failed:
917 sd_card->pre_cmd_err = 1;
918 set_sense_type(chip, lun, SENSE_TYPE_NO_SENSE);
919 if (write_err)
920 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
921 release_sd_card(chip);
922 do_reset_sd_card(chip);
923 if (!(chip->card_ready & SD_CARD))
924 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
926 TRACE_RET(chip, TRANSPORT_FAILED);
929 int sd_pass_thru_mode(struct scsi_cmnd *srb, struct rts51x_chip *chip)
931 struct sd_info *sd_card = &(chip->sd_card);
932 unsigned int lun = SCSI_LUN(srb);
933 int len;
934 u8 buf[18] = {
935 0x00,
936 0x00,
937 0x00,
938 0x0E,
939 0x00, /* Version Number */
940 0x00, /* WP | Media Type */
941 0x00, /* RCA (Low byte) */
942 0x00, /* RCA (High byte) */
943 0x53, /* 'S' */
944 0x44, /* 'D' */
945 0x20, /* ' ' */
946 0x43, /* 'C' */
947 0x61, /* 'a' */
948 0x72, /* 'r' */
949 0x64, /* 'd' */
950 0x00, /* Max LUN Number */
951 0x00,
952 0x00,
955 sd_card->pre_cmd_err = 0;
957 if (!(CHK_BIT(chip->lun_mc, lun))) {
958 SET_BIT(chip->lun_mc, lun);
959 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
960 TRACE_RET(chip, TRANSPORT_FAILED);
963 if ((0x53 != srb->cmnd[2]) || (0x44 != srb->cmnd[3])
964 || (0x20 != srb->cmnd[4]) || (0x43 != srb->cmnd[5])
965 || (0x61 != srb->cmnd[6]) || (0x72 != srb->cmnd[7])
966 || (0x64 != srb->cmnd[8])) {
967 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
968 TRACE_RET(chip, TRANSPORT_FAILED);
971 switch (srb->cmnd[1] & 0x0F) {
972 case 0:
973 sd_card->sd_pass_thru_en = 0;
974 break;
976 case 1:
977 sd_card->sd_pass_thru_en = 1;
978 break;
980 default:
981 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
982 TRACE_RET(chip, TRANSPORT_FAILED);
985 /* 0x01:SD Memory Card; 0x02:Other Media; 0x03:Illegal Media; */
986 buf[5] = (1 == CHK_SD(sd_card)) ? 0x01 : 0x02;
987 if (chip->card_wp & SD_CARD)
988 buf[5] |= 0x80;
990 buf[6] = (u8) (sd_card->sd_addr >> 16);
991 buf[7] = (u8) (sd_card->sd_addr >> 24);
993 buf[15] = chip->max_lun;
995 len = min(18, (int)scsi_bufflen(srb));
996 rts51x_set_xfer_buf(buf, len, srb);
998 return TRANSPORT_GOOD;
1001 int sd_execute_no_data(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1003 struct sd_info *sd_card = &(chip->sd_card);
1004 unsigned int lun = SCSI_LUN(srb);
1005 int retval;
1006 u8 cmd_idx, rsp_code;
1007 u8 standby = 0, acmd = 0;
1008 u32 arg;
1010 if (!sd_card->sd_pass_thru_en) {
1011 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1012 TRACE_RET(chip, TRANSPORT_FAILED);
1015 cmd_idx = srb->cmnd[2] & 0x3F;
1016 if (srb->cmnd[1] & 0x02)
1017 standby = 1;
1018 if (srb->cmnd[1] & 0x01)
1019 acmd = 1;
1021 arg = ((u32) srb->cmnd[3] << 24) | ((u32) srb->cmnd[4] << 16) |
1022 ((u32) srb->cmnd[5] << 8) | srb->cmnd[6];
1024 rsp_code = srb->cmnd[10];
1026 retval =
1027 ext_sd_execute_no_data(chip, lun, cmd_idx, standby, acmd, rsp_code,
1028 arg);
1029 scsi_set_resid(srb, 0);
1030 return retval;
1033 int sd_execute_read_data(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1035 struct sd_info *sd_card = &(chip->sd_card);
1036 int retval;
1037 unsigned int lun = SCSI_LUN(srb);
1038 u8 cmd_idx, rsp_code, send_cmd12 = 0, standby = 0, acmd = 0;
1039 u32 arg, data_len;
1041 if (!sd_card->sd_pass_thru_en) {
1042 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1043 TRACE_RET(chip, TRANSPORT_FAILED);
1046 cmd_idx = srb->cmnd[2] & 0x3F;
1047 if (srb->cmnd[1] & 0x04)
1048 send_cmd12 = 1;
1049 if (srb->cmnd[1] & 0x02)
1050 standby = 1;
1051 if (srb->cmnd[1] & 0x01)
1052 acmd = 1;
1054 arg = ((u32) srb->cmnd[3] << 24) | ((u32) srb->cmnd[4] << 16) |
1055 ((u32) srb->cmnd[5] << 8) | srb->cmnd[6];
1057 data_len =
1058 ((u32) srb->cmnd[7] << 16) | ((u32) srb->cmnd[8] << 8) |
1059 srb->cmnd[9];
1060 rsp_code = srb->cmnd[10];
1062 retval =
1063 ext_sd_execute_read_data(chip, lun, cmd_idx, send_cmd12, standby,
1064 acmd, rsp_code, arg, data_len,
1065 scsi_sglist(srb), scsi_bufflen(srb),
1066 scsi_sg_count(srb));
1067 scsi_set_resid(srb, 0);
1068 return retval;
1071 int sd_execute_write_data(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1073 struct sd_info *sd_card = &(chip->sd_card);
1074 int retval;
1075 unsigned int lun = SCSI_LUN(srb);
1076 u8 cmd_idx, rsp_code, send_cmd12 = 0, standby = 0, acmd = 0;
1077 u32 data_len, arg;
1079 if (!sd_card->sd_pass_thru_en) {
1080 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1081 TRACE_RET(chip, TRANSPORT_FAILED);
1084 cmd_idx = srb->cmnd[2] & 0x3F;
1085 if (srb->cmnd[1] & 0x04)
1086 send_cmd12 = 1;
1087 if (srb->cmnd[1] & 0x02)
1088 standby = 1;
1089 if (srb->cmnd[1] & 0x01)
1090 acmd = 1;
1092 data_len =
1093 ((u32) srb->cmnd[7] << 16) | ((u32) srb->cmnd[8] << 8) |
1094 srb->cmnd[9];
1095 arg =
1096 ((u32) srb->cmnd[3] << 24) | ((u32) srb->cmnd[4] << 16) |
1097 ((u32) srb->cmnd[5] << 8) | srb->cmnd[6];
1098 rsp_code = srb->cmnd[10];
1100 retval =
1101 ext_sd_execute_write_data(chip, lun, cmd_idx, send_cmd12, standby,
1102 acmd, rsp_code, arg, data_len,
1103 scsi_sglist(srb), scsi_bufflen(srb),
1104 scsi_sg_count(srb));
1105 scsi_set_resid(srb, 0);
1106 return retval;
1109 int sd_get_cmd_rsp(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1111 struct sd_info *sd_card = &(chip->sd_card);
1112 unsigned int lun = SCSI_LUN(srb);
1113 int count;
1114 u16 data_len;
1116 if (!sd_card->sd_pass_thru_en) {
1117 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1118 TRACE_RET(chip, TRANSPORT_FAILED);
1121 if (sd_card->pre_cmd_err) {
1122 sd_card->pre_cmd_err = 0;
1123 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
1124 TRACE_RET(chip, TRANSPORT_FAILED);
1127 data_len = ((u16) srb->cmnd[7] << 8) | srb->cmnd[8];
1129 if (sd_card->last_rsp_type == SD_RSP_TYPE_R0) {
1130 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1131 TRACE_RET(chip, TRANSPORT_FAILED);
1132 } else if (sd_card->last_rsp_type == SD_RSP_TYPE_R2) {
1133 count = (data_len < 17) ? data_len : 17;
1134 } else {
1135 count = (data_len < 6) ? data_len : 6;
1137 rts51x_set_xfer_buf(sd_card->rsp, count, srb);
1139 RTS51X_DEBUGP("Response length: %d\n", data_len);
1140 RTS51X_DEBUGP("Response: 0x%x 0x%x 0x%x 0x%x\n",
1141 sd_card->rsp[0], sd_card->rsp[1], sd_card->rsp[2],
1142 sd_card->rsp[3]);
1144 scsi_set_resid(srb, 0);
1145 return TRANSPORT_GOOD;
1148 int sd_hw_rst(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1150 struct sd_info *sd_card = &(chip->sd_card);
1151 unsigned int lun = SCSI_LUN(srb);
1152 int retval;
1154 if (!sd_card->sd_pass_thru_en) {
1155 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1156 TRACE_RET(chip, TRANSPORT_FAILED);
1159 if (sd_card->pre_cmd_err) {
1160 sd_card->pre_cmd_err = 0;
1161 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
1162 TRACE_RET(chip, TRANSPORT_FAILED);
1165 if ((0x53 != srb->cmnd[2]) || (0x44 != srb->cmnd[3])
1166 || (0x20 != srb->cmnd[4]) || (0x43 != srb->cmnd[5])
1167 || (0x61 != srb->cmnd[6]) || (0x72 != srb->cmnd[7])
1168 || (0x64 != srb->cmnd[8])) {
1169 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1170 TRACE_RET(chip, TRANSPORT_FAILED);
1173 switch (srb->cmnd[1] & 0x0F) {
1174 case 0:
1175 /* SD Card Power Off -> ON and Initialization */
1176 #ifdef SUPPORT_SD_LOCK
1177 if (0x64 == srb->cmnd[9]) {
1178 /* Command Mode */
1179 sd_card->sd_lock_status |= SD_SDR_RST;
1181 #endif /* SUPPORT_SD_LOCK */
1182 retval = reset_sd_card(chip);
1183 if (retval != STATUS_SUCCESS) {
1184 #ifdef SUPPORT_SD_LOCK
1185 sd_card->sd_lock_status &= ~SD_SDR_RST;
1186 #endif
1187 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1188 sd_card->pre_cmd_err = 1;
1189 TRACE_RET(chip, TRANSPORT_FAILED);
1191 #ifdef SUPPORT_SD_LOCK
1192 sd_card->sd_lock_status &= ~SD_SDR_RST;
1193 #endif
1194 break;
1196 case 1:
1197 /* reset CMD(CMD0) and Initialization
1198 * (without SD Card Power Off -> ON) */
1199 retval = soft_reset_sd_card(chip);
1200 if (retval != STATUS_SUCCESS) {
1201 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1202 sd_card->pre_cmd_err = 1;
1203 TRACE_RET(chip, TRANSPORT_FAILED);
1205 break;
1207 default:
1208 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1209 TRACE_RET(chip, TRANSPORT_FAILED);
1212 scsi_set_resid(srb, 0);
1213 return TRANSPORT_GOOD;
1215 #endif