annotate libuwrap/find_busdev.c @ 10:fd3fcba5a8ac

libuwrap: implement locating by bus/dev
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 07 Sep 2023 04:18:30 +0000
parents libuwrap/find_matchspec.c@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
10
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
3 * by bus and device number/filename strings.
9
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <usb.h>
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "find_dev.h"
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "prelim_init.h"
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
10
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
14 struct usb_device *
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
15 find_usbdev_by_busdev(const char *bus_dev_spec)
9
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 {
10
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
17 const char *slash;
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
18 unsigned bus_name_len;
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
19 struct usb_bus *bus;
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
20 struct usb_device *dev;
9
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
10
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
22 slash = index(bus_dev_spec, '/');
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
23 if (!slash) {
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
24 fprintf(stderr,
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
25 "error: bus/dev path expected, but no slash found\n");
9
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 exit(1);
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 }
10
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
28 bus_name_len = slash - bus_dev_spec;
9
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 libusb_prelim_init();
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 for (bus = usb_get_busses(); bus; bus = bus->next) {
10
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
31 if (strlen(bus->dirname) != bus_name_len)
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
32 continue;
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
33 if (strncmp(bus->dirname, bus_dev_spec, bus_name_len))
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
34 continue;
9
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 for (dev = bus->devices; dev; dev = dev->next) {
10
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
36 if (!strcmp(dev->filename, slash + 1))
fd3fcba5a8ac libuwrap: implement locating by bus/dev
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
37 return dev;
9
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 return 0;
ab506f6aa57c libuwrap started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }