int client_command(int sock_fd, const char *cmd, int timeout, const char *what) { if (client_command_noack(sock_fd, cmd, timeout, what) == 0) return 0; return check_ack(sock_fd, what, timeout) == 1; }
int SiSTouch_Zeus_USB::usb_command_D8_P810_04_query() { unsigned char buffer[BUFFER_SIZE]; LOGI( "Query (0x04)" ); int ret = sis_usb_io( 0xD8, 1, 0x4, 0, buffer, 2, DEFAULT_TIMEOUT ); if( ret < 0 ) { return ret; } return check_ack( buffer ); }
int SiSTouch_Zeus_USB::usb_simple_command( int request ) { unsigned char buffer[BUFFER_SIZE]; int ret = sis_usb_io( request, 1, 0, 0, buffer, 2, DEFAULT_TIMEOUT ); if( ret < 0 ) { return ret; } return check_ack( buffer ); }
/* * Return values: success: positive non-zero sequence number * error: -errno * short: 0 */ int audit_send(int fd, int type, const void *data, unsigned int size) { static int sequence = 0; struct audit_message req; int retval; struct sockaddr_nl addr; /* Due to user space library callbacks, there's a chance that a -1 for the fd could be passed. Just check for and handle it. */ if (fd < 0) { errno = EBADF; return -errno; } if (NLMSG_SPACE(size) > MAX_AUDIT_MESSAGE_LENGTH) { errno = EINVAL; return -errno; } if (++sequence < 0) sequence = 1; memset(&req, 0, sizeof(req)); req.nlh.nlmsg_len = NLMSG_SPACE(size); req.nlh.nlmsg_type = type; req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; req.nlh.nlmsg_seq = sequence; if (size && data) memcpy(NLMSG_DATA(&req.nlh), data, size); memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_pid = 0; addr.nl_groups = 0; do { retval = sendto(fd, &req, req.nlh.nlmsg_len, 0, (struct sockaddr*)&addr, sizeof(addr)); } while (retval < 0 && errno == EINTR); if (retval == (int)req.nlh.nlmsg_len) { if ((retval = check_ack(fd, sequence)) == 0) return sequence; else return retval; } if (retval < 0) return -errno; return 0; }
// MASTER_I2C_WRITE // write whatever's inside the TX_buffer to the slave device // // PARAM device_ID : I2C address of the slave // registerAddr: register address for a specific variable void master_I2C_write(unsigned int device_ID, int registerAddr) { int i; SSP1CON2bits.RCEN = 0; IdleI2C(); // Wait until the bus is idle StartI2C(); // Send START condition IdleI2C(); // Wait for the end of the START condition WriteI2C( device_ID << 1 | 0x00 ); // Send address with R/W cleared for write IdleI2C(); // Wait for ACK check_ack(); WriteI2C(registerAddr); // Write first byte of data IdleI2C(); // Wait for ACK check_ack(); for (i = 0; i < TX_buffer_size; i++) { WriteI2C(I2C_TX_buffer_ptr[i]); // Write first byte of data IdleI2C(); // Wait for ACK check_ack(); } StopI2C(); // Hang up, send STOP condition }
// wait for device to reply, and read the data into RX buffer void master_I2C_read(unsigned int device_ID, int registerAddr) { int i; IdleI2C(); c_StartI2C(); // Send START condition IdleI2C(); // Wait for the end of the START condition c_WriteI2C( device_ID << 1 | 0x01 ); // Send address with R/W set for read IdleI2C(); check_ack(); // wait for data sent and acked for(i=0; i< RX_buffer_size-1; i++) { I2C_RX_buffer_ptr[i] = c_ReadI2C(); // Read first byte of data c_ackI2C(); //send ACK IdleI2C(); } I2C_RX_buffer_ptr[RX_buffer_size-1] = c_ReadI2C(); // Read nth byte of data NotAckI2C(); // Send NACK c_stopI2C(); // Hang up, send STOP condition }
int main(int argc, char* argv[]) { int res,i,tot=0; char* s_dev="COM2"; char* s_fname=0; char* s_add=0; char* s_len=0; unsigned address=0x500000,len=0,tlen; FILE* mybin=0; struct stat info; char buffer[BUFFER_SIZE]; unsigned ok=0xc1a0c1a0; unsigned long lenw; for(i=1;i<argc;i++){ if(!strcmp(argv[i],"-d")){ s_dev = argv[++i]; continue; } if(!strcmp(argv[i],"-a")){ s_add = argv[++i]; continue; } /* if(!strcmp(argv[i],"-reset")){ operation = CMD_RESET; continue; }*/ if(!strcmp(argv[i],"-h")){ printf("usage is %s <bin> [-a <flash address>]\n",argv[0]); exit(0); } s_fname = argv[i]; // printf("name -> %s",s_fname); } if(s_fname){ mybin=fopen(s_fname,"rb"); if(mybin==0){ printf("## you must specify a valid filename\n"); exit(1); } } else { printf("## you must specify a valid filename\n"); exit(1); } if(s_add){ address=strtoul(s_add,0,0); } initCom(s_dev); { int fdf=fseek(mybin,0,SEEK_END ); fdf = ftell(mybin); fseek(mybin,0,SEEK_SET ); len = fdf- ftell(mybin); } printf("* binary len %d bytes flash add 0x%x..\n",len,address); if(ask(PRG_FLASH,address,len)!=0) { fclose(mybin); CloseHandle(hComm); return 0; } // check flsh id if((res=check_ack())!=0){ printf("## flash id check failed 0x%x\n",res); CloseHandle(hComm); exit(1); } printf("* flash id check ok\n"); WriteFile(hComm,&ok,4,&lenw,NULL); if((res=check_ack())!=0){ printf("## flash id check failed 0x%x\n",res); CloseHandle(hComm); exit(1); } printf("* erasing space address 0x%x... please wait\n",address); // verify erase if((res=check_ack())!=0){ printf("## verify erase failed 0x%x\n",res); CloseHandle(hComm); exit(1); } printf("* flash erase check ok\n"); printf("* start programming %d bytes.\n",len); tlen=len; while(len>0){ unsigned long ll=(len<BUFFER_SIZE)?len:BUFFER_SIZE; tot+=ll; fread(buffer,1,ll,mybin); //usleep(8000); //write(fd,buffer,ll); WriteFile(hComm,buffer,ll,&lenw,NULL); if(ll!=lenw){ printf("## error writing byte write %d != byte to write %d\n",lenw,ll); exit(1); } printf("* downloading %.3f (rem:%d bytes)\r",(tlen-len)*100.0/tlen,tot); if((res=check_ack())!=0){ printf("## programming failed base add 0x%x\n",res); // close(fd); // exit(1); } len-=ll; } printf("\n* 100%% end programming\n"); return 0; }
void serialboot() { struct sfl_frame frame; int failed; unsigned int cmdline_adr, initrdstart_adr, initrdend_adr; static const char str[SFL_MAGIC_LEN] = SFL_MAGIC_REQ; const char *c; printf("I: Attempting serial firmware loading\n"); usb_debug_enable(0); c = str; while(*c) { uart_write(*c); c++; } if(!check_ack()) { printf("E: Timeout\n"); usb_debug_enable(1); return; } failed = 0; cmdline_adr = initrdstart_adr = initrdend_adr = 0; while(1) { int i; int actualcrc; int goodcrc; /* Grab one frame */ frame.length = uart_read(); frame.crc[0] = uart_read(); frame.crc[1] = uart_read(); frame.cmd = uart_read(); for(i=0;i<frame.length;i++) frame.payload[i] = uart_read(); /* Check CRC */ actualcrc = ((int)frame.crc[0] << 8)|(int)frame.crc[1]; goodcrc = crc16(&frame.cmd, frame.length+1); if(actualcrc != goodcrc) { failed++; if(failed == MAX_FAILED) { printf("E: Too many consecutive errors, aborting"); usb_debug_enable(1); return; } uart_write(SFL_ACK_CRCERROR); continue; } /* CRC OK */ switch(frame.cmd) { case SFL_CMD_ABORT: failed = 0; uart_write(SFL_ACK_SUCCESS); usb_debug_enable(1); return; case SFL_CMD_LOAD: { char *writepointer; failed = 0; writepointer = (char *)( ((unsigned int)frame.payload[0] << 24) |((unsigned int)frame.payload[1] << 16) |((unsigned int)frame.payload[2] << 8) |((unsigned int)frame.payload[3] << 0)); for(i=4;i<frame.length;i++) *(writepointer++) = frame.payload[i]; uart_write(SFL_ACK_SUCCESS); break; } case SFL_CMD_JUMP: { unsigned int addr; failed = 0; addr = ((unsigned int)frame.payload[0] << 24) |((unsigned int)frame.payload[1] << 16) |((unsigned int)frame.payload[2] << 8) |((unsigned int)frame.payload[3] << 0); uart_write(SFL_ACK_SUCCESS); boot(cmdline_adr, initrdstart_adr, initrdend_adr, rescue, addr); break; } case SFL_CMD_CMDLINE: failed = 0; cmdline_adr = ((unsigned int)frame.payload[0] << 24) |((unsigned int)frame.payload[1] << 16) |((unsigned int)frame.payload[2] << 8) |((unsigned int)frame.payload[3] << 0); uart_write(SFL_ACK_SUCCESS); break; case SFL_CMD_INITRDSTART: failed = 0; initrdstart_adr = ((unsigned int)frame.payload[0] << 24) |((unsigned int)frame.payload[1] << 16) |((unsigned int)frame.payload[2] << 8) |((unsigned int)frame.payload[3] << 0); uart_write(SFL_ACK_SUCCESS); break; case SFL_CMD_INITRDEND: failed = 0; initrdend_adr = ((unsigned int)frame.payload[0] << 24) |((unsigned int)frame.payload[1] << 16) |((unsigned int)frame.payload[2] << 8) |((unsigned int)frame.payload[3] << 0); uart_write(SFL_ACK_SUCCESS); break; default: failed++; if(failed == MAX_FAILED) { printf("E: Too many consecutive errors, aborting"); usb_debug_enable(1); return; } uart_write(SFL_ACK_UNKNOWN); break; } } }
int rxe_completer(void *arg) { struct rxe_qp *qp = (struct rxe_qp *)arg; struct rxe_dev *rxe = to_rdev(qp->ibqp.device); struct rxe_send_wqe *wqe = wqe; struct sk_buff *skb = NULL; struct rxe_pkt_info *pkt = NULL; enum comp_state state; rxe_add_ref(qp); if (!qp->valid || qp->req.state == QP_STATE_ERROR || qp->req.state == QP_STATE_RESET) { rxe_drain_resp_pkts(qp, qp->valid && qp->req.state == QP_STATE_ERROR); goto exit; } if (qp->comp.timeout) { qp->comp.timeout_retry = 1; qp->comp.timeout = 0; } else { qp->comp.timeout_retry = 0; } if (qp->req.need_retry) goto exit; state = COMPST_GET_ACK; while (1) { pr_debug("qp#%d state = %s\n", qp_num(qp), comp_state_name[state]); switch (state) { case COMPST_GET_ACK: skb = skb_dequeue(&qp->resp_pkts); if (skb) { pkt = SKB_TO_PKT(skb); qp->comp.timeout_retry = 0; } state = COMPST_GET_WQE; break; case COMPST_GET_WQE: state = get_wqe(qp, pkt, &wqe); break; case COMPST_CHECK_PSN: state = check_psn(qp, pkt, wqe); break; case COMPST_CHECK_ACK: state = check_ack(qp, pkt, wqe); break; case COMPST_READ: state = do_read(qp, pkt, wqe); break; case COMPST_ATOMIC: state = do_atomic(qp, pkt, wqe); break; case COMPST_WRITE_SEND: if (wqe->state == wqe_state_pending && wqe->last_psn == pkt->psn) state = COMPST_COMP_ACK; else state = COMPST_UPDATE_COMP; break; case COMPST_COMP_ACK: state = complete_ack(qp, pkt, wqe); break; case COMPST_COMP_WQE: state = complete_wqe(qp, pkt, wqe); break; case COMPST_UPDATE_COMP: if (pkt->mask & RXE_END_MASK) qp->comp.opcode = -1; else qp->comp.opcode = pkt->opcode; if (psn_compare(pkt->psn, qp->comp.psn) >= 0) qp->comp.psn = (pkt->psn + 1) & BTH_PSN_MASK; if (qp->req.wait_psn) { qp->req.wait_psn = 0; rxe_run_task(&qp->req.task, 1); } state = COMPST_DONE; break; case COMPST_DONE: if (pkt) { rxe_drop_ref(pkt->qp); kfree_skb(skb); skb = NULL; } goto done; case COMPST_EXIT: if (qp->comp.timeout_retry && wqe) { state = COMPST_ERROR_RETRY; break; } /* re reset the timeout counter if * (1) QP is type RC * (2) the QP is alive * (3) there is a packet sent by the requester that * might be acked (we still might get spurious * timeouts but try to keep them as few as possible) * (4) the timeout parameter is set */ if ((qp_type(qp) == IB_QPT_RC) && (qp->req.state == QP_STATE_READY) && (psn_compare(qp->req.psn, qp->comp.psn) > 0) && qp->qp_timeout_jiffies) mod_timer(&qp->retrans_timer, jiffies + qp->qp_timeout_jiffies); WARN_ON_ONCE(skb); goto exit; case COMPST_ERROR_RETRY: /* we come here if the retry timer fired and we did * not receive a response packet. try to retry the send * queue if that makes sense and the limits have not * been exceeded. remember that some timeouts are * spurious since we do not reset the timer but kick * it down the road or let it expire */ /* there is nothing to retry in this case */ if (!wqe || (wqe->state == wqe_state_posted)) { WARN_ON_ONCE(skb); goto exit; } if (qp->comp.retry_cnt > 0) { if (qp->comp.retry_cnt != 7) qp->comp.retry_cnt--; /* no point in retrying if we have already * seen the last ack that the requester could * have caused */ if (psn_compare(qp->req.psn, qp->comp.psn) > 0) { /* tell the requester to retry the * send queue next time around */ rxe_counter_inc(rxe, RXE_CNT_COMP_RETRY); qp->req.need_retry = 1; rxe_run_task(&qp->req.task, 1); } if (pkt) { rxe_drop_ref(pkt->qp); kfree_skb(skb); skb = NULL; } WARN_ON_ONCE(skb); goto exit; } else { rxe_counter_inc(rxe, RXE_CNT_RETRY_EXCEEDED); wqe->status = IB_WC_RETRY_EXC_ERR; state = COMPST_ERROR; } break; case COMPST_RNR_RETRY: if (qp->comp.rnr_retry > 0) { if (qp->comp.rnr_retry != 7) qp->comp.rnr_retry--; qp->req.need_retry = 1; pr_debug("qp#%d set rnr nak timer\n", qp_num(qp)); mod_timer(&qp->rnr_nak_timer, jiffies + rnrnak_jiffies(aeth_syn(pkt) & ~AETH_TYPE_MASK)); rxe_drop_ref(pkt->qp); kfree_skb(skb); skb = NULL; goto exit; } else { rxe_counter_inc(rxe, RXE_CNT_RNR_RETRY_EXCEEDED); wqe->status = IB_WC_RNR_RETRY_EXC_ERR; state = COMPST_ERROR; } break; case COMPST_ERROR: WARN_ON_ONCE(wqe->status == IB_WC_SUCCESS); do_complete(qp, wqe); rxe_qp_error(qp); if (pkt) { rxe_drop_ref(pkt->qp); kfree_skb(skb); skb = NULL; } WARN_ON_ONCE(skb); goto exit; } } exit: /* we come here if we are done with processing and want the task to * exit from the loop calling us */ WARN_ON_ONCE(skb); rxe_drop_ref(qp); return -EAGAIN; done: /* we come here if we have processed a packet we want the task to call * us again to see if there is anything else to do */ WARN_ON_ONCE(skb); rxe_drop_ref(qp); return 0; }