Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Driver for Freecom USB/IDE adaptor |
2 | * | |
3 | * $Id: freecom.c,v 1.22 2002/04/22 03:39:43 mdharm Exp $ | |
4 | * | |
5 | * Freecom v0.1: | |
6 | * | |
7 | * First release | |
8 | * | |
9 | * Current development and maintenance by: | |
10 | * (C) 2000 David Brown <usb-storage@davidb.org> | |
11 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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. | |
25 | * | |
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/) | |
29 | */ | |
30 | ||
1da177e4 LT |
31 | #include <linux/hdreg.h> |
32 | ||
33 | #include <scsi/scsi.h> | |
34 | #include <scsi/scsi_cmnd.h> | |
35 | ||
36 | #include "usb.h" | |
37 | #include "transport.h" | |
38 | #include "protocol.h" | |
39 | #include "debug.h" | |
40 | #include "freecom.h" | |
41 | ||
42 | #ifdef CONFIG_USB_STORAGE_DEBUG | |
43 | static void pdump (void *, int); | |
44 | #endif | |
45 | ||
46 | /* Bits of HD_STATUS */ | |
47 | #define ERR_STAT 0x01 | |
48 | #define DRQ_STAT 0x08 | |
49 | ||
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. */ | |
56 | }; | |
57 | ||
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)); | |
64 | ||
65 | struct freecom_ide_out { | |
66 | u8 Type; /* Type + IDE register. */ | |
67 | u8 Pad; | |
68 | __le16 Value; /* Value to write. */ | |
69 | u8 Pad2[60]; | |
70 | }; | |
71 | ||
72 | struct freecom_ide_in { | |
73 | u8 Type; /* Type | IDE register. */ | |
74 | u8 Pad[63]; | |
75 | }; | |
76 | ||
77 | struct freecom_status { | |
78 | u8 Status; | |
79 | u8 Reason; | |
80 | __le16 Count; | |
81 | u8 Pad[60]; | |
82 | }; | |
83 | ||
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 | |
88 | ||
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 | |
93 | ||
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 | |
97 | ||
98 | /* Send data to the IDE interface. */ | |
99 | #define FCM_PACKET_OUTPUT 0x01 | |
100 | ||
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 | |
105 | ||
106 | /* All packets (except for status) are 64 bytes long. */ | |
107 | #define FCM_PACKET_LENGTH 64 | |
108 | #define FCM_STATUS_PACKET_LENGTH 4 | |
109 | ||
110 | static int | |
111 | freecom_readdata (struct scsi_cmnd *srb, struct us_data *us, | |
112 | unsigned int ipipe, unsigned int opipe, int count) | |
113 | { | |
114 | struct freecom_xfer_wrap *fxfr = | |
115 | (struct freecom_xfer_wrap *) us->iobuf; | |
116 | int result; | |
117 | ||
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)); | |
122 | ||
123 | US_DEBUGP("Read data Freecom! (c=%d)\n", count); | |
124 | ||
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; | |
131 | } | |
132 | ||
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"); | |
138 | ||
139 | if (result > USB_STOR_XFER_SHORT) | |
140 | return USB_STOR_TRANSPORT_ERROR; | |
141 | return USB_STOR_TRANSPORT_GOOD; | |
142 | } | |
143 | ||
144 | static int | |
145 | freecom_writedata (struct scsi_cmnd *srb, struct us_data *us, | |
146 | int unsigned ipipe, unsigned int opipe, int count) | |
147 | { | |
148 | struct freecom_xfer_wrap *fxfr = | |
149 | (struct freecom_xfer_wrap *) us->iobuf; | |
150 | int result; | |
151 | ||
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)); | |
156 | ||
157 | US_DEBUGP("Write data Freecom! (c=%d)\n", count); | |
158 | ||
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; | |
165 | } | |
166 | ||
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); | |
171 | ||
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; | |
176 | } | |
177 | ||
178 | /* | |
179 | * Transport for the Freecom USB/IDE adaptor. | |
180 | * | |
181 | */ | |
182 | int freecom_transport(struct scsi_cmnd *srb, struct us_data *us) | |
183 | { | |
184 | struct freecom_cb_wrap *fcb; | |
185 | struct freecom_status *fst; | |
186 | unsigned int ipipe, opipe; /* We need both pipes. */ | |
187 | int result; | |
188 | unsigned int partial; | |
189 | int length; | |
190 | ||
191 | fcb = (struct freecom_cb_wrap *) us->iobuf; | |
192 | fst = (struct freecom_status *) us->iobuf; | |
193 | ||
194 | US_DEBUGP("Freecom TRANSPORT STARTED\n"); | |
195 | ||
196 | /* Get handles for both transports. */ | |
197 | opipe = us->send_bulk_pipe; | |
198 | ipipe = us->recv_bulk_pipe; | |
199 | ||
200 | /* The ATAPI Command always goes out first. */ | |
201 | fcb->Type = FCM_PACKET_ATAPI | 0x00; | |
202 | fcb->Timeout = 0; | |
203 | memcpy (fcb->Atapi, srb->cmnd, 12); | |
204 | memset (fcb->Filler, 0, sizeof (fcb->Filler)); | |
205 | ||
206 | US_DEBUG(pdump (srb->cmnd, 12)); | |
207 | ||
208 | /* Send it out. */ | |
209 | result = usb_stor_bulk_transfer_buf (us, opipe, fcb, | |
210 | FCM_PACKET_LENGTH, NULL); | |
211 | ||
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; | |
218 | } | |
219 | ||
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; | |
227 | ||
228 | US_DEBUG(pdump ((void *) fst, partial)); | |
229 | ||
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. | |
234 | * | |
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. | |
237 | */ | |
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); | |
241 | ||
242 | /* Get the status again */ | |
243 | fcb->Type = FCM_PACKET_STATUS; | |
244 | fcb->Timeout = 0; | |
245 | memset (fcb->Atapi, 0, sizeof(fcb->Atapi)); | |
246 | memset (fcb->Filler, 0, sizeof (fcb->Filler)); | |
247 | ||
248 | /* Send it out. */ | |
249 | result = usb_stor_bulk_transfer_buf (us, opipe, fcb, | |
250 | FCM_PACKET_LENGTH, NULL); | |
251 | ||
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. | |
255 | */ | |
256 | if (result != USB_STOR_XFER_GOOD) { | |
257 | US_DEBUGP ("freecom transport error\n"); | |
258 | return USB_STOR_TRANSPORT_ERROR; | |
259 | } | |
260 | ||
261 | /* get the data */ | |
262 | result = usb_stor_bulk_transfer_buf (us, ipipe, fst, | |
263 | FCM_STATUS_PACKET_LENGTH, &partial); | |
264 | ||
265 | US_DEBUGP("bar Status result %d %u\n", result, partial); | |
266 | if (result != USB_STOR_XFER_GOOD) | |
267 | return USB_STOR_TRANSPORT_ERROR; | |
268 | ||
269 | US_DEBUG(pdump ((void *) fst, partial)); | |
270 | } | |
271 | ||
272 | if (partial != 4) | |
273 | return USB_STOR_TRANSPORT_ERROR; | |
274 | if ((fst->Status & 1) != 0) { | |
275 | US_DEBUGP("operation failed\n"); | |
276 | return USB_STOR_TRANSPORT_FAILED; | |
277 | } | |
278 | ||
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); | |
285 | ||
286 | /* Find the length we desire to read. */ | |
287 | switch (srb->cmnd[0]) { | |
288 | case INQUIRY: | |
289 | case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */ | |
290 | case MODE_SENSE: | |
291 | case MODE_SENSE_10: | |
292 | length = le16_to_cpu(fst->Count); | |
293 | break; | |
294 | default: | |
295 | length = srb->request_bufflen; | |
296 | } | |
297 | ||
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); | |
302 | } | |
303 | ||
304 | /* What we do now depends on what direction the data is supposed to | |
305 | * move in. */ | |
306 | ||
307 | switch (us->srb->sc_data_direction) { | |
308 | case DMA_FROM_DEVICE: | |
309 | /* catch bogus "read 0 length" case */ | |
310 | if (!length) | |
311 | break; | |
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; | |
317 | } | |
318 | result = freecom_readdata (srb, us, ipipe, opipe, length); | |
319 | if (result != USB_STOR_TRANSPORT_GOOD) | |
320 | return result; | |
321 | ||
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)); | |
326 | ||
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; | |
332 | } | |
333 | if ((fst->Reason & 3) != 3) { | |
334 | US_DEBUGP("Drive seems still hungry\n"); | |
335 | return USB_STOR_TRANSPORT_FAILED; | |
336 | } | |
337 | US_DEBUGP("Transfer happy\n"); | |
338 | break; | |
339 | ||
340 | case DMA_TO_DEVICE: | |
341 | /* catch bogus "write 0 length" case */ | |
342 | if (!length) | |
343 | break; | |
344 | /* Make sure the status indicates that the device wants to | |
345 | * send us data. */ | |
346 | /* !!IMPLEMENT!! */ | |
347 | result = freecom_writedata (srb, us, ipipe, opipe, length); | |
348 | if (result != USB_STOR_TRANSPORT_GOOD) | |
349 | return result; | |
350 | ||
351 | US_DEBUGP("FCM: Waiting for status\n"); | |
352 | result = usb_stor_bulk_transfer_buf (us, ipipe, fst, | |
353 | FCM_PACKET_LENGTH, &partial); | |
354 | ||
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; | |
360 | } | |
361 | if ((fst->Reason & 3) != 3) { | |
362 | US_DEBUGP("Drive seems still hungry\n"); | |
363 | return USB_STOR_TRANSPORT_FAILED; | |
364 | } | |
365 | ||
366 | US_DEBUGP("Transfer happy\n"); | |
367 | break; | |
368 | ||
369 | ||
370 | case DMA_NONE: | |
371 | /* Easy, do nothing. */ | |
372 | break; | |
373 | ||
374 | default: | |
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; | |
380 | break; | |
381 | } | |
382 | ||
383 | return USB_STOR_TRANSPORT_GOOD; | |
384 | } | |
385 | ||
386 | int | |
387 | freecom_init (struct us_data *us) | |
388 | { | |
389 | int result; | |
390 | char *buffer = us->iobuf; | |
391 | ||
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. | |
394 | */ | |
395 | ||
396 | result = usb_stor_control_msg(us, us->recv_ctrl_pipe, | |
397 | 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ); | |
398 | buffer[32] = '\0'; | |
399 | US_DEBUGP("String returned from FC init is: %s\n", buffer); | |
400 | ||
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 | |
404 | * properly reset. | |
405 | */ | |
406 | ||
407 | /* send reset */ | |
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); | |
411 | ||
412 | /* wait 250ms */ | |
413 | mdelay(250); | |
414 | ||
415 | /* clear reset */ | |
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); | |
419 | ||
420 | /* wait 3 seconds */ | |
421 | mdelay(3 * 1000); | |
422 | ||
423 | return USB_STOR_TRANSPORT_GOOD; | |
424 | } | |
425 | ||
426 | int usb_stor_freecom_reset(struct us_data *us) | |
427 | { | |
428 | printk (KERN_CRIT "freecom reset called\n"); | |
429 | ||
430 | /* We don't really have this feature. */ | |
431 | return FAILED; | |
432 | } | |
433 | ||
434 | #ifdef CONFIG_USB_STORAGE_DEBUG | |
435 | static void pdump (void *ibuffer, int length) | |
436 | { | |
437 | static char line[80]; | |
438 | int offset = 0; | |
439 | unsigned char *buffer = (unsigned char *) ibuffer; | |
440 | int i, j; | |
441 | int from, base; | |
442 | ||
443 | offset = 0; | |
444 | for (i = 0; i < length; i++) { | |
445 | if ((i & 15) == 0) { | |
446 | if (i > 0) { | |
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]; | |
451 | else | |
452 | line[offset++] = '.'; | |
453 | } | |
454 | line[offset] = 0; | |
455 | US_DEBUGP("%s\n", line); | |
456 | offset = 0; | |
457 | } | |
458 | offset += sprintf (line+offset, "%08x:", i); | |
459 | } | |
460 | else if ((i & 7) == 0) { | |
461 | offset += sprintf (line+offset, " -"); | |
462 | } | |
463 | offset += sprintf (line+offset, " %02x", buffer[i] & 0xff); | |
464 | } | |
465 | ||
466 | /* Add the last "chunk" of data. */ | |
467 | from = (length - 1) % 16; | |
468 | base = ((length - 1) / 16) * 16; | |
469 | ||
470 | for (i = from + 1; i < 16; i++) | |
471 | offset += sprintf (line+offset, " "); | |
472 | if (from < 8) | |
473 | offset += sprintf (line+offset, " "); | |
474 | offset += sprintf (line+offset, " - "); | |
475 | ||
476 | for (i = 0; i <= from; i++) { | |
477 | if (buffer[base+i] >= 32 && buffer[base+i] <= 126) | |
478 | line[offset++] = buffer[base+i]; | |
479 | else | |
480 | line[offset++] = '.'; | |
481 | } | |
482 | line[offset] = 0; | |
483 | US_DEBUGP("%s\n", line); | |
484 | offset = 0; | |
485 | } | |
486 | #endif | |
487 |