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 #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
106 static int init_freecom(struct us_data *us);
110 * The table of devices
112 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
113 vendorName, productName, useProtocol, useTransport, \
114 initFunction, flags) \
115 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
116 .driver_info = (flags)|(USB_US_TYPE_STOR<<24) }
118 struct usb_device_id freecom_usb_ids[] = {
119 # include "unusual_freecom.h"
120 { } /* Terminating entry */
122 MODULE_DEVICE_TABLE(usb, freecom_usb_ids);
129 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
130 vendor_name, product_name, use_protocol, use_transport, \
131 init_function, Flags) \
133 .vendorName = vendor_name, \
134 .productName = product_name, \
135 .useProtocol = use_protocol, \
136 .useTransport = use_transport, \
137 .initFunction = init_function, \
140 static struct us_unusual_dev freecom_unusual_dev_list[] = {
141 # include "unusual_freecom.h"
142 { } /* Terminating entry */
148 freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
149 unsigned int ipipe, unsigned int opipe, int count)
151 struct freecom_xfer_wrap *fxfr =
152 (struct freecom_xfer_wrap *) us->iobuf;
155 fxfr->Type = FCM_PACKET_INPUT | 0x00;
156 fxfr->Timeout = 0; /* Short timeout for debugging. */
157 fxfr->Count = cpu_to_le32 (count);
158 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
160 US_DEBUGP("Read data Freecom! (c=%d)\n", count);
162 /* Issue the transfer command. */
163 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
164 FCM_PACKET_LENGTH, NULL);
165 if (result != USB_STOR_XFER_GOOD) {
166 US_DEBUGP ("Freecom readdata transport error\n");
167 return USB_STOR_TRANSPORT_ERROR;
170 /* Now transfer all of our blocks. */
171 US_DEBUGP("Start of read\n");
172 result = usb_stor_bulk_srb(us, ipipe, srb);
173 US_DEBUGP("freecom_readdata done!\n");
175 if (result > USB_STOR_XFER_SHORT)
176 return USB_STOR_TRANSPORT_ERROR;
177 return USB_STOR_TRANSPORT_GOOD;
181 freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
182 int unsigned ipipe, unsigned int opipe, int count)
184 struct freecom_xfer_wrap *fxfr =
185 (struct freecom_xfer_wrap *) us->iobuf;
188 fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
189 fxfr->Timeout = 0; /* Short timeout for debugging. */
190 fxfr->Count = cpu_to_le32 (count);
191 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
193 US_DEBUGP("Write data Freecom! (c=%d)\n", count);
195 /* Issue the transfer command. */
196 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
197 FCM_PACKET_LENGTH, NULL);
198 if (result != USB_STOR_XFER_GOOD) {
199 US_DEBUGP ("Freecom writedata transport error\n");
200 return USB_STOR_TRANSPORT_ERROR;
203 /* Now transfer all of our blocks. */
204 US_DEBUGP("Start of write\n");
205 result = usb_stor_bulk_srb(us, opipe, srb);
207 US_DEBUGP("freecom_writedata done!\n");
208 if (result > USB_STOR_XFER_SHORT)
209 return USB_STOR_TRANSPORT_ERROR;
210 return USB_STOR_TRANSPORT_GOOD;
214 * Transport for the Freecom USB/IDE adaptor.
217 static int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
219 struct freecom_cb_wrap *fcb;
220 struct freecom_status *fst;
221 unsigned int ipipe, opipe; /* We need both pipes. */
223 unsigned int partial;
226 fcb = (struct freecom_cb_wrap *) us->iobuf;
227 fst = (struct freecom_status *) us->iobuf;
229 US_DEBUGP("Freecom TRANSPORT STARTED\n");
231 /* Get handles for both transports. */
232 opipe = us->send_bulk_pipe;
233 ipipe = us->recv_bulk_pipe;
235 /* The ATAPI Command always goes out first. */
236 fcb->Type = FCM_PACKET_ATAPI | 0x00;
238 memcpy (fcb->Atapi, srb->cmnd, 12);
239 memset (fcb->Filler, 0, sizeof (fcb->Filler));
241 US_DEBUG(pdump (srb->cmnd, 12));
244 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
245 FCM_PACKET_LENGTH, NULL);
247 /* The Freecom device will only fail if there is something wrong in
248 * USB land. It returns the status in its own registers, which
249 * 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;
255 /* There are times we can optimize out this status read, but it
256 * doesn't hurt us to always do it now. */
257 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
258 FCM_STATUS_PACKET_LENGTH, &partial);
259 US_DEBUGP("foo 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));
265 /* The firmware will time-out commands after 20 seconds. Some commands
266 * can legitimately take longer than this, so we use a different
267 * command that only waits for the interrupt and then sends status,
268 * without having to send a new ATAPI command to the device.
270 * NOTE: There is some indication that a data transfer after a timeout
271 * may not work, but that is a condition that should never happen.
273 while (fst->Status & FCM_STATUS_BUSY) {
274 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
275 US_DEBUGP("fst->Status is %x\n", fst->Status);
277 /* Get the status again */
278 fcb->Type = FCM_PACKET_STATUS;
280 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
281 memset (fcb->Filler, 0, sizeof (fcb->Filler));
284 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
285 FCM_PACKET_LENGTH, NULL);
287 /* The Freecom device will only fail if there is something
288 * wrong in USB land. It returns the status in its own
289 * registers, which come back in the bulk pipe.
291 if (result != USB_STOR_XFER_GOOD) {
292 US_DEBUGP ("freecom transport error\n");
293 return USB_STOR_TRANSPORT_ERROR;
297 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
298 FCM_STATUS_PACKET_LENGTH, &partial);
300 US_DEBUGP("bar Status result %d %u\n", result, partial);
301 if (result != USB_STOR_XFER_GOOD)
302 return USB_STOR_TRANSPORT_ERROR;
304 US_DEBUG(pdump ((void *) fst, partial));
308 return USB_STOR_TRANSPORT_ERROR;
309 if ((fst->Status & 1) != 0) {
310 US_DEBUGP("operation failed\n");
311 return USB_STOR_TRANSPORT_FAILED;
314 /* The device might not have as much data available as we
315 * requested. If you ask for more than the device has, this reads
316 * and such will hang. */
317 US_DEBUGP("Device indicates that it has %d bytes available\n",
318 le16_to_cpu (fst->Count));
319 US_DEBUGP("SCSI requested %d\n", scsi_bufflen(srb));
321 /* Find the length we desire to read. */
322 switch (srb->cmnd[0]) {
324 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
327 length = le16_to_cpu(fst->Count);
330 length = scsi_bufflen(srb);
333 /* verify that this amount is legal */
334 if (length > scsi_bufflen(srb)) {
335 length = scsi_bufflen(srb);
336 US_DEBUGP("Truncating request to match buffer length: %d\n", length);
339 /* What we do now depends on what direction the data is supposed to
342 switch (us->srb->sc_data_direction) {
343 case DMA_FROM_DEVICE:
344 /* catch bogus "read 0 length" case */
347 /* Make sure that the status indicates that the device
348 * wants data as well. */
349 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
350 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
351 return USB_STOR_TRANSPORT_FAILED;
353 result = freecom_readdata (srb, us, ipipe, opipe, length);
354 if (result != USB_STOR_TRANSPORT_GOOD)
357 US_DEBUGP("FCM: Waiting for status\n");
358 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
359 FCM_PACKET_LENGTH, &partial);
360 US_DEBUG(pdump ((void *) fst, partial));
362 if (partial != 4 || result > USB_STOR_XFER_SHORT)
363 return USB_STOR_TRANSPORT_ERROR;
364 if ((fst->Status & ERR_STAT) != 0) {
365 US_DEBUGP("operation failed\n");
366 return USB_STOR_TRANSPORT_FAILED;
368 if ((fst->Reason & 3) != 3) {
369 US_DEBUGP("Drive seems still hungry\n");
370 return USB_STOR_TRANSPORT_FAILED;
372 US_DEBUGP("Transfer happy\n");
376 /* catch bogus "write 0 length" case */
379 /* Make sure the status indicates that the device wants to
382 result = freecom_writedata (srb, us, ipipe, opipe, length);
383 if (result != USB_STOR_TRANSPORT_GOOD)
386 US_DEBUGP("FCM: Waiting for status\n");
387 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
388 FCM_PACKET_LENGTH, &partial);
390 if (partial != 4 || result > USB_STOR_XFER_SHORT)
391 return USB_STOR_TRANSPORT_ERROR;
392 if ((fst->Status & ERR_STAT) != 0) {
393 US_DEBUGP("operation failed\n");
394 return USB_STOR_TRANSPORT_FAILED;
396 if ((fst->Reason & 3) != 3) {
397 US_DEBUGP("Drive seems still hungry\n");
398 return USB_STOR_TRANSPORT_FAILED;
401 US_DEBUGP("Transfer happy\n");
406 /* Easy, do nothing. */
410 /* should never hit here -- filtered in usb.c */
411 US_DEBUGP ("freecom unimplemented direction: %d\n",
412 us->srb->sc_data_direction);
413 // Return fail, SCSI seems to handle this better.
414 return USB_STOR_TRANSPORT_FAILED;
418 return USB_STOR_TRANSPORT_GOOD;
421 static int init_freecom(struct us_data *us)
424 char *buffer = us->iobuf;
426 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
427 * all our packets. No need to allocate any extra buffer space.
430 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
431 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
433 US_DEBUGP("String returned from FC init is: %s\n", buffer);
435 /* Special thanks to the people at Freecom for providing me with
436 * this "magic sequence", which they use in their Windows and MacOS
437 * drivers to make sure that all the attached perhiperals are
442 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
443 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
444 US_DEBUGP("result from activate reset is %d\n", result);
450 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
451 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
452 US_DEBUGP("result from clear reset is %d\n", result);
457 return USB_STOR_TRANSPORT_GOOD;
460 static int usb_stor_freecom_reset(struct us_data *us)
462 printk (KERN_CRIT "freecom reset called\n");
464 /* We don't really have this feature. */
468 #ifdef CONFIG_USB_STORAGE_DEBUG
469 static void pdump (void *ibuffer, int length)
471 static char line[80];
473 unsigned char *buffer = (unsigned char *) ibuffer;
478 for (i = 0; i < length; i++) {
481 offset += sprintf (line+offset, " - ");
482 for (j = i - 16; j < i; j++) {
483 if (buffer[j] >= 32 && buffer[j] <= 126)
484 line[offset++] = buffer[j];
486 line[offset++] = '.';
489 US_DEBUGP("%s\n", line);
492 offset += sprintf (line+offset, "%08x:", i);
494 else if ((i & 7) == 0) {
495 offset += sprintf (line+offset, " -");
497 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
500 /* Add the last "chunk" of data. */
501 from = (length - 1) % 16;
502 base = ((length - 1) / 16) * 16;
504 for (i = from + 1; i < 16; i++)
505 offset += sprintf (line+offset, " ");
507 offset += sprintf (line+offset, " ");
508 offset += sprintf (line+offset, " - ");
510 for (i = 0; i <= from; i++) {
511 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
512 line[offset++] = buffer[base+i];
514 line[offset++] = '.';
517 US_DEBUGP("%s\n", line);
522 static int freecom_probe(struct usb_interface *intf,
523 const struct usb_device_id *id)
528 result = usb_stor_probe1(&us, intf, id,
529 (id - freecom_usb_ids) + freecom_unusual_dev_list);
533 us->transport_name = "Freecom";
534 us->transport = freecom_transport;
535 us->transport_reset = usb_stor_freecom_reset;
538 result = usb_stor_probe2(us);
542 static struct usb_driver freecom_driver = {
543 .name = "ums-freecom",
544 .probe = freecom_probe,
545 .disconnect = usb_stor_disconnect,
546 .suspend = usb_stor_suspend,
547 .resume = usb_stor_resume,
548 .reset_resume = usb_stor_reset_resume,
549 .pre_reset = usb_stor_pre_reset,
550 .post_reset = usb_stor_post_reset,
551 .id_table = freecom_usb_ids,
555 static int __init freecom_init(void)
557 return usb_register(&freecom_driver);
560 static void __exit freecom_exit(void)
562 usb_deregister(&freecom_driver);
565 module_init(freecom_init);
566 module_exit(freecom_exit);