xref: /openbmc/linux/drivers/usb/storage/protocol.c (revision 552c69b36ebd966186573b9c7a286b390935cce1)
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 
301da177e4SLinus Torvalds #include <linux/highmem.h>
31f940fcd8SPaul Gortmaker #include <linux/export.h>
321da177e4SLinus Torvalds #include <scsi/scsi.h>
331da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include "usb.h"
361da177e4SLinus Torvalds #include "protocol.h"
371da177e4SLinus Torvalds #include "debug.h"
381da177e4SLinus Torvalds #include "scsiglue.h"
391da177e4SLinus Torvalds #include "transport.h"
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /***********************************************************************
421da177e4SLinus Torvalds  * Protocol routines
431da177e4SLinus Torvalds  ***********************************************************************/
441da177e4SLinus Torvalds 
usb_stor_pad12_command(struct scsi_cmnd * srb,struct us_data * us)453dae5345SAlan Stern void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
461da177e4SLinus Torvalds {
472f640bf4SAlan Stern 	/*
482f640bf4SAlan Stern 	 * Pad the SCSI command with zeros out to 12 bytes.  If the
492f640bf4SAlan Stern 	 * command already is 12 bytes or longer, leave it alone.
501da177e4SLinus Torvalds 	 *
511da177e4SLinus Torvalds 	 * NOTE: This only works because a scsi_cmnd struct field contains
521da177e4SLinus Torvalds 	 * a unsigned char cmnd[16], so we know we have storage available
531da177e4SLinus Torvalds 	 */
541da177e4SLinus Torvalds 	for (; srb->cmd_len < 12; srb->cmd_len++)
551da177e4SLinus Torvalds 		srb->cmnd[srb->cmd_len] = 0;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	/* send the command to the transport layer */
581da177e4SLinus Torvalds 	usb_stor_invoke_transport(srb, us);
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
usb_stor_ufi_command(struct scsi_cmnd * srb,struct us_data * us)611da177e4SLinus Torvalds void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
621da177e4SLinus Torvalds {
63f0183a33SFelipe Balbi 	/*
64f0183a33SFelipe Balbi 	 * fix some commands -- this is a form of mode translation
651da177e4SLinus Torvalds 	 * UFI devices only accept 12 byte long commands
661da177e4SLinus Torvalds 	 *
671da177e4SLinus Torvalds 	 * NOTE: This only works because a scsi_cmnd struct field contains
681da177e4SLinus Torvalds 	 * a unsigned char cmnd[16], so we know we have storage available
691da177e4SLinus Torvalds 	 */
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	/* Pad the ATAPI command with zeros */
721da177e4SLinus Torvalds 	for (; srb->cmd_len < 12; srb->cmd_len++)
731da177e4SLinus Torvalds 		srb->cmnd[srb->cmd_len] = 0;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	/* set command length to 12 bytes (this affects the transport layer) */
761da177e4SLinus Torvalds 	srb->cmd_len = 12;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	/* XXX We should be constantly re-evaluating the need for these */
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	/* determine the correct data length for these commands */
811da177e4SLinus Torvalds 	switch (srb->cmnd[0]) {
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 		/* for INQUIRY, UFI devices only ever return 36 bytes */
841da177e4SLinus Torvalds 	case INQUIRY:
851da177e4SLinus Torvalds 		srb->cmnd[4] = 36;
861da177e4SLinus Torvalds 		break;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 		/* again, for MODE_SENSE_10, we get the minimum (8) */
891da177e4SLinus Torvalds 	case MODE_SENSE_10:
901da177e4SLinus Torvalds 		srb->cmnd[7] = 0;
911da177e4SLinus Torvalds 		srb->cmnd[8] = 8;
921da177e4SLinus Torvalds 		break;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 		/* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
951da177e4SLinus Torvalds 	case REQUEST_SENSE:
961da177e4SLinus Torvalds 		srb->cmnd[4] = 18;
971da177e4SLinus Torvalds 		break;
981da177e4SLinus Torvalds 	} /* end switch on cmnd[0] */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	/* send the command to the transport layer */
1011da177e4SLinus Torvalds 	usb_stor_invoke_transport(srb, us);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
usb_stor_transparent_scsi_command(struct scsi_cmnd * srb,struct us_data * us)1041da177e4SLinus Torvalds void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
1051da177e4SLinus Torvalds 				       struct us_data *us)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	/* send the command to the transport layer */
1081da177e4SLinus Torvalds 	usb_stor_invoke_transport(srb, us);
1091da177e4SLinus Torvalds }
110e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /***********************************************************************
1131da177e4SLinus Torvalds  * Scatter-gather transfer buffer access routines
1141da177e4SLinus Torvalds  ***********************************************************************/
1151da177e4SLinus Torvalds 
116f0183a33SFelipe Balbi /*
117f0183a33SFelipe Balbi  * Copy a buffer of length buflen to/from the srb's transfer buffer.
118dd829d23SBoaz Harrosh  * Update the **sgptr and *offset variables so that the next copy will
1197084191dSAlan Stern  * pick up from where this one left off.
1207084191dSAlan Stern  */
usb_stor_access_xfer_buf(unsigned char * buffer,unsigned int buflen,struct scsi_cmnd * srb,struct scatterlist ** sgptr,unsigned int * offset,enum xfer_buf_dir dir)1211da177e4SLinus Torvalds unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
1221f6f31a0SJens Axboe 	unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
1231da177e4SLinus Torvalds 	unsigned int *offset, enum xfer_buf_dir dir)
1241da177e4SLinus Torvalds {
125e5fc70d5SMing Lei 	unsigned int cnt = 0;
1267084191dSAlan Stern 	struct scatterlist *sg = *sgptr;
127e5fc70d5SMing Lei 	struct sg_mapping_iter miter;
128e5fc70d5SMing Lei 	unsigned int nents = scsi_sg_count(srb);
1291da177e4SLinus Torvalds 
130e5fc70d5SMing Lei 	if (sg)
131e5fc70d5SMing Lei 		nents = sg_nents(sg);
132e5fc70d5SMing Lei 	else
133dd829d23SBoaz Harrosh 		sg = scsi_sglist(srb);
1341da177e4SLinus Torvalds 
135e5fc70d5SMing Lei 	sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
136e5fc70d5SMing Lei 		SG_MITER_FROM_SG: SG_MITER_TO_SG);
1371da177e4SLinus Torvalds 
138e5fc70d5SMing Lei 	if (!sg_miter_skip(&miter, *offset))
139e5fc70d5SMing Lei 		return cnt;
1401da177e4SLinus Torvalds 
141e5fc70d5SMing Lei 	while (sg_miter_next(&miter) && cnt < buflen) {
14240fcd88bSMing Lei 		unsigned int len = min_t(unsigned int, miter.length,
14340fcd88bSMing Lei 				buflen - cnt);
1441da177e4SLinus Torvalds 
145e5fc70d5SMing Lei 		if (dir == FROM_XFER_BUF)
146e5fc70d5SMing Lei 			memcpy(buffer + cnt, miter.addr, len);
1471da177e4SLinus Torvalds 		else
148e5fc70d5SMing Lei 			memcpy(miter.addr, buffer + cnt, len);
1491da177e4SLinus Torvalds 
150e5fc70d5SMing Lei 		if (*offset + len < miter.piter.sg->length) {
151e5fc70d5SMing Lei 			*offset += len;
152e5fc70d5SMing Lei 			*sgptr = miter.piter.sg;
153e5fc70d5SMing Lei 		} else {
154e5fc70d5SMing Lei 			*offset = 0;
155e5fc70d5SMing Lei 			*sgptr = sg_next(miter.piter.sg);
1561da177e4SLinus Torvalds 		}
157e5fc70d5SMing Lei 		cnt += len;
1581da177e4SLinus Torvalds 	}
159e5fc70d5SMing Lei 	sg_miter_stop(&miter);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	return cnt;
1621da177e4SLinus Torvalds }
163e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
1641da177e4SLinus Torvalds 
165f0183a33SFelipe Balbi /*
166f0183a33SFelipe Balbi  * Store the contents of buffer into srb's transfer buffer and set the
1677084191dSAlan Stern  * SCSI residue.
1687084191dSAlan Stern  */
usb_stor_set_xfer_buf(unsigned char * buffer,unsigned int buflen,struct scsi_cmnd * srb)1691da177e4SLinus Torvalds void usb_stor_set_xfer_buf(unsigned char *buffer,
1701da177e4SLinus Torvalds 	unsigned int buflen, struct scsi_cmnd *srb)
1711da177e4SLinus Torvalds {
1721f6f31a0SJens Axboe 	unsigned int offset = 0;
1731f6f31a0SJens Axboe 	struct scatterlist *sg = NULL;
1741da177e4SLinus Torvalds 
1756d512a80SAlan Stern 	buflen = min(buflen, scsi_bufflen(srb));
1767084191dSAlan Stern 	buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
1771da177e4SLinus Torvalds 			TO_XFER_BUF);
178dd829d23SBoaz Harrosh 	if (buflen < scsi_bufflen(srb))
179dd829d23SBoaz Harrosh 		scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
1801da177e4SLinus Torvalds }
181e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);
182