2016-04-18 18:09:11 +08:00
|
|
|
/*
|
|
|
|
* Driver for USB Mass Storage compliant devices
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Current development and maintenance by:
|
|
|
|
* (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
|
|
|
|
*
|
|
|
|
* Developed with the assistance of:
|
|
|
|
* (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
|
|
|
|
* (c) 2002 Alan Stern (stern@rowland.org)
|
|
|
|
*
|
|
|
|
* Initial work by:
|
|
|
|
* (c) 1999 Michael Gee (michael@linuxspecific.com)
|
|
|
|
*
|
|
|
|
* This driver is based on the 'USB Mass Storage Class' document. This
|
|
|
|
* describes in detail the protocol used to communicate with such
|
|
|
|
* devices. Clearly, the designers had SCSI and ATAPI commands in
|
|
|
|
* mind when they created this document. The commands are all very
|
|
|
|
* similar to commands in the SCSI-II and ATAPI specifications.
|
|
|
|
*
|
|
|
|
* It is important to note that in a number of cases this class
|
|
|
|
* exhibits class-specific exemptions from the USB specification.
|
|
|
|
* Notably the usage of NAK, STALL and ACK differs from the norm, in
|
|
|
|
* that they are used to communicate wait, failed and OK on commands.
|
|
|
|
*
|
|
|
|
* Also, for certain devices, the interrupt endpoint is used to convey
|
|
|
|
* status of a command.
|
|
|
|
*
|
|
|
|
* Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
|
|
|
|
* information about this driver.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/highmem.h>
|
2011-05-27 21:56:31 +08:00
|
|
|
#include <linux/export.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <scsi/scsi.h>
|
|
|
|
#include <scsi/scsi_cmnd.h>
|
|
|
|
|
|
|
|
#include "usb.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "scsiglue.h"
|
|
|
|
#include "transport.h"
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Protocol routines
|
|
|
|
***********************************************************************/
|
|
|
|
|
2008-11-21 03:22:18 +08:00
|
|
|
void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-10-25 22:50:58 +08:00
|
|
|
/*
|
|
|
|
* Pad the SCSI command with zeros out to 12 bytes. If the
|
|
|
|
* command already is 12 bytes or longer, leave it alone.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* NOTE: This only works because a scsi_cmnd struct field contains
|
|
|
|
* a unsigned char cmnd[16], so we know we have storage available
|
|
|
|
*/
|
2012-05-30 20:35:47 +08:00
|
|
|
for (; srb->cmd_len < 12; srb->cmd_len++)
|
2005-04-17 06:20:36 +08:00
|
|
|
srb->cmnd[srb->cmd_len] = 0;
|
|
|
|
|
|
|
|
/* send the command to the transport layer */
|
|
|
|
usb_stor_invoke_transport(srb, us);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
|
|
|
|
{
|
2016-04-18 18:09:11 +08:00
|
|
|
/*
|
|
|
|
* fix some commands -- this is a form of mode translation
|
2012-05-30 20:35:47 +08:00
|
|
|
* UFI devices only accept 12 byte long commands
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* NOTE: This only works because a scsi_cmnd struct field contains
|
|
|
|
* a unsigned char cmnd[16], so we know we have storage available
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Pad the ATAPI command with zeros */
|
2012-05-30 20:35:47 +08:00
|
|
|
for (; srb->cmd_len < 12; srb->cmd_len++)
|
2005-04-17 06:20:36 +08:00
|
|
|
srb->cmnd[srb->cmd_len] = 0;
|
|
|
|
|
|
|
|
/* set command length to 12 bytes (this affects the transport layer) */
|
|
|
|
srb->cmd_len = 12;
|
|
|
|
|
|
|
|
/* XXX We should be constantly re-evaluating the need for these */
|
|
|
|
|
|
|
|
/* determine the correct data length for these commands */
|
|
|
|
switch (srb->cmnd[0]) {
|
|
|
|
|
|
|
|
/* for INQUIRY, UFI devices only ever return 36 bytes */
|
|
|
|
case INQUIRY:
|
|
|
|
srb->cmnd[4] = 36;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* again, for MODE_SENSE_10, we get the minimum (8) */
|
|
|
|
case MODE_SENSE_10:
|
|
|
|
srb->cmnd[7] = 0;
|
|
|
|
srb->cmnd[8] = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
|
|
|
|
case REQUEST_SENSE:
|
|
|
|
srb->cmnd[4] = 18;
|
|
|
|
break;
|
|
|
|
} /* end switch on cmnd[0] */
|
|
|
|
|
|
|
|
/* send the command to the transport layer */
|
|
|
|
usb_stor_invoke_transport(srb, us);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
|
|
|
|
struct us_data *us)
|
|
|
|
{
|
|
|
|
/* send the command to the transport layer */
|
|
|
|
usb_stor_invoke_transport(srb, us);
|
|
|
|
}
|
usb-storage: prepare for subdriver separation
This patch (as1206) is the first step in converting usb-storage's
subdrivers into separate modules. It makes the following large-scale
changes:
Remove a bunch of unnecessary #ifdef's from usb_usual.h.
Not truly necessary, but it does clean things up.
Move the USB device-ID table (which is duplicated between
libusual and usb-storage) into its own source file,
usual-tables.c, and arrange for this to be linked with
either libusual or usb-storage according to whether
USB_LIBUSUAL is configured.
Add to usual-tables.c a new usb_usual_ignore_device()
function to detect whether a particular device needs to be
managed by a subdriver and not by the standard handlers
in usb-storage.
Export a whole bunch of functions in usb-storage, renaming
some of them because their names don't already begin with
"usb_stor_". These functions will be needed by the new
subdriver modules.
Split usb-storage's probe routine into two functions.
The subdrivers will call the probe1 routine, then fill in
their transport and protocol settings, and then call the
probe2 routine.
Take the default cases and error checking out of
get_transport() and get_protocol(), which run during
probe1, and instead put a check for invalid transport
or protocol values into the probe2 function.
Add a new probe routine to be used for standard devices,
i.e., those that don't need a subdriver. This new routine
checks whether the device should be ignored (because it
should be handled by ub or by a subdriver), and if not,
calls the probe1 and probe2 functions.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Matthew Dharm <mdharm-usb@one-eyed-alien.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-02-13 03:47:44 +08:00
|
|
|
EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Scatter-gather transfer buffer access routines
|
|
|
|
***********************************************************************/
|
|
|
|
|
2016-04-18 18:09:11 +08:00
|
|
|
/*
|
|
|
|
* Copy a buffer of length buflen to/from the srb's transfer buffer.
|
2007-09-10 01:22:23 +08:00
|
|
|
* Update the **sgptr and *offset variables so that the next copy will
|
2008-02-21 03:15:58 +08:00
|
|
|
* pick up from where this one left off.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
|
2007-05-11 18:33:09 +08:00
|
|
|
unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned int *offset, enum xfer_buf_dir dir)
|
|
|
|
{
|
2013-11-26 12:44:13 +08:00
|
|
|
unsigned int cnt = 0;
|
2008-02-21 03:15:58 +08:00
|
|
|
struct scatterlist *sg = *sgptr;
|
2013-11-26 12:44:13 +08:00
|
|
|
struct sg_mapping_iter miter;
|
|
|
|
unsigned int nents = scsi_sg_count(srb);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-11-26 12:44:13 +08:00
|
|
|
if (sg)
|
|
|
|
nents = sg_nents(sg);
|
|
|
|
else
|
2007-09-10 01:22:23 +08:00
|
|
|
sg = scsi_sglist(srb);
|
|
|
|
|
2013-11-26 12:44:13 +08:00
|
|
|
sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
|
|
|
|
SG_MITER_FROM_SG: SG_MITER_TO_SG);
|
2007-09-10 01:22:23 +08:00
|
|
|
|
2013-11-26 12:44:13 +08:00
|
|
|
if (!sg_miter_skip(&miter, *offset))
|
|
|
|
return cnt;
|
|
|
|
|
|
|
|
while (sg_miter_next(&miter) && cnt < buflen) {
|
2013-12-10 15:01:42 +08:00
|
|
|
unsigned int len = min_t(unsigned int, miter.length,
|
|
|
|
buflen - cnt);
|
2013-11-26 12:44:13 +08:00
|
|
|
|
|
|
|
if (dir == FROM_XFER_BUF)
|
|
|
|
memcpy(buffer + cnt, miter.addr, len);
|
|
|
|
else
|
|
|
|
memcpy(miter.addr, buffer + cnt, len);
|
2007-09-10 01:22:23 +08:00
|
|
|
|
2013-11-26 12:44:13 +08:00
|
|
|
if (*offset + len < miter.piter.sg->length) {
|
|
|
|
*offset += len;
|
|
|
|
*sgptr = miter.piter.sg;
|
|
|
|
} else {
|
|
|
|
*offset = 0;
|
|
|
|
*sgptr = sg_next(miter.piter.sg);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-11-26 12:44:13 +08:00
|
|
|
cnt += len;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-11-26 12:44:13 +08:00
|
|
|
sg_miter_stop(&miter);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
usb-storage: prepare for subdriver separation
This patch (as1206) is the first step in converting usb-storage's
subdrivers into separate modules. It makes the following large-scale
changes:
Remove a bunch of unnecessary #ifdef's from usb_usual.h.
Not truly necessary, but it does clean things up.
Move the USB device-ID table (which is duplicated between
libusual and usb-storage) into its own source file,
usual-tables.c, and arrange for this to be linked with
either libusual or usb-storage according to whether
USB_LIBUSUAL is configured.
Add to usual-tables.c a new usb_usual_ignore_device()
function to detect whether a particular device needs to be
managed by a subdriver and not by the standard handlers
in usb-storage.
Export a whole bunch of functions in usb-storage, renaming
some of them because their names don't already begin with
"usb_stor_". These functions will be needed by the new
subdriver modules.
Split usb-storage's probe routine into two functions.
The subdrivers will call the probe1 routine, then fill in
their transport and protocol settings, and then call the
probe2 routine.
Take the default cases and error checking out of
get_transport() and get_protocol(), which run during
probe1, and instead put a check for invalid transport
or protocol values into the probe2 function.
Add a new probe routine to be used for standard devices,
i.e., those that don't need a subdriver. This new routine
checks whether the device should be ignored (because it
should be handled by ub or by a subdriver), and if not,
calls the probe1 and probe2 functions.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Matthew Dharm <mdharm-usb@one-eyed-alien.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-02-13 03:47:44 +08:00
|
|
|
EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-04-18 18:09:11 +08:00
|
|
|
/*
|
|
|
|
* Store the contents of buffer into srb's transfer buffer and set the
|
2008-02-21 03:15:58 +08:00
|
|
|
* SCSI residue.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
void usb_stor_set_xfer_buf(unsigned char *buffer,
|
|
|
|
unsigned int buflen, struct scsi_cmnd *srb)
|
|
|
|
{
|
2007-05-11 18:33:09 +08:00
|
|
|
unsigned int offset = 0;
|
|
|
|
struct scatterlist *sg = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-02-23 06:00:06 +08:00
|
|
|
buflen = min(buflen, scsi_bufflen(srb));
|
2008-02-21 03:15:58 +08:00
|
|
|
buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
|
2005-04-17 06:20:36 +08:00
|
|
|
TO_XFER_BUF);
|
2007-09-10 01:22:23 +08:00
|
|
|
if (buflen < scsi_bufflen(srb))
|
|
|
|
scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
usb-storage: prepare for subdriver separation
This patch (as1206) is the first step in converting usb-storage's
subdrivers into separate modules. It makes the following large-scale
changes:
Remove a bunch of unnecessary #ifdef's from usb_usual.h.
Not truly necessary, but it does clean things up.
Move the USB device-ID table (which is duplicated between
libusual and usb-storage) into its own source file,
usual-tables.c, and arrange for this to be linked with
either libusual or usb-storage according to whether
USB_LIBUSUAL is configured.
Add to usual-tables.c a new usb_usual_ignore_device()
function to detect whether a particular device needs to be
managed by a subdriver and not by the standard handlers
in usb-storage.
Export a whole bunch of functions in usb-storage, renaming
some of them because their names don't already begin with
"usb_stor_". These functions will be needed by the new
subdriver modules.
Split usb-storage's probe routine into two functions.
The subdrivers will call the probe1 routine, then fill in
their transport and protocol settings, and then call the
probe2 routine.
Take the default cases and error checking out of
get_transport() and get_protocol(), which run during
probe1, and instead put a check for invalid transport
or protocol values into the probe2 function.
Add a new probe routine to be used for standard devices,
i.e., those that don't need a subdriver. This new routine
checks whether the device should be ignored (because it
should be handled by ub or by a subdriver), and if not,
calls the probe1 and probe2 functions.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Matthew Dharm <mdharm-usb@one-eyed-alien.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2009-02-13 03:47:44 +08:00
|
|
|
EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);
|