/ util / uio_usbdebug / uio_usbdebug.c
uio_usbdebug.c
 1  /* uio_usbdebug - Run coreboot's usbdebug driver in userspace */
 2  /* SPDX-License-Identifier: GPL-2.0-only */
 3  
 4  #include <stdio.h>
 5  #include <sys/stat.h>
 6  #include <fcntl.h>
 7  #include <unistd.h>
 8  #include <sys/mman.h>
 9  
10  #include <pci/pci.h>
11  
12  /* coreboot's arch/io.h conflicts with libc's sys/io.h, so declare this here: */
13  int ioperm(unsigned long from, unsigned long num, int turn_on);
14  
15  #include <arch/io.h>
16  #include <console/usb.h>
17  
18  void *ehci_bar;
19  struct pci_access *pci_access;
20  
21  int main(int argc, char *argv[])
22  {
23  	if (argc != 2) {
24  		fprintf(stderr, "Usage: %s <uio-dev>\n", argv[0]);
25  		return 1;
26  	}
27  	const int fd = open(argv[1], O_RDWR);
28  	if (fd < 0) {
29  		perror("Failed to open uio device");
30  		return 2;
31  	}
32  	ehci_bar =
33  		mmap(NULL, 1 << 8, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
34  	if (MAP_FAILED == ehci_bar) {
35  		perror("Failed to map ehci bar");
36  		close(fd);
37  		return 3;
38  	}
39  
40  	ioperm(0x80, 1, 1);
41  
42  	pci_access = pci_alloc();
43  	pci_init(pci_access);
44  
45  	usbdebug_init();
46  
47  	pci_cleanup(pci_access);
48  	munmap(ehci_bar, 1 << 8);
49  	close(fd);
50  	return 0;
51  }