freecom.c 15 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 (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 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)|(USB_US_TYPE_STOR<<24) }
  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. US_DEBUGP("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. US_DEBUGP ("Freecom readdata transport error\n");
  143. return USB_STOR_TRANSPORT_ERROR;
  144. }
  145. /* Now transfer all of our blocks. */
  146. US_DEBUGP("Start of read\n");
  147. result = usb_stor_bulk_srb(us, ipipe, srb);
  148. US_DEBUGP("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. US_DEBUGP("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. US_DEBUGP ("Freecom writedata transport error\n");
  170. return USB_STOR_TRANSPORT_ERROR;
  171. }
  172. /* Now transfer all of our blocks. */
  173. US_DEBUGP("Start of write\n");
  174. result = usb_stor_bulk_srb(us, opipe, srb);
  175. US_DEBUGP("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. US_DEBUGP("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 (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. US_DEBUGP ("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. US_DEBUGP("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 ((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. US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
  232. US_DEBUGP("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. US_DEBUGP ("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. US_DEBUGP("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 ((void *) fst, partial));
  256. }
  257. if (partial != 4)
  258. return USB_STOR_TRANSPORT_ERROR;
  259. if ((fst->Status & 1) != 0) {
  260. US_DEBUGP("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. US_DEBUGP("Device indicates that it has %d bytes available\n",
  267. le16_to_cpu (fst->Count));
  268. US_DEBUGP("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. US_DEBUGP("Truncating request to match buffer length: %d\n", length);
  284. }
  285. /* What we do now depends on what direction the data is supposed to
  286. * move in. */
  287. switch (us->srb->sc_data_direction) {
  288. case DMA_FROM_DEVICE:
  289. /* catch bogus "read 0 length" case */
  290. if (!length)
  291. break;
  292. /* Make sure that the status indicates that the device
  293. * wants data as well. */
  294. if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
  295. US_DEBUGP("SCSI wants data, drive doesn't have any\n");
  296. return USB_STOR_TRANSPORT_FAILED;
  297. }
  298. result = freecom_readdata (srb, us, ipipe, opipe, length);
  299. if (result != USB_STOR_TRANSPORT_GOOD)
  300. return result;
  301. US_DEBUGP("FCM: Waiting for status\n");
  302. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  303. FCM_PACKET_LENGTH, &partial);
  304. US_DEBUG(pdump ((void *) fst, partial));
  305. if (partial != 4 || result > USB_STOR_XFER_SHORT)
  306. return USB_STOR_TRANSPORT_ERROR;
  307. if ((fst->Status & ERR_STAT) != 0) {
  308. US_DEBUGP("operation failed\n");
  309. return USB_STOR_TRANSPORT_FAILED;
  310. }
  311. if ((fst->Reason & 3) != 3) {
  312. US_DEBUGP("Drive seems still hungry\n");
  313. return USB_STOR_TRANSPORT_FAILED;
  314. }
  315. US_DEBUGP("Transfer happy\n");
  316. break;
  317. case DMA_TO_DEVICE:
  318. /* catch bogus "write 0 length" case */
  319. if (!length)
  320. break;
  321. /* Make sure the status indicates that the device wants to
  322. * send us data. */
  323. /* !!IMPLEMENT!! */
  324. result = freecom_writedata (srb, us, ipipe, opipe, length);
  325. if (result != USB_STOR_TRANSPORT_GOOD)
  326. return result;
  327. US_DEBUGP("FCM: Waiting for status\n");
  328. result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
  329. FCM_PACKET_LENGTH, &partial);
  330. if (partial != 4 || result > USB_STOR_XFER_SHORT)
  331. return USB_STOR_TRANSPORT_ERROR;
  332. if ((fst->Status & ERR_STAT) != 0) {
  333. US_DEBUGP("operation failed\n");
  334. return USB_STOR_TRANSPORT_FAILED;
  335. }
  336. if ((fst->Reason & 3) != 3) {
  337. US_DEBUGP("Drive seems still hungry\n");
  338. return USB_STOR_TRANSPORT_FAILED;
  339. }
  340. US_DEBUGP("Transfer happy\n");
  341. break;
  342. case DMA_NONE:
  343. /* Easy, do nothing. */
  344. break;
  345. default:
  346. /* should never hit here -- filtered in usb.c */
  347. US_DEBUGP ("freecom unimplemented direction: %d\n",
  348. us->srb->sc_data_direction);
  349. /* Return fail, SCSI seems to handle this better. */
  350. return USB_STOR_TRANSPORT_FAILED;
  351. break;
  352. }
  353. return USB_STOR_TRANSPORT_GOOD;
  354. }
  355. static int init_freecom(struct us_data *us)
  356. {
  357. int result;
  358. char *buffer = us->iobuf;
  359. /* The DMA-mapped I/O buffer is 64 bytes long, just right for
  360. * all our packets. No need to allocate any extra buffer space.
  361. */
  362. result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
  363. 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
  364. buffer[32] = '\0';
  365. US_DEBUGP("String returned from FC init is: %s\n", buffer);
  366. /* Special thanks to the people at Freecom for providing me with
  367. * this "magic sequence", which they use in their Windows and MacOS
  368. * drivers to make sure that all the attached perhiperals are
  369. * properly reset.
  370. */
  371. /* send reset */
  372. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  373. 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
  374. US_DEBUGP("result from activate reset is %d\n", result);
  375. /* wait 250ms */
  376. mdelay(250);
  377. /* clear reset */
  378. result = usb_stor_control_msg(us, us->send_ctrl_pipe,
  379. 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
  380. US_DEBUGP("result from clear reset is %d\n", result);
  381. /* wait 3 seconds */
  382. mdelay(3 * 1000);
  383. return USB_STOR_TRANSPORT_GOOD;
  384. }
  385. static int usb_stor_freecom_reset(struct us_data *us)
  386. {
  387. printk (KERN_CRIT "freecom reset called\n");
  388. /* We don't really have this feature. */
  389. return FAILED;
  390. }
  391. #ifdef CONFIG_USB_STORAGE_DEBUG
  392. static void pdump (void *ibuffer, int length)
  393. {
  394. static char line[80];
  395. int offset = 0;
  396. unsigned char *buffer = (unsigned char *) ibuffer;
  397. int i, j;
  398. int from, base;
  399. offset = 0;
  400. for (i = 0; i < length; i++) {
  401. if ((i & 15) == 0) {
  402. if (i > 0) {
  403. offset += sprintf (line+offset, " - ");
  404. for (j = i - 16; j < i; j++) {
  405. if (buffer[j] >= 32 && buffer[j] <= 126)
  406. line[offset++] = buffer[j];
  407. else
  408. line[offset++] = '.';
  409. }
  410. line[offset] = 0;
  411. US_DEBUGP("%s\n", line);
  412. offset = 0;
  413. }
  414. offset += sprintf (line+offset, "%08x:", i);
  415. } else if ((i & 7) == 0) {
  416. offset += sprintf (line+offset, " -");
  417. }
  418. offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
  419. }
  420. /* Add the last "chunk" of data. */
  421. from = (length - 1) % 16;
  422. base = ((length - 1) / 16) * 16;
  423. for (i = from + 1; i < 16; i++)
  424. offset += sprintf (line+offset, " ");
  425. if (from < 8)
  426. offset += sprintf (line+offset, " ");
  427. offset += sprintf (line+offset, " - ");
  428. for (i = 0; i <= from; i++) {
  429. if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
  430. line[offset++] = buffer[base+i];
  431. else
  432. line[offset++] = '.';
  433. }
  434. line[offset] = 0;
  435. US_DEBUGP("%s\n", line);
  436. offset = 0;
  437. }
  438. #endif
  439. static int freecom_probe(struct usb_interface *intf,
  440. const struct usb_device_id *id)
  441. {
  442. struct us_data *us;
  443. int result;
  444. result = usb_stor_probe1(&us, intf, id,
  445. (id - freecom_usb_ids) + freecom_unusual_dev_list);
  446. if (result)
  447. return result;
  448. us->transport_name = "Freecom";
  449. us->transport = freecom_transport;
  450. us->transport_reset = usb_stor_freecom_reset;
  451. us->max_lun = 0;
  452. result = usb_stor_probe2(us);
  453. return result;
  454. }
  455. static struct usb_driver freecom_driver = {
  456. .name = "ums-freecom",
  457. .probe = freecom_probe,
  458. .disconnect = usb_stor_disconnect,
  459. .suspend = usb_stor_suspend,
  460. .resume = usb_stor_resume,
  461. .reset_resume = usb_stor_reset_resume,
  462. .pre_reset = usb_stor_pre_reset,
  463. .post_reset = usb_stor_post_reset,
  464. .id_table = freecom_usb_ids,
  465. .soft_unbind = 1,
  466. .no_dynamic_id = 1,
  467. };
  468. module_usb_driver(freecom_driver);