freecom.c 15 KB

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