⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 otgcmd.c

📁 philips公司ISP1362 USB OTG控制芯片的驱动
💻 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;



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) {

	otgtool_mount_local_disk(sys_cmd);
	system("mount |grep local\n");

	return 0;
}

int umount_otg_local_disk(void) {

	otgtool_umount_local_disk(sys_cmd);

	return 0;
}

int	mount_otg_rmt_disk(unsigned char	flag) {

	if(otgtool_mount_rmt_disk(flag, dev_file_name, proc_file_name,sys_cmd) == 0) {
		system("mount | grep rmt\n");
		return 0;
	}

	return -1;
}

int	umount_otg_rmt_disk(void) {

	otgtool_umount_rmt_disk(sys_cmd);

	return 0;
}

int otg_host(void) {

	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);


		otgtool_host(otgfsm_fd, &get_otg_info, printf);
	}

	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) {

	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();
	}

	/* 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);

	otgtool_idle(otgfsm_fd, &get_otg_info, printf);

	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) {

	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);

		otgtool_clear_error(otgfsm_fd, &get_otg_info, printf);

		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 + -