| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894 |
- /*
- * Copyright 2012-2013 Andrew Smith
- * Copyright 2013 Con Kolivas <kernel@kolivas.org>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version. See COPYING for more details.
- */
- #include "config.h"
- #include <ctype.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include "logging.h"
- #include "miner.h"
- #include "usbutils.h"
- #define NODEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
- (err) == LIBUSB_ERROR_PIPE || \
- (err) == LIBUSB_ERROR_OTHER)
- #define NOCONTROLDEV(err) ((err) == LIBUSB_ERROR_NO_DEVICE || \
- (err) == LIBUSB_ERROR_OTHER)
- /*
- * WARNING - these assume DEVLOCK(cgpu, pstate) is called first and
- * DEVUNLOCK(cgpu, pstate) in called in the same function with the same pstate
- * given to DEVLOCK.
- * You must call DEVUNLOCK(cgpu, pstate) before exiting the function or it will leave
- * the thread Cancelability unrestored
- */
- #define DEVLOCK(cgpu, _pth_state) do { \
- pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_pth_state); \
- wr_lock(cgpu->usbinfo.devlock); \
- } while (0)
- #define DEVUNLOCK(cgpu, _pth_state) do { \
- wr_unlock(cgpu->usbinfo.devlock); \
- pthread_setcancelstate(_pth_state, NULL); \
- } while (0)
- #ifdef USE_BFLSC
- #define DRV_BFLSC 1
- #endif
- #ifdef USE_BITFORCE
- #define DRV_BITFORCE 2
- #endif
- #ifdef USE_MODMINER
- #define DRV_MODMINER 3
- #endif
- #ifdef USE_ZTEX
- #define DRV_ZTEX 4
- #endif
- #ifdef USE_ICARUS
- #define DRV_ICARUS 5
- #endif
- #ifdef USE_AVALON
- #define DRV_AVALON 6
- #endif
- #ifdef USE_BITFURY
- #define DRV_BITFURY 8
- #endif
- #define DRV_LAST -1
- #define USB_CONFIG 1
- #ifdef WIN32
- #define BFLSC_TIMEOUT_MS 999
- #define BITFORCE_TIMEOUT_MS 999
- #define BITFURY_TIMEOUT_MS 999
- #define MODMINER_TIMEOUT_MS 999
- #define AVALON_TIMEOUT_MS 999
- #define ICARUS_TIMEOUT_MS 999
- #else
- #define BFLSC_TIMEOUT_MS 300
- #define BITFORCE_TIMEOUT_MS 200
- #define BITFURY_TIMEOUT_MS 100
- #define MODMINER_TIMEOUT_MS 100
- #define AVALON_TIMEOUT_MS 200
- #define ICARUS_TIMEOUT_MS 200
- #endif
- #define USB_READ_MINPOLL 40
- #define USB_EPS(_intx, _epinfosx) { \
- .interface = _intx, \
- .epinfo_count = ARRAY_SIZE(_epinfosx), \
- .epinfos = _epinfosx \
- }
- #ifdef USE_BFLSC
- // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
- static struct usb_epinfo bas_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
- };
- static struct usb_intinfo bas_ints[] = {
- USB_EPS(0, bas_epinfos)
- };
- #endif
- #ifdef USE_BITFORCE
- // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
- static struct usb_epinfo bfl_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
- };
- static struct usb_intinfo bfl_ints[] = {
- USB_EPS(0, bfl_epinfos)
- };
- #endif
- #ifdef USE_BITFURY
- static struct usb_epinfo bfu_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 16, EPI(3), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 16, EPO(4), 0 }
- };
- static struct usb_intinfo bfu_ints[] = {
- USB_EPS(1, bfu_epinfos)
- };
- #endif
- #ifdef USE_MODMINER
- static struct usb_epinfo mmq_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0 }
- };
- static struct usb_intinfo mmq_ints[] = {
- USB_EPS(1, mmq_epinfos)
- };
- #endif
- #ifdef USE_AVALON
- static struct usb_epinfo ava_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
- };
- static struct usb_intinfo ava_ints[] = {
- USB_EPS(0, ava_epinfos)
- };
- #endif
- #ifdef USE_ICARUS
- static struct usb_epinfo ica_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
- };
- static struct usb_intinfo ica_ints[] = {
- USB_EPS(0, ica_epinfos)
- };
- static struct usb_epinfo amu_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0 }
- };
- static struct usb_intinfo amu_ints[] = {
- USB_EPS(0, amu_epinfos)
- };
- static struct usb_epinfo llt_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
- };
- static struct usb_intinfo llt_ints[] = {
- USB_EPS(0, llt_epinfos)
- };
- static struct usb_epinfo cmr1_epinfos[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
- };
- static struct usb_intinfo cmr1_ints[] = {
- USB_EPS(0, cmr1_epinfos)
- };
- static struct usb_epinfo cmr2_epinfos0[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0 }
- };
- #ifndef WIN32
- static struct usb_epinfo cmr2_epinfos1[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(4), 0 },
- };
- static struct usb_epinfo cmr2_epinfos2[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(5), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(6), 0 },
- };
- static struct usb_epinfo cmr2_epinfos3[] = {
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(7), 0 },
- { LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(8), 0 }
- };
- #endif
- static struct usb_intinfo cmr2_ints[] = {
- USB_EPS(0, cmr2_epinfos0)
- #ifndef WIN32
- ,
- USB_EPS(1, cmr2_epinfos1),
- USB_EPS(2, cmr2_epinfos2),
- USB_EPS(3, cmr2_epinfos3)
- #endif
- };
- #endif
- #define IDVENDOR_FTDI 0x0403
- #define INTINFO(_ints) \
- .which_intinfo = 0, \
- .intinfo_count = ARRAY_SIZE(_ints), \
- .intinfos = _ints
- #define USBEP(_usbdev, _epinfo) (_usbdev->found->intinfos[_usbdev->found->which_intinfo].epinfos[_epinfo].ep)
- #define FOUNDIF(_found) (_found->intinfos[_found->which_intinfo].interface)
- #define USBIF(_usbdev) FOUNDIF(_usbdev->found)
- // TODO: Add support for (at least) Isochronous endpoints
- static struct usb_find_devices find_dev[] = {
- #ifdef USE_BFLSC
- {
- .drv = DRV_BFLSC,
- .name = "BAS",
- .ident = IDENT_BAS,
- .idVendor = IDVENDOR_FTDI,
- .idProduct = 0x6014,
- //.iManufacturer = "Butterfly Labs",
- .iProduct = "BitFORCE SHA256 SC",
- .config = 1,
- .timeout = BFLSC_TIMEOUT_MS,
- .latency = LATENCY_STD,
- INTINFO(bas_ints) },
- #endif
- #ifdef USE_BITFORCE
- {
- .drv = DRV_BITFORCE,
- .name = "BFL",
- .ident = IDENT_BFL,
- .idVendor = IDVENDOR_FTDI,
- .idProduct = 0x6014,
- .iManufacturer = "Butterfly Labs Inc.",
- .iProduct = "BitFORCE SHA256",
- .config = 1,
- .timeout = BITFORCE_TIMEOUT_MS,
- .latency = LATENCY_STD,
- INTINFO(bfl_ints) },
- #endif
- #ifdef USE_BITFURY
- {
- .drv = DRV_BITFURY,
- .name = "BF1",
- .ident = IDENT_BFU,
- .idVendor = 0x03eb,
- .idProduct = 0x204b,
- .config = 1,
- .timeout = BITFURY_TIMEOUT_MS,
- .latency = LATENCY_UNUSED,
- .iManufacturer = "BPMC",
- .iProduct = "Bitfury BF1",
- INTINFO(bfu_ints)
- },
- #endif
- #ifdef USE_MODMINER
- {
- .drv = DRV_MODMINER,
- .name = "MMQ",
- .ident = IDENT_MMQ,
- .idVendor = 0x1fc9,
- .idProduct = 0x0003,
- .config = 1,
- .timeout = MODMINER_TIMEOUT_MS,
- .latency = LATENCY_UNUSED,
- INTINFO(mmq_ints) },
- #endif
- #ifdef USE_AVALON
- {
- .drv = DRV_AVALON,
- .name = "BTB",
- .ident = IDENT_BTB,
- .idVendor = IDVENDOR_FTDI,
- .idProduct = 0x6001,
- .iManufacturer = "Burnin Electronics",
- .iProduct = "BitBurner",
- .config = 1,
- .timeout = AVALON_TIMEOUT_MS,
- .latency = 10,
- INTINFO(ava_ints) },
- {
- .drv = DRV_AVALON,
- .name = "AVA",
- .ident = IDENT_AVA,
- .idVendor = IDVENDOR_FTDI,
- .idProduct = 0x6001,
- .config = 1,
- .timeout = AVALON_TIMEOUT_MS,
- .latency = 10,
- INTINFO(ava_ints) },
- #endif
- #ifdef USE_ICARUS
- {
- .drv = DRV_ICARUS,
- .name = "ICA",
- .ident = IDENT_ICA,
- .idVendor = 0x067b,
- .idProduct = 0x2303,
- .config = 1,
- .timeout = ICARUS_TIMEOUT_MS,
- .latency = LATENCY_UNUSED,
- INTINFO(ica_ints) },
- {
- .drv = DRV_ICARUS,
- .name = "AMU",
- .ident = IDENT_AMU,
- .idVendor = 0x10c4,
- .idProduct = 0xea60,
- .config = 1,
- .timeout = ICARUS_TIMEOUT_MS,
- .latency = LATENCY_UNUSED,
- INTINFO(amu_ints) },
- {
- .drv = DRV_ICARUS,
- .name = "BLT",
- .ident = IDENT_BLT,
- .idVendor = IDVENDOR_FTDI,
- .idProduct = 0x6001,
- .iProduct = "FT232R USB UART",
- .config = 1,
- .timeout = ICARUS_TIMEOUT_MS,
- .latency = LATENCY_STD,
- INTINFO(llt_ints) },
- // For any that don't match the above "BLT"
- {
- .drv = DRV_ICARUS,
- .name = "LLT",
- .ident = IDENT_LLT,
- .idVendor = IDVENDOR_FTDI,
- .idProduct = 0x6001,
- .config = 1,
- .timeout = ICARUS_TIMEOUT_MS,
- .latency = LATENCY_STD,
- INTINFO(llt_ints) },
- {
- .drv = DRV_ICARUS,
- .name = "CMR",
- .ident = IDENT_CMR1,
- .idVendor = IDVENDOR_FTDI,
- .idProduct = 0x6014,
- .iProduct = "Cairnsmore1",
- .config = 1,
- .timeout = ICARUS_TIMEOUT_MS,
- .latency = LATENCY_STD,
- INTINFO(cmr1_ints) },
- {
- .drv = DRV_ICARUS,
- .name = "CMR",
- .ident = IDENT_CMR2,
- .idVendor = IDVENDOR_FTDI,
- .idProduct = 0x8350,
- .iProduct = "Cairnsmore1",
- .config = 1,
- .timeout = ICARUS_TIMEOUT_MS,
- .latency = LATENCY_STD,
- INTINFO(cmr2_ints) },
- #endif
- #ifdef USE_ZTEX
- // This is here so cgminer -n shows them
- // the ztex driver (as at 201303) doesn't use usbutils
- {
- .drv = DRV_ZTEX,
- .name = "ZTX",
- .ident = IDENT_ZTX,
- .idVendor = 0x221a,
- .idProduct = 0x0100,
- .config = 1,
- .timeout = 100,
- .latency = LATENCY_UNUSED,
- .which_intinfo = 0,
- .intinfo_count = 0,
- .intinfos = NULL },
- #endif
- { DRV_LAST, NULL, 0, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, NULL }
- };
- #ifdef USE_BFLSC
- extern struct device_drv bflsc_drv;
- #endif
- #ifdef USE_BITFORCE
- extern struct device_drv bitforce_drv;
- #endif
- #ifdef USE_BITFURY
- extern struct device_drv bitfury_drv;
- #endif
- #ifdef USE_MODMINER
- extern struct device_drv modminer_drv;
- #endif
- #ifdef USE_ICARUS
- extern struct device_drv icarus_drv;
- #endif
- #ifdef USE_AVALON
- extern struct device_drv avalon_drv;
- #endif
- #define STRBUFLEN 256
- static const char *BLANK = "";
- static const char *space = " ";
- static const char *nodatareturned = "no data returned ";
- #define IOERR_CHECK(cgpu, err) \
- if (err == LIBUSB_ERROR_IO) { \
- cgpu->usbinfo.ioerr_count++; \
- cgpu->usbinfo.continuous_ioerr_count++; \
- } else { \
- cgpu->usbinfo.continuous_ioerr_count = 0; \
- }
- #if 0 // enable USBDEBUG - only during development testing
- static const char *debug_true_str = "true";
- static const char *debug_false_str = "false";
- static const char *nodevstr = "=NODEV";
- #define bool_str(boo) ((boo) ? debug_true_str : debug_false_str)
- #define isnodev(err) (NODEV(err) ? nodevstr : BLANK)
- #define USBDEBUG(fmt, ...) applog(LOG_WARNING, fmt, ##__VA_ARGS__)
- #else
- #define USBDEBUG(fmt, ...)
- #endif
- // For device limits by driver
- static struct driver_count {
- uint32_t count;
- uint32_t limit;
- } drv_count[DRIVER_MAX];
- // For device limits by list of bus/dev
- static struct usb_busdev {
- int bus_number;
- int device_address;
- void *resource1;
- void *resource2;
- } *busdev;
- static int busdev_count = 0;
- // Total device limit
- static int total_count = 0;
- static int total_limit = 999999;
- struct usb_in_use_list {
- struct usb_busdev in_use;
- struct usb_in_use_list *prev;
- struct usb_in_use_list *next;
- };
- // List of in use devices
- static struct usb_in_use_list *in_use_head = NULL;
- struct resource_work {
- bool lock;
- const char *dname;
- uint8_t bus_number;
- uint8_t device_address;
- struct resource_work *next;
- };
- // Pending work for the reslock thread
- struct resource_work *res_work_head = NULL;
- struct resource_reply {
- uint8_t bus_number;
- uint8_t device_address;
- bool got;
- struct resource_reply *next;
- };
- // Replies to lock requests
- struct resource_reply *res_reply_head = NULL;
- // Some stats need to always be defined
- #define SEQ0 0
- #define SEQ1 1
- // NONE must be 0 - calloced
- #define MODE_NONE 0
- #define MODE_CTRL_READ (1 << 0)
- #define MODE_CTRL_WRITE (1 << 1)
- #define MODE_BULK_READ (1 << 2)
- #define MODE_BULK_WRITE (1 << 3)
- // Set this to 0 to remove stats processing
- #define DO_USB_STATS 1
- static bool stats_initialised = false;
- #if DO_USB_STATS
- #define MODE_SEP_STR "+"
- #define MODE_NONE_STR "X"
- #define MODE_CTRL_READ_STR "cr"
- #define MODE_CTRL_WRITE_STR "cw"
- #define MODE_BULK_READ_STR "br"
- #define MODE_BULK_WRITE_STR "bw"
- // One for each CMD, TIMEOUT, ERROR
- struct cg_usb_stats_item {
- uint64_t count;
- double total_delay;
- double min_delay;
- double max_delay;
- struct timeval first;
- struct timeval last;
- };
- #define CMD_CMD 0
- #define CMD_TIMEOUT 1
- #define CMD_ERROR 2
- // One for each C_CMD
- struct cg_usb_stats_details {
- int seq;
- uint32_t modes;
- struct cg_usb_stats_item item[CMD_ERROR+1];
- };
- // One for each device
- struct cg_usb_stats {
- char *name;
- int device_id;
- struct cg_usb_stats_details *details;
- };
- static struct cg_usb_stats *usb_stats = NULL;
- static int next_stat = USB_NOSTAT;
- #define SECTOMS(s) ((int)((s) * 1000))
- #define USB_STATS(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_) \
- stats(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_)
- #define STATS_TIMEVAL(tv_) cgtime(tv_)
- #define USB_REJECT(sgpu_, mode_) rejected_inc(sgpu_, mode_)
- #else
- #define USB_STATS(sgpu_, sta_, fin_, err_, mode_, cmd_, seq_, tmo_)
- #define STATS_TIMEVAL(tv_)
- #define USB_REJECT(sgpu_, mode_)
- #endif // DO_USB_STATS
- /* Create usb_commands array from USB_PARSE_COMMANDS macro in usbutils.h */
- #define USB_ADD_COMMAND(X, Y) Y,
- char *usb_commands[] = {
- USB_PARSE_COMMANDS
- "Null"
- };
- #undef USB_ADD_COMMAND
- #ifdef EOL
- #undef EOL
- #endif
- #define EOL "\n"
- static const char *DESDEV = "Device";
- static const char *DESCON = "Config";
- static const char *DESSTR = "String";
- static const char *DESINT = "Interface";
- static const char *DESEP = "Endpoint";
- static const char *DESHID = "HID";
- static const char *DESRPT = "Report";
- static const char *DESPHY = "Physical";
- static const char *DESHUB = "Hub";
- static const char *EPIN = "In: ";
- static const char *EPOUT = "Out: ";
- static const char *EPX = "?: ";
- static const char *CONTROL = "Control";
- static const char *ISOCHRONOUS_X = "Isochronous+?";
- static const char *ISOCHRONOUS_N_X = "Isochronous+None+?";
- static const char *ISOCHRONOUS_N_D = "Isochronous+None+Data";
- static const char *ISOCHRONOUS_N_F = "Isochronous+None+Feedback";
- static const char *ISOCHRONOUS_N_I = "Isochronous+None+Implicit";
- static const char *ISOCHRONOUS_A_X = "Isochronous+Async+?";
- static const char *ISOCHRONOUS_A_D = "Isochronous+Async+Data";
- static const char *ISOCHRONOUS_A_F = "Isochronous+Async+Feedback";
- static const char *ISOCHRONOUS_A_I = "Isochronous+Async+Implicit";
- static const char *ISOCHRONOUS_D_X = "Isochronous+Adaptive+?";
- static const char *ISOCHRONOUS_D_D = "Isochronous+Adaptive+Data";
- static const char *ISOCHRONOUS_D_F = "Isochronous+Adaptive+Feedback";
- static const char *ISOCHRONOUS_D_I = "Isochronous+Adaptive+Implicit";
- static const char *ISOCHRONOUS_S_X = "Isochronous+Sync+?";
- static const char *ISOCHRONOUS_S_D = "Isochronous+Sync+Data";
- static const char *ISOCHRONOUS_S_F = "Isochronous+Sync+Feedback";
- static const char *ISOCHRONOUS_S_I = "Isochronous+Sync+Implicit";
- static const char *BULK = "Bulk";
- static const char *INTERRUPT = "Interrupt";
- static const char *UNKNOWN = "Unknown";
- static const char *err_io_str = " IO Error";
- static const char *err_access_str = " Access Denied-a";
- static const char *err_timeout_str = " Reply Timeout";
- static const char *err_pipe_str = " Access denied-p";
- static const char *err_other_str = " Access denied-o";
- static const char *usberrstr(int err)
- {
- switch (err) {
- case LIBUSB_ERROR_IO:
- return err_io_str;
- case LIBUSB_ERROR_ACCESS:
- return err_access_str;
- case LIBUSB_ERROR_TIMEOUT:
- return err_timeout_str;
- case LIBUSB_ERROR_PIPE:
- return err_pipe_str;
- case LIBUSB_ERROR_OTHER:
- return err_other_str;
- }
- return BLANK;
- }
- static const char *destype(uint8_t bDescriptorType)
- {
- switch (bDescriptorType) {
- case LIBUSB_DT_DEVICE:
- return DESDEV;
- case LIBUSB_DT_CONFIG:
- return DESCON;
- case LIBUSB_DT_STRING:
- return DESSTR;
- case LIBUSB_DT_INTERFACE:
- return DESINT;
- case LIBUSB_DT_ENDPOINT:
- return DESEP;
- case LIBUSB_DT_HID:
- return DESHID;
- case LIBUSB_DT_REPORT:
- return DESRPT;
- case LIBUSB_DT_PHYSICAL:
- return DESPHY;
- case LIBUSB_DT_HUB:
- return DESHUB;
- }
- return UNKNOWN;
- }
- static const char *epdir(uint8_t bEndpointAddress)
- {
- switch (bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
- case LIBUSB_ENDPOINT_IN:
- return EPIN;
- case LIBUSB_ENDPOINT_OUT:
- return EPOUT;
- }
- return EPX;
- }
- static const char *epatt(uint8_t bmAttributes)
- {
- switch(bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
- case LIBUSB_TRANSFER_TYPE_CONTROL:
- return CONTROL;
- case LIBUSB_TRANSFER_TYPE_BULK:
- return BULK;
- case LIBUSB_TRANSFER_TYPE_INTERRUPT:
- return INTERRUPT;
- case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
- switch(bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
- case LIBUSB_ISO_SYNC_TYPE_NONE:
- switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
- case LIBUSB_ISO_USAGE_TYPE_DATA:
- return ISOCHRONOUS_N_D;
- case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
- return ISOCHRONOUS_N_F;
- case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
- return ISOCHRONOUS_N_I;
- }
- return ISOCHRONOUS_N_X;
- case LIBUSB_ISO_SYNC_TYPE_ASYNC:
- switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
- case LIBUSB_ISO_USAGE_TYPE_DATA:
- return ISOCHRONOUS_A_D;
- case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
- return ISOCHRONOUS_A_F;
- case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
- return ISOCHRONOUS_A_I;
- }
- return ISOCHRONOUS_A_X;
- case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
- switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
- case LIBUSB_ISO_USAGE_TYPE_DATA:
- return ISOCHRONOUS_D_D;
- case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
- return ISOCHRONOUS_D_F;
- case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
- return ISOCHRONOUS_D_I;
- }
- return ISOCHRONOUS_D_X;
- case LIBUSB_ISO_SYNC_TYPE_SYNC:
- switch(bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
- case LIBUSB_ISO_USAGE_TYPE_DATA:
- return ISOCHRONOUS_S_D;
- case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
- return ISOCHRONOUS_S_F;
- case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
- return ISOCHRONOUS_S_I;
- }
- return ISOCHRONOUS_S_X;
- }
- return ISOCHRONOUS_X;
- }
- return UNKNOWN;
- }
- static void append(char **buf, char *append, size_t *off, size_t *len)
- {
- int new = strlen(append);
- if ((new + *off) >= *len)
- {
- *len *= 2;
- *buf = realloc(*buf, *len);
- if (unlikely(!*buf))
- quit(1, "USB failed to realloc append");
- }
- strcpy(*buf + *off, append);
- *off += new;
- }
- 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)
- {
- char tmp[512];
- int err;
- err = libusb_set_configuration(handle, cd);
- if (err) {
- snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to set config descriptor to %d, err %d",
- (int)count, cd, err);
- append(buf, tmp, off, len);
- return false;
- }
- err = libusb_get_active_config_descriptor(dev, config);
- if (err) {
- snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to get active config descriptor set to %d, err %d",
- (int)count, cd, err);
- append(buf, tmp, off, len);
- return false;
- }
- snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Set & Got active config descriptor to %d, err %d",
- (int)count, cd, err);
- append(buf, tmp, off, len);
- return true;
- }
- static void usb_full(ssize_t *count, libusb_device *dev, char **buf, size_t *off, size_t *len, int level)
- {
- struct libusb_device_descriptor desc;
- uint8_t bus_number;
- uint8_t device_address;
- struct libusb_device_handle *handle;
- struct libusb_config_descriptor *config;
- const struct libusb_interface_descriptor *idesc;
- const struct libusb_endpoint_descriptor *epdesc;
- unsigned char man[STRBUFLEN+1];
- unsigned char prod[STRBUFLEN+1];
- unsigned char ser[STRBUFLEN+1];
- char tmp[512];
- int err, i, j, k;
- err = libusb_get_device_descriptor(dev, &desc);
- if (opt_usb_list_all && err) {
- snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Failed to get descriptor, err %d",
- (int)(++(*count)), err);
- append(buf, tmp, off, len);
- return;
- }
- bus_number = libusb_get_bus_number(dev);
- device_address = libusb_get_device_address(dev);
- if (!opt_usb_list_all) {
- bool known = false;
- for (i = 0; find_dev[i].drv != DRV_LAST; i++)
- if ((find_dev[i].idVendor == desc.idVendor) &&
- (find_dev[i].idProduct == desc.idProduct)) {
- known = true;
- break;
- }
- if (!known)
- return;
- }
- (*count)++;
- if (level == 0) {
- snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Bus %d Device %d ID: %04x:%04x",
- (int)(*count), (int)bus_number, (int)device_address,
- desc.idVendor, desc.idProduct);
- } else {
- snprintf(tmp, sizeof(tmp), EOL ".USB dev %d: Bus %d Device %d Device Descriptor:" EOL "\tLength: %d" EOL
- "\tDescriptor Type: %s" EOL "\tUSB: %04x" EOL "\tDeviceClass: %d" EOL
- "\tDeviceSubClass: %d" EOL "\tDeviceProtocol: %d" EOL "\tMaxPacketSize0: %d" EOL
- "\tidVendor: %04x" EOL "\tidProduct: %04x" EOL "\tDeviceRelease: %x" EOL
- "\tNumConfigurations: %d",
- (int)(*count), (int)bus_number, (int)device_address,
- (int)(desc.bLength), destype(desc.bDescriptorType),
- desc.bcdUSB, (int)(desc.bDeviceClass), (int)(desc.bDeviceSubClass),
- (int)(desc.bDeviceProtocol), (int)(desc.bMaxPacketSize0),
- desc.idVendor, desc.idProduct, desc.bcdDevice,
- (int)(desc.bNumConfigurations));
- }
- append(buf, tmp, off, len);
- err = libusb_open(dev, &handle);
- if (err) {
- snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to open, err %d", (int)(*count), err);
- append(buf, tmp, off, len);
- return;
- }
- err = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, man, STRBUFLEN);
- if (err < 0)
- snprintf((char *)man, sizeof(man), "** err(%d)%s", err, usberrstr(err));
- err = libusb_get_string_descriptor_ascii(handle, desc.iProduct, prod, STRBUFLEN);
- if (err < 0)
- snprintf((char *)prod, sizeof(prod), "** err(%d)%s", err, usberrstr(err));
- if (level == 0) {
- libusb_close(handle);
- snprintf(tmp, sizeof(tmp), EOL " Manufacturer: '%s'" EOL " Product: '%s'", man, prod);
- append(buf, tmp, off, len);
- return;
- }
- if (libusb_kernel_driver_active(handle, 0) == 1) {
- snprintf(tmp, sizeof(tmp), EOL " * dev %d: kernel attached", (int)(*count));
- append(buf, tmp, off, len);
- }
- err = libusb_get_active_config_descriptor(dev, &config);
- if (err) {
- if (!setgetdes(*count, dev, handle, &config, 1, buf, off, len)
- && !setgetdes(*count, dev, handle, &config, 0, buf, off, len)) {
- libusb_close(handle);
- snprintf(tmp, sizeof(tmp), EOL " ** dev %d: Failed to set config descriptor to %d or %d",
- (int)(*count), 1, 0);
- append(buf, tmp, off, len);
- return;
- }
- }
- snprintf(tmp, sizeof(tmp), EOL " dev %d: Active Config:" EOL "\tDescriptorType: %s" EOL
- "\tNumInterfaces: %d" EOL "\tConfigurationValue: %d" EOL
- "\tAttributes: %d" EOL "\tMaxPower: %d",
- (int)(*count), destype(config->bDescriptorType),
- (int)(config->bNumInterfaces), (int)(config->iConfiguration),
- (int)(config->bmAttributes), (int)(config->MaxPower));
- append(buf, tmp, off, len);
- for (i = 0; i < (int)(config->bNumInterfaces); i++) {
- for (j = 0; j < config->interface[i].num_altsetting; j++) {
- idesc = &(config->interface[i].altsetting[j]);
- snprintf(tmp, sizeof(tmp), EOL " _dev %d: Interface Descriptor %d:" EOL
- "\tDescriptorType: %s" EOL "\tInterfaceNumber: %d" EOL
- "\tNumEndpoints: %d" EOL "\tInterfaceClass: %d" EOL
- "\tInterfaceSubClass: %d" EOL "\tInterfaceProtocol: %d",
- (int)(*count), j, destype(idesc->bDescriptorType),
- (int)(idesc->bInterfaceNumber),
- (int)(idesc->bNumEndpoints),
- (int)(idesc->bInterfaceClass),
- (int)(idesc->bInterfaceSubClass),
- (int)(idesc->bInterfaceProtocol));
- append(buf, tmp, off, len);
- for (k = 0; k < (int)(idesc->bNumEndpoints); k++) {
- epdesc = &(idesc->endpoint[k]);
- snprintf(tmp, sizeof(tmp), EOL " __dev %d: Interface %d Endpoint %d:" EOL
- "\tDescriptorType: %s" EOL
- "\tEndpointAddress: %s0x%x" EOL
- "\tAttributes: %s" EOL "\tMaxPacketSize: %d" EOL
- "\tInterval: %d" EOL "\tRefresh: %d",
- (int)(*count), (int)(idesc->bInterfaceNumber), k,
- destype(epdesc->bDescriptorType),
- epdir(epdesc->bEndpointAddress),
- (int)(epdesc->bEndpointAddress),
- epatt(epdesc->bmAttributes),
- epdesc->wMaxPacketSize,
- (int)(epdesc->bInterval),
- (int)(epdesc->bRefresh));
- append(buf, tmp, off, len);
- }
- }
- }
- libusb_free_config_descriptor(config);
- config = NULL;
- err = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, ser, STRBUFLEN);
- if (err < 0)
- snprintf((char *)ser, sizeof(ser), "** err(%d)%s", err, usberrstr(err));
- snprintf(tmp, sizeof(tmp), EOL " dev %d: More Info:" EOL "\tManufacturer: '%s'" EOL
- "\tProduct: '%s'" EOL "\tSerial '%s'",
- (int)(*count), man, prod, ser);
- append(buf, tmp, off, len);
- libusb_close(handle);
- }
- // Function to dump all USB devices
- void usb_all(int level)
- {
- libusb_device **list;
- ssize_t count, i, j;
- char *buf;
- size_t len, off;
- count = libusb_get_device_list(NULL, &list);
- if (count < 0) {
- applog(LOG_ERR, "USB all: failed, err %d%s", (int)count, usberrstr((int)count));
- return;
- }
- if (count == 0)
- applog(LOG_WARNING, "USB all: found no devices");
- else
- {
- len = 10000;
- buf = malloc(len+1);
- if (unlikely(!buf))
- quit(1, "USB failed to malloc buf in usb_all");
- sprintf(buf, "USB all: found %d devices", (int)count);
- off = strlen(buf);
- if (!opt_usb_list_all)
- append(&buf, " - listing known devices", &off, &len);
- j = -1;
- for (i = 0; i < count; i++)
- usb_full(&j, list[i], &buf, &off, &len, level);
- _applog(LOG_WARNING, buf);
- free(buf);
- if (j == -1)
- applog(LOG_WARNING, "No known USB devices");
- else
- applog(LOG_WARNING, "%d %sUSB devices",
- (int)(++j), opt_usb_list_all ? BLANK : "known ");
- }
- libusb_free_device_list(list, 1);
- }
- static void cgusb_check_init()
- {
- mutex_lock(&cgusb_lock);
- if (stats_initialised == false) {
- // N.B. environment LIBUSB_DEBUG also sets libusb_set_debug()
- if (opt_usbdump >= 0) {
- libusb_set_debug(NULL, opt_usbdump);
- usb_all(opt_usbdump);
- }
- stats_initialised = true;
- }
- mutex_unlock(&cgusb_lock);
- }
- const char *usb_cmdname(enum usb_cmds cmd)
- {
- cgusb_check_init();
- return usb_commands[cmd];
- }
- void usb_applog(struct cgpu_info *cgpu, enum usb_cmds cmd, char *msg, int amount, int err)
- {
- if (msg && !*msg)
- msg = NULL;
- if (!msg && amount == 0 && err == LIBUSB_SUCCESS)
- msg = (char *)nodatareturned;
- applog(LOG_ERR, "%s%i: %s failed%s%s (err=%d amt=%d)",
- cgpu->drv->name, cgpu->device_id,
- usb_cmdname(cmd),
- msg ? space : BLANK, msg ? msg : BLANK,
- err, amount);
- }
- static void in_use_store_ress(uint8_t bus_number, uint8_t device_address, void *resource1, void *resource2)
- {
- struct usb_in_use_list *in_use_tmp;
- bool found = false, empty = true;
- mutex_lock(&cgusb_lock);
- in_use_tmp = in_use_head;
- while (in_use_tmp) {
- if (in_use_tmp->in_use.bus_number == (int)bus_number &&
- in_use_tmp->in_use.device_address == (int)device_address) {
- found = true;
- if (in_use_tmp->in_use.resource1)
- empty = false;
- in_use_tmp->in_use.resource1 = resource1;
- if (in_use_tmp->in_use.resource2)
- empty = false;
- in_use_tmp->in_use.resource2 = resource2;
- break;
- }
- in_use_tmp = in_use_tmp->next;
- }
- mutex_unlock(&cgusb_lock);
- if (found == false)
- applog(LOG_ERR, "FAIL: USB store_ress not found (%d:%d)",
- (int)bus_number, (int)device_address);
- if (empty == false)
- applog(LOG_ERR, "FAIL: USB store_ress not empty (%d:%d)",
- (int)bus_number, (int)device_address);
- }
- static void in_use_get_ress(uint8_t bus_number, uint8_t device_address, void **resource1, void **resource2)
- {
- struct usb_in_use_list *in_use_tmp;
- bool found = false, empty = false;
- mutex_lock(&cgusb_lock);
- in_use_tmp = in_use_head;
- while (in_use_tmp) {
- if (in_use_tmp->in_use.bus_number == (int)bus_number &&
- in_use_tmp->in_use.device_address == (int)device_address) {
- found = true;
- if (!in_use_tmp->in_use.resource1)
- empty = true;
- *resource1 = in_use_tmp->in_use.resource1;
- in_use_tmp->in_use.resource1 = NULL;
- if (!in_use_tmp->in_use.resource2)
- empty = true;
- *resource2 = in_use_tmp->in_use.resource2;
- in_use_tmp->in_use.resource2 = NULL;
- break;
- }
- in_use_tmp = in_use_tmp->next;
- }
- mutex_unlock(&cgusb_lock);
- if (found == false)
- applog(LOG_ERR, "FAIL: USB get_lock not found (%d:%d)",
- (int)bus_number, (int)device_address);
- if (empty == true)
- applog(LOG_ERR, "FAIL: USB get_lock empty (%d:%d)",
- (int)bus_number, (int)device_address);
- }
- static bool __is_in_use(uint8_t bus_number, uint8_t device_address)
- {
- struct usb_in_use_list *in_use_tmp;
- bool ret = false;
- in_use_tmp = in_use_head;
- while (in_use_tmp) {
- if (in_use_tmp->in_use.bus_number == (int)bus_number &&
- in_use_tmp->in_use.device_address == (int)device_address) {
- ret = true;
- break;
- }
- in_use_tmp = in_use_tmp->next;
- }
- return ret;
- }
- static bool is_in_use_bd(uint8_t bus_number, uint8_t device_address)
- {
- bool ret;
- mutex_lock(&cgusb_lock);
- ret = __is_in_use(bus_number, device_address);
- mutex_unlock(&cgusb_lock);
- return ret;
- }
- static bool is_in_use(libusb_device *dev)
- {
- return is_in_use_bd(libusb_get_bus_number(dev), libusb_get_device_address(dev));
- }
- static void add_in_use(uint8_t bus_number, uint8_t device_address)
- {
- struct usb_in_use_list *in_use_tmp;
- bool found = false;
- mutex_lock(&cgusb_lock);
- if (unlikely(__is_in_use(bus_number, device_address))) {
- found = true;
- goto nofway;
- }
- in_use_tmp = calloc(1, sizeof(*in_use_tmp));
- if (unlikely(!in_use_tmp))
- quit(1, "USB failed to calloc in_use_tmp");
- in_use_tmp->in_use.bus_number = (int)bus_number;
- in_use_tmp->in_use.device_address = (int)device_address;
- in_use_tmp->next = in_use_head;
- if (in_use_head)
- in_use_head->prev = in_use_tmp;
- in_use_head = in_use_tmp;
- nofway:
- mutex_unlock(&cgusb_lock);
- if (found)
- applog(LOG_ERR, "FAIL: USB add already in use (%d:%d)",
- (int)bus_number, (int)device_address);
- }
- static void remove_in_use(uint8_t bus_number, uint8_t device_address)
- {
- struct usb_in_use_list *in_use_tmp;
- bool found = false;
- mutex_lock(&cgusb_lock);
- in_use_tmp = in_use_head;
- while (in_use_tmp) {
- if (in_use_tmp->in_use.bus_number == (int)bus_number &&
- in_use_tmp->in_use.device_address == (int)device_address) {
- found = true;
- if (in_use_tmp == in_use_head) {
- in_use_head = in_use_head->next;
- if (in_use_head)
- in_use_head->prev = NULL;
- } else {
- in_use_tmp->prev->next = in_use_tmp->next;
- if (in_use_tmp->next)
- in_use_tmp->next->prev = in_use_tmp->prev;
- }
- free(in_use_tmp);
- break;
- }
- in_use_tmp = in_use_tmp->next;
- }
- mutex_unlock(&cgusb_lock);
- if (!found)
- applog(LOG_ERR, "FAIL: USB remove not already in use (%d:%d)",
- (int)bus_number, (int)device_address);
- }
- static bool cgminer_usb_lock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
- {
- struct resource_work *res_work;
- bool ret;
- applog(LOG_DEBUG, "USB lock %s %d-%d", drv->dname, (int)bus_number, (int)device_address);
- res_work = calloc(1, sizeof(*res_work));
- if (unlikely(!res_work))
- quit(1, "USB failed to calloc lock res_work");
- res_work->lock = true;
- res_work->dname = (const char *)(drv->dname);
- res_work->bus_number = bus_number;
- res_work->device_address = device_address;
- mutex_lock(&cgusbres_lock);
- res_work->next = res_work_head;
- res_work_head = res_work;
- mutex_unlock(&cgusbres_lock);
- cgsem_post(&usb_resource_sem);
- // TODO: add a timeout fail - restart the resource thread?
- while (true) {
- cgsleep_ms(50);
- mutex_lock(&cgusbres_lock);
- if (res_reply_head) {
- struct resource_reply *res_reply_prev = NULL;
- struct resource_reply *res_reply = res_reply_head;
- while (res_reply) {
- if (res_reply->bus_number == bus_number &&
- res_reply->device_address == device_address) {
- if (res_reply_prev)
- res_reply_prev->next = res_reply->next;
- else
- res_reply_head = res_reply->next;
- mutex_unlock(&cgusbres_lock);
- ret = res_reply->got;
- free(res_reply);
- return ret;
- }
- res_reply_prev = res_reply;
- res_reply = res_reply->next;
- }
- }
- mutex_unlock(&cgusbres_lock);
- }
- }
- static bool cgminer_usb_lock(struct device_drv *drv, libusb_device *dev)
- {
- return cgminer_usb_lock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
- }
- static void cgminer_usb_unlock_bd(struct device_drv *drv, uint8_t bus_number, uint8_t device_address)
- {
- struct resource_work *res_work;
- applog(LOG_DEBUG, "USB unlock %s %d-%d", drv->dname, (int)bus_number, (int)device_address);
- res_work = calloc(1, sizeof(*res_work));
- if (unlikely(!res_work))
- quit(1, "USB failed to calloc unlock res_work");
- res_work->lock = false;
- res_work->dname = (const char *)(drv->dname);
- res_work->bus_number = bus_number;
- res_work->device_address = device_address;
- mutex_lock(&cgusbres_lock);
- res_work->next = res_work_head;
- res_work_head = res_work;
- mutex_unlock(&cgusbres_lock);
- cgsem_post(&usb_resource_sem);
- return;
- }
- static void cgminer_usb_unlock(struct device_drv *drv, libusb_device *dev)
- {
- cgminer_usb_unlock_bd(drv, libusb_get_bus_number(dev), libusb_get_device_address(dev));
- }
- static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)
- {
- applog(LOG_DEBUG, "USB free %s", cgusb->found->name);
- if (cgusb->serial_string && cgusb->serial_string != BLANK)
- free(cgusb->serial_string);
- if (cgusb->manuf_string && cgusb->manuf_string != BLANK)
- free(cgusb->manuf_string);
- if (cgusb->prod_string && cgusb->prod_string != BLANK)
- free(cgusb->prod_string);
- if (cgusb->descriptor)
- free(cgusb->descriptor);
- free(cgusb->found);
- if (cgusb->buffer)
- free(cgusb->buffer);
- free(cgusb);
- return NULL;
- }
- static void _usb_uninit(struct cgpu_info *cgpu)
- {
- applog(LOG_DEBUG, "USB uninit %s%i",
- cgpu->drv->name, cgpu->device_id);
- // May have happened already during a failed initialisation
- // if release_cgpu() was called due to a USB NODEV(err)
- if (!cgpu->usbdev)
- return;
- if (cgpu->usbdev->handle) {
- libusb_release_interface(cgpu->usbdev->handle, USBIF(cgpu->usbdev));
- cg_wlock(&cgusb_fd_lock);
- libusb_close(cgpu->usbdev->handle);
- cgpu->usbdev->handle = NULL;
- cg_wunlock(&cgusb_fd_lock);
- }
- cgpu->usbdev = free_cgusb(cgpu->usbdev);
- }
- void usb_uninit(struct cgpu_info *cgpu)
- {
- int pstate;
- DEVLOCK(cgpu, pstate);
- _usb_uninit(cgpu);
- DEVUNLOCK(cgpu, pstate);
- }
- /*
- * N.B. this is always called inside
- * DEVLOCK(cgpu, pstate);
- */
- static void release_cgpu(struct cgpu_info *cgpu)
- {
- struct cg_usb_device *cgusb = cgpu->usbdev;
- struct cgpu_info *lookcgpu;
- int which_intinfo, i;
- applog(LOG_DEBUG, "USB release %s%i",
- cgpu->drv->name, cgpu->device_id);
- // It has already been done
- if (cgpu->usbinfo.nodev)
- return;
- zombie_devs++;
- total_count--;
- drv_count[cgpu->drv->drv_id].count--;
- cgpu->usbinfo.nodev = true;
- cgpu->usbinfo.nodev_count++;
- cgtime(&cgpu->usbinfo.last_nodev);
- // Any devices sharing the same USB device should be marked also
- // Currently only MMQ shares a USB device
- for (i = 0; i < total_devices; i++) {
- lookcgpu = get_devices(i);
- if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
- total_count--;
- drv_count[lookcgpu->drv->drv_id].count--;
- lookcgpu->usbinfo.nodev = true;
- lookcgpu->usbinfo.nodev_count++;
- memcpy(&(lookcgpu->usbinfo.last_nodev),
- &(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
- lookcgpu->usbdev = NULL;
- }
- }
- which_intinfo = cgpu->usbdev->found->which_intinfo;
- _usb_uninit(cgpu);
- if (which_intinfo == 0)
- cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address);
- }
- /*
- * Used by MMQ - use the same usbdev thus locking is across all 4 related devices
- * since they must use the same USB handle since they use the same interface
- */
- struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig)
- {
- struct cgpu_info *copy;
- int pstate;
- DEVLOCK(orig, pstate);
- copy = calloc(1, sizeof(*copy));
- if (unlikely(!copy))
- quit(1, "Failed to calloc cgpu for %s in usb_copy_cgpu", orig->drv->dname);
- copy->name = orig->name;
- copy->drv = copy_drv(orig->drv);
- copy->deven = orig->deven;
- copy->threads = orig->threads;
- copy->usbdev = orig->usbdev;
- memcpy(&(copy->usbinfo), &(orig->usbinfo), sizeof(copy->usbinfo));
- copy->usbinfo.nodev = (copy->usbdev == NULL);
- copy->usbinfo.devlock = orig->usbinfo.devlock;
- DEVUNLOCK(orig, pstate);
- return copy;
- }
- /*
- * Used by CMR - use a different usbdev - since they must use a different
- * USB handle due to using different interfaces (libusb requirement)
- * N.B. multiple interfaces don't as yet work in windows libusb
- * so the CMR defines above that use them are defined out in windows
- * Nothing else uses multiple interfaces as at 20130922
- */
- static struct cgpu_info *usb_dup_cgpu(struct cgpu_info *orig, int intinfo)
- {
- struct cgpu_info *copy;
- copy = calloc(1, sizeof(*copy));
- if (unlikely(!copy))
- quit(1, "Failed to calloc cgpu for %s in usb_dup_cgpu", orig->drv->dname);
- copy->name = orig->name;
- copy->drv = copy_drv(orig->drv);
- copy->deven = orig->deven;
- copy->threads = orig->threads;
- if (orig->usbdev) {
- copy->usbdev = calloc(1, sizeof(*(copy->usbdev)));
- if (unlikely(!copy->usbdev))
- quit(1, "Failed to calloc usbdev for %s in usb_dup_cgpu", orig->drv->dname);
- copy->usbdev->found = malloc(sizeof(*(copy->usbdev->found)));
- if (unlikely(!copy->usbdev->found))
- quit(1, "Failed to malloc found for %s in usb_dup_cgpu", orig->drv->dname);
- memcpy(copy->usbdev->found, orig->usbdev->found, sizeof(*(copy->usbdev->found)));
- copy->usbdev->found->which_intinfo = intinfo;
- copy->usbdev->descriptor = NULL; // don't need it
- copy->usbdev->usb_type = orig->usbdev->usb_type;
- copy->usbdev->ident = orig->usbdev->ident;
- copy->usbdev->usbver = orig->usbdev->usbver;
- copy->usbdev->cps = orig->usbdev->cps;
- copy->usbdev->usecps = orig->usbdev->usecps;
- if (orig->usbdev->prod_string == BLANK)
- copy->usbdev->prod_string = (char *)BLANK;
- else
- copy->usbdev->prod_string = strdup(orig->usbdev->prod_string);
- if (orig->usbdev->manuf_string == BLANK)
- copy->usbdev->manuf_string = (char *)BLANK;
- else
- copy->usbdev->manuf_string = strdup(orig->usbdev->manuf_string);
- if (orig->usbdev->serial_string == BLANK)
- copy->usbdev->serial_string = (char *)BLANK;
- else
- copy->usbdev->serial_string = strdup(orig->usbdev->serial_string);
- copy->usbdev->fwVersion = orig->usbdev->fwVersion;
- copy->usbdev->interfaceVersion = orig->usbdev->interfaceVersion;
- }
- memcpy(&(copy->usbinfo), &(orig->usbinfo), sizeof(copy->usbinfo));
- copy->usbinfo.nodev = (copy->usbdev == NULL);
- copy->usbinfo.devlock = calloc(1, sizeof(*(copy->usbinfo.devlock)));
- if (unlikely(!copy->usbinfo.devlock))
- quit(1, "Failed to calloc devlock for %s in usb_dup_cgpu", orig->drv->dname);
- rwlock_init(copy->usbinfo.devlock);
- return copy;
- }
- struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads)
- {
- struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu));
- if (unlikely(!cgpu))
- quit(1, "Failed to calloc cgpu for %s in usb_alloc_cgpu", drv->dname);
- cgpu->drv = drv;
- cgpu->deven = DEV_ENABLED;
- cgpu->threads = threads;
- cgpu->usbinfo.nodev = true;
- cgpu->usbinfo.devlock = calloc(1, sizeof(*(cgpu->usbinfo.devlock)));
- if (unlikely(!cgpu->usbinfo.devlock))
- quit(1, "Failed to calloc devlock for %s in usb_alloc_cgpu", drv->dname);
- rwlock_init(cgpu->usbinfo.devlock);
- return cgpu;
- }
- struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devlock)
- {
- if (cgpu->drv->copy)
- free(cgpu->drv);
- free(cgpu->device_path);
- if (free_devlock)
- free(cgpu->usbinfo.devlock);
- free(cgpu);
- return NULL;
- }
- #define USB_INIT_FAIL 0
- #define USB_INIT_OK 1
- #define USB_INIT_IGNORE 2
- static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
- {
- struct cg_usb_device *cgusb = NULL;
- struct libusb_config_descriptor *config = NULL;
- const struct libusb_interface_descriptor *idesc;
- const struct libusb_endpoint_descriptor *epdesc;
- unsigned char strbuf[STRBUFLEN+1];
- char devpath[32];
- char devstr[STRBUFLEN+1];
- int err, ifinfo, epinfo, alt, epnum, pstate;
- int bad = USB_INIT_FAIL;
- int cfg;
- DEVLOCK(cgpu, pstate);
- cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
- cgpu->usbinfo.device_address = libusb_get_device_address(dev);
- if (found->intinfo_count > 1) {
- snprintf(devpath, sizeof(devpath), "%d:%d-i%d",
- (int)(cgpu->usbinfo.bus_number),
- (int)(cgpu->usbinfo.device_address),
- FOUNDIF(found));
- } else {
- snprintf(devpath, sizeof(devpath), "%d:%d",
- (int)(cgpu->usbinfo.bus_number),
- (int)(cgpu->usbinfo.device_address));
- }
- cgpu->device_path = strdup(devpath);
- snprintf(devstr, sizeof(devstr), "- %s device %s", found->name, devpath);
- cgusb = calloc(1, sizeof(*cgusb));
- if (unlikely(!cgusb))
- quit(1, "USB failed to calloc _usb_init cgusb");
- cgusb->found = found;
- if (found->idVendor == IDVENDOR_FTDI)
- cgusb->usb_type = USB_TYPE_FTDI;
- cgusb->ident = found->ident;
- cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
- if (unlikely(!cgusb->descriptor))
- quit(1, "USB failed to calloc _usb_init cgusb descriptor");
- err = libusb_get_device_descriptor(dev, cgusb->descriptor);
- if (err) {
- applog(LOG_DEBUG,
- "USB init failed to get descriptor, err %d %s",
- err, devstr);
- goto dame;
- }
- cg_wlock(&cgusb_fd_lock);
- err = libusb_open(dev, &(cgusb->handle));
- cg_wunlock(&cgusb_fd_lock);
- if (err) {
- switch (err) {
- case LIBUSB_ERROR_ACCESS:
- applog(LOG_ERR,
- "USB init, open device failed, err %d, "
- "you don't have privilege to access %s",
- err, devstr);
- break;
- #ifdef WIN32
- // Windows specific message
- case LIBUSB_ERROR_NOT_SUPPORTED:
- applog(LOG_ERR,
- "USB init, open device failed, err %d, "
- "you need to install a WinUSB driver for %s",
- err, devstr);
- break;
- #endif
- default:
- applog(LOG_DEBUG,
- "USB init, open failed, err %d %s",
- err, devstr);
- }
- goto dame;
- }
- #ifndef WIN32
- if (libusb_kernel_driver_active(cgusb->handle, FOUNDIF(found)) == 1) {
- applog(LOG_DEBUG, "USB init, kernel attached ... %s", devstr);
- err = libusb_detach_kernel_driver(cgusb->handle, FOUNDIF(found));
- if (err == 0) {
- applog(LOG_DEBUG,
- "USB init, kernel detached interface %d successfully %s",
- FOUNDIF(found), devstr);
- } else {
- applog(LOG_WARNING,
- "USB init, kernel detach interface %d failed, err %d in use? %s",
- FOUNDIF(found), err, devstr);
- goto cldame;
- }
- }
- #endif
- if (found->iManufacturer) {
- unsigned char man[STRBUFLEN+1];
- err = libusb_get_string_descriptor_ascii(cgusb->handle,
- cgusb->descriptor->iManufacturer,
- man, STRBUFLEN);
- if (err < 0) {
- applog(LOG_DEBUG,
- "USB init, failed to get iManufacturer, err %d %s",
- err, devstr);
- goto cldame;
- }
- if (strcmp((char *)man, found->iManufacturer)) {
- applog(LOG_DEBUG, "USB init, iManufacturer mismatch %s",
- devstr);
- bad = USB_INIT_IGNORE;
- goto cldame;
- }
- }
- if (found->iProduct) {
- unsigned char prod[STRBUFLEN+1];
- err = libusb_get_string_descriptor_ascii(cgusb->handle,
- cgusb->descriptor->iProduct,
- prod, STRBUFLEN);
- if (err < 0) {
- applog(LOG_DEBUG,
- "USB init, failed to get iProduct, err %d %s",
- err, devstr);
- goto cldame;
- }
- if (strcmp((char *)prod, found->iProduct)) {
- applog(LOG_DEBUG, "USB init, iProduct mismatch %s",
- devstr);
- bad = USB_INIT_IGNORE;
- goto cldame;
- }
- }
- cfg = -1;
- err = libusb_get_configuration(cgusb->handle, &cfg);
- if (err)
- cfg = -1;
- // Try to set it if we can't read it or it's different
- if (cfg != found->config) {
- err = libusb_set_configuration(cgusb->handle, found->config);
- if (err) {
- switch(err) {
- case LIBUSB_ERROR_BUSY:
- applog(LOG_WARNING,
- "USB init, set config %d in use %s",
- found->config, devstr);
- break;
- default:
- applog(LOG_DEBUG,
- "USB init, failed to set config to %d, err %d %s",
- found->config, err, devstr);
- }
- goto cldame;
- }
- }
- err = libusb_get_active_config_descriptor(dev, &config);
- if (err) {
- applog(LOG_DEBUG,
- "USB init, failed to get config descriptor, err %d %s",
- err, devstr);
- goto cldame;
- }
- int imax = -1;
- for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++)
- if (found->intinfos[ifinfo].interface > imax)
- imax = found->intinfos[ifinfo].interface;
- if ((int)(config->bNumInterfaces) <= imax) {
- applog(LOG_DEBUG, "USB init bNumInterfaces %d <= interface max %d for %s",
- (int)(config->bNumInterfaces), imax, devstr);
- goto cldame;
- }
- for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++)
- for (epinfo = 0; epinfo < found->intinfos[ifinfo].epinfo_count; epinfo++)
- found->intinfos[ifinfo].epinfos[epinfo].found = false;
- for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++) {
- int interface = found->intinfos[ifinfo].interface;
- for (alt = 0; alt < config->interface[interface].num_altsetting; alt++) {
- idesc = &(config->interface[interface].altsetting[alt]);
- for (epnum = 0; epnum < (int)(idesc->bNumEndpoints); epnum++) {
- struct usb_epinfo *epinfos = found->intinfos[ifinfo].epinfos;
- epdesc = &(idesc->endpoint[epnum]);
- for (epinfo = 0; epinfo < found->intinfos[ifinfo].epinfo_count; epinfo++) {
- if (!epinfos[epinfo].found) {
- if (epdesc->bmAttributes == epinfos[epinfo].att
- && epdesc->wMaxPacketSize >= epinfos[epinfo].size
- && epdesc->bEndpointAddress == epinfos[epinfo].ep) {
- epinfos[epinfo].found = true;
- // TODO: it's an ep (not device) attribute
- found->wMaxPacketSize = epdesc->wMaxPacketSize;
- break;
- }
- }
- }
- }
- }
- }
- for (ifinfo = 0; ifinfo < found->intinfo_count; ifinfo++)
- for (epinfo = 0; epinfo < found->intinfos[ifinfo].epinfo_count; epinfo++)
- if (found->intinfos[ifinfo].epinfos[epinfo].found == false) {
- applog(LOG_DEBUG, "USB init found (%d,%d) == false %s",
- ifinfo, epinfo, devstr);
- goto cldame;
- }
- err = libusb_claim_interface(cgusb->handle, FOUNDIF(found));
- if (err) {
- switch(err) {
- case LIBUSB_ERROR_BUSY:
- applog(LOG_WARNING,
- "USB init, claim interface %d in use %s",
- FOUNDIF(found), devstr);
- break;
- default:
- applog(LOG_DEBUG,
- "USB init, claim interface %d failed, err %d %s",
- FOUNDIF(found), err, devstr);
- }
- goto reldame;
- }
- cfg = -1;
- err = libusb_get_configuration(cgusb->handle, &cfg);
- if (err)
- cfg = -1;
- if (cfg != found->config) {
- applog(LOG_WARNING,
- "USB init, incorrect config (%d!=%d) after claim of %s",
- cfg, found->config, devstr);
- goto reldame;
- }
- cgusb->usbver = cgusb->descriptor->bcdUSB;
- // TODO: allow this with the right version of the libusb include and running library
- // cgusb->speed = libusb_get_device_speed(dev);
- err = libusb_get_string_descriptor_ascii(cgusb->handle,
- cgusb->descriptor->iProduct, strbuf, STRBUFLEN);
- if (err > 0)
- cgusb->prod_string = strdup((char *)strbuf);
- else
- cgusb->prod_string = (char *)BLANK;
- err = libusb_get_string_descriptor_ascii(cgusb->handle,
- cgusb->descriptor->iManufacturer, strbuf, STRBUFLEN);
- if (err > 0)
- cgusb->manuf_string = strdup((char *)strbuf);
- else
- cgusb->manuf_string = (char *)BLANK;
- err = libusb_get_string_descriptor_ascii(cgusb->handle,
- cgusb->descriptor->iSerialNumber, strbuf, STRBUFLEN);
- if (err > 0)
- cgusb->serial_string = strdup((char *)strbuf);
- else
- cgusb->serial_string = (char *)BLANK;
- // TODO: ?
- // cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
- // cgusb->interfaceVersion
- applog(LOG_DEBUG,
- "USB init %s usbver=%04x prod='%s' manuf='%s' serial='%s'",
- devstr, cgusb->usbver, cgusb->prod_string,
- cgusb->manuf_string, cgusb->serial_string);
- cgpu->usbdev = cgusb;
- cgpu->usbinfo.nodev = false;
- libusb_free_config_descriptor(config);
- // Allow a name change based on the idVendor+idProduct
- // N.B. must be done before calling add_cgpu()
- if (strcmp(cgpu->drv->name, found->name)) {
- if (!cgpu->drv->copy)
- cgpu->drv = copy_drv(cgpu->drv);
- cgpu->drv->name = (char *)(found->name);
- }
- bad = USB_INIT_OK;
- goto out_unlock;
- reldame:
- libusb_release_interface(cgusb->handle, FOUNDIF(found));
- cldame:
- cg_wlock(&cgusb_fd_lock);
- libusb_close(cgusb->handle);
- cgusb->handle = NULL;
- cg_wunlock(&cgusb_fd_lock);
- dame:
- if (config)
- libusb_free_config_descriptor(config);
- cgusb = free_cgusb(cgusb);
- out_unlock:
- DEVUNLOCK(cgpu, pstate);
- return bad;
- }
- // To get the extra interfaces on a multi interface device
- struct cgpu_info *usb_init_intinfo(struct cgpu_info *orig, int intinfo)
- {
- struct usb_find_devices *found;
- struct libusb_device *dev;
- struct cgpu_info *copy = NULL;
- char msgstr[STRBUFLEN+1];
- char devstr[STRBUFLEN+1];
- char devpath[32];
- int err, pstate;
- DEVLOCK(orig, pstate);
- snprintf(msgstr, sizeof(msgstr), "USB %s init_intinfo (%d:%d-i%d)",
- orig->drv->dname,
- (int)(orig->usbinfo.bus_number),
- (int)(orig->usbinfo.device_address),
- orig->usbdev->found->which_intinfo);
- if (orig->usbinfo.nodev) {
- applog(LOG_ERR, "%s cgpu has nodev", msgstr);
- goto Hitagi;
- }
- if (orig->usbdev->found->which_intinfo != 0) {
- applog(LOG_ERR, "%s incorrect cgpu (must be i0)", msgstr);
- goto Hitagi;
- }
- if (orig->usbdev->found->intinfo_count < 2) {
- applog(LOG_ERR, "%s cgpu only has 1 interface", msgstr);
- goto Hitagi;
- }
- if (intinfo < 1 || intinfo >= orig->usbdev->found->intinfo_count) {
- applog(LOG_ERR, "%s invalid intinfo (%d) must be > 0 && < %d",
- msgstr, intinfo, orig->usbdev->found->intinfo_count);
- goto Hitagi;
- }
- dev = libusb_get_device(orig->usbdev->handle);
- copy = usb_dup_cgpu(orig, intinfo);
- if (!copy)
- goto Hitagi;
- found = copy->usbdev->found;
- snprintf(devpath, sizeof(devpath), "%d:%d-i%d",
- (int)(copy->usbinfo.bus_number),
- (int)(copy->usbinfo.device_address),
- FOUNDIF(found));
- copy->device_path = strdup(devpath);
- snprintf(devstr, sizeof(devstr), "- %s device %s", found->name, devpath);
- cg_wlock(&cgusb_fd_lock);
- err = libusb_open(dev, &(copy->usbdev->handle));
- cg_wunlock(&cgusb_fd_lock);
- if (err) {
- switch (err) {
- case LIBUSB_ERROR_ACCESS:
- applog(LOG_ERR,
- "USB init_intinfo, open device failed, err %d, "
- "you don't have privilege to access %s",
- err, devstr);
- break;
- #ifdef WIN32
- // Windows specific message
- case LIBUSB_ERROR_NOT_SUPPORTED:
- applog(LOG_ERR,
- "USB init_intinfo, open device failed, err %d, "
- "you need to install a WinUSB driver for %s",
- err, devstr);
- break;
- #endif
- default:
- applog(LOG_DEBUG,
- "USB init_intinfo, open failed, err %d %s",
- err, devstr);
- }
- goto Hitagi;
- }
- #ifndef WIN32
- if (libusb_kernel_driver_active(copy->usbdev->handle, FOUNDIF(found)) == 1) {
- applog(LOG_DEBUG, "USB init_intinfo, kernel attached ... %s", devstr);
- err = libusb_detach_kernel_driver(copy->usbdev->handle, FOUNDIF(found));
- if (err == 0) {
- applog(LOG_DEBUG,
- "USB init_intinfo, kernel detached interface %d successfully %s",
- FOUNDIF(found), devstr);
- } else {
- applog(LOG_WARNING,
- "USB init_intinfo, kernel detach interface %d failed, err %d in use? %s",
- FOUNDIF(found), err, devstr);
- goto HitagiClose;
- }
- }
- #endif
- err = libusb_claim_interface(copy->usbdev->handle, FOUNDIF(found));
- if (err) {
- switch(err) {
- case LIBUSB_ERROR_BUSY:
- applog(LOG_WARNING,
- "USB init_intinfo, claim interface %d in use %s",
- FOUNDIF(found), devstr);
- break;
- default:
- applog(LOG_DEBUG,
- "USB init_intinfo, claim interface %d failed, err %d %s",
- FOUNDIF(found), err, devstr);
- }
- goto HitagiClose;
- }
- goto Hitagi;
- HitagiClose:
- cg_wlock(&cgusb_fd_lock);
- libusb_close(copy->usbdev->handle);
- copy->usbdev->handle = NULL;
- cg_wunlock(&cgusb_fd_lock);
- copy->usbdev = free_cgusb(copy->usbdev);
- copy = usb_free_cgpu(copy);
- Hitagi:
- DEVUNLOCK(orig, pstate);
- return copy;
- }
- bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found_match)
- {
- struct usb_find_devices *found_use = NULL;
- int uninitialised_var(ret);
- int i;
- for (i = 0; find_dev[i].drv != DRV_LAST; i++) {
- if (find_dev[i].drv == found_match->drv &&
- find_dev[i].idVendor == found_match->idVendor &&
- find_dev[i].idProduct == found_match->idProduct) {
- found_use = malloc(sizeof(*found_use));
- if (unlikely(!found_use))
- quit(1, "USB failed to malloc found_use");
- memcpy(found_use, &(find_dev[i]), sizeof(*found_use));
- ret = _usb_init(cgpu, dev, found_use);
- if (ret != USB_INIT_IGNORE)
- break;
- }
- }
- if (ret == USB_INIT_FAIL)
- applog(LOG_ERR, "%s detect (%d:%d) failed to initialise (incorrect device?)",
- cgpu->drv->dname,
- (int)(cgpu->usbinfo.bus_number),
- (int)(cgpu->usbinfo.device_address));
- return (ret == USB_INIT_OK);
- }
- static bool usb_check_device(struct device_drv *drv, struct libusb_device *dev, struct usb_find_devices *look)
- {
- struct libusb_device_descriptor desc;
- int bus_number, device_address;
- int err, i;
- bool ok;
- err = libusb_get_device_descriptor(dev, &desc);
- if (err) {
- applog(LOG_DEBUG, "USB check device: Failed to get descriptor, err %d", err);
- return false;
- }
- if (desc.idVendor != look->idVendor || desc.idProduct != look->idProduct) {
- applog(LOG_DEBUG, "%s looking for %s %04x:%04x but found %04x:%04x instead",
- drv->name, look->name, look->idVendor, look->idProduct, desc.idVendor, desc.idProduct);
- return false;
- }
- if (busdev_count > 0) {
- bus_number = (int)libusb_get_bus_number(dev);
- device_address = (int)libusb_get_device_address(dev);
- ok = false;
- for (i = 0; i < busdev_count; i++) {
- if (bus_number == busdev[i].bus_number) {
- if (busdev[i].device_address == -1 ||
- device_address == busdev[i].device_address) {
- ok = true;
- break;
- }
- }
- }
- if (!ok) {
- applog(LOG_DEBUG, "%s rejected %s %04x:%04x with bus:dev (%d:%d)",
- drv->name, look->name, look->idVendor, look->idProduct,
- bus_number, device_address);
- return false;
- }
- }
- applog(LOG_DEBUG, "%s looking for and found %s %04x:%04x",
- drv->name, look->name, look->idVendor, look->idProduct);
- return true;
- }
- static struct usb_find_devices *usb_check_each(int drvnum, struct device_drv *drv, struct libusb_device *dev)
- {
- struct usb_find_devices *found;
- int i;
- for (i = 0; find_dev[i].drv != DRV_LAST; i++)
- if (find_dev[i].drv == drvnum) {
- if (usb_check_device(drv, dev, &(find_dev[i]))) {
- found = malloc(sizeof(*found));
- if (unlikely(!found))
- quit(1, "USB failed to malloc found");
- memcpy(found, &(find_dev[i]), sizeof(*found));
- return found;
- }
- }
- return NULL;
- }
- static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv, __maybe_unused struct libusb_device *dev)
- {
- if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
- applog(LOG_DEBUG,
- "USB scan devices3: %s limit %d reached",
- drv->dname, drv_count[drv->drv_id].limit);
- return NULL;
- }
- #ifdef USE_BFLSC
- if (drv->drv_id == DRIVER_BFLSC)
- return usb_check_each(DRV_BFLSC, drv, dev);
- #endif
- #ifdef USE_BITFORCE
- if (drv->drv_id == DRIVER_BITFORCE)
- return usb_check_each(DRV_BITFORCE, drv, dev);
- #endif
- #ifdef USE_BITFURY
- if (drv->drv_id == DRIVER_BITFURY)
- return usb_check_each(DRV_BITFURY, drv, dev);
- #endif
- #ifdef USE_MODMINER
- if (drv->drv_id == DRIVER_MODMINER)
- return usb_check_each(DRV_MODMINER, drv, dev);
- #endif
- #ifdef USE_ICARUS
- if (drv->drv_id == DRIVER_ICARUS)
- return usb_check_each(DRV_ICARUS, drv, dev);
- #endif
- #ifdef USE_AVALON
- if (drv->drv_id == DRIVER_AVALON)
- return usb_check_each(DRV_AVALON, drv, dev);
- #endif
- return NULL;
- }
- void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
- {
- libusb_device **list;
- ssize_t count, i;
- struct usb_find_devices *found;
- applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
- if (total_count >= total_limit) {
- applog(LOG_DEBUG, "USB scan devices: total limit %d reached", total_limit);
- return;
- }
- if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
- applog(LOG_DEBUG,
- "USB scan devices: %s limit %d reached",
- drv->dname, drv_count[drv->drv_id].limit);
- return;
- }
- count = libusb_get_device_list(NULL, &list);
- if (count < 0) {
- applog(LOG_DEBUG, "USB scan devices: failed, err %d", (int)count);
- return;
- }
- if (count == 0)
- applog(LOG_DEBUG, "USB scan devices: found no devices");
- else
- cgsleep_ms(166);
- for (i = 0; i < count; i++) {
- if (total_count >= total_limit) {
- applog(LOG_DEBUG, "USB scan devices2: total limit %d reached", total_limit);
- break;
- }
- if (drv_count[drv->drv_id].count >= drv_count[drv->drv_id].limit) {
- applog(LOG_DEBUG,
- "USB scan devices2: %s limit %d reached",
- drv->dname, drv_count[drv->drv_id].limit);
- break;
- }
- found = usb_check(drv, list[i]);
- if (found != NULL) {
- if (is_in_use(list[i]) || cgminer_usb_lock(drv, list[i]) == false)
- free(found);
- else {
- if (!device_detect(list[i], found))
- cgminer_usb_unlock(drv, list[i]);
- else {
- total_count++;
- drv_count[drv->drv_id].count++;
- }
- free(found);
- }
- }
- }
- libusb_free_device_list(list, 1);
- }
- #if DO_USB_STATS
- static void modes_str(char *buf, uint32_t modes)
- {
- bool first;
- *buf = '\0';
- if (modes == MODE_NONE)
- strcpy(buf, MODE_NONE_STR);
- else {
- first = true;
- if (modes & MODE_CTRL_READ) {
- strcpy(buf, MODE_CTRL_READ_STR);
- first = false;
- }
- if (modes & MODE_CTRL_WRITE) {
- if (!first)
- strcat(buf, MODE_SEP_STR);
- strcat(buf, MODE_CTRL_WRITE_STR);
- first = false;
- }
- if (modes & MODE_BULK_READ) {
- if (!first)
- strcat(buf, MODE_SEP_STR);
- strcat(buf, MODE_BULK_READ_STR);
- first = false;
- }
- if (modes & MODE_BULK_WRITE) {
- if (!first)
- strcat(buf, MODE_SEP_STR);
- strcat(buf, MODE_BULK_WRITE_STR);
- first = false;
- }
- }
- }
- #endif
- // The stat data can be spurious due to not locking it before copying it -
- // however that would require the stat() function to also lock and release
- // a mutex every time a usb read or write is called which would slow
- // things down more
- struct api_data *api_usb_stats(__maybe_unused int *count)
- {
- #if DO_USB_STATS
- struct cg_usb_stats_details *details;
- struct cg_usb_stats *sta;
- struct api_data *root = NULL;
- int device;
- int cmdseq;
- char modes_s[32];
- if (next_stat == USB_NOSTAT)
- return NULL;
- while (*count < next_stat * C_MAX * 2) {
- device = *count / (C_MAX * 2);
- cmdseq = *count % (C_MAX * 2);
- (*count)++;
- sta = &(usb_stats[device]);
- details = &(sta->details[cmdseq]);
- // Only show stats that have results
- if (details->item[CMD_CMD].count == 0 &&
- details->item[CMD_TIMEOUT].count == 0 &&
- details->item[CMD_ERROR].count == 0)
- continue;
- root = api_add_string(root, "Name", sta->name, false);
- root = api_add_int(root, "ID", &(sta->device_id), false);
- root = api_add_const(root, "Stat", usb_commands[cmdseq/2], false);
- root = api_add_int(root, "Seq", &(details->seq), true);
- modes_str(modes_s, details->modes);
- root = api_add_string(root, "Modes", modes_s, true);
- root = api_add_uint64(root, "Count",
- &(details->item[CMD_CMD].count), true);
- root = api_add_double(root, "Total Delay",
- &(details->item[CMD_CMD].total_delay), true);
- root = api_add_double(root, "Min Delay",
- &(details->item[CMD_CMD].min_delay), true);
- root = api_add_double(root, "Max Delay",
- &(details->item[CMD_CMD].max_delay), true);
- root = api_add_uint64(root, "Timeout Count",
- &(details->item[CMD_TIMEOUT].count), true);
- root = api_add_double(root, "Timeout Total Delay",
- &(details->item[CMD_TIMEOUT].total_delay), true);
- root = api_add_double(root, "Timeout Min Delay",
- &(details->item[CMD_TIMEOUT].min_delay), true);
- root = api_add_double(root, "Timeout Max Delay",
- &(details->item[CMD_TIMEOUT].max_delay), true);
- root = api_add_uint64(root, "Error Count",
- &(details->item[CMD_ERROR].count), true);
- root = api_add_double(root, "Error Total Delay",
- &(details->item[CMD_ERROR].total_delay), true);
- root = api_add_double(root, "Error Min Delay",
- &(details->item[CMD_ERROR].min_delay), true);
- root = api_add_double(root, "Error Max Delay",
- &(details->item[CMD_ERROR].max_delay), true);
- root = api_add_timeval(root, "First Command",
- &(details->item[CMD_CMD].first), true);
- root = api_add_timeval(root, "Last Command",
- &(details->item[CMD_CMD].last), true);
- root = api_add_timeval(root, "First Timeout",
- &(details->item[CMD_TIMEOUT].first), true);
- root = api_add_timeval(root, "Last Timeout",
- &(details->item[CMD_TIMEOUT].last), true);
- root = api_add_timeval(root, "First Error",
- &(details->item[CMD_ERROR].first), true);
- root = api_add_timeval(root, "Last Error",
- &(details->item[CMD_ERROR].last), true);
- return root;
- }
- #endif
- return NULL;
- }
- #if DO_USB_STATS
- static void newstats(struct cgpu_info *cgpu)
- {
- int i;
- mutex_lock(&cgusb_lock);
- cgpu->usbinfo.usbstat = next_stat + 1;
- usb_stats = realloc(usb_stats, sizeof(*usb_stats) * (next_stat+1));
- if (unlikely(!usb_stats))
- quit(1, "USB failed to realloc usb_stats %d", next_stat+1);
- usb_stats[next_stat].name = cgpu->drv->name;
- usb_stats[next_stat].device_id = -1;
- usb_stats[next_stat].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
- if (unlikely(!usb_stats[next_stat].details))
- quit(1, "USB failed to calloc details for %d", next_stat+1);
- for (i = 1; i < C_MAX * 2; i += 2)
- usb_stats[next_stat].details[i].seq = 1;
- next_stat++;
- mutex_unlock(&cgusb_lock);
- }
- #endif
- void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
- {
- #if DO_USB_STATS
- if (cgpu->usbinfo.usbstat < 1)
- newstats(cgpu);
- // we don't know the device_id until after add_cgpu()
- usb_stats[cgpu->usbinfo.usbstat - 1].device_id = cgpu->device_id;
- #endif
- }
- #if DO_USB_STATS
- 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)
- {
- struct cg_usb_stats_details *details;
- double diff;
- int item, extrams;
- if (cgpu->usbinfo.usbstat < 1)
- newstats(cgpu);
- cgpu->usbinfo.tmo_count++;
- // timeout checks are only done when stats are enabled
- extrams = SECTOMS(tdiff(tv_finish, tv_start)) - timeout;
- if (extrams >= USB_TMO_0) {
- uint32_t totms = (uint32_t)(timeout + extrams);
- int offset = 0;
- if (extrams >= USB_TMO_2) {
- applog(LOG_ERR, "%s%i: TIMEOUT %s took %dms but was %dms",
- cgpu->drv->name, cgpu->device_id,
- usb_cmdname(cmd), totms, timeout) ;
- offset = 2;
- } else if (extrams >= USB_TMO_1)
- offset = 1;
- cgpu->usbinfo.usb_tmo[offset].count++;
- cgpu->usbinfo.usb_tmo[offset].total_over += extrams;
- cgpu->usbinfo.usb_tmo[offset].total_tmo += timeout;
- if (cgpu->usbinfo.usb_tmo[offset].min_tmo == 0) {
- cgpu->usbinfo.usb_tmo[offset].min_tmo = totms;
- cgpu->usbinfo.usb_tmo[offset].max_tmo = totms;
- } else {
- if (cgpu->usbinfo.usb_tmo[offset].min_tmo > totms)
- cgpu->usbinfo.usb_tmo[offset].min_tmo = totms;
- if (cgpu->usbinfo.usb_tmo[offset].max_tmo < totms)
- cgpu->usbinfo.usb_tmo[offset].max_tmo = totms;
- }
- }
- details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
- details->modes |= mode;
- diff = tdiff(tv_finish, tv_start);
- switch (err) {
- case LIBUSB_SUCCESS:
- item = CMD_CMD;
- break;
- case LIBUSB_ERROR_TIMEOUT:
- item = CMD_TIMEOUT;
- break;
- default:
- item = CMD_ERROR;
- break;
- }
- if (details->item[item].count == 0) {
- details->item[item].min_delay = diff;
- memcpy(&(details->item[item].first), tv_start, sizeof(*tv_start));
- } else if (diff < details->item[item].min_delay)
- details->item[item].min_delay = diff;
- if (diff > details->item[item].max_delay)
- details->item[item].max_delay = diff;
- details->item[item].total_delay += diff;
- memcpy(&(details->item[item].last), tv_start, sizeof(*tv_start));
- details->item[item].count++;
- }
- static void rejected_inc(struct cgpu_info *cgpu, uint32_t mode)
- {
- struct cg_usb_stats_details *details;
- int item = CMD_ERROR;
- if (cgpu->usbinfo.usbstat < 1)
- newstats(cgpu);
- details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
- details->modes |= mode;
- details->item[item].count++;
- }
- #endif
- static char *find_end(unsigned char *buf, unsigned char *ptr, int ptrlen, int tot, char *end, int endlen, bool first)
- {
- unsigned char *search;
- if (endlen > tot)
- return NULL;
- // If end is only 1 char - do a faster search
- if (endlen == 1) {
- if (first)
- search = buf;
- else
- search = ptr;
- return strchr((char *)search, *end);
- } else {
- if (first)
- search = buf;
- else {
- // must allow end to have been chopped in 2
- if ((tot - ptrlen) >= (endlen - 1))
- search = ptr - (endlen - 1);
- else
- search = ptr - (tot - ptrlen);
- }
- return strstr((char *)search, end);
- }
- }
- #define USB_MAX_READ 8192
- #define USB_RETRY_MAX 5
- static int
- usb_bulk_transfer(struct libusb_device_handle *dev_handle,
- unsigned char endpoint, unsigned char *data, int length,
- int *transferred, unsigned int timeout,
- struct cgpu_info *cgpu, __maybe_unused int mode,
- enum usb_cmds cmd, __maybe_unused int seq)
- {
- uint16_t MaxPacketSize;
- int err, errn, tries = 0;
- #if DO_USB_STATS
- struct timeval tv_start, tv_finish;
- #endif
- unsigned char *buf;
- /* Limit length of transfer to the largest this descriptor supports
- * and leave the higher level functions to transfer more if needed. */
- if (cgpu->usbdev->PrefPacketSize)
- MaxPacketSize = cgpu->usbdev->PrefPacketSize;
- else
- MaxPacketSize = cgpu->usbdev->found->wMaxPacketSize;
- if (length > MaxPacketSize)
- length = MaxPacketSize;
- buf = alloca(MaxPacketSize);
- if (endpoint == LIBUSB_ENDPOINT_OUT)
- memcpy(buf, data, length);
- STATS_TIMEVAL(&tv_start);
- cg_rlock(&cgusb_fd_lock);
- err = libusb_bulk_transfer(dev_handle, endpoint, data, length,
- transferred, timeout);
- errn = errno;
- cg_runlock(&cgusb_fd_lock);
- STATS_TIMEVAL(&tv_finish);
- USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq, timeout);
- if (err < 0)
- applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
- cgpu->drv->name, cgpu->device_id,
- usb_cmdname(cmd), *transferred, err, errn);
- if (err == LIBUSB_ERROR_PIPE) {
- cgpu->usbinfo.last_pipe = time(NULL);
- cgpu->usbinfo.pipe_count++;
- applog(LOG_INFO, "%s%i: libusb pipe error, trying to clear",
- cgpu->drv->name, cgpu->device_id);
- do {
- err = libusb_clear_halt(dev_handle, endpoint);
- if (unlikely(err == LIBUSB_ERROR_NOT_FOUND ||
- err == LIBUSB_ERROR_NO_DEVICE)) {
- cgpu->usbinfo.clear_err_count++;
- break;
- }
- STATS_TIMEVAL(&tv_start);
- cg_rlock(&cgusb_fd_lock);
- err = libusb_bulk_transfer(dev_handle, endpoint, data,
- length, transferred, timeout);
- errn = errno;
- cg_runlock(&cgusb_fd_lock);
- STATS_TIMEVAL(&tv_finish);
- USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq, timeout);
- if (err < 0)
- applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
- cgpu->drv->name, cgpu->device_id,
- usb_cmdname(cmd), *transferred, err, errn);
- if (err)
- cgpu->usbinfo.retry_err_count++;
- } while (err == LIBUSB_ERROR_PIPE && tries++ < USB_RETRY_MAX);
- applog(LOG_DEBUG, "%s%i: libusb pipe error%scleared",
- cgpu->drv->name, cgpu->device_id, err ? " not " : " ");
- if (err)
- cgpu->usbinfo.clear_fail_count++;
- }
- if (endpoint == LIBUSB_ENDPOINT_OUT)
- memcpy(data, buf, length);
- return err;
- }
- int _usb_read(struct cgpu_info *cgpu, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce)
- {
- struct cg_usb_device *usbdev;
- bool ftdi;
- struct timeval read_start, tv_finish;
- unsigned int initial_timeout;
- double max, done;
- int bufleft, err, got, tot, pstate;
- bool first = true;
- char *search;
- int endlen;
- unsigned char *ptr, *usbbuf = cgpu->usbinfo.bulkbuf;
- size_t usbbufread;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbinfo.nodev) {
- *buf = '\0';
- *processed = 0;
- USB_REJECT(cgpu, MODE_BULK_READ);
- err = LIBUSB_ERROR_NO_DEVICE;
- goto out_unlock;
- }
- usbdev = cgpu->usbdev;
- ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
- USBDEBUG("USB debug: _usb_read(%s (nodev=%s),epinfo=%d,buf=%p,bufsiz=%zu,proc=%p,timeout=%u,end=%s,cmd=%s,ftdi=%s,readonce=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), epinfo, buf, bufsiz, processed, timeout, end ? (char *)str_text((char *)end) : "NULL", usb_cmdname(cmd), bool_str(ftdi), bool_str(readonce));
- if (bufsiz > USB_MAX_READ)
- quit(1, "%s USB read request %d too large (max=%d)", cgpu->drv->name, (int)bufsiz, USB_MAX_READ);
- if (timeout == DEVTIMEOUT)
- timeout = usbdev->found->timeout;
- if (end == NULL) {
- if (usbdev->buffer && usbdev->bufamt) {
- tot = usbdev->bufamt;
- bufleft = bufsiz - tot;
- memcpy(usbbuf, usbdev->buffer, tot);
- ptr = usbbuf + tot;
- usbdev->bufamt = 0;
- } else {
- tot = 0;
- bufleft = bufsiz;
- ptr = usbbuf;
- }
- err = LIBUSB_SUCCESS;
- initial_timeout = timeout;
- max = ((double)timeout) / 1000.0;
- cgtime(&read_start);
- while (bufleft > 0) {
- // TODO: use (USB_MAX_READ - tot) always?
- if (usbdev->buffer)
- usbbufread = USB_MAX_READ - tot;
- else {
- if (ftdi)
- usbbufread = bufleft + 2;
- else
- usbbufread = bufleft;
- }
- got = 0;
- if (first && usbdev->usecps && usbdev->last_write_siz) {
- cgtimer_t now, already_done;
- double sleep_estimate;
- double write_time = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps);
- cgtimer_time(&now);
- cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
- sleep_estimate = write_time - cgtimer_to_ms(&already_done);
- if (sleep_estimate > 0.0) {
- cgsleep_ms_r(&usbdev->cgt_last_write, write_time * 1000.0);
- cgpu->usbinfo.read_delay_count++;
- cgpu->usbinfo.total_read_delay += sleep_estimate;
- }
- }
- err = usb_bulk_transfer(usbdev->handle,
- USBEP(usbdev, epinfo),
- ptr, usbbufread, &got, timeout,
- cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
- cgtime(&tv_finish);
- ptr[got] = '\0';
- USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
- IOERR_CHECK(cgpu, err);
- if (ftdi) {
- // first 2 bytes returned are an FTDI status
- if (got > 2) {
- got -= 2;
- memmove(ptr, ptr+2, got+1);
- } else {
- got = 0;
- *ptr = '\0';
- }
- }
- tot += got;
- if (err || readonce)
- break;
- ptr += got;
- bufleft -= got;
- first = false;
- done = tdiff(&tv_finish, &read_start);
- // N.B. this is: return LIBUSB_SUCCESS with whatever size has already been read
- if (unlikely(done >= max))
- break;
- timeout = initial_timeout - (done * 1000);
- if (!timeout)
- break;
- }
- // N.B. usbdev->buffer was emptied before the while() loop
- if (usbdev->buffer && tot > (int)bufsiz) {
- usbdev->bufamt = tot - bufsiz;
- memcpy(usbdev->buffer, usbbuf + bufsiz, usbdev->bufamt);
- tot -= usbdev->bufamt;
- usbbuf[tot] = '\0';
- applog(LOG_INFO, "USB: %s%i read1 buffering %d extra bytes",
- cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
- }
- *processed = tot;
- memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
- if (NODEV(err))
- release_cgpu(cgpu);
- goto out_unlock;
- }
- if (usbdev->buffer && usbdev->bufamt) {
- tot = usbdev->bufamt;
- bufleft = bufsiz - tot;
- memcpy(usbbuf, usbdev->buffer, tot);
- ptr = usbbuf + tot;
- usbdev->bufamt = 0;
- } else {
- tot = 0;
- bufleft = bufsiz;
- ptr = usbbuf;
- }
- endlen = strlen(end);
- err = LIBUSB_SUCCESS;
- initial_timeout = timeout;
- max = ((double)timeout) / 1000.0;
- cgtime(&read_start);
- while (bufleft > 0) {
- // TODO: use (USB_MAX_READ - tot) always?
- if (usbdev->buffer)
- usbbufread = USB_MAX_READ - tot;
- else {
- if (ftdi)
- usbbufread = bufleft + 2;
- else
- usbbufread = bufleft;
- }
- got = 0;
- if (first && usbdev->usecps && usbdev->last_write_siz) {
- cgtimer_t now, already_done;
- double sleep_estimate;
- double write_time = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps);
- cgtimer_time(&now);
- cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
- sleep_estimate = write_time - cgtimer_to_ms(&already_done);
- if (sleep_estimate > 0.0) {
- cgsleep_ms_r(&usbdev->cgt_last_write, write_time * 1000.0);
- cgpu->usbinfo.read_delay_count++;
- cgpu->usbinfo.total_read_delay += sleep_estimate;
- }
- }
- err = usb_bulk_transfer(usbdev->handle,
- USBEP(usbdev, epinfo), ptr,
- usbbufread, &got, timeout,
- cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
- cgtime(&tv_finish);
- ptr[got] = '\0';
- USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
- IOERR_CHECK(cgpu, err);
- if (ftdi) {
- // first 2 bytes returned are an FTDI status
- if (got > 2) {
- got -= 2;
- memmove(ptr, ptr+2, got+1);
- } else {
- got = 0;
- *ptr = '\0';
- }
- }
- tot += got;
- if (err || readonce)
- break;
- if (find_end(usbbuf, ptr, got, tot, (char *)end, endlen, first))
- break;
- ptr += got;
- bufleft -= got;
- first = false;
- done = tdiff(&tv_finish, &read_start);
- // N.B. this is: return LIBUSB_SUCCESS with whatever size has already been read
- if (unlikely(done >= max))
- break;
- timeout = initial_timeout - (done * 1000);
- if (!timeout)
- break;
- }
- if (usbdev->buffer) {
- bool dobuffer = false;
- if ((search = find_end(usbbuf, usbbuf, tot, tot, (char *)end, endlen, true))) {
- // end finishes after bufsiz
- if ((search + endlen - (char *)usbbuf) > (int)bufsiz) {
- usbdev->bufamt = tot - bufsiz;
- dobuffer = true;
- } else {
- // extra data after end
- if (*(search + endlen)) {
- usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
- dobuffer = true;
- }
- }
- } else {
- // no end, but still bigger than bufsiz
- if (tot > (int)bufsiz) {
- usbdev->bufamt = tot - bufsiz;
- dobuffer = true;
- }
- }
- if (dobuffer) {
- tot -= usbdev->bufamt;
- memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
- usbbuf[tot] = '\0';
- applog(LOG_ERR, "USB: %s%i read2 buffering %d extra bytes",
- cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
- }
- }
- *processed = tot;
- memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);
- if (NODEV(err))
- release_cgpu(cgpu);
- out_unlock:
- DEVUNLOCK(cgpu, pstate);
- return err;
- }
- int _usb_write(struct cgpu_info *cgpu, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
- {
- struct cg_usb_device *usbdev;
- struct timeval read_start, tv_finish;
- unsigned int initial_timeout;
- double max, done;
- __maybe_unused bool first = true;
- int err, sent, tot, pstate;
- DEVLOCK(cgpu, pstate);
- USBDEBUG("USB debug: _usb_write(%s (nodev=%s),epinfo=%d,buf='%s',bufsiz=%zu,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), epinfo, (char *)str_text(buf), bufsiz, processed, timeout, usb_cmdname(cmd));
- *processed = 0;
- if (cgpu->usbinfo.nodev) {
- USB_REJECT(cgpu, MODE_BULK_WRITE);
- err = LIBUSB_ERROR_NO_DEVICE;
- goto out_unlock;
- }
- usbdev = cgpu->usbdev;
- if (timeout == DEVTIMEOUT)
- timeout = usbdev->found->timeout;
- tot = 0;
- err = LIBUSB_SUCCESS;
- initial_timeout = timeout;
- max = ((double)timeout) / 1000.0;
- cgtime(&read_start);
- while (bufsiz > 0) {
- sent = 0;
- if (usbdev->usecps) {
- if (usbdev->last_write_siz) {
- cgtimer_t now, already_done;
- double sleep_estimate;
- double write_time = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps);
- cgtimer_time(&now);
- cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
- sleep_estimate = write_time - cgtimer_to_ms(&already_done);
- if (sleep_estimate > 0.0) {
- cgsleep_ms_r(&usbdev->cgt_last_write, write_time * 1000.0);
- cgpu->usbinfo.write_delay_count++;
- cgpu->usbinfo.total_write_delay += sleep_estimate;
- }
- }
- cgsleep_prepare_r(&usbdev->cgt_last_write);
- usbdev->last_write_siz = bufsiz;
- }
- err = usb_bulk_transfer(usbdev->handle,
- USBEP(usbdev, epinfo),
- (unsigned char *)buf, bufsiz, &sent,
- timeout, cgpu, MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1);
- cgtime(&tv_finish);
- 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);
- IOERR_CHECK(cgpu, err);
- tot += sent;
- if (err)
- break;
- buf += sent;
- bufsiz -= sent;
- first = false;
- done = tdiff(&tv_finish, &read_start);
- // N.B. this is: return LIBUSB_SUCCESS with whatever size was written
- if (unlikely(done >= max))
- break;
- timeout = initial_timeout - (done * 1000);
- if (!timeout)
- break;
- }
- *processed = tot;
- if (NODEV(err))
- release_cgpu(cgpu);
- out_unlock:
- DEVUNLOCK(cgpu, pstate);
- return err;
- }
- 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)
- {
- struct cg_usb_device *usbdev;
- #if DO_USB_STATS
- struct timeval tv_start, tv_finish;
- #endif
- unsigned char buf[64];
- uint32_t *buf32 = (uint32_t *)buf;
- int err, i, bufsiz;
- 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));
- if (cgpu->usbinfo.nodev) {
- USB_REJECT(cgpu, MODE_CTRL_WRITE);
- err = LIBUSB_ERROR_NO_DEVICE;
- goto out_;
- }
- usbdev = cgpu->usbdev;
- if (timeout == DEVTIMEOUT)
- timeout = usbdev->found->timeout;
- USBDEBUG("USB debug: @_usb_transfer() data=%s", bin2hex((unsigned char *)data, (size_t)siz));
- if (siz > 0) {
- bufsiz = siz - 1;
- bufsiz >>= 2;
- bufsiz++;
- for (i = 0; i < bufsiz; i++)
- buf32[i] = htole32(data[i]);
- }
- USBDEBUG("USB debug: @_usb_transfer() buf=%s", bin2hex(buf, (size_t)siz));
- if (usbdev->usecps) {
- if (usbdev->last_write_siz) {
- cgtimer_t now, already_done;
- double sleep_estimate;
- double write_time = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps);
- cgtimer_time(&now);
- cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
- sleep_estimate = write_time - cgtimer_to_ms(&already_done);
- if (sleep_estimate > 0.0) {
- cgsleep_ms_r(&usbdev->cgt_last_write, write_time * 1000.0);
- cgpu->usbinfo.write_delay_count++;
- cgpu->usbinfo.total_write_delay += sleep_estimate;
- }
- }
- cgsleep_prepare_r(&usbdev->cgt_last_write);
- usbdev->last_write_siz = siz;
- }
- STATS_TIMEVAL(&tv_start);
- cg_rlock(&cgusb_fd_lock);
- err = libusb_control_transfer(usbdev->handle, request_type,
- bRequest, wValue, wIndex, buf, (uint16_t)siz, timeout);
- cg_runlock(&cgusb_fd_lock);
- STATS_TIMEVAL(&tv_finish);
- USB_STATS(cgpu, &tv_start, &tv_finish, err, MODE_CTRL_WRITE, cmd, SEQ0, timeout);
- USBDEBUG("USB debug: @_usb_transfer(%s (nodev=%s)) err=%d%s", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err));
- IOERR_CHECK(cgpu, err);
- if (NOCONTROLDEV(err))
- release_cgpu(cgpu);
- out_:
- return err;
- }
- 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)
- {
- int pstate, err;
- DEVLOCK(cgpu, pstate);
- err = __usb_transfer(cgpu, request_type, bRequest, wValue, wIndex, data, siz, timeout, cmd);
- DEVUNLOCK(cgpu, pstate);
- return err;
- }
- 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)
- {
- struct cg_usb_device *usbdev;
- #if DO_USB_STATS
- struct timeval tv_start, tv_finish;
- #endif
- unsigned char tbuf[64];
- int err, pstate;
- DEVLOCK(cgpu, pstate);
- 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));
- if (cgpu->usbinfo.nodev) {
- USB_REJECT(cgpu, MODE_CTRL_READ);
- err = LIBUSB_ERROR_NO_DEVICE;
- goto out_unlock;
- }
- usbdev = cgpu->usbdev;
- if (timeout == DEVTIMEOUT)
- timeout = usbdev->found->timeout;
- *amount = 0;
- if (usbdev->usecps && usbdev->last_write_siz) {
- cgtimer_t now, already_done;
- double sleep_estimate;
- double write_time = (double)(usbdev->last_write_siz) /
- (double)(usbdev->cps);
- cgtimer_time(&now);
- cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
- sleep_estimate = write_time - cgtimer_to_ms(&already_done);
- if (sleep_estimate > 0.0) {
- cgsleep_ms_r(&usbdev->cgt_last_write, write_time * 1000.0);
- cgpu->usbinfo.read_delay_count++;
- cgpu->usbinfo.total_read_delay += sleep_estimate;
- }
- }
- memset(tbuf, 0, 64);
- STATS_TIMEVAL(&tv_start);
- cg_rlock(&cgusb_fd_lock);
- err = libusb_control_transfer(usbdev->handle, request_type,
- bRequest, wValue, wIndex,
- tbuf, (uint16_t)bufsiz, timeout);
- cg_runlock(&cgusb_fd_lock);
- STATS_TIMEVAL(&tv_finish);
- USB_STATS(cgpu, &tv_start, &tv_finish, err, MODE_CTRL_READ, cmd, SEQ0, timeout);
- memcpy(buf, tbuf, bufsiz);
- 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);
- IOERR_CHECK(cgpu, err);
- if (err > 0) {
- *amount = err;
- err = 0;
- } else if (NOCONTROLDEV(err))
- release_cgpu(cgpu);
- out_unlock:
- DEVUNLOCK(cgpu, pstate);
- return err;
- }
- #define FTDI_STATUS_B0_MASK (FTDI_RS0_CTS | FTDI_RS0_DSR | FTDI_RS0_RI | FTDI_RS0_RLSD)
- #define FTDI_RS0_CTS (1 << 4)
- #define FTDI_RS0_DSR (1 << 5)
- #define FTDI_RS0_RI (1 << 6)
- #define FTDI_RS0_RLSD (1 << 7)
- /* Clear to send for FTDI */
- int usb_ftdi_cts(struct cgpu_info *cgpu)
- {
- char buf[2], ret;
- int err, amount;
- err = _usb_transfer_read(cgpu, (uint8_t)FTDI_TYPE_IN, (uint8_t)5,
- (uint16_t)0, (uint16_t)0, buf, 2,
- &amount, DEVTIMEOUT, C_FTDI_STATUS);
- /* We return true in case drivers are waiting indefinitely to try and
- * write to something that's not there. */
- if (err)
- return true;
- ret = buf[0] & FTDI_STATUS_B0_MASK;
- return (ret & FTDI_RS0_CTS);
- }
- int usb_ftdi_set_latency(struct cgpu_info *cgpu)
- {
- int err = 0;
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev) {
- if (cgpu->usbdev->usb_type != USB_TYPE_FTDI) {
- applog(LOG_ERR, "%s: cgid %d latency request on non-FTDI device",
- cgpu->drv->name, cgpu->cgminer_id);
- err = LIBUSB_ERROR_NOT_SUPPORTED;
- } else if (cgpu->usbdev->found->latency == LATENCY_UNUSED) {
- applog(LOG_ERR, "%s: cgid %d invalid latency (UNUSED)",
- cgpu->drv->name, cgpu->cgminer_id);
- err = LIBUSB_ERROR_NOT_SUPPORTED;
- }
- if (!err)
- err = __usb_transfer(cgpu, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
- cgpu->usbdev->found->latency,
- USBIF(cgpu->usbdev),
- NULL, 0, DEVTIMEOUT, C_LATENCY);
- }
- DEVUNLOCK(cgpu, pstate);
- applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
- cgpu->drv->name, cgpu->cgminer_id,
- usb_cmdname(C_LATENCY), err);
- return err;
- }
- void usb_buffer_enable(struct cgpu_info *cgpu)
- {
- struct cg_usb_device *cgusb;
- int pstate;
- DEVLOCK(cgpu, pstate);
- cgusb = cgpu->usbdev;
- if (cgusb && !cgusb->buffer) {
- cgusb->bufamt = 0;
- cgusb->buffer = malloc(USB_MAX_READ+1);
- if (!cgusb->buffer)
- quit(1, "Failed to malloc buffer for USB %s%i",
- cgpu->drv->name, cgpu->device_id);
- cgusb->bufsiz = USB_MAX_READ;
- }
- DEVUNLOCK(cgpu, pstate);
- }
- void usb_buffer_disable(struct cgpu_info *cgpu)
- {
- struct cg_usb_device *cgusb;
- int pstate;
- DEVLOCK(cgpu, pstate);
- cgusb = cgpu->usbdev;
- if (cgusb && cgusb->buffer) {
- cgusb->bufamt = 0;
- cgusb->bufsiz = 0;
- free(cgusb->buffer);
- cgusb->buffer = NULL;
- }
- DEVUNLOCK(cgpu, pstate);
- }
- void usb_buffer_clear(struct cgpu_info *cgpu)
- {
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev)
- cgpu->usbdev->bufamt = 0;
- DEVUNLOCK(cgpu, pstate);
- }
- uint32_t usb_buffer_size(struct cgpu_info *cgpu)
- {
- uint32_t ret = 0;
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev)
- ret = cgpu->usbdev->bufamt;
- DEVUNLOCK(cgpu, pstate);
- return ret;
- }
- void usb_set_cps(struct cgpu_info *cgpu, int cps)
- {
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev)
- cgpu->usbdev->cps = cps;
- DEVUNLOCK(cgpu, pstate);
- }
- void usb_enable_cps(struct cgpu_info *cgpu)
- {
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev)
- cgpu->usbdev->usecps = true;
- DEVUNLOCK(cgpu, pstate);
- }
- void usb_disable_cps(struct cgpu_info *cgpu)
- {
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev)
- cgpu->usbdev->usecps = false;
- DEVUNLOCK(cgpu, pstate);
- }
- /*
- * The value returned (0) when usbdev is NULL
- * doesn't matter since it also means the next call to
- * any usbutils function will fail with a nodev
- */
- int usb_interface(struct cgpu_info *cgpu)
- {
- int interface = 0;
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev)
- interface = USBIF(cgpu->usbdev);
- DEVUNLOCK(cgpu, pstate);
- return interface;
- }
- enum sub_ident usb_ident(struct cgpu_info *cgpu)
- {
- enum sub_ident ident = IDENT_UNK;
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev)
- ident = cgpu->usbdev->ident;
- DEVUNLOCK(cgpu, pstate);
- return ident;
- }
- void usb_set_pps(struct cgpu_info *cgpu, uint16_t PrefPacketSize)
- {
- int pstate;
- DEVLOCK(cgpu, pstate);
- if (cgpu->usbdev)
- cgpu->usbdev->PrefPacketSize = PrefPacketSize;
- DEVUNLOCK(cgpu, pstate);
- }
- // Need to set all devices with matching usbdev
- void usb_set_dev_start(struct cgpu_info *cgpu)
- {
- struct cg_usb_device *cgusb;
- struct cgpu_info *cgpu2;
- struct timeval now;
- int pstate;
- DEVLOCK(cgpu, pstate);
- cgusb = cgpu->usbdev;
- // If the device wasn't dropped
- if (cgusb != NULL) {
- int i;
- cgtime(&now);
- for (i = 0; i < total_devices; i++) {
- cgpu2 = get_devices(i);
- if (cgpu2->usbdev == cgusb)
- copy_time(&(cgpu2->dev_start_tv), &now);
- }
- }
- DEVUNLOCK(cgpu, pstate);
- }
- void usb_cleanup()
- {
- struct cgpu_info *cgpu;
- int count;
- int i;
- hotplug_time = 0;
- cgsleep_ms(10);
- count = 0;
- for (i = 0; i < total_devices; i++) {
- cgpu = devices[i];
- switch (cgpu->drv->drv_id) {
- case DRIVER_BFLSC:
- case DRIVER_BITFORCE:
- case DRIVER_BITFURY:
- case DRIVER_MODMINER:
- case DRIVER_ICARUS:
- case DRIVER_AVALON:
- wr_lock(cgpu->usbinfo.devlock);
- release_cgpu(cgpu);
- wr_unlock(cgpu->usbinfo.devlock);
- count++;
- break;
- default:
- break;
- }
- }
- /*
- * Must attempt to wait for the resource thread to release coz
- * during a restart it won't automatically release them in linux
- */
- if (count) {
- struct timeval start, now;
- cgtime(&start);
- while (42) {
- cgsleep_ms(50);
- mutex_lock(&cgusbres_lock);
- if (!res_work_head)
- break;
- cgtime(&now);
- if (tdiff(&now, &start) > 0.366) {
- applog(LOG_WARNING,
- "usb_cleanup gave up waiting for resource thread");
- break;
- }
- mutex_unlock(&cgusbres_lock);
- }
- mutex_unlock(&cgusbres_lock);
- }
- cgsem_destroy(&usb_resource_sem);
- }
- void usb_initialise()
- {
- char *fre, *ptr, *comma, *colon;
- int bus, dev, lim, i;
- bool found;
- for (i = 0; i < DRIVER_MAX; i++) {
- drv_count[i].count = 0;
- drv_count[i].limit = 999999;
- }
- cgusb_check_init();
- if (opt_usb_select && *opt_usb_select) {
- // Absolute device limit
- if (*opt_usb_select == ':') {
- total_limit = atoi(opt_usb_select+1);
- if (total_limit < 0)
- quit(1, "Invalid --usb total limit");
- // Comma list of bus:dev devices to match
- } else if (isdigit(*opt_usb_select)) {
- fre = ptr = strdup(opt_usb_select);
- do {
- comma = strchr(ptr, ',');
- if (comma)
- *(comma++) = '\0';
- colon = strchr(ptr, ':');
- if (!colon)
- quit(1, "Invalid --usb bus:dev missing ':'");
- *(colon++) = '\0';
- if (!isdigit(*ptr))
- quit(1, "Invalid --usb bus:dev - bus must be a number");
- if (!isdigit(*colon) && *colon != '*')
- quit(1, "Invalid --usb bus:dev - dev must be a number or '*'");
- bus = atoi(ptr);
- if (bus <= 0)
- quit(1, "Invalid --usb bus:dev - bus must be > 0");
- if (*colon == '*')
- dev = -1;
- else {
- dev = atoi(colon);
- if (dev <= 0)
- quit(1, "Invalid --usb bus:dev - dev must be > 0 or '*'");
- }
- busdev = realloc(busdev, sizeof(*busdev) * (++busdev_count));
- if (unlikely(!busdev))
- quit(1, "USB failed to realloc busdev");
- busdev[busdev_count-1].bus_number = bus;
- busdev[busdev_count-1].device_address = dev;
- ptr = comma;
- } while (ptr);
- free(fre);
- // Comma list of DRV:limit
- } else {
- fre = ptr = strdup(opt_usb_select);
- do {
- comma = strchr(ptr, ',');
- if (comma)
- *(comma++) = '\0';
- colon = strchr(ptr, ':');
- if (!colon)
- quit(1, "Invalid --usb DRV:limit missing ':'");
- *(colon++) = '\0';
- if (!isdigit(*colon))
- quit(1, "Invalid --usb DRV:limit - limit must be a number");
- lim = atoi(colon);
- if (lim < 0)
- quit(1, "Invalid --usb DRV:limit - limit must be >= 0");
- found = false;
- #ifdef USE_BFLSC
- if (strcasecmp(ptr, bflsc_drv.name) == 0) {
- drv_count[bflsc_drv.drv_id].limit = lim;
- found = true;
- }
- #endif
- #ifdef USE_BITFORCE
- if (!found && strcasecmp(ptr, bitforce_drv.name) == 0) {
- drv_count[bitforce_drv.drv_id].limit = lim;
- found = true;
- }
- #endif
- #ifdef USE_BITFURY
- if (!found && strcasecmp(ptr, bitfury_drv.name) == 0) {
- drv_count[bitfury_drv.drv_id].limit = lim;
- found = true;
- }
- #endif
- #ifdef USE_MODMINER
- if (!found && strcasecmp(ptr, modminer_drv.name) == 0) {
- drv_count[modminer_drv.drv_id].limit = lim;
- found = true;
- }
- #endif
- #ifdef USE_ICARUS
- if (!found && strcasecmp(ptr, icarus_drv.name) == 0) {
- drv_count[icarus_drv.drv_id].limit = lim;
- found = true;
- }
- #endif
- #ifdef USE_AVALON
- if (!found && strcasecmp(ptr, avalon_drv.name) == 0) {
- drv_count[avalon_drv.drv_id].limit = lim;
- found = true;
- }
- #endif
- if (!found)
- quit(1, "Invalid --usb DRV:limit - unknown DRV='%s'", ptr);
- ptr = comma;
- } while (ptr);
- free(fre);
- }
- }
- }
- #ifndef WIN32
- #include <errno.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #ifndef __APPLE__
- union semun {
- int val;
- struct semid_ds *buf;
- unsigned short *array;
- struct seminfo *__buf;
- };
- #endif
- #else
- static LPSECURITY_ATTRIBUTES unsec(LPSECURITY_ATTRIBUTES sec)
- {
- FreeSid(((PSECURITY_DESCRIPTOR)(sec->lpSecurityDescriptor))->Group);
- free(sec->lpSecurityDescriptor);
- free(sec);
- return NULL;
- }
- static LPSECURITY_ATTRIBUTES mksec(const char *dname, uint8_t bus_number, uint8_t device_address)
- {
- SID_IDENTIFIER_AUTHORITY SIDAuthWorld = {SECURITY_WORLD_SID_AUTHORITY};
- PSID gsid = NULL;
- LPSECURITY_ATTRIBUTES sec_att = NULL;
- PSECURITY_DESCRIPTOR sec_des = NULL;
- sec_des = malloc(sizeof(*sec_des));
- if (unlikely(!sec_des))
- quit(1, "MTX: Failed to malloc LPSECURITY_DESCRIPTOR");
- if (!InitializeSecurityDescriptor(sec_des, SECURITY_DESCRIPTOR_REVISION)) {
- applog(LOG_ERR,
- "MTX: %s (%d:%d) USB failed to init secdes err (%d)",
- dname, (int)bus_number, (int)device_address,
- (int)GetLastError());
- free(sec_des);
- return NULL;
- }
- if (!SetSecurityDescriptorDacl(sec_des, TRUE, NULL, FALSE)) {
- applog(LOG_ERR,
- "MTX: %s (%d:%d) USB failed to secdes dacl err (%d)",
- dname, (int)bus_number, (int)device_address,
- (int)GetLastError());
- free(sec_des);
- return NULL;
- }
- if(!AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &gsid)) {
- applog(LOG_ERR,
- "MTX: %s (%d:%d) USB failed to create gsid err (%d)",
- dname, (int)bus_number, (int)device_address,
- (int)GetLastError());
- free(sec_des);
- return NULL;
- }
- if (!SetSecurityDescriptorGroup(sec_des, gsid, FALSE)) {
- applog(LOG_ERR,
- "MTX: %s (%d:%d) USB failed to secdes grp err (%d)",
- dname, (int)bus_number, (int)device_address,
- (int)GetLastError());
- FreeSid(gsid);
- free(sec_des);
- return NULL;
- }
- sec_att = malloc(sizeof(*sec_att));
- if (unlikely(!sec_att))
- quit(1, "MTX: Failed to malloc LPSECURITY_ATTRIBUTES");
- sec_att->nLength = sizeof(*sec_att);
- sec_att->lpSecurityDescriptor = sec_des;
- sec_att->bInheritHandle = FALSE;
- return sec_att;
- }
- #endif
- // Any errors should always be printed since they will rarely if ever occur
- // and thus it is best to always display them
- static bool resource_lock(const char *dname, uint8_t bus_number, uint8_t device_address)
- {
- applog(LOG_DEBUG, "USB res lock %s %d-%d", dname, (int)bus_number, (int)device_address);
- #ifdef WIN32
- struct cgpu_info *cgpu;
- LPSECURITY_ATTRIBUTES sec;
- HANDLE usbMutex;
- char name[64];
- DWORD res;
- int i;
- if (is_in_use_bd(bus_number, device_address))
- return false;
- snprintf(name, sizeof(name), "cg-usb-%d-%d", (int)bus_number, (int)device_address);
- sec = mksec(dname, bus_number, device_address);
- if (!sec)
- return false;
- usbMutex = CreateMutex(sec, FALSE, name);
- if (usbMutex == NULL) {
- applog(LOG_ERR,
- "MTX: %s USB failed to get '%s' err (%d)",
- dname, name, (int)GetLastError());
- sec = unsec(sec);
- return false;
- }
- res = WaitForSingleObject(usbMutex, 0);
- switch(res) {
- case WAIT_OBJECT_0:
- case WAIT_ABANDONED:
- // Am I using it already?
- for (i = 0; i < total_devices; i++) {
- cgpu = get_devices(i);
- if (cgpu->usbinfo.bus_number == bus_number &&
- cgpu->usbinfo.device_address == device_address &&
- cgpu->usbinfo.nodev == false) {
- if (ReleaseMutex(usbMutex)) {
- applog(LOG_WARNING,
- "MTX: %s USB can't get '%s' - device in use",
- dname, name);
- goto fail;
- }
- applog(LOG_ERR,
- "MTX: %s USB can't get '%s' - device in use - failure (%d)",
- dname, name, (int)GetLastError());
- goto fail;
- }
- }
- break;
- case WAIT_TIMEOUT:
- if (!hotplug_mode)
- applog(LOG_WARNING,
- "MTX: %s USB failed to get '%s' - device in use",
- dname, name);
- goto fail;
- case WAIT_FAILED:
- applog(LOG_ERR,
- "MTX: %s USB failed to get '%s' err (%d)",
- dname, name, (int)GetLastError());
- goto fail;
- default:
- applog(LOG_ERR,
- "MTX: %s USB failed to get '%s' unknown reply (%d)",
- dname, name, (int)res);
- goto fail;
- }
- add_in_use(bus_number, device_address);
- in_use_store_ress(bus_number, device_address, (void *)usbMutex, (void *)sec);
- return true;
- fail:
- CloseHandle(usbMutex);
- sec = unsec(sec);
- return false;
- #else
- struct semid_ds seminfo;
- union semun opt;
- char name[64];
- key_t *key;
- int *sem;
- int fd, count;
- if (is_in_use_bd(bus_number, device_address))
- return false;
- snprintf(name, sizeof(name), "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
- fd = open(name, O_CREAT|O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
- if (fd == -1) {
- applog(LOG_ERR,
- "SEM: %s USB open failed '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- goto _out;
- }
- close(fd);
- key = malloc(sizeof(*key));
- if (unlikely(!key))
- quit(1, "SEM: Failed to malloc key");
- sem = malloc(sizeof(*sem));
- if (unlikely(!sem))
- quit(1, "SEM: Failed to malloc sem");
- *key = ftok(name, 'K');
- *sem = semget(*key, 1, IPC_CREAT | IPC_EXCL | 438);
- if (*sem < 0) {
- if (errno != EEXIST) {
- applog(LOG_ERR,
- "SEM: %s USB failed to get '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- goto free_out;
- }
- *sem = semget(*key, 1, 0);
- if (*sem < 0) {
- applog(LOG_ERR,
- "SEM: %s USB failed to access '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- goto free_out;
- }
- opt.buf = &seminfo;
- count = 0;
- while (++count) {
- // Should NEVER take 100ms
- if (count > 99) {
- applog(LOG_ERR,
- "SEM: %s USB timeout waiting for (%d) '%s'",
- dname, *sem, name);
- goto free_out;
- }
- if (semctl(*sem, 0, IPC_STAT, opt) == -1) {
- applog(LOG_ERR,
- "SEM: %s USB failed to wait for (%d) '%s' count %d err (%d) %s",
- dname, *sem, name, count, errno, strerror(errno));
- goto free_out;
- }
- if (opt.buf->sem_otime != 0)
- break;
- cgsleep_ms(1);
- }
- }
- struct sembuf sops[] = {
- { 0, 0, IPC_NOWAIT | SEM_UNDO },
- { 0, 1, IPC_NOWAIT | SEM_UNDO }
- };
- if (semop(*sem, sops, 2)) {
- if (errno == EAGAIN) {
- if (!hotplug_mode)
- applog(LOG_WARNING,
- "SEM: %s USB failed to get (%d) '%s' - device in use",
- dname, *sem, name);
- } else {
- applog(LOG_DEBUG,
- "SEM: %s USB failed to get (%d) '%s' err (%d) %s",
- dname, *sem, name, errno, strerror(errno));
- }
- goto free_out;
- }
- add_in_use(bus_number, device_address);
- in_use_store_ress(bus_number, device_address, (void *)key, (void *)sem);
- return true;
- free_out:
- free(sem);
- free(key);
- _out:
- return false;
- #endif
- }
- // Any errors should always be printed since they will rarely if ever occur
- // and thus it is best to always display them
- static void resource_unlock(const char *dname, uint8_t bus_number, uint8_t device_address)
- {
- applog(LOG_DEBUG, "USB res unlock %s %d-%d", dname, (int)bus_number, (int)device_address);
- #ifdef WIN32
- LPSECURITY_ATTRIBUTES sec = NULL;
- HANDLE usbMutex = NULL;
- char name[64];
- snprintf(name, sizeof(name), "cg-usb-%d-%d", (int)bus_number, (int)device_address);
- in_use_get_ress(bus_number, device_address, (void **)(&usbMutex), (void **)(&sec));
- if (!usbMutex || !sec)
- goto fila;
- if (!ReleaseMutex(usbMutex))
- applog(LOG_ERR,
- "MTX: %s USB failed to release '%s' err (%d)",
- dname, name, (int)GetLastError());
- fila:
- if (usbMutex)
- CloseHandle(usbMutex);
- if (sec)
- unsec(sec);
- remove_in_use(bus_number, device_address);
- return;
- #else
- char name[64];
- key_t *key = NULL;
- int *sem = NULL;
- snprintf(name, sizeof(name), "/tmp/cgminer-usb-%d-%d", (int)bus_number, (int)device_address);
- in_use_get_ress(bus_number, device_address, (void **)(&key), (void **)(&sem));
- if (!key || !sem)
- goto fila;
- struct sembuf sops[] = {
- { 0, -1, SEM_UNDO }
- };
- // Allow a 10ms timeout
- // exceeding this timeout means it would probably never succeed anyway
- struct timespec timeout = { 0, 10000000 };
- if (semtimedop(*sem, sops, 1, &timeout)) {
- applog(LOG_ERR,
- "SEM: %s USB failed to release '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- }
- if (semctl(*sem, 0, IPC_RMID)) {
- applog(LOG_WARNING,
- "SEM: %s USB failed to remove SEM '%s' err (%d) %s",
- dname, name, errno, strerror(errno));
- }
- fila:
- free(sem);
- free(key);
- remove_in_use(bus_number, device_address);
- return;
- #endif
- }
- static void resource_process()
- {
- struct resource_work *res_work = NULL;
- struct resource_reply *res_reply = NULL;
- bool ok;
- applog(LOG_DEBUG, "RES: %s (%d:%d) lock=%d",
- res_work_head->dname,
- (int)res_work_head->bus_number,
- (int)res_work_head->device_address,
- res_work_head->lock);
- if (res_work_head->lock) {
- ok = resource_lock(res_work_head->dname,
- res_work_head->bus_number,
- res_work_head->device_address);
- applog(LOG_DEBUG, "RES: %s (%d:%d) lock ok=%d",
- res_work_head->dname,
- (int)res_work_head->bus_number,
- (int)res_work_head->device_address,
- ok);
- res_reply = calloc(1, sizeof(*res_reply));
- if (unlikely(!res_reply))
- quit(1, "USB failed to calloc res_reply");
- res_reply->bus_number = res_work_head->bus_number;
- res_reply->device_address = res_work_head->device_address;
- res_reply->got = ok;
- res_reply->next = res_reply_head;
- res_reply_head = res_reply;
- }
- else
- resource_unlock(res_work_head->dname,
- res_work_head->bus_number,
- res_work_head->device_address);
- res_work = res_work_head;
- res_work_head = res_work_head->next;
- free(res_work);
- }
- void *usb_resource_thread(void __maybe_unused *userdata)
- {
- pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
- RenameThread("usbresource");
- applog(LOG_DEBUG, "RES: thread starting");
- while (42) {
- /* Wait to be told we have work to do */
- cgsem_wait(&usb_resource_sem);
- mutex_lock(&cgusbres_lock);
- while (res_work_head)
- resource_process();
- mutex_unlock(&cgusbres_lock);
- }
- return NULL;
- }
|