protocol.c 5.2 KB

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