added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / usb / storage / cypress_atacb.c
blob9466a99baab635755d540acc754c057aef4fb4bd
1 /*
2 * Support for emulating SAT (ata pass through) on devices based
3 * on the Cypress USB/ATA bridge supporting ATACB.
5 * Copyright (c) 2008 Matthieu Castet (castet.matthieu@free.fr)
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_cmnd.h>
24 #include <scsi/scsi_eh.h>
25 #include <linux/ata.h>
27 #include "usb.h"
28 #include "protocol.h"
29 #include "scsiglue.h"
30 #include "debug.h"
33 * ATACB is a protocol used on cypress usb<->ata bridge to
34 * send raw ATA command over mass storage
35 * There is a ATACB2 protocol that support LBA48 on newer chip.
36 * More info that be found on cy7c68310_8.pdf and cy7c68300c_8.pdf
37 * datasheet from cypress.com.
39 void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
41 unsigned char save_cmnd[MAX_COMMAND_SIZE];
43 if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
44 usb_stor_transparent_scsi_command(srb, us);
45 return;
48 memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
49 memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
51 /* check if we support the command */
52 if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
53 goto invalid_fld;
54 /* check protocol */
55 switch((save_cmnd[1] >> 1) & 0xf) {
56 case 3: /*no DATA */
57 case 4: /* PIO in */
58 case 5: /* PIO out */
59 break;
60 default:
61 goto invalid_fld;
64 /* first build the ATACB command */
65 srb->cmd_len = 16;
67 srb->cmnd[0] = 0x24; /* bVSCBSignature : vendor-specific command
68 this value can change, but most(all ?) manufacturers
69 keep the cypress default : 0x24 */
70 srb->cmnd[1] = 0x24; /* bVSCBSubCommand : 0x24 for ATACB */
72 srb->cmnd[3] = 0xff - 1; /* features, sector count, lba low, lba med
73 lba high, device, command are valid */
74 srb->cmnd[4] = 1; /* TransferBlockCount : 512 */
76 if (save_cmnd[0] == ATA_16) {
77 srb->cmnd[ 6] = save_cmnd[ 4]; /* features */
78 srb->cmnd[ 7] = save_cmnd[ 6]; /* sector count */
79 srb->cmnd[ 8] = save_cmnd[ 8]; /* lba low */
80 srb->cmnd[ 9] = save_cmnd[10]; /* lba med */
81 srb->cmnd[10] = save_cmnd[12]; /* lba high */
82 srb->cmnd[11] = save_cmnd[13]; /* device */
83 srb->cmnd[12] = save_cmnd[14]; /* command */
85 if (save_cmnd[1] & 0x01) {/* extended bit set for LBA48 */
86 /* this could be supported by atacb2 */
87 if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
88 || save_cmnd[11])
89 goto invalid_fld;
92 else { /* ATA12 */
93 srb->cmnd[ 6] = save_cmnd[3]; /* features */
94 srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
95 srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
96 srb->cmnd[ 9] = save_cmnd[6]; /* lba med */
97 srb->cmnd[10] = save_cmnd[7]; /* lba high */
98 srb->cmnd[11] = save_cmnd[8]; /* device */
99 srb->cmnd[12] = save_cmnd[9]; /* command */
102 /* Filter SET_FEATURES - XFER MODE command */
103 if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
104 && (srb->cmnd[6] == SETFEATURES_XFER))
105 goto invalid_fld;
107 if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
108 srb->cmnd[2] |= (1<<7); /* set IdentifyPacketDevice for these cmds */
111 usb_stor_transparent_scsi_command(srb, us);
113 /* if the device doesn't support ATACB
115 if (srb->result == SAM_STAT_CHECK_CONDITION &&
116 memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
117 sizeof(usb_stor_sense_invalidCDB)) == 0) {
118 US_DEBUGP("cypress atacb not supported ???\n");
119 goto end;
122 /* if ck_cond flags is set, and there wasn't critical error,
123 * build the special sense
125 if ((srb->result != (DID_ERROR << 16) &&
126 srb->result != (DID_ABORT << 16)) &&
127 save_cmnd[2] & 0x20) {
128 struct scsi_eh_save ses;
129 unsigned char regs[8];
130 unsigned char *sb = srb->sense_buffer;
131 unsigned char *desc = sb + 8;
132 int tmp_result;
134 /* build the command for
135 * reading the ATA registers */
136 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sizeof(regs));
138 /* we use the same command as before, but we set
139 * the read taskfile bit, for not executing atacb command,
140 * but reading register selected in srb->cmnd[4]
142 srb->cmd_len = 16;
143 srb->cmnd = ses.cmnd;
144 srb->cmnd[2] = 1;
146 usb_stor_transparent_scsi_command(srb, us);
147 memcpy(regs, srb->sense_buffer, sizeof(regs));
148 tmp_result = srb->result;
149 scsi_eh_restore_cmnd(srb, &ses);
150 /* we fail to get registers, report invalid command */
151 if (tmp_result != SAM_STAT_GOOD)
152 goto invalid_fld;
154 /* build the sense */
155 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
157 /* set sk, asc for a good command */
158 sb[1] = RECOVERED_ERROR;
159 sb[2] = 0; /* ATA PASS THROUGH INFORMATION AVAILABLE */
160 sb[3] = 0x1D;
162 /* XXX we should generate sk, asc, ascq from status and error
163 * regs
164 * (see 11.1 Error translation ATA device error to SCSI error
165 * map, and ata_to_sense_error from libata.)
168 /* Sense data is current and format is descriptor. */
169 sb[0] = 0x72;
170 desc[0] = 0x09; /* ATA_RETURN_DESCRIPTOR */
172 /* set length of additional sense data */
173 sb[7] = 14;
174 desc[1] = 12;
176 /* Copy registers into sense buffer. */
177 desc[ 2] = 0x00;
178 desc[ 3] = regs[1]; /* features */
179 desc[ 5] = regs[2]; /* sector count */
180 desc[ 7] = regs[3]; /* lba low */
181 desc[ 9] = regs[4]; /* lba med */
182 desc[11] = regs[5]; /* lba high */
183 desc[12] = regs[6]; /* device */
184 desc[13] = regs[7]; /* command */
186 srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
188 goto end;
189 invalid_fld:
190 srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
192 memcpy(srb->sense_buffer,
193 usb_stor_sense_invalidCDB,
194 sizeof(usb_stor_sense_invalidCDB));
195 end:
196 memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
197 if (srb->cmnd[0] == ATA_12)
198 srb->cmd_len = 12;