Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Felipe Balbi | f0183a3 | 2016-04-18 13:09:11 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Driver for USB Mass Storage compliant devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Current development and maintenance by: |
| 6 | * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) |
| 7 | * |
| 8 | * Developed with the assistance of: |
| 9 | * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) |
| 10 | * (c) 2002 Alan Stern (stern@rowland.org) |
| 11 | * |
| 12 | * Initial work by: |
| 13 | * (c) 1999 Michael Gee (michael@linuxspecific.com) |
| 14 | * |
| 15 | * This driver is based on the 'USB Mass Storage Class' document. This |
| 16 | * describes in detail the protocol used to communicate with such |
| 17 | * devices. Clearly, the designers had SCSI and ATAPI commands in |
| 18 | * mind when they created this document. The commands are all very |
| 19 | * similar to commands in the SCSI-II and ATAPI specifications. |
| 20 | * |
| 21 | * It is important to note that in a number of cases this class |
| 22 | * exhibits class-specific exemptions from the USB specification. |
| 23 | * Notably the usage of NAK, STALL and ACK differs from the norm, in |
| 24 | * that they are used to communicate wait, failed and OK on commands. |
| 25 | * |
| 26 | * Also, for certain devices, the interrupt endpoint is used to convey |
| 27 | * status of a command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #include <linux/highmem.h> |
Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 31 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <scsi/scsi.h> |
| 33 | #include <scsi/scsi_cmnd.h> |
| 34 | |
| 35 | #include "usb.h" |
| 36 | #include "protocol.h" |
| 37 | #include "debug.h" |
| 38 | #include "scsiglue.h" |
| 39 | #include "transport.h" |
| 40 | |
| 41 | /*********************************************************************** |
| 42 | * Protocol routines |
| 43 | ***********************************************************************/ |
| 44 | |
Alan Stern | 3dae534 | 2008-11-20 14:22:18 -0500 | [diff] [blame] | 45 | void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Alan Stern | 2f640bf | 2011-10-25 10:50:58 -0400 | [diff] [blame] | 47 | /* |
| 48 | * Pad the SCSI command with zeros out to 12 bytes. If the |
| 49 | * command already is 12 bytes or longer, leave it alone. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * |
| 51 | * NOTE: This only works because a scsi_cmnd struct field contains |
| 52 | * a unsigned char cmnd[16], so we know we have storage available |
| 53 | */ |
Jeffrin Jose | fc8ef48 | 2012-05-30 18:05:47 +0530 | [diff] [blame] | 54 | for (; srb->cmd_len < 12; srb->cmd_len++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | srb->cmnd[srb->cmd_len] = 0; |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* send the command to the transport layer */ |
| 58 | usb_stor_invoke_transport(srb, us); |
| 59 | } |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us) |
| 62 | { |
Felipe Balbi | f0183a3 | 2016-04-18 13:09:11 +0300 | [diff] [blame] | 63 | /* |
| 64 | * fix some commands -- this is a form of mode translation |
Jeffrin Jose | fc8ef48 | 2012-05-30 18:05:47 +0530 | [diff] [blame] | 65 | * UFI devices only accept 12 byte long commands |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 70 | |
| 71 | /* Pad the ATAPI command with zeros */ |
Jeffrin Jose | fc8ef48 | 2012-05-30 18:05:47 +0530 | [diff] [blame] | 72 | for (; srb->cmd_len < 12; srb->cmd_len++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | srb->cmnd[srb->cmd_len] = 0; |
| 74 | |
| 75 | /* set command length to 12 bytes (this affects the transport layer) */ |
| 76 | srb->cmd_len = 12; |
| 77 | |
| 78 | /* XXX We should be constantly re-evaluating the need for these */ |
| 79 | |
| 80 | /* determine the correct data length for these commands */ |
| 81 | switch (srb->cmnd[0]) { |
| 82 | |
| 83 | /* for INQUIRY, UFI devices only ever return 36 bytes */ |
| 84 | case INQUIRY: |
| 85 | srb->cmnd[4] = 36; |
| 86 | break; |
| 87 | |
| 88 | /* again, for MODE_SENSE_10, we get the minimum (8) */ |
| 89 | case MODE_SENSE_10: |
| 90 | srb->cmnd[7] = 0; |
| 91 | srb->cmnd[8] = 8; |
| 92 | break; |
| 93 | |
| 94 | /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */ |
| 95 | case REQUEST_SENSE: |
| 96 | srb->cmnd[4] = 18; |
| 97 | break; |
| 98 | } /* end switch on cmnd[0] */ |
| 99 | |
| 100 | /* send the command to the transport layer */ |
| 101 | usb_stor_invoke_transport(srb, us); |
| 102 | } |
| 103 | |
| 104 | void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb, |
| 105 | struct us_data *us) |
| 106 | { |
| 107 | /* send the command to the transport layer */ |
| 108 | usb_stor_invoke_transport(srb, us); |
| 109 | } |
Alan Stern | e6e244b | 2009-02-12 14:47:44 -0500 | [diff] [blame] | 110 | EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | /*********************************************************************** |
| 113 | * Scatter-gather transfer buffer access routines |
| 114 | ***********************************************************************/ |
| 115 | |
Felipe Balbi | f0183a3 | 2016-04-18 13:09:11 +0300 | [diff] [blame] | 116 | /* |
| 117 | * Copy a buffer of length buflen to/from the srb's transfer buffer. |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 118 | * Update the **sgptr and *offset variables so that the next copy will |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 119 | * pick up from where this one left off. |
| 120 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 122 | unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | unsigned int *offset, enum xfer_buf_dir dir) |
| 124 | { |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 125 | unsigned int cnt = 0; |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 126 | struct scatterlist *sg = *sgptr; |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 127 | struct sg_mapping_iter miter; |
| 128 | unsigned int nents = scsi_sg_count(srb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 130 | if (sg) |
| 131 | nents = sg_nents(sg); |
| 132 | else |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 133 | sg = scsi_sglist(srb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 135 | sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ? |
| 136 | SG_MITER_FROM_SG: SG_MITER_TO_SG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 138 | if (!sg_miter_skip(&miter, *offset)) |
| 139 | return cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 141 | while (sg_miter_next(&miter) && cnt < buflen) { |
Ming Lei | 40fcd88b8 | 2013-12-10 15:01:42 +0800 | [diff] [blame] | 142 | unsigned int len = min_t(unsigned int, miter.length, |
| 143 | buflen - cnt); |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 144 | |
| 145 | if (dir == FROM_XFER_BUF) |
| 146 | memcpy(buffer + cnt, miter.addr, len); |
| 147 | else |
| 148 | memcpy(miter.addr, buffer + cnt, len); |
| 149 | |
| 150 | if (*offset + len < miter.piter.sg->length) { |
| 151 | *offset += len; |
| 152 | *sgptr = miter.piter.sg; |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 153 | } else { |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 154 | *offset = 0; |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 155 | *sgptr = sg_next(miter.piter.sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 157 | cnt += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
Ming Lei | e5fc70d | 2013-11-26 12:44:13 +0800 | [diff] [blame] | 159 | sg_miter_stop(&miter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return cnt; |
| 162 | } |
Alan Stern | e6e244b | 2009-02-12 14:47:44 -0500 | [diff] [blame] | 163 | EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Felipe Balbi | f0183a3 | 2016-04-18 13:09:11 +0300 | [diff] [blame] | 165 | /* |
| 166 | * Store the contents of buffer into srb's transfer buffer and set the |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 167 | * SCSI residue. |
| 168 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | void usb_stor_set_xfer_buf(unsigned char *buffer, |
| 170 | unsigned int buflen, struct scsi_cmnd *srb) |
| 171 | { |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 172 | unsigned int offset = 0; |
| 173 | struct scatterlist *sg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Alan Stern | 6d512a8 | 2008-02-22 17:00:06 -0500 | [diff] [blame] | 175 | buflen = min(buflen, scsi_bufflen(srb)); |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 176 | buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | TO_XFER_BUF); |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 178 | if (buflen < scsi_bufflen(srb)) |
| 179 | scsi_set_resid(srb, scsi_bufflen(srb) - buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
Alan Stern | e6e244b | 2009-02-12 14:47:44 -0500 | [diff] [blame] | 181 | EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf); |