1/*
2 * Copyright (c) 2001-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23#include "AppleRAID.h"
24
25AppleRAIDGlobals gAppleRAIDGlobals;
26
27AppleRAIDGlobals::AppleRAIDGlobals()
28{
29    IOLog1("AppleRAIDGlobals() initing\n");
30    raidGlobalLock = IORecursiveLockAlloc();
31    raidControllerReferences = 0;
32}
33
34AppleRAIDGlobals::~AppleRAIDGlobals()
35{
36    IOLog1("AppleRAIDGlobals::~AppleRAIDGlobals called.\n");
37
38    assert(raidControllerReferences == 0);
39
40    if (raidGlobalLock) {
41        IORecursiveLockFree(raidGlobalLock);
42        raidGlobalLock = 0;
43    }
44}
45
46void AppleRAIDGlobals::lock(void)
47{
48    IORecursiveLockLock(raidGlobalLock);
49}
50
51void AppleRAIDGlobals::unlock(void)
52{
53    IORecursiveLockUnlock(raidGlobalLock);
54}
55
56bool AppleRAIDGlobals::islocked(void)
57{
58    return IORecursiveLockHaveLock(raidGlobalLock);
59}
60
61AppleRAID * AppleRAIDGlobals::getController(void)
62{
63    lock();
64
65    if (!raidController) {
66
67	IOLog1("AppleRAIDGlobals::getController - creating AppleRAID\n");
68	assert(raidControllerReferences == 0);
69
70	// XXX - move all this to AppleRAID class, same for releaseController
71	raidController = new AppleRAID;
72	if (raidController) {
73
74	    raidController->init();
75
76	    const OSSymbol * userClient = OSSymbol::withCStringNoCopy("AppleRAIDUserClient");
77	    if (userClient) {
78		raidController->setProperty(gIOUserClientClassKey, (OSObject *)userClient);
79		userClient->release();
80	    }
81	    raidController->attach(IOService::getResourceService());
82	    raidController->registerService();
83	}
84    }
85
86    if (raidControllerReferences++) raidController->retain();
87
88    unlock();
89
90    return raidController;
91}
92
93void AppleRAIDGlobals::releaseController(void)
94{
95    lock();
96
97    if (raidController) {
98
99	if (--raidControllerReferences == 0) {
100	    raidController->detach(IOService::getResourceService());
101	    raidController->release();
102	    raidController = 0;
103	} else {
104	    raidController->release();
105	}
106    }
107
108    unlock();
109}
110
111