FreeCalypso > hg > fc-usbser-tools
annotate libuwrap/find_busdev.c @ 15:e1629a7c8ae3
top Makefile: add new local libraries
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 07 Sep 2023 08:11:12 +0000 |
parents | fd3fcba5a8ac |
children |
rev | line source |
---|---|
9 | 1 /* |
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 | 4 */ |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <usb.h> | |
11 #include "find_dev.h" | |
12 #include "prelim_init.h" | |
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 | 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 | 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 | 26 exit(1); |
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 | 29 libusb_prelim_init(); |
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 | 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 | 38 } |
39 } | |
40 return 0; | |
41 } |