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 "transport.h"
37 #include "linux/hdreg.h"
39 #ifdef CONFIG_USB_STORAGE_DEBUG
40 static void pdump (void *, int);
43 /* Bits of HD_STATUS */
47 /* All of the outgoing packets are 64 bytes long. */
48 struct freecom_cb_wrap {
49 u8 Type; /* Command type. */
50 u8 Timeout; /* Timeout in seconds. */
51 u8 Atapi[12]; /* An ATAPI packet. */
52 u8 Filler[50]; /* Padding Data. */
55 struct freecom_xfer_wrap {
56 u8 Type; /* Command type. */
57 u8 Timeout; /* Timeout in seconds. */
58 u32 Count; /* Number of bytes to transfer. */
60 } __attribute__ ((packed));
62 struct freecom_ide_out {
63 u8 Type; /* Type + IDE register. */
65 u16 Value; /* Value to write. */
69 struct freecom_ide_in {
70 u8 Type; /* Type | IDE register. */
74 struct freecom_status {
81 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
83 #define FCM_INT_STATUS 0x02 /* INDEX_STAT */
84 #define FCM_STATUS_BUSY 0x80
86 /* These are the packet types. The low bit indicates that this command
87 * should wait for an interrupt. */
88 #define FCM_PACKET_ATAPI 0x21
89 #define FCM_PACKET_STATUS 0x20
91 /* Receive data from the IDE interface. The ATAPI packet has already
92 * waited, so the data should be immediately available. */
93 #define FCM_PACKET_INPUT 0x81
95 /* Send data to the IDE interface. */
96 #define FCM_PACKET_OUTPUT 0x01
98 /* Write a value to an ide register. Or the ide register to write after
99 * munging the address a bit. */
100 #define FCM_PACKET_IDE_WRITE 0x40
101 #define FCM_PACKET_IDE_READ 0xC0
103 /* All packets (except for status) are 64 bytes long. */
104 #define FCM_PACKET_LENGTH 64
105 #define FCM_STATUS_PACKET_LENGTH 4
108 freecom_readdata (Scsi_Cmnd *srb, struct us_data *us,
109 unsigned int ipipe, unsigned int opipe, int count)
111 struct freecom_xfer_wrap *fxfr =
112 (struct freecom_xfer_wrap *) us->iobuf;
115 fxfr->Type = FCM_PACKET_INPUT | 0x00;
116 fxfr->Timeout = 0; /* Short timeout for debugging. */
117 fxfr->Count = cpu_to_le32 (count);
118 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
120 US_DEBUGP("Read data Freecom! (c=%d)\n", count);
122 /* Issue the transfer command. */
123 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
124 FCM_PACKET_LENGTH, NULL);
125 if (result != USB_STOR_XFER_GOOD) {
126 US_DEBUGP ("Freecom readdata transport error\n");
127 return USB_STOR_TRANSPORT_ERROR;
130 /* Now transfer all of our blocks. */
131 US_DEBUGP("Start of read\n");
132 result = usb_stor_bulk_transfer_sg(us, ipipe, srb->request_buffer,
133 count, srb->use_sg, &srb->resid);
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 (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_transfer_sg(us, opipe, srb->request_buffer,
167 count, srb->use_sg, &srb->resid);
169 US_DEBUGP("freecom_writedata done!\n");
170 if (result > USB_STOR_XFER_SHORT)
171 return USB_STOR_TRANSPORT_ERROR;
172 return USB_STOR_TRANSPORT_GOOD;
176 * Transport for the Freecom USB/IDE adaptor.
179 int freecom_transport(Scsi_Cmnd *srb, struct us_data *us)
181 struct freecom_cb_wrap *fcb;
182 struct freecom_status *fst;
183 unsigned int ipipe, opipe; /* We need both pipes. */
185 unsigned int partial;
188 fcb = (struct freecom_cb_wrap *) us->iobuf;
189 fst = (struct freecom_status *) us->iobuf;
191 US_DEBUGP("Freecom TRANSPORT STARTED\n");
193 /* Get handles for both transports. */
194 opipe = us->send_bulk_pipe;
195 ipipe = us->recv_bulk_pipe;
197 /* The ATAPI Command always goes out first. */
198 fcb->Type = FCM_PACKET_ATAPI | 0x00;
200 memcpy (fcb->Atapi, srb->cmnd, 12);
201 memset (fcb->Filler, 0, sizeof (fcb->Filler));
203 US_DEBUG(pdump (srb->cmnd, 12));
206 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
207 FCM_PACKET_LENGTH, NULL);
209 /* The Freecom device will only fail if there is something wrong in
210 * USB land. It returns the status in its own registers, which
211 * come back in the bulk pipe. */
212 if (result != USB_STOR_XFER_GOOD) {
213 US_DEBUGP ("freecom transport error\n");
214 return USB_STOR_TRANSPORT_ERROR;
217 /* There are times we can optimize out this status read, but it
218 * doesn't hurt us to always do it now. */
219 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
220 FCM_STATUS_PACKET_LENGTH, &partial);
221 US_DEBUGP("foo Status result %d %u\n", result, partial);
222 if (result != USB_STOR_XFER_GOOD)
223 return USB_STOR_TRANSPORT_ERROR;
225 US_DEBUG(pdump ((void *) fst, partial));
227 /* The firmware will time-out commands after 20 seconds. Some commands
228 * can legitimately take longer than this, so we use a different
229 * command that only waits for the interrupt and then sends status,
230 * without having to send a new ATAPI command to the device.
232 * NOTE: There is some indication that a data transfer after a timeout
233 * may not work, but that is a condition that should never happen.
235 while (fst->Status & FCM_STATUS_BUSY) {
236 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
237 US_DEBUGP("fst->Status is %x\n", fst->Status);
239 /* Get the status again */
240 fcb->Type = FCM_PACKET_STATUS;
242 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
243 memset (fcb->Filler, 0, sizeof (fcb->Filler));
246 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
247 FCM_PACKET_LENGTH, NULL);
249 /* The Freecom device will only fail if there is something
250 * wrong in USB land. It returns the status in its own
251 * registers, which come back in the bulk pipe.
253 if (result != USB_STOR_XFER_GOOD) {
254 US_DEBUGP ("freecom transport error\n");
255 return USB_STOR_TRANSPORT_ERROR;
259 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
260 FCM_STATUS_PACKET_LENGTH, &partial);
262 US_DEBUGP("bar Status result %d %u\n", result, partial);
263 if (result != USB_STOR_XFER_GOOD)
264 return USB_STOR_TRANSPORT_ERROR;
266 US_DEBUG(pdump ((void *) fst, partial));
270 return USB_STOR_TRANSPORT_ERROR;
271 if ((fst->Status & 1) != 0) {
272 US_DEBUGP("operation failed\n");
273 return USB_STOR_TRANSPORT_FAILED;
276 /* The device might not have as much data available as we
277 * requested. If you ask for more than the device has, this reads
278 * and such will hang. */
279 US_DEBUGP("Device indicates that it has %d bytes available\n",
280 le16_to_cpu (fst->Count));
281 US_DEBUGP("SCSI requested %d\n", srb->request_bufflen);
283 /* Find the length we desire to read. */
284 switch (srb->cmnd[0]) {
286 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
292 length = srb->request_bufflen;
295 /* verify that this amount is legal */
296 if (length > srb->request_bufflen) {
297 length = srb->request_bufflen;
298 US_DEBUGP("Truncating request to match buffer length: %d\n", length);
301 /* What we do now depends on what direction the data is supposed to
304 switch (us->srb->sc_data_direction) {
306 /* catch bogus "read 0 length" case */
309 /* Make sure that the status indicates that the device
310 * wants data as well. */
311 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
312 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
313 return USB_STOR_TRANSPORT_FAILED;
315 result = freecom_readdata (srb, us, ipipe, opipe, length);
316 if (result != USB_STOR_TRANSPORT_GOOD)
319 US_DEBUGP("FCM: Waiting for status\n");
320 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
321 FCM_PACKET_LENGTH, &partial);
322 US_DEBUG(pdump ((void *) fst, partial));
324 if (partial != 4 || result > USB_STOR_XFER_SHORT)
325 return USB_STOR_TRANSPORT_ERROR;
326 if ((fst->Status & ERR_STAT) != 0) {
327 US_DEBUGP("operation failed\n");
328 return USB_STOR_TRANSPORT_FAILED;
330 if ((fst->Reason & 3) != 3) {
331 US_DEBUGP("Drive seems still hungry\n");
332 return USB_STOR_TRANSPORT_FAILED;
334 US_DEBUGP("Transfer happy\n");
337 case SCSI_DATA_WRITE:
338 /* catch bogus "write 0 length" case */
341 /* Make sure the status indicates that the device wants to
344 result = freecom_writedata (srb, us, ipipe, opipe, length);
345 if (result != USB_STOR_TRANSPORT_GOOD)
348 US_DEBUGP("FCM: Waiting for status\n");
349 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
350 FCM_PACKET_LENGTH, &partial);
352 if (partial != 4 || result > USB_STOR_XFER_SHORT)
353 return USB_STOR_TRANSPORT_ERROR;
354 if ((fst->Status & ERR_STAT) != 0) {
355 US_DEBUGP("operation failed\n");
356 return USB_STOR_TRANSPORT_FAILED;
358 if ((fst->Reason & 3) != 3) {
359 US_DEBUGP("Drive seems still hungry\n");
360 return USB_STOR_TRANSPORT_FAILED;
363 US_DEBUGP("Transfer happy\n");
368 /* Easy, do nothing. */
372 /* should never hit here -- filtered in usb.c */
373 US_DEBUGP ("freecom unimplemented direction: %d\n",
374 us->srb->sc_data_direction);
375 // Return fail, SCSI seems to handle this better.
376 return USB_STOR_TRANSPORT_FAILED;
380 return USB_STOR_TRANSPORT_GOOD;
384 freecom_init (struct us_data *us)
387 char *buffer = us->iobuf;
389 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
390 * all our packets. No need to allocate any extra buffer space.
393 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
394 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
396 US_DEBUGP("String returned from FC init is: %s\n", buffer);
398 /* Special thanks to the people at Freecom for providing me with
399 * this "magic sequence", which they use in their Windows and MacOS
400 * drivers to make sure that all the attached perhiperals are
405 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
406 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
407 US_DEBUGP("result from activate reset is %d\n", result);
413 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
414 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
415 US_DEBUGP("result from clear reset is %d\n", result);
420 return USB_STOR_TRANSPORT_GOOD;
423 int usb_stor_freecom_reset(struct us_data *us)
425 printk (KERN_CRIT "freecom reset called\n");
427 /* We don't really have this feature. */
431 #ifdef CONFIG_USB_STORAGE_DEBUG
432 static void pdump (void *ibuffer, int length)
434 static char line[80];
436 unsigned char *buffer = (unsigned char *) ibuffer;
441 for (i = 0; i < length; i++) {
444 offset += sprintf (line+offset, " - ");
445 for (j = i - 16; j < i; j++) {
446 if (buffer[j] >= 32 && buffer[j] <= 126)
447 line[offset++] = buffer[j];
449 line[offset++] = '.';
452 US_DEBUGP("%s\n", line);
455 offset += sprintf (line+offset, "%08x:", i);
457 else if ((i & 7) == 0) {
458 offset += sprintf (line+offset, " -");
460 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
463 /* Add the last "chunk" of data. */
464 from = (length - 1) % 16;
465 base = ((length - 1) / 16) * 16;
467 for (i = from + 1; i < 16; i++)
468 offset += sprintf (line+offset, " ");
470 offset += sprintf (line+offset, " ");
471 offset += sprintf (line+offset, " - ");
473 for (i = 0; i <= from; i++) {
474 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
475 line[offset++] = buffer[base+i];
477 line[offset++] = '.';
480 US_DEBUGP("%s\n", line);