├── xmos_dfu ├── setup.sh ├── xmosdfu ├── libusb-1.0.0.dylib ├── xmosdfu.dSYM │ └── Contents │ │ ├── Resources │ │ └── DWARF │ │ │ └── xmosdfu │ │ └── Info.plist ├── Makefile ├── xmosdfu.cpp └── libusb.h ├── .gitattributes ├── .gitignore ├── LICENSE.txt ├── packaging └── linux │ └── 70-jdslabs.rules └── README.md /xmos_dfu/setup.sh: -------------------------------------------------------------------------------- 1 | export DYLD_LIBRARY_PATH=$PWD:$DYLD_LIBRARY_PATH 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /xmos_dfu/xmosdfu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdslabs/xmos_dfu/HEAD/xmos_dfu/xmosdfu -------------------------------------------------------------------------------- /xmos_dfu/libusb-1.0.0.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdslabs/xmos_dfu/HEAD/xmos_dfu/libusb-1.0.0.dylib -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | .build/ 3 | .metadata/ 4 | .settings/ 5 | bin/ 6 | .DS_Store 7 | *.xe 8 | *.bin 9 | .vs/* -------------------------------------------------------------------------------- /xmos_dfu/xmosdfu.dSYM/Contents/Resources/DWARF/xmosdfu: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jdslabs/xmos_dfu/HEAD/xmos_dfu/xmosdfu.dSYM/Contents/Resources/DWARF/xmosdfu -------------------------------------------------------------------------------- /xmos_dfu/Makefile: -------------------------------------------------------------------------------- 1 | # Common flags 2 | CFLAGS = -g 3 | CC = g++ 4 | 5 | # Linux target 6 | linux: xmosdfu.cpp 7 | $(CC) $(CFLAGS) -o xmosdfu xmosdfu.cpp -I. `pkg-config --libs libusb-1.0` 8 | 9 | # macOS targets (both do the same thing, maintained for compatibility) 10 | mac: xmosdfu.cpp 11 | $(CC) $(CFLAGS) -o xmosdfu xmosdfu.cpp -I. -IOSX $(shell brew --prefix libusb)/lib/libusb-1.0.0.dylib 12 | 13 | mac-m1: mac 14 | 15 | clean: 16 | rm -f xmosdfu 17 | -------------------------------------------------------------------------------- /xmos_dfu/xmosdfu.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.xmosdfu 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Software License Agreement 2 | 3 | Copyright (c) 2011, XMOS Ltd, All rights reserved. 4 | 5 | The copyright holders hereby grant to any person obtaining a copy of this software (the "Software") and/or its associated 6 | documentation files (the Documentation), the perpetual, irrevocable (except in the case of breach of this license) no-cost, 7 | royalty free, sublicensable rights to use, copy, modify, merge, publish, display, publicly perform, distribute, and/or 8 | sell copies of the Software and the Documentation, together or separately, and to permit persons to whom the Software and/or 9 | Documentation is furnished to do so, subject to the following conditions: 10 | 11 | . Redistributions of the Software in source code must retain the above copyright notice, this list of conditions and the 12 | following disclaimers. 13 | 14 | . Redistributions of the Software in binary form must reproduce the above copyright notice, this list of conditions and 15 | the following disclaimers in the documentation and/or other materials provided with the distribution. 16 | 17 | . Redistributions of the Documentation must retain the above copyright notice, this list of conditions and the following 18 | disclaimers. 19 | 20 | Neither the name of XMOS, nor the names of its contributors may be used to endorse or promote products derived from this 21 | Software or the Documentation without specific prior written permission of the copyright holder. 22 | 23 | THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 24 | LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 26 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION OR THE USE OF OR OTHER 27 | DEALINGS WITH THE SOFTWARE OR DOCUMENTATION. -------------------------------------------------------------------------------- /packaging/linux/70-jdslabs.rules: -------------------------------------------------------------------------------- 1 | # generated from the PID list in xmos_dfu/xmosdfu.cpp 2 | # regexp: 3 | # ^#define\s+(\S+)\s+0x(\S+)$ 4 | # 5 | # replacement string: 6 | # # $1 7 | # SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="$2", TAG+="uaccess" 8 | # SUBSYSTEM=="usb", ATTRS{idVendor}=="152A", ATTRS{idProduct}=="$2", TAG+="uaccess" 9 | # 10 | # This basically follows the logic from the code later on. If either vendor ID is found it looks for the productIDs. 11 | # 12 | 13 | # ATOMDAC2_PID 14 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="88e8", TAG+="uaccess" 15 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="88e8", TAG+="uaccess" 16 | # ATOMDAC_PID 17 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="30e1", TAG+="uaccess" 18 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="30e1", TAG+="uaccess" 19 | # EL4_PID 20 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="88fa", TAG+="uaccess" 21 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="88fa", TAG+="uaccess" 22 | # EL4_DFU_PID 23 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="88fc", TAG+="uaccess" 24 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="88fc", TAG+="uaccess" 25 | # ELEMENTII_PID 26 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="30da", TAG+="uaccess" 27 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="30da", TAG+="uaccess" 28 | # ELDACII_PID 29 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="30E0", TAG+="uaccess" 30 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="30E0", TAG+="uaccess" 31 | # ELEMENTIII_PID 32 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="8885", TAG+="uaccess" 33 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="8885", TAG+="uaccess" 34 | # ELDACIIPLUS_PID 35 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="8886", TAG+="uaccess" 36 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="8886", TAG+="uaccess" 37 | # ELDACIIPLUSBal_PID 38 | SUBSYSTEM=="usb", ATTRS{idVendor}=="20b1", ATTRS{idProduct}=="8889", TAG+="uaccess" 39 | SUBSYSTEM=="usb", ATTRS{idVendor}=="152a", ATTRS{idProduct}=="8889", TAG+="uaccess" 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DFU Loader for XMOS based JDS Labs DACs 2 | This script was originally distributed as a component of the [XMOS USB Audio 2.0 Reference Software](http://www.xmos.com/products/development-kits/usbaudio2) Version 6.15.2, and has been modified to facilitate firmware updates via DFU mode for the following XMOS based JDS Labs, Inc. devices: 3 | 4 | - Atom DAC(+) 5 | - EL DAC II(+) 6 | - Element II 7 | - Element III 8 | 9 | ## License 10 | A copy of the original AS-IS [License](https://github.com/jdslabs/xmos_dfu/blob/main/LICENSE.txt) is included within this repository. 11 | 12 | ## General Knowledge 13 | - A _direct_ connection to a USB 2.0/3.0 port is required. USB hubs and some Type C or USB 3.1 ports may prevent DFU communication. 14 | - This script only supports the USB devices listed above. Only connect one (1) JDS Labs DAC at a time! 15 | - While this repository is customized for JDS Labs USB VID/PIDs, the `pidList[]` in `xmosdfu.cpp` can be modiified to suit any XMOS devices with DFU capability. 16 | 17 | ## WARNING 18 | This command line tool only checks for a valid JDS Labs USB Vendor and Product ID before loading firmware to your DAC. It does *not* check the .bin file you supply, nor can it check that the DAC matches the .bin file you provide. Thus, triple check that you are loading the right firmware for the right model! 19 | 20 | In the event that you accidentally load firmware for the wrong JDS Labs DAC, you can normally recover by flashing the intended firmware. If not, please reach out to jdslabs.com/support 21 | 22 | # Usage in macOS 23 | This process has been tested under Big Sur and Monterey on both Intel and M1 based Macs. 24 | For Monterey, please see #4 25 | ## Prerequisites 26 | - Install XCode Command Line Tools from the App Store, or from a command line: `xcode-select --install` 27 | - Install [Homebrew](https://brew.sh/). Make sure to run commands presented towards the end of installation to add Homebrew to your PATH. 28 | - Install libusb: `brew install libusb` 29 | 30 | ## Loading Firmware in macOS 31 | 1. Open a terminal in the `/xmos_dfu/xmos_dfu` folder and compile the Makefile using the appropriate target for your system. Warnings may be ignored. 32 | 33 | #### Macs (Intel or Apple Silicon): 34 | ```sh 35 | make -f Makefile mac 36 | ``` 37 | 38 | 2. Next, run: 39 | ``` 40 | chmod +x setup.sh 41 | chmod +x xmosdfu 42 | source setup.sh 43 | ``` 44 | 45 | 3. Make sure your DAC is connected by USB, then run the following command to verify the connection and current firmware version. Your DAC will appear as “VID = 0x20b1” or "VID = 0x152A: 46 | ``` 47 | ./xmosdfu --listdevices 48 | ``` 49 | 50 | 4. To flash new firmware, download the [desired .bin file](https://blog.jdslabs.com/2020/08/custom-firmware-builds-for-element-ii-el-dac-ii-and-atom-dac/), then run the command below. *Only 1 DAC may be plugged in at a time!* 51 | You will need to edit the path to reflect the full path of your downloaded firmware file (tip: select the downloaded file in Finder, then right click on its path in the bottom of the window and choose Copy as Pathname): 52 | ``` 53 | sudo ./xmosdfu --download "/edit/this/path/Atom_DAC_Plus-v184.bin" 54 | ``` 55 | 56 | # Usage in Ubuntu 20.0.4 57 | ## Prerequisites 58 | - Install libusb: `sudo apt-get install libusb-1.0-0-dev` 59 | 60 | ## Loading Firmware in Ubuntu 61 | 62 | 1. Open a terminal in the `/xmos_dfu/xmos_dfu` folder and compile Makefile.Linux. Warnings may be ignored: 63 | ``` 64 | make -f Makefile linux 65 | ``` 66 | 2. Next, run: 67 | ``` 68 | source setup.sh 69 | ``` 70 | 71 | 3. Make sure your DAC is connected by USB, then run the following command to verify the connection and current firmware version. Your DAC will appear as “VID = 0x20b1” or "VID = 0x152A: 72 | ``` 73 | sudo ./xmosdfu --listdevices 74 | ``` 75 | 4. To flash new firmware, download the [desired .bin file](https://blog.jdslabs.com/2020/08/custom-firmware-builds-for-element-ii-el-dac-ii-and-atom-dac/), then run the command below as `sudo`. *Only 1 DAC may be plugged in at a time!* 76 | You will need to edit the path to reflect the full path of your downloaded firmware file: 77 | ``` 78 | sudo ./xmosdfu --download "/edit/this/path/Atom_DAC_Plus-v184.bin" 79 | ``` 80 | -------------------------------------------------------------------------------- /xmos_dfu/xmosdfu.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | This script was originally distributed as a component of the XMOS USB Audio 2.0 Reference Software Version 6.15.2, and has been modified to 3 | facilitate firmware updates via DFU mode for the following XMOS based JDS Labs, Inc. devices: 4 | - Atom DAC(+) 5 | - EL DAC II(+) 6 | - Element II 7 | - Element III 8 | - Element IV 9 | 10 | A copy of the original AS-IS (MIT) License, found below, is also included in the source repository: 11 | https://github.com/jdslabs/xmos_dfu/blob/main/LICENSE.txt 12 | 13 | Software License Agreement 14 | 15 | Copyright (c) 2011, XMOS Ltd, All rights reserved. 16 | 17 | The copyright holders hereby grant to any person obtaining a copy of this software (the "Software") and/or its associated 18 | documentation files (the Documentation), the perpetual, irrevocable (except in the case of breach of this license) no-cost, 19 | royalty free, sublicensable rights to use, copy, modify, merge, publish, display, publicly perform, distribute, and/or 20 | sell copies of the Software and the Documentation, together or separately, and to permit persons to whom the Software and/or 21 | Documentation is furnished to do so, subject to the following conditions: 22 | 23 | . Redistributions of the Software in source code must retain the above copyright notice, this list of conditions and the 24 | following disclaimers. 25 | 26 | . Redistributions of the Software in binary form must reproduce the above copyright notice, this list of conditions and 27 | the following disclaimers in the documentation and/or other materials provided with the distribution. 28 | 29 | . Redistributions of the Documentation must retain the above copyright notice, this list of conditions and the following 30 | disclaimers. 31 | 32 | Neither the name of XMOS, nor the names of its contributors may be used to endorse or promote products derived from this 33 | Software or the Documentation without specific prior written permission of the copyright holder. 34 | 35 | THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 36 | LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 37 | CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 38 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION OR THE USE OF OR OTHER 39 | DEALINGS WITH THE SOFTWARE OR DOCUMENTATION. 40 | */ 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | /* libusb 1.0.0-dev must be installed. Please see README */ 48 | #ifdef __linux 49 | #include 50 | #endif 51 | 52 | #ifdef __APPLE__ 53 | #include "libusb.h" 54 | #endif 55 | 56 | /* the device's vendor and product id */ 57 | #define XMOS_VID 0x20b1 58 | #define THESYCON_VID 0x152A 59 | 60 | #define ATOMDAC2_PID 0x88E8 61 | #define ATOMDAC_PID 0x30E1 62 | #define EL4_PID 0x88fa 63 | #define EL4_DFU_PID 0x88fc 64 | #define ELEMENTII_PID 0x30DA 65 | #define ELDACII_PID 0x30E0 66 | #define ELEMENTIII_PID 0x8885 67 | #define ELDACIIPLUS_PID 0x8886 68 | #define ELDACIIPLUSBal_PID 0x8889 69 | 70 | unsigned short pidList[] = {ATOMDAC2_PID, 71 | ATOMDAC_PID, 72 | EL4_PID, 73 | EL4_DFU_PID, 74 | ELEMENTII_PID, 75 | ELDACII_PID, 76 | ELEMENTIII_PID, 77 | ELDACIIPLUS_PID, 78 | ELDACIIPLUSBal_PID}; 79 | 80 | unsigned int XMOS_DFU_IF = 0; 81 | 82 | #define DFU_REQUEST_TO_DEV 0x21 83 | #define DFU_REQUEST_FROM_DEV 0xa1 84 | 85 | // Standard DFU requests 86 | #define DFU_DETACH 0 87 | #define DFU_DNLOAD 1 88 | #define DFU_UPLOAD 2 89 | #define DFU_GETSTATUS 3 90 | #define DFU_CLRSTATUS 4 91 | #define DFU_GETSTATE 5 92 | #define DFU_ABORT 6 93 | 94 | // XMOS alternate setting requests 95 | #define XMOS_DFU_RESETDEVICE 0xf0 96 | #define XMOS_DFU_REVERTFACTORY 0xf1 97 | #define XMOS_DFU_RESETINTODFU 0xf2 98 | #define XMOS_DFU_RESETFROMDFU 0xf3 99 | #define XMOS_DFU_SAVESTATE 0xf5 100 | #define XMOS_DFU_RESTORESTATE 0xf6 101 | 102 | // At the top of the file, add color definitions 103 | #define COLOR_GREEN "\033[1;32m" 104 | #define COLOR_BLUE "\033[38;5;33m" 105 | #define COLOR_RESET "\033[0m" 106 | 107 | static libusb_device_handle *devh = NULL; 108 | 109 | static int find_xmos_device(unsigned int id, unsigned int list) 110 | { 111 | libusb_device *dev; 112 | libusb_device **devs; 113 | int i = 0; 114 | int found = 0; 115 | 116 | libusb_get_device_list(NULL, &devs); 117 | 118 | while ((dev = devs[i++]) != NULL) 119 | { 120 | int foundDev = 0; 121 | struct libusb_device_descriptor desc; 122 | libusb_get_device_descriptor(dev, &desc); 123 | 124 | if(desc.idVendor == XMOS_VID || desc.idVendor == THESYCON_VID) 125 | { 126 | for(int j = 0; j < sizeof(pidList)/sizeof(unsigned short); j++) 127 | { 128 | if(desc.idProduct == pidList[j] && !list) 129 | { 130 | foundDev = 1; 131 | break; 132 | } 133 | } 134 | } 135 | 136 | if (foundDev || list) 137 | { 138 | libusb_device_handle *handle; 139 | if (libusb_open(dev, &handle) == 0) 140 | { 141 | unsigned char product[256]; 142 | if (libusb_get_string_descriptor_ascii(handle, desc.iProduct, product, sizeof(product)) > 0) 143 | { 144 | // Format firmware version from hex to decimal (1.2.0 format) 145 | int major = (desc.bcdDevice >> 8) & 0xFF; 146 | int minor = (desc.bcdDevice >> 4) & 0x0F; 147 | int patch = desc.bcdDevice & 0x0F; 148 | 149 | printf("- VID: 0x%x, PID: 0x%x | %s - Firmware v%d.%d.%d\n", 150 | desc.idVendor, desc.idProduct, product, major, minor, patch); 151 | } 152 | else 153 | { 154 | printf("- VID: 0x%x, PID: 0x%x | Unknown Device - Firmware v%x\n", 155 | desc.idVendor, desc.idProduct, desc.bcdDevice); 156 | } 157 | 158 | if (foundDev && found == id) 159 | { 160 | devh = handle; 161 | libusb_config_descriptor *config_desc = NULL; 162 | libusb_get_active_config_descriptor(dev, &config_desc); 163 | if (config_desc != NULL) 164 | { 165 | for (int j = 0; j < config_desc->bNumInterfaces; j++) 166 | { 167 | const libusb_interface_descriptor *inter_desc = 168 | ((libusb_interface *)&config_desc->interface[j])->altsetting; 169 | if (inter_desc->bInterfaceClass == 0xFE && 170 | inter_desc->bInterfaceSubClass == 0x1) 171 | { 172 | XMOS_DFU_IF = j; 173 | } 174 | } 175 | } 176 | else 177 | { 178 | XMOS_DFU_IF = 0; 179 | } 180 | break; 181 | } 182 | else 183 | { 184 | libusb_close(handle); 185 | } 186 | 187 | if (foundDev) found++; 188 | } 189 | } 190 | } 191 | 192 | libusb_free_device_list(devs, 1); 193 | 194 | return devh ? 0 : -1; 195 | } 196 | 197 | int xmos_dfu_resetdevice(void) { 198 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETDEVICE, 0, 0, NULL, 0, 0); 199 | return 0; 200 | } 201 | 202 | int xmos_dfu_revertfactory(void) { 203 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_REVERTFACTORY, 0, 0, NULL, 0, 0); 204 | return 0; 205 | } 206 | 207 | int xmos_dfu_resetintodfu(unsigned int interface) { 208 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETINTODFU, 0, interface, NULL, 0, 0); 209 | return 0; 210 | } 211 | 212 | int xmos_dfu_resetfromdfu(unsigned int interface) { 213 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETFROMDFU, 0, interface, NULL, 0, 0); 214 | return 0; 215 | } 216 | 217 | int dfu_detach(unsigned int interface, unsigned int timeout) { 218 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_DETACH, timeout, interface, NULL, 0, 0); 219 | return 0; 220 | } 221 | 222 | int dfu_getState(unsigned int interface, unsigned char *state) { 223 | libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_GETSTATE, 0, interface, state, 1, 0); 224 | return 0; 225 | } 226 | 227 | int dfu_getStatus(unsigned int interface, unsigned char *state, unsigned int *timeout, 228 | unsigned char *nextState, unsigned char *strIndex) { 229 | unsigned int data[2]; 230 | libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_GETSTATUS, 0, interface, (unsigned char *)data, 6, 0); 231 | 232 | *state = data[0] & 0xff; 233 | *timeout = (data[0] >> 8) & 0xffffff; 234 | *nextState = data[1] & 0xff; 235 | *strIndex = (data[1] >> 8) & 0xff; 236 | return 0; 237 | } 238 | 239 | int dfu_clrStatus(unsigned int interface) { 240 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_CLRSTATUS, 0, interface, NULL, 0, 0); 241 | return 0; 242 | } 243 | 244 | int dfu_abort(unsigned int interface) { 245 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_ABORT, 0, interface, NULL, 0, 0); 246 | return 0; 247 | } 248 | 249 | 250 | int xmos_dfu_save_state(unsigned int interface) { 251 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_SAVESTATE, 0, interface, NULL, 0, 0); 252 | printf("Save state command sent\n"); 253 | return 0; 254 | } 255 | 256 | int xmos_dfu_restore_state(unsigned int interface) { 257 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESTORESTATE, 0, interface, NULL, 0, 0); 258 | printf("Restore state command sent\n"); 259 | return 0; 260 | } 261 | 262 | int dfu_download(unsigned int interface, unsigned int block_num, unsigned int size, unsigned char *data) { 263 | //printf("... Downloading block number %d size %d\r", block_num, size); 264 | libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_DNLOAD, block_num, interface, data, size, 0); 265 | return 0; 266 | } 267 | 268 | int dfu_upload(unsigned int interface, unsigned int block_num, unsigned int size, unsigned char*data) { 269 | unsigned int numBytes = 0; 270 | numBytes = libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_UPLOAD, block_num, interface, (unsigned char *)data, size, 0); 271 | return numBytes; 272 | } 273 | 274 | int write_dfu_image(char *file) { 275 | int i = 0; 276 | FILE* inFile = NULL; 277 | int image_size = 0; 278 | unsigned int num_blocks = 0; 279 | unsigned int block_size = 64; 280 | unsigned int remainder = 0; 281 | unsigned char block_data[256]; 282 | 283 | unsigned char dfuState = 0; 284 | unsigned char nextDfuState = 0; 285 | unsigned int timeout = 0; 286 | unsigned char strIndex = 0; 287 | unsigned int dfuBlockCount = 0; 288 | 289 | inFile = fopen( file, "rb" ); 290 | if( inFile == NULL ) { 291 | fprintf(stderr,"Error: Failed to open input data file.\n"); 292 | return -1; 293 | } 294 | 295 | /* Discover the size of the image. */ 296 | if( 0 != fseek( inFile, 0, SEEK_END ) ) { 297 | fprintf(stderr,"Error: Failed to discover input data file size.\n"); 298 | return -1; 299 | } 300 | 301 | image_size = (int)ftell( inFile ); 302 | 303 | if( 0 != fseek( inFile, 0, SEEK_SET ) ) { 304 | fprintf(stderr,"Error: Failed to input file pointer.\n"); 305 | return -1; 306 | } 307 | 308 | num_blocks = image_size/block_size; 309 | remainder = image_size - (num_blocks * block_size); 310 | 311 | printf("... Sending image (%s) to device\n", file); 312 | printf("[%s", COLOR_BLUE); // Use BLUE for progress bar 313 | fflush(stdout); 314 | 315 | dfuBlockCount = 0; 316 | 317 | // Calculate progress bar width 318 | const int progress_width = 50; 319 | int last_percent = -1; 320 | 321 | for (i = 0; i < num_blocks; i++) { 322 | memset(block_data, 0x0, block_size); 323 | fread(block_data, 1, block_size, inFile); 324 | dfu_download(0, dfuBlockCount, block_size, block_data); 325 | dfu_getStatus(0, &dfuState, &timeout, &nextDfuState, &strIndex); 326 | dfuBlockCount++; 327 | 328 | // Update progress bar 329 | int percent = (i * 100) / num_blocks; 330 | if (percent != last_percent) { 331 | int num_chars = (percent * progress_width) / 100; 332 | while (last_percent < percent) { 333 | if (last_percent % 2 == 0) { 334 | printf("#"); 335 | fflush(stdout); 336 | } 337 | last_percent++; 338 | } 339 | } 340 | } 341 | 342 | if (remainder) { 343 | memset(block_data, 0x0, block_size); 344 | fread(block_data, 1, remainder, inFile); 345 | dfu_download(0, dfuBlockCount, block_size, block_data); 346 | dfu_getStatus(0, &dfuState, &timeout, &nextDfuState, &strIndex); 347 | 348 | // Fill the rest of the progress bar 349 | while (last_percent < 100) { 350 | if (last_percent % 2 == 0) { 351 | printf("#"); 352 | fflush(stdout); 353 | } 354 | last_percent++; 355 | } 356 | } 357 | 358 | // 0 length download terminates 359 | dfu_download(0, 0, 0, NULL); 360 | dfu_getStatus(0, &dfuState, &timeout, &nextDfuState, &strIndex); 361 | 362 | printf("%s", COLOR_RESET); // Reset color after progress bar 363 | printf("] 100%%\n"); 364 | 365 | // Get current time 366 | time_t now = time(NULL); 367 | struct tm *t = localtime(&now); 368 | char time_str[16]; 369 | strftime(time_str, sizeof(time_str), "%I:%M%p", t); 370 | 371 | // Convert to lowercase am/pm 372 | for(char *p = time_str; *p; p++) { 373 | if(*p >= 'A' && *p <= 'Z') *p |= 0x20; 374 | } 375 | 376 | printf("... %sFirmware update complete @ %s%s\n", COLOR_GREEN, time_str, COLOR_RESET); 377 | 378 | return 0; 379 | } 380 | 381 | int read_dfu_image(char *file) { 382 | FILE *outFile = NULL; 383 | unsigned int block_count = 0; 384 | unsigned int block_size = 64; 385 | unsigned char block_data[64]; 386 | 387 | outFile = fopen( file, "wb" ); 388 | if( outFile == NULL ) { 389 | fprintf(stderr,"Error: Failed to open output data file.\n"); 390 | return -1; 391 | } 392 | 393 | printf("... Uploading image (%s) from device\n", file); 394 | 395 | while (1) { 396 | unsigned int numBytes = 0; 397 | numBytes = dfu_upload(0, block_count, 64, block_data); 398 | if (numBytes == 0) 399 | break; 400 | fwrite(block_data, 1, block_size, outFile); 401 | block_count++; 402 | } 403 | 404 | return fclose(outFile); 405 | } 406 | 407 | int main(int argc, char **argv) { 408 | int r = 1; 409 | unsigned char dfuState = 0; 410 | unsigned char nextDfuState = 0; 411 | unsigned int timeout = 0; 412 | unsigned char strIndex = 0; 413 | 414 | unsigned int download = 0; 415 | unsigned int upload = 0; 416 | unsigned int revert = 0; 417 | unsigned int save = 0; 418 | unsigned int restore = 0; 419 | unsigned int listdev = 0; 420 | 421 | char *firmware_filename = NULL; 422 | 423 | if (argc < 2) { 424 | fprintf(stderr, "No options passed to DFU application\n"); 425 | return -1; 426 | } 427 | 428 | if (strcmp(argv[1], "--download") == 0) { 429 | if (argv[2]) { 430 | firmware_filename = argv[2]; 431 | } else { 432 | fprintf(stderr, "No filename specified for download option\n"); 433 | return -1; 434 | } 435 | download = 1; 436 | } else if (strcmp(argv[1], "--upload") == 0) { 437 | if (argv[2]) { 438 | firmware_filename = argv[2]; 439 | } else { 440 | fprintf(stderr, "No filename specified for upload option\n"); 441 | return -1; 442 | } 443 | upload = 1; 444 | } else if (strcmp(argv[1], "--revertfactory") == 0) { 445 | revert = 1; 446 | } 447 | else if(strcmp(argv[1], "--savecustomstate") == 0) 448 | { 449 | save = 1; 450 | } 451 | else if(strcmp(argv[1], "--restorecustomstate") == 0) 452 | { 453 | restore = 1; 454 | } 455 | else if(strcmp(argv[1], "--listdevices") == 0) 456 | { 457 | listdev = 1; 458 | } 459 | else { 460 | fprintf(stderr, "Invalid option passed to dfu application\n"); 461 | return -1; 462 | } 463 | 464 | 465 | 466 | r = libusb_init(NULL); 467 | if (r < 0) { 468 | fprintf(stderr, "failed to initialise libusb\n"); 469 | return -1; 470 | } 471 | 472 | r = find_xmos_device(0, listdev); 473 | if (r < 0) 474 | { 475 | if(!listdev) 476 | { 477 | fprintf(stderr, "Could not find/open an XMOS based device. Is the device is listed above, and did you use sudo?\n"); 478 | return -1; 479 | } 480 | return 0; 481 | } 482 | 483 | r = libusb_claim_interface(devh, XMOS_DFU_IF); 484 | if (r < 0) { 485 | fprintf(stderr, "Error claiming interface %d %d\n", XMOS_DFU_IF, r); 486 | return -1; 487 | } 488 | printf("XMOS DFU application started - Interface %d claimed\n", XMOS_DFU_IF); 489 | 490 | /* Dont go into DFU mode for save/restore */ 491 | if(save) 492 | { 493 | xmos_dfu_save_state(XMOS_DFU_IF); 494 | } 495 | else if(restore) 496 | { 497 | xmos_dfu_restore_state(XMOS_DFU_IF); 498 | } 499 | else if(!listdev) 500 | { 501 | 502 | printf("Detaching XMOS device from application mode.\n"); 503 | xmos_dfu_resetintodfu(XMOS_DFU_IF); 504 | 505 | libusb_release_interface(devh, XMOS_DFU_IF); 506 | libusb_close(devh); 507 | 508 | 509 | printf("Waiting for XMOS device to restart and enter DFU mode...\n"); 510 | 511 | // Wait for device to enter dfu mode and restart 512 | system("sleep 3"); 513 | 514 | // NOW IN DFU APPLICATION MODE 515 | 516 | r = find_xmos_device(0, 0); 517 | if (r < 0) { 518 | fprintf(stderr, "Could not find/open DFU device\n"); 519 | return -1; 520 | } 521 | 522 | r = libusb_claim_interface(devh, 0); 523 | if (r != 0) 524 | { 525 | fprintf(stderr, "Error claiming DFU interface 0\n"); 526 | 527 | switch(r) 528 | { 529 | case LIBUSB_ERROR_NOT_FOUND: 530 | printf("The requested interface does not exist'n"); 531 | break; 532 | case LIBUSB_ERROR_BUSY: 533 | printf("Another program or driver has claimed the interface\n"); 534 | break; 535 | case LIBUSB_ERROR_NO_DEVICE: 536 | printf("The device has been disconnected\n"); 537 | break; 538 | case LIBUSB_ERROR_ACCESS: 539 | printf("LIBUSB_ERROR_ACCESS: Access denied. Try another USB port or reboot?\n"); 540 | break; 541 | default: 542 | printf("Unknown error code: %d\n", r); 543 | break; 544 | 545 | } 546 | return -1; 547 | } 548 | 549 | printf("... DFU firmware upgrade device opened\n"); 550 | 551 | if (download) { 552 | write_dfu_image(firmware_filename); 553 | xmos_dfu_resetfromdfu(XMOS_DFU_IF); 554 | } else if (upload) { 555 | read_dfu_image(firmware_filename); 556 | xmos_dfu_resetfromdfu(XMOS_DFU_IF); 557 | } else if (revert) { 558 | printf("... Reverting DAC to factory image\n"); 559 | xmos_dfu_revertfactory(); 560 | // Give device time to revert firmware 561 | system("sleep 2"); 562 | xmos_dfu_resetfromdfu(XMOS_DFU_IF); 563 | } 564 | else{ 565 | xmos_dfu_resetfromdfu(XMOS_DFU_IF); 566 | } 567 | 568 | printf("... Returning DAC to application mode\n"); 569 | 570 | } // END OF DFU APPLICATION MODE 571 | 572 | libusb_release_interface(devh, 0); 573 | libusb_close(devh); 574 | libusb_exit(NULL); 575 | 576 | return 1; 577 | } 578 | -------------------------------------------------------------------------------- /xmos_dfu/libusb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Public libusb header file 3 | * Copyright (C) 2007-2008 Daniel Drake 4 | * Copyright (c) 2001 Johannes Erdfelt 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 | */ 20 | 21 | #ifndef __LIBUSB_H__ 22 | #define __LIBUSB_H__ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /** \def libusb_cpu_to_le16 35 | * \ingroup misc 36 | * Convert a 16-bit value from host-endian to little-endian format. On 37 | * little endian systems, this function does nothing. On big endian systems, 38 | * the bytes are swapped. 39 | * \param x the host-endian value to convert 40 | * \returns the value in little-endian byte order 41 | */ 42 | #define libusb_cpu_to_le16(x) ({ \ 43 | union { \ 44 | uint8_t b8[2]; \ 45 | uint16_t b16; \ 46 | } _tmp; \ 47 | uint16_t _tmp2 = (uint16_t)(x); \ 48 | _tmp.b8[1] = _tmp2 >> 8; \ 49 | _tmp.b8[0] = _tmp2 & 0xff; \ 50 | _tmp.b16; \ 51 | }) 52 | 53 | /** \def libusb_le16_to_cpu 54 | * \ingroup misc 55 | * Convert a 16-bit value from little-endian to host-endian format. On 56 | * little endian systems, this function does nothing. On big endian systems, 57 | * the bytes are swapped. 58 | * \param x the little-endian value to convert 59 | * \returns the value in host-endian byte order 60 | */ 61 | #define libusb_le16_to_cpu libusb_cpu_to_le16 62 | 63 | /* standard USB stuff */ 64 | 65 | /** \ingroup desc 66 | * Device and/or Interface Class codes */ 67 | enum libusb_class_code { 68 | /** In the context of a \ref libusb_device_descriptor "device descriptor", 69 | * this bDeviceClass value indicates that each interface specifies its 70 | * own class information and all interfaces operate independently. 71 | */ 72 | LIBUSB_CLASS_PER_INTERFACE = 0, 73 | 74 | /** Audio class */ 75 | LIBUSB_CLASS_AUDIO = 1, 76 | 77 | /** Communications class */ 78 | LIBUSB_CLASS_COMM = 2, 79 | 80 | /** Human Interface Device class */ 81 | LIBUSB_CLASS_HID = 3, 82 | 83 | /** Printer dclass */ 84 | LIBUSB_CLASS_PRINTER = 7, 85 | 86 | /** Picture transfer protocol class */ 87 | LIBUSB_CLASS_PTP = 6, 88 | 89 | /** Mass storage class */ 90 | LIBUSB_CLASS_MASS_STORAGE = 8, 91 | 92 | /** Hub class */ 93 | LIBUSB_CLASS_HUB = 9, 94 | 95 | /** Data class */ 96 | LIBUSB_CLASS_DATA = 10, 97 | 98 | /** Class is vendor-specific */ 99 | LIBUSB_CLASS_VENDOR_SPEC = 0xff 100 | }; 101 | 102 | /** \ingroup desc 103 | * Descriptor types as defined by the USB specification. */ 104 | enum libusb_descriptor_type { 105 | /** Device descriptor. See libusb_device_descriptor. */ 106 | LIBUSB_DT_DEVICE = 0x01, 107 | 108 | /** Configuration descriptor. See libusb_config_descriptor. */ 109 | LIBUSB_DT_CONFIG = 0x02, 110 | 111 | /** String descriptor */ 112 | LIBUSB_DT_STRING = 0x03, 113 | 114 | /** Interface descriptor. See libusb_interface_descriptor. */ 115 | LIBUSB_DT_INTERFACE = 0x04, 116 | 117 | /** Endpoint descriptor. See libusb_endpoint_descriptor. */ 118 | LIBUSB_DT_ENDPOINT = 0x05, 119 | 120 | /** HID descriptor */ 121 | LIBUSB_DT_HID = 0x21, 122 | 123 | /** HID report descriptor */ 124 | LIBUSB_DT_REPORT = 0x22, 125 | 126 | /** Physical descriptor */ 127 | LIBUSB_DT_PHYSICAL = 0x23, 128 | 129 | /** Hub descriptor */ 130 | LIBUSB_DT_HUB = 0x29 131 | }; 132 | 133 | /* Descriptor sizes per descriptor type */ 134 | #define LIBUSB_DT_DEVICE_SIZE 18 135 | #define LIBUSB_DT_CONFIG_SIZE 9 136 | #define LIBUSB_DT_INTERFACE_SIZE 9 137 | #define LIBUSB_DT_ENDPOINT_SIZE 7 138 | #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 139 | #define LIBUSB_DT_HUB_NONVAR_SIZE 7 140 | 141 | #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ 142 | #define LIBUSB_ENDPOINT_DIR_MASK 0x80 143 | 144 | /** \ingroup desc 145 | * Endpoint direction. Values for bit 7 of the 146 | * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme. 147 | */ 148 | enum libusb_endpoint_direction { 149 | /** In: device-to-host */ 150 | LIBUSB_ENDPOINT_IN = 0x80, 151 | 152 | /** Out: host-to-device */ 153 | LIBUSB_ENDPOINT_OUT = 0x00 154 | }; 155 | 156 | #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */ 157 | 158 | /** \ingroup desc 159 | * Endpoint transfer type. Values for bits 0:1 of the 160 | * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field. 161 | */ 162 | enum libusb_transfer_type { 163 | /** Control endpoint */ 164 | LIBUSB_TRANSFER_TYPE_CONTROL = 0, 165 | 166 | /** Isochronous endpoint */ 167 | LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, 168 | 169 | /** Bulk endpoint */ 170 | LIBUSB_TRANSFER_TYPE_BULK = 2, 171 | 172 | /** Interrupt endpoint */ 173 | LIBUSB_TRANSFER_TYPE_INTERRUPT = 3 174 | }; 175 | 176 | /** \ingroup misc 177 | * Standard requests, as defined in table 9-3 of the USB2 specifications */ 178 | enum libusb_standard_request { 179 | /** Request status of the specific recipient */ 180 | LIBUSB_REQUEST_GET_STATUS = 0x00, 181 | 182 | /** Clear or disable a specific feature */ 183 | LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, 184 | 185 | /* 0x02 is reserved */ 186 | 187 | /** Set or enable a specific feature */ 188 | LIBUSB_REQUEST_SET_FEATURE = 0x03, 189 | 190 | /* 0x04 is reserved */ 191 | 192 | /** Set device address for all future accesses */ 193 | LIBUSB_REQUEST_SET_ADDRESS = 0x05, 194 | 195 | /** Get the specified descriptor */ 196 | LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, 197 | 198 | /** Used to update existing descriptors or add new descriptors */ 199 | LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, 200 | 201 | /** Get the current device configuration value */ 202 | LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, 203 | 204 | /** Set device configuration */ 205 | LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, 206 | 207 | /** Return the selected alternate setting for the specified interface */ 208 | LIBUSB_REQUEST_GET_INTERFACE = 0x0A, 209 | 210 | /** Select an alternate interface for the specified interface */ 211 | LIBUSB_REQUEST_SET_INTERFACE = 0x0B, 212 | 213 | /** Set then report an endpoint's synchronization frame */ 214 | LIBUSB_REQUEST_SYNCH_FRAME = 0x0C 215 | }; 216 | 217 | /** \ingroup misc 218 | * Request type bits of the 219 | * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control 220 | * transfers. */ 221 | enum libusb_request_type { 222 | /** Standard */ 223 | LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), 224 | 225 | /** Class */ 226 | LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), 227 | 228 | /** Vendor */ 229 | LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), 230 | 231 | /** Reserved */ 232 | LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5) 233 | }; 234 | 235 | /** \ingroup misc 236 | * Recipient bits of the 237 | * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control 238 | * transfers. Values 4 through 31 are reserved. */ 239 | enum libusb_request_recipient { 240 | /** Device */ 241 | LIBUSB_RECIPIENT_DEVICE = 0x00, 242 | 243 | /** Interface */ 244 | LIBUSB_RECIPIENT_INTERFACE = 0x01, 245 | 246 | /** Endpoint */ 247 | LIBUSB_RECIPIENT_ENDPOINT = 0x02, 248 | 249 | /** Other */ 250 | LIBUSB_RECIPIENT_OTHER = 0x03 251 | }; 252 | 253 | #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C 254 | 255 | /** \ingroup desc 256 | * Synchronization type for isochronous endpoints. Values for bits 2:3 of the 257 | * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in 258 | * libusb_endpoint_descriptor. 259 | */ 260 | enum libusb_iso_sync_type { 261 | /** No synchronization */ 262 | LIBUSB_ISO_SYNC_TYPE_NONE = 0, 263 | 264 | /** Asynchronous */ 265 | LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, 266 | 267 | /** Adaptive */ 268 | LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, 269 | 270 | /** Synchronous */ 271 | LIBUSB_ISO_SYNC_TYPE_SYNC = 3 272 | }; 273 | 274 | #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 275 | 276 | /** \ingroup desc 277 | * Usage type for isochronous endpoints. Values for bits 4:5 of the 278 | * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in 279 | * libusb_endpoint_descriptor. 280 | */ 281 | enum libusb_iso_usage_type { 282 | /** Data endpoint */ 283 | LIBUSB_ISO_USAGE_TYPE_DATA = 0, 284 | 285 | /** Feedback endpoint */ 286 | LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, 287 | 288 | /** Implicit feedback Data endpoint */ 289 | LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2 290 | }; 291 | 292 | /** \ingroup desc 293 | * A structure representing the standard USB device descriptor. This 294 | * descriptor is documented in section 9.6.1 of the USB 2.0 specification. 295 | * All multiple-byte fields are represented in host-endian format. 296 | */ 297 | struct libusb_device_descriptor { 298 | /** Size of this descriptor (in bytes) */ 299 | uint8_t bLength; 300 | 301 | /** Descriptor type. Will have value 302 | * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this 303 | * context. */ 304 | uint8_t bDescriptorType; 305 | 306 | /** USB specification release number in binary-coded decimal. A value of 307 | * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */ 308 | uint16_t bcdUSB; 309 | 310 | /** USB-IF class code for the device. See \ref libusb_class_code. */ 311 | uint8_t bDeviceClass; 312 | 313 | /** USB-IF subclass code for the device, qualified by the bDeviceClass 314 | * value */ 315 | uint8_t bDeviceSubClass; 316 | 317 | /** USB-IF protocol code for the device, qualified by the bDeviceClass and 318 | * bDeviceSubClass values */ 319 | uint8_t bDeviceProtocol; 320 | 321 | /** Maximum packet size for endpoint 0 */ 322 | uint8_t bMaxPacketSize0; 323 | 324 | /** USB-IF vendor ID */ 325 | uint16_t idVendor; 326 | 327 | /** USB-IF product ID */ 328 | uint16_t idProduct; 329 | 330 | /** Device release number in binary-coded decimal */ 331 | uint16_t bcdDevice; 332 | 333 | /** Index of string descriptor describing manufacturer */ 334 | uint8_t iManufacturer; 335 | 336 | /** Index of string descriptor describing product */ 337 | uint8_t iProduct; 338 | 339 | /** Index of string descriptor containing device serial number */ 340 | uint8_t iSerialNumber; 341 | 342 | /** Number of possible configurations */ 343 | uint8_t bNumConfigurations; 344 | }; 345 | 346 | /** \ingroup desc 347 | * A structure representing the standard USB endpoint descriptor. This 348 | * descriptor is documented in section 9.6.3 of the USB 2.0 specification. 349 | * All multiple-byte fields are represented in host-endian format. 350 | */ 351 | struct libusb_endpoint_descriptor { 352 | /** Size of this descriptor (in bytes) */ 353 | uint8_t bLength; 354 | 355 | /** Descriptor type. Will have value 356 | * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in 357 | * this context. */ 358 | uint8_t bDescriptorType; 359 | 360 | /** The address of the endpoint described by this descriptor. Bits 0:3 are 361 | * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction, 362 | * see \ref libusb_endpoint_direction. 363 | */ 364 | uint8_t bEndpointAddress; 365 | 366 | /** Attributes which apply to the endpoint when it is configured using 367 | * the bConfigurationValue. Bits 0:1 determine the transfer type and 368 | * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for 369 | * isochronous endpoints and correspond to \ref libusb_iso_sync_type. 370 | * Bits 4:5 are also only used for isochronous endpoints and correspond to 371 | * \ref libusb_iso_usage_type. Bits 6:7 are reserved. 372 | */ 373 | uint8_t bmAttributes; 374 | 375 | /** Maximum packet size this endpoint is capable of sending/receiving. */ 376 | uint16_t wMaxPacketSize; 377 | 378 | /** Interval for polling endpoint for data transfers. */ 379 | uint8_t bInterval; 380 | 381 | /** For audio devices only: the rate at which synchronization feedback 382 | * is provided. */ 383 | uint8_t bRefresh; 384 | 385 | /** For audio devices only: the address if the synch endpoint */ 386 | uint8_t bSynchAddress; 387 | 388 | /** Extra descriptors. If libusb encounters unknown endpoint descriptors, 389 | * it will store them here, should you wish to parse them. */ 390 | const unsigned char *extra; 391 | 392 | /** Length of the extra descriptors, in bytes. */ 393 | int extra_length; 394 | }; 395 | 396 | /** \ingroup desc 397 | * A structure representing the standard USB interface descriptor. This 398 | * descriptor is documented in section 9.6.5 of the USB 2.0 specification. 399 | * All multiple-byte fields are represented in host-endian format. 400 | */ 401 | struct libusb_interface_descriptor { 402 | /** Size of this descriptor (in bytes) */ 403 | uint8_t bLength; 404 | 405 | /** Descriptor type. Will have value 406 | * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE 407 | * in this context. */ 408 | uint8_t bDescriptorType; 409 | 410 | /** Number of this interface */ 411 | uint8_t bInterfaceNumber; 412 | 413 | /** Value used to select this alternate setting for this interface */ 414 | uint8_t bAlternateSetting; 415 | 416 | /** Number of endpoints used by this interface (excluding the control 417 | * endpoint). */ 418 | uint8_t bNumEndpoints; 419 | 420 | /** USB-IF class code for this interface. See \ref libusb_class_code. */ 421 | uint8_t bInterfaceClass; 422 | 423 | /** USB-IF subclass code for this interface, qualified by the 424 | * bInterfaceClass value */ 425 | uint8_t bInterfaceSubClass; 426 | 427 | /** USB-IF protocol code for this interface, qualified by the 428 | * bInterfaceClass and bInterfaceSubClass values */ 429 | uint8_t bInterfaceProtocol; 430 | 431 | /** Index of string descriptor describing this interface */ 432 | uint8_t iInterface; 433 | 434 | /** Array of endpoint descriptors. This length of this array is determined 435 | * by the bNumEndpoints field. */ 436 | const struct libusb_endpoint_descriptor *endpoint; 437 | 438 | /** Extra descriptors. If libusb encounters unknown interface descriptors, 439 | * it will store them here, should you wish to parse them. */ 440 | const unsigned char *extra; 441 | 442 | /** Length of the extra descriptors, in bytes. */ 443 | int extra_length; 444 | }; 445 | 446 | /** \ingroup desc 447 | * A collection of alternate settings for a particular USB interface. 448 | */ 449 | struct libusb_interface { 450 | /** Array of interface descriptors. The length of this array is determined 451 | * by the num_altsetting field. */ 452 | const struct libusb_interface_descriptor *altsetting; 453 | 454 | /** The number of alternate settings that belong to this interface */ 455 | int num_altsetting; 456 | }; 457 | 458 | /** \ingroup desc 459 | * A structure representing the standard USB configuration descriptor. This 460 | * descriptor is documented in section 9.6.3 of the USB 2.0 specification. 461 | * All multiple-byte fields are represented in host-endian format. 462 | */ 463 | struct libusb_config_descriptor { 464 | /** Size of this descriptor (in bytes) */ 465 | uint8_t bLength; 466 | 467 | /** Descriptor type. Will have value 468 | * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG 469 | * in this context. */ 470 | uint8_t bDescriptorType; 471 | 472 | /** Total length of data returned for this configuration */ 473 | uint16_t wTotalLength; 474 | 475 | /** Number of interfaces supported by this configuration */ 476 | uint8_t bNumInterfaces; 477 | 478 | /** Identifier value for this configuration */ 479 | uint8_t bConfigurationValue; 480 | 481 | /** Index of string descriptor describing this configuration */ 482 | uint8_t iConfiguration; 483 | 484 | /** Configuration characteristics */ 485 | uint8_t bmAttributes; 486 | 487 | /** Maximum power consumption of the USB device from this bus in this 488 | * configuration when the device is fully opreation. Expressed in units 489 | * of 2 mA. */ 490 | uint8_t MaxPower; 491 | 492 | /** Array of interfaces supported by this configuration. The length of 493 | * this array is determined by the bNumInterfaces field. */ 494 | const struct libusb_interface *interface; 495 | 496 | /** Extra descriptors. If libusb encounters unknown configuration 497 | * descriptors, it will store them here, should you wish to parse them. */ 498 | const unsigned char *extra; 499 | 500 | /** Length of the extra descriptors, in bytes. */ 501 | int extra_length; 502 | }; 503 | 504 | /** \ingroup asyncio 505 | * Setup packet for control transfers. */ 506 | struct libusb_control_setup { 507 | /** Request type. Bits 0:4 determine recipient, see 508 | * \ref libusb_request_recipient. Bits 5:6 determine type, see 509 | * \ref libusb_request_type. Bit 7 determines data transfer direction, see 510 | * \ref libusb_endpoint_direction. 511 | */ 512 | uint8_t bmRequestType; 513 | 514 | /** Request. If the type bits of bmRequestType are equal to 515 | * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD 516 | * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to 517 | * \ref libusb_standard_request. For other cases, use of this field is 518 | * application-specific. */ 519 | uint8_t bRequest; 520 | 521 | /** Value. Varies according to request */ 522 | uint16_t wValue; 523 | 524 | /** Index. Varies according to request, typically used to pass an index 525 | * or offset */ 526 | uint16_t wIndex; 527 | 528 | /** Number of bytes to transfer */ 529 | uint16_t wLength; 530 | }; 531 | 532 | #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup)) 533 | 534 | /* libusb */ 535 | 536 | struct libusb_context; 537 | struct libusb_device; 538 | struct libusb_device_handle; 539 | 540 | /** \ingroup lib 541 | * Structure representing a libusb session. The concept of individual libusb 542 | * sessions allows for your program to use two libraries (or dynamically 543 | * load two modules) which both independently use libusb. This will prevent 544 | * interference between the individual libusb users - for example 545 | * libusb_set_debug() will not affect the other user of the library, and 546 | * libusb_exit() will not destroy resources that the other user is still 547 | * using. 548 | * 549 | * Sessions are created by libusb_init() and destroyed through libusb_exit(). 550 | * If your application is guaranteed to only ever include a single libusb 551 | * user (i.e. you), you do not have to worry about contexts: pass NULL in 552 | * every function call where a context is required. The default context 553 | * will be used. 554 | * 555 | * For more information, see \ref contexts. 556 | */ 557 | typedef struct libusb_context libusb_context; 558 | 559 | /** \ingroup dev 560 | * Structure representing a USB device detected on the system. This is an 561 | * opaque type for which you are only ever provided with a pointer, usually 562 | * originating from libusb_get_device_list(). 563 | * 564 | * Certain operations can be performed on a device, but in order to do any 565 | * I/O you will have to first obtain a device handle using libusb_open(). 566 | * 567 | * Devices are reference counted with libusb_device_ref() and 568 | * libusb_device_unref(), and are freed when the reference count reaches 0. 569 | * New devices presented by libusb_get_device_list() have a reference count of 570 | * 1, and libusb_free_device_list() can optionally decrease the reference count 571 | * on all devices in the list. libusb_open() adds another reference which is 572 | * later destroyed by libusb_close(). 573 | */ 574 | typedef struct libusb_device libusb_device; 575 | 576 | 577 | /** \ingroup dev 578 | * Structure representing a handle on a USB device. This is an opaque type for 579 | * which you are only ever provided with a pointer, usually originating from 580 | * libusb_open(). 581 | * 582 | * A device handle is used to perform I/O and other operations. When finished 583 | * with a device handle, you should call libusb_close(). 584 | */ 585 | typedef struct libusb_device_handle libusb_device_handle; 586 | 587 | /** \ingroup misc 588 | * Error codes. Most libusb functions return 0 on success or one of these 589 | * codes on failure. 590 | */ 591 | enum libusb_error { 592 | /** Success (no error) */ 593 | LIBUSB_SUCCESS = 0, 594 | 595 | /** Input/output error */ 596 | LIBUSB_ERROR_IO = -1, 597 | 598 | /** Invalid parameter */ 599 | LIBUSB_ERROR_INVALID_PARAM = -2, 600 | 601 | /** Access denied (insufficient permissions) */ 602 | LIBUSB_ERROR_ACCESS = -3, 603 | 604 | /** No such device (it may have been disconnected) */ 605 | LIBUSB_ERROR_NO_DEVICE = -4, 606 | 607 | /** Entity not found */ 608 | LIBUSB_ERROR_NOT_FOUND = -5, 609 | 610 | /** Resource busy */ 611 | LIBUSB_ERROR_BUSY = -6, 612 | 613 | /** Operation timed out */ 614 | LIBUSB_ERROR_TIMEOUT = -7, 615 | 616 | /** Overflow */ 617 | LIBUSB_ERROR_OVERFLOW = -8, 618 | 619 | /** Pipe error */ 620 | LIBUSB_ERROR_PIPE = -9, 621 | 622 | /** System call interrupted (perhaps due to signal) */ 623 | LIBUSB_ERROR_INTERRUPTED = -10, 624 | 625 | /** Insufficient memory */ 626 | LIBUSB_ERROR_NO_MEM = -11, 627 | 628 | /** Operation not supported or unimplemented on this platform */ 629 | LIBUSB_ERROR_NOT_SUPPORTED = -12, 630 | 631 | /** Other error */ 632 | LIBUSB_ERROR_OTHER = -99 633 | }; 634 | 635 | /** \ingroup asyncio 636 | * Transfer status codes */ 637 | enum libusb_transfer_status { 638 | /** Transfer completed without error. Note that this does not indicate 639 | * that the entire amount of requested data was transferred. */ 640 | LIBUSB_TRANSFER_COMPLETED, 641 | 642 | /** Transfer failed */ 643 | LIBUSB_TRANSFER_ERROR, 644 | 645 | /** Transfer timed out */ 646 | LIBUSB_TRANSFER_TIMED_OUT, 647 | 648 | /** Transfer was cancelled */ 649 | LIBUSB_TRANSFER_CANCELLED, 650 | 651 | /** For bulk/interrupt endpoints: halt condition detected (endpoint 652 | * stalled). For control endpoints: control request not supported. */ 653 | LIBUSB_TRANSFER_STALL, 654 | 655 | /** Device was disconnected */ 656 | LIBUSB_TRANSFER_NO_DEVICE, 657 | 658 | /** Device sent more data than requested */ 659 | LIBUSB_TRANSFER_OVERFLOW 660 | }; 661 | 662 | /** \ingroup asyncio 663 | * libusb_transfer.flags values */ 664 | enum libusb_transfer_flags { 665 | /** Report short frames as errors */ 666 | LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0, 667 | 668 | /** Automatically free() transfer buffer during libusb_free_transfer() */ 669 | LIBUSB_TRANSFER_FREE_BUFFER = 1<<1, 670 | 671 | /** Automatically call libusb_free_transfer() after callback returns. 672 | * If this flag is set, it is illegal to call libusb_free_transfer() 673 | * from your transfer callback, as this will result in a double-free 674 | * when this flag is acted upon. */ 675 | LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2 676 | }; 677 | 678 | /** \ingroup asyncio 679 | * Isochronous packet descriptor. */ 680 | struct libusb_iso_packet_descriptor { 681 | /** Length of data to request in this packet */ 682 | unsigned int length; 683 | 684 | /** Amount of data that was actually transferred */ 685 | unsigned int actual_length; 686 | 687 | /** Status code for this packet */ 688 | enum libusb_transfer_status status; 689 | }; 690 | 691 | struct libusb_transfer; 692 | 693 | /** \ingroup asyncio 694 | * Asynchronous transfer callback function type. When submitting asynchronous 695 | * transfers, you pass a pointer to a callback function of this type via the 696 | * \ref libusb_transfer::callback "callback" member of the libusb_transfer 697 | * structure. libusb will call this function later, when the transfer has 698 | * completed or failed. See \ref asyncio for more information. 699 | * \param transfer The libusb_transfer struct the callback function is being 700 | * notified about. 701 | */ 702 | typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer); 703 | 704 | /** \ingroup asyncio 705 | * The generic USB transfer structure. The user populates this structure and 706 | * then submits it in order to request a transfer. After the transfer has 707 | * completed, the library populates the transfer with the results and passes 708 | * it back to the user. 709 | */ 710 | struct libusb_transfer { 711 | /** Handle of the device that this transfer will be submitted to */ 712 | libusb_device_handle *dev_handle; 713 | 714 | /** A bitwise OR combination of \ref libusb_transfer_flags. */ 715 | uint8_t flags; 716 | 717 | /** Address of the endpoint where this transfer will be sent. */ 718 | unsigned char endpoint; 719 | 720 | /** Type of the endpoint from \ref libusb_transfer_type */ 721 | unsigned char type; 722 | 723 | /** Timeout for this transfer in millseconds. A value of 0 indicates no 724 | * timeout. */ 725 | unsigned int timeout; 726 | 727 | /** The status of the transfer. Read-only, and only for use within 728 | * transfer callback function. 729 | * 730 | * If this is an isochronous transfer, this field may read COMPLETED even 731 | * if there were errors in the frames. Use the 732 | * \ref libusb_iso_packet_descriptor::status "status" field in each packet 733 | * to determine if errors occurred. */ 734 | enum libusb_transfer_status status; 735 | 736 | /** Length of the data buffer */ 737 | int length; 738 | 739 | /** Actual length of data that was transferred. Read-only, and only for 740 | * use within transfer callback function. Not valid for isochronous 741 | * endpoint transfers. */ 742 | int actual_length; 743 | 744 | /** Callback function. This will be invoked when the transfer completes, 745 | * fails, or is cancelled. */ 746 | libusb_transfer_cb_fn callback; 747 | 748 | /** User context data to pass to the callback function. */ 749 | void *user_data; 750 | 751 | /** Data buffer */ 752 | unsigned char *buffer; 753 | 754 | /** Number of isochronous packets. Only used for I/O with isochronous 755 | * endpoints. */ 756 | int num_iso_packets; 757 | 758 | /** Isochronous packet descriptors, for isochronous transfers only. */ 759 | struct libusb_iso_packet_descriptor iso_packet_desc 760 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) 761 | [] /* valid C99 code */ 762 | #else 763 | [0] /* non-standard, but usually working code */ 764 | #endif 765 | ; 766 | }; 767 | 768 | int libusb_init(libusb_context **ctx); 769 | void libusb_exit(libusb_context *ctx); 770 | void libusb_set_debug(libusb_context *ctx, int level); 771 | 772 | ssize_t libusb_get_device_list(libusb_context *ctx, 773 | libusb_device ***list); 774 | void libusb_free_device_list(libusb_device **list, int unref_devices); 775 | libusb_device *libusb_ref_device(libusb_device *dev); 776 | void libusb_unref_device(libusb_device *dev); 777 | 778 | int libusb_get_configuration(libusb_device_handle *dev, int *config); 779 | int libusb_get_device_descriptor(libusb_device *dev, 780 | struct libusb_device_descriptor *desc); 781 | int libusb_get_active_config_descriptor(libusb_device *dev, 782 | struct libusb_config_descriptor **config); 783 | int libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index, 784 | struct libusb_config_descriptor **config); 785 | int libusb_get_config_descriptor_by_value(libusb_device *dev, 786 | uint8_t bConfigurationValue, struct libusb_config_descriptor **config); 787 | void libusb_free_config_descriptor(struct libusb_config_descriptor *config); 788 | uint8_t libusb_get_bus_number(libusb_device *dev); 789 | uint8_t libusb_get_device_address(libusb_device *dev); 790 | int libusb_get_max_packet_size(libusb_device *dev, unsigned char endpoint); 791 | 792 | int libusb_open(libusb_device *dev, libusb_device_handle **handle); 793 | void libusb_close(libusb_device_handle *dev_handle); 794 | libusb_device *libusb_get_device(libusb_device_handle *dev_handle); 795 | 796 | int libusb_set_configuration(libusb_device_handle *dev, int configuration); 797 | int libusb_claim_interface(libusb_device_handle *dev, int iface); 798 | int libusb_release_interface(libusb_device_handle *dev, int iface); 799 | 800 | libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context *ctx, 801 | uint16_t vendor_id, uint16_t product_id); 802 | 803 | int libusb_set_interface_alt_setting(libusb_device_handle *dev, 804 | int interface_number, int alternate_setting); 805 | int libusb_clear_halt(libusb_device_handle *dev, unsigned char endpoint); 806 | int libusb_reset_device(libusb_device_handle *dev); 807 | 808 | int libusb_kernel_driver_active(libusb_device_handle *dev, int interface); 809 | int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface); 810 | int libusb_attach_kernel_driver(libusb_device_handle *dev, int interface); 811 | 812 | /* async I/O */ 813 | 814 | /** \ingroup asyncio 815 | * Get the data section of a control transfer. This convenience function is here 816 | * to remind you that the data does not start until 8 bytes into the actual 817 | * buffer, as the setup packet comes first. 818 | * 819 | * Calling this function only makes sense from a transfer callback function, 820 | * or situations where you have already allocated a suitably sized buffer at 821 | * transfer->buffer. 822 | * 823 | * \param transfer a transfer 824 | * \returns pointer to the first byte of the data section 825 | */ 826 | static inline unsigned char *libusb_control_transfer_get_data( 827 | struct libusb_transfer *transfer) 828 | { 829 | return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE; 830 | } 831 | 832 | /** \ingroup asyncio 833 | * Get the control setup packet of a control transfer. This convenience 834 | * function is here to remind you that the control setup occupies the first 835 | * 8 bytes of the transfer data buffer. 836 | * 837 | * Calling this function only makes sense from a transfer callback function, 838 | * or situations where you have already allocated a suitably sized buffer at 839 | * transfer->buffer. 840 | * 841 | * \param transfer a transfer 842 | * \returns a casted pointer to the start of the transfer data buffer 843 | */ 844 | static inline struct libusb_control_setup *libusb_control_transfer_get_setup( 845 | struct libusb_transfer *transfer) 846 | { 847 | return (struct libusb_control_setup *) transfer->buffer; 848 | } 849 | 850 | /** \ingroup asyncio 851 | * Helper function to populate the setup packet (first 8 bytes of the data 852 | * buffer) for a control transfer. The wIndex, wValue and wLength values should 853 | * be given in host-endian byte order. 854 | * 855 | * \param buffer buffer to output the setup packet into 856 | * \param bmRequestType see the 857 | * \ref libusb_control_setup::bmRequestType "bmRequestType" field of 858 | * \ref libusb_control_setup 859 | * \param bRequest see the 860 | * \ref libusb_control_setup::bRequest "bRequest" field of 861 | * \ref libusb_control_setup 862 | * \param wValue see the 863 | * \ref libusb_control_setup::wValue "wValue" field of 864 | * \ref libusb_control_setup 865 | * \param wIndex see the 866 | * \ref libusb_control_setup::wIndex "wIndex" field of 867 | * \ref libusb_control_setup 868 | * \param wLength see the 869 | * \ref libusb_control_setup::wLength "wLength" field of 870 | * \ref libusb_control_setup 871 | */ 872 | static inline void libusb_fill_control_setup(unsigned char *buffer, 873 | uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 874 | uint16_t wLength) 875 | { 876 | struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer; 877 | setup->bmRequestType = bmRequestType; 878 | setup->bRequest = bRequest; 879 | setup->wValue = libusb_cpu_to_le16(wValue); 880 | setup->wIndex = libusb_cpu_to_le16(wIndex); 881 | setup->wLength = libusb_cpu_to_le16(wLength); 882 | } 883 | 884 | struct libusb_transfer *libusb_alloc_transfer(int iso_packets); 885 | int libusb_submit_transfer(struct libusb_transfer *transfer); 886 | int libusb_cancel_transfer(struct libusb_transfer *transfer); 887 | void libusb_free_transfer(struct libusb_transfer *transfer); 888 | 889 | /** \ingroup asyncio 890 | * Helper function to populate the required \ref libusb_transfer fields 891 | * for a control transfer. 892 | * 893 | * If you pass a transfer buffer to this function, the first 8 bytes will 894 | * be interpreted as a control setup packet, and the wLength field will be 895 | * used to automatically populate the \ref libusb_transfer::length "length" 896 | * field of the transfer. Therefore the recommended approach is: 897 | * -# Allocate a suitably sized data buffer (including space for control setup) 898 | * -# Call libusb_fill_control_setup() 899 | * -# If this is a host-to-device transfer with a data stage, put the data 900 | * in place after the setup packet 901 | * -# Call this function 902 | * -# Call libusb_submit_transfer() 903 | * 904 | * It is also legal to pass a NULL buffer to this function, in which case this 905 | * function will not attempt to populate the length field. Remember that you 906 | * must then populate the buffer and length fields later. 907 | * 908 | * \param transfer the transfer to populate 909 | * \param dev_handle handle of the device that will handle the transfer 910 | * \param buffer data buffer. If provided, this function will interpret the 911 | * first 8 bytes as a setup packet and infer the transfer length from that. 912 | * \param callback callback function to be invoked on transfer completion 913 | * \param user_data user data to pass to callback function 914 | * \param timeout timeout for the transfer in milliseconds 915 | */ 916 | static inline void libusb_fill_control_transfer( 917 | struct libusb_transfer *transfer, libusb_device_handle *dev_handle, 918 | unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data, 919 | unsigned int timeout) 920 | { 921 | struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer; 922 | transfer->dev_handle = dev_handle; 923 | transfer->endpoint = 0; 924 | transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; 925 | transfer->timeout = timeout; 926 | transfer->buffer = buffer; 927 | if (setup) 928 | transfer->length = LIBUSB_CONTROL_SETUP_SIZE 929 | + libusb_le16_to_cpu(setup->wLength); 930 | transfer->user_data = user_data; 931 | transfer->callback = callback; 932 | } 933 | 934 | /** \ingroup asyncio 935 | * Helper function to populate the required \ref libusb_transfer fields 936 | * for a bulk transfer. 937 | * 938 | * \param transfer the transfer to populate 939 | * \param dev_handle handle of the device that will handle the transfer 940 | * \param endpoint address of the endpoint where this transfer will be sent 941 | * \param buffer data buffer 942 | * \param length length of data buffer 943 | * \param callback callback function to be invoked on transfer completion 944 | * \param user_data user data to pass to callback function 945 | * \param timeout timeout for the transfer in milliseconds 946 | */ 947 | static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer, 948 | libusb_device_handle *dev_handle, unsigned char endpoint, 949 | unsigned char *buffer, int length, libusb_transfer_cb_fn callback, 950 | void *user_data, unsigned int timeout) 951 | { 952 | transfer->dev_handle = dev_handle; 953 | transfer->endpoint = endpoint; 954 | transfer->type = LIBUSB_TRANSFER_TYPE_BULK; 955 | transfer->timeout = timeout; 956 | transfer->buffer = buffer; 957 | transfer->length = length; 958 | transfer->user_data = user_data; 959 | transfer->callback = callback; 960 | } 961 | 962 | /** \ingroup asyncio 963 | * Helper function to populate the required \ref libusb_transfer fields 964 | * for an interrupt transfer. 965 | * 966 | * \param transfer the transfer to populate 967 | * \param dev_handle handle of the device that will handle the transfer 968 | * \param endpoint address of the endpoint where this transfer will be sent 969 | * \param buffer data buffer 970 | * \param length length of data buffer 971 | * \param callback callback function to be invoked on transfer completion 972 | * \param user_data user data to pass to callback function 973 | * \param timeout timeout for the transfer in milliseconds 974 | */ 975 | static inline void libusb_fill_interrupt_transfer( 976 | struct libusb_transfer *transfer, libusb_device_handle *dev_handle, 977 | unsigned char endpoint, unsigned char *buffer, int length, 978 | libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) 979 | { 980 | transfer->dev_handle = dev_handle; 981 | transfer->endpoint = endpoint; 982 | transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; 983 | transfer->timeout = timeout; 984 | transfer->buffer = buffer; 985 | transfer->length = length; 986 | transfer->user_data = user_data; 987 | transfer->callback = callback; 988 | } 989 | 990 | /** \ingroup asyncio 991 | * Helper function to populate the required \ref libusb_transfer fields 992 | * for an isochronous transfer. 993 | * 994 | * \param transfer the transfer to populate 995 | * \param dev_handle handle of the device that will handle the transfer 996 | * \param endpoint address of the endpoint where this transfer will be sent 997 | * \param buffer data buffer 998 | * \param length length of data buffer 999 | * \param num_iso_packets the number of isochronous packets 1000 | * \param callback callback function to be invoked on transfer completion 1001 | * \param user_data user data to pass to callback function 1002 | * \param timeout timeout for the transfer in milliseconds 1003 | */ 1004 | static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer, 1005 | libusb_device_handle *dev_handle, unsigned char endpoint, 1006 | unsigned char *buffer, int length, int num_iso_packets, 1007 | libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) 1008 | { 1009 | transfer->dev_handle = dev_handle; 1010 | transfer->endpoint = endpoint; 1011 | transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; 1012 | transfer->timeout = timeout; 1013 | transfer->buffer = buffer; 1014 | transfer->length = length; 1015 | transfer->num_iso_packets = num_iso_packets; 1016 | transfer->user_data = user_data; 1017 | transfer->callback = callback; 1018 | } 1019 | 1020 | /** \ingroup asyncio 1021 | * Convenience function to set the length of all packets in an isochronous 1022 | * transfer, based on the num_iso_packets field in the transfer structure. 1023 | * 1024 | * \param transfer a transfer 1025 | * \param length the length to set in each isochronous packet descriptor 1026 | * \see libusb_get_max_packet_size() 1027 | */ 1028 | static inline void libusb_set_iso_packet_lengths( 1029 | struct libusb_transfer *transfer, unsigned int length) 1030 | { 1031 | int i; 1032 | for (i = 0; i < transfer->num_iso_packets; i++) 1033 | transfer->iso_packet_desc[i].length = length; 1034 | } 1035 | 1036 | /** \ingroup asyncio 1037 | * Convenience function to locate the position of an isochronous packet 1038 | * within the buffer of an isochronous transfer. 1039 | * 1040 | * This is a thorough function which loops through all preceding packets, 1041 | * accumulating their lengths to find the position of the specified packet. 1042 | * Typically you will assign equal lengths to each packet in the transfer, 1043 | * and hence the above method is sub-optimal. You may wish to use 1044 | * libusb_get_iso_packet_buffer_simple() instead. 1045 | * 1046 | * \param transfer a transfer 1047 | * \param packet the packet to return the address of 1048 | * \returns the base address of the packet buffer inside the transfer buffer, 1049 | * or NULL if the packet does not exist. 1050 | * \see libusb_get_iso_packet_buffer_simple() 1051 | */ 1052 | static inline unsigned char *libusb_get_iso_packet_buffer( 1053 | struct libusb_transfer *transfer, unsigned int packet) 1054 | { 1055 | int i; 1056 | size_t offset = 0; 1057 | int _packet; 1058 | 1059 | /* oops..slight bug in the API. packet is an unsigned int, but we use 1060 | * signed integers almost everywhere else. range-check and convert to 1061 | * signed to avoid compiler warnings. FIXME for libusb-2. */ 1062 | if (packet > INT_MAX) 1063 | return NULL; 1064 | _packet = packet; 1065 | 1066 | if (_packet >= transfer->num_iso_packets) 1067 | return NULL; 1068 | 1069 | for (i = 0; i < _packet; i++) 1070 | offset += transfer->iso_packet_desc[i].length; 1071 | 1072 | return transfer->buffer + offset; 1073 | } 1074 | 1075 | /** \ingroup asyncio 1076 | * Convenience function to locate the position of an isochronous packet 1077 | * within the buffer of an isochronous transfer, for transfers where each 1078 | * packet is of identical size. 1079 | * 1080 | * This function relies on the assumption that every packet within the transfer 1081 | * is of identical size to the first packet. Calculating the location of 1082 | * the packet buffer is then just a simple calculation: 1083 | * buffer + (packet_size * packet) 1084 | * 1085 | * Do not use this function on transfers other than those that have identical 1086 | * packet lengths for each packet. 1087 | * 1088 | * \param transfer a transfer 1089 | * \param packet the packet to return the address of 1090 | * \returns the base address of the packet buffer inside the transfer buffer, 1091 | * or NULL if the packet does not exist. 1092 | * \see libusb_get_iso_packet_buffer() 1093 | */ 1094 | static inline unsigned char *libusb_get_iso_packet_buffer_simple( 1095 | struct libusb_transfer *transfer, unsigned int packet) 1096 | { 1097 | int _packet; 1098 | 1099 | /* oops..slight bug in the API. packet is an unsigned int, but we use 1100 | * signed integers almost everywhere else. range-check and convert to 1101 | * signed to avoid compiler warnings. FIXME for libusb-2. */ 1102 | if (packet > INT_MAX) 1103 | return NULL; 1104 | _packet = packet; 1105 | 1106 | if (_packet >= transfer->num_iso_packets) 1107 | return NULL; 1108 | 1109 | return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet); 1110 | } 1111 | 1112 | /* sync I/O */ 1113 | 1114 | int libusb_control_transfer(libusb_device_handle *dev_handle, 1115 | uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, 1116 | unsigned char *data, uint16_t length, unsigned int timeout); 1117 | 1118 | int libusb_bulk_transfer(libusb_device_handle *dev_handle, 1119 | unsigned char endpoint, unsigned char *data, int length, 1120 | int *actual_length, unsigned int timeout); 1121 | 1122 | int libusb_interrupt_transfer(libusb_device_handle *dev_handle, 1123 | unsigned char endpoint, unsigned char *data, int length, 1124 | int *actual_length, unsigned int timeout); 1125 | 1126 | /** \ingroup desc 1127 | * Retrieve a descriptor from the default control pipe. 1128 | * This is a convenience function which formulates the appropriate control 1129 | * message to retrieve the descriptor. 1130 | * 1131 | * \param dev a device handle 1132 | * \param desc_type the descriptor type, see \ref libusb_descriptor_type 1133 | * \param desc_index the index of the descriptor to retrieve 1134 | * \param data output buffer for descriptor 1135 | * \param length size of data buffer 1136 | * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure 1137 | */ 1138 | static inline int libusb_get_descriptor(libusb_device_handle *dev, 1139 | uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length) 1140 | { 1141 | return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, 1142 | LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data, 1143 | length, 1000); 1144 | } 1145 | 1146 | /** \ingroup desc 1147 | * Retrieve a descriptor from a device. 1148 | * This is a convenience function which formulates the appropriate control 1149 | * message to retrieve the descriptor. The string returned is Unicode, as 1150 | * detailed in the USB specifications. 1151 | * 1152 | * \param dev a device handle 1153 | * \param desc_index the index of the descriptor to retrieve 1154 | * \param langid the language ID for the string descriptor 1155 | * \param data output buffer for descriptor 1156 | * \param length size of data buffer 1157 | * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure 1158 | * \see libusb_get_string_descriptor_ascii() 1159 | */ 1160 | static inline int libusb_get_string_descriptor(libusb_device_handle *dev, 1161 | uint8_t desc_index, uint16_t langid, unsigned char *data, int length) 1162 | { 1163 | return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, 1164 | LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index, 1165 | langid, data, length, 1000); 1166 | } 1167 | 1168 | int libusb_get_string_descriptor_ascii(libusb_device_handle *dev, 1169 | uint8_t index, unsigned char *data, int length); 1170 | 1171 | /* polling and timeouts */ 1172 | 1173 | int libusb_try_lock_events(libusb_context *ctx); 1174 | void libusb_lock_events(libusb_context *ctx); 1175 | void libusb_unlock_events(libusb_context *ctx); 1176 | int libusb_event_handling_ok(libusb_context *ctx); 1177 | int libusb_event_handler_active(libusb_context *ctx); 1178 | void libusb_lock_event_waiters(libusb_context *ctx); 1179 | void libusb_unlock_event_waiters(libusb_context *ctx); 1180 | int libusb_wait_for_event(libusb_context *ctx, struct timeval *tv); 1181 | 1182 | int libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv); 1183 | int libusb_handle_events(libusb_context *ctx); 1184 | int libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv); 1185 | int libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv); 1186 | 1187 | /** \ingroup poll 1188 | * File descriptor for polling 1189 | */ 1190 | struct libusb_pollfd { 1191 | /** Numeric file descriptor */ 1192 | int fd; 1193 | 1194 | /** Event flags to poll for from . POLLIN indicates that you 1195 | * should monitor this file descriptor for becoming ready to read from, 1196 | * and POLLOUT indicates that you should monitor this file descriptor for 1197 | * nonblocking write readiness. */ 1198 | short events; 1199 | }; 1200 | 1201 | /** \ingroup poll 1202 | * Callback function, invoked when a new file descriptor should be added 1203 | * to the set of file descriptors monitored for events. 1204 | * \param fd the new file descriptor 1205 | * \param events events to monitor for, see \ref libusb_pollfd for a 1206 | * description 1207 | * \param user_data User data pointer specified in 1208 | * libusb_set_pollfd_notifiers() call 1209 | * \see libusb_set_pollfd_notifiers() 1210 | */ 1211 | typedef void (*libusb_pollfd_added_cb)(int fd, short events, void *user_data); 1212 | 1213 | /** \ingroup poll 1214 | * Callback function, invoked when a file descriptor should be removed from 1215 | * the set of file descriptors being monitored for events. After returning 1216 | * from this callback, do not use that file descriptor again. 1217 | * \param fd the file descriptor to stop monitoring 1218 | * \param user_data User data pointer specified in 1219 | * libusb_set_pollfd_notifiers() call 1220 | * \see libusb_set_pollfd_notifiers() 1221 | */ 1222 | typedef void (*libusb_pollfd_removed_cb)(int fd, void *user_data); 1223 | 1224 | const struct libusb_pollfd **libusb_get_pollfds(libusb_context *ctx); 1225 | void libusb_set_pollfd_notifiers(libusb_context *ctx, 1226 | libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, 1227 | void *user_data); 1228 | 1229 | #ifdef __cplusplus 1230 | } 1231 | #endif 1232 | 1233 | #endif 1234 | --------------------------------------------------------------------------------