protocol.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Driver for USB Mass Storage compliant devices
  3. *
  4. * Current development and maintenance by:
  5. * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  6. *
  7. * Developed with the assistance of:
  8. * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
  9. * (c) 2002 Alan Stern (stern@rowland.org)
  10. *
  11. * Initial work by:
  12. * (c) 1999 Michael Gee (michael@linuxspecific.com)
  13. *
  14. * This driver is based on the 'USB Mass Storage Class' document. This
  15. * describes in detail the protocol used to communicate with such
  16. * devices. Clearly, the designers had SCSI and ATAPI commands in
  17. * mind when they created this document. The commands are all very
  18. * similar to commands in the SCSI-II and ATAPI specifications.
  19. *
  20. * It is important to note that in a number of cases this class
  21. * exhibits class-specific exemptions from the USB specification.
  22. * Notably the usage of NAK, STALL and ACK differs from the norm, in
  23. * that they are used to communicate wait, failed and OK on commands.
  24. *
  25. * Also, for certain devices, the interrupt endpoint is used to convey
  26. * status of a command.
  27. *
  28. * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
  29. * information about this driver.
  30. *
  31. * This program is free software; you can redistribute it and/or modify it
  32. * under the terms of the GNU General Public License as published by the
  33. * Free Software Foundation; either version 2, or (at your option) any
  34. * later version.
  35. *
  36. * This program is distributed in the hope that it will be useful, but
  37. * WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  39. * General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License along
  42. * with this program; if not, write to the Free Software Foundation, Inc.,
  43. * 675 Mass Ave, Cambridge, MA 02139, USA.
  44. */
  45. #include <linux/highmem.h>
  46. #include <linux/export.h>
  47. #include <scsi/scsi.h>
  48. #include <scsi/scsi_cmnd.h>
  49. #include "usb.h"
  50. #include "protocol.h"
  51. #include "debug.h"
  52. #include "scsiglue.h"
  53. #include "transport.h"
  54. /***********************************************************************
  55. * Protocol routines
  56. ***********************************************************************/
  57. void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
  58. {
  59. /*
  60. * Pad the SCSI command with zeros out to 12 bytes. If the
  61. * command already is 12 bytes or longer, leave it alone.
  62. *
  63. * NOTE: This only works because a scsi_cmnd struct field contains
  64. * a unsigned char cmnd[16], so we know we have storage available
  65. */
  66. for (; srb->cmd_len < 12; srb->cmd_len++)
  67. srb->cmnd[srb->cmd_len] = 0;
  68. /* send the command to the transport layer */
  69. usb_stor_invoke_transport(srb, us);
  70. }
  71. void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
  72. {
  73. /*
  74. * fix some commands -- this is a form of mode translation
  75. * UFI devices only accept 12 byte long commands
  76. *
  77. * NOTE: This only works because a scsi_cmnd struct field contains
  78. * a unsigned char cmnd[16], so we know we have storage available
  79. */
  80. /* Pad the ATAPI command with zeros */
  81. for (; srb->cmd_len < 12; srb->cmd_len++)
  82. srb->cmnd[srb->cmd_len] = 0;
  83. /* set command length to 12 bytes (this affects the transport layer) */
  84. srb->cmd_len = 12;
  85. /* XXX We should be constantly re-evaluating the need for these */
  86. /* determine the correct data length for these commands */
  87. switch (srb->cmnd[0]) {
  88. /* for INQUIRY, UFI devices only ever return 36 bytes */
  89. case INQUIRY:
  90. srb->cmnd[4] = 36;
  91. break;
  92. /* again, for MODE_SENSE_10, we get the minimum (8) */
  93. case MODE_SENSE_10:
  94. srb->cmnd[7] = 0;
  95. srb->cmnd[8] = 8;
  96. break;
  97. /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
  98. case REQUEST_SENSE:
  99. srb->cmnd[4] = 18;
  100. break;
  101. } /* end switch on cmnd[0] */
  102. /* send the command to the transport layer */
  103. usb_stor_invoke_transport(srb, us);
  104. }
  105. void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
  106. struct us_data *us)
  107. {
  108. /* send the command to the transport layer */
  109. usb_stor_invoke_transport(srb, us);
  110. }
  111. EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
  112. /***********************************************************************
  113. * Scatter-gather transfer buffer access routines
  114. ***********************************************************************/
  115. /*
  116. * Copy a buffer of length buflen to/from the srb's transfer buffer.
  117. * Update the **sgptr and *offset variables so that the next copy will
  118. * pick up from where this one left off.
  119. */
  120. unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
  121. unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
  122. unsigned int *offset, enum xfer_buf_dir dir)
  123. {
  124. unsigned int cnt = 0;
  125. struct scatterlist *sg = *sgptr;
  126. struct sg_mapping_iter miter;
  127. unsigned int nents = scsi_sg_count(srb);
  128. if (sg)
  129. nents = sg_nents(sg);
  130. else
  131. sg = scsi_sglist(srb);
  132. sg_miter_start(&miter, sg, nents, dir == FROM_XFER_BUF ?
  133. SG_MITER_FROM_SG: SG_MITER_TO_SG);
  134. if (!sg_miter_skip(&miter, *offset))
  135. return cnt;
  136. while (sg_miter_next(&miter) && cnt < buflen) {
  137. unsigned int len = min_t(unsigned int, miter.length,
  138. buflen - cnt);
  139. if (dir == FROM_XFER_BUF)
  140. memcpy(buffer + cnt, miter.addr, len);
  141. else
  142. memcpy(miter.addr, buffer + cnt, len);
  143. if (*offset + len < miter.piter.sg->length) {
  144. *offset += len;
  145. *sgptr = miter.piter.sg;
  146. } else {
  147. *offset = 0;
  148. *sgptr = sg_next(miter.piter.sg);
  149. }
  150. cnt += len;
  151. }
  152. sg_miter_stop(&miter);
  153. return cnt;
  154. }
  155. EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
  156. /*
  157. * Store the contents of buffer into srb's transfer buffer and set the
  158. * SCSI residue.
  159. */
  160. void usb_stor_set_xfer_buf(unsigned char *buffer,
  161. unsigned int buflen, struct scsi_cmnd *srb)
  162. {
  163. unsigned int offset = 0;
  164. struct scatterlist *sg = NULL;
  165. buflen = min(buflen, scsi_bufflen(srb));
  166. buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
  167. TO_XFER_BUF);
  168. if (buflen < scsi_bufflen(srb))
  169. scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
  170. }
  171. EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);