1.1 --- a/test.c Sat Feb 23 19:34:47 2013 +0000
1.2 +++ b/test.c Sat Feb 23 21:58:24 2013 +0000
1.3 @@ -19,6 +19,7 @@
1.4 #include <stdlib.h>
1.5 #include <usb.h>
1.6 #include <unistd.h>
1.7 +#include <string.h>
1.8
1.9 /* Found in Python's asdl.h. */
1.10
1.11 @@ -33,9 +34,19 @@
1.12 MAX_DEVSTATE_INIT = 0,
1.13 MAX_DEVSTATE_CONNECTED = 1,
1.14 MAX_DEVSTATE_RESET = 2,
1.15 - MAX_DEVSTATE_READY = 3
1.16 + MAX_DEVSTATE_INSPECTED = 3,
1.17 + MAX_DEVSTATE_RESET_AGAIN = 4,
1.18 + MAX_DEVSTATE_READY = 5
1.19 } max_devstate;
1.20
1.21 +/* Device details. */
1.22 +
1.23 +typedef struct
1.24 +{
1.25 + bool in_toggle, out_toggle;
1.26 + struct usb_device_descriptor desc;
1.27 +} max_device;
1.28 +
1.29 /* Pin assignments:
1.30 *
1.31 * Sniffer UBB Shield
1.32 @@ -507,7 +518,8 @@
1.33
1.34 /**
1.35 * Handle the connection or disconnection of a device, returning true if the
1.36 - * device is now connected or false otherwise.
1.37 + * device is now connected or false otherwise. If the device is connected, a
1.38 + * bus reset is performed.
1.39 */
1.40 bool devicechanged()
1.41 {
1.42 @@ -565,34 +577,37 @@
1.43 setup[7] = length >> 8;
1.44 }
1.45
1.46 -bool max_init_device(bool *in_toggle)
1.47 +bool max_init_device(max_device *device)
1.48 {
1.49 uint8_t data[64], len = 64, setup[8];
1.50 - struct usb_device_descriptor *desc;
1.51
1.52 printf("Sending control request to address 0, endpoint 0...\n");
1.53 setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, USB_DT_DEVICE, 0, USB_DT_DEVICE_SIZE);
1.54 max_control(0, setup);
1.55 - max_recv(0, 0, data, &len, in_toggle);
1.56 + max_recv(0, 0, data, &len, &device->in_toggle);
1.57 max_control_status(true);
1.58
1.59 if (len >= sizeof(struct usb_device_descriptor))
1.60 {
1.61 - desc = (struct usb_device_descriptor *) data;
1.62 - printf("bLength: %d\n", desc->bLength);
1.63 - printf("bDescriptorType: %d\n", desc->bDescriptorType);
1.64 - printf("bcdUSB: %d\n", desc->bcdUSB);
1.65 - printf("bDeviceClass: %d\n", desc->bDeviceClass);
1.66 - printf("bDeviceSubClass: %d\n", desc->bDeviceSubClass);
1.67 - printf("bDeviceProtocol: %d\n", desc->bDeviceProtocol);
1.68 - printf("bMaxPacketSize0: %d\n", desc->bMaxPacketSize0);
1.69 - printf("idVendor: %x\n", desc->idVendor);
1.70 - printf("idProduct: %x\n", desc->idProduct);
1.71 - printf("bcdDevice: %d\n", desc->bcdDevice);
1.72 - printf("iManufacturer: %d\n", desc->iManufacturer);
1.73 - printf("iProduct: %d\n", desc->iProduct);
1.74 - printf("iSerialNumber: %d\n", desc->iSerialNumber);
1.75 - printf("bNumConfigurations: %d\n", desc->bNumConfigurations);
1.76 + memcpy(&device->desc, data, sizeof(struct usb_device_descriptor));
1.77 + printf("bLength: %d\n", device->desc.bLength);
1.78 + printf("bDescriptorType: %d\n", device->desc.bDescriptorType);
1.79 + printf("bcdUSB: %d\n", device->desc.bcdUSB);
1.80 + printf("bDeviceClass: %d\n", device->desc.bDeviceClass);
1.81 + printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass);
1.82 + printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol);
1.83 + printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0);
1.84 + printf("idVendor: %x\n", device->desc.idVendor);
1.85 + printf("idProduct: %x\n", device->desc.idProduct);
1.86 + printf("bcdDevice: %d\n", device->desc.bcdDevice);
1.87 + printf("iManufacturer: %d\n", device->desc.iManufacturer);
1.88 + printf("iProduct: %d\n", device->desc.iProduct);
1.89 + printf("iSerialNumber: %d\n", device->desc.iSerialNumber);
1.90 + printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations);
1.91 +
1.92 + /* Reset the device. */
1.93 +
1.94 + max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST);
1.95 return true;
1.96 }
1.97
1.98 @@ -610,9 +625,13 @@
1.99 {
1.100 uint8_t status = 0, revision = 0;
1.101 uint16_t count;
1.102 - bool in_toggle = 0, bus_event, data_event, suspended_event, connection_event, frame_event;
1.103 + bool bus_event, data_event, suspended_event, connection_event, frame_event;
1.104 + max_device device;
1.105 max_devstate devstate = MAX_DEVSTATE_INIT;
1.106
1.107 + device.in_toggle = false;
1.108 + device.out_toggle = false;
1.109 +
1.110 signal(SIGINT, &shutdown);
1.111
1.112 if (ubb_open(0) < 0) {
1.113 @@ -695,18 +714,26 @@
1.114 {
1.115 if (!PIN(MAX_INT))
1.116 {
1.117 + /* Obtain interrupt conditions. */
1.118 +
1.119 status = max_read(MAX_REG_HIRQ, NULL);
1.120
1.121 - if ((bus_event = status & MAX_HIRQ_BUSEVENTIRQ))
1.122 - printf("Bus ");
1.123 - if ((data_event = status & MAX_HIRQ_RCVDAVIRQ))
1.124 - printf("Data ");
1.125 - if ((suspended_event = status & MAX_HIRQ_SUSDNIRQ))
1.126 - printf("Suspended ");
1.127 - if ((connection_event = status & MAX_HIRQ_CONDETIRQ))
1.128 - printf("Connection ");
1.129 + bus_event = status & MAX_HIRQ_BUSEVENTIRQ;
1.130 + data_event = status & MAX_HIRQ_RCVDAVIRQ;
1.131 + suspended_event = status & MAX_HIRQ_SUSDNIRQ;
1.132 + connection_event = status & MAX_HIRQ_CONDETIRQ;
1.133 frame_event = status & MAX_HIRQ_FRAMEIRQ;
1.134 +
1.135 + #ifdef DEBUG
1.136 + if (bus_event) printf("Bus ");
1.137 + if (data_event) printf("Data ");
1.138 + if (suspended_event) printf("Suspended ");
1.139 + if (connection_event) printf("Connection ");
1.140 + if (frame_event) printf("Frame ");
1.141 printf("\n");
1.142 + #endif
1.143 +
1.144 + /* Acknowledge the interrupts. */
1.145
1.146 max_write(MAX_REG_HIRQ, status);
1.147
1.148 @@ -715,6 +742,7 @@
1.149 if ((devstate == MAX_DEVSTATE_INIT) && connection_event && devicechanged())
1.150 {
1.151 devstate = MAX_DEVSTATE_CONNECTED;
1.152 + printf("CONNECTED\n");
1.153 }
1.154
1.155 /* Handle device reset initiation. */
1.156 @@ -723,15 +751,34 @@
1.157 {
1.158 max_write(MAX_REG_MODE, max_read(MAX_REG_MODE, NULL) | MAX_MODE_SOFKAENAB);
1.159 devstate = MAX_DEVSTATE_RESET;
1.160 + printf("RESET\n");
1.161 }
1.162
1.163 - /* Handle device reset completion, initiating communications. */
1.164 + /* Handle device reset completion, getting device details and
1.165 + initiating a second reset. */
1.166
1.167 else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && max_can_send(&status))
1.168 {
1.169 - max_init_device(&in_toggle);
1.170 + max_init_device(&device);
1.171 + devstate = MAX_DEVSTATE_INSPECTED;
1.172 + printf("INSPECTED\n");
1.173 + }
1.174 +
1.175 + /* Handle the second device reset initiation. */
1.176 +
1.177 + else if ((devstate == MAX_DEVSTATE_INSPECTED) && bus_event)
1.178 + {
1.179 + devstate = MAX_DEVSTATE_RESET_AGAIN;
1.180 + printf("RESET AGAIN\n");
1.181 + }
1.182 +
1.183 + /* Handle the second device reset completion, initiating communications. */
1.184 +
1.185 + else if ((devstate == MAX_DEVSTATE_RESET_AGAIN) && frame_event && max_can_send(&status))
1.186 + {
1.187 + max_write(MAX_REG_MODE, max_read(MAX_REG_MODE, NULL) & ~MAX_MODE_SOFKAENAB);
1.188 devstate = MAX_DEVSTATE_READY;
1.189 - max_write(MAX_REG_MODE, max_read(MAX_REG_MODE, NULL) & ~MAX_MODE_SOFKAENAB);
1.190 + printf("READY\n");
1.191 }
1.192
1.193 /* Handle device disconnection. */
1.194 @@ -739,6 +786,7 @@
1.195 else if ((devstate != MAX_DEVSTATE_INIT) && connection_event)
1.196 {
1.197 devstate = MAX_DEVSTATE_INIT;
1.198 + printf("INIT\n");
1.199 }
1.200 }
1.201 }