1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for Freecom USB/IDE adaptor 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/module.h> 32 #include <scsi/scsi.h> 33 #include <scsi/scsi_cmnd.h> 34 35 #include "usb.h" 36 #include "transport.h" 37 #include "protocol.h" 38 #include "debug.h" 39 #include "scsiglue.h" 40 41 #define DRV_NAME "ums-freecom" 42 43 MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor"); 44 MODULE_AUTHOR("David Brown <usb-storage@davidb.org>"); 45 MODULE_LICENSE("GPL"); 46 47 #ifdef CONFIG_USB_STORAGE_DEBUG 48 static void pdump(struct us_data *us, void *ibuffer, int length); 49 #endif 50 51 /* Bits of HD_STATUS */ 52 #define ERR_STAT 0x01 53 #define DRQ_STAT 0x08 54 55 /* All of the outgoing packets are 64 bytes long. */ 56 struct freecom_cb_wrap { 57 u8 Type; /* Command type. */ 58 u8 Timeout; /* Timeout in seconds. */ 59 u8 Atapi[12]; /* An ATAPI packet. */ 60 u8 Filler[50]; /* Padding Data. */ 61 }; 62 63 struct freecom_xfer_wrap { 64 u8 Type; /* Command type. */ 65 u8 Timeout; /* Timeout in seconds. */ 66 __le32 Count; /* Number of bytes to transfer. */ 67 u8 Pad[58]; 68 } __attribute__ ((packed)); 69 70 struct freecom_ide_out { 71 u8 Type; /* Type + IDE register. */ 72 u8 Pad; 73 __le16 Value; /* Value to write. */ 74 u8 Pad2[60]; 75 }; 76 77 struct freecom_ide_in { 78 u8 Type; /* Type | IDE register. */ 79 u8 Pad[63]; 80 }; 81 82 struct freecom_status { 83 u8 Status; 84 u8 Reason; 85 __le16 Count; 86 u8 Pad[60]; 87 }; 88 89 /* 90 * Freecom stuffs the interrupt status in the INDEX_STAT bit of the ide 91 * register. 92 */ 93 #define FCM_INT_STATUS 0x02 /* INDEX_STAT */ 94 #define FCM_STATUS_BUSY 0x80 95 96 /* 97 * These are the packet types. The low bit indicates that this command 98 * should wait for an interrupt. 99 */ 100 #define FCM_PACKET_ATAPI 0x21 101 #define FCM_PACKET_STATUS 0x20 102 103 /* 104 * Receive data from the IDE interface. The ATAPI packet has already 105 * waited, so the data should be immediately available. 106 */ 107 #define FCM_PACKET_INPUT 0x81 108 109 /* Send data to the IDE interface. */ 110 #define FCM_PACKET_OUTPUT 0x01 111 112 /* 113 * Write a value to an ide register. Or the ide register to write after 114 * munging the address a bit. 115 */ 116 #define FCM_PACKET_IDE_WRITE 0x40 117 #define FCM_PACKET_IDE_READ 0xC0 118 119 /* All packets (except for status) are 64 bytes long. */ 120 #define FCM_PACKET_LENGTH 64 121 #define FCM_STATUS_PACKET_LENGTH 4 122 123 static int init_freecom(struct us_data *us); 124 125 126 /* 127 * The table of devices 128 */ 129 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \ 130 vendorName, productName, useProtocol, useTransport, \ 131 initFunction, flags) \ 132 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \ 133 .driver_info = (flags) } 134 135 static struct usb_device_id freecom_usb_ids[] = { 136 # include "unusual_freecom.h" 137 { } /* Terminating entry */ 138 }; 139 MODULE_DEVICE_TABLE(usb, freecom_usb_ids); 140 141 #undef UNUSUAL_DEV 142 143 /* 144 * The flags table 145 */ 146 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \ 147 vendor_name, product_name, use_protocol, use_transport, \ 148 init_function, Flags) \ 149 { \ 150 .vendorName = vendor_name, \ 151 .productName = product_name, \ 152 .useProtocol = use_protocol, \ 153 .useTransport = use_transport, \ 154 .initFunction = init_function, \ 155 } 156 157 static struct us_unusual_dev freecom_unusual_dev_list[] = { 158 # include "unusual_freecom.h" 159 { } /* Terminating entry */ 160 }; 161 162 #undef UNUSUAL_DEV 163 164 static int 165 freecom_readdata (struct scsi_cmnd *srb, struct us_data *us, 166 unsigned int ipipe, unsigned int opipe, int count) 167 { 168 struct freecom_xfer_wrap *fxfr = 169 (struct freecom_xfer_wrap *) us->iobuf; 170 int result; 171 172 fxfr->Type = FCM_PACKET_INPUT | 0x00; 173 fxfr->Timeout = 0; /* Short timeout for debugging. */ 174 fxfr->Count = cpu_to_le32 (count); 175 memset (fxfr->Pad, 0, sizeof (fxfr->Pad)); 176 177 usb_stor_dbg(us, "Read data Freecom! (c=%d)\n", count); 178 179 /* Issue the transfer command. */ 180 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr, 181 FCM_PACKET_LENGTH, NULL); 182 if (result != USB_STOR_XFER_GOOD) { 183 usb_stor_dbg(us, "Freecom readdata transport error\n"); 184 return USB_STOR_TRANSPORT_ERROR; 185 } 186 187 /* Now transfer all of our blocks. */ 188 usb_stor_dbg(us, "Start of read\n"); 189 result = usb_stor_bulk_srb(us, ipipe, srb); 190 usb_stor_dbg(us, "freecom_readdata done!\n"); 191 192 if (result > USB_STOR_XFER_SHORT) 193 return USB_STOR_TRANSPORT_ERROR; 194 return USB_STOR_TRANSPORT_GOOD; 195 } 196 197 static int 198 freecom_writedata (struct scsi_cmnd *srb, struct us_data *us, 199 int unsigned ipipe, unsigned int opipe, int count) 200 { 201 struct freecom_xfer_wrap *fxfr = 202 (struct freecom_xfer_wrap *) us->iobuf; 203 int result; 204 205 fxfr->Type = FCM_PACKET_OUTPUT | 0x00; 206 fxfr->Timeout = 0; /* Short timeout for debugging. */ 207 fxfr->Count = cpu_to_le32 (count); 208 memset (fxfr->Pad, 0, sizeof (fxfr->Pad)); 209 210 usb_stor_dbg(us, "Write data Freecom! (c=%d)\n", count); 211 212 /* Issue the transfer command. */ 213 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr, 214 FCM_PACKET_LENGTH, NULL); 215 if (result != USB_STOR_XFER_GOOD) { 216 usb_stor_dbg(us, "Freecom writedata transport error\n"); 217 return USB_STOR_TRANSPORT_ERROR; 218 } 219 220 /* Now transfer all of our blocks. */ 221 usb_stor_dbg(us, "Start of write\n"); 222 result = usb_stor_bulk_srb(us, opipe, srb); 223 224 usb_stor_dbg(us, "freecom_writedata done!\n"); 225 if (result > USB_STOR_XFER_SHORT) 226 return USB_STOR_TRANSPORT_ERROR; 227 return USB_STOR_TRANSPORT_GOOD; 228 } 229 230 /* 231 * Transport for the Freecom USB/IDE adaptor. 232 * 233 */ 234 static int freecom_transport(struct scsi_cmnd *srb, struct us_data *us) 235 { 236 struct freecom_cb_wrap *fcb; 237 struct freecom_status *fst; 238 unsigned int ipipe, opipe; /* We need both pipes. */ 239 int result; 240 unsigned int partial; 241 int length; 242 243 fcb = (struct freecom_cb_wrap *) us->iobuf; 244 fst = (struct freecom_status *) us->iobuf; 245 246 usb_stor_dbg(us, "Freecom TRANSPORT STARTED\n"); 247 248 /* Get handles for both transports. */ 249 opipe = us->send_bulk_pipe; 250 ipipe = us->recv_bulk_pipe; 251 252 /* The ATAPI Command always goes out first. */ 253 fcb->Type = FCM_PACKET_ATAPI | 0x00; 254 fcb->Timeout = 0; 255 memcpy (fcb->Atapi, srb->cmnd, 12); 256 memset (fcb->Filler, 0, sizeof (fcb->Filler)); 257 258 US_DEBUG(pdump(us, srb->cmnd, 12)); 259 260 /* Send it out. */ 261 result = usb_stor_bulk_transfer_buf (us, opipe, fcb, 262 FCM_PACKET_LENGTH, NULL); 263 264 /* 265 * The Freecom device will only fail if there is something wrong in 266 * USB land. It returns the status in its own registers, which 267 * come back in the bulk pipe. 268 */ 269 if (result != USB_STOR_XFER_GOOD) { 270 usb_stor_dbg(us, "freecom transport error\n"); 271 return USB_STOR_TRANSPORT_ERROR; 272 } 273 274 /* 275 * There are times we can optimize out this status read, but it 276 * doesn't hurt us to always do it now. 277 */ 278 result = usb_stor_bulk_transfer_buf (us, ipipe, fst, 279 FCM_STATUS_PACKET_LENGTH, &partial); 280 usb_stor_dbg(us, "foo Status result %d %u\n", result, partial); 281 if (result != USB_STOR_XFER_GOOD) 282 return USB_STOR_TRANSPORT_ERROR; 283 284 US_DEBUG(pdump(us, (void *)fst, partial)); 285 286 /* 287 * The firmware will time-out commands after 20 seconds. Some commands 288 * can legitimately take longer than this, so we use a different 289 * command that only waits for the interrupt and then sends status, 290 * without having to send a new ATAPI command to the device. 291 * 292 * NOTE: There is some indication that a data transfer after a timeout 293 * may not work, but that is a condition that should never happen. 294 */ 295 while (fst->Status & FCM_STATUS_BUSY) { 296 usb_stor_dbg(us, "20 second USB/ATAPI bridge TIMEOUT occurred!\n"); 297 usb_stor_dbg(us, "fst->Status is %x\n", fst->Status); 298 299 /* Get the status again */ 300 fcb->Type = FCM_PACKET_STATUS; 301 fcb->Timeout = 0; 302 memset (fcb->Atapi, 0, sizeof(fcb->Atapi)); 303 memset (fcb->Filler, 0, sizeof (fcb->Filler)); 304 305 /* Send it out. */ 306 result = usb_stor_bulk_transfer_buf (us, opipe, fcb, 307 FCM_PACKET_LENGTH, NULL); 308 309 /* 310 * The Freecom device will only fail if there is something 311 * wrong in USB land. It returns the status in its own 312 * registers, which come back in the bulk pipe. 313 */ 314 if (result != USB_STOR_XFER_GOOD) { 315 usb_stor_dbg(us, "freecom transport error\n"); 316 return USB_STOR_TRANSPORT_ERROR; 317 } 318 319 /* get the data */ 320 result = usb_stor_bulk_transfer_buf (us, ipipe, fst, 321 FCM_STATUS_PACKET_LENGTH, &partial); 322 323 usb_stor_dbg(us, "bar Status result %d %u\n", result, partial); 324 if (result != USB_STOR_XFER_GOOD) 325 return USB_STOR_TRANSPORT_ERROR; 326 327 US_DEBUG(pdump(us, (void *)fst, partial)); 328 } 329 330 if (partial != 4) 331 return USB_STOR_TRANSPORT_ERROR; 332 if ((fst->Status & 1) != 0) { 333 usb_stor_dbg(us, "operation failed\n"); 334 return USB_STOR_TRANSPORT_FAILED; 335 } 336 337 /* 338 * The device might not have as much data available as we 339 * requested. If you ask for more than the device has, this reads 340 * and such will hang. 341 */ 342 usb_stor_dbg(us, "Device indicates that it has %d bytes available\n", 343 le16_to_cpu(fst->Count)); 344 usb_stor_dbg(us, "SCSI requested %d\n", scsi_bufflen(srb)); 345 346 /* Find the length we desire to read. */ 347 switch (srb->cmnd[0]) { 348 case INQUIRY: 349 case REQUEST_SENSE: /* 16 or 18 bytes? spec says 18, lots of devices only have 16 */ 350 case MODE_SENSE: 351 case MODE_SENSE_10: 352 length = le16_to_cpu(fst->Count); 353 break; 354 default: 355 length = scsi_bufflen(srb); 356 } 357 358 /* verify that this amount is legal */ 359 if (length > scsi_bufflen(srb)) { 360 length = scsi_bufflen(srb); 361 usb_stor_dbg(us, "Truncating request to match buffer length: %d\n", 362 length); 363 } 364 365 /* 366 * What we do now depends on what direction the data is supposed to 367 * move in. 368 */ 369 370 switch (us->srb->sc_data_direction) { 371 case DMA_FROM_DEVICE: 372 /* catch bogus "read 0 length" case */ 373 if (!length) 374 break; 375 /* 376 * Make sure that the status indicates that the device 377 * wants data as well. 378 */ 379 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) { 380 usb_stor_dbg(us, "SCSI wants data, drive doesn't have any\n"); 381 return USB_STOR_TRANSPORT_FAILED; 382 } 383 result = freecom_readdata (srb, us, ipipe, opipe, length); 384 if (result != USB_STOR_TRANSPORT_GOOD) 385 return result; 386 387 usb_stor_dbg(us, "Waiting for status\n"); 388 result = usb_stor_bulk_transfer_buf (us, ipipe, fst, 389 FCM_PACKET_LENGTH, &partial); 390 US_DEBUG(pdump(us, (void *)fst, partial)); 391 392 if (partial != 4 || result > USB_STOR_XFER_SHORT) 393 return USB_STOR_TRANSPORT_ERROR; 394 if ((fst->Status & ERR_STAT) != 0) { 395 usb_stor_dbg(us, "operation failed\n"); 396 return USB_STOR_TRANSPORT_FAILED; 397 } 398 if ((fst->Reason & 3) != 3) { 399 usb_stor_dbg(us, "Drive seems still hungry\n"); 400 return USB_STOR_TRANSPORT_FAILED; 401 } 402 usb_stor_dbg(us, "Transfer happy\n"); 403 break; 404 405 case DMA_TO_DEVICE: 406 /* catch bogus "write 0 length" case */ 407 if (!length) 408 break; 409 /* 410 * Make sure the status indicates that the device wants to 411 * send us data. 412 */ 413 /* !!IMPLEMENT!! */ 414 result = freecom_writedata (srb, us, ipipe, opipe, length); 415 if (result != USB_STOR_TRANSPORT_GOOD) 416 return result; 417 418 usb_stor_dbg(us, "Waiting for status\n"); 419 result = usb_stor_bulk_transfer_buf (us, ipipe, fst, 420 FCM_PACKET_LENGTH, &partial); 421 422 if (partial != 4 || result > USB_STOR_XFER_SHORT) 423 return USB_STOR_TRANSPORT_ERROR; 424 if ((fst->Status & ERR_STAT) != 0) { 425 usb_stor_dbg(us, "operation failed\n"); 426 return USB_STOR_TRANSPORT_FAILED; 427 } 428 if ((fst->Reason & 3) != 3) { 429 usb_stor_dbg(us, "Drive seems still hungry\n"); 430 return USB_STOR_TRANSPORT_FAILED; 431 } 432 433 usb_stor_dbg(us, "Transfer happy\n"); 434 break; 435 436 437 case DMA_NONE: 438 /* Easy, do nothing. */ 439 break; 440 441 default: 442 /* should never hit here -- filtered in usb.c */ 443 usb_stor_dbg(us, "freecom unimplemented direction: %d\n", 444 us->srb->sc_data_direction); 445 /* Return fail, SCSI seems to handle this better. */ 446 return USB_STOR_TRANSPORT_FAILED; 447 break; 448 } 449 450 return USB_STOR_TRANSPORT_GOOD; 451 } 452 453 static int init_freecom(struct us_data *us) 454 { 455 int result; 456 char *buffer = us->iobuf; 457 458 /* 459 * The DMA-mapped I/O buffer is 64 bytes long, just right for 460 * all our packets. No need to allocate any extra buffer space. 461 */ 462 463 result = usb_stor_control_msg(us, us->recv_ctrl_pipe, 464 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ); 465 buffer[32] = '\0'; 466 usb_stor_dbg(us, "String returned from FC init is: %s\n", buffer); 467 468 /* 469 * Special thanks to the people at Freecom for providing me with 470 * this "magic sequence", which they use in their Windows and MacOS 471 * drivers to make sure that all the attached perhiperals are 472 * properly reset. 473 */ 474 475 /* send reset */ 476 result = usb_stor_control_msg(us, us->send_ctrl_pipe, 477 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ); 478 usb_stor_dbg(us, "result from activate reset is %d\n", result); 479 480 /* wait 250ms */ 481 mdelay(250); 482 483 /* clear reset */ 484 result = usb_stor_control_msg(us, us->send_ctrl_pipe, 485 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ); 486 usb_stor_dbg(us, "result from clear reset is %d\n", result); 487 488 /* wait 3 seconds */ 489 mdelay(3 * 1000); 490 491 return USB_STOR_TRANSPORT_GOOD; 492 } 493 494 static int usb_stor_freecom_reset(struct us_data *us) 495 { 496 printk (KERN_CRIT "freecom reset called\n"); 497 498 /* We don't really have this feature. */ 499 return FAILED; 500 } 501 502 #ifdef CONFIG_USB_STORAGE_DEBUG 503 static void pdump(struct us_data *us, void *ibuffer, int length) 504 { 505 static char line[80]; 506 int offset = 0; 507 unsigned char *buffer = (unsigned char *) ibuffer; 508 int i, j; 509 int from, base; 510 511 offset = 0; 512 for (i = 0; i < length; i++) { 513 if ((i & 15) == 0) { 514 if (i > 0) { 515 offset += sprintf (line+offset, " - "); 516 for (j = i - 16; j < i; j++) { 517 if (buffer[j] >= 32 && buffer[j] <= 126) 518 line[offset++] = buffer[j]; 519 else 520 line[offset++] = '.'; 521 } 522 line[offset] = 0; 523 usb_stor_dbg(us, "%s\n", line); 524 offset = 0; 525 } 526 offset += sprintf (line+offset, "%08x:", i); 527 } else if ((i & 7) == 0) { 528 offset += sprintf (line+offset, " -"); 529 } 530 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff); 531 } 532 533 /* Add the last "chunk" of data. */ 534 from = (length - 1) % 16; 535 base = ((length - 1) / 16) * 16; 536 537 for (i = from + 1; i < 16; i++) 538 offset += sprintf (line+offset, " "); 539 if (from < 8) 540 offset += sprintf (line+offset, " "); 541 offset += sprintf (line+offset, " - "); 542 543 for (i = 0; i <= from; i++) { 544 if (buffer[base+i] >= 32 && buffer[base+i] <= 126) 545 line[offset++] = buffer[base+i]; 546 else 547 line[offset++] = '.'; 548 } 549 line[offset] = 0; 550 usb_stor_dbg(us, "%s\n", line); 551 offset = 0; 552 } 553 #endif 554 555 static struct scsi_host_template freecom_host_template; 556 557 static int freecom_probe(struct usb_interface *intf, 558 const struct usb_device_id *id) 559 { 560 struct us_data *us; 561 int result; 562 563 result = usb_stor_probe1(&us, intf, id, 564 (id - freecom_usb_ids) + freecom_unusual_dev_list, 565 &freecom_host_template); 566 if (result) 567 return result; 568 569 us->transport_name = "Freecom"; 570 us->transport = freecom_transport; 571 us->transport_reset = usb_stor_freecom_reset; 572 us->max_lun = 0; 573 574 result = usb_stor_probe2(us); 575 return result; 576 } 577 578 static struct usb_driver freecom_driver = { 579 .name = DRV_NAME, 580 .probe = freecom_probe, 581 .disconnect = usb_stor_disconnect, 582 .suspend = usb_stor_suspend, 583 .resume = usb_stor_resume, 584 .reset_resume = usb_stor_reset_resume, 585 .pre_reset = usb_stor_pre_reset, 586 .post_reset = usb_stor_post_reset, 587 .id_table = freecom_usb_ids, 588 .soft_unbind = 1, 589 .no_dynamic_id = 1, 590 }; 591 592 module_usb_stor_driver(freecom_driver, freecom_host_template, DRV_NAME); 593