freecom.c 16 KB

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