usbutils.c 112 KB

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