1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998 |
- /*
- * windows backend for libusb 1.0
- * Copyright (c) 2009-2010 Pete Batard <pbatard@gmail.com>
- * With contributions from Michael Plante, Orin Eman et al.
- * Parts of this code adapted from libusb-win32-v1 by Stephan Meyer
- * Hash table functions adapted from glibc, by Ulrich Drepper et al.
- * Major code testing contribution by Xiaofan Chen
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include <config.h>
- #include <windows.h>
- #include <setupapi.h>
- #include <ctype.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <process.h>
- #include <stdio.h>
- #include <objbase.h>
- #include <winioctl.h>
- #include <libusbi.h>
- #include "poll_windows.h"
- #include "windows_usb.h"
- // The following prevents "banned API" errors when using the MS's WDK OACR/Prefast
- #if defined(_PREFAST_)
- #pragma warning(disable:28719)
- #endif
- // The 2 macros below are used in conjunction with safe loops.
- #define LOOP_CHECK(fcall) { r=fcall; if (r != LIBUSB_SUCCESS) continue; }
- #define LOOP_BREAK(err) { r=err; continue; }
- extern void usbi_fd_notification(struct libusb_context *ctx);
- // Helper prototypes
- static int windows_get_active_config_descriptor(struct libusb_device *dev, unsigned char *buffer, size_t len, int *host_endian);
- static int windows_clock_gettime(int clk_id, struct timespec *tp);
- unsigned __stdcall windows_clock_gettime_threaded(void* param);
- // WinUSB API prototypes
- static int winusb_init(struct libusb_context *ctx);
- static int winusb_exit(void);
- static int winusb_open(struct libusb_device_handle *dev_handle);
- static void winusb_close(struct libusb_device_handle *dev_handle);
- static int winusb_configure_endpoints(struct libusb_device_handle *dev_handle, int iface);
- static int winusb_claim_interface(struct libusb_device_handle *dev_handle, int iface);
- static int winusb_release_interface(struct libusb_device_handle *dev_handle, int iface);
- static int winusb_submit_control_transfer(struct usbi_transfer *itransfer);
- static int winusb_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting);
- static int winusb_submit_bulk_transfer(struct usbi_transfer *itransfer);
- static int winusb_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint);
- static int winusb_abort_transfers(struct usbi_transfer *itransfer);
- static int winusb_abort_control(struct usbi_transfer *itransfer);
- static int winusb_reset_device(struct libusb_device_handle *dev_handle);
- static int winusb_copy_transfer_data(struct usbi_transfer *itransfer, uint32_t io_size);
- // Composite API prototypes
- static int composite_init(struct libusb_context *ctx);
- static int composite_exit(void);
- static int composite_open(struct libusb_device_handle *dev_handle);
- static void composite_close(struct libusb_device_handle *dev_handle);
- static int composite_claim_interface(struct libusb_device_handle *dev_handle, int iface);
- static int composite_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting);
- static int composite_release_interface(struct libusb_device_handle *dev_handle, int iface);
- static int composite_submit_control_transfer(struct usbi_transfer *itransfer);
- static int composite_submit_bulk_transfer(struct usbi_transfer *itransfer);
- static int composite_submit_iso_transfer(struct usbi_transfer *itransfer);
- static int composite_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint);
- static int composite_abort_transfers(struct usbi_transfer *itransfer);
- static int composite_abort_control(struct usbi_transfer *itransfer);
- static int composite_reset_device(struct libusb_device_handle *dev_handle);
- static int composite_copy_transfer_data(struct usbi_transfer *itransfer, uint32_t io_size);
- // Global variables
- uint64_t hires_frequency, hires_ticks_to_ps;
- const uint64_t epoch_time = UINT64_C(116444736000000000); // 1970.01.01 00:00:000 in MS Filetime
- enum windows_version windows_version = WINDOWS_UNSUPPORTED;
- // Concurrency
- static int concurrent_usage = -1;
- usbi_mutex_t autoclaim_lock;
- // Timer thread
- // NB: index 0 is for monotonic and 1 is for the thread exit event
- HANDLE timer_thread = NULL;
- HANDLE timer_mutex = NULL;
- struct timespec timer_tp;
- volatile LONG request_count[2] = {0, 1}; // last one must be > 0
- HANDLE timer_request[2] = { NULL, NULL };
- HANDLE timer_response = NULL;
- // API globals
- bool api_winusb_available = false;
- #define CHECK_WINUSB_AVAILABLE do { if (!api_winusb_available) return LIBUSB_ERROR_ACCESS; } while (0)
- static inline BOOLEAN guid_eq(const GUID *guid1, const GUID *guid2) {
- if ((guid1 != NULL) && (guid2 != NULL)) {
- return (memcmp(guid1, guid2, sizeof(GUID)) == 0);
- }
- return false;
- }
- #if defined(ENABLE_DEBUG_LOGGING) || (defined(_MSC_VER) && _MSC_VER < 1400)
- static char* guid_to_string(const GUID* guid)
- {
- static char guid_string[MAX_GUID_STRING_LENGTH];
- if (guid == NULL) return NULL;
- sprintf(guid_string, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
- (unsigned int)guid->Data1, guid->Data2, guid->Data3,
- guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
- guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
- return guid_string;
- }
- #endif
- /*
- * Converts a windows error to human readable string
- * uses retval as errorcode, or, if 0, use GetLastError()
- */
- static char *windows_error_str(uint32_t retval)
- {
- static char err_string[ERR_BUFFER_SIZE];
- DWORD size;
- size_t i;
- uint32_t error_code, format_error;
- error_code = retval?retval:GetLastError();
- safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%d] ", error_code);
- size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[safe_strlen(err_string)],
- ERR_BUFFER_SIZE - (DWORD)safe_strlen(err_string), NULL);
- if (size == 0) {
- format_error = GetLastError();
- if (format_error)
- safe_sprintf(err_string, ERR_BUFFER_SIZE,
- "Windows error code %u (FormatMessage error code %u)", error_code, format_error);
- else
- safe_sprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", error_code);
- } else {
- // Remove CR/LF terminators
- for (i=safe_strlen(err_string)-1; ((err_string[i]==0x0A) || (err_string[i]==0x0D)); i--) {
- err_string[i] = 0;
- }
- }
- return err_string;
- }
- /*
- * Sanitize Microsoft's paths: convert to uppercase, add prefix and fix backslashes.
- * Return an allocated sanitized string or NULL on error.
- */
- static char* sanitize_path(const char* path)
- {
- const char root_prefix[] = "\\\\.\\";
- size_t j, size, root_size;
- char* ret_path = NULL;
- size_t add_root = 0;
- if (path == NULL)
- return NULL;
- size = safe_strlen(path)+1;
- root_size = sizeof(root_prefix)-1;
- // Microsoft indiscriminatly uses '\\?\', '\\.\', '##?#" or "##.#" for root prefixes.
- if (!((size > 3) && (((path[0] == '\\') && (path[1] == '\\') && (path[3] == '\\')) ||
- ((path[0] == '#') && (path[1] == '#') && (path[3] == '#'))))) {
- add_root = root_size;
- size += add_root;
- }
- if ((ret_path = (char*)calloc(size, 1)) == NULL)
- return NULL;
- safe_strcpy(&ret_path[add_root], size-add_root, path);
- // Ensure consistancy with root prefix
- for (j=0; j<root_size; j++)
- ret_path[j] = root_prefix[j];
- // Same goes for '\' and '#' after the root prefix. Ensure '#' is used
- for(j=root_size; j<size; j++) {
- ret_path[j] = (char)toupper((int)ret_path[j]); // Fix case too
- if (ret_path[j] == '\\')
- ret_path[j] = '#';
- }
- return ret_path;
- }
- /*
- * Cfgmgr32, OLE32 and SetupAPI DLL functions
- */
- static int init_dlls(void)
- {
- DLL_LOAD(Cfgmgr32.dll, CM_Get_Parent, TRUE);
- DLL_LOAD(Cfgmgr32.dll, CM_Get_Child, TRUE);
- DLL_LOAD(Cfgmgr32.dll, CM_Get_Sibling, TRUE);
- DLL_LOAD(Cfgmgr32.dll, CM_Get_Device_IDA, TRUE);
- // Prefixed to avoid conflict with header files
- DLL_LOAD_PREFIXED(OLE32.dll, p, CLSIDFromString, TRUE);
- DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetClassDevsA, TRUE);
- DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiEnumDeviceInfo, TRUE);
- DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiEnumDeviceInterfaces, TRUE);
- DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetDeviceInterfaceDetailA, TRUE);
- DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiDestroyDeviceInfoList, TRUE);
- DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiOpenDevRegKey, TRUE);
- DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetDeviceRegistryPropertyA, TRUE);
- DLL_LOAD_PREFIXED(AdvAPI32.dll, p, RegQueryValueExW, TRUE);
- DLL_LOAD_PREFIXED(AdvAPI32.dll, p, RegCloseKey, TRUE);
- return LIBUSB_SUCCESS;
- }
- /*
- * enumerate interfaces for the whole USB class
- *
- * Parameters:
- * dev_info: a pointer to a dev_info list
- * dev_info_data: a pointer to an SP_DEVINFO_DATA to be filled (or NULL if not needed)
- * usb_class: the generic USB class for which to retrieve interface details
- * index: zero based index of the interface in the device info list
- *
- * Note: it is the responsibility of the caller to free the DEVICE_INTERFACE_DETAIL_DATA
- * structure returned and call this function repeatedly using the same guid (with an
- * incremented index starting at zero) until all interfaces have been returned.
- */
- static bool get_devinfo_data(struct libusb_context *ctx,
- HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, char* usb_class, unsigned _index)
- {
- if (_index <= 0) {
- *dev_info = pSetupDiGetClassDevsA(NULL, usb_class, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
- if (*dev_info == INVALID_HANDLE_VALUE) {
- return false;
- }
- }
- dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA);
- if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) {
- if (GetLastError() != ERROR_NO_MORE_ITEMS) {
- usbi_err(ctx, "Could not obtain device info data for index %u: %s",
- _index, windows_error_str(0));
- }
- pSetupDiDestroyDeviceInfoList(*dev_info);
- *dev_info = INVALID_HANDLE_VALUE;
- return false;
- }
- return true;
- }
- /*
- * enumerate interfaces for a specific GUID
- *
- * Parameters:
- * dev_info: a pointer to a dev_info list
- * dev_info_data: a pointer to an SP_DEVINFO_DATA to be filled (or NULL if not needed)
- * guid: the GUID for which to retrieve interface details
- * index: zero based index of the interface in the device info list
- *
- * Note: it is the responsibility of the caller to free the DEVICE_INTERFACE_DETAIL_DATA
- * structure returned and call this function repeatedly using the same guid (with an
- * incremented index starting at zero) until all interfaces have been returned.
- */
- static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_context *ctx,
- HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, const GUID* guid, unsigned _index)
- {
- SP_DEVICE_INTERFACE_DATA dev_interface_data;
- SP_DEVICE_INTERFACE_DETAIL_DATA_A *dev_interface_details = NULL;
- DWORD size;
- if (_index <= 0) {
- *dev_info = pSetupDiGetClassDevsA(guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
- }
- if (dev_info_data != NULL) {
- dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA);
- if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) {
- if (GetLastError() != ERROR_NO_MORE_ITEMS) {
- usbi_err(ctx, "Could not obtain device info data for index %u: %s",
- _index, windows_error_str(0));
- }
- pSetupDiDestroyDeviceInfoList(*dev_info);
- *dev_info = INVALID_HANDLE_VALUE;
- return NULL;
- }
- }
- dev_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
- if (!pSetupDiEnumDeviceInterfaces(*dev_info, NULL, guid, _index, &dev_interface_data)) {
- if (GetLastError() != ERROR_NO_MORE_ITEMS) {
- usbi_err(ctx, "Could not obtain interface data for index %u: %s",
- _index, windows_error_str(0));
- }
- pSetupDiDestroyDeviceInfoList(*dev_info);
- *dev_info = INVALID_HANDLE_VALUE;
- return NULL;
- }
- // Read interface data (dummy + actual) to access the device path
- if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, NULL, 0, &size, NULL)) {
- // The dummy call should fail with ERROR_INSUFFICIENT_BUFFER
- if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
- usbi_err(ctx, "could not access interface data (dummy) for index %u: %s",
- _index, windows_error_str(0));
- goto err_exit;
- }
- } else {
- usbi_err(ctx, "program assertion failed - http://msdn.microsoft.com/en-us/library/ms792901.aspx is wrong.");
- goto err_exit;
- }
- if ((dev_interface_details = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) calloc(size, 1)) == NULL) {
- usbi_err(ctx, "could not allocate interface data for index %u.", _index);
- goto err_exit;
- }
- dev_interface_details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
- if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data,
- dev_interface_details, size, &size, NULL)) {
- usbi_err(ctx, "could not access interface data (actual) for index %u: %s",
- _index, windows_error_str(0));
- }
- return dev_interface_details;
- err_exit:
- pSetupDiDestroyDeviceInfoList(*dev_info);
- *dev_info = INVALID_HANDLE_VALUE;
- return NULL;
- }
- /* Hash table functions - modified From glibc 2.3.2:
- [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
- [Knuth] The Art of Computer Programming, part 3 (6.4) */
- typedef struct htab_entry {
- unsigned long used;
- char* str;
- } htab_entry;
- htab_entry* htab_table = NULL;
- usbi_mutex_t htab_write_mutex = NULL;
- unsigned long htab_size, htab_filled;
- /* For the used double hash method the table size has to be a prime. To
- correct the user given table size we need a prime test. This trivial
- algorithm is adequate because the code is called only during init and
- the number is likely to be small */
- static int isprime(unsigned long number)
- {
- // no even number will be passed
- unsigned int divider = 3;
- while((divider * divider < number) && (number % divider != 0))
- divider += 2;
- return (number % divider != 0);
- }
- /* Before using the hash table we must allocate memory for it.
- We allocate one element more as the found prime number says.
- This is done for more effective indexing as explained in the
- comment for the hash function. */
- static int htab_create(struct libusb_context *ctx, unsigned long nel)
- {
- if (htab_table != NULL) {
- usbi_err(ctx, "hash table already allocated");
- }
- // Create a mutex
- usbi_mutex_init(&htab_write_mutex, NULL);
- // Change nel to the first prime number not smaller as nel.
- nel |= 1;
- while(!isprime(nel))
- nel += 2;
- htab_size = nel;
- usbi_dbg("using %d entries hash table", nel);
- htab_filled = 0;
- // allocate memory and zero out.
- htab_table = (htab_entry*)calloc(htab_size + 1, sizeof(htab_entry));
- if (htab_table == NULL) {
- usbi_err(ctx, "could not allocate space for hash table");
- return 0;
- }
- return 1;
- }
- /* After using the hash table it has to be destroyed. */
- static void htab_destroy(void)
- {
- size_t i;
- if (htab_table == NULL) {
- return;
- }
- for (i=0; i<htab_size; i++) {
- if (htab_table[i].used) {
- safe_free(htab_table[i].str);
- }
- }
- usbi_mutex_destroy(&htab_write_mutex);
- safe_free(htab_table);
- }
- /* This is the search function. It uses double hashing with open addressing.
- We use an trick to speed up the lookup. The table is created with one
- more element available. This enables us to use the index zero special.
- This index will never be used because we store the first hash index in
- the field used where zero means not used. Every other value means used.
- The used field can be used as a first fast comparison for equality of
- the stored and the parameter value. This helps to prevent unnecessary
- expensive calls of strcmp. */
- static unsigned long htab_hash(char* str)
- {
- unsigned long hval, hval2;
- unsigned long idx;
- unsigned long r = 5381;
- int c;
- char* sz = str;
- // Compute main hash value (algorithm suggested by Nokia)
- while ((c = *sz++))
- r = ((r << 5) + r) + c;
- if (r == 0)
- ++r;
- // compute table hash: simply take the modulus
- hval = r % htab_size;
- if (hval == 0)
- ++hval;
- // Try the first index
- idx = hval;
- if (htab_table[idx].used) {
- if ( (htab_table[idx].used == hval)
- && (safe_strcmp(str, htab_table[idx].str) == 0) ) {
- // existing hash
- return idx;
- }
- usbi_dbg("hash collision ('%s' vs '%s')", str, htab_table[idx].str);
- // Second hash function, as suggested in [Knuth]
- hval2 = 1 + hval % (htab_size - 2);
- do {
- // Because size is prime this guarantees to step through all available indexes
- if (idx <= hval2) {
- idx = htab_size + idx - hval2;
- } else {
- idx -= hval2;
- }
- // If we visited all entries leave the loop unsuccessfully
- if (idx == hval) {
- break;
- }
- // If entry is found use it.
- if ( (htab_table[idx].used == hval)
- && (safe_strcmp(str, htab_table[idx].str) == 0) ) {
- return idx;
- }
- }
- while (htab_table[idx].used);
- }
- // Not found => New entry
- // If the table is full return an error
- if (htab_filled >= htab_size) {
- usbi_err(NULL, "hash table is full (%d entries)", htab_size);
- return 0;
- }
- // Concurrent threads might be storing the same entry at the same time
- // (eg. "simultaneous" enums from different threads) => use a mutex
- usbi_mutex_lock(&htab_write_mutex);
- // Just free any previously allocated string (which should be the same as
- // new one). The possibility of concurrent threads storing a collision
- // string (same hash, different string) at the same time is extremely low
- safe_free(htab_table[idx].str);
- htab_table[idx].used = hval;
- htab_table[idx].str = (char*) calloc(1, safe_strlen(str)+1);
- if (htab_table[idx].str == NULL) {
- usbi_err(NULL, "could not duplicate string for hash table");
- usbi_mutex_unlock(&htab_write_mutex);
- return 0;
- }
- memcpy(htab_table[idx].str, str, safe_strlen(str)+1);
- ++htab_filled;
- usbi_mutex_unlock(&htab_write_mutex);
- return idx;
- }
- /*
- * Returns the session ID of a device's nth level ancestor
- * If there's no device at the nth level, return 0
- */
- static unsigned long get_ancestor_session_id(DWORD devinst, unsigned level)
- {
- DWORD parent_devinst;
- unsigned long session_id = 0;
- char* sanitized_path = NULL;
- char path[MAX_PATH_LENGTH];
- unsigned i;
- if (level < 1) return 0;
- for (i = 0; i<level; i++) {
- if (CM_Get_Parent(&parent_devinst, devinst, 0) != CR_SUCCESS) {
- return 0;
- }
- devinst = parent_devinst;
- }
- if (CM_Get_Device_IDA(devinst, path, MAX_PATH_LENGTH, 0) != CR_SUCCESS) {
- return 0;
- }
- // TODO (post hotplug): try without sanitizing
- sanitized_path = sanitize_path(path);
- if (sanitized_path == NULL) {
- return 0;
- }
- session_id = htab_hash(sanitized_path);
- safe_free(sanitized_path);
- return session_id;
- }
- /*
- * Populate the endpoints addresses of the device_priv interface helper structs
- */
- static int windows_assign_endpoints(struct libusb_device_handle *dev_handle, int iface, int altsetting)
- {
- int i, r;
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- struct libusb_config_descriptor *conf_desc;
- const struct libusb_interface_descriptor *if_desc;
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- r = libusb_get_config_descriptor(dev_handle->dev, 0, &conf_desc);
- if (r != LIBUSB_SUCCESS) {
- usbi_warn(ctx, "could not read config descriptor: error %d", r);
- return r;
- }
- if_desc = &conf_desc->interface[iface].altsetting[altsetting];
- safe_free(priv->usb_interface[iface].endpoint);
- if (if_desc->bNumEndpoints == 0) {
- usbi_dbg("no endpoints found for interface %d", iface);
- return LIBUSB_SUCCESS;
- }
- priv->usb_interface[iface].endpoint = (uint8_t*) calloc(1, if_desc->bNumEndpoints);
- if (priv->usb_interface[iface].endpoint == NULL) {
- return LIBUSB_ERROR_NO_MEM;
- }
- priv->usb_interface[iface].nb_endpoints = if_desc->bNumEndpoints;
- for (i=0; i<if_desc->bNumEndpoints; i++) {
- priv->usb_interface[iface].endpoint[i] = if_desc->endpoint[i].bEndpointAddress;
- usbi_dbg("(re)assigned endpoint %02X to interface %d", priv->usb_interface[iface].endpoint[i], iface);
- }
- libusb_free_config_descriptor(conf_desc);
- // Extra init is required for WinUSB endpoints
- if (priv->apib->id == USB_API_WINUSB) {
- return winusb_configure_endpoints(dev_handle, iface);
- }
- return LIBUSB_SUCCESS;
- }
- // Lookup for a match in the list of API driver names
- static bool is_api_driver(char* driver, uint8_t api)
- {
- uint8_t i;
- const char sep_str[2] = {LIST_SEPARATOR, 0};
- char *tok, *tmp_str;
- size_t len = safe_strlen(driver);
- if (len == 0) return false;
- tmp_str = (char*) calloc(1, len+1);
- if (tmp_str == NULL) return false;
- memcpy(tmp_str, driver, len+1);
- tok = strtok(tmp_str, sep_str);
- while (tok != NULL) {
- for (i=0; i<usb_api_backend[api].nb_driver_names; i++) {
- if (safe_strcmp(tok, usb_api_backend[api].driver_name_list[i]) == 0) {
- free(tmp_str);
- return true;
- }
- }
- tok = strtok(NULL, sep_str);
- }
- free (tmp_str);
- return false;
- }
- /*
- * auto-claiming and auto-release helper functions
- */
- static int auto_claim(struct libusb_transfer *transfer, int *interface_number, int api_type)
- {
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(
- transfer->dev_handle);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- int current_interface = *interface_number;
- int r = LIBUSB_SUCCESS;
- usbi_mutex_lock(&autoclaim_lock);
- if (current_interface < 0) // No serviceable interface was found
- {
- for (current_interface=0; current_interface<USB_MAXINTERFACES; current_interface++) {
- // Must claim an interface of the same API type
- if ( (priv->usb_interface[current_interface].apib->id == api_type)
- && (libusb_claim_interface(transfer->dev_handle, current_interface) == LIBUSB_SUCCESS) ) {
- usbi_dbg("auto-claimed interface %d for control request", current_interface);
- if (handle_priv->autoclaim_count[current_interface] != 0) {
- usbi_warn(ctx, "program assertion failed - autoclaim_count was nonzero");
- }
- handle_priv->autoclaim_count[current_interface]++;
- break;
- }
- }
- if (current_interface == USB_MAXINTERFACES) {
- usbi_err(ctx, "could not auto-claim any interface");
- r = LIBUSB_ERROR_NOT_FOUND;
- }
- } else {
- // If we have a valid interface that was autoclaimed, we must increment
- // its autoclaim count so that we can prevent an early release.
- if (handle_priv->autoclaim_count[current_interface] != 0) {
- handle_priv->autoclaim_count[current_interface]++;
- }
- }
- usbi_mutex_unlock(&autoclaim_lock);
- *interface_number = current_interface;
- return r;
- }
- static void auto_release(struct usbi_transfer *itransfer)
- {
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- libusb_device_handle *dev_handle = transfer->dev_handle;
- struct windows_device_handle_priv* handle_priv = _device_handle_priv(dev_handle);
- int r;
- usbi_mutex_lock(&autoclaim_lock);
- if (handle_priv->autoclaim_count[transfer_priv->interface_number] > 0) {
- handle_priv->autoclaim_count[transfer_priv->interface_number]--;
- if (handle_priv->autoclaim_count[transfer_priv->interface_number] == 0) {
- r = libusb_release_interface(dev_handle, transfer_priv->interface_number);
- if (r == LIBUSB_SUCCESS) {
- usbi_dbg("auto-released interface %d", transfer_priv->interface_number);
- } else {
- usbi_dbg("failed to auto-release interface %d (%s)",
- transfer_priv->interface_number, libusb_error_name((enum libusb_error)r));
- }
- }
- }
- usbi_mutex_unlock(&autoclaim_lock);
- }
- /*
- * init: libusb backend init function
- *
- * This function enumerates the HCDs (Host Controller Drivers) and populates our private HCD list
- * In our implementation, we equate Windows' "HCD" to LibUSB's "bus". Note that bus is zero indexed.
- * HCDs are not expected to change after init (might not hold true for hot pluggable USB PCI card?)
- */
- static int windows_init(struct libusb_context *ctx)
- {
- int i, r = LIBUSB_ERROR_OTHER;
- OSVERSIONINFO os_version;
- HANDLE semaphore;
- char sem_name[11+1+8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
- sprintf(sem_name, "libusb_init%08X", (unsigned int)GetCurrentProcessId()&0xFFFFFFFF);
- semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name);
- if (semaphore == NULL) {
- usbi_err(ctx, "could not create semaphore: %s", windows_error_str(0));
- return LIBUSB_ERROR_NO_MEM;
- }
- // A successful wait brings our semaphore count to 0 (unsignaled)
- // => any concurent wait stalls until the semaphore's release
- if (WaitForSingleObject(semaphore, INFINITE) != WAIT_OBJECT_0) {
- usbi_err(ctx, "failure to access semaphore: %s", windows_error_str(0));
- CloseHandle(semaphore);
- return LIBUSB_ERROR_NO_MEM;
- }
- // NB: concurrent usage supposes that init calls are equally balanced with
- // exit calls. If init is called more than exit, we will not exit properly
- if ( ++concurrent_usage == 0 ) { // First init?
- // Detect OS version
- memset(&os_version, 0, sizeof(OSVERSIONINFO));
- os_version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- windows_version = WINDOWS_UNSUPPORTED;
- if ((GetVersionEx(&os_version) != 0) && (os_version.dwPlatformId == VER_PLATFORM_WIN32_NT)) {
- if ((os_version.dwMajorVersion == 5) && (os_version.dwMinorVersion == 1)) {
- windows_version = WINDOWS_XP;
- } else if ((os_version.dwMajorVersion == 5) && (os_version.dwMinorVersion == 2)) {
- windows_version = WINDOWS_2003; // also includes XP 64
- } else if (os_version.dwMajorVersion >= 6) {
- windows_version = WINDOWS_VISTA_AND_LATER;
- }
- }
- if (windows_version == WINDOWS_UNSUPPORTED) {
- usbi_err(ctx, "This version of Windows is NOT supported");
- r = LIBUSB_ERROR_NOT_SUPPORTED;
- goto init_exit;
- }
- // We need a lock for proper auto-release
- usbi_mutex_init(&autoclaim_lock, NULL);
- // Initialize pollable file descriptors
- init_polling();
- // Load DLL imports
- if (init_dlls() != LIBUSB_SUCCESS) {
- usbi_err(ctx, "could not resolve DLL functions");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- // Initialize the low level APIs (we don't care about errors at this stage)
- for (i=0; i<USB_API_MAX; i++) {
- usb_api_backend[i].init(ctx);
- }
- // Because QueryPerformanceCounter might report different values when
- // running on different cores, we create a separate thread for the timer
- // calls, which we glue to the first core always to prevent timing discrepancies.
- r = LIBUSB_ERROR_NO_MEM;
- for (i = 0; i < 2; i++) {
- timer_request[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
- if (timer_request[i] == NULL) {
- usbi_err(ctx, "could not create timer request event %d - aborting", i);
- goto init_exit;
- }
- }
- timer_response = CreateSemaphore(NULL, 0, MAX_TIMER_SEMAPHORES, NULL);
- if (timer_response == NULL) {
- usbi_err(ctx, "could not create timer response semaphore - aborting");
- goto init_exit;
- }
- timer_mutex = CreateMutex(NULL, FALSE, NULL);
- if (timer_mutex == NULL) {
- usbi_err(ctx, "could not create timer mutex - aborting");
- goto init_exit;
- }
- timer_thread = (HANDLE)_beginthreadex(NULL, 0, windows_clock_gettime_threaded, NULL, 0, NULL);
- if (timer_thread == NULL) {
- usbi_err(ctx, "Unable to create timer thread - aborting");
- goto init_exit;
- }
- SetThreadAffinityMask(timer_thread, 0);
- // Create a hash table to store session ids. Second parameter is better if prime
- htab_create(ctx, HTAB_SIZE);
- }
- // At this stage, either we went through full init successfully, or didn't need to
- r = LIBUSB_SUCCESS;
- init_exit: // Holds semaphore here.
- if (!concurrent_usage && r != LIBUSB_SUCCESS) { // First init failed?
- if (timer_thread) {
- SetEvent(timer_request[1]); // actually the signal to quit the thread.
- if (WAIT_OBJECT_0 != WaitForSingleObject(timer_thread, INFINITE)) {
- usbi_warn(ctx, "could not wait for timer thread to quit");
- TerminateThread(timer_thread, 1); // shouldn't happen, but we're destroying
- // all objects it might have held anyway.
- }
- CloseHandle(timer_thread);
- timer_thread = NULL;
- }
- for (i = 0; i < 2; i++) {
- if (timer_request[i]) {
- CloseHandle(timer_request[i]);
- timer_request[i] = NULL;
- }
- }
- if (timer_response) {
- CloseHandle(timer_response);
- timer_response = NULL;
- }
- if (timer_mutex) {
- CloseHandle(timer_mutex);
- timer_mutex = NULL;
- }
- htab_destroy();
- }
- if (r != LIBUSB_SUCCESS)
- --concurrent_usage; // Not expected to call libusb_exit if we failed.
- ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
- CloseHandle(semaphore);
- return r;
- }
- /*
- * HCD (root) hubs need to have their device descriptor manually populated
- *
- * Note that, like Microsoft does in the device manager, we populate the
- * Vendor and Device ID for HCD hubs with the ones from the PCI HCD device.
- */
- static int force_hcd_device_descriptor(struct libusb_device *dev)
- {
- struct windows_device_priv *parent_priv, *priv = _device_priv(dev);
- struct libusb_context *ctx = DEVICE_CTX(dev);
- int vid, pid;
- dev->num_configurations = 1;
- priv->dev_descriptor.bLength = sizeof(USB_DEVICE_DESCRIPTOR);
- priv->dev_descriptor.bDescriptorType = USB_DEVICE_DESCRIPTOR_TYPE;
- priv->dev_descriptor.bNumConfigurations = 1;
- priv->active_config = 1;
- if (priv->parent_dev == NULL) {
- usbi_err(ctx, "program assertion failed - HCD hub has no parent");
- return LIBUSB_ERROR_NO_DEVICE;
- }
- parent_priv = _device_priv(priv->parent_dev);
- if (sscanf(parent_priv->path, "\\\\.\\PCI#VEN_%04x&DEV_%04x%*s", &vid, &pid) == 2) {
- priv->dev_descriptor.idVendor = (uint16_t)vid;
- priv->dev_descriptor.idProduct = (uint16_t)pid;
- } else {
- usbi_warn(ctx, "could not infer VID/PID of HCD hub from '%s'", parent_priv->path);
- priv->dev_descriptor.idVendor = 0x1d6b; // Linux Foundation root hub
- priv->dev_descriptor.idProduct = 1;
- }
- return LIBUSB_SUCCESS;
- }
- /*
- * fetch and cache all the config descriptors through I/O
- */
- static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle, char* device_id)
- {
- DWORD size, ret_size;
- struct libusb_context *ctx = DEVICE_CTX(dev);
- struct windows_device_priv *priv = _device_priv(dev);
- int r;
- uint8_t i;
- USB_CONFIGURATION_DESCRIPTOR_SHORT cd_buf_short; // dummy request
- PUSB_DESCRIPTOR_REQUEST cd_buf_actual = NULL; // actual request
- PUSB_CONFIGURATION_DESCRIPTOR cd_data = NULL;
- if (dev->num_configurations == 0)
- return LIBUSB_ERROR_INVALID_PARAM;
- priv->config_descriptor = (unsigned char**) calloc(dev->num_configurations, sizeof(PUSB_CONFIGURATION_DESCRIPTOR));
- if (priv->config_descriptor == NULL)
- return LIBUSB_ERROR_NO_MEM;
- for (i=0; i<dev->num_configurations; i++)
- priv->config_descriptor[i] = NULL;
- for (i=0, r=LIBUSB_SUCCESS; ; i++)
- {
- // safe loop: release all dynamic resources
- safe_free(cd_buf_actual);
- // safe loop: end of loop condition
- if ((i >= dev->num_configurations) || (r != LIBUSB_SUCCESS))
- break;
- size = sizeof(USB_CONFIGURATION_DESCRIPTOR_SHORT);
- memset(&cd_buf_short, 0, size);
- cd_buf_short.req.ConnectionIndex = (ULONG)priv->port;
- cd_buf_short.req.SetupPacket.bmRequest = LIBUSB_ENDPOINT_IN;
- cd_buf_short.req.SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR;
- cd_buf_short.req.SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | i;
- cd_buf_short.req.SetupPacket.wIndex = i;
- cd_buf_short.req.SetupPacket.wLength = (USHORT)(size - sizeof(USB_DESCRIPTOR_REQUEST));
- // Dummy call to get the required data size
- if (!DeviceIoControl(hub_handle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &cd_buf_short, size,
- &cd_buf_short, size, &ret_size, NULL)) {
- usbi_err(ctx, "could not access configuration descriptor (dummy) for '%s': %s", device_id, windows_error_str(0));
- LOOP_BREAK(LIBUSB_ERROR_IO);
- }
- if ((ret_size != size) || (cd_buf_short.data.wTotalLength < sizeof(USB_CONFIGURATION_DESCRIPTOR))) {
- usbi_err(ctx, "unexpected configuration descriptor size (dummy) for '%s'.", device_id);
- LOOP_BREAK(LIBUSB_ERROR_IO);
- }
- size = sizeof(USB_DESCRIPTOR_REQUEST) + cd_buf_short.data.wTotalLength;
- if ((cd_buf_actual = (PUSB_DESCRIPTOR_REQUEST) calloc(1, size)) == NULL) {
- usbi_err(ctx, "could not allocate configuration descriptor buffer for '%s'.", device_id);
- LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
- }
- memset(cd_buf_actual, 0, size);
- // Actual call
- cd_buf_actual->ConnectionIndex = (ULONG)priv->port;
- cd_buf_actual->SetupPacket.bmRequest = LIBUSB_ENDPOINT_IN;
- cd_buf_actual->SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR;
- cd_buf_actual->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | i;
- cd_buf_actual->SetupPacket.wIndex = i;
- cd_buf_actual->SetupPacket.wLength = (USHORT)(size - sizeof(USB_DESCRIPTOR_REQUEST));
- if (!DeviceIoControl(hub_handle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, cd_buf_actual, size,
- cd_buf_actual, size, &ret_size, NULL)) {
- usbi_err(ctx, "could not access configuration descriptor (actual) for '%s': %s", device_id, windows_error_str(0));
- LOOP_BREAK(LIBUSB_ERROR_IO);
- }
- cd_data = (PUSB_CONFIGURATION_DESCRIPTOR)((UCHAR*)cd_buf_actual+sizeof(USB_DESCRIPTOR_REQUEST));
- if ((size != ret_size) || (cd_data->wTotalLength != cd_buf_short.data.wTotalLength)) {
- usbi_err(ctx, "unexpected configuration descriptor size (actual) for '%s'.", device_id);
- LOOP_BREAK(LIBUSB_ERROR_IO);
- }
- if (cd_data->bDescriptorType != USB_CONFIGURATION_DESCRIPTOR_TYPE) {
- usbi_err(ctx, "not a configuration descriptor for '%s'", device_id);
- LOOP_BREAK(LIBUSB_ERROR_IO);
- }
- usbi_dbg("cached config descriptor %d (bConfigurationValue=%d, %d bytes)",
- i, cd_data->bConfigurationValue, cd_data->wTotalLength);
- // Cache the descriptor
- priv->config_descriptor[i] = (unsigned char*) calloc(1, cd_data->wTotalLength);
- if (priv->config_descriptor[i] == NULL)
- return LIBUSB_ERROR_NO_MEM;
- memcpy(priv->config_descriptor[i], cd_data, cd_data->wTotalLength);
- }
- return LIBUSB_SUCCESS;
- }
- /*
- * Populate a libusb device structure
- */
- static int init_device(struct libusb_device* dev, struct libusb_device* parent_dev,
- uint8_t port_number, char* device_id, DWORD devinst)
- {
- HANDLE handle;
- DWORD size;
- USB_NODE_CONNECTION_INFORMATION_EX conn_info;
- struct windows_device_priv *priv, *parent_priv;
- struct libusb_context *ctx = DEVICE_CTX(dev);
- struct libusb_device* tmp_dev;
- unsigned i;
- if ((dev == NULL) || (parent_dev == NULL)) {
- return LIBUSB_ERROR_NOT_FOUND;
- }
- priv = _device_priv(dev);
- parent_priv = _device_priv(parent_dev);
- if (parent_priv->apib->id != USB_API_HUB) {
- usbi_warn(ctx, "parent for device '%s' is not a hub", device_id);
- return LIBUSB_ERROR_NOT_FOUND;
- }
- // It is possible for the parent hub not to have been initialized yet
- // If that's the case, lookup the ancestors to set the bus number
- if (parent_dev->bus_number == 0) {
- for (i=2; ; i++) {
- tmp_dev = usbi_get_device_by_session_id(ctx, get_ancestor_session_id(devinst, i));
- if (tmp_dev == NULL) break;
- if (tmp_dev->bus_number != 0) {
- usbi_dbg("got bus number from ancestor #%d", i);
- parent_dev->bus_number = tmp_dev->bus_number;
- break;
- }
- }
- }
- if (parent_dev->bus_number == 0) {
- usbi_err(ctx, "program assertion failed: unable to find ancestor bus number for '%s'", device_id);
- return LIBUSB_ERROR_NOT_FOUND;
- }
- dev->bus_number = parent_dev->bus_number;
- priv->port = port_number;
- priv->depth = parent_priv->depth + 1;
- priv->parent_dev = parent_dev;
- // If the device address is already set, we can stop here
- if (dev->device_address != 0) {
- return LIBUSB_SUCCESS;
- }
- memset(&conn_info, 0, sizeof(conn_info));
- if (priv->depth != 0) { // Not a HCD hub
- handle = CreateFileA(parent_priv->path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
- FILE_FLAG_OVERLAPPED, NULL);
- if (handle == INVALID_HANDLE_VALUE) {
- usbi_warn(ctx, "could not open hub %s: %s", parent_priv->path, windows_error_str(0));
- return LIBUSB_ERROR_ACCESS;
- }
- size = sizeof(conn_info);
- conn_info.ConnectionIndex = (ULONG)port_number;
- if (!DeviceIoControl(handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, &conn_info, size,
- &conn_info, size, &size, NULL)) {
- usbi_warn(ctx, "could not get node connection information for device '%s': %s",
- device_id, windows_error_str(0));
- safe_closehandle(handle);
- return LIBUSB_ERROR_NO_DEVICE;
- }
- if (conn_info.ConnectionStatus == NoDeviceConnected) {
- usbi_err(ctx, "device '%s' is no longer connected!", device_id);
- safe_closehandle(handle);
- return LIBUSB_ERROR_NO_DEVICE;
- }
- memcpy(&priv->dev_descriptor, &(conn_info.DeviceDescriptor), sizeof(USB_DEVICE_DESCRIPTOR));
- dev->num_configurations = priv->dev_descriptor.bNumConfigurations;
- priv->active_config = conn_info.CurrentConfigurationValue;
- usbi_dbg("found %d configurations (active conf: %d)", dev->num_configurations, priv->active_config);
- // If we can't read the config descriptors, just set the number of confs to zero
- if (cache_config_descriptors(dev, handle, device_id) != LIBUSB_SUCCESS) {
- dev->num_configurations = 0;
- priv->dev_descriptor.bNumConfigurations = 0;
- }
- safe_closehandle(handle);
- if (conn_info.DeviceAddress > UINT8_MAX) {
- usbi_err(ctx, "program assertion failed: device address overflow");
- }
- dev->device_address = (uint8_t)conn_info.DeviceAddress;
- switch (conn_info.Speed) {
- case 0: dev->speed = LIBUSB_SPEED_LOW; break;
- case 1: dev->speed = LIBUSB_SPEED_FULL; break;
- case 2: dev->speed = LIBUSB_SPEED_HIGH; break;
- case 3: dev->speed = LIBUSB_SPEED_SUPER; break;
- default:
- usbi_warn(ctx, "Got unknown device speed %d", conn_info.Speed);
- break;
- }
- } else {
- dev->device_address = UINT8_MAX; // Hubs from HCD have a devaddr of 255
- force_hcd_device_descriptor(dev);
- }
- usbi_sanitize_device(dev);
- usbi_dbg("(bus: %d, addr: %d, depth: %d, port: %d): '%s'",
- dev->bus_number, dev->device_address, priv->depth, priv->port, device_id);
- return LIBUSB_SUCCESS;
- }
- // Returns the api type, or 0 if not found/unsupported
- static uint8_t get_api_type(struct libusb_context *ctx,
- HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data)
- {
- // Precedence for filter drivers vs driver is in the order of this array
- struct driver_lookup lookup[3] = {
- {"\0\0", SPDRP_SERVICE, "driver"},
- {"\0\0", SPDRP_UPPERFILTERS, "upper filter driver"},
- {"\0\0", SPDRP_LOWERFILTERS, "lower filter driver"}
- };
- DWORD size, reg_type;
- unsigned k, l;
- uint8_t api;
- // Check the service & filter names to know the API we should use
- for (k=0; k<3; k++) {
- if (pSetupDiGetDeviceRegistryPropertyA(*dev_info, dev_info_data, lookup[k].reg_prop,
- ®_type, (BYTE*)lookup[k].list, MAX_KEY_LENGTH, &size)) {
- // Turn the REG_SZ SPDRP_SERVICE into REG_MULTI_SZ
- if (lookup[k].reg_prop == SPDRP_SERVICE) {
- // our buffers are MAX_KEY_LENGTH+1 so we can overflow if needed
- lookup[k].list[safe_strlen(lookup[k].list)+1] = 0;
- }
- // MULTI_SZ is a pain to work with. Turn it into something much more manageable
- // NB: none of the driver names we check against contain LIST_SEPARATOR,
- // (currently ';'), so even if an unsuported one does, it's not an issue
- for (l=0; (lookup[k].list[l] != 0) || (lookup[k].list[l+1] != 0); l++) {
- if (lookup[k].list[l] == 0) {
- lookup[k].list[l] = LIST_SEPARATOR;
- }
- }
- upperize(lookup[k].list);
- usbi_dbg("%s(s): %s", lookup[k].designation, lookup[k].list);
- } else {
- if (GetLastError() != ERROR_INVALID_DATA) {
- usbi_dbg("could not access %s: %s", lookup[k].designation, windows_error_str(0));
- }
- lookup[k].list[0] = 0;
- }
- }
- for (api=1; api<USB_API_MAX; api++) {
- for (k=0; k<3; k++) {
- if (is_api_driver(lookup[k].list, api)) {
- usbi_dbg("matched %s name against %s", lookup[k].designation, usb_api_backend[api].designation);
- break;
- }
- }
- if (k >= 3) continue;
- return api;
- }
- return 0;
- }
- static int set_composite_interface(struct libusb_context* ctx, struct libusb_device* dev,
- char* dev_interface_path, char* device_id, uint8_t api)
- {
- unsigned i;
- struct windows_device_priv *priv = _device_priv(dev);
- int interface_number;
- if (priv->apib->id != USB_API_COMPOSITE) {
- usbi_err(ctx, "program assertion failed: '%s' is not composite", device_id);
- return LIBUSB_ERROR_NO_DEVICE;
- }
- // Because MI_## are not necessarily in sequential order (some composite
- // devices will have only MI_00 & MI_03 for instance), we retrieve the actual
- // interface number from the path's MI value
- interface_number = 0;
- for (i=0; device_id[i] != 0; ) {
- if ( (device_id[i++] == 'M') && (device_id[i++] == 'I')
- && (device_id[i++] == '_') ) {
- interface_number = (device_id[i++] - '0')*10;
- interface_number += device_id[i] - '0';
- break;
- }
- }
- if (device_id[i] == 0) {
- usbi_warn(ctx, "failure to read interface number for %s. Using default value %d",
- device_id, interface_number);
- }
- if (priv->usb_interface[interface_number].path != NULL) {
- usbi_warn(ctx, "interface[%d] already set - ignoring: %s", interface_number, device_id);
- return LIBUSB_ERROR_ACCESS;
- }
- usbi_dbg("interface[%d] = %s", interface_number, dev_interface_path);
- priv->usb_interface[interface_number].path = dev_interface_path;
- priv->usb_interface[interface_number].apib = &usb_api_backend[api];
- priv->composite_api_flags |= 1<<api;
- return LIBUSB_SUCCESS;
- }
- /*
- * get_device_list: libusb backend device enumeration function
- */
- static int windows_get_device_list(struct libusb_context *ctx, struct discovered_devs **_discdevs)
- {
- struct discovered_devs *discdevs;
- HDEVINFO dev_info = { 0 };
- char* usb_class[2] = {"USB", "NUSB3"};
- SP_DEVINFO_DATA dev_info_data;
- SP_DEVICE_INTERFACE_DETAIL_DATA_A *dev_interface_details = NULL;
- #define MAX_ENUM_GUIDS 64
- const GUID* guid[MAX_ENUM_GUIDS];
- #define HCD_PASS 0
- #define HUB_PASS 1
- #define GEN_PASS 2
- #define DEV_PASS 3
- int r = LIBUSB_SUCCESS;
- int class_index = 0;
- unsigned int nb_guids, pass, i, j, ancestor;
- char path[MAX_PATH_LENGTH];
- char strbuf[MAX_PATH_LENGTH];
- struct libusb_device *dev, *parent_dev;
- struct windows_device_priv *priv, *parent_priv;
- char* dev_interface_path = NULL;
- char* dev_id_path = NULL;
- unsigned long session_id;
- DWORD size, reg_type, port_nr, install_state;
- BOOL b = FALSE;
- HKEY key;
- WCHAR guid_string_w[MAX_GUID_STRING_LENGTH];
- GUID* if_guid;
- LONG s;
- uint8_t api;
- // Keep a list of newly allocated devs to unref
- libusb_device** unref_list;
- unsigned int unref_size = 64;
- unsigned int unref_cur = 0;
- // PASS 1 : (re)enumerate HCDs (allows for HCD hotplug)
- // PASS 2 : (re)enumerate HUBS
- // PASS 3 : (re)enumerate generic USB devices (including driverless)
- // and list additional USB device interface GUIDs to explore
- // PASS 4 : (re)enumerate master USB devices that have a device interface
- // PASS 5+: (re)enumerate device interfaced GUIDs and set the device interfaces.
- // Init the GUID table
- guid[HCD_PASS] = &GUID_DEVINTERFACE_USB_HOST_CONTROLLER;
- guid[HUB_PASS] = &GUID_DEVINTERFACE_USB_HUB;
- guid[GEN_PASS] = NULL;
- guid[DEV_PASS] = &GUID_DEVINTERFACE_USB_DEVICE;
- nb_guids = DEV_PASS+1;
- unref_list = (libusb_device**) calloc(unref_size, sizeof(libusb_device*));
- if (unref_list == NULL) {
- return LIBUSB_ERROR_NO_MEM;
- }
- for (pass = 0; ((pass < nb_guids) && (r == LIBUSB_SUCCESS)); pass++) {
- //#define ENUM_DEBUG
- #ifdef ENUM_DEBUG
- switch(pass) {
- case HCD_PASS:
- usbi_dbg("PROCESSING HCDs %s", guid_to_string(guid[pass]));
- break;
- case HUB_PASS:
- usbi_dbg("PROCESSING HUBs %s", guid_to_string(guid[pass]));
- break;
- case DEV_PASS:
- usbi_dbg("PROCESSING DEVs %s", guid_to_string(guid[pass]));
- break;
- case GEN_PASS:
- usbi_dbg("PROCESSING GENs");
- break;
- default:
- usbi_dbg("PROCESSING EXTs %s", guid_to_string(guid[pass]));
- break;
- }
- #endif
- for (i = 0; ; i++) {
- // safe loop: free up any (unprotected) dynamic resource
- // NB: this is always executed before breaking the loop
- safe_free(dev_interface_details);
- safe_free(dev_interface_path);
- safe_free(dev_id_path);
- priv = parent_priv = NULL;
- dev = parent_dev = NULL;
- // Safe loop: end of loop conditions
- if (r != LIBUSB_SUCCESS) {
- break;
- }
- if ((pass == HCD_PASS) && (i == UINT8_MAX)) {
- usbi_warn(ctx, "program assertion failed - found more than %d buses, skipping the rest.", UINT8_MAX);
- break;
- }
- if (pass != GEN_PASS) {
- // Except for GEN, all passes deal with device interfaces
- dev_interface_details = get_interface_details(ctx, &dev_info, &dev_info_data, guid[pass], i);
- if (dev_interface_details == NULL) {
- break;
- } else {
- dev_interface_path = sanitize_path(dev_interface_details->DevicePath);
- if (dev_interface_path == NULL) {
- usbi_warn(ctx, "could not sanitize device interface path for '%s'", dev_interface_details->DevicePath);
- continue;
- }
- }
- } else {
- // Workaround for a Nec/Renesas USB 3.0 driver bug where root hubs are
- // being listed under the "NUSB3" PnP Symbolic Name rather than "USB"
- while ( (class_index < 2) &&
- (!(b = get_devinfo_data(ctx, &dev_info, &dev_info_data, usb_class[class_index], i))) ) {
- class_index++;
- i = 0;
- }
- if (!b) break;
- }
- // Read the Device ID path. This is what we'll use as UID
- // Note that if the device is plugged in a different port or hub, the Device ID changes
- if (CM_Get_Device_IDA(dev_info_data.DevInst, path, sizeof(path), 0) != CR_SUCCESS) {
- usbi_warn(ctx, "could not read the device id path for devinst %X, skipping",
- dev_info_data.DevInst);
- continue;
- }
- dev_id_path = sanitize_path(path);
- if (dev_id_path == NULL) {
- usbi_warn(ctx, "could not sanitize device id path for devinst %X, skipping",
- dev_info_data.DevInst);
- continue;
- }
- #ifdef ENUM_DEBUG
- usbi_dbg("PRO: %s", dev_id_path);
- #endif
- // The SPDRP_ADDRESS for USB devices is the device port number on the hub
- port_nr = 0;
- if ((pass >= HUB_PASS) && (pass <= GEN_PASS)) {
- if ( (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_ADDRESS,
- ®_type, (BYTE*)&port_nr, 4, &size))
- || (size != 4) ) {
- usbi_warn(ctx, "could not retrieve port number for device '%s', skipping: %s",
- dev_id_path, windows_error_str(0));
- continue;
- }
- }
- // Set API to use or get additional data from generic pass
- api = USB_API_UNSUPPORTED;
- switch (pass) {
- case HCD_PASS:
- break;
- case GEN_PASS:
- // We use the GEN pass to detect driverless devices...
- size = sizeof(strbuf);
- if (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_DRIVER,
- ®_type, (BYTE*)strbuf, size, &size)) {
- usbi_info(ctx, "The following device has no driver: '%s'", dev_id_path);
- usbi_info(ctx, "libusb will not be able to access it.");
- }
- // ...and to add the additional device interface GUIDs
- key = pSetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
- if (key != INVALID_HANDLE_VALUE) {
- size = sizeof(guid_string_w);
- s = pRegQueryValueExW(key, L"DeviceInterfaceGUIDs", NULL, ®_type,
- (BYTE*)guid_string_w, &size);
- pRegCloseKey(key);
- if (s == ERROR_SUCCESS) {
- if (nb_guids >= MAX_ENUM_GUIDS) {
- // If this assert is ever reported, grow a GUID table dynamically
- usbi_err(ctx, "program assertion failed: too many GUIDs");
- LOOP_BREAK(LIBUSB_ERROR_OVERFLOW);
- }
- if_guid = (GUID*) calloc(1, sizeof(GUID));
- pCLSIDFromString(guid_string_w, if_guid);
- guid[nb_guids++] = if_guid;
- usbi_dbg("extra GUID: %s", guid_to_string(if_guid));
- }
- }
- break;
- default:
- // Get the API type (after checking that the driver installation is OK)
- if ( (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_INSTALL_STATE,
- ®_type, (BYTE*)&install_state, 4, &size))
- || (size != 4) ){
- usbi_warn(ctx, "could not detect installation state of driver for '%s': %s",
- dev_id_path, windows_error_str(0));
- } else if (install_state != 0) {
- usbi_warn(ctx, "driver for device '%s' is reporting an issue (code: %d) - skipping",
- dev_id_path, install_state);
- continue;
- }
- api = get_api_type(ctx, &dev_info, &dev_info_data);
- break;
- }
- // Find parent device (for the passes that need it)
- switch (pass) {
- case HCD_PASS:
- case DEV_PASS:
- case HUB_PASS:
- break;
- default:
- // Go through the ancestors until we see a face we recognize
- parent_dev = NULL;
- for (ancestor = 1; parent_dev == NULL; ancestor++) {
- session_id = get_ancestor_session_id(dev_info_data.DevInst, ancestor);
- if (session_id == 0) {
- break;
- }
- parent_dev = usbi_get_device_by_session_id(ctx, session_id);
- }
- if (parent_dev == NULL) {
- usbi_dbg("unlisted ancestor for '%s' (newly connected, etc.) - ignoring", dev_id_path);
- continue;
- }
- parent_priv = _device_priv(parent_dev);
- // virtual USB devices are also listed during GEN - don't process these yet
- if ( (pass == GEN_PASS) && (parent_priv->apib->id != USB_API_HUB) ) {
- continue;
- }
- break;
- }
- // Create new or match existing device, using the (hashed) device_id as session id
- if (pass <= DEV_PASS) { // For subsequent passes, we'll lookup the parent
- // These are the passes that create "new" devices
- session_id = htab_hash(dev_id_path);
- dev = usbi_get_device_by_session_id(ctx, session_id);
- if (dev == NULL) {
- if (pass == DEV_PASS) {
- // This can occur if the OS only reports a newly plugged device after we started enum
- usbi_warn(ctx, "'%s' was only detected in late pass (newly connected device?)"
- " - ignoring", dev_id_path);
- continue;
- }
- usbi_dbg("allocating new device for session [%X]", session_id);
- if ((dev = usbi_alloc_device(ctx, session_id)) == NULL) {
- LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
- }
- windows_device_priv_init(dev);
- // Keep track of devices that need unref
- unref_list[unref_cur++] = dev;
- if (unref_cur >= unref_size) {
- unref_size += 64;
- unref_list = realloc(unref_list, unref_size*sizeof(libusb_device*));
- if (unref_list == NULL) {
- usbi_err(ctx, "could not realloc list for unref - aborting.");
- LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
- }
- }
- } else {
- usbi_dbg("found existing device for session [%X] (%d.%d)",
- session_id, dev->bus_number, dev->device_address);
- }
- priv = _device_priv(dev);
- }
- // Setup device
- switch (pass) {
- case HCD_PASS:
- dev->bus_number = (uint8_t)(i + 1); // bus 0 is reserved for disconnected
- dev->device_address = 0;
- dev->num_configurations = 0;
- priv->apib = &usb_api_backend[USB_API_HUB];
- priv->depth = UINT8_MAX; // Overflow to 0 for HCD Hubs
- priv->path = dev_interface_path; dev_interface_path = NULL;
- break;
- case HUB_PASS:
- case DEV_PASS:
- // If the device has already been setup, don't do it again
- if (priv->path != NULL)
- break;
- // Take care of API initialization
- priv->path = dev_interface_path; dev_interface_path = NULL;
- priv->apib = &usb_api_backend[api];
- switch(api) {
- case USB_API_COMPOSITE:
- case USB_API_HUB:
- break;
- default:
- // For other devices, the first interface is the same as the device
- priv->usb_interface[0].path = (char*) calloc(safe_strlen(priv->path)+1, 1);
- if (priv->usb_interface[0].path != NULL) {
- safe_strcpy(priv->usb_interface[0].path, safe_strlen(priv->path)+1, priv->path);
- } else {
- usbi_warn(ctx, "could not duplicate interface path '%s'", priv->path);
- }
- // The following is needed if we want API calls to work for both simple
- // and composite devices.
- for(j=0; j<USB_MAXINTERFACES; j++) {
- priv->usb_interface[j].apib = &usb_api_backend[api];
- }
- break;
- }
- break;
- case GEN_PASS:
- r = init_device(dev, parent_dev, (uint8_t)port_nr, dev_id_path, dev_info_data.DevInst);
- if (r == LIBUSB_SUCCESS) {
- // Append device to the list of discovered devices
- discdevs = discovered_devs_append(*_discdevs, dev);
- if (!discdevs) {
- LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
- }
- *_discdevs = discdevs;
- } else if (r == LIBUSB_ERROR_NO_DEVICE) {
- // This can occur if the device was disconnected but Windows hasn't
- // refreshed its enumeration yet - in that case, we ignore the device
- r = LIBUSB_SUCCESS;
- }
- break;
- default: // later passes
- if (parent_priv->apib->id == USB_API_COMPOSITE) {
- usbi_dbg("setting composite interface for [%lX]:", parent_dev->session_data);
- switch (set_composite_interface(ctx, parent_dev, dev_interface_path, dev_id_path, api)) {
- case LIBUSB_SUCCESS:
- dev_interface_path = NULL;
- break;
- case LIBUSB_ERROR_ACCESS:
- // interface has already been set => make sure dev_interface_path is freed then
- break;
- default:
- LOOP_BREAK(r);
- break;
- }
- }
- break;
- }
- }
- }
- // Free any additional GUIDs
- for (pass = DEV_PASS+1; pass < nb_guids; pass++) {
- safe_free(guid[pass]);
- }
- // Unref newly allocated devs
- for (i=0; i<unref_cur; i++) {
- safe_unref_device(unref_list[i]);
- }
- safe_free(unref_list);
- return r;
- }
- /*
- * exit: libusb backend deinitialization function
- */
- static void windows_exit(void)
- {
- int i;
- HANDLE semaphore;
- char sem_name[11+1+8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
- sprintf(sem_name, "libusb_init%08X", (unsigned int)GetCurrentProcessId()&0xFFFFFFFF);
- semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name);
- if (semaphore == NULL) {
- return;
- }
- // A successful wait brings our semaphore count to 0 (unsignaled)
- // => any concurent wait stalls until the semaphore release
- if (WaitForSingleObject(semaphore, INFINITE) != WAIT_OBJECT_0) {
- CloseHandle(semaphore);
- return;
- }
- // Only works if exits and inits are balanced exactly
- if (--concurrent_usage < 0) { // Last exit
- for (i=0; i<USB_API_MAX; i++) {
- usb_api_backend[i].exit();
- }
- exit_polling();
- if (timer_thread) {
- SetEvent(timer_request[1]); // actually the signal to quit the thread.
- if (WAIT_OBJECT_0 != WaitForSingleObject(timer_thread, INFINITE)) {
- usbi_dbg("could not wait for timer thread to quit");
- TerminateThread(timer_thread, 1);
- }
- CloseHandle(timer_thread);
- timer_thread = NULL;
- }
- for (i = 0; i < 2; i++) {
- if (timer_request[i]) {
- CloseHandle(timer_request[i]);
- timer_request[i] = NULL;
- }
- }
- if (timer_response) {
- CloseHandle(timer_response);
- timer_response = NULL;
- }
- if (timer_mutex) {
- CloseHandle(timer_mutex);
- timer_mutex = NULL;
- }
- htab_destroy();
- }
- ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
- CloseHandle(semaphore);
- }
- static int windows_get_device_descriptor(struct libusb_device *dev, unsigned char *buffer, int *host_endian)
- {
- struct windows_device_priv *priv = _device_priv(dev);
- memcpy(buffer, &(priv->dev_descriptor), DEVICE_DESC_LENGTH);
- *host_endian = 0;
- return LIBUSB_SUCCESS;
- }
- static int windows_get_config_descriptor(struct libusb_device *dev, uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian)
- {
- struct windows_device_priv *priv = _device_priv(dev);
- PUSB_CONFIGURATION_DESCRIPTOR config_header;
- size_t size;
- // config index is zero based
- if (config_index >= dev->num_configurations)
- return LIBUSB_ERROR_INVALID_PARAM;
- if ((priv->config_descriptor == NULL) || (priv->config_descriptor[config_index] == NULL))
- return LIBUSB_ERROR_NOT_FOUND;
- config_header = (PUSB_CONFIGURATION_DESCRIPTOR)priv->config_descriptor[config_index];
- size = min(config_header->wTotalLength, len);
- memcpy(buffer, priv->config_descriptor[config_index], size);
- return LIBUSB_SUCCESS;
- }
- /*
- * return the cached copy of the active config descriptor
- */
- static int windows_get_active_config_descriptor(struct libusb_device *dev, unsigned char *buffer, size_t len, int *host_endian)
- {
- struct windows_device_priv *priv = _device_priv(dev);
- if (priv->active_config == 0)
- return LIBUSB_ERROR_NOT_FOUND;
- // config index is zero based
- return windows_get_config_descriptor(dev, (uint8_t)(priv->active_config-1), buffer, len, host_endian);
- }
- static int windows_open(struct libusb_device_handle *dev_handle)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- if (priv->apib == NULL) {
- usbi_err(ctx, "program assertion failed - device is not initialized");
- return LIBUSB_ERROR_NO_DEVICE;
- }
- return priv->apib->open(dev_handle);
- }
- static void windows_close(struct libusb_device_handle *dev_handle)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- priv->apib->close(dev_handle);
- }
- static int windows_get_configuration(struct libusb_device_handle *dev_handle, int *config)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- if (priv->active_config == 0) {
- *config = 0;
- return LIBUSB_ERROR_NOT_FOUND;
- }
- *config = priv->active_config;
- return LIBUSB_SUCCESS;
- }
- /*
- * from http://msdn.microsoft.com/en-us/library/ms793522.aspx: "The port driver
- * does not currently expose a service that allows higher-level drivers to set
- * the configuration."
- */
- static int windows_set_configuration(struct libusb_device_handle *dev_handle, int config)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- int r = LIBUSB_SUCCESS;
- if (config >= USB_MAXCONFIG)
- return LIBUSB_ERROR_INVALID_PARAM;
- r = libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_OUT |
- LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE,
- LIBUSB_REQUEST_SET_CONFIGURATION, (uint16_t)config,
- 0, NULL, 0, 1000);
- if (r == LIBUSB_SUCCESS) {
- priv->active_config = (uint8_t)config;
- }
- return r;
- }
- static int windows_claim_interface(struct libusb_device_handle *dev_handle, int iface)
- {
- int r = LIBUSB_SUCCESS;
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- if (iface >= USB_MAXINTERFACES)
- return LIBUSB_ERROR_INVALID_PARAM;
- safe_free(priv->usb_interface[iface].endpoint);
- priv->usb_interface[iface].nb_endpoints= 0;
- r = priv->apib->claim_interface(dev_handle, iface);
- if (r == LIBUSB_SUCCESS) {
- r = windows_assign_endpoints(dev_handle, iface, 0);
- }
- return r;
- }
- static int windows_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting)
- {
- int r = LIBUSB_SUCCESS;
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- safe_free(priv->usb_interface[iface].endpoint);
- priv->usb_interface[iface].nb_endpoints= 0;
- r = priv->apib->set_interface_altsetting(dev_handle, iface, altsetting);
- if (r == LIBUSB_SUCCESS) {
- r = windows_assign_endpoints(dev_handle, iface, altsetting);
- }
- return r;
- }
- static int windows_release_interface(struct libusb_device_handle *dev_handle, int iface)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- return priv->apib->release_interface(dev_handle, iface);
- }
- static int windows_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- return priv->apib->clear_halt(dev_handle, endpoint);
- }
- static int windows_reset_device(struct libusb_device_handle *dev_handle)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- return priv->apib->reset_device(dev_handle);
- }
- // The 3 functions below are unlikely to ever get supported on Windows
- static int windows_kernel_driver_active(struct libusb_device_handle *dev_handle, int iface)
- {
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- static int windows_attach_kernel_driver(struct libusb_device_handle *dev_handle, int iface)
- {
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- static int windows_detach_kernel_driver(struct libusb_device_handle *dev_handle, int iface)
- {
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
- static void windows_destroy_device(struct libusb_device *dev)
- {
- windows_device_priv_release(dev);
- }
- static void windows_clear_transfer_priv(struct usbi_transfer *itransfer)
- {
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
- usbi_free_fd(transfer_priv->pollable_fd.fd);
- // When auto claim is in use, attempt to release the auto-claimed interface
- auto_release(itransfer);
- }
- static int submit_bulk_transfer(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- int r;
- r = priv->apib->submit_bulk_transfer(itransfer);
- if (r != LIBUSB_SUCCESS) {
- return r;
- }
- usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd,
- (short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
- itransfer->flags |= USBI_TRANSFER_UPDATED_FDS;
- return LIBUSB_SUCCESS;
- }
- static int submit_iso_transfer(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- int r;
- r = priv->apib->submit_iso_transfer(itransfer);
- if (r != LIBUSB_SUCCESS) {
- return r;
- }
- usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd,
- (short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
- itransfer->flags |= USBI_TRANSFER_UPDATED_FDS;
- return LIBUSB_SUCCESS;
- }
- static int submit_control_transfer(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- int r;
- r = priv->apib->submit_control_transfer(itransfer);
- if (r != LIBUSB_SUCCESS) {
- return r;
- }
- usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, POLLIN);
- itransfer->flags |= USBI_TRANSFER_UPDATED_FDS;
- return LIBUSB_SUCCESS;
- }
- static int windows_submit_transfer(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- switch (transfer->type) {
- case LIBUSB_TRANSFER_TYPE_CONTROL:
- return submit_control_transfer(itransfer);
- case LIBUSB_TRANSFER_TYPE_BULK:
- case LIBUSB_TRANSFER_TYPE_INTERRUPT:
- return submit_bulk_transfer(itransfer);
- case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
- return submit_iso_transfer(itransfer);
- default:
- usbi_err(TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
- return LIBUSB_ERROR_INVALID_PARAM;
- }
- }
- static int windows_abort_control(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- return priv->apib->abort_control(itransfer);
- }
- static int windows_abort_transfers(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- return priv->apib->abort_transfers(itransfer);
- }
- static int windows_cancel_transfer(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- switch (transfer->type) {
- case LIBUSB_TRANSFER_TYPE_CONTROL:
- return windows_abort_control(itransfer);
- case LIBUSB_TRANSFER_TYPE_BULK:
- case LIBUSB_TRANSFER_TYPE_INTERRUPT:
- case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
- return windows_abort_transfers(itransfer);
- default:
- usbi_err(ITRANSFER_CTX(itransfer), "unknown endpoint type %d", transfer->type);
- return LIBUSB_ERROR_INVALID_PARAM;
- }
- }
- static void windows_transfer_callback(struct usbi_transfer *itransfer, uint32_t io_result, uint32_t io_size)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- int status;
- usbi_dbg("handling I/O completion with errcode %d", io_result);
- switch(io_result) {
- case NO_ERROR:
- status = priv->apib->copy_transfer_data(itransfer, io_size);
- break;
- case ERROR_GEN_FAILURE:
- usbi_dbg("detected endpoint stall");
- status = LIBUSB_TRANSFER_STALL;
- break;
- case ERROR_SEM_TIMEOUT:
- usbi_dbg("detected semaphore timeout");
- status = LIBUSB_TRANSFER_TIMED_OUT;
- break;
- case ERROR_OPERATION_ABORTED:
- if (itransfer->flags & USBI_TRANSFER_TIMED_OUT) {
- usbi_dbg("detected timeout");
- status = LIBUSB_TRANSFER_TIMED_OUT;
- } else {
- usbi_dbg("detected operation aborted");
- status = LIBUSB_TRANSFER_CANCELLED;
- }
- break;
- default:
- usbi_err(ITRANSFER_CTX(itransfer), "detected I/O error: %s", windows_error_str(0));
- status = LIBUSB_TRANSFER_ERROR;
- break;
- }
- windows_clear_transfer_priv(itransfer); // Cancel polling
- usbi_handle_transfer_completion(itransfer, (enum libusb_transfer_status)status);
- }
- static void windows_handle_callback (struct usbi_transfer *itransfer, uint32_t io_result, uint32_t io_size)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- switch (transfer->type) {
- case LIBUSB_TRANSFER_TYPE_CONTROL:
- case LIBUSB_TRANSFER_TYPE_BULK:
- case LIBUSB_TRANSFER_TYPE_INTERRUPT:
- case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
- windows_transfer_callback (itransfer, io_result, io_size);
- break;
- default:
- usbi_err(ITRANSFER_CTX(itransfer), "unknown endpoint type %d", transfer->type);
- }
- }
- static int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready)
- {
- struct windows_transfer_priv* transfer_priv = NULL;
- POLL_NFDS_TYPE i = 0;
- bool found = false;
- struct usbi_transfer *transfer;
- DWORD io_size, io_result;
- usbi_mutex_lock(&ctx->open_devs_lock);
- for (i = 0; i < nfds && num_ready > 0; i++) {
- usbi_dbg("checking fd %d with revents = %04x", fds[i].fd, fds[i].revents);
- if (!fds[i].revents) {
- continue;
- }
- num_ready--;
- // Because a Windows OVERLAPPED is used for poll emulation,
- // a pollable fd is created and stored with each transfer
- usbi_mutex_lock(&ctx->flying_transfers_lock);
- list_for_each_entry(transfer, &ctx->flying_transfers, list, struct usbi_transfer) {
- transfer_priv = usbi_transfer_get_os_priv(transfer);
- if (transfer_priv->pollable_fd.fd == fds[i].fd) {
- found = true;
- break;
- }
- }
- usbi_mutex_unlock(&ctx->flying_transfers_lock);
- if (found) {
- // Handle async requests that completed synchronously first
- if (HasOverlappedIoCompletedSync(transfer_priv->pollable_fd.overlapped)) {
- io_result = NO_ERROR;
- io_size = (DWORD)transfer_priv->pollable_fd.overlapped->InternalHigh;
- // Regular async overlapped
- } else if (GetOverlappedResult(transfer_priv->pollable_fd.handle,
- transfer_priv->pollable_fd.overlapped, &io_size, false)) {
- io_result = NO_ERROR;
- } else {
- io_result = GetLastError();
- }
- usbi_remove_pollfd(ctx, transfer_priv->pollable_fd.fd);
- // let handle_callback free the event using the transfer wfd
- // If you don't use the transfer wfd, you run a risk of trying to free a
- // newly allocated wfd that took the place of the one from the transfer.
- windows_handle_callback(transfer, io_result, io_size);
- } else {
- usbi_err(ctx, "could not find a matching transfer for fd %x", fds[i]);
- return LIBUSB_ERROR_NOT_FOUND;
- }
- }
- usbi_mutex_unlock(&ctx->open_devs_lock);
- return LIBUSB_SUCCESS;
- }
- /*
- * Monotonic and real time functions
- */
- unsigned __stdcall windows_clock_gettime_threaded(void* param)
- {
- LARGE_INTEGER hires_counter, li_frequency;
- LONG nb_responses;
- int timer_index;
- // Init - find out if we have access to a monotonic (hires) timer
- if (!QueryPerformanceFrequency(&li_frequency)) {
- usbi_dbg("no hires timer available on this platform");
- hires_frequency = 0;
- hires_ticks_to_ps = UINT64_C(0);
- } else {
- hires_frequency = li_frequency.QuadPart;
- // The hires frequency can go as high as 4 GHz, so we'll use a conversion
- // to picoseconds to compute the tv_nsecs part in clock_gettime
- hires_ticks_to_ps = UINT64_C(1000000000000) / hires_frequency;
- usbi_dbg("hires timer available (Frequency: %I64u Hz)", hires_frequency);
- }
- // Main loop - wait for requests
- while (1) {
- timer_index = WaitForMultipleObjects(2, timer_request, FALSE, INFINITE) - WAIT_OBJECT_0;
- if ( (timer_index != 0) && (timer_index != 1) ) {
- usbi_dbg("failure to wait on requests: %s", windows_error_str(0));
- continue;
- }
- if (request_count[timer_index] == 0) {
- // Request already handled
- ResetEvent(timer_request[timer_index]);
- // There's still a possiblity that a thread sends a request between the
- // time we test request_count[] == 0 and we reset the event, in which case
- // the request would be ignored. The simple solution to that is to test
- // request_count again and process requests if non zero.
- if (request_count[timer_index] == 0)
- continue;
- }
- switch (timer_index) {
- case 0:
- WaitForSingleObject(timer_mutex, INFINITE);
- // Requests to this thread are for hires always
- if (QueryPerformanceCounter(&hires_counter) != 0) {
- timer_tp.tv_sec = (long)(hires_counter.QuadPart / hires_frequency);
- timer_tp.tv_nsec = (long)(((hires_counter.QuadPart % hires_frequency)/1000) * hires_ticks_to_ps);
- } else {
- // Fallback to real-time if we can't get monotonic value
- // Note that real-time clock does not wait on the mutex or this thread.
- windows_clock_gettime(USBI_CLOCK_REALTIME, &timer_tp);
- }
- ReleaseMutex(timer_mutex);
- nb_responses = InterlockedExchange((LONG*)&request_count[0], 0);
- if ( (nb_responses)
- && (ReleaseSemaphore(timer_response, nb_responses, NULL) == 0) ) {
- usbi_dbg("unable to release timer semaphore %d: %s", windows_error_str(0));
- }
- continue;
- case 1: // time to quit
- usbi_dbg("timer thread quitting");
- return 0;
- }
- }
- usbi_dbg("ERROR: broken timer thread");
- return 1;
- }
- static int windows_clock_gettime(int clk_id, struct timespec *tp)
- {
- FILETIME filetime;
- ULARGE_INTEGER rtime;
- DWORD r;
- switch(clk_id) {
- case USBI_CLOCK_MONOTONIC:
- if (hires_frequency != 0) {
- while (1) {
- InterlockedIncrement((LONG*)&request_count[0]);
- SetEvent(timer_request[0]);
- r = WaitForSingleObject(timer_response, TIMER_REQUEST_RETRY_MS);
- switch(r) {
- case WAIT_OBJECT_0:
- WaitForSingleObject(timer_mutex, INFINITE);
- *tp = timer_tp;
- ReleaseMutex(timer_mutex);
- return LIBUSB_SUCCESS;
- case WAIT_TIMEOUT:
- usbi_dbg("could not obtain a timer value within reasonable timeframe - too much load?");
- break; // Retry until successful
- default:
- usbi_dbg("WaitForSingleObject failed: %s", windows_error_str(0));
- return LIBUSB_ERROR_OTHER;
- }
- }
- }
- // Fall through and return real-time if monotonic was not detected @ timer init
- case USBI_CLOCK_REALTIME:
- // We follow http://msdn.microsoft.com/en-us/library/ms724928%28VS.85%29.aspx
- // with a predef epoch_time to have an epoch that starts at 1970.01.01 00:00
- // Note however that our resolution is bounded by the Windows system time
- // functions and is at best of the order of 1 ms (or, usually, worse)
- GetSystemTimeAsFileTime(&filetime);
- rtime.LowPart = filetime.dwLowDateTime;
- rtime.HighPart = filetime.dwHighDateTime;
- rtime.QuadPart -= epoch_time;
- tp->tv_sec = (long)(rtime.QuadPart / 10000000);
- tp->tv_nsec = (long)((rtime.QuadPart % 10000000)*100);
- return LIBUSB_SUCCESS;
- default:
- return LIBUSB_ERROR_INVALID_PARAM;
- }
- }
- // NB: MSVC6 does not support named initializers.
- const struct usbi_os_backend windows_backend = {
- "Windows",
- windows_init,
- windows_exit,
- windows_get_device_list,
- windows_open,
- windows_close,
- windows_get_device_descriptor,
- windows_get_active_config_descriptor,
- windows_get_config_descriptor,
- windows_get_configuration,
- windows_set_configuration,
- windows_claim_interface,
- windows_release_interface,
- windows_set_interface_altsetting,
- windows_clear_halt,
- windows_reset_device,
- windows_kernel_driver_active,
- windows_detach_kernel_driver,
- windows_attach_kernel_driver,
- windows_destroy_device,
- windows_submit_transfer,
- windows_cancel_transfer,
- windows_clear_transfer_priv,
- windows_handle_events,
- windows_clock_gettime,
- #if defined(USBI_TIMERFD_AVAILABLE)
- NULL,
- #endif
- sizeof(struct windows_device_priv),
- sizeof(struct windows_device_handle_priv),
- sizeof(struct windows_transfer_priv),
- 0,
- };
- /*
- * USB API backends
- */
- static int unsupported_init(struct libusb_context *ctx) {
- return LIBUSB_SUCCESS;
- }
- static int unsupported_exit(void) {
- return LIBUSB_SUCCESS;
- }
- static int unsupported_open(struct libusb_device_handle *dev_handle) {
- PRINT_UNSUPPORTED_API(open);
- }
- static void unsupported_close(struct libusb_device_handle *dev_handle) {
- usbi_dbg("unsupported API call for 'close'");
- }
- static int unsupported_claim_interface(struct libusb_device_handle *dev_handle, int iface) {
- PRINT_UNSUPPORTED_API(claim_interface);
- }
- static int unsupported_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting) {
- PRINT_UNSUPPORTED_API(set_interface_altsetting);
- }
- static int unsupported_release_interface(struct libusb_device_handle *dev_handle, int iface) {
- PRINT_UNSUPPORTED_API(release_interface);
- }
- static int unsupported_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint) {
- PRINT_UNSUPPORTED_API(clear_halt);
- }
- static int unsupported_reset_device(struct libusb_device_handle *dev_handle) {
- PRINT_UNSUPPORTED_API(reset_device);
- }
- static int unsupported_submit_bulk_transfer(struct usbi_transfer *itransfer) {
- PRINT_UNSUPPORTED_API(submit_bulk_transfer);
- }
- static int unsupported_submit_iso_transfer(struct usbi_transfer *itransfer) {
- PRINT_UNSUPPORTED_API(submit_iso_transfer);
- }
- static int unsupported_submit_control_transfer(struct usbi_transfer *itransfer) {
- PRINT_UNSUPPORTED_API(submit_control_transfer);
- }
- static int unsupported_abort_control(struct usbi_transfer *itransfer) {
- PRINT_UNSUPPORTED_API(abort_control);
- }
- static int unsupported_abort_transfers(struct usbi_transfer *itransfer) {
- PRINT_UNSUPPORTED_API(abort_transfers);
- }
- static int unsupported_copy_transfer_data(struct usbi_transfer *itransfer, uint32_t io_size) {
- PRINT_UNSUPPORTED_API(copy_transfer_data);
- }
- // These names must be uppercase
- const char* hub_driver_names[] = {"USBHUB", "USBHUB3", "USB3HUB", "NUSB3HUB", "RUSB3HUB", "FLXHCIH", "TIHUB3", "ETRONHUB3", "VIAHUB3", "ASMTHUB3", "IUSB3HUB", "VUSB3HUB", "AMDHUB30"};
- const char* composite_driver_names[] = {"USBCCGP"};
- const char* winusb_driver_names[] = {"WINUSB"};
- const struct windows_usb_api_backend usb_api_backend[USB_API_MAX] = {
- {
- USB_API_UNSUPPORTED,
- "Unsupported API",
- &CLASS_GUID_UNSUPPORTED,
- NULL,
- 0,
- unsupported_init,
- unsupported_exit,
- unsupported_open,
- unsupported_close,
- unsupported_claim_interface,
- unsupported_set_interface_altsetting,
- unsupported_release_interface,
- unsupported_clear_halt,
- unsupported_reset_device,
- unsupported_submit_bulk_transfer,
- unsupported_submit_iso_transfer,
- unsupported_submit_control_transfer,
- unsupported_abort_control,
- unsupported_abort_transfers,
- unsupported_copy_transfer_data,
- }, {
- USB_API_HUB,
- "HUB API",
- &CLASS_GUID_UNSUPPORTED,
- hub_driver_names,
- sizeof(hub_driver_names)/sizeof(hub_driver_names[0]),
- unsupported_init,
- unsupported_exit,
- unsupported_open,
- unsupported_close,
- unsupported_claim_interface,
- unsupported_set_interface_altsetting,
- unsupported_release_interface,
- unsupported_clear_halt,
- unsupported_reset_device,
- unsupported_submit_bulk_transfer,
- unsupported_submit_iso_transfer,
- unsupported_submit_control_transfer,
- unsupported_abort_control,
- unsupported_abort_transfers,
- unsupported_copy_transfer_data,
- }, {
- USB_API_COMPOSITE,
- "Composite API",
- &CLASS_GUID_COMPOSITE,
- composite_driver_names,
- sizeof(composite_driver_names)/sizeof(composite_driver_names[0]),
- composite_init,
- composite_exit,
- composite_open,
- composite_close,
- composite_claim_interface,
- composite_set_interface_altsetting,
- composite_release_interface,
- composite_clear_halt,
- composite_reset_device,
- composite_submit_bulk_transfer,
- composite_submit_iso_transfer,
- composite_submit_control_transfer,
- composite_abort_control,
- composite_abort_transfers,
- composite_copy_transfer_data,
- }, {
- USB_API_WINUSB,
- "WinUSB API",
- &CLASS_GUID_LIBUSB_WINUSB,
- winusb_driver_names,
- sizeof(winusb_driver_names)/sizeof(winusb_driver_names[0]),
- winusb_init,
- winusb_exit,
- winusb_open,
- winusb_close,
- winusb_claim_interface,
- winusb_set_interface_altsetting,
- winusb_release_interface,
- winusb_clear_halt,
- winusb_reset_device,
- winusb_submit_bulk_transfer,
- unsupported_submit_iso_transfer,
- winusb_submit_control_transfer,
- winusb_abort_control,
- winusb_abort_transfers,
- winusb_copy_transfer_data,
- },
- };
- /*
- * WinUSB API functions
- */
- static int winusb_init(struct libusb_context *ctx)
- {
- DLL_LOAD(winusb.dll, WinUsb_Initialize, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_Free, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_GetAssociatedInterface, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_GetDescriptor, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_QueryInterfaceSettings, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_QueryDeviceInformation, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_SetCurrentAlternateSetting, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_GetCurrentAlternateSetting, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_QueryPipe, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_SetPipePolicy, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_GetPipePolicy, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_ReadPipe, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_WritePipe, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_ControlTransfer, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_ResetPipe, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_AbortPipe, TRUE);
- DLL_LOAD(winusb.dll, WinUsb_FlushPipe, TRUE);
- api_winusb_available = true;
- return LIBUSB_SUCCESS;
- }
- static int winusb_exit(void)
- {
- return LIBUSB_SUCCESS;
- }
- // NB: open and close must ensure that they only handle interface of
- // the right API type, as these functions can be called wholesale from
- // composite_open(), with interfaces belonging to different APIs
- static int winusb_open(struct libusb_device_handle *dev_handle)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- HANDLE file_handle;
- int i;
- CHECK_WINUSB_AVAILABLE;
- // WinUSB requires a seperate handle for each interface
- for (i = 0; i < USB_MAXINTERFACES; i++) {
- if ( (priv->usb_interface[i].path != NULL)
- && (priv->usb_interface[i].apib->id == USB_API_WINUSB) ) {
- file_handle = CreateFileA(priv->usb_interface[i].path, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ,
- NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
- if (file_handle == INVALID_HANDLE_VALUE) {
- usbi_err(ctx, "could not open device %s (interface %d): %s", priv->usb_interface[i].path, i, windows_error_str(0));
- switch(GetLastError()) {
- case ERROR_FILE_NOT_FOUND: // The device was disconnected
- return LIBUSB_ERROR_NO_DEVICE;
- case ERROR_ACCESS_DENIED:
- return LIBUSB_ERROR_ACCESS;
- default:
- return LIBUSB_ERROR_IO;
- }
- }
- handle_priv->interface_handle[i].dev_handle = file_handle;
- }
- }
- return LIBUSB_SUCCESS;
- }
- static void winusb_close(struct libusb_device_handle *dev_handle)
- {
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- HANDLE file_handle;
- int i;
- if (!api_winusb_available)
- return;
- for (i = 0; i < USB_MAXINTERFACES; i++) {
- if (priv->usb_interface[i].apib->id == USB_API_WINUSB) {
- file_handle = handle_priv->interface_handle[i].dev_handle;
- if ( (file_handle != 0) && (file_handle != INVALID_HANDLE_VALUE)) {
- CloseHandle(file_handle);
- }
- }
- }
- }
- static int winusb_configure_endpoints(struct libusb_device_handle *dev_handle, int iface)
- {
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- HANDLE winusb_handle = handle_priv->interface_handle[iface].api_handle;
- UCHAR policy;
- ULONG timeout = 0;
- uint8_t endpoint_address;
- int i;
- CHECK_WINUSB_AVAILABLE;
- // With handle and enpoints set (in parent), we can setup the default pipe properties
- // see http://download.microsoft.com/download/D/1/D/D1DD7745-426B-4CC3-A269-ABBBE427C0EF/DVC-T705_DDC08.pptx
- for (i=-1; i<priv->usb_interface[iface].nb_endpoints; i++) {
- endpoint_address =(i==-1)?0:priv->usb_interface[iface].endpoint[i];
- if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
- PIPE_TRANSFER_TIMEOUT, sizeof(ULONG), &timeout)) {
- usbi_dbg("failed to set PIPE_TRANSFER_TIMEOUT for control endpoint %02X", endpoint_address);
- }
- if (i == -1)
- continue; // Other policies don't apply to control endpoint
- policy = true;
- if (!WinUsb_SetPipePolicy(winusb_handle, endpoint_address,
- AUTO_CLEAR_STALL, sizeof(UCHAR), &policy)) {
- usbi_dbg("failed to enable AUTO_CLEAR_STALL for endpoint %02X", endpoint_address);
- }
- }
- return LIBUSB_SUCCESS;
- }
- static int winusb_claim_interface(struct libusb_device_handle *dev_handle, int iface)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- bool is_using_usbccgp = (priv->apib->id == USB_API_COMPOSITE);
- HANDLE file_handle, winusb_handle;
- CHECK_WINUSB_AVAILABLE;
- // If the device is composite, but using the default Windows composite parent driver (usbccgp)
- // or if it's the first WinUSB interface, we get a handle through WinUsb_Initialize().
- if ((is_using_usbccgp) || (iface == 0)) {
- // composite device (independent interfaces) or interface 0
- file_handle = handle_priv->interface_handle[iface].dev_handle;
- if ((file_handle == 0) || (file_handle == INVALID_HANDLE_VALUE)) {
- return LIBUSB_ERROR_NOT_FOUND;
- }
- if (!WinUsb_Initialize(file_handle, &winusb_handle)) {
- usbi_err(ctx, "could not access interface %d: %s", iface, windows_error_str(0));
- handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE;
- switch(GetLastError()) {
- case ERROR_BAD_COMMAND: // The device was disconnected
- return LIBUSB_ERROR_NO_DEVICE;
- default:
- usbi_err(ctx, "could not claim interface %d: %s", iface, windows_error_str(0));
- return LIBUSB_ERROR_ACCESS;
- }
- }
- handle_priv->interface_handle[iface].api_handle = winusb_handle;
- } else {
- // For all other interfaces, use WinUsb_GetAssociatedInterface()
- winusb_handle = handle_priv->interface_handle[0].api_handle;
- // It is a requirement for multiple interface devices using WinUSB that you
- // must first claim the first interface before you claim any other
- if ((winusb_handle == 0) || (winusb_handle == INVALID_HANDLE_VALUE)) {
- file_handle = handle_priv->interface_handle[0].dev_handle;
- if (WinUsb_Initialize(file_handle, &winusb_handle)) {
- handle_priv->interface_handle[0].api_handle = winusb_handle;
- usbi_warn(ctx, "auto-claimed interface 0 (required to claim %d with WinUSB)", iface);
- } else {
- usbi_warn(ctx, "failed to auto-claim interface 0 (required to claim %d with WinUSB)", iface);
- return LIBUSB_ERROR_ACCESS;
- }
- }
- if (!WinUsb_GetAssociatedInterface(winusb_handle, (UCHAR)(iface-1),
- &handle_priv->interface_handle[iface].api_handle)) {
- handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE;
- switch(GetLastError()) {
- case ERROR_NO_MORE_ITEMS: // invalid iface
- return LIBUSB_ERROR_NOT_FOUND;
- case ERROR_BAD_COMMAND: // The device was disconnected
- return LIBUSB_ERROR_NO_DEVICE;
- case ERROR_ALREADY_EXISTS: // already claimed
- return LIBUSB_ERROR_BUSY;
- default:
- usbi_err(ctx, "could not claim interface %d: %s", iface, windows_error_str(0));
- return LIBUSB_ERROR_ACCESS;
- }
- }
- }
- usbi_dbg("claimed interface %d", iface);
- handle_priv->active_interface = iface;
- return LIBUSB_SUCCESS;
- }
- static int winusb_release_interface(struct libusb_device_handle *dev_handle, int iface)
- {
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- HANDLE winusb_handle;
- CHECK_WINUSB_AVAILABLE;
- winusb_handle = handle_priv->interface_handle[iface].api_handle;
- if ((winusb_handle == 0) || (winusb_handle == INVALID_HANDLE_VALUE)) {
- return LIBUSB_ERROR_NOT_FOUND;
- }
- WinUsb_Free(winusb_handle);
- handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE;
- return LIBUSB_SUCCESS;
- }
- /*
- * Return the first valid interface (of the same API type), for control transfers
- */
- static int winusb_get_valid_interface(struct libusb_device_handle *dev_handle)
- {
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- int i;
- for (i=0; i<USB_MAXINTERFACES; i++) {
- if ( (handle_priv->interface_handle[i].dev_handle != 0)
- && (handle_priv->interface_handle[i].dev_handle != INVALID_HANDLE_VALUE)
- && (handle_priv->interface_handle[i].api_handle != 0)
- && (handle_priv->interface_handle[i].api_handle != INVALID_HANDLE_VALUE) ) {
- return i;
- }
- }
- return -1;
- }
- /*
- * Lookup interface by endpoint address. -1 if not found
- */
- static int interface_by_endpoint(struct windows_device_priv *priv,
- struct windows_device_handle_priv *handle_priv, uint8_t endpoint_address)
- {
- int i, j;
- for (i=0; i<USB_MAXINTERFACES; i++) {
- if (handle_priv->interface_handle[i].api_handle == INVALID_HANDLE_VALUE)
- continue;
- if (handle_priv->interface_handle[i].api_handle == 0)
- continue;
- if (priv->usb_interface[i].endpoint == NULL)
- continue;
- for (j=0; j<priv->usb_interface[i].nb_endpoints; j++) {
- if (priv->usb_interface[i].endpoint[j] == endpoint_address) {
- return i;
- }
- }
- }
- return -1;
- }
- static int winusb_submit_control_transfer(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(
- transfer->dev_handle);
- WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *) transfer->buffer;
- ULONG size;
- HANDLE winusb_handle;
- int current_interface;
- struct winfd wfd;
- CHECK_WINUSB_AVAILABLE;
- transfer_priv->pollable_fd = INVALID_WINFD;
- size = transfer->length - LIBUSB_CONTROL_SETUP_SIZE;
- if (size > MAX_CTRL_BUFFER_LENGTH)
- return LIBUSB_ERROR_INVALID_PARAM;
- current_interface = winusb_get_valid_interface(transfer->dev_handle);
- if (current_interface < 0) {
- if (auto_claim(transfer, ¤t_interface, USB_API_WINUSB) != LIBUSB_SUCCESS) {
- return LIBUSB_ERROR_NOT_FOUND;
- }
- }
- usbi_dbg("will use interface %d", current_interface);
- winusb_handle = handle_priv->interface_handle[current_interface].api_handle;
- wfd = usbi_create_fd(winusb_handle, _O_RDONLY);
- // Always use the handle returned from usbi_create_fd (wfd.handle)
- if (wfd.fd < 0) {
- return LIBUSB_ERROR_NO_MEM;
- }
- // Sending of set configuration control requests from WinUSB creates issues
- if ( ((setup->request_type & (0x03 << 5)) == LIBUSB_REQUEST_TYPE_STANDARD)
- && (setup->request == LIBUSB_REQUEST_SET_CONFIGURATION) ) {
- if (setup->value != priv->active_config) {
- usbi_warn(ctx, "cannot set configuration other than the default one");
- usbi_free_fd(wfd.fd);
- return LIBUSB_ERROR_INVALID_PARAM;
- }
- wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
- wfd.overlapped->InternalHigh = 0;
- } else {
- if (!WinUsb_ControlTransfer(wfd.handle, *setup, transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE, size, NULL, wfd.overlapped)) {
- if(GetLastError() != ERROR_IO_PENDING) {
- usbi_err(ctx, "WinUsb_ControlTransfer failed: %s", windows_error_str(0));
- usbi_free_fd(wfd.fd);
- return LIBUSB_ERROR_IO;
- }
- } else {
- wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
- wfd.overlapped->InternalHigh = (DWORD)size;
- }
- }
- // Use priv_transfer to store data needed for async polling
- transfer_priv->pollable_fd = wfd;
- transfer_priv->interface_number = (uint8_t)current_interface;
- return LIBUSB_SUCCESS;
- }
- static int winusb_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- HANDLE winusb_handle;
- CHECK_WINUSB_AVAILABLE;
- if (altsetting > 255) {
- return LIBUSB_ERROR_INVALID_PARAM;
- }
- winusb_handle = handle_priv->interface_handle[iface].api_handle;
- if ((winusb_handle == 0) || (winusb_handle == INVALID_HANDLE_VALUE)) {
- usbi_err(ctx, "interface must be claimed first");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- if (!WinUsb_SetCurrentAlternateSetting(winusb_handle, (UCHAR)altsetting)) {
- usbi_err(ctx, "WinUsb_SetCurrentAlternateSetting failed: %s", windows_error_str(0));
- return LIBUSB_ERROR_IO;
- }
- return LIBUSB_SUCCESS;
- }
- static int winusb_submit_bulk_transfer(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- HANDLE winusb_handle;
- bool ret;
- int current_interface;
- struct winfd wfd;
- ULONG ppolicy = sizeof(UCHAR);
- UCHAR policy;
- CHECK_WINUSB_AVAILABLE;
- transfer_priv->pollable_fd = INVALID_WINFD;
- current_interface = interface_by_endpoint(priv, handle_priv, transfer->endpoint);
- if (current_interface < 0) {
- usbi_err(ctx, "unable to match endpoint to an open interface - cancelling transfer");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- usbi_dbg("matched endpoint %02X with interface %d", transfer->endpoint, current_interface);
- winusb_handle = handle_priv->interface_handle[current_interface].api_handle;
- wfd = usbi_create_fd(winusb_handle, IS_XFERIN(transfer) ? _O_RDONLY : _O_WRONLY);
- // Always use the handle returned from usbi_create_fd (wfd.handle)
- if (wfd.fd < 0) {
- return LIBUSB_ERROR_NO_MEM;
- }
- if (IS_XFERIN(transfer)) {
- WinUsb_GetPipePolicy(wfd.handle, transfer->endpoint, AUTO_CLEAR_STALL, &ppolicy, &policy);
- if (!policy) {
- policy = TRUE;
- WinUsb_SetPipePolicy(wfd.handle, transfer->endpoint, AUTO_CLEAR_STALL, ppolicy, &policy);
- }
- ret = WinUsb_ReadPipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
- } else {
- if (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
- WinUsb_GetPipePolicy(wfd.handle, transfer->endpoint, SHORT_PACKET_TERMINATE, &ppolicy, &policy);
- if (!policy) {
- policy = TRUE;
- WinUsb_SetPipePolicy(wfd.handle, transfer->endpoint, SHORT_PACKET_TERMINATE, ppolicy, &policy);
- }
- }
- usbi_dbg("writing %d bytes", transfer->length);
- ret = WinUsb_WritePipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
- }
- if (!ret) {
- if(GetLastError() != ERROR_IO_PENDING) {
- usbi_err(ctx, "WinUsb_Pipe Transfer failed: %s", windows_error_str(0));
- usbi_free_fd(wfd.fd);
- return LIBUSB_ERROR_IO;
- }
- } else {
- wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
- wfd.overlapped->InternalHigh = (DWORD)transfer->length;
- }
- transfer_priv->pollable_fd = wfd;
- transfer_priv->interface_number = (uint8_t)current_interface;
- return LIBUSB_SUCCESS;
- }
- static int winusb_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- HANDLE winusb_handle;
- int current_interface;
- CHECK_WINUSB_AVAILABLE;
- current_interface = interface_by_endpoint(priv, handle_priv, endpoint);
- if (current_interface < 0) {
- usbi_err(ctx, "unable to match endpoint to an open interface - cannot clear");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- usbi_dbg("matched endpoint %02X with interface %d", endpoint, current_interface);
- winusb_handle = handle_priv->interface_handle[current_interface].api_handle;
- if (!WinUsb_ResetPipe(winusb_handle, endpoint)) {
- usbi_err(ctx, "WinUsb_ResetPipe failed: %s", windows_error_str(0));
- return LIBUSB_ERROR_NO_DEVICE;
- }
- return LIBUSB_SUCCESS;
- }
- /*
- * from http://www.winvistatips.com/winusb-bugchecks-t335323.html (confirmed
- * through testing as well):
- * "You can not call WinUsb_AbortPipe on control pipe. You can possibly cancel
- * the control transfer using CancelIo"
- */
- static int winusb_abort_control(struct usbi_transfer *itransfer)
- {
- // Cancelling of the I/O is done in the parent
- return LIBUSB_SUCCESS;
- }
- static int winusb_abort_transfers(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
- struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
- HANDLE winusb_handle;
- int current_interface;
- CHECK_WINUSB_AVAILABLE;
- current_interface = transfer_priv->interface_number;
- if ((current_interface < 0) || (current_interface >= USB_MAXINTERFACES)) {
- usbi_err(ctx, "program assertion failed: invalid interface_number");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- usbi_dbg("will use interface %d", current_interface);
- winusb_handle = handle_priv->interface_handle[current_interface].api_handle;
- if (!WinUsb_AbortPipe(winusb_handle, transfer->endpoint)) {
- usbi_err(ctx, "WinUsb_AbortPipe failed: %s", windows_error_str(0));
- return LIBUSB_ERROR_NO_DEVICE;
- }
- return LIBUSB_SUCCESS;
- }
- /*
- * from the "How to Use WinUSB to Communicate with a USB Device" Microsoft white paper
- * (http://www.microsoft.com/whdc/connect/usb/winusb_howto.mspx):
- * "WinUSB does not support host-initiated reset port and cycle port operations" and
- * IOCTL_INTERNAL_USB_CYCLE_PORT is only available in kernel mode and the
- * IOCTL_USB_HUB_CYCLE_PORT ioctl was removed from Vista => the best we can do is
- * cycle the pipes (and even then, the control pipe can not be reset using WinUSB)
- */
- // TODO (post hotplug): see if we can force eject the device and redetect it (reuse hotplug?)
- static int winusb_reset_device(struct libusb_device_handle *dev_handle)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- struct winfd wfd;
- HANDLE winusb_handle;
- int i, j;
- CHECK_WINUSB_AVAILABLE;
- // Reset any available pipe (except control)
- for (i=0; i<USB_MAXINTERFACES; i++) {
- winusb_handle = handle_priv->interface_handle[i].api_handle;
- for (wfd = handle_to_winfd(winusb_handle); wfd.fd > 0;)
- {
- // Cancel any pollable I/O
- usbi_remove_pollfd(ctx, wfd.fd);
- usbi_free_fd(wfd.fd);
- wfd = handle_to_winfd(winusb_handle);
- }
- if ( (winusb_handle != 0) && (winusb_handle != INVALID_HANDLE_VALUE)) {
- for (j=0; j<priv->usb_interface[i].nb_endpoints; j++) {
- usbi_dbg("resetting ep %02X", priv->usb_interface[i].endpoint[j]);
- if (!WinUsb_AbortPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) {
- usbi_err(ctx, "WinUsb_AbortPipe (pipe address %02X) failed: %s",
- priv->usb_interface[i].endpoint[j], windows_error_str(0));
- }
- // FlushPipe seems to fail on OUT pipes
- if (IS_EPIN(priv->usb_interface[i].endpoint[j])
- && (!WinUsb_FlushPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) ) {
- usbi_err(ctx, "WinUsb_FlushPipe (pipe address %02X) failed: %s",
- priv->usb_interface[i].endpoint[j], windows_error_str(0));
- }
- if (!WinUsb_ResetPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) {
- usbi_err(ctx, "WinUsb_ResetPipe (pipe address %02X) failed: %s",
- priv->usb_interface[i].endpoint[j], windows_error_str(0));
- }
- }
- }
- }
- return LIBUSB_SUCCESS;
- }
- static int winusb_copy_transfer_data(struct usbi_transfer *itransfer, uint32_t io_size)
- {
- itransfer->transferred += io_size;
- return LIBUSB_TRANSFER_COMPLETED;
- }
- /*
- * Composite API functions
- */
- static int composite_init(struct libusb_context *ctx)
- {
- return LIBUSB_SUCCESS;
- }
- static int composite_exit(void)
- {
- return LIBUSB_SUCCESS;
- }
- static int composite_open(struct libusb_device_handle *dev_handle)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- unsigned api;
- int r;
- uint8_t flag = 1<<USB_API_WINUSB;
- for (api=USB_API_WINUSB; api<USB_API_MAX; api++) {
- if (priv->composite_api_flags & flag) {
- r = usb_api_backend[api].open(dev_handle);
- if (r != LIBUSB_SUCCESS) {
- return r;
- }
- }
- flag <<= 1;
- }
- return LIBUSB_SUCCESS;
- }
- static void composite_close(struct libusb_device_handle *dev_handle)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- unsigned api;
- uint8_t flag = 1<<USB_API_WINUSB;
- for (api=USB_API_WINUSB; api<USB_API_MAX; api++) {
- if (priv->composite_api_flags & flag) {
- usb_api_backend[api].close(dev_handle);
- }
- flag <<= 1;
- }
- }
- static int composite_claim_interface(struct libusb_device_handle *dev_handle, int iface)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- return priv->usb_interface[iface].apib->claim_interface(dev_handle, iface);
- }
- static int composite_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- return priv->usb_interface[iface].apib->set_interface_altsetting(dev_handle, iface, altsetting);
- }
- static int composite_release_interface(struct libusb_device_handle *dev_handle, int iface)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- return priv->usb_interface[iface].apib->release_interface(dev_handle, iface);
- }
- static int composite_submit_control_transfer(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- int i;
- for (i=0; i<USB_MAXINTERFACES; i++) {
- if (priv->usb_interface[i].path != NULL) {
- usbi_dbg("using interface %d", i);
- return priv->usb_interface[i].apib->submit_control_transfer(itransfer);
- }
- }
- usbi_err(ctx, "no libusb supported interfaces to complete request");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- static int composite_submit_bulk_transfer(struct usbi_transfer *itransfer) {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- int current_interface;
- current_interface = interface_by_endpoint(priv, handle_priv, transfer->endpoint);
- if (current_interface < 0) {
- usbi_err(ctx, "unable to match endpoint to an open interface - cancelling transfer");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- return priv->usb_interface[current_interface].apib->submit_bulk_transfer(itransfer);
- }
- static int composite_submit_iso_transfer(struct usbi_transfer *itransfer) {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- int current_interface;
- current_interface = interface_by_endpoint(priv, handle_priv, transfer->endpoint);
- if (current_interface < 0) {
- usbi_err(ctx, "unable to match endpoint to an open interface - cancelling transfer");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- return priv->usb_interface[current_interface].apib->submit_iso_transfer(itransfer);
- }
- static int composite_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint)
- {
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- int current_interface;
- current_interface = interface_by_endpoint(priv, handle_priv, endpoint);
- if (current_interface < 0) {
- usbi_err(ctx, "unable to match endpoint to an open interface - cannot clear");
- return LIBUSB_ERROR_NOT_FOUND;
- }
- return priv->usb_interface[current_interface].apib->clear_halt(dev_handle, endpoint);
- }
- static int composite_abort_control(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- return priv->usb_interface[transfer_priv->interface_number].apib->abort_control(itransfer);
- }
- static int composite_abort_transfers(struct usbi_transfer *itransfer)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- return priv->usb_interface[transfer_priv->interface_number].apib->abort_transfers(itransfer);
- }
- static int composite_reset_device(struct libusb_device_handle *dev_handle)
- {
- struct windows_device_priv *priv = _device_priv(dev_handle->dev);
- unsigned api;
- int r;
- uint8_t flag = 1<<USB_API_WINUSB;
- for (api=USB_API_WINUSB; api<USB_API_MAX; api++) {
- if (priv->composite_api_flags & flag) {
- r = usb_api_backend[api].reset_device(dev_handle);
- if (r != LIBUSB_SUCCESS) {
- return r;
- }
- }
- flag <<= 1;
- }
- return LIBUSB_SUCCESS;
- }
- static int composite_copy_transfer_data(struct usbi_transfer *itransfer, uint32_t io_size)
- {
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
- struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- return priv->usb_interface[transfer_priv->interface_number].apib->copy_transfer_data(itransfer, io_size);
- }
|