4 * Copyright (c) 2000-2003 Johannes Erdfelt <johannes@erdfelt.com>
6 * This library is covered by the LGPL, read LICENSE for details.
9 #include <stdlib.h> /* getenv */
10 #include <stdio.h> /* stderr */
11 #include <string.h> /* strcmp */
17 struct usb_bus
*usb_busses
= NULL
;
19 int usb_find_busses(void)
21 struct usb_bus
*busses
, *bus
;
24 ret
= usb_os_find_busses(&busses
);
29 * Now walk through all of the busses we know about and compare against
30 * this new list. Any duplicates will be removed from the new list.
31 * If we don't find it in the new list, the bus was removed. Any
32 * busses still in the new list, are new to us.
37 struct usb_bus
*nbus
, *tbus
= bus
->next
;
41 struct usb_bus
*tnbus
= nbus
->next
;
43 if (!strcmp(bus
->dirname
, nbus
->dirname
)) {
44 /* Remove it from the new busses list */
45 LIST_DEL(busses
, nbus
);
56 /* The bus was removed from the system */
57 LIST_DEL(usb_busses
, bus
);
66 * Anything on the *busses list is new. So add them to usb_busses and
67 * process them like the new bus it is.
71 struct usb_bus
*tbus
= bus
->next
;
74 * Remove it from the temporary list first and add it to the real
77 LIST_DEL(busses
, bus
);
79 LIST_ADD(usb_busses
, bus
);
89 int usb_find_devices(void)
94 for (bus
= usb_busses
; bus
; bus
= bus
->next
) {
95 struct usb_device
*devices
, *dev
;
97 /* Find all of the devices and put them into a temporary list */
98 ret
= usb_os_find_devices(bus
, &devices
);
103 * Now walk through all of the devices we know about and compare
104 * against this new list. Any duplicates will be removed from the new
105 * list. If we don't find it in the new list, the device was removed.
106 * Any devices still in the new list, are new to us.
111 struct usb_device
*ndev
, *tdev
= dev
->next
;
115 struct usb_device
*tndev
= ndev
->next
;
117 if (!strcmp(dev
->filename
, ndev
->filename
)) {
118 /* Remove it from the new devices list */
119 LIST_DEL(devices
, ndev
);
130 /* The device was removed from the system */
131 LIST_DEL(bus
->devices
, dev
);
140 * Anything on the *devices list is new. So add them to bus->devices and
141 * process them like the new device it is.
145 struct usb_device
*tdev
= dev
->next
;
148 * Remove it from the temporary list first and add it to the real
151 LIST_DEL(devices
, dev
);
153 LIST_ADD(bus
->devices
, dev
);
156 * Some ports fetch the descriptors on scanning (like Linux) so we don't
157 * need to fetch them again.
160 usb_dev_handle
*udev
;
162 udev
= usb_open(dev
);
164 usb_fetch_and_parse_descriptors(udev
);
175 usb_os_determine_children(bus
);
181 void usb_set_debug(int level
)
183 if (usb_debug
|| level
)
184 fprintf(stderr
, "usb_set_debug: Setting debugging level to %d (%s)\n",
185 level
, level
? "on" : "off");
192 if (getenv("USB_DEBUG"))
193 usb_set_debug(atoi(getenv("USB_DEBUG")));
198 usb_dev_handle
*usb_open(struct usb_device
*dev
)
200 usb_dev_handle
*udev
;
202 udev
= malloc(sizeof(*udev
));
208 udev
->bus
= dev
->bus
;
209 udev
->config
= udev
->interface
= udev
->altsetting
= -1;
211 if (usb_os_open(udev
) < 0) {
219 int usb_get_string(usb_dev_handle
*dev
, int index
, int langid
, char *buf
,
223 * We can't use usb_get_descriptor() because it's lacking the index
224 * parameter. This will be fixed in libusb 1.0
226 return usb_control_msg(dev
, USB_ENDPOINT_IN
, USB_REQ_GET_DESCRIPTOR
,
227 (USB_DT_STRING
<< 8) + index
, langid
, buf
, buflen
, 1000);
230 int usb_get_string_simple(usb_dev_handle
*dev
, int index
, char *buf
, size_t buflen
)
232 char tbuf
[255]; /* Some devices choke on size > 255 */
233 int ret
, langid
, si
, di
;
236 * Asking for the zero'th index is special - it returns a string
237 * descriptor that contains all the language IDs supported by the
238 * device. Typically there aren't many - often only one. The
239 * language IDs are 16 bit numbers, and they start at the third byte
240 * in the descriptor. See USB 2.0 specification, section 9.6.7, for
241 * more information on this. */
242 ret
= usb_get_string(dev
, 0, 0, tbuf
, sizeof(tbuf
));
249 langid
= tbuf
[2] | (tbuf
[3] << 8);
251 ret
= usb_get_string(dev
, index
, langid
, tbuf
, sizeof(tbuf
));
255 if (tbuf
[1] != USB_DT_STRING
)
261 for (di
= 0, si
= 2; si
< tbuf
[0]; si
+= 2) {
262 if (di
>= (buflen
- 1))
265 if (tbuf
[si
+ 1]) /* high byte */
268 buf
[di
++] = tbuf
[si
];
276 int usb_close(usb_dev_handle
*dev
)
280 ret
= usb_os_close(dev
);
286 struct usb_device
*usb_device(usb_dev_handle
*dev
)
291 void usb_free_dev(struct usb_device
*dev
)
293 usb_destroy_configuration(dev
);
298 struct usb_bus
*usb_get_busses(void)
303 void usb_free_bus(struct usb_bus
*bus
)