1 /* Driver for Freecom USB/IDE adaptor
7 * Current development and maintenance by:
8 * (C) 2000 David Brown <usb-storage@davidb.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 675 Mass Ave, Cambridge, MA 02139, USA.
24 * This driver was developed with information provided in FREECOM's USB
25 * Programmers Reference Guide. For further information contact Freecom
26 * (http://www.freecom.de/)
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
33 #include "transport.h"
38 #ifdef CONFIG_USB_STORAGE_DEBUG
39 static void pdump (void *, int);
42 /* Bits of HD_STATUS */
46 /* All of the outgoing packets are 64 bytes long. */
47 struct freecom_cb_wrap
{
48 u8 Type
; /* Command type. */
49 u8 Timeout
; /* Timeout in seconds. */
50 u8 Atapi
[12]; /* An ATAPI packet. */
51 u8 Filler
[50]; /* Padding Data. */
54 struct freecom_xfer_wrap
{
55 u8 Type
; /* Command type. */
56 u8 Timeout
; /* Timeout in seconds. */
57 __le32 Count
; /* Number of bytes to transfer. */
59 } __attribute__ ((packed
));
61 struct freecom_ide_out
{
62 u8 Type
; /* Type + IDE register. */
64 __le16 Value
; /* Value to write. */
68 struct freecom_ide_in
{
69 u8 Type
; /* Type | IDE register. */
73 struct freecom_status
{
80 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
82 #define FCM_INT_STATUS 0x02 /* INDEX_STAT */
83 #define FCM_STATUS_BUSY 0x80
85 /* These are the packet types. The low bit indicates that this command
86 * should wait for an interrupt. */
87 #define FCM_PACKET_ATAPI 0x21
88 #define FCM_PACKET_STATUS 0x20
90 /* Receive data from the IDE interface. The ATAPI packet has already
91 * waited, so the data should be immediately available. */
92 #define FCM_PACKET_INPUT 0x81
94 /* Send data to the IDE interface. */
95 #define FCM_PACKET_OUTPUT 0x01
97 /* Write a value to an ide register. Or the ide register to write after
98 * munging the address a bit. */
99 #define FCM_PACKET_IDE_WRITE 0x40
100 #define FCM_PACKET_IDE_READ 0xC0
102 /* All packets (except for status) are 64 bytes long. */
103 #define FCM_PACKET_LENGTH 64
104 #define FCM_STATUS_PACKET_LENGTH 4
107 freecom_readdata (struct scsi_cmnd
*srb
, struct us_data
*us
,
108 unsigned int ipipe
, unsigned int opipe
, int count
)
110 struct freecom_xfer_wrap
*fxfr
=
111 (struct freecom_xfer_wrap
*) us
->iobuf
;
114 fxfr
->Type
= FCM_PACKET_INPUT
| 0x00;
115 fxfr
->Timeout
= 0; /* Short timeout for debugging. */
116 fxfr
->Count
= cpu_to_le32 (count
);
117 memset (fxfr
->Pad
, 0, sizeof (fxfr
->Pad
));
119 US_DEBUGP("Read data Freecom! (c=%d)\n", count
);
121 /* Issue the transfer command. */
122 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fxfr
,
123 FCM_PACKET_LENGTH
, NULL
);
124 if (result
!= USB_STOR_XFER_GOOD
) {
125 US_DEBUGP ("Freecom readdata transport error\n");
126 return USB_STOR_TRANSPORT_ERROR
;
129 /* Now transfer all of our blocks. */
130 US_DEBUGP("Start of read\n");
131 result
= usb_stor_bulk_srb(us
, ipipe
, srb
);
132 US_DEBUGP("freecom_readdata done!\n");
134 if (result
> USB_STOR_XFER_SHORT
)
135 return USB_STOR_TRANSPORT_ERROR
;
136 return USB_STOR_TRANSPORT_GOOD
;
140 freecom_writedata (struct scsi_cmnd
*srb
, struct us_data
*us
,
141 int unsigned ipipe
, unsigned int opipe
, int count
)
143 struct freecom_xfer_wrap
*fxfr
=
144 (struct freecom_xfer_wrap
*) us
->iobuf
;
147 fxfr
->Type
= FCM_PACKET_OUTPUT
| 0x00;
148 fxfr
->Timeout
= 0; /* Short timeout for debugging. */
149 fxfr
->Count
= cpu_to_le32 (count
);
150 memset (fxfr
->Pad
, 0, sizeof (fxfr
->Pad
));
152 US_DEBUGP("Write data Freecom! (c=%d)\n", count
);
154 /* Issue the transfer command. */
155 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fxfr
,
156 FCM_PACKET_LENGTH
, NULL
);
157 if (result
!= USB_STOR_XFER_GOOD
) {
158 US_DEBUGP ("Freecom writedata transport error\n");
159 return USB_STOR_TRANSPORT_ERROR
;
162 /* Now transfer all of our blocks. */
163 US_DEBUGP("Start of write\n");
164 result
= usb_stor_bulk_srb(us
, opipe
, srb
);
166 US_DEBUGP("freecom_writedata done!\n");
167 if (result
> USB_STOR_XFER_SHORT
)
168 return USB_STOR_TRANSPORT_ERROR
;
169 return USB_STOR_TRANSPORT_GOOD
;
173 * Transport for the Freecom USB/IDE adaptor.
176 int freecom_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
178 struct freecom_cb_wrap
*fcb
;
179 struct freecom_status
*fst
;
180 unsigned int ipipe
, opipe
; /* We need both pipes. */
182 unsigned int partial
;
185 fcb
= (struct freecom_cb_wrap
*) us
->iobuf
;
186 fst
= (struct freecom_status
*) us
->iobuf
;
188 US_DEBUGP("Freecom TRANSPORT STARTED\n");
190 /* Get handles for both transports. */
191 opipe
= us
->send_bulk_pipe
;
192 ipipe
= us
->recv_bulk_pipe
;
194 /* The ATAPI Command always goes out first. */
195 fcb
->Type
= FCM_PACKET_ATAPI
| 0x00;
197 memcpy (fcb
->Atapi
, srb
->cmnd
, 12);
198 memset (fcb
->Filler
, 0, sizeof (fcb
->Filler
));
200 US_DEBUG(pdump (srb
->cmnd
, 12));
203 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fcb
,
204 FCM_PACKET_LENGTH
, NULL
);
206 /* The Freecom device will only fail if there is something wrong in
207 * USB land. It returns the status in its own registers, which
208 * come back in the bulk pipe. */
209 if (result
!= USB_STOR_XFER_GOOD
) {
210 US_DEBUGP ("freecom transport error\n");
211 return USB_STOR_TRANSPORT_ERROR
;
214 /* There are times we can optimize out this status read, but it
215 * doesn't hurt us to always do it now. */
216 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
217 FCM_STATUS_PACKET_LENGTH
, &partial
);
218 US_DEBUGP("foo Status result %d %u\n", result
, partial
);
219 if (result
!= USB_STOR_XFER_GOOD
)
220 return USB_STOR_TRANSPORT_ERROR
;
222 US_DEBUG(pdump ((void *) fst
, partial
));
224 /* The firmware will time-out commands after 20 seconds. Some commands
225 * can legitimately take longer than this, so we use a different
226 * command that only waits for the interrupt and then sends status,
227 * without having to send a new ATAPI command to the device.
229 * NOTE: There is some indication that a data transfer after a timeout
230 * may not work, but that is a condition that should never happen.
232 while (fst
->Status
& FCM_STATUS_BUSY
) {
233 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
234 US_DEBUGP("fst->Status is %x\n", fst
->Status
);
236 /* Get the status again */
237 fcb
->Type
= FCM_PACKET_STATUS
;
239 memset (fcb
->Atapi
, 0, sizeof(fcb
->Atapi
));
240 memset (fcb
->Filler
, 0, sizeof (fcb
->Filler
));
243 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fcb
,
244 FCM_PACKET_LENGTH
, NULL
);
246 /* The Freecom device will only fail if there is something
247 * wrong in USB land. It returns the status in its own
248 * registers, which come back in the bulk pipe.
250 if (result
!= USB_STOR_XFER_GOOD
) {
251 US_DEBUGP ("freecom transport error\n");
252 return USB_STOR_TRANSPORT_ERROR
;
256 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
257 FCM_STATUS_PACKET_LENGTH
, &partial
);
259 US_DEBUGP("bar Status result %d %u\n", result
, partial
);
260 if (result
!= USB_STOR_XFER_GOOD
)
261 return USB_STOR_TRANSPORT_ERROR
;
263 US_DEBUG(pdump ((void *) fst
, partial
));
267 return USB_STOR_TRANSPORT_ERROR
;
268 if ((fst
->Status
& 1) != 0) {
269 US_DEBUGP("operation failed\n");
270 return USB_STOR_TRANSPORT_FAILED
;
273 /* The device might not have as much data available as we
274 * requested. If you ask for more than the device has, this reads
275 * and such will hang. */
276 US_DEBUGP("Device indicates that it has %d bytes available\n",
277 le16_to_cpu (fst
->Count
));
278 US_DEBUGP("SCSI requested %d\n", scsi_bufflen(srb
));
280 /* Find the length we desire to read. */
281 switch (srb
->cmnd
[0]) {
283 case REQUEST_SENSE
: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
286 length
= le16_to_cpu(fst
->Count
);
289 length
= scsi_bufflen(srb
);
292 /* verify that this amount is legal */
293 if (length
> scsi_bufflen(srb
)) {
294 length
= scsi_bufflen(srb
);
295 US_DEBUGP("Truncating request to match buffer length: %d\n", length
);
298 /* What we do now depends on what direction the data is supposed to
301 switch (us
->srb
->sc_data_direction
) {
302 case DMA_FROM_DEVICE
:
303 /* catch bogus "read 0 length" case */
306 /* Make sure that the status indicates that the device
307 * wants data as well. */
308 if ((fst
->Status
& DRQ_STAT
) == 0 || (fst
->Reason
& 3) != 2) {
309 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
310 return USB_STOR_TRANSPORT_FAILED
;
312 result
= freecom_readdata (srb
, us
, ipipe
, opipe
, length
);
313 if (result
!= USB_STOR_TRANSPORT_GOOD
)
316 US_DEBUGP("FCM: Waiting for status\n");
317 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
318 FCM_PACKET_LENGTH
, &partial
);
319 US_DEBUG(pdump ((void *) fst
, partial
));
321 if (partial
!= 4 || result
> USB_STOR_XFER_SHORT
)
322 return USB_STOR_TRANSPORT_ERROR
;
323 if ((fst
->Status
& ERR_STAT
) != 0) {
324 US_DEBUGP("operation failed\n");
325 return USB_STOR_TRANSPORT_FAILED
;
327 if ((fst
->Reason
& 3) != 3) {
328 US_DEBUGP("Drive seems still hungry\n");
329 return USB_STOR_TRANSPORT_FAILED
;
331 US_DEBUGP("Transfer happy\n");
335 /* catch bogus "write 0 length" case */
338 /* Make sure the status indicates that the device wants to
341 result
= freecom_writedata (srb
, us
, ipipe
, opipe
, length
);
342 if (result
!= USB_STOR_TRANSPORT_GOOD
)
345 US_DEBUGP("FCM: Waiting for status\n");
346 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
347 FCM_PACKET_LENGTH
, &partial
);
349 if (partial
!= 4 || result
> USB_STOR_XFER_SHORT
)
350 return USB_STOR_TRANSPORT_ERROR
;
351 if ((fst
->Status
& ERR_STAT
) != 0) {
352 US_DEBUGP("operation failed\n");
353 return USB_STOR_TRANSPORT_FAILED
;
355 if ((fst
->Reason
& 3) != 3) {
356 US_DEBUGP("Drive seems still hungry\n");
357 return USB_STOR_TRANSPORT_FAILED
;
360 US_DEBUGP("Transfer happy\n");
365 /* Easy, do nothing. */
369 /* should never hit here -- filtered in usb.c */
370 US_DEBUGP ("freecom unimplemented direction: %d\n",
371 us
->srb
->sc_data_direction
);
372 // Return fail, SCSI seems to handle this better.
373 return USB_STOR_TRANSPORT_FAILED
;
377 return USB_STOR_TRANSPORT_GOOD
;
381 freecom_init (struct us_data
*us
)
384 char *buffer
= us
->iobuf
;
386 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
387 * all our packets. No need to allocate any extra buffer space.
390 result
= usb_stor_control_msg(us
, us
->recv_ctrl_pipe
,
391 0x4c, 0xc0, 0x4346, 0x0, buffer
, 0x20, 3*HZ
);
393 US_DEBUGP("String returned from FC init is: %s\n", buffer
);
395 /* Special thanks to the people at Freecom for providing me with
396 * this "magic sequence", which they use in their Windows and MacOS
397 * drivers to make sure that all the attached perhiperals are
402 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
403 0x4d, 0x40, 0x24d8, 0x0, NULL
, 0x0, 3*HZ
);
404 US_DEBUGP("result from activate reset is %d\n", result
);
410 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
411 0x4d, 0x40, 0x24f8, 0x0, NULL
, 0x0, 3*HZ
);
412 US_DEBUGP("result from clear reset is %d\n", result
);
417 return USB_STOR_TRANSPORT_GOOD
;
420 int usb_stor_freecom_reset(struct us_data
*us
)
422 printk (KERN_CRIT
"freecom reset called\n");
424 /* We don't really have this feature. */
428 #ifdef CONFIG_USB_STORAGE_DEBUG
429 static void pdump (void *ibuffer
, int length
)
431 static char line
[80];
433 unsigned char *buffer
= (unsigned char *) ibuffer
;
438 for (i
= 0; i
< length
; i
++) {
441 offset
+= sprintf (line
+offset
, " - ");
442 for (j
= i
- 16; j
< i
; j
++) {
443 if (buffer
[j
] >= 32 && buffer
[j
] <= 126)
444 line
[offset
++] = buffer
[j
];
446 line
[offset
++] = '.';
449 US_DEBUGP("%s\n", line
);
452 offset
+= sprintf (line
+offset
, "%08x:", i
);
454 else if ((i
& 7) == 0) {
455 offset
+= sprintf (line
+offset
, " -");
457 offset
+= sprintf (line
+offset
, " %02x", buffer
[i
] & 0xff);
460 /* Add the last "chunk" of data. */
461 from
= (length
- 1) % 16;
462 base
= ((length
- 1) / 16) * 16;
464 for (i
= from
+ 1; i
< 16; i
++)
465 offset
+= sprintf (line
+offset
, " ");
467 offset
+= sprintf (line
+offset
, " ");
468 offset
+= sprintf (line
+offset
, " - ");
470 for (i
= 0; i
<= from
; i
++) {
471 if (buffer
[base
+i
] >= 32 && buffer
[base
+i
] <= 126)
472 line
[offset
++] = buffer
[base
+i
];
474 line
[offset
++] = '.';
477 US_DEBUGP("%s\n", line
);