protocol.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* Driver for USB Mass Storage compliant devices
  2.  *
  3.  * $Id: protocol.c,v 1.13 2002/02/25 00:34:56 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 "protocol.h"
  47. #include "usb.h"
  48. #include "debug.h"
  49. #include "scsiglue.h"
  50. #include "transport.h"
  51. /***********************************************************************
  52.  * Helper routines
  53.  ***********************************************************************/
  54. /* Fix-up the return data from an INQUIRY command to show 
  55.  * ANSI SCSI rev 2 so we don't confuse the SCSI layers above us
  56.  */
  57. void fix_inquiry_data(Scsi_Cmnd *srb)
  58. {
  59. unsigned char *data_ptr;
  60. /* verify that it's an INQUIRY command */
  61. if (srb->cmnd[0] != INQUIRY)
  62. return;
  63. US_DEBUGP("Fixing INQUIRY data to show SCSI rev 2n");
  64. /* find the location of the data */
  65. if (srb->use_sg) {
  66. struct scatterlist *sg;
  67. sg = (struct scatterlist *) srb->request_buffer;
  68. data_ptr = (unsigned char *) sg[0].address;
  69. } else
  70. data_ptr = (unsigned char *)srb->request_buffer;
  71. /* Change the SCSI revision number */
  72. data_ptr[2] = (data_ptr[2] & ~7) | 2;
  73. }
  74. /***********************************************************************
  75.  * Protocol routines
  76.  ***********************************************************************/
  77. void usb_stor_qic157_command(Scsi_Cmnd *srb, struct us_data *us)
  78. {
  79. /* Pad the ATAPI command with zeros 
  80.  * NOTE: This only works because a Scsi_Cmnd struct field contains
  81.  * a unsigned char cmnd[12], so we know we have storage available
  82.  */
  83. for (; srb->cmd_len<12; srb->cmd_len++)
  84. srb->cmnd[srb->cmd_len] = 0;
  85. /* set command length to 12 bytes */
  86. srb->cmd_len = 12;
  87. /* send the command to the transport layer */
  88. usb_stor_invoke_transport(srb, us);
  89. if (srb->result == GOOD << 1) {
  90. /* fix the INQUIRY data if necessary */
  91. fix_inquiry_data(srb);
  92. }
  93. }
  94. void usb_stor_ATAPI_command(Scsi_Cmnd *srb, struct us_data *us)
  95. {
  96. int old_cmnd = 0;
  97. /* Fix some commands -- this is a form of mode translation
  98.  * ATAPI devices only accept 12 byte long commands 
  99.  *
  100.  * NOTE: This only works because a Scsi_Cmnd struct field contains
  101.  * a unsigned char cmnd[12], so we know we have storage available
  102.  */
  103. /* Pad the ATAPI command with zeros */
  104. for (; srb->cmd_len<12; srb->cmd_len++)
  105. srb->cmnd[srb->cmd_len] = 0;
  106. /* set command length to 12 bytes */
  107. srb->cmd_len = 12;
  108. /* determine the correct (or minimum) data length for these commands */
  109. switch (srb->cmnd[0]) {
  110. /* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
  111. case MODE_SENSE:
  112. case MODE_SELECT:
  113. /* save the command so we can tell what it was */
  114. old_cmnd = srb->cmnd[0];
  115. srb->cmnd[11] = 0;
  116. srb->cmnd[10] = 0;
  117. srb->cmnd[9] = 0;
  118. srb->cmnd[8] = srb->cmnd[4];
  119. srb->cmnd[7] = 0;
  120. srb->cmnd[6] = 0;
  121. srb->cmnd[5] = 0;
  122. srb->cmnd[4] = 0;
  123. srb->cmnd[3] = 0;
  124. srb->cmnd[2] = srb->cmnd[2];
  125. srb->cmnd[1] = srb->cmnd[1];
  126. srb->cmnd[0] = srb->cmnd[0] | 0x40;
  127. break;
  128. /* change READ_6/WRITE_6 to READ_10/WRITE_10, which 
  129.  * are ATAPI commands */
  130. case WRITE_6:
  131. case READ_6:
  132. srb->cmnd[11] = 0;
  133. srb->cmnd[10] = 0;
  134. srb->cmnd[9] = 0;
  135. srb->cmnd[8] = srb->cmnd[4];
  136. srb->cmnd[7] = 0;
  137. srb->cmnd[6] = 0;
  138. srb->cmnd[5] = srb->cmnd[3];
  139. srb->cmnd[4] = srb->cmnd[2];
  140. srb->cmnd[3] = srb->cmnd[1] & 0x1F;
  141. srb->cmnd[2] = 0;
  142. srb->cmnd[1] = srb->cmnd[1] & 0xE0;
  143. srb->cmnd[0] = srb->cmnd[0] | 0x20;
  144. break;
  145. } /* end switch on cmnd[0] */
  146. /* convert MODE_SELECT data here */
  147. if (old_cmnd == MODE_SELECT)
  148. usb_stor_scsiSense6to10(srb);
  149. /* send the command to the transport layer */
  150. usb_stor_invoke_transport(srb, us);
  151. if (srb->result == GOOD << 1) {
  152. /* Fix the MODE_SENSE data if we translated the command */
  153. if (old_cmnd == MODE_SENSE)
  154. usb_stor_scsiSense10to6(srb);
  155. /* fix the INQUIRY data if necessary */
  156. fix_inquiry_data(srb);
  157. }
  158. }
  159. void usb_stor_ufi_command(Scsi_Cmnd *srb, struct us_data *us)
  160. {
  161. int old_cmnd = 0;
  162. /* fix some commands -- this is a form of mode translation
  163.  * UFI devices only accept 12 byte long commands 
  164.  *
  165.  * NOTE: This only works because a Scsi_Cmnd struct field contains
  166.  * a unsigned char cmnd[12], so we know we have storage available
  167.  */
  168. /* set command length to 12 bytes (this affects the transport layer) */
  169. srb->cmd_len = 12;
  170. /* determine the correct (or minimum) data length for these commands */
  171. switch (srb->cmnd[0]) {
  172. /* for INQUIRY, UFI devices only ever return 36 bytes */
  173. case INQUIRY:
  174. srb->cmnd[4] = 36;
  175. break;
  176. /* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
  177. case MODE_SENSE:
  178. case MODE_SELECT:
  179. /* save the command so we can tell what it was */
  180. old_cmnd = srb->cmnd[0];
  181. srb->cmnd[11] = 0;
  182. srb->cmnd[10] = 0;
  183. srb->cmnd[9] = 0;
  184. /* if we're sending data, we send all. If getting data, 
  185.  * get the minimum */
  186. if (srb->cmnd[0] == MODE_SELECT)
  187. srb->cmnd[8] = srb->cmnd[4];
  188. else
  189. srb->cmnd[8] = 8;
  190. srb->cmnd[7] = 0;
  191. srb->cmnd[6] = 0;
  192. srb->cmnd[5] = 0;
  193. srb->cmnd[4] = 0;
  194. srb->cmnd[3] = 0;
  195. srb->cmnd[2] = srb->cmnd[2];
  196. srb->cmnd[1] = srb->cmnd[1];
  197. srb->cmnd[0] = srb->cmnd[0] | 0x40;
  198. break;
  199. /* again, for MODE_SENSE_10, we get the minimum (8) */
  200. case MODE_SENSE_10:
  201. srb->cmnd[7] = 0;
  202. srb->cmnd[8] = 8;
  203. break;
  204. /* for REQUEST_SENSE, UFI devices only ever return 18 bytes */
  205. case REQUEST_SENSE:
  206. srb->cmnd[4] = 18;
  207. break;
  208. /* change READ_6/WRITE_6 to READ_10/WRITE_10, which 
  209.  * are UFI commands */
  210. case WRITE_6:
  211. case READ_6:
  212. srb->cmnd[11] = 0;
  213. srb->cmnd[10] = 0;
  214. srb->cmnd[9] = 0;
  215. srb->cmnd[8] = srb->cmnd[4];
  216. srb->cmnd[7] = 0;
  217. srb->cmnd[6] = 0;
  218. srb->cmnd[5] = srb->cmnd[3];
  219. srb->cmnd[4] = srb->cmnd[2];
  220. srb->cmnd[3] = srb->cmnd[1] & 0x1F;
  221. srb->cmnd[2] = 0;
  222. srb->cmnd[1] = srb->cmnd[1] & 0xE0;
  223. srb->cmnd[0] = srb->cmnd[0] | 0x20;
  224. break;
  225. } /* end switch on cmnd[0] */
  226. /* convert MODE_SELECT data here */
  227. if (old_cmnd == MODE_SELECT)
  228. usb_stor_scsiSense6to10(srb);
  229. /* send the command to the transport layer */
  230. usb_stor_invoke_transport(srb, us);
  231. if (srb->result == GOOD << 1) {
  232. /* Fix the MODE_SENSE data if we translated the command */
  233. if (old_cmnd == MODE_SENSE)
  234. usb_stor_scsiSense10to6(srb);
  235. /* Fix the data for an INQUIRY, if necessary */
  236. fix_inquiry_data(srb);
  237. }
  238. }
  239. void usb_stor_transparent_scsi_command(Scsi_Cmnd *srb, struct us_data *us)
  240. {
  241. int old_cmnd = 0;
  242. /* This code supports devices which do not support {READ|WRITE}_6
  243.  * Apparently, neither Windows or MacOS will use these commands,
  244.  * so some devices do not support them
  245.  */
  246. if (us->flags & US_FL_MODE_XLATE) {
  247. US_DEBUGP("Invoking Mode Translationn");
  248. /* save the old command for later */
  249. old_cmnd = srb->cmnd[0];
  250. switch (srb->cmnd[0]) {
  251. /* change READ_6/WRITE_6 to READ_10/WRITE_10 */
  252. case WRITE_6:
  253. case READ_6:
  254. srb->cmd_len = 12;
  255. srb->cmnd[11] = 0;
  256. srb->cmnd[10] = 0;
  257. srb->cmnd[9] = 0;
  258. srb->cmnd[8] = srb->cmnd[4];
  259. srb->cmnd[7] = 0;
  260. srb->cmnd[6] = 0;
  261. srb->cmnd[5] = srb->cmnd[3];
  262. srb->cmnd[4] = srb->cmnd[2];
  263. srb->cmnd[3] = srb->cmnd[1] & 0x1F;
  264. srb->cmnd[2] = 0;
  265. srb->cmnd[1] = srb->cmnd[1] & 0xE0;
  266. srb->cmnd[0] = srb->cmnd[0] | 0x20;
  267. break;
  268. /* convert MODE_SELECT data here */
  269. case MODE_SENSE:
  270. case MODE_SELECT:
  271. srb->cmd_len = 12;
  272. srb->cmnd[11] = 0;
  273. srb->cmnd[10] = 0;
  274. srb->cmnd[9] = 0;
  275. srb->cmnd[8] = srb->cmnd[4];
  276. srb->cmnd[7] = 0;
  277. srb->cmnd[6] = 0;
  278. srb->cmnd[5] = 0;
  279. srb->cmnd[4] = 0;
  280. srb->cmnd[3] = 0;
  281. srb->cmnd[2] = srb->cmnd[2];
  282. srb->cmnd[1] = srb->cmnd[1];
  283. srb->cmnd[0] = srb->cmnd[0] | 0x40;
  284. break;
  285. } /* switch (srb->cmnd[0]) */
  286. } /* if (us->flags & US_FL_MODE_XLATE) */
  287. /* convert MODE_SELECT data here */
  288. if ((us->flags & US_FL_MODE_XLATE) && (old_cmnd == MODE_SELECT))
  289. usb_stor_scsiSense6to10(srb);
  290. /* send the command to the transport layer */
  291. usb_stor_invoke_transport(srb, us);
  292. if (srb->result == GOOD << 1) {
  293. /* Fix the MODE_SENSE data if we translated the command */
  294. if ((us->flags & US_FL_MODE_XLATE) && (old_cmnd == MODE_SENSE))
  295. usb_stor_scsiSense10to6(srb);
  296. /* fix the INQUIRY data if necessary */
  297. fix_inquiry_data(srb);
  298. }
  299. }