]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/storage/freecom.c
[CIFS] list entry can not return null
[linux-2.6-omap-h63xx.git] / drivers / usb / storage / freecom.c
1 /* Driver for Freecom USB/IDE adaptor
2  *
3  * Freecom v0.1:
4  *
5  * First release
6  *
7  * Current development and maintenance by:
8  *   (C) 2000 David Brown <usb-storage@davidb.org>
9  *
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.
14  *
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.
19  *
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.
23  *
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/)
27  */
28
29 #include <linux/hdreg.h>
30
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33
34 #include "usb.h"
35 #include "transport.h"
36 #include "protocol.h"
37 #include "debug.h"
38 #include "freecom.h"
39
40 #ifdef CONFIG_USB_STORAGE_DEBUG
41 static void pdump (void *, int);
42 #endif
43
44 /* Bits of HD_STATUS */
45 #define ERR_STAT                0x01
46 #define DRQ_STAT                0x08
47
48 /* All of the outgoing packets are 64 bytes long. */
49 struct freecom_cb_wrap {
50         u8    Type;             /* Command type. */
51         u8    Timeout;          /* Timeout in seconds. */
52         u8    Atapi[12];        /* An ATAPI packet. */
53         u8    Filler[50];       /* Padding Data. */
54 };
55
56 struct freecom_xfer_wrap {
57         u8    Type;             /* Command type. */
58         u8    Timeout;          /* Timeout in seconds. */
59         __le32   Count;         /* Number of bytes to transfer. */
60         u8    Pad[58];
61 } __attribute__ ((packed));
62
63 struct freecom_ide_out {
64         u8    Type;             /* Type + IDE register. */
65         u8    Pad;
66         __le16   Value;         /* Value to write. */
67         u8    Pad2[60];
68 };
69
70 struct freecom_ide_in {
71         u8    Type;             /* Type | IDE register. */
72         u8    Pad[63];
73 };
74
75 struct freecom_status {
76         u8    Status;
77         u8    Reason;
78         __le16   Count;
79         u8    Pad[60];
80 };
81
82 /* Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide
83  * register. */
84 #define FCM_INT_STATUS          0x02 /* INDEX_STAT */
85 #define FCM_STATUS_BUSY         0x80
86
87 /* These are the packet types.  The low bit indicates that this command
88  * should wait for an interrupt. */
89 #define FCM_PACKET_ATAPI        0x21
90 #define FCM_PACKET_STATUS       0x20
91
92 /* Receive data from the IDE interface.  The ATAPI packet has already
93  * waited, so the data should be immediately available. */
94 #define FCM_PACKET_INPUT        0x81
95
96 /* Send data to the IDE interface. */
97 #define FCM_PACKET_OUTPUT       0x01
98
99 /* Write a value to an ide register.  Or the ide register to write after
100  * munging the address a bit. */
101 #define FCM_PACKET_IDE_WRITE    0x40
102 #define FCM_PACKET_IDE_READ     0xC0
103
104 /* All packets (except for status) are 64 bytes long. */
105 #define FCM_PACKET_LENGTH               64
106 #define FCM_STATUS_PACKET_LENGTH        4
107
108 static int
109 freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
110                 unsigned int ipipe, unsigned int opipe, int count)
111 {
112         struct freecom_xfer_wrap *fxfr =
113                 (struct freecom_xfer_wrap *) us->iobuf;
114         int result;
115
116         fxfr->Type = FCM_PACKET_INPUT | 0x00;
117         fxfr->Timeout = 0;    /* Short timeout for debugging. */
118         fxfr->Count = cpu_to_le32 (count);
119         memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
120
121         US_DEBUGP("Read data Freecom! (c=%d)\n", count);
122
123         /* Issue the transfer command. */
124         result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
125                         FCM_PACKET_LENGTH, NULL);
126         if (result != USB_STOR_XFER_GOOD) {
127                 US_DEBUGP ("Freecom readdata transport error\n");
128                 return USB_STOR_TRANSPORT_ERROR;
129         }
130
131         /* Now transfer all of our blocks. */
132         US_DEBUGP("Start of read\n");
133         result = usb_stor_bulk_srb(us, ipipe, srb);
134         US_DEBUGP("freecom_readdata done!\n");
135
136         if (result > USB_STOR_XFER_SHORT)
137                 return USB_STOR_TRANSPORT_ERROR;
138         return USB_STOR_TRANSPORT_GOOD;
139 }
140
141 static int
142 freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
143                 int unsigned ipipe, unsigned int opipe, int count)
144 {
145         struct freecom_xfer_wrap *fxfr =
146                 (struct freecom_xfer_wrap *) us->iobuf;
147         int result;
148
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));
153
154         US_DEBUGP("Write data Freecom! (c=%d)\n", count);
155
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;
162         }
163
164         /* Now transfer all of our blocks. */
165         US_DEBUGP("Start of write\n");
166         result = usb_stor_bulk_srb(us, opipe, srb);
167
168         US_DEBUGP("freecom_writedata done!\n");
169         if (result > USB_STOR_XFER_SHORT)
170                 return USB_STOR_TRANSPORT_ERROR;
171         return USB_STOR_TRANSPORT_GOOD;
172 }
173
174 /*
175  * Transport for the Freecom USB/IDE adaptor.
176  *
177  */
178 int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
179 {
180         struct freecom_cb_wrap *fcb;
181         struct freecom_status  *fst;
182         unsigned int ipipe, opipe;              /* We need both pipes. */
183         int result;
184         unsigned int partial;
185         int length;
186
187         fcb = (struct freecom_cb_wrap *) us->iobuf;
188         fst = (struct freecom_status *) us->iobuf;
189
190         US_DEBUGP("Freecom TRANSPORT STARTED\n");
191
192         /* Get handles for both transports. */
193         opipe = us->send_bulk_pipe;
194         ipipe = us->recv_bulk_pipe;
195
196         /* The ATAPI Command always goes out first. */
197         fcb->Type = FCM_PACKET_ATAPI | 0x00;
198         fcb->Timeout = 0;
199         memcpy (fcb->Atapi, srb->cmnd, 12);
200         memset (fcb->Filler, 0, sizeof (fcb->Filler));
201
202         US_DEBUG(pdump (srb->cmnd, 12));
203
204         /* Send it out. */
205         result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
206                         FCM_PACKET_LENGTH, NULL);
207
208         /* The Freecom device will only fail if there is something wrong in
209          * USB land.  It returns the status in its own registers, which
210          * come back in the bulk pipe. */
211         if (result != USB_STOR_XFER_GOOD) {
212                 US_DEBUGP ("freecom transport error\n");
213                 return USB_STOR_TRANSPORT_ERROR;
214         }
215
216         /* There are times we can optimize out this status read, but it
217          * doesn't hurt us to always do it now. */
218         result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
219                         FCM_STATUS_PACKET_LENGTH, &partial);
220         US_DEBUGP("foo Status result %d %u\n", result, partial);
221         if (result != USB_STOR_XFER_GOOD)
222                 return USB_STOR_TRANSPORT_ERROR;
223
224         US_DEBUG(pdump ((void *) fst, partial));
225
226         /* The firmware will time-out commands after 20 seconds. Some commands
227          * can legitimately take longer than this, so we use a different
228          * command that only waits for the interrupt and then sends status,
229          * without having to send a new ATAPI command to the device. 
230          *
231          * NOTE: There is some indication that a data transfer after a timeout
232          * may not work, but that is a condition that should never happen.
233          */
234         while (fst->Status & FCM_STATUS_BUSY) {
235                 US_DEBUGP("20 second USB/ATAPI bridge TIMEOUT occurred!\n");
236                 US_DEBUGP("fst->Status is %x\n", fst->Status);
237
238                 /* Get the status again */
239                 fcb->Type = FCM_PACKET_STATUS;
240                 fcb->Timeout = 0;
241                 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
242                 memset (fcb->Filler, 0, sizeof (fcb->Filler));
243
244                 /* Send it out. */
245                 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
246                                 FCM_PACKET_LENGTH, NULL);
247
248                 /* The Freecom device will only fail if there is something
249                  * wrong in USB land.  It returns the status in its own
250                  * registers, which come back in the bulk pipe.
251                  */
252                 if (result != USB_STOR_XFER_GOOD) {
253                         US_DEBUGP ("freecom transport error\n");
254                         return USB_STOR_TRANSPORT_ERROR;
255                 }
256
257                 /* get the data */
258                 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
259                                 FCM_STATUS_PACKET_LENGTH, &partial);
260
261                 US_DEBUGP("bar Status result %d %u\n", result, partial);
262                 if (result != USB_STOR_XFER_GOOD)
263                         return USB_STOR_TRANSPORT_ERROR;
264
265                 US_DEBUG(pdump ((void *) fst, partial));
266         }
267
268         if (partial != 4)
269                 return USB_STOR_TRANSPORT_ERROR;
270         if ((fst->Status & 1) != 0) {
271                 US_DEBUGP("operation failed\n");
272                 return USB_STOR_TRANSPORT_FAILED;
273         }
274
275         /* The device might not have as much data available as we
276          * requested.  If you ask for more than the device has, this reads
277          * and such will hang. */
278         US_DEBUGP("Device indicates that it has %d bytes available\n",
279                         le16_to_cpu (fst->Count));
280         US_DEBUGP("SCSI requested %d\n", scsi_bufflen(srb));
281
282         /* Find the length we desire to read. */
283         switch (srb->cmnd[0]) {
284                 case INQUIRY:
285                 case REQUEST_SENSE:             /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */
286                 case MODE_SENSE:
287                 case MODE_SENSE_10:
288                         length = le16_to_cpu(fst->Count);
289                         break;
290                 default:
291                         length = scsi_bufflen(srb);
292         }
293
294         /* verify that this amount is legal */
295         if (length > scsi_bufflen(srb)) {
296                 length = scsi_bufflen(srb);
297                 US_DEBUGP("Truncating request to match buffer length: %d\n", length);
298         }
299
300         /* What we do now depends on what direction the data is supposed to
301          * move in. */
302
303         switch (us->srb->sc_data_direction) {
304         case DMA_FROM_DEVICE:
305                 /* catch bogus "read 0 length" case */
306                 if (!length)
307                         break;
308                 /* Make sure that the status indicates that the device
309                  * wants data as well. */
310                 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
311                         US_DEBUGP("SCSI wants data, drive doesn't have any\n");
312                         return USB_STOR_TRANSPORT_FAILED;
313                 }
314                 result = freecom_readdata (srb, us, ipipe, opipe, length);
315                 if (result != USB_STOR_TRANSPORT_GOOD)
316                         return result;
317
318                 US_DEBUGP("FCM: Waiting for status\n");
319                 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
320                                 FCM_PACKET_LENGTH, &partial);
321                 US_DEBUG(pdump ((void *) fst, partial));
322
323                 if (partial != 4 || result > USB_STOR_XFER_SHORT)
324                         return USB_STOR_TRANSPORT_ERROR;
325                 if ((fst->Status & ERR_STAT) != 0) {
326                         US_DEBUGP("operation failed\n");
327                         return USB_STOR_TRANSPORT_FAILED;
328                 }
329                 if ((fst->Reason & 3) != 3) {
330                         US_DEBUGP("Drive seems still hungry\n");
331                         return USB_STOR_TRANSPORT_FAILED;
332                 }
333                 US_DEBUGP("Transfer happy\n");
334                 break;
335
336         case DMA_TO_DEVICE:
337                 /* catch bogus "write 0 length" case */
338                 if (!length)
339                         break;
340                 /* Make sure the status indicates that the device wants to
341                  * send us data. */
342                 /* !!IMPLEMENT!! */
343                 result = freecom_writedata (srb, us, ipipe, opipe, length);
344                 if (result != USB_STOR_TRANSPORT_GOOD)
345                         return result;
346
347                 US_DEBUGP("FCM: Waiting for status\n");
348                 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
349                                 FCM_PACKET_LENGTH, &partial);
350
351                 if (partial != 4 || result > USB_STOR_XFER_SHORT)
352                         return USB_STOR_TRANSPORT_ERROR;
353                 if ((fst->Status & ERR_STAT) != 0) {
354                         US_DEBUGP("operation failed\n");
355                         return USB_STOR_TRANSPORT_FAILED;
356                 }
357                 if ((fst->Reason & 3) != 3) {
358                         US_DEBUGP("Drive seems still hungry\n");
359                         return USB_STOR_TRANSPORT_FAILED;
360                 }
361
362                 US_DEBUGP("Transfer happy\n");
363                 break;
364
365
366         case DMA_NONE:
367                 /* Easy, do nothing. */
368                 break;
369
370         default:
371                 /* should never hit here -- filtered in usb.c */
372                 US_DEBUGP ("freecom unimplemented direction: %d\n",
373                                 us->srb->sc_data_direction);
374                 // Return fail, SCSI seems to handle this better.
375                 return USB_STOR_TRANSPORT_FAILED;
376                 break;
377         }
378
379         return USB_STOR_TRANSPORT_GOOD;
380 }
381
382 int
383 freecom_init (struct us_data *us)
384 {
385         int result;
386         char *buffer = us->iobuf;
387
388         /* The DMA-mapped I/O buffer is 64 bytes long, just right for
389          * all our packets.  No need to allocate any extra buffer space.
390          */
391
392         result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
393                         0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
394         buffer[32] = '\0';
395         US_DEBUGP("String returned from FC init is: %s\n", buffer);
396
397         /* Special thanks to the people at Freecom for providing me with
398          * this "magic sequence", which they use in their Windows and MacOS
399          * drivers to make sure that all the attached perhiperals are
400          * properly reset.
401          */
402
403         /* send reset */
404         result = usb_stor_control_msg(us, us->send_ctrl_pipe,
405                         0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
406         US_DEBUGP("result from activate reset is %d\n", result);
407
408         /* wait 250ms */
409         mdelay(250);
410
411         /* clear reset */
412         result = usb_stor_control_msg(us, us->send_ctrl_pipe,
413                         0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
414         US_DEBUGP("result from clear reset is %d\n", result);
415
416         /* wait 3 seconds */
417         mdelay(3 * 1000);
418
419         return USB_STOR_TRANSPORT_GOOD;
420 }
421
422 int usb_stor_freecom_reset(struct us_data *us)
423 {
424         printk (KERN_CRIT "freecom reset called\n");
425
426         /* We don't really have this feature. */
427         return FAILED;
428 }
429
430 #ifdef CONFIG_USB_STORAGE_DEBUG
431 static void pdump (void *ibuffer, int length)
432 {
433         static char line[80];
434         int offset = 0;
435         unsigned char *buffer = (unsigned char *) ibuffer;
436         int i, j;
437         int from, base;
438
439         offset = 0;
440         for (i = 0; i < length; i++) {
441                 if ((i & 15) == 0) {
442                         if (i > 0) {
443                                 offset += sprintf (line+offset, " - ");
444                                 for (j = i - 16; j < i; j++) {
445                                         if (buffer[j] >= 32 && buffer[j] <= 126)
446                                                 line[offset++] = buffer[j];
447                                         else
448                                                 line[offset++] = '.';
449                                 }
450                                 line[offset] = 0;
451                                 US_DEBUGP("%s\n", line);
452                                 offset = 0;
453                         }
454                         offset += sprintf (line+offset, "%08x:", i);
455                 }
456                 else if ((i & 7) == 0) {
457                         offset += sprintf (line+offset, " -");
458                 }
459                 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
460         }
461
462         /* Add the last "chunk" of data. */
463         from = (length - 1) % 16;
464         base = ((length - 1) / 16) * 16;
465
466         for (i = from + 1; i < 16; i++)
467                 offset += sprintf (line+offset, "   ");
468         if (from < 8)
469                 offset += sprintf (line+offset, "  ");
470         offset += sprintf (line+offset, " - ");
471
472         for (i = 0; i <= from; i++) {
473                 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
474                         line[offset++] = buffer[base+i];
475                 else
476                         line[offset++] = '.';
477         }
478         line[offset] = 0;
479         US_DEBUGP("%s\n", line);
480         offset = 0;
481 }
482 #endif
483