xref: /haiku/src/add-ons/kernel/bus_managers/pci/pci_device.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2004-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Copyright 2002-2003, Thomas Kurschel. All rights reserved.
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "pci.h"
14 #include "pci_private.h"
15 
16 
17 // information about one PCI device
18 struct pci_device {
19 	PCIDev*					device;
20 	device_node*			node;
21 };
22 
23 
24 static uint8
25 pci_device_read_io_8(pci_device* device, addr_t mappedIOAddress)
26 {
27 	return pci_read_io_8(mappedIOAddress);
28 }
29 
30 
31 static void
32 pci_device_write_io_8(pci_device* device, addr_t mappedIOAddress,
33 	uint8 value)
34 {
35 	pci_write_io_8(mappedIOAddress, value);
36 }
37 
38 
39 static uint16
40 pci_device_read_io_16(pci_device* device, addr_t mappedIOAddress)
41 {
42 	return pci_read_io_16(mappedIOAddress);
43 }
44 
45 
46 static void
47 pci_device_write_io_16(pci_device* device, addr_t mappedIOAddress,
48 	uint16 value)
49 {
50 	pci_write_io_16(mappedIOAddress, value);
51 }
52 
53 
54 static uint32
55 pci_device_read_io_32(pci_device* device, addr_t mappedIOAddress)
56 {
57 	return pci_read_io_32(mappedIOAddress);
58 }
59 
60 
61 static void
62 pci_device_write_io_32(pci_device* device, addr_t mappedIOAddress, uint32 value)
63 {
64 	pci_write_io_32(mappedIOAddress, value);
65 }
66 
67 
68 static uint32
69 pci_device_read_pci_config(pci_device* device, uint16 offset, uint8 size)
70 {
71 	return gPCI->ReadConfig(device->device, offset, size);
72 }
73 
74 
75 static void
76 pci_device_write_pci_config(pci_device* device, uint16 offset, uint8 size,
77 	uint32 value)
78 {
79 	gPCI->WriteConfig(device->device, offset, size, value);
80 }
81 
82 
83 static phys_addr_t
84 pci_device_ram_address(pci_device* device, phys_addr_t physicalAddress)
85 {
86 	return pci_ram_address(physicalAddress);
87 }
88 
89 
90 static status_t
91 pci_device_find_capability(pci_device* device, uint8 capID, uint8* offset)
92 {
93 	return gPCI->FindCapability(device->device, capID, offset);
94 }
95 
96 
97 static status_t
98 pci_device_find_extended_capability(pci_device* device, uint16 capID,
99 	uint16* offset)
100 {
101 	return gPCI->FindExtendedCapability(device->device, capID, offset);
102 }
103 
104 
105 static void
106 pci_device_get_pci_info(pci_device* device, struct pci_info* info)
107 {
108 	if (info == NULL)
109 		return;
110 
111 	*info = device->device->info;
112 }
113 
114 
115 static status_t
116 pci_device_init_driver(device_node* node, void** _cookie)
117 {
118 	uint8 domain;
119 	uint8 bus, deviceNumber, function;
120 	if (gDeviceManager->get_attr_uint8(node, B_PCI_DEVICE_DOMAIN, &domain,
121 			false) != B_OK
122 		|| gDeviceManager->get_attr_uint8(node, B_PCI_DEVICE_BUS, &bus,
123 			false) != B_OK
124 		|| gDeviceManager->get_attr_uint8(node, B_PCI_DEVICE_DEVICE,
125 			&deviceNumber, false) != B_OK
126 		|| gDeviceManager->get_attr_uint8(node, B_PCI_DEVICE_FUNCTION,
127 			&function, false) != B_OK)
128 		return B_ERROR;
129 
130 	PCIDev *dev = gPCI->FindDevice(domain, bus, deviceNumber, function);
131 	if (dev == NULL) {
132 		panic("device not found!\n");
133 		return ENODEV;
134 	}
135 
136 	pci_device* device = (pci_device*)malloc(sizeof(*device));
137 	if (device == NULL)
138 		return B_NO_MEMORY;
139 
140 	device->device = dev;
141 	device->node = node;
142 
143 	*_cookie = device;
144 	return B_OK;
145 }
146 
147 
148 static void
149 pci_device_uninit_driver(void* cookie)
150 {
151 	pci_device* device = (pci_device*)cookie;
152 	free(device);
153 }
154 
155 
156 static status_t
157 pci_device_std_ops(int32 op, ...)
158 {
159 	switch (op) {
160 		case B_MODULE_INIT:
161 		case B_MODULE_UNINIT:
162 			return B_OK;
163 	}
164 
165 	return B_BAD_VALUE;
166 }
167 
168 
169 pci_device_module_info gPCIDeviceModule = {
170 	{
171 		{
172 			PCI_DEVICE_MODULE_NAME,
173 			0,
174 			pci_device_std_ops
175 		},
176 
177 		NULL,		// supports device
178 		NULL,		// register device (our parent registered us)
179 		pci_device_init_driver,
180 		pci_device_uninit_driver,
181 		NULL,		// register child devices
182 		NULL,		// rescan devices
183 		NULL,		// device removed
184 	},
185 
186 	pci_device_read_io_8,
187 	pci_device_write_io_8,
188 	pci_device_read_io_16,
189 	pci_device_write_io_16,
190 	pci_device_read_io_32,
191 	pci_device_write_io_32,
192 
193 	pci_device_ram_address,
194 
195 	pci_device_read_pci_config,
196 	pci_device_write_pci_config,
197 	pci_device_find_capability,
198 	pci_device_get_pci_info,
199 	pci_device_find_extended_capability
200 };
201