freecom.c 16 KB

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