📄 otgtool.h
字号:
/************************************************************************
* Philips ISP1362 otg demo tools
*
* (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: otgtool.h
*
* History:
*
* Version Date Author Comments
* -------------------------------------------------
* 0.9 06/26/02 SYARRA Initial Creation
* 1.21 08/04/03 SYARRA OPT1.2 compliance
*
* Note: use tab space 4
************************************************************************/
#ifndef __OTGTOOL_H__
#define __OTGTOOL_H__
#include <errno.h>
#include "usb_otg.h"
#include "msbridge.h"
#include "disk_emu.h"
#define LOCAL_OTG_DEV_FILE MSDISK_FILE
#define RMT_OTG_DEV_FILE "/dev/sda"
#define LOCAL_OTG_DISK "/mnt/localdisk"
#define RMT_OTG_DISK "/mnt/rmtdisk"
#define OTG_FSM_FILE "/dev/usb/otg/otgfsm0"
#define OTG_DEV_DIR "/dev/usb/otg"
#define USBSLAVE_DEV_FILE "/dev/usb/otg/otgdev0"
extern int otgtool_host(int otg_fd, usb_otg_info_t *get_info,
int (*otg_display) (__const char *__restrict __format, ...));
extern int otgtool_idle(int otg_fd, usb_otg_info_t *get_info,
int (*otg_display) (__const char *__restrict __format, ...));
extern int otgtool_clear_error(int otg_fd, usb_otg_info_t *get_info,
int (*otg_display) (__const char *__restrict __format, ...));
extern int otgtool_mount_local_disk(char *sys_cmd);
extern int otgtool_umount_local_disk(char *sys_cmd);
extern int otgtool_umount_rmt_disk(char *sys_cmd);
extern int otgtool_mount_rmt_disk(unsigned char flag, char *dev_file, char *proc_file, char *sys_cmd);
/* OTG TOOL Library */
/* Mount the local disk on /mnt/localdisk */
int otgtool_mount_local_disk(char *sys_cmd)
{
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);
return 0;
}
/* unmount the loacl disk mounted on /mnt/localdisk */
int otgtool_umount_local_disk(char *sys_cmd)
{
#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;
}
/* Unmount the remote device mounted on /mnt/rmtdisk */
int otgtool_umount_rmt_disk(char *sys_cmd)
{
sprintf(sys_cmd,"umount %s\n",RMT_OTG_DISK);
system(sys_cmd);
return 0;
}
/* mount the remote device, it mounts the first connected mass storage
* device with first partition */
int otgtool_mount_rmt_disk(unsigned char flag, char *dev_file, char *proc_file, char *sys_cmd)
{
char dev_c, proc_c;
int result = -1;
int dev_fd;
dev_c = 'a';
proc_c = '0';
strcpy(dev_file,"/dev/sd");
strcpy(proc_file,"/proc/scsi/usb-storage-");
while(dev_c < 'e') {
sprintf(dev_file,"%s%c","/dev/sd",dev_c);
dev_fd = open(dev_file,O_RDWR);
if(dev_fd > 0) {
#if 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);
#endif
close(dev_fd);
if(flag == 1) {
sprintf(sys_cmd,"mount %s1 %s\n",dev_file,RMT_OTG_DISK);
system(sys_cmd);
}
result = 0;
}
dev_c++;
proc_c++;
}
return result;
}
#define OTG_TIMEOUT 9;
/* SEt the OTG FSM State to HOST */
int otgtool_host(int otg_fd, usb_otg_info_t *get_info,
int (*otg_display) (__const char *__restrict __format, ...))
{
usb_otg_info_t set_otg_info;
unsigned char timer_cnt = 0;
int result = 0;
get_info->state = OTG_INV_STATE;
ioctl(otg_fd, OTG_IOC_GET_STATE, get_info);
if(get_info->state != OTG_A_HOST && get_info->state != OTG_B_HOST) {
otg_display("Trying to open session with the remote device\n");
}
set_otg_info.state = OTG_HOST;
ioctl(otg_fd, OTG_IOC_SET_STATE, &set_otg_info);
ioctl(otg_fd, OTG_IOC_GET_STATE, get_info);
/* Wait for the signal to come */
timer_cnt = OTG_TIMEOUT;
do{
sleep(1);
timer_cnt--;
} while((get_info->state != OTG_A_HOST) && (timer_cnt) &&
(get_info->state != OTG_B_HOST) && (get_info->state != OTG_A_VBUS_ERR));
if(((get_info->state == OTG_A_HOST) || (get_info->state == OTG_B_HOST)) &&
(get_info->status_code != OTG_STATUS_ENUM_SUCCESS)) {
/* Give little more time for enumeration */
timer_cnt = OTG_TIMEOUT;
while((get_info->state == OTG_A_HOST || get_info->state == OTG_B_HOST) &&
(get_info->status_code != OTG_STATUS_ENUM_SUCCESS) &&
(timer_cnt)) {
sleep(1);
timer_cnt--;
}
if((timer_cnt == 0)||((get_info->state != OTG_A_HOST) && (get_info->state != OTG_B_HOST))) {
otg_display("Open Session with remote device failed (enumeration) \n");
result = -ENODEV;
}
}
if(get_info->state == OTG_A_VBUS_ERR) {
otg_display("Open Session with remote device failed (overcurrent)\n");
otgtool_clear_error(otg_fd, get_info, otg_display);
return -1;
}
if(timer_cnt == 0) {
if(result == 0) otg_display("Open Session with remote device failed (timeout) \n");
otgtool_idle(otg_fd, get_info, otg_display);
if(result == 0) result = -ETIMEDOUT;
return result;
}
otg_display("Open Session with remote device success\n");
return result;
}
/* Bring down the state of the OTG FSM to IDLE */
int otgtool_idle(int otg_fd, usb_otg_info_t *get_info,
int (*otg_display) (__const char *__restrict __format, ...))
{
usb_otg_info_t set_otg_info;
unsigned char timer_cnt = 0;
get_info->state = OTG_INV_STATE;
ioctl(otg_fd, OTG_IOC_GET_STATE, get_info);
otg_display("Closing the session with remote device\n");
set_otg_info.state = OTG_IDLE;
ioctl(otg_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_info->state != OTG_A_IDLE) && (timer_cnt) &&
(get_info->state != OTG_B_IDLE));
return 0;
}
/* Clear VBUS error condition on the OTG cable */
int otgtool_clear_error(int otg_fd, usb_otg_info_t *get_info,
int (*otg_display) (__const char *__restrict __format, ...))
{
usb_otg_info_t set_otg_info;
unsigned char timer_cnt = 0;
/* Get the stae of the OTG FSM */
get_info->state = OTG_INV_STATE;
ioctl(otg_fd, OTG_IOC_GET_STATE, get_info);
if(get_info->state == OTG_A_VBUS_ERR) {
/* Set the state to OTG Host */
set_otg_info.state = OTG_BUS_DROP;
ioctl(otg_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_info->state != OTG_A_IDLE) && (timer_cnt) &&
(get_info->state != OTG_B_IDLE));
}
return 0;
}
#endif /* __OTGTOOL_H__ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -