freecom.c 13 KB

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