Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | /* * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. * * IOATAPIHDDrive.h - Generic ATAPI Direct-Access driver. * * HISTORY * Sep 2, 1999 jliu - Ported from AppleATAPIDrive. */ #include <IOKit/assert.h> #include <IOKit/storage/ata/IOATAPIHDDrive.h> #include <IOKit/storage/ata/IOATAPIHDDriveNub.h> #define super IOATAHDDrive OSDefineMetaClassAndStructors( IOATAPIHDDrive, IOATAHDDrive ) //--------------------------------------------------------------------------- // Override the init() method in IOATAHDDrive. bool IOATAPIHDDrive::init(OSDictionary * properties) { _mediaPresent = false; _isRemovable = false; return super::init(properties); } //--------------------------------------------------------------------------- // Override probe() method inherited from IOATAHDDrive. We need to // perform additional matching on ATAPI device type based on the // Inquiry data revealed by an ATA(PI) device nub. IOService * IOATAPIHDDrive::probe(IOService * provider, SInt32 * score) { UInt8 deviceType; IOService * ret = 0; bool wasOpened = false; // Let superclass have a go at probe first. // if (!super::probe(provider, score)) return 0; // Our provider must be a IOATADevice nub, most likely created // by an IOATAController instance. // _ataDevice = OSDynamicCast(IOATADevice, provider); if (_ataDevice == 0) return 0; // IOATADevice nub not found. do { // Since we may issue command to the IOATADevice to perform // device matching, we express interest in using the device by // performing an open. // if (_ataDevice->open(this) == false) break; wasOpened = true; // Perform ATAPI type matching, CD-ROM, Direct-Access, Tape, etc. // if (!_ataDevice->getInquiryData(1, (ATAPIInquiry *) &deviceType) || !matchATAPIDeviceType(deviceType & 0x1f, score)) break; ret = this; } while (false); if (wasOpened) _ataDevice->close(this); _ataDevice = 0; return ret; } //--------------------------------------------------------------------------- // Report as an ATAPI device. ATADeviceType IOATAPIHDDrive::reportATADeviceType() const { return kATADeviceATAPI; } //--------------------------------------------------------------------------- // Looks for an ATAPI device which is a direct-access device. bool IOATAPIHDDrive::matchATAPIDeviceType(UInt8 type, SInt32 * score) { if (type == kIOATAPIDeviceTypeDirectAccess) return true; return false; } //--------------------------------------------------------------------------- // Gather information about the ATAPI device nub. bool IOATAPIHDDrive::inspectDevice(IOATADevice * device) { OSString * string; // Fetch ATAPI device information from the nub. // string = OSDynamicCast(OSString, device->getProperty(kATAPropertyVendorName)); if (string) { strncpy(_vendor, string->getCStringNoCopy(), 8); _vendor[8] = '\0'; } string = OSDynamicCast(OSString, device->getProperty(kATAPropertyProductName)); if (string) { strncpy(_product, string->getCStringNoCopy(), 16); _product[16] = '\0'; } string = OSDynamicCast(OSString, device->getProperty(kATAPropertyProductRevision)); if (string) { strncpy(_revision, string->getCStringNoCopy(), 4); _revision[4] = '\0'; } // Device wants to be power-managed. // _supportedFeatures |= kIOATAFeaturePowerManagement; return true; } //--------------------------------------------------------------------------- // Async read/write requests. IOReturn IOATAPIHDDrive::doAsyncReadWrite(IOMemoryDescriptor * buffer, UInt32 block, UInt32 nblks, IOStorageCompletion completion) { IOReturn ret; IOATACommand * cmd = atapiCommandReadWrite(buffer, block, nblks); if (!cmd) return kIOReturnNoMemory; ret = asyncExecute(cmd, completion); cmd->release(); return ret; } //--------------------------------------------------------------------------- // Sync read/write requests. IOReturn IOATAPIHDDrive::doSyncReadWrite(IOMemoryDescriptor * buffer, UInt32 block, UInt32 nblks) { IOReturn ret; IOATACommand * cmd = atapiCommandReadWrite(buffer, block, nblks); if (!cmd) return kIOReturnNoMemory; ret = syncExecute(cmd); cmd->release(); return ret; } //--------------------------------------------------------------------------- // Eject the media in the removable drive. IOReturn IOATAPIHDDrive::doEjectMedia() { IOReturn ret; IOATACommand * cmd = atapiCommandStartStopUnit(false, /* start unit */ true, /* Load/Eject */ false); /* Immediate */ if (!cmd) return kIOReturnNoMemory; ret = syncExecute(cmd); cmd->release(); return ret; } //--------------------------------------------------------------------------- // Format the media in the drive. IOReturn IOATAPIHDDrive::doFormatMedia(UInt64 byteCapacity, IOMemoryDescriptor * formatData = 0) { IOReturn ret; IOATACommand * cmd = atapiCommandFormatUnit(0, 0, 0, formatData); if (!cmd) return kIOReturnNoMemory; ret = syncExecute(cmd, 15 * 60 * 1000); // 15 min timeout cmd->release(); return ret; } //--------------------------------------------------------------------------- // Lock/unlock the media in the removable drive. IOReturn IOATAPIHDDrive::doLockUnlockMedia(bool doLock) { IOReturn ret; IOATACommand * cmd = atapiCommandPreventAllowRemoval(doLock); if (!cmd) return kIOReturnNoMemory; ret = syncExecute(cmd); cmd->release(); // Cache the state on the media lock, and restore it when // the driver wakes up from sleep. _isLocked = doLock; return ret; } //--------------------------------------------------------------------------- // Sync the write cache. IOReturn IOATAPIHDDrive::doSynchronizeCache() { IOReturn ret; IOATACommand * cmd = atapiCommandSynchronizeCache(); if (!cmd) return kIOReturnNoMemory; ret = syncExecute(cmd); cmd->release(); return ret; } //--------------------------------------------------------------------------- // Start up the drive. IOReturn IOATAPIHDDrive::doStart() { return doStartStop(true); } //--------------------------------------------------------------------------- // Stop the drive. IOReturn IOATAPIHDDrive::doStop() { return doStartStop(false); } //--------------------------------------------------------------------------- // Issue a START/STOP Unit command. IOReturn IOATAPIHDDrive::doStartStop(bool doStart) { IOReturn ret; IOATACommand * cmd; cmd = atapiCommandStartStopUnit(doStart, /* start unit */ false, /* Load/Eject */ false); /* Immediate operation */ if (!cmd) return kIOReturnNoMemory; ret = syncExecute(cmd); cmd->release(); return ret; } //--------------------------------------------------------------------------- // Return device identification strings char * IOATAPIHDDrive::getVendorString() { return _vendor; } char * IOATAPIHDDrive::getProductString() { return _product; } char * IOATAPIHDDrive::getRevisionString() { return _revision; } char * IOATAPIHDDrive::getAdditionalDeviceInfoString() { return ("[ATAPI]"); } //--------------------------------------------------------------------------- // Report whether the media in the drive is ejectable. IOReturn IOATAPIHDDrive::reportEjectability(bool * isEjectable) { *isEjectable = true; /* default: if it's removable, it's ejectable */ return kIOReturnSuccess; } //--------------------------------------------------------------------------- // Report whether the drive can prevent user-initiated ejects by locking // the media in the drive. IOReturn IOATAPIHDDrive::reportLockability(bool * isLockable) { *isLockable = true; /* default: if it's removable, it's lockable */ return kIOReturnSuccess; } //--------------------------------------------------------------------------- // Report our polling requirments. IOReturn IOATAPIHDDrive::reportPollRequirements(bool * pollRequired, bool * pollIsExpensive) { *pollIsExpensive = false; *pollRequired = _isRemovable; return kIOReturnSuccess; } //--------------------------------------------------------------------------- // Report the current state of the media. IOReturn IOATAPIHDDrive::reportMediaState(bool * mediaPresent, bool * changed) { IOATACommand * cmd = 0; IOMemoryDescriptor * senseData = 0; UInt8 senseBuf[18]; ATAResults results; IOReturn ret; assert(mediaPresent && changed); do { ret = kIOReturnNoMemory; bzero((void *) senseBuf, sizeof(senseBuf)); senseData = IOMemoryDescriptor::withAddress(senseBuf, sizeof(senseBuf), kIODirectionIn); if (!senseData) break; cmd = atapiCommandTestUnitReady(); if (!cmd) break; // Execute the Test Unit Ready command with no retries. // syncExecute(cmd, kATADefaultTimeout, kATAZeroRetry, senseData); ret = kIOReturnSuccess; if (cmd->getResults(&results) == kIOReturnSuccess) { *mediaPresent = true; *changed = (*mediaPresent != _mediaPresent); _mediaPresent = true; } else { UInt8 errorCode = senseBuf[0]; UInt8 senseKey = senseBuf[2]; #ifdef DEBUG_LOG UInt8 senseCode = senseBuf[12]; UInt8 senseQualifier = senseBuf[13]; IOLog("-- IOATAPIHDDrive::reportMediaState --\n"); IOLog("Error code: %02x\n", errorCode); IOLog("Sense Key : %02x\n", senseKey); IOLog("ASC : %02x\n", senseCode); IOLog("ASCQ : %02x\n", senseQualifier); #endif *mediaPresent = false; *changed = (*mediaPresent != _mediaPresent); _mediaPresent = false; // The error code field for ATAPI request sense should always // be 0x70 or 0x71. Otherwise ignore the sense data. // if ((errorCode == 0x70) || (errorCode == 0x71)) { switch (senseKey) { case 5: /* Invalid ATAPI command */ ret = kIOReturnIOError; break; case 2: /* Not ready */ break; default: break; } } } } while (false); if (cmd) cmd->release(); if (senseData) senseData->release(); #if 0 IOLog("%s: media present %s, changed %s\n", getName(), *mediaPresent ? "Y" : "N", *changed ? "Y" : "N" ); #endif return ret; } //--------------------------------------------------------------------------- // Report media removability. IOReturn IOATAPIHDDrive::reportRemovability(bool * isRemovable) { UInt8 inqBuf[2]; *isRemovable = false; if (_ataDevice->getInquiryData(sizeof(inqBuf), (ATAPIInquiry *) inqBuf)) { if (inqBuf[1] & 0x80) *isRemovable = _isRemovable = true; else *isRemovable = _isRemovable = false; } return kIOReturnSuccess; } //--------------------------------------------------------------------------- // Report whether media is write-protected. IOReturn IOATAPIHDDrive::reportWriteProtection(bool * isWriteProtected) { *isWriteProtected = false; // defaults to read-write return kIOReturnSuccess; } //--------------------------------------------------------------------------- // Instantiate an ATAPI specific subclass of IOBlockStorageDevice. IOService * IOATAPIHDDrive::instantiateNub() { IOService * nub = new IOATAPIHDDriveNub; return nub; } //--------------------------------------------------------------------------- // Override the handleActiveStateTransition() method in IOATAHDDrive and // perform ATAPI specific handling. void IOATAPIHDDrive::handleActiveStateTransition( UInt32 stage, IOReturn status ) { // Restore the lock on the media after the ATAPI device wakes up from // sleep. Assume that the drive will always power up in the unlocked state. // Technically, some drives may have a jumper to set the default state // at power up. if ( ( stage == kIOATAActiveStage0 ) && _isLocked ) { IOStorageCompletion completion; IOReturn ret; IOATACommand * cmd = atapiCommandPreventAllowRemoval( true ); completion.target = this; completion.action = sHandleActiveStateTransition; completion.parameter = (void *) kIOATAActiveStage1; if ( cmd ) { cmd->setQueueInfo( kATAQTypeBypassQ ); ret = asyncExecute( cmd, completion ); cmd->release(); } } else { super::handleActiveStateTransition( stage, status ); } } |