forked from apple-oss-distributions/IOUSBMassStorageClass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOUSBMassStorageClass.cpp
3236 lines (2253 loc) · 98.7 KB
/
IOUSBMassStorageClass.cpp
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998-2015 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The 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, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
//--------------------------------------------------------------------------------------------------
// Includes
//--------------------------------------------------------------------------------------------------
// General OS Services header files
#include <libkern/OSByteOrder.h>
// Local includes
#include "IOUSBMassStorageClass.h"
#include "IOUSBMassStorageClassTimestamps.h"
#include "Debugging.h"
// IOKit includes
#include <IOKit/scsi/IOSCSIPeripheralDeviceNub.h>
#include <IOKit/IODeviceTreeSupport.h>
#include <IOKit/IOKitKeys.h>
//--------------------------------------------------------------------------------------------------
// Defines
//--------------------------------------------------------------------------------------------------
// Default timeout values
enum
{
kDefaultReadTimeoutDuration = 30000,
kDefaultWriteTimeoutDuration = 30000
};
// Default maximum transfer count, in bytes
enum
{
kDefaultMaximumByteCountRead = 131072,
kDefaultMaximumByteCountWrite = 131072,
kDefaultMaximumByteCountReadUSB3 = 1048576,
kDefaultMaximumByteCountWriteUSB3 = 1048576
};
// Maximum number of consecutive I/Os which can be aborted with a Device Reset,
// before the device will be considered removed.
enum
{
kMaxConsecutiveResets = 5
};
//--------------------------------------------------------------------------------------------------
// Macros
//--------------------------------------------------------------------------------------------------
#define fWorkLoop fIOSCSIProtocolInterfaceReserved->fWorkLoop
//--------------------------------------------------------------------------------------------------
// Declarations - USBMassStorageClassGlobals
//--------------------------------------------------------------------------------------------------
class USBMassStorageClassGlobals
{
public:
// Constructor
USBMassStorageClassGlobals ( void );
// Desctructor
virtual ~USBMassStorageClassGlobals ( void );
};
//--------------------------------------------------------------------------------------------------
// Globals - USBMassStorageClassGlobals
//--------------------------------------------------------------------------------------------------
UInt32 gUSBDebugFlags = 0; // Externally defined in IOUSBMassStorageClass.h
static USBMassStorageClassGlobals gUSBGlobals;
static int USBMassStorageClassSysctl ( struct sysctl_oid * oidp, void * arg1, int arg2, struct sysctl_req * req );
SYSCTL_PROC ( _debug, OID_AUTO, USBMassStorageClass, CTLFLAG_RW, 0, 0, USBMassStorageClassSysctl, "USBMassStorageClass", "USBMassStorageClass debug interface" );
//--------------------------------------------------------------------------------------------------
// USBMassStorageClassSysctl - Sysctl handler. [STATIC]
//--------------------------------------------------------------------------------------------------
static int
USBMassStorageClassSysctl ( struct sysctl_oid * oidp, void * arg1, int arg2, struct sysctl_req * req )
{
int error = 0;
USBSysctlArgs usbArgs;
UNUSED ( oidp );
UNUSED ( arg1 );
UNUSED ( arg2 );
STATUS_LOG ( ( 1, "+USBMassStorageClassGlobals: gUSBDebugFlags = 0x%08X\n", ( unsigned int ) gUSBDebugFlags ) );
error = SYSCTL_IN ( req, &usbArgs, sizeof ( usbArgs ) );
if ( ( error == 0 ) && ( usbArgs.type == kUSBTypeDebug ) )
{
if ( usbArgs.operation == kUSBOperationGetFlags )
{
usbArgs.debugFlags = gUSBDebugFlags;
error = SYSCTL_OUT ( req, &usbArgs, sizeof ( usbArgs ) );
}
else if ( usbArgs.operation == kUSBOperationSetFlags )
{
gUSBDebugFlags = usbArgs.debugFlags;
}
}
STATUS_LOG ( ( 1, "-USBMassStorageClassGlobals: gUSBDebugFlags = 0x%08X\n", ( unsigned int ) gUSBDebugFlags ) );
return error;
}
//--------------------------------------------------------------------------------------------------
// USBMassStorageClassGlobals - Default Constructor [PUBLIC]
//--------------------------------------------------------------------------------------------------
USBMassStorageClassGlobals::USBMassStorageClassGlobals ( void )
{
int debugFlags;
STATUS_LOG ( ( 1, "+USBMassStorageClassGlobals::USBMassStorageClassGlobals\n" ) );
if ( PE_parse_boot_argn ( "USB-MassStorage", &debugFlags, sizeof ( debugFlags ) ) )
{
gUSBDebugFlags = debugFlags;
}
// Register our sysctl interface
sysctl_register_oid ( &sysctl__debug_USBMassStorageClass );
STATUS_LOG ( ( 1, "-USBMassStorageClassGlobals::USBMassStorageClassGlobals\n" ) );
}
//--------------------------------------------------------------------------------------------------
// USBMassStorageClassGlobals - Destructor [PUBLIC]
//--------------------------------------------------------------------------------------------------
USBMassStorageClassGlobals::~USBMassStorageClassGlobals ( void )
{
STATUS_LOG ( ( 1, "+~USBMassStorageClassGlobals::USBMassStorageClassGlobals\n" ) );
// Unregister our sysctl interface
sysctl_unregister_oid ( &sysctl__debug_USBMassStorageClass );
STATUS_LOG ( ( 1, "-~USBMassStorageClassGlobals::USBMassStorageClassGlobals\n" ) );
}
//--------------------------------------------------------------------------------------------------
// Declarations - IOUSBMassStorageClass
//--------------------------------------------------------------------------------------------------
#define super IOSCSIProtocolServices
OSDefineMetaClassAndStructors( IOUSBMassStorageClass, IOSCSIProtocolServices )
//--------------------------------------------------------------------------------------------------
// init - Called at initialization time [PUBLIC]
//--------------------------------------------------------------------------------------------------
bool
IOUSBMassStorageClass::init ( OSDictionary * propTable )
{
if ( super::init( propTable ) == false)
{
return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------
// start - Called at services start time (after successful matching) [PUBLIC]
//--------------------------------------------------------------------------------------------------
bool
IOUSBMassStorageClass::start ( IOService * provider )
{
IOUSBFindEndpointRequest request;
IOUSBInterface * interface = 0;
OSDictionary * characterDict = NULL;
OSObject * obj = NULL;
IOReturn result = kIOReturnError;
OSNumber * number = NULL;
bool success = false;
if ( super::start ( provider ) == false )
{
STATUS_LOG ( ( 1, "%s[%p]: superclass start failure.", getName ( ), this ) );
goto Exit;
}
RecordUSBTimeStamp ( UMC_TRACE ( kIOUSBMassStorageClassStart ),
( uintptr_t ) this, NULL,
NULL, NULL );
#ifndef EMBEDDED
// Allocate data for our expansion data.
reserved = ( ExpansionData * ) IOMalloc ( sizeof ( ExpansionData ) );
bzero ( reserved, sizeof ( ExpansionData ) );
#endif // EMBEDDED
// Save the reference to the interface on the device that will be
// the provider for this object.
interface = OSDynamicCast ( IOUSBInterface, provider );
require_nonzero ( interface, AbortStart );
SetInterfaceReference ( interface );
// Check if a subclass has marked this device as not to be operated at all.
if ( provider->getProperty ( kIOUSBMassStorageDoNotMatch ) != NULL )
{
goto AbortStart;
}
RecordUSBTimeStamp ( UMC_TRACE ( kAtUSBAddress ),
( uintptr_t ) this, ( unsigned int ) GetInterfaceReference ( )->GetDevice ( )->GetAddress ( ),
NULL, NULL );
STATUS_LOG ( ( 6, "%s[%p]: USB Mass Storage @ %d",
getName(), this,
GetInterfaceReference ( )->GetDevice ( )->GetAddress ( ) ) );
if ( GetInterfaceReference()->open ( this ) == false )
{
STATUS_LOG ( ( 1, "%s[%p]: could not open the interface", getName ( ), this ) );
goto AbortStart;
}
// Set the IOUSBPipe object pointers to NULL so that the driver can
// release these objects if instantition is not successful.
fBulkInPipe = NULL;
fBulkOutPipe = NULL;
fInterruptPipe = NULL;
// Default is to have no clients
fClients = NULL;
// Default is to have a max lun of 0.
SetMaxLogicalUnitNumber ( 0 );
// Initialize all Bulk Only related member variables to their default
// states.
fBulkOnlyCommandTag = 0;
fBulkOnlyCommandStructInUse = false;
// Initialize all CBI related member variables to their default
// states.
fCBICommandStructInUse = false;
// Flag we use to indicate whether or not the device requires the standard
// USB device reset instead of the BO reset. This applies to BO devices only.
fUseUSBResetNotBOReset = false;
// Certain Bulk-Only device are subject to erroneous CSW tags.
fKnownCSWTagMismatchIssues = false;
// Flag to let us know if we've seen the reconfiguration message following a device reset.
// If we proceed with operations prior to receiving the message we may end up booting a
// CBW out on the bus prior to the SET_CONFIGURATION which follows the reset. This will
// hamper recovery and confuse the state machine of the USB device we're operating.
fWaitingForReconfigurationMessage = false;
// Used to determine if we're going to block on the reset thread or not.
fBlockOnResetThread = false;
// Used to determine where we should close our provider at time of termination.
fTerminating = false;
// IOSAM may request that we suspend/resume our port instead of spin up/down media.
fPortSuspendResumeForPMEnabled = false;
#ifndef EMBEDDED
// Workaround flag for devices which spin themselves up/down and have problems with driver intervention.
fAutonomousSpinDownWorkAround = false;
// Some devices with complicated interal logic require some "cool down" time following a
// USB device reset before they can resume servicing requests.
fPostDeviceResetCoolDownInterval = 0;
#endif // EMBEDDED
// Check if the personality for this device specifies a preferred protocol
characterDict = OSDynamicCast ( OSDictionary, getProperty ( kIOUSBMassStorageCharacteristics ) );
if ( characterDict == NULL )
{
// This device does not specify a preferred protocol, use the protocol
// defined in the descriptor.
fPreferredProtocol = GetInterfaceReference ( )->GetInterfaceProtocol ( );
fPreferredSubclass = GetInterfaceReference ( )->GetInterfaceSubClass ( );
}
else
{
OSNumber * preferredProtocol;
OSNumber * preferredSubclass;
RecordUSBTimeStamp ( UMC_TRACE ( kIOUMCStorageCharacDictFound ),
( uintptr_t ) this, NULL, NULL, NULL );
// Check if we have a USB storage personality for this particular device.
preferredProtocol = OSDynamicCast ( OSNumber, characterDict->getObject ( kIOUSBMassStoragePreferredProtocol ) );
if ( preferredProtocol == NULL )
{
// This device does not specify a preferred protocol, use the
// protocol defined in the interface descriptor.
fPreferredProtocol = GetInterfaceReference()->GetInterfaceProtocol ( );
}
else
{
// This device has a preferred protocol, use that.
fPreferredProtocol = preferredProtocol->unsigned32BitValue ( );
}
// Check if this device is not to be operated at all.
if ( characterDict->getObject( kIOUSBMassStorageDoNotOperate ) != NULL )
{
goto AbortStart;
}
// Check if this device is known not to support the bulk-only USB reset.
if ( characterDict->getObject ( kIOUSBMassStorageUseStandardUSBReset ) != NULL )
{
fUseUSBResetNotBOReset = true;
}
// Is this a device which has CBW/CSW tag issues?
if ( characterDict->getObject ( kIOUSBKnownCSWTagIssues ) != NULL )
{
fKnownCSWTagMismatchIssues = true;
}
if ( characterDict->getObject( kIOUSBMassStorageEnableSuspendResumePM ) != NULL )
{
fPortSuspendResumeForPMEnabled = true;
}
#ifndef EMBEDDED
// Check if this device is known to have problems when waking from sleep
if ( characterDict->getObject ( kIOUSBMassStorageResetOnResume ) != NULL )
{
STATUS_LOG ( ( 4, "%s[%p]: knownResetOnResumeDevice", getName ( ), this ) );
fRequiresResetOnResume = true;
}
// Check to see if this device requires some time after USB reset to collect itself.
if ( characterDict->getObject ( kIOUSBMassStoragePostResetCoolDown ) != NULL )
{
OSNumber * coolDownPeriod = NULL;
coolDownPeriod = OSDynamicCast ( OSNumber, characterDict->getObject( kIOUSBMassStoragePostResetCoolDown ) );
// Ensure we didn't get something of thew wrong type.
if ( coolDownPeriod != NULL )
{
// Fetch our cool down interval.
fPostDeviceResetCoolDownInterval = coolDownPeriod->unsigned32BitValue ( );
}
}
#endif // EMBEDDED
// Check if the personality for this device specifies a preferred subclass
preferredSubclass = OSDynamicCast ( OSNumber, characterDict->getObject ( kIOUSBMassStoragePreferredSubclass ) );
if ( preferredSubclass == NULL )
{
// This device does not specify a preferred subclass, use the
// subclass defined in the interface descriptor.
fPreferredSubclass = GetInterfaceReference ( )->GetInterfaceSubClass ( );
}
else
{
// This device has a preferred protocol, use that.
fPreferredSubclass = preferredSubclass->unsigned32BitValue ( );
}
#ifndef EMBEDDED
// Check if the device needs to be suspended on reboot
if ( characterDict->getObject ( kIOUSBMassStorageSuspendOnReboot ) != NULL )
{
fSuspendOnReboot = true;
}
#endif // EMBEDDED
}
STATUS_LOG ( ( 6, "%s[%p]: Preferred Protocol is: %d", getName ( ), this, fPreferredProtocol ) );
STATUS_LOG ( ( 6, "%s[%p]: Preferred Subclass is: %d", getName ( ), this, fPreferredSubclass ) );
// Verify that the device has a supported interface type and configure that
// Interrupt pipe if the protocol requires one.
STATUS_LOG ( ( 7, "%s[%p]: Configure the Storage interface", getName ( ), this ) );
switch ( GetInterfaceProtocol ( ) )
{
case kProtocolControlBulkInterrupt:
{
RecordUSBTimeStamp ( UMC_TRACE ( kCBIProtocolDeviceDetected ),
( uintptr_t ) this, NULL,
NULL, NULL );
// Find the interrupt pipe for the device.
// Note that the pipe will already be retained on our behalf.
request.type = kUSBInterrupt;
request.direction = kUSBIn;
fInterruptPipe = GetInterfaceReference ( )->FindNextPipe ( NULL, &request, true );
STATUS_LOG ( ( 7, "%s[%p]: find interrupt pipe", getName ( ), this ) );
require_nonzero ( fInterruptPipe, AbortStart );
fCBIMemoryDescriptor = IOBufferMemoryDescriptor::withOptions ( kIODirectionIn,
round_page(kUSBStorageAutoStatusSize),
PAGE_SIZE );
require_nonzero ( fCBIMemoryDescriptor, AbortStart );
fCBIMemoryDescriptor->setLength ( kUSBStorageAutoStatusSize );
bzero(fCBIMemoryDescriptor->getBytesNoCopy ( ),
fCBIMemoryDescriptor->getCapacity ( ) );
result = fCBIMemoryDescriptor->prepare ( );
require_success ( result, AbortStart );
}
break;
case kProtocolControlBulk:
// Since all the CB devices I have seen do not use the interrupt
// endpoint, even if it exists, ignore it if present.
case kProtocolBulkOnly:
{
STATUS_LOG ( ( 7, "%s[%p]: Bulk Only - skip interrupt pipe", getName ( ), this ) );
RecordUSBTimeStamp ( UMC_TRACE ( kBODeviceDetected ),
( uintptr_t ) this, NULL, NULL, NULL );
// Allocate the memory descriptor needed to send the CBW out.
fBulkOnlyCBWMemoryDescriptor = IOBufferMemoryDescriptor::withOptions ( kIODirectionOut,
round_page(kByteCountOfCBW),
PAGE_SIZE);
require_nonzero ( fBulkOnlyCBWMemoryDescriptor, AbortStart );
fBulkOnlyCBWMemoryDescriptor->setLength ( kByteCountOfCBW );
bzero(fBulkOnlyCBWMemoryDescriptor->getBytesNoCopy ( ),
fBulkOnlyCBWMemoryDescriptor->getCapacity ( ) );
result = fBulkOnlyCBWMemoryDescriptor->prepare ( );
require_success ( result, AbortStart );
// Allocate the memory descriptor needed to retrieve the CSW.
fBulkOnlyCSWMemoryDescriptor = IOBufferMemoryDescriptor::withOptions ( kIODirectionIn,
round_page(kByteCountOfCSW),
PAGE_SIZE );
require_nonzero ( fBulkOnlyCSWMemoryDescriptor, AbortStart );
fBulkOnlyCSWMemoryDescriptor->setLength ( kByteCountOfCSW );
bzero(fBulkOnlyCSWMemoryDescriptor->getBytesNoCopy ( ),
fBulkOnlyCSWMemoryDescriptor->getCapacity ( ) );
result = fBulkOnlyCSWMemoryDescriptor->prepare ( );
require_success ( result, AbortStart );
}
break;
default:
{
RecordUSBTimeStamp ( UMC_TRACE ( kNoProtocolForDevice ),
( uintptr_t ) this, NULL, NULL, NULL );
// The device has a protocol that the driver does not
// support. Return false to indicate that instantiation was
// not successful.
goto AbortStart;
}
break;
}
// Find the Bulk In pipe for the device
STATUS_LOG ( ( 7, "%s[%p]: find bulk in pipe", getName ( ), this ) );
request.type = kUSBBulk;
request.direction = kUSBIn;
fBulkInPipe = GetInterfaceReference ( )->FindNextPipe ( NULL, &request, true );
require_nonzero ( fBulkInPipe, AbortStart );
// Find the Bulk Out pipe for the device
STATUS_LOG ( ( 7, "%s[%p]: find bulk out pipe", getName(), this ) );
request.type = kUSBBulk;
request.direction = kUSBOut;
fBulkOutPipe = GetInterfaceReference ( )->FindNextPipe ( NULL, &request, true );
require_nonzero ( fBulkOutPipe, AbortStart );
// Build the Protocol Characteristics dictionary since not all devices will have a
// SCSI Peripheral Device Nub to guarantee its existance.
characterDict = OSDynamicCast ( OSDictionary, getProperty ( kIOPropertyProtocolCharacteristicsKey ) );
if ( characterDict == NULL )
{
characterDict = OSDictionary::withCapacity ( 1 );
}
else
{
characterDict->retain ( );
}
require_nonzero ( characterDict, AbortStart );
obj = getProperty ( kIOPropertyPhysicalInterconnectTypeKey );
if ( obj != NULL )
{
characterDict->setObject ( kIOPropertyPhysicalInterconnectTypeKey, obj );
}
obj = getProperty ( kIOPropertyPhysicalInterconnectLocationKey );
if ( obj != NULL )
{
characterDict->setObject ( kIOPropertyPhysicalInterconnectLocationKey, obj );
}
// Check to see if this device is internal or not. The results of this check if successful
// will override location data provided in IOKit personalities.
if ( IsPhysicalInterconnectLocationInternal ( ) )
{
OSString * internalString = NULL;
internalString = OSString::withCString ( kIOPropertyInternalKey );
if ( internalString != NULL )
{
characterDict->setObject ( kIOPropertyPhysicalInterconnectLocationKey, internalString );
internalString->release ( );
internalString = NULL;
}
}
obj = getProperty ( kIOPropertyReadTimeOutDurationKey );
if ( obj != NULL )
{
characterDict->setObject ( kIOPropertyReadTimeOutDurationKey, obj );
}
else
{
number = OSNumber::withNumber ( kDefaultReadTimeoutDuration, 32 );
if ( number != NULL )
{
characterDict->setObject ( kIOPropertyReadTimeOutDurationKey, number );
number->release ( );
number = NULL;
}
}
obj = getProperty ( kIOPropertyWriteTimeOutDurationKey );
if ( obj != NULL )
{
characterDict->setObject ( kIOPropertyWriteTimeOutDurationKey, obj );
}
else
{
number = OSNumber::withNumber ( kDefaultWriteTimeoutDuration, 32 );
if ( number != NULL )
{
characterDict->setObject ( kIOPropertyWriteTimeOutDurationKey, number );
number->release ( );
number = NULL;
}
}
setProperty ( kIOPropertyProtocolCharacteristicsKey, characterDict );
characterDict->release ( );
characterDict = NULL;
STATUS_LOG ( ( 6, "%s[%p]: successfully configured", getName ( ), this ) );
#if defined (__i386__) || defined (__x86_64__)
{
// As USB booting is only supporting on i386 based, do not compile for PPC.
char usbDeviceAddress [ kUSBDAddressLength ];
OSNumber * usbDeviceID;
snprintf ( usbDeviceAddress, kUSBDAddressLength, "%x", ( int ) GetInterfaceReference ( )->GetDevice ( )->GetAddress ( ) );
usbDeviceID = OSNumber::withNumber ( ( int ) GetInterfaceReference ( )->GetDevice ( )->GetAddress ( ), 64 );
if ( usbDeviceID != NULL )
{
setProperty ( kIOPropertyIOUnitKey, usbDeviceID );
setLocation ( ( const char * ) usbDeviceAddress, gIODTPlane );
usbDeviceID->release ( );
}
}
#endif
// Device configured. We're attached.
fDeviceAttached = true;
InitializePowerManagement ( GetInterfaceReference ( ) );
success = BeginProvidedServices ( );
require ( success, AbortStart );
return true;
AbortStart:
STATUS_LOG ( ( 1, "%s[%p]: aborting start.", getName ( ), this ) );
// Stop PM.
if ( IsPowerManagementIntialized ( ) )
{
PMstop ( );
}
// Close and release our USB pipe references.
if ( fBulkInPipe != NULL )
{
fBulkInPipe->release ( );
fBulkInPipe = NULL;
}
if ( fBulkOutPipe != NULL )
{
fBulkOutPipe->release ( );
fBulkOutPipe = NULL;
}
if ( fInterruptPipe != NULL )
{
fInterruptPipe->release ( );
fInterruptPipe = NULL;
}
// Close and nullify our USB Interface.
if ( interface != NULL )
{
SetInterfaceReference ( NULL );
interface->close ( this );
}
// Clean up our protocol packet related IOMDs.
if ( fCBIMemoryDescriptor != NULL )
{
fCBIMemoryDescriptor->complete ( );
fCBIMemoryDescriptor->release ( );
fCBIMemoryDescriptor = NULL;
}
if ( fBulkOnlyCBWMemoryDescriptor != NULL )
{
fBulkOnlyCBWMemoryDescriptor->complete ( );
fBulkOnlyCBWMemoryDescriptor->release ( );
fBulkOnlyCBWMemoryDescriptor = NULL;
}
if ( fBulkOnlyCSWMemoryDescriptor != NULL )
{
fBulkOnlyCSWMemoryDescriptor->complete ( );
fBulkOnlyCSWMemoryDescriptor->release ( );
fBulkOnlyCSWMemoryDescriptor = NULL;
}
super::stop ( provider );
Exit:
return false;
}
//--------------------------------------------------------------------------------------------------
// stop - Called at stop time [PUBLIC]
//--------------------------------------------------------------------------------------------------
void
IOUSBMassStorageClass::stop ( IOService * provider )
{
// I am logging this as a 1 because if anything is logging after this we want to know about it.
// This should be the last message we see. Bye bye!
STATUS_LOG ( ( 1, "%s[%p]: stop: Called", getName(), this ) );
RecordUSBTimeStamp ( UMC_TRACE ( kIOUSBMassStorageClassStop ),
( uintptr_t ) this, NULL, NULL, NULL );
EndProvidedServices ( );
super::stop ( provider );
}
//--------------------------------------------------------------------------------------------------
// free - Called by IOKit to free any resources. [PUBLIC]
//--------------------------------------------------------------------------------------------------
void
IOUSBMassStorageClass::free ( void )
{
STATUS_LOG ( ( 1, "%s[%p]: free: Called", getName(), this ) );
#ifndef EMBEDDED
require_nonzero ( reserved, Exit );
#endif // EMBEDDED
// Since fClients is defined as reserved->fClients we don't want
// to dereference it unless reserved is non-NULL.
if ( fClients != NULL )
{
fClients->release ( );
fClients = NULL;
}
if ( fBulkInPipe != NULL )
{
fBulkInPipe->release ( );
fBulkInPipe = NULL;
}
if ( fBulkOutPipe != NULL )
{
fBulkOutPipe->release ( );
fBulkOutPipe = NULL;
}
if ( fInterruptPipe != NULL )
{
fInterruptPipe->release ( );
fInterruptPipe = NULL;
}
if ( fCBIMemoryDescriptor != NULL )
{
fCBIMemoryDescriptor->complete ( );
fCBIMemoryDescriptor->release ( );
fCBIMemoryDescriptor = NULL;
}
if ( fBulkOnlyCBWMemoryDescriptor != NULL )
{
fBulkOnlyCBWMemoryDescriptor->complete ( );
fBulkOnlyCBWMemoryDescriptor->release ( );
fBulkOnlyCBWMemoryDescriptor = NULL;
}
if ( fBulkOnlyCSWMemoryDescriptor != NULL )
{
fBulkOnlyCSWMemoryDescriptor->complete ( );
fBulkOnlyCSWMemoryDescriptor->release ( );
fBulkOnlyCSWMemoryDescriptor = NULL;
}
#ifndef EMBEDDED
IOFree ( reserved, sizeof ( ExpansionData ) );
reserved = NULL;
Exit:
#endif // EMBEDDED
super::free ( );
}
//--------------------------------------------------------------------------------------------------
// message - Called by IOKit to deliver messages. [PUBLIC]
//--------------------------------------------------------------------------------------------------
IOReturn
IOUSBMassStorageClass::message ( UInt32 type, IOService * provider, void * argument )
{
IOReturn result = kIOReturnSuccess;
RecordUSBTimeStamp ( UMC_TRACE ( kMessagedCalled ), ( uintptr_t ) this, type, NULL, NULL );
STATUS_LOG ( ( 4, "%s[%p]: message = %lx called", getName ( ), this, type ) );
switch ( type )
{
case kIOUSBMessagePortHasBeenResumed:
{
STATUS_LOG ( ( 2, "%s[%p]: message kIOUSBMessagePortHasBeenResumed.", getName ( ), this ) );
#ifndef EMBEDDED
if ( fRequiresResetOnResume == true )
{
result = ResetDeviceNow ( true );
}
#else // EMBEDDED
result = ResetDeviceNow ( true );
#endif // EMBEDDED
}
break;
case kIOUSBMessageCompositeDriverReconfigured:
{
fWaitingForReconfigurationMessage = false;
}
break;
default:
{
STATUS_LOG ( ( 2, "%s[%p]: message default case.", getName ( ), this ) );
result = super::message ( type, provider, argument );
}
}
return result;
}
//--------------------------------------------------------------------------------------------------
// willTerminate [PUBLIC]
//--------------------------------------------------------------------------------------------------
bool
IOUSBMassStorageClass::willTerminate ( IOService * provider,
IOOptionBits options )
{
STATUS_LOG ( ( 2, "%s[%p]: willTerminate called.", getName ( ), this ) );
RecordUSBTimeStamp ( UMC_TRACE ( kWillTerminateCalled ),
( uintptr_t ) this, ( uintptr_t ) GetInterfaceReference ( ),
( unsigned int ) isInactive ( ), NULL );
// Mark ourselves as terminating so we don't accept any additional I/O.
fTerminating = true;
// Let clients know that the device is gone.
SendNotification_DeviceRemoved ( );
return super::willTerminate ( provider, options );
}
//--------------------------------------------------------------------------------------------------
// didTerminate [PUBLIC]
//--------------------------------------------------------------------------------------------------
bool
IOUSBMassStorageClass::didTerminate ( IOService * provider, IOOptionBits options, bool * defer )
{
IOUSBInterface * currentInterface;
IOReturn status;
bool success;
// This method comes at the end of the termination sequence. Hopefully, all of our outstanding IO is complete
// in which case we can just close our provider and IOKit will take care of the rest. Otherwise, we need to
// hold on to the device and IOKit will terminate us when we close it later.
STATUS_LOG ( ( 3 , "%s[%p]::didTerminate: Entered with options=0x%x defer=%d", getName ( ), this, options, ( defer ? *defer : false ) ) );
// Abort pipes to ensure that any outstanding USB requests are returned to us.
// For USB it is the responsibility of the client driver to request outstanding
// I/O requests be returned once driver termination has been initiated.
if ( fBulkInPipe != NULL )
{
fBulkInPipe->Abort ( );
}
if ( fBulkOutPipe != NULL )
{
fBulkOutPipe->Abort ( );
}
if ( fInterruptPipe != NULL )
{
fInterruptPipe->Abort ( );
}
// If we have a SCSI task outstanding, we will block here until it completes.
// This ensures that we don't try to send requests to our provider after we have closed it.
fTerminationDeferred = fBulkOnlyCommandStructInUse | fCBICommandStructInUse;
RecordUSBTimeStamp ( UMC_TRACE ( kDidTerminateCalled ),
( uintptr_t ) this, ( unsigned int ) fTerminationDeferred, NULL, NULL );
while ( fTerminationDeferred == true )
{