FreeCalypso > hg > fc-usbser-tools
annotate libuwrap/find_desc_str.c @ 78:d46ea7a3fa0c
eeproms: add ftdi-chip and eeprom settings as appropriate
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 26 Sep 2023 01:37:33 +0000 |
parents | fe4231326fb2 |
children |
rev | line source |
---|---|
9 | 1 /* |
2 * In this module we implement the function that locates a USB device | |
11
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
3 * by libftdi-style description-string. |
9 | 4 */ |
5 | |
11
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
6 #include <ctype.h> |
9 | 7 #include <stdio.h> |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <strings.h> | |
11 #include <usb.h> | |
12 #include "find_dev.h" | |
13 | |
11
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
14 struct usb_device * |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
15 find_usbdev_by_desc_string(const char *desc_string) |
9 | 16 { |
11
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
17 struct usbdev_matchspec match; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
18 const char *cp; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
19 char *endp; |
9 | 20 |
11
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
21 if (!desc_string[0] || desc_string[1] != ':') { |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
22 inv_syntax: fprintf(stderr, |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
23 "error: USB device selector string has invalid syntax\n"); |
9 | 24 exit(1); |
25 } | |
11
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
26 switch (desc_string[0]) { |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
27 case 'd': |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
28 return find_usbdev_by_busdev(desc_string + 2); |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
29 case 'i': |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
30 case 's': |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
31 break; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
32 default: |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
33 goto inv_syntax; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
34 } |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
35 bzero(&match, sizeof match); |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
36 cp = desc_string + 2; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
37 if (!isdigit(*cp)) |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
38 goto inv_syntax; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
39 match.usb_vid = strtoul(cp, &endp, 0); |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
40 cp = endp; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
41 if (*cp++ != ':') |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
42 goto inv_syntax; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
43 if (!isdigit(*cp)) |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
44 goto inv_syntax; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
45 match.usb_pid = strtoul(cp, &endp, 0); |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
46 cp = endp; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
47 if (!*cp) { |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
48 if (desc_string[0] != 'i') |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
49 goto inv_syntax; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
50 return find_usbdev_by_matchspec(&match); |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
51 } |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
52 if (*cp++ != ':') |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
53 goto inv_syntax; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
54 switch (desc_string[0]) { |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
55 case 'i': |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
56 if (!isdigit(*cp)) |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
57 goto inv_syntax; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
58 match.index = strtoul(cp, &endp, 0); |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
59 if (*endp) |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
60 goto inv_syntax; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
61 break; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
62 case 's': |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
63 match.serial = cp; |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
64 } |
fe4231326fb2
libuwrap: implement locating by description-string
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
65 return find_usbdev_by_matchspec(&match); |
9 | 66 } |