freecom.c 13 KB

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