Matthew.Shyu | 55f40e9 | 2014-07-18 11:47:47 +0800 | [diff] [blame^] | 1 | /* main.c */
|
| 2 |
|
| 3 | #include <stdio.h>
|
| 4 | #include <stdlib.h>
|
| 5 | #include <errno.h>
|
| 6 | #include <string.h>
|
| 7 | #include <sys/stat.h>
|
| 8 | #include <sys/types.h>
|
| 9 |
|
| 10 | #include <fcntl.h>
|
| 11 | #include <dirent.h>
|
| 12 | #include "porting.h"
|
| 13 |
|
| 14 | #define LOG_TAG "USBtestpm"
|
| 15 |
|
| 16 | #include "cutils/log.h"
|
| 17 | #include "cutils/properties.h"
|
| 18 |
|
| 19 | #include "usbctrl.h"
|
| 20 |
|
| 21 |
|
| 22 | int usbcheck(int index)
|
| 23 | {
|
| 24 | int rc = 0;
|
| 25 | rc = usbpower(index,USB_CMD_IF);
|
| 26 | return rc;
|
| 27 | }
|
| 28 |
|
| 29 | int usbset(int index,int cmd)
|
| 30 | {
|
| 31 | int rc = 0;
|
| 32 | rc = usbpower(index,cmd);
|
| 33 | return rc;
|
| 34 | }
|
| 35 |
|
| 36 | static const char USBA_DEVICE_PATH[] = "/sys/devices/lm0";
|
| 37 | static const char USBB_DEVICE_PATH[] = "/sys/devices/lm1";
|
| 38 |
|
| 39 | int check_usb_devices_exists(int port)
|
| 40 | {
|
| 41 | if (port == 0) {
|
| 42 | if (access(USBA_DEVICE_PATH, R_OK) == 0) {
|
| 43 | return 0;
|
| 44 | } else {
|
| 45 | return -1;
|
| 46 | }
|
| 47 | } else {
|
| 48 | if (access(USBB_DEVICE_PATH, R_OK) == 0) {
|
| 49 | return 0;
|
| 50 | } else {
|
| 51 | return -1;
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | }
|
| 56 |
|
| 57 | int main(int argc, char * argv[])
|
| 58 | {
|
| 59 | int usb_a_exist = 0;
|
| 60 | int usb_b_exist = 0;
|
| 61 | int a_on_flag,b_on_flag;
|
| 62 | int ret;
|
| 63 | char * parastr=NULL;
|
| 64 | int para_a=1,para_b=1;
|
| 65 |
|
| 66 | parastr = argv[1];
|
| 67 |
|
| 68 | if(parastr!=NULL)
|
| 69 | {
|
| 70 | if(strcmp(parastr, "AB")==0)
|
| 71 | {
|
| 72 | para_a = 1;
|
| 73 | para_b = 1;
|
| 74 | }
|
| 75 | else if(strcmp(parastr, "A")==0)
|
| 76 | {
|
| 77 | para_a = 1;
|
| 78 | para_b = 0;
|
| 79 | }
|
| 80 | else if(strcmp(parastr, "B")==0)
|
| 81 | {
|
| 82 | para_a = 0;
|
| 83 | para_b = 1;
|
| 84 | }
|
| 85 | }
|
| 86 | ret = get_dwc_driver_version();
|
| 87 |
|
| 88 | if(ret == -1)
|
| 89 | {
|
| 90 | printf("This dwc_otg version not support. Please check!\n");
|
| 91 | return -1;
|
| 92 | }
|
| 93 |
|
| 94 | if(para_a && (check_usb_devices_exists(0) == 0))
|
| 95 | {
|
| 96 | usb_a_exist = 1;
|
| 97 | a_on_flag = 1;
|
| 98 | }
|
| 99 | else
|
| 100 | usbset(0,USB_CMD_OFF);
|
| 101 |
|
| 102 | if (para_b &&(check_usb_devices_exists(1) == 0))
|
| 103 | {
|
| 104 | usb_b_exist = 1;
|
| 105 | b_on_flag = 1;
|
| 106 | }
|
| 107 | else
|
| 108 | usbset(1,USB_CMD_OFF);
|
| 109 |
|
| 110 | if (usb_a_exist || usb_b_exist) {
|
| 111 | while(1) {
|
| 112 | if (usb_a_exist) {
|
| 113 | if(a_on_flag == 0)
|
| 114 | {
|
| 115 | usbset(0,USB_CMD_ON);
|
| 116 | a_on_flag = 1;
|
| 117 | usleep(500000);
|
| 118 | if (usbcheck(0) != 1)
|
| 119 | {
|
| 120 | usbset(0,USB_CMD_OFF);
|
| 121 | a_on_flag = 0;
|
| 122 | }
|
| 123 | }
|
| 124 | else
|
| 125 | {
|
| 126 | if (usbcheck(0) != 1)
|
| 127 | {
|
| 128 | usbset(0,USB_CMD_OFF);
|
| 129 | a_on_flag = 0;
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | if (usb_b_exist) {
|
| 135 | if(b_on_flag == 0)
|
| 136 | {
|
| 137 | usbset(1,USB_CMD_ON);
|
| 138 | b_on_flag = 1;
|
| 139 | usleep(500000);
|
| 140 | if (usbcheck(1) != 1)
|
| 141 | {
|
| 142 | usbset(1,USB_CMD_OFF);
|
| 143 | b_on_flag = 0;
|
| 144 | }
|
| 145 | }
|
| 146 | else
|
| 147 | {
|
| 148 | if (usbcheck(1) != 1)
|
| 149 | {
|
| 150 | usbset(1,USB_CMD_OFF);
|
| 151 | b_on_flag = 0;
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | usleep(500000); //
|
| 157 |
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | }
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|