blob: be441d84bc6460db0b56a89c067010a4804eb9e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Driver for USB Mass Storage compliant devices
2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Current development and maintenance by:
4 * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 *
6 * Developed with the assistance of:
7 * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
8 * (c) 2002 Alan Stern (stern@rowland.org)
9 *
10 * Initial work by:
11 * (c) 1999 Michael Gee (michael@linuxspecific.com)
12 *
13 * This driver is based on the 'USB Mass Storage Class' document. This
14 * describes in detail the protocol used to communicate with such
15 * devices. Clearly, the designers had SCSI and ATAPI commands in
16 * mind when they created this document. The commands are all very
17 * similar to commands in the SCSI-II and ATAPI specifications.
18 *
19 * It is important to note that in a number of cases this class
20 * exhibits class-specific exemptions from the USB specification.
21 * Notably the usage of NAK, STALL and ACK differs from the norm, in
22 * that they are used to communicate wait, failed and OK on commands.
23 *
24 * Also, for certain devices, the interrupt endpoint is used to convey
25 * status of a command.
26 *
27 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
28 * information about this driver.
29 *
30 * This program is free software; you can redistribute it and/or modify it
31 * under the terms of the GNU General Public License as published by the
32 * Free Software Foundation; either version 2, or (at your option) any
33 * later version.
34 *
35 * This program is distributed in the hope that it will be useful, but
36 * WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 675 Mass Ave, Cambridge, MA 02139, USA.
43 */
44
45#include <linux/highmem.h>
46#include <scsi/scsi.h>
47#include <scsi/scsi_cmnd.h>
48
49#include "usb.h"
50#include "protocol.h"
51#include "debug.h"
52#include "scsiglue.h"
53#include "transport.h"
54
55/***********************************************************************
56 * Protocol routines
57 ***********************************************************************/
58
Alan Stern3dae5342008-11-20 14:22:18 -050059void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Alan Stern3dae5342008-11-20 14:22:18 -050061 /* Pad the SCSI command with zeros out to 12 bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 * NOTE: This only works because a scsi_cmnd struct field contains
64 * a unsigned char cmnd[16], so we know we have storage available
65 */
66 for (; srb->cmd_len<12; srb->cmd_len++)
67 srb->cmnd[srb->cmd_len] = 0;
68
69 /* set command length to 12 bytes */
70 srb->cmd_len = 12;
71
72 /* send the command to the transport layer */
73 usb_stor_invoke_transport(srb, us);
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
77{
78 /* fix some commands -- this is a form of mode translation
79 * UFI devices only accept 12 byte long commands
80 *
81 * NOTE: This only works because a scsi_cmnd struct field contains
82 * a unsigned char cmnd[16], so we know we have storage available
83 */
84
85 /* Pad the ATAPI command with zeros */
86 for (; srb->cmd_len<12; srb->cmd_len++)
87 srb->cmnd[srb->cmd_len] = 0;
88
89 /* set command length to 12 bytes (this affects the transport layer) */
90 srb->cmd_len = 12;
91
92 /* XXX We should be constantly re-evaluating the need for these */
93
94 /* determine the correct data length for these commands */
95 switch (srb->cmnd[0]) {
96
97 /* for INQUIRY, UFI devices only ever return 36 bytes */
98 case INQUIRY:
99 srb->cmnd[4] = 36;
100 break;
101
102 /* again, for MODE_SENSE_10, we get the minimum (8) */
103 case MODE_SENSE_10:
104 srb->cmnd[7] = 0;
105 srb->cmnd[8] = 8;
106 break;
107
108 /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
109 case REQUEST_SENSE:
110 srb->cmnd[4] = 18;
111 break;
112 } /* end switch on cmnd[0] */
113
114 /* send the command to the transport layer */
115 usb_stor_invoke_transport(srb, us);
116}
117
118void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
119 struct us_data *us)
120{
121 /* send the command to the transport layer */
122 usb_stor_invoke_transport(srb, us);
123}
124
125/***********************************************************************
126 * Scatter-gather transfer buffer access routines
127 ***********************************************************************/
128
129/* Copy a buffer of length buflen to/from the srb's transfer buffer.
Boaz Harroshdd829d22007-09-09 20:22:23 +0300130 * Update the **sgptr and *offset variables so that the next copy will
Alan Stern70841912008-02-20 14:15:58 -0500131 * pick up from where this one left off.
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200134 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned int *offset, enum xfer_buf_dir dir)
136{
137 unsigned int cnt;
Alan Stern70841912008-02-20 14:15:58 -0500138 struct scatterlist *sg = *sgptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Boaz Harroshdd829d22007-09-09 20:22:23 +0300140 /* We have to go through the list one entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * at a time. Each s-g entry contains some number of pages, and
142 * each page has to be kmap()'ed separately. If the page is already
143 * in kernel-addressable memory then kmap() will return its address.
144 * If the page is not directly accessible -- such as a user buffer
145 * located in high memory -- then kmap() will map it to a temporary
Alan Stern70841912008-02-20 14:15:58 -0500146 * position in the kernel's virtual address space.
147 */
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200148
Boaz Harroshdd829d22007-09-09 20:22:23 +0300149 if (!sg)
150 sg = scsi_sglist(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Boaz Harroshdd829d22007-09-09 20:22:23 +0300152 /* This loop handles a single s-g list entry, which may
Alan Stern70841912008-02-20 14:15:58 -0500153 * include multiple pages. Find the initial page structure
154 * and the starting offset within the page, and update
155 * the *offset and **sgptr values for the next loop.
156 */
Boaz Harroshdd829d22007-09-09 20:22:23 +0300157 cnt = 0;
Alan Stern70841912008-02-20 14:15:58 -0500158 while (cnt < buflen && sg) {
Boaz Harroshdd829d22007-09-09 20:22:23 +0300159 struct page *page = sg_page(sg) +
160 ((sg->offset + *offset) >> PAGE_SHIFT);
Alan Stern70841912008-02-20 14:15:58 -0500161 unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
Boaz Harroshdd829d22007-09-09 20:22:23 +0300162 unsigned int sglen = sg->length - *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Boaz Harroshdd829d22007-09-09 20:22:23 +0300164 if (sglen > buflen - cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Boaz Harroshdd829d22007-09-09 20:22:23 +0300166 /* Transfer ends within this s-g entry */
167 sglen = buflen - cnt;
168 *offset += sglen;
169 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Boaz Harroshdd829d22007-09-09 20:22:23 +0300171 /* Transfer continues to next s-g entry */
172 *offset = 0;
173 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
Boaz Harroshdd829d22007-09-09 20:22:23 +0300175
176 /* Transfer the data for all the pages in this
177 * s-g entry. For each page: call kmap(), do the
178 * transfer, and call kunmap() immediately after. */
179 while (sglen > 0) {
180 unsigned int plen = min(sglen, (unsigned int)
181 PAGE_SIZE - poff);
182 unsigned char *ptr = kmap(page);
183
184 if (dir == TO_XFER_BUF)
185 memcpy(ptr + poff, buffer + cnt, plen);
186 else
187 memcpy(buffer + cnt, ptr + poff, plen);
188 kunmap(page);
189
190 /* Start at the beginning of the next page */
191 poff = 0;
192 ++page;
193 cnt += plen;
194 sglen -= plen;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Boaz Harroshdd829d22007-09-09 20:22:23 +0300197 *sgptr = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* Return the amount actually transferred */
200 return cnt;
201}
202
203/* Store the contents of buffer into srb's transfer buffer and set the
Alan Stern70841912008-02-20 14:15:58 -0500204 * SCSI residue.
205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206void usb_stor_set_xfer_buf(unsigned char *buffer,
207 unsigned int buflen, struct scsi_cmnd *srb)
208{
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200209 unsigned int offset = 0;
210 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Alan Stern6d512a82008-02-22 17:00:06 -0500212 buflen = min(buflen, scsi_bufflen(srb));
Alan Stern70841912008-02-20 14:15:58 -0500213 buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 TO_XFER_BUF);
Boaz Harroshdd829d22007-09-09 20:22:23 +0300215 if (buflen < scsi_bufflen(srb))
216 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}