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 | /* * 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. * * IOATAPIHDCommand.cpp - Performs ATAPI command processing. * * HISTORY * Sep 2, 1999 jliu - Ported from AppleATAPIDrive. */ #include <IOKit/assert.h> #include <IOKit/storage/ata/IOATAPIHDDrive.h> #define super IOATAHDDrive // Enable this define to generate debugging messages. // #define DEBUG_LOG 1 //--------------------------------------------------------------------------- // Returns the Command protocol to use (e.g. ataProtocolPIO, ataProtocolDMA). bool IOATAPIHDDrive::selectCommandProtocol(bool isDMA) { super::selectCommandProtocol(isDMA); if (isDMA) _atapiProtocol = kATAProtocolATAPIDMA; else _atapiProtocol = kATAProtocolATAPIPIO; return true; } //--------------------------------------------------------------------------- // Setup a ATATaskFile for an ATAPI packet command from the parameters given. void IOATAPIHDDrive::setupPacketTaskFile(ATATaskfile * taskfile, ATAProtocol protocol, UInt16 byteCount) { bzero( taskfile, sizeof(ATATaskfile) ); taskfile->protocol = protocol; taskfile->regmask = ATARegtoMask(kATARegATAPIDeviceSelect) | ATARegtoMask(kATARegATAPICommand) | ATARegtoMask(kATARegATAPIByteCountLow) | ATARegtoMask(kATARegATAPIByteCountHigh) | ATARegtoMask(kATARegATAPIFeatures); taskfile->resultmask = ATARegtoMask(kATARegATAPIError); taskfile->ataRegs[kATARegATAPIDeviceSelect] = kATAModeLBA | (_unit << 4); taskfile->ataRegs[kATARegATAPICommand] = kATACommandATAPIPacket; taskfile->ataRegs[kATARegATAPIByteCountLow] = byteCount & 0xff; taskfile->ataRegs[kATARegATAPIByteCountHigh] = (byteCount >> 8) & 0xff; taskfile->ataRegs[kATARegATAPIFeatures] = (protocol == kATAProtocolATAPIPIO) ? 0 : kIOATAPIFeaturesDMA; } //--------------------------------------------------------------------------- // Create a generic ATAPI command object. IOATACommand * IOATAPIHDDrive::atapiCommand(ATACDBInfo * packetCommand, IOMemoryDescriptor * transferBuffer = 0) { ATATaskfile taskfile; bool isWrite; UInt32 transferLength; IOATACommand * cmd = allocateCommand(); if (!cmd) return 0; // error, command allocation failed. // Create ATA packet command. // setupPacketTaskFile(&taskfile, _atapiProtocol, kIOATAPIMaxTransfer); // Get a pointer to the client data buffer, and record parameters // which shall be later used by the completion routine. // IOATAClientData * clientData = ATA_CLIENT_DATA(cmd); assert(clientData); clientData->buffer = transferBuffer; cmd->setTaskfile(&taskfile); cmd->setCDB(packetCommand); if (transferBuffer) { isWrite = (transferBuffer->getDirection() == kIODirectionOut); transferLength = transferBuffer->getLength(); } else { isWrite = false; transferLength = 0; } cmd->setPointers(transferBuffer, transferLength, isWrite); return cmd; } //--------------------------------------------------------------------------- // Allocates and return an IOATACommand to perform a read/write operation. IOATACommand * IOATAPIHDDrive::atapiCommandReadWrite(IOMemoryDescriptor * buffer, UInt32 block, UInt32 nblks) { ATACDBInfo atapiCmd; assert(buffer); #ifdef DEBUG_LOG IOLog("%s: atapiCommandReadWrite %08x (%d) %s %d %d\n", getName(), buffer, buffer->getLength(), (buffer->getDirection() == kIODirectionOut) ? "WR" : "RD", block, nblks); #endif // Create the ATAPI packet (bytes 1, 10, 11 are reserved). // bzero(&atapiCmd, sizeof(atapiCmd)); atapiCmd.cdbLength = 12; atapiCmd.cdb[0] = (buffer->getDirection() == kIODirectionOut) ? kIOATAPICommandWrite : kIOATAPICommandRead; atapiCmd.cdb[2] = (UInt8)(block >> 24); atapiCmd.cdb[3] = (UInt8)(block >> 16); atapiCmd.cdb[4] = (UInt8)(block >> 8); atapiCmd.cdb[5] = (UInt8)(block); atapiCmd.cdb[6] = (UInt8)(nblks >> 24); atapiCmd.cdb[7] = (UInt8)(nblks >> 16); atapiCmd.cdb[8] = (UInt8)(nblks >> 8); atapiCmd.cdb[9] = (UInt8)(nblks); return atapiCommand(&atapiCmd, buffer); } //--------------------------------------------------------------------------- // ATAPI Start/Stop Unit command (1B). IOATACommand * IOATAPIHDDrive::atapiCommandStartStopUnit(bool doStart, bool doLoadEject, bool immediate) { ATACDBInfo atapiCmd; #ifdef DEBUG_LOG IOLog("%s: atapiCommandStartStopUnit: %s\n", getName(), doStart ? "start" : "stop"); #endif // Create the ATAPI packet. // bzero(&atapiCmd, sizeof(atapiCmd)); atapiCmd.cdbLength = 12; atapiCmd.cdb[0] = kIOATAPICommandStartStopUnit; atapiCmd.cdb[1] = immediate ? 0x01 : 0x00; atapiCmd.cdb[4] = (doStart ? 0x01 : 0) | (doLoadEject ? 0x02 : 0); return atapiCommand(&atapiCmd); } //--------------------------------------------------------------------------- // ATAPI Format Unit command (04). IOATACommand * IOATAPIHDDrive::atapiCommandFormatUnit(UInt16 interleave, UInt8 flagBits, UInt8 vendorBits, IOMemoryDescriptor * formatData) { ATACDBInfo atapiCmd; // Create the ATAPI packet. // bzero(&atapiCmd, sizeof(atapiCmd)); atapiCmd.cdbLength = 12; atapiCmd.cdb[0] = kIOATAPICommandFormatUnit; atapiCmd.cdb[1] = flagBits; atapiCmd.cdb[3] = (UInt8)(interleave >> 8); atapiCmd.cdb[4] = (UInt8)(interleave); atapiCmd.cdb[5] = vendorBits; if (formatData) atapiCmd.cdb[1] |= 0x10; return atapiCommand(&atapiCmd, formatData); } //--------------------------------------------------------------------------- // ATAPI Synchronize Cache command (35). IOATACommand * IOATAPIHDDrive::atapiCommandSynchronizeCache() { ATACDBInfo atapiCmd; // Create the ATAPI packet. // bzero(&atapiCmd, sizeof(atapiCmd)); atapiCmd.cdbLength = 12; atapiCmd.cdb[0] = kIOATAPICommandSynchronizeCache; return atapiCommand(&atapiCmd); } //--------------------------------------------------------------------------- // ATAPI Prevent/Allow medium removal command (1E). IOATACommand * IOATAPIHDDrive::atapiCommandPreventAllowRemoval(bool doLock) { ATACDBInfo atapiCmd; // Create the ATAPI packet. // bzero(&atapiCmd, sizeof(atapiCmd)); atapiCmd.cdbLength = 12; atapiCmd.cdb[0] = kIOATAPICommandPreventAllow; atapiCmd.cdb[4] = doLock ? 0x01 : 0; return atapiCommand(&atapiCmd); } //--------------------------------------------------------------------------- // ATAPI Test Unit Ready command (00). IOATACommand * IOATAPIHDDrive::atapiCommandTestUnitReady() { ATACDBInfo atapiCmd; #ifdef DEBUG_LOG IOLog("%s: atapiCommandTestUnitReady\n", getName()); #endif // Create the ATAPI packet. // bzero(&atapiCmd, sizeof(atapiCmd)); atapiCmd.cdbLength = 12; atapiCmd.cdb[0] = kIOATAPICommandTestUnitReady; return atapiCommand(&atapiCmd); } //--------------------------------------------------------------------------- // atapiCommandModeSense IOATACommand * IOATAPIHDDrive::atapiCommandModeSense(IOMemoryDescriptor * buffer, UInt8 pageCode, UInt8 pageControl) { ATACDBInfo atapiCmd; assert(buffer); bzero(&atapiCmd, sizeof(atapiCmd)); atapiCmd.cdbLength = 12; atapiCmd.cdb[0] = kIOATAPICommandModeSense; atapiCmd.cdb[2] = (pageCode & 0x3f) | ((pageControl & 0x3) << 6); atapiCmd.cdb[7] = (buffer->getLength() >> 8) & 0xff; atapiCmd.cdb[8] = buffer->getLength() & 0xff; return atapiCommand(&atapiCmd, buffer); } //--------------------------------------------------------------------------- // atapiCommandModeSelect IOATACommand * IOATAPIHDDrive::atapiCommandModeSelect(IOMemoryDescriptor * buffer) { ATACDBInfo atapiCmd; assert(buffer); bzero(&atapiCmd, sizeof(atapiCmd)); atapiCmd.cdbLength = 12; atapiCmd.cdb[0] = kIOATAPICommandModeSelect; atapiCmd.cdb[1] = 0x10; atapiCmd.cdb[7] = (buffer->getLength() >> 8) & 0xff; atapiCmd.cdb[8] = buffer->getLength() & 0xff; return atapiCommand(&atapiCmd, buffer); } |