annotate duart28/find_usb.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 b9ecfa54fe2b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements the step of locating a connected FreeCalypso DUART28
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * device (either C or S) at the libusb level, without booting off the kernel
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * driver and claiming the interface yet.
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <usb.h>
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "../libuwrap/prelim_init.h"
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
14 unsigned usb_presenting_pid;
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
15
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 static void
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 get_string(usb_dev_handle *usbh, int index, char *buf, size_t buflen)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 int rc;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 rc = usb_get_string_simple(usbh, index, buf, buflen);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (rc <= 0) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 fprintf(stderr, "error: USB string retrieval failed\n");
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 static int
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 is_match(struct usb_device *dev)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 struct usb_device_descriptor *desc = &dev->descriptor;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 usb_dev_handle *usbh;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 char strbuf[1024];
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (desc->idVendor != 0x0403)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 return 0;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (desc->idProduct != 0x6010 && desc->idProduct != 0x7152)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 return 0;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 usbh = usb_open(dev);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (!usbh) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 fprintf(stderr, "error: usb_open() failed\n");
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 get_string(usbh, desc->iManufacturer, strbuf, sizeof strbuf);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (strcmp(strbuf, "FreeCalypso")) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 usb_close(usbh);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 return 0;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 get_string(usbh, desc->iProduct, strbuf, sizeof strbuf);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (strcmp(strbuf, "DUART28C") && strcmp(strbuf, "DUART28S")) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 usb_close(usbh);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return 0;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 usb_close(usbh);
39
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
55 printf("Found FreeCalypso DUART28 adapter at bus %s device %s\n",
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
56 dev->bus->dirname, dev->filename);
b9ecfa54fe2b fc-duart28-conf: replace find and check-eeprom with single show command
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
57 usb_presenting_pid = desc->idProduct;
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 return 1;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 struct usb_device *
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 find_duart28_usbdev()
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 struct usb_bus *bus;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 struct usb_device *dev;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 libusb_prelim_init();
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 for (bus = usb_get_busses(); bus; bus = bus->next) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 for (dev = bus->devices; dev; dev = dev->next) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (is_match(dev))
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 return dev;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 return 0;
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }