Browse Source

7.968u-pci: add function pci_lspci()

rain 5 years ago
parent
commit
a2c84251cc
5 changed files with 37 additions and 2 deletions
  1. 1 0
      LINES
  2. 4 1
      README.md
  3. 12 0
      include/kernel/pci.h
  4. 2 0
      init/main.c
  5. 18 1
      kernel/pci.c

+ 1 - 0
LINES

@@ -32,4 +32,5 @@
 2021-8-12 7.689: 7689 lines, 55 files, rain
 2021-8-13 7.783: 7783 lines, 55 files, rain
 2021-8-13 7.937: 7937 lines, 55 files, rain
+2021-8-13 7.968: 7968 lines, 55 files, rain
 

+ 4 - 1
README.md

@@ -133,4 +133,7 @@ source software, you can get its source code from <https://gitee.com/pengruiyang
 
 7.937u-keyboard:  
 &nbsp;&nbsp;&nbsp;&nbsp;now we can input characters, by `put_keycode()`.
- 
+
+7.968u-pci:  
+&nbsp;&nbsp;&nbsp;&nbsp;add the function `pci_lspci()` to scan all devices. 
+

+ 12 - 0
include/kernel/pci.h

@@ -32,6 +32,18 @@
 
 
 __uint32_t pci_read_config(__uint8_t, __uint8_t, __uint8_t, __uint8_t);
+__uint32_t pci_lspci(void);
+
+
+struct pci_device {
+	__uint16_t devid;
+	__uint16_t vendor;
+
+	char *devname;
+
+	/* the device configure word (256 bytes) */
+	char *config;
+};
 
 #endif
 

+ 2 - 0
init/main.c

@@ -71,6 +71,8 @@ int init(void) {
 
 	sti();
 
+	printk(white, "total %d devices. \n", pci_lspci());
+
 	printk(white, "System boots from the device %b: ", bootarg->bootdev);
 
 	if (bootarg->bootdev & 0x80) 

+ 18 - 1
kernel/pci.c

@@ -49,4 +49,21 @@ __uint32_t pci_read_config(__uint8_t bus, __uint8_t slot, __uint8_t func, __uint
 	return inl(0xcfc);
 }
 
-
+__uint32_t pci_lspci(void) {
+	__uint8_t bus, slot, func;
+	__uint32_t devs = 0;
+
+	for (bus = 0; bus < 255; bus++) {
+		for (slot = 0; slot < 32; slot++) {
+			for (func = 0; func < 8; func++) {
+				if (pci_read_config(bus, slot, func, 0) != 0xffffffff) {
+					/* exists */
+					printk(yellow, "pci: %b:%b:%b exists: %x. \n", bus, slot, func, pci_read_config(bus, slot, func, 0));
+					devs++;
+				}
+			}
+		}
+	}
+
+	return devs;
+}