protocol.c 7.3 KB

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