Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6/kvm.git] / drivers / usb / storage / freecom.c
blob73ac7262239e510ad9af1ec62883b4d588ca0332
1 /* Driver for Freecom USB/IDE adaptor
3 * Freecom v0.1:
5 * First release
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
13 * later version.
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 <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
32 #include "usb.h"
33 #include "transport.h"
34 #include "protocol.h"
35 #include "debug.h"
36 #include "freecom.h"
38 #ifdef CONFIG_USB_STORAGE_DEBUG
39 static void pdump (void *, int);
40 #endif
42 /* Bits of HD_STATUS */
43 #define ERR_STAT 0x01
44 #define DRQ_STAT 0x08
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. */
58 u8 Pad[58];
59 } __attribute__ ((packed));
61 struct freecom_ide_out {
62 u8 Type; /* Type + IDE register. */
63 u8 Pad;
64 __le16 Value; /* Value to write. */
65 u8 Pad2[60];
68 struct freecom_ide_in {
69 u8 Type; /* Type | IDE register. */
70 u8 Pad[63];
73 struct freecom_status {
74 u8 Status;
75 u8 Reason;
76 __le16 Count;
77 u8 Pad[60];
80 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
81 * register. */
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
107 freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
108 unsigned int ipipe, unsigned int opipe, int count)
110 struct freecom_xfer_wrap *fxfr =
111 (struct freecom_xfer_wrap *) us->iobuf;
112 int result;
114 fxfr->Type = FCM_PACKET_INPUT | 0x00;
115 fxfr->Timeout = 0; /* Short timeout for debugging. */
116 fxfr->Count = cpu_to_le32 (count);
117 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
119 US_DEBUGP("Read data Freecom! (c=%d)\n", count);
121 /* Issue the transfer command. */
122 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
123 FCM_PACKET_LENGTH, NULL);
124 if (result != USB_STOR_XFER_GOOD) {
125 US_DEBUGP ("Freecom readdata transport error\n");
126 return USB_STOR_TRANSPORT_ERROR;
129 /* Now transfer all of our blocks. */
130 US_DEBUGP("Start of read\n");
131 result = usb_stor_bulk_srb(us, ipipe, srb);
132 US_DEBUGP("freecom_readdata done!\n");
134 if (result > USB_STOR_XFER_SHORT)
135 return USB_STOR_TRANSPORT_ERROR;
136 return USB_STOR_TRANSPORT_GOOD;
139 static int
140 freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
141 int unsigned ipipe, unsigned int opipe, int count)
143 struct freecom_xfer_wrap *fxfr =
144 (struct freecom_xfer_wrap *) us->iobuf;
145 int result;
147 fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
148 fxfr->Timeout = 0; /* Short timeout for debugging. */
149 fxfr->Count = cpu_to_le32 (count);
150 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
152 US_DEBUGP("Write data Freecom! (c=%d)\n", count);
154 /* Issue the transfer command. */
155 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
156 FCM_PACKET_LENGTH, NULL);
157 if (result != USB_STOR_XFER_GOOD) {
158 US_DEBUGP ("Freecom writedata transport error\n");
159 return USB_STOR_TRANSPORT_ERROR;
162 /* Now transfer all of our blocks. */
163 US_DEBUGP("Start of write\n");
164 result = usb_stor_bulk_srb(us, opipe, srb);
166 US_DEBUGP("freecom_writedata done!\n");
167 if (result > USB_STOR_XFER_SHORT)
168 return USB_STOR_TRANSPORT_ERROR;
169 return USB_STOR_TRANSPORT_GOOD;
173 * Transport for the Freecom USB/IDE adaptor.
176 int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
178 struct freecom_cb_wrap *fcb;
179 struct freecom_status *fst;
180 unsigned int ipipe, opipe; /* We need both pipes. */
181 int result;
182 unsigned int partial;
183 int length;
185 fcb = (struct freecom_cb_wrap *) us->iobuf;
186 fst = (struct freecom_status *) us->iobuf;
188 US_DEBUGP("Freecom TRANSPORT STARTED\n");
190 /* Get handles for both transports. */
191 opipe = us->send_bulk_pipe;
192 ipipe = us->recv_bulk_pipe;
194 /* The ATAPI Command always goes out first. */
195 fcb->Type = FCM_PACKET_ATAPI | 0x00;
196 fcb->Timeout = 0;
197 memcpy (fcb->Atapi, srb->cmnd, 12);
198 memset (fcb->Filler, 0, sizeof (fcb->Filler));
200 US_DEBUG(pdump (srb->cmnd, 12));
202 /* Send it out. */
203 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
204 FCM_PACKET_LENGTH, NULL);
206 /* The Freecom device will only fail if there is something wrong in
207 * USB land. It returns the status in its own registers, which
208 * come back in the bulk pipe. */
209 if (result != USB_STOR_XFER_GOOD) {
210 US_DEBUGP ("freecom transport error\n");
211 return USB_STOR_TRANSPORT_ERROR;
214 /* There are times we can optimize out this status read, but it
215 * doesn't hurt us to always do it now. */
216 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
217 FCM_STATUS_PACKET_LENGTH, &partial);
218 US_DEBUGP("foo Status result %d %u\n", result, partial);
219 if (result != USB_STOR_XFER_GOOD)
220 return USB_STOR_TRANSPORT_ERROR;
222 US_DEBUG(pdump ((void *) fst, partial));
224 /* The firmware will time-out commands after 20 seconds. Some commands
225 * can legitimately take longer than this, so we use a different
226 * command that only waits for the interrupt and then sends status,
227 * without having to send a new ATAPI command to the device.
229 * NOTE: There is some indication that a data transfer after a timeout
230 * may not work, but that is a condition that should never happen.
232 while (fst->Status & FCM_STATUS_BUSY) {
233 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
234 US_DEBUGP("fst->Status is %x\n", fst->Status);
236 /* Get the status again */
237 fcb->Type = FCM_PACKET_STATUS;
238 fcb->Timeout = 0;
239 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
240 memset (fcb->Filler, 0, sizeof (fcb->Filler));
242 /* Send it out. */
243 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
244 FCM_PACKET_LENGTH, NULL);
246 /* The Freecom device will only fail if there is something
247 * wrong in USB land. It returns the status in its own
248 * registers, which 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 /* get the data */
256 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
257 FCM_STATUS_PACKET_LENGTH, &partial);
259 US_DEBUGP("bar 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));
266 if (partial != 4)
267 return USB_STOR_TRANSPORT_ERROR;
268 if ((fst->Status & 1) != 0) {
269 US_DEBUGP("operation failed\n");
270 return USB_STOR_TRANSPORT_FAILED;
273 /* The device might not have as much data available as we
274 * requested. If you ask for more than the device has, this reads
275 * and such will hang. */
276 US_DEBUGP("Device indicates that it has %d bytes available\n",
277 le16_to_cpu (fst->Count));
278 US_DEBUGP("SCSI requested %d\n", scsi_bufflen(srb));
280 /* Find the length we desire to read. */
281 switch (srb->cmnd[0]) {
282 case INQUIRY:
283 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
284 case MODE_SENSE:
285 case MODE_SENSE_10:
286 length = le16_to_cpu(fst->Count);
287 break;
288 default:
289 length = scsi_bufflen(srb);
292 /* verify that this amount is legal */
293 if (length > scsi_bufflen(srb)) {
294 length = scsi_bufflen(srb);
295 US_DEBUGP("Truncating request to match buffer length: %d\n", length);
298 /* What we do now depends on what direction the data is supposed to
299 * move in. */
301 switch (us->srb->sc_data_direction) {
302 case DMA_FROM_DEVICE:
303 /* catch bogus "read 0 length" case */
304 if (!length)
305 break;
306 /* Make sure that the status indicates that the device
307 * wants data as well. */
308 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
309 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
310 return USB_STOR_TRANSPORT_FAILED;
312 result = freecom_readdata (srb, us, ipipe, opipe, length);
313 if (result != USB_STOR_TRANSPORT_GOOD)
314 return result;
316 US_DEBUGP("FCM: Waiting for status\n");
317 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
318 FCM_PACKET_LENGTH, &partial);
319 US_DEBUG(pdump ((void *) fst, partial));
321 if (partial != 4 || result > USB_STOR_XFER_SHORT)
322 return USB_STOR_TRANSPORT_ERROR;
323 if ((fst->Status & ERR_STAT) != 0) {
324 US_DEBUGP("operation failed\n");
325 return USB_STOR_TRANSPORT_FAILED;
327 if ((fst->Reason & 3) != 3) {
328 US_DEBUGP("Drive seems still hungry\n");
329 return USB_STOR_TRANSPORT_FAILED;
331 US_DEBUGP("Transfer happy\n");
332 break;
334 case DMA_TO_DEVICE:
335 /* catch bogus "write 0 length" case */
336 if (!length)
337 break;
338 /* Make sure the status indicates that the device wants to
339 * send us data. */
340 /* !!IMPLEMENT!! */
341 result = freecom_writedata (srb, us, ipipe, opipe, length);
342 if (result != USB_STOR_TRANSPORT_GOOD)
343 return result;
345 US_DEBUGP("FCM: Waiting for status\n");
346 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
347 FCM_PACKET_LENGTH, &partial);
349 if (partial != 4 || result > USB_STOR_XFER_SHORT)
350 return USB_STOR_TRANSPORT_ERROR;
351 if ((fst->Status & ERR_STAT) != 0) {
352 US_DEBUGP("operation failed\n");
353 return USB_STOR_TRANSPORT_FAILED;
355 if ((fst->Reason & 3) != 3) {
356 US_DEBUGP("Drive seems still hungry\n");
357 return USB_STOR_TRANSPORT_FAILED;
360 US_DEBUGP("Transfer happy\n");
361 break;
364 case DMA_NONE:
365 /* Easy, do nothing. */
366 break;
368 default:
369 /* should never hit here -- filtered in usb.c */
370 US_DEBUGP ("freecom unimplemented direction: %d\n",
371 us->srb->sc_data_direction);
372 // Return fail, SCSI seems to handle this better.
373 return USB_STOR_TRANSPORT_FAILED;
374 break;
377 return USB_STOR_TRANSPORT_GOOD;
381 freecom_init (struct us_data *us)
383 int result;
384 char *buffer = us->iobuf;
386 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
387 * all our packets. No need to allocate any extra buffer space.
390 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
391 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
392 buffer[32] = '\0';
393 US_DEBUGP("String returned from FC init is: %s\n", buffer);
395 /* Special thanks to the people at Freecom for providing me with
396 * this "magic sequence", which they use in their Windows and MacOS
397 * drivers to make sure that all the attached perhiperals are
398 * properly reset.
401 /* send reset */
402 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
403 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
404 US_DEBUGP("result from activate reset is %d\n", result);
406 /* wait 250ms */
407 mdelay(250);
409 /* clear reset */
410 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
411 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
412 US_DEBUGP("result from clear reset is %d\n", result);
414 /* wait 3 seconds */
415 mdelay(3 * 1000);
417 return USB_STOR_TRANSPORT_GOOD;
420 int usb_stor_freecom_reset(struct us_data *us)
422 printk (KERN_CRIT "freecom reset called\n");
424 /* We don't really have this feature. */
425 return FAILED;
428 #ifdef CONFIG_USB_STORAGE_DEBUG
429 static void pdump (void *ibuffer, int length)
431 static char line[80];
432 int offset = 0;
433 unsigned char *buffer = (unsigned char *) ibuffer;
434 int i, j;
435 int from, base;
437 offset = 0;
438 for (i = 0; i < length; i++) {
439 if ((i & 15) == 0) {
440 if (i > 0) {
441 offset += sprintf (line+offset, " - ");
442 for (j = i - 16; j < i; j++) {
443 if (buffer[j] >= 32 && buffer[j] <= 126)
444 line[offset++] = buffer[j];
445 else
446 line[offset++] = '.';
448 line[offset] = 0;
449 US_DEBUGP("%s\n", line);
450 offset = 0;
452 offset += sprintf (line+offset, "%08x:", i);
454 else if ((i & 7) == 0) {
455 offset += sprintf (line+offset, " -");
457 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
460 /* Add the last "chunk" of data. */
461 from = (length - 1) % 16;
462 base = ((length - 1) / 16) * 16;
464 for (i = from + 1; i < 16; i++)
465 offset += sprintf (line+offset, " ");
466 if (from < 8)
467 offset += sprintf (line+offset, " ");
468 offset += sprintf (line+offset, " - ");
470 for (i = 0; i <= from; i++) {
471 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
472 line[offset++] = buffer[base+i];
473 else
474 line[offset++] = '.';
476 line[offset] = 0;
477 US_DEBUGP("%s\n", line);
478 offset = 0;
480 #endif