The Pedigree Project  0.1
modules/drivers/common/cdi/pci.cc
1 /*
2  * Copyright (c) 2007-2009 Kevin Wolf
3  *
4  * This program is free software. It comes without any warranty, to
5  * the extent permitted by applicable law. You can redistribute it
6  * and/or modify it under the terms of the Do What The Fuck You Want
7  * To Public License, Version 2, as published by Sam Hocevar. See
8  * http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9  */
10 
11 #include "cdi-osdep.h"
12 #include "cdi/lists.h"
13 #include "cdi/pci.h"
14 #include "pedigree/kernel/compiler.h"
15 #include "pedigree/kernel/machine/Device.h"
16 #include "pedigree/kernel/utilities/Vector.h"
17 #include "pedigree/kernel/utilities/utility.h"
18 
19 extern "C" {
20 
21 static void add_child_device(cdi_list_t list, Device *pDev)
22 {
23  // Add device to list
24  // Let's hope the available information is enough
25  struct cdi_pci_device* dev = (struct cdi_pci_device*) malloc(sizeof(*dev));
26  ByteSet(dev, 0, sizeof(*dev));
27 
28  dev->vendor_id = pDev->getPciVendorId();
29  dev->device_id = pDev->getPciDeviceId();
30  dev->class_id = pDev->getPciClassCode();
31  dev->subclass_id = pDev->getPciSubclassCode();
32  dev->irq = pDev->getInterruptNumber();
33  dev->meta.backdev = reinterpret_cast<void*>(pDev);
34 
35  // Add BARs
36  Vector<Device::Address*> addresses = pDev->addresses();
37  dev->resources = cdi_list_create();
38  for (unsigned int j = 0; j < addresses.size(); j++) {
39  Device::Address* bar = addresses[j];
40 
41  struct cdi_pci_resource* res = (struct cdi_pci_resource*) malloc(sizeof(*res));
42  ByteSet(res, 0, sizeof(*res));
43 
44  res->type = bar->m_IsIoSpace ? CDI_PCI_IOPORTS : CDI_PCI_MEMORY;
45  res->start = bar->m_Address;
46  res->length = bar->m_Size;
47  res->index = j;
48  res->address = 0;
49 
50  cdi_list_push(dev->resources, res);
51  }
52 
53  cdi_list_push(list, dev);
54 }
55 
60 EXPORTED_PUBLIC void cdi_pci_get_all_devices(cdi_list_t list)
61 {
62  auto f = [list] (Device *p) {
63  add_child_device(list, p);
64  return p;
65  };
66 
67  auto c = pedigree_std::make_callable(f);
68  Device::foreach(c, 0);
69 }
70 
74 EXPORTED_PUBLIC void cdi_pci_device_destroy(struct cdi_pci_device* device)
75 {
76  // TODO Liste abbauen
77  free(device);
78 }
79 
83 EXPORTED_PUBLIC void cdi_pci_alloc_ioports(struct cdi_pci_device* device)
84 {
85  struct cdi_pci_resource* res;
86  int i = 0;
87 
88  for (i = 0; (res = (struct cdi_pci_resource*) cdi_list_get(device->resources, i)); i++) {
89  if (res->type == CDI_PCI_IOPORTS) {
90 // request_ports(res->start, res->length);
91  }
92  }
93 }
94 
98 EXPORTED_PUBLIC void cdi_pci_free_ioports(struct cdi_pci_device* device)
99 {
100  struct cdi_pci_resource* res;
101  int i = 0;
102 
103  for (i = 0; (res = (struct cdi_pci_resource*)cdi_list_get(device->resources, i)); i++) {
104  if (res->type == CDI_PCI_IOPORTS) {
105 // release_ports(res->start, res->length);
106  }
107  }
108 }
112 EXPORTED_PUBLIC void cdi_pci_alloc_memory(struct cdi_pci_device* device)
113 {
114  struct cdi_pci_resource* res;
115  int i = 0;
116 
117  for (i = 0; (res = (struct cdi_pci_resource*)cdi_list_get(device->resources, i)); i++) {
118  if (res->type == CDI_PCI_MEMORY) {
119 // TODO res->address = mem_allocate_physical(res->length, res->start, 0);
120  }
121  }
122 }
123 
127 EXPORTED_PUBLIC void cdi_pci_free_memory(struct cdi_pci_device* device)
128 {
129  struct cdi_pci_resource* res;
130  int i = 0;
131 
132  for (i = 0; (res = (struct cdi_pci_resource*)cdi_list_get(device->resources, i)); i++) {
133  if (res->type == CDI_PCI_MEMORY) {
134 // TODO mem_free_physical(res->address, res->length);
135  res->address = 0;
136  }
137  }
138 }
139 
140 }
uintptr_t m_Address
Definition: Device.h:89
uint8_t getPciSubclassCode()
Definition: Device.h:214
size_t m_Size
Definition: Device.h:91
virtual Vector< Address * > & addresses()
Definition: Device.h:256
virtual uintptr_t getInterruptNumber()
Definition: Device.h:262
Definition: Device.h:43
bool m_IsIoSpace
Definition: Device.h:94
uint16_t getPciDeviceId()
Definition: Device.h:224
size_t size() const
Definition: Vector.h:258
static void foreach(Callback callback, Device *root=0)
Definition: Device.cc:94
uint8_t getPciClassCode()
Definition: Device.h:209
uint16_t getPciVendorId()
Definition: Device.h:219