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 <linux/hdreg.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
35 #include "transport.h"
40 #ifdef CONFIG_USB_STORAGE_DEBUG
41 static void pdump (void *, int);
44 /* Bits of HD_STATUS */
48 /* All of the outgoing packets are 64 bytes long. */
49 struct freecom_cb_wrap
{
50 u8 Type
; /* Command type. */
51 u8 Timeout
; /* Timeout in seconds. */
52 u8 Atapi
[12]; /* An ATAPI packet. */
53 u8 Filler
[50]; /* Padding Data. */
56 struct freecom_xfer_wrap
{
57 u8 Type
; /* Command type. */
58 u8 Timeout
; /* Timeout in seconds. */
59 __le32 Count
; /* Number of bytes to transfer. */
61 } __attribute__ ((packed
));
63 struct freecom_ide_out
{
64 u8 Type
; /* Type + IDE register. */
66 __le16 Value
; /* Value to write. */
70 struct freecom_ide_in
{
71 u8 Type
; /* Type | IDE register. */
75 struct freecom_status
{
82 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
84 #define FCM_INT_STATUS 0x02 /* INDEX_STAT */
85 #define FCM_STATUS_BUSY 0x80
87 /* These are the packet types. The low bit indicates that this command
88 * should wait for an interrupt. */
89 #define FCM_PACKET_ATAPI 0x21
90 #define FCM_PACKET_STATUS 0x20
92 /* Receive data from the IDE interface. The ATAPI packet has already
93 * waited, so the data should be immediately available. */
94 #define FCM_PACKET_INPUT 0x81
96 /* Send data to the IDE interface. */
97 #define FCM_PACKET_OUTPUT 0x01
99 /* Write a value to an ide register. Or the ide register to write after
100 * munging the address a bit. */
101 #define FCM_PACKET_IDE_WRITE 0x40
102 #define FCM_PACKET_IDE_READ 0xC0
104 /* All packets (except for status) are 64 bytes long. */
105 #define FCM_PACKET_LENGTH 64
106 #define FCM_STATUS_PACKET_LENGTH 4
109 freecom_readdata (struct scsi_cmnd
*srb
, struct us_data
*us
,
110 unsigned int ipipe
, unsigned int opipe
, int count
)
112 struct freecom_xfer_wrap
*fxfr
=
113 (struct freecom_xfer_wrap
*) us
->iobuf
;
116 fxfr
->Type
= FCM_PACKET_INPUT
| 0x00;
117 fxfr
->Timeout
= 0; /* Short timeout for debugging. */
118 fxfr
->Count
= cpu_to_le32 (count
);
119 memset (fxfr
->Pad
, 0, sizeof (fxfr
->Pad
));
121 US_DEBUGP("Read data Freecom! (c=%d)\n", count
);
123 /* Issue the transfer command. */
124 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fxfr
,
125 FCM_PACKET_LENGTH
, NULL
);
126 if (result
!= USB_STOR_XFER_GOOD
) {
127 US_DEBUGP ("Freecom readdata transport error\n");
128 return USB_STOR_TRANSPORT_ERROR
;
131 /* Now transfer all of our blocks. */
132 US_DEBUGP("Start of read\n");
133 result
= usb_stor_bulk_srb(us
, ipipe
, srb
);
134 US_DEBUGP("freecom_readdata done!\n");
136 if (result
> USB_STOR_XFER_SHORT
)
137 return USB_STOR_TRANSPORT_ERROR
;
138 return USB_STOR_TRANSPORT_GOOD
;
142 freecom_writedata (struct scsi_cmnd
*srb
, struct us_data
*us
,
143 int unsigned ipipe
, unsigned int opipe
, int count
)
145 struct freecom_xfer_wrap
*fxfr
=
146 (struct freecom_xfer_wrap
*) us
->iobuf
;
149 fxfr
->Type
= FCM_PACKET_OUTPUT
| 0x00;
150 fxfr
->Timeout
= 0; /* Short timeout for debugging. */
151 fxfr
->Count
= cpu_to_le32 (count
);
152 memset (fxfr
->Pad
, 0, sizeof (fxfr
->Pad
));
154 US_DEBUGP("Write data Freecom! (c=%d)\n", count
);
156 /* Issue the transfer command. */
157 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fxfr
,
158 FCM_PACKET_LENGTH
, NULL
);
159 if (result
!= USB_STOR_XFER_GOOD
) {
160 US_DEBUGP ("Freecom writedata transport error\n");
161 return USB_STOR_TRANSPORT_ERROR
;
164 /* Now transfer all of our blocks. */
165 US_DEBUGP("Start of write\n");
166 result
= usb_stor_bulk_srb(us
, opipe
, srb
);
168 US_DEBUGP("freecom_writedata done!\n");
169 if (result
> USB_STOR_XFER_SHORT
)
170 return USB_STOR_TRANSPORT_ERROR
;
171 return USB_STOR_TRANSPORT_GOOD
;
175 * Transport for the Freecom USB/IDE adaptor.
178 int freecom_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
180 struct freecom_cb_wrap
*fcb
;
181 struct freecom_status
*fst
;
182 unsigned int ipipe
, opipe
; /* We need both pipes. */
184 unsigned int partial
;
187 fcb
= (struct freecom_cb_wrap
*) us
->iobuf
;
188 fst
= (struct freecom_status
*) us
->iobuf
;
190 US_DEBUGP("Freecom TRANSPORT STARTED\n");
192 /* Get handles for both transports. */
193 opipe
= us
->send_bulk_pipe
;
194 ipipe
= us
->recv_bulk_pipe
;
196 /* The ATAPI Command always goes out first. */
197 fcb
->Type
= FCM_PACKET_ATAPI
| 0x00;
199 memcpy (fcb
->Atapi
, srb
->cmnd
, 12);
200 memset (fcb
->Filler
, 0, sizeof (fcb
->Filler
));
202 US_DEBUG(pdump (srb
->cmnd
, 12));
205 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fcb
,
206 FCM_PACKET_LENGTH
, NULL
);
208 /* The Freecom device will only fail if there is something wrong in
209 * USB land. It returns the status in its own registers, which
210 * come back in the bulk pipe. */
211 if (result
!= USB_STOR_XFER_GOOD
) {
212 US_DEBUGP ("freecom transport error\n");
213 return USB_STOR_TRANSPORT_ERROR
;
216 /* There are times we can optimize out this status read, but it
217 * doesn't hurt us to always do it now. */
218 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
219 FCM_STATUS_PACKET_LENGTH
, &partial
);
220 US_DEBUGP("foo Status result %d %u\n", result
, partial
);
221 if (result
!= USB_STOR_XFER_GOOD
)
222 return USB_STOR_TRANSPORT_ERROR
;
224 US_DEBUG(pdump ((void *) fst
, partial
));
226 /* The firmware will time-out commands after 20 seconds. Some commands
227 * can legitimately take longer than this, so we use a different
228 * command that only waits for the interrupt and then sends status,
229 * without having to send a new ATAPI command to the device.
231 * NOTE: There is some indication that a data transfer after a timeout
232 * may not work, but that is a condition that should never happen.
234 while (fst
->Status
& FCM_STATUS_BUSY
) {
235 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
236 US_DEBUGP("fst->Status is %x\n", fst
->Status
);
238 /* Get the status again */
239 fcb
->Type
= FCM_PACKET_STATUS
;
241 memset (fcb
->Atapi
, 0, sizeof(fcb
->Atapi
));
242 memset (fcb
->Filler
, 0, sizeof (fcb
->Filler
));
245 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fcb
,
246 FCM_PACKET_LENGTH
, NULL
);
248 /* The Freecom device will only fail if there is something
249 * wrong in USB land. It returns the status in its own
250 * registers, which come back in the bulk pipe.
252 if (result
!= USB_STOR_XFER_GOOD
) {
253 US_DEBUGP ("freecom transport error\n");
254 return USB_STOR_TRANSPORT_ERROR
;
258 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
259 FCM_STATUS_PACKET_LENGTH
, &partial
);
261 US_DEBUGP("bar Status result %d %u\n", result
, partial
);
262 if (result
!= USB_STOR_XFER_GOOD
)
263 return USB_STOR_TRANSPORT_ERROR
;
265 US_DEBUG(pdump ((void *) fst
, partial
));
269 return USB_STOR_TRANSPORT_ERROR
;
270 if ((fst
->Status
& 1) != 0) {
271 US_DEBUGP("operation failed\n");
272 return USB_STOR_TRANSPORT_FAILED
;
275 /* The device might not have as much data available as we
276 * requested. If you ask for more than the device has, this reads
277 * and such will hang. */
278 US_DEBUGP("Device indicates that it has %d bytes available\n",
279 le16_to_cpu (fst
->Count
));
280 US_DEBUGP("SCSI requested %d\n", scsi_bufflen(srb
));
282 /* Find the length we desire to read. */
283 switch (srb
->cmnd
[0]) {
285 case REQUEST_SENSE
: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
288 length
= le16_to_cpu(fst
->Count
);
291 length
= scsi_bufflen(srb
);
294 /* verify that this amount is legal */
295 if (length
> scsi_bufflen(srb
)) {
296 length
= scsi_bufflen(srb
);
297 US_DEBUGP("Truncating request to match buffer length: %d\n", length
);
300 /* What we do now depends on what direction the data is supposed to
303 switch (us
->srb
->sc_data_direction
) {
304 case DMA_FROM_DEVICE
:
305 /* catch bogus "read 0 length" case */
308 /* Make sure that the status indicates that the device
309 * wants data as well. */
310 if ((fst
->Status
& DRQ_STAT
) == 0 || (fst
->Reason
& 3) != 2) {
311 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
312 return USB_STOR_TRANSPORT_FAILED
;
314 result
= freecom_readdata (srb
, us
, ipipe
, opipe
, length
);
315 if (result
!= USB_STOR_TRANSPORT_GOOD
)
318 US_DEBUGP("FCM: Waiting for status\n");
319 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
320 FCM_PACKET_LENGTH
, &partial
);
321 US_DEBUG(pdump ((void *) fst
, partial
));
323 if (partial
!= 4 || result
> USB_STOR_XFER_SHORT
)
324 return USB_STOR_TRANSPORT_ERROR
;
325 if ((fst
->Status
& ERR_STAT
) != 0) {
326 US_DEBUGP("operation failed\n");
327 return USB_STOR_TRANSPORT_FAILED
;
329 if ((fst
->Reason
& 3) != 3) {
330 US_DEBUGP("Drive seems still hungry\n");
331 return USB_STOR_TRANSPORT_FAILED
;
333 US_DEBUGP("Transfer happy\n");
337 /* catch bogus "write 0 length" case */
340 /* Make sure the status indicates that the device wants to
343 result
= freecom_writedata (srb
, us
, ipipe
, opipe
, length
);
344 if (result
!= USB_STOR_TRANSPORT_GOOD
)
347 US_DEBUGP("FCM: Waiting for status\n");
348 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
349 FCM_PACKET_LENGTH
, &partial
);
351 if (partial
!= 4 || result
> USB_STOR_XFER_SHORT
)
352 return USB_STOR_TRANSPORT_ERROR
;
353 if ((fst
->Status
& ERR_STAT
) != 0) {
354 US_DEBUGP("operation failed\n");
355 return USB_STOR_TRANSPORT_FAILED
;
357 if ((fst
->Reason
& 3) != 3) {
358 US_DEBUGP("Drive seems still hungry\n");
359 return USB_STOR_TRANSPORT_FAILED
;
362 US_DEBUGP("Transfer happy\n");
367 /* Easy, do nothing. */
371 /* should never hit here -- filtered in usb.c */
372 US_DEBUGP ("freecom unimplemented direction: %d\n",
373 us
->srb
->sc_data_direction
);
374 // Return fail, SCSI seems to handle this better.
375 return USB_STOR_TRANSPORT_FAILED
;
379 return USB_STOR_TRANSPORT_GOOD
;
383 freecom_init (struct us_data
*us
)
386 char *buffer
= us
->iobuf
;
388 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
389 * all our packets. No need to allocate any extra buffer space.
392 result
= usb_stor_control_msg(us
, us
->recv_ctrl_pipe
,
393 0x4c, 0xc0, 0x4346, 0x0, buffer
, 0x20, 3*HZ
);
395 US_DEBUGP("String returned from FC init is: %s\n", buffer
);
397 /* Special thanks to the people at Freecom for providing me with
398 * this "magic sequence", which they use in their Windows and MacOS
399 * drivers to make sure that all the attached perhiperals are
404 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
405 0x4d, 0x40, 0x24d8, 0x0, NULL
, 0x0, 3*HZ
);
406 US_DEBUGP("result from activate reset is %d\n", result
);
412 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
413 0x4d, 0x40, 0x24f8, 0x0, NULL
, 0x0, 3*HZ
);
414 US_DEBUGP("result from clear reset is %d\n", result
);
419 return USB_STOR_TRANSPORT_GOOD
;
422 int usb_stor_freecom_reset(struct us_data
*us
)
424 printk (KERN_CRIT
"freecom reset called\n");
426 /* We don't really have this feature. */
430 #ifdef CONFIG_USB_STORAGE_DEBUG
431 static void pdump (void *ibuffer
, int length
)
433 static char line
[80];
435 unsigned char *buffer
= (unsigned char *) ibuffer
;
440 for (i
= 0; i
< length
; i
++) {
443 offset
+= sprintf (line
+offset
, " - ");
444 for (j
= i
- 16; j
< i
; j
++) {
445 if (buffer
[j
] >= 32 && buffer
[j
] <= 126)
446 line
[offset
++] = buffer
[j
];
448 line
[offset
++] = '.';
451 US_DEBUGP("%s\n", line
);
454 offset
+= sprintf (line
+offset
, "%08x:", i
);
456 else if ((i
& 7) == 0) {
457 offset
+= sprintf (line
+offset
, " -");
459 offset
+= sprintf (line
+offset
, " %02x", buffer
[i
] & 0xff);
462 /* Add the last "chunk" of data. */
463 from
= (length
- 1) % 16;
464 base
= ((length
- 1) / 16) * 16;
466 for (i
= from
+ 1; i
< 16; i
++)
467 offset
+= sprintf (line
+offset
, " ");
469 offset
+= sprintf (line
+offset
, " ");
470 offset
+= sprintf (line
+offset
, " - ");
472 for (i
= 0; i
<= from
; i
++) {
473 if (buffer
[base
+i
] >= 32 && buffer
[base
+i
] <= 126)
474 line
[offset
++] = buffer
[base
+i
];
476 line
[offset
++] = '.';
479 US_DEBUGP("%s\n", line
);