1*f0183a33SFelipe Balbi /* 2*f0183a33SFelipe Balbi * Driver for USB Mass Storage compliant devices 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Current development and maintenance by: 51da177e4SLinus Torvalds * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net) 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Developed with the assistance of: 81da177e4SLinus Torvalds * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org) 91da177e4SLinus Torvalds * (c) 2002 Alan Stern (stern@rowland.org) 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Initial work by: 121da177e4SLinus Torvalds * (c) 1999 Michael Gee (michael@linuxspecific.com) 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * This driver is based on the 'USB Mass Storage Class' document. This 151da177e4SLinus Torvalds * describes in detail the protocol used to communicate with such 161da177e4SLinus Torvalds * devices. Clearly, the designers had SCSI and ATAPI commands in 171da177e4SLinus Torvalds * mind when they created this document. The commands are all very 181da177e4SLinus Torvalds * similar to commands in the SCSI-II and ATAPI specifications. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * It is important to note that in a number of cases this class 211da177e4SLinus Torvalds * exhibits class-specific exemptions from the USB specification. 221da177e4SLinus Torvalds * Notably the usage of NAK, STALL and ACK differs from the norm, in 231da177e4SLinus Torvalds * that they are used to communicate wait, failed and OK on commands. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * Also, for certain devices, the interrupt endpoint is used to convey 261da177e4SLinus Torvalds * status of a command. 271da177e4SLinus Torvalds * 281da177e4SLinus Torvalds * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more 291da177e4SLinus Torvalds * information about this driver. 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify it 321da177e4SLinus Torvalds * under the terms of the GNU General Public License as published by the 331da177e4SLinus Torvalds * Free Software Foundation; either version 2, or (at your option) any 341da177e4SLinus Torvalds * later version. 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, but 371da177e4SLinus Torvalds * WITHOUT ANY WARRANTY; without even the implied warranty of 381da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 391da177e4SLinus Torvalds * General Public License for more details. 401da177e4SLinus Torvalds * 411da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License along 421da177e4SLinus Torvalds * with this program; if not, write to the Free Software Foundation, Inc., 431da177e4SLinus Torvalds * 675 Mass Ave, Cambridge, MA 02139, USA. 441da177e4SLinus Torvalds */ 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include <linux/highmem.h> 47f940fcd8SPaul Gortmaker #include <linux/export.h> 481da177e4SLinus Torvalds #include <scsi/scsi.h> 491da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h> 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds #include "usb.h" 521da177e4SLinus Torvalds #include "protocol.h" 531da177e4SLinus Torvalds #include "debug.h" 541da177e4SLinus Torvalds #include "scsiglue.h" 551da177e4SLinus Torvalds #include "transport.h" 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /*********************************************************************** 581da177e4SLinus Torvalds * Protocol routines 591da177e4SLinus Torvalds ***********************************************************************/ 601da177e4SLinus Torvalds 613dae5345SAlan Stern void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us) 621da177e4SLinus Torvalds { 632f640bf4SAlan Stern /* 642f640bf4SAlan Stern * Pad the SCSI command with zeros out to 12 bytes. If the 652f640bf4SAlan Stern * command already is 12 bytes or longer, leave it alone. 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 for (; srb->cmd_len < 12; srb->cmd_len++) 711da177e4SLinus Torvalds srb->cmnd[srb->cmd_len] = 0; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* send the command to the transport layer */ 741da177e4SLinus Torvalds usb_stor_invoke_transport(srb, us); 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us) 781da177e4SLinus Torvalds { 79*f0183a33SFelipe Balbi /* 80*f0183a33SFelipe Balbi * fix some commands -- this is a form of mode translation 811da177e4SLinus Torvalds * UFI devices only accept 12 byte long commands 821da177e4SLinus Torvalds * 831da177e4SLinus Torvalds * NOTE: This only works because a scsi_cmnd struct field contains 841da177e4SLinus Torvalds * a unsigned char cmnd[16], so we know we have storage available 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* Pad the ATAPI command with zeros */ 881da177e4SLinus Torvalds for (; srb->cmd_len < 12; srb->cmd_len++) 891da177e4SLinus Torvalds srb->cmnd[srb->cmd_len] = 0; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* set command length to 12 bytes (this affects the transport layer) */ 921da177e4SLinus Torvalds srb->cmd_len = 12; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* XXX We should be constantly re-evaluating the need for these */ 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* determine the correct data length for these commands */ 971da177e4SLinus Torvalds switch (srb->cmnd[0]) { 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* for INQUIRY, UFI devices only ever return 36 bytes */ 1001da177e4SLinus Torvalds case INQUIRY: 1011da177e4SLinus Torvalds srb->cmnd[4] = 36; 1021da177e4SLinus Torvalds break; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* again, for MODE_SENSE_10, we get the minimum (8) */ 1051da177e4SLinus Torvalds case MODE_SENSE_10: 1061da177e4SLinus Torvalds srb->cmnd[7] = 0; 1071da177e4SLinus Torvalds srb->cmnd[8] = 8; 1081da177e4SLinus Torvalds break; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */ 1111da177e4SLinus Torvalds case REQUEST_SENSE: 1121da177e4SLinus Torvalds srb->cmnd[4] = 18; 1131da177e4SLinus Torvalds break; 1141da177e4SLinus Torvalds } /* end switch on cmnd[0] */ 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds /* send the command to the transport layer */ 1171da177e4SLinus Torvalds usb_stor_invoke_transport(srb, us); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb, 1211da177e4SLinus Torvalds struct us_data *us) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds /* send the command to the transport layer */ 1241da177e4SLinus Torvalds usb_stor_invoke_transport(srb, us); 1251da177e4SLinus Torvalds } 126e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command); 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /*********************************************************************** 1291da177e4SLinus Torvalds * Scatter-gather transfer buffer access routines 1301da177e4SLinus Torvalds ***********************************************************************/ 1311da177e4SLinus Torvalds 132*f0183a33SFelipe Balbi /* 133*f0183a33SFelipe Balbi * Copy a buffer of length buflen to/from the srb's transfer buffer. 134dd829d23SBoaz Harrosh * Update the **sgptr and *offset variables so that the next copy will 1357084191dSAlan Stern * pick up from where this one left off. 1367084191dSAlan Stern */ 1371da177e4SLinus Torvalds unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, 1381f6f31a0SJens Axboe unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr, 1391da177e4SLinus Torvalds unsigned int *offset, enum xfer_buf_dir dir) 1401da177e4SLinus Torvalds { 141e5fc70d5SMing Lei unsigned int cnt = 0; 1427084191dSAlan Stern struct scatterlist *sg = *sgptr; 143e5fc70d5SMing Lei struct sg_mapping_iter miter; 144e5fc70d5SMing Lei unsigned int nents = scsi_sg_count(srb); 1451da177e4SLinus Torvalds 146e5fc70d5SMing Lei if (sg) 147e5fc70d5SMing Lei nents = sg_nents(sg); 148e5fc70d5SMing Lei else 149dd829d23SBoaz Harrosh sg = scsi_sglist(srb); 1501da177e4SLinus Torvalds 151e5fc70d5SMing Lei sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ? 152e5fc70d5SMing Lei SG_MITER_FROM_SG: SG_MITER_TO_SG); 1531da177e4SLinus Torvalds 154e5fc70d5SMing Lei if (!sg_miter_skip(&miter, *offset)) 155e5fc70d5SMing Lei return cnt; 1561da177e4SLinus Torvalds 157e5fc70d5SMing Lei while (sg_miter_next(&miter) && cnt < buflen) { 15840fcd88bSMing Lei unsigned int len = min_t(unsigned int, miter.length, 15940fcd88bSMing Lei buflen - cnt); 1601da177e4SLinus Torvalds 161e5fc70d5SMing Lei if (dir == FROM_XFER_BUF) 162e5fc70d5SMing Lei memcpy(buffer + cnt, miter.addr, len); 1631da177e4SLinus Torvalds else 164e5fc70d5SMing Lei memcpy(miter.addr, buffer + cnt, len); 1651da177e4SLinus Torvalds 166e5fc70d5SMing Lei if (*offset + len < miter.piter.sg->length) { 167e5fc70d5SMing Lei *offset += len; 168e5fc70d5SMing Lei *sgptr = miter.piter.sg; 169e5fc70d5SMing Lei } else { 170e5fc70d5SMing Lei *offset = 0; 171e5fc70d5SMing Lei *sgptr = sg_next(miter.piter.sg); 1721da177e4SLinus Torvalds } 173e5fc70d5SMing Lei cnt += len; 1741da177e4SLinus Torvalds } 175e5fc70d5SMing Lei sg_miter_stop(&miter); 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds return cnt; 1781da177e4SLinus Torvalds } 179e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf); 1801da177e4SLinus Torvalds 181*f0183a33SFelipe Balbi /* 182*f0183a33SFelipe Balbi * Store the contents of buffer into srb's transfer buffer and set the 1837084191dSAlan Stern * SCSI residue. 1847084191dSAlan Stern */ 1851da177e4SLinus Torvalds void usb_stor_set_xfer_buf(unsigned char *buffer, 1861da177e4SLinus Torvalds unsigned int buflen, struct scsi_cmnd *srb) 1871da177e4SLinus Torvalds { 1881f6f31a0SJens Axboe unsigned int offset = 0; 1891f6f31a0SJens Axboe struct scatterlist *sg = NULL; 1901da177e4SLinus Torvalds 1916d512a80SAlan Stern buflen = min(buflen, scsi_bufflen(srb)); 1927084191dSAlan Stern buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset, 1931da177e4SLinus Torvalds TO_XFER_BUF); 194dd829d23SBoaz Harrosh if (buflen < scsi_bufflen(srb)) 195dd829d23SBoaz Harrosh scsi_set_resid(srb, scsi_bufflen(srb) - buflen); 1961da177e4SLinus Torvalds } 197e6e244b6SAlan Stern EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf); 198