protocol.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Driver for USB Mass Storage compliant devices
  2. *
  3. * $Id: protocol.c,v 1.14 2002/04/22 03:39:43 mdharm Exp $
  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. * Please see http://www.one-eyed-alien.net/~mdharm/linux-usb for more
  30. * information about this driver.
  31. *
  32. * This program is free software; you can redistribute it and/or modify it
  33. * under the terms of the GNU General Public License as published by the
  34. * Free Software Foundation; either version 2, or (at your option) any
  35. * later version.
  36. *
  37. * This program is distributed in the hope that it will be useful, but
  38. * WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  40. * General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License along
  43. * with this program; if not, write to the Free Software Foundation, Inc.,
  44. * 675 Mass Ave, Cambridge, MA 02139, USA.
  45. */
  46. #include <linux/highmem.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_qic157_command(struct scsi_cmnd *srb, struct us_data *us)
  58. {
  59. /* Pad the ATAPI command with zeros
  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. for (; srb->cmd_len<12; srb->cmd_len++)
  65. srb->cmnd[srb->cmd_len] = 0;
  66. /* set command length to 12 bytes */
  67. srb->cmd_len = 12;
  68. /* send the command to the transport layer */
  69. usb_stor_invoke_transport(srb, us);
  70. }
  71. void usb_stor_ATAPI_command(struct scsi_cmnd *srb, struct us_data *us)
  72. {
  73. /* Pad the ATAPI command with zeros
  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 */
  82. srb->cmd_len = 12;
  83. /* send the command to the transport layer */
  84. usb_stor_invoke_transport(srb, us);
  85. }
  86. void usb_stor_ufi_command(struct scsi_cmnd *srb, struct us_data *us)
  87. {
  88. /* fix some commands -- this is a form of mode translation
  89. * UFI devices only accept 12 byte long commands
  90. *
  91. * NOTE: This only works because a scsi_cmnd struct field contains
  92. * a unsigned char cmnd[16], so we know we have storage available
  93. */
  94. /* Pad the ATAPI command with zeros */
  95. for (; srb->cmd_len<12; srb->cmd_len++)
  96. srb->cmnd[srb->cmd_len] = 0;
  97. /* set command length to 12 bytes (this affects the transport layer) */
  98. srb->cmd_len = 12;
  99. /* XXX We should be constantly re-evaluating the need for these */
  100. /* determine the correct data length for these commands */
  101. switch (srb->cmnd[0]) {
  102. /* for INQUIRY, UFI devices only ever return 36 bytes */
  103. case INQUIRY:
  104. srb->cmnd[4] = 36;
  105. break;
  106. /* again, for MODE_SENSE_10, we get the minimum (8) */
  107. case MODE_SENSE_10:
  108. srb->cmnd[7] = 0;
  109. srb->cmnd[8] = 8;
  110. break;
  111. /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
  112. case REQUEST_SENSE:
  113. srb->cmnd[4] = 18;
  114. break;
  115. } /* end switch on cmnd[0] */
  116. /* send the command to the transport layer */
  117. usb_stor_invoke_transport(srb, us);
  118. }
  119. void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
  120. struct us_data *us)
  121. {
  122. /* send the command to the transport layer */
  123. usb_stor_invoke_transport(srb, us);
  124. }
  125. /***********************************************************************
  126. * Scatter-gather transfer buffer access routines
  127. ***********************************************************************/
  128. /* Copy a buffer of length buflen to/from the srb's transfer buffer.
  129. * (Note: for scatter-gather transfers (srb->use_sg > 0), srb->request_buffer
  130. * points to a list of s-g entries and we ignore srb->request_bufflen.
  131. * For non-scatter-gather transfers, srb->request_buffer points to the
  132. * transfer buffer itself and srb->request_bufflen is the buffer's length.)
  133. * Update the *index and *offset variables so that the next copy will
  134. * pick up from where this one left off. */
  135. unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
  136. unsigned int buflen, struct scsi_cmnd *srb, unsigned int *index,
  137. unsigned int *offset, enum xfer_buf_dir dir)
  138. {
  139. unsigned int cnt;
  140. /* If not using scatter-gather, just transfer the data directly.
  141. * Make certain it will fit in the available buffer space. */
  142. if (srb->use_sg == 0) {
  143. if (*offset >= srb->request_bufflen)
  144. return 0;
  145. cnt = min(buflen, srb->request_bufflen - *offset);
  146. if (dir == TO_XFER_BUF)
  147. memcpy((unsigned char *) srb->request_buffer + *offset,
  148. buffer, cnt);
  149. else
  150. memcpy(buffer, (unsigned char *) srb->request_buffer +
  151. *offset, cnt);
  152. *offset += cnt;
  153. /* Using scatter-gather. We have to go through the list one entry
  154. * at a time. Each s-g entry contains some number of pages, and
  155. * each page has to be kmap()'ed separately. If the page is already
  156. * in kernel-addressable memory then kmap() will return its address.
  157. * If the page is not directly accessible -- such as a user buffer
  158. * located in high memory -- then kmap() will map it to a temporary
  159. * position in the kernel's virtual address space. */
  160. } else {
  161. struct scatterlist *sg =
  162. (struct scatterlist *) srb->request_buffer
  163. + *index;
  164. /* This loop handles a single s-g list entry, which may
  165. * include multiple pages. Find the initial page structure
  166. * and the starting offset within the page, and update
  167. * the *offset and *index values for the next loop. */
  168. cnt = 0;
  169. while (cnt < buflen && *index < srb->use_sg) {
  170. struct page *page = sg->page +
  171. ((sg->offset + *offset) >> PAGE_SHIFT);
  172. unsigned int poff =
  173. (sg->offset + *offset) & (PAGE_SIZE-1);
  174. unsigned int sglen = sg->length - *offset;
  175. if (sglen > buflen - cnt) {
  176. /* Transfer ends within this s-g entry */
  177. sglen = buflen - cnt;
  178. *offset += sglen;
  179. } else {
  180. /* Transfer continues to next s-g entry */
  181. *offset = 0;
  182. ++*index;
  183. ++sg;
  184. }
  185. /* Transfer the data for all the pages in this
  186. * s-g entry. For each page: call kmap(), do the
  187. * transfer, and call kunmap() immediately after. */
  188. while (sglen > 0) {
  189. unsigned int plen = min(sglen, (unsigned int)
  190. PAGE_SIZE - poff);
  191. unsigned char *ptr = kmap(page);
  192. if (dir == TO_XFER_BUF)
  193. memcpy(ptr + poff, buffer + cnt, plen);
  194. else
  195. memcpy(buffer + cnt, ptr + poff, plen);
  196. kunmap(page);
  197. /* Start at the beginning of the next page */
  198. poff = 0;
  199. ++page;
  200. cnt += plen;
  201. sglen -= plen;
  202. }
  203. }
  204. }
  205. /* Return the amount actually transferred */
  206. return cnt;
  207. }
  208. /* Store the contents of buffer into srb's transfer buffer and set the
  209. * SCSI residue. */
  210. void usb_stor_set_xfer_buf(unsigned char *buffer,
  211. unsigned int buflen, struct scsi_cmnd *srb)
  212. {
  213. unsigned int index = 0, offset = 0;
  214. usb_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset,
  215. TO_XFER_BUF);
  216. if (buflen < srb->request_bufflen)
  217. srb->resid = srb->request_bufflen - buflen;
  218. }