blob: 12e3c2fac64205f349530b4e9df9b49f37c85fdb [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>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040046#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <scsi/scsi.h>
48#include <scsi/scsi_cmnd.h>
49
50#include "usb.h"
51#include "protocol.h"
52#include "debug.h"
53#include "scsiglue.h"
54#include "transport.h"
55
56/***********************************************************************
57 * Protocol routines
58 ***********************************************************************/
59
Alan Stern3dae5342008-11-20 14:22:18 -050060void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Alan Stern2f640bf2011-10-25 10:50:58 -040062 /*
63 * Pad the SCSI command with zeros out to 12 bytes. If the
64 * command already is 12 bytes or longer, leave it alone.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
66 * NOTE: This only works because a scsi_cmnd struct field contains
67 * a unsigned char cmnd[16], so we know we have storage available
68 */
Jeffrin Josefc8ef482012-05-30 18:05:47 +053069 for (; srb->cmd_len < 12; srb->cmd_len++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 srb->cmnd[srb->cmd_len] = 0;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* 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
Jeffrin Josefc8ef482012-05-30 18:05:47 +053079 * UFI devices only accept 12 byte long commands
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
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 */
Jeffrin Josefc8ef482012-05-30 18:05:47 +053086 for (; srb->cmd_len < 12; srb->cmd_len++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 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}
Alan Sterne6e244b2009-02-12 14:47:44 -0500124EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/***********************************************************************
127 * Scatter-gather transfer buffer access routines
128 ***********************************************************************/
129
130/* Copy a buffer of length buflen to/from the srb's transfer buffer.
Boaz Harroshdd829d22007-09-09 20:22:23 +0300131 * Update the **sgptr and *offset variables so that the next copy will
Alan Stern70841912008-02-20 14:15:58 -0500132 * pick up from where this one left off.
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200135 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned int *offset, enum xfer_buf_dir dir)
137{
Ming Leie5fc70d2013-11-26 12:44:13 +0800138 unsigned int cnt = 0;
Alan Stern70841912008-02-20 14:15:58 -0500139 struct scatterlist *sg = *sgptr;
Ming Leie5fc70d2013-11-26 12:44:13 +0800140 struct sg_mapping_iter miter;
141 unsigned int nents = scsi_sg_count(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Ming Leie5fc70d2013-11-26 12:44:13 +0800143 if (sg)
144 nents = sg_nents(sg);
145 else
Boaz Harroshdd829d22007-09-09 20:22:23 +0300146 sg = scsi_sglist(srb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ming Leie5fc70d2013-11-26 12:44:13 +0800148 sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
149 SG_MITER_FROM_SG: SG_MITER_TO_SG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Ming Leie5fc70d2013-11-26 12:44:13 +0800151 if (!sg_miter_skip(&miter, *offset))
152 return cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ming Leie5fc70d2013-11-26 12:44:13 +0800154 while (sg_miter_next(&miter) && cnt < buflen) {
Ming Lei40fcd88b82013-12-10 15:01:42 +0800155 unsigned int len = min_t(unsigned int, miter.length,
156 buflen - cnt);
Ming Leie5fc70d2013-11-26 12:44:13 +0800157
158 if (dir == FROM_XFER_BUF)
159 memcpy(buffer + cnt, miter.addr, len);
160 else
161 memcpy(miter.addr, buffer + cnt, len);
162
163 if (*offset + len < miter.piter.sg->length) {
164 *offset += len;
165 *sgptr = miter.piter.sg;
Boaz Harroshdd829d22007-09-09 20:22:23 +0300166 } else {
Boaz Harroshdd829d22007-09-09 20:22:23 +0300167 *offset = 0;
Ming Leie5fc70d2013-11-26 12:44:13 +0800168 *sgptr = sg_next(miter.piter.sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Ming Leie5fc70d2013-11-26 12:44:13 +0800170 cnt += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Ming Leie5fc70d2013-11-26 12:44:13 +0800172 sg_miter_stop(&miter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return cnt;
175}
Alan Sterne6e244b2009-02-12 14:47:44 -0500176EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Store the contents of buffer into srb's transfer buffer and set the
Alan Stern70841912008-02-20 14:15:58 -0500179 * SCSI residue.
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181void usb_stor_set_xfer_buf(unsigned char *buffer,
182 unsigned int buflen, struct scsi_cmnd *srb)
183{
Jens Axboe1f6f31a2007-05-11 12:33:09 +0200184 unsigned int offset = 0;
185 struct scatterlist *sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Alan Stern6d512a82008-02-22 17:00:06 -0500187 buflen = min(buflen, scsi_bufflen(srb));
Alan Stern70841912008-02-20 14:15:58 -0500188 buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 TO_XFER_BUF);
Boaz Harroshdd829d22007-09-09 20:22:23 +0300190 if (buflen < scsi_bufflen(srb))
191 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Alan Sterne6e244b2009-02-12 14:47:44 -0500193EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);