freecom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* Driver for Freecom USB/IDE adaptor
  2. *
  3. * Freecom v0.1:
  4. *
  5. * First release
  6. *
  7. * Current development and maintenance by:
  8. * (C) 2000 David Brown <usb-storage@davidb.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * This driver was developed with information provided in FREECOM's USB
  25. * Programmers Reference Guide. For further information contact Freecom
  26. * (http://www.freecom.de/)
  27. */
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include "usb.h"
  31. #include "transport.h"
  32. #include "protocol.h"
  33. #include "debug.h"
  34. #include "freecom.h"
  35. #ifdef CONFIG_USB_STORAGE_DEBUG
  36. static void pdump (void *, int);
  37. #endif
  38. /* Bits of HD_STATUS */
  39. #define ERR_STAT 0x01
  40. #define DRQ_STAT 0x08
  41. /* All of the outgoing packets are 64 bytes long. */
  42. struct freecom_cb_wrap {
  43. u8 Type; /* Command type. */
  44. u8 Timeout; /* Timeout in seconds. */
  45. u8 Atapi[12]; /* An ATAPI packet. */
  46. u8 Filler[50]; /* Padding Data. */
  47. };
  48. struct freecom_xfer_wrap {
  49. u8 Type; /* Command type. */
  50. u8 Timeout; /* Timeout in seconds. */
  51. __le32 Count; /* Number of bytes to transfer. */
  52. u8 Pad[58];
  53. } __attribute__ ((packed));
  54. struct freecom_ide_out {
  55. u8 Type; /* Type + IDE register. */
  56. u8 Pad;
  57. __le16 Value; /* Value to write. */
  58. u8 Pad2[60];
  59. };
  60. struct freecom_ide_in {
  61. u8 Type; /* Type | IDE register. */
  62. u8 Pad[63];
  63. };
  64. struct freecom_status {
  65. u8 Status;
  66. u8 Reason;
  67. __le16 Count;
  68. u8 Pad[60];
  69. };
  70. /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
  71. * register. */
  72. #define FCM_INT_STATUS 0x02 /* INDEX_STAT */
  73. #define FCM_STATUS_BUSY 0x80
  74. /* These are the packet types. The low bit indicates that this command
  75. * should wait for an interrupt. */
  76. #define FCM_PACKET_ATAPI 0x21
  77. #define FCM_PACKET_STATUS 0x20
  78. /* Receive data from the IDE interface. The ATAPI packet has already
  79. * waited, so the data should be immediately available. */
  80. #define FCM_PACKET_INPUT 0x81
  81. /* Send data to the IDE interface. */
  82. #define FCM_PACKET_OUTPUT 0x01
  83. /* Write a value to an ide register. Or the ide register to write after
  84. * munging the address a bit. */
  85. #define FCM_PACKET_IDE_WRITE 0x40
  86. #define FCM_PACKET_IDE_READ 0xC0
  87. /* All packets (except for status) are 64 bytes long. */
  88. #define FCM_PACKET_LENGTH 64
  89. #define FCM_STATUS_PACKET_LENGTH 4
  90. static int
  91. freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
  92. unsigned int ipipe, unsigned int opipe, int count)
  93. {
  94. struct freecom_xfer_wrap *fxfr =
  95. (struct freecom_xfer_wrap *) us->iobuf;
  96. int result;
  97. fxfr->Type = FCM_PACKET_INPUT | 0x00;
  98. fxfr->Timeout = 0; /* Short timeout for debugging. */
  99. fxfr->Count = cpu_to_le32 (count);
  100. memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
  101. US_DEBUGP("Read data Freecom! (c=%d)\n", count);
  102. /* Issue the transfer command. */
  103. result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
  104. FCM_PACKET_LENGTH, NULL);
  105. if (result != USB_STOR_XFER_GOOD) {
  106. US_DEBUGP ("Freecom readdata transport error\n");
  107. return USB_STOR_TRANSPORT_ERROR;
  108. }
  109. /* Now transfer all of our blocks. */
  110. US_DEBUGP("Start of read\n");
  111. result = usb_stor_bulk_srb(us, ipipe, srb);
  112. US_DEBUGP("freecom_readdata done!\n");
  113. if (result > USB_STOR_XFER_SHORT)
  114. return USB_STOR_TRANSPORT_ERROR;
  115. return USB_STOR_TRANSPORT_GOOD;
  116. }
  117. static int
  118. freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
  119. int unsigned ipipe, unsigned int opipe, int count)
  120. {
  121. struct freecom_xfer_wrap *fxfr =
  122. (struct freecom_xfer_wrap *) us->iobuf;
  123. int result;
  124. fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
  125. fxfr->Timeout = 0; /* Short timeout for debugging. */
  126. fxfr->Count = cpu_to_le32 (count);
  127. memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
  128. US_DEBUGP("Write data Freecom! (c=%d)\n", count);
  129. /* Issue the transfer command. */
  130. result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
  131. FCM_PACKET_LENGTH, NULL);
  132. if (result != USB_STOR_XFER_GOOD) {
  133. US_DEBUGP ("Freecom writedata transport error\n");
  134. return USB_STOR_TRANSPORT_ERROR;
  135. }
  136. /* Now transfer all of our blocks. */
  137. US_DEBUGP("Start of write\n");
  138. result = usb_stor_bulk_srb(us, opipe, srb);
  139. US_DEBUGP("freecom_writedata done!\n");
  140. if (result > USB_STOR_XFER_SHORT)
  141. return USB_STOR_TRANSPORT_ERROR;
  142. return USB_STOR_TRANSPORT_GOOD;
  143. }
  144. /*
  145. * Transport for the Freecom USB/IDE adaptor.
  146. *
  147. */
  148. int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
  149. {
  150. struct freecom_cb_wrap *fcb;
  151. struct freecom_status *fst;
  152. unsigned int ipipe, opipe; /* We need both pipes. */
  153. int result;
  154. unsigned int partial;
  155. int length;
  156. fcb = (struct freecom_cb_wrap *) us->iobuf;
  157. fst = (struct freecom_status *) us->iobuf;
  158. US_DEBUGP("Freecom TRANSPORT STARTED\n");
  159. /* Get handles for both transports. */
  160. opipe = us->send_bulk_pipe;
  161. ipipe = us->recv_bulk_pipe;
  162. /* The ATAPI Command always goes out first. */
  163. fcb->Type = FCM_PACKET_ATAPI | 0x00;
  164. fcb->Timeout = 0;
  165. memcpy (fcb->Atapi, srb->cmnd, 12);
  166. memset (fcb->Filler, 0, sizeof (fcb->Filler));
  167. US_DEBUG(pdump (srb->cmnd, 12));
  168. /* Send it out. */
  169. result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
  170. FCM_PACKET_LENGTH, NULL);
  171. /* The Freecom device will only fail if there is something wrong in
  172. * USB land. It returns the status in its own registers, which
  173. * come back in the bulk pipe. */
  174. if (result != USB_STOR_XFER_GOOD) {
  175. US_DEBUGP ("freecom transport error\n");
  176. return USB_STOR_TRANSPORT_ERROR;
  177. }
  178. /* There are times we can optimize out this status read, but it
  179. * doesn't hurt us to always do it now. */
  180. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  181. FCM_STATUS_PACKET_LENGTH, &partial);
  182. US_DEBUGP("foo Status result %d %u\n", result, partial);
  183. if (result != USB_STOR_XFER_GOOD)
  184. return USB_STOR_TRANSPORT_ERROR;
  185. US_DEBUG(pdump ((void *) fst, partial));
  186. /* The firmware will time-out commands after 20 seconds. Some commands
  187. * can legitimately take longer than this, so we use a different
  188. * command that only waits for the interrupt and then sends status,
  189. * without having to send a new ATAPI command to the device.
  190. *
  191. * NOTE: There is some indication that a data transfer after a timeout
  192. * may not work, but that is a condition that should never happen.
  193. */
  194. while (fst->Status & FCM_STATUS_BUSY) {
  195. US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
  196. US_DEBUGP("fst->Status is %x\n", fst->Status);
  197. /* Get the status again */
  198. fcb->Type = FCM_PACKET_STATUS;
  199. fcb->Timeout = 0;
  200. memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
  201. memset (fcb->Filler, 0, sizeof (fcb->Filler));
  202. /* Send it out. */
  203. result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
  204. FCM_PACKET_LENGTH, NULL);
  205. /* The Freecom device will only fail if there is something
  206. * wrong in USB land. It returns the status in its own
  207. * registers, which come back in the bulk pipe.
  208. */
  209. if (result != USB_STOR_XFER_GOOD) {
  210. US_DEBUGP ("freecom transport error\n");
  211. return USB_STOR_TRANSPORT_ERROR;
  212. }
  213. /* get the data */
  214. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  215. FCM_STATUS_PACKET_LENGTH, &partial);
  216. US_DEBUGP("bar Status result %d %u\n", result, partial);
  217. if (result != USB_STOR_XFER_GOOD)
  218. return USB_STOR_TRANSPORT_ERROR;
  219. US_DEBUG(pdump ((void *) fst, partial));
  220. }
  221. if (partial != 4)
  222. return USB_STOR_TRANSPORT_ERROR;
  223. if ((fst->Status & 1) != 0) {
  224. US_DEBUGP("operation failed\n");
  225. return USB_STOR_TRANSPORT_FAILED;
  226. }
  227. /* The device might not have as much data available as we
  228. * requested. If you ask for more than the device has, this reads
  229. * and such will hang. */
  230. US_DEBUGP("Device indicates that it has %d bytes available\n",
  231. le16_to_cpu (fst->Count));
  232. US_DEBUGP("SCSI requested %d\n", scsi_bufflen(srb));
  233. /* Find the length we desire to read. */
  234. switch (srb->cmnd[0]) {
  235. case INQUIRY:
  236. case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
  237. case MODE_SENSE:
  238. case MODE_SENSE_10:
  239. length = le16_to_cpu(fst->Count);
  240. break;
  241. default:
  242. length = scsi_bufflen(srb);
  243. }
  244. /* verify that this amount is legal */
  245. if (length > scsi_bufflen(srb)) {
  246. length = scsi_bufflen(srb);
  247. US_DEBUGP("Truncating request to match buffer length: %d\n", length);
  248. }
  249. /* What we do now depends on what direction the data is supposed to
  250. * move in. */
  251. switch (us->srb->sc_data_direction) {
  252. case DMA_FROM_DEVICE:
  253. /* catch bogus "read 0 length" case */
  254. if (!length)
  255. break;
  256. /* Make sure that the status indicates that the device
  257. * wants data as well. */
  258. if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
  259. US_DEBUGP("SCSI wants data, drive doesn't have any\n");
  260. return USB_STOR_TRANSPORT_FAILED;
  261. }
  262. result = freecom_readdata (srb, us, ipipe, opipe, length);
  263. if (result != USB_STOR_TRANSPORT_GOOD)
  264. return result;
  265. US_DEBUGP("FCM: Waiting for status\n");
  266. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  267. FCM_PACKET_LENGTH, &partial);
  268. US_DEBUG(pdump ((void *) fst, partial));
  269. if (partial != 4 || result > USB_STOR_XFER_SHORT)
  270. return USB_STOR_TRANSPORT_ERROR;
  271. if ((fst->Status & ERR_STAT) != 0) {
  272. US_DEBUGP("operation failed\n");
  273. return USB_STOR_TRANSPORT_FAILED;
  274. }
  275. if ((fst->Reason & 3) != 3) {
  276. US_DEBUGP("Drive seems still hungry\n");
  277. return USB_STOR_TRANSPORT_FAILED;
  278. }
  279. US_DEBUGP("Transfer happy\n");
  280. break;
  281. case DMA_TO_DEVICE:
  282. /* catch bogus "write 0 length" case */
  283. if (!length)
  284. break;
  285. /* Make sure the status indicates that the device wants to
  286. * send us data. */
  287. /* !!IMPLEMENT!! */
  288. result = freecom_writedata (srb, us, ipipe, opipe, length);
  289. if (result != USB_STOR_TRANSPORT_GOOD)
  290. return result;
  291. US_DEBUGP("FCM: Waiting for status\n");
  292. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  293. FCM_PACKET_LENGTH, &partial);
  294. if (partial != 4 || result > USB_STOR_XFER_SHORT)
  295. return USB_STOR_TRANSPORT_ERROR;
  296. if ((fst->Status & ERR_STAT) != 0) {
  297. US_DEBUGP("operation failed\n");
  298. return USB_STOR_TRANSPORT_FAILED;
  299. }
  300. if ((fst->Reason & 3) != 3) {
  301. US_DEBUGP("Drive seems still hungry\n");
  302. return USB_STOR_TRANSPORT_FAILED;
  303. }
  304. US_DEBUGP("Transfer happy\n");
  305. break;
  306. case DMA_NONE:
  307. /* Easy, do nothing. */
  308. break;
  309. default:
  310. /* should never hit here -- filtered in usb.c */
  311. US_DEBUGP ("freecom unimplemented direction: %d\n",
  312. us->srb->sc_data_direction);
  313. // Return fail, SCSI seems to handle this better.
  314. return USB_STOR_TRANSPORT_FAILED;
  315. break;
  316. }
  317. return USB_STOR_TRANSPORT_GOOD;
  318. }
  319. int
  320. freecom_init (struct us_data *us)
  321. {
  322. int result;
  323. char *buffer = us->iobuf;
  324. /* The DMA-mapped I/O buffer is 64 bytes long, just right for
  325. * all our packets. No need to allocate any extra buffer space.
  326. */
  327. result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
  328. 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
  329. buffer[32] = '\0';
  330. US_DEBUGP("String returned from FC init is: %s\n", buffer);
  331. /* Special thanks to the people at Freecom for providing me with
  332. * this "magic sequence", which they use in their Windows and MacOS
  333. * drivers to make sure that all the attached perhiperals are
  334. * properly reset.
  335. */
  336. /* send reset */
  337. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  338. 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
  339. US_DEBUGP("result from activate reset is %d\n", result);
  340. /* wait 250ms */
  341. mdelay(250);
  342. /* clear reset */
  343. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  344. 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
  345. US_DEBUGP("result from clear reset is %d\n", result);
  346. /* wait 3 seconds */
  347. mdelay(3 * 1000);
  348. return USB_STOR_TRANSPORT_GOOD;
  349. }
  350. int usb_stor_freecom_reset(struct us_data *us)
  351. {
  352. printk (KERN_CRIT "freecom reset called\n");
  353. /* We don't really have this feature. */
  354. return FAILED;
  355. }
  356. #ifdef CONFIG_USB_STORAGE_DEBUG
  357. static void pdump (void *ibuffer, int length)
  358. {
  359. static char line[80];
  360. int offset = 0;
  361. unsigned char *buffer = (unsigned char *) ibuffer;
  362. int i, j;
  363. int from, base;
  364. offset = 0;
  365. for (i = 0; i < length; i++) {
  366. if ((i & 15) == 0) {
  367. if (i > 0) {
  368. offset += sprintf (line+offset, " - ");
  369. for (j = i - 16; j < i; j++) {
  370. if (buffer[j] >= 32 && buffer[j] <= 126)
  371. line[offset++] = buffer[j];
  372. else
  373. line[offset++] = '.';
  374. }
  375. line[offset] = 0;
  376. US_DEBUGP("%s\n", line);
  377. offset = 0;
  378. }
  379. offset += sprintf (line+offset, "%08x:", i);
  380. }
  381. else if ((i & 7) == 0) {
  382. offset += sprintf (line+offset, " -");
  383. }
  384. offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
  385. }
  386. /* Add the last "chunk" of data. */
  387. from = (length - 1) % 16;
  388. base = ((length - 1) / 16) * 16;
  389. for (i = from + 1; i < 16; i++)
  390. offset += sprintf (line+offset, " ");
  391. if (from < 8)
  392. offset += sprintf (line+offset, " ");
  393. offset += sprintf (line+offset, " - ");
  394. for (i = 0; i <= from; i++) {
  395. if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
  396. line[offset++] = buffer[base+i];
  397. else
  398. line[offset++] = '.';
  399. }
  400. line[offset] = 0;
  401. US_DEBUGP("%s\n", line);
  402. offset = 0;
  403. }
  404. #endif