error device::open(std::shared_ptr<handle>& handle) { std::unique_lock<std::recursive_mutex> lock(m_mutex); /* get a pointer so that it's not destroyed during the runtime of the function, * the pointer will be released at the end of the function */ std::shared_ptr<context> ctx = get_context(); if(!ctx) return error::NO_CONTEXT; /* do not even try if device is disconnected */ if(!connected()) return error::DISCONNECTED; /* NOTE at the moment handle is state-less which means that we can * safely give the same handle each time open() is called without callers * interfering with each other. If handle eventually get a state, * one will need to create a proxy class to encapsulate the state */ handle = m_handle.lock(); if(has_multiple_open() || !handle) { error err = open_dev(handle); m_handle = handle; return err; } else return error::SUCCESS; }
void connectDevice() { if ( Connection_Status != CONNECTED) // if not connected already { usb_init(); /* initialize the library */ usb_find_busses(); /* find all busses */ usb_find_devices(); /* find all connected devices */ if(!(MyLibusbDeviceHandle = open_dev())) { fprintf(stderr, "open_dev() failed\n"); return; } if(usb_set_configuration(MyLibusbDeviceHandle, 1) < 0) // Sets the Active configuration of the device { fprintf(stderr, "usb_set_configuration() failed\n"); usb_close(MyLibusbDeviceHandle); return; } if(usb_claim_interface(MyLibusbDeviceHandle, 0) < 0) //claims the interface with the Operating System { fprintf(stderr, "usb_claim_interface() failed\n"); //Closes a device opened since the claim interface is failed. usb_close(MyLibusbDeviceHandle); return ; } Connection_Status = CONNECTED; // Everything went well. Now connection status is CONNECTED } }
int main(int argc, char *argv[]) { int fd; int err; if (argc < 2) { help(argv[0]); exit(1); } if (strcmp(argv[1], "enable") == 0) { err = enable(argc, argv); } else if (strcmp(argv[1], "disable") == 0) { fd = open_dev(); err = ioctl(fd, JAILHOUSE_DISABLE); if (err) perror("JAILHOUSE_DISABLE"); close(fd); } else if (strcmp(argv[1], "cell") == 0) { err = cell_management(argc, argv); } else { help(argv[0]); exit(1); } return err ? 1 : 0; }
int main(void) { usb_dev_handle *dev = NULL; /* the device handle */ uint8_t readBuffer[0x200*0x100]; /* Set up USB */ usb_init(); usb_find_busses(); usb_find_devices(); if (!(dev = open_dev())) { perror("device not found!\n"); exit(EXIT_FAILURE); } /* if (usb_set_configuration(dev, 1) < 0) { perror("setting config 1 failed\n"); usb_close(dev); exit(EXIT_FAILURE); } */ if (usb_claim_interface(dev, 0) < 0) { perror("claiming interface failed\n"); usb_close(dev); exit(EXIT_FAILURE); } //FIXME: sniffed read10(readBuffer,0x100,0x0,dev); printReadBuffer(readBuffer); /* Clean up */ usb_release_interface(dev, 0); usb_close(dev); exit(0); }
int etherflow_open_socket_C(const char *dev, unsigned char *destmac, unsigned char *srcmac) { // src mac can't be modified using the bpf. it's automatically replaced by the real mac address. bpf = open_dev(); bpf_buf_len = set_buf_len(bpf); assoc_dev(bpf, dev); //This size must match the number of instructions in the filter program my_bpf_program.bf_len = 8; my_bpf_program.bf_insns = &insns; if (ioctl(bpf, BIOCSETF, &my_bpf_program) < 0) // Setting filter { perror("ioctl BIOCSETF"); exit(EXIT_FAILURE); } printf("<etherflow> Filter program set\n"); // Allocate space for bpf packet bpf_buf = (struct bpf_hdr*) malloc(bpf_buf_len); if (bpf_buf == 0){ fprintf(stderr, "bpf buffer alloc failed: %s\n", strerror(errno)); return -1; } bpf_ptr = (char*)bpf_buf; bpf_read_bytes = 0; printf("<etherflow> bpf buffer created size : %d\n", bpf_buf_len); // Message printf("<etherflow> started on device %s\n", dev); }
int main(int argc, char *argv[]) { int fd; int nwrite, nread; char r_buf[512], w_buf[512]; fd = open_dev(); // set_speed(fd, 115200); // if (set_parity(fd, 8, 1, 'N') == FALSE) { // printf("Set Parity Error "); // exit(0); // } // // while (1) { // memset(w_buf, 0, sizeof(w_buf)); // get_time(w_buf); // // nwrite = write(fd, w_buf, 30); // printf("w_buf = %s\n", w_buf); // sleep(1); // //#if 0 // memset(r_buf, 0, sizeof(r_buf)); // while((nread = read(fd, r_buf, 30)) > 0) // { // printf("r_buf = %s\n", r_buf); // memset(r_buf, 0, sizeof(r_buf)); // } //#endif // } return 0; }
static int listing_xwayip() { int i, ret; struct in_addr in; fd = open_dev(); ret = read(fd, buf, 100); if(ret < 0) { printf("/dev/xwaya read : %s\n", strerror(errno)); return -1; } if(buf[0] <= 0) printf("There is no xway enabled IP\n"); for(i=0; i<buf[0]; i++) { in.s_addr = buf[i+1]; printf("[%d] %s\n", i+1, inet_ntoa(in)); } close(fd); return 0; }
int shm_cmd(unsigned long vma, key_t key, int cmd) { shm_lock_t shml; int ret = 0, fd = 0; printf("Received 'cmd' : %d", cmd); printf("Opening device file\n"); fd = open_dev(); shml.vma = vma; shml.key = key; shml.len = 4096; printf("Sending ioctl\n"); if (ioctl(fd, cmd, &shml) < 0) { perror("ioctl lock"); ret = -1; goto errout; } errout: printf("Closing the dev_fd file\n"); close(fd); return ret; }
int connectDevice() { if ( Connection_Status != CONNECTED) { libusb_init(NULL); if(!(MyLibusbDeviceHandle = open_dev())) { fprintf(stderr, "open_dev() failed\n"); return -1; } // Sets the Active configuration of the device if (libusb_set_configuration(MyLibusbDeviceHandle, 1) < 0) { fprintf(stderr, "usb_set_configuration() failed\n"); libusb_close(MyLibusbDeviceHandle); return -2; } // claims the interface with the Operating System if(libusb_claim_interface(MyLibusbDeviceHandle, 0) < 0) { fprintf(stderr, "usb_claim_interface() failed\n"); //Closes a device opened since the claim interface is failed. libusb_close(MyLibusbDeviceHandle); return -3; } // Everything went better than expected Connection_Status = CONNECTED; } return 0; }
/**ltl * 功能: 将目标设备加入到列表中 * 参数: t ->映射表 * ti ->映射目标 * path ->映射目标<major:minor> * start-> 映射目标相对低层设备的起始偏移量(类似磁盘分区的起始地址) * len -> 此目标设备在dm设备的长度 * mode -> rw * result-> 底层设备对象 * 返回值: * 说明: */ static int __table_get_device(struct dm_table *t, struct dm_target *ti, const char *path, sector_t start, sector_t len, int mode, struct dm_dev **result) { int r; dev_t dev; struct dm_dev *dd; unsigned int major, minor; BUG_ON(!t); /* 获取主设备号和次设备号 */ if (sscanf(path, "%u:%u", &major, &minor) == 2) { /* Extract the major/minor numbers */ dev = MKDEV(major, minor); if (MAJOR(dev) != major || MINOR(dev) != minor) return -EOVERFLOW; } else { /* convert the path to a device */ if ((r = lookup_device(path, &dev))) /* 根据设备路径/dev/sdb获取<major:minor> */ return r; } /* 在列表中查找映射目标 */ dd = find_device(&t->devices, dev); if (!dd) { dd = kmalloc(sizeof(*dd), GFP_KERNEL); if (!dd) return -ENOMEM; dd->mode = mode; dd->bdev = NULL; /* 打开设备 */ if ((r = open_dev(dd, dev, t->md))) { kfree(dd); return r; } format_dev_t(dd->name, dev); /* 主设备号次设备号 */ atomic_set(&dd->count, 0); /* 将目标设备插入到映射表中 */ list_add(&dd->list, &t->devices); } else if (dd->mode != (mode | dd->mode)) { r = upgrade_mode(dd, mode, t->md); if (r) return r; } atomic_inc(&dd->count); /* 检查区域是否超过设备 */ if (!check_device_area(dd, start, len)) { DMWARN("device %s too small for target", path); dm_put_device(ti, dd); return -EINVAL; } *result = dd; return 0; }
void create_mtree(const char *path) { struct dev *dev; create_dev(path); dev = open_dev(path, NUM_MTREE_BLOCKS); close_dev(dev); }
void devlist(int argc, char *argv[]) { struct nvm_identify_controller cdata; struct nvm_identify_namespace nsdata; char name[64]; uint8_t mn[64]; uint32_t i; int ch, ctrlr, fd, found, ret; while ((ch = getopt(argc, argv, "")) != -1) { switch (ch) { default: devlist_usage(); } } ctrlr = -1; found = 0; while (1) { ctrlr++; sprintf(name, "%s%d", NVME_CTRLR_PREFIX, ctrlr); ret = open_dev(name, &fd, 0, 0); if (ret != 0) { if (ret == EACCES) { warnx("could not open "_PATH_DEV"%s\n", name); continue; } else break; } found++; read_controller_data(fd, &cdata); nvme_strvis(mn, sizeof(mn), cdata.mn, sizeof(cdata.mn)); printf("%6s: %s\n", name, mn); for (i = 0; i < cdata.nn; i++) { sprintf(name, "%s%d%s%d", NVME_CTRLR_PREFIX, ctrlr, NVME_NS_PREFIX, i+1); read_namespace_data(fd, i+1, &nsdata); printf(" %10s (%lldMB)\n", name, nsdata.nsze * (long long)ns_get_sector_size(&nsdata) / 1024 / 1024); } close(fd); } if (found == 0) printf("No NVMe controllers found.\n"); exit(1); }
/* * Add a device to the list, or just increment the usage count if * it's already present. */ static int __table_get_device(struct dm_table *t, struct dm_target *ti, const char *path, sector_t start, sector_t len, int mode, struct dm_dev **result) { int r; dev_t dev; struct dm_dev *dd; unsigned int major, minor; if (!t) BUG(); if (sscanf(path, "%u:%u", &major, &minor) == 2) { /* Extract the major/minor numbers */ dev = MKDEV(major, minor); if (MAJOR(dev) != major || MINOR(dev) != minor) return -EOVERFLOW; } else { /* convert the path to a device */ if ((r = lookup_device(path, &dev))) return r; } dd = find_device(&t->devices, dev); if (!dd) { dd = kmalloc(sizeof(*dd), GFP_KERNEL); if (!dd) return -ENOMEM; dd->mode = mode; dd->bdev = NULL; if ((r = open_dev(dd, dev))) { kfree(dd); return r; } atomic_set(&dd->count, 0); list_add(&dd->list, &t->devices); } else if (dd->mode != (mode | dd->mode)) { r = upgrade_mode(dd, mode); if (r) return r; } atomic_inc(&dd->count); if (!check_device_area(dd, start, len)) { DMWARN("device %s too small for target", path); dm_put_device(ti, dd); return -EINVAL; } *result = dd; return 0; }
/** * @breif open serial port * @param port_num [in] number of the serial port, from 0~255 * @return file description of the serial port */ int open_serial_port(int port_num) { assert(port_num > -1 && port_num < 256); char dev[MAX_DEV_NAME]= {0}; // dev/ttyS0 = serial port 0 sprintf(dev,"/dev/ttyUSB%d",port_num); return open_dev(dev); }
struct mtree *open_mtree(const char *path) { struct mtree *mt; mt = ezalloc(sizeof(*mt)); mt->dev = open_dev(path, NUM_MTREE_BLOCKS); mt->seqnum = 1; mt->next_block = 1; return mt; }
static int task_read(int chip, unsigned int reg, unsigned int *value) { int fd, err; fd = open_dev(chip); if (fd < 0) return -1; err = do_read(chip, fd, 0xFFFF, reg, value); close(fd); return err; }
int max_ctx_on_plun(int cmd) { int i; int rc = 0; struct ctx myctx; struct ctx *p_ctx=&myctx; pid = getpid(); pthread_t thread; int max_p = MAX_OPENS; for (i=0; i<max_p;i++) { if (0==fork()) { //child process pid = getpid(); debug("%d: ......process %d created...\n",pid,i); memset(p_ctx, 0, sizeof(myctx)); strcpy(p_ctx->dev, cflash_path); if ((p_ctx->fd = open_dev(p_ctx->dev, O_RDWR)) < 0) { fprintf(stderr,"open failed %s, errno %d\n",cflash_path, errno); exit(rc); } #ifdef _AIX rc |= ioctl_dk_capi_query_path(p_ctx); rc|=ctx_init_internal(p_ctx, 0, p_ctx->devno); #else rc|=ctx_init_internal(p_ctx, 0x2, p_ctx->devno); #endif if (2 == cmd) rc |=create_resource(p_ctx,0,0,LUN_VIRTUAL); if (3 == cmd) rc |=create_resource(p_ctx,0,0,LUN_DIRECT); if (4 == cmd) { //do io all vluns created on path_id_mask pthread_create(&thread, NULL,ctx_rrq_rx,p_ctx); rc |= create_resource(p_ctx,p_ctx->chunk_size,0,LUN_VIRTUAL); rc |= do_io(p_ctx,0x10); pthread_cancel(thread); } sleep(10); //lets all context get created if ( 1 != cmd ) rc|=close_res(p_ctx); rc|=ctx_close(p_ctx); debug("%d:.exiting with rc=%d\n",pid,rc); exit(rc); } } rc=wait4all(); return rc; }
int main(int argc, char **argv) { int handle; if (argc < 2) usage(); if (argc > 1 && (streq(argv[1], "-h") || streq(argv[1], "--help"))) usage(); handle = open_dev("/dev/usb/hiddev%d"); if (handle == -1) handle = open_dev("/dev/hiddev%d"); if (handle == -1) trouble_shooting(); init_dev(handle); configure(handle, argc, argv); close_dev(handle); exit(0); }
int lkgdb_init() { pthread_t lkgdb_tid; /* Open the lkgdb device */ dev_lkgdb_fd = open_dev(); if (dev_lkgdb_fd < 0) { return -1; } /* Create the lkgdb thread */ if (pthread_create(&lkgdb_tid, NULL, lkgdb_kwork, NULL) < 0) return -1; return 0; }
static int get_dev_fd(uint32_t *gen_caps) { int fd; char name[PATH_MAX]; size_t n; for (n = 0; n < MAX_DEV_SEQ; n++) { snprintf(name, sizeof(name), "/dev/teepriv%zu", n); fd = open_dev(name, gen_caps); if (fd >= 0) return fd; } return -1; }
static int task_maskset(int chip, unsigned int reg, unsigned int mask, unsigned int set) { int fd, err; unsigned int value; mask &= 0xFFFF; set &= 0xFFFF; fd = open_dev(chip); if (fd < 0) return -1; err = do_write(chip, fd, mask, reg, set); close(fd); return err; }
int main(void) { usb_dev_handle *dev = NULL; /* the device handle */ char tmp[BUF_SIZE]; usb_init(); /* initialize the library */ usb_find_busses(); /* find all busses */ usb_find_devices(); /* find all connected devices */ if(!(dev = open_dev())) { printf("error: device not found!\n"); return 0; } if(usb_set_configuration(dev, 1) < 0) { printf("error: setting config 1 failed\n"); usb_close(dev); return 0; } if(usb_claim_interface(dev, 0) < 0) { printf("error: claiming interface 0 failed\n"); usb_close(dev); return 0; } if(usb_bulk_write(dev, EP_OUT, tmp, sizeof(tmp), 5000) != sizeof(tmp)) { printf("error: bulk write failed\n"); } if(usb_bulk_read(dev, EP_IN, tmp, sizeof(tmp), 5000) != sizeof(tmp)) { printf("error: bulk read failed\n"); } usb_release_interface(dev, 0); usb_close(dev); return 0; }
static void identify_ctrlr(int argc, char *argv[]) { struct nvme_controller_data cdata; int ch, fd, hexflag = 0, hexlength; int verboseflag = 0; while ((ch = getopt(argc, argv, "vx")) != -1) { switch ((char)ch) { case 'v': verboseflag = 1; break; case 'x': hexflag = 1; break; default: identify_usage(); } } /* Check that a controller was specified. */ if (optind >= argc) identify_usage(); open_dev(argv[optind], &fd, 1, 1); read_controller_data(fd, &cdata); close(fd); if (hexflag == 1) { if (verboseflag == 1) hexlength = sizeof(struct nvme_controller_data); else hexlength = offsetof(struct nvme_controller_data, reserved5); print_hex(&cdata, hexlength); exit(0); } if (verboseflag == 1) { fprintf(stderr, "-v not currently supported without -x\n"); identify_usage(); } print_controller(&cdata); exit(0); }
static int cell_create(int argc, char *argv[]) { struct { struct jailhouse_new_cell cell; struct jailhouse_preload_image image; } params; struct jailhouse_new_cell *cell = ¶ms.cell; struct jailhouse_preload_image *image = params.cell.image; size_t size; int err, fd; char *endp; if (argc != 5 && argc != 7) { help(argv[0]); exit(1); } cell->config_address = (unsigned long)read_file(argv[3], &size); cell->config_size = size; cell->num_preload_images = 1; image->source_address = (unsigned long)read_file(argv[4], &size); image->size = size; image->target_address = 0; if (argc == 7) { errno = 0; image->target_address = strtoll(argv[6], &endp, 0); if (errno != 0 || *endp != 0 || strcmp(argv[5], "-l") != 0) { help(argv[0]); exit(1); } } fd = open_dev(); err = ioctl(fd, JAILHOUSE_CELL_CREATE, ¶ms); if (err) perror("JAILHOUSE_CELL_CREATE"); close(fd); free((void *)(unsigned long)cell->config_address); free((void *)(unsigned long)image->source_address); return err; }
int init_dev(void) { char *dev_path; if(!(dev_path = get_dev_path())) { fprintf(stderr, "failed to find the spaceball device file\n"); return -1; } printf("using device: %s\n", dev_path); if(open_dev(dev_path) == -1) { return -1; } printf("device name: %s\n", dev_name); return 0; }
/* * This upgrades the mode on an already open dm_dev. Being * careful to leave things as they were if we fail to reopen the * device. */ static int upgrade_mode(struct dm_dev *dd, int new_mode, struct mapped_device *md) { int r; struct dm_dev dd_copy; dev_t dev = dd->bdev->bd_dev; dd_copy = *dd; dd->mode |= new_mode; dd->bdev = NULL; r = open_dev(dd, dev, md); if (!r) close_dev(&dd_copy, md); else *dd = dd_copy; return r; }
int main(int argc, char *argv[]) { parse_arg(argc, argv); check_params_before(); check_mount(); stat_dev(); open_dev(); check_params_after(); create_super_block(); init_inodes(); init_root_block(); link_free_blocks(); close_dev(); return 0; }
/* * This upgrades the mode on an already open dm_dev. Being * careful to leave things as they were if we fail to reopen the * device. */ static int upgrade_mode(struct dm_dev *dd, int new_mode) { int r; struct dm_dev dd_copy; dev_t dev = dd->bdev->bd_dev; memcpy(&dd_copy, dd, sizeof(dd_copy)); dd->mode |= new_mode; dd->bdev = NULL; r = open_dev(dd, dev); if (!r) close_dev(&dd_copy); else memcpy(dd, &dd_copy, sizeof(dd_copy)); return r; }
static int config(struct audio_config *config) { unsigned int bitdepth; bitdepth = config->precision & ~7; if (bitdepth == 0) bitdepth = 16; else if (bitdepth > 32) bitdepth = 32; if (opened) { if (drain() == -1) return -1; close_dev(wave_handle); } if (open_dev(&wave_handle, config->channels, config->speed, bitdepth) == -1) return -1; switch (config->precision = bitdepth) { case 8: audio_pcm = audio_pcm_u8; break; case 16: audio_pcm = audio_pcm_s16le; break; case 24: audio_pcm = audio_pcm_s24le; break; case 32: audio_pcm = audio_pcm_s32le; break; } samplerate = config->speed; samplesize = bitdepth / 8; return 0; }
static int cell_destroy(int argc, char *argv[]) { int err, fd; if (argc != 4) { help(argv[0]); exit(1); } fd = open_dev(); err = ioctl(fd, JAILHOUSE_CELL_DESTROY, argv[3]); if (err) perror("JAILHOUSE_CELL_DESTROY"); close(fd); return err; }