protocol.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include <linux/highmem.h>
  45. #include <linux/export.h>
  46. #include <scsi/scsi.h>
  47. #include <scsi/scsi_cmnd.h>
  48. #include "usb.h"
  49. #include "protocol.h"
  50. #include "debug.h"
  51. #include "scsiglue.h"
  52. #include "transport.h"
  53. /***********************************************************************
  54. * Protocol routines
  55. ***********************************************************************/
  56. void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
  57. {
  58. /*
  59. * Pad the SCSI command with zeros out to 12 bytes. If the
  60. * command already is 12 bytes or longer, leave it alone.
  61. *
  62. * NOTE: This only works because a scsi_cmnd struct field contains
  63. * a unsigned char cmnd[16], so we know we have storage available
  64. */
  65. for (; srb->cmd_len < 12; srb->cmd_len++)
  66. srb->cmnd[srb->cmd_len] = 0;
  67. /* send the command to the transport layer */
  68. usb_stor_invoke_transport(srb, us);
  69. }
  70. void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
  71. {
  72. /* fix some commands -- this is a form of mode translation
  73. * UFI devices only accept 12 byte long commands
  74. *
  75. * NOTE: This only works because a scsi_cmnd struct field contains
  76. * a unsigned char cmnd[16], so we know we have storage available
  77. */
  78. /* Pad the ATAPI command with zeros */
  79. for (; srb->cmd_len < 12; srb->cmd_len++)
  80. srb->cmnd[srb->cmd_len] = 0;
  81. /* set command length to 12 bytes (this affects the transport layer) */
  82. srb->cmd_len = 12;
  83. /* XXX We should be constantly re-evaluating the need for these */
  84. /* determine the correct data length for these commands */
  85. switch (srb->cmnd[0]) {
  86. /* for INQUIRY, UFI devices only ever return 36 bytes */
  87. case INQUIRY:
  88. srb->cmnd[4] = 36;
  89. break;
  90. /* again, for MODE_SENSE_10, we get the minimum (8) */
  91. case MODE_SENSE_10:
  92. srb->cmnd[7] = 0;
  93. srb->cmnd[8] = 8;
  94. break;
  95. /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
  96. case REQUEST_SENSE:
  97. srb->cmnd[4] = 18;
  98. break;
  99. } /* end switch on cmnd[0] */
  100. /* send the command to the transport layer */
  101. usb_stor_invoke_transport(srb, us);
  102. }
  103. void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
  104. struct us_data *us)
  105. {
  106. /* send the command to the transport layer */
  107. usb_stor_invoke_transport(srb, us);
  108. }
  109. EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
  110. /***********************************************************************
  111. * Scatter-gather transfer buffer access routines
  112. ***********************************************************************/
  113. /* Copy a buffer of length buflen to/from the srb's transfer buffer.
  114. * Update the **sgptr and *offset variables so that the next copy will
  115. * pick up from where this one left off.
  116. */
  117. unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
  118. unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
  119. unsigned int *offset, enum xfer_buf_dir dir)
  120. {
  121. unsigned int cnt = 0;
  122. struct scatterlist *sg = *sgptr;
  123. struct sg_mapping_iter miter;
  124. unsigned int nents = scsi_sg_count(srb);
  125. if (sg)
  126. nents = sg_nents(sg);
  127. else
  128. sg = scsi_sglist(srb);
  129. sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
  130. SG_MITER_FROM_SG: SG_MITER_TO_SG);
  131. if (!sg_miter_skip(&miter, *offset))
  132. return cnt;
  133. while (sg_miter_next(&miter) && cnt < buflen) {
  134. unsigned int len = min_t(unsigned int, miter.length,
  135. buflen - cnt);
  136. if (dir == FROM_XFER_BUF)
  137. memcpy(buffer + cnt, miter.addr, len);
  138. else
  139. memcpy(miter.addr, buffer + cnt, len);
  140. if (*offset + len < miter.piter.sg->length) {
  141. *offset += len;
  142. *sgptr = miter.piter.sg;
  143. } else {
  144. *offset = 0;
  145. *sgptr = sg_next(miter.piter.sg);
  146. }
  147. cnt += len;
  148. }
  149. sg_miter_stop(&miter);
  150. return cnt;
  151. }
  152. EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
  153. /* Store the contents of buffer into srb's transfer buffer and set the
  154. * SCSI residue.
  155. */
  156. void usb_stor_set_xfer_buf(unsigned char *buffer,
  157. unsigned int buflen, struct scsi_cmnd *srb)
  158. {
  159. unsigned int offset = 0;
  160. struct scatterlist *sg = NULL;
  161. buflen = min(buflen, scsi_bufflen(srb));
  162. buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
  163. TO_XFER_BUF);
  164. if (buflen < scsi_bufflen(srb))
  165. scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
  166. }
  167. EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);