1.1 --- a/test.c Wed Mar 13 17:11:10 2013 +0000
1.2 +++ b/test.c Thu Mar 14 23:26:31 2013 +0000
1.3 @@ -341,7 +341,7 @@
1.4 * the given length, indicating the preserved toggle state of the endpoint
1.5 * (which will be updated).
1.6 */
1.7 -uint8_t max_send(uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle)
1.8 +uint8_t max_send_packet(uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle)
1.9 {
1.10 uint8_t status, hrsl = 0;
1.11
1.12 @@ -375,13 +375,12 @@
1.13
1.14 /**
1.15 * Make a request for data from the given endpoint, collecting it in the
1.16 - * supplied buffer with the given length, indicating the preserved toggle state
1.17 - * of the endpoint (which will be updated). The length will be updated to
1.18 - * indicate the total length of the received data.
1.19 + * supplied buffer, updating the given length of data received, and recording
1.20 + * the data within the given limit, also indicating the preserved toggle state
1.21 + * of the endpoint (which will be updated).
1.22 */
1.23 -bool max_recv(uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle)
1.24 +bool max_recv_packet(uint8_t endpoint, uint8_t **data, uint8_t *received, uint8_t *datalimit, bool *toggle)
1.25 {
1.26 - uint8_t *datalimit = data + *len;
1.27 uint8_t status, hrsl = 0;
1.28 uint16_t attempt = 1024;
1.29
1.30 @@ -410,7 +409,7 @@
1.31
1.32 while (status & MAX_HIRQ_RCVDAVIRQ)
1.33 {
1.34 - max_read_fifo(&data, len, datalimit);
1.35 + max_read_fifo(data, received, datalimit);
1.36
1.37 /* Indicate that all data has been read. */
1.38
1.39 @@ -424,6 +423,26 @@
1.40 }
1.41
1.42 /**
1.43 + * Make a request for data from the given endpoint, collecting it in the
1.44 + * supplied buffer, updating the given length of data received, and also
1.45 + * indicating the preserved toggle state of the endpoint (which will be updated).
1.46 + */
1.47 +bool max_recv(uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle)
1.48 +{
1.49 + uint8_t *datalimit = data + *len;
1.50 +
1.51 + *len = 0;
1.52 +
1.53 + while (max_recv_packet(endpoint, &data, len, datalimit, toggle))
1.54 + {
1.55 + if (data == datalimit)
1.56 + return true;
1.57 + }
1.58 +
1.59 + return false;
1.60 +}
1.61 +
1.62 +/**
1.63 * Send a control request consisting of the given setup data.
1.64 */
1.65 uint8_t max_control(uint8_t *setup)
1.66 @@ -603,20 +622,15 @@
1.67 }
1.68
1.69 /**
1.70 - * Get a descriptor from the device having the given type, value and index.
1.71 + * Get a descriptor from the device having the given type, value, index and length.
1.72 * The descriptor buffer is static and must be copied if it is to be preserved.
1.73 */
1.74 -uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index, bool initial)
1.75 +uint8_t *max_get_descriptor_with_length(max_device *device, uint8_t type, uint8_t value, uint8_t index, uint8_t length)
1.76 {
1.77 - static uint8_t data[64];
1.78 - uint8_t len = 64, setup[8];
1.79 + static uint8_t data[1024];
1.80 + uint8_t len = length, setup[8];
1.81
1.82 - printf("Sending descriptor request to address %d, endpoint 0...\n", device->address);
1.83 -
1.84 - max_write(MAX_REG_PERADDR, device->address);
1.85 -
1.86 - setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index,
1.87 - initial ? 8 : 64);
1.88 + setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index, length);
1.89
1.90 max_control(setup);
1.91 if (!max_control_input(data, &len, device))
1.92 @@ -626,23 +640,71 @@
1.93 }
1.94 max_control_status(true);
1.95
1.96 - if (len >= usb_descriptor_size(type))
1.97 + if (len >= length)
1.98 return data;
1.99 else
1.100 {
1.101 - printf("Expected %d but received %d.\n", usb_descriptor_size(type), len);
1.102 + printf("Expected %d but received %d.\n", length, len);
1.103 return NULL;
1.104 }
1.105 }
1.106
1.107 /**
1.108 + * Get a descriptor from the device having the given type, value, index and
1.109 + * length. If the length is given as zero, the descriptor header will first be
1.110 + * received and then the full descriptor will be retrieved.
1.111 + * The descriptor buffer is static and must be copied if it is to be preserved.
1.112 + */
1.113 +uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index, uint8_t length)
1.114 +{
1.115 + uint8_t *data;
1.116 +
1.117 + printf("Sending descriptor request to address %d, endpoint 0...\n", device->address);
1.118 +
1.119 + max_write(MAX_REG_PERADDR, device->address);
1.120 +
1.121 + if (!length)
1.122 + {
1.123 + data = max_get_descriptor_with_length(device, type, value, index, usb_descriptor_size(type));
1.124 + if (data == NULL)
1.125 + return NULL;
1.126 +
1.127 + if (type == USB_DT_CONFIG)
1.128 + length = le16toh(((struct usb_config_descriptor *) data)->wTotalLength);
1.129 + else
1.130 + length = ((struct usb_descriptor_header *) data)->bLength;
1.131 +
1.132 + if (length == usb_descriptor_size(type))
1.133 + return data;
1.134 + }
1.135 +
1.136 + return max_get_descriptor_with_length(device, type, value, index, length);
1.137 +}
1.138 +
1.139 +bool usb_init_device(max_device *device, uint8_t *data)
1.140 +{
1.141 + struct usb_device_descriptor *desc = (struct usb_device_descriptor *) data;
1.142 +
1.143 + if (data == NULL)
1.144 + return false;
1.145 +
1.146 + memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor));
1.147 + device->desc.bcdUSB = le16toh(device->desc.bcdUSB);
1.148 + device->desc.idVendor = le16toh(device->desc.idVendor);
1.149 + device->desc.idProduct = le16toh(device->desc.idProduct);
1.150 + device->desc.bcdDevice = le16toh(device->desc.bcdDevice);
1.151 +
1.152 + return true;
1.153 +}
1.154 +
1.155 +/**
1.156 * Perform initialisation on a device, obtaining the device details and storing
1.157 * this information in the device structure, returning true if successful and
1.158 * false otherwise.
1.159 */
1.160 bool max_init_device(max_device *device)
1.161 {
1.162 - struct usb_device_descriptor *desc;
1.163 + uint8_t *data;
1.164
1.165 printf("Sending control request to address 0, endpoint 0...\n");
1.166
1.167 @@ -650,10 +712,11 @@
1.168
1.169 /* Send a "get descriptor" request for the device descriptor. */
1.170
1.171 - desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0, true);
1.172 + data = max_get_descriptor(device, USB_DT_DEVICE, 0, 0, 8);
1.173
1.174 - if (desc != NULL)
1.175 + if (data != NULL)
1.176 {
1.177 + usb_init_device(device, data);
1.178 device->max_packet_size = device->desc.bMaxPacketSize0;
1.179
1.180 /* Reset the device. */
1.181 @@ -665,47 +728,6 @@
1.182 return false;
1.183 }
1.184
1.185 -bool max_complete_device(max_device *device)
1.186 -{
1.187 - struct usb_device_descriptor *desc;
1.188 -
1.189 - printf("Sending control request to address 0, endpoint 0...\n");
1.190 -
1.191 - device->address = 0;
1.192 -
1.193 - /* Send a "get descriptor" request for the device descriptor. */
1.194 -
1.195 - desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0, false);
1.196 -
1.197 - if (desc != NULL)
1.198 - {
1.199 - memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor));
1.200 - device->desc.bcdUSB = le16toh(device->desc.bcdUSB);
1.201 - device->desc.idVendor = le16toh(device->desc.idVendor);
1.202 - device->desc.idProduct = le16toh(device->desc.idProduct);
1.203 - device->desc.bcdDevice = le16toh(device->desc.bcdDevice);
1.204 -
1.205 - printf("bLength: %d\n", device->desc.bLength);
1.206 - printf("bDescriptorType: %d\n", device->desc.bDescriptorType);
1.207 - printf("bcdUSB: %04x\n", device->desc.bcdUSB);
1.208 - printf("bDeviceClass: %d\n", device->desc.bDeviceClass);
1.209 - printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass);
1.210 - printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol);
1.211 - printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0);
1.212 - printf("idVendor: %04x\n", device->desc.idVendor);
1.213 - printf("idProduct: %04x\n", device->desc.idProduct);
1.214 - printf("bcdDevice: %04x\n", device->desc.bcdDevice);
1.215 - printf("iManufacturer: %d\n", device->desc.iManufacturer);
1.216 - printf("iProduct: %d\n", device->desc.iProduct);
1.217 - printf("iSerialNumber: %d\n", device->desc.iSerialNumber);
1.218 - printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations);
1.219 -
1.220 - return true;
1.221 - }
1.222 -
1.223 - return false;
1.224 -}
1.225 -
1.226 /**
1.227 * Assign a new address to the given device.
1.228 */
1.229 @@ -724,6 +746,24 @@
1.230 max_control_status(false);
1.231 }
1.232
1.233 +void usb_show_device(max_device *device)
1.234 +{
1.235 + printf("bLength: %d\n", device->desc.bLength);
1.236 + printf("bDescriptorType: %d\n", device->desc.bDescriptorType);
1.237 + printf("bcdUSB: %04x\n", device->desc.bcdUSB);
1.238 + printf("bDeviceClass: %d\n", device->desc.bDeviceClass);
1.239 + printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass);
1.240 + printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol);
1.241 + printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0);
1.242 + printf("idVendor: %04x\n", device->desc.idVendor);
1.243 + printf("idProduct: %04x\n", device->desc.idProduct);
1.244 + printf("bcdDevice: %04x\n", device->desc.bcdDevice);
1.245 + printf("iManufacturer: %d\n", device->desc.iManufacturer);
1.246 + printf("iProduct: %d\n", device->desc.iProduct);
1.247 + printf("iSerialNumber: %d\n", device->desc.iSerialNumber);
1.248 + printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations);
1.249 +}
1.250 +
1.251 void usb_show_languages(uint8_t *data)
1.252 {
1.253 struct usb_string_descriptor *desc = (struct usb_string_descriptor *) data;
1.254 @@ -989,23 +1029,23 @@
1.255
1.256 else if ((devstate == MAX_DEVSTATE_RESET_AGAIN) && frame_event && !(--framecount))
1.257 {
1.258 - if (!max_complete_device(&device))
1.259 - printf("FAILED: RESET_AGAIN -> ADDRESSING\n");
1.260 - else
1.261 - {
1.262 - max_set_address(&device);
1.263 - devstate = MAX_DEVSTATE_ADDRESSING;
1.264 - framecount = 30;
1.265 - printf("ADDRESSING\n");
1.266 - }
1.267 + max_set_address(&device);
1.268 + devstate = MAX_DEVSTATE_ADDRESSING;
1.269 + framecount = 30;
1.270 + printf("ADDRESSING\n");
1.271 }
1.272
1.273 /* Post-addressing. */
1.274
1.275 else if ((devstate == MAX_DEVSTATE_ADDRESSING) && frame_event && !(--framecount))
1.276 {
1.277 - usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0, false));
1.278 - usb_show_configuration(max_get_descriptor(&device, USB_DT_CONFIG, 0, 0, false));
1.279 + if (usb_init_device(&device, max_get_descriptor(&device, USB_DT_DEVICE, 0, 0, usb_descriptor_size(USB_DT_DEVICE))))
1.280 + usb_show_device(&device);
1.281 + else
1.282 + printf("DEVICE?\n");
1.283 +
1.284 + usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0, 0));
1.285 + usb_show_configuration(max_get_descriptor(&device, USB_DT_CONFIG, 0, 0, 0));
1.286 devstate = MAX_DEVSTATE_READY;
1.287 printf("READY\n");
1.288 }