Initial Version
[qa-tools:mnts.git] / mnts-power-tests / src / check_usb_autosusp.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2009 Intel Corporation.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms and conditions of the GNU General Public License,
7 # version 2, as published by the Free Software Foundation.
8
9 # This program is distributed in the hope it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.  
13
14 # You should have received a copy of the GNU General Public License along with
15 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 # Place - Suite 330, Boston, MA 02111-1307 USA.
17
18 #
19 # Authors:
20 #       Gong, Zhipeng <zhipeng.gong@intel.com>
21
22
23 import sys
24 import usb
25
26
27 result = []
28 manual_suspend_child = 1
29 excluding_devices=['1:1']
30
31 def check_usbdev_susp(x):
32         print '\nfind usb device %s:%s %s at bus %s dev %s' % (vp[0], vp[1], x['name'], x['busnum'], x['devnum'])
33         if x['child'] != []:
34                 if usb.check_for_hub(x, manual_suspend_child) != 0:
35                         return -1               
36         if usb.prep_for_autosuspend(x) == 0:
37                 if usb.autosuspend_device(x) == 0 :     
38                         if usb.autoresume_device(x) == 0:
39                                 return 0
40                 elif ignore_fail_autosuspend == True:
41                         return 0
42         return -2
43
44 def is_excluding_devices(dev):
45         for x in excluding_devices:
46                 if dev['busnum'] == x.split(":")[0] and dev['devnum'] == x.split(":")[1] :
47                         return 1
48         return 0
49
50
51 if __name__ == '__main__':
52         if len(sys.argv) < 2:
53                 print 'usage: %s device_file_name [True]' % sys.argv[0]
54                 exit(-1)
55
56         global ignore_fail_autosuspend
57         if len(sys.argv) == 3 and sys.argv[2] == 'True':
58                 ignore_fail_autosuspend = True
59         else:
60                 ignore_fail_autosuspend = False
61                 
62         usb.init_usb_devices()
63
64         for idev in open(sys.argv[1]):
65                 vp = idev.strip().split(":")
66                 print vp
67                 if (len(vp) == 2):
68                         devs = usb.get_usbdev_by_id(vp[0], vp[1])               
69                         if  len(devs) != 0:
70                                 for x in devs:
71                                         if is_excluding_devices(x) == 0:
72                                                 result.append(check_usbdev_susp(x))
73                                         else:
74                                                 print 'device at bus %s dev %s is excluded' %(x['busnum'], x['devnum'])
75                         else:
76                                 print "can't find usbdevice %s:%s" % (vp[0], vp[1])
77                                 #result.append(-3)
78                                 
79         usb.restore_device_status()
80
81         for x in result:
82                 print x
83                 if x != 0:
84                         print 'error!!!'
85                         exit(1) 
86
87         exit(0)
88                         
89         
90
91