1*5fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 2f0183a33SFelipe Balbi /* 3f0183a33SFelipe Balbi * Driver for USB Mass Storage compliant devices 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Current development and maintenance by: 61da177e4SLinus Torvalds * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Developed with the assistance of: 91da177e4SLinus Torvalds * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) 101da177e4SLinus Torvalds * (c) 2002 Alan Stern (stern@rowland.org) 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Initial work by: 131da177e4SLinus Torvalds * (c) 1999 Michael Gee (michael@linuxspecific.com) 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * This driver is based on the 'USB Mass Storage Class' document. This 161da177e4SLinus Torvalds * describes in detail the protocol used to communicate with such 171da177e4SLinus Torvalds * devices. Clearly, the designers had SCSI and ATAPI commands in 181da177e4SLinus Torvalds * mind when they created this document. The commands are all very 191da177e4SLinus Torvalds * similar to commands in the SCSI-II and ATAPI specifications. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * It is important to note that in a number of cases this class 221da177e4SLinus Torvalds * exhibits class-specific exemptions from the USB specification. 231da177e4SLinus Torvalds * Notably the usage of NAK, STALL and ACK differs from the norm, in 241da177e4SLinus Torvalds * that they are used to communicate wait, failed and OK on commands. 251da177e4SLinus Torvalds * 261da177e4SLinus Torvalds * Also, for certain devices, the interrupt endpoint is used to convey 271da177e4SLinus Torvalds * status of a command. 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more 301da177e4SLinus Torvalds * information about this driver. 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify it 331da177e4SLinus Torvalds * under the terms of the GNU General Public License as published by the 341da177e4SLinus Torvalds * Free Software Foundation; either version 2, or (at your option) any 351da177e4SLinus Torvalds * later version. 361da177e4SLinus Torvalds * 371da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, but 381da177e4SLinus Torvalds * WITHOUT ANY WARRANTY; without even the implied warranty of 391da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 401da177e4SLinus Torvalds * General Public License for more details. 411da177e4SLinus Torvalds * 421da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License along 431da177e4SLinus Torvalds * with this program; if not, write to the Free Software Foundation, Inc., 441da177e4SLinus Torvalds * 675 Mass Ave, Cambridge, MA 02139, USA. 451da177e4SLinus Torvalds */ 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include <linux/highmem.h> 48f940fcd8SPaul Gortmaker #include <linux/export.h> 491da177e4SLinus Torvalds #include <scsi/scsi.h> 501da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include "usb.h" 531da177e4SLinus Torvalds #include "protocol.h" 541da177e4SLinus Torvalds #include "debug.h" 551da177e4SLinus Torvalds #include "scsiglue.h" 561da177e4SLinus Torvalds #include "transport.h" 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /*********************************************************************** 591da177e4SLinus Torvalds * Protocol routines 601da177e4SLinus Torvalds ***********************************************************************/ 611da177e4SLinus Torvalds 623dae5345SAlan Stern void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us) 631da177e4SLinus Torvalds { 642f640bf4SAlan Stern /* 652f640bf4SAlan Stern * Pad the SCSI command with zeros out to 12 bytes. If the 662f640bf4SAlan Stern * command already is 12 bytes or longer, leave it alone. 671da177e4SLinus Torvalds * 681da177e4SLinus Torvalds * NOTE: This only works because a scsi_cmnd struct field contains 691da177e4SLinus Torvalds * a unsigned char cmnd[16], so we know we have storage available 701da177e4SLinus Torvalds */ 711da177e4SLinus Torvalds for (; srb->cmd_len < 12; srb->cmd_len++) 721da177e4SLinus Torvalds srb->cmnd[srb->cmd_len] = 0; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* send the command to the transport layer */ 751da177e4SLinus Torvalds usb_stor_invoke_transport(srb, us); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us) 791da177e4SLinus Torvalds { 80f0183a33SFelipe Balbi /* 81f0183a33SFelipe Balbi * fix some commands -- this is a form of mode translation 821da177e4SLinus Torvalds * UFI devices only accept 12 byte long commands 831da177e4SLinus Torvalds * 841da177e4SLinus Torvalds * NOTE: This only works because a scsi_cmnd struct field contains 851da177e4SLinus Torvalds * a unsigned char cmnd[16], so we know we have storage available 861da177e4SLinus Torvalds */ 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* Pad the ATAPI command with zeros */ 891da177e4SLinus Torvalds for (; srb->cmd_len < 12; srb->cmd_len++) 901da177e4SLinus Torvalds srb->cmnd[srb->cmd_len] = 0; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* set command length to 12 bytes (this affects the transport layer) */ 931da177e4SLinus Torvalds srb->cmd_len = 12; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds /* XXX We should be constantly re-evaluating the need for these */ 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* determine the correct data length for these commands */ 981da177e4SLinus Torvalds switch (srb->cmnd[0]) { 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* for INQUIRY, UFI devices only ever return 36 bytes */ 1011da177e4SLinus Torvalds case INQUIRY: 1021da177e4SLinus Torvalds srb->cmnd[4] = 36; 1031da177e4SLinus Torvalds break; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* again, for MODE_SENSE_10, we get the minimum (8) */ 1061da177e4SLinus Torvalds case MODE_SENSE_10: 1071da177e4SLinus Torvalds srb->cmnd[7] = 0; 1081da177e4SLinus Torvalds srb->cmnd[8] = 8; 1091da177e4SLinus Torvalds break; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */ 1121da177e4SLinus Torvalds case REQUEST_SENSE: 1131da177e4SLinus Torvalds srb->cmnd[4] = 18; 1141da177e4SLinus Torvalds break; 1151da177e4SLinus Torvalds } /* end switch on cmnd[0] */ 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds /* send the command to the transport layer */ 1181da177e4SLinus Torvalds usb_stor_invoke_transport(srb, us); 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb, 1221da177e4SLinus Torvalds struct us_data *us) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds /* send the command to the transport layer */ 1251da177e4SLinus Torvalds usb_stor_invoke_transport(srb, us); 1261da177e4SLinus Torvalds } 127e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /*********************************************************************** 1301da177e4SLinus Torvalds * Scatter-gather transfer buffer access routines 1311da177e4SLinus Torvalds ***********************************************************************/ 1321da177e4SLinus Torvalds 133f0183a33SFelipe Balbi /* 134f0183a33SFelipe Balbi * Copy a buffer of length buflen to/from the srb's transfer buffer. 135dd829d23SBoaz Harrosh * Update the **sgptr and *offset variables so that the next copy will 1367084191dSAlan Stern * pick up from where this one left off. 1377084191dSAlan Stern */ 1381da177e4SLinus Torvalds unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, 1391f6f31a0SJens Axboe unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr, 1401da177e4SLinus Torvalds unsigned int *offset, enum xfer_buf_dir dir) 1411da177e4SLinus Torvalds { 142e5fc70d5SMing Lei unsigned int cnt = 0; 1437084191dSAlan Stern struct scatterlist *sg = *sgptr; 144e5fc70d5SMing Lei struct sg_mapping_iter miter; 145e5fc70d5SMing Lei unsigned int nents = scsi_sg_count(srb); 1461da177e4SLinus Torvalds 147e5fc70d5SMing Lei if (sg) 148e5fc70d5SMing Lei nents = sg_nents(sg); 149e5fc70d5SMing Lei else 150dd829d23SBoaz Harrosh sg = scsi_sglist(srb); 1511da177e4SLinus Torvalds 152e5fc70d5SMing Lei sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ? 153e5fc70d5SMing Lei SG_MITER_FROM_SG: SG_MITER_TO_SG); 1541da177e4SLinus Torvalds 155e5fc70d5SMing Lei if (!sg_miter_skip(&miter, *offset)) 156e5fc70d5SMing Lei return cnt; 1571da177e4SLinus Torvalds 158e5fc70d5SMing Lei while (sg_miter_next(&miter) && cnt < buflen) { 15940fcd88bSMing Lei unsigned int len = min_t(unsigned int, miter.length, 16040fcd88bSMing Lei buflen - cnt); 1611da177e4SLinus Torvalds 162e5fc70d5SMing Lei if (dir == FROM_XFER_BUF) 163e5fc70d5SMing Lei memcpy(buffer + cnt, miter.addr, len); 1641da177e4SLinus Torvalds else 165e5fc70d5SMing Lei memcpy(miter.addr, buffer + cnt, len); 1661da177e4SLinus Torvalds 167e5fc70d5SMing Lei if (*offset + len < miter.piter.sg->length) { 168e5fc70d5SMing Lei *offset += len; 169e5fc70d5SMing Lei *sgptr = miter.piter.sg; 170e5fc70d5SMing Lei } else { 171e5fc70d5SMing Lei *offset = 0; 172e5fc70d5SMing Lei *sgptr = sg_next(miter.piter.sg); 1731da177e4SLinus Torvalds } 174e5fc70d5SMing Lei cnt += len; 1751da177e4SLinus Torvalds } 176e5fc70d5SMing Lei sg_miter_stop(&miter); 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds return cnt; 1791da177e4SLinus Torvalds } 180e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf); 1811da177e4SLinus Torvalds 182f0183a33SFelipe Balbi /* 183f0183a33SFelipe Balbi * Store the contents of buffer into srb's transfer buffer and set the 1847084191dSAlan Stern * SCSI residue. 1857084191dSAlan Stern */ 1861da177e4SLinus Torvalds void usb_stor_set_xfer_buf(unsigned char *buffer, 1871da177e4SLinus Torvalds unsigned int buflen, struct scsi_cmnd *srb) 1881da177e4SLinus Torvalds { 1891f6f31a0SJens Axboe unsigned int offset = 0; 1901f6f31a0SJens Axboe struct scatterlist *sg = NULL; 1911da177e4SLinus Torvalds 1926d512a80SAlan Stern buflen = min(buflen, scsi_bufflen(srb)); 1937084191dSAlan Stern buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset, 1941da177e4SLinus Torvalds TO_XFER_BUF); 195dd829d23SBoaz Harrosh if (buflen < scsi_bufflen(srb)) 196dd829d23SBoaz Harrosh scsi_set_resid(srb, scsi_bufflen(srb) - buflen); 1971da177e4SLinus Torvalds } 198e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf); 199