/* Copyright (c) 2010, Peter Barrett ** Sleep/Wakeup support added by Michael Dreher ** ** Permission to use, copy, modify, and/or distribute this software for ** any purpose with or without fee is hereby granted, provided that the ** above copyright notice and this permission notice appear in all copies. ** ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS ** SOFTWARE. */ #include "USBAPI.h" #include "PluggableUSB.h" #include #if defined(USBCON) /** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */ #define TX_RX_LED_PULSE_MS 100 volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ //================================================================== //================================================================== extern const u16 STRING_LANGUAGE[] PROGMEM; extern const u8 STRING_PRODUCT[] PROGMEM; extern const u8 STRING_MANUFACTURER[] PROGMEM; extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM; extern const DeviceDescriptor USB_DeviceDescriptorB PROGMEM; extern bool _updatedLUFAbootloader; const u16 STRING_LANGUAGE[2] = { (3<<8) | (2+2), 0x0409 // English }; #ifndef USB_PRODUCT // If no product is provided, use USB IO Board #define USB_PRODUCT "USB IO Board" #endif const u8 STRING_PRODUCT[] PROGMEM = USB_PRODUCT; #if USB_VID == 0x2341 # if defined(USB_MANUFACTURER) # undef USB_MANUFACTURER # endif # define USB_MANUFACTURER "Arduino LLC" #elif USB_VID == 0x1b4f # if defined(USB_MANUFACTURER) # undef USB_MANUFACTURER # endif # define USB_MANUFACTURER "SparkFun" #elif !defined(USB_MANUFACTURER) // Fall through to unknown if no manufacturer name was provided in a macro # define USB_MANUFACTURER "Unknown" #endif const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER; #define DEVICE_CLASS 0x02 // DEVICE DESCRIPTOR const DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1); const DeviceDescriptor USB_DeviceDescriptorB = D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1); //================================================================== //================================================================== volatile u8 _usbConfiguration = 0; volatile u8 _usbCurrentStatus = 0; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device volatile u8 _usbSuspendState = 0; // copy of UDINT to check SUSPI and WAKEUPI bits static inline void WaitIN(void) { while (!(UEINTX & (1< len) n = len; { LockEP lock(ep); // Frame may have been released by the SOF interrupt handler if (!ReadWriteAllowed()) continue; len -= n; if (ep & TRANSFER_ZERO) { while (n--) Send8(0); } else if (ep & TRANSFER_PGM) { while (n--) Send8(pgm_read_byte(data++)); } else { while (n--) Send8(*data++); } if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer ReleaseTX(); } } TXLED1; // light the TX LED TxLEDPulse = TX_RX_LED_PULSE_MS; return r; } u8 _initEndpoints[USB_ENDPOINTS] = { 0, // Control Endpoint EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN // Following endpoints are automatically initialized to 0 }; #define EP_SINGLE_64 0x32 // EP0 #define EP_DOUBLE_64 0x36 // Other endpoints #define EP_SINGLE_16 0x12 static void InitEP(u8 index, u8 type, u8 size) { UENUM = index; UECONX = (1< 64){ recvLength = 64; } // Write data to fit to the end (not the beginning) of the array WaitOUT(); Recv((u8*)d + len - length, recvLength); ClearOUT(); length -= recvLength; } return len; } static u8 SendInterfaces() { u8 interfaces = 0; CDC_GetInterface(&interfaces); #ifdef PLUGGABLE_USB_ENABLED PluggableUSB().getInterface(&interfaces); #endif return interfaces; } // Construct a dynamic configuration descriptor // This really needs dynamic endpoint allocation etc // TODO static bool SendConfiguration(int maxlen) { // Count and measure interfaces InitControl(0); u8 interfaces = SendInterfaces(); ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces); // Now send them InitControl(maxlen); USB_SendControl(0,&config,sizeof(ConfigDescriptor)); SendInterfaces(); return true; } static u8 _cdcComposite = 0; static bool SendDescriptor(USBSetup& setup) { int ret; u8 t = setup.wValueH; if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t) return SendConfiguration(setup.wLength); InitControl(setup.wLength); #ifdef PLUGGABLE_USB_ENABLED ret = PluggableUSB().getDescriptor(setup); if (ret != 0) { return (ret > 0 ? true : false); } #endif const u8* desc_addr = 0; if (USB_DEVICE_DESCRIPTOR_TYPE == t) { if (setup.wLength == 8) _cdcComposite = 1; desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorB : (const u8*)&USB_DeviceDescriptor; } else if (USB_STRING_DESCRIPTOR_TYPE == t) { if (setup.wValueL == 0) { desc_addr = (const u8*)&STRING_LANGUAGE; } else if (setup.wValueL == IPRODUCT) { return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT), TRANSFER_PGM); } else if (setup.wValueL == IMANUFACTURER) { return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER), TRANSFER_PGM); } else if (setup.wValueL == ISERIAL) { #ifdef PLUGGABLE_USB_ENABLED char name[ISERIAL_MAX_LEN]; PluggableUSB().getShortName(name); return USB_SendStringDescriptor((uint8_t*)name, strlen(name), 0); #endif } else return false; } if (desc_addr == 0) return false; u8 desc_length = pgm_read_byte(desc_addr); USB_SendControl(TRANSFER_PGM,desc_addr,desc_length); return true; } // Endpoint 0 interrupt ISR(USB_COM_vect) { SetEP(0); if (!ReceivedSetupInt()) return; USBSetup setup; Recv((u8*)&setup,8); ClearSetupInt(); u8 requestType = setup.bmRequestType; if (requestType & REQUEST_DEVICETOHOST) WaitIN(); else ClearIN(); bool ok = true; if (REQUEST_STANDARD == (requestType & REQUEST_TYPE)) { // Standard Requests u8 r = setup.bRequest; u16 wValue = setup.wValueL | (setup.wValueH << 8); if (GET_STATUS == r) { if (requestType == (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_DEVICE)) { Send8(_usbCurrentStatus); Send8(0); } else { // TODO: handle the HALT state of an endpoint here // see "Figure 9-6. Information Returned by a GetStatus() Request to an Endpoint" in usb_20.pdf for more information Send8(0); Send8(0); } } else if (CLEAR_FEATURE == r) { if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE)) && (wValue == DEVICE_REMOTE_WAKEUP)) { _usbCurrentStatus &= ~FEATURE_REMOTE_WAKEUP_ENABLED; } } else if (SET_FEATURE == r) { if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE)) && (wValue == DEVICE_REMOTE_WAKEUP)) { _usbCurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED; } } else if (SET_ADDRESS == r) { WaitIN(); UDADDR = setup.wValueL | (1<