This fork re-enables FRDM boards and adds WebUSB CDC functionality

Fork of USBDevice_STM32F103 by Devan Lai

Committer:
Lars Knudsen
Date:
Tue Jul 11 21:02:39 2017 +0200
Revision:
72:1d8a6665d607
Parent:
67:39396cc073f2
Adding MS OS 2.0 support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
devanlai 67:39396cc073f2 1 /*
devanlai 67:39396cc073f2 2 * Copyright 2016 Devan Lai
Lars Knudsen 72:1d8a6665d607 3 * Modifications copyright 2017 Lars Gunder Knudsen
devanlai 67:39396cc073f2 4 *
devanlai 67:39396cc073f2 5 * Licensed under the Apache License, Version 2.0 (the "License");
devanlai 67:39396cc073f2 6 * you may not use this file except in compliance with the License.
devanlai 67:39396cc073f2 7 * You may obtain a copy of the License at
devanlai 67:39396cc073f2 8 *
devanlai 67:39396cc073f2 9 * http://www.apache.org/licenses/LICENSE-2.0
devanlai 67:39396cc073f2 10 *
devanlai 67:39396cc073f2 11 * Unless required by applicable law or agreed to in writing, software
devanlai 67:39396cc073f2 12 * distributed under the License is distributed on an "AS IS" BASIS,
devanlai 67:39396cc073f2 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
devanlai 67:39396cc073f2 14 * See the License for the specific language governing permissions and
devanlai 67:39396cc073f2 15 * limitations under the License.
devanlai 67:39396cc073f2 16 */
devanlai 67:39396cc073f2 17
devanlai 67:39396cc073f2 18 #include "stdint.h"
devanlai 67:39396cc073f2 19
devanlai 67:39396cc073f2 20 #include "USBEndpoints.h"
devanlai 67:39396cc073f2 21 #include "USBDevice.h"
devanlai 67:39396cc073f2 22 #include "USBDescriptor.h"
devanlai 67:39396cc073f2 23 #include "WebUSB.h"
devanlai 67:39396cc073f2 24 #include "WebUSBDevice.h"
Lars Knudsen 72:1d8a6665d607 25 #include "../USBDFU/WinUSB.h"
devanlai 67:39396cc073f2 26
devanlai 67:39396cc073f2 27 WebUSBDevice::WebUSBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
devanlai 67:39396cc073f2 28 : USBDevice(vendor_id, product_id, product_release)
devanlai 67:39396cc073f2 29 {
devanlai 67:39396cc073f2 30
devanlai 67:39396cc073f2 31 };
devanlai 67:39396cc073f2 32
devanlai 67:39396cc073f2 33 bool WebUSBDevice::requestGetDescriptor(void)
devanlai 67:39396cc073f2 34 {
devanlai 67:39396cc073f2 35 bool success = false;
devanlai 67:39396cc073f2 36 CONTROL_TRANSFER * transfer = getTransferPtr();
devanlai 67:39396cc073f2 37 switch (DESCRIPTOR_TYPE(transfer->setup.wValue))
devanlai 67:39396cc073f2 38 {
devanlai 67:39396cc073f2 39 case BINARY_OBJECT_STORE_DESCRIPTOR:
devanlai 67:39396cc073f2 40 if (binaryObjectStoreDesc() != NULL)
devanlai 67:39396cc073f2 41 {
devanlai 67:39396cc073f2 42 transfer->remaining = (binaryObjectStoreDesc()[2]
devanlai 67:39396cc073f2 43 | (binaryObjectStoreDesc()[3] << 8));
devanlai 67:39396cc073f2 44 transfer->ptr = binaryObjectStoreDesc();
devanlai 67:39396cc073f2 45 transfer->direction = DEVICE_TO_HOST;
devanlai 67:39396cc073f2 46 success = true;
devanlai 67:39396cc073f2 47 }
devanlai 67:39396cc073f2 48 break;
devanlai 67:39396cc073f2 49 default:
devanlai 67:39396cc073f2 50 success = USBDevice::requestGetDescriptor();
devanlai 67:39396cc073f2 51 break;
devanlai 67:39396cc073f2 52 }
devanlai 67:39396cc073f2 53
devanlai 67:39396cc073f2 54 return success;
devanlai 67:39396cc073f2 55 }
devanlai 67:39396cc073f2 56
devanlai 67:39396cc073f2 57 bool WebUSBDevice::requestWebUSB(void)
devanlai 67:39396cc073f2 58 {
devanlai 67:39396cc073f2 59 bool success = false;
devanlai 67:39396cc073f2 60
devanlai 67:39396cc073f2 61 CONTROL_TRANSFER * transfer = getTransferPtr();
devanlai 67:39396cc073f2 62 switch (transfer->setup.wIndex)
devanlai 67:39396cc073f2 63 {
devanlai 67:39396cc073f2 64 case WEBUSB_GET_ALLOWED_ORIGINS:
devanlai 67:39396cc073f2 65 if (allowedOriginsDesc())
devanlai 67:39396cc073f2 66 {
devanlai 67:39396cc073f2 67 transfer->remaining = (allowedOriginsDesc()[2]
devanlai 67:39396cc073f2 68 | (allowedOriginsDesc()[3] << 8));
devanlai 67:39396cc073f2 69 transfer->ptr = allowedOriginsDesc();
devanlai 67:39396cc073f2 70 transfer->direction = DEVICE_TO_HOST;
devanlai 67:39396cc073f2 71 success = true;
devanlai 67:39396cc073f2 72 }
devanlai 67:39396cc073f2 73 break;
devanlai 67:39396cc073f2 74 case WEBUSB_GET_URL:
devanlai 67:39396cc073f2 75 if (transfer->setup.wValue == URL_OFFSET_LANDING_PAGE)
devanlai 67:39396cc073f2 76 {
devanlai 67:39396cc073f2 77 transfer->remaining = urlIlandingPage()[0];
devanlai 67:39396cc073f2 78 transfer->ptr = urlIlandingPage();
devanlai 67:39396cc073f2 79 transfer->direction = DEVICE_TO_HOST;
devanlai 67:39396cc073f2 80 success = true;
devanlai 67:39396cc073f2 81 }
devanlai 67:39396cc073f2 82 else if (transfer->setup.wValue == URL_OFFSET_ALLOWED_ORIGIN)
devanlai 67:39396cc073f2 83 {
devanlai 67:39396cc073f2 84 transfer->remaining = urlIallowedOrigin()[0];
devanlai 67:39396cc073f2 85 transfer->ptr = urlIallowedOrigin();
devanlai 67:39396cc073f2 86 transfer->direction = DEVICE_TO_HOST;
devanlai 67:39396cc073f2 87 success = true;
devanlai 67:39396cc073f2 88 }
devanlai 67:39396cc073f2 89 break;
devanlai 67:39396cc073f2 90 default:
devanlai 67:39396cc073f2 91 break;
devanlai 67:39396cc073f2 92 }
devanlai 67:39396cc073f2 93
devanlai 67:39396cc073f2 94 return success;
devanlai 67:39396cc073f2 95 }
devanlai 67:39396cc073f2 96
devanlai 67:39396cc073f2 97 bool WebUSBDevice::USBCallback_request()
devanlai 67:39396cc073f2 98 {
devanlai 67:39396cc073f2 99 bool success = false;
devanlai 67:39396cc073f2 100 /* Process WebUSB requests */
devanlai 67:39396cc073f2 101 CONTROL_TRANSFER * transfer = getTransferPtr();
devanlai 67:39396cc073f2 102 if ((transfer->setup.bmRequestType.Type == VENDOR_TYPE) &&
devanlai 67:39396cc073f2 103 (transfer->setup.bRequest == WEBUSB_VENDOR_CODE))
devanlai 67:39396cc073f2 104 {
devanlai 67:39396cc073f2 105 success = requestWebUSB();
devanlai 67:39396cc073f2 106 }
devanlai 67:39396cc073f2 107
devanlai 67:39396cc073f2 108 return success;
devanlai 67:39396cc073f2 109 }
devanlai 67:39396cc073f2 110
devanlai 67:39396cc073f2 111 uint8_t * WebUSBDevice::deviceDesc() {
devanlai 67:39396cc073f2 112 static uint8_t deviceDescriptor[] = {
devanlai 67:39396cc073f2 113 DEVICE_DESCRIPTOR_LENGTH, /* bLength */
devanlai 67:39396cc073f2 114 DEVICE_DESCRIPTOR, /* bDescriptorType */
devanlai 67:39396cc073f2 115 LSB(USB_VERSION_2_1), /* bcdUSB (LSB) */
devanlai 67:39396cc073f2 116 MSB(USB_VERSION_2_1), /* bcdUSB (MSB) */
devanlai 67:39396cc073f2 117 0x00, /* bDeviceClass */
devanlai 67:39396cc073f2 118 0x00, /* bDeviceSubClass */
devanlai 67:39396cc073f2 119 0x00, /* bDeviceprotocol */
devanlai 67:39396cc073f2 120 MAX_PACKET_SIZE_EP0, /* bMaxPacketSize0 */
devanlai 67:39396cc073f2 121 (uint8_t)(LSB(VENDOR_ID)), /* idVendor (LSB) */
devanlai 67:39396cc073f2 122 (uint8_t)(MSB(VENDOR_ID)), /* idVendor (MSB) */
devanlai 67:39396cc073f2 123 (uint8_t)(LSB(PRODUCT_ID)), /* idProduct (LSB) */
devanlai 67:39396cc073f2 124 (uint8_t)(MSB(PRODUCT_ID)), /* idProduct (MSB) */
devanlai 67:39396cc073f2 125 (uint8_t)(LSB(PRODUCT_RELEASE)), /* bcdDevice (LSB) */
devanlai 67:39396cc073f2 126 (uint8_t)(MSB(PRODUCT_RELEASE)), /* bcdDevice (MSB) */
devanlai 67:39396cc073f2 127 STRING_OFFSET_IMANUFACTURER, /* iManufacturer */
devanlai 67:39396cc073f2 128 STRING_OFFSET_IPRODUCT, /* iProduct */
devanlai 67:39396cc073f2 129 STRING_OFFSET_ISERIAL, /* iSerialNumber */
devanlai 67:39396cc073f2 130 0x01 /* bNumConfigurations */
devanlai 67:39396cc073f2 131 };
devanlai 67:39396cc073f2 132 return deviceDescriptor;
devanlai 67:39396cc073f2 133 }
devanlai 67:39396cc073f2 134
devanlai 67:39396cc073f2 135 #define WEBUSB_BOS_TOTAL_LENGTH (BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH \
Lars Knudsen 72:1d8a6665d607 136 + WEBUSB_PLATFORM_DESCRIPTOR_LENGTH + 28)
devanlai 67:39396cc073f2 137
devanlai 67:39396cc073f2 138 uint8_t * WebUSBDevice::binaryObjectStoreDesc() {
devanlai 67:39396cc073f2 139 static uint8_t binaryObjectStoreDescriptor[] = {
devanlai 67:39396cc073f2 140 BINARY_OBJECT_STORE_DESCRIPTOR_LENGTH, /* bLength */
devanlai 67:39396cc073f2 141 BINARY_OBJECT_STORE_DESCRIPTOR, /* bDescriptorType */
devanlai 67:39396cc073f2 142 LSB(WEBUSB_BOS_TOTAL_LENGTH), /* wTotalLength (LSB) */
devanlai 67:39396cc073f2 143 MSB(WEBUSB_BOS_TOTAL_LENGTH), /* wTotalLength (MSB) */
Lars Knudsen 72:1d8a6665d607 144 0x02, /* bNumDeviceCaps (WebUSB + WinUSB) */
Lars Knudsen 72:1d8a6665d607 145
Lars Knudsen 72:1d8a6665d607 146 // WebUSB
devanlai 67:39396cc073f2 147 WEBUSB_PLATFORM_DESCRIPTOR_LENGTH, /* bLength */
devanlai 67:39396cc073f2 148 DEVICE_CAPABILITY_DESCRIPTOR, /* bDescriptorType */
devanlai 67:39396cc073f2 149 USB_DC_PLATFORM, /* bDevCapabilityType */
devanlai 67:39396cc073f2 150 0x00, /* bReserved */
devanlai 67:39396cc073f2 151 0x38, 0xB6, 0x08, 0x34, /* PlatformCapabilityUUID */
devanlai 67:39396cc073f2 152 0xA9, 0x09, 0xA0, 0x47,
devanlai 67:39396cc073f2 153 0x8B, 0xFD, 0xA0, 0x76,
devanlai 67:39396cc073f2 154 0x88, 0x15, 0xB6, 0x65,
devanlai 67:39396cc073f2 155 LSB(WEBUSB_VERSION_1_0), /* bcdVersion (LSB) */
devanlai 67:39396cc073f2 156 MSB(WEBUSB_VERSION_1_0), /* bcdVersion (MSB) */
devanlai 67:39396cc073f2 157 WEBUSB_VENDOR_CODE, /* bVendorCode */
devanlai 67:39396cc073f2 158 URL_OFFSET_LANDING_PAGE, /* iLandingPage */
Lars Knudsen 72:1d8a6665d607 159
Lars Knudsen 72:1d8a6665d607 160 // Windows related (to get WinUSB auto-loaded)
Lars Knudsen 72:1d8a6665d607 161 0x1C, 0x10, 0x05, 0x00, /* Microsoft OS 2.0 Platform Capability Descriptor */
Lars Knudsen 72:1d8a6665d607 162
Lars Knudsen 72:1d8a6665d607 163 0xDF, 0x60, 0xDD, 0xD8, /* MS OS 2.0 Platform Capability ID */
Lars Knudsen 72:1d8a6665d607 164 0x89, 0x45, 0xC7, 0x4C,
Lars Knudsen 72:1d8a6665d607 165 0x9C, 0xD2, 0x65, 0x9D,
Lars Knudsen 72:1d8a6665d607 166 0x9E, 0x64, 0x8A, 0x9F,
Lars Knudsen 72:1d8a6665d607 167
Lars Knudsen 72:1d8a6665d607 168 0x00, 0x00, 0x03, 0x06, /* Windows version */
Lars Knudsen 72:1d8a6665d607 169
Lars Knudsen 72:1d8a6665d607 170 0xB2, 0x00, WINUSB_VENDOR_CODE, 0x00 /* MS OS 2.0 descriptor size (word), vendor code, no alternate enumeration */
devanlai 67:39396cc073f2 171 };
devanlai 67:39396cc073f2 172 return binaryObjectStoreDescriptor;
devanlai 67:39396cc073f2 173 }