usbutils.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. * Copyright 2013-2015 Con Kolivas <kernel@kolivas.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <ctype.h>
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. #include "logging.h"
  15. #include "miner.h"
  16. #include "usbutils.h"
  17. static pthread_mutex_t cgusb_lock;
  18. static pthread_mutex_t cgusbres_lock;
  19. static cglock_t cgusb_fd_lock;
  20. static cgtimer_t usb11_cgt;
  21. #define NODEV(err) ((err) != LIBUSB_SUCCESS && (err) != LIBUSB_ERROR_TIMEOUT)
  22. #define NOCONTROLDEV(err) ((err) < 0 && NODEV(err))
  23. /*
  24. * WARNING - these assume DEVLOCK(cgpu, pstate) is called first and
  25. * DEVUNLOCK(cgpu, pstate) in called in the same function with the same pstate
  26. * given to DEVLOCK.
  27. * You must call DEVUNLOCK(cgpu, pstate) before exiting the function or it will leave
  28. * the thread Cancelability unrestored
  29. */
  30. #define DEVWLOCK(cgpu, _pth_state) do { \
  31. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_pth_state); \
  32. cg_wlock(&cgpu->usbinfo.devlock); \
  33. } while (0)
  34. #define DEVWUNLOCK(cgpu, _pth_state) do { \
  35. cg_wunlock(&cgpu->usbinfo.devlock); \
  36. pthread_setcancelstate(_pth_state, NULL); \
  37. } while (0)
  38. #define DEVRLOCK(cgpu, _pth_state) do { \
  39. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_pth_state); \
  40. cg_rlock(&cgpu->usbinfo.devlock); \
  41. } while (0)
  42. #define DEVRUNLOCK(cgpu, _pth_state) do { \
  43. cg_runlock(&cgpu->usbinfo.devlock); \
  44. pthread_setcancelstate(_pth_state, NULL); \
  45. } while (0)
  46. #define USB_CONFIG 1
  47. #define BITFURY_TIMEOUT_MS 999
  48. #define DRILLBIT_TIMEOUT_MS 999
  49. #define ICARUS_TIMEOUT_MS 999
  50. // There is no windows version
  51. #define ANT_S1_TIMEOUT_MS 200
  52. #define ANT_S3_TIMEOUT_MS 200
  53. #ifdef WIN32
  54. #define BFLSC_TIMEOUT_MS 999
  55. #define BITFORCE_TIMEOUT_MS 999
  56. #define MODMINER_TIMEOUT_MS 999
  57. #define AVALON_TIMEOUT_MS 999
  58. #define AVALON4_TIMEOUT_MS 999
  59. #define AVALON7_TIMEOUT_MS 999
  60. #define AVALON8_TIMEOUT_MS 999
  61. #define AVALON9_TIMEOUT_MS 999
  62. #define AVALONLC3_TIMEOUT_MS 999
  63. #define AVALONM_TIMEOUT_MS 999
  64. #define KLONDIKE_TIMEOUT_MS 999
  65. #define COINTERRA_TIMEOUT_MS 999
  66. #define HASHFAST_TIMEOUT_MS 999
  67. #define HASHRATIO_TIMEOUT_MS 999
  68. #define BLOCKERUPTER_TIMEOUT_MS 999
  69. #define COMPAC_TIMEOUT_MS 999
  70. /* The safety timeout we use, cancelling async transfers on windows that fail
  71. * to timeout on their own. */
  72. #define WIN_CALLBACK_EXTRA 40
  73. #define WIN_WRITE_CBEXTRA 5000
  74. #else
  75. #define BFLSC_TIMEOUT_MS 300
  76. #define BITFORCE_TIMEOUT_MS 200
  77. #define MODMINER_TIMEOUT_MS 100
  78. #define AVALON_TIMEOUT_MS 200
  79. #define AVALON4_TIMEOUT_MS 200
  80. #define AVALON7_TIMEOUT_MS 200
  81. #define AVALON8_TIMEOUT_MS 200
  82. #define AVALON9_TIMEOUT_MS 200
  83. #define AVALONLC3_TIMEOUT_MS 200
  84. #define AVALONM_TIMEOUT_MS 300
  85. #define KLONDIKE_TIMEOUT_MS 200
  86. #define COINTERRA_TIMEOUT_MS 200
  87. #define HASHFAST_TIMEOUT_MS 500
  88. #define HASHRATIO_TIMEOUT_MS 200
  89. #define BLOCKERUPTER_TIMEOUT_MS 300
  90. #define COMPAC_TIMEOUT_MS 300
  91. #endif
  92. #define USB_EPS(_intx, _epinfosx) { \
  93. .interface = _intx, \
  94. .ctrl_transfer = _intx, \
  95. .epinfo_count = ARRAY_SIZE(_epinfosx), \
  96. .epinfos = _epinfosx \
  97. }
  98. #define USB_EPS_CTRL(_inty, _ctrlinty, _epinfosy) { \
  99. .interface = _inty, \
  100. .ctrl_transfer = _ctrlinty, \
  101. .epinfo_count = ARRAY_SIZE(_epinfosy), \
  102. .epinfos = _epinfosy \
  103. }
  104. /* Linked list of all async transfers in progress. Protected by cgusb_fd_lock.
  105. * This allows us to not stop the usb polling thread till all are complete, and
  106. * to find cancellable transfers. */
  107. static struct list_head ut_list;
  108. #ifdef USE_BFLSC
  109. static struct usb_epinfo bflsc_epinfos[] = {
  110. { LIBUSB_TRANSFER_TYPE_BULK, 512, EPI(1), 0, 0 },
  111. { LIBUSB_TRANSFER_TYPE_BULK, 512, EPO(2), 0, 0 }
  112. };
  113. static struct usb_intinfo bflsc_ints[] = {
  114. USB_EPS(0, bflsc_epinfos)
  115. };
  116. #endif
  117. #ifdef USE_BITFORCE
  118. // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
  119. static struct usb_epinfo bfl_epinfos[] = {
  120. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  121. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  122. };
  123. static struct usb_intinfo bfl_ints[] = {
  124. USB_EPS(0, bfl_epinfos)
  125. };
  126. #endif
  127. #ifdef USE_BITFURY
  128. static struct usb_epinfo bfu0_epinfos[] = {
  129. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(2), 0, 0 }
  130. };
  131. static struct usb_epinfo bfu1_epinfos[] = {
  132. { LIBUSB_TRANSFER_TYPE_BULK, 16, EPI(3), 0, 0 },
  133. { LIBUSB_TRANSFER_TYPE_BULK, 16, EPO(4), 0, 0 }
  134. };
  135. /* Default to interface 1 */
  136. static struct usb_intinfo bfu_ints[] = {
  137. USB_EPS(1, bfu1_epinfos),
  138. USB_EPS(0, bfu0_epinfos)
  139. };
  140. static struct usb_epinfo bxf0_epinfos[] = {
  141. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(1), 0, 0 }
  142. };
  143. static struct usb_epinfo bxf1_epinfos[] = {
  144. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(2), 0, 0 },
  145. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  146. };
  147. static struct usb_intinfo bxf_ints[] = {
  148. USB_EPS(1, bxf1_epinfos),
  149. USB_EPS(0, bxf0_epinfos)
  150. };
  151. static struct usb_epinfo nfu_epinfos[] = {
  152. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 64, EPI(1), 0, 0 },
  153. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 64, EPO(1), 0, 0 },
  154. };
  155. static struct usb_intinfo nfu_ints[] = {
  156. USB_EPS(0, nfu_epinfos)
  157. };
  158. static struct usb_epinfo bxm_epinfos[] = {
  159. { LIBUSB_TRANSFER_TYPE_BULK, 512, EPI(1), 0, 0 },
  160. { LIBUSB_TRANSFER_TYPE_BULK, 512, EPO(2), 0, 0 }
  161. };
  162. static struct usb_intinfo bxm_ints[] = {
  163. USB_EPS(0, bxm_epinfos)
  164. };
  165. #endif
  166. #ifdef USE_BLOCKERUPTER
  167. // BlockErupter Device
  168. static struct usb_epinfo bet_epinfos[] = {
  169. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  170. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  171. };
  172. static struct usb_intinfo bet_ints[] = {
  173. USB_EPS(0, bet_epinfos)
  174. };
  175. #endif
  176. #ifdef USE_GEKKO
  177. // CP210X Devices
  178. static struct usb_epinfo gek1_epinfos[] = {
  179. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  180. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  181. };
  182. static struct usb_intinfo gek1_ints[] = {
  183. USB_EPS(0, gek1_epinfos)
  184. };
  185. // FTDI Devices
  186. static struct usb_epinfo gek2_epinfos[] = {
  187. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  188. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  189. };
  190. static struct usb_intinfo gek2_ints[] = {
  191. USB_EPS(0, gek2_epinfos)
  192. };
  193. #endif
  194. #ifdef USE_DRILLBIT
  195. // Drillbit Bitfury devices
  196. static struct usb_epinfo drillbit_int_epinfos[] = {
  197. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(3), 0, 0 }
  198. };
  199. static struct usb_epinfo drillbit_bulk_epinfos[] = {
  200. { LIBUSB_TRANSFER_TYPE_BULK, 16, EPI(1), 0, 0 },
  201. { LIBUSB_TRANSFER_TYPE_BULK, 16, EPO(2), 0, 0 },
  202. };
  203. /* Default to interface 1 */
  204. static struct usb_intinfo drillbit_ints[] = {
  205. USB_EPS(1, drillbit_bulk_epinfos),
  206. USB_EPS(0, drillbit_int_epinfos)
  207. };
  208. #endif
  209. #ifdef USE_HASHFAST
  210. #include "driver-hashfast.h"
  211. static struct usb_epinfo hfa0_epinfos[] = {
  212. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(3), 0, 0 }
  213. };
  214. static struct usb_epinfo hfa1_epinfos[] = {
  215. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  216. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  217. };
  218. /* Default to interface 1 */
  219. static struct usb_intinfo hfa_ints[] = {
  220. USB_EPS(1, hfa1_epinfos),
  221. USB_EPS(0, hfa0_epinfos)
  222. };
  223. #endif
  224. #ifdef USE_HASHRATIO
  225. static struct usb_epinfo hro_epinfos[] = {
  226. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  227. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  228. };
  229. static struct usb_intinfo hro_ints[] = {
  230. USB_EPS(0, hro_epinfos)
  231. };
  232. #endif
  233. #ifdef USE_MODMINER
  234. static struct usb_epinfo mmq_epinfos[] = {
  235. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
  236. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0, 0 }
  237. };
  238. static struct usb_intinfo mmq_ints[] = {
  239. USB_EPS(1, mmq_epinfos)
  240. };
  241. #endif
  242. #ifdef USE_AVALON
  243. static struct usb_epinfo ava_epinfos[] = {
  244. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  245. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  246. };
  247. static struct usb_intinfo ava_ints[] = {
  248. USB_EPS(0, ava_epinfos)
  249. };
  250. #endif
  251. #ifdef USE_AVALON2
  252. static struct usb_epinfo ava2_epinfos[] = {
  253. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
  254. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  255. };
  256. static struct usb_intinfo ava2_ints[] = {
  257. USB_EPS(0, ava2_epinfos)
  258. };
  259. #endif
  260. #ifdef USE_AVALON4
  261. static struct usb_epinfo ava4_epinfos[] = {
  262. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  263. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  264. };
  265. static struct usb_intinfo ava4_ints[] = {
  266. USB_EPS(1, ava4_epinfos)
  267. };
  268. #endif
  269. #ifdef USE_AVALON7
  270. static struct usb_epinfo ava7_epinfos[] = {
  271. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  272. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  273. };
  274. static struct usb_intinfo ava7_ints[] = {
  275. USB_EPS(1, ava7_epinfos)
  276. };
  277. #endif
  278. #ifdef USE_AVALON8
  279. static struct usb_epinfo ava8_epinfos[] = {
  280. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  281. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  282. };
  283. static struct usb_intinfo ava8_ints[] = {
  284. USB_EPS(1, ava8_epinfos)
  285. };
  286. #endif
  287. #ifdef USE_AVALON9
  288. static struct usb_epinfo ava9_epinfos[] = {
  289. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  290. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  291. };
  292. static struct usb_intinfo ava9_ints[] = {
  293. USB_EPS(1, ava9_epinfos)
  294. };
  295. #endif
  296. #ifdef USE_AVALONLC3
  297. static struct usb_epinfo avalc3_epinfos[] = {
  298. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  299. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  300. };
  301. static struct usb_intinfo avalc3_ints[] = {
  302. USB_EPS(1, avalc3_epinfos)
  303. };
  304. #endif
  305. #ifdef USE_AVALON_MINER
  306. static struct usb_epinfo avam_epinfos[] = {
  307. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 40, EPI(1), 0, 0 },
  308. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 40, EPO(1), 0, 0 }
  309. };
  310. static struct usb_intinfo avam_ints[] = {
  311. USB_EPS(1, avam_epinfos)
  312. };
  313. static struct usb_epinfo av3u_epinfos[] = {
  314. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 40, EPI(1), 0, 0 },
  315. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 40, EPO(1), 0, 0 }
  316. };
  317. static struct usb_intinfo av3u_ints[] = {
  318. USB_EPS(0, av3u_epinfos)
  319. };
  320. #endif
  321. #ifdef USE_KLONDIKE
  322. static struct usb_epinfo kln_epinfos[] = {
  323. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  324. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  325. };
  326. static struct usb_intinfo kln_ints[] = {
  327. USB_EPS(0, kln_epinfos)
  328. };
  329. static struct usb_epinfo kli0_epinfos[] = {
  330. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(1), 0, 0 }
  331. };
  332. static struct usb_epinfo kli1_epinfos[] = {
  333. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(2), 0, 0 },
  334. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  335. };
  336. static struct usb_intinfo kli_ints[] = {
  337. USB_EPS(1, kli1_epinfos),
  338. USB_EPS(0, kli0_epinfos)
  339. };
  340. #endif
  341. #ifdef USE_ICARUS
  342. static struct usb_epinfo ica_epinfos[] = {
  343. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
  344. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  345. };
  346. static struct usb_intinfo ica_ints[] = {
  347. USB_EPS(0, ica_epinfos)
  348. };
  349. static struct usb_epinfo ica1_epinfos0[] = {
  350. { LIBUSB_TRANSFER_TYPE_INTERRUPT, 16, EPI(0x82), 0, 0 }
  351. };
  352. static struct usb_epinfo ica1_epinfos1[] = {
  353. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(0x81), 0, 0 },
  354. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(0x01), 0, 0 }
  355. };
  356. static struct usb_intinfo ica1_ints[] = {
  357. USB_EPS(1, ica1_epinfos1),
  358. USB_EPS(0, ica1_epinfos0)
  359. };
  360. static struct usb_epinfo amu_epinfos[] = {
  361. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  362. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  363. };
  364. static struct usb_intinfo amu_ints[] = {
  365. USB_EPS(0, amu_epinfos)
  366. };
  367. static struct usb_epinfo llt_epinfos[] = {
  368. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  369. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  370. };
  371. static struct usb_intinfo llt_ints[] = {
  372. USB_EPS(0, llt_epinfos)
  373. };
  374. static struct usb_epinfo cmr1_epinfos[] = {
  375. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  376. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  377. };
  378. static struct usb_intinfo cmr1_ints[] = {
  379. USB_EPS(0, cmr1_epinfos)
  380. };
  381. static struct usb_epinfo cmr2_epinfos0[] = {
  382. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  383. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
  384. };
  385. static struct usb_epinfo cmr2_epinfos1[] = {
  386. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
  387. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(4), 0, 0 },
  388. };
  389. static struct usb_epinfo cmr2_epinfos2[] = {
  390. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(5), 0, 0 },
  391. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(6), 0, 0 },
  392. };
  393. static struct usb_epinfo cmr2_epinfos3[] = {
  394. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(7), 0, 0 },
  395. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(8), 0, 0 }
  396. };
  397. static struct usb_intinfo cmr2_ints[] = {
  398. USB_EPS_CTRL(0, 1, cmr2_epinfos0),
  399. USB_EPS_CTRL(1, 2, cmr2_epinfos1),
  400. USB_EPS_CTRL(2, 3, cmr2_epinfos2),
  401. USB_EPS_CTRL(3, 4, cmr2_epinfos3)
  402. };
  403. #endif
  404. #ifdef USE_COINTERRA
  405. static struct usb_epinfo cointerra_epinfos[] = {
  406. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  407. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  408. };
  409. static struct usb_intinfo cointerra_ints[] = {
  410. USB_EPS(0, cointerra_epinfos)
  411. };
  412. #endif
  413. #ifdef USE_ANT_S1
  414. static struct usb_epinfo ants1_epinfos[] = {
  415. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  416. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  417. };
  418. static struct usb_intinfo ants1_ints[] = {
  419. USB_EPS(0, ants1_epinfos)
  420. };
  421. #endif
  422. #ifdef USE_ANT_S3
  423. static struct usb_epinfo ants3_epinfos[] = {
  424. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
  425. { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
  426. };
  427. static struct usb_intinfo ants3_ints[] = {
  428. USB_EPS(0, ants3_epinfos)
  429. };
  430. #endif
  431. #define IDVENDOR_FTDI 0x0403
  432. #define INTINFO(_ints) \
  433. .intinfo_count = ARRAY_SIZE(_ints), \
  434. .intinfos = _ints
  435. #define USBEP(_usbdev, _intinfo, _epinfo) (_usbdev->found->intinfos[_intinfo].epinfos[_epinfo].ep)
  436. #define THISIF(_found, _this) (_found->intinfos[_this].interface)
  437. #define USBIF(_usbdev, _this) THISIF(_usbdev->found, _this)
  438. static struct usb_find_devices find_dev[] = {
  439. #ifdef USE_BFLSC
  440. /* Wish these guys would be more consistent with setting these fields */
  441. {
  442. .drv = DRIVER_bflsc,
  443. .name = "BAS",
  444. .ident = IDENT_BAS,
  445. .idVendor = IDVENDOR_FTDI,
  446. .idProduct = 0x6014,
  447. .iManufacturer = "FTDI",
  448. .iProduct = "BitFORCE SHA256 SC",
  449. .config = 1,
  450. .timeout = BFLSC_TIMEOUT_MS,
  451. .latency = LATENCY_STD,
  452. INTINFO(bflsc_ints) },
  453. {
  454. .drv = DRIVER_bflsc,
  455. .name = "BAS",
  456. .ident = IDENT_BAS,
  457. .idVendor = IDVENDOR_FTDI,
  458. .idProduct = 0x6014,
  459. .iManufacturer = "Butterfly Labs",
  460. .iProduct = "BitFORCE SHA256 SC",
  461. .config = 1,
  462. .timeout = BFLSC_TIMEOUT_MS,
  463. .latency = LATENCY_STD,
  464. INTINFO(bflsc_ints) },
  465. {
  466. .drv = DRIVER_bflsc,
  467. .name = "BMA",
  468. .ident = IDENT_BMA,
  469. .idVendor = IDVENDOR_FTDI,
  470. .idProduct = 0x6014,
  471. .iManufacturer = "BUTTERFLY LABS",
  472. .iProduct = "BitFORCE SHA256 SC",
  473. .config = 1,
  474. .timeout = BFLSC_TIMEOUT_MS,
  475. .latency = LATENCY_STD,
  476. INTINFO(bflsc_ints) },
  477. {
  478. .drv = DRIVER_bflsc,
  479. .name = "BMA",
  480. .ident = IDENT_BMA,
  481. .idVendor = IDVENDOR_FTDI,
  482. .idProduct = 0x6014,
  483. .iManufacturer = "BUTTERFLY LABS",
  484. .iProduct = "BitFORCE SC-28nm",
  485. .config = 1,
  486. .timeout = BFLSC_TIMEOUT_MS,
  487. .latency = LATENCY_STD,
  488. INTINFO(bflsc_ints) },
  489. {
  490. .drv = DRIVER_bflsc,
  491. .name = "BMA",
  492. .ident = IDENT_BMA,
  493. .idVendor = IDVENDOR_FTDI,
  494. .idProduct = 0x6014,
  495. .iManufacturer = "BUTTERFLY LABS",
  496. .iProduct = "BitFORCE SHA256",
  497. .config = 1,
  498. .timeout = BFLSC_TIMEOUT_MS,
  499. .latency = LATENCY_STD,
  500. INTINFO(bflsc_ints) },
  501. #endif
  502. #ifdef USE_BITFORCE
  503. {
  504. .drv = DRIVER_bitforce,
  505. .name = "BFL",
  506. .ident = IDENT_BFL,
  507. .idVendor = IDVENDOR_FTDI,
  508. .idProduct = 0x6014,
  509. .iManufacturer = "Butterfly Labs Inc.",
  510. .iProduct = "BitFORCE SHA256",
  511. .config = 1,
  512. .timeout = BITFORCE_TIMEOUT_MS,
  513. .latency = LATENCY_STD,
  514. INTINFO(bfl_ints) },
  515. #endif
  516. #ifdef USE_BITFURY
  517. {
  518. .drv = DRIVER_bitfury,
  519. .name = "BF1",
  520. .ident = IDENT_BF1,
  521. .idVendor = 0x03eb,
  522. .idProduct = 0x204b,
  523. .config = 1,
  524. .timeout = BITFURY_TIMEOUT_MS,
  525. .latency = LATENCY_UNUSED,
  526. //.iManufacturer = "BPMC",
  527. .iProduct = "Bitfury BF1",
  528. INTINFO(bfu_ints)
  529. },
  530. {
  531. .drv = DRIVER_bitfury,
  532. .name = "BXF",
  533. .ident = IDENT_BXF,
  534. .idVendor = 0x198c,
  535. .idProduct = 0xb1f1,
  536. .config = 1,
  537. .timeout = BITFURY_TIMEOUT_MS,
  538. .latency = LATENCY_UNUSED,
  539. .iManufacturer = "c-scape",
  540. .iProduct = "bi?fury",
  541. INTINFO(bxf_ints)
  542. },
  543. {
  544. .drv = DRIVER_bitfury,
  545. .name = "OSM",
  546. .ident = IDENT_OSM,
  547. .idVendor = 0x198c,
  548. .idProduct = 0xb1f1,
  549. .config = 1,
  550. .timeout = BITFURY_TIMEOUT_MS,
  551. .latency = LATENCY_UNUSED,
  552. .iManufacturer = "c-scape",
  553. .iProduct = "OneString",
  554. INTINFO(bxf_ints)
  555. },
  556. {
  557. .drv = DRIVER_bitfury,
  558. .name = "NFU",
  559. .ident = IDENT_NFU,
  560. .idVendor = 0x04d8,
  561. .idProduct = 0x00de,
  562. .config = 1,
  563. .timeout = BITFURY_TIMEOUT_MS,
  564. .latency = LATENCY_UNUSED,
  565. INTINFO(nfu_ints)
  566. },
  567. {
  568. .drv = DRIVER_bitfury,
  569. .name = "BXM",
  570. .ident = IDENT_BXM,
  571. .idVendor = 0x0403,
  572. .idProduct = 0x6014,
  573. .config = 1,
  574. .timeout = BITFURY_TIMEOUT_MS,
  575. .latency = LATENCY_UNUSED,
  576. INTINFO(bxm_ints)
  577. },
  578. #endif
  579. #ifdef USE_DRILLBIT
  580. {
  581. .drv = DRIVER_drillbit,
  582. .name = "DRB",
  583. .ident = IDENT_DRB,
  584. .idVendor = 0x03eb,
  585. .idProduct = 0x2404,
  586. .config = 1,
  587. .timeout = DRILLBIT_TIMEOUT_MS,
  588. .latency = LATENCY_UNUSED,
  589. .iManufacturer = "Drillbit Systems",
  590. .iProduct = NULL, /* Can be Thumb or Eight, same driver */
  591. INTINFO(drillbit_ints)
  592. },
  593. #endif
  594. #ifdef USE_MODMINER
  595. {
  596. .drv = DRIVER_modminer,
  597. .name = "MMQ",
  598. .ident = IDENT_MMQ,
  599. .idVendor = 0x1fc9,
  600. .idProduct = 0x0003,
  601. .config = 1,
  602. .timeout = MODMINER_TIMEOUT_MS,
  603. .latency = LATENCY_UNUSED,
  604. INTINFO(mmq_ints) },
  605. #endif
  606. #ifdef USE_AVALON
  607. {
  608. .drv = DRIVER_avalon,
  609. .name = "BTB",
  610. .ident = IDENT_BTB,
  611. .idVendor = IDVENDOR_FTDI,
  612. .idProduct = 0x6001,
  613. .iManufacturer = "Burnin Electronics",
  614. .iProduct = "BitBurner",
  615. .config = 1,
  616. .timeout = AVALON_TIMEOUT_MS,
  617. .latency = 10,
  618. INTINFO(ava_ints) },
  619. {
  620. .drv = DRIVER_avalon,
  621. .name = "BBF",
  622. .ident = IDENT_BBF,
  623. .idVendor = IDVENDOR_FTDI,
  624. .idProduct = 0x6001,
  625. .iManufacturer = "Burnin Electronics",
  626. .iProduct = "BitBurner Fury",
  627. .config = 1,
  628. .timeout = AVALON_TIMEOUT_MS,
  629. .latency = 10,
  630. INTINFO(ava_ints) },
  631. {
  632. .drv = DRIVER_avalon,
  633. .name = "AVA",
  634. .ident = IDENT_AVA,
  635. .idVendor = IDVENDOR_FTDI,
  636. .idProduct = 0x6001,
  637. .config = 1,
  638. .timeout = AVALON_TIMEOUT_MS,
  639. .latency = 10,
  640. INTINFO(ava_ints) },
  641. #endif
  642. #ifdef USE_AVALON2
  643. {
  644. .drv = DRIVER_avalon2,
  645. .name = "AV2",
  646. .ident = IDENT_AV2,
  647. .idVendor = 0x067b,
  648. .idProduct = 0x2303,
  649. .config = 1,
  650. .timeout = AVALON_TIMEOUT_MS,
  651. .latency = LATENCY_UNUSED,
  652. INTINFO(ava2_ints) },
  653. #endif
  654. #ifdef USE_AVALON4
  655. {
  656. .drv = DRIVER_avalon4,
  657. .name = "AV4",
  658. .ident = IDENT_AV4,
  659. .idVendor = 0x29f1,
  660. .idProduct = 0x33f2,
  661. .iManufacturer = "CANAAN",
  662. .iProduct = "USB2IIC Converter",
  663. .config = 1,
  664. .timeout = AVALON4_TIMEOUT_MS,
  665. .latency = LATENCY_UNUSED,
  666. INTINFO(ava4_ints) },
  667. #endif
  668. #ifdef USE_AVALON7
  669. {
  670. .drv = DRIVER_avalon7,
  671. .name = "AV7",
  672. .ident = IDENT_AV7,
  673. .idVendor = 0x29f1,
  674. .idProduct = 0x33f2,
  675. .iManufacturer = "CANAAN",
  676. .iProduct = "USB2IIC Converter",
  677. .config = 1,
  678. .timeout = AVALON7_TIMEOUT_MS,
  679. .latency = LATENCY_UNUSED,
  680. INTINFO(ava7_ints) },
  681. #endif
  682. #ifdef USE_AVALON8
  683. {
  684. .drv = DRIVER_avalon8,
  685. .name = "AV8",
  686. .ident = IDENT_AV8,
  687. .idVendor = 0x29f1,
  688. .idProduct = 0x33f2,
  689. .iManufacturer = "CANAAN",
  690. .iProduct = "USB2IIC Converter",
  691. .config = 1,
  692. .timeout = AVALON8_TIMEOUT_MS,
  693. .latency = LATENCY_UNUSED,
  694. INTINFO(ava8_ints) },
  695. #endif
  696. #ifdef USE_AVALON9
  697. {
  698. .drv = DRIVER_avalon9,
  699. .name = "AV9",
  700. .ident = IDENT_AV9,
  701. .idVendor = 0x29f1,
  702. .idProduct = 0x33f2,
  703. .iManufacturer = "CANAAN",
  704. .iProduct = "USB2IIC Converter",
  705. .config = 1,
  706. .timeout = AVALON9_TIMEOUT_MS,
  707. .latency = LATENCY_UNUSED,
  708. INTINFO(ava9_ints) },
  709. #endif
  710. #ifdef USE_AVALONLC3
  711. {
  712. .drv = DRIVER_avalonlc3,
  713. .name = "AVLC3",
  714. .ident = IDENT_AVLC3,
  715. .idVendor = 0x29f1,
  716. .idProduct = 0x33f2,
  717. .iManufacturer = "CANAAN",
  718. .iProduct = "USB2IIC Converter",
  719. .config = 1,
  720. .timeout = AVALONLC3_TIMEOUT_MS,
  721. .latency = LATENCY_UNUSED,
  722. INTINFO(avalc3_ints) },
  723. #endif
  724. #ifdef USE_AVALON_MINER
  725. {
  726. .drv = DRIVER_avalonm,
  727. .name = "AV4M",
  728. .ident = IDENT_AVM,
  729. .idVendor = 0x29f1,
  730. .idProduct = 0x40f1,
  731. .iManufacturer = "CANAAN",
  732. .iProduct = "Avalon4 mini",
  733. .config = 1,
  734. .timeout = AVALONM_TIMEOUT_MS,
  735. .latency = LATENCY_UNUSED,
  736. INTINFO(avam_ints) },
  737. {
  738. .drv = DRIVER_avalonm,
  739. .name = "AV3U",
  740. .ident = IDENT_AVM,
  741. .idVendor = 0x29f1,
  742. .idProduct = 0x33f3,
  743. .iManufacturer = "CANAAN",
  744. .iProduct = "Avalon nano",
  745. .config = 1,
  746. .timeout = AVALONM_TIMEOUT_MS,
  747. .latency = LATENCY_UNUSED,
  748. INTINFO(av3u_ints) },
  749. #endif
  750. #ifdef USE_HASHFAST
  751. {
  752. .drv = DRIVER_hashfast,
  753. .name = "HFA",
  754. .ident = IDENT_HFA,
  755. .idVendor = HF_USB_VENDOR_ID,
  756. .idProduct = HF_USB_PRODUCT_ID_G1,
  757. .iManufacturer = "HashFast LLC",
  758. .iProduct = "M1 Module",
  759. .config = 1,
  760. .timeout = HASHFAST_TIMEOUT_MS,
  761. .latency = LATENCY_UNUSED,
  762. INTINFO(hfa_ints) },
  763. #endif
  764. #ifdef USE_HASHRATIO
  765. {
  766. .drv = DRIVER_hashratio,
  767. .name = "HRO",
  768. .ident = IDENT_HRO,
  769. .idVendor = IDVENDOR_FTDI,
  770. .idProduct = 0x6001,
  771. .config = 1,
  772. .timeout = HASHRATIO_TIMEOUT_MS,
  773. .latency = LATENCY_UNUSED,
  774. INTINFO(hro_ints) },
  775. #endif
  776. #ifdef USE_KLONDIKE
  777. {
  778. .drv = DRIVER_klondike,
  779. .name = "KLN",
  780. .ident = IDENT_KLN,
  781. .idVendor = 0x04D8,
  782. .idProduct = 0xF60A,
  783. .config = 1,
  784. .timeout = KLONDIKE_TIMEOUT_MS,
  785. .latency = 10,
  786. INTINFO(kln_ints) },
  787. {
  788. .drv = DRIVER_klondike,
  789. .name = "KLI",
  790. .ident = IDENT_KLN,
  791. .idVendor = 0x04D8,
  792. .idProduct = 0xF60A,
  793. .config = 1,
  794. .timeout = KLONDIKE_TIMEOUT_MS,
  795. .latency = 10,
  796. INTINFO(kli_ints) },
  797. #endif
  798. #ifdef USE_ICARUS
  799. {
  800. .drv = DRIVER_icarus,
  801. .name = "ICA",
  802. .ident = IDENT_ICA,
  803. .idVendor = 0x067b,
  804. .idProduct = 0x2303,
  805. .config = 1,
  806. .timeout = ICARUS_TIMEOUT_MS,
  807. .latency = LATENCY_UNUSED,
  808. INTINFO(ica_ints) },
  809. {
  810. .drv = DRIVER_icarus,
  811. .name = "ICA",
  812. .ident = IDENT_AVA,
  813. .idVendor = 0x1fc9,
  814. .idProduct = 0x0083,
  815. .config = 1,
  816. .timeout = ICARUS_TIMEOUT_MS,
  817. .latency = LATENCY_UNUSED,
  818. INTINFO(ica1_ints) },
  819. {
  820. .drv = DRIVER_icarus,
  821. .name = "AMU",
  822. .ident = IDENT_AMU,
  823. .idVendor = 0x10c4,
  824. .idProduct = 0xea60,
  825. .config = 1,
  826. .timeout = ICARUS_TIMEOUT_MS,
  827. .latency = LATENCY_UNUSED,
  828. INTINFO(amu_ints) },
  829. {
  830. .drv = DRIVER_icarus,
  831. .name = "LIN",
  832. .ident = IDENT_LIN,
  833. .idVendor = 0x10c4,
  834. .idProduct = 0xea60,
  835. .config = 1,
  836. .timeout = ICARUS_TIMEOUT_MS,
  837. .latency = LATENCY_UNUSED,
  838. INTINFO(amu_ints) },
  839. {
  840. .drv = DRIVER_icarus,
  841. .name = "ANU",
  842. .ident = IDENT_ANU,
  843. .idVendor = 0x10c4,
  844. .idProduct = 0xea60,
  845. .config = 1,
  846. .timeout = ICARUS_TIMEOUT_MS,
  847. .latency = LATENCY_UNUSED,
  848. INTINFO(amu_ints) },
  849. {
  850. .drv = DRIVER_icarus,
  851. .name = "BLT",
  852. .ident = IDENT_BLT,
  853. .idVendor = IDVENDOR_FTDI,
  854. .idProduct = 0x6001,
  855. .iProduct = "FT232R USB UART",
  856. .config = 1,
  857. .timeout = ICARUS_TIMEOUT_MS,
  858. .latency = LATENCY_STD,
  859. INTINFO(llt_ints) },
  860. // For any that don't match the above "BLT"
  861. {
  862. .drv = DRIVER_icarus,
  863. .name = "LLT",
  864. .ident = IDENT_LLT,
  865. .idVendor = IDVENDOR_FTDI,
  866. .idProduct = 0x6001,
  867. .config = 1,
  868. .timeout = ICARUS_TIMEOUT_MS,
  869. .latency = LATENCY_STD,
  870. INTINFO(llt_ints) },
  871. {
  872. .drv = DRIVER_icarus,
  873. .name = "CMR",
  874. .ident = IDENT_CMR1,
  875. .idVendor = IDVENDOR_FTDI,
  876. .idProduct = 0x6014,
  877. .iProduct = "Cairnsmore1",
  878. .config = 1,
  879. .timeout = ICARUS_TIMEOUT_MS,
  880. .latency = LATENCY_STD,
  881. INTINFO(cmr1_ints) },
  882. {
  883. .drv = DRIVER_icarus,
  884. .name = "CMR",
  885. .ident = IDENT_CMR2,
  886. .idVendor = IDVENDOR_FTDI,
  887. .idProduct = 0x8350,
  888. .iProduct = "Cairnsmore1",
  889. .config = 1,
  890. .timeout = ICARUS_TIMEOUT_MS,
  891. .latency = LATENCY_STD,
  892. INTINFO(cmr2_ints) },
  893. #endif
  894. #ifdef USE_COINTERRA
  895. {
  896. .drv = DRIVER_cointerra,
  897. .name = "CTA",
  898. .ident = IDENT_CTA,
  899. .idVendor = 0x1cbe,
  900. .idProduct = 0x0003,
  901. .config = 1,
  902. .timeout = COINTERRA_TIMEOUT_MS,
  903. .latency = LATENCY_STD,
  904. INTINFO(cointerra_ints) },
  905. #endif
  906. #ifdef USE_ANT_S1
  907. {
  908. .drv = DRIVER_ants1,
  909. .name = "ANT",
  910. .ident = IDENT_ANT,
  911. .idVendor = 0x4254,
  912. .idProduct = 0x4153,
  913. .config = 1,
  914. .timeout = ANT_S1_TIMEOUT_MS,
  915. .latency = LATENCY_ANTS1,
  916. INTINFO(ants1_ints) },
  917. #endif
  918. #ifdef USE_ANT_S3
  919. {
  920. .drv = DRIVER_ants3,
  921. .name = "AS3",
  922. .ident = IDENT_AS3,
  923. .idVendor = 0x4254,
  924. .idProduct = 0x4153,
  925. .config = 1,
  926. .timeout = ANT_S3_TIMEOUT_MS,
  927. .latency = LATENCY_ANTS3,
  928. INTINFO(ants3_ints) },
  929. #endif
  930. #ifdef USE_BLOCKERUPTER
  931. {
  932. .drv = DRIVER_blockerupter,
  933. .name = "BET",
  934. .ident = IDENT_BET,
  935. .idVendor = 0x10c4,
  936. .idProduct = 0xea60,
  937. .config = 1,
  938. .timeout = BLOCKERUPTER_TIMEOUT_MS,
  939. .latency = LATENCY_UNUSED,
  940. INTINFO(bet_ints) },
  941. #endif
  942. #ifdef USE_GEKKO
  943. {
  944. .drv = DRIVER_gekko,
  945. .name = "BSC",
  946. .ident = IDENT_BSC,
  947. .idVendor = 0x10c4,
  948. .idProduct = 0xea60,
  949. .iManufacturer = "bitshopperde",
  950. .iProduct = "Compac BM1384 Bitcoin Miner",
  951. .config = 1,
  952. .timeout = COMPAC_TIMEOUT_MS,
  953. .latency = LATENCY_UNUSED,
  954. INTINFO(gek1_ints) },
  955. {
  956. .drv = DRIVER_gekko,
  957. .name = "BSD",
  958. .ident = IDENT_BSD,
  959. .idVendor = 0x10c4,
  960. .idProduct = 0xea60,
  961. .iManufacturer = "bitshopperde",
  962. .iProduct = "2Pac BM1384 Bitcoin Miner",
  963. .config = 1,
  964. .timeout = COMPAC_TIMEOUT_MS,
  965. .latency = LATENCY_UNUSED,
  966. INTINFO(gek1_ints) },
  967. {
  968. .drv = DRIVER_gekko,
  969. .name = "BSE",
  970. .ident = IDENT_BSE,
  971. .idVendor = 0x10c4,
  972. .idProduct = 0xea60,
  973. .iManufacturer = "bitshopperde",
  974. .iProduct = "Terminus BM1384 Bitcoin Miner",
  975. .config = 1,
  976. .timeout = COMPAC_TIMEOUT_MS,
  977. .latency = LATENCY_UNUSED,
  978. INTINFO(gek1_ints) },
  979. {
  980. .drv = DRIVER_gekko,
  981. .name = "GSC",
  982. .ident = IDENT_GSC,
  983. .idVendor = 0x10c4,
  984. .idProduct = 0xea60,
  985. .iManufacturer = "GekkoScience",
  986. .iProduct = "Compac BM1384 Bitcoin Miner",
  987. .config = 1,
  988. .timeout = COMPAC_TIMEOUT_MS,
  989. .latency = LATENCY_UNUSED,
  990. INTINFO(gek1_ints) },
  991. {
  992. .drv = DRIVER_gekko,
  993. .name = "GSD",
  994. .ident = IDENT_GSD,
  995. .idVendor = 0x10c4,
  996. .idProduct = 0xea60,
  997. .iManufacturer = "GekkoScience",
  998. .iProduct = "2Pac BM1384 Bitcoin Miner",
  999. .config = 1,
  1000. .timeout = COMPAC_TIMEOUT_MS,
  1001. .latency = LATENCY_UNUSED,
  1002. INTINFO(gek1_ints) },
  1003. {
  1004. .drv = DRIVER_gekko,
  1005. .name = "GSE",
  1006. .ident = IDENT_GSE,
  1007. .idVendor = 0x10c4,
  1008. .idProduct = 0xea60,
  1009. .iManufacturer = "GekkoScience",
  1010. .iProduct = "Terminus BM1384 Bitcoin Miner",
  1011. .config = 1,
  1012. .timeout = COMPAC_TIMEOUT_MS,
  1013. .latency = LATENCY_UNUSED,
  1014. INTINFO(gek1_ints) },
  1015. {
  1016. .drv = DRIVER_gekko,
  1017. .name = "GSH",
  1018. .ident = IDENT_GSH,
  1019. .idVendor = 0x0403,
  1020. .idProduct = 0x6015,
  1021. .iManufacturer = "GekkoScience",
  1022. .iProduct = "NewPac Bitcoin Miner",
  1023. .config = 1,
  1024. .timeout = COMPAC_TIMEOUT_MS,
  1025. .latency = LATENCY_UNUSED,
  1026. INTINFO(gek2_ints) },
  1027. {
  1028. .drv = DRIVER_gekko,
  1029. .name = "BAX",
  1030. .ident = IDENT_BAX,
  1031. .idVendor = 0x0403,
  1032. .idProduct = 0x6001,
  1033. .iManufacturer = "FTDI",
  1034. .iProduct = "FT232R USB UART",
  1035. .config = 1,
  1036. .timeout = COMPAC_TIMEOUT_MS,
  1037. .latency = LATENCY_UNUSED,
  1038. INTINFO(gek2_ints) },
  1039. {
  1040. .drv = DRIVER_gekko,
  1041. .name = "GSI",
  1042. .ident = IDENT_GSI,
  1043. .idVendor = 0x0403,
  1044. .idProduct = 0x6015,
  1045. .iManufacturer = "GekkoScience",
  1046. .iProduct = "R606 Bitcoin Miner",
  1047. .config = 1,
  1048. .timeout = COMPAC_TIMEOUT_MS,
  1049. .latency = LATENCY_UNUSED,
  1050. INTINFO(gek2_ints) },
  1051. {
  1052. .drv = DRIVER_gekko,
  1053. .name = "GSF",
  1054. .ident = IDENT_GSF,
  1055. .idVendor = 0x0403,
  1056. .idProduct = 0x6015,
  1057. .iManufacturer = "GekkoScience",
  1058. .iProduct = "CompacF Bitcoin Miner",
  1059. .config = 1,
  1060. .timeout = COMPAC_TIMEOUT_MS,
  1061. .latency = LATENCY_UNUSED,
  1062. INTINFO(gek2_ints) },
  1063. {
  1064. .drv = DRIVER_gekko,
  1065. .name = "GSF",
  1066. .ident = IDENT_GSFM,
  1067. .idVendor = 0x0403,
  1068. .idProduct = 0x6015,
  1069. .iManufacturer = "GekkoScience",
  1070. .iProduct = "R909 Bitcoin Miner",
  1071. .config = 1,
  1072. .timeout = COMPAC_TIMEOUT_MS,
  1073. .latency = LATENCY_UNUSED,
  1074. INTINFO(gek2_ints) },
  1075. #endif
  1076. { DRIVER_MAX, NULL, 0, 0, 0, NULL, NULL, 0, 0, 0, 0, NULL }
  1077. };
  1078. #define STRBUFLEN 256
  1079. static const char *BLANK = "";
  1080. static const char *space = " ";
  1081. static const char *nodatareturned = "no data returned ";
  1082. #if 0 // enable USBDEBUG - only during development testing
  1083. static const char *debug_true_str = "true";
  1084. static const char *debug_false_str = "false";
  1085. static const char *nodevstr = "=NODEV";
  1086. #define bool_str(boo) ((boo) ? debug_true_str : debug_false_str)
  1087. #define isnodev(err) (NODEV(err) ? nodevstr : BLANK)
  1088. #define USBDEBUG(fmt, ...) applog(LOG_WARNING, fmt, ##__VA_ARGS__)
  1089. #else
  1090. #define USBDEBUG(fmt, ...)
  1091. #endif
  1092. // For device limits by driver
  1093. static struct driver_count {
  1094. int count;
  1095. int limit;
  1096. } drv_count[DRIVER_MAX];
  1097. // For device limits by list of bus/dev
  1098. static struct usb_busdev {
  1099. int bus_number;
  1100. int device_address;
  1101. #ifdef WIN32
  1102. void *resource1;
  1103. void *resource2;
  1104. #else
  1105. int fd;
  1106. #endif
  1107. } *busdev;
  1108. static int busdev_count = 0;
  1109. // Total device limit
  1110. static int total_count = 0;
  1111. static int total_limit = 999999;
  1112. struct usb_in_use_list {
  1113. struct usb_busdev in_use;
  1114. struct usb_in_use_list *prev;
  1115. struct usb_in_use_list *next;
  1116. };
  1117. // List of in use devices
  1118. static struct usb_in_use_list *in_use_head = NULL;
  1119. static struct usb_in_use_list *blacklist_head = NULL;
  1120. struct resource_work {
  1121. bool lock;
  1122. const char *dname;
  1123. uint8_t bus_number;
  1124. uint8_t device_address;
  1125. struct resource_work *next;
  1126. };
  1127. // Pending work for the reslock thread
  1128. struct resource_work *res_work_head = NULL;
  1129. struct resource_reply {
  1130. uint8_t bus_number;
  1131. uint8_t device_address;
  1132. bool got;
  1133. struct resource_reply *next;
  1134. };
  1135. // Replies to lock requests
  1136. struct resource_reply *res_reply_head = NULL;
  1137. // Some stats need to always be defined
  1138. #define SEQ0 0
  1139. #define SEQ1 1
  1140. // NONE must be 0 - calloced
  1141. #define MODE_NONE 0
  1142. #define MODE_CTRL_READ (1 << 0)
  1143. #define MODE_CTRL_WRITE (1 << 1)
  1144. #define MODE_BULK_READ (1 << 2)
  1145. #define MODE_BULK_WRITE (1 << 3)
  1146. // Set this to 0 to remove stats processing
  1147. #define DO_USB_STATS 1
  1148. static bool stats_initialised = false;
  1149. #if DO_USB_STATS
  1150. #define MODE_SEP_STR "+"
  1151. #define MODE_NONE_STR "X"
  1152. #define MODE_CTRL_READ_STR "cr"
  1153. #define MODE_CTRL_WRITE_STR "cw"
  1154. #define MODE_BULK_READ_STR "br"
  1155. #define MODE_BULK_WRITE_STR "bw"
  1156. // One for each CMD, TIMEOUT, ERROR
  1157. struct cg_usb_stats_item {
  1158. uint64_t count;
  1159. double total_delay;
  1160. double min_delay;
  1161. double max_delay;
  1162. struct timeval first;
  1163. struct timeval last;
  1164. };
  1165. #define CMD_CMD 0
  1166. #define CMD_TIMEOUT 1
  1167. #define CMD_ERROR 2
  1168. // One for each C_CMD
  1169. struct cg_usb_stats_details {
  1170. int seq;
  1171. uint32_t modes;
  1172. struct cg_usb_stats_item item[CMD_ERROR+1];
  1173. };
  1174. // One for each device
  1175. struct cg_usb_stats {
  1176. char *name;
  1177. int device_id;
  1178. struct cg_usb_stats_details *details;
  1179. };
  1180. static struct cg_usb_stats *usb_stats = NULL;
  1181. static int next_stat = USB_NOSTAT;
  1182. #define SECTOMS(s) ((int)((s) * 1000))
  1183. #define USB_STATS(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_) \
  1184. stats(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_)
  1185. #define STATS_TIMEVAL(tv_) cgtime(tv_)
  1186. #define USB_REJECT(sgpu_, mode_) rejected_inc(sgpu_, mode_)
  1187. #else
  1188. #define USB_STATS(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_)
  1189. #define STATS_TIMEVAL(tv_)
  1190. #define USB_REJECT(sgpu_, mode_)
  1191. #endif // DO_USB_STATS
  1192. /* Create usb_commands array from USB_PARSE_COMMANDS macro in usbutils.h */
  1193. char *usb_commands[] = {
  1194. USB_PARSE_COMMANDS(JUMPTABLE)
  1195. "Null"
  1196. };
  1197. #ifdef EOL
  1198. #undef EOL
  1199. #endif
  1200. #define EOL "\n"
  1201. static const char *DESDEV = "Device";
  1202. static const char *DESCON = "Config";
  1203. static const char *DESSTR = "String";
  1204. static const char *DESINT = "Interface";
  1205. static const char *DESEP = "Endpoint";
  1206. static const char *DESHID = "HID";
  1207. static const char *DESRPT = "Report";
  1208. static const char *DESPHY = "Physical";
  1209. static const char *DESHUB = "Hub";
  1210. static const char *EPIN = "In: ";
  1211. static const char *EPOUT = "Out: ";
  1212. static const char *EPX = "?: ";
  1213. static const char *CONTROL = "Control";
  1214. static const char *ISOCHRONOUS_X = "Isochronous+?";
  1215. static const char *ISOCHRONOUS_N_X = "Isochronous+None+?";
  1216. static const char *ISOCHRONOUS_N_D = "Isochronous+None+Data";
  1217. static const char *ISOCHRONOUS_N_F = "Isochronous+None+Feedback";
  1218. static const char *ISOCHRONOUS_N_I = "Isochronous+None+Implicit";
  1219. static const char *ISOCHRONOUS_A_X = "Isochronous+Async+?";
  1220. static const char *ISOCHRONOUS_A_D = "Isochronous+Async+Data";
  1221. static const char *ISOCHRONOUS_A_F = "Isochronous+Async+Feedback";
  1222. static const char *ISOCHRONOUS_A_I = "Isochronous+Async+Implicit";
  1223. static const char *ISOCHRONOUS_D_X = "Isochronous+Adaptive+?";
  1224. static const char *ISOCHRONOUS_D_D = "Isochronous+Adaptive+Data";
  1225. static const char *ISOCHRONOUS_D_F = "Isochronous+Adaptive+Feedback";
  1226. static const char *ISOCHRONOUS_D_I = "Isochronous+Adaptive+Implicit";
  1227. static const char *ISOCHRONOUS_S_X = "Isochronous+Sync+?";
  1228. static const char *ISOCHRONOUS_S_D = "Isochronous+Sync+Data";
  1229. static const char *ISOCHRONOUS_S_F = "Isochronous+Sync+Feedback";
  1230. static const char *ISOCHRONOUS_S_I = "Isochronous+Sync+Implicit";
  1231. static const char *BULK = "Bulk";
  1232. static const char *INTERRUPT = "Interrupt";
  1233. static const char *UNKNOWN = "Unknown";
  1234. static const char *destype(uint8_t bDescriptorType)
  1235. {
  1236. switch (bDescriptorType) {
  1237. case LIBUSB_DT_DEVICE:
  1238. return DESDEV;
  1239. case LIBUSB_DT_CONFIG:
  1240. return DESCON;
  1241. case LIBUSB_DT_STRING:
  1242. return DESSTR;
  1243. case LIBUSB_DT_INTERFACE:
  1244. return DESINT;
  1245. case LIBUSB_DT_ENDPOINT:
  1246. return DESEP;
  1247. case LIBUSB_DT_HID:
  1248. return DESHID;
  1249. case LIBUSB_DT_REPORT:
  1250. return DESRPT;
  1251. case LIBUSB_DT_PHYSICAL:
  1252. return DESPHY;
  1253. case LIBUSB_DT_HUB:
  1254. return DESHUB;
  1255. }
  1256. return UNKNOWN;
  1257. }
  1258. static const char *epdir(uint8_t bEndpointAddress)
  1259. {
  1260. switch (bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
  1261. case LIBUSB_ENDPOINT_IN:
  1262. return EPIN;
  1263. case LIBUSB_ENDPOINT_OUT:
  1264. return EPOUT;
  1265. }
  1266. return EPX;
  1267. }
  1268. static const char *epatt(uint8_t bmAttributes)
  1269. {
  1270. switch(bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
  1271. case LIBUSB_TRANSFER_TYPE_CONTROL:
  1272. return CONTROL;
  1273. case LIBUSB_TRANSFER_TYPE_BULK:
  1274. return BULK;
  1275. case LIBUSB_TRANSFER_TYPE_INTERRUPT:
  1276. return INTERRUPT;
  1277. case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
  1278. switch(bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
  1279. case LIBUSB_ISO_SYNC_TYPE_NONE:
  1280. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  1281. case LIBUSB_ISO_USAGE_TYPE_DATA:
  1282. return ISOCHRONOUS_N_D;
  1283. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  1284. return ISOCHRONOUS_N_F;
  1285. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  1286. return ISOCHRONOUS_N_I;
  1287. }
  1288. return ISOCHRONOUS_N_X;
  1289. case LIBUSB_ISO_SYNC_TYPE_ASYNC:
  1290. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  1291. case LIBUSB_ISO_USAGE_TYPE_DATA:
  1292. return ISOCHRONOUS_A_D;
  1293. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  1294. return ISOCHRONOUS_A_F;
  1295. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  1296. return ISOCHRONOUS_A_I;
  1297. }
  1298. return ISOCHRONOUS_A_X;
  1299. case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
  1300. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  1301. case LIBUSB_ISO_USAGE_TYPE_DATA:
  1302. return ISOCHRONOUS_D_D;
  1303. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  1304. return ISOCHRONOUS_D_F;
  1305. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  1306. return ISOCHRONOUS_D_I;
  1307. }
  1308. return ISOCHRONOUS_D_X;
  1309. case LIBUSB_ISO_SYNC_TYPE_SYNC:
  1310. switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
  1311. case LIBUSB_ISO_USAGE_TYPE_DATA:
  1312. return ISOCHRONOUS_S_D;
  1313. case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
  1314. return ISOCHRONOUS_S_F;
  1315. case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
  1316. return ISOCHRONOUS_S_I;
  1317. }
  1318. return ISOCHRONOUS_S_X;
  1319. }
  1320. return ISOCHRONOUS_X;
  1321. }
  1322. return UNKNOWN;
  1323. }
  1324. static void append(char **buf, char *append, size_t *off, size_t *len)
  1325. {
  1326. int new = strlen(append);
  1327. if ((new + *off) >= *len)
  1328. {
  1329. *len *= 2;
  1330. *buf = cgrealloc(*buf, *len);
  1331. }
  1332. strcpy(*buf + *off, append);
  1333. *off += new;
  1334. }
  1335. static bool setgetdes(ssize_t count, libusb_device *dev, struct libusb_device_handle *handle, struct libusb_config_descriptor **config, int cd, char **buf, size_t *off, size_t *len)
  1336. {
  1337. char tmp[512];
  1338. int err;
  1339. err = libusb_set_configuration(handle, cd);
  1340. if (err) {
  1341. snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to set config descriptor to %d, err %d",
  1342. (int)count, cd, err);
  1343. append(buf, tmp, off, len);
  1344. return false;
  1345. }
  1346. err = libusb_get_active_config_descriptor(dev, config);
  1347. if (err) {
  1348. snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to get active config descriptor set to %d, err %d",
  1349. (int)count, cd, err);
  1350. append(buf, tmp, off, len);
  1351. return false;
  1352. }
  1353. snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Set & Got active config descriptor to %d, err %d",
  1354. (int)count, cd, err);
  1355. append(buf, tmp, off, len);
  1356. return true;
  1357. }
  1358. static void usb_full(ssize_t *count, libusb_device *dev, char **buf, size_t *off, size_t *len, int level)
  1359. {
  1360. struct libusb_device_descriptor desc;
  1361. uint8_t bus_number;
  1362. uint8_t device_address;
  1363. struct libusb_device_handle *handle;
  1364. struct libusb_config_descriptor *config;
  1365. const struct libusb_interface_descriptor *idesc;
  1366. const struct libusb_endpoint_descriptor *epdesc;
  1367. unsigned char man[STRBUFLEN+1];
  1368. unsigned char prod[STRBUFLEN+1];
  1369. unsigned char ser[STRBUFLEN+1];
  1370. char tmp[512];
  1371. int err, i, j, k;
  1372. err = libusb_get_device_descriptor(dev, &desc);
  1373. if (opt_usb_list_all && err) {
  1374. snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Failed to get descriptor, err %d",
  1375. (int)(++(*count)), err);
  1376. append(buf, tmp, off, len);
  1377. return;
  1378. }
  1379. bus_number = libusb_get_bus_number(dev);
  1380. device_address = libusb_get_device_address(dev);
  1381. if (!opt_usb_list_all) {
  1382. bool known = false;
  1383. for (i = 0; find_dev[i].drv != DRIVER_MAX; i++)
  1384. if ((find_dev[i].idVendor == desc.idVendor) &&
  1385. (find_dev[i].idProduct == desc.idProduct)) {
  1386. known = true;
  1387. break;
  1388. }
  1389. if (!known)
  1390. return;
  1391. }
  1392. (*count)++;
  1393. if (level == 0) {
  1394. snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Bus %d Device %d ID: %04x:%04x",
  1395. (int)(*count), (int)bus_number, (int)device_address,
  1396. desc.idVendor, desc.idProduct);
  1397. } else {
  1398. snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Bus %d Device %d Device Descriptor:" EOL "\tLength: %d" EOL
  1399. "\tDescriptor Type: %s" EOL "\tUSB: %04x" EOL "\tDeviceClass: %d" EOL
  1400. "\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
  1401. "\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
  1402. "\tNumConfigurations: %d",
  1403. (int)(*count), (int)bus_number, (int)device_address,
  1404. (int)(desc.bLength), destype(desc.bDescriptorType),
  1405. desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
  1406. (int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
  1407. desc.idVendor, desc.idProduct, desc.bcdDevice,
  1408. (int)(desc.bNumConfigurations));
  1409. }
  1410. append(buf, tmp, off, len);
  1411. err = libusb_open(dev, &handle);
  1412. if (err) {
  1413. snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to open, err %d", (int)(*count), err);
  1414. append(buf, tmp, off, len);
  1415. return;
  1416. }
  1417. err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
  1418. if (err < 0)
  1419. snprintf((char *)man, sizeof(man), "** err:(%d) %s", err, libusb_error_name(err));
  1420. err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
  1421. if (err < 0)
  1422. snprintf((char *)prod, sizeof(prod), "** err:(%d) %s", err, libusb_error_name(err));
  1423. if (level == 0) {
  1424. libusb_close(handle);
  1425. snprintf(tmp, sizeof(tmp), EOL " Manufacturer: '%s'" EOL " Product: '%s'", man, prod);
  1426. append(buf, tmp, off, len);
  1427. return;
  1428. }
  1429. if (libusb_kernel_driver_active(handle, 0) == 1) {
  1430. snprintf(tmp, sizeof(tmp), EOL " * dev %d: kernel attached", (int)(*count));
  1431. append(buf, tmp, off, len);
  1432. }
  1433. err = libusb_get_active_config_descriptor(dev, &config);
  1434. if (err) {
  1435. if (!setgetdes(*count, dev, handle, &config, 1, buf, off, len)
  1436. && !setgetdes(*count, dev, handle, &config, 0, buf, off, len)) {
  1437. libusb_close(handle);
  1438. snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to set config descriptor to %d or %d",
  1439. (int)(*count), 1, 0);
  1440. append(buf, tmp, off, len);
  1441. return;
  1442. }
  1443. }
  1444. snprintf(tmp, sizeof(tmp), EOL " dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
  1445. "\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
  1446. "\tAttributes: %d" EOL "\tMaxPower: %d",
  1447. (int)(*count), destype(config->bDescriptorType),
  1448. (int)(config->bNumInterfaces), (int)(config->iConfiguration),
  1449. (int)(config->bmAttributes), (int)(config->MaxPower));
  1450. append(buf, tmp, off, len);
  1451. for (i = 0; i < (int)(config->bNumInterfaces); i++) {
  1452. for (j = 0; j < config->interface[i].num_altsetting; j++) {
  1453. idesc = &(config->interface[i].altsetting[j]);
  1454. snprintf(tmp, sizeof(tmp), EOL " _dev %d: Interface Descriptor %d:" EOL
  1455. "\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
  1456. "\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
  1457. "\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
  1458. (int)(*count), j, destype(idesc->bDescriptorType),
  1459. (int)(idesc->bInterfaceNumber),
  1460. (int)(idesc->bNumEndpoints),
  1461. (int)(idesc->bInterfaceClass),
  1462. (int)(idesc->bInterfaceSubClass),
  1463. (int)(idesc->bInterfaceProtocol));
  1464. append(buf, tmp, off, len);
  1465. for (k = 0; k < (int)(idesc->bNumEndpoints); k++) {
  1466. epdesc = &(idesc->endpoint[k]);
  1467. snprintf(tmp, sizeof(tmp), EOL " __dev %d: Interface %d Endpoint %d:" EOL
  1468. "\tDescriptorType: %s" EOL
  1469. "\tEndpointAddress: %s0x%x" EOL
  1470. "\tAttributes: %s" EOL "\tMaxPacketSize: %d" EOL
  1471. "\tInterval: %d" EOL "\tRefresh: %d",
  1472. (int)(*count), (int)(idesc->bInterfaceNumber), k,
  1473. destype(epdesc->bDescriptorType),
  1474. epdir(epdesc->bEndpointAddress),
  1475. (int)(epdesc->bEndpointAddress),
  1476. epatt(epdesc->bmAttributes),
  1477. epdesc->wMaxPacketSize,
  1478. (int)(epdesc->bInterval),
  1479. (int)(epdesc->bRefresh));
  1480. append(buf, tmp, off, len);
  1481. }
  1482. }
  1483. }
  1484. libusb_free_config_descriptor(config);
  1485. config = NULL;
  1486. err = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, ser, STRBUFLEN);
  1487. if (err < 0)
  1488. snprintf((char *)ser, sizeof(ser), "** err:(%d) %s", err, libusb_error_name(err));
  1489. snprintf(tmp, sizeof(tmp), EOL " dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
  1490. "\tProduct: '%s'" EOL "\tSerial '%s'",
  1491. (int)(*count), man, prod, ser);
  1492. append(buf, tmp, off, len);
  1493. libusb_close(handle);
  1494. }
  1495. // Function to dump all USB devices
  1496. void usb_all(int level)
  1497. {
  1498. libusb_device **list;
  1499. ssize_t count, i, j;
  1500. char *buf;
  1501. size_t len, off;
  1502. count = libusb_get_device_list(NULL, &list);
  1503. if (count < 0) {
  1504. applog(LOG_ERR, "USB all: failed, err:(%d) %s", (int)count, libusb_error_name((int)count));
  1505. return;
  1506. }
  1507. if (count == 0)
  1508. applog(LOG_WARNING, "USB all: found no devices");
  1509. else
  1510. {
  1511. len = 10000;
  1512. buf = malloc(len+1);
  1513. if (unlikely(!buf))
  1514. quit(1, "USB failed to malloc buf in usb_all");
  1515. sprintf(buf, "USB all: found %d devices", (int)count);
  1516. off = strlen(buf);
  1517. if (!opt_usb_list_all)
  1518. append(&buf, " - listing known devices", &off, &len);
  1519. j = -1;
  1520. for (i = 0; i < count; i++)
  1521. usb_full(&j, list[i], &buf, &off, &len, level);
  1522. _applog(LOG_WARNING, buf, false);
  1523. free(buf);
  1524. if (j == -1)
  1525. applog(LOG_WARNING, "No known USB devices");
  1526. else
  1527. applog(LOG_WARNING, "%d %sUSB devices",
  1528. (int)(++j), opt_usb_list_all ? BLANK : "known ");
  1529. }
  1530. libusb_free_device_list(list, 1);
  1531. }
  1532. static void cgusb_check_init()
  1533. {
  1534. mutex_lock(&cgusb_lock);
  1535. if (stats_initialised == false) {
  1536. // N.B. environment LIBUSB_DEBUG also sets libusb_set_debug()
  1537. if (opt_usbdump >= 0) {
  1538. #if LIBUSB_API_VERSION >= 0x01000106
  1539. libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, opt_usbdump);
  1540. #else
  1541. libusb_set_debug(NULL, opt_usbdump);
  1542. #endif
  1543. usb_all(opt_usbdump);
  1544. }
  1545. stats_initialised = true;
  1546. }
  1547. mutex_unlock(&cgusb_lock);
  1548. }
  1549. const char *usb_cmdname(enum usb_cmds cmd)
  1550. {
  1551. cgusb_check_init();
  1552. return usb_commands[cmd];
  1553. }
  1554. void usb_applog(struct cgpu_info *cgpu, enum usb_cmds cmd, char *msg, int amount, int err)
  1555. {
  1556. if (msg && !*msg)
  1557. msg = NULL;
  1558. if (!msg && amount == 0 && err == LIBUSB_SUCCESS)
  1559. msg = (char *)nodatareturned;
  1560. applog(LOG_ERR, "%s%i: %s failed%s%s (err=%d amt=%d)",
  1561. cgpu->drv->name, cgpu->device_id,
  1562. usb_cmdname(cmd),
  1563. msg ? space : BLANK, msg ? msg : BLANK,
  1564. err, amount);
  1565. }
  1566. #ifdef WIN32
  1567. static void in_use_store_ress(uint8_t bus_number, uint8_t device_address, void *resource1, void *resource2)
  1568. {
  1569. struct usb_in_use_list *in_use_tmp;
  1570. bool found = false, empty = true;
  1571. mutex_lock(&cgusb_lock);
  1572. in_use_tmp = in_use_head;
  1573. while (in_use_tmp) {
  1574. if (in_use_tmp->in_use.bus_number == (int)bus_number &&
  1575. in_use_tmp->in_use.device_address == (int)device_address) {
  1576. found = true;
  1577. if (in_use_tmp->in_use.resource1)
  1578. empty = false;
  1579. in_use_tmp->in_use.resource1 = resource1;
  1580. if (in_use_tmp->in_use.resource2)
  1581. empty = false;
  1582. in_use_tmp->in_use.resource2 = resource2;
  1583. break;
  1584. }
  1585. in_use_tmp = in_use_tmp->next;
  1586. }
  1587. mutex_unlock(&cgusb_lock);
  1588. if (found == false)
  1589. applog(LOG_ERR, "FAIL: USB store_ress not found (%d:%d)",
  1590. (int)bus_number, (int)device_address);
  1591. if (empty == false)
  1592. applog(LOG_ERR, "FAIL: USB store_ress not empty (%d:%d)",
  1593. (int)bus_number, (int)device_address);
  1594. }
  1595. static void in_use_get_ress(uint8_t bus_number, uint8_t device_address, void **resource1, void **resource2)
  1596. {
  1597. struct usb_in_use_list *in_use_tmp;
  1598. bool found = false, empty = false;
  1599. mutex_lock(&cgusb_lock);
  1600. in_use_tmp = in_use_head;
  1601. while (in_use_tmp) {
  1602. if (in_use_tmp->in_use.bus_number == (int)bus_number &&
  1603. in_use_tmp->in_use.device_address == (int)device_address) {
  1604. found = true;
  1605. if (!in_use_tmp->in_use.resource1)
  1606. empty = true;
  1607. *resource1 = in_use_tmp->in_use.resource1;
  1608. in_use_tmp->in_use.resource1 = NULL;
  1609. if (!in_use_tmp->in_use.resource2)
  1610. empty = true;
  1611. *resource2 = in_use_tmp->in_use.resource2;
  1612. in_use_tmp->in_use.resource2 = NULL;
  1613. break;
  1614. }
  1615. in_use_tmp = in_use_tmp->next;
  1616. }
  1617. mutex_unlock(&cgusb_lock);
  1618. if (found == false)
  1619. applog(LOG_ERR, "FAIL: USB get_lock not found (%d:%d)",
  1620. (int)bus_number, (int)device_address);
  1621. if (empty == true)
  1622. applog(LOG_ERR, "FAIL: USB get_lock empty (%d:%d)",
  1623. (int)bus_number, (int)device_address);
  1624. }
  1625. #else
  1626. static void in_use_store_fd(uint8_t bus_number, uint8_t device_address, int fd)
  1627. {
  1628. struct usb_in_use_list *in_use_tmp;
  1629. bool found = false;
  1630. mutex_lock(&cgusb_lock);
  1631. in_use_tmp = in_use_head;
  1632. while (in_use_tmp) {
  1633. if (in_use_tmp->in_use.bus_number == (int)bus_number &&
  1634. in_use_tmp->in_use.device_address == (int)device_address) {
  1635. found = true;
  1636. in_use_tmp->in_use.fd = fd;
  1637. break;
  1638. }
  1639. in_use_tmp = in_use_tmp->next;
  1640. }
  1641. mutex_unlock(&cgusb_lock);
  1642. if (found == false) {
  1643. applog(LOG_ERR, "FAIL: USB store_fd not found (%d:%d)",
  1644. (int)bus_number, (int)device_address);
  1645. }
  1646. }
  1647. static int in_use_get_fd(uint8_t bus_number, uint8_t device_address)
  1648. {
  1649. struct usb_in_use_list *in_use_tmp;
  1650. bool found = false;
  1651. int fd = -1;
  1652. mutex_lock(&cgusb_lock);
  1653. in_use_tmp = in_use_head;
  1654. while (in_use_tmp) {
  1655. if (in_use_tmp->in_use.bus_number == (int)bus_number &&
  1656. in_use_tmp->in_use.device_address == (int)device_address) {
  1657. found = true;
  1658. fd = in_use_tmp->in_use.fd;
  1659. break;
  1660. }
  1661. in_use_tmp = in_use_tmp->next;
  1662. }
  1663. mutex_unlock(&cgusb_lock);
  1664. if (found == false) {
  1665. applog(LOG_ERR, "FAIL: USB get_lock not found (%d:%d)",
  1666. (int)bus_number, (int)device_address);
  1667. }
  1668. return fd;
  1669. }
  1670. #endif
  1671. static bool _in_use(struct usb_in_use_list *head, uint8_t bus_number,
  1672. uint8_t device_address)
  1673. {
  1674. struct usb_in_use_list *in_use_tmp;
  1675. bool ret = false;
  1676. in_use_tmp = head;
  1677. while (in_use_tmp) {
  1678. if (in_use_tmp->in_use.bus_number == (int)bus_number &&
  1679. in_use_tmp->in_use.device_address == (int)device_address) {
  1680. ret = true;
  1681. break;
  1682. }
  1683. in_use_tmp = in_use_tmp->next;
  1684. if (in_use_tmp == head)
  1685. break;
  1686. }
  1687. return ret;
  1688. }
  1689. static bool __is_in_use(uint8_t bus_number, uint8_t device_address)
  1690. {
  1691. if (_in_use(in_use_head, bus_number, device_address))
  1692. return true;
  1693. if (_in_use(blacklist_head, bus_number, device_address))
  1694. return true;
  1695. return false;
  1696. }
  1697. static bool is_in_use_bd(uint8_t bus_number, uint8_t device_address)
  1698. {
  1699. bool ret;
  1700. mutex_lock(&cgusb_lock);
  1701. ret = __is_in_use(bus_number, device_address);
  1702. mutex_unlock(&cgusb_lock);
  1703. return ret;
  1704. }
  1705. static bool is_in_use(libusb_device *dev)
  1706. {
  1707. return is_in_use_bd(libusb_get_bus_number(dev), libusb_get_device_address(dev));
  1708. }
  1709. static bool how_in_use(uint8_t bus_number, uint8_t device_address, bool *blacklisted)
  1710. {
  1711. bool ret;
  1712. mutex_lock(&cgusb_lock);
  1713. ret = _in_use(in_use_head, bus_number, device_address);
  1714. if (!ret) {
  1715. if (_in_use(blacklist_head, bus_number, device_address))
  1716. *blacklisted = true;
  1717. }
  1718. mutex_unlock(&cgusb_lock);
  1719. return ret;
  1720. }
  1721. void usb_list(void)
  1722. {
  1723. struct libusb_device_descriptor desc;
  1724. struct libusb_device_handle *handle;
  1725. uint8_t bus_number;
  1726. uint8_t device_address;
  1727. libusb_device **list;
  1728. ssize_t count, i, j;
  1729. int err, total = 0;
  1730. count = libusb_get_device_list(NULL, &list);
  1731. if (count < 0) {
  1732. applog(LOG_ERR, "USB list: failed, err:(%d) %s", (int)count, libusb_error_name((int)count));
  1733. return;
  1734. }
  1735. if (count == 0) {
  1736. applog(LOG_WARNING, "USB list: found no devices");
  1737. return;
  1738. }
  1739. for (i = 0; i < count; i++) {
  1740. bool known = false, blacklisted = false, active;
  1741. unsigned char manuf[256], prod[256];
  1742. libusb_device *dev = list[i];
  1743. err = libusb_get_device_descriptor(dev, &desc);
  1744. if (err) {
  1745. applog(LOG_WARNING, "USB list: Failed to get descriptor %d", (int)i);
  1746. break;
  1747. }
  1748. bus_number = libusb_get_bus_number(dev);
  1749. device_address = libusb_get_device_address(dev);
  1750. for (j = 0; find_dev[j].drv != DRIVER_MAX; j++) {
  1751. if ((find_dev[j].idVendor == desc.idVendor) &&
  1752. (find_dev[j].idProduct == desc.idProduct)) {
  1753. known = true;
  1754. break;
  1755. }
  1756. }
  1757. if (!known)
  1758. continue;
  1759. err = libusb_open(dev, &handle);
  1760. if (err) {
  1761. applog(LOG_WARNING, "USB list: Failed to open %d", (int)i);
  1762. break;
  1763. }
  1764. libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, manuf, 255);
  1765. libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, 255);
  1766. total++;
  1767. active = how_in_use(bus_number, device_address, &blacklisted);
  1768. simplelog(LOG_WARNING, "Bus %u Device %u ID: %04x:%04x %s %s %sactive %s",
  1769. bus_number, device_address, desc.idVendor, desc.idProduct,
  1770. manuf, prod, active ? "" : "in", blacklisted ? "blacklisted" : "");
  1771. }
  1772. libusb_free_device_list(list, 1);
  1773. simplelog(LOG_WARNING, "%d total known USB device%s", total, total > 1 ? "s": "");
  1774. }
  1775. static void add_in_use(uint8_t bus_number, uint8_t device_address, bool blacklist)
  1776. {
  1777. struct usb_in_use_list *in_use_tmp, **head;
  1778. bool found = false;
  1779. mutex_lock(&cgusb_lock);
  1780. if (unlikely(!blacklist && __is_in_use(bus_number, device_address))) {
  1781. found = true;
  1782. goto nofway;
  1783. }
  1784. if (blacklist)
  1785. head = &blacklist_head;
  1786. else
  1787. head = &in_use_head;
  1788. in_use_tmp = cgcalloc(1, sizeof(*in_use_tmp));
  1789. in_use_tmp->in_use.bus_number = (int)bus_number;
  1790. in_use_tmp->in_use.device_address = (int)device_address;
  1791. in_use_tmp->next = in_use_head;
  1792. if (*head)
  1793. (*head)->prev = in_use_tmp;
  1794. *head = in_use_tmp;
  1795. nofway:
  1796. mutex_unlock(&cgusb_lock);
  1797. if (found)
  1798. applog(LOG_ERR, "FAIL: USB add already in use (%d:%d)",
  1799. (int)bus_number, (int)device_address);
  1800. }
  1801. static void __remove_in_use(uint8_t bus_number, uint8_t device_address, bool blacklist)
  1802. {
  1803. struct usb_in_use_list *in_use_tmp, **head;
  1804. bool found = false;
  1805. mutex_lock(&cgusb_lock);
  1806. if (blacklist)
  1807. head = &blacklist_head;
  1808. else
  1809. head = &in_use_head;
  1810. in_use_tmp = *head;
  1811. while (in_use_tmp) {
  1812. if (in_use_tmp->in_use.bus_number == (int)bus_number &&
  1813. in_use_tmp->in_use.device_address == (int)device_address) {
  1814. found = true;
  1815. if (in_use_tmp == *head) {
  1816. *head = (*head)->next;
  1817. if (*head)
  1818. (*head)->prev = NULL;
  1819. } else {
  1820. in_use_tmp->prev->next = in_use_tmp->next;
  1821. if (in_use_tmp->next)
  1822. in_use_tmp->next->prev = in_use_tmp->prev;
  1823. }
  1824. free(in_use_tmp);
  1825. break;
  1826. }
  1827. in_use_tmp = in_use_tmp->next;
  1828. if (in_use_tmp == *head)
  1829. break;
  1830. }
  1831. mutex_unlock(&cgusb_lock);
  1832. if (!found) {
  1833. applog(LOG_ERR, "FAIL: USB remove not already in use (%d:%d)",
  1834. (int)bus_number, (int)device_address);
  1835. }
  1836. }
  1837. static void remove_in_use(uint8_t bus_number, uint8_t device_address)
  1838. {
  1839. __remove_in_use(bus_number, device_address, false);
  1840. }
  1841. static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
  1842. {
  1843. struct resource_work *res_work;
  1844. bool ret;
  1845. applog(LOG_DEBUG, "USB lock %s %d-%d", drv->dname, (int)bus_number, (int)device_address);
  1846. res_work = cgcalloc(1, sizeof(*res_work));
  1847. res_work->lock = true;
  1848. res_work->dname = (const char *)(drv->dname);
  1849. res_work->bus_number = bus_number;
  1850. res_work->device_address = device_address;
  1851. mutex_lock(&cgusbres_lock);
  1852. res_work->next = res_work_head;
  1853. res_work_head = res_work;
  1854. mutex_unlock(&cgusbres_lock);
  1855. cgsem_post(&usb_resource_sem);
  1856. // TODO: add a timeout fail - restart the resource thread?
  1857. while (true) {
  1858. cgsleep_ms(50);
  1859. mutex_lock(&cgusbres_lock);
  1860. if (res_reply_head) {
  1861. struct resource_reply *res_reply_prev = NULL;
  1862. struct resource_reply *res_reply = res_reply_head;
  1863. while (res_reply) {
  1864. if (res_reply->bus_number == bus_number &&
  1865. res_reply->device_address == device_address) {
  1866. if (res_reply_prev)
  1867. res_reply_prev->next = res_reply->next;
  1868. else
  1869. res_reply_head = res_reply->next;
  1870. mutex_unlock(&cgusbres_lock);
  1871. ret = res_reply->got;
  1872. free(res_reply);
  1873. return ret;
  1874. }
  1875. res_reply_prev = res_reply;
  1876. res_reply = res_reply->next;
  1877. }
  1878. }
  1879. mutex_unlock(&cgusbres_lock);
  1880. }
  1881. }
  1882. static bool cgminer_usb_lock(struct device_drv *drv, libusb_device *dev)
  1883. {
  1884. return cgminer_usb_lock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  1885. }
  1886. static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
  1887. {
  1888. struct resource_work *res_work;
  1889. applog(LOG_DEBUG, "USB unlock %s %d-%d", drv->dname, (int)bus_number, (int)device_address);
  1890. res_work = cgcalloc(1, sizeof(*res_work));
  1891. res_work->lock = false;
  1892. res_work->dname = (const char *)(drv->dname);
  1893. res_work->bus_number = bus_number;
  1894. res_work->device_address = device_address;
  1895. mutex_lock(&cgusbres_lock);
  1896. res_work->next = res_work_head;
  1897. res_work_head = res_work;
  1898. mutex_unlock(&cgusbres_lock);
  1899. cgsem_post(&usb_resource_sem);
  1900. return;
  1901. }
  1902. static void cgminer_usb_unlock(struct device_drv *drv, libusb_device *dev)
  1903. {
  1904. cgminer_usb_unlock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
  1905. }
  1906. static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
  1907. {
  1908. applog(LOG_DEBUG, "USB free %s", cgusb->found->name);
  1909. if (cgusb->serial_string && cgusb->serial_string != BLANK)
  1910. free(cgusb->serial_string);
  1911. if (cgusb->manuf_string && cgusb->manuf_string != BLANK)
  1912. free(cgusb->manuf_string);
  1913. if (cgusb->prod_string && cgusb->prod_string != BLANK)
  1914. free(cgusb->prod_string);
  1915. if (cgusb->descriptor)
  1916. free(cgusb->descriptor);
  1917. free(cgusb->found);
  1918. free(cgusb);
  1919. return NULL;
  1920. }
  1921. static void _usb_uninit(struct cgpu_info *cgpu)
  1922. {
  1923. int ifinfo;
  1924. // May have happened already during a failed initialisation
  1925. // if release_cgpu() was called due to a USB NODEV(err)
  1926. if (!cgpu->usbdev)
  1927. return;
  1928. applog(LOG_DEBUG, "USB uninit %s%i",
  1929. cgpu->drv->name, cgpu->device_id);
  1930. if (cgpu->usbdev->handle) {
  1931. for (ifinfo = cgpu->usbdev->found->intinfo_count - 1; ifinfo >= 0; ifinfo--) {
  1932. libusb_release_interface(cgpu->usbdev->handle,
  1933. THISIF(cgpu->usbdev->found, ifinfo));
  1934. #ifdef LINUX
  1935. libusb_attach_kernel_driver(cgpu->usbdev->handle, THISIF(cgpu->usbdev->found, ifinfo));
  1936. #endif
  1937. }
  1938. cg_wlock(&cgusb_fd_lock);
  1939. libusb_close(cgpu->usbdev->handle);
  1940. cgpu->usbdev->handle = NULL;
  1941. cg_wunlock(&cgusb_fd_lock);
  1942. }
  1943. cgpu->usbdev = free_cgusb(cgpu->usbdev);
  1944. }
  1945. void usb_uninit(struct cgpu_info *cgpu)
  1946. {
  1947. int pstate;
  1948. DEVWLOCK(cgpu, pstate);
  1949. _usb_uninit(cgpu);
  1950. DEVWUNLOCK(cgpu, pstate);
  1951. }
  1952. /* We have dropped the read devlock before entering this function but we pick
  1953. * up the write lock to prevent any attempts to work on dereferenced code once
  1954. * the nodev flag has been set. */
  1955. static bool __release_cgpu(struct cgpu_info *cgpu)
  1956. {
  1957. struct cg_usb_device *cgusb = cgpu->usbdev;
  1958. bool initted = cgpu->usbinfo.initialised;
  1959. struct cgpu_info *lookcgpu;
  1960. int i;
  1961. // It has already been done
  1962. if (cgpu->usbinfo.nodev)
  1963. return false;
  1964. applog(LOG_DEBUG, "USB release %s%i",
  1965. cgpu->drv->name, cgpu->device_id);
  1966. if (initted) {
  1967. zombie_devs++;
  1968. total_count--;
  1969. drv_count[cgpu->drv->drv_id].count--;
  1970. }
  1971. cgpu->usbinfo.nodev = true;
  1972. cgpu->usbinfo.nodev_count++;
  1973. cgtime(&cgpu->usbinfo.last_nodev);
  1974. // Any devices sharing the same USB device should be marked also
  1975. for (i = 0; i < total_devices; i++) {
  1976. lookcgpu = get_devices(i);
  1977. if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
  1978. if (initted) {
  1979. total_count--;
  1980. drv_count[lookcgpu->drv->drv_id].count--;
  1981. }
  1982. lookcgpu->usbinfo.nodev = true;
  1983. lookcgpu->usbinfo.nodev_count++;
  1984. cg_memcpy(&(lookcgpu->usbinfo.last_nodev),
  1985. &(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
  1986. lookcgpu->usbdev = NULL;
  1987. }
  1988. }
  1989. _usb_uninit(cgpu);
  1990. return true;
  1991. }
  1992. static void release_cgpu(struct cgpu_info *cgpu)
  1993. {
  1994. if (__release_cgpu(cgpu))
  1995. cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  1996. }
  1997. void blacklist_cgpu(struct cgpu_info *cgpu)
  1998. {
  1999. if (cgpu->blacklisted) {
  2000. applog(LOG_WARNING, "Device already blacklisted");
  2001. return;
  2002. }
  2003. cgpu->blacklisted = true;
  2004. add_in_use(cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address, true);
  2005. if (__release_cgpu(cgpu))
  2006. cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
  2007. }
  2008. void whitelist_cgpu(struct cgpu_info *cgpu)
  2009. {
  2010. if (!cgpu->blacklisted) {
  2011. applog(LOG_WARNING, "Device not blacklisted");
  2012. return;
  2013. }
  2014. __remove_in_use(cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address, true);
  2015. cgpu->blacklisted = false;
  2016. }
  2017. /*
  2018. * Force a NODEV on a device so it goes back to hotplug
  2019. */
  2020. void usb_nodev(struct cgpu_info *cgpu)
  2021. {
  2022. int pstate;
  2023. DEVWLOCK(cgpu, pstate);
  2024. release_cgpu(cgpu);
  2025. DEVWUNLOCK(cgpu, pstate);
  2026. }
  2027. /*
  2028. * Use the same usbdev thus locking is across all related devices
  2029. */
  2030. struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig)
  2031. {
  2032. struct cgpu_info *copy;
  2033. int pstate;
  2034. DEVWLOCK(orig, pstate);
  2035. copy = cgcalloc(1, sizeof(*copy));
  2036. copy->name = orig->name;
  2037. copy->drv = copy_drv(orig->drv);
  2038. copy->deven = orig->deven;
  2039. copy->threads = orig->threads;
  2040. copy->usbdev = orig->usbdev;
  2041. cg_memcpy(&(copy->usbinfo), &(orig->usbinfo), sizeof(copy->usbinfo));
  2042. copy->usbinfo.nodev = (copy->usbdev == NULL);
  2043. DEVWUNLOCK(orig, pstate);
  2044. return copy;
  2045. }
  2046. struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads)
  2047. {
  2048. struct cgpu_info *cgpu = cgcalloc(1, sizeof(*cgpu));
  2049. cgpu->drv = drv;
  2050. cgpu->deven = DEV_ENABLED;
  2051. cgpu->threads = threads;
  2052. cgpu->usbinfo.nodev = true;
  2053. cglock_init(&cgpu->usbinfo.devlock);
  2054. return cgpu;
  2055. }
  2056. struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu)
  2057. {
  2058. if (cgpu->drv->copy)
  2059. free(cgpu->drv);
  2060. free(cgpu->device_path);
  2061. free(cgpu);
  2062. return NULL;
  2063. }
  2064. #define USB_INIT_FAIL 0
  2065. #define USB_INIT_OK 1
  2066. #define USB_INIT_IGNORE 2
  2067. static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
  2068. {
  2069. unsigned char man[STRBUFLEN+1], prod[STRBUFLEN+1];
  2070. struct cg_usb_device *cgusb = NULL;
  2071. struct libusb_config_descriptor *config = NULL;
  2072. const struct libusb_interface_descriptor *idesc;
  2073. const struct libusb_endpoint_descriptor *epdesc;
  2074. unsigned char strbuf[STRBUFLEN+1];
  2075. char devpath[32];
  2076. char devstr[STRBUFLEN+1];
  2077. int err, ifinfo, epinfo, alt, epnum, pstate;
  2078. int bad = USB_INIT_FAIL;
  2079. int cfg, claimed = 0, i;
  2080. DEVWLOCK(cgpu, pstate);
  2081. cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
  2082. cgpu->usbinfo.device_address = libusb_get_device_address(dev);
  2083. if (found->intinfo_count > 1) {
  2084. snprintf(devpath, sizeof(devpath), "%d:%d-i%d",
  2085. (int)(cgpu->usbinfo.bus_number),
  2086. (int)(cgpu->usbinfo.device_address),
  2087. THISIF(found, 0));
  2088. } else {
  2089. snprintf(devpath, sizeof(devpath), "%d:%d",
  2090. (int)(cgpu->usbinfo.bus_number),
  2091. (int)(cgpu->usbinfo.device_address));
  2092. }
  2093. cgpu->device_path = strdup(devpath);
  2094. snprintf(devstr, sizeof(devstr), "- %s device %s", found->name, devpath);
  2095. cgusb = cgcalloc(1, sizeof(*cgusb));
  2096. cgusb->found = found;
  2097. if (found->idVendor == IDVENDOR_FTDI)
  2098. cgusb->usb_type = USB_TYPE_FTDI;
  2099. cgusb->ident = found->ident;
  2100. cgusb->descriptor = cgcalloc(1, sizeof(*(cgusb->descriptor)));
  2101. err = libusb_get_device_descriptor(dev, cgusb->descriptor);
  2102. if (err) {
  2103. applog(LOG_DEBUG,
  2104. "USB init failed to get descriptor, err %d %s",
  2105. err, devstr);
  2106. goto dame;
  2107. }
  2108. cg_wlock(&cgusb_fd_lock);
  2109. err = libusb_open(dev, &(cgusb->handle));
  2110. cg_wunlock(&cgusb_fd_lock);
  2111. if (err) {
  2112. switch (err) {
  2113. case LIBUSB_ERROR_ACCESS:
  2114. applog(LOG_ERR,
  2115. "USB init, open device failed, err %d, "
  2116. "you don't have privilege to access %s",
  2117. err, devstr);
  2118. applog(LOG_ERR, "See README file included for help");
  2119. break;
  2120. #ifdef WIN32
  2121. // Windows specific message
  2122. case LIBUSB_ERROR_NOT_SUPPORTED:
  2123. applog(LOG_ERR, "USB init, open device failed, err %d, ", err);
  2124. applog(LOG_ERR, "You need to install a WinUSB driver for %s", devstr);
  2125. applog(LOG_ERR, "And associate %s with WinUSB using zadig", devstr);
  2126. applog(LOG_ERR, "See README.txt file included for help");
  2127. break;
  2128. #endif
  2129. default:
  2130. applog(LOG_DEBUG,
  2131. "USB init, open failed, err %d %s",
  2132. err, devstr);
  2133. }
  2134. goto dame;
  2135. }
  2136. #ifdef LINUX
  2137. for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++) {
  2138. if (libusb_kernel_driver_active(cgusb->handle, THISIF(found, ifinfo)) == 1) {
  2139. applog(LOG_DEBUG, "USB init, kernel attached ... %s", devstr);
  2140. err = libusb_detach_kernel_driver(cgusb->handle, THISIF(found, ifinfo));
  2141. if (err == 0) {
  2142. applog(LOG_DEBUG,
  2143. "USB init, kernel detached ifinfo %d interface %d"
  2144. " successfully %s",
  2145. ifinfo, THISIF(found, ifinfo), devstr);
  2146. } else {
  2147. applog(LOG_WARNING,
  2148. "USB init, kernel detach ifinfo %d interface %d failed,"
  2149. " err %d in use? %s",
  2150. ifinfo, THISIF(found, ifinfo), err, devstr);
  2151. goto nokernel;
  2152. }
  2153. }
  2154. }
  2155. #endif
  2156. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  2157. cgusb->descriptor->iManufacturer,
  2158. man, STRBUFLEN);
  2159. if (err < 0) {
  2160. applog(LOG_DEBUG,
  2161. "USB init, failed to get iManufacturer, err %d %s",
  2162. err, devstr);
  2163. goto cldame;
  2164. }
  2165. if (found->iManufacturer) {
  2166. if (strcmp((char *)man, found->iManufacturer)) {
  2167. applog(LOG_DEBUG, "USB init, iManufacturer mismatch %s",
  2168. devstr);
  2169. applog(LOG_DEBUG, "Found %s vs %s", man, found->iManufacturer);
  2170. bad = USB_INIT_IGNORE;
  2171. goto cldame;
  2172. }
  2173. } else {
  2174. for (i = 0; find_dev[i].drv != DRIVER_MAX; i++) {
  2175. const char *iManufacturer = find_dev[i].iManufacturer;
  2176. /* If other drivers has an iManufacturer set that match,
  2177. * don't try to claim this device. */
  2178. if (!iManufacturer)
  2179. continue;
  2180. /* If the alternative driver also has an iProduct, only
  2181. * use that for comparison. */
  2182. if (find_dev[i].iProduct)
  2183. continue;
  2184. if (!strcmp((char *)man, iManufacturer)) {
  2185. applog(LOG_DEBUG, "USB init, alternative iManufacturer match %s",
  2186. devstr);
  2187. applog(LOG_DEBUG, "Found %s", iManufacturer);
  2188. bad = USB_INIT_IGNORE;
  2189. goto cldame;
  2190. }
  2191. }
  2192. }
  2193. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  2194. cgusb->descriptor->iProduct,
  2195. prod, STRBUFLEN);
  2196. if (err < 0) {
  2197. applog(LOG_DEBUG,
  2198. "USB init, failed to get iProduct, err %d %s",
  2199. err, devstr);
  2200. goto cldame;
  2201. }
  2202. if (found->iProduct) {
  2203. if (strcasecmp((char *)prod, found->iProduct)) {
  2204. applog(LOG_DEBUG, "USB init, iProduct mismatch %s",
  2205. devstr);
  2206. applog(LOG_DEBUG, "Found %s vs %s", prod, found->iProduct);
  2207. bad = USB_INIT_IGNORE;
  2208. goto cldame;
  2209. }
  2210. } else {
  2211. for (i = 0; find_dev[i].drv != DRIVER_MAX; i++) {
  2212. const char *iProduct = find_dev[i].iProduct;
  2213. /* Do same for iProduct as iManufacturer above */
  2214. if (!iProduct)
  2215. continue;
  2216. if (!strcasecmp((char *)prod, iProduct)) {
  2217. applog(LOG_DEBUG, "USB init, alternative iProduct match %s",
  2218. devstr);
  2219. applog(LOG_DEBUG, "Found %s", iProduct);
  2220. bad = USB_INIT_IGNORE;
  2221. goto cldame;
  2222. }
  2223. }
  2224. }
  2225. cfg = -1;
  2226. err = libusb_get_configuration(cgusb->handle, &cfg);
  2227. if (err)
  2228. cfg = -1;
  2229. // Try to set it if we can't read it or it's different
  2230. if (cfg != found->config) {
  2231. err = libusb_set_configuration(cgusb->handle, found->config);
  2232. if (err) {
  2233. switch(err) {
  2234. case LIBUSB_ERROR_BUSY:
  2235. applog(LOG_WARNING,
  2236. "USB init, set config %d in use %s",
  2237. found->config, devstr);
  2238. break;
  2239. default:
  2240. applog(LOG_DEBUG,
  2241. "USB init, failed to set config to %d, err %d %s",
  2242. found->config, err, devstr);
  2243. }
  2244. goto cldame;
  2245. }
  2246. }
  2247. err = libusb_get_active_config_descriptor(dev, &config);
  2248. if (err) {
  2249. applog(LOG_DEBUG,
  2250. "USB init, failed to get config descriptor, err %d %s",
  2251. err, devstr);
  2252. goto cldame;
  2253. }
  2254. int imax = -1;
  2255. for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++)
  2256. if (found->intinfos[ifinfo].interface > imax)
  2257. imax = found->intinfos[ifinfo].interface;
  2258. if ((int)(config->bNumInterfaces) <= imax) {
  2259. applog(LOG_DEBUG, "USB init bNumInterfaces %d <= interface max %d for %s",
  2260. (int)(config->bNumInterfaces), imax, devstr);
  2261. goto cldame;
  2262. }
  2263. for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++)
  2264. for (epinfo = 0; epinfo < found->intinfos[ifinfo].epinfo_count; epinfo++)
  2265. found->intinfos[ifinfo].epinfos[epinfo].found = false;
  2266. for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++) {
  2267. int interface = found->intinfos[ifinfo].interface;
  2268. for (alt = 0; alt < config->interface[interface].num_altsetting; alt++) {
  2269. idesc = &(config->interface[interface].altsetting[alt]);
  2270. for (epnum = 0; epnum < (int)(idesc->bNumEndpoints); epnum++) {
  2271. struct usb_epinfo *epinfos = found->intinfos[ifinfo].epinfos;
  2272. epdesc = &(idesc->endpoint[epnum]);
  2273. for (epinfo = 0; epinfo < found->intinfos[ifinfo].epinfo_count; epinfo++) {
  2274. if (!epinfos[epinfo].found) {
  2275. if (epdesc->bmAttributes == epinfos[epinfo].att
  2276. && epdesc->wMaxPacketSize >= epinfos[epinfo].size
  2277. && epdesc->bEndpointAddress == epinfos[epinfo].ep) {
  2278. epinfos[epinfo].found = true;
  2279. epinfos[epinfo].wMaxPacketSize = epdesc->wMaxPacketSize;
  2280. break;
  2281. }
  2282. }
  2283. }
  2284. }
  2285. }
  2286. }
  2287. for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++)
  2288. for (epinfo = 0; epinfo < found->intinfos[ifinfo].epinfo_count; epinfo++)
  2289. if (found->intinfos[ifinfo].epinfos[epinfo].found == false) {
  2290. applog(LOG_DEBUG, "USB init found (%d,%d) == false %s",
  2291. ifinfo, epinfo, devstr);
  2292. goto cldame;
  2293. }
  2294. claimed = 0;
  2295. for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++) {
  2296. err = libusb_claim_interface(cgusb->handle, THISIF(found, ifinfo));
  2297. if (err == 0)
  2298. claimed++;
  2299. else {
  2300. switch(err) {
  2301. case LIBUSB_ERROR_BUSY:
  2302. applog(LOG_WARNING,
  2303. "USB init, claim ifinfo %d interface %d in use %s",
  2304. ifinfo, THISIF(found, ifinfo), devstr);
  2305. break;
  2306. default:
  2307. applog(LOG_DEBUG,
  2308. "USB init, claim ifinfo %d interface %d failed,"
  2309. " err %d %s",
  2310. ifinfo, THISIF(found, ifinfo), err, devstr);
  2311. }
  2312. goto reldame;
  2313. }
  2314. }
  2315. cfg = -1;
  2316. err = libusb_get_configuration(cgusb->handle, &cfg);
  2317. if (err)
  2318. cfg = -1;
  2319. if (cfg != found->config) {
  2320. applog(LOG_WARNING,
  2321. "USB init, incorrect config (%d!=%d) after claim of %s",
  2322. cfg, found->config, devstr);
  2323. goto reldame;
  2324. }
  2325. cgusb->usbver = cgusb->descriptor->bcdUSB;
  2326. if (cgusb->usbver < 0x0200) {
  2327. cgusb->usb11 = true;
  2328. cgusb->tt = true;
  2329. }
  2330. // TODO: allow this with the right version of the libusb include and running library
  2331. // cgusb->speed = libusb_get_device_speed(dev);
  2332. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  2333. cgusb->descriptor->iProduct, strbuf, STRBUFLEN);
  2334. if (err > 0)
  2335. cgusb->prod_string = strdup((char *)strbuf);
  2336. else
  2337. cgusb->prod_string = (char *)BLANK;
  2338. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  2339. cgusb->descriptor->iManufacturer, strbuf, STRBUFLEN);
  2340. if (err > 0)
  2341. cgusb->manuf_string = strdup((char *)strbuf);
  2342. else
  2343. cgusb->manuf_string = (char *)BLANK;
  2344. err = libusb_get_string_descriptor_ascii(cgusb->handle,
  2345. cgusb->descriptor->iSerialNumber, strbuf, STRBUFLEN);
  2346. if (err > 0)
  2347. cgusb->serial_string = strdup((char *)strbuf);
  2348. else
  2349. cgusb->serial_string = (char *)BLANK;
  2350. // TODO: ?
  2351. // cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
  2352. // cgusb->interfaceVersion
  2353. applog(LOG_DEBUG,
  2354. "USB init %s usbver=%04x prod='%s' manuf='%s' serial='%s'",
  2355. devstr, cgusb->usbver, cgusb->prod_string,
  2356. cgusb->manuf_string, cgusb->serial_string);
  2357. cgpu->usbdev = cgusb;
  2358. cgpu->usbinfo.nodev = false;
  2359. libusb_free_config_descriptor(config);
  2360. // Allow a name change based on the idVendor+idProduct
  2361. // N.B. must be done before calling add_cgpu()
  2362. if (strcasecmp(cgpu->drv->name, found->name)) {
  2363. if (!cgpu->drv->copy)
  2364. cgpu->drv = copy_drv(cgpu->drv);
  2365. cgpu->drv->name = (char *)(found->name);
  2366. }
  2367. bad = USB_INIT_OK;
  2368. goto out_unlock;
  2369. reldame:
  2370. ifinfo = claimed;
  2371. while (ifinfo-- > 0)
  2372. libusb_release_interface(cgusb->handle, THISIF(found, ifinfo));
  2373. cldame:
  2374. #ifdef LINUX
  2375. libusb_attach_kernel_driver(cgusb->handle, THISIF(found, ifinfo));
  2376. nokernel:
  2377. #endif
  2378. cg_wlock(&cgusb_fd_lock);
  2379. libusb_close(cgusb->handle);
  2380. cgusb->handle = NULL;
  2381. cg_wunlock(&cgusb_fd_lock);
  2382. dame:
  2383. if (config)
  2384. libusb_free_config_descriptor(config);
  2385. cgusb = free_cgusb(cgusb);
  2386. out_unlock:
  2387. DEVWUNLOCK(cgpu, pstate);
  2388. return bad;
  2389. }
  2390. bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found_match)
  2391. {
  2392. struct usb_find_devices *found_use = NULL;
  2393. int uninitialised_var(ret);
  2394. int i;
  2395. for (i = 0; find_dev[i].drv != DRIVER_MAX; i++) {
  2396. if (find_dev[i].drv == found_match->drv &&
  2397. find_dev[i].idVendor == found_match->idVendor &&
  2398. find_dev[i].idProduct == found_match->idProduct) {
  2399. found_use = cgmalloc(sizeof(*found_use));
  2400. cg_memcpy(found_use, &(find_dev[i]), sizeof(*found_use));
  2401. ret = _usb_init(cgpu, dev, found_use);
  2402. if (ret != USB_INIT_IGNORE)
  2403. break;
  2404. }
  2405. }
  2406. if (ret == USB_INIT_FAIL) {
  2407. applog(LOG_ERR, "%s detect (%d:%d) failed to initialise (incorrect device?), resetting",
  2408. cgpu->drv->dname,
  2409. (int)(cgpu->usbinfo.bus_number),
  2410. (int)(cgpu->usbinfo.device_address));
  2411. if (cgpu->usbdev && cgpu->usbdev->handle)
  2412. libusb_reset_device(cgpu->usbdev->handle);
  2413. }
  2414. return (ret == USB_INIT_OK);
  2415. }
  2416. static bool usb_check_device(struct device_drv *drv, struct libusb_device *dev, struct usb_find_devices *look)
  2417. {
  2418. struct libusb_device_descriptor desc;
  2419. int bus_number, device_address;
  2420. int err, i;
  2421. bool ok;
  2422. err = libusb_get_device_descriptor(dev, &desc);
  2423. if (err) {
  2424. applog(LOG_DEBUG, "USB check device: Failed to get descriptor, err %d", err);
  2425. return false;
  2426. }
  2427. if (desc.idVendor != look->idVendor || desc.idProduct != look->idProduct) {
  2428. applog(LOG_DEBUG, "%s looking for %s %04x:%04x but found %04x:%04x instead",
  2429. drv->name, look->name, look->idVendor, look->idProduct, desc.idVendor, desc.idProduct);
  2430. return false;
  2431. }
  2432. if (busdev_count > 0) {
  2433. bus_number = (int)libusb_get_bus_number(dev);
  2434. device_address = (int)libusb_get_device_address(dev);
  2435. ok = false;
  2436. for (i = 0; i < busdev_count; i++) {
  2437. if (bus_number == busdev[i].bus_number) {
  2438. if (busdev[i].device_address == -1 ||
  2439. device_address == busdev[i].device_address) {
  2440. ok = true;
  2441. break;
  2442. }
  2443. }
  2444. }
  2445. if (!ok) {
  2446. applog(LOG_DEBUG, "%s rejected %s %04x:%04x with bus:dev (%d:%d)",
  2447. drv->name, look->name, look->idVendor, look->idProduct,
  2448. bus_number, device_address);
  2449. return false;
  2450. }
  2451. }
  2452. applog(LOG_DEBUG, "%s looking for and found %s %04x:%04x",
  2453. drv->name, look->name, look->idVendor, look->idProduct);
  2454. return true;
  2455. }
  2456. static struct usb_find_devices *usb_check_each(int drvnum, struct device_drv *drv, struct libusb_device *dev)
  2457. {
  2458. struct usb_find_devices *found;
  2459. int i;
  2460. for (i = 0; find_dev[i].drv != DRIVER_MAX; i++)
  2461. if (find_dev[i].drv == drvnum) {
  2462. if (usb_check_device(drv, dev, &(find_dev[i]))) {
  2463. found = cgmalloc(sizeof(*found));
  2464. cg_memcpy(found, &(find_dev[i]), sizeof(*found));
  2465. return found;
  2466. }
  2467. }
  2468. return NULL;
  2469. }
  2470. #define DRIVER_USB_CHECK_EACH(X) if (drv->drv_id == DRIVER_##X) \
  2471. return usb_check_each(DRIVER_##X, drv, dev);
  2472. static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv, __maybe_unused struct libusb_device *dev)
  2473. {
  2474. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  2475. applog(LOG_DEBUG,
  2476. "USB scan devices3: %s limit %d reached",
  2477. drv->dname, drv_count[drv->drv_id].limit);
  2478. return NULL;
  2479. }
  2480. DRIVER_PARSE_COMMANDS(DRIVER_USB_CHECK_EACH)
  2481. return NULL;
  2482. }
  2483. void __usb_detect(struct device_drv *drv, struct cgpu_info *(*device_detect)(struct libusb_device *, struct usb_find_devices *),
  2484. bool single)
  2485. {
  2486. libusb_device **list;
  2487. ssize_t count, i;
  2488. struct usb_find_devices *found;
  2489. struct cgpu_info *cgpu;
  2490. applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
  2491. if (total_count >= total_limit) {
  2492. applog(LOG_DEBUG, "USB scan devices: total limit %d reached", total_limit);
  2493. return;
  2494. }
  2495. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  2496. applog(LOG_DEBUG,
  2497. "USB scan devices: %s limit %d reached",
  2498. drv->dname, drv_count[drv->drv_id].limit);
  2499. return;
  2500. }
  2501. count = libusb_get_device_list(NULL, &list);
  2502. if (count < 0) {
  2503. applog(LOG_DEBUG, "USB scan devices: failed, err %d", (int)count);
  2504. return;
  2505. }
  2506. if (count == 0)
  2507. applog(LOG_DEBUG, "USB scan devices: found no devices");
  2508. else
  2509. cgsleep_ms(166);
  2510. for (i = 0; i < count; i++) {
  2511. if (total_count >= total_limit) {
  2512. applog(LOG_DEBUG, "USB scan devices2: total limit %d reached", total_limit);
  2513. break;
  2514. }
  2515. if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
  2516. applog(LOG_DEBUG,
  2517. "USB scan devices2: %s limit %d reached",
  2518. drv->dname, drv_count[drv->drv_id].limit);
  2519. break;
  2520. }
  2521. found = usb_check(drv, list[i]);
  2522. if (found != NULL) {
  2523. bool new_dev = false;
  2524. if (is_in_use(list[i]) || cgminer_usb_lock(drv, list[i]) == false)
  2525. free(found);
  2526. else {
  2527. cgpu = device_detect(list[i], found);
  2528. if (!cgpu)
  2529. cgminer_usb_unlock(drv, list[i]);
  2530. else {
  2531. new_dev = true;
  2532. cgpu->usbinfo.initialised = true;
  2533. total_count++;
  2534. drv_count[drv->drv_id].count++;
  2535. }
  2536. free(found);
  2537. }
  2538. if (single && new_dev)
  2539. break;
  2540. }
  2541. }
  2542. libusb_free_device_list(list, 1);
  2543. }
  2544. #if DO_USB_STATS
  2545. static void modes_str(char *buf, uint32_t modes)
  2546. {
  2547. bool first;
  2548. *buf = '\0';
  2549. if (modes == MODE_NONE)
  2550. strcpy(buf, MODE_NONE_STR);
  2551. else {
  2552. first = true;
  2553. if (modes & MODE_CTRL_READ) {
  2554. strcpy(buf, MODE_CTRL_READ_STR);
  2555. first = false;
  2556. }
  2557. if (modes & MODE_CTRL_WRITE) {
  2558. if (!first)
  2559. strcat(buf, MODE_SEP_STR);
  2560. strcat(buf, MODE_CTRL_WRITE_STR);
  2561. first = false;
  2562. }
  2563. if (modes & MODE_BULK_READ) {
  2564. if (!first)
  2565. strcat(buf, MODE_SEP_STR);
  2566. strcat(buf, MODE_BULK_READ_STR);
  2567. first = false;
  2568. }
  2569. if (modes & MODE_BULK_WRITE) {
  2570. if (!first)
  2571. strcat(buf, MODE_SEP_STR);
  2572. strcat(buf, MODE_BULK_WRITE_STR);
  2573. first = false;
  2574. }
  2575. }
  2576. }
  2577. #endif
  2578. // The stat data can be spurious due to not locking it before copying it -
  2579. // however that would require the stat() function to also lock and release
  2580. // a mutex every time a usb read or write is called which would slow
  2581. // things down more
  2582. struct api_data *api_usb_stats(__maybe_unused int *count)
  2583. {
  2584. #if DO_USB_STATS
  2585. struct cg_usb_stats_details *details;
  2586. struct cg_usb_stats *sta;
  2587. struct api_data *root = NULL;
  2588. int device;
  2589. int cmdseq;
  2590. char modes_s[32];
  2591. if (next_stat == USB_NOSTAT)
  2592. return NULL;
  2593. while (*count < next_stat * C_MAX * 2) {
  2594. device = *count / (C_MAX * 2);
  2595. cmdseq = *count % (C_MAX * 2);
  2596. (*count)++;
  2597. sta = &(usb_stats[device]);
  2598. details = &(sta->details[cmdseq]);
  2599. // Only show stats that have results
  2600. if (details->item[CMD_CMD].count == 0 &&
  2601. details->item[CMD_TIMEOUT].count == 0 &&
  2602. details->item[CMD_ERROR].count == 0)
  2603. continue;
  2604. root = api_add_string(root, "Name", sta->name, false);
  2605. root = api_add_int(root, "ID", &(sta->device_id), false);
  2606. root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
  2607. root = api_add_int(root, "Seq", &(details->seq), true);
  2608. modes_str(modes_s, details->modes);
  2609. root = api_add_string(root, "Modes", modes_s, true);
  2610. root = api_add_uint64(root, "Count",
  2611. &(details->item[CMD_CMD].count), true);
  2612. root = api_add_double(root, "Total Delay",
  2613. &(details->item[CMD_CMD].total_delay), true);
  2614. root = api_add_double(root, "Min Delay",
  2615. &(details->item[CMD_CMD].min_delay), true);
  2616. root = api_add_double(root, "Max Delay",
  2617. &(details->item[CMD_CMD].max_delay), true);
  2618. root = api_add_uint64(root, "Timeout Count",
  2619. &(details->item[CMD_TIMEOUT].count), true);
  2620. root = api_add_double(root, "Timeout Total Delay",
  2621. &(details->item[CMD_TIMEOUT].total_delay), true);
  2622. root = api_add_double(root, "Timeout Min Delay",
  2623. &(details->item[CMD_TIMEOUT].min_delay), true);
  2624. root = api_add_double(root, "Timeout Max Delay",
  2625. &(details->item[CMD_TIMEOUT].max_delay), true);
  2626. root = api_add_uint64(root, "Error Count",
  2627. &(details->item[CMD_ERROR].count), true);
  2628. root = api_add_double(root, "Error Total Delay",
  2629. &(details->item[CMD_ERROR].total_delay), true);
  2630. root = api_add_double(root, "Error Min Delay",
  2631. &(details->item[CMD_ERROR].min_delay), true);
  2632. root = api_add_double(root, "Error Max Delay",
  2633. &(details->item[CMD_ERROR].max_delay), true);
  2634. root = api_add_timeval(root, "First Command",
  2635. &(details->item[CMD_CMD].first), true);
  2636. root = api_add_timeval(root, "Last Command",
  2637. &(details->item[CMD_CMD].last), true);
  2638. root = api_add_timeval(root, "First Timeout",
  2639. &(details->item[CMD_TIMEOUT].first), true);
  2640. root = api_add_timeval(root, "Last Timeout",
  2641. &(details->item[CMD_TIMEOUT].last), true);
  2642. root = api_add_timeval(root, "First Error",
  2643. &(details->item[CMD_ERROR].first), true);
  2644. root = api_add_timeval(root, "Last Error",
  2645. &(details->item[CMD_ERROR].last), true);
  2646. return root;
  2647. }
  2648. #endif
  2649. return NULL;
  2650. }
  2651. #if DO_USB_STATS
  2652. static void newstats(struct cgpu_info *cgpu)
  2653. {
  2654. int i;
  2655. mutex_lock(&cgusb_lock);
  2656. cgpu->usbinfo.usbstat = next_stat + 1;
  2657. usb_stats = cgrealloc(usb_stats, sizeof(*usb_stats) * (next_stat+1));
  2658. usb_stats[next_stat].name = cgpu->drv->name;
  2659. usb_stats[next_stat].device_id = -1;
  2660. usb_stats[next_stat].details = cgcalloc(2, sizeof(struct cg_usb_stats_details) * (C_MAX + 1));
  2661. for (i = 1; i < C_MAX * 2; i += 2)
  2662. usb_stats[next_stat].details[i].seq = 1;
  2663. next_stat++;
  2664. mutex_unlock(&cgusb_lock);
  2665. }
  2666. #endif
  2667. void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
  2668. {
  2669. #if DO_USB_STATS
  2670. if (cgpu->usbinfo.usbstat < 1)
  2671. newstats(cgpu);
  2672. // we don't know the device_id until after add_cgpu()
  2673. usb_stats[cgpu->usbinfo.usbstat - 1].device_id = cgpu->device_id;
  2674. #endif
  2675. }
  2676. #if DO_USB_STATS
  2677. static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timeval *tv_finish, int err, int mode, enum usb_cmds cmd, int seq, int timeout)
  2678. {
  2679. struct cg_usb_stats_details *details;
  2680. double diff;
  2681. int item, extrams;
  2682. if (cgpu->usbinfo.usbstat < 1)
  2683. newstats(cgpu);
  2684. cgpu->usbinfo.tmo_count++;
  2685. // timeout checks are only done when stats are enabled
  2686. extrams = SECTOMS(tdiff(tv_finish, tv_start)) - timeout;
  2687. if (extrams >= USB_TMO_0) {
  2688. uint32_t totms = (uint32_t)(timeout + extrams);
  2689. int offset = 0;
  2690. if (extrams >= USB_TMO_2) {
  2691. applog(LOG_INFO, "%s%i: TIMEOUT %s took %dms but was %dms",
  2692. cgpu->drv->name, cgpu->device_id,
  2693. usb_cmdname(cmd), totms, timeout) ;
  2694. offset = 2;
  2695. } else if (extrams >= USB_TMO_1)
  2696. offset = 1;
  2697. cgpu->usbinfo.usb_tmo[offset].count++;
  2698. cgpu->usbinfo.usb_tmo[offset].total_over += extrams;
  2699. cgpu->usbinfo.usb_tmo[offset].total_tmo += timeout;
  2700. if (cgpu->usbinfo.usb_tmo[offset].min_tmo == 0) {
  2701. cgpu->usbinfo.usb_tmo[offset].min_tmo = totms;
  2702. cgpu->usbinfo.usb_tmo[offset].max_tmo = totms;
  2703. } else {
  2704. if (cgpu->usbinfo.usb_tmo[offset].min_tmo > totms)
  2705. cgpu->usbinfo.usb_tmo[offset].min_tmo = totms;
  2706. if (cgpu->usbinfo.usb_tmo[offset].max_tmo < totms)
  2707. cgpu->usbinfo.usb_tmo[offset].max_tmo = totms;
  2708. }
  2709. }
  2710. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
  2711. details->modes |= mode;
  2712. diff = tdiff(tv_finish, tv_start);
  2713. switch (err) {
  2714. case LIBUSB_SUCCESS:
  2715. item = CMD_CMD;
  2716. break;
  2717. case LIBUSB_ERROR_TIMEOUT:
  2718. item = CMD_TIMEOUT;
  2719. break;
  2720. default:
  2721. item = CMD_ERROR;
  2722. break;
  2723. }
  2724. if (details->item[item].count == 0) {
  2725. details->item[item].min_delay = diff;
  2726. cg_memcpy(&(details->item[item].first), tv_start, sizeof(*tv_start));
  2727. } else if (diff < details->item[item].min_delay)
  2728. details->item[item].min_delay = diff;
  2729. if (diff > details->item[item].max_delay)
  2730. details->item[item].max_delay = diff;
  2731. details->item[item].total_delay += diff;
  2732. cg_memcpy(&(details->item[item].last), tv_start, sizeof(*tv_start));
  2733. details->item[item].count++;
  2734. }
  2735. static void rejected_inc(struct cgpu_info *cgpu, uint32_t mode)
  2736. {
  2737. struct cg_usb_stats_details *details;
  2738. int item = CMD_ERROR;
  2739. if (cgpu->usbinfo.usbstat < 1)
  2740. newstats(cgpu);
  2741. details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
  2742. details->modes |= mode;
  2743. details->item[item].count++;
  2744. }
  2745. #endif
  2746. #define USB_RETRY_MAX 5
  2747. struct usb_transfer {
  2748. cgsem_t cgsem;
  2749. struct libusb_transfer *transfer;
  2750. bool cancellable;
  2751. struct list_head list;
  2752. };
  2753. bool async_usb_transfers(void)
  2754. {
  2755. bool ret;
  2756. cg_rlock(&cgusb_fd_lock);
  2757. ret = !list_empty(&ut_list);
  2758. cg_runlock(&cgusb_fd_lock);
  2759. return ret;
  2760. }
  2761. /* Cancellable transfers should only be labelled as such if it is safe for them
  2762. * to effectively mimic timing out early. This flag is usually used to signify
  2763. * a read is waiting on a non-critical response that takes a long time and the
  2764. * driver wishes it be aborted if work restart message has been sent. */
  2765. void cancel_usb_transfers(void)
  2766. {
  2767. struct usb_transfer *ut;
  2768. int cancellations = 0;
  2769. cg_wlock(&cgusb_fd_lock);
  2770. list_for_each_entry(ut, &ut_list, list) {
  2771. if (ut->cancellable) {
  2772. ut->cancellable = false;
  2773. libusb_cancel_transfer(ut->transfer);
  2774. cancellations++;
  2775. }
  2776. }
  2777. cg_wunlock(&cgusb_fd_lock);
  2778. if (cancellations)
  2779. applog(LOG_DEBUG, "Cancelled %d USB transfers", cancellations);
  2780. }
  2781. static void init_usb_transfer(struct usb_transfer *ut)
  2782. {
  2783. cgsem_init(&ut->cgsem);
  2784. ut->transfer = libusb_alloc_transfer(0);
  2785. if (unlikely(!ut->transfer))
  2786. quit(1, "Failed to libusb_alloc_transfer");
  2787. ut->transfer->user_data = ut;
  2788. ut->cancellable = false;
  2789. }
  2790. static void complete_usb_transfer(struct usb_transfer *ut)
  2791. {
  2792. cg_wlock(&cgusb_fd_lock);
  2793. list_del(&ut->list);
  2794. cg_wunlock(&cgusb_fd_lock);
  2795. cgsem_destroy(&ut->cgsem);
  2796. libusb_free_transfer(ut->transfer);
  2797. }
  2798. static void LIBUSB_CALL transfer_callback(struct libusb_transfer *transfer)
  2799. {
  2800. struct usb_transfer *ut = transfer->user_data;
  2801. ut->cancellable = false;
  2802. cgsem_post(&ut->cgsem);
  2803. }
  2804. static int usb_transfer_toerr(int ret)
  2805. {
  2806. if (ret <= 0)
  2807. return ret;
  2808. switch (ret) {
  2809. default:
  2810. case LIBUSB_TRANSFER_COMPLETED:
  2811. ret = LIBUSB_SUCCESS;
  2812. break;
  2813. case LIBUSB_TRANSFER_ERROR:
  2814. ret = LIBUSB_ERROR_IO;
  2815. break;
  2816. case LIBUSB_TRANSFER_TIMED_OUT:
  2817. case LIBUSB_TRANSFER_CANCELLED:
  2818. ret = LIBUSB_ERROR_TIMEOUT;
  2819. break;
  2820. case LIBUSB_TRANSFER_STALL:
  2821. ret = LIBUSB_ERROR_PIPE;
  2822. break;
  2823. case LIBUSB_TRANSFER_NO_DEVICE:
  2824. ret = LIBUSB_ERROR_NO_DEVICE;
  2825. break;
  2826. case LIBUSB_TRANSFER_OVERFLOW:
  2827. ret = LIBUSB_ERROR_OVERFLOW;
  2828. break;
  2829. }
  2830. return ret;
  2831. }
  2832. /* Wait for callback function to tell us it has finished the USB transfer, but
  2833. * use our own timer to cancel the request if we go beyond the timeout. */
  2834. static int callback_wait(struct usb_transfer *ut, int *transferred, unsigned int timeout)
  2835. {
  2836. struct libusb_transfer *transfer= ut->transfer;
  2837. int ret;
  2838. ret = cgsem_mswait(&ut->cgsem, timeout);
  2839. if (ret == ETIMEDOUT) {
  2840. /* We are emulating a timeout ourself here */
  2841. libusb_cancel_transfer(transfer);
  2842. /* Now wait for the callback function to be invoked. */
  2843. cgsem_wait(&ut->cgsem);
  2844. }
  2845. ret = transfer->status;
  2846. ret = usb_transfer_toerr(ret);
  2847. /* No need to sort out mutexes here since they won't be reused */
  2848. *transferred = transfer->actual_length;
  2849. return ret;
  2850. }
  2851. static int usb_submit_transfer(struct usb_transfer *ut, struct libusb_transfer *transfer,
  2852. bool cancellable, bool tt)
  2853. {
  2854. int err;
  2855. INIT_LIST_HEAD(&ut->list);
  2856. cg_wlock(&cgusb_fd_lock);
  2857. /* Imitate a transaction translator for writes to usb1.1 devices */
  2858. if (tt)
  2859. cgsleep_ms_r(&usb11_cgt, 1);
  2860. err = libusb_submit_transfer(transfer);
  2861. if (likely(!err))
  2862. ut->cancellable = cancellable;
  2863. list_add(&ut->list, &ut_list);
  2864. if (tt)
  2865. cgtimer_time(&usb11_cgt);
  2866. cg_wunlock(&cgusb_fd_lock);
  2867. return err;
  2868. }
  2869. static int
  2870. usb_perform_transfer(struct cgpu_info *cgpu, struct cg_usb_device *usbdev, int intinfo,
  2871. int epinfo, unsigned char *data, int length, int *transferred,
  2872. unsigned int timeout, __maybe_unused int mode, enum usb_cmds cmd,
  2873. __maybe_unused int seq, bool cancellable, bool tt)
  2874. {
  2875. int bulk_timeout, callback_timeout = timeout, err_retries = 0;
  2876. struct libusb_device_handle *dev_handle = usbdev->handle;
  2877. struct usb_epinfo *usb_epinfo;
  2878. struct usb_transfer ut;
  2879. unsigned char endpoint;
  2880. bool interrupt;
  2881. int err, errn;
  2882. #if DO_USB_STATS
  2883. struct timeval tv_start, tv_finish;
  2884. #endif
  2885. unsigned char buf[512];
  2886. #ifdef WIN32
  2887. /* On windows the callback_timeout is a safety mechanism only. */
  2888. bulk_timeout = timeout;
  2889. callback_timeout += WIN_CALLBACK_EXTRA;
  2890. #else
  2891. /* We give the transfer no timeout since we manage timeouts ourself on
  2892. * non windows. */
  2893. bulk_timeout = 0;
  2894. #endif
  2895. usb_epinfo = &(usbdev->found->intinfos[intinfo].epinfos[epinfo]);
  2896. interrupt = usb_epinfo->att == LIBUSB_TRANSFER_TYPE_INTERRUPT;
  2897. endpoint = usb_epinfo->ep;
  2898. if (unlikely(!data)) {
  2899. applog(LOG_ERR, "USB error: usb_perform_transfer sent NULL data (%s,intinfo=%d,epinfo=%d,length=%d,timeout=%u,mode=%d,cmd=%s,seq=%d) endpoint=%d",
  2900. cgpu->drv->name, intinfo, epinfo, length, timeout, mode, usb_cmdname(cmd), seq, (int)endpoint);
  2901. err = LIBUSB_ERROR_IO;
  2902. goto out_fail;
  2903. }
  2904. /* Avoid any async transfers during shutdown to allow the polling
  2905. * thread to be shut down after all existing transfers are complete */
  2906. if (opt_lowmem || cgpu->shutdown)
  2907. return libusb_bulk_transfer(dev_handle, endpoint, data, length, transferred, timeout);
  2908. err_retry:
  2909. init_usb_transfer(&ut);
  2910. if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) {
  2911. cg_memcpy(buf, data, length);
  2912. #ifndef HAVE_LIBUSB
  2913. /* Older versions may not have this feature so only enable it
  2914. * when we know we're compiling with included static libusb. We
  2915. * only do this for bulk transfer, not interrupt. */
  2916. if (!cgpu->nozlp && !interrupt)
  2917. ut.transfer->flags |= LIBUSB_TRANSFER_ADD_ZERO_PACKET;
  2918. #endif
  2919. #ifdef WIN32
  2920. /* Writes on windows really don't like to be cancelled, but
  2921. * are prone to timeouts under heavy USB traffic, so make this
  2922. * a last resort cancellation delayed long after the write
  2923. * would have timed out on its own. */
  2924. callback_timeout += WIN_WRITE_CBEXTRA;
  2925. #endif
  2926. }
  2927. USBDEBUG("USB debug: @usb_perform_transfer(%s (nodev=%s),intinfo=%d,epinfo=%d,data=%p,length=%d,timeout=%u,mode=%d,cmd=%s,seq=%d) endpoint=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, data, length, timeout, mode, usb_cmdname(cmd), seq, (int)endpoint);
  2928. if (interrupt) {
  2929. libusb_fill_interrupt_transfer(ut.transfer, dev_handle, endpoint,
  2930. buf, length, transfer_callback, &ut,
  2931. bulk_timeout);
  2932. } else {
  2933. libusb_fill_bulk_transfer(ut.transfer, dev_handle, endpoint, buf,
  2934. length, transfer_callback, &ut, bulk_timeout);
  2935. }
  2936. STATS_TIMEVAL(&tv_start);
  2937. err = usb_submit_transfer(&ut, ut.transfer, cancellable, tt);
  2938. errn = errno;
  2939. if (!err)
  2940. err = callback_wait(&ut, transferred, callback_timeout);
  2941. else
  2942. err = usb_transfer_toerr(err);
  2943. complete_usb_transfer(&ut);
  2944. STATS_TIMEVAL(&tv_finish);
  2945. USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq, timeout);
  2946. if (err < 0) {
  2947. applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
  2948. cgpu->drv->name, cgpu->device_id,
  2949. usb_cmdname(cmd), *transferred, err, errn);
  2950. }
  2951. if (err == LIBUSB_ERROR_PIPE) {
  2952. int pipeerr, retries = 0;
  2953. do {
  2954. cgpu->usbinfo.last_pipe = time(NULL);
  2955. cgpu->usbinfo.pipe_count++;
  2956. applog(LOG_INFO, "%s%i: libusb pipe error, trying to clear",
  2957. cgpu->drv->name, cgpu->device_id);
  2958. pipeerr = libusb_clear_halt(dev_handle, endpoint);
  2959. applog(LOG_DEBUG, "%s%i: libusb pipe error%scleared",
  2960. cgpu->drv->name, cgpu->device_id, err ? " not " : " ");
  2961. if (pipeerr)
  2962. cgpu->usbinfo.clear_fail_count++;
  2963. } while (pipeerr && ++retries < USB_RETRY_MAX);
  2964. if (!pipeerr && ++err_retries < USB_RETRY_MAX)
  2965. goto err_retry;
  2966. }
  2967. if (err == LIBUSB_ERROR_IO && ++err_retries < USB_RETRY_MAX)
  2968. goto err_retry;
  2969. out_fail:
  2970. if (NODEV(err))
  2971. *transferred = 0;
  2972. else if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN && *transferred)
  2973. cg_memcpy(data, buf, *transferred);
  2974. return err;
  2975. }
  2976. void usb_reset(struct cgpu_info *cgpu)
  2977. {
  2978. int pstate, err = 0;
  2979. DEVWLOCK(cgpu, pstate);
  2980. if (!cgpu->usbinfo.nodev) {
  2981. err = libusb_reset_device(cgpu->usbdev->handle);
  2982. applog(LOG_WARNING, "%s %i attempted reset got err:(%d) %s",
  2983. cgpu->drv->name, cgpu->device_id, err, libusb_error_name(err));
  2984. }
  2985. if (NODEV(err))
  2986. release_cgpu(cgpu);
  2987. DEVWUNLOCK(cgpu, pstate);
  2988. }
  2989. int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz,
  2990. int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable)
  2991. {
  2992. unsigned char *ptr, usbbuf[USB_READ_BUFSIZE];
  2993. struct timeval read_start, tv_finish;
  2994. int bufleft, err, got, tot, pstate, tried_reset;
  2995. struct cg_usb_device *usbdev;
  2996. unsigned int initial_timeout;
  2997. bool first = true;
  2998. size_t usbbufread;
  2999. int endlen = 0;
  3000. char *eom = NULL;
  3001. double done;
  3002. bool ftdi;
  3003. memset(usbbuf, 0, USB_READ_BUFSIZE);
  3004. memset(buf, 0, bufsiz);
  3005. if (end)
  3006. endlen = strlen(end);
  3007. DEVRLOCK(cgpu, pstate);
  3008. if (cgpu->usbinfo.nodev) {
  3009. *processed = 0;
  3010. USB_REJECT(cgpu, MODE_BULK_READ);
  3011. err = LIBUSB_ERROR_NO_DEVICE;
  3012. goto out_noerrmsg;
  3013. }
  3014. usbdev = cgpu->usbdev;
  3015. /* Interrupt transfers are guaranteed to be of an expected size (we hope) */
  3016. if (usbdev->found->intinfos[intinfo].epinfos[epinfo].att == LIBUSB_TRANSFER_TYPE_INTERRUPT)
  3017. usbbufread = bufsiz;
  3018. else
  3019. usbbufread = 512;
  3020. ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
  3021. USBDEBUG("USB debug: _usb_read(%s (nodev=%s),intinfo=%d,epinfo=%d,buf=%p,bufsiz=%d,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s,readonce=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, buf, (int)bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi), bool_str(readonce));
  3022. if (bufsiz > USB_MAX_READ)
  3023. quit(1, "%s USB read request %d too large (max=%d)", cgpu->drv->name, (int)bufsiz, USB_MAX_READ);
  3024. if (timeout == DEVTIMEOUT)
  3025. timeout = usbdev->found->timeout;
  3026. tot = usbdev->bufamt;
  3027. bufleft = bufsiz - tot;
  3028. if (tot)
  3029. cg_memcpy(usbbuf, usbdev->buffer, tot);
  3030. ptr = usbbuf + tot;
  3031. usbdev->bufamt = 0;
  3032. err = LIBUSB_SUCCESS;
  3033. if (end != NULL)
  3034. eom = strstr((const char *)usbbuf, end);
  3035. initial_timeout = timeout;
  3036. cgtime(&read_start);
  3037. tried_reset = 0;
  3038. while (bufleft > 0 && !eom) {
  3039. err = usb_perform_transfer(cgpu, usbdev, intinfo, epinfo, ptr, usbbufread,
  3040. &got, timeout, MODE_BULK_READ, cmd,
  3041. first ? SEQ0 : SEQ1, cancellable, false);
  3042. if (NODEV(err))
  3043. goto out_noerrmsg;
  3044. cgtime(&tv_finish);
  3045. ptr[got] = '\0';
  3046. USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), (int)usbbufread);
  3047. if (ftdi) {
  3048. // first 2 bytes returned are an FTDI status
  3049. if (got > 2) {
  3050. got -= 2;
  3051. memmove(ptr, ptr+2, got+1);
  3052. } else {
  3053. got = 0;
  3054. *ptr = '\0';
  3055. }
  3056. }
  3057. tot += got;
  3058. if (end != NULL)
  3059. eom = strstr((const char *)usbbuf, end);
  3060. /* Attempt a usb reset for an error that will otherwise cause
  3061. * this device to drop out provided we know the device still
  3062. * might exist. */
  3063. if (err && err != LIBUSB_ERROR_TIMEOUT) {
  3064. applog(LOG_WARNING, "%s %i %s usb read err:(%d) %s", cgpu->drv->name,
  3065. cgpu->device_id, usb_cmdname(cmd), err, libusb_error_name(err));
  3066. if (err != LIBUSB_ERROR_NO_DEVICE && !tried_reset) {
  3067. err = libusb_reset_device(usbdev->handle);
  3068. tried_reset = 1; // don't call reset twice in a row
  3069. applog(LOG_WARNING, "%s %i attempted reset got err:(%d) %s",
  3070. cgpu->drv->name, cgpu->device_id, err, libusb_error_name(err));
  3071. }
  3072. } else {
  3073. tried_reset = 0;
  3074. }
  3075. if (NODEV(err))
  3076. goto out_noerrmsg;
  3077. ptr += got;
  3078. bufleft -= got;
  3079. if (bufleft < 1)
  3080. err = LIBUSB_SUCCESS;
  3081. if (err || readonce)
  3082. break;
  3083. first = false;
  3084. done = tdiff(&tv_finish, &read_start);
  3085. // N.B. this is: return last err with whatever size has already been read
  3086. timeout = initial_timeout - (done * 1000);
  3087. if (timeout <= 0)
  3088. break;
  3089. }
  3090. /* If we found the end of message marker, just use that data and
  3091. * return success. */
  3092. if (eom) {
  3093. size_t eomlen = (void *)eom - (void *)usbbuf + endlen;
  3094. if (eomlen < bufsiz) {
  3095. bufsiz = eomlen;
  3096. err = LIBUSB_SUCCESS;
  3097. }
  3098. }
  3099. // N.B. usbdev->buffer was emptied before the while() loop
  3100. if (tot > (int)bufsiz) {
  3101. usbdev->bufamt = tot - bufsiz;
  3102. cg_memcpy(usbdev->buffer, usbbuf + bufsiz, usbdev->bufamt);
  3103. tot -= usbdev->bufamt;
  3104. usbbuf[tot] = '\0';
  3105. applog(LOG_DEBUG, "USB: %s%i read1 buffering %d extra bytes",
  3106. cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
  3107. }
  3108. *processed = tot;
  3109. cg_memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
  3110. out_noerrmsg:
  3111. if (NODEV(err)) {
  3112. cg_ruwlock(&cgpu->usbinfo.devlock);
  3113. release_cgpu(cgpu);
  3114. DEVWUNLOCK(cgpu, pstate);
  3115. } else
  3116. DEVRUNLOCK(cgpu, pstate);
  3117. return err;
  3118. }
  3119. int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, enum usb_cmds cmd)
  3120. {
  3121. struct timeval write_start, tv_finish;
  3122. struct cg_usb_device *usbdev;
  3123. unsigned int initial_timeout;
  3124. int err, sent, tot, pstate, tried_reset;
  3125. bool first = true;
  3126. double done;
  3127. DEVRLOCK(cgpu, pstate);
  3128. USBDEBUG("USB debug: _usb_write(%s (nodev=%s),intinfo=%d,epinfo=%d,buf='%s',bufsiz=%d,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, (char *)str_text(buf), (int)bufsiz, processed, timeout, usb_cmdname(cmd));
  3129. *processed = 0;
  3130. if (cgpu->usbinfo.nodev) {
  3131. USB_REJECT(cgpu, MODE_BULK_WRITE);
  3132. err = LIBUSB_ERROR_NO_DEVICE;
  3133. goto out_noerrmsg;
  3134. }
  3135. usbdev = cgpu->usbdev;
  3136. if (timeout == DEVTIMEOUT)
  3137. timeout = usbdev->found->timeout;
  3138. tot = 0;
  3139. err = LIBUSB_SUCCESS;
  3140. initial_timeout = timeout;
  3141. cgtime(&write_start);
  3142. tried_reset = 0;
  3143. while (bufsiz > 0) {
  3144. int tosend = bufsiz;
  3145. /* USB 1.1 devices don't handle zero packets well so split them
  3146. * up to not have the final transfer equal to the wMaxPacketSize
  3147. * or they will stall waiting for more data. */
  3148. if (usbdev->usb11) {
  3149. struct usb_epinfo *ue = &usbdev->found->intinfos[intinfo].epinfos[epinfo];
  3150. if (tosend == ue->wMaxPacketSize) {
  3151. tosend >>= 1;
  3152. if (unlikely(!tosend))
  3153. tosend = 1;
  3154. }
  3155. }
  3156. err = usb_perform_transfer(cgpu, usbdev, intinfo, epinfo, (unsigned char *)buf,
  3157. tosend, &sent, timeout, MODE_BULK_WRITE,
  3158. cmd, first ? SEQ0 : SEQ1, false, usbdev->tt);
  3159. if (NODEV(err))
  3160. goto out_noerrmsg;
  3161. cgtime(&tv_finish);
  3162. USBDEBUG("USB debug: @_usb_write(%s (nodev=%s)) err=%d%s sent=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), sent);
  3163. tot += sent;
  3164. /* Unlike reads, even a timeout error is unrecoverable on
  3165. * writes. */
  3166. if (err) {
  3167. applog(LOG_WARNING, "%s %i %s usb write err:(%d) %s", cgpu->drv->name,
  3168. cgpu->device_id, usb_cmdname(cmd), err, libusb_error_name(err));
  3169. if (err != LIBUSB_ERROR_NO_DEVICE && !tried_reset) {
  3170. err = libusb_reset_device(usbdev->handle);
  3171. tried_reset = 1; // don't try reset twice in a row
  3172. applog(LOG_WARNING, "%s %i attempted reset got err:(%d) %s",
  3173. cgpu->drv->name, cgpu->device_id, err, libusb_error_name(err));
  3174. }
  3175. } else {
  3176. tried_reset = 0;
  3177. }
  3178. if (err)
  3179. break;
  3180. buf += sent;
  3181. bufsiz -= sent;
  3182. first = false;
  3183. done = tdiff(&tv_finish, &write_start);
  3184. // N.B. this is: return last err with whatever size was written
  3185. timeout = initial_timeout - (done * 1000);
  3186. if (timeout <= 0)
  3187. break;
  3188. }
  3189. *processed = tot;
  3190. out_noerrmsg:
  3191. if (NODEV(err)) {
  3192. cg_ruwlock(&cgpu->usbinfo.devlock);
  3193. release_cgpu(cgpu);
  3194. DEVWUNLOCK(cgpu, pstate);
  3195. } else
  3196. DEVRUNLOCK(cgpu, pstate);
  3197. return err;
  3198. }
  3199. /* As we do for bulk reads, emulate a sync function for control transfers using
  3200. * our own timeouts that takes the same parameters as libusb_control_transfer.
  3201. */
  3202. static int usb_control_transfer(struct cgpu_info *cgpu, libusb_device_handle *dev_handle, uint8_t bmRequestType,
  3203. uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
  3204. unsigned char *buffer, uint16_t wLength, unsigned int timeout)
  3205. {
  3206. struct usb_transfer ut;
  3207. unsigned char buf[70];
  3208. int err, transferred;
  3209. bool tt = false;
  3210. if (unlikely(cgpu->shutdown))
  3211. return libusb_control_transfer(dev_handle, bmRequestType, bRequest, wValue, wIndex, buffer, wLength, timeout);
  3212. init_usb_transfer(&ut);
  3213. libusb_fill_control_setup(buf, bmRequestType, bRequest, wValue,
  3214. wIndex, wLength);
  3215. if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) {
  3216. if (wLength)
  3217. cg_memcpy(buf + LIBUSB_CONTROL_SETUP_SIZE, buffer, wLength);
  3218. if (cgpu->usbdev->descriptor->bcdUSB < 0x0200)
  3219. tt = true;
  3220. }
  3221. libusb_fill_control_transfer(ut.transfer, dev_handle, buf, transfer_callback,
  3222. &ut, 0);
  3223. err = usb_submit_transfer(&ut, ut.transfer, false, tt);
  3224. if (!err)
  3225. err = callback_wait(&ut, &transferred, timeout);
  3226. if (err == LIBUSB_SUCCESS && transferred) {
  3227. if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
  3228. cg_memcpy(buffer, libusb_control_transfer_get_data(ut.transfer),
  3229. transferred);
  3230. err = transferred;
  3231. goto out;
  3232. }
  3233. err = usb_transfer_toerr(err);
  3234. out:
  3235. complete_usb_transfer(&ut);
  3236. return err;
  3237. }
  3238. int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, __maybe_unused enum usb_cmds cmd)
  3239. {
  3240. struct cg_usb_device *usbdev;
  3241. #if DO_USB_STATS
  3242. struct timeval tv_start, tv_finish;
  3243. #endif
  3244. unsigned char buf[64];
  3245. uint32_t *buf32 = (uint32_t *)buf;
  3246. int err, i, bufsiz;
  3247. USBDEBUG("USB debug: _usb_transfer(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",siz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, siz, timeout, usb_cmdname(cmd));
  3248. if (cgpu->usbinfo.nodev) {
  3249. USB_REJECT(cgpu, MODE_CTRL_WRITE);
  3250. err = LIBUSB_ERROR_NO_DEVICE;
  3251. goto out_;
  3252. }
  3253. usbdev = cgpu->usbdev;
  3254. if (timeout == DEVTIMEOUT)
  3255. timeout = usbdev->found->timeout;
  3256. USBDEBUG("USB debug: @_usb_transfer() data=%s", bin2hex((unsigned char *)data, (size_t)siz));
  3257. if (siz > 0) {
  3258. bufsiz = siz - 1;
  3259. bufsiz >>= 2;
  3260. bufsiz++;
  3261. for (i = 0; i < bufsiz; i++)
  3262. buf32[i] = htole32(data[i]);
  3263. }
  3264. USBDEBUG("USB debug: @_usb_transfer() buf=%s", bin2hex(buf, (size_t)siz));
  3265. STATS_TIMEVAL(&tv_start);
  3266. err = usb_control_transfer(cgpu, usbdev->handle, request_type, bRequest,
  3267. wValue, wIndex, buf, (uint16_t)siz, timeout);
  3268. STATS_TIMEVAL(&tv_finish);
  3269. USB_STATS(cgpu, &tv_start, &tv_finish, err, MODE_CTRL_WRITE, cmd, SEQ0, timeout);
  3270. USBDEBUG("USB debug: @_usb_transfer(%s (nodev=%s)) err=%d%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err));
  3271. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  3272. applog(LOG_WARNING, "%s %i usb transfer err:(%d) %s", cgpu->drv->name, cgpu->device_id,
  3273. err, libusb_error_name(err));
  3274. }
  3275. out_:
  3276. return err;
  3277. }
  3278. /* We use the write devlock for control transfers since some control transfers
  3279. * are rare but may be changing settings within the device causing problems
  3280. * if concurrent transfers are happening. Using the write lock serialises
  3281. * any transfers. */
  3282. int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, enum usb_cmds cmd)
  3283. {
  3284. int pstate, err;
  3285. DEVWLOCK(cgpu, pstate);
  3286. err = __usb_transfer(cgpu, request_type, bRequest, wValue, wIndex, data, siz, timeout, cmd);
  3287. if (NOCONTROLDEV(err))
  3288. release_cgpu(cgpu);
  3289. DEVWUNLOCK(cgpu, pstate);
  3290. return err;
  3291. }
  3292. int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, unsigned int timeout, __maybe_unused enum usb_cmds cmd)
  3293. {
  3294. struct cg_usb_device *usbdev;
  3295. #if DO_USB_STATS
  3296. struct timeval tv_start, tv_finish;
  3297. #endif
  3298. unsigned char tbuf[64];
  3299. int err, pstate;
  3300. DEVWLOCK(cgpu, pstate);
  3301. USBDEBUG("USB debug: _usb_transfer_read(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",bufsiz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, bufsiz, timeout, usb_cmdname(cmd));
  3302. if (cgpu->usbinfo.nodev) {
  3303. USB_REJECT(cgpu, MODE_CTRL_READ);
  3304. err = LIBUSB_ERROR_NO_DEVICE;
  3305. goto out_noerrmsg;
  3306. }
  3307. usbdev = cgpu->usbdev;
  3308. if (timeout == DEVTIMEOUT)
  3309. timeout = usbdev->found->timeout;
  3310. *amount = 0;
  3311. memset(tbuf, 0, 64);
  3312. STATS_TIMEVAL(&tv_start);
  3313. err = usb_control_transfer(cgpu, usbdev->handle, request_type, bRequest,
  3314. wValue, wIndex, tbuf, (uint16_t)bufsiz, timeout);
  3315. STATS_TIMEVAL(&tv_finish);
  3316. USB_STATS(cgpu, &tv_start, &tv_finish, err, MODE_CTRL_READ, cmd, SEQ0, timeout);
  3317. cg_memcpy(buf, tbuf, bufsiz);
  3318. USBDEBUG("USB debug: @_usb_transfer_read(%s (nodev=%s)) amt/err=%d%s%s%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), err > 0 ? " = " : BLANK, err > 0 ? bin2hex((unsigned char *)buf, (size_t)err) : BLANK);
  3319. if (err > 0) {
  3320. *amount = err;
  3321. err = 0;
  3322. }
  3323. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  3324. applog(LOG_WARNING, "%s %i usb transfer read err:(%d) %s", cgpu->drv->name, cgpu->device_id,
  3325. err, libusb_error_name(err));
  3326. }
  3327. out_noerrmsg:
  3328. if (NOCONTROLDEV(err))
  3329. release_cgpu(cgpu);
  3330. DEVWUNLOCK(cgpu, pstate);
  3331. return err;
  3332. }
  3333. #define FTDI_STATUS_B0_MASK (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD)
  3334. #define FTDI_RS0_CTS (1 << 4)
  3335. #define FTDI_RS0_DSR (1 << 5)
  3336. #define FTDI_RS0_RI (1 << 6)
  3337. #define FTDI_RS0_RLSD (1 << 7)
  3338. /* Clear to send for FTDI */
  3339. int usb_ftdi_cts(struct cgpu_info *cgpu)
  3340. {
  3341. char buf[2], ret;
  3342. int err, amount;
  3343. err = _usb_transfer_read(cgpu, (uint8_t)FTDI_TYPE_IN, (uint8_t)5,
  3344. (uint16_t)0, (uint16_t)0, buf, 2,
  3345. &amount, DEVTIMEOUT, C_FTDI_STATUS);
  3346. /* We return true in case drivers are waiting indefinitely to try and
  3347. * write to something that's not there. */
  3348. if (err)
  3349. return true;
  3350. ret = buf[0] & FTDI_STATUS_B0_MASK;
  3351. return (ret & FTDI_RS0_CTS);
  3352. }
  3353. int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo)
  3354. {
  3355. int err = 0;
  3356. int pstate;
  3357. DEVWLOCK(cgpu, pstate);
  3358. if (cgpu->usbdev) {
  3359. if (cgpu->usbdev->usb_type != USB_TYPE_FTDI) {
  3360. applog(LOG_ERR, "%s: cgid %d latency request on non-FTDI device",
  3361. cgpu->drv->name, cgpu->cgminer_id);
  3362. err = LIBUSB_ERROR_NOT_SUPPORTED;
  3363. } else if (cgpu->usbdev->found->latency == LATENCY_UNUSED) {
  3364. applog(LOG_ERR, "%s: cgid %d invalid latency (UNUSED)",
  3365. cgpu->drv->name, cgpu->cgminer_id);
  3366. err = LIBUSB_ERROR_NOT_SUPPORTED;
  3367. }
  3368. if (!err)
  3369. err = __usb_transfer(cgpu, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
  3370. cgpu->usbdev->found->latency,
  3371. USBIF(cgpu->usbdev, intinfo),
  3372. NULL, 0, DEVTIMEOUT, C_LATENCY);
  3373. }
  3374. DEVWUNLOCK(cgpu, pstate);
  3375. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  3376. cgpu->drv->name, cgpu->cgminer_id,
  3377. usb_cmdname(C_LATENCY), err);
  3378. return err;
  3379. }
  3380. void usb_buffer_clear(struct cgpu_info *cgpu)
  3381. {
  3382. int pstate;
  3383. DEVWLOCK(cgpu, pstate);
  3384. if (cgpu->usbdev)
  3385. cgpu->usbdev->bufamt = 0;
  3386. DEVWUNLOCK(cgpu, pstate);
  3387. }
  3388. uint32_t usb_buffer_size(struct cgpu_info *cgpu)
  3389. {
  3390. uint32_t ret = 0;
  3391. int pstate;
  3392. DEVRLOCK(cgpu, pstate);
  3393. if (cgpu->usbdev)
  3394. ret = cgpu->usbdev->bufamt;
  3395. DEVRUNLOCK(cgpu, pstate);
  3396. return ret;
  3397. }
  3398. /*
  3399. * The value returned (0) when usbdev is NULL
  3400. * doesn't matter since it also means the next call to
  3401. * any usbutils function will fail with a nodev
  3402. * N.B. this is to get the interface number to use in a control_transfer
  3403. * which for some devices isn't actually the interface number
  3404. */
  3405. int _usb_interface(struct cgpu_info *cgpu, int intinfo)
  3406. {
  3407. int interface = 0;
  3408. int pstate;
  3409. DEVRLOCK(cgpu, pstate);
  3410. if (cgpu->usbdev)
  3411. interface = cgpu->usbdev->found->intinfos[intinfo].ctrl_transfer;
  3412. DEVRUNLOCK(cgpu, pstate);
  3413. return interface;
  3414. }
  3415. enum sub_ident usb_ident(struct cgpu_info *cgpu)
  3416. {
  3417. enum sub_ident ident = IDENT_UNK;
  3418. int pstate;
  3419. DEVRLOCK(cgpu, pstate);
  3420. if (cgpu->usbdev)
  3421. ident = cgpu->usbdev->ident;
  3422. DEVRUNLOCK(cgpu, pstate);
  3423. return ident;
  3424. }
  3425. // Need to set all devices with matching usbdev
  3426. void usb_set_dev_start(struct cgpu_info *cgpu)
  3427. {
  3428. struct cg_usb_device *cgusb;
  3429. struct cgpu_info *cgpu2;
  3430. struct timeval now;
  3431. int pstate;
  3432. DEVWLOCK(cgpu, pstate);
  3433. cgusb = cgpu->usbdev;
  3434. // If the device wasn't dropped
  3435. if (cgusb != NULL) {
  3436. int i;
  3437. cgtime(&now);
  3438. for (i = 0; i < total_devices; i++) {
  3439. cgpu2 = get_devices(i);
  3440. if (cgpu2->usbdev == cgusb)
  3441. copy_time(&(cgpu2->dev_start_tv), &now);
  3442. }
  3443. }
  3444. DEVWUNLOCK(cgpu, pstate);
  3445. }
  3446. void usb_cleanup(void)
  3447. {
  3448. struct cgpu_info *cgpu;
  3449. int count, pstate;
  3450. int i;
  3451. hotplug_time = 0;
  3452. cgsleep_ms(10);
  3453. count = 0;
  3454. for (i = 0; i < total_devices; i++) {
  3455. cgpu = devices[i];
  3456. switch (cgpu->drv->drv_id) {
  3457. case DRIVER_bflsc:
  3458. case DRIVER_bitforce:
  3459. case DRIVER_bitfury:
  3460. case DRIVER_cointerra:
  3461. case DRIVER_drillbit:
  3462. case DRIVER_gekko:
  3463. case DRIVER_modminer:
  3464. case DRIVER_icarus:
  3465. case DRIVER_avalon:
  3466. case DRIVER_avalon2:
  3467. case DRIVER_avalon4:
  3468. case DRIVER_avalon7:
  3469. case DRIVER_avalon8:
  3470. case DRIVER_avalon9:
  3471. case DRIVER_avalonlc3:
  3472. case DRIVER_avalonm:
  3473. case DRIVER_klondike:
  3474. case DRIVER_hashfast:
  3475. DEVWLOCK(cgpu, pstate);
  3476. release_cgpu(cgpu);
  3477. DEVWUNLOCK(cgpu, pstate);
  3478. count++;
  3479. break;
  3480. default:
  3481. break;
  3482. }
  3483. }
  3484. /*
  3485. * Must attempt to wait for the resource thread to release coz
  3486. * during a restart it won't automatically release them in linux
  3487. */
  3488. if (count) {
  3489. struct timeval start, now;
  3490. cgtime(&start);
  3491. while (42) {
  3492. cgsleep_ms(50);
  3493. mutex_lock(&cgusbres_lock);
  3494. if (!res_work_head)
  3495. break;
  3496. cgtime(&now);
  3497. if (tdiff(&now, &start) > 0.366) {
  3498. applog(LOG_WARNING,
  3499. "usb_cleanup gave up waiting for resource thread");
  3500. break;
  3501. }
  3502. mutex_unlock(&cgusbres_lock);
  3503. }
  3504. mutex_unlock(&cgusbres_lock);
  3505. }
  3506. cgsem_destroy(&usb_resource_sem);
  3507. }
  3508. #define DRIVER_COUNT_FOUND(X) if (X##_drv.name && strcasecmp(ptr, X##_drv.name) == 0) { \
  3509. drv_count[X##_drv.drv_id].limit = lim; \
  3510. found = true; \
  3511. }
  3512. void usb_initialise(void)
  3513. {
  3514. char *fre, *ptr, *comma, *colon;
  3515. int bus, dev, lim, i;
  3516. bool found;
  3517. INIT_LIST_HEAD(&ut_list);
  3518. for (i = 0; i < DRIVER_MAX; i++) {
  3519. drv_count[i].count = 0;
  3520. drv_count[i].limit = 999999;
  3521. }
  3522. cgusb_check_init();
  3523. if (opt_usb_select && *opt_usb_select) {
  3524. // Absolute device limit
  3525. if (*opt_usb_select == ':') {
  3526. total_limit = atoi(opt_usb_select+1);
  3527. if (total_limit < 0)
  3528. quit(1, "Invalid --usb total limit");
  3529. // Comma list of bus:dev devices to match
  3530. } else if (isdigit(*opt_usb_select)) {
  3531. fre = ptr = strdup(opt_usb_select);
  3532. do {
  3533. comma = strchr(ptr, ',');
  3534. if (comma)
  3535. *(comma++) = '\0';
  3536. colon = strchr(ptr, ':');
  3537. if (!colon)
  3538. quit(1, "Invalid --usb bus:dev missing ':'");
  3539. *(colon++) = '\0';
  3540. if (!isdigit(*ptr))
  3541. quit(1, "Invalid --usb bus:dev - bus must be a number");
  3542. if (!isdigit(*colon) && *colon != '*')
  3543. quit(1, "Invalid --usb bus:dev - dev must be a number or '*'");
  3544. bus = atoi(ptr);
  3545. if (bus <= 0)
  3546. quit(1, "Invalid --usb bus:dev - bus must be > 0");
  3547. if (*colon == '*')
  3548. dev = -1;
  3549. else {
  3550. dev = atoi(colon);
  3551. if (dev <= 0)
  3552. quit(1, "Invalid --usb bus:dev - dev must be > 0 or '*'");
  3553. }
  3554. busdev = cgrealloc(busdev, sizeof(*busdev) * (++busdev_count));
  3555. busdev[busdev_count-1].bus_number = bus;
  3556. busdev[busdev_count-1].device_address = dev;
  3557. ptr = comma;
  3558. } while (ptr);
  3559. free(fre);
  3560. // Comma list of DRV:limit
  3561. } else {
  3562. fre = ptr = strdup(opt_usb_select);
  3563. do {
  3564. comma = strchr(ptr, ',');
  3565. if (comma)
  3566. *(comma++) = '\0';
  3567. colon = strchr(ptr, ':');
  3568. if (!colon)
  3569. quit(1, "Invalid --usb DRV:limit missing ':'");
  3570. *(colon++) = '\0';
  3571. if (!isdigit(*colon))
  3572. quit(1, "Invalid --usb DRV:limit - limit must be a number");
  3573. lim = atoi(colon);
  3574. if (lim < 0)
  3575. quit(1, "Invalid --usb DRV:limit - limit must be >= 0");
  3576. found = false;
  3577. /* Use the DRIVER_PARSE_COMMANDS macro to iterate
  3578. * over all the drivers. */
  3579. DRIVER_PARSE_COMMANDS(DRIVER_COUNT_FOUND)
  3580. if (!found)
  3581. quit(1, "Invalid --usb DRV:limit - unknown DRV='%s'", ptr);
  3582. ptr = comma;
  3583. } while (ptr);
  3584. free(fre);
  3585. }
  3586. }
  3587. }
  3588. #ifndef WIN32
  3589. #include <errno.h>
  3590. #include <unistd.h>
  3591. #include <sys/stat.h>
  3592. #include <sys/file.h>
  3593. #include <fcntl.h>
  3594. #ifndef __APPLE__
  3595. union semun {
  3596. int val;
  3597. struct semid_ds *buf;
  3598. unsigned short *array;
  3599. struct seminfo *__buf;
  3600. };
  3601. #endif
  3602. #else
  3603. static LPSECURITY_ATTRIBUTES unsec(LPSECURITY_ATTRIBUTES sec)
  3604. {
  3605. SECURITY_DESCRIPTOR *sd = (PSECURITY_DESCRIPTOR)(sec->lpSecurityDescriptor);
  3606. FreeSid(sd->Group);
  3607. free(sec->lpSecurityDescriptor);
  3608. free(sec);
  3609. return NULL;
  3610. }
  3611. static LPSECURITY_ATTRIBUTES mksec(const char *dname, uint8_t bus_number, uint8_t device_address)
  3612. {
  3613. SID_IDENTIFIER_AUTHORITY SIDAuthWorld = {SECURITY_WORLD_SID_AUTHORITY};
  3614. PSID gsid = NULL;
  3615. LPSECURITY_ATTRIBUTES sec_att = NULL;
  3616. PSECURITY_DESCRIPTOR sec_des = NULL;
  3617. sec_des = cgmalloc(SECURITY_DESCRIPTOR_MIN_LENGTH);
  3618. if (!InitializeSecurityDescriptor(sec_des, SECURITY_DESCRIPTOR_REVISION)) {
  3619. applog(LOG_ERR,
  3620. "MTX: %s (%d:%d) USB failed to init secdes err (%d)",
  3621. dname, (int)bus_number, (int)device_address,
  3622. (int)GetLastError());
  3623. free(sec_des);
  3624. return NULL;
  3625. }
  3626. if (!SetSecurityDescriptorDacl(sec_des, TRUE, NULL, FALSE)) {
  3627. applog(LOG_ERR,
  3628. "MTX: %s (%d:%d) USB failed to secdes dacl err (%d)",
  3629. dname, (int)bus_number, (int)device_address,
  3630. (int)GetLastError());
  3631. free(sec_des);
  3632. return NULL;
  3633. }
  3634. if(!AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &gsid)) {
  3635. applog(LOG_ERR,
  3636. "MTX: %s (%d:%d) USB failed to create gsid err (%d)",
  3637. dname, (int)bus_number, (int)device_address,
  3638. (int)GetLastError());
  3639. free(sec_des);
  3640. return NULL;
  3641. }
  3642. if (!SetSecurityDescriptorGroup(sec_des, gsid, FALSE)) {
  3643. applog(LOG_ERR,
  3644. "MTX: %s (%d:%d) USB failed to secdes grp err (%d)",
  3645. dname, (int)bus_number, (int)device_address,
  3646. (int)GetLastError());
  3647. FreeSid(gsid);
  3648. free(sec_des);
  3649. return NULL;
  3650. }
  3651. sec_att = cgmalloc(sizeof(*sec_att));
  3652. sec_att->nLength = sizeof(*sec_att);
  3653. sec_att->lpSecurityDescriptor = sec_des;
  3654. sec_att->bInheritHandle = FALSE;
  3655. return sec_att;
  3656. }
  3657. #endif
  3658. // Any errors should always be printed since they will rarely if ever occur
  3659. // and thus it is best to always display them
  3660. static bool resource_lock(const char *dname, uint8_t bus_number, uint8_t device_address)
  3661. {
  3662. applog(LOG_DEBUG, "USB res lock %s %d-%d", dname, (int)bus_number, (int)device_address);
  3663. #ifdef WIN32
  3664. struct cgpu_info *cgpu;
  3665. LPSECURITY_ATTRIBUTES sec;
  3666. HANDLE usbMutex;
  3667. char name[64];
  3668. DWORD res;
  3669. int i;
  3670. if (is_in_use_bd(bus_number, device_address))
  3671. return false;
  3672. snprintf(name, sizeof(name), "cg-usb-%d-%d", (int)bus_number, (int)device_address);
  3673. sec = mksec(dname, bus_number, device_address);
  3674. if (!sec)
  3675. return false;
  3676. usbMutex = CreateMutex(sec, FALSE, name);
  3677. if (usbMutex == NULL) {
  3678. applog(LOG_ERR,
  3679. "MTX: %s USB failed to get '%s' err (%d)",
  3680. dname, name, (int)GetLastError());
  3681. sec = unsec(sec);
  3682. return false;
  3683. }
  3684. res = WaitForSingleObject(usbMutex, 0);
  3685. switch(res) {
  3686. case WAIT_OBJECT_0:
  3687. case WAIT_ABANDONED:
  3688. // Am I using it already?
  3689. for (i = 0; i < total_devices; i++) {
  3690. cgpu = get_devices(i);
  3691. if (cgpu->usbinfo.bus_number == bus_number &&
  3692. cgpu->usbinfo.device_address == device_address &&
  3693. cgpu->usbinfo.nodev == false) {
  3694. if (ReleaseMutex(usbMutex)) {
  3695. applog(LOG_WARNING,
  3696. "MTX: %s USB can't get '%s' - device in use",
  3697. dname, name);
  3698. goto fail;
  3699. }
  3700. applog(LOG_ERR,
  3701. "MTX: %s USB can't get '%s' - device in use - failure (%d)",
  3702. dname, name, (int)GetLastError());
  3703. goto fail;
  3704. }
  3705. }
  3706. break;
  3707. case WAIT_TIMEOUT:
  3708. if (!hotplug_mode)
  3709. applog(LOG_WARNING,
  3710. "MTX: %s USB failed to get '%s' - device in use",
  3711. dname, name);
  3712. goto fail;
  3713. case WAIT_FAILED:
  3714. applog(LOG_ERR,
  3715. "MTX: %s USB failed to get '%s' err (%d)",
  3716. dname, name, (int)GetLastError());
  3717. goto fail;
  3718. default:
  3719. applog(LOG_ERR,
  3720. "MTX: %s USB failed to get '%s' unknown reply (%d)",
  3721. dname, name, (int)res);
  3722. goto fail;
  3723. }
  3724. add_in_use(bus_number, device_address, false);
  3725. in_use_store_ress(bus_number, device_address, (void *)usbMutex, (void *)sec);
  3726. return true;
  3727. fail:
  3728. CloseHandle(usbMutex);
  3729. sec = unsec(sec);
  3730. return false;
  3731. #else
  3732. char name[64];
  3733. int fd;
  3734. if (is_in_use_bd(bus_number, device_address))
  3735. return false;
  3736. snprintf(name, sizeof(name), "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  3737. fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  3738. if (fd == -1) {
  3739. applog(LOG_ERR, "%s USB open failed '%s' err (%d) %s",
  3740. dname, name, errno, strerror(errno));
  3741. return false;
  3742. }
  3743. if (flock(fd, LOCK_EX | LOCK_NB)) {
  3744. applog(LOG_INFO, "%s USB failed to get '%s' - device in use",
  3745. dname, name);
  3746. close(fd);
  3747. return false;
  3748. }
  3749. add_in_use(bus_number, device_address, false);
  3750. in_use_store_fd(bus_number, device_address, fd);
  3751. return true;
  3752. #endif
  3753. }
  3754. // Any errors should always be printed since they will rarely if ever occur
  3755. // and thus it is best to always display them
  3756. static void resource_unlock(const char *dname, uint8_t bus_number, uint8_t device_address)
  3757. {
  3758. applog(LOG_DEBUG, "USB res unlock %s %d-%d", dname, (int)bus_number, (int)device_address);
  3759. #ifdef WIN32
  3760. LPSECURITY_ATTRIBUTES sec = NULL;
  3761. HANDLE usbMutex = NULL;
  3762. char name[64];
  3763. snprintf(name, sizeof(name), "cg-usb-%d-%d", (int)bus_number, (int)device_address);
  3764. in_use_get_ress(bus_number, device_address, (void **)(&usbMutex), (void **)(&sec));
  3765. if (!usbMutex || !sec)
  3766. goto fila;
  3767. if (!ReleaseMutex(usbMutex))
  3768. applog(LOG_ERR,
  3769. "MTX: %s USB failed to release '%s' err (%d)",
  3770. dname, name, (int)GetLastError());
  3771. fila:
  3772. if (usbMutex)
  3773. CloseHandle(usbMutex);
  3774. if (sec)
  3775. unsec(sec);
  3776. remove_in_use(bus_number, device_address);
  3777. return;
  3778. #else
  3779. char name[64];
  3780. int fd;
  3781. snprintf(name, sizeof(name), "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
  3782. fd = in_use_get_fd(bus_number, device_address);
  3783. if (fd < 0)
  3784. return;
  3785. remove_in_use(bus_number, device_address);
  3786. close(fd);
  3787. unlink(name);
  3788. return;
  3789. #endif
  3790. }
  3791. static void resource_process()
  3792. {
  3793. struct resource_work *res_work = NULL;
  3794. struct resource_reply *res_reply = NULL;
  3795. bool ok;
  3796. applog(LOG_DEBUG, "RES: %s (%d:%d) lock=%d",
  3797. res_work_head->dname,
  3798. (int)res_work_head->bus_number,
  3799. (int)res_work_head->device_address,
  3800. res_work_head->lock);
  3801. if (res_work_head->lock) {
  3802. ok = resource_lock(res_work_head->dname,
  3803. res_work_head->bus_number,
  3804. res_work_head->device_address);
  3805. applog(LOG_DEBUG, "RES: %s (%d:%d) lock ok=%d",
  3806. res_work_head->dname,
  3807. (int)res_work_head->bus_number,
  3808. (int)res_work_head->device_address,
  3809. ok);
  3810. res_reply = cgcalloc(1, sizeof(*res_reply));
  3811. res_reply->bus_number = res_work_head->bus_number;
  3812. res_reply->device_address = res_work_head->device_address;
  3813. res_reply->got = ok;
  3814. res_reply->next = res_reply_head;
  3815. res_reply_head = res_reply;
  3816. }
  3817. else
  3818. resource_unlock(res_work_head->dname,
  3819. res_work_head->bus_number,
  3820. res_work_head->device_address);
  3821. res_work = res_work_head;
  3822. res_work_head = res_work_head->next;
  3823. free(res_work);
  3824. }
  3825. void *usb_resource_thread(void __maybe_unused *userdata)
  3826. {
  3827. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  3828. RenameThread("USBResource");
  3829. applog(LOG_DEBUG, "RES: thread starting");
  3830. while (42) {
  3831. /* Wait to be told we have work to do */
  3832. cgsem_wait(&usb_resource_sem);
  3833. mutex_lock(&cgusbres_lock);
  3834. while (res_work_head)
  3835. resource_process();
  3836. mutex_unlock(&cgusbres_lock);
  3837. }
  3838. return NULL;
  3839. }
  3840. void initialise_usblocks(void)
  3841. {
  3842. mutex_init(&cgusb_lock);
  3843. mutex_init(&cgusbres_lock);
  3844. cglock_init(&cgusb_fd_lock);
  3845. }