protocol.c 5.3 KB

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