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/hdreg.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_cmnd.h>
37 #include "transport.h"
42 #ifdef CONFIG_USB_STORAGE_DEBUG
43 static void pdump (void *, int);
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
111 freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
112 unsigned int ipipe, unsigned int opipe, int count)
114 struct freecom_xfer_wrap *fxfr =
115 (struct freecom_xfer_wrap *) us->iobuf;
118 fxfr->Type = FCM_PACKET_INPUT | 0x00;
119 fxfr->Timeout = 0; /* Short timeout for debugging. */
120 fxfr->Count = cpu_to_le32 (count);
121 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
123 US_DEBUGP("Read data Freecom! (c=%d)\n", count);
125 /* Issue the transfer command. */
126 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
127 FCM_PACKET_LENGTH, NULL);
128 if (result != USB_STOR_XFER_GOOD) {
129 US_DEBUGP ("Freecom readdata transport error\n");
130 return USB_STOR_TRANSPORT_ERROR;
133 /* Now transfer all of our blocks. */
134 US_DEBUGP("Start of read\n");
135 result = usb_stor_bulk_transfer_sg(us, ipipe, srb->request_buffer,
136 count, srb->use_sg, &srb->resid);
137 US_DEBUGP("freecom_readdata done!\n");
139 if (result > USB_STOR_XFER_SHORT)
140 return USB_STOR_TRANSPORT_ERROR;
141 return USB_STOR_TRANSPORT_GOOD;
145 freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
146 int unsigned ipipe, unsigned int opipe, int count)
148 struct freecom_xfer_wrap *fxfr =
149 (struct freecom_xfer_wrap *) us->iobuf;
152 fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
153 fxfr->Timeout = 0; /* Short timeout for debugging. */
154 fxfr->Count = cpu_to_le32 (count);
155 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
157 US_DEBUGP("Write data Freecom! (c=%d)\n", count);
159 /* Issue the transfer command. */
160 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
161 FCM_PACKET_LENGTH, NULL);
162 if (result != USB_STOR_XFER_GOOD) {
163 US_DEBUGP ("Freecom writedata transport error\n");
164 return USB_STOR_TRANSPORT_ERROR;
167 /* Now transfer all of our blocks. */
168 US_DEBUGP("Start of write\n");
169 result = usb_stor_bulk_transfer_sg(us, opipe, srb->request_buffer,
170 count, srb->use_sg, &srb->resid);
172 US_DEBUGP("freecom_writedata done!\n");
173 if (result > USB_STOR_XFER_SHORT)
174 return USB_STOR_TRANSPORT_ERROR;
175 return USB_STOR_TRANSPORT_GOOD;
179 * Transport for the Freecom USB/IDE adaptor.
182 int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
184 struct freecom_cb_wrap *fcb;
185 struct freecom_status *fst;
186 unsigned int ipipe, opipe; /* We need both pipes. */
188 unsigned int partial;
191 fcb = (struct freecom_cb_wrap *) us->iobuf;
192 fst = (struct freecom_status *) us->iobuf;
194 US_DEBUGP("Freecom TRANSPORT STARTED\n");
196 /* Get handles for both transports. */
197 opipe = us->send_bulk_pipe;
198 ipipe = us->recv_bulk_pipe;
200 /* The ATAPI Command always goes out first. */
201 fcb->Type = FCM_PACKET_ATAPI | 0x00;
203 memcpy (fcb->Atapi, srb->cmnd, 12);
204 memset (fcb->Filler, 0, sizeof (fcb->Filler));
206 US_DEBUG(pdump (srb->cmnd, 12));
209 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
210 FCM_PACKET_LENGTH, NULL);
212 /* The Freecom device will only fail if there is something wrong in
213 * USB land. It returns the status in its own registers, which
214 * come back in the bulk pipe. */
215 if (result != USB_STOR_XFER_GOOD) {
216 US_DEBUGP ("freecom transport error\n");
217 return USB_STOR_TRANSPORT_ERROR;
220 /* There are times we can optimize out this status read, but it
221 * doesn't hurt us to always do it now. */
222 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
223 FCM_STATUS_PACKET_LENGTH, &partial);
224 US_DEBUGP("foo Status result %d %u\n", result, partial);
225 if (result != USB_STOR_XFER_GOOD)
226 return USB_STOR_TRANSPORT_ERROR;
228 US_DEBUG(pdump ((void *) fst, partial));
230 /* The firmware will time-out commands after 20 seconds. Some commands
231 * can legitimately take longer than this, so we use a different
232 * command that only waits for the interrupt and then sends status,
233 * without having to send a new ATAPI command to the device.
235 * NOTE: There is some indication that a data transfer after a timeout
236 * may not work, but that is a condition that should never happen.
238 while (fst->Status & FCM_STATUS_BUSY) {
239 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
240 US_DEBUGP("fst->Status is %x\n", fst->Status);
242 /* Get the status again */
243 fcb->Type = FCM_PACKET_STATUS;
245 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
246 memset (fcb->Filler, 0, sizeof (fcb->Filler));
249 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
250 FCM_PACKET_LENGTH, NULL);
252 /* The Freecom device will only fail if there is something
253 * wrong in USB land. It returns the status in its own
254 * registers, which come back in the bulk pipe.
256 if (result != USB_STOR_XFER_GOOD) {
257 US_DEBUGP ("freecom transport error\n");
258 return USB_STOR_TRANSPORT_ERROR;
262 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
263 FCM_STATUS_PACKET_LENGTH, &partial);
265 US_DEBUGP("bar Status result %d %u\n", result, partial);
266 if (result != USB_STOR_XFER_GOOD)
267 return USB_STOR_TRANSPORT_ERROR;
269 US_DEBUG(pdump ((void *) fst, partial));
273 return USB_STOR_TRANSPORT_ERROR;
274 if ((fst->Status & 1) != 0) {
275 US_DEBUGP("operation failed\n");
276 return USB_STOR_TRANSPORT_FAILED;
279 /* The device might not have as much data available as we
280 * requested. If you ask for more than the device has, this reads
281 * and such will hang. */
282 US_DEBUGP("Device indicates that it has %d bytes available\n",
283 le16_to_cpu (fst->Count));
284 US_DEBUGP("SCSI requested %d\n", srb->request_bufflen);
286 /* Find the length we desire to read. */
287 switch (srb->cmnd[0]) {
289 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
292 length = le16_to_cpu(fst->Count);
295 length = srb->request_bufflen;
298 /* verify that this amount is legal */
299 if (length > srb->request_bufflen) {
300 length = srb->request_bufflen;
301 US_DEBUGP("Truncating request to match buffer length: %d\n", length);
304 /* What we do now depends on what direction the data is supposed to
307 switch (us->srb->sc_data_direction) {
308 case DMA_FROM_DEVICE:
309 /* catch bogus "read 0 length" case */
312 /* Make sure that the status indicates that the device
313 * wants data as well. */
314 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
315 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
316 return USB_STOR_TRANSPORT_FAILED;
318 result = freecom_readdata (srb, us, ipipe, opipe, length);
319 if (result != USB_STOR_TRANSPORT_GOOD)
322 US_DEBUGP("FCM: Waiting for status\n");
323 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
324 FCM_PACKET_LENGTH, &partial);
325 US_DEBUG(pdump ((void *) fst, partial));
327 if (partial != 4 || result > USB_STOR_XFER_SHORT)
328 return USB_STOR_TRANSPORT_ERROR;
329 if ((fst->Status & ERR_STAT) != 0) {
330 US_DEBUGP("operation failed\n");
331 return USB_STOR_TRANSPORT_FAILED;
333 if ((fst->Reason & 3) != 3) {
334 US_DEBUGP("Drive seems still hungry\n");
335 return USB_STOR_TRANSPORT_FAILED;
337 US_DEBUGP("Transfer happy\n");
341 /* catch bogus "write 0 length" case */
344 /* Make sure the status indicates that the device wants to
347 result = freecom_writedata (srb, us, ipipe, opipe, length);
348 if (result != USB_STOR_TRANSPORT_GOOD)
351 US_DEBUGP("FCM: Waiting for status\n");
352 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
353 FCM_PACKET_LENGTH, &partial);
355 if (partial != 4 || result > USB_STOR_XFER_SHORT)
356 return USB_STOR_TRANSPORT_ERROR;
357 if ((fst->Status & ERR_STAT) != 0) {
358 US_DEBUGP("operation failed\n");
359 return USB_STOR_TRANSPORT_FAILED;
361 if ((fst->Reason & 3) != 3) {
362 US_DEBUGP("Drive seems still hungry\n");
363 return USB_STOR_TRANSPORT_FAILED;
366 US_DEBUGP("Transfer happy\n");
371 /* Easy, do nothing. */
375 /* should never hit here -- filtered in usb.c */
376 US_DEBUGP ("freecom unimplemented direction: %d\n",
377 us->srb->sc_data_direction);
378 // Return fail, SCSI seems to handle this better.
379 return USB_STOR_TRANSPORT_FAILED;
383 return USB_STOR_TRANSPORT_GOOD;
387 freecom_init (struct us_data *us)
390 char *buffer = us->iobuf;
392 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
393 * all our packets. No need to allocate any extra buffer space.
396 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
397 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
399 US_DEBUGP("String returned from FC init is: %s\n", buffer);
401 /* Special thanks to the people at Freecom for providing me with
402 * this "magic sequence", which they use in their Windows and MacOS
403 * drivers to make sure that all the attached perhiperals are
408 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
409 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
410 US_DEBUGP("result from activate reset is %d\n", result);
416 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
417 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
418 US_DEBUGP("result from clear reset is %d\n", result);
423 return USB_STOR_TRANSPORT_GOOD;
426 int usb_stor_freecom_reset(struct us_data *us)
428 printk (KERN_CRIT "freecom reset called\n");
430 /* We don't really have this feature. */
434 #ifdef CONFIG_USB_STORAGE_DEBUG
435 static void pdump (void *ibuffer, int length)
437 static char line[80];
439 unsigned char *buffer = (unsigned char *) ibuffer;
444 for (i = 0; i < length; i++) {
447 offset += sprintf (line+offset, " - ");
448 for (j = i - 16; j < i; j++) {
449 if (buffer[j] >= 32 && buffer[j] <= 126)
450 line[offset++] = buffer[j];
452 line[offset++] = '.';
455 US_DEBUGP("%s\n", line);
458 offset += sprintf (line+offset, "%08x:", i);
460 else if ((i & 7) == 0) {
461 offset += sprintf (line+offset, " -");
463 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
466 /* Add the last "chunk" of data. */
467 from = (length - 1) % 16;
468 base = ((length - 1) / 16) * 16;
470 for (i = from + 1; i < 16; i++)
471 offset += sprintf (line+offset, " ");
473 offset += sprintf (line+offset, " ");
474 offset += sprintf (line+offset, " - ");
476 for (i = 0; i <= from; i++) {
477 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
478 line[offset++] = buffer[base+i];
480 line[offset++] = '.';
483 US_DEBUGP("%s\n", line);