|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.1 (the "License"). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License.
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24: /*
25: * Copyright (c) 1994 NeXT Computer, Inc.
26: *
27: * PCI direct device implementation.
28: *
29: * HISTORY
30: *
31: * 19 Aug 1994 Dean Reece at NeXT
32: * Added class methods.
33: *
34: * 13 May 1994 Dean Reece at NeXT
35: * Created.
36: *
37: */
38:
39: #import <driverkit/i386/PCI.h>
40: #import <driverkit/i386/PCIKernBus.h>
41: #import <driverkit/i386/IOPCIDirectDevice.h>
42: #import <driverkit/i386/IOPCIDeviceDescription.h>
43: #import <driverkit/IODeviceDescription.h>
44:
45: static inline id
46: getThePCIBus(void)
47: {
48: return [KernBus lookupBusInstanceWithName:"PCI" busId:0];
49: }
50:
51: @implementation IODirectDevice(IOPCIDirectDevice)
52:
53: /*
54: * Determine whether or not the associated device is connected to a PCI
55: * bus. Returns YES if so, else returns NO.
56: */
57: + (BOOL)isPCIPresent
58: {
59: id thePCIBus = getThePCIBus();
60:
61: if (thePCIBus == nil) return NO;
62: return [thePCIBus isPCIPresent];
63: }
64:
65: - (BOOL)isPCIPresent
66: {
67: id thePCIBus = getThePCIBus();
68:
69: if (thePCIBus == nil) return NO;
70: return [thePCIBus isPCIPresent];
71: }
72:
73:
74: /*
75: * Reads the device's entire configuration space. Returns IO_R_SUCCESS if
76: * successful. If this method fails, the driver should make no assumptions
77: * about the state of the data returned in the IOPCIConfigSpace struct.
78: */
79: + (IOReturn)getPCIConfigSpace: (IOPCIConfigSpace *) configSpace
80: withDeviceDescription: descr
81: {
82: unsigned char devNum, funNum, busNum;
83: unsigned long *ptr;
84: int address;
85: IOReturn ret;
86: id thePCIBus = getThePCIBus();
87:
88: if (![self isPCIPresent]) return IO_R_NO_DEVICE;
89:
90: ret = [descr getPCIdevice: &devNum function: &funNum bus: &busNum];
91: if (ret != IO_R_SUCCESS) return ret;
92:
93: ptr = (unsigned long *)configSpace;
94: for (address=0x00; address<0x100; address+=0x04) {
95: ret = [thePCIBus getRegister: address
96: device: devNum
97: function: funNum
98: bus: busNum
99: data: ptr++];
100: if (ret != IO_R_SUCCESS) return ret;
101: }
102: return IO_R_SUCCESS;
103: }
104:
105: - (IOReturn)getPCIConfigSpace: (IOPCIConfigSpace *) configSpace
106: {
107: return [[self class] getPCIConfigSpace: configSpace
108: withDeviceDescription:[self deviceDescription]];
109: }
110:
111:
112: /*
113: * Writes the device's entire configuration space. Returns IO_R_SUCCESS if
114: * successful. If this method fails, the driver should make no assumptions
115: * about the state of the device's configuration space.
116: */
117: + (IOReturn)setPCIConfigSpace: (IOPCIConfigSpace *) configSpace
118: withDeviceDescription: descr
119: {
120: unsigned char devNum, funNum, busNum;
121: unsigned long *ptr;
122: int address;
123: IOReturn ret;
124: id thePCIBus = getThePCIBus();
125:
126: if (![self isPCIPresent]) return IO_R_NO_DEVICE;
127:
128: ret = [descr getPCIdevice: &devNum function: &funNum bus: &busNum];
129: if (ret != IO_R_SUCCESS) return ret;
130:
131: ptr = (unsigned long *)configSpace;
132: for (address=0x00; address<0x100; address+=0x04) {
133: ret = [thePCIBus setRegister: address
134: device: devNum
135: function: funNum
136: bus: busNum
137: data: *ptr++];
138: if (ret != IO_R_SUCCESS) return ret;
139: }
140: return IO_R_SUCCESS;
141: }
142:
143: - (IOReturn)setPCIConfigSpace: (IOPCIConfigSpace *) configSpace
144: {
145: return [[self class] setPCIConfigSpace: configSpace
146: withDeviceDescription:[self deviceDescription]];
147: }
148:
149:
150: /*
151: * Reads from the device's configuration space. All access are 32 bits wide
152: * and the address must be aligned as such.
153: */
154: + (IOReturn)getPCIConfigData: (unsigned long *) data
155: atRegister: (unsigned char) address
156: withDeviceDescription: descr
157: {
158: unsigned char devNum, funNum, busNum;
159: IOReturn ret;
160: id thePCIBus = getThePCIBus();
161:
162: if (![self isPCIPresent]) return IO_R_NO_DEVICE;
163:
164: ret = [descr getPCIdevice: &devNum function: &funNum bus: &busNum];
165: if (ret != IO_R_SUCCESS) return ret;
166:
167: return [thePCIBus getRegister: address
168: device: devNum
169: function: funNum
170: bus: busNum
171: data: data];
172: }
173:
174: - (IOReturn)getPCIConfigData: (unsigned long *) data
175: atRegister: (unsigned char) address
176: {
177: return [[self class] getPCIConfigData: data atRegister: address
178: withDeviceDescription:[self deviceDescription]];
179: }
180:
181:
182: /*
183: * Writes to the device's configuration space. All access are 32 bits wide
184: * and the address must be aligned as such. This method reads the register
185: * back after setting it, and returns that value. If this method fails, it
186: * will return PCI_DEFAULT_DATA
187: */
188: + (IOReturn)setPCIConfigData: (unsigned long) data
189: atRegister: (unsigned char) address
190: withDeviceDescription: descr
191: {
192: unsigned char devNum, funNum, busNum;
193: IOReturn ret;
194: id thePCIBus = getThePCIBus();
195:
196: if (![self isPCIPresent]) return IO_R_NO_DEVICE;
197:
198: ret = [descr getPCIdevice: &devNum function: &funNum bus: &busNum];
199: if (ret != IO_R_SUCCESS) return ret;
200:
201: return [thePCIBus setRegister: address
202: device: devNum
203: function: funNum
204: bus: busNum
205: data: data];
206: }
207:
208: - (IOReturn)setPCIConfigData: (unsigned long) data
209: atRegister: (unsigned char) address
210: {
211: return [[self class] setPCIConfigData: data atRegister: address
212: withDeviceDescription:[self deviceDescription]];
213: }
214:
215: @end
216:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.