/** * Open PCI configuration space * * @v pci PCI device * @v flags Access mode flags * @v where Address within configuration space * @ret fd File handle, or negative error */ static int linux_pci_open ( struct pci_device *pci, int flags, unsigned long where ) { char filename[ 22 /* "/proc/bus/pci/xx/xx.x" + NUL */ ]; int fd; int rc; /* Construct filename */ snprintf ( filename, sizeof ( filename ), "/proc/bus/pci/%02x/%02x.%x", PCI_BUS ( pci->busdevfn ), PCI_SLOT ( pci->busdevfn ), PCI_FUNC ( pci->busdevfn ) ); /* Open file */ fd = linux_open ( filename, flags ); if ( fd < 0 ) { DBGC ( pci, "PCI could not open %s: %s\n", filename, linux_strerror ( linux_errno ) ); rc = -ELINUX ( linux_errno ); goto err_open; } /* Seek to location */ if ( linux_lseek ( fd, where, SEEK_SET ) < 0 ) { DBGC ( pci, "PCI could not seek to %s offset %#02lx: %s\n", filename, where, linux_strerror ( linux_errno ) ); rc = -ELINUX ( linux_errno ); goto err_seek; } return fd; err_seek: linux_close ( fd ); err_open: return rc; }
static int linux_probe(struct bladerf_devinfo_list *info_list) { int status = 0; struct dirent **matches; int num_matches, i; struct bladerf *dev; struct bladerf_devinfo devinfo; num_matches = scandir(BLADERF_DEV_DIR, &matches, device_filter, alphasort); if (num_matches > 0) { for (i = 0; i < num_matches; i++) { status = 0; /* Open this specific instance. */ bladerf_init_devinfo(&devinfo); devinfo.instance = str2instance(matches[i]->d_name); status = linux_open(&dev, &devinfo); if (status < 0) { log_error("Failed to open instance=%d\n", devinfo.instance); } else { /* Since this device was opened by instance, it will have * had it's device info (dev->ident) filled out already */ bladerf_devinfo_list_add(info_list, &dev->ident); linux_close(dev); } } } free_dirents(matches, num_matches); return (!status && num_matches > 0) ? status : BLADERF_ERR_NODEV; }
void unmap(char *progname) { char buf[1024], *p; char rbuf[2048]; int cc, fd; fd = linux_open("/proc/self/maps", 0, 0); p = &buf[0]; while (0 < (cc = linux_read(fd, rbuf, sizeof(rbuf)))) { int i; for (i = 0; i < cc; ++i) { int c = rbuf[i]; if ('\n' != c) *p++ = c; else { *p = '\0'; /* When a line from /proc/self/maps shows up as having been * mapped in from this running program, ld.so or libc, unmap it. * This will keep the exec'd program's address space a lot * cleaner. But even a 32-bit address space can hold 2 copies * of glibc without ill effects, so you don't really have to * munmap() anything other than the program calling ul_exec() */ if (strstr(buf, progname) || strstr(buf, "libdl") || strstr(buf, "/usr/lib/ld-") || strstr(buf, "/lib64/ld-") || strstr(buf, "libc")) { char *u; char *first, *second; unsigned long low, high; u = strchr(buf, ' '); *u = '\0'; first = buf; second = strchr(first, '-'); *second = '\0'; ++second; low = strtoul(first, NULL, 0x10); high = strtoul(second, NULL, 0x10); linux_munmap((void *)low, high-low); } p = &buf[0]; } } } linux_close(fd); }
void print_maps(void) { char rbuf[1024]; int fd, cc; fd = linux_open("/proc/self/maps", 0, 0); while (0 < (cc = linux_read(fd, rbuf, sizeof(rbuf)))) linux_write(1, rbuf, cc); linux_close(fd); }
/** * Enable entropy gathering * * @ret rc Return status code */ static int linux_entropy_enable ( void ) { /* Open entropy source */ entropy_fd = linux_open ( entropy_filename, O_RDONLY ); if ( entropy_fd < 0 ) { DBGC ( &entropy_fd, "ENTROPY could not open %s: %s\n", entropy_filename, linux_strerror ( linux_errno ) ); return entropy_fd; } return 0; }
AUDIO_OUT * audio_open (int channels, int samplerate) { #if defined (__linux__) return linux_open (channels, samplerate) ; #elif (defined (__MACH__) && defined (__APPLE__)) return macosx_open (channels, samplerate) ; #elif (defined (sun) && defined (unix)) return solaris_open (channels, samplerate) ; #elif (defined (_WIN32) || defined (WIN32)) return win32_open (channels, samplerate) ; #else #warning "*** Playing sound not yet supported on this platform." #warning "*** Please feel free to submit a patch." printf ("Error : Playing sound not yet supported on this platform.\n") ; return NULL ; #endif return NULL ; } /* audio_open */
/* For a floppy drive, opening it and creating it are the same thing, * unless we wanted to mess around with mknod() here */ dsk_err_t linux_creat(DSK_DRIVER *self, const char *filename) { return linux_open(self, filename); }
static int linux_open( struct bladerf **device, struct bladerf_devinfo *info) { char dev_name[32]; struct bladerf_linux *backend; struct bladerf *ret = NULL; int status = BLADERF_ERR_IO; int fd; /* Everything here starts with a driver file descriptor, * so no need to allocate backend and ret until we know we * have said fd */ assert(info->backend == BLADERF_BACKEND_LINUX || info->backend == BLADERF_BACKEND_ANY); /* If an instance is specified, we start with that */ if (info->instance != DEVINFO_INST_ANY) { snprintf(dev_name, sizeof(dev_name), "/dev/bladerf%d", info->instance); fd = open(dev_name, O_RDWR); if (fd >= 0) { backend = calloc(1, sizeof(*backend)); if (backend == NULL) { return BLADERF_ERR_MEM; } ret = calloc(1, sizeof(*ret)); if (ret == NULL) { free(backend); return BLADERF_ERR_MEM; } ret->fpga_version.describe = calloc(1, BLADERF_VERSION_STR_MAX); if (ret->fpga_version.describe == NULL) { free(ret); free(backend); return BLADERF_ERR_MEM; } ret->fw_version.describe = calloc(1, BLADERF_VERSION_STR_MAX); if (ret->fw_version.describe == NULL) { free((void*)ret->fpga_version.describe); free(ret); free(backend); return BLADERF_ERR_MEM; } backend->fd = fd; ret->fn = &bladerf_linux_fn; ret->backend = backend; *device = ret; /* Ensure all dev-info fields are written */ status = linux_populate_devinfo(ret, &ret->ident, info->instance); if (status < 0) { goto linux_open_err; } /* Populate version fields */ status = linux_populate_fpga_version(ret); if (status < 0) { goto linux_open_err; } status = linux_populate_fw_version(ret); linux_open_err: if (status < 0) { free((void*)ret->fw_version.describe); free((void*)ret->fpga_version.describe); free(ret); free(backend); } } else { log_error("Failed to open %s: %s\n", dev_name, strerror(errno)); } } else { /* Otherwise, we user our probe routine to get a device info list, * and then search it */ struct bladerf_devinfo_list list; size_t i; status = bladerf_devinfo_list_init(&list); if (status < 0) { log_error("Failed to initialized devinfo list!\n"); } else { status = linux_probe(&list); if (status < 0) { if (status == BLADERF_ERR_NODEV) { log_debug("No devices available on the Linux driver backend.\n"); } else { log_error("Probe failed: %s\n", bladerf_strerror(status)); } } else { for (i = 0; i < list.num_elt && !ret; i++) { if (bladerf_devinfo_matches(&list.elt[i], info)) { status = linux_open(device, &list.elt[i]); if (status) { log_error("Failed to open instance %d - " "trying next\n", list.elt[i].instance); } else { backend =(*device)->backend; } } } free(list.elt); } } } return status; }