1
2
3
4
5
6
7
8
9#include <linux/module.h>
10#include <scsi/scsi.h>
11#include <scsi/scsi_cmnd.h>
12#include <scsi/scsi_eh.h>
13#include <linux/ata.h>
14
15#include "usb.h"
16#include "protocol.h"
17#include "scsiglue.h"
18#include "debug.h"
19
20#define DRV_NAME "ums-cypress"
21
22MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
23MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
24MODULE_LICENSE("GPL");
25
26
27
28
29#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
30 vendorName, productName, useProtocol, useTransport, \
31 initFunction, flags) \
32{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
33 .driver_info = (flags) }
34
35static struct usb_device_id cypress_usb_ids[] = {
36# include "unusual_cypress.h"
37 { }
38};
39MODULE_DEVICE_TABLE(usb, cypress_usb_ids);
40
41#undef UNUSUAL_DEV
42
43
44
45
46#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
47 vendor_name, product_name, use_protocol, use_transport, \
48 init_function, Flags) \
49{ \
50 .vendorName = vendor_name, \
51 .productName = product_name, \
52 .useProtocol = use_protocol, \
53 .useTransport = use_transport, \
54 .initFunction = init_function, \
55}
56
57static struct us_unusual_dev cypress_unusual_dev_list[] = {
58# include "unusual_cypress.h"
59 { }
60};
61
62#undef UNUSUAL_DEV
63
64
65
66
67
68
69
70
71
72static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
73{
74 unsigned char save_cmnd[MAX_COMMAND_SIZE];
75
76 if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
77 usb_stor_transparent_scsi_command(srb, us);
78 return;
79 }
80
81 memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
82 memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
83
84
85 if (save_cmnd[1] >> 5)
86 goto invalid_fld;
87
88 switch ((save_cmnd[1] >> 1) & 0xf) {
89 case 3:
90 case 4:
91 case 5:
92 break;
93 default:
94 goto invalid_fld;
95 }
96
97
98 srb->cmd_len = 16;
99
100 srb->cmnd[0] = 0x24;
101
102
103
104
105 srb->cmnd[1] = 0x24;
106
107 srb->cmnd[3] = 0xff - 1;
108
109
110
111 srb->cmnd[4] = 1;
112
113 if (save_cmnd[0] == ATA_16) {
114 srb->cmnd[ 6] = save_cmnd[ 4];
115 srb->cmnd[ 7] = save_cmnd[ 6];
116 srb->cmnd[ 8] = save_cmnd[ 8];
117 srb->cmnd[ 9] = save_cmnd[10];
118 srb->cmnd[10] = save_cmnd[12];
119 srb->cmnd[11] = save_cmnd[13];
120 srb->cmnd[12] = save_cmnd[14];
121
122 if (save_cmnd[1] & 0x01) {
123
124 if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
125 || save_cmnd[11])
126 goto invalid_fld;
127 }
128 } else {
129 srb->cmnd[ 6] = save_cmnd[3];
130 srb->cmnd[ 7] = save_cmnd[4];
131 srb->cmnd[ 8] = save_cmnd[5];
132 srb->cmnd[ 9] = save_cmnd[6];
133 srb->cmnd[10] = save_cmnd[7];
134 srb->cmnd[11] = save_cmnd[8];
135 srb->cmnd[12] = save_cmnd[9];
136
137 }
138
139 if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
140 && (srb->cmnd[6] == SETFEATURES_XFER))
141 goto invalid_fld;
142
143 if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
144 srb->cmnd[2] |= (1<<7);
145
146
147 usb_stor_transparent_scsi_command(srb, us);
148
149
150 if (srb->result == SAM_STAT_CHECK_CONDITION &&
151 memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
152 sizeof(usb_stor_sense_invalidCDB)) == 0) {
153 usb_stor_dbg(us, "cypress atacb not supported ???\n");
154 goto end;
155 }
156
157
158
159
160
161 if ((srb->result != (DID_ERROR << 16) &&
162 srb->result != (DID_ABORT << 16)) &&
163 save_cmnd[2] & 0x20) {
164 struct scsi_eh_save ses;
165 unsigned char regs[8];
166 unsigned char *sb = srb->sense_buffer;
167 unsigned char *desc = sb + 8;
168 int tmp_result;
169
170
171 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sizeof(regs));
172
173
174
175
176
177
178 srb->cmd_len = 16;
179 srb->cmnd = ses.cmnd;
180 srb->cmnd[2] = 1;
181
182 usb_stor_transparent_scsi_command(srb, us);
183 memcpy(regs, srb->sense_buffer, sizeof(regs));
184 tmp_result = srb->result;
185 scsi_eh_restore_cmnd(srb, &ses);
186
187 if (tmp_result != SAM_STAT_GOOD)
188 goto invalid_fld;
189
190
191 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
192
193
194 sb[1] = RECOVERED_ERROR;
195 sb[2] = 0;
196 sb[3] = 0x1D;
197
198
199
200
201
202
203
204
205
206 sb[0] = 0x72;
207 desc[0] = 0x09;
208
209
210 sb[7] = 14;
211 desc[1] = 12;
212
213
214 desc[ 2] = 0x00;
215 desc[ 3] = regs[1];
216 desc[ 5] = regs[2];
217 desc[ 7] = regs[3];
218 desc[ 9] = regs[4];
219 desc[11] = regs[5];
220 desc[12] = regs[6];
221 desc[13] = regs[7];
222
223 srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
224 }
225 goto end;
226invalid_fld:
227 srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
228
229 memcpy(srb->sense_buffer,
230 usb_stor_sense_invalidCDB,
231 sizeof(usb_stor_sense_invalidCDB));
232end:
233 memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
234 if (srb->cmnd[0] == ATA_12)
235 srb->cmd_len = 12;
236}
237
238static struct scsi_host_template cypress_host_template;
239
240static int cypress_probe(struct usb_interface *intf,
241 const struct usb_device_id *id)
242{
243 struct us_data *us;
244 int result;
245 struct usb_device *device;
246
247 result = usb_stor_probe1(&us, intf, id,
248 (id - cypress_usb_ids) + cypress_unusual_dev_list,
249 &cypress_host_template);
250 if (result)
251 return result;
252
253
254
255
256
257 device = interface_to_usbdev(intf);
258 if (device->descriptor.iManufacturer != 0x38 ||
259 device->descriptor.iProduct != 0x4e ||
260 device->descriptor.iSerialNumber != 0x64) {
261 us->protocol_name = "Transparent SCSI with Cypress ATACB";
262 us->proto_handler = cypress_atacb_passthrough;
263 } else {
264 us->protocol_name = "Transparent SCSI";
265 us->proto_handler = usb_stor_transparent_scsi_command;
266 }
267
268 result = usb_stor_probe2(us);
269 return result;
270}
271
272static struct usb_driver cypress_driver = {
273 .name = DRV_NAME,
274 .probe = cypress_probe,
275 .disconnect = usb_stor_disconnect,
276 .suspend = usb_stor_suspend,
277 .resume = usb_stor_resume,
278 .reset_resume = usb_stor_reset_resume,
279 .pre_reset = usb_stor_pre_reset,
280 .post_reset = usb_stor_post_reset,
281 .id_table = cypress_usb_ids,
282 .soft_unbind = 1,
283 .no_dynamic_id = 1,
284};
285
286module_usb_stor_driver(cypress_driver, cypress_host_template, DRV_NAME);
287