+ private long find_usb_descriptors() {
+ AltosHexsym usb_descriptors = lookup_symbol("ao_usb_descriptors");
+ long a;
+
+ if (usb_descriptors == null)
+ return -1;
+
+ /* Walk the descriptors looking for the device */
+ a = usb_descriptors.address;
+ while (get_u8(a+1) != AO_USB_DESC_DEVICE) {
+ int delta = get_u8(a);
+ a += delta;
+ if (delta == 0 || a >= max_address)
+ return -1;
+ }
+ return a;
+ }
+
+ public AltosUsbId find_usb_id() {
+ long a = find_usb_descriptors();
+
+ if (a == -1)
+ return null;
+
+ /* Walk the descriptors looking for the device */
+ while (get_u8(a+1) != AO_USB_DESC_DEVICE) {
+ int delta = get_u8(a);
+ a += delta;
+ if (delta == 0 || a >= max_address)
+ return null;
+ }
+
+ return new AltosUsbId(get_u16(a + 8),
+ get_u16(a + 10));
+ }
+
+ public String find_usb_product() {
+ long a = find_usb_descriptors();
+ int num_strings;
+ int product_string;
+
+ if (a == -1)
+ return null;
+
+ product_string = get_u8(a+15);
+
+ /* Walk the descriptors looking for the device */
+ num_strings = 0;
+ for (;;) {
+ if (get_u8(a+1) == AO_USB_DESC_STRING) {
+ ++num_strings;
+ if (num_strings == product_string + 1)
+ break;
+ }
+
+ int delta = get_u8(a);
+ a += delta;
+ if (delta == 0 || a >= max_address)
+ return null;
+ }
+
+ int product_len = get_u8(a);
+
+ System.out.printf("Product is at %x length %d\n", a, product_len);
+
+ for (int i = 0; i < product_len; i++)
+ System.out.printf(" %2d: %02x\n", i, get_u8(a+i));
+
+ if (product_len <= 0)
+ return null;
+
+ String product = "";
+
+ for (int i = 0; i < product_len - 2; i += 2) {
+ int c = get_u16(a + 2 + i);
+
+ System.out.printf("character %x\n", c);
+
+ product += Character.toString((char) c);
+ }
+
+ System.out.printf("product %s\n", product);
+
+ return product;
+ }
+