protocol.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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;
  122. struct scatterlist *sg = *sgptr;
  123. /* We have to go through the list one entry
  124. * at a time. Each s-g entry contains some number of pages, and
  125. * each page has to be kmap()'ed separately. If the page is already
  126. * in kernel-addressable memory then kmap() will return its address.
  127. * If the page is not directly accessible -- such as a user buffer
  128. * located in high memory -- then kmap() will map it to a temporary
  129. * position in the kernel's virtual address space.
  130. */
  131. if (!sg)
  132. sg = scsi_sglist(srb);
  133. /* This loop handles a single s-g list entry, which may
  134. * include multiple pages. Find the initial page structure
  135. * and the starting offset within the page, and update
  136. * the *offset and **sgptr values for the next loop.
  137. */
  138. cnt = 0;
  139. while (cnt < buflen && sg) {
  140. struct page *page = sg_page(sg) +
  141. ((sg->offset + *offset) >> PAGE_SHIFT);
  142. unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
  143. unsigned int sglen = sg->length - *offset;
  144. if (sglen > buflen - cnt) {
  145. /* Transfer ends within this s-g entry */
  146. sglen = buflen - cnt;
  147. *offset += sglen;
  148. } else {
  149. /* Transfer continues to next s-g entry */
  150. *offset = 0;
  151. sg = sg_next(sg);
  152. }
  153. /* Transfer the data for all the pages in this
  154. * s-g entry. For each page: call kmap(), do the
  155. * transfer, and call kunmap() immediately after. */
  156. while (sglen > 0) {
  157. unsigned int plen = min(sglen, (unsigned int)
  158. PAGE_SIZE - poff);
  159. unsigned char *ptr = kmap(page);
  160. if (dir == TO_XFER_BUF)
  161. memcpy(ptr + poff, buffer + cnt, plen);
  162. else
  163. memcpy(buffer + cnt, ptr + poff, plen);
  164. kunmap(page);
  165. /* Start at the beginning of the next page */
  166. poff = 0;
  167. ++page;
  168. cnt += plen;
  169. sglen -= plen;
  170. }
  171. }
  172. *sgptr = sg;
  173. /* Return the amount actually transferred */
  174. return cnt;
  175. }
  176. EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
  177. /* Store the contents of buffer into srb's transfer buffer and set the
  178. * SCSI residue.
  179. */
  180. void usb_stor_set_xfer_buf(unsigned char *buffer,
  181. unsigned int buflen, struct scsi_cmnd *srb)
  182. {
  183. unsigned int offset = 0;
  184. struct scatterlist *sg = NULL;
  185. buflen = min(buflen, scsi_bufflen(srb));
  186. buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
  187. TO_XFER_BUF);
  188. if (buflen < scsi_bufflen(srb))
  189. scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
  190. }
  191. EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);