1.1 --- a/test.c Sat Mar 23 17:31:34 2013 +0000
1.2 +++ b/test.c Sat Mar 23 22:13:00 2013 +0000
1.3 @@ -622,6 +622,39 @@
1.4 }
1.5
1.6 /**
1.7 + * Obtain a device descriptor with usable structure members.
1.8 + */
1.9 +struct usb_device_descriptor *usb_get_device_descriptor(uint8_t *data)
1.10 +{
1.11 + struct usb_device_descriptor *desc = (struct usb_device_descriptor *) data;
1.12 + desc->bcdUSB = le16toh(desc->bcdUSB);
1.13 + desc->idVendor = le16toh(desc->idVendor);
1.14 + desc->idProduct = le16toh(desc->idProduct);
1.15 + desc->bcdDevice = le16toh(desc->bcdDevice);
1.16 + return desc;
1.17 +}
1.18 +
1.19 +/**
1.20 + * Obtain a configuration descriptor with usable structure members.
1.21 + */
1.22 +struct usb_config_descriptor *usb_get_config_descriptor(uint8_t *data)
1.23 +{
1.24 + struct usb_config_descriptor *conf = (struct usb_config_descriptor *) data;
1.25 + conf->wTotalLength = le16toh(conf->wTotalLength);
1.26 + return conf;
1.27 +}
1.28 +
1.29 +/**
1.30 + * Obtain an endpoint descriptor with usable structure members.
1.31 + */
1.32 +struct usb_endpoint_descriptor *usb_get_endpoint_descriptor(uint8_t *data)
1.33 +{
1.34 + struct usb_endpoint_descriptor *endp = (struct usb_endpoint_descriptor *) data;
1.35 + endp->wMaxPacketSize = le16toh(endp->wMaxPacketSize);
1.36 + return endp;
1.37 +}
1.38 +
1.39 +/**
1.40 * Get a descriptor from the device having the given type, value, index and length.
1.41 * The descriptor buffer is static and must be copied if it is to be preserved.
1.42 */
1.43 @@ -670,7 +703,7 @@
1.44 return NULL;
1.45
1.46 if (type == USB_DT_CONFIG)
1.47 - length = le16toh(((struct usb_config_descriptor *) data)->wTotalLength);
1.48 + length = usb_get_config_descriptor(data)->wTotalLength;
1.49 else
1.50 length = ((struct usb_descriptor_header *) data)->bLength;
1.51
1.52 @@ -683,17 +716,13 @@
1.53
1.54 bool usb_init_device(max_device *device, uint8_t *data)
1.55 {
1.56 - struct usb_device_descriptor *desc = (struct usb_device_descriptor *) data;
1.57 + struct usb_device_descriptor *desc;
1.58
1.59 if (data == NULL)
1.60 return false;
1.61
1.62 + desc = usb_get_device_descriptor(data);
1.63 memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor));
1.64 - device->desc.bcdUSB = le16toh(device->desc.bcdUSB);
1.65 - device->desc.idVendor = le16toh(device->desc.idVendor);
1.66 - device->desc.idProduct = le16toh(device->desc.idProduct);
1.67 - device->desc.bcdDevice = le16toh(device->desc.bcdDevice);
1.68 -
1.69 return true;
1.70 }
1.71
1.72 @@ -746,6 +775,23 @@
1.73 max_control_status(false);
1.74 }
1.75
1.76 +/**
1.77 + * Set the configuration of the given device.
1.78 + */
1.79 +void max_set_configuration(max_device *device, uint8_t config)
1.80 +{
1.81 + uint8_t setup[8];
1.82 +
1.83 + max_write(MAX_REG_PERADDR, device->address);
1.84 +
1.85 + setup_packet(setup, USB_ENDPOINT_OUT, USB_REQ_SET_CONFIGURATION, config, 0, 0);
1.86 + max_control(setup);
1.87 + max_control_status(false);
1.88 +}
1.89 +
1.90 +/**
1.91 + * Return a string representation of an endpoint address.
1.92 + */
1.93 const char *usb_endpoint_address_str(uint8_t bEndpointAddress)
1.94 {
1.95 static char output[] = "255 (127 OUT)";
1.96 @@ -757,6 +803,9 @@
1.97 return output;
1.98 }
1.99
1.100 +/**
1.101 + * Return a string representation of endpoint attributes.
1.102 + */
1.103 const char *usb_endpoint_attributes_str(uint8_t bmAttributes)
1.104 {
1.105 switch (bmAttributes & USB_ENDPOINT_TYPE_MASK)
1.106 @@ -813,26 +862,6 @@
1.107 }
1.108 }
1.109
1.110 -/**
1.111 - * Obtain a configuration descriptor with usable structure members.
1.112 - */
1.113 -struct usb_config_descriptor *usb_get_config_descriptor(uint8_t *data)
1.114 -{
1.115 - struct usb_config_descriptor *conf = (struct usb_config_descriptor *) data;
1.116 - conf->wTotalLength = le16toh(conf->wTotalLength);
1.117 - return conf;
1.118 -}
1.119 -
1.120 -/**
1.121 - * Obtain an endpoint descriptor with usable structure members.
1.122 - */
1.123 -struct usb_endpoint_descriptor *usb_get_endpoint_descriptor(uint8_t *data)
1.124 -{
1.125 - struct usb_endpoint_descriptor *endp = (struct usb_endpoint_descriptor *) data;
1.126 - endp->wMaxPacketSize = le16toh(endp->wMaxPacketSize);
1.127 - return endp;
1.128 -}
1.129 -
1.130 uint8_t *usb_descriptor_start(struct usb_config_descriptor *conf)
1.131 {
1.132 return ((uint8_t *) conf) + conf->bLength;
1.133 @@ -930,6 +959,70 @@
1.134 }
1.135 }
1.136
1.137 +void usb_show_configurations(max_device *device)
1.138 +{
1.139 + uint8_t n;
1.140 +
1.141 + for (n = 0; n < device->desc.bNumConfigurations; n++)
1.142 + {
1.143 + usb_show_configuration_full(max_get_descriptor(device, USB_DT_CONFIG, n, 0, 0));
1.144 + }
1.145 +}
1.146 +
1.147 +bool usb_configuration_has_interface(uint8_t *data, uint8_t bInterfaceClass, uint8_t bInterfaceProtocol)
1.148 +{
1.149 + struct usb_config_descriptor *conf;
1.150 + struct usb_descriptor_header *desc;
1.151 + struct usb_interface_descriptor *intf;
1.152 + uint8_t *current, *last;
1.153 +
1.154 + if (data == NULL)
1.155 + return false;
1.156 +
1.157 + conf = usb_get_config_descriptor(data);
1.158 + current = usb_descriptor_start(conf);
1.159 + last = usb_descriptor_end(conf);
1.160 +
1.161 + while ((current != NULL) && (current < last))
1.162 + {
1.163 + desc = (struct usb_descriptor_header *) current;
1.164 + switch (desc->bDescriptorType)
1.165 + {
1.166 + case USB_DT_INTERFACE:
1.167 + intf = (struct usb_interface_descriptor *) current;
1.168 + if ((intf->bInterfaceClass == bInterfaceClass) && (intf->bInterfaceProtocol == bInterfaceProtocol))
1.169 + return true;
1.170 + break;
1.171 +
1.172 + default:
1.173 + break;
1.174 + }
1.175 +
1.176 + current = usb_descriptor_next(current);
1.177 + }
1.178 +
1.179 + return false;
1.180 +}
1.181 +
1.182 +bool usb_configure_device(max_device *device)
1.183 +{
1.184 + uint8_t n, *data;
1.185 +
1.186 + for (n = 0; n < device->desc.bNumConfigurations; n++)
1.187 + {
1.188 + data = max_get_descriptor(device, USB_DT_CONFIG, n, 0, 0);
1.189 +
1.190 + /* USB HID mouse. */
1.191 +
1.192 + if (usb_configuration_has_interface(data, 3, 2))
1.193 + {
1.194 + return true;
1.195 + }
1.196 + }
1.197 +
1.198 + return false;
1.199 +}
1.200 +
1.201 /**
1.202 * Handle termination of the process.
1.203 */
1.204 @@ -1130,9 +1223,13 @@
1.205 printf("DEVICE?\n");
1.206
1.207 usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0, 0));
1.208 - usb_show_configuration_full(max_get_descriptor(&device, USB_DT_CONFIG, 0, 0, 0));
1.209 + usb_show_configurations(&device);
1.210 devstate = MAX_DEVSTATE_READY;
1.211 printf("READY\n");
1.212 +
1.213 + /* Detect supported interfaces and configure devices to use them. */
1.214 +
1.215 + /* usb_configure_device(&device); */
1.216 }
1.217
1.218 /* Handle device disconnection. */