Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Driver for USB Mass Storage compliant devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Current development and maintenance by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Developed with the assistance of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *   (c) 2002 Alan Stern (stern@rowland.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Initial work by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *   (c) 1999 Michael Gee (michael@linuxspecific.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * This driver is based on the 'USB Mass Storage Class' document. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * describes in detail the protocol used to communicate with such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * devices.  Clearly, the designers had SCSI and ATAPI commands in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * mind when they created this document.  The commands are all very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * similar to commands in the SCSI-II and ATAPI specifications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * It is important to note that in a number of cases this class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * exhibits class-specific exemptions from the USB specification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * Notably the usage of NAK, STALL and ACK differs from the norm, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * that they are used to communicate wait, failed and OK on commands.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * Also, for certain devices, the interrupt endpoint is used to convey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * status of a command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <scsi/scsi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include "usb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include "protocol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include "scsiglue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include "transport.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /***********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * Protocol routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  ***********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	 * Pad the SCSI command with zeros out to 12 bytes.  If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	 * command already is 12 bytes or longer, leave it alone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 * NOTE: This only works because a scsi_cmnd struct field contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 * a unsigned char cmnd[16], so we know we have storage available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	for (; srb->cmd_len < 12; srb->cmd_len++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		srb->cmnd[srb->cmd_len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/* send the command to the transport layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	usb_stor_invoke_transport(srb, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	 * fix some commands -- this is a form of mode translation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 * UFI devices only accept 12 byte long commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	 * NOTE: This only works because a scsi_cmnd struct field contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	 * a unsigned char cmnd[16], so we know we have storage available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	/* Pad the ATAPI command with zeros */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	for (; srb->cmd_len < 12; srb->cmd_len++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		srb->cmnd[srb->cmd_len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* set command length to 12 bytes (this affects the transport layer) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	srb->cmd_len = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	/* XXX We should be constantly re-evaluating the need for these */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/* determine the correct data length for these commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	switch (srb->cmnd[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		/* for INQUIRY, UFI devices only ever return 36 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	case INQUIRY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		srb->cmnd[4] = 36;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		/* again, for MODE_SENSE_10, we get the minimum (8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	case MODE_SENSE_10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		srb->cmnd[7] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		srb->cmnd[8] = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		/* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	case REQUEST_SENSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		srb->cmnd[4] = 18;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	} /* end switch on cmnd[0] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	/* send the command to the transport layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	usb_stor_invoke_transport(srb, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				       struct us_data *us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	/* send the command to the transport layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	usb_stor_invoke_transport(srb, us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /***********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * Scatter-gather transfer buffer access routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  ***********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * Copy a buffer of length buflen to/from the srb's transfer buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * Update the **sgptr and *offset variables so that the next copy will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * pick up from where this one left off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	unsigned int *offset, enum xfer_buf_dir dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	unsigned int cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct scatterlist *sg = *sgptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct sg_mapping_iter miter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unsigned int nents = scsi_sg_count(srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		nents = sg_nents(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		sg = scsi_sglist(srb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		SG_MITER_FROM_SG: SG_MITER_TO_SG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (!sg_miter_skip(&miter, *offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	while (sg_miter_next(&miter) && cnt < buflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		unsigned int len = min_t(unsigned int, miter.length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				buflen - cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (dir == FROM_XFER_BUF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			memcpy(buffer + cnt, miter.addr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			memcpy(miter.addr, buffer + cnt, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		if (*offset + len < miter.piter.sg->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			*offset += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			*sgptr = miter.piter.sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			*offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			*sgptr = sg_next(miter.piter.sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		cnt += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	sg_miter_stop(&miter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * Store the contents of buffer into srb's transfer buffer and set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * SCSI residue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) void usb_stor_set_xfer_buf(unsigned char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	unsigned int buflen, struct scsi_cmnd *srb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	unsigned int offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct scatterlist *sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	buflen = min(buflen, scsi_bufflen(srb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			TO_XFER_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (buflen < scsi_bufflen(srb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);