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/module.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
34 #include "transport.h"
38 MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor");
39 MODULE_AUTHOR("David Brown <usb-storage@davidb.org>");
40 MODULE_LICENSE("GPL");
42 #ifdef CONFIG_USB_STORAGE_DEBUG
43 static void pdump(struct us_data
*us
, void *ibuffer
, int length
);
46 /* Bits of HD_STATUS */
50 /* All of the outgoing packets are 64 bytes long. */
51 struct freecom_cb_wrap
{
52 u8 Type
; /* Command type. */
53 u8 Timeout
; /* Timeout in seconds. */
54 u8 Atapi
[12]; /* An ATAPI packet. */
55 u8 Filler
[50]; /* Padding Data. */
58 struct freecom_xfer_wrap
{
59 u8 Type
; /* Command type. */
60 u8 Timeout
; /* Timeout in seconds. */
61 __le32 Count
; /* Number of bytes to transfer. */
63 } __attribute__ ((packed
));
65 struct freecom_ide_out
{
66 u8 Type
; /* Type + IDE register. */
68 __le16 Value
; /* Value to write. */
72 struct freecom_ide_in
{
73 u8 Type
; /* Type | IDE register. */
77 struct freecom_status
{
84 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
86 #define FCM_INT_STATUS 0x02 /* INDEX_STAT */
87 #define FCM_STATUS_BUSY 0x80
89 /* These are the packet types. The low bit indicates that this command
90 * should wait for an interrupt. */
91 #define FCM_PACKET_ATAPI 0x21
92 #define FCM_PACKET_STATUS 0x20
94 /* Receive data from the IDE interface. The ATAPI packet has already
95 * waited, so the data should be immediately available. */
96 #define FCM_PACKET_INPUT 0x81
98 /* Send data to the IDE interface. */
99 #define FCM_PACKET_OUTPUT 0x01
101 /* Write a value to an ide register. Or the ide register to write after
102 * munging the address a bit. */
103 #define FCM_PACKET_IDE_WRITE 0x40
104 #define FCM_PACKET_IDE_READ 0xC0
106 /* All packets (except for status) are 64 bytes long. */
107 #define FCM_PACKET_LENGTH 64
108 #define FCM_STATUS_PACKET_LENGTH 4
110 static int init_freecom(struct us_data
*us
);
114 * The table of devices
116 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
117 vendorName, productName, useProtocol, useTransport, \
118 initFunction, flags) \
119 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
120 .driver_info = (flags) }
122 static struct usb_device_id freecom_usb_ids
[] = {
123 # include "unusual_freecom.h"
124 { } /* Terminating entry */
126 MODULE_DEVICE_TABLE(usb
, freecom_usb_ids
);
133 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
134 vendor_name, product_name, use_protocol, use_transport, \
135 init_function, Flags) \
137 .vendorName = vendor_name, \
138 .productName = product_name, \
139 .useProtocol = use_protocol, \
140 .useTransport = use_transport, \
141 .initFunction = init_function, \
144 static struct us_unusual_dev freecom_unusual_dev_list
[] = {
145 # include "unusual_freecom.h"
146 { } /* Terminating entry */
152 freecom_readdata (struct scsi_cmnd
*srb
, struct us_data
*us
,
153 unsigned int ipipe
, unsigned int opipe
, int count
)
155 struct freecom_xfer_wrap
*fxfr
=
156 (struct freecom_xfer_wrap
*) us
->iobuf
;
159 fxfr
->Type
= FCM_PACKET_INPUT
| 0x00;
160 fxfr
->Timeout
= 0; /* Short timeout for debugging. */
161 fxfr
->Count
= cpu_to_le32 (count
);
162 memset (fxfr
->Pad
, 0, sizeof (fxfr
->Pad
));
164 usb_stor_dbg(us
, "Read data Freecom! (c=%d)\n", count
);
166 /* Issue the transfer command. */
167 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fxfr
,
168 FCM_PACKET_LENGTH
, NULL
);
169 if (result
!= USB_STOR_XFER_GOOD
) {
170 usb_stor_dbg(us
, "Freecom readdata transport error\n");
171 return USB_STOR_TRANSPORT_ERROR
;
174 /* Now transfer all of our blocks. */
175 usb_stor_dbg(us
, "Start of read\n");
176 result
= usb_stor_bulk_srb(us
, ipipe
, srb
);
177 usb_stor_dbg(us
, "freecom_readdata done!\n");
179 if (result
> USB_STOR_XFER_SHORT
)
180 return USB_STOR_TRANSPORT_ERROR
;
181 return USB_STOR_TRANSPORT_GOOD
;
185 freecom_writedata (struct scsi_cmnd
*srb
, struct us_data
*us
,
186 int unsigned ipipe
, unsigned int opipe
, int count
)
188 struct freecom_xfer_wrap
*fxfr
=
189 (struct freecom_xfer_wrap
*) us
->iobuf
;
192 fxfr
->Type
= FCM_PACKET_OUTPUT
| 0x00;
193 fxfr
->Timeout
= 0; /* Short timeout for debugging. */
194 fxfr
->Count
= cpu_to_le32 (count
);
195 memset (fxfr
->Pad
, 0, sizeof (fxfr
->Pad
));
197 usb_stor_dbg(us
, "Write data Freecom! (c=%d)\n", count
);
199 /* Issue the transfer command. */
200 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fxfr
,
201 FCM_PACKET_LENGTH
, NULL
);
202 if (result
!= USB_STOR_XFER_GOOD
) {
203 usb_stor_dbg(us
, "Freecom writedata transport error\n");
204 return USB_STOR_TRANSPORT_ERROR
;
207 /* Now transfer all of our blocks. */
208 usb_stor_dbg(us
, "Start of write\n");
209 result
= usb_stor_bulk_srb(us
, opipe
, srb
);
211 usb_stor_dbg(us
, "freecom_writedata done!\n");
212 if (result
> USB_STOR_XFER_SHORT
)
213 return USB_STOR_TRANSPORT_ERROR
;
214 return USB_STOR_TRANSPORT_GOOD
;
218 * Transport for the Freecom USB/IDE adaptor.
221 static int freecom_transport(struct scsi_cmnd
*srb
, struct us_data
*us
)
223 struct freecom_cb_wrap
*fcb
;
224 struct freecom_status
*fst
;
225 unsigned int ipipe
, opipe
; /* We need both pipes. */
227 unsigned int partial
;
230 fcb
= (struct freecom_cb_wrap
*) us
->iobuf
;
231 fst
= (struct freecom_status
*) us
->iobuf
;
233 usb_stor_dbg(us
, "Freecom TRANSPORT STARTED\n");
235 /* Get handles for both transports. */
236 opipe
= us
->send_bulk_pipe
;
237 ipipe
= us
->recv_bulk_pipe
;
239 /* The ATAPI Command always goes out first. */
240 fcb
->Type
= FCM_PACKET_ATAPI
| 0x00;
242 memcpy (fcb
->Atapi
, srb
->cmnd
, 12);
243 memset (fcb
->Filler
, 0, sizeof (fcb
->Filler
));
245 US_DEBUG(pdump(us
, srb
->cmnd
, 12));
248 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fcb
,
249 FCM_PACKET_LENGTH
, NULL
);
251 /* The Freecom device will only fail if there is something wrong in
252 * USB land. It returns the status in its own registers, which
253 * come back in the bulk pipe. */
254 if (result
!= USB_STOR_XFER_GOOD
) {
255 usb_stor_dbg(us
, "freecom transport error\n");
256 return USB_STOR_TRANSPORT_ERROR
;
259 /* There are times we can optimize out this status read, but it
260 * doesn't hurt us to always do it now. */
261 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
262 FCM_STATUS_PACKET_LENGTH
, &partial
);
263 usb_stor_dbg(us
, "foo Status result %d %u\n", result
, partial
);
264 if (result
!= USB_STOR_XFER_GOOD
)
265 return USB_STOR_TRANSPORT_ERROR
;
267 US_DEBUG(pdump(us
, (void *)fst
, partial
));
269 /* The firmware will time-out commands after 20 seconds. Some commands
270 * can legitimately take longer than this, so we use a different
271 * command that only waits for the interrupt and then sends status,
272 * without having to send a new ATAPI command to the device.
274 * NOTE: There is some indication that a data transfer after a timeout
275 * may not work, but that is a condition that should never happen.
277 while (fst
->Status
& FCM_STATUS_BUSY
) {
278 usb_stor_dbg(us
, "20 second USB/ATAPI bridge TIMEOUT occurred!\n");
279 usb_stor_dbg(us
, "fst->Status is %x\n", fst
->Status
);
281 /* Get the status again */
282 fcb
->Type
= FCM_PACKET_STATUS
;
284 memset (fcb
->Atapi
, 0, sizeof(fcb
->Atapi
));
285 memset (fcb
->Filler
, 0, sizeof (fcb
->Filler
));
288 result
= usb_stor_bulk_transfer_buf (us
, opipe
, fcb
,
289 FCM_PACKET_LENGTH
, NULL
);
291 /* The Freecom device will only fail if there is something
292 * wrong in USB land. It returns the status in its own
293 * registers, which come back in the bulk pipe.
295 if (result
!= USB_STOR_XFER_GOOD
) {
296 usb_stor_dbg(us
, "freecom transport error\n");
297 return USB_STOR_TRANSPORT_ERROR
;
301 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
302 FCM_STATUS_PACKET_LENGTH
, &partial
);
304 usb_stor_dbg(us
, "bar Status result %d %u\n", result
, partial
);
305 if (result
!= USB_STOR_XFER_GOOD
)
306 return USB_STOR_TRANSPORT_ERROR
;
308 US_DEBUG(pdump(us
, (void *)fst
, partial
));
312 return USB_STOR_TRANSPORT_ERROR
;
313 if ((fst
->Status
& 1) != 0) {
314 usb_stor_dbg(us
, "operation failed\n");
315 return USB_STOR_TRANSPORT_FAILED
;
318 /* The device might not have as much data available as we
319 * requested. If you ask for more than the device has, this reads
320 * and such will hang. */
321 usb_stor_dbg(us
, "Device indicates that it has %d bytes available\n",
322 le16_to_cpu(fst
->Count
));
323 usb_stor_dbg(us
, "SCSI requested %d\n", scsi_bufflen(srb
));
325 /* Find the length we desire to read. */
326 switch (srb
->cmnd
[0]) {
328 case REQUEST_SENSE
: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
331 length
= le16_to_cpu(fst
->Count
);
334 length
= scsi_bufflen(srb
);
337 /* verify that this amount is legal */
338 if (length
> scsi_bufflen(srb
)) {
339 length
= scsi_bufflen(srb
);
340 usb_stor_dbg(us
, "Truncating request to match buffer length: %d\n",
344 /* What we do now depends on what direction the data is supposed to
347 switch (us
->srb
->sc_data_direction
) {
348 case DMA_FROM_DEVICE
:
349 /* catch bogus "read 0 length" case */
352 /* Make sure that the status indicates that the device
353 * wants data as well. */
354 if ((fst
->Status
& DRQ_STAT
) == 0 || (fst
->Reason
& 3) != 2) {
355 usb_stor_dbg(us
, "SCSI wants data, drive doesn't have any\n");
356 return USB_STOR_TRANSPORT_FAILED
;
358 result
= freecom_readdata (srb
, us
, ipipe
, opipe
, length
);
359 if (result
!= USB_STOR_TRANSPORT_GOOD
)
362 usb_stor_dbg(us
, "Waiting for status\n");
363 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
364 FCM_PACKET_LENGTH
, &partial
);
365 US_DEBUG(pdump(us
, (void *)fst
, partial
));
367 if (partial
!= 4 || result
> USB_STOR_XFER_SHORT
)
368 return USB_STOR_TRANSPORT_ERROR
;
369 if ((fst
->Status
& ERR_STAT
) != 0) {
370 usb_stor_dbg(us
, "operation failed\n");
371 return USB_STOR_TRANSPORT_FAILED
;
373 if ((fst
->Reason
& 3) != 3) {
374 usb_stor_dbg(us
, "Drive seems still hungry\n");
375 return USB_STOR_TRANSPORT_FAILED
;
377 usb_stor_dbg(us
, "Transfer happy\n");
381 /* catch bogus "write 0 length" case */
384 /* Make sure the status indicates that the device wants to
387 result
= freecom_writedata (srb
, us
, ipipe
, opipe
, length
);
388 if (result
!= USB_STOR_TRANSPORT_GOOD
)
391 usb_stor_dbg(us
, "Waiting for status\n");
392 result
= usb_stor_bulk_transfer_buf (us
, ipipe
, fst
,
393 FCM_PACKET_LENGTH
, &partial
);
395 if (partial
!= 4 || result
> USB_STOR_XFER_SHORT
)
396 return USB_STOR_TRANSPORT_ERROR
;
397 if ((fst
->Status
& ERR_STAT
) != 0) {
398 usb_stor_dbg(us
, "operation failed\n");
399 return USB_STOR_TRANSPORT_FAILED
;
401 if ((fst
->Reason
& 3) != 3) {
402 usb_stor_dbg(us
, "Drive seems still hungry\n");
403 return USB_STOR_TRANSPORT_FAILED
;
406 usb_stor_dbg(us
, "Transfer happy\n");
411 /* Easy, do nothing. */
415 /* should never hit here -- filtered in usb.c */
416 usb_stor_dbg(us
, "freecom unimplemented direction: %d\n",
417 us
->srb
->sc_data_direction
);
418 /* Return fail, SCSI seems to handle this better. */
419 return USB_STOR_TRANSPORT_FAILED
;
423 return USB_STOR_TRANSPORT_GOOD
;
426 static int init_freecom(struct us_data
*us
)
429 char *buffer
= us
->iobuf
;
431 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
432 * all our packets. No need to allocate any extra buffer space.
435 result
= usb_stor_control_msg(us
, us
->recv_ctrl_pipe
,
436 0x4c, 0xc0, 0x4346, 0x0, buffer
, 0x20, 3*HZ
);
438 usb_stor_dbg(us
, "String returned from FC init is: %s\n", buffer
);
440 /* Special thanks to the people at Freecom for providing me with
441 * this "magic sequence", which they use in their Windows and MacOS
442 * drivers to make sure that all the attached perhiperals are
447 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
448 0x4d, 0x40, 0x24d8, 0x0, NULL
, 0x0, 3*HZ
);
449 usb_stor_dbg(us
, "result from activate reset is %d\n", result
);
455 result
= usb_stor_control_msg(us
, us
->send_ctrl_pipe
,
456 0x4d, 0x40, 0x24f8, 0x0, NULL
, 0x0, 3*HZ
);
457 usb_stor_dbg(us
, "result from clear reset is %d\n", result
);
462 return USB_STOR_TRANSPORT_GOOD
;
465 static int usb_stor_freecom_reset(struct us_data
*us
)
467 printk (KERN_CRIT
"freecom reset called\n");
469 /* We don't really have this feature. */
473 #ifdef CONFIG_USB_STORAGE_DEBUG
474 static void pdump(struct us_data
*us
, void *ibuffer
, int length
)
476 static char line
[80];
478 unsigned char *buffer
= (unsigned char *) ibuffer
;
483 for (i
= 0; i
< length
; i
++) {
486 offset
+= sprintf (line
+offset
, " - ");
487 for (j
= i
- 16; j
< i
; j
++) {
488 if (buffer
[j
] >= 32 && buffer
[j
] <= 126)
489 line
[offset
++] = buffer
[j
];
491 line
[offset
++] = '.';
494 usb_stor_dbg(us
, "%s\n", line
);
497 offset
+= sprintf (line
+offset
, "%08x:", i
);
498 } else if ((i
& 7) == 0) {
499 offset
+= sprintf (line
+offset
, " -");
501 offset
+= sprintf (line
+offset
, " %02x", buffer
[i
] & 0xff);
504 /* Add the last "chunk" of data. */
505 from
= (length
- 1) % 16;
506 base
= ((length
- 1) / 16) * 16;
508 for (i
= from
+ 1; i
< 16; i
++)
509 offset
+= sprintf (line
+offset
, " ");
511 offset
+= sprintf (line
+offset
, " ");
512 offset
+= sprintf (line
+offset
, " - ");
514 for (i
= 0; i
<= from
; i
++) {
515 if (buffer
[base
+i
] >= 32 && buffer
[base
+i
] <= 126)
516 line
[offset
++] = buffer
[base
+i
];
518 line
[offset
++] = '.';
521 usb_stor_dbg(us
, "%s\n", line
);
526 static int freecom_probe(struct usb_interface
*intf
,
527 const struct usb_device_id
*id
)
532 result
= usb_stor_probe1(&us
, intf
, id
,
533 (id
- freecom_usb_ids
) + freecom_unusual_dev_list
);
537 us
->transport_name
= "Freecom";
538 us
->transport
= freecom_transport
;
539 us
->transport_reset
= usb_stor_freecom_reset
;
542 result
= usb_stor_probe2(us
);
546 static struct usb_driver freecom_driver
= {
547 .name
= "ums-freecom",
548 .probe
= freecom_probe
,
549 .disconnect
= usb_stor_disconnect
,
550 .suspend
= usb_stor_suspend
,
551 .resume
= usb_stor_resume
,
552 .reset_resume
= usb_stor_reset_resume
,
553 .pre_reset
= usb_stor_pre_reset
,
554 .post_reset
= usb_stor_post_reset
,
555 .id_table
= freecom_usb_ids
,
560 module_usb_driver(freecom_driver
);