File: /usr/src/linux/drivers/usb/storage/shuttle_usbat.c

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