freecom.c 13 KB

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