shuttle_usbat.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:26k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* Driver for SCM Microsystems USB-ATAPI cable
  2.  *
  3.  * $Id: shuttle_usbat.c,v 1.15 2001/12/08 23:32:48 mdharm Exp $
  4.  *
  5.  * Current development and maintenance by:
  6.  *   (c) 2000, 2001 Robert Baruch (autophile@starband.net)
  7.  *
  8.  * Many originally ATAPI devices were slightly modified to meet the USB
  9.  * market by using some kind of translation from ATAPI to USB on the host,
  10.  * and the peripheral would translate from USB back to ATAPI.
  11.  *
  12.  * SCM Microsystems (www.scmmicro.com) makes a device, sold to OEM's only, 
  13.  * which does the USB-to-ATAPI conversion.  By obtaining the data sheet on
  14.  * their device under nondisclosure agreement, I have been able to write
  15.  * this driver for Linux.
  16.  *
  17.  * The chip used in the device can also be used for EPP and ISA translation
  18.  * as well. This driver is only guaranteed to work with the ATAPI
  19.  * translation.
  20.  *
  21.  * The only peripheral that I know of (as of 27 Mar 2001) that uses this
  22.  * device is the Hewlett-Packard 8200e/8210e/8230e CD-Writer Plus.
  23.  *
  24.  * This program is free software; you can redistribute it and/or modify it
  25.  * under the terms of the GNU General Public License as published by the
  26.  * Free Software Foundation; either version 2, or (at your option) any
  27.  * later version.
  28.  *
  29.  * This program is distributed in the hope that it will be useful, but
  30.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  31.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  32.  * General Public License for more details.
  33.  *
  34.  * You should have received a copy of the GNU General Public License along
  35.  * with this program; if not, write to the Free Software Foundation, Inc.,
  36.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  37.  */
  38. #include "transport.h"
  39. #include "protocol.h"
  40. #include "usb.h"
  41. #include "debug.h"
  42. #include "shuttle_usbat.h"
  43. #include <linux/sched.h>
  44. #include <linux/errno.h>
  45. #include <linux/slab.h>
  46. extern int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
  47. u8 request, u8 requesttype, u16 value, u16 index,
  48. void *data, u16 size);
  49. extern int usb_stor_bulk_msg(struct us_data *us, void *data, int pipe,
  50. unsigned int len, unsigned int *act_len);
  51. #define short_pack(LSB,MSB) ( ((u16)(LSB)) | ( ((u16)(MSB))<<8 ) )
  52. #define LSB_of(s) ((s)&0xFF)
  53. #define MSB_of(s) ((s)>>8)
  54. int transferred = 0;
  55. /*
  56.  * Send a control message and wait for the response.
  57.  *
  58.  * us - the pointer to the us_data structure for the device to use
  59.  *
  60.  * request - the URB Setup Packet's first 6 bytes. The first byte always
  61.  *  corresponds to the request type, and the second byte always corresponds
  62.  *  to the request.  The other 4 bytes do not correspond to value and index,
  63.  *  since they are used in a custom way by the SCM protocol.
  64.  *
  65.  * xfer_data - a buffer from which to get, or to which to store, any data
  66.  *  that gets send or received, respectively, with the URB. Even though
  67.  *  it looks like we allocate a buffer in this code for the data, xfer_data
  68.  *  must contain enough allocated space.
  69.  *
  70.  * xfer_len - the number of bytes to send or receive with the URB.
  71.  *
  72.  */
  73. static int usbat_send_control(struct us_data *us,
  74. int pipe,
  75. unsigned char request,
  76. unsigned char requesttype,
  77. unsigned short value,
  78. unsigned short index,
  79. unsigned char *xfer_data,
  80. unsigned int xfer_len) {
  81. int result;
  82. // Send the URB to the device and wait for a response.
  83. /* Why are request and request type reversed in this call? */
  84. result = usb_stor_control_msg(us, pipe,
  85. request, requesttype, value, index,
  86. xfer_data, xfer_len);
  87. // Check the return code for the command.
  88. if (result < 0) {
  89. /* if the command was aborted, indicate that */
  90. if (result == -ENOENT)
  91. return USB_STOR_TRANSPORT_ABORTED;
  92. /* a stall is a fatal condition from the device */
  93. if (result == -EPIPE) {
  94. US_DEBUGP("-- Stall on control pipe. Clearingn");
  95. result = usb_clear_halt(us->pusb_dev, pipe);
  96. US_DEBUGP("-- usb_clear_halt() returns %dn", result);
  97. return USB_STOR_TRANSPORT_FAILED;
  98. }
  99. /* Uh oh... serious problem here */
  100. return USB_STOR_TRANSPORT_ERROR;
  101. }
  102. return USB_STOR_TRANSPORT_GOOD;
  103. }
  104. static int usbat_raw_bulk(struct us_data *us, 
  105. int direction,
  106. unsigned char *data,
  107. unsigned short len) {
  108. int result;
  109. int act_len;
  110. int pipe;
  111. if (direction == SCSI_DATA_READ)
  112. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  113. else
  114. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  115. result = usb_stor_bulk_msg(us, data, pipe, len, &act_len);
  116.         /* if we stall, we need to clear it before we go on */
  117.         if (result == -EPIPE) {
  118.                 US_DEBUGP("EPIPE: clearing endpoint halt for"
  119. " pipe 0x%x, stalled at %d bytesn",
  120. pipe, act_len);
  121.                 usb_clear_halt(us->pusb_dev, pipe);
  122.         }
  123. if (result) {
  124.                 /* NAK - that means we've retried a few times already */
  125.                 if (result == -ETIMEDOUT) {
  126.                         US_DEBUGP("usbat_raw_bulk():"
  127. " device NAKedn");
  128.                         return US_BULK_TRANSFER_FAILED;
  129.                 }
  130.                 /* -ENOENT -- we canceled this transfer */
  131.                 if (result == -ENOENT) {
  132.                         US_DEBUGP("usbat_raw_bulk():"
  133. " transfer abortedn");
  134.                         return US_BULK_TRANSFER_ABORTED;
  135.                 }
  136. if (result == -EPIPE) {
  137. US_DEBUGP("usbat_raw_bulk():"
  138. " output pipe stalledn");
  139. return US_BULK_TRANSFER_SHORT;
  140. }
  141.                 /* the catch-all case */
  142.                 US_DEBUGP("us_transfer_partial(): unknown errorn");
  143.                 return US_BULK_TRANSFER_FAILED;
  144.         }
  145. if (act_len != len) {
  146. US_DEBUGP("Warning: Transferred only %d bytesn",
  147. act_len);
  148. return US_BULK_TRANSFER_SHORT;
  149. }
  150. US_DEBUGP("Transferred %s %d of %d bytesn", 
  151. direction==SCSI_DATA_READ ? "in" : "out", act_len, len);
  152. return US_BULK_TRANSFER_GOOD;
  153. }
  154. /*
  155.  * Note: direction must be set if command_len == 0.
  156.  */
  157. static int usbat_bulk_transport(struct us_data *us,
  158.   unsigned char *command,
  159.   unsigned short command_len,
  160.   int direction,
  161.   unsigned char *data,
  162.   unsigned short len,
  163.   int use_sg) {
  164. int result = USB_STOR_TRANSPORT_GOOD;
  165. int transferred = 0;
  166. int i;
  167. struct scatterlist *sg;
  168. if (len==0)
  169. return USB_STOR_TRANSPORT_GOOD;
  170. /* transfer the data payload for the command, if there is any */
  171. if (command_len != 0)
  172. direction = (command[0]&0x80) ? SCSI_DATA_READ :
  173. SCSI_DATA_WRITE;
  174. if (!use_sg)
  175. result = usbat_raw_bulk(us, direction, data, len);
  176. else {
  177. sg = (struct scatterlist *)data;
  178. for (i=0; i<use_sg && transferred<len; i++) {
  179. result = usbat_raw_bulk(us, direction,
  180. sg[i].address, 
  181. len-transferred > sg[i].length ?
  182. sg[i].length : len-transferred);
  183. if (result!=US_BULK_TRANSFER_GOOD)
  184. break;
  185. transferred += sg[i].length;
  186. }
  187. }
  188. return result;
  189. }
  190. int usbat_read(struct us_data *us,
  191.      unsigned char access,
  192.      unsigned char reg, 
  193.      unsigned char *content) {
  194. int result;
  195. result = usbat_send_control(us,
  196. usb_rcvctrlpipe(us->pusb_dev,0),
  197. access,
  198. 0xC0,
  199. (u16)reg,
  200. 0,
  201. content,
  202. 1);
  203. return result;
  204. }
  205. int usbat_write(struct us_data *us,
  206.      unsigned char access,
  207.      unsigned char reg, 
  208.      unsigned char content) {
  209. int result;
  210. result = usbat_send_control(us,
  211. usb_sndctrlpipe(us->pusb_dev,0),
  212. access|0x01,
  213. 0x40,
  214. short_pack(reg, content),
  215. 0,
  216. NULL,
  217. 0);
  218. return result;
  219. }
  220. int usbat_set_shuttle_features(struct us_data *us,
  221.      unsigned char external_trigger,
  222.      unsigned char epp_control, 
  223.      unsigned char mask_byte, 
  224.      unsigned char test_pattern, 
  225.      unsigned char subcountH, 
  226.      unsigned char subcountL) {
  227. int result;
  228. unsigned char command[8] = {
  229. 0x40, 0x81, epp_control, external_trigger,
  230. test_pattern, mask_byte, subcountL, subcountH
  231. };
  232. result = usbat_send_control(us,
  233. usb_sndctrlpipe(us->pusb_dev,0),
  234. 0x80,
  235. 0x40,
  236. 0,
  237. 0,
  238. command,
  239. 8);
  240. return result;
  241. }
  242. int usbat_read_block(struct us_data *us,
  243.      unsigned char access,
  244.      unsigned char reg, 
  245.      unsigned char *content,
  246.      unsigned short len,
  247.      int use_sg) {
  248. int result;
  249. unsigned char command[8] = {
  250. 0xC0, access|0x02, reg, 0x00, 0x00, 0x00, 
  251. LSB_of(len), MSB_of(len)
  252. };
  253. result = usbat_send_control(us,
  254. usb_sndctrlpipe(us->pusb_dev,0),
  255. 0x80,
  256. 0x40,
  257. 0,
  258. 0,
  259. command,
  260. 8);
  261. if (result != USB_STOR_TRANSPORT_GOOD)
  262. return result;
  263. result = usbat_bulk_transport(us,
  264. NULL, 0, SCSI_DATA_READ, content, len, use_sg);
  265. return result;
  266. }
  267. /*
  268.  * Block, waiting for an ATA device to become not busy or to report
  269.  * an error condition.
  270.  */
  271. int usbat_wait_not_busy(struct us_data *us, int minutes) {
  272. int i;
  273. int result;
  274. unsigned char status;
  275. /* Synchronizing cache on a CDR could take a heck of a long time,
  276.  * but probably not more than 10 minutes or so. On the other hand,
  277.  * doing a full blank on a CDRW at speed 1 will take about 75
  278.  * minutes!
  279.  */
  280. for (i=0; i<1200+minutes*60; i++) {
  281.   result = usbat_read(us, USBAT_ATA, 0x17, &status);
  282. if (result!=USB_STOR_TRANSPORT_GOOD)
  283. return result;
  284. if (status&0x01) { // check condition
  285. result = usbat_read(us, USBAT_ATA, 0x10, &status);
  286. return USB_STOR_TRANSPORT_FAILED;
  287. }
  288. if (status&0x20) // device fault
  289. return USB_STOR_TRANSPORT_FAILED;
  290. if ((status&0x80)==0x00) { // not busy
  291. US_DEBUGP("Waited not busy for %d stepsn", i);
  292. return USB_STOR_TRANSPORT_GOOD;
  293. }
  294. if (i<500)
  295. wait_ms(10); // 5 seconds
  296. else if (i<700)
  297. wait_ms(50); // 10 seconds
  298. else if (i<1200)
  299. wait_ms(100); // 50 seconds
  300. else
  301. wait_ms(1000); // X minutes
  302. }
  303. US_DEBUGP("Waited not busy for %d minutes, timing out.n",
  304. minutes);
  305. return USB_STOR_TRANSPORT_FAILED;
  306. }
  307. int usbat_write_block(struct us_data *us,
  308.      unsigned char access,
  309.      unsigned char reg, 
  310.      unsigned char *content,
  311.      unsigned short len,
  312.      int use_sg,
  313.      int minutes) {
  314. int result;
  315. unsigned char command[8] = {
  316. 0x40, access|0x03, reg, 0x00, 0x00, 0x00, 
  317. LSB_of(len), MSB_of(len)
  318. };
  319. result = usbat_send_control(us,
  320. usb_sndctrlpipe(us->pusb_dev,0),
  321. 0x80,
  322. 0x40,
  323. 0,
  324. 0,
  325. command,
  326. 8);
  327. if (result != USB_STOR_TRANSPORT_GOOD)
  328. return result;
  329. result = usbat_bulk_transport(us,
  330. NULL, 0, SCSI_DATA_WRITE, content, len, use_sg);
  331. if (result != USB_STOR_TRANSPORT_GOOD)
  332. return result;
  333. return usbat_wait_not_busy(us, minutes);
  334. }
  335. int usbat_rw_block_test(struct us_data *us,
  336.      unsigned char access,
  337.      unsigned char *registers,
  338.      unsigned char *data_out,
  339.      unsigned short num_registers,
  340.      unsigned char data_reg, 
  341.      unsigned char status_reg, 
  342.      unsigned char timeout, 
  343.      unsigned char qualifier, 
  344.      int direction,
  345.      unsigned char *content,
  346.      unsigned short len,
  347.      int use_sg,
  348.      int minutes) {
  349. int result;
  350. // Not really sure the 0x07, 0x17, 0xfc, 0xe7 is necessary here,
  351. // but that's what came out of the trace every single time.
  352. unsigned char command[16] = {
  353. 0x40, access|0x07, 0x07, 0x17, 0xfc, 0xe7,
  354. LSB_of(num_registers*2), MSB_of(num_registers*2),
  355. (direction==SCSI_DATA_WRITE ? 0x40 : 0xC0), 
  356. access|(direction==SCSI_DATA_WRITE ? 0x05 : 0x04), 
  357. data_reg, status_reg,
  358. timeout, qualifier, LSB_of(len), MSB_of(len)
  359. };
  360. int i;
  361. unsigned char data[num_registers*2];
  362. unsigned char status;
  363. for (i=0; i<num_registers; i++) {
  364. data[i<<1] = registers[i];
  365. data[1+(i<<1)] = data_out[i];
  366. }
  367. for (i=0; i<20; i++) {
  368. /*
  369.  * The first time we send the full command, which consists
  370.  * of downloading the SCSI command followed by downloading
  371.  * the data via a write-and-test.  Any other time we only
  372.  * send the command to download the data -- the SCSI command
  373.  * is still 'active' in some sense in the device.
  374.  * 
  375.  * We're only going to try sending the data 10 times. After
  376.  * that, we just return a failure.
  377.  */
  378. result = usbat_send_control(us,
  379.   usb_sndctrlpipe(us->pusb_dev,0),
  380. 0x80,
  381. 0x40,
  382. 0,
  383. 0,
  384. (i==0 ? command : command+8),
  385. (i==0 ? 16 : 8));
  386. if (result != USB_STOR_TRANSPORT_GOOD)
  387. return result;
  388. if (i==0) {
  389. result = usbat_bulk_transport(us,
  390. NULL, 0, SCSI_DATA_WRITE, 
  391. data, num_registers*2, 0);
  392. if (result!=USB_STOR_TRANSPORT_GOOD)
  393. return result;
  394. }
  395. //US_DEBUGP("Transfer %s %d bytes, sg buffers %dn",
  396. // direction == SCSI_DATA_WRITE ? "out" : "in",
  397. // len, use_sg);
  398. result = usbat_bulk_transport(us,
  399. NULL, 0, direction, content, len, use_sg);
  400. /*
  401.  * If we get a stall on the bulk download, we'll retry
  402.  * the bulk download -- but not the SCSI command because
  403.  * in some sense the SCSI command is still 'active' and
  404.  * waiting for the data. Don't ask me why this should be;
  405.  * I'm only following what the Windoze driver did.
  406.  *
  407.  * Note that a stall for the test-and-read/write command means
  408.  * that the test failed. In this case we're testing to make
  409.  * sure that the device is error-free
  410.  * (i.e. bit 0 -- CHK -- of status is 0). The most likely
  411.  * hypothesis is that the USBAT chip somehow knows what
  412.  * the device will accept, but doesn't give the device any
  413.  * data until all data is received. Thus, the device would
  414.  * still be waiting for the first byte of data if a stall
  415.  * occurs, even if the stall implies that some data was
  416.  * transferred.
  417.  */
  418. if (result == US_BULK_TRANSFER_SHORT) {
  419. /*
  420.  * If we're reading and we stalled, then clear
  421.  * the bulk output pipe only the first time.
  422.  */
  423. if (direction==SCSI_DATA_READ && i==0)
  424. usb_clear_halt(us->pusb_dev,
  425. usb_sndbulkpipe(us->pusb_dev,
  426.   us->ep_out));
  427. /*
  428.  * Read status: is the device angry, or just busy?
  429.  */
  430.   result = usbat_read(us, USBAT_ATA, 
  431. direction==SCSI_DATA_WRITE ? 0x17 : 0x0E, 
  432. &status);
  433. if (result!=USB_STOR_TRANSPORT_GOOD)
  434. return result;
  435. if (status&0x01) // check condition
  436. return USB_STOR_TRANSPORT_FAILED;
  437. if (status&0x20) // device fault
  438. return USB_STOR_TRANSPORT_FAILED;
  439. US_DEBUGP("Redoing %sn",
  440.   direction==SCSI_DATA_WRITE ? "write" : "read");
  441. } else if (result != US_BULK_TRANSFER_GOOD)
  442. return result;
  443. else
  444. return usbat_wait_not_busy(us, minutes);
  445. }
  446. US_DEBUGP("Bummer! %s bulk data 20 times failed.n",
  447. direction==SCSI_DATA_WRITE ? "Writing" : "Reading");
  448. return USB_STOR_TRANSPORT_FAILED;
  449. }
  450. /*
  451.  * Write data to multiple registers at once. Not meant for large
  452.  * transfers of data!
  453.  */
  454. int usbat_multiple_write(struct us_data *us, 
  455. unsigned char access,
  456. unsigned char *registers,
  457. unsigned char *data_out,
  458. unsigned short num_registers) {
  459. int result;
  460. unsigned char data[num_registers*2];
  461. int i;
  462. unsigned char command[8] = {
  463. 0x40, access|0x07, 0x00, 0x00, 0x00, 0x00,
  464. LSB_of(num_registers*2), MSB_of(num_registers*2)
  465. };
  466. for (i=0; i<num_registers; i++) {
  467. data[i<<1] = registers[i];
  468. data[1+(i<<1)] = data_out[i];
  469. }
  470. result = usbat_send_control(us,
  471. usb_sndctrlpipe(us->pusb_dev,0),
  472. 0x80,
  473. 0x40,
  474. 0,
  475. 0,
  476. command,
  477. 8);
  478. if (result != USB_STOR_TRANSPORT_GOOD)
  479. return result;
  480. result = usbat_bulk_transport(us,
  481. NULL, 0, SCSI_DATA_WRITE, data, num_registers*2, 0);
  482. if (result!=USB_STOR_TRANSPORT_GOOD)
  483. return result;
  484. return usbat_wait_not_busy(us, 0);
  485. }
  486. int usbat_read_user_io(struct us_data *us,
  487. unsigned char *data_flags) {
  488. int result;
  489. result = usbat_send_control(us,
  490. usb_rcvctrlpipe(us->pusb_dev,0),
  491. 0x82,
  492. 0xC0,
  493. 0,
  494. 0,
  495. data_flags,
  496. 1);
  497. return result;
  498. }
  499. int usbat_write_user_io(struct us_data *us,
  500. unsigned char enable_flags,
  501. unsigned char data_flags) {
  502. int result;
  503. result = usbat_send_control(us,
  504. usb_sndctrlpipe(us->pusb_dev,0),
  505. 0x82,
  506. 0x40,
  507. short_pack(enable_flags, data_flags),
  508. 0,
  509. NULL,
  510. 0);
  511. return result;
  512. }
  513. /*
  514.  * Squeeze a potentially huge (> 65535 byte) read10 command into
  515.  * a little ( <= 65535 byte) ATAPI pipe
  516.  */
  517. int usbat_handle_read10(struct us_data *us,
  518. unsigned char *registers,
  519. unsigned char *data,
  520. Scsi_Cmnd *srb) {
  521. int result = USB_STOR_TRANSPORT_GOOD;
  522. unsigned char *buffer;
  523. unsigned int len;
  524. unsigned int sector;
  525. unsigned int amount;
  526. struct scatterlist *sg = NULL;
  527. int sg_segment = 0;
  528. int sg_offset = 0;
  529. US_DEBUGP("handle_read10: transfersize %dn",
  530. srb->transfersize);
  531. if (srb->request_bufflen < 0x10000) {
  532. result = usbat_rw_block_test(us, USBAT_ATA, 
  533. registers, data, 19,
  534. 0x10, 0x17, 0xFD, 0x30,
  535. SCSI_DATA_READ,
  536. srb->request_buffer, 
  537. srb->request_bufflen, srb->use_sg, 1);
  538. return result;
  539. }
  540. /*
  541.  * Since we're requesting more data than we can handle in
  542.  * a single read command (max is 64k-1), we will perform
  543.  * multiple reads, but each read must be in multiples of
  544.  * a sector.  Luckily the sector size is in srb->transfersize
  545.  * (see linux/drivers/scsi/sr.c).
  546.  */
  547. if (data[7+0] == GPCMD_READ_CD) {
  548. len = short_pack(data[7+9], data[7+8]);
  549. len <<= 16;
  550. len |= data[7+7];
  551. srb->transfersize = srb->request_bufflen/len;
  552. }
  553. len = (65535/srb->transfersize) * srb->transfersize;
  554. US_DEBUGP("Max read is %d bytesn", len);
  555. buffer = kmalloc(len, GFP_NOIO);
  556. if (buffer == NULL) // bloody hell!
  557. return USB_STOR_TRANSPORT_FAILED;
  558. sector = short_pack(data[7+3], data[7+2]);
  559. sector <<= 16;
  560. sector |= short_pack(data[7+5], data[7+4]);
  561. transferred = 0;
  562. if (srb->use_sg) {
  563. sg = (struct scatterlist *)srb->request_buffer;
  564. sg_segment = 0; // for keeping track of where we are in
  565. sg_offset = 0;  // the scatter/gather list
  566. }
  567. while (transferred != srb->request_bufflen) {
  568. if (len > srb->request_bufflen - transferred)
  569. len = srb->request_bufflen - transferred;
  570. data[3] = len&0xFF;    // (cylL) = expected length (L)
  571. data[4] = (len>>8)&0xFF;  // (cylH) = expected length (H)
  572. // Fix up the SCSI command sector and num sectors
  573. data[7+2] = MSB_of(sector>>16); // SCSI command sector
  574. data[7+3] = LSB_of(sector>>16);
  575. data[7+4] = MSB_of(sector&0xFFFF);
  576. data[7+5] = LSB_of(sector&0xFFFF);
  577. if (data[7+0] == GPCMD_READ_CD)
  578. data[7+6] = 0;
  579. data[7+7] = MSB_of(len / srb->transfersize); // SCSI command
  580. data[7+8] = LSB_of(len / srb->transfersize); // num sectors
  581. result = usbat_rw_block_test(us, USBAT_ATA, 
  582. registers, data, 19,
  583. 0x10, 0x17, 0xFD, 0x30,
  584. SCSI_DATA_READ,
  585. buffer,
  586. len, 0, 1);
  587. if (result != USB_STOR_TRANSPORT_GOOD)
  588. break;
  589. // Transfer the received data into the srb buffer
  590. if (!srb->use_sg) {
  591. memcpy(srb->request_buffer+transferred, buffer, len);
  592. } else {
  593. amount = 0;
  594. while (amount<len) {
  595. if (len - amount >= 
  596.   sg[sg_segment].length-sg_offset) {
  597.   memcpy(sg[sg_segment].address + sg_offset,
  598. buffer + amount,
  599. sg[sg_segment].length - sg_offset);
  600.   amount += 
  601.   sg[sg_segment].length-sg_offset;
  602.   sg_segment++;
  603.   sg_offset=0;
  604. } else {
  605.   memcpy(sg[sg_segment].address + sg_offset,
  606. buffer + amount,
  607. len - amount);
  608.   sg_offset += (len - amount);
  609.   amount = len;
  610. }
  611. }
  612. }
  613. // Update the amount transferred and the sector number
  614. transferred += len;
  615. sector += len / srb->transfersize;
  616. } // while transferred != srb->request_bufflen
  617. kfree(buffer);
  618. return result;
  619. }
  620. static int hp_8200e_select_and_test_registers(struct us_data *us) {
  621. int result;
  622. int selector;
  623. unsigned char status;
  624. // try device = master, then device = slave.
  625. for (selector = 0xA0; selector <= 0xB0; selector += 0x10) {
  626. if ( (result = usbat_write(us, USBAT_ATA, 0x16, selector)) != 
  627. USB_STOR_TRANSPORT_GOOD)
  628. return result;
  629. if ( (result = usbat_read(us, USBAT_ATA, 0x17, &status)) != 
  630. USB_STOR_TRANSPORT_GOOD)
  631. return result;
  632. if ( (result = usbat_read(us, USBAT_ATA, 0x16, &status)) != 
  633. USB_STOR_TRANSPORT_GOOD)
  634. return result;
  635. if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) != 
  636. USB_STOR_TRANSPORT_GOOD)
  637. return result;
  638. if ( (result = usbat_read(us, USBAT_ATA, 0x15, &status)) != 
  639. USB_STOR_TRANSPORT_GOOD)
  640. return result;
  641. if ( (result = usbat_write(us, USBAT_ATA, 0x14, 0x55)) != 
  642. USB_STOR_TRANSPORT_GOOD)
  643. return result;
  644. if ( (result = usbat_write(us, USBAT_ATA, 0x15, 0xAA)) != 
  645. USB_STOR_TRANSPORT_GOOD)
  646. return result;
  647. if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) != 
  648. USB_STOR_TRANSPORT_GOOD)
  649. return result;
  650. if ( (result = usbat_read(us, USBAT_ATA, 0x15, &status)) != 
  651. USB_STOR_TRANSPORT_GOOD)
  652. return result;
  653. }
  654. return result;
  655. }
  656. int init_8200e(struct us_data *us) {
  657. int result;
  658. unsigned char status;
  659. // Enable peripheral control signals
  660. if ( (result = usbat_write_user_io(us,
  661.   USBAT_UIO_OE1 | USBAT_UIO_OE0,
  662.   USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
  663. return result;
  664. US_DEBUGP("INIT 1n");
  665. wait_ms(2000);
  666. if ( (result = usbat_read_user_io(us, &status)) !=
  667. USB_STOR_TRANSPORT_GOOD)
  668. return result;
  669. US_DEBUGP("INIT 2n");
  670. if ( (result = usbat_read_user_io(us, &status)) !=
  671. USB_STOR_TRANSPORT_GOOD)
  672. return result;
  673. US_DEBUGP("INIT 3n");
  674. // Reset peripheral, enable periph control signals
  675. // (bring reset signal up)
  676. if ( (result = usbat_write_user_io(us,
  677.   USBAT_UIO_DRVRST | USBAT_UIO_OE1 | USBAT_UIO_OE0,
  678.   USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
  679. return result;
  680. US_DEBUGP("INIT 4n");
  681. // Enable periph control signals
  682. // (bring reset signal down)
  683. if ( (result = usbat_write_user_io(us,
  684.   USBAT_UIO_OE1 | USBAT_UIO_OE0,
  685.   USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
  686. return result;
  687. US_DEBUGP("INIT 5n");
  688. wait_ms(250);
  689. // Write 0x80 to ISA port 0x3F
  690. if ( (result = usbat_write(us, USBAT_ISA, 0x3F, 0x80)) !=
  691. USB_STOR_TRANSPORT_GOOD)
  692. return result;
  693. US_DEBUGP("INIT 6n");
  694. // Read ISA port 0x27
  695. if ( (result = usbat_read(us, USBAT_ISA, 0x27, &status)) !=
  696. USB_STOR_TRANSPORT_GOOD)
  697. return result;
  698. US_DEBUGP("INIT 7n");
  699. if ( (result = usbat_read_user_io(us, &status)) !=
  700. USB_STOR_TRANSPORT_GOOD)
  701. return result;
  702. US_DEBUGP("INIT 8n");
  703. if ( (result = hp_8200e_select_and_test_registers(us)) !=
  704.  USB_STOR_TRANSPORT_GOOD)
  705. return result;
  706. US_DEBUGP("INIT 9n");
  707. if ( (result = usbat_read_user_io(us, &status)) !=
  708. USB_STOR_TRANSPORT_GOOD)
  709. return result;
  710. US_DEBUGP("INIT 10n");
  711. // Enable periph control signals and card detect
  712. if ( (result = usbat_write_user_io(us,
  713.   USBAT_UIO_ACKD |USBAT_UIO_OE1 | USBAT_UIO_OE0,
  714.   USBAT_UIO_EPAD | USBAT_UIO_1)) != USB_STOR_TRANSPORT_GOOD)
  715. return result;
  716. US_DEBUGP("INIT 11n");
  717. if ( (result = usbat_read_user_io(us, &status)) !=
  718. USB_STOR_TRANSPORT_GOOD)
  719. return result;
  720. US_DEBUGP("INIT 12n");
  721. wait_ms(1400);
  722. if ( (result = usbat_read_user_io(us, &status)) !=
  723. USB_STOR_TRANSPORT_GOOD)
  724. return result;
  725. US_DEBUGP("INIT 13n");
  726. if ( (result = hp_8200e_select_and_test_registers(us)) !=
  727.  USB_STOR_TRANSPORT_GOOD)
  728. return result;
  729. US_DEBUGP("INIT 14n");
  730. if ( (result = usbat_set_shuttle_features(us, 
  731. 0x83, 0x00, 0x88, 0x08, 0x15, 0x14)) !=
  732.  USB_STOR_TRANSPORT_GOOD)
  733. return result;
  734. US_DEBUGP("INIT 15n");
  735. return result;
  736. }
  737. /*
  738.  * Transport for the HP 8200e
  739.  */
  740. int hp8200e_transport(Scsi_Cmnd *srb, struct us_data *us)
  741. {
  742. int result;
  743. unsigned char status;
  744. unsigned char registers[32];
  745. unsigned char data[32];
  746. unsigned int len;
  747. int i;
  748. char string[64];
  749. len = srb->request_bufflen;
  750. /* Send A0 (ATA PACKET COMMAND).
  751.    Note: I guess we're never going to get any of the ATA
  752.    commands... just ATA Packet Commands.
  753.    */
  754. registers[0] = 0x11;
  755. registers[1] = 0x12;
  756. registers[2] = 0x13;
  757. registers[3] = 0x14;
  758. registers[4] = 0x15;
  759. registers[5] = 0x16;
  760. registers[6] = 0x17;
  761. data[0] = 0x00;
  762. data[1] = 0x00;
  763. data[2] = 0x00;
  764. data[3] = len&0xFF;  // (cylL) = expected length (L)
  765. data[4] = (len>>8)&0xFF;  // (cylH) = expected length (H)
  766. data[5] = 0xB0;  // (device sel) = slave
  767. data[6] = 0xA0;  // (command) = ATA PACKET COMMAND
  768. for (i=7; i<19; i++) {
  769. registers[i] = 0x10;
  770. data[i] = (i-7 >= srb->cmd_len) ? 0 : srb->cmnd[i-7];
  771. }
  772. result = usbat_read(us, USBAT_ATA, 0x17, &status);
  773. US_DEBUGP("Status = %02Xn", status);
  774. if (srb->cmnd[0] == TEST_UNIT_READY)
  775. transferred = 0;
  776. if (srb->sc_data_direction == SCSI_DATA_WRITE) {
  777. result = usbat_rw_block_test(us, USBAT_ATA, 
  778. registers, data, 19,
  779. 0x10, 0x17, 0xFD, 0x30,
  780. SCSI_DATA_WRITE,
  781. srb->request_buffer, 
  782. len, srb->use_sg, 10);
  783. if (result == USB_STOR_TRANSPORT_GOOD) {
  784. transferred += len;
  785. US_DEBUGP("Wrote %08X bytesn", transferred);
  786. }
  787. return result;
  788. } else if (srb->cmnd[0] == READ_10 ||
  789.    srb->cmnd[0] == GPCMD_READ_CD) {
  790. return usbat_handle_read10(us, registers, data, srb);
  791. }
  792. if (len > 0xFFFF) {
  793. US_DEBUGP("Error: len = %08X... what do I do now?n",
  794. len);
  795. return USB_STOR_TRANSPORT_ERROR;
  796. }
  797. if ( (result = usbat_multiple_write(us, 
  798. USBAT_ATA,
  799. registers, data, 7)) != USB_STOR_TRANSPORT_GOOD) {
  800. return result;
  801. }
  802. // Write the 12-byte command header.
  803. // If the command is BLANK then set the timer for 75 minutes.
  804. // Otherwise set it for 10 minutes.
  805. // NOTE: THE 8200 DOCUMENTATION STATES THAT BLANKING A CDRW
  806. // AT SPEED 4 IS UNRELIABLE!!!
  807. if ( (result = usbat_write_block(us, 
  808. USBAT_ATA, 0x10, srb->cmnd, 12, 0,
  809. srb->cmnd[0]==GPCMD_BLANK ? 75 : 10)) !=
  810. USB_STOR_TRANSPORT_GOOD) {
  811. return result;
  812. }
  813. // If there is response data to be read in 
  814. // then do it here.
  815. if (len != 0 && (srb->sc_data_direction == SCSI_DATA_READ)) {
  816. // How many bytes to read in? Check cylL register
  817. if ( (result = usbat_read(us, USBAT_ATA, 0x14, &status)) != 
  818.     USB_STOR_TRANSPORT_GOOD) {
  819. return result;
  820. }
  821. if (len>0xFF) { // need to read cylH also
  822. len = status;
  823. if ( (result = usbat_read(us, USBAT_ATA, 0x15,
  824. &status)) !=
  825.     USB_STOR_TRANSPORT_GOOD) {
  826. return result;
  827. }
  828. len += ((unsigned int)status)<<8;
  829. }
  830. else
  831. len = status;
  832. result = usbat_read_block(us, USBAT_ATA, 0x10, 
  833. srb->request_buffer, len, srb->use_sg);
  834. /* Debug-print the first 32 bytes of the transfer */
  835. if (!srb->use_sg) {
  836. string[0] = 0;
  837. for (i=0; i<len && i<32; i++) {
  838. sprintf(string+strlen(string), "%02X ",
  839.   ((unsigned char *)srb->request_buffer)[i]);
  840. if ((i%16)==15) {
  841. US_DEBUGP("%sn", string);
  842. string[0] = 0;
  843. }
  844. }
  845. if (string[0]!=0)
  846. US_DEBUGP("%sn", string);
  847. }
  848. }
  849. return result;
  850. }