freecom.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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/module.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. MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor");
  36. MODULE_AUTHOR("David Brown <usb-storage@davidb.org>");
  37. MODULE_LICENSE("GPL");
  38. #ifdef CONFIG_USB_STORAGE_DEBUG
  39. static void pdump(struct us_data *us, void *ibuffer, int length);
  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 init_freecom(struct us_data *us);
  94. /*
  95. * The table of devices
  96. */
  97. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  98. vendorName, productName, useProtocol, useTransport, \
  99. initFunction, flags) \
  100. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  101. .driver_info = (flags) }
  102. static struct usb_device_id freecom_usb_ids[] = {
  103. # include "unusual_freecom.h"
  104. { } /* Terminating entry */
  105. };
  106. MODULE_DEVICE_TABLE(usb, freecom_usb_ids);
  107. #undef UNUSUAL_DEV
  108. /*
  109. * The flags table
  110. */
  111. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  112. vendor_name, product_name, use_protocol, use_transport, \
  113. init_function, Flags) \
  114. { \
  115. .vendorName = vendor_name, \
  116. .productName = product_name, \
  117. .useProtocol = use_protocol, \
  118. .useTransport = use_transport, \
  119. .initFunction = init_function, \
  120. }
  121. static struct us_unusual_dev freecom_unusual_dev_list[] = {
  122. # include "unusual_freecom.h"
  123. { } /* Terminating entry */
  124. };
  125. #undef UNUSUAL_DEV
  126. static int
  127. freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
  128. unsigned int ipipe, unsigned int opipe, int count)
  129. {
  130. struct freecom_xfer_wrap *fxfr =
  131. (struct freecom_xfer_wrap *) us->iobuf;
  132. int result;
  133. fxfr->Type = FCM_PACKET_INPUT | 0x00;
  134. fxfr->Timeout = 0; /* Short timeout for debugging. */
  135. fxfr->Count = cpu_to_le32 (count);
  136. memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
  137. usb_stor_dbg(us, "Read data Freecom! (c=%d)\n", count);
  138. /* Issue the transfer command. */
  139. result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
  140. FCM_PACKET_LENGTH, NULL);
  141. if (result != USB_STOR_XFER_GOOD) {
  142. usb_stor_dbg(us, "Freecom readdata transport error\n");
  143. return USB_STOR_TRANSPORT_ERROR;
  144. }
  145. /* Now transfer all of our blocks. */
  146. usb_stor_dbg(us, "Start of read\n");
  147. result = usb_stor_bulk_srb(us, ipipe, srb);
  148. usb_stor_dbg(us, "freecom_readdata done!\n");
  149. if (result > USB_STOR_XFER_SHORT)
  150. return USB_STOR_TRANSPORT_ERROR;
  151. return USB_STOR_TRANSPORT_GOOD;
  152. }
  153. static int
  154. freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
  155. int unsigned ipipe, unsigned int opipe, int count)
  156. {
  157. struct freecom_xfer_wrap *fxfr =
  158. (struct freecom_xfer_wrap *) us->iobuf;
  159. int result;
  160. fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
  161. fxfr->Timeout = 0; /* Short timeout for debugging. */
  162. fxfr->Count = cpu_to_le32 (count);
  163. memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
  164. usb_stor_dbg(us, "Write data Freecom! (c=%d)\n", count);
  165. /* Issue the transfer command. */
  166. result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
  167. FCM_PACKET_LENGTH, NULL);
  168. if (result != USB_STOR_XFER_GOOD) {
  169. usb_stor_dbg(us, "Freecom writedata transport error\n");
  170. return USB_STOR_TRANSPORT_ERROR;
  171. }
  172. /* Now transfer all of our blocks. */
  173. usb_stor_dbg(us, "Start of write\n");
  174. result = usb_stor_bulk_srb(us, opipe, srb);
  175. usb_stor_dbg(us, "freecom_writedata done!\n");
  176. if (result > USB_STOR_XFER_SHORT)
  177. return USB_STOR_TRANSPORT_ERROR;
  178. return USB_STOR_TRANSPORT_GOOD;
  179. }
  180. /*
  181. * Transport for the Freecom USB/IDE adaptor.
  182. *
  183. */
  184. static int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
  185. {
  186. struct freecom_cb_wrap *fcb;
  187. struct freecom_status *fst;
  188. unsigned int ipipe, opipe; /* We need both pipes. */
  189. int result;
  190. unsigned int partial;
  191. int length;
  192. fcb = (struct freecom_cb_wrap *) us->iobuf;
  193. fst = (struct freecom_status *) us->iobuf;
  194. usb_stor_dbg(us, "Freecom TRANSPORT STARTED\n");
  195. /* Get handles for both transports. */
  196. opipe = us->send_bulk_pipe;
  197. ipipe = us->recv_bulk_pipe;
  198. /* The ATAPI Command always goes out first. */
  199. fcb->Type = FCM_PACKET_ATAPI | 0x00;
  200. fcb->Timeout = 0;
  201. memcpy (fcb->Atapi, srb->cmnd, 12);
  202. memset (fcb->Filler, 0, sizeof (fcb->Filler));
  203. US_DEBUG(pdump(us, srb->cmnd, 12));
  204. /* Send it out. */
  205. result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
  206. FCM_PACKET_LENGTH, NULL);
  207. /* The Freecom device will only fail if there is something wrong in
  208. * USB land. It returns the status in its own registers, which
  209. * come back in the bulk pipe. */
  210. if (result != USB_STOR_XFER_GOOD) {
  211. usb_stor_dbg(us, "freecom transport error\n");
  212. return USB_STOR_TRANSPORT_ERROR;
  213. }
  214. /* There are times we can optimize out this status read, but it
  215. * doesn't hurt us to always do it now. */
  216. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  217. FCM_STATUS_PACKET_LENGTH, &partial);
  218. usb_stor_dbg(us, "foo Status result %d %u\n", result, partial);
  219. if (result != USB_STOR_XFER_GOOD)
  220. return USB_STOR_TRANSPORT_ERROR;
  221. US_DEBUG(pdump(us, (void *)fst, partial));
  222. /* The firmware will time-out commands after 20 seconds. Some commands
  223. * can legitimately take longer than this, so we use a different
  224. * command that only waits for the interrupt and then sends status,
  225. * without having to send a new ATAPI command to the device.
  226. *
  227. * NOTE: There is some indication that a data transfer after a timeout
  228. * may not work, but that is a condition that should never happen.
  229. */
  230. while (fst->Status & FCM_STATUS_BUSY) {
  231. usb_stor_dbg(us, "20 second USB/ATAPI bridge TIMEOUT occurred!\n");
  232. usb_stor_dbg(us, "fst->Status is %x\n", fst->Status);
  233. /* Get the status again */
  234. fcb->Type = FCM_PACKET_STATUS;
  235. fcb->Timeout = 0;
  236. memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
  237. memset (fcb->Filler, 0, sizeof (fcb->Filler));
  238. /* Send it out. */
  239. result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
  240. FCM_PACKET_LENGTH, NULL);
  241. /* The Freecom device will only fail if there is something
  242. * wrong in USB land. It returns the status in its own
  243. * registers, which come back in the bulk pipe.
  244. */
  245. if (result != USB_STOR_XFER_GOOD) {
  246. usb_stor_dbg(us, "freecom transport error\n");
  247. return USB_STOR_TRANSPORT_ERROR;
  248. }
  249. /* get the data */
  250. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  251. FCM_STATUS_PACKET_LENGTH, &partial);
  252. usb_stor_dbg(us, "bar Status result %d %u\n", result, partial);
  253. if (result != USB_STOR_XFER_GOOD)
  254. return USB_STOR_TRANSPORT_ERROR;
  255. US_DEBUG(pdump(us, (void *)fst, partial));
  256. }
  257. if (partial != 4)
  258. return USB_STOR_TRANSPORT_ERROR;
  259. if ((fst->Status & 1) != 0) {
  260. usb_stor_dbg(us, "operation failed\n");
  261. return USB_STOR_TRANSPORT_FAILED;
  262. }
  263. /* The device might not have as much data available as we
  264. * requested. If you ask for more than the device has, this reads
  265. * and such will hang. */
  266. usb_stor_dbg(us, "Device indicates that it has %d bytes available\n",
  267. le16_to_cpu(fst->Count));
  268. usb_stor_dbg(us, "SCSI requested %d\n", scsi_bufflen(srb));
  269. /* Find the length we desire to read. */
  270. switch (srb->cmnd[0]) {
  271. case INQUIRY:
  272. case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
  273. case MODE_SENSE:
  274. case MODE_SENSE_10:
  275. length = le16_to_cpu(fst->Count);
  276. break;
  277. default:
  278. length = scsi_bufflen(srb);
  279. }
  280. /* verify that this amount is legal */
  281. if (length > scsi_bufflen(srb)) {
  282. length = scsi_bufflen(srb);
  283. usb_stor_dbg(us, "Truncating request to match buffer length: %d\n",
  284. length);
  285. }
  286. /* What we do now depends on what direction the data is supposed to
  287. * move in. */
  288. switch (us->srb->sc_data_direction) {
  289. case DMA_FROM_DEVICE:
  290. /* catch bogus "read 0 length" case */
  291. if (!length)
  292. break;
  293. /* Make sure that the status indicates that the device
  294. * wants data as well. */
  295. if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
  296. usb_stor_dbg(us, "SCSI wants data, drive doesn't have any\n");
  297. return USB_STOR_TRANSPORT_FAILED;
  298. }
  299. result = freecom_readdata (srb, us, ipipe, opipe, length);
  300. if (result != USB_STOR_TRANSPORT_GOOD)
  301. return result;
  302. usb_stor_dbg(us, "Waiting for status\n");
  303. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  304. FCM_PACKET_LENGTH, &partial);
  305. US_DEBUG(pdump(us, (void *)fst, partial));
  306. if (partial != 4 || result > USB_STOR_XFER_SHORT)
  307. return USB_STOR_TRANSPORT_ERROR;
  308. if ((fst->Status & ERR_STAT) != 0) {
  309. usb_stor_dbg(us, "operation failed\n");
  310. return USB_STOR_TRANSPORT_FAILED;
  311. }
  312. if ((fst->Reason & 3) != 3) {
  313. usb_stor_dbg(us, "Drive seems still hungry\n");
  314. return USB_STOR_TRANSPORT_FAILED;
  315. }
  316. usb_stor_dbg(us, "Transfer happy\n");
  317. break;
  318. case DMA_TO_DEVICE:
  319. /* catch bogus "write 0 length" case */
  320. if (!length)
  321. break;
  322. /* Make sure the status indicates that the device wants to
  323. * send us data. */
  324. /* !!IMPLEMENT!! */
  325. result = freecom_writedata (srb, us, ipipe, opipe, length);
  326. if (result != USB_STOR_TRANSPORT_GOOD)
  327. return result;
  328. usb_stor_dbg(us, "Waiting for status\n");
  329. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  330. FCM_PACKET_LENGTH, &partial);
  331. if (partial != 4 || result > USB_STOR_XFER_SHORT)
  332. return USB_STOR_TRANSPORT_ERROR;
  333. if ((fst->Status & ERR_STAT) != 0) {
  334. usb_stor_dbg(us, "operation failed\n");
  335. return USB_STOR_TRANSPORT_FAILED;
  336. }
  337. if ((fst->Reason & 3) != 3) {
  338. usb_stor_dbg(us, "Drive seems still hungry\n");
  339. return USB_STOR_TRANSPORT_FAILED;
  340. }
  341. usb_stor_dbg(us, "Transfer happy\n");
  342. break;
  343. case DMA_NONE:
  344. /* Easy, do nothing. */
  345. break;
  346. default:
  347. /* should never hit here -- filtered in usb.c */
  348. usb_stor_dbg(us, "freecom unimplemented direction: %d\n",
  349. us->srb->sc_data_direction);
  350. /* Return fail, SCSI seems to handle this better. */
  351. return USB_STOR_TRANSPORT_FAILED;
  352. break;
  353. }
  354. return USB_STOR_TRANSPORT_GOOD;
  355. }
  356. static int init_freecom(struct us_data *us)
  357. {
  358. int result;
  359. char *buffer = us->iobuf;
  360. /* The DMA-mapped I/O buffer is 64 bytes long, just right for
  361. * all our packets. No need to allocate any extra buffer space.
  362. */
  363. result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
  364. 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
  365. buffer[32] = '\0';
  366. usb_stor_dbg(us, "String returned from FC init is: %s\n", buffer);
  367. /* Special thanks to the people at Freecom for providing me with
  368. * this "magic sequence", which they use in their Windows and MacOS
  369. * drivers to make sure that all the attached perhiperals are
  370. * properly reset.
  371. */
  372. /* send reset */
  373. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  374. 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
  375. usb_stor_dbg(us, "result from activate reset is %d\n", result);
  376. /* wait 250ms */
  377. mdelay(250);
  378. /* clear reset */
  379. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  380. 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
  381. usb_stor_dbg(us, "result from clear reset is %d\n", result);
  382. /* wait 3 seconds */
  383. mdelay(3 * 1000);
  384. return USB_STOR_TRANSPORT_GOOD;
  385. }
  386. static int usb_stor_freecom_reset(struct us_data *us)
  387. {
  388. printk (KERN_CRIT "freecom reset called\n");
  389. /* We don't really have this feature. */
  390. return FAILED;
  391. }
  392. #ifdef CONFIG_USB_STORAGE_DEBUG
  393. static void pdump(struct us_data *us, void *ibuffer, int length)
  394. {
  395. static char line[80];
  396. int offset = 0;
  397. unsigned char *buffer = (unsigned char *) ibuffer;
  398. int i, j;
  399. int from, base;
  400. offset = 0;
  401. for (i = 0; i < length; i++) {
  402. if ((i & 15) == 0) {
  403. if (i > 0) {
  404. offset += sprintf (line+offset, " - ");
  405. for (j = i - 16; j < i; j++) {
  406. if (buffer[j] >= 32 && buffer[j] <= 126)
  407. line[offset++] = buffer[j];
  408. else
  409. line[offset++] = '.';
  410. }
  411. line[offset] = 0;
  412. usb_stor_dbg(us, "%s\n", line);
  413. offset = 0;
  414. }
  415. offset += sprintf (line+offset, "%08x:", i);
  416. } else if ((i & 7) == 0) {
  417. offset += sprintf (line+offset, " -");
  418. }
  419. offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
  420. }
  421. /* Add the last "chunk" of data. */
  422. from = (length - 1) % 16;
  423. base = ((length - 1) / 16) * 16;
  424. for (i = from + 1; i < 16; i++)
  425. offset += sprintf (line+offset, " ");
  426. if (from < 8)
  427. offset += sprintf (line+offset, " ");
  428. offset += sprintf (line+offset, " - ");
  429. for (i = 0; i <= from; i++) {
  430. if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
  431. line[offset++] = buffer[base+i];
  432. else
  433. line[offset++] = '.';
  434. }
  435. line[offset] = 0;
  436. usb_stor_dbg(us, "%s\n", line);
  437. offset = 0;
  438. }
  439. #endif
  440. static int freecom_probe(struct usb_interface *intf,
  441. const struct usb_device_id *id)
  442. {
  443. struct us_data *us;
  444. int result;
  445. result = usb_stor_probe1(&us, intf, id,
  446. (id - freecom_usb_ids) + freecom_unusual_dev_list);
  447. if (result)
  448. return result;
  449. us->transport_name = "Freecom";
  450. us->transport = freecom_transport;
  451. us->transport_reset = usb_stor_freecom_reset;
  452. us->max_lun = 0;
  453. result = usb_stor_probe2(us);
  454. return result;
  455. }
  456. static struct usb_driver freecom_driver = {
  457. .name = "ums-freecom",
  458. .probe = freecom_probe,
  459. .disconnect = usb_stor_disconnect,
  460. .suspend = usb_stor_suspend,
  461. .resume = usb_stor_resume,
  462. .reset_resume = usb_stor_reset_resume,
  463. .pre_reset = usb_stor_pre_reset,
  464. .post_reset = usb_stor_post_reset,
  465. .id_table = freecom_usb_ids,
  466. .soft_unbind = 1,
  467. .no_dynamic_id = 1,
  468. };
  469. module_usb_driver(freecom_driver);