18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for USB Mass Storage compliant devices
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Current development and maintenance by:
68c2ecf20Sopenharmony_ci *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Developed with the assistance of:
98c2ecf20Sopenharmony_ci *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
108c2ecf20Sopenharmony_ci *   (c) 2002 Alan Stern (stern@rowland.org)
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Initial work by:
138c2ecf20Sopenharmony_ci *   (c) 1999 Michael Gee (michael@linuxspecific.com)
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * This driver is based on the 'USB Mass Storage Class' document. This
168c2ecf20Sopenharmony_ci * describes in detail the protocol used to communicate with such
178c2ecf20Sopenharmony_ci * devices.  Clearly, the designers had SCSI and ATAPI commands in
188c2ecf20Sopenharmony_ci * mind when they created this document.  The commands are all very
198c2ecf20Sopenharmony_ci * similar to commands in the SCSI-II and ATAPI specifications.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * It is important to note that in a number of cases this class
228c2ecf20Sopenharmony_ci * exhibits class-specific exemptions from the USB specification.
238c2ecf20Sopenharmony_ci * Notably the usage of NAK, STALL and ACK differs from the norm, in
248c2ecf20Sopenharmony_ci * that they are used to communicate wait, failed and OK on commands.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * Also, for certain devices, the interrupt endpoint is used to convey
278c2ecf20Sopenharmony_ci * status of a command.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/highmem.h>
318c2ecf20Sopenharmony_ci#include <linux/export.h>
328c2ecf20Sopenharmony_ci#include <scsi/scsi.h>
338c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include "usb.h"
368c2ecf20Sopenharmony_ci#include "protocol.h"
378c2ecf20Sopenharmony_ci#include "debug.h"
388c2ecf20Sopenharmony_ci#include "scsiglue.h"
398c2ecf20Sopenharmony_ci#include "transport.h"
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/***********************************************************************
428c2ecf20Sopenharmony_ci * Protocol routines
438c2ecf20Sopenharmony_ci ***********************************************************************/
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_civoid usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	/*
488c2ecf20Sopenharmony_ci	 * Pad the SCSI command with zeros out to 12 bytes.  If the
498c2ecf20Sopenharmony_ci	 * command already is 12 bytes or longer, leave it alone.
508c2ecf20Sopenharmony_ci	 *
518c2ecf20Sopenharmony_ci	 * NOTE: This only works because a scsi_cmnd struct field contains
528c2ecf20Sopenharmony_ci	 * a unsigned char cmnd[16], so we know we have storage available
538c2ecf20Sopenharmony_ci	 */
548c2ecf20Sopenharmony_ci	for (; srb->cmd_len < 12; srb->cmd_len++)
558c2ecf20Sopenharmony_ci		srb->cmnd[srb->cmd_len] = 0;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/* send the command to the transport layer */
588c2ecf20Sopenharmony_ci	usb_stor_invoke_transport(srb, us);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_civoid usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	/*
648c2ecf20Sopenharmony_ci	 * fix some commands -- this is a form of mode translation
658c2ecf20Sopenharmony_ci	 * UFI devices only accept 12 byte long commands
668c2ecf20Sopenharmony_ci	 *
678c2ecf20Sopenharmony_ci	 * NOTE: This only works because a scsi_cmnd struct field contains
688c2ecf20Sopenharmony_ci	 * a unsigned char cmnd[16], so we know we have storage available
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* Pad the ATAPI command with zeros */
728c2ecf20Sopenharmony_ci	for (; srb->cmd_len < 12; srb->cmd_len++)
738c2ecf20Sopenharmony_ci		srb->cmnd[srb->cmd_len] = 0;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* set command length to 12 bytes (this affects the transport layer) */
768c2ecf20Sopenharmony_ci	srb->cmd_len = 12;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* XXX We should be constantly re-evaluating the need for these */
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	/* determine the correct data length for these commands */
818c2ecf20Sopenharmony_ci	switch (srb->cmnd[0]) {
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		/* for INQUIRY, UFI devices only ever return 36 bytes */
848c2ecf20Sopenharmony_ci	case INQUIRY:
858c2ecf20Sopenharmony_ci		srb->cmnd[4] = 36;
868c2ecf20Sopenharmony_ci		break;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		/* again, for MODE_SENSE_10, we get the minimum (8) */
898c2ecf20Sopenharmony_ci	case MODE_SENSE_10:
908c2ecf20Sopenharmony_ci		srb->cmnd[7] = 0;
918c2ecf20Sopenharmony_ci		srb->cmnd[8] = 8;
928c2ecf20Sopenharmony_ci		break;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci		/* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
958c2ecf20Sopenharmony_ci	case REQUEST_SENSE:
968c2ecf20Sopenharmony_ci		srb->cmnd[4] = 18;
978c2ecf20Sopenharmony_ci		break;
988c2ecf20Sopenharmony_ci	} /* end switch on cmnd[0] */
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* send the command to the transport layer */
1018c2ecf20Sopenharmony_ci	usb_stor_invoke_transport(srb, us);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_civoid usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
1058c2ecf20Sopenharmony_ci				       struct us_data *us)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	/* send the command to the transport layer */
1088c2ecf20Sopenharmony_ci	usb_stor_invoke_transport(srb, us);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/***********************************************************************
1138c2ecf20Sopenharmony_ci * Scatter-gather transfer buffer access routines
1148c2ecf20Sopenharmony_ci ***********************************************************************/
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/*
1178c2ecf20Sopenharmony_ci * Copy a buffer of length buflen to/from the srb's transfer buffer.
1188c2ecf20Sopenharmony_ci * Update the **sgptr and *offset variables so that the next copy will
1198c2ecf20Sopenharmony_ci * pick up from where this one left off.
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_ciunsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
1228c2ecf20Sopenharmony_ci	unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
1238c2ecf20Sopenharmony_ci	unsigned int *offset, enum xfer_buf_dir dir)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	unsigned int cnt = 0;
1268c2ecf20Sopenharmony_ci	struct scatterlist *sg = *sgptr;
1278c2ecf20Sopenharmony_ci	struct sg_mapping_iter miter;
1288c2ecf20Sopenharmony_ci	unsigned int nents = scsi_sg_count(srb);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (sg)
1318c2ecf20Sopenharmony_ci		nents = sg_nents(sg);
1328c2ecf20Sopenharmony_ci	else
1338c2ecf20Sopenharmony_ci		sg = scsi_sglist(srb);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
1368c2ecf20Sopenharmony_ci		SG_MITER_FROM_SG: SG_MITER_TO_SG);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (!sg_miter_skip(&miter, *offset))
1398c2ecf20Sopenharmony_ci		return cnt;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	while (sg_miter_next(&miter) && cnt < buflen) {
1428c2ecf20Sopenharmony_ci		unsigned int len = min_t(unsigned int, miter.length,
1438c2ecf20Sopenharmony_ci				buflen - cnt);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		if (dir == FROM_XFER_BUF)
1468c2ecf20Sopenharmony_ci			memcpy(buffer + cnt, miter.addr, len);
1478c2ecf20Sopenharmony_ci		else
1488c2ecf20Sopenharmony_ci			memcpy(miter.addr, buffer + cnt, len);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci		if (*offset + len < miter.piter.sg->length) {
1518c2ecf20Sopenharmony_ci			*offset += len;
1528c2ecf20Sopenharmony_ci			*sgptr = miter.piter.sg;
1538c2ecf20Sopenharmony_ci		} else {
1548c2ecf20Sopenharmony_ci			*offset = 0;
1558c2ecf20Sopenharmony_ci			*sgptr = sg_next(miter.piter.sg);
1568c2ecf20Sopenharmony_ci		}
1578c2ecf20Sopenharmony_ci		cnt += len;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci	sg_miter_stop(&miter);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return cnt;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/*
1668c2ecf20Sopenharmony_ci * Store the contents of buffer into srb's transfer buffer and set the
1678c2ecf20Sopenharmony_ci * SCSI residue.
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_civoid usb_stor_set_xfer_buf(unsigned char *buffer,
1708c2ecf20Sopenharmony_ci	unsigned int buflen, struct scsi_cmnd *srb)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	unsigned int offset = 0;
1738c2ecf20Sopenharmony_ci	struct scatterlist *sg = NULL;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	buflen = min(buflen, scsi_bufflen(srb));
1768c2ecf20Sopenharmony_ci	buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
1778c2ecf20Sopenharmony_ci			TO_XFER_BUF);
1788c2ecf20Sopenharmony_ci	if (buflen < scsi_bufflen(srb))
1798c2ecf20Sopenharmony_ci		scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);
182