📄 otgcmd.c
字号:
/************************************************************* * Philips otg tools program * * (c) 2002 Koninklijke Philips Electronics N.V., All rights reserved * * This source code and any compilation or derivative thereof is the * proprietary information of Koninklijke Philips Electronics N.V. * and is confidential in nature. * Under no circumstances is this software to be exposed to or placed * under an Open Source License of any type without the expressed * written permission of Koninklijke Philips Electronics N.V. * * File Name: otgcmd.c * * History: * * Version Date Author Comments * ------------------------------------------------- * 1.0 09/23/02 SYARRA Initial Creation * *************************************************************/#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<signal.h>#include<sys/ioctl.h>#include<string.h>#include "usb_otg.h"#include "otgtool.h"usb_otg_info_t get_otg_info;usb_otg_info_t set_otg_info;#define OTG_TIMEOUT 20;int otgfsm_fd, dev_fd; /* OTG FSM file descriptor */int mount_flag = 0; /* Mounted the fs or not */char sys_cmd[128];char otg_state_name[25][15] = { /* State Name Array */ "Idle", "Host", "Peripheral", "Wait_Acon", "Srp_Init", "Inv_State", "Inv_State", "Inv_State", "Inv_State", "Inv_State", "Inv_State", "Inv_State", "Inv_State", "Inv_State", "Inv_State", "Inv_State", "Idle", "Host", "Peripheral", "Wait_Bcon", "Wait_Vrise", "Suspend", "Wait_Vfall", "Vbus_Err"};int otg_idle(void);int otg_host(void);int mount_otg_local_disk(void);int umount_otg_local_disk(void);int mount_otg_rmt_disk(unsigned char flag);int umount_otg_rmt_disk(void);int otg_clear_error(void);int otg_status(void);/*********************************************************************************** INT Signal Handler Clears the variables and exits.************************************************************************************/void sigint_handler(int signal){ /* SIGINT handler */ close(otgfsm_fd) ; /* Close file handler */ exit(0); /* Come out */}/*********************************************************************************** IO Signal Handler This handles the IO signals from the OTG FSM. Clears VBUS Err and Enumeration failures************************************************************************************/void sigio_handler(int signal){ ioctl(otgfsm_fd, OTG_IOC_GET_STATE, &get_otg_info); /* Get OTG fsm Information from the driver */}static char *program_name = "otgcmd"; /* Name of the program */ static char *otgdev_file = OTG_FSM_FILE; /* path of the otg device file*/ static char dev_file_name[32];static char proc_file_name[64];void usage (void) { fprintf (stderr,"Usage: %s [ host | idle | error | mount | umount | help ]\nTry: '%s help' for more information\n",program_name,program_name); exit(1);}void usage_help (void) { fprintf (stderr,"Usage: %s [ host | idle | error | mount | umount | help ]\n\n",program_name); fprintf (stderr," host: Change the otg state from Idle to Host\n idle: Change the otg state from host to Idle\n error: Change the otg state from error to Idle\n mount: Mount local and remote otg disks\n umount: Unmount local and remote otg disks \n help: Print this help information\n\n"); exit(1);}/********************************************************************************** Main Function Initialization: OTG fsm file, Shared memory, child process Wait for the user input**********************************************************************************/int main (int argc, char **argv) { int result = -1; if (argc && *argv) /* What's the program name? */ program_name = *argv; fprintf (stderr, "%s: OTG mass storage command utility\n",program_name); if(argv[1] == NULL) { usage(); return 0; } if((strcmp(argv[1],"help")==0)) { usage_help(); } else if(strcmp(argv[1],"host") == 0) { result = otg_host(); } else if(strcmp(argv[1],"idle") == 0) { result = otg_idle(); } else if(strcmp(argv[1],"error") == 0) { result = otg_clear_error(); } else if(strcmp(argv[1],"mount") == 0) { if(argv[2] == NULL) { if(mount_otg_rmt_disk(1) == 0) { result = mount_otg_local_disk(); } else { result = -1; } } else if(strcmp(argv[2],"local") == 0) { result = mount_otg_local_disk(); } else if(strcmp(argv[2],"rmt") == 0) { result = mount_otg_rmt_disk(1); } } else if(strcmp(argv[1],"umount") == 0) { if(argv[2] == NULL) { umount_otg_local_disk(); result = umount_otg_rmt_disk(); } else if(strcmp(argv[2],"local") == 0) { result = umount_otg_local_disk(); } else if(strcmp(argv[2],"rmt") == 0) { result = umount_otg_rmt_disk(); } } else if(strcmp(argv[1],"status") == 0) { result = 0; } else { usage(); return 0; } otg_status(); return result;}int mount_otg_local_disk(void) { sprintf(sys_cmd,"dd if=%s of=%s1 bs=%d skip=%d >/dev/null\n",MSDISK_FILE,MSDISK_FILE,BLOCK_SIZE,LINUX_UNIT_SIZE); system(sys_cmd); sprintf(sys_cmd,"mount %s1 %s -o loop\n",MSDISK_FILE,LOCAL_OTG_DISK); system(sys_cmd); system("mount |grep local\n"); return 0;}int umount_otg_local_disk(void) {#ifdef CONFIG_1362_PXA250 sprintf(sys_cmd,"umount -d %s\n",LOCAL_OTG_DISK);#else sprintf(sys_cmd,"umount %s\n",LOCAL_OTG_DISK);#endif /* CONFOG_1362_PXA250 */ system(sys_cmd); sprintf(sys_cmd,"dd if=%s1 of=%s bs=%d seek=%d >/dev/null\n",MSDISK_FILE,MSDISK_FILE,BLOCK_SIZE,LINUX_UNIT_SIZE); system(sys_cmd); return 0;}int mount_otg_rmt_disk(unsigned char flag) { char dev_c, proc_c; int result = -1; dev_c = 'a'; proc_c = '0'; strcpy(dev_file_name,"/dev/sd"); strcpy(proc_file_name,"/proc/scsi/usb-storage-"); while(dev_c < 'e') { sprintf(dev_file_name,"%s%c","/dev/sd",dev_c); dev_fd = open(dev_file_name,O_RDWR); if(dev_fd > 0) { sprintf(sys_cmd,"cat %s%c/%c |grep Vendor\n","/proc/scsi/usb-storage-",proc_c,proc_c); system(sys_cmd); sprintf(sys_cmd,"cat %s%c/%c |grep Attached\n","/proc/scsi/usb-storage-",proc_c,proc_c); system(sys_cmd); close(dev_fd); if(flag == 1) { sprintf(sys_cmd,"mount %s1 %s\n",dev_file_name,RMT_OTG_DISK); system(sys_cmd); } system("mount |grep rmt\n"); result = 0; } dev_c++; proc_c++; } return result;}int umount_otg_rmt_disk(void) { sprintf(sys_cmd,"umount %s\n",RMT_OTG_DISK); system(sys_cmd); return 0;}int otg_host(void) { unsigned char timer_cnt = 0; long flags; otgfsm_fd = open(otgdev_file, O_RDWR); if(otgfsm_fd < 0) { printf("error in opening otg dev file: %s\n",otgdev_file); usage(); } /* Get the stae of the OTG FSM */ get_otg_info.state = OTG_INV_STATE; ioctl(otgfsm_fd, OTG_IOC_GET_STATE, &get_otg_info); if(get_otg_info.state != OTG_A_HOST && get_otg_info.state != OTG_B_HOST) { /* Set the state to OTG Host */ fcntl(otgfsm_fd, F_SETOWN, getpid()); flags = fcntl(otgfsm_fd, F_GETFL); fcntl(otgfsm_fd, F_SETFL, (flags|FASYNC)); signal(SIGIO, &sigio_handler); signal(SIGINT, &sigint_handler); set_otg_info.state = OTG_HOST; ioctl(otgfsm_fd, OTG_IOC_SET_STATE, &set_otg_info); ioctl(otgfsm_fd, OTG_IOC_GET_STATE, &get_otg_info); /* Wait for the signal to come */ timer_cnt = OTG_TIMEOUT; do{ sleep(1); timer_cnt--; } while((get_otg_info.state != OTG_A_HOST) && (timer_cnt) && (get_otg_info.state != OTG_B_HOST) && (get_otg_info.state != OTG_A_VBUS_ERR)); timer_cnt = OTG_TIMEOUT; while((get_otg_info.status_code != OTG_STATUS_ENUM_SUCCESS) && (timer_cnt)) { sleep(1); timer_cnt--; } flags = fcntl(otgfsm_fd, F_GETFL); fcntl(otgfsm_fd, F_SETFL, (flags&(~FASYNC))); if(get_otg_info.state == OTG_A_VBUS_ERR) { close(otgfsm_fd); return otg_clear_error(); } if(timer_cnt == 0) { close(otgfsm_fd); return otg_idle(); } } close(otgfsm_fd); if(get_otg_info.state >= OTG_A_LAST_STATE) get_otg_info.state = 10; printf(" OTG State = %s\n", otg_state_name[get_otg_info.state]); return 0;}int otg_idle(void) { unsigned char timer_cnt = 0; long flags; otgfsm_fd = open(otgdev_file, O_RDWR); if(otgfsm_fd < 0) { printf("error in opening otg dev file: %s\n",otgdev_file); usage(); } /* Get the stae of the OTG FSM */ get_otg_info.state = OTG_INV_STATE; ioctl(otgfsm_fd, OTG_IOC_GET_STATE, &get_otg_info); /* Set the state to OTG Host */ fcntl(otgfsm_fd, F_SETOWN, getpid()); flags = fcntl(otgfsm_fd, F_GETFL); fcntl(otgfsm_fd, F_SETFL, (flags|FASYNC)); signal(SIGIO, &sigio_handler); signal(SIGINT, &sigint_handler); set_otg_info.state = OTG_IDLE; ioctl(otgfsm_fd, OTG_IOC_SET_STATE, &set_otg_info); /* Wait for the signal to come */ /* Add a timeout later */ timer_cnt = OTG_TIMEOUT; do{ sleep(1); timer_cnt--; } while((get_otg_info.state != OTG_A_IDLE) && (timer_cnt) && (get_otg_info.state != OTG_B_IDLE)); flags = fcntl(otgfsm_fd, F_GETFL); fcntl(otgfsm_fd, F_SETFL, (flags&(~FASYNC))); close(otgfsm_fd); if(get_otg_info.state >= OTG_A_LAST_STATE) get_otg_info.state = 10; printf(" OTG State = %s\n", otg_state_name[get_otg_info.state]); return 0;}int otg_clear_error(void) { unsigned char timer_cnt = 0; long flags; otgfsm_fd = open(otgdev_file, O_RDWR); if(otgfsm_fd < 0) { printf("error in opening otg dev file: %s\n",otgdev_file); usage(); } /* Get the stae of the OTG FSM */ get_otg_info.state = OTG_INV_STATE; ioctl(otgfsm_fd, OTG_IOC_GET_STATE, &get_otg_info); if(get_otg_info.state == OTG_A_VBUS_ERR) { /* Set the state to OTG Host */ fcntl(otgfsm_fd, F_SETOWN, getpid()); flags = fcntl(otgfsm_fd, F_GETFL); fcntl(otgfsm_fd, F_SETFL, (flags|FASYNC)); signal(SIGIO, &sigio_handler); signal(SIGINT, &sigint_handler); set_otg_info.state = OTG_BUS_DROP; ioctl(otgfsm_fd, OTG_IOC_SET_STATE, &set_otg_info); /* Wait for the signal to come */ /* Add a timeout later */ timer_cnt = OTG_TIMEOUT; do{ sleep(1); timer_cnt--; } while((get_otg_info.state != OTG_A_IDLE) && (timer_cnt) && (get_otg_info.state != OTG_B_IDLE)); flags = fcntl(otgfsm_fd, F_GETFL); fcntl(otgfsm_fd, F_SETFL, (flags&(~FASYNC))); } close(otgfsm_fd); if(get_otg_info.state >= OTG_A_LAST_STATE) get_otg_info.state = 10; printf(" OTG State = %s\n", otg_state_name[get_otg_info.state]); return 0;}int otg_status(void) { FILE *fp; char str[128]; unsigned char flag = 0; otgfsm_fd = open(otgdev_file, O_RDWR); if(otgfsm_fd < 0) { printf("error in opening otg dev file: %s\n",otgdev_file); usage(); } /* Get the stae of the OTG FSM */ get_otg_info.state = OTG_INV_STATE; ioctl(otgfsm_fd, OTG_IOC_GET_STATE, &get_otg_info); close(otgfsm_fd); if(get_otg_info.state >= OTG_A_LAST_STATE) get_otg_info.state = 10; printf("\n%s: OTG %c device State - %s\n", program_name, (get_otg_info.id) ? 'B' : 'A', otg_state_name[get_otg_info.state]); /* Get the mount points */ fp = fopen("/proc/mounts","r"); while(!feof(fp)) { fgets(str,128,fp); if(strstr(str,"/mnt/localdisk")!=NULL) { flag |= 0x01; } if(strstr(str,"/mnt/rmtdisk")!=NULL) { flag |= 0x10; } } if(flag&0x01) printf("%s: OTG local disk mounted on - %s\n",program_name,LOCAL_OTG_DISK); else printf("%s: OTG local disk mounted on - none\n",program_name); if(flag&0x10) printf("%s: OTG remote disk mounted on - %s\n\n",program_name,RMT_OTG_DISK); else printf("%s: OTG remote disk mounted on - none\n\n",program_name); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -