freecom.c 15 KB

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