usb-storage: make shuttle_usbat a separate module
[linux-2.6] / drivers / usb / storage / cypress_atacb.c
1 /*
2  * Support for emulating SAT (ata pass through) on devices based
3  *       on the Cypress USB/ATA bridge supporting ATACB.
4  *
5  * Copyright (c) 2008 Matthieu Castet (castet.matthieu@free.fr)
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <linux/module.h>
23 #include <scsi/scsi.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_eh.h>
26 #include <linux/ata.h>
27
28 #include "usb.h"
29 #include "protocol.h"
30 #include "scsiglue.h"
31 #include "debug.h"
32
33
34 /*
35  * The table of devices
36  */
37 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
38                     vendorName, productName, useProtocol, useTransport, \
39                     initFunction, flags) \
40 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
41   .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
42
43 struct usb_device_id cypress_usb_ids[] = {
44 #       include "unusual_cypress.h"
45         { }             /* Terminating entry */
46 };
47 MODULE_DEVICE_TABLE(usb, cypress_usb_ids);
48
49 #undef UNUSUAL_DEV
50
51 /*
52  * The flags table
53  */
54 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
55                     vendor_name, product_name, use_protocol, use_transport, \
56                     init_function, Flags) \
57 { \
58         .vendorName = vendor_name,      \
59         .productName = product_name,    \
60         .useProtocol = use_protocol,    \
61         .useTransport = use_transport,  \
62         .initFunction = init_function,  \
63 }
64
65 static struct us_unusual_dev cypress_unusual_dev_list[] = {
66 #       include "unusual_cypress.h"
67         { }             /* Terminating entry */
68 };
69
70 #undef UNUSUAL_DEV
71
72
73 /*
74  * ATACB is a protocol used on cypress usb<->ata bridge to
75  * send raw ATA command over mass storage
76  * There is a ATACB2 protocol that support LBA48 on newer chip.
77  * More info that be found on cy7c68310_8.pdf and cy7c68300c_8.pdf
78  * datasheet from cypress.com.
79  */
80 static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
81 {
82         unsigned char save_cmnd[MAX_COMMAND_SIZE];
83
84         if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
85                 usb_stor_transparent_scsi_command(srb, us);
86                 return;
87         }
88
89         memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
90         memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
91
92         /* check if we support the command */
93         if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
94                 goto invalid_fld;
95         /* check protocol */
96         switch((save_cmnd[1] >> 1) & 0xf) {
97                 case 3: /*no DATA */
98                 case 4: /* PIO in */
99                 case 5: /* PIO out */
100                         break;
101                 default:
102                         goto invalid_fld;
103         }
104
105         /* first build the ATACB command */
106         srb->cmd_len = 16;
107
108         srb->cmnd[0] = 0x24; /* bVSCBSignature : vendor-specific command
109                                 this value can change, but most(all ?) manufacturers
110                                                         keep the cypress default : 0x24 */
111         srb->cmnd[1] = 0x24; /* bVSCBSubCommand : 0x24 for ATACB */
112
113         srb->cmnd[3] = 0xff - 1; /* features, sector count, lba low, lba med
114                                                                 lba high, device, command are valid */
115         srb->cmnd[4] = 1; /* TransferBlockCount : 512 */
116
117         if (save_cmnd[0] == ATA_16) {
118                 srb->cmnd[ 6] = save_cmnd[ 4]; /* features */
119                 srb->cmnd[ 7] = save_cmnd[ 6]; /* sector count */
120                 srb->cmnd[ 8] = save_cmnd[ 8]; /* lba low */
121                 srb->cmnd[ 9] = save_cmnd[10]; /* lba med */
122                 srb->cmnd[10] = save_cmnd[12]; /* lba high */
123                 srb->cmnd[11] = save_cmnd[13]; /* device */
124                 srb->cmnd[12] = save_cmnd[14]; /* command */
125
126                 if (save_cmnd[1] & 0x01) {/* extended bit set for LBA48 */
127                         /* this could be supported by atacb2 */
128                         if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
129                                         || save_cmnd[11])
130                                 goto invalid_fld;
131                 }
132         }
133         else { /* ATA12 */
134                 srb->cmnd[ 6] = save_cmnd[3]; /* features */
135                 srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
136                 srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
137                 srb->cmnd[ 9] = save_cmnd[6]; /* lba med */
138                 srb->cmnd[10] = save_cmnd[7]; /* lba high */
139                 srb->cmnd[11] = save_cmnd[8]; /* device */
140                 srb->cmnd[12] = save_cmnd[9]; /* command */
141
142         }
143         /* Filter SET_FEATURES - XFER MODE command */
144         if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
145                         && (srb->cmnd[6] == SETFEATURES_XFER))
146                 goto invalid_fld;
147
148         if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
149                 srb->cmnd[2] |= (1<<7); /* set  IdentifyPacketDevice for these cmds */
150
151
152         usb_stor_transparent_scsi_command(srb, us);
153
154         /* if the device doesn't support ATACB
155          */
156         if (srb->result == SAM_STAT_CHECK_CONDITION &&
157                         memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
158                                 sizeof(usb_stor_sense_invalidCDB)) == 0) {
159                 US_DEBUGP("cypress atacb not supported ???\n");
160                 goto end;
161         }
162
163         /* if ck_cond flags is set, and there wasn't critical error,
164          * build the special sense
165          */
166         if ((srb->result != (DID_ERROR << 16) &&
167                                 srb->result != (DID_ABORT << 16)) &&
168                         save_cmnd[2] & 0x20) {
169                 struct scsi_eh_save ses;
170                 unsigned char regs[8];
171                 unsigned char *sb = srb->sense_buffer;
172                 unsigned char *desc = sb + 8;
173                 int tmp_result;
174
175                 /* build the command for
176                  * reading the ATA registers */
177                 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sizeof(regs));
178
179                 /* we use the same command as before, but we set
180                  * the read taskfile bit, for not executing atacb command,
181                  * but reading register selected in srb->cmnd[4]
182                  */
183                 srb->cmd_len = 16;
184                 srb->cmnd = ses.cmnd;
185                 srb->cmnd[2] = 1;
186
187                 usb_stor_transparent_scsi_command(srb, us);
188                 memcpy(regs, srb->sense_buffer, sizeof(regs));
189                 tmp_result = srb->result;
190                 scsi_eh_restore_cmnd(srb, &ses);
191                 /* we fail to get registers, report invalid command */
192                 if (tmp_result != SAM_STAT_GOOD)
193                         goto invalid_fld;
194
195                 /* build the sense */
196                 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
197
198                 /* set sk, asc for a good command */
199                 sb[1] = RECOVERED_ERROR;
200                 sb[2] = 0; /* ATA PASS THROUGH INFORMATION AVAILABLE */
201                 sb[3] = 0x1D;
202
203                 /* XXX we should generate sk, asc, ascq from status and error
204                  * regs
205                  * (see 11.1 Error translation ATA device error to SCSI error
206                  *  map, and ata_to_sense_error from libata.)
207                  */
208
209                 /* Sense data is current and format is descriptor. */
210                 sb[0] = 0x72;
211                 desc[0] = 0x09; /* ATA_RETURN_DESCRIPTOR */
212
213                 /* set length of additional sense data */
214                 sb[7] = 14;
215                 desc[1] = 12;
216
217                 /* Copy registers into sense buffer. */
218                 desc[ 2] = 0x00;
219                 desc[ 3] = regs[1];  /* features */
220                 desc[ 5] = regs[2];  /* sector count */
221                 desc[ 7] = regs[3];  /* lba low */
222                 desc[ 9] = regs[4];  /* lba med */
223                 desc[11] = regs[5];  /* lba high */
224                 desc[12] = regs[6];  /* device */
225                 desc[13] = regs[7];  /* command */
226
227                 srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
228         }
229         goto end;
230 invalid_fld:
231         srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
232
233         memcpy(srb->sense_buffer,
234                         usb_stor_sense_invalidCDB,
235                         sizeof(usb_stor_sense_invalidCDB));
236 end:
237         memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
238         if (srb->cmnd[0] == ATA_12)
239                 srb->cmd_len = 12;
240 }
241
242
243 static int cypress_probe(struct usb_interface *intf,
244                          const struct usb_device_id *id)
245 {
246         struct us_data *us;
247         int result;
248
249         result = usb_stor_probe1(&us, intf, id,
250                         (id - cypress_usb_ids) + cypress_unusual_dev_list);
251         if (result)
252                 return result;
253
254         us->protocol_name = "Transparent SCSI with Cypress ATACB";
255         us->proto_handler = cypress_atacb_passthrough;
256
257         result = usb_stor_probe2(us);
258         return result;
259 }
260
261 static struct usb_driver cypress_driver = {
262         .name =         "ums-cypress",
263         .probe =        cypress_probe,
264         .disconnect =   usb_stor_disconnect,
265         .suspend =      usb_stor_suspend,
266         .resume =       usb_stor_resume,
267         .reset_resume = usb_stor_reset_resume,
268         .pre_reset =    usb_stor_pre_reset,
269         .post_reset =   usb_stor_post_reset,
270         .id_table =     cypress_usb_ids,
271         .soft_unbind =  1,
272 };
273
274 static int __init cypress_init(void)
275 {
276         return usb_register(&cypress_driver);
277 }
278
279 static void __exit cypress_exit(void)
280 {
281         usb_deregister(&cypress_driver);
282 }
283
284 module_init(cypress_init);
285 module_exit(cypress_exit);