162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Driver for USB Mass Storage compliant devices 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Current development and maintenance by: 662306a36Sopenharmony_ci * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * Developed with the assistance of: 962306a36Sopenharmony_ci * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) 1062306a36Sopenharmony_ci * (c) 2002 Alan Stern (stern@rowland.org) 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * Initial work by: 1362306a36Sopenharmony_ci * (c) 1999 Michael Gee (michael@linuxspecific.com) 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * This driver is based on the 'USB Mass Storage Class' document. This 1662306a36Sopenharmony_ci * describes in detail the protocol used to communicate with such 1762306a36Sopenharmony_ci * devices. Clearly, the designers had SCSI and ATAPI commands in 1862306a36Sopenharmony_ci * mind when they created this document. The commands are all very 1962306a36Sopenharmony_ci * similar to commands in the SCSI-II and ATAPI specifications. 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * It is important to note that in a number of cases this class 2262306a36Sopenharmony_ci * exhibits class-specific exemptions from the USB specification. 2362306a36Sopenharmony_ci * Notably the usage of NAK, STALL and ACK differs from the norm, in 2462306a36Sopenharmony_ci * that they are used to communicate wait, failed and OK on commands. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * Also, for certain devices, the interrupt endpoint is used to convey 2762306a36Sopenharmony_ci * status of a command. 2862306a36Sopenharmony_ci */ 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci#include <linux/highmem.h> 3162306a36Sopenharmony_ci#include <linux/export.h> 3262306a36Sopenharmony_ci#include <scsi/scsi.h> 3362306a36Sopenharmony_ci#include <scsi/scsi_cmnd.h> 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci#include "usb.h" 3662306a36Sopenharmony_ci#include "protocol.h" 3762306a36Sopenharmony_ci#include "debug.h" 3862306a36Sopenharmony_ci#include "scsiglue.h" 3962306a36Sopenharmony_ci#include "transport.h" 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci/*********************************************************************** 4262306a36Sopenharmony_ci * Protocol routines 4362306a36Sopenharmony_ci ***********************************************************************/ 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_civoid usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us) 4662306a36Sopenharmony_ci{ 4762306a36Sopenharmony_ci /* 4862306a36Sopenharmony_ci * Pad the SCSI command with zeros out to 12 bytes. If the 4962306a36Sopenharmony_ci * command already is 12 bytes or longer, leave it alone. 5062306a36Sopenharmony_ci * 5162306a36Sopenharmony_ci * NOTE: This only works because a scsi_cmnd struct field contains 5262306a36Sopenharmony_ci * a unsigned char cmnd[16], so we know we have storage available 5362306a36Sopenharmony_ci */ 5462306a36Sopenharmony_ci for (; srb->cmd_len < 12; srb->cmd_len++) 5562306a36Sopenharmony_ci srb->cmnd[srb->cmd_len] = 0; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci /* send the command to the transport layer */ 5862306a36Sopenharmony_ci usb_stor_invoke_transport(srb, us); 5962306a36Sopenharmony_ci} 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_civoid usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us) 6262306a36Sopenharmony_ci{ 6362306a36Sopenharmony_ci /* 6462306a36Sopenharmony_ci * fix some commands -- this is a form of mode translation 6562306a36Sopenharmony_ci * UFI devices only accept 12 byte long commands 6662306a36Sopenharmony_ci * 6762306a36Sopenharmony_ci * NOTE: This only works because a scsi_cmnd struct field contains 6862306a36Sopenharmony_ci * a unsigned char cmnd[16], so we know we have storage available 6962306a36Sopenharmony_ci */ 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci /* Pad the ATAPI command with zeros */ 7262306a36Sopenharmony_ci for (; srb->cmd_len < 12; srb->cmd_len++) 7362306a36Sopenharmony_ci srb->cmnd[srb->cmd_len] = 0; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci /* set command length to 12 bytes (this affects the transport layer) */ 7662306a36Sopenharmony_ci srb->cmd_len = 12; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci /* XXX We should be constantly re-evaluating the need for these */ 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci /* determine the correct data length for these commands */ 8162306a36Sopenharmony_ci switch (srb->cmnd[0]) { 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci /* for INQUIRY, UFI devices only ever return 36 bytes */ 8462306a36Sopenharmony_ci case INQUIRY: 8562306a36Sopenharmony_ci srb->cmnd[4] = 36; 8662306a36Sopenharmony_ci break; 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci /* again, for MODE_SENSE_10, we get the minimum (8) */ 8962306a36Sopenharmony_ci case MODE_SENSE_10: 9062306a36Sopenharmony_ci srb->cmnd[7] = 0; 9162306a36Sopenharmony_ci srb->cmnd[8] = 8; 9262306a36Sopenharmony_ci break; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */ 9562306a36Sopenharmony_ci case REQUEST_SENSE: 9662306a36Sopenharmony_ci srb->cmnd[4] = 18; 9762306a36Sopenharmony_ci break; 9862306a36Sopenharmony_ci } /* end switch on cmnd[0] */ 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci /* send the command to the transport layer */ 10162306a36Sopenharmony_ci usb_stor_invoke_transport(srb, us); 10262306a36Sopenharmony_ci} 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_civoid usb_stor_transparent_scsi_command(struct scsi_cmnd *srb, 10562306a36Sopenharmony_ci struct us_data *us) 10662306a36Sopenharmony_ci{ 10762306a36Sopenharmony_ci /* send the command to the transport layer */ 10862306a36Sopenharmony_ci usb_stor_invoke_transport(srb, us); 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command); 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci/*********************************************************************** 11362306a36Sopenharmony_ci * Scatter-gather transfer buffer access routines 11462306a36Sopenharmony_ci ***********************************************************************/ 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci/* 11762306a36Sopenharmony_ci * Copy a buffer of length buflen to/from the srb's transfer buffer. 11862306a36Sopenharmony_ci * Update the **sgptr and *offset variables so that the next copy will 11962306a36Sopenharmony_ci * pick up from where this one left off. 12062306a36Sopenharmony_ci */ 12162306a36Sopenharmony_ciunsigned int usb_stor_access_xfer_buf(unsigned char *buffer, 12262306a36Sopenharmony_ci unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr, 12362306a36Sopenharmony_ci unsigned int *offset, enum xfer_buf_dir dir) 12462306a36Sopenharmony_ci{ 12562306a36Sopenharmony_ci unsigned int cnt = 0; 12662306a36Sopenharmony_ci struct scatterlist *sg = *sgptr; 12762306a36Sopenharmony_ci struct sg_mapping_iter miter; 12862306a36Sopenharmony_ci unsigned int nents = scsi_sg_count(srb); 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci if (sg) 13162306a36Sopenharmony_ci nents = sg_nents(sg); 13262306a36Sopenharmony_ci else 13362306a36Sopenharmony_ci sg = scsi_sglist(srb); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ? 13662306a36Sopenharmony_ci SG_MITER_FROM_SG: SG_MITER_TO_SG); 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci if (!sg_miter_skip(&miter, *offset)) 13962306a36Sopenharmony_ci return cnt; 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci while (sg_miter_next(&miter) && cnt < buflen) { 14262306a36Sopenharmony_ci unsigned int len = min_t(unsigned int, miter.length, 14362306a36Sopenharmony_ci buflen - cnt); 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci if (dir == FROM_XFER_BUF) 14662306a36Sopenharmony_ci memcpy(buffer + cnt, miter.addr, len); 14762306a36Sopenharmony_ci else 14862306a36Sopenharmony_ci memcpy(miter.addr, buffer + cnt, len); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci if (*offset + len < miter.piter.sg->length) { 15162306a36Sopenharmony_ci *offset += len; 15262306a36Sopenharmony_ci *sgptr = miter.piter.sg; 15362306a36Sopenharmony_ci } else { 15462306a36Sopenharmony_ci *offset = 0; 15562306a36Sopenharmony_ci *sgptr = sg_next(miter.piter.sg); 15662306a36Sopenharmony_ci } 15762306a36Sopenharmony_ci cnt += len; 15862306a36Sopenharmony_ci } 15962306a36Sopenharmony_ci sg_miter_stop(&miter); 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci return cnt; 16262306a36Sopenharmony_ci} 16362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf); 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci/* 16662306a36Sopenharmony_ci * Store the contents of buffer into srb's transfer buffer and set the 16762306a36Sopenharmony_ci * SCSI residue. 16862306a36Sopenharmony_ci */ 16962306a36Sopenharmony_civoid usb_stor_set_xfer_buf(unsigned char *buffer, 17062306a36Sopenharmony_ci unsigned int buflen, struct scsi_cmnd *srb) 17162306a36Sopenharmony_ci{ 17262306a36Sopenharmony_ci unsigned int offset = 0; 17362306a36Sopenharmony_ci struct scatterlist *sg = NULL; 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci buflen = min(buflen, scsi_bufflen(srb)); 17662306a36Sopenharmony_ci buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset, 17762306a36Sopenharmony_ci TO_XFER_BUF); 17862306a36Sopenharmony_ci if (buflen < scsi_bufflen(srb)) 17962306a36Sopenharmony_ci scsi_set_resid(srb, scsi_bufflen(srb) - buflen); 18062306a36Sopenharmony_ci} 18162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf); 182