int can_register(FAR const char *path, FAR struct can_dev_s *dev) { int i; /* Initialize the CAN device structure */ dev->cd_ocount = 0; dev->cd_ntxwaiters = 0; dev->cd_nrxwaiters = 0; dev->cd_npendrtr = 0; sem_init(&dev->cd_xmit.tx_sem, 0, 0); sem_init(&dev->cd_recv.rx_sem, 0, 0); sem_init(&dev->cd_closesem, 0, 1); for (i = 0; i < CONFIG_CAN_NPENDINGRTR; i++) { sem_init(&dev->cd_rtr[i].cr_sem, 0, 0); dev->cd_rtr[i].cr_msg = NULL; } /* Initialize/reset the CAN hardware */ dev_reset(dev); /* Register the CAN device */ canvdbg("Registering %s\n", path); return register_driver(path, &g_canops, 0666, dev); }
static int pdev_reset(struct device *dev) { struct perf_device *pdev = dev_pdev(dev); struct timeval t1, t2; int rc; assert(!gettimeofday(&t1, NULL)); rc = dev_reset(pdev->shadow_dev); assert(!gettimeofday(&t2, NULL)); pdev->reset_count++; pdev->reset_time_us += diff_timeval_us(&t1, &t2); return rc; }
/* Caller must guarantee that the left bock is good, and written. */ static int search_edge(struct device *dev, uint64_t *pleft_pos, uint64_t right_pos, const char *stamp_blk, char *probe_blk) { uint64_t gap = right_pos - *pleft_pos; uint64_t prv_gap = gap + 1; while (prv_gap > gap && gap >= 1) { uint64_t a, b, max_idx; if (write_test_blocks(dev, stamp_blk, *pleft_pos, right_pos, &a, &b, &max_idx)) return true; /* Reset. */ if (dev_reset(dev) && dev_reset(dev)) return true; if (probe_test_blocks(dev, stamp_blk, probe_blk, pleft_pos, &right_pos, a, b, max_idx)) return true; prv_gap = gap; gap = right_pos - *pleft_pos; } return false; }
/* Return true if the block at @pos is damaged. */ static int test_block(struct device *dev, const char *stamp_blk, char *probe_blk, uint64_t pos) { /* Write block. */ if (dev_write_block(dev, stamp_blk, pos) && dev_write_block(dev, stamp_blk, pos)) return true; /* Reset. */ if (dev_reset(dev) && dev_reset(dev)) return true; /* * Test block. */ if (dev_read_block(dev, probe_blk, pos) && dev_read_block(dev, probe_blk, pos)) return true; if (equal_blk(dev, stamp_blk, probe_blk)) return false; /* Save time with certainly damaged blocks. */ if (!similar_blk(dev, stamp_blk, probe_blk, 8)) { /* The probe block is damaged. */ return true; } /* The probe block seems to be damaged. * Trying a second time... */ return dev_write_and_reset(dev, stamp_blk, pos) || dev_read_block(dev, probe_blk, pos) || !equal_blk(dev, stamp_blk, probe_blk); }
/** * Main function. Initializes the USB-device, parses commandline-parameters and * calls the functions that communicate with the device. * \param argc Number of arguments. * \param argv Arguments. * \return Error code. */ int main(int argc, char **argv) { usb_dev_handle *handle = NULL; if (argc < 2) { usage(argv[0]); exit(1); } usb_init(); if (usbOpenDevice (&handle, USBDEV_SHARED_VENDOR, "www.schatenseite.de", USBDEV_SHARED_PRODUCT, "USB-LED-Fader") != 0) { fprintf(stderr, "Could not find USB device \"USB-LED-Fader\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); exit(1); } /* We have searched all devices on all busses for our USB device above. Now * try to open it and perform the vendor specific control operations for the * function requested by the user. */ if (strcmp(argv[1], "test") == 0) { dev_test(handle, argc, argv); } else if (strcmp(argv[1], "set") == 0) { dev_set(handle, argc, argv); } else if (strcmp(argv[1], "clear") == 0) { dev_clear(handle, argc, argv); } else if (strcmp(argv[1], "status") == 0) { dev_status(handle, argc, argv); } else if (strcmp(argv[1], "reset") == 0) { dev_reset(handle, argc, argv); } else if (strcmp(argv[1], "show") == 0) { dev_show(handle, argc, argv); } else { usage(argv[0]); exit(1); } usb_close(handle); return 0; }
int loop_socket_handle(int socket, int tt_ms) { unsigned char outhexbuf[ONE_TCP_MAX_LEN] = {0}; PRO *pro = (PRO*)malloc(sizeof(PRO)); memset(pro, 0, sizeof(PRO)); pro->payload = (unsigned char*)malloc(ONLINE_MAX_LEN); memset(pro->payload, 0, (ONLINE_MAX_LEN)); int ret = 0; int login_ok = 0; unsigned int sendseq = 0; int outinx = 0; int hexlen = 0; MACH_STAT stat = IDLE; while(1) { debug(LOG_NOTICE, "stat=%d,login_flag=%d,outinx=%d,last_cmd=%04x,last_recv_ret=%d,dev_work_status=%d\n", stat,login_ok,outinx,pro->hd.cmd,ret,dev_work_status); switch(stat) { case IDLE: //sleep_intp_s(5); //will block tcp recving break; case TO_LOGIN: memset(&pro->hd, 0, sizeof(PRO_HD)); memset(pro->payload, 0, (ONLINE_MAX_LEN)); send_login_info(socket, pro, sendseq++); break; case TO_DEVSTATUS: //recv_uart(); //send_dev_data(); if(glb_cfg.glb_sock != socket) glb_cfg.glb_sock = socket; // uart thread will write glb_sock directly. break; case TO_WORKSTATUS: memset(pro->payload, 0, (ONLINE_MAX_LEN)); if(dev_work_status == E_DEV_TAKEUP) { send_busy_info(socket, pro, dev_work_status, sendseq++); } else { send_status_info(socket, pro, dev_work_status, sendseq++); } stat = IDLE; break; case TO_RESPSTATUS: memset(pro->payload, 0, (ONLINE_MAX_LEN)); response_status_info(socket, pro, dev_work_status, sendseq++); stat = IDLE; break; case TO_HEART: memset(&pro->hd, 0, sizeof(PRO_HD)); send_heart_info(socket, &pro->hd, sendseq++); if(outinx++ > HEART_EXIT_COUNT) goto EXIT_ERR; else stat = IDLE; break; case TO_RESP: //for testing;all back pro->hd.stat = PRO_RSP; memset(outhexbuf, 0, sizeof(outhexbuf)); hexlen = 0; pro_pro2hexbuf(pro,outhexbuf,&hexlen); send(socket, outhexbuf, hexlen, sendseq++); debug(LOG_DEBUG, "Send Resp Len:%d\n",hexlen); stat = IDLE; break; case NET_DISCONN: goto EXIT_ERR; break; default: break; } //memset(pro, 0, sizeof(PRO)); //NOTE***: will init payload as NULL; this will cause crash!!! memset(&pro->hd, 0, sizeof(PRO_HD)); memset(pro->payload, 0, (ONLINE_MAX_LEN)); ret = socket_recv_tt(socket, pro, tt_ms); if(ret == 0) { // timeout if(!login_ok) { stat = TO_LOGIN; } else { stat = TO_HEART; } } else if(ret < 0) { // socket error goto EXIT_ERR; } else if( (ret != sizeof(PRO_HD)) && (ret != sizeof(PRO_HD)+pro->hd.len) ) { debug(LOG_NOTICE, "Recv Length %d Error!\n",ret); continue; } else { // handle data... switch(pro->hd.cmd) { case IDM_DEV_LOGIN: debug(LOG_NOTICE, "---< server back: login\n"); login_ok = 1; if(dev_work_status != E_DEV_TAKEUP) dev_work_status = E_DEV_ONLINE; stat = TO_DEVSTATUS; break; case IDM_DEV_HEART: debug(LOG_NOTICE, "---< server back: heart\n"); outinx = 0; break; case IDM_GETDEV: debug(LOG_NOTICE, "---< server cmd: get dev\n"); stat = TO_RESPSTATUS; break; /* for response testing */ case 0xfffe: debug(LOG_NOTICE, "---< server cmd: test response\n"); stat = TO_RESP; break; case IDM_TAKEUPDEV: case IDM_RELEASEDEV: case IDM_RESET: case IDM_DOSTART: case IDM_DOSTOP: case IDM_SETPARS: case IDM_DELPARS: if(0 == memcmp(taskid, pro->payload+32, 32)) { break; } else { if(dev_work_status == E_DEV_TAKEUP) { debug(LOG_WARNING, "---< server taskid is not correct!\n"); stat = TO_WORKSTATUS; continue; } else { /* Firstly takeup device */ break; } } default: debug(LOG_ERR, "==== Handle: Unknow Server CMD 0x%02x\n", pro->hd.cmd); continue; } switch(pro->hd.cmd) { /* handle at local */ case IDM_TAKEUPDEV: debug(LOG_NOTICE, "---< server cmd: takeup dev\n"); dev_work_status = E_DEV_TAKEUP; stat = TO_WORKSTATUS; memcpy(taskid, pro->payload+32, sizeof(taskid)); /* skip devid 32*/ break; case IDM_RELEASEDEV: debug(LOG_NOTICE, "---< server cmd: release dev\n"); dev_work_status = E_DEV_ONLINE; stat = TO_WORKSTATUS; memset(taskid, 0, sizeof(taskid)); break; /* send to uart */ case IDM_RESET: debug(LOG_NOTICE, "---< server cmd: reset dev\n"); dev_work_status = E_DEV_ONLINE; stat = TO_WORKSTATUS; /* stop and clear */ dev_reset(pro->payload, pro->hd.len); break; case IDM_DOSTART: debug(LOG_NOTICE, "---< server cmd: start dev\n"); glb_cfg.rsp_cmd_type = SVR_NEED_TASK_RET; glb_cfg.rsp_cmd_type |= SVR_NEED_TASK_PERCT; /* only send payload to uart */ ret = dev_start(pro->payload, pro->hd.len); debug(LOG_DEBUG, "---Send To Com ret %d\n",ret); break; case IDM_DOSTOP: debug(LOG_NOTICE, "---< server cmd: stop dev\n"); glb_cfg.rsp_cmd_type = SVR_NEED_TASK_RET; /* only send payload to uart */ ret = dev_stop(pro->payload, pro->hd.len); debug(LOG_DEBUG, "---Send To Com ret %d\n",ret); break; case IDM_SETPARS: debug(LOG_NOTICE, "---< server cmd: set params dev\n"); /* only send params to uart */ ret = dev_setpar(pro->payload+64+2, (pro->hd.len-66)>0?(pro->hd.len-66):0); debug(LOG_DEBUG, "---Send To Com ret %d\n",ret); break; case IDM_DELPARS: debug(LOG_NOTICE, "---< server cmd: del params\n"); /* only send payload to uart */ ret = dev_clrpar(pro->payload, pro->hd.len); debug(LOG_DEBUG, "---Send To Com ret %d\n",ret); break; } } continue; } /* end while(1) */ EXIT_ERR: debug(LOG_NOTICE, "<--- Socket recv out!\n"); glb_cfg.glb_sock = -1; glb_cfg.rsp_cmd_type = 0; if(socket > 0) close(socket); if(pro && pro->payload) { free(pro->payload); free(pro); } return -1; }
void uaeserialdev_reset (void) { if (!currprefs.uaeserial) return; dev_reset (); }
void netdev_reset (void) { if (!currprefs.sana2) return; dev_reset (); }
void scsidev_reset (void) { if (!currprefs.scsi) return; dev_reset (); }
static int sdev_reset(struct device *dev) { return dev_reset(dev_sdev(dev)->shadow_dev); }