Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Driver for USB Mass Storage compliant devices
  4 *
  5 * Current development and maintenance by:
  6 *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  7 *
  8 * Developed with the assistance of:
  9 *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
 10 *   (c) 2002 Alan Stern (stern@rowland.org)
 11 *
 12 * Initial work by:
 13 *   (c) 1999 Michael Gee (michael@linuxspecific.com)
 14 *
 15 * This driver is based on the 'USB Mass Storage Class' document. This
 16 * describes in detail the protocol used to communicate with such
 17 * devices.  Clearly, the designers had SCSI and ATAPI commands in
 18 * mind when they created this document.  The commands are all very
 19 * similar to commands in the SCSI-II and ATAPI specifications.
 20 *
 21 * It is important to note that in a number of cases this class
 22 * exhibits class-specific exemptions from the USB specification.
 23 * Notably the usage of NAK, STALL and ACK differs from the norm, in
 24 * that they are used to communicate wait, failed and OK on commands.
 25 *
 26 * Also, for certain devices, the interrupt endpoint is used to convey
 27 * status of a command.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 28 */
 29
 30#include <linux/highmem.h>
 31#include <linux/export.h>
 32#include <scsi/scsi.h>
 33#include <scsi/scsi_cmnd.h>
 34
 35#include "usb.h"
 36#include "protocol.h"
 37#include "debug.h"
 38#include "scsiglue.h"
 39#include "transport.h"
 40
 41/***********************************************************************
 42 * Protocol routines
 43 ***********************************************************************/
 44
 45void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
 46{
 47	/*
 48	 * Pad the SCSI command with zeros out to 12 bytes.  If the
 49	 * command already is 12 bytes or longer, leave it alone.
 50	 *
 51	 * NOTE: This only works because a scsi_cmnd struct field contains
 52	 * a unsigned char cmnd[16], so we know we have storage available
 53	 */
 54	for (; srb->cmd_len < 12; srb->cmd_len++)
 55		srb->cmnd[srb->cmd_len] = 0;
 56
 
 
 
 57	/* send the command to the transport layer */
 58	usb_stor_invoke_transport(srb, us);
 59}
 60
 61void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
 62{
 63	/*
 64	 * fix some commands -- this is a form of mode translation
 65	 * UFI devices only accept 12 byte long commands
 66	 *
 67	 * NOTE: This only works because a scsi_cmnd struct field contains
 68	 * a unsigned char cmnd[16], so we know we have storage available
 69	 */
 70
 71	/* Pad the ATAPI command with zeros */
 72	for (; srb->cmd_len < 12; srb->cmd_len++)
 73		srb->cmnd[srb->cmd_len] = 0;
 74
 75	/* set command length to 12 bytes (this affects the transport layer) */
 76	srb->cmd_len = 12;
 77
 78	/* XXX We should be constantly re-evaluating the need for these */
 79
 80	/* determine the correct data length for these commands */
 81	switch (srb->cmnd[0]) {
 82
 83		/* for INQUIRY, UFI devices only ever return 36 bytes */
 84	case INQUIRY:
 85		srb->cmnd[4] = 36;
 86		break;
 87
 88		/* again, for MODE_SENSE_10, we get the minimum (8) */
 89	case MODE_SENSE_10:
 90		srb->cmnd[7] = 0;
 91		srb->cmnd[8] = 8;
 92		break;
 93
 94		/* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
 95	case REQUEST_SENSE:
 96		srb->cmnd[4] = 18;
 97		break;
 98	} /* end switch on cmnd[0] */
 99
100	/* send the command to the transport layer */
101	usb_stor_invoke_transport(srb, us);
102}
103
104void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
105				       struct us_data *us)
106{
107	/* send the command to the transport layer */
108	usb_stor_invoke_transport(srb, us);
109}
110EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
111
112/***********************************************************************
113 * Scatter-gather transfer buffer access routines
114 ***********************************************************************/
115
116/*
117 * Copy a buffer of length buflen to/from the srb's transfer buffer.
118 * Update the **sgptr and *offset variables so that the next copy will
119 * pick up from where this one left off.
120 */
121unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
122	unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
123	unsigned int *offset, enum xfer_buf_dir dir)
124{
125	unsigned int cnt = 0;
126	struct scatterlist *sg = *sgptr;
127	struct sg_mapping_iter miter;
128	unsigned int nents = scsi_sg_count(srb);
129
130	if (sg)
131		nents = sg_nents(sg);
132	else
133		sg = scsi_sglist(srb);
134
135	sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
136		SG_MITER_FROM_SG: SG_MITER_TO_SG);
 
 
 
 
 
 
137
138	if (!sg_miter_skip(&miter, *offset))
139		return cnt;
140
141	while (sg_miter_next(&miter) && cnt < buflen) {
142		unsigned int len = min_t(unsigned int, miter.length,
143				buflen - cnt);
144
145		if (dir == FROM_XFER_BUF)
146			memcpy(buffer + cnt, miter.addr, len);
147		else
148			memcpy(miter.addr, buffer + cnt, len);
149
150		if (*offset + len < miter.piter.sg->length) {
151			*offset += len;
152			*sgptr = miter.piter.sg;
 
 
 
 
 
153		} else {
 
 
154			*offset = 0;
155			*sgptr = sg_next(miter.piter.sg);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156		}
157		cnt += len;
158	}
159	sg_miter_stop(&miter);
160
 
161	return cnt;
162}
163EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
164
165/*
166 * Store the contents of buffer into srb's transfer buffer and set the
167 * SCSI residue.
168 */
169void usb_stor_set_xfer_buf(unsigned char *buffer,
170	unsigned int buflen, struct scsi_cmnd *srb)
171{
172	unsigned int offset = 0;
173	struct scatterlist *sg = NULL;
174
175	buflen = min(buflen, scsi_bufflen(srb));
176	buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
177			TO_XFER_BUF);
178	if (buflen < scsi_bufflen(srb))
179		scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
180}
181EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);
v3.1
  1/* Driver for USB Mass Storage compliant devices
 
 
  2 *
  3 * Current development and maintenance by:
  4 *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  5 *
  6 * Developed with the assistance of:
  7 *   (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
  8 *   (c) 2002 Alan Stern (stern@rowland.org)
  9 *
 10 * Initial work by:
 11 *   (c) 1999 Michael Gee (michael@linuxspecific.com)
 12 *
 13 * This driver is based on the 'USB Mass Storage Class' document. This
 14 * describes in detail the protocol used to communicate with such
 15 * devices.  Clearly, the designers had SCSI and ATAPI commands in
 16 * mind when they created this document.  The commands are all very
 17 * similar to commands in the SCSI-II and ATAPI specifications.
 18 *
 19 * It is important to note that in a number of cases this class
 20 * exhibits class-specific exemptions from the USB specification.
 21 * Notably the usage of NAK, STALL and ACK differs from the norm, in
 22 * that they are used to communicate wait, failed and OK on commands.
 23 *
 24 * Also, for certain devices, the interrupt endpoint is used to convey
 25 * status of a command.
 26 *
 27 * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
 28 * information about this driver.
 29 *
 30 * This program is free software; you can redistribute it and/or modify it
 31 * under the terms of the GNU General Public License as published by the
 32 * Free Software Foundation; either version 2, or (at your option) any
 33 * later version.
 34 *
 35 * This program is distributed in the hope that it will be useful, but
 36 * WITHOUT ANY WARRANTY; without even the implied warranty of
 37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 38 * General Public License for more details.
 39 *
 40 * You should have received a copy of the GNU General Public License along
 41 * with this program; if not, write to the Free Software Foundation, Inc.,
 42 * 675 Mass Ave, Cambridge, MA 02139, USA.
 43 */
 44
 45#include <linux/highmem.h>
 
 46#include <scsi/scsi.h>
 47#include <scsi/scsi_cmnd.h>
 48
 49#include "usb.h"
 50#include "protocol.h"
 51#include "debug.h"
 52#include "scsiglue.h"
 53#include "transport.h"
 54
 55/***********************************************************************
 56 * Protocol routines
 57 ***********************************************************************/
 58
 59void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
 60{
 61	/* Pad the SCSI command with zeros out to 12 bytes
 
 
 62	 *
 63	 * NOTE: This only works because a scsi_cmnd struct field contains
 64	 * a unsigned char cmnd[16], so we know we have storage available
 65	 */
 66	for (; srb->cmd_len<12; srb->cmd_len++)
 67		srb->cmnd[srb->cmd_len] = 0;
 68
 69	/* set command length to 12 bytes */
 70	srb->cmd_len = 12;
 71
 72	/* send the command to the transport layer */
 73	usb_stor_invoke_transport(srb, us);
 74}
 75
 76void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
 77{
 78	/* fix some commands -- this is a form of mode translation
 79	 * UFI devices only accept 12 byte long commands 
 
 80	 *
 81	 * NOTE: This only works because a scsi_cmnd struct field contains
 82	 * a unsigned char cmnd[16], so we know we have storage available
 83	 */
 84
 85	/* Pad the ATAPI command with zeros */
 86	for (; srb->cmd_len<12; srb->cmd_len++)
 87		srb->cmnd[srb->cmd_len] = 0;
 88
 89	/* set command length to 12 bytes (this affects the transport layer) */
 90	srb->cmd_len = 12;
 91
 92	/* XXX We should be constantly re-evaluating the need for these */
 93
 94	/* determine the correct data length for these commands */
 95	switch (srb->cmnd[0]) {
 96
 97		/* for INQUIRY, UFI devices only ever return 36 bytes */
 98	case INQUIRY:
 99		srb->cmnd[4] = 36;
100		break;
101
102		/* again, for MODE_SENSE_10, we get the minimum (8) */
103	case MODE_SENSE_10:
104		srb->cmnd[7] = 0;
105		srb->cmnd[8] = 8;
106		break;
107
108		/* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
109	case REQUEST_SENSE:
110		srb->cmnd[4] = 18;
111		break;
112	} /* end switch on cmnd[0] */
113
114	/* send the command to the transport layer */
115	usb_stor_invoke_transport(srb, us);
116}
117
118void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
119				       struct us_data *us)
120{
121	/* send the command to the transport layer */
122	usb_stor_invoke_transport(srb, us);
123}
124EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
125
126/***********************************************************************
127 * Scatter-gather transfer buffer access routines
128 ***********************************************************************/
129
130/* Copy a buffer of length buflen to/from the srb's transfer buffer.
 
131 * Update the **sgptr and *offset variables so that the next copy will
132 * pick up from where this one left off.
133 */
134unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
135	unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
136	unsigned int *offset, enum xfer_buf_dir dir)
137{
138	unsigned int cnt;
139	struct scatterlist *sg = *sgptr;
 
 
 
 
 
 
 
140
141	/* We have to go through the list one entry
142	 * at a time.  Each s-g entry contains some number of pages, and
143	 * each page has to be kmap()'ed separately.  If the page is already
144	 * in kernel-addressable memory then kmap() will return its address.
145	 * If the page is not directly accessible -- such as a user buffer
146	 * located in high memory -- then kmap() will map it to a temporary
147	 * position in the kernel's virtual address space.
148	 */
149
150	if (!sg)
151		sg = scsi_sglist(srb);
152
153	/* This loop handles a single s-g list entry, which may
154	 * include multiple pages.  Find the initial page structure
155	 * and the starting offset within the page, and update
156	 * the *offset and **sgptr values for the next loop.
157	 */
158	cnt = 0;
159	while (cnt < buflen && sg) {
160		struct page *page = sg_page(sg) +
161				((sg->offset + *offset) >> PAGE_SHIFT);
162		unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
163		unsigned int sglen = sg->length - *offset;
164
165		if (sglen > buflen - cnt) {
166
167			/* Transfer ends within this s-g entry */
168			sglen = buflen - cnt;
169			*offset += sglen;
170		} else {
171
172			/* Transfer continues to next s-g entry */
173			*offset = 0;
174			sg = sg_next(sg);
175		}
176
177		/* Transfer the data for all the pages in this
178			* s-g entry.  For each page: call kmap(), do the
179			* transfer, and call kunmap() immediately after. */
180		while (sglen > 0) {
181			unsigned int plen = min(sglen, (unsigned int)
182					PAGE_SIZE - poff);
183			unsigned char *ptr = kmap(page);
184
185			if (dir == TO_XFER_BUF)
186				memcpy(ptr + poff, buffer + cnt, plen);
187			else
188				memcpy(buffer + cnt, ptr + poff, plen);
189			kunmap(page);
190
191			/* Start at the beginning of the next page */
192			poff = 0;
193			++page;
194			cnt += plen;
195			sglen -= plen;
196		}
 
197	}
198	*sgptr = sg;
199
200	/* Return the amount actually transferred */
201	return cnt;
202}
203EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
204
205/* Store the contents of buffer into srb's transfer buffer and set the
 
206 * SCSI residue.
207 */
208void usb_stor_set_xfer_buf(unsigned char *buffer,
209	unsigned int buflen, struct scsi_cmnd *srb)
210{
211	unsigned int offset = 0;
212	struct scatterlist *sg = NULL;
213
214	buflen = min(buflen, scsi_bufflen(srb));
215	buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
216			TO_XFER_BUF);
217	if (buflen < scsi_bufflen(srb))
218		scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
219}
220EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);