PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / usb / storage / freecom.c
blobf5a4e8d6a3b1460274eb551343982f071e16ab0b
1 /* Driver for Freecom USB/IDE adaptor
3 * $Id: freecom.c,v 1.22 2002/04/22 03:39:43 mdharm Exp $
5 * Freecom v0.1:
7 * First release
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
15 * later version.
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>
36 #include "usb.h"
37 #include "transport.h"
38 #include "protocol.h"
39 #include "debug.h"
40 #include "freecom.h"
42 #ifdef CONFIG_USB_STORAGE_DEBUG
43 static void pdump (void *, int);
44 #endif
46 /* Bits of HD_STATUS */
47 #define ERR_STAT 0x01
48 #define DRQ_STAT 0x08
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. */
62 u8 Pad[58];
63 } __attribute__ ((packed));
65 struct freecom_ide_out {
66 u8 Type; /* Type + IDE register. */
67 u8 Pad;
68 __le16 Value; /* Value to write. */
69 u8 Pad2[60];
72 struct freecom_ide_in {
73 u8 Type; /* Type | IDE register. */
74 u8 Pad[63];
77 struct freecom_status {
78 u8 Status;
79 u8 Reason;
80 __le16 Count;
81 u8 Pad[60];
84 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
85 * register. */
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
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;
116 int result;
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_srb(us, ipipe, srb);
136 US_DEBUGP("freecom_readdata done!\n");
138 if (result > USB_STOR_XFER_SHORT)
139 return USB_STOR_TRANSPORT_ERROR;
140 return USB_STOR_TRANSPORT_GOOD;
143 static int
144 freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
145 int unsigned ipipe, unsigned int opipe, int count)
147 struct freecom_xfer_wrap *fxfr =
148 (struct freecom_xfer_wrap *) us->iobuf;
149 int result;
151 fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
152 fxfr->Timeout = 0; /* Short timeout for debugging. */
153 fxfr->Count = cpu_to_le32 (count);
154 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
156 US_DEBUGP("Write data Freecom! (c=%d)\n", count);
158 /* Issue the transfer command. */
159 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
160 FCM_PACKET_LENGTH, NULL);
161 if (result != USB_STOR_XFER_GOOD) {
162 US_DEBUGP ("Freecom writedata transport error\n");
163 return USB_STOR_TRANSPORT_ERROR;
166 /* Now transfer all of our blocks. */
167 US_DEBUGP("Start of write\n");
168 result = usb_stor_bulk_srb(us, opipe, srb);
170 US_DEBUGP("freecom_writedata done!\n");
171 if (result > USB_STOR_XFER_SHORT)
172 return USB_STOR_TRANSPORT_ERROR;
173 return USB_STOR_TRANSPORT_GOOD;
177 * Transport for the Freecom USB/IDE adaptor.
180 int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
182 struct freecom_cb_wrap *fcb;
183 struct freecom_status *fst;
184 unsigned int ipipe, opipe; /* We need both pipes. */
185 int result;
186 unsigned int partial;
187 int length;
189 fcb = (struct freecom_cb_wrap *) us->iobuf;
190 fst = (struct freecom_status *) us->iobuf;
192 US_DEBUGP("Freecom TRANSPORT STARTED\n");
194 /* Get handles for both transports. */
195 opipe = us->send_bulk_pipe;
196 ipipe = us->recv_bulk_pipe;
198 /* The ATAPI Command always goes out first. */
199 fcb->Type = FCM_PACKET_ATAPI | 0x00;
200 fcb->Timeout = 0;
201 memcpy (fcb->Atapi, srb->cmnd, 12);
202 memset (fcb->Filler, 0, sizeof (fcb->Filler));
204 US_DEBUG(pdump (srb->cmnd, 12));
206 /* Send it out. */
207 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
208 FCM_PACKET_LENGTH, NULL);
210 /* The Freecom device will only fail if there is something wrong in
211 * USB land. It returns the status in its own registers, which
212 * come back in the bulk pipe. */
213 if (result != USB_STOR_XFER_GOOD) {
214 US_DEBUGP ("freecom transport error\n");
215 return USB_STOR_TRANSPORT_ERROR;
218 /* There are times we can optimize out this status read, but it
219 * doesn't hurt us to always do it now. */
220 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
221 FCM_STATUS_PACKET_LENGTH, &partial);
222 US_DEBUGP("foo Status result %d %u\n", result, partial);
223 if (result != USB_STOR_XFER_GOOD)
224 return USB_STOR_TRANSPORT_ERROR;
226 US_DEBUG(pdump ((void *) fst, partial));
228 /* The firmware will time-out commands after 20 seconds. Some commands
229 * can legitimately take longer than this, so we use a different
230 * command that only waits for the interrupt and then sends status,
231 * without having to send a new ATAPI command to the device.
233 * NOTE: There is some indication that a data transfer after a timeout
234 * may not work, but that is a condition that should never happen.
236 while (fst->Status & FCM_STATUS_BUSY) {
237 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
238 US_DEBUGP("fst->Status is %x\n", fst->Status);
240 /* Get the status again */
241 fcb->Type = FCM_PACKET_STATUS;
242 fcb->Timeout = 0;
243 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
244 memset (fcb->Filler, 0, sizeof (fcb->Filler));
246 /* Send it out. */
247 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
248 FCM_PACKET_LENGTH, NULL);
250 /* The Freecom device will only fail if there is something
251 * wrong in USB land. It returns the status in its own
252 * registers, which come back in the bulk pipe.
254 if (result != USB_STOR_XFER_GOOD) {
255 US_DEBUGP ("freecom transport error\n");
256 return USB_STOR_TRANSPORT_ERROR;
259 /* get the data */
260 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
261 FCM_STATUS_PACKET_LENGTH, &partial);
263 US_DEBUGP("bar Status result %d %u\n", result, partial);
264 if (result != USB_STOR_XFER_GOOD)
265 return USB_STOR_TRANSPORT_ERROR;
267 US_DEBUG(pdump ((void *) fst, partial));
270 if (partial != 4)
271 return USB_STOR_TRANSPORT_ERROR;
272 if ((fst->Status & 1) != 0) {
273 US_DEBUGP("operation failed\n");
274 return USB_STOR_TRANSPORT_FAILED;
277 /* The device might not have as much data available as we
278 * requested. If you ask for more than the device has, this reads
279 * and such will hang. */
280 US_DEBUGP("Device indicates that it has %d bytes available\n",
281 le16_to_cpu (fst->Count));
282 US_DEBUGP("SCSI requested %d\n", scsi_bufflen(srb));
284 /* Find the length we desire to read. */
285 switch (srb->cmnd[0]) {
286 case INQUIRY:
287 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
288 case MODE_SENSE:
289 case MODE_SENSE_10:
290 length = le16_to_cpu(fst->Count);
291 break;
292 default:
293 length = scsi_bufflen(srb);
296 /* verify that this amount is legal */
297 if (length > scsi_bufflen(srb)) {
298 length = scsi_bufflen(srb);
299 US_DEBUGP("Truncating request to match buffer length: %d\n", length);
302 /* What we do now depends on what direction the data is supposed to
303 * move in. */
305 switch (us->srb->sc_data_direction) {
306 case DMA_FROM_DEVICE:
307 /* catch bogus "read 0 length" case */
308 if (!length)
309 break;
310 /* Make sure that the status indicates that the device
311 * wants data as well. */
312 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
313 US_DEBUGP("SCSI wants data, drive doesn't have any\n");
314 return USB_STOR_TRANSPORT_FAILED;
316 result = freecom_readdata (srb, us, ipipe, opipe, length);
317 if (result != USB_STOR_TRANSPORT_GOOD)
318 return result;
320 US_DEBUGP("FCM: Waiting for status\n");
321 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
322 FCM_PACKET_LENGTH, &partial);
323 US_DEBUG(pdump ((void *) fst, partial));
325 if (partial != 4 || result > USB_STOR_XFER_SHORT)
326 return USB_STOR_TRANSPORT_ERROR;
327 if ((fst->Status & ERR_STAT) != 0) {
328 US_DEBUGP("operation failed\n");
329 return USB_STOR_TRANSPORT_FAILED;
331 if ((fst->Reason & 3) != 3) {
332 US_DEBUGP("Drive seems still hungry\n");
333 return USB_STOR_TRANSPORT_FAILED;
335 US_DEBUGP("Transfer happy\n");
336 break;
338 case DMA_TO_DEVICE:
339 /* catch bogus "write 0 length" case */
340 if (!length)
341 break;
342 /* Make sure the status indicates that the device wants to
343 * send us data. */
344 /* !!IMPLEMENT!! */
345 result = freecom_writedata (srb, us, ipipe, opipe, length);
346 if (result != USB_STOR_TRANSPORT_GOOD)
347 return result;
349 US_DEBUGP("FCM: Waiting for status\n");
350 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
351 FCM_PACKET_LENGTH, &partial);
353 if (partial != 4 || result > USB_STOR_XFER_SHORT)
354 return USB_STOR_TRANSPORT_ERROR;
355 if ((fst->Status & ERR_STAT) != 0) {
356 US_DEBUGP("operation failed\n");
357 return USB_STOR_TRANSPORT_FAILED;
359 if ((fst->Reason & 3) != 3) {
360 US_DEBUGP("Drive seems still hungry\n");
361 return USB_STOR_TRANSPORT_FAILED;
364 US_DEBUGP("Transfer happy\n");
365 break;
368 case DMA_NONE:
369 /* Easy, do nothing. */
370 break;
372 default:
373 /* should never hit here -- filtered in usb.c */
374 US_DEBUGP ("freecom unimplemented direction: %d\n",
375 us->srb->sc_data_direction);
376 // Return fail, SCSI seems to handle this better.
377 return USB_STOR_TRANSPORT_FAILED;
378 break;
381 return USB_STOR_TRANSPORT_GOOD;
385 freecom_init (struct us_data *us)
387 int result;
388 char *buffer = us->iobuf;
390 /* The DMA-mapped I/O buffer is 64 bytes long, just right for
391 * all our packets. No need to allocate any extra buffer space.
394 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
395 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
396 buffer[32] = '\0';
397 US_DEBUGP("String returned from FC init is: %s\n", buffer);
399 /* Special thanks to the people at Freecom for providing me with
400 * this "magic sequence", which they use in their Windows and MacOS
401 * drivers to make sure that all the attached perhiperals are
402 * properly reset.
405 /* send reset */
406 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
407 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
408 US_DEBUGP("result from activate reset is %d\n", result);
410 /* wait 250ms */
411 mdelay(250);
413 /* clear reset */
414 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
415 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
416 US_DEBUGP("result from clear reset is %d\n", result);
418 /* wait 3 seconds */
419 mdelay(3 * 1000);
421 return USB_STOR_TRANSPORT_GOOD;
424 int usb_stor_freecom_reset(struct us_data *us)
426 printk (KERN_CRIT "freecom reset called\n");
428 /* We don't really have this feature. */
429 return FAILED;
432 #ifdef CONFIG_USB_STORAGE_DEBUG
433 static void pdump (void *ibuffer, int length)
435 static char line[80];
436 int offset = 0;
437 unsigned char *buffer = (unsigned char *) ibuffer;
438 int i, j;
439 int from, base;
441 offset = 0;
442 for (i = 0; i < length; i++) {
443 if ((i & 15) == 0) {
444 if (i > 0) {
445 offset += sprintf (line+offset, " - ");
446 for (j = i - 16; j < i; j++) {
447 if (buffer[j] >= 32 && buffer[j] <= 126)
448 line[offset++] = buffer[j];
449 else
450 line[offset++] = '.';
452 line[offset] = 0;
453 US_DEBUGP("%s\n", line);
454 offset = 0;
456 offset += sprintf (line+offset, "%08x:", i);
458 else if ((i & 7) == 0) {
459 offset += sprintf (line+offset, " -");
461 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
464 /* Add the last "chunk" of data. */
465 from = (length - 1) % 16;
466 base = ((length - 1) / 16) * 16;
468 for (i = from + 1; i < 16; i++)
469 offset += sprintf (line+offset, " ");
470 if (from < 8)
471 offset += sprintf (line+offset, " ");
472 offset += sprintf (line+offset, " - ");
474 for (i = 0; i <= from; i++) {
475 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
476 line[offset++] = buffer[base+i];
477 else
478 line[offset++] = '.';
480 line[offset] = 0;
481 US_DEBUGP("%s\n", line);
482 offset = 0;
484 #endif