1 /* Driver for Freecom USB/IDE adaptor
3 * $Id: freecom.c,v 1.22 2002/04/22 03:39:43 mdharm Exp $
9 * Current development and maintenance by:
10 * (C) 2000 David Brown <usb-storage@davidb.org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 675 Mass Ave, Cambridge, MA 02139, USA.
26 * This driver was developed with information provided in FREECOM's USB
27 * Programmers Reference Guide. For further information contact Freecom
28 * (http://www.freecom.de/)
31 #include <linux/config.h>
32 #include <linux/hdreg.h>
34 #include <scsi/scsi.h>
35 #include <scsi/scsi_cmnd.h>
38 #include "transport.h"
43 #ifdef CONFIG_USB_STORAGE_DEBUG
44 static void pdump (void *, int);
47 /* Bits of HD_STATUS */
51 /* All of the outgoing packets are 64 bytes long. */
52 struct freecom_cb_wrap
{
53 u8 Type
; /* Command type. */
54 u8 Timeout
; /* Timeout in seconds. */
55 u8 Atapi
[12]; /* An ATAPI packet. */
56 u8 Filler
[50]; /* Padding Data. */
59 struct freecom_xfer_wrap
{
60 u8 Type
; /* Command type. */
61 u8 Timeout
; /* Timeout in seconds. */
62 __le32 Count
; /* Number of bytes to transfer. */
64 } __attribute__ ((packed
));
66 struct freecom_ide_out
{
67 u8 Type
; /* Type + IDE register. */
69 __le16 Value
; /* Value to write. */
73 struct freecom_ide_in
{
74 u8 Type
; /* Type | IDE register. */
78 struct freecom_status
{
85 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
87 #define FCM_INT_STATUS 0x02 /* INDEX_STAT */
88 #define FCM_STATUS_BUSY 0x80
90 /* These are the packet types. The low bit indicates that this command
91 * should wait for an interrupt. */
92 #define FCM_PACKET_ATAPI 0x21
93 #define FCM_PACKET_STATUS 0x20
95 /* Receive data from the IDE interface. The ATAPI packet has already
96 * waited, so the data should be immediately available. */
97 #define FCM_PACKET_INPUT 0x81
99 /* Send data to the IDE interface. */
100 #define FCM_PACKET_OUTPUT 0x01
102 /* Write a value to an ide register. Or the ide register to write after
103 * munging the address a bit. */
104 #define FCM_PACKET_IDE_WRITE 0x40
105 #define FCM_PACKET_IDE_READ 0xC0
107 /* All packets (except for status) are 64 bytes long. */
108 #define FCM_PACKET_LENGTH 64
109 #define FCM_STATUS_PACKET_LENGTH 4
112 freecom_readdata (struct scsi_cmnd
*srb
, struct us_data
*us
,
113 unsigned int ipipe
, unsigned int opipe
, int count
)
115 struct freecom_xfer_wrap
*fxfr
=
116 (struct freecom_xfer_wrap
*) us
->iobuf
;
119 fxfr
->Type
= FCM_PACKET_INPUT
| 0x00;
120 fxfr
->Timeout
= 0; /* Short timeout for debugging. */
121 fxfr
->Count
= cpu_to_le32 (count
);
122 memset (fxfr
->Pad
, 0, sizeof (fxfr
->Pad
));
124 US_DEBUGP("Read data Freecom! (c=%d)\n", count
);
126 /* Issue the transfer command. */
127 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fxfr
,
128 FCM_PACKET_LENGTH
, NULL
);
129 if (result
!= USB_STOR_XFER_GOOD
) {
130 US_DEBUGP ("Freecom readdata transport error\n");
131 return USB_STOR_TRANSPORT_ERROR
;
134 /* Now transfer all of our blocks. */
135 US_DEBUGP("Start of read\n");
136 result
= usb_stor_bulk_transfer_sg(us
, ipipe
, srb
->request_buffer
,
137 count
, srb
->use_sg
, &srb
->resid
);
138 US_DEBUGP("freecom_readdata done!\n");
140 if (result
> USB_STOR_XFER_SHORT
)
141 return USB_STOR_TRANSPORT_ERROR
;
142 return USB_STOR_TRANSPORT_GOOD
;
146 freecom_writedata (struct scsi_cmnd
*srb
, struct us_data
*us
,
147 int unsigned ipipe
, unsigned int opipe
, int count
)
149 struct freecom_xfer_wrap
*fxfr
=
150 (struct freecom_xfer_wrap
*) us
->iobuf
;
153 fxfr
->Type
= FCM_PACKET_OUTPUT
| 0x00;
154 fxfr
->Timeout
= 0; /* Short timeout for debugging. */
155 fxfr
->Count
= cpu_to_le32 (count
);
156 memset (fxfr
->Pad
, 0, sizeof (fxfr
->Pad
));
158 US_DEBUGP("Write data Freecom! (c=%d)\n", count
);
160 /* Issue the transfer command. */
161 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fxfr
,
162 FCM_PACKET_LENGTH
, NULL
);
163 if (result
!= USB_STOR_XFER_GOOD
) {
164 US_DEBUGP ("Freecom writedata transport error\n");
165 return USB_STOR_TRANSPORT_ERROR
;
168 /* Now transfer all of our blocks. */
169 US_DEBUGP("Start of write\n");
170 result
= usb_stor_bulk_transfer_sg(us
, opipe
, srb
->request_buffer
,
171 count
, srb
->use_sg
, &srb
->resid
);
173 US_DEBUGP("freecom_writedata done!\n");
174 if (result
> USB_STOR_XFER_SHORT
)
175 return USB_STOR_TRANSPORT_ERROR
;
176 return USB_STOR_TRANSPORT_GOOD
;
180 * Transport for the Freecom USB/IDE adaptor.
183 int freecom_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
185 struct freecom_cb_wrap
*fcb
;
186 struct freecom_status
*fst
;
187 unsigned int ipipe
, opipe
; /* We need both pipes. */
189 unsigned int partial
;
192 fcb
= (struct freecom_cb_wrap
*) us
->iobuf
;
193 fst
= (struct freecom_status
*) us
->iobuf
;
195 US_DEBUGP("Freecom TRANSPORT STARTED\n");
197 /* Get handles for both transports. */
198 opipe
= us
->send_bulk_pipe
;
199 ipipe
= us
->recv_bulk_pipe
;
201 /* The ATAPI Command always goes out first. */
202 fcb
->Type
= FCM_PACKET_ATAPI
| 0x00;
204 memcpy (fcb
->Atapi
, srb
->cmnd
, 12);
205 memset (fcb
->Filler
, 0, sizeof (fcb
->Filler
));
207 US_DEBUG(pdump (srb
->cmnd
, 12));
210 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fcb
,
211 FCM_PACKET_LENGTH
, NULL
);
213 /* The Freecom device will only fail if there is something wrong in
214 * USB land. It returns the status in its own registers, which
215 * come back in the bulk pipe. */
216 if (result
!= USB_STOR_XFER_GOOD
) {
217 US_DEBUGP ("freecom transport error\n");
218 return USB_STOR_TRANSPORT_ERROR
;
221 /* There are times we can optimize out this status read, but it
222 * doesn't hurt us to always do it now. */
223 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
224 FCM_STATUS_PACKET_LENGTH
, &partial
);
225 US_DEBUGP("foo Status result %d %u\n", result
, partial
);
226 if (result
!= USB_STOR_XFER_GOOD
)
227 return USB_STOR_TRANSPORT_ERROR
;
229 US_DEBUG(pdump ((void *) fst
, partial
));
231 /* The firmware will time-out commands after 20 seconds. Some commands
232 * can legitimately take longer than this, so we use a different
233 * command that only waits for the interrupt and then sends status,
234 * without having to send a new ATAPI command to the device.
236 * NOTE: There is some indication that a data transfer after a timeout
237 * may not work, but that is a condition that should never happen.
239 while (fst
->Status
& FCM_STATUS_BUSY
) {
240 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
241 US_DEBUGP("fst->Status is %x\n", fst
->Status
);
243 /* Get the status again */
244 fcb
->Type
= FCM_PACKET_STATUS
;
246 memset (fcb
->Atapi
, 0, sizeof(fcb
->Atapi
));
247 memset (fcb
->Filler
, 0, sizeof (fcb
->Filler
));
250 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fcb
,
251 FCM_PACKET_LENGTH
, NULL
);
253 /* The Freecom device will only fail if there is something
254 * wrong in USB land. It returns the status in its own
255 * registers, which come back in the bulk pipe.
257 if (result
!= USB_STOR_XFER_GOOD
) {
258 US_DEBUGP ("freecom transport error\n");
259 return USB_STOR_TRANSPORT_ERROR
;
263 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
264 FCM_STATUS_PACKET_LENGTH
, &partial
);
266 US_DEBUGP("bar Status result %d %u\n", result
, partial
);
267 if (result
!= USB_STOR_XFER_GOOD
)
268 return USB_STOR_TRANSPORT_ERROR
;
270 US_DEBUG(pdump ((void *) fst
, partial
));
274 return USB_STOR_TRANSPORT_ERROR
;
275 if ((fst
->Status
& 1) != 0) {
276 US_DEBUGP("operation failed\n");
277 return USB_STOR_TRANSPORT_FAILED
;
280 /* The device might not have as much data available as we
281 * requested. If you ask for more than the device has, this reads
282 * and such will hang. */
283 US_DEBUGP("Device indicates that it has %d bytes available\n",
284 le16_to_cpu (fst
->Count
));
285 US_DEBUGP("SCSI requested %d\n", srb
->request_bufflen
);
287 /* Find the length we desire to read. */
288 switch (srb
->cmnd
[0]) {
290 case REQUEST_SENSE
: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
293 length
= le16_to_cpu(fst
->Count
);
296 length
= srb
->request_bufflen
;
299 /* verify that this amount is legal */
300 if (length
> srb
->request_bufflen
) {
301 length
= srb
->request_bufflen
;
302 US_DEBUGP("Truncating request to match buffer length: %d\n", length
);
305 /* What we do now depends on what direction the data is supposed to
308 switch (us
->srb
->sc_data_direction
) {
309 case DMA_FROM_DEVICE
:
310 /* catch bogus "read 0 length" case */
313 /* Make sure that the status indicates that the device
314 * wants data as well. */
315 if ((fst
->Status
& DRQ_STAT
) == 0 || (fst
->Reason
& 3) != 2) {
316 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
317 return USB_STOR_TRANSPORT_FAILED
;
319 result
= freecom_readdata (srb
, us
, ipipe
, opipe
, length
);
320 if (result
!= USB_STOR_TRANSPORT_GOOD
)
323 US_DEBUGP("FCM: Waiting for status\n");
324 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
325 FCM_PACKET_LENGTH
, &partial
);
326 US_DEBUG(pdump ((void *) fst
, partial
));
328 if (partial
!= 4 || result
> USB_STOR_XFER_SHORT
)
329 return USB_STOR_TRANSPORT_ERROR
;
330 if ((fst
->Status
& ERR_STAT
) != 0) {
331 US_DEBUGP("operation failed\n");
332 return USB_STOR_TRANSPORT_FAILED
;
334 if ((fst
->Reason
& 3) != 3) {
335 US_DEBUGP("Drive seems still hungry\n");
336 return USB_STOR_TRANSPORT_FAILED
;
338 US_DEBUGP("Transfer happy\n");
342 /* catch bogus "write 0 length" case */
345 /* Make sure the status indicates that the device wants to
348 result
= freecom_writedata (srb
, us
, ipipe
, opipe
, length
);
349 if (result
!= USB_STOR_TRANSPORT_GOOD
)
352 US_DEBUGP("FCM: Waiting for status\n");
353 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
354 FCM_PACKET_LENGTH
, &partial
);
356 if (partial
!= 4 || result
> USB_STOR_XFER_SHORT
)
357 return USB_STOR_TRANSPORT_ERROR
;
358 if ((fst
->Status
& ERR_STAT
) != 0) {
359 US_DEBUGP("operation failed\n");
360 return USB_STOR_TRANSPORT_FAILED
;
362 if ((fst
->Reason
& 3) != 3) {
363 US_DEBUGP("Drive seems still hungry\n");
364 return USB_STOR_TRANSPORT_FAILED
;
367 US_DEBUGP("Transfer happy\n");
372 /* Easy, do nothing. */
376 /* should never hit here -- filtered in usb.c */
377 US_DEBUGP ("freecom unimplemented direction: %d\n",
378 us
->srb
->sc_data_direction
);
379 // Return fail, SCSI seems to handle this better.
380 return USB_STOR_TRANSPORT_FAILED
;
384 return USB_STOR_TRANSPORT_GOOD
;
388 freecom_init (struct us_data
*us
)
391 char *buffer
= us
->iobuf
;
393 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
394 * all our packets. No need to allocate any extra buffer space.
397 result
= usb_stor_control_msg(us
, us
->recv_ctrl_pipe
,
398 0x4c, 0xc0, 0x4346, 0x0, buffer
, 0x20, 3*HZ
);
400 US_DEBUGP("String returned from FC init is: %s\n", buffer
);
402 /* Special thanks to the people at Freecom for providing me with
403 * this "magic sequence", which they use in their Windows and MacOS
404 * drivers to make sure that all the attached perhiperals are
409 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
410 0x4d, 0x40, 0x24d8, 0x0, NULL
, 0x0, 3*HZ
);
411 US_DEBUGP("result from activate reset is %d\n", result
);
417 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
418 0x4d, 0x40, 0x24f8, 0x0, NULL
, 0x0, 3*HZ
);
419 US_DEBUGP("result from clear reset is %d\n", result
);
424 return USB_STOR_TRANSPORT_GOOD
;
427 int usb_stor_freecom_reset(struct us_data
*us
)
429 printk (KERN_CRIT
"freecom reset called\n");
431 /* We don't really have this feature. */
435 #ifdef CONFIG_USB_STORAGE_DEBUG
436 static void pdump (void *ibuffer
, int length
)
438 static char line
[80];
440 unsigned char *buffer
= (unsigned char *) ibuffer
;
445 for (i
= 0; i
< length
; i
++) {
448 offset
+= sprintf (line
+offset
, " - ");
449 for (j
= i
- 16; j
< i
; j
++) {
450 if (buffer
[j
] >= 32 && buffer
[j
] <= 126)
451 line
[offset
++] = buffer
[j
];
453 line
[offset
++] = '.';
456 US_DEBUGP("%s\n", line
);
459 offset
+= sprintf (line
+offset
, "%08x:", i
);
461 else if ((i
& 7) == 0) {
462 offset
+= sprintf (line
+offset
, " -");
464 offset
+= sprintf (line
+offset
, " %02x", buffer
[i
] & 0xff);
467 /* Add the last "chunk" of data. */
468 from
= (length
- 1) % 16;
469 base
= ((length
- 1) / 16) * 16;
471 for (i
= from
+ 1; i
< 16; i
++)
472 offset
+= sprintf (line
+offset
, " ");
474 offset
+= sprintf (line
+offset
, " ");
475 offset
+= sprintf (line
+offset
, " - ");
477 for (i
= 0; i
<= from
; i
++) {
478 if (buffer
[base
+i
] >= 32 && buffer
[base
+i
] <= 126)
479 line
[offset
++] = buffer
[base
+i
];
481 line
[offset
++] = '.';
484 US_DEBUGP("%s\n", line
);