Commit | Line | Data |
---|---|---|
f0183a33 FB |
1 | /* |
2 | * Driver for USB Mass Storage compliant devices | |
1da177e4 LT |
3 | * |
4 | * Current development and maintenance by: | |
5 | * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) | |
6 | * | |
7 | * Developed with the assistance of: | |
8 | * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) | |
9 | * (c) 2002 Alan Stern (stern@rowland.org) | |
10 | * | |
11 | * Initial work by: | |
12 | * (c) 1999 Michael Gee (michael@linuxspecific.com) | |
13 | * | |
14 | * This driver is based on the 'USB Mass Storage Class' document. This | |
15 | * describes in detail the protocol used to communicate with such | |
16 | * devices. Clearly, the designers had SCSI and ATAPI commands in | |
17 | * mind when they created this document. The commands are all very | |
18 | * similar to commands in the SCSI-II and ATAPI specifications. | |
19 | * | |
20 | * It is important to note that in a number of cases this class | |
21 | * exhibits class-specific exemptions from the USB specification. | |
22 | * Notably the usage of NAK, STALL and ACK differs from the norm, in | |
23 | * that they are used to communicate wait, failed and OK on commands. | |
24 | * | |
25 | * Also, for certain devices, the interrupt endpoint is used to convey | |
26 | * status of a command. | |
27 | * | |
28 | * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more | |
29 | * information about this driver. | |
30 | * | |
31 | * This program is free software; you can redistribute it and/or modify it | |
32 | * under the terms of the GNU General Public License as published by the | |
33 | * Free Software Foundation; either version 2, or (at your option) any | |
34 | * later version. | |
35 | * | |
36 | * This program is distributed in the hope that it will be useful, but | |
37 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
39 | * General Public License for more details. | |
40 | * | |
41 | * You should have received a copy of the GNU General Public License along | |
42 | * with this program; if not, write to the Free Software Foundation, Inc., | |
43 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |
44 | */ | |
45 | ||
46 | #include <linux/highmem.h> | |
f940fcd8 | 47 | #include <linux/export.h> |
1da177e4 LT |
48 | #include <scsi/scsi.h> |
49 | #include <scsi/scsi_cmnd.h> | |
50 | ||
51 | #include "usb.h" | |
52 | #include "protocol.h" | |
53 | #include "debug.h" | |
54 | #include "scsiglue.h" | |
55 | #include "transport.h" | |
56 | ||
57 | /*********************************************************************** | |
58 | * Protocol routines | |
59 | ***********************************************************************/ | |
60 | ||
3dae5345 | 61 | void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us) |
1da177e4 | 62 | { |
2f640bf4 AS |
63 | /* |
64 | * Pad the SCSI command with zeros out to 12 bytes. If the | |
65 | * command already is 12 bytes or longer, leave it alone. | |
1da177e4 LT |
66 | * |
67 | * NOTE: This only works because a scsi_cmnd struct field contains | |
68 | * a unsigned char cmnd[16], so we know we have storage available | |
69 | */ | |
fc8ef481 | 70 | for (; srb->cmd_len < 12; srb->cmd_len++) |
1da177e4 LT |
71 | srb->cmnd[srb->cmd_len] = 0; |
72 | ||
1da177e4 LT |
73 | /* send the command to the transport layer */ |
74 | usb_stor_invoke_transport(srb, us); | |
75 | } | |
76 | ||
1da177e4 LT |
77 | void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us) |
78 | { | |
f0183a33 FB |
79 | /* |
80 | * fix some commands -- this is a form of mode translation | |
fc8ef481 | 81 | * UFI devices only accept 12 byte long commands |
1da177e4 LT |
82 | * |
83 | * NOTE: This only works because a scsi_cmnd struct field contains | |
84 | * a unsigned char cmnd[16], so we know we have storage available | |
85 | */ | |
86 | ||
87 | /* Pad the ATAPI command with zeros */ | |
fc8ef481 | 88 | for (; srb->cmd_len < 12; srb->cmd_len++) |
1da177e4 LT |
89 | srb->cmnd[srb->cmd_len] = 0; |
90 | ||
91 | /* set command length to 12 bytes (this affects the transport layer) */ | |
92 | srb->cmd_len = 12; | |
93 | ||
94 | /* XXX We should be constantly re-evaluating the need for these */ | |
95 | ||
96 | /* determine the correct data length for these commands */ | |
97 | switch (srb->cmnd[0]) { | |
98 | ||
99 | /* for INQUIRY, UFI devices only ever return 36 bytes */ | |
100 | case INQUIRY: | |
101 | srb->cmnd[4] = 36; | |
102 | break; | |
103 | ||
104 | /* again, for MODE_SENSE_10, we get the minimum (8) */ | |
105 | case MODE_SENSE_10: | |
106 | srb->cmnd[7] = 0; | |
107 | srb->cmnd[8] = 8; | |
108 | break; | |
109 | ||
110 | /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */ | |
111 | case REQUEST_SENSE: | |
112 | srb->cmnd[4] = 18; | |
113 | break; | |
114 | } /* end switch on cmnd[0] */ | |
115 | ||
116 | /* send the command to the transport layer */ | |
117 | usb_stor_invoke_transport(srb, us); | |
118 | } | |
119 | ||
120 | void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb, | |
121 | struct us_data *us) | |
122 | { | |
123 | /* send the command to the transport layer */ | |
124 | usb_stor_invoke_transport(srb, us); | |
125 | } | |
e6e244b6 | 126 | EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command); |
1da177e4 LT |
127 | |
128 | /*********************************************************************** | |
129 | * Scatter-gather transfer buffer access routines | |
130 | ***********************************************************************/ | |
131 | ||
f0183a33 FB |
132 | /* |
133 | * Copy a buffer of length buflen to/from the srb's transfer buffer. | |
dd829d23 | 134 | * Update the **sgptr and *offset variables so that the next copy will |
7084191d AS |
135 | * pick up from where this one left off. |
136 | */ | |
1da177e4 | 137 | unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, |
1f6f31a0 | 138 | unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr, |
1da177e4 LT |
139 | unsigned int *offset, enum xfer_buf_dir dir) |
140 | { | |
e5fc70d5 | 141 | unsigned int cnt = 0; |
7084191d | 142 | struct scatterlist *sg = *sgptr; |
e5fc70d5 ML |
143 | struct sg_mapping_iter miter; |
144 | unsigned int nents = scsi_sg_count(srb); | |
1da177e4 | 145 | |
e5fc70d5 ML |
146 | if (sg) |
147 | nents = sg_nents(sg); | |
148 | else | |
dd829d23 BH |
149 | sg = scsi_sglist(srb); |
150 | ||
e5fc70d5 ML |
151 | sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ? |
152 | SG_MITER_FROM_SG: SG_MITER_TO_SG); | |
dd829d23 | 153 | |
e5fc70d5 ML |
154 | if (!sg_miter_skip(&miter, *offset)) |
155 | return cnt; | |
156 | ||
157 | while (sg_miter_next(&miter) && cnt < buflen) { | |
40fcd88b ML |
158 | unsigned int len = min_t(unsigned int, miter.length, |
159 | buflen - cnt); | |
e5fc70d5 ML |
160 | |
161 | if (dir == FROM_XFER_BUF) | |
162 | memcpy(buffer + cnt, miter.addr, len); | |
163 | else | |
164 | memcpy(miter.addr, buffer + cnt, len); | |
dd829d23 | 165 | |
e5fc70d5 ML |
166 | if (*offset + len < miter.piter.sg->length) { |
167 | *offset += len; | |
168 | *sgptr = miter.piter.sg; | |
169 | } else { | |
170 | *offset = 0; | |
171 | *sgptr = sg_next(miter.piter.sg); | |
1da177e4 | 172 | } |
e5fc70d5 | 173 | cnt += len; |
1da177e4 | 174 | } |
e5fc70d5 | 175 | sg_miter_stop(&miter); |
1da177e4 | 176 | |
1da177e4 LT |
177 | return cnt; |
178 | } | |
e6e244b6 | 179 | EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf); |
1da177e4 | 180 | |
f0183a33 FB |
181 | /* |
182 | * Store the contents of buffer into srb's transfer buffer and set the | |
7084191d AS |
183 | * SCSI residue. |
184 | */ | |
1da177e4 LT |
185 | void usb_stor_set_xfer_buf(unsigned char *buffer, |
186 | unsigned int buflen, struct scsi_cmnd *srb) | |
187 | { | |
1f6f31a0 JA |
188 | unsigned int offset = 0; |
189 | struct scatterlist *sg = NULL; | |
1da177e4 | 190 | |
6d512a80 | 191 | buflen = min(buflen, scsi_bufflen(srb)); |
7084191d | 192 | buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset, |
1da177e4 | 193 | TO_XFER_BUF); |
dd829d23 BH |
194 | if (buflen < scsi_bufflen(srb)) |
195 | scsi_set_resid(srb, scsi_bufflen(srb) - buflen); | |
1da177e4 | 196 | } |
e6e244b6 | 197 | EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf); |