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