qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

usb_descriptor.h (8820B)


      1 /*
      2  * Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
      3  * This file is based on:
      4  *     LUFA-120219/Demos/Device/Lowlevel/KeyboardMouse
      5  *     LUFA-120219/Demos/Device/Lowlevel/GenericHID
      6  */
      7 
      8 /*
      9                          LUFA Library
     10          Copyright (C) Dean Camera, 2012.
     11 
     12     dean [at] fourwalledcubicle [dot] com
     13                      www.lufa-lib.org
     14 */
     15 
     16 /*
     17     Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)
     18     Copyright 2010  Denver Gingerich (denver [at] ossguy [dot] com)
     19 
     20     Permission to use, copy, modify, distribute, and sell this
     21     software and its documentation for any purpose is hereby granted
     22     without fee, provided that the above copyright notice appear in
     23     all copies and that both that the copyright notice and this
     24     permission notice and warranty disclaimer appear in supporting
     25     documentation, and that the name of the author not be used in
     26     advertising or publicity pertaining to distribution of the
     27     software without specific, written prior permission.
     28 
     29     The author disclaim all warranties with regard to this
     30     software, including all implied warranties of merchantability
     31     and fitness.  In no event shall the author be liable for any
     32     special, indirect or consequential damages or any damages
     33     whatsoever resulting from loss of use, data or profits, whether
     34     in an action of contract, negligence or other tortious action,
     35     arising out of or in connection with the use or performance of
     36     this software.
     37 */
     38 
     39 /** \file
     40  *
     41  *  Header file for Descriptors.c.
     42  */
     43 
     44 #pragma once
     45 
     46 #include <LUFA/Drivers/USB/USB.h>
     47 
     48 #ifdef PROTOCOL_CHIBIOS
     49 #    include <hal.h>
     50 #    if STM32_USB_USE_OTG1 == TRUE
     51 #        define USB_ENDPOINTS_ARE_REORDERABLE
     52 #    endif
     53 #endif
     54 
     55 /*
     56  * USB descriptor structure
     57  */
     58 typedef struct {
     59     USB_Descriptor_Configuration_Header_t Config;
     60 
     61 #ifndef KEYBOARD_SHARED_EP
     62     // Keyboard HID Interface
     63     USB_Descriptor_Interface_t Keyboard_Interface;
     64     USB_HID_Descriptor_HID_t   Keyboard_HID;
     65     USB_Descriptor_Endpoint_t  Keyboard_INEndpoint;
     66 #else
     67     // Shared Interface
     68     USB_Descriptor_Interface_t Shared_Interface;
     69     USB_HID_Descriptor_HID_t   Shared_HID;
     70     USB_Descriptor_Endpoint_t  Shared_INEndpoint;
     71 #endif
     72 
     73 #ifdef RAW_ENABLE
     74     // Raw HID Interface
     75     USB_Descriptor_Interface_t Raw_Interface;
     76     USB_HID_Descriptor_HID_t   Raw_HID;
     77     USB_Descriptor_Endpoint_t  Raw_INEndpoint;
     78     USB_Descriptor_Endpoint_t  Raw_OUTEndpoint;
     79 #endif
     80 
     81 #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
     82     // Mouse HID Interface
     83     USB_Descriptor_Interface_t Mouse_Interface;
     84     USB_HID_Descriptor_HID_t   Mouse_HID;
     85     USB_Descriptor_Endpoint_t  Mouse_INEndpoint;
     86 #endif
     87 
     88 #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
     89     // Shared Interface
     90     USB_Descriptor_Interface_t Shared_Interface;
     91     USB_HID_Descriptor_HID_t   Shared_HID;
     92     USB_Descriptor_Endpoint_t  Shared_INEndpoint;
     93 #endif
     94 
     95 #ifdef CONSOLE_ENABLE
     96     // Console HID Interface
     97     USB_Descriptor_Interface_t Console_Interface;
     98     USB_HID_Descriptor_HID_t   Console_HID;
     99     USB_Descriptor_Endpoint_t  Console_INEndpoint;
    100 #endif
    101 
    102 #ifdef MIDI_ENABLE
    103     USB_Descriptor_Interface_Association_t Audio_Interface_Association;
    104     // MIDI Audio Control Interface
    105     USB_Descriptor_Interface_t          Audio_ControlInterface;
    106     USB_Audio_Descriptor_Interface_AC_t Audio_ControlInterface_SPC;
    107     // MIDI Audio Streaming Interface
    108     USB_Descriptor_Interface_t                Audio_StreamInterface;
    109     USB_MIDI_Descriptor_AudioInterface_AS_t   Audio_StreamInterface_SPC;
    110     USB_MIDI_Descriptor_InputJack_t           MIDI_In_Jack_Emb;
    111     USB_MIDI_Descriptor_InputJack_t           MIDI_In_Jack_Ext;
    112     USB_MIDI_Descriptor_OutputJack_t          MIDI_Out_Jack_Emb;
    113     USB_MIDI_Descriptor_OutputJack_t          MIDI_Out_Jack_Ext;
    114     USB_Audio_Descriptor_StreamEndpoint_Std_t MIDI_In_Jack_Endpoint;
    115     USB_MIDI_Descriptor_Jack_Endpoint_t       MIDI_In_Jack_Endpoint_SPC;
    116     USB_Audio_Descriptor_StreamEndpoint_Std_t MIDI_Out_Jack_Endpoint;
    117     USB_MIDI_Descriptor_Jack_Endpoint_t       MIDI_Out_Jack_Endpoint_SPC;
    118 #endif
    119 
    120 #ifdef VIRTSER_ENABLE
    121     USB_Descriptor_Interface_Association_t CDC_Interface_Association;
    122     // CDC Control Interface
    123     USB_Descriptor_Interface_t            CDC_CCI_Interface;
    124     USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
    125     USB_CDC_Descriptor_FunctionalACM_t    CDC_Functional_ACM;
    126     USB_CDC_Descriptor_FunctionalUnion_t  CDC_Functional_Union;
    127     USB_Descriptor_Endpoint_t             CDC_NotificationEndpoint;
    128     // CDC Data Interface
    129     USB_Descriptor_Interface_t CDC_DCI_Interface;
    130     USB_Descriptor_Endpoint_t  CDC_DataOutEndpoint;
    131     USB_Descriptor_Endpoint_t  CDC_DataInEndpoint;
    132 #endif
    133 
    134 #if defined(JOYSTICK_ENABLE) && !defined(JOYSTICK_SHARED_EP)
    135     // Joystick HID Interface
    136     USB_Descriptor_Interface_t Joystick_Interface;
    137     USB_HID_Descriptor_HID_t   Joystick_HID;
    138     USB_Descriptor_Endpoint_t  Joystick_INEndpoint;
    139 #endif
    140 
    141 #if defined(DIGITIZER_ENABLE) && !defined(DIGITIZER_SHARED_EP)
    142     // Digitizer HID Interface
    143     USB_Descriptor_Interface_t Digitizer_Interface;
    144     USB_HID_Descriptor_HID_t   Digitizer_HID;
    145     USB_Descriptor_Endpoint_t  Digitizer_INEndpoint;
    146 #endif
    147 } USB_Descriptor_Configuration_t;
    148 
    149 /*
    150  * Interface indexes
    151  */
    152 enum usb_interfaces {
    153 #ifndef KEYBOARD_SHARED_EP
    154     KEYBOARD_INTERFACE,
    155 #else
    156     SHARED_INTERFACE,
    157 #    define KEYBOARD_INTERFACE SHARED_INTERFACE
    158 #endif
    159 
    160 // It is important that the Raw HID interface is at a constant
    161 // interface number, to support Linux/OSX platforms and chrome.hid
    162 // If Raw HID is enabled, let it be always 1.
    163 #ifdef RAW_ENABLE
    164     RAW_INTERFACE,
    165 #endif
    166 
    167 #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
    168     MOUSE_INTERFACE,
    169 #endif
    170 
    171 #if defined(SHARED_EP_ENABLE) && !defined(KEYBOARD_SHARED_EP)
    172     SHARED_INTERFACE,
    173 #endif
    174 
    175 #ifdef CONSOLE_ENABLE
    176     CONSOLE_INTERFACE,
    177 #endif
    178 
    179 #ifdef MIDI_ENABLE
    180     AC_INTERFACE,
    181     AS_INTERFACE,
    182 #endif
    183 
    184 #ifdef VIRTSER_ENABLE
    185     CCI_INTERFACE,
    186     CDI_INTERFACE,
    187 #endif
    188 
    189 #if defined(JOYSTICK_ENABLE) && !defined(JOYSTICK_SHARED_EP)
    190     JOYSTICK_INTERFACE,
    191 #endif
    192 
    193 #if defined(DIGITIZER_ENABLE) && !defined(DIGITIZER_SHARED_EP)
    194     DIGITIZER_INTERFACE,
    195 #endif
    196     TOTAL_INTERFACES
    197 };
    198 
    199 #define IS_VALID_INTERFACE(i) ((i) >= 0 && (i) < TOTAL_INTERFACES)
    200 
    201 #define NEXT_EPNUM __COUNTER__
    202 
    203 /*
    204  * Endpoint numbers
    205  */
    206 enum usb_endpoints {
    207     __unused_epnum__ = NEXT_EPNUM, // Endpoint numbering starts at 1
    208 
    209 #ifndef KEYBOARD_SHARED_EP
    210     KEYBOARD_IN_EPNUM = NEXT_EPNUM,
    211 #else
    212 #    define KEYBOARD_IN_EPNUM SHARED_IN_EPNUM
    213 #endif
    214 
    215 #if defined(MOUSE_ENABLE) && !defined(MOUSE_SHARED_EP)
    216     MOUSE_IN_EPNUM = NEXT_EPNUM,
    217 #else
    218 #    define MOUSE_IN_EPNUM SHARED_IN_EPNUM
    219 #endif
    220 
    221 #ifdef RAW_ENABLE
    222     RAW_IN_EPNUM = NEXT_EPNUM,
    223 #    ifdef USB_ENDPOINTS_ARE_REORDERABLE
    224 #        define RAW_OUT_EPNUM RAW_IN_EPNUM
    225 #    else
    226     RAW_OUT_EPNUM = NEXT_EPNUM,
    227 #    endif
    228 #endif
    229 
    230 #ifdef SHARED_EP_ENABLE
    231     SHARED_IN_EPNUM = NEXT_EPNUM,
    232 #endif
    233 
    234 #ifdef CONSOLE_ENABLE
    235     CONSOLE_IN_EPNUM = NEXT_EPNUM,
    236 #endif
    237 
    238 #ifdef MIDI_ENABLE
    239     MIDI_STREAM_IN_EPNUM = NEXT_EPNUM,
    240 #    ifdef USB_ENDPOINTS_ARE_REORDERABLE
    241 #        define MIDI_STREAM_OUT_EPNUM MIDI_STREAM_IN_EPNUM
    242 #    else
    243     MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM,
    244 #    endif
    245 #endif
    246 
    247 #ifdef VIRTSER_ENABLE
    248     CDC_NOTIFICATION_EPNUM = NEXT_EPNUM,
    249     CDC_IN_EPNUM           = NEXT_EPNUM,
    250 #    ifdef USB_ENDPOINTS_ARE_REORDERABLE
    251 #        define CDC_OUT_EPNUM CDC_IN_EPNUM
    252 #    else
    253     CDC_OUT_EPNUM = NEXT_EPNUM,
    254 #    endif
    255 #endif
    256 
    257 #ifdef JOYSTICK_ENABLE
    258 #    if !defined(JOYSTICK_SHARED_EP)
    259     JOYSTICK_IN_EPNUM = NEXT_EPNUM,
    260 #    else
    261 #        define JOYSTICK_IN_EPNUM SHARED_IN_EPNUM
    262 #    endif
    263 #endif
    264 
    265 #ifdef DIGITIZER_ENABLE
    266 #    if !defined(DIGITIZER_SHARED_EP)
    267     DIGITIZER_IN_EPNUM = NEXT_EPNUM,
    268 #    else
    269 #        define DIGITIZER_IN_EPNUM SHARED_IN_EPNUM
    270 #    endif
    271 #endif
    272 };
    273 
    274 #ifdef PROTOCOL_LUFA
    275 // LUFA tells us total endpoints including control
    276 #    define MAX_ENDPOINTS (ENDPOINT_TOTAL_ENDPOINTS - 1)
    277 #elif defined(PROTOCOL_CHIBIOS)
    278 // ChibiOS gives us number of available user endpoints, not control
    279 #    define MAX_ENDPOINTS USB_MAX_ENDPOINTS
    280 #endif
    281 
    282 #if (NEXT_EPNUM - 1) > MAX_ENDPOINTS
    283 #    error There are not enough available endpoints to support all functions. Please disable one or more of the following: Mouse Keys, Extra Keys, Console, NKRO, MIDI, Serial, Steno
    284 #endif
    285 
    286 #define KEYBOARD_EPSIZE 8
    287 #define SHARED_EPSIZE 32
    288 #define MOUSE_EPSIZE 16
    289 #define RAW_EPSIZE 32
    290 #define CONSOLE_EPSIZE 32
    291 #define MIDI_STREAM_EPSIZE 64
    292 #define CDC_NOTIFICATION_EPSIZE 8
    293 #define CDC_EPSIZE 16
    294 #define JOYSTICK_EPSIZE 8
    295 #define DIGITIZER_EPSIZE 8
    296 
    297 uint16_t get_usb_descriptor(const uint16_t wValue, const uint16_t wIndex, const uint16_t wLength, const void** const DescriptorAddress);