1.1 --- a/test.c Sat Feb 23 21:58:24 2013 +0000
1.2 +++ b/test.c Sun Feb 24 01:18:13 2013 +0000
1.3 @@ -20,6 +20,7 @@
1.4 #include <usb.h>
1.5 #include <unistd.h>
1.6 #include <string.h>
1.7 +#include <endian.h>
1.8
1.9 /* Found in Python's asdl.h. */
1.10
1.11 @@ -291,7 +292,8 @@
1.12 status = max_read(MAX_REG_HIRQ, NULL);
1.13 }
1.14
1.15 - return status;
1.16 + max_write(MAX_REG_HIRQ, MAX_HIRQ_HXFRDNIRQ);
1.17 + return max_read(MAX_REG_HIRQ, NULL);
1.18 }
1.19
1.20 /**
1.21 @@ -313,32 +315,28 @@
1.22 /**
1.23 * Read the data from the FIFO.
1.24 */
1.25 -void max_read_fifo(uint8_t *data, uint8_t *len, uint8_t *datalimit)
1.26 +void max_read_fifo(uint8_t **data, uint8_t *len, uint8_t *datalimit)
1.27 {
1.28 uint8_t count, received = max_read(MAX_REG_RCVBC, NULL);
1.29 + printf("Received %d bytes.\n", received);
1.30
1.31 *len += received;
1.32
1.33 - for (count = 0; (count < received) && (data < datalimit); count++)
1.34 + for (count = 0; (count < received) && (*data < datalimit); count++)
1.35 {
1.36 - *data++ = max_read(MAX_REG_RCVFIFO, NULL);
1.37 + *((*data)++) = max_read(MAX_REG_RCVFIFO, NULL);
1.38 }
1.39 }
1.40
1.41 /**
1.42 - * Send a control request to the given address consisting of the given setup
1.43 - * data.
1.44 + * Send a control request consisting of the given setup data.
1.45 */
1.46 -uint8_t max_control(uint8_t address, uint8_t *setup)
1.47 +uint8_t max_control(uint8_t *setup)
1.48 {
1.49 uint8_t status, hrsl;
1.50
1.51 max_write_fifo(0, setup, 8);
1.52
1.53 - /* Set the address. */
1.54 -
1.55 - max_write(MAX_REG_PERADDR, address);
1.56 -
1.57 /* Initiate the transfer. */
1.58
1.59 do
1.60 @@ -373,22 +371,17 @@
1.61 }
1.62
1.63 /**
1.64 - * Send a request to the given address and endpoint, using the supplied data
1.65 - * payload with the given length, indicating the preserved toggle state of the
1.66 - * endpoint (which will be updated).
1.67 + * Send a request to the given endpoint, using the supplied data payload with
1.68 + * the given length, indicating the preserved toggle state of the endpoint
1.69 + * (which will be updated).
1.70 */
1.71 -uint8_t max_send(uint8_t address, uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle)
1.72 +uint8_t max_send(uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle)
1.73 {
1.74 uint8_t status, hrsl = 0;
1.75
1.76 max_write_fifo(endpoint, data, len);
1.77
1.78 - if (endpoint)
1.79 - max_set_send_toggle(*toggle);
1.80 -
1.81 - /* Set the address. */
1.82 -
1.83 - max_write(MAX_REG_PERADDR, address);
1.84 + max_set_send_toggle(*toggle);
1.85
1.86 /* Initiate the transfer. */
1.87
1.88 @@ -399,36 +392,34 @@
1.89
1.90 /* Test for usable data. */
1.91
1.92 - if (!(status & MAX_HIRQ_SNDBAVIRQ))
1.93 - continue;
1.94 + if (status & MAX_HIRQ_SNDBAVIRQ)
1.95 + {
1.96 + hrsl = max_read(MAX_REG_HRSL, &status);
1.97
1.98 - hrsl = max_read(MAX_REG_HRSL, &status);
1.99 + if (!(hrsl & MAX_HRSL_HRSLT))
1.100 + break;
1.101 + }
1.102 }
1.103 - while (hrsl & MAX_HRSL_HRSLT);
1.104 + while (true);
1.105
1.106 - if (endpoint)
1.107 - *toggle = max_get_send_toggle();
1.108 + *toggle = max_get_send_toggle();
1.109
1.110 return status;
1.111 }
1.112
1.113 /**
1.114 - * Make a request for data from the given address and endpoint, collecting it in
1.115 - * the supplied buffer with the given length, indicating the preserved toggle
1.116 - * state of the endpoint (which will be updated) The length will be updated to
1.117 + * Make a request for data from the given endpoint, collecting it in the
1.118 + * supplied buffer with the given length, indicating the preserved toggle state
1.119 + * of the endpoint (which will be updated). The length will be updated to
1.120 * indicate the total length of the received data.
1.121 */
1.122 -uint8_t max_recv(uint8_t address, uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle)
1.123 +bool max_recv(uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle)
1.124 {
1.125 uint8_t *datalimit = data + *len;
1.126 uint8_t status, hrsl = 0;
1.127 -
1.128 - if (endpoint)
1.129 - max_set_send_toggle(*toggle);
1.130 + uint16_t attempt = 1024;
1.131
1.132 - /* Set the address. */
1.133 -
1.134 - max_write(MAX_REG_PERADDR, address);
1.135 + max_set_recv_toggle(*toggle);
1.136
1.137 /* Initiate the transfer. */
1.138
1.139 @@ -439,27 +430,31 @@
1.140
1.141 /* Test for usable data. */
1.142
1.143 - if (!(status & MAX_HIRQ_RCVDAVIRQ))
1.144 - continue;
1.145 + hrsl = max_read(MAX_REG_HRSL, &status);
1.146
1.147 - hrsl = max_read(MAX_REG_HRSL, &status);
1.148 + attempt--;
1.149 }
1.150 - while (hrsl & MAX_HRSL_HRSLT);
1.151 + while ((hrsl & MAX_HRSL_HRSLT) && attempt);
1.152
1.153 - do
1.154 + if (!attempt)
1.155 {
1.156 - max_read_fifo(data, len, datalimit);
1.157 + printf("HRSL is %x\n", hrsl);
1.158 + return false;
1.159 + }
1.160 +
1.161 + while (status & MAX_HIRQ_RCVDAVIRQ)
1.162 + {
1.163 + max_read_fifo(&data, len, datalimit);
1.164
1.165 /* Indicate that all data has been read. */
1.166
1.167 - status = max_write(MAX_REG_HIRQ, MAX_HIRQ_RCVDAVIRQ);
1.168 + max_write(MAX_REG_HIRQ, MAX_HIRQ_RCVDAVIRQ);
1.169 + status = max_read(MAX_REG_HIRQ, NULL);
1.170 }
1.171 - while (status & MAX_HIRQ_RCVDAVIRQ);
1.172
1.173 - if (endpoint)
1.174 - *toggle = max_get_send_toggle();
1.175 + *toggle = max_get_recv_toggle();
1.176
1.177 - return status;
1.178 + return true;
1.179 }
1.180
1.181 void chipreset()
1.182 @@ -582,24 +577,33 @@
1.183 uint8_t data[64], len = 64, setup[8];
1.184
1.185 printf("Sending control request to address 0, endpoint 0...\n");
1.186 - setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, USB_DT_DEVICE, 0, USB_DT_DEVICE_SIZE);
1.187 - max_control(0, setup);
1.188 - max_recv(0, 0, data, &len, &device->in_toggle);
1.189 +
1.190 + max_write(MAX_REG_PERADDR, 0);
1.191 + setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, USB_DT_DEVICE << 8, 0, USB_DT_DEVICE_SIZE);
1.192 + max_control(setup);
1.193 + device->in_toggle = true;
1.194 + if (!max_recv(0, data, &len, &device->in_toggle))
1.195 + return false;
1.196 max_control_status(true);
1.197
1.198 if (len >= sizeof(struct usb_device_descriptor))
1.199 {
1.200 memcpy(&device->desc, data, sizeof(struct usb_device_descriptor));
1.201 + device->desc.bcdUSB = le16toh(device->desc.bcdUSB);
1.202 + device->desc.idVendor = le16toh(device->desc.idVendor);
1.203 + device->desc.idProduct = le16toh(device->desc.idProduct);
1.204 + device->desc.bcdDevice = le16toh(device->desc.bcdDevice);
1.205 +
1.206 printf("bLength: %d\n", device->desc.bLength);
1.207 printf("bDescriptorType: %d\n", device->desc.bDescriptorType);
1.208 - printf("bcdUSB: %d\n", device->desc.bcdUSB);
1.209 + printf("bcdUSB: %04x\n", device->desc.bcdUSB);
1.210 printf("bDeviceClass: %d\n", device->desc.bDeviceClass);
1.211 printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass);
1.212 printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol);
1.213 printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0);
1.214 - printf("idVendor: %x\n", device->desc.idVendor);
1.215 - printf("idProduct: %x\n", device->desc.idProduct);
1.216 - printf("bcdDevice: %d\n", device->desc.bcdDevice);
1.217 + printf("idVendor: %04x\n", device->desc.idVendor);
1.218 + printf("idProduct: %04x\n", device->desc.idProduct);
1.219 + printf("bcdDevice: %04x\n", device->desc.bcdDevice);
1.220 printf("iManufacturer: %d\n", device->desc.iManufacturer);
1.221 printf("iProduct: %d\n", device->desc.iProduct);
1.222 printf("iSerialNumber: %d\n", device->desc.iSerialNumber);
1.223 @@ -759,9 +763,13 @@
1.224
1.225 else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && max_can_send(&status))
1.226 {
1.227 - max_init_device(&device);
1.228 - devstate = MAX_DEVSTATE_INSPECTED;
1.229 - printf("INSPECTED\n");
1.230 + if (max_init_device(&device))
1.231 + {
1.232 + devstate = MAX_DEVSTATE_INSPECTED;
1.233 + printf("INSPECTED\n");
1.234 + }
1.235 + else
1.236 + printf("FAILED: RESET -> INSPECTED\n");
1.237 }
1.238
1.239 /* Handle the second device reset initiation. */