@@ -59,14 +59,8 @@ void sleep_ms(int milliseconds)
59
59
#endif
60
60
}
61
61
62
- /* Max number of hub ports supported.
63
- * This is somewhat artificially limited by "-p" option parser.
64
- * If "-p" parser is improved, we can support up to 32 ports.
65
- * However, biggest number of ports on smart hub I've seen was 8.
66
- * I've also observed onboard USB hub with whopping 14 ports,
67
- * but that hub did not support per-port power switching.
68
- */
69
- #define MAX_HUB_PORTS 9
62
+ /* Max number of hub ports supported */
63
+ #define MAX_HUB_PORTS 14
70
64
#define ALL_HUB_PORTS ((1 << MAX_HUB_PORTS) - 1) /* bitmask */
71
65
72
66
#define USB_CTRL_GET_TIMEOUT 5000
@@ -266,6 +260,59 @@ static char* rtrim(char* str)
266
260
return str ;
267
261
}
268
262
263
+ /*
264
+ * Convert port list into bitmap.
265
+ * Following port list specifications are equivalent:
266
+ * 1,3,4,5,11,12,13
267
+ * 1,3-5,11-13
268
+ * Returns: bitmap of specified ports, max port is MAX_HUB_PORTS.
269
+ */
270
+
271
+ static int ports2bitmap (char * const portlist )
272
+ {
273
+ int ports = 0 ;
274
+ char * position = portlist ;
275
+ char * comma ;
276
+ char * dash ;
277
+ int len ;
278
+ int i ;
279
+ while (position ) {
280
+ char buf [8 ] = {0 };
281
+ comma = strchr (position , ',' );
282
+ len = sizeof (buf ) - 1 ;
283
+ if (comma ) {
284
+ if (len > comma - position )
285
+ len = comma - position ;
286
+ strncpy (buf , position , len );
287
+ position = comma + 1 ;
288
+ } else {
289
+ strncpy (buf , position , len );
290
+ position = NULL ;
291
+ }
292
+ /* Check if we have port range, e.g.: a-b */
293
+ int a = 0 , b = 0 ;
294
+ a = atoi (buf );
295
+ dash = strchr (buf , '-' );
296
+ if (dash ) {
297
+ b = atoi (dash + 1 );
298
+ } else {
299
+ b = a ;
300
+ }
301
+ if (a > b ) {
302
+ fprintf (stderr , "Bad port spec %d-%d, first port must be less than last\n" , a , b );
303
+ exit (1 );
304
+ }
305
+ if (a <= 0 || a > MAX_HUB_PORTS || b <= 0 || b > MAX_HUB_PORTS ) {
306
+ fprintf (stderr , "Bad port spec %d-%d, port numbers must be from 1 to %d\n" , a , b , MAX_HUB_PORTS );
307
+ exit (1 );
308
+ }
309
+ for (i = a ; i <=b ; i ++ ) {
310
+ ports |= (1 << (i - 1 ));
311
+ }
312
+ }
313
+ return ports ;
314
+ }
315
+
269
316
270
317
/*
271
318
* get USB hub properties.
@@ -694,16 +741,7 @@ int main(int argc, char *argv[])
694
741
break ;
695
742
}
696
743
if (strlen (optarg )) {
697
- /* parse port list */
698
- opt_ports = 0 ;
699
- size_t i ;
700
- for (i = 0 ; i < strlen (optarg ); i ++ ) {
701
- if (!isdigit (optarg [i ]) || optarg [i ] == '0' ) {
702
- printf ("%s must be list of ports 1 to %d\n" , optarg , MAX_HUB_PORTS );
703
- }
704
- int d = optarg [i ]- '1' ;
705
- opt_ports |= (1 << d );
706
- }
744
+ opt_ports = ports2bitmap (optarg );
707
745
}
708
746
break ;
709
747
case 'a' :
0 commit comments