FreeCalypso > hg > fc-usbser-tools
comparison duart28/find_usb.c @ 27:2413a54a1bfc
fc-duart28-conf started
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 09 Sep 2023 17:53:21 +0000 |
parents | |
children | b9ecfa54fe2b |
comparison
equal
deleted
inserted
replaced
26:49239efbdcc8 | 27:2413a54a1bfc |
---|---|
1 /* | |
2 * This module implements the step of locating a connected FreeCalypso DUART28 | |
3 * device (either C or S) at the libusb level, without booting off the kernel | |
4 * driver and claiming the interface yet. | |
5 */ | |
6 | |
7 #include <stdio.h> | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <strings.h> | |
11 #include <usb.h> | |
12 #include "../libuwrap/prelim_init.h" | |
13 | |
14 static void | |
15 get_string(usb_dev_handle *usbh, int index, char *buf, size_t buflen) | |
16 { | |
17 int rc; | |
18 | |
19 rc = usb_get_string_simple(usbh, index, buf, buflen); | |
20 if (rc <= 0) { | |
21 fprintf(stderr, "error: USB string retrieval failed\n"); | |
22 exit(1); | |
23 } | |
24 } | |
25 | |
26 static void | |
27 report_found_dev(struct usb_device *dev, unsigned usb_pid, char string_letter) | |
28 { | |
29 printf("Found FreeCalypso DUART28 adapter at bus %s device %s,\n", | |
30 dev->bus->dirname, dev->filename); | |
31 if (usb_pid == 0x7152 && string_letter == 'C') | |
32 printf("presenting as DUART28C\n"); | |
33 else if (usb_pid == 0x6010 && string_letter == 'S') | |
34 printf("presenting as DUART28S\n"); | |
35 else | |
36 printf("presenting with inconsistent ID!\n"); | |
37 } | |
38 | |
39 static int | |
40 is_match(struct usb_device *dev) | |
41 { | |
42 struct usb_device_descriptor *desc = &dev->descriptor; | |
43 usb_dev_handle *usbh; | |
44 char strbuf[1024]; | |
45 | |
46 if (desc->idVendor != 0x0403) | |
47 return 0; | |
48 if (desc->idProduct != 0x6010 && desc->idProduct != 0x7152) | |
49 return 0; | |
50 usbh = usb_open(dev); | |
51 if (!usbh) { | |
52 fprintf(stderr, "error: usb_open() failed\n"); | |
53 exit(1); | |
54 } | |
55 get_string(usbh, desc->iManufacturer, strbuf, sizeof strbuf); | |
56 if (strcmp(strbuf, "FreeCalypso")) { | |
57 usb_close(usbh); | |
58 return 0; | |
59 } | |
60 get_string(usbh, desc->iProduct, strbuf, sizeof strbuf); | |
61 if (strcmp(strbuf, "DUART28C") && strcmp(strbuf, "DUART28S")) { | |
62 usb_close(usbh); | |
63 return 0; | |
64 } | |
65 usb_close(usbh); | |
66 report_found_dev(dev, desc->idProduct, strbuf[7]); | |
67 return 1; | |
68 } | |
69 | |
70 struct usb_device * | |
71 find_duart28_usbdev() | |
72 { | |
73 struct usb_bus *bus; | |
74 struct usb_device *dev; | |
75 | |
76 libusb_prelim_init(); | |
77 for (bus = usb_get_busses(); bus; bus = bus->next) { | |
78 for (dev = bus->devices; dev; dev = dev->next) { | |
79 if (is_match(dev)) | |
80 return dev; | |
81 } | |
82 } | |
83 return 0; | |
84 } |