usbutils.c 112 KB

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