xref: /haiku/src/add-ons/kernel/bus_managers/pci/pci_device.cpp (revision f75a7bf508f3156d63a14f8fd77c5e0ca4d08c42)
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, uint8 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, uint8 offset, uint8 size,
77 	uint32 value)
78 {
79 	gPCI->WriteConfig(device->device, offset, size, value);
80 }
81 
82 
83 static void*
84 pci_device_ram_address(pci_device* device, const void* 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 void
98 pci_device_get_pci_info(pci_device* device, struct pci_info* info)
99 {
100 	if (info == NULL)
101 		return;
102 
103 	*info = device->device->info;
104 }
105 
106 
107 static status_t
108 pci_device_init_driver(device_node* node, void** _cookie)
109 {
110 	uint32 domain;
111 	uint8 bus, deviceNumber, function;
112 	if (gDeviceManager->get_attr_uint32(node, B_PCI_DEVICE_DOMAIN, &domain,
113 			false) != B_OK
114 		|| gDeviceManager->get_attr_uint8(node, B_PCI_DEVICE_BUS, &bus,
115 			false) != B_OK
116 		|| gDeviceManager->get_attr_uint8(node, B_PCI_DEVICE_DEVICE,
117 			&deviceNumber, false) != B_OK
118 		|| gDeviceManager->get_attr_uint8(node, B_PCI_DEVICE_FUNCTION,
119 			&function, false) != B_OK)
120 		return B_ERROR;
121 
122 	pci_device* device = (pci_device*)malloc(sizeof(*device));
123 	if (device == NULL)
124 		return B_NO_MEMORY;
125 
126 	device->device = gPCI->FindDevice(domain, bus, deviceNumber, function);
127 	if (device->device == NULL)
128 		panic("device not found!\n");
129 
130 	device->node = node;
131 
132 	*_cookie = device;
133 	return B_OK;
134 }
135 
136 
137 static void
138 pci_device_uninit_driver(void* cookie)
139 {
140 	pci_device* device = (pci_device*)cookie;
141 	free(device);
142 }
143 
144 
145 static status_t
146 pci_device_std_ops(int32 op, ...)
147 {
148 	switch (op) {
149 		case B_MODULE_INIT:
150 		case B_MODULE_UNINIT:
151 			return B_OK;
152 	}
153 
154 	return B_BAD_VALUE;
155 }
156 
157 
158 pci_device_module_info gPCIDeviceModule = {
159 	{
160 		{
161 			PCI_DEVICE_MODULE_NAME,
162 			0,
163 			pci_device_std_ops
164 		},
165 
166 		NULL,		// supports device
167 		NULL,		// register device (our parent registered us)
168 		pci_device_init_driver,
169 		pci_device_uninit_driver,
170 		NULL,		// register child devices
171 		NULL,		// rescan devices
172 		NULL,		// device removed
173 	},
174 
175 	pci_device_read_io_8,
176 	pci_device_write_io_8,
177 	pci_device_read_io_16,
178 	pci_device_write_io_16,
179 	pci_device_read_io_32,
180 	pci_device_write_io_32,
181 
182 	pci_device_ram_address,
183 
184 	pci_device_read_pci_config,
185 	pci_device_write_pci_config,
186 	pci_device_find_capability,
187 	pci_device_get_pci_info
188 };
189