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 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_srb(us, ipipe, srb); 136 US_DEBUGP("freecom_readdata done!\n"); 137 138 if (result > USB_STOR_XFER_SHORT) 139 return USB_STOR_TRANSPORT_ERROR; 140 return USB_STOR_TRANSPORT_GOOD; 141 } 142 143 static int 144 freecom_writedata (struct scsi_cmnd *srb, struct us_data *us, 145 int unsigned ipipe, unsigned int opipe, int count) 146 { 147 struct freecom_xfer_wrap *fxfr = 148 (struct freecom_xfer_wrap *) us->iobuf; 149 int result; 150 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)); 155 156 US_DEBUGP("Write data Freecom! (c=%d)\n", count); 157 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; 164 } 165 166 /* Now transfer all of our blocks. */ 167 US_DEBUGP("Start of write\n"); 168 result = usb_stor_bulk_srb(us, opipe, srb); 169 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; 174 } 175 176 /* 177 * Transport for the Freecom USB/IDE adaptor. 178 * 179 */ 180 int freecom_transport(struct scsi_cmnd *srb, struct us_data *us) 181 { 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; 188 189 fcb = (struct freecom_cb_wrap *) us->iobuf; 190 fst = (struct freecom_status *) us->iobuf; 191 192 US_DEBUGP("Freecom TRANSPORT STARTED\n"); 193 194 /* Get handles for both transports. */ 195 opipe = us->send_bulk_pipe; 196 ipipe = us->recv_bulk_pipe; 197 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)); 203 204 US_DEBUG(pdump (srb->cmnd, 12)); 205 206 /* Send it out. */ 207 result = usb_stor_bulk_transfer_buf (us, opipe, fcb, 208 FCM_PACKET_LENGTH, NULL); 209 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; 216 } 217 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; 225 226 US_DEBUG(pdump ((void *) fst, partial)); 227 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. 232 * 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. 235 */ 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); 239 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)); 245 246 /* Send it out. */ 247 result = usb_stor_bulk_transfer_buf (us, opipe, fcb, 248 FCM_PACKET_LENGTH, NULL); 249 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. 253 */ 254 if (result != USB_STOR_XFER_GOOD) { 255 US_DEBUGP ("freecom transport error\n"); 256 return USB_STOR_TRANSPORT_ERROR; 257 } 258 259 /* get the data */ 260 result = usb_stor_bulk_transfer_buf (us, ipipe, fst, 261 FCM_STATUS_PACKET_LENGTH, &partial); 262 263 US_DEBUGP("bar Status result %d %u\n", result, partial); 264 if (result != USB_STOR_XFER_GOOD) 265 return USB_STOR_TRANSPORT_ERROR; 266 267 US_DEBUG(pdump ((void *) fst, partial)); 268 } 269 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; 275 } 276 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)); 283 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); 294 } 295 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); 300 } 301 302 /* What we do now depends on what direction the data is supposed to 303 * move in. */ 304 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; 315 } 316 result = freecom_readdata (srb, us, ipipe, opipe, length); 317 if (result != USB_STOR_TRANSPORT_GOOD) 318 return result; 319 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)); 324 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; 330 } 331 if ((fst->Reason & 3) != 3) { 332 US_DEBUGP("Drive seems still hungry\n"); 333 return USB_STOR_TRANSPORT_FAILED; 334 } 335 US_DEBUGP("Transfer happy\n"); 336 break; 337 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; 348 349 US_DEBUGP("FCM: Waiting for status\n"); 350 result = usb_stor_bulk_transfer_buf (us, ipipe, fst, 351 FCM_PACKET_LENGTH, &partial); 352 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; 358 } 359 if ((fst->Reason & 3) != 3) { 360 US_DEBUGP("Drive seems still hungry\n"); 361 return USB_STOR_TRANSPORT_FAILED; 362 } 363 364 US_DEBUGP("Transfer happy\n"); 365 break; 366 367 368 case DMA_NONE: 369 /* Easy, do nothing. */ 370 break; 371 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; 379 } 380 381 return USB_STOR_TRANSPORT_GOOD; 382 } 383 384 int 385 freecom_init (struct us_data *us) 386 { 387 int result; 388 char *buffer = us->iobuf; 389 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. 392 */ 393 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); 398 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. 403 */ 404 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); 409 410 /* wait 250ms */ 411 mdelay(250); 412 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); 417 418 /* wait 3 seconds */ 419 mdelay(3 * 1000); 420 421 return USB_STOR_TRANSPORT_GOOD; 422 } 423 424 int usb_stor_freecom_reset(struct us_data *us) 425 { 426 printk (KERN_CRIT "freecom reset called\n"); 427 428 /* We don't really have this feature. */ 429 return FAILED; 430 } 431 432 #ifdef CONFIG_USB_STORAGE_DEBUG 433 static void pdump (void *ibuffer, int length) 434 { 435 static char line[80]; 436 int offset = 0; 437 unsigned char *buffer = (unsigned char *) ibuffer; 438 int i, j; 439 int from, base; 440 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++] = '.'; 451 } 452 line[offset] = 0; 453 US_DEBUGP("%s\n", line); 454 offset = 0; 455 } 456 offset += sprintf (line+offset, "%08x:", i); 457 } 458 else if ((i & 7) == 0) { 459 offset += sprintf (line+offset, " -"); 460 } 461 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff); 462 } 463 464 /* Add the last "chunk" of data. */ 465 from = (length - 1) % 16; 466 base = ((length - 1) / 16) * 16; 467 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, " - "); 473 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++] = '.'; 479 } 480 line[offset] = 0; 481 US_DEBUGP("%s\n", line); 482 offset = 0; 483 } 484 #endif 485 486