xref: /haiku/src/add-ons/kernel/bus_managers/pci/pci_device.cpp (revision 58481f0f6ef1a61ba07283f012cafbc2ed874ead)
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 	PCIDev *dev = gPCI->FindDevice(domain, bus, deviceNumber, function);
123 	if (dev == NULL) {
124 		panic("device not found!\n");
125 		return ENODEV;
126 	}
127 
128 	pci_device* device = (pci_device*)malloc(sizeof(*device));
129 	if (device == NULL)
130 		return B_NO_MEMORY;
131 
132 	device->device = dev;
133 	device->node = node;
134 
135 	*_cookie = device;
136 	return B_OK;
137 }
138 
139 
140 static void
141 pci_device_uninit_driver(void* cookie)
142 {
143 	pci_device* device = (pci_device*)cookie;
144 	free(device);
145 }
146 
147 
148 static status_t
149 pci_device_std_ops(int32 op, ...)
150 {
151 	switch (op) {
152 		case B_MODULE_INIT:
153 		case B_MODULE_UNINIT:
154 			return B_OK;
155 	}
156 
157 	return B_BAD_VALUE;
158 }
159 
160 
161 pci_device_module_info gPCIDeviceModule = {
162 	{
163 		{
164 			PCI_DEVICE_MODULE_NAME,
165 			0,
166 			pci_device_std_ops
167 		},
168 
169 		NULL,		// supports device
170 		NULL,		// register device (our parent registered us)
171 		pci_device_init_driver,
172 		pci_device_uninit_driver,
173 		NULL,		// register child devices
174 		NULL,		// rescan devices
175 		NULL,		// device removed
176 	},
177 
178 	pci_device_read_io_8,
179 	pci_device_write_io_8,
180 	pci_device_read_io_16,
181 	pci_device_write_io_16,
182 	pci_device_read_io_32,
183 	pci_device_write_io_32,
184 
185 	pci_device_ram_address,
186 
187 	pci_device_read_pci_config,
188 	pci_device_write_pci_config,
189 	pci_device_find_capability,
190 	pci_device_get_pci_info
191 };
192