2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
25 * IOATAPIHDCommand.cpp - Performs ATAPI command processing.
28 * Sep 2, 1999 jliu - Ported from AppleATAPIDrive.
31 #include <IOKit/assert.h>
32 #include <IOKit/storage/ata/IOATAPIHDDrive.h>
34 #define super IOATAHDDrive
36 // Enable this define to generate debugging messages.
37 // #define DEBUG_LOG 1
39 //---------------------------------------------------------------------------
40 // Returns the Command protocol to use (e.g. ataProtocolPIO, ataProtocolDMA).
43 IOATAPIHDDrive::selectCommandProtocol(bool isDMA
)
45 super::selectCommandProtocol(isDMA
);
48 _atapiProtocol
= kATAProtocolATAPIDMA
;
50 _atapiProtocol
= kATAProtocolATAPIPIO
;
55 //---------------------------------------------------------------------------
56 // Setup a ATATaskFile for an ATAPI packet command from the parameters given.
59 IOATAPIHDDrive::setupPacketTaskFile(ATATaskfile
* taskfile
,
63 bzero( taskfile
, sizeof(ATATaskfile
) );
65 taskfile
->protocol
= protocol
;
67 taskfile
->regmask
= ATARegtoMask(kATARegATAPIDeviceSelect
)
68 | ATARegtoMask(kATARegATAPICommand
)
69 | ATARegtoMask(kATARegATAPIByteCountLow
)
70 | ATARegtoMask(kATARegATAPIByteCountHigh
)
71 | ATARegtoMask(kATARegATAPIFeatures
);
73 taskfile
->resultmask
= ATARegtoMask(kATARegATAPIError
);
75 taskfile
->ataRegs
[kATARegATAPIDeviceSelect
] = kATAModeLBA
| (_unit
<< 4);
76 taskfile
->ataRegs
[kATARegATAPICommand
] = kATACommandATAPIPacket
;
77 taskfile
->ataRegs
[kATARegATAPIByteCountLow
] = byteCount
& 0xff;
78 taskfile
->ataRegs
[kATARegATAPIByteCountHigh
] = (byteCount
>> 8) & 0xff;
79 taskfile
->ataRegs
[kATARegATAPIFeatures
] = (protocol
==
80 kATAProtocolATAPIPIO
) ?
81 0 : kIOATAPIFeaturesDMA
;
84 //---------------------------------------------------------------------------
85 // Create a generic ATAPI command object.
88 IOATAPIHDDrive::atapiCommand(ATACDBInfo
* packetCommand
,
89 IOMemoryDescriptor
* transferBuffer
= 0)
93 UInt32 transferLength
;
94 IOATACommand
* cmd
= allocateCommand();
96 if (!cmd
) return 0; // error, command allocation failed.
98 // Create ATA packet command.
100 setupPacketTaskFile(&taskfile
, _atapiProtocol
, kIOATAPIMaxTransfer
);
102 // Get a pointer to the client data buffer, and record parameters
103 // which shall be later used by the completion routine.
105 IOATAClientData
* clientData
= ATA_CLIENT_DATA(cmd
);
108 clientData
->buffer
= transferBuffer
;
110 cmd
->setTaskfile(&taskfile
);
111 cmd
->setCDB(packetCommand
);
113 if (transferBuffer
) {
114 isWrite
= (transferBuffer
->getDirection() == kIODirectionOut
);
115 transferLength
= transferBuffer
->getLength();
121 cmd
->setPointers(transferBuffer
, transferLength
, isWrite
);
126 //---------------------------------------------------------------------------
127 // Allocates and return an IOATACommand to perform a read/write operation.
130 IOATAPIHDDrive::atapiCommandReadWrite(IOMemoryDescriptor
* buffer
,
139 IOLog("%s: atapiCommandReadWrite %08x (%d) %s %d %d\n",
143 (buffer
->getDirection() == kIODirectionOut
) ? "WR" :
149 // Create the ATAPI packet (bytes 1, 10, 11 are reserved).
151 bzero(&atapiCmd
, sizeof(atapiCmd
));
153 atapiCmd
.cdbLength
= 12;
154 atapiCmd
.cdb
[0] = (buffer
->getDirection() == kIODirectionOut
) ?
155 kIOATAPICommandWrite
: kIOATAPICommandRead
;
156 atapiCmd
.cdb
[2] = (UInt8
)(block
>> 24);
157 atapiCmd
.cdb
[3] = (UInt8
)(block
>> 16);
158 atapiCmd
.cdb
[4] = (UInt8
)(block
>> 8);
159 atapiCmd
.cdb
[5] = (UInt8
)(block
);
160 atapiCmd
.cdb
[6] = (UInt8
)(nblks
>> 24);
161 atapiCmd
.cdb
[7] = (UInt8
)(nblks
>> 16);
162 atapiCmd
.cdb
[8] = (UInt8
)(nblks
>> 8);
163 atapiCmd
.cdb
[9] = (UInt8
)(nblks
);
165 return atapiCommand(&atapiCmd
, buffer
);
168 //---------------------------------------------------------------------------
169 // ATAPI Start/Stop Unit command (1B).
172 IOATAPIHDDrive::atapiCommandStartStopUnit(bool doStart
,
179 IOLog("%s: atapiCommandStartStopUnit: %s\n", getName(),
180 doStart
? "start" : "stop");
183 // Create the ATAPI packet.
185 bzero(&atapiCmd
, sizeof(atapiCmd
));
187 atapiCmd
.cdbLength
= 12;
188 atapiCmd
.cdb
[0] = kIOATAPICommandStartStopUnit
;
189 atapiCmd
.cdb
[1] = immediate
? 0x01 : 0x00;
190 atapiCmd
.cdb
[4] = (doStart
? 0x01 : 0) |
191 (doLoadEject
? 0x02 : 0);
193 return atapiCommand(&atapiCmd
);
196 //---------------------------------------------------------------------------
197 // ATAPI Format Unit command (04).
200 IOATAPIHDDrive::atapiCommandFormatUnit(UInt16 interleave
,
203 IOMemoryDescriptor
* formatData
)
207 // Create the ATAPI packet.
209 bzero(&atapiCmd
, sizeof(atapiCmd
));
211 atapiCmd
.cdbLength
= 12;
212 atapiCmd
.cdb
[0] = kIOATAPICommandFormatUnit
;
213 atapiCmd
.cdb
[1] = flagBits
;
214 atapiCmd
.cdb
[3] = (UInt8
)(interleave
>> 8);
215 atapiCmd
.cdb
[4] = (UInt8
)(interleave
);
216 atapiCmd
.cdb
[5] = vendorBits
;
219 atapiCmd
.cdb
[1] |= 0x10;
221 return atapiCommand(&atapiCmd
, formatData
);
224 //---------------------------------------------------------------------------
225 // ATAPI Synchronize Cache command (35).
228 IOATAPIHDDrive::atapiCommandSynchronizeCache()
232 // Create the ATAPI packet.
234 bzero(&atapiCmd
, sizeof(atapiCmd
));
236 atapiCmd
.cdbLength
= 12;
237 atapiCmd
.cdb
[0] = kIOATAPICommandSynchronizeCache
;
239 return atapiCommand(&atapiCmd
);
242 //---------------------------------------------------------------------------
243 // ATAPI Prevent/Allow medium removal command (1E).
246 IOATAPIHDDrive::atapiCommandPreventAllowRemoval(bool doLock
)
250 // Create the ATAPI packet.
252 bzero(&atapiCmd
, sizeof(atapiCmd
));
254 atapiCmd
.cdbLength
= 12;
255 atapiCmd
.cdb
[0] = kIOATAPICommandPreventAllow
;
256 atapiCmd
.cdb
[4] = doLock
? 0x01 : 0;
258 return atapiCommand(&atapiCmd
);
261 //---------------------------------------------------------------------------
262 // ATAPI Test Unit Ready command (00).
265 IOATAPIHDDrive::atapiCommandTestUnitReady()
270 IOLog("%s: atapiCommandTestUnitReady\n", getName());
273 // Create the ATAPI packet.
275 bzero(&atapiCmd
, sizeof(atapiCmd
));
277 atapiCmd
.cdbLength
= 12;
278 atapiCmd
.cdb
[0] = kIOATAPICommandTestUnitReady
;
280 return atapiCommand(&atapiCmd
);
283 //---------------------------------------------------------------------------
284 // atapiCommandModeSense
287 IOATAPIHDDrive::atapiCommandModeSense(IOMemoryDescriptor
* buffer
,
295 bzero(&atapiCmd
, sizeof(atapiCmd
));
297 atapiCmd
.cdbLength
= 12;
298 atapiCmd
.cdb
[0] = kIOATAPICommandModeSense
;
299 atapiCmd
.cdb
[2] = (pageCode
& 0x3f) | ((pageControl
& 0x3) << 6);
300 atapiCmd
.cdb
[7] = (buffer
->getLength() >> 8) & 0xff;
301 atapiCmd
.cdb
[8] = buffer
->getLength() & 0xff;
303 return atapiCommand(&atapiCmd
, buffer
);
306 //---------------------------------------------------------------------------
307 // atapiCommandModeSelect
310 IOATAPIHDDrive::atapiCommandModeSelect(IOMemoryDescriptor
* buffer
)
316 bzero(&atapiCmd
, sizeof(atapiCmd
));
318 atapiCmd
.cdbLength
= 12;
319 atapiCmd
.cdb
[0] = kIOATAPICommandModeSelect
;
320 atapiCmd
.cdb
[1] = 0x10;
321 atapiCmd
.cdb
[7] = (buffer
->getLength() >> 8) & 0xff;
322 atapiCmd
.cdb
[8] = buffer
->getLength() & 0xff;
324 return atapiCommand(&atapiCmd
, buffer
);