annotate libuwrap/find_matchspec.c @ 107:bc3367755586 default tip

add INSTALL document
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 21 Nov 2023 22:11:09 +0000
parents ab506f6aa57c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we implement the function that locates a USB device
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * by matchspec structure: looking for specific VID/PID, possibly qualified
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * by strings and/or index.
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <usb.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "find_dev.h"
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "prelim_init.h"
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 static void
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 get_string(usb_dev_handle *usbh, int index, char *buf, size_t buflen)
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 int rc;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 rc = usb_get_string_simple(usbh, index, buf, buflen);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 if (rc <= 0) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 fprintf(stderr, "error: USB string retrieval failed\n");
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 exit(1);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 static int
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 is_match(struct usb_device *dev, const struct usbdev_matchspec *match)
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 struct usb_device_descriptor *desc = &dev->descriptor;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 usb_dev_handle *usbh;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 char strbuf[1024];
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (desc->idVendor != match->usb_vid)
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 return 0;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (desc->idProduct != match->usb_pid)
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return 0;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (match->manuf_string || match->product_string || match->serial) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 usbh = usb_open(dev);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (!usbh) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 fprintf(stderr, "error: usb_open() failed\n");
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 exit(1);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (match->manuf_string) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 get_string(usbh, desc->iManufacturer,
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 strbuf, sizeof strbuf);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (strncmp(match->manuf_string,
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 strbuf, sizeof strbuf)) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 usb_close(usbh);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 return 0;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 if (match->product_string) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 get_string(usbh, desc->iProduct, strbuf, sizeof strbuf);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (strncmp(match->product_string,
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 strbuf, sizeof strbuf)) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 usb_close(usbh);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 return 0;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (match->serial) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 get_string(usbh, desc->iSerialNumber,
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 strbuf, sizeof strbuf);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (strncmp(match->serial, strbuf, sizeof strbuf)) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 usb_close(usbh);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 return 0;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 usb_close(usbh);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 return 1;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 struct usb_device *
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 find_usbdev_by_matchspec(const struct usbdev_matchspec *match)
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 struct usb_bus *bus;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 struct usb_device *dev;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 unsigned index = match->index;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 libusb_prelim_init();
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 for (bus = usb_get_busses(); bus; bus = bus->next) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 for (dev = bus->devices; dev; dev = dev->next) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 if (is_match(dev, match)) {
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (!index)
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 return dev;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 index--;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 return 0;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }