freecom.c 13 KB

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