Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Driver for USB Mass Storage compliant devices |
| 2 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 46 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #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 Stern | 3dae534 | 2008-11-20 14:22:18 -0500 | [diff] [blame] | 60 | 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] | 61 | { |
Alan Stern | 3dae534 | 2008-11-20 14:22:18 -0500 | [diff] [blame] | 62 | /* Pad the SCSI command with zeros out to 12 bytes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * |
| 64 | * NOTE: This only works because a scsi_cmnd struct field contains |
| 65 | * a unsigned char cmnd[16], so we know we have storage available |
| 66 | */ |
| 67 | for (; srb->cmd_len<12; srb->cmd_len++) |
| 68 | srb->cmnd[srb->cmd_len] = 0; |
| 69 | |
| 70 | /* set command length to 12 bytes */ |
| 71 | srb->cmd_len = 12; |
| 72 | |
| 73 | /* send the command to the transport layer */ |
| 74 | usb_stor_invoke_transport(srb, us); |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us) |
| 78 | { |
| 79 | /* fix some commands -- this is a form of mode translation |
| 80 | * UFI devices only accept 12 byte long commands |
| 81 | * |
| 82 | * NOTE: This only works because a scsi_cmnd struct field contains |
| 83 | * a unsigned char cmnd[16], so we know we have storage available |
| 84 | */ |
| 85 | |
| 86 | /* Pad the ATAPI command with zeros */ |
| 87 | for (; srb->cmd_len<12; srb->cmd_len++) |
| 88 | srb->cmnd[srb->cmd_len] = 0; |
| 89 | |
| 90 | /* set command length to 12 bytes (this affects the transport layer) */ |
| 91 | srb->cmd_len = 12; |
| 92 | |
| 93 | /* XXX We should be constantly re-evaluating the need for these */ |
| 94 | |
| 95 | /* determine the correct data length for these commands */ |
| 96 | switch (srb->cmnd[0]) { |
| 97 | |
| 98 | /* for INQUIRY, UFI devices only ever return 36 bytes */ |
| 99 | case INQUIRY: |
| 100 | srb->cmnd[4] = 36; |
| 101 | break; |
| 102 | |
| 103 | /* again, for MODE_SENSE_10, we get the minimum (8) */ |
| 104 | case MODE_SENSE_10: |
| 105 | srb->cmnd[7] = 0; |
| 106 | srb->cmnd[8] = 8; |
| 107 | break; |
| 108 | |
| 109 | /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */ |
| 110 | case REQUEST_SENSE: |
| 111 | srb->cmnd[4] = 18; |
| 112 | break; |
| 113 | } /* end switch on cmnd[0] */ |
| 114 | |
| 115 | /* send the command to the transport layer */ |
| 116 | usb_stor_invoke_transport(srb, us); |
| 117 | } |
| 118 | |
| 119 | void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb, |
| 120 | struct us_data *us) |
| 121 | { |
| 122 | /* send the command to the transport layer */ |
| 123 | usb_stor_invoke_transport(srb, us); |
| 124 | } |
Alan Stern | e6e244b | 2009-02-12 14:47:44 -0500 | [diff] [blame] | 125 | EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | /*********************************************************************** |
| 128 | * Scatter-gather transfer buffer access routines |
| 129 | ***********************************************************************/ |
| 130 | |
| 131 | /* 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] | 132 | * Update the **sgptr and *offset variables so that the next copy will |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 133 | * pick up from where this one left off. |
| 134 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 136 | unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | unsigned int *offset, enum xfer_buf_dir dir) |
| 138 | { |
| 139 | unsigned int cnt; |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 140 | struct scatterlist *sg = *sgptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 142 | /* We have to go through the list one entry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | * at a time. Each s-g entry contains some number of pages, and |
| 144 | * each page has to be kmap()'ed separately. If the page is already |
| 145 | * in kernel-addressable memory then kmap() will return its address. |
| 146 | * If the page is not directly accessible -- such as a user buffer |
| 147 | * located in high memory -- then kmap() will map it to a temporary |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 148 | * position in the kernel's virtual address space. |
| 149 | */ |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 150 | |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 151 | if (!sg) |
| 152 | sg = scsi_sglist(srb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 154 | /* This loop handles a single s-g list entry, which may |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 155 | * include multiple pages. Find the initial page structure |
| 156 | * and the starting offset within the page, and update |
| 157 | * the *offset and **sgptr values for the next loop. |
| 158 | */ |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 159 | cnt = 0; |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 160 | while (cnt < buflen && sg) { |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 161 | struct page *page = sg_page(sg) + |
| 162 | ((sg->offset + *offset) >> PAGE_SHIFT); |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 163 | unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1); |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 164 | unsigned int sglen = sg->length - *offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 166 | if (sglen > buflen - cnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 168 | /* Transfer ends within this s-g entry */ |
| 169 | sglen = buflen - cnt; |
| 170 | *offset += sglen; |
| 171 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 173 | /* Transfer continues to next s-g entry */ |
| 174 | *offset = 0; |
| 175 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 177 | |
| 178 | /* Transfer the data for all the pages in this |
| 179 | * s-g entry. For each page: call kmap(), do the |
| 180 | * transfer, and call kunmap() immediately after. */ |
| 181 | while (sglen > 0) { |
| 182 | unsigned int plen = min(sglen, (unsigned int) |
| 183 | PAGE_SIZE - poff); |
| 184 | unsigned char *ptr = kmap(page); |
| 185 | |
| 186 | if (dir == TO_XFER_BUF) |
| 187 | memcpy(ptr + poff, buffer + cnt, plen); |
| 188 | else |
| 189 | memcpy(buffer + cnt, ptr + poff, plen); |
| 190 | kunmap(page); |
| 191 | |
| 192 | /* Start at the beginning of the next page */ |
| 193 | poff = 0; |
| 194 | ++page; |
| 195 | cnt += plen; |
| 196 | sglen -= plen; |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 199 | *sgptr = sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | /* Return the amount actually transferred */ |
| 202 | return cnt; |
| 203 | } |
Alan Stern | e6e244b | 2009-02-12 14:47:44 -0500 | [diff] [blame] | 204 | EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | /* 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] | 207 | * SCSI residue. |
| 208 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | void usb_stor_set_xfer_buf(unsigned char *buffer, |
| 210 | unsigned int buflen, struct scsi_cmnd *srb) |
| 211 | { |
Jens Axboe | 1f6f31a | 2007-05-11 12:33:09 +0200 | [diff] [blame] | 212 | unsigned int offset = 0; |
| 213 | struct scatterlist *sg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Alan Stern | 6d512a8 | 2008-02-22 17:00:06 -0500 | [diff] [blame] | 215 | buflen = min(buflen, scsi_bufflen(srb)); |
Alan Stern | 7084191 | 2008-02-20 14:15:58 -0500 | [diff] [blame] | 216 | buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | TO_XFER_BUF); |
Boaz Harrosh | dd829d2 | 2007-09-09 20:22:23 +0300 | [diff] [blame] | 218 | if (buflen < scsi_bufflen(srb)) |
| 219 | scsi_set_resid(srb, scsi_bufflen(srb) - buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
Alan Stern | e6e244b | 2009-02-12 14:47:44 -0500 | [diff] [blame] | 221 | EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf); |