2.1 --- a/test.c Sat Feb 23 21:58:24 2013 +0000
2.2 +++ b/test.c Sun Feb 24 01:18:13 2013 +0000
2.3 @@ -20,6 +20,7 @@
2.4 #include <usb.h>
2.5 #include <unistd.h>
2.6 #include <string.h>
2.7 +#include <endian.h>
2.8
2.9 /* Found in Python's asdl.h. */
2.10
2.11 @@ -291,7 +292,8 @@
2.12 status = max_read(MAX_REG_HIRQ, NULL);
2.13 }
2.14
2.15 - return status;
2.16 + max_write(MAX_REG_HIRQ, MAX_HIRQ_HXFRDNIRQ);
2.17 + return max_read(MAX_REG_HIRQ, NULL);
2.18 }
2.19
2.20 /**
2.21 @@ -313,32 +315,28 @@
2.22 /**
2.23 * Read the data from the FIFO.
2.24 */
2.25 -void max_read_fifo(uint8_t *data, uint8_t *len, uint8_t *datalimit)
2.26 +void max_read_fifo(uint8_t **data, uint8_t *len, uint8_t *datalimit)
2.27 {
2.28 uint8_t count, received = max_read(MAX_REG_RCVBC, NULL);
2.29 + printf("Received %d bytes.\n", received);
2.30
2.31 *len += received;
2.32
2.33 - for (count = 0; (count < received) && (data < datalimit); count++)
2.34 + for (count = 0; (count < received) && (*data < datalimit); count++)
2.35 {
2.36 - *data++ = max_read(MAX_REG_RCVFIFO, NULL);
2.37 + *((*data)++) = max_read(MAX_REG_RCVFIFO, NULL);
2.38 }
2.39 }
2.40
2.41 /**
2.42 - * Send a control request to the given address consisting of the given setup
2.43 - * data.
2.44 + * Send a control request consisting of the given setup data.
2.45 */
2.46 -uint8_t max_control(uint8_t address, uint8_t *setup)
2.47 +uint8_t max_control(uint8_t *setup)
2.48 {
2.49 uint8_t status, hrsl;
2.50
2.51 max_write_fifo(0, setup, 8);
2.52
2.53 - /* Set the address. */
2.54 -
2.55 - max_write(MAX_REG_PERADDR, address);
2.56 -
2.57 /* Initiate the transfer. */
2.58
2.59 do
2.60 @@ -373,22 +371,17 @@
2.61 }
2.62
2.63 /**
2.64 - * Send a request to the given address and endpoint, using the supplied data
2.65 - * payload with the given length, indicating the preserved toggle state of the
2.66 - * endpoint (which will be updated).
2.67 + * Send a request to the given endpoint, using the supplied data payload with
2.68 + * the given length, indicating the preserved toggle state of the endpoint
2.69 + * (which will be updated).
2.70 */
2.71 -uint8_t max_send(uint8_t address, uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle)
2.72 +uint8_t max_send(uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle)
2.73 {
2.74 uint8_t status, hrsl = 0;
2.75
2.76 max_write_fifo(endpoint, data, len);
2.77
2.78 - if (endpoint)
2.79 - max_set_send_toggle(*toggle);
2.80 -
2.81 - /* Set the address. */
2.82 -
2.83 - max_write(MAX_REG_PERADDR, address);
2.84 + max_set_send_toggle(*toggle);
2.85
2.86 /* Initiate the transfer. */
2.87
2.88 @@ -399,36 +392,34 @@
2.89
2.90 /* Test for usable data. */
2.91
2.92 - if (!(status & MAX_HIRQ_SNDBAVIRQ))
2.93 - continue;
2.94 + if (status & MAX_HIRQ_SNDBAVIRQ)
2.95 + {
2.96 + hrsl = max_read(MAX_REG_HRSL, &status);
2.97
2.98 - hrsl = max_read(MAX_REG_HRSL, &status);
2.99 + if (!(hrsl & MAX_HRSL_HRSLT))
2.100 + break;
2.101 + }
2.102 }
2.103 - while (hrsl & MAX_HRSL_HRSLT);
2.104 + while (true);
2.105
2.106 - if (endpoint)
2.107 - *toggle = max_get_send_toggle();
2.108 + *toggle = max_get_send_toggle();
2.109
2.110 return status;
2.111 }
2.112
2.113 /**
2.114 - * Make a request for data from the given address and endpoint, collecting it in
2.115 - * the supplied buffer with the given length, indicating the preserved toggle
2.116 - * state of the endpoint (which will be updated) The length will be updated to
2.117 + * Make a request for data from the given endpoint, collecting it in the
2.118 + * supplied buffer with the given length, indicating the preserved toggle state
2.119 + * of the endpoint (which will be updated). The length will be updated to
2.120 * indicate the total length of the received data.
2.121 */
2.122 -uint8_t max_recv(uint8_t address, uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle)
2.123 +bool max_recv(uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle)
2.124 {
2.125 uint8_t *datalimit = data + *len;
2.126 uint8_t status, hrsl = 0;
2.127 -
2.128 - if (endpoint)
2.129 - max_set_send_toggle(*toggle);
2.130 + uint16_t attempt = 1024;
2.131
2.132 - /* Set the address. */
2.133 -
2.134 - max_write(MAX_REG_PERADDR, address);
2.135 + max_set_recv_toggle(*toggle);
2.136
2.137 /* Initiate the transfer. */
2.138
2.139 @@ -439,27 +430,31 @@
2.140
2.141 /* Test for usable data. */
2.142
2.143 - if (!(status & MAX_HIRQ_RCVDAVIRQ))
2.144 - continue;
2.145 + hrsl = max_read(MAX_REG_HRSL, &status);
2.146
2.147 - hrsl = max_read(MAX_REG_HRSL, &status);
2.148 + attempt--;
2.149 }
2.150 - while (hrsl & MAX_HRSL_HRSLT);
2.151 + while ((hrsl & MAX_HRSL_HRSLT) && attempt);
2.152
2.153 - do
2.154 + if (!attempt)
2.155 {
2.156 - max_read_fifo(data, len, datalimit);
2.157 + printf("HRSL is %x\n", hrsl);
2.158 + return false;
2.159 + }
2.160 +
2.161 + while (status & MAX_HIRQ_RCVDAVIRQ)
2.162 + {
2.163 + max_read_fifo(&data, len, datalimit);
2.164
2.165 /* Indicate that all data has been read. */
2.166
2.167 - status = max_write(MAX_REG_HIRQ, MAX_HIRQ_RCVDAVIRQ);
2.168 + max_write(MAX_REG_HIRQ, MAX_HIRQ_RCVDAVIRQ);
2.169 + status = max_read(MAX_REG_HIRQ, NULL);
2.170 }
2.171 - while (status & MAX_HIRQ_RCVDAVIRQ);
2.172
2.173 - if (endpoint)
2.174 - *toggle = max_get_send_toggle();
2.175 + *toggle = max_get_recv_toggle();
2.176
2.177 - return status;
2.178 + return true;
2.179 }
2.180
2.181 void chipreset()
2.182 @@ -582,24 +577,33 @@
2.183 uint8_t data[64], len = 64, setup[8];
2.184
2.185 printf("Sending control request to address 0, endpoint 0...\n");
2.186 - setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, USB_DT_DEVICE, 0, USB_DT_DEVICE_SIZE);
2.187 - max_control(0, setup);
2.188 - max_recv(0, 0, data, &len, &device->in_toggle);
2.189 +
2.190 + max_write(MAX_REG_PERADDR, 0);
2.191 + setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, USB_DT_DEVICE << 8, 0, USB_DT_DEVICE_SIZE);
2.192 + max_control(setup);
2.193 + device->in_toggle = true;
2.194 + if (!max_recv(0, data, &len, &device->in_toggle))
2.195 + return false;
2.196 max_control_status(true);
2.197
2.198 if (len >= sizeof(struct usb_device_descriptor))
2.199 {
2.200 memcpy(&device->desc, data, sizeof(struct usb_device_descriptor));
2.201 + device->desc.bcdUSB = le16toh(device->desc.bcdUSB);
2.202 + device->desc.idVendor = le16toh(device->desc.idVendor);
2.203 + device->desc.idProduct = le16toh(device->desc.idProduct);
2.204 + device->desc.bcdDevice = le16toh(device->desc.bcdDevice);
2.205 +
2.206 printf("bLength: %d\n", device->desc.bLength);
2.207 printf("bDescriptorType: %d\n", device->desc.bDescriptorType);
2.208 - printf("bcdUSB: %d\n", device->desc.bcdUSB);
2.209 + printf("bcdUSB: %04x\n", device->desc.bcdUSB);
2.210 printf("bDeviceClass: %d\n", device->desc.bDeviceClass);
2.211 printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass);
2.212 printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol);
2.213 printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0);
2.214 - printf("idVendor: %x\n", device->desc.idVendor);
2.215 - printf("idProduct: %x\n", device->desc.idProduct);
2.216 - printf("bcdDevice: %d\n", device->desc.bcdDevice);
2.217 + printf("idVendor: %04x\n", device->desc.idVendor);
2.218 + printf("idProduct: %04x\n", device->desc.idProduct);
2.219 + printf("bcdDevice: %04x\n", device->desc.bcdDevice);
2.220 printf("iManufacturer: %d\n", device->desc.iManufacturer);
2.221 printf("iProduct: %d\n", device->desc.iProduct);
2.222 printf("iSerialNumber: %d\n", device->desc.iSerialNumber);
2.223 @@ -759,9 +763,13 @@
2.224
2.225 else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && max_can_send(&status))
2.226 {
2.227 - max_init_device(&device);
2.228 - devstate = MAX_DEVSTATE_INSPECTED;
2.229 - printf("INSPECTED\n");
2.230 + if (max_init_device(&device))
2.231 + {
2.232 + devstate = MAX_DEVSTATE_INSPECTED;
2.233 + printf("INSPECTED\n");
2.234 + }
2.235 + else
2.236 + printf("FAILED: RESET -> INSPECTED\n");
2.237 }
2.238
2.239 /* Handle the second device reset initiation. */