annotate cp2102/find_dev.c @ 97:8d35346f1d46

cp2102 tools: accept "default" as device-selector
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 03:26:11 +0000
parents
children 1cacc1ae56f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
97
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * The function implemented in this module is common to all programs in
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * FC CP2102 tools suite that operate on physical hardware: it asks libuwrap
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * to locate either the default CP2102 VID:PID or a custom device selector
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * string.
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdio.h>
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <unistd.h>
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <usb.h>
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "../libuwrap/find_dev.h"
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 static const struct usbdev_matchspec default_id = {0x10C4, 0xEA60};
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 struct usb_device *
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 find_cp2102_device(devsel_arg)
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 char *devsel_arg;
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 {
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 struct usb_device *dev;
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (!strcmp(devsel_arg, "default")) {
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 dev = find_usbdev_by_matchspec(&default_id);
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (!dev) {
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 fprintf(stderr,
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 "error: no default-ID CP2102 device found\n");
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 exit(1);
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 } else {
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 dev = find_usbdev_by_desc_string(devsel_arg);
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (!dev) {
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 fprintf(stderr,
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 "error: specified USB device not found\n");
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 exit(1);
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 return dev;
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }