protocol.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <scsi/scsi.h>
  46. #include <scsi/scsi_cmnd.h>
  47. #include "usb.h"
  48. #include "protocol.h"
  49. #include "debug.h"
  50. #include "scsiglue.h"
  51. #include "transport.h"
  52. /***********************************************************************
  53. * Protocol routines
  54. ***********************************************************************/
  55. void usb_stor_pad12_command(struct scsi_cmnd *srb, struct us_data *us)
  56. {
  57. /* Pad the SCSI command with zeros out to 12 bytes
  58. *
  59. * NOTE: This only works because a scsi_cmnd struct field contains
  60. * a unsigned char cmnd[16], so we know we have storage available
  61. */
  62. for (; srb->cmd_len<12; srb->cmd_len++)
  63. srb->cmnd[srb->cmd_len] = 0;
  64. /* set command length to 12 bytes */
  65. srb->cmd_len = 12;
  66. /* send the command to the transport layer */
  67. usb_stor_invoke_transport(srb, us);
  68. }
  69. void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
  70. {
  71. /* fix some commands -- this is a form of mode translation
  72. * UFI devices only accept 12 byte long commands
  73. *
  74. * NOTE: This only works because a scsi_cmnd struct field contains
  75. * a unsigned char cmnd[16], so we know we have storage available
  76. */
  77. /* Pad the ATAPI command with zeros */
  78. for (; srb->cmd_len<12; srb->cmd_len++)
  79. srb->cmnd[srb->cmd_len] = 0;
  80. /* set command length to 12 bytes (this affects the transport layer) */
  81. srb->cmd_len = 12;
  82. /* XXX We should be constantly re-evaluating the need for these */
  83. /* determine the correct data length for these commands */
  84. switch (srb->cmnd[0]) {
  85. /* for INQUIRY, UFI devices only ever return 36 bytes */
  86. case INQUIRY:
  87. srb->cmnd[4] = 36;
  88. break;
  89. /* again, for MODE_SENSE_10, we get the minimum (8) */
  90. case MODE_SENSE_10:
  91. srb->cmnd[7] = 0;
  92. srb->cmnd[8] = 8;
  93. break;
  94. /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
  95. case REQUEST_SENSE:
  96. srb->cmnd[4] = 18;
  97. break;
  98. } /* end switch on cmnd[0] */
  99. /* send the command to the transport layer */
  100. usb_stor_invoke_transport(srb, us);
  101. }
  102. void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
  103. struct us_data *us)
  104. {
  105. /* send the command to the transport layer */
  106. usb_stor_invoke_transport(srb, us);
  107. }
  108. EXPORT_SYMBOL_GPL(usb_stor_transparent_scsi_command);
  109. /***********************************************************************
  110. * Scatter-gather transfer buffer access routines
  111. ***********************************************************************/
  112. /* Copy a buffer of length buflen to/from the srb's transfer buffer.
  113. * Update the **sgptr and *offset variables so that the next copy will
  114. * pick up from where this one left off.
  115. */
  116. unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
  117. unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
  118. unsigned int *offset, enum xfer_buf_dir dir)
  119. {
  120. unsigned int cnt;
  121. struct scatterlist *sg = *sgptr;
  122. /* We have to go through the list one entry
  123. * at a time. Each s-g entry contains some number of pages, and
  124. * each page has to be kmap()'ed separately. If the page is already
  125. * in kernel-addressable memory then kmap() will return its address.
  126. * If the page is not directly accessible -- such as a user buffer
  127. * located in high memory -- then kmap() will map it to a temporary
  128. * position in the kernel's virtual address space.
  129. */
  130. if (!sg)
  131. sg = scsi_sglist(srb);
  132. /* This loop handles a single s-g list entry, which may
  133. * include multiple pages. Find the initial page structure
  134. * and the starting offset within the page, and update
  135. * the *offset and **sgptr values for the next loop.
  136. */
  137. cnt = 0;
  138. while (cnt < buflen && sg) {
  139. struct page *page = sg_page(sg) +
  140. ((sg->offset + *offset) >> PAGE_SHIFT);
  141. unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
  142. unsigned int sglen = sg->length - *offset;
  143. if (sglen > buflen - cnt) {
  144. /* Transfer ends within this s-g entry */
  145. sglen = buflen - cnt;
  146. *offset += sglen;
  147. } else {
  148. /* Transfer continues to next s-g entry */
  149. *offset = 0;
  150. sg = sg_next(sg);
  151. }
  152. /* Transfer the data for all the pages in this
  153. * s-g entry. For each page: call kmap(), do the
  154. * transfer, and call kunmap() immediately after. */
  155. while (sglen > 0) {
  156. unsigned int plen = min(sglen, (unsigned int)
  157. PAGE_SIZE - poff);
  158. unsigned char *ptr = kmap(page);
  159. if (dir == TO_XFER_BUF)
  160. memcpy(ptr + poff, buffer + cnt, plen);
  161. else
  162. memcpy(buffer + cnt, ptr + poff, plen);
  163. kunmap(page);
  164. /* Start at the beginning of the next page */
  165. poff = 0;
  166. ++page;
  167. cnt += plen;
  168. sglen -= plen;
  169. }
  170. }
  171. *sgptr = sg;
  172. /* Return the amount actually transferred */
  173. return cnt;
  174. }
  175. EXPORT_SYMBOL_GPL(usb_stor_access_xfer_buf);
  176. /* Store the contents of buffer into srb's transfer buffer and set the
  177. * SCSI residue.
  178. */
  179. void usb_stor_set_xfer_buf(unsigned char *buffer,
  180. unsigned int buflen, struct scsi_cmnd *srb)
  181. {
  182. unsigned int offset = 0;
  183. struct scatterlist *sg = NULL;
  184. buflen = min(buflen, scsi_bufflen(srb));
  185. buflen = usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
  186. TO_XFER_BUF);
  187. if (buflen < scsi_bufflen(srb))
  188. scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
  189. }
  190. EXPORT_SYMBOL_GPL(usb_stor_set_xfer_buf);