1.1 --- a/test.c Fri Mar 01 23:38:54 2013 +0000
1.2 +++ b/test.c Mon Mar 04 19:27:25 2013 +0000
1.3 @@ -586,6 +586,43 @@
1.4 setup[7] = length >> 8;
1.5 }
1.6
1.7 +uint8_t usb_descriptor_size(uint8_t type)
1.8 +{
1.9 + switch (type)
1.10 + {
1.11 + case USB_DT_DEVICE: return USB_DT_DEVICE_SIZE;
1.12 + case USB_DT_CONFIG: return USB_DT_CONFIG_SIZE;
1.13 + case USB_DT_STRING: return sizeof(struct usb_string_descriptor);
1.14 + case USB_DT_INTERFACE: return USB_DT_INTERFACE_SIZE;
1.15 + case USB_DT_ENDPOINT: return USB_DT_ENDPOINT_SIZE;
1.16 + default: return 0;
1.17 + }
1.18 +}
1.19 +
1.20 +uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index)
1.21 +{
1.22 + static uint8_t data[64];
1.23 + uint8_t len = 64, setup[8];
1.24 +
1.25 + printf("Sending descriptor request to address %d, endpoint 0...\n", device->address);
1.26 +
1.27 + max_write(MAX_REG_PERADDR, device->address);
1.28 +
1.29 + setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index, 64);
1.30 + max_control(setup);
1.31 + if (!max_control_input(data, &len, device))
1.32 + {
1.33 + printf("Failed.\n");
1.34 + return NULL;
1.35 + }
1.36 + max_control_status(true);
1.37 +
1.38 + if (len >= usb_descriptor_size(type))
1.39 + return data;
1.40 + else
1.41 + return NULL;
1.42 +}
1.43 +
1.44 /**
1.45 * Perform initialisation on a device, obtaining the device details and storing
1.46 * this information in the device structure, returning true if successful and
1.47 @@ -593,23 +630,19 @@
1.48 */
1.49 bool max_init_device(max_device *device)
1.50 {
1.51 - uint8_t data[64], len = 64, setup[8];
1.52 + struct usb_device_descriptor *desc;
1.53
1.54 printf("Sending control request to address 0, endpoint 0...\n");
1.55
1.56 - max_write(MAX_REG_PERADDR, 0);
1.57 + device->address = 0;
1.58
1.59 /* Send a "get descriptor" request for the device descriptor. */
1.60
1.61 - setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(USB_DT_DEVICE), 0, USB_DT_DEVICE_SIZE);
1.62 - max_control(setup);
1.63 - if (!max_control_input(data, &len, device))
1.64 - return false;
1.65 - max_control_status(true);
1.66 + desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0);
1.67
1.68 - if (len >= sizeof(struct usb_device_descriptor))
1.69 + if (desc != NULL)
1.70 {
1.71 - memcpy(&device->desc, data, sizeof(struct usb_device_descriptor));
1.72 + memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor));
1.73 device->desc.bcdUSB = le16toh(device->desc.bcdUSB);
1.74 device->desc.idVendor = le16toh(device->desc.idVendor);
1.75 device->desc.idProduct = le16toh(device->desc.idProduct);
1.76 @@ -657,37 +690,20 @@
1.77 max_control_status(false);
1.78 }
1.79
1.80 -
1.81 -void max_get_descriptor(max_device *device)
1.82 +void usb_show_languages(uint8_t *data)
1.83 {
1.84 - uint8_t data[64], len = 64, setup[8];
1.85 - struct usb_string_descriptor *desc;
1.86 + struct usb_string_descriptor *desc = (struct usb_string_descriptor *) data;
1.87 uint16_t *lang;
1.88
1.89 - printf("Sending descriptor request to address %d, endpoint 0...\n", device->address);
1.90 -
1.91 - max_write(MAX_REG_PERADDR, device->address);
1.92 -
1.93 - setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(USB_DT_STRING), 0, 64);
1.94 - max_control(setup);
1.95 - if (!max_control_input(data, &len, device))
1.96 - {
1.97 - printf("Failed.\n");
1.98 + if (data == NULL)
1.99 return;
1.100 - }
1.101 - max_control_status(true);
1.102
1.103 - if (len >= sizeof(struct usb_string_descriptor))
1.104 - {
1.105 - desc = (struct usb_string_descriptor *) data;
1.106 + printf("bLength: %d\n", desc->bLength);
1.107 + printf("bDescriptorType: %d\n", desc->bDescriptorType);
1.108
1.109 - printf("bLength: %d\n", desc->bLength);
1.110 - printf("bDescriptorType: %d\n", desc->bDescriptorType);
1.111 -
1.112 - for (lang = desc->wData; lang < (uint16_t *) (data + desc->bLength); lang++)
1.113 - {
1.114 - printf("wLangId: %04x\n", le16toh(*lang));
1.115 - }
1.116 + for (lang = desc->wData; lang < (uint16_t *) (data + desc->bLength); lang++)
1.117 + {
1.118 + printf("wLangId: %04x\n", le16toh(*lang));
1.119 }
1.120 }
1.121
1.122 @@ -866,7 +882,7 @@
1.123 max_write(MAX_REG_MODE, max_read(MAX_REG_MODE, NULL) & ~MAX_MODE_SOFKAENAB);
1.124 max_set_address(&device);
1.125 devstate = MAX_DEVSTATE_READY;
1.126 - max_get_descriptor(&device);
1.127 + usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0));
1.128 printf("READY\n");
1.129 }
1.130