cypress_atacb.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Support for emulating SAT (ata pass through) on devices based
  3. * on the Cypress USB/ATA bridge supporting ATACB.
  4. *
  5. * Copyright (c) 2008 Matthieu Castet (castet.matthieu@free.fr)
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <scsi/scsi.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/scsi_eh.h>
  25. #include <linux/ata.h>
  26. #include "usb.h"
  27. #include "protocol.h"
  28. #include "scsiglue.h"
  29. #include "debug.h"
  30. #define DRV_NAME "ums-cypress"
  31. MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
  32. MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
  33. MODULE_LICENSE("GPL");
  34. /*
  35. * The table of devices
  36. */
  37. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  38. vendorName, productName, useProtocol, useTransport, \
  39. initFunction, flags) \
  40. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  41. .driver_info = (flags) }
  42. static struct usb_device_id cypress_usb_ids[] = {
  43. # include "unusual_cypress.h"
  44. { } /* Terminating entry */
  45. };
  46. MODULE_DEVICE_TABLE(usb, cypress_usb_ids);
  47. #undef UNUSUAL_DEV
  48. /*
  49. * The flags table
  50. */
  51. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  52. vendor_name, product_name, use_protocol, use_transport, \
  53. init_function, Flags) \
  54. { \
  55. .vendorName = vendor_name, \
  56. .productName = product_name, \
  57. .useProtocol = use_protocol, \
  58. .useTransport = use_transport, \
  59. .initFunction = init_function, \
  60. }
  61. static struct us_unusual_dev cypress_unusual_dev_list[] = {
  62. # include "unusual_cypress.h"
  63. { } /* Terminating entry */
  64. };
  65. #undef UNUSUAL_DEV
  66. /*
  67. * ATACB is a protocol used on cypress usb<->ata bridge to
  68. * send raw ATA command over mass storage
  69. * There is a ATACB2 protocol that support LBA48 on newer chip.
  70. * More info that be found on cy7c68310_8.pdf and cy7c68300c_8.pdf
  71. * datasheet from cypress.com.
  72. */
  73. static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
  74. {
  75. unsigned char save_cmnd[MAX_COMMAND_SIZE];
  76. if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
  77. usb_stor_transparent_scsi_command(srb, us);
  78. return;
  79. }
  80. memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
  81. memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
  82. /* check if we support the command */
  83. if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
  84. goto invalid_fld;
  85. /* check protocol */
  86. switch ((save_cmnd[1] >> 1) & 0xf) {
  87. case 3: /*no DATA */
  88. case 4: /* PIO in */
  89. case 5: /* PIO out */
  90. break;
  91. default:
  92. goto invalid_fld;
  93. }
  94. /* first build the ATACB command */
  95. srb->cmd_len = 16;
  96. srb->cmnd[0] = 0x24; /*
  97. * bVSCBSignature : vendor-specific command
  98. * this value can change, but most(all ?) manufacturers
  99. * keep the cypress default : 0x24
  100. */
  101. srb->cmnd[1] = 0x24; /* bVSCBSubCommand : 0x24 for ATACB */
  102. srb->cmnd[3] = 0xff - 1; /*
  103. * features, sector count, lba low, lba med
  104. * lba high, device, command are valid
  105. */
  106. srb->cmnd[4] = 1; /* TransferBlockCount : 512 */
  107. if (save_cmnd[0] == ATA_16) {
  108. srb->cmnd[ 6] = save_cmnd[ 4]; /* features */
  109. srb->cmnd[ 7] = save_cmnd[ 6]; /* sector count */
  110. srb->cmnd[ 8] = save_cmnd[ 8]; /* lba low */
  111. srb->cmnd[ 9] = save_cmnd[10]; /* lba med */
  112. srb->cmnd[10] = save_cmnd[12]; /* lba high */
  113. srb->cmnd[11] = save_cmnd[13]; /* device */
  114. srb->cmnd[12] = save_cmnd[14]; /* command */
  115. if (save_cmnd[1] & 0x01) {/* extended bit set for LBA48 */
  116. /* this could be supported by atacb2 */
  117. if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
  118. || save_cmnd[11])
  119. goto invalid_fld;
  120. }
  121. } else { /* ATA12 */
  122. srb->cmnd[ 6] = save_cmnd[3]; /* features */
  123. srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
  124. srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
  125. srb->cmnd[ 9] = save_cmnd[6]; /* lba med */
  126. srb->cmnd[10] = save_cmnd[7]; /* lba high */
  127. srb->cmnd[11] = save_cmnd[8]; /* device */
  128. srb->cmnd[12] = save_cmnd[9]; /* command */
  129. }
  130. /* Filter SET_FEATURES - XFER MODE command */
  131. if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
  132. && (srb->cmnd[6] == SETFEATURES_XFER))
  133. goto invalid_fld;
  134. if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
  135. srb->cmnd[2] |= (1<<7); /* set IdentifyPacketDevice for these cmds */
  136. usb_stor_transparent_scsi_command(srb, us);
  137. /* if the device doesn't support ATACB */
  138. if (srb->result == SAM_STAT_CHECK_CONDITION &&
  139. memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
  140. sizeof(usb_stor_sense_invalidCDB)) == 0) {
  141. usb_stor_dbg(us, "cypress atacb not supported ???\n");
  142. goto end;
  143. }
  144. /*
  145. * if ck_cond flags is set, and there wasn't critical error,
  146. * build the special sense
  147. */
  148. if ((srb->result != (DID_ERROR << 16) &&
  149. srb->result != (DID_ABORT << 16)) &&
  150. save_cmnd[2] & 0x20) {
  151. struct scsi_eh_save ses;
  152. unsigned char regs[8];
  153. unsigned char *sb = srb->sense_buffer;
  154. unsigned char *desc = sb + 8;
  155. int tmp_result;
  156. /* build the command for reading the ATA registers */
  157. scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sizeof(regs));
  158. /*
  159. * we use the same command as before, but we set
  160. * the read taskfile bit, for not executing atacb command,
  161. * but reading register selected in srb->cmnd[4]
  162. */
  163. srb->cmd_len = 16;
  164. srb->cmnd = ses.cmnd;
  165. srb->cmnd[2] = 1;
  166. usb_stor_transparent_scsi_command(srb, us);
  167. memcpy(regs, srb->sense_buffer, sizeof(regs));
  168. tmp_result = srb->result;
  169. scsi_eh_restore_cmnd(srb, &ses);
  170. /* we fail to get registers, report invalid command */
  171. if (tmp_result != SAM_STAT_GOOD)
  172. goto invalid_fld;
  173. /* build the sense */
  174. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  175. /* set sk, asc for a good command */
  176. sb[1] = RECOVERED_ERROR;
  177. sb[2] = 0; /* ATA PASS THROUGH INFORMATION AVAILABLE */
  178. sb[3] = 0x1D;
  179. /*
  180. * XXX we should generate sk, asc, ascq from status and error
  181. * regs
  182. * (see 11.1 Error translation ATA device error to SCSI error
  183. * map, and ata_to_sense_error from libata.)
  184. */
  185. /* Sense data is current and format is descriptor. */
  186. sb[0] = 0x72;
  187. desc[0] = 0x09; /* ATA_RETURN_DESCRIPTOR */
  188. /* set length of additional sense data */
  189. sb[7] = 14;
  190. desc[1] = 12;
  191. /* Copy registers into sense buffer. */
  192. desc[ 2] = 0x00;
  193. desc[ 3] = regs[1]; /* features */
  194. desc[ 5] = regs[2]; /* sector count */
  195. desc[ 7] = regs[3]; /* lba low */
  196. desc[ 9] = regs[4]; /* lba med */
  197. desc[11] = regs[5]; /* lba high */
  198. desc[12] = regs[6]; /* device */
  199. desc[13] = regs[7]; /* command */
  200. srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  201. }
  202. goto end;
  203. invalid_fld:
  204. srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  205. memcpy(srb->sense_buffer,
  206. usb_stor_sense_invalidCDB,
  207. sizeof(usb_stor_sense_invalidCDB));
  208. end:
  209. memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
  210. if (srb->cmnd[0] == ATA_12)
  211. srb->cmd_len = 12;
  212. }
  213. static struct scsi_host_template cypress_host_template;
  214. static int cypress_probe(struct usb_interface *intf,
  215. const struct usb_device_id *id)
  216. {
  217. struct us_data *us;
  218. int result;
  219. struct usb_device *device;
  220. result = usb_stor_probe1(&us, intf, id,
  221. (id - cypress_usb_ids) + cypress_unusual_dev_list,
  222. &cypress_host_template);
  223. if (result)
  224. return result;
  225. /*
  226. * Among CY7C68300 chips, the A revision does not support Cypress ATACB
  227. * Filter out this revision from EEPROM default descriptor values
  228. */
  229. device = interface_to_usbdev(intf);
  230. if (device->descriptor.iManufacturer != 0x38 ||
  231. device->descriptor.iProduct != 0x4e ||
  232. device->descriptor.iSerialNumber != 0x64) {
  233. us->protocol_name = "Transparent SCSI with Cypress ATACB";
  234. us->proto_handler = cypress_atacb_passthrough;
  235. } else {
  236. us->protocol_name = "Transparent SCSI";
  237. us->proto_handler = usb_stor_transparent_scsi_command;
  238. }
  239. result = usb_stor_probe2(us);
  240. return result;
  241. }
  242. static struct usb_driver cypress_driver = {
  243. .name = DRV_NAME,
  244. .probe = cypress_probe,
  245. .disconnect = usb_stor_disconnect,
  246. .suspend = usb_stor_suspend,
  247. .resume = usb_stor_resume,
  248. .reset_resume = usb_stor_reset_resume,
  249. .pre_reset = usb_stor_pre_reset,
  250. .post_reset = usb_stor_post_reset,
  251. .id_table = cypress_usb_ids,
  252. .soft_unbind = 1,
  253. .no_dynamic_id = 1,
  254. };
  255. module_usb_stor_driver(cypress_driver, cypress_host_template, DRV_NAME);