static int pci_connect( device_t *igb_dev ) { struct pci_access *pacc; struct pci_dev *dev; int err; char devpath[IGB_BIND_NAMESZ]; memset( igb_dev, 0, sizeof(device_t)); pacc = pci_alloc(); pci_init(pacc); pci_scan_bus(pacc); for (dev=pacc->devices; dev; dev=dev->next) { pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS); igb_dev->pci_vendor_id = dev->vendor_id; igb_dev->pci_device_id = dev->device_id; igb_dev->domain = dev->domain; igb_dev->bus = dev->bus; igb_dev->dev = dev->dev; igb_dev->func = dev->func; snprintf (devpath, IGB_BIND_NAMESZ, "%04x:%02x:%02x.%d", dev->domain, dev->bus, dev->dev, dev->func ); err = igb_probe( igb_dev ); if (err) { continue; } printf ("attaching to %s\n", devpath); err = igb_attach( devpath, igb_dev ); if (err) { printf ("attach failed! (%s)\n", strerror(errno)); continue; } goto out; } pci_cleanup(pacc); return ENXIO; out: pci_cleanup(pacc); return 0; }
void TunersDB::initVendors() { Tuners *tuners; struct Tuner tuner; struct pci_access *pacc; char devbuf[128]; pacc = pci_alloc(); for (int i = 0; card_tuners[i].tuner_name != NULL; i++) { QString vendor = (card_tuners[i].vendor_id == 0xffff ? i18n("Other") : pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_VENDOR, card_tuners[i].vendor_id, 0)); tuner.tuner_id = card_tuners[i].tuner_id; tuner.tuner_name = card_tuners[i].tuner_name; if (!m_vendors.contains(vendor)) { tuners = new Tuners(); m_vendors[vendor] = tuners; } else { tuners = m_vendors[vendor]; } tuners->push_back(tuner); } pci_cleanup(pacc); }
int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <uio-dev>\n", argv[0]); return 1; } const int fd = open(argv[1], O_RDWR); if (fd < 0) { perror("Failed to open uio device"); return 2; } ehci_bar = mmap(NULL, 1 << 8, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (MAP_FAILED == ehci_bar) { perror("Failed to map ehci bar"); close(fd); return 3; } ioperm(0x80, 1, 1); pci_access = pci_alloc(); pci_init(pci_access); usbdebug_init(); pci_cleanup(pci_access); munmap(ehci_bar, 1 << 8); close(fd); return 0; }
static int nicintel_shutdown(void *data) { physunmap(nicintel_control_bar, NICINTEL_CONTROL_MEMMAP_SIZE); physunmap(nicintel_bar, NICINTEL_MEMMAP_SIZE); pci_cleanup(pacc); return 0; }
/* --- implementations --- */ int detect_pci22(void){ unsigned int c; char counter=0; pacc = pci_alloc(); /* Get the pci_access structure */ pci_init(pacc); /* Initialize the PCI library */ pci_scan_bus(pacc); /* We want to get the list of devices */ /* Iterate over all PCI devices */ for(pci22_dev=pacc->devices; pci22_dev; pci22_dev=pci22_dev->next){ pci_fill_info(pci22_dev, PCI_FILL_IDENT | PCI_FILL_BASES); // Detect the specified device if( (pci22_dev->vendor_id == PCI22_PCI_VENDOR_ID) && (pci22_dev->device_id == PCI22_PCI_DEVICE_ID) ){ break; } } if (pci22_dev==NULL){ printf("\n\nERROR: PCI22 card not detected\n\n\n"); exit(0); } printf("\n\nPCI22 card detected on %02x:%02x.%d\nVendorID=%04x DeviceID=%04x irq=%d\n", pci22_dev->bus,pci22_dev->dev, pci22_dev->func,pci22_dev->vendor_id, pci22_dev->device_id, pci22_dev->irq); printf("\n\n--- Baseaddresses ---\n"); printf("BAR1 %x\n",pci22_dev->base_addr[0]); printf("BAR2 %x\n",pci22_dev->base_addr[1]); printf("BAR3 %x\n",pci22_dev->base_addr[2]); printf("BAR4 %x\n",pci22_dev->base_addr[3]); printf("BAR5 %x\n",pci22_dev->base_addr[4]); printf("BAR6 %x\n",pci22_dev->base_addr[6]); printf("\n"); //printPCIDevice(pci22_dev); /* The baseaddress of the COM20022 is at BAR2 */ //g_pci22dipswitch = pci22_dev->base_addr[1]; //Nonworking g_pci22base = pci22_dev->base_addr[2]; /* Close everything */ pci_cleanup(pacc); return 0; }
void CardsDB::addCards(struct card_info *card_infos, ChipSet chipset) { Cards *cards; struct Card card; struct pci_access *pacc; char devbuf[128]; pacc = pci_alloc(); for (int i = 0; card_infos[i].card_name != NULL; i++) { QString vendor = (card_infos[i].vendor_id == 0xffff ? i18n("Other") : pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_VENDOR, card_infos[i].vendor_id, 0)); card.card_id = card_infos[i].card_id; card.card_name = card_infos[i].card_name; card.chipset = chipset; if (!m_vendors.contains(vendor)) { cards = new Cards(); m_vendors[vendor] = cards; } else { cards = m_vendors[vendor]; } cards->push_back(card); } pci_cleanup(pacc); }
int main() { struct pci_access *pci; if(create_pci(PCI_ACCESS_AUTO, &pci)) { MSG("Is this an Intel platform?"); return 1; } int fatal = 0; if(try_ich(pci, REG_ICH0_GPIOBASE, REG_ICH0_GC, "ICH0..ICH5", &fatal) && fatal) { return 1; } else if(try_ich(pci, REG_ICH6_GPIOBASE, REG_ICH6_GC, "ICH6..ICH9 or Series 5..9 PCH", &fatal) && fatal) { return 1; } else { pci_cleanup(pci); // Letting Linux discover P2SB (and reassign its BAR) hangs the system, // so we need to enumerate the device bypassing it. if(create_pci(PCI_ACCESS_I386_TYPE1, &pci)) { return 1; } if(try_pch(pci)) { return 1; } } printf("[+] Done\n"); return 0; }
int ogp_spi_shutdown(void) { physunmap(ogp_spibar, 4096); pci_cleanup(pacc); release_io_perms(); return 0; }
int iface_desc(const char *iface, char *desc, int size) { struct ifreq ifr; int fd, err, len, device, vendor; struct ethtool_drvinfo drvinfo; char buf[512], path[PATH_MAX]; struct pci_access *pacc; memset(&ifr, 0, sizeof(ifr)); strcpy(ifr.ifr_name, iface); fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { perror("Cannot get control socket"); return 1; } drvinfo.cmd = ETHTOOL_GDRVINFO; ifr.ifr_data = (caddr_t)&drvinfo; err = ioctl(fd, SIOCETHTOOL, &ifr); if (err < 0) { perror("Cannot get driver information"); printf("%d\n", errno); return 2; } close(fd); snprintf(path, PATH_MAX-1, "/sys/bus/pci/devices/%s/vendor", drvinfo.bus_info); fd = open(path, O_RDONLY); if (fd < 0) { perror("Cannot open the vendor file"); return 3; } len = read(fd, buf, sizeof(buf)); buf[len-1] = '\0'; close(fd); sscanf(buf,"%X", &vendor); snprintf(path, PATH_MAX, "/sys/bus/pci/devices/%s/device", drvinfo.bus_info); fd = open(path, O_RDONLY); if (fd < 0) { perror("Cannot open the device file"); return 3; } len = read(fd, buf, sizeof(buf)); buf[len-1] = '\0'; close(fd); sscanf(buf,"%X", &device); pacc = pci_alloc(); pci_init(pacc); pci_lookup_name(pacc, desc, size, PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, vendor, device); pci_cleanup(pacc); return(0); }
static void amd_fam14h_unregister(void) { int num; for (num = 0; num < AMD_FAM14H_STATE_NUM - 1; num++) { free(previous_count[num]); free(current_count[num]); } pci_cleanup(pci_acc); }
int gfxnvidia_shutdown(void) { /* Flash interface access is disabled (and screen enabled) automatically * by PCI restore. */ pci_cleanup(pacc); release_io_perms(); return 0; }
static int gfxnvidia_shutdown(void *data) { physunmap(nvidia_bar, GFXNVIDIA_MEMMAP_SIZE); /* Flash interface access is disabled (and screen enabled) automatically * by PCI restore. */ pci_cleanup(pacc); return 0; }
static int pcidev_shutdown(void *data) { if (pacc == NULL) { msg_perr("%s: Tried to cleanup an invalid PCI context!\n" "Please report a bug at [email protected]\n", __func__); return 1; } pci_cleanup(pacc); return 0; }
int nicintel_init(void) { uintptr_t addr; /* Needed only for PCI accesses on some platforms. * FIXME: Refactor that into get_mem_perms/rget_io_perms/get_pci_perms? */ if (rget_io_perms()) return 1; /* No need to check for errors, pcidev_init() will not return in case * of errors. * FIXME: BAR2 is not available if the device uses the CardBus function. */ addr = pcidev_init(PCI_BASE_ADDRESS_2, nics_intel); nicintel_bar = physmap("Intel NIC flash", addr, NICINTEL_MEMMAP_SIZE); if (nicintel_bar == ERROR_PTR) goto error_out_unmap; /* FIXME: Using pcidev_dev _will_ cause pretty explosions in the future. */ addr = pcidev_readbar(pcidev_dev, PCI_BASE_ADDRESS_0); /* FIXME: This is not an aligned mapping. Use 4k? */ nicintel_control_bar = physmap("Intel NIC control/status reg", addr, NICINTEL_CONTROL_MEMMAP_SIZE); if (nicintel_control_bar == ERROR_PTR) goto error_out; if (register_shutdown(nicintel_shutdown, NULL)) return 1; /* FIXME: This register is pretty undocumented in all publicly available * documentation from Intel. Let me quote the complete info we have: * "Flash Control Register: The Flash Control register allows the CPU to * enable writes to an external Flash. The Flash Control Register is a * 32-bit field that allows access to an external Flash device." * Ah yes, we also know where it is, but we have absolutely _no_ idea * what we should do with it. Write 0x0001 because we have nothing * better to do with our time. */ pci_rmmio_writew(0x0001, nicintel_control_bar + CSR_FCR); max_rom_decode.parallel = NICINTEL_MEMMAP_SIZE; register_par_programmer(&par_programmer_nicintel, BUS_PARALLEL); return 0; error_out_unmap: physunmap(nicintel_bar, NICINTEL_MEMMAP_SIZE); error_out: pci_cleanup(pacc); return 1; }
int main (int argc, char** argv) { int vendor = (int)strtol(argv[1], NULL, 16); int device = (int)strtol(argv[2], NULL, 16); char buf[512]; pci_access* acc = pci_alloc(); acc->numeric_ids = 0; acc->debugging = 0; acc->writeable = 0; acc->id_file_name = PCI_PATH_IDS; pci_init (acc); printf ("%s\n", pci_lookup_name(acc, buf, 512, PCI_LOOKUP_DEVICE, vendor, device)); pci_cleanup(acc); return 0; }
/* * pci_acc_init * * PCI access helper function depending on libpci * * **pacc : if a valid pci_dev is returned * *pacc must be passed to pci_acc_cleanup to free it * * domain: domain * bus: bus * slot: slot * func: func * vendor: vendor * device: device * Pass -1 for one of the six above to match any * * Returns : * struct pci_dev which can be used with pci_{read,write}_* functions * to access the PCI config space of matching pci devices */ struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain, int bus, int slot, int func, int vendor, int dev) { struct pci_filter filter_nb_link = { domain, bus, slot, func, vendor, dev }; struct pci_dev *device; *pacc = pci_alloc(); if (*pacc == NULL) return NULL; pci_init(*pacc); pci_scan_bus(*pacc); for (device = (*pacc)->devices; device; device = device->next) { if (pci_filter_match(&filter_nb_link, device)) return device; } pci_cleanup(*pacc); return NULL; }
int show_pci(void) { struct pci_access *pacc; struct pci_dev *dev; unsigned int c; pacc = pci_alloc(); /* Get the pci_access structure */ /* Set all options you want -- here we stick with the defaults */ pacc->debugging=1; pci_init(pacc); /* Initialize the PCI library */ pci_scan_bus(pacc); /* We want to get the list of devices */ for(dev=pacc->devices; dev; dev=dev->next) /* Iterate over all devices */ { pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES); /* Fill in header info we need */ c = pci_read_word(dev, PCI_CLASS_DEVICE); /* Read config register directly */ printf("%02x:%02x.%d vendor=%04x device=%04x class=%04x irq=%d base0=%lx\n", dev->bus, dev->dev, dev->func, dev->vendor_id, dev->device_id, c, dev->irq, dev->base_addr[0]); } pci_cleanup(pacc); /* Close everything */ return 0; }
static inline PdaDebugReturnCode PciDevice_invoke_libpci ( PciDevice *device, const char *bus_id ) { DEBUG_PRINTF(PDADEBUG_ENTER, ""); if(device == NULL) { RETURN( ERROR( EINVAL, "Invalid pointer!\n") ); } if(bus_id == NULL) { RETURN( ERROR( EINVAL, "Invalid pointer!\n") ); } if ( (strlen(bus_id) != 12) || (bus_id[4] != ':') || (bus_id[7] != ':') || (bus_id[10] != '.') ) { RETURN( ERROR( EINVAL, "Invalid bus ID string!\n") ); } struct pci_access *pci_access_handler = pci_alloc(); pci_init(pci_access_handler); pci_scan_bus(pci_access_handler); for ( struct pci_dev *pci_device = pci_access_handler->devices; pci_device; pci_device = pci_device->next ) { pci_fill_info(pci_device, PCI_FILL_IDENT | PCI_FILL_IRQ | PCI_FILL_BASES | PCI_FILL_ROM_BASE | PCI_FILL_SIZES | PCI_FILL_CLASS | PCI_FILL_CAPS | PCI_FILL_EXT_CAPS | PCI_FILL_PHYS_SLOT ); char device_bus_id[] = "0000:00:00.0"; snprintf(device_bus_id, 13,"%04x:%02x:%02x.%d", pci_device->domain, pci_device->bus, pci_device->dev, pci_device->func); if(strcmp(device_bus_id, bus_id) == 0) { device->domain_id = pci_device->domain; device->bus_id = pci_device->bus; device->device_id = pci_device->dev; device->function_id = pci_device->func; for(uint32_t i = 0; i < PDA_MAX_PCI_32_BARS; i++) { uint32_t bar = pci_device->base_addr[i]; DEBUG_PRINTF(PDADEBUG_VALUE, "bar%d : 0x%x\n", i, bar); uint8_t config[64]; pci_read_block(pci_device, 0, config, 64); uint64_t addr = PCI_BASE_ADDRESS_0 + (4*i); uint32_t virt_flag = (config[addr+0] ) | (config[addr+1] << 8 ) | (config[addr+2] << 16 ) | (config[addr+3] << 24 ) ; if(bar && !virt_flag) { device->bar_types[i] = PCIBARTYPES_NOT_MAPPED; DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "bar%u is virtual\n", i); continue; } if(bar == 0x0) { if(device->bar_types[i] != PCIBARTYPES_NOT_MAPPED) { DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "Inconsistency detected -> bar%u not classified correctly\n", i); device->bar_types[i] = PCIBARTYPES_NOT_MAPPED; } PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]); continue; } if( (bar & 0x00000001) == 0x1) { if(device->bar_types[i] != PCIBARTYPES_IO) { DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "Inconsistency detected -> bar%u not classified correctly\n", i); device->bar_types[i] = PCIBARTYPES_IO; } PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]); continue; } uint32_t width_bit = bar & 0x00000006; if(width_bit == 0x00000000) { if(device->bar_types[i] != PCIBARTYPES_BAR32) { DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "Inconsistency detected -> bar%u not classified correctly\n", i); device->bar_types[i] = PCIBARTYPES_BAR32; } PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]); continue; } if(width_bit == 0x00000004) { if(device->bar_types[i] != PCIBARTYPES_BAR64) { DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "Inconsistency detected -> bar%u not classified correctly\n", i); device->bar_types[i] = PCIBARTYPES_BAR64; device->bar_types[i + 1] = PCIBARTYPES_NOT_MAPPED; } PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]); continue; } } /** Older versions of libpci leak memory when this function is called. **/ #if PCI_LIB_VERSION >= 0x030301 pci_lookup_name(pci_access_handler, device->description, PDA_STRING_LIMIT, PCI_LOOKUP_DEVICE, pci_device->vendor_id, pci_device->device_id); #else #warning "At least version 3.3.1 of libpci is needed. Otherwise the device description can't be returned." strcpy(device->description, ""); #endif DEBUG_PRINTF(PDADEBUG_VALUE, " (%s) %s\n", device->description, device_bus_id); } } pci_cleanup(pci_access_handler); RETURN(PDA_SUCCESS); }
int main(int argc, char *argv[]) { bool has_intel = 0; bool has_nvidia = 0; bool abi_compat = 0; int ret = 0; int c = 0; while ((c = getopt_long(argc, argv, "lxVh", longopts, 0)) != EOF) switch (c) { case 'l': opt_list = true; break; case 'x': opt_xorg = true; break; case 'V': version(); exit(0); case 'h': usage(); exit(0); default: usage(); exit(0); } pacc = pci_alloc(); /* Get the pci_access structure */ pci_init(pacc); /* Initialize the PCI library */ if (opt_list) { list_all_nvidia_devices(); goto exit; } pci_scan_bus(pacc); /* Scan the bus for devices */ printf("Probing for supported NVIDIA devices...\n"); /* Iterate over all devices */ for (dev=pacc->devices; dev; dev=dev->next) { if (!dev->device_class) { fprintf(stderr, "Error getting device_class\n"); ret = -1; goto exit; } if ((dev->device_class & 0xff00) == 0x0300) { /* Get the name of the device */ name = pci_lookup_name(pacc, namebuf, sizeof(namebuf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id); printf("[%04x:%04x] %s\n", dev->vendor_id, dev->device_id, name); /* Find NVIDIA device */ if (dev->vendor_id == PCI_VENDOR_ID_NVIDIA) { has_nvidia = true; ret = nv_lookup_device_id(dev->device_id); } /* * Find Intel device for simplistic detection * of Optimus hardware configurations */ if (dev->vendor_id == PCI_VENDOR_ID_INTEL) has_intel = true; } /* End of device_class */ } /* End iteration of devices */ /* Check Xorg ABI compatibility */ if (ret > 0) { if (opt_xorg) printf("\nChecking ABI compatibility with Xorg Server...\n"); abi_compat = check_xorg_abi_compat(ret); if (!abi_compat) printf("WARNING: The driver for this device " "does not support the current Xorg version\n"); else if (opt_xorg) printf("ABI compatibility check passed\n"); } /* Check for Optimus hardware */ if (has_intel && has_nvidia) has_optimus(); /* Catch cases where no NVIDIA devices were detected */ if (!has_nvidia) printf("No NVIDIA devices were found.\n"); exit: pci_cleanup(pacc); /* Close everything */ exit(ret); }
int main(int argc, char* argv[]) { int err = EXIT_FAILURE; struct cmd_line cmd_line; setlocale(LC_ALL, ""); if (parse_cmd_line(argc, argv, &cmd_line) < 0) goto arg_check_failed; struct pci_access * pci = pci_alloc(); if (! pci) goto pci_alloc_failed; /* This access bypass the kernel and use a memory mapping * to PCI configuration registers */ pci->method = PCI_ACCESS_I386_TYPE1; struct pci_dev * dev = create_pci_dev(pci, cmd_line.slot); if (! dev) goto create_pci_dev_failed; print_device_name(pci, dev); unsigned long * timestamps = malloc(sizeof(*timestamps) * cmd_line.iteration_count); if (! timestamps) { fprintf(stderr, "Can't allocate timestamp storage (%s)\n", strerror(errno)); goto malloc_failed; } struct timestamp t; read_timestamp_counter(&t); perform_reads(dev, timestamps, cmd_line.iteration_count, cmd_line.wait_time_us); unsigned long test_duration_cycles = cycle_since_timestamp(&t); double cpu_mhz = get_cpu_mhz(); if (cpu_mhz < 0) goto get_cpu_mhz_failed; print_results(cpu_mhz, timestamps, cmd_line.iteration_count, test_duration_cycles, cmd_line.limit_ns); err = EXIT_SUCCESS; get_cpu_mhz_failed: free(timestamps); malloc_failed: pci_free_dev(dev); create_pci_dev_failed: pci_cleanup(pci); pci_alloc_failed: arg_check_failed: return err; }
int main (int argc, char *argv[]) { off_t address = 0; size_t length = 0; int fd; int state; int retval = 0; struct pci_access *pacc; struct pci_dev *dev; DBusError err; setup_logger (); udi = getenv ("UDI"); HAL_DEBUG (("udi=%s", udi)); if (udi == NULL) { HAL_ERROR (("No device specified")); return -2; } dbus_error_init (&err); if ((halctx = libhal_ctx_init_direct (&err)) == NULL) { HAL_ERROR (("Cannot connect to hald")); retval = -3; goto out; } if (!libhal_device_addon_is_ready (halctx, udi, &err)) { retval = -4; goto out; } conn = libhal_ctx_get_dbus_connection (halctx); dbus_connection_setup_with_g_main (conn, NULL); dbus_connection_set_exit_on_disconnect (conn, 0); dbus_connection_add_filter (conn, filter_function, NULL, NULL); /* Search for the graphics card. */ /* Default values: */ /* address = 0x90300000; */ /* length = 0x20000; */ pacc = pci_alloc(); pci_init(pacc); pci_scan_bus(pacc); for(dev=pacc->devices; dev; dev=dev->next) { /* Iterate over all devices */ pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES); if ((dev->vendor_id == 0x1002) && (dev->device_id == 0x71c5)) { // ATI X1600 address = dev->base_addr[2]; length = dev->size[2]; } } pci_cleanup(pacc); HAL_DEBUG (("addr 0x%x len=%d", address, length)); if (!address) { HAL_DEBUG (("Failed to detect ATI X1600, aborting...")); retval = 1; goto out; } fd = open ("/dev/mem", O_RDWR); if (fd < 0) { HAL_DEBUG (("cannot open /dev/mem")); retval = 1; goto out; } memory = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, address); if (memory == MAP_FAILED) { HAL_ERROR (("mmap failed")); retval = 2; goto out; } /* Is it really necessary ? */ OUTREG(0x4dc, 0x00000005); state = INREG(0x7ae4); OUTREG(0x7ae4, state); /* Allow access to porta 0x300 through 0x304 */ if (ioperm (0x300, 5, 1) < 0) { HAL_ERROR (("ioperm failed (you should be root).")); exit(1); } /* this works because we hardcoded the udi's in the <spawn> in the fdi files */ if (!libhal_device_claim_interface (halctx, "/org/freedesktop/Hal/devices/macbook_pro_lcd_panel", "org.freedesktop.Hal.Device.LaptopPanel", " <method name=\"SetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n" " <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n" " </method>\n" " <method name=\"GetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n" " </method>\n", &err)) { HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LaptopPanel'")); retval = -4; goto out; } if (!libhal_device_claim_interface (halctx, "/org/freedesktop/Hal/devices/macbook_pro_light_sensor", "org.freedesktop.Hal.Device.LightSensor", " <method name=\"GetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"out\" type=\"ai\"/>\n" " </method>\n", &err)) { HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LightSensor'")); retval = -4; goto out; } if (!libhal_device_claim_interface (halctx, "/org/freedesktop/Hal/devices/macbook_pro_keyboard_backlight", "org.freedesktop.Hal.Device.KeyboardBacklight", " <method name=\"GetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n" " </method>\n" " <method name=\"SetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n" " </method>\n", &err)) { HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.KeyboardBacklight'")); retval = -4; goto out; } main_loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (main_loop); return 0; out: HAL_DEBUG (("An error occured, exiting cleanly")); LIBHAL_FREE_DBUS_ERROR (&err); if (halctx != NULL) { libhal_ctx_shutdown (halctx, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_ctx_free (halctx); } return retval; }
pci::~pci() { pci_cleanup(_pacc); }
sbone_err_t sbone_dev_open_pcie (sbone_dev_t* dev, int dom, int bus, int _dev, int fun) { static const size_t nbar = sizeof(dev->bar_addrs) / sizeof(dev->bar_addrs[0]); int mem_fd; size_t i; struct pci_access* pci_access; struct pci_dev* pci_dev; int err = -1; /* zero device */ for (i = 0; i < nbar; ++i) dev->bar_sizes[i] = 0; dev->nid = -1; /* find the pci device */ pci_access = pci_alloc(); if (pci_access == NULL) { PERROR(); goto on_error_0; } pci_init(pci_access); pci_scan_bus(pci_access); pci_dev = pci_get_dev(pci_access, dom, bus, _dev, fun); if (pci_dev == NULL) { PERROR(); goto on_error_1; } pci_fill_info(pci_dev, PCI_FILL_IDENT | PCI_FILL_BASES); /* map bars */ mem_fd = open("/dev/mem", O_RDWR | O_SYNC); if (mem_fd == -1) { PERROR(); goto on_error_2; } for (i = 0; i < nbar; ++i) { #if 1 /* FIXME: pci_dev->size[i] not detected by pci_fill_info ... */ if (pci_dev->base_addr[i] != 0) pci_dev->size[i] = get_bar_size(pci_access, pci_dev, i); #endif /* FIXME */ dev->bar_sizes[i] = pci_dev->size[i]; if (dev->bar_sizes[i] == 0) continue ; dev->bar_addrs[i] = (uintptr_t)mmap ( NULL, pci_dev->size[i], PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, mem_fd, pci_dev->base_addr[i] ); if (dev->bar_addrs[i] == (uintptr_t)MAP_FAILED) { size_t j; for (j = 0; j < i; ++j) munmap((void*)dev->bar_addrs[i], dev->bar_sizes[i]); PERROR(); goto on_error_3; } } if ((dev->fd = open("/dev/sbone", O_RDWR)) == -1) { PERROR(); goto on_error_3; } /* success */ err = 0; on_error_3: close(mem_fd); on_error_2: pci_free_dev(pci_dev); on_error_1: pci_cleanup(pci_access); on_error_0: return err; }
nsresult nsSystemInfo::Init() { struct pci_access *pacc; struct pci_dev *p; pciaddr_t ram = 0; char buf[128]; int i, found, v, n; Display *dpy; char *display = NULL; Window root; XVisualInfo *info, templ; XWindowAttributes wts; XPixmapFormatValues *pf; XSetWindowAttributes attr; Window win; mWidth = 0; mHeight = 0; mDepth = 0; pacc = pci_alloc(); pci_init(pacc); pci_scan_bus(pacc); for (p = pacc->devices; p; p=p->next) { pci_fill_info(p, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_BASES | PCI_FILL_SIZES); if (p->device_class == PCI_CLASS_DISPLAY_VGA) { pci_lookup_name(pacc, buf, sizeof(buf), PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, p->vendor_id, p->device_id); mDeviceName.AssignLiteral(buf); sprintf(buf, "0x%04X", p->vendor_id); mVendorID.AssignLiteral(buf); sprintf(buf, "0x%04X", p->device_id); mDeviceID.AssignLiteral(buf); for (i=0; i<6; i++) { pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0; if (len > ram) ram = len; } vram = ram / 1024 / 1024; } else { Log("Grafx Bot: No PCI VGA device found"); } } pci_cleanup(pacc); if (NULL != (display = getenv("DISPLAY"))) { if (display[0] != ':') { display = strchr(display, ':'); if (NULL == display) { Log("Grafx Bot: unable to find display"); return NS_OK; } } if (NULL == (dpy = XOpenDisplay(display))) { Log("Grafx Bot: unable to find X display"); return NS_OK; } root = DefaultRootWindow(dpy); XGetWindowAttributes(dpy, root, &wts); mWidth = wts.width; mHeight = wts.height; templ.screen = XDefaultScreen(dpy); info = XGetVisualInfo(dpy, VisualScreenMask, &templ, &found); v = -1; for (i = 0; v == -1 && i < found; i++) { if (info[i].depth >= 15) v = i; } for (i = 0; v == -1 && i < found; i++) { if (info[i].depth == 8) v = i; } if (-1 == v) { Log("Grafx Bot: can't find visual"); return NS_OK; } pf = XListPixmapFormats(dpy, &n); for (i = 0; i < n; i++) { if (pf[i].depth == info[v].depth) { mDepth = pf[i].depth; } } if (gGLXWrap.OpenLibrary("libGL.so.1") && gGLXWrap.Init()) { attr.background_pixel = 0; attr.border_pixel = 0; attr.colormap = XCreateColormap(dpy, root, info[v].visual, AllocNone); attr.event_mask = StructureNotifyMask | ExposureMask; win = XCreateWindow(dpy, root, 0, 0, 100, 100, 0, info[v].depth, InputOutput, info[v].visual, CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &attr); GLXContext ctx = gGLXWrap.fCreateContext(dpy, info, NULL, true); if (ctx) { if (gGLXWrap.fMakeCurrent(dpy, win, ctx)) { mDriverVersion.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VERSION)); mDriver.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_RENDERER)); mDriver.AppendLiteral(" ("); mDriver.AppendLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VENDOR)); mDriver.AppendLiteral(")"); } else { Log("Grafx Bot: unable to make current"); } gGLXWrap.fDestroyContext(dpy, ctx); } else { Log("Grafx Bot: unable to create context"); } XDestroyWindow(dpy, win); } else { Log("Grafx Bot: can't init libGL.so.1"); } } return NS_OK; }
static int hwloc_look_pci(struct hwloc_backend *backend) { struct hwloc_topology *topology = backend->topology; struct hwloc_obj *first_obj = NULL, *last_obj = NULL; #ifdef HWLOC_HAVE_LIBPCIACCESS int ret; struct pci_device_iterator *iter; struct pci_device *pcidev; #else /* HWLOC_HAVE_PCIUTILS */ struct pci_access *pciaccess; struct pci_dev *pcidev; #endif if (!(hwloc_topology_get_flags(topology) & (HWLOC_TOPOLOGY_FLAG_IO_DEVICES|HWLOC_TOPOLOGY_FLAG_WHOLE_IO))) return 0; if (hwloc_get_next_pcidev(topology, NULL)) { hwloc_debug("%s", "PCI objects already added, ignoring pci backend.\n"); return 0; } if (!hwloc_topology_is_thissystem(topology)) { hwloc_debug("%s", "\nno PCI detection (not thissystem)\n"); return 0; } hwloc_debug("%s", "\nScanning PCI buses...\n"); /* initialize PCI scanning */ #ifdef HWLOC_HAVE_LIBPCIACCESS ret = pci_system_init(); if (ret) { hwloc_debug("%s", "Can not initialize libpciaccess\n"); return -1; } iter = pci_slot_match_iterator_create(NULL); #else /* HWLOC_HAVE_PCIUTILS */ pciaccess = pci_alloc(); pciaccess->error = hwloc_pci_error; pciaccess->warning = hwloc_pci_warning; if (setjmp(err_buf)) { pci_cleanup(pciaccess); return -1; } pci_init(pciaccess); pci_scan_bus(pciaccess); #endif /* iterate over devices */ #ifdef HWLOC_HAVE_LIBPCIACCESS for (pcidev = pci_device_next(iter); pcidev; pcidev = pci_device_next(iter)) #else /* HWLOC_HAVE_PCIUTILS */ for (pcidev = pciaccess->devices; pcidev; pcidev = pcidev->next) #endif { const char *vendorname, *devicename, *fullname; unsigned char config_space_cache[CONFIG_SPACE_CACHESIZE]; struct hwloc_obj *obj; unsigned os_index; unsigned domain; unsigned device_class; unsigned short tmp16; char name[128]; unsigned offset; #ifdef HWLOC_HAVE_PCI_FIND_CAP struct pci_cap *cap; #endif /* initialize the config space in case we fail to read it (missing permissions, etc). */ memset(config_space_cache, 0xff, CONFIG_SPACE_CACHESIZE); #ifdef HWLOC_HAVE_LIBPCIACCESS pci_device_probe(pcidev); pci_device_cfg_read(pcidev, config_space_cache, 0, CONFIG_SPACE_CACHESIZE, NULL); #else /* HWLOC_HAVE_PCIUTILS */ pci_read_block(pcidev, 0, config_space_cache, CONFIG_SPACE_CACHESIZE); /* doesn't even tell how much it actually reads */ #endif /* try to read the domain */ #if (defined HWLOC_HAVE_LIBPCIACCESS) || (defined HWLOC_HAVE_PCIDEV_DOMAIN) domain = pcidev->domain; #else domain = 0; /* default domain number */ #endif /* try to read the device_class */ #ifdef HWLOC_HAVE_LIBPCIACCESS device_class = pcidev->device_class >> 8; #else /* HWLOC_HAVE_PCIUTILS */ #ifdef HWLOC_HAVE_PCIDEV_DEVICE_CLASS device_class = pcidev->device_class; #else device_class = config_space_cache[PCI_CLASS_DEVICE] | (config_space_cache[PCI_CLASS_DEVICE+1] << 8); #endif #endif /* might be useful for debugging (note that domain might be truncated) */ os_index = (domain << 20) + (pcidev->bus << 12) + (pcidev->dev << 4) + pcidev->func; obj = hwloc_alloc_setup_object(HWLOC_OBJ_PCI_DEVICE, os_index); obj->attr->pcidev.domain = domain; obj->attr->pcidev.bus = pcidev->bus; obj->attr->pcidev.dev = pcidev->dev; obj->attr->pcidev.func = pcidev->func; obj->attr->pcidev.vendor_id = pcidev->vendor_id; obj->attr->pcidev.device_id = pcidev->device_id; obj->attr->pcidev.class_id = device_class; obj->attr->pcidev.revision = config_space_cache[PCI_REVISION_ID]; obj->attr->pcidev.linkspeed = 0; /* unknown */ #ifdef HWLOC_HAVE_PCI_FIND_CAP cap = pci_find_cap(pcidev, PCI_CAP_ID_EXP, PCI_CAP_NORMAL); offset = cap ? cap->addr : 0; #else offset = hwloc_pci_find_cap(config_space_cache, PCI_CAP_ID_EXP); #endif /* HWLOC_HAVE_PCI_FIND_CAP */ if (0xffff == pcidev->vendor_id && 0xffff == pcidev->device_id) { /* SR-IOV puts ffff:ffff in Virtual Function config space. * The actual VF device ID is stored at a special (dynamic) location in the Physical Function config space. * VF and PF have the same vendor ID. * * libpciaccess just returns ffff:ffff, needs to be fixed. * linuxpci is OK because sysfs files are already fixed the kernel. * pciutils is OK when it uses those Linux sysfs files. * * Reading these files is an easy way to work around the libpciaccess issue on Linux, * but we have no way to know if this is caused by SR-IOV or not. * * TODO: * If PF has CAP_ID_PCIX or CAP_ID_EXP (offset>0), * look for extended capability PCI_EXT_CAP_ID_SRIOV (need extended config space (more than 256 bytes)), * then read the VF device ID after it (PCI_IOV_DID bytes later). * Needs access to extended config space (needs root on Linux). * TODO: * Add string info attributes in VF and PF objects? */ #ifdef HWLOC_LINUX_SYS /* Workaround for Linux (the kernel returns the VF device/vendor IDs). */ char path[64]; char value[16]; FILE *file; snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/vendor", domain, pcidev->bus, pcidev->dev, pcidev->func); file = fopen(path, "r"); if (file) { fread(value, sizeof(value), 1, file); fclose(file); obj->attr->pcidev.vendor_id = strtoul(value, NULL, 16); } snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/device", domain, pcidev->bus, pcidev->dev, pcidev->func); file = fopen(path, "r"); if (file) { fread(value, sizeof(value), 1, file); fclose(file); obj->attr->pcidev.device_id = strtoul(value, NULL, 16); } #endif } if (offset > 0 && offset + 20 /* size of PCI express block up to link status */ <= CONFIG_SPACE_CACHESIZE) hwloc_pci_find_linkspeed(config_space_cache, offset, &obj->attr->pcidev.linkspeed); hwloc_pci_prepare_bridge(obj, config_space_cache); if (obj->type == HWLOC_OBJ_PCI_DEVICE) { memcpy(&tmp16, &config_space_cache[PCI_SUBSYSTEM_VENDOR_ID], sizeof(tmp16)); obj->attr->pcidev.subvendor_id = tmp16; memcpy(&tmp16, &config_space_cache[PCI_SUBSYSTEM_ID], sizeof(tmp16)); obj->attr->pcidev.subdevice_id = tmp16; } else { /* TODO: * bridge must lookup PCI_CAP_ID_SSVID and then look at offset+PCI_SSVID_VENDOR/DEVICE_ID * cardbus must look at PCI_CB_SUBSYSTEM_VENDOR_ID and PCI_CB_SUBSYSTEM_ID */ } /* starting from pciutils 2.2, pci_lookup_name() takes a variable number * of arguments, and supports the PCI_LOOKUP_NO_NUMBERS flag. */ /* get the vendor name */ #ifdef HWLOC_HAVE_LIBPCIACCESS vendorname = pci_device_get_vendor_name(pcidev); #else /* HWLOC_HAVE_PCIUTILS */ vendorname = pci_lookup_name(pciaccess, name, sizeof(name), #if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS PCI_LOOKUP_VENDOR|PCI_LOOKUP_NO_NUMBERS, pcidev->vendor_id #else PCI_LOOKUP_VENDOR, pcidev->vendor_id, 0, 0, 0 #endif ); #endif /* HWLOC_HAVE_PCIUTILS */ if (vendorname && *vendorname) hwloc_obj_add_info(obj, "PCIVendor", vendorname); /* get the device name */ #ifdef HWLOC_HAVE_LIBPCIACCESS devicename = pci_device_get_device_name(pcidev); #else /* HWLOC_HAVE_PCIUTILS */ devicename = pci_lookup_name(pciaccess, name, sizeof(name), #if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS PCI_LOOKUP_DEVICE|PCI_LOOKUP_NO_NUMBERS, pcidev->vendor_id, pcidev->device_id #else PCI_LOOKUP_DEVICE, pcidev->vendor_id, pcidev->device_id, 0, 0 #endif ); #endif /* HWLOC_HAVE_PCIUTILS */ if (devicename && *devicename) hwloc_obj_add_info(obj, "PCIDevice", devicename); /* generate or get the fullname */ #ifdef HWLOC_HAVE_LIBPCIACCESS snprintf(name, sizeof(name), "%s%s%s", vendorname ? vendorname : "", vendorname && devicename ? " " : "", devicename ? devicename : ""); fullname = name; if (*name) obj->name = strdup(name); #else /* HWLOC_HAVE_PCIUTILS */ fullname = pci_lookup_name(pciaccess, name, sizeof(name), #if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS PCI_LOOKUP_VENDOR|PCI_LOOKUP_DEVICE|PCI_LOOKUP_NO_NUMBERS, pcidev->vendor_id, pcidev->device_id #else PCI_LOOKUP_VENDOR|PCI_LOOKUP_DEVICE, pcidev->vendor_id, pcidev->device_id, 0, 0 #endif ); if (fullname && *fullname) obj->name = strdup(fullname); #endif /* HWLOC_HAVE_PCIUTILS */ hwloc_debug(" %04x:%02x:%02x.%01x %04x %04x:%04x %s\n", domain, pcidev->bus, pcidev->dev, pcidev->func, device_class, pcidev->vendor_id, pcidev->device_id, fullname && *fullname ? fullname : "??"); /* queue the object for now */ if (first_obj) last_obj->next_sibling = obj; else first_obj = obj; last_obj = obj; } /* finalize device scanning */ #ifdef HWLOC_HAVE_LIBPCIACCESS pci_iterator_destroy(iter); pci_system_cleanup(); #else /* HWLOC_HAVE_PCIUTILS */ pci_cleanup(pciaccess); #endif return hwloc_insert_pci_device_list(backend, first_obj); }