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