hub.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233
  1. /*
  2. * USB hub driver.
  3. *
  4. * (C) Copyright 1999 Linus Torvalds
  5. * (C) Copyright 1999 Johannes Erdfelt
  6. * (C) Copyright 1999 Gregory P. Smith
  7. * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
  8. *
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/completion.h>
  15. #include <linux/sched.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/ioctl.h>
  19. #include <linux/usb.h>
  20. #include <linux/usbdevice_fs.h>
  21. #include <linux/usb/hcd.h>
  22. #include <linux/usb/quirks.h>
  23. #include <linux/kthread.h>
  24. #include <linux/mutex.h>
  25. #include <linux/freezer.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/byteorder.h>
  28. #include "usb.h"
  29. /* if we are in debug mode, always announce new devices */
  30. #ifdef DEBUG
  31. #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
  32. #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
  33. #endif
  34. #endif
  35. struct usb_hub {
  36. struct device *intfdev; /* the "interface" device */
  37. struct usb_device *hdev;
  38. struct kref kref;
  39. struct urb *urb; /* for interrupt polling pipe */
  40. /* buffer for urb ... with extra space in case of babble */
  41. char (*buffer)[8];
  42. union {
  43. struct usb_hub_status hub;
  44. struct usb_port_status port;
  45. } *status; /* buffer for status reports */
  46. struct mutex status_mutex; /* for the status buffer */
  47. int error; /* last reported error */
  48. int nerrors; /* track consecutive errors */
  49. struct list_head event_list; /* hubs w/data or errs ready */
  50. unsigned long event_bits[1]; /* status change bitmask */
  51. unsigned long change_bits[1]; /* ports with logical connect
  52. status change */
  53. unsigned long busy_bits[1]; /* ports being reset or
  54. resumed */
  55. unsigned long removed_bits[1]; /* ports with a "removed"
  56. device present */
  57. unsigned long wakeup_bits[1]; /* ports that have signaled
  58. remote wakeup */
  59. #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
  60. #error event_bits[] is too short!
  61. #endif
  62. struct usb_hub_descriptor *descriptor; /* class descriptor */
  63. struct usb_tt tt; /* Transaction Translator */
  64. unsigned mA_per_port; /* current for each child */
  65. unsigned limited_power:1;
  66. unsigned quiescing:1;
  67. unsigned disconnected:1;
  68. unsigned has_indicators:1;
  69. u8 indicator[USB_MAXCHILDREN];
  70. struct delayed_work leds;
  71. struct delayed_work init_work;
  72. void **port_owners;
  73. };
  74. static inline int hub_is_superspeed(struct usb_device *hdev)
  75. {
  76. return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
  77. }
  78. /* Protect struct usb_device->state and ->children members
  79. * Note: Both are also protected by ->dev.sem, except that ->state can
  80. * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
  81. static DEFINE_SPINLOCK(device_state_lock);
  82. /* khubd's worklist and its lock */
  83. static DEFINE_SPINLOCK(hub_event_lock);
  84. static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
  85. /* Wakes up khubd */
  86. static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
  87. static struct task_struct *khubd_task;
  88. /* cycle leds on hubs that aren't blinking for attention */
  89. static bool blinkenlights = 0;
  90. module_param (blinkenlights, bool, S_IRUGO);
  91. MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
  92. /*
  93. * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
  94. * 10 seconds to send reply for the initial 64-byte descriptor request.
  95. */
  96. /* define initial 64-byte descriptor request timeout in milliseconds */
  97. static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
  98. module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
  99. MODULE_PARM_DESC(initial_descriptor_timeout,
  100. "initial 64-byte descriptor request timeout in milliseconds "
  101. "(default 5000 - 5.0 seconds)");
  102. /*
  103. * As of 2.6.10 we introduce a new USB device initialization scheme which
  104. * closely resembles the way Windows works. Hopefully it will be compatible
  105. * with a wider range of devices than the old scheme. However some previously
  106. * working devices may start giving rise to "device not accepting address"
  107. * errors; if that happens the user can try the old scheme by adjusting the
  108. * following module parameters.
  109. *
  110. * For maximum flexibility there are two boolean parameters to control the
  111. * hub driver's behavior. On the first initialization attempt, if the
  112. * "old_scheme_first" parameter is set then the old scheme will be used,
  113. * otherwise the new scheme is used. If that fails and "use_both_schemes"
  114. * is set, then the driver will make another attempt, using the other scheme.
  115. */
  116. static bool old_scheme_first = 0;
  117. module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
  118. MODULE_PARM_DESC(old_scheme_first,
  119. "start with the old device initialization scheme");
  120. static bool use_both_schemes = 1;
  121. module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
  122. MODULE_PARM_DESC(use_both_schemes,
  123. "try the other device initialization scheme if the "
  124. "first one fails");
  125. /* Mutual exclusion for EHCI CF initialization. This interferes with
  126. * port reset on some companion controllers.
  127. */
  128. DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
  129. EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
  130. #define HUB_DEBOUNCE_TIMEOUT 1500
  131. #define HUB_DEBOUNCE_STEP 25
  132. #define HUB_DEBOUNCE_STABLE 100
  133. static int usb_reset_and_verify_device(struct usb_device *udev);
  134. static inline char *portspeed(struct usb_hub *hub, int portstatus)
  135. {
  136. if (hub_is_superspeed(hub->hdev))
  137. return "5.0 Gb/s";
  138. if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  139. return "480 Mb/s";
  140. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  141. return "1.5 Mb/s";
  142. else
  143. return "12 Mb/s";
  144. }
  145. /* Note that hdev or one of its children must be locked! */
  146. static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
  147. {
  148. if (!hdev || !hdev->actconfig)
  149. return NULL;
  150. return usb_get_intfdata(hdev->actconfig->interface[0]);
  151. }
  152. /* USB 2.0 spec Section 11.24.4.5 */
  153. static int get_hub_descriptor(struct usb_device *hdev, void *data)
  154. {
  155. int i, ret, size;
  156. unsigned dtype;
  157. if (hub_is_superspeed(hdev)) {
  158. dtype = USB_DT_SS_HUB;
  159. size = USB_DT_SS_HUB_SIZE;
  160. } else {
  161. dtype = USB_DT_HUB;
  162. size = sizeof(struct usb_hub_descriptor);
  163. }
  164. for (i = 0; i < 3; i++) {
  165. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  166. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  167. dtype << 8, 0, data, size,
  168. USB_CTRL_GET_TIMEOUT);
  169. if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
  170. return ret;
  171. }
  172. return -EINVAL;
  173. }
  174. /*
  175. * USB 2.0 spec Section 11.24.2.1
  176. */
  177. static int clear_hub_feature(struct usb_device *hdev, int feature)
  178. {
  179. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  180. USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
  181. }
  182. /*
  183. * USB 2.0 spec Section 11.24.2.2
  184. */
  185. static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
  186. {
  187. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  188. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
  189. NULL, 0, 1000);
  190. }
  191. /*
  192. * USB 2.0 spec Section 11.24.2.13
  193. */
  194. static int set_port_feature(struct usb_device *hdev, int port1, int feature)
  195. {
  196. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  197. USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
  198. NULL, 0, 1000);
  199. }
  200. /*
  201. * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
  202. * for info about using port indicators
  203. */
  204. static void set_port_led(
  205. struct usb_hub *hub,
  206. int port1,
  207. int selector
  208. )
  209. {
  210. int status = set_port_feature(hub->hdev, (selector << 8) | port1,
  211. USB_PORT_FEAT_INDICATOR);
  212. if (status < 0)
  213. dev_dbg (hub->intfdev,
  214. "port %d indicator %s status %d\n",
  215. port1,
  216. ({ char *s; switch (selector) {
  217. case HUB_LED_AMBER: s = "amber"; break;
  218. case HUB_LED_GREEN: s = "green"; break;
  219. case HUB_LED_OFF: s = "off"; break;
  220. case HUB_LED_AUTO: s = "auto"; break;
  221. default: s = "??"; break;
  222. }; s; }),
  223. status);
  224. }
  225. #define LED_CYCLE_PERIOD ((2*HZ)/3)
  226. static void led_work (struct work_struct *work)
  227. {
  228. struct usb_hub *hub =
  229. container_of(work, struct usb_hub, leds.work);
  230. struct usb_device *hdev = hub->hdev;
  231. unsigned i;
  232. unsigned changed = 0;
  233. int cursor = -1;
  234. if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
  235. return;
  236. for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
  237. unsigned selector, mode;
  238. /* 30%-50% duty cycle */
  239. switch (hub->indicator[i]) {
  240. /* cycle marker */
  241. case INDICATOR_CYCLE:
  242. cursor = i;
  243. selector = HUB_LED_AUTO;
  244. mode = INDICATOR_AUTO;
  245. break;
  246. /* blinking green = sw attention */
  247. case INDICATOR_GREEN_BLINK:
  248. selector = HUB_LED_GREEN;
  249. mode = INDICATOR_GREEN_BLINK_OFF;
  250. break;
  251. case INDICATOR_GREEN_BLINK_OFF:
  252. selector = HUB_LED_OFF;
  253. mode = INDICATOR_GREEN_BLINK;
  254. break;
  255. /* blinking amber = hw attention */
  256. case INDICATOR_AMBER_BLINK:
  257. selector = HUB_LED_AMBER;
  258. mode = INDICATOR_AMBER_BLINK_OFF;
  259. break;
  260. case INDICATOR_AMBER_BLINK_OFF:
  261. selector = HUB_LED_OFF;
  262. mode = INDICATOR_AMBER_BLINK;
  263. break;
  264. /* blink green/amber = reserved */
  265. case INDICATOR_ALT_BLINK:
  266. selector = HUB_LED_GREEN;
  267. mode = INDICATOR_ALT_BLINK_OFF;
  268. break;
  269. case INDICATOR_ALT_BLINK_OFF:
  270. selector = HUB_LED_AMBER;
  271. mode = INDICATOR_ALT_BLINK;
  272. break;
  273. default:
  274. continue;
  275. }
  276. if (selector != HUB_LED_AUTO)
  277. changed = 1;
  278. set_port_led(hub, i + 1, selector);
  279. hub->indicator[i] = mode;
  280. }
  281. if (!changed && blinkenlights) {
  282. cursor++;
  283. cursor %= hub->descriptor->bNbrPorts;
  284. set_port_led(hub, cursor + 1, HUB_LED_GREEN);
  285. hub->indicator[cursor] = INDICATOR_CYCLE;
  286. changed++;
  287. }
  288. if (changed)
  289. schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
  290. }
  291. /* use a short timeout for hub/port status fetches */
  292. #define USB_STS_TIMEOUT 1000
  293. #define USB_STS_RETRIES 5
  294. /*
  295. * USB 2.0 spec Section 11.24.2.6
  296. */
  297. static int get_hub_status(struct usb_device *hdev,
  298. struct usb_hub_status *data)
  299. {
  300. int i, status = -ETIMEDOUT;
  301. for (i = 0; i < USB_STS_RETRIES &&
  302. (status == -ETIMEDOUT || status == -EPIPE); i++) {
  303. status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  304. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  305. data, sizeof(*data), USB_STS_TIMEOUT);
  306. }
  307. return status;
  308. }
  309. /*
  310. * USB 2.0 spec Section 11.24.2.7
  311. */
  312. static int get_port_status(struct usb_device *hdev, int port1,
  313. struct usb_port_status *data)
  314. {
  315. int i, status = -ETIMEDOUT;
  316. for (i = 0; i < USB_STS_RETRIES &&
  317. (status == -ETIMEDOUT || status == -EPIPE); i++) {
  318. status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  319. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
  320. data, sizeof(*data), USB_STS_TIMEOUT);
  321. }
  322. return status;
  323. }
  324. static int hub_port_status(struct usb_hub *hub, int port1,
  325. u16 *status, u16 *change)
  326. {
  327. int ret;
  328. mutex_lock(&hub->status_mutex);
  329. ret = get_port_status(hub->hdev, port1, &hub->status->port);
  330. if (ret < 4) {
  331. dev_err(hub->intfdev,
  332. "%s failed (err = %d)\n", __func__, ret);
  333. if (ret >= 0)
  334. ret = -EIO;
  335. } else {
  336. *status = le16_to_cpu(hub->status->port.wPortStatus);
  337. *change = le16_to_cpu(hub->status->port.wPortChange);
  338. ret = 0;
  339. }
  340. mutex_unlock(&hub->status_mutex);
  341. return ret;
  342. }
  343. static void kick_khubd(struct usb_hub *hub)
  344. {
  345. unsigned long flags;
  346. spin_lock_irqsave(&hub_event_lock, flags);
  347. if (!hub->disconnected && list_empty(&hub->event_list)) {
  348. list_add_tail(&hub->event_list, &hub_event_list);
  349. /* Suppress autosuspend until khubd runs */
  350. usb_autopm_get_interface_no_resume(
  351. to_usb_interface(hub->intfdev));
  352. wake_up(&khubd_wait);
  353. }
  354. spin_unlock_irqrestore(&hub_event_lock, flags);
  355. }
  356. void usb_kick_khubd(struct usb_device *hdev)
  357. {
  358. struct usb_hub *hub = hdev_to_hub(hdev);
  359. if (hub)
  360. kick_khubd(hub);
  361. }
  362. /*
  363. * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
  364. * Notification, which indicates it had initiated remote wakeup.
  365. *
  366. * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
  367. * device initiates resume, so the USB core will not receive notice of the
  368. * resume through the normal hub interrupt URB.
  369. */
  370. void usb_wakeup_notification(struct usb_device *hdev,
  371. unsigned int portnum)
  372. {
  373. struct usb_hub *hub;
  374. if (!hdev)
  375. return;
  376. hub = hdev_to_hub(hdev);
  377. if (hub) {
  378. set_bit(portnum, hub->wakeup_bits);
  379. kick_khubd(hub);
  380. }
  381. }
  382. EXPORT_SYMBOL_GPL(usb_wakeup_notification);
  383. /* completion function, fires on port status changes and various faults */
  384. static void hub_irq(struct urb *urb)
  385. {
  386. struct usb_hub *hub = urb->context;
  387. int status = urb->status;
  388. unsigned i;
  389. unsigned long bits;
  390. switch (status) {
  391. case -ENOENT: /* synchronous unlink */
  392. case -ECONNRESET: /* async unlink */
  393. case -ESHUTDOWN: /* hardware going away */
  394. return;
  395. default: /* presumably an error */
  396. /* Cause a hub reset after 10 consecutive errors */
  397. dev_dbg (hub->intfdev, "transfer --> %d\n", status);
  398. if ((++hub->nerrors < 10) || hub->error)
  399. goto resubmit;
  400. hub->error = status;
  401. /* FALL THROUGH */
  402. /* let khubd handle things */
  403. case 0: /* we got data: port status changed */
  404. bits = 0;
  405. for (i = 0; i < urb->actual_length; ++i)
  406. bits |= ((unsigned long) ((*hub->buffer)[i]))
  407. << (i*8);
  408. hub->event_bits[0] = bits;
  409. break;
  410. }
  411. hub->nerrors = 0;
  412. /* Something happened, let khubd figure it out */
  413. kick_khubd(hub);
  414. resubmit:
  415. if (hub->quiescing)
  416. return;
  417. if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
  418. && status != -ENODEV && status != -EPERM)
  419. dev_err (hub->intfdev, "resubmit --> %d\n", status);
  420. }
  421. /* USB 2.0 spec Section 11.24.2.3 */
  422. static inline int
  423. hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
  424. {
  425. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  426. HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
  427. tt, NULL, 0, 1000);
  428. }
  429. /*
  430. * enumeration blocks khubd for a long time. we use keventd instead, since
  431. * long blocking there is the exception, not the rule. accordingly, HCDs
  432. * talking to TTs must queue control transfers (not just bulk and iso), so
  433. * both can talk to the same hub concurrently.
  434. */
  435. static void hub_tt_work(struct work_struct *work)
  436. {
  437. struct usb_hub *hub =
  438. container_of(work, struct usb_hub, tt.clear_work);
  439. unsigned long flags;
  440. int limit = 100;
  441. spin_lock_irqsave (&hub->tt.lock, flags);
  442. while (--limit && !list_empty (&hub->tt.clear_list)) {
  443. struct list_head *next;
  444. struct usb_tt_clear *clear;
  445. struct usb_device *hdev = hub->hdev;
  446. const struct hc_driver *drv;
  447. int status;
  448. next = hub->tt.clear_list.next;
  449. clear = list_entry (next, struct usb_tt_clear, clear_list);
  450. list_del (&clear->clear_list);
  451. /* drop lock so HCD can concurrently report other TT errors */
  452. spin_unlock_irqrestore (&hub->tt.lock, flags);
  453. status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
  454. if (status)
  455. dev_err (&hdev->dev,
  456. "clear tt %d (%04x) error %d\n",
  457. clear->tt, clear->devinfo, status);
  458. /* Tell the HCD, even if the operation failed */
  459. drv = clear->hcd->driver;
  460. if (drv->clear_tt_buffer_complete)
  461. (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
  462. kfree(clear);
  463. spin_lock_irqsave(&hub->tt.lock, flags);
  464. }
  465. spin_unlock_irqrestore (&hub->tt.lock, flags);
  466. }
  467. /**
  468. * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
  469. * @urb: an URB associated with the failed or incomplete split transaction
  470. *
  471. * High speed HCDs use this to tell the hub driver that some split control or
  472. * bulk transaction failed in a way that requires clearing internal state of
  473. * a transaction translator. This is normally detected (and reported) from
  474. * interrupt context.
  475. *
  476. * It may not be possible for that hub to handle additional full (or low)
  477. * speed transactions until that state is fully cleared out.
  478. */
  479. int usb_hub_clear_tt_buffer(struct urb *urb)
  480. {
  481. struct usb_device *udev = urb->dev;
  482. int pipe = urb->pipe;
  483. struct usb_tt *tt = udev->tt;
  484. unsigned long flags;
  485. struct usb_tt_clear *clear;
  486. /* we've got to cope with an arbitrary number of pending TT clears,
  487. * since each TT has "at least two" buffers that can need it (and
  488. * there can be many TTs per hub). even if they're uncommon.
  489. */
  490. if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
  491. dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
  492. /* FIXME recover somehow ... RESET_TT? */
  493. return -ENOMEM;
  494. }
  495. /* info that CLEAR_TT_BUFFER needs */
  496. clear->tt = tt->multi ? udev->ttport : 1;
  497. clear->devinfo = usb_pipeendpoint (pipe);
  498. clear->devinfo |= udev->devnum << 4;
  499. clear->devinfo |= usb_pipecontrol (pipe)
  500. ? (USB_ENDPOINT_XFER_CONTROL << 11)
  501. : (USB_ENDPOINT_XFER_BULK << 11);
  502. if (usb_pipein (pipe))
  503. clear->devinfo |= 1 << 15;
  504. /* info for completion callback */
  505. clear->hcd = bus_to_hcd(udev->bus);
  506. clear->ep = urb->ep;
  507. /* tell keventd to clear state for this TT */
  508. spin_lock_irqsave (&tt->lock, flags);
  509. list_add_tail (&clear->clear_list, &tt->clear_list);
  510. schedule_work(&tt->clear_work);
  511. spin_unlock_irqrestore (&tt->lock, flags);
  512. return 0;
  513. }
  514. EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
  515. /* If do_delay is false, return the number of milliseconds the caller
  516. * needs to delay.
  517. */
  518. static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
  519. {
  520. int port1;
  521. unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
  522. unsigned delay;
  523. u16 wHubCharacteristics =
  524. le16_to_cpu(hub->descriptor->wHubCharacteristics);
  525. /* Enable power on each port. Some hubs have reserved values
  526. * of LPSM (> 2) in their descriptors, even though they are
  527. * USB 2.0 hubs. Some hubs do not implement port-power switching
  528. * but only emulate it. In all cases, the ports won't work
  529. * unless we send these messages to the hub.
  530. */
  531. if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
  532. dev_dbg(hub->intfdev, "enabling power on all ports\n");
  533. else
  534. dev_dbg(hub->intfdev, "trying to enable port power on "
  535. "non-switchable hub\n");
  536. for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
  537. set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
  538. /* Wait at least 100 msec for power to become stable */
  539. delay = max(pgood_delay, (unsigned) 100);
  540. if (do_delay)
  541. msleep(delay);
  542. return delay;
  543. }
  544. static int hub_hub_status(struct usb_hub *hub,
  545. u16 *status, u16 *change)
  546. {
  547. int ret;
  548. mutex_lock(&hub->status_mutex);
  549. ret = get_hub_status(hub->hdev, &hub->status->hub);
  550. if (ret < 0)
  551. dev_err (hub->intfdev,
  552. "%s failed (err = %d)\n", __func__, ret);
  553. else {
  554. *status = le16_to_cpu(hub->status->hub.wHubStatus);
  555. *change = le16_to_cpu(hub->status->hub.wHubChange);
  556. ret = 0;
  557. }
  558. mutex_unlock(&hub->status_mutex);
  559. return ret;
  560. }
  561. static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
  562. {
  563. struct usb_device *hdev = hub->hdev;
  564. int ret = 0;
  565. if (hdev->children[port1-1] && set_state)
  566. usb_set_device_state(hdev->children[port1-1],
  567. USB_STATE_NOTATTACHED);
  568. if (!hub->error && !hub_is_superspeed(hub->hdev))
  569. ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
  570. if (ret)
  571. dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
  572. port1, ret);
  573. return ret;
  574. }
  575. /*
  576. * Disable a port and mark a logical connect-change event, so that some
  577. * time later khubd will disconnect() any existing usb_device on the port
  578. * and will re-enumerate if there actually is a device attached.
  579. */
  580. static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
  581. {
  582. dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
  583. hub_port_disable(hub, port1, 1);
  584. /* FIXME let caller ask to power down the port:
  585. * - some devices won't enumerate without a VBUS power cycle
  586. * - SRP saves power that way
  587. * - ... new call, TBD ...
  588. * That's easy if this hub can switch power per-port, and
  589. * khubd reactivates the port later (timer, SRP, etc).
  590. * Powerdown must be optional, because of reset/DFU.
  591. */
  592. set_bit(port1, hub->change_bits);
  593. kick_khubd(hub);
  594. }
  595. /**
  596. * usb_remove_device - disable a device's port on its parent hub
  597. * @udev: device to be disabled and removed
  598. * Context: @udev locked, must be able to sleep.
  599. *
  600. * After @udev's port has been disabled, khubd is notified and it will
  601. * see that the device has been disconnected. When the device is
  602. * physically unplugged and something is plugged in, the events will
  603. * be received and processed normally.
  604. */
  605. int usb_remove_device(struct usb_device *udev)
  606. {
  607. struct usb_hub *hub;
  608. struct usb_interface *intf;
  609. if (!udev->parent) /* Can't remove a root hub */
  610. return -EINVAL;
  611. hub = hdev_to_hub(udev->parent);
  612. intf = to_usb_interface(hub->intfdev);
  613. usb_autopm_get_interface(intf);
  614. set_bit(udev->portnum, hub->removed_bits);
  615. hub_port_logical_disconnect(hub, udev->portnum);
  616. usb_autopm_put_interface(intf);
  617. return 0;
  618. }
  619. enum hub_activation_type {
  620. HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
  621. HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
  622. };
  623. static void hub_init_func2(struct work_struct *ws);
  624. static void hub_init_func3(struct work_struct *ws);
  625. static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
  626. {
  627. struct usb_device *hdev = hub->hdev;
  628. struct usb_hcd *hcd;
  629. int ret;
  630. int port1;
  631. int status;
  632. bool need_debounce_delay = false;
  633. unsigned delay;
  634. /* Continue a partial initialization */
  635. if (type == HUB_INIT2)
  636. goto init2;
  637. if (type == HUB_INIT3)
  638. goto init3;
  639. /* The superspeed hub except for root hub has to use Hub Depth
  640. * value as an offset into the route string to locate the bits
  641. * it uses to determine the downstream port number. So hub driver
  642. * should send a set hub depth request to superspeed hub after
  643. * the superspeed hub is set configuration in initialization or
  644. * reset procedure.
  645. *
  646. * After a resume, port power should still be on.
  647. * For any other type of activation, turn it on.
  648. */
  649. if (type != HUB_RESUME) {
  650. if (hdev->parent && hub_is_superspeed(hdev)) {
  651. ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  652. HUB_SET_DEPTH, USB_RT_HUB,
  653. hdev->level - 1, 0, NULL, 0,
  654. USB_CTRL_SET_TIMEOUT);
  655. if (ret < 0)
  656. dev_err(hub->intfdev,
  657. "set hub depth failed\n");
  658. }
  659. /* Speed up system boot by using a delayed_work for the
  660. * hub's initial power-up delays. This is pretty awkward
  661. * and the implementation looks like a home-brewed sort of
  662. * setjmp/longjmp, but it saves at least 100 ms for each
  663. * root hub (assuming usbcore is compiled into the kernel
  664. * rather than as a module). It adds up.
  665. *
  666. * This can't be done for HUB_RESUME or HUB_RESET_RESUME
  667. * because for those activation types the ports have to be
  668. * operational when we return. In theory this could be done
  669. * for HUB_POST_RESET, but it's easier not to.
  670. */
  671. if (type == HUB_INIT) {
  672. delay = hub_power_on(hub, false);
  673. PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
  674. schedule_delayed_work(&hub->init_work,
  675. msecs_to_jiffies(delay));
  676. /* Suppress autosuspend until init is done */
  677. usb_autopm_get_interface_no_resume(
  678. to_usb_interface(hub->intfdev));
  679. return; /* Continues at init2: below */
  680. } else if (type == HUB_RESET_RESUME) {
  681. /* The internal host controller state for the hub device
  682. * may be gone after a host power loss on system resume.
  683. * Update the device's info so the HW knows it's a hub.
  684. */
  685. hcd = bus_to_hcd(hdev->bus);
  686. if (hcd->driver->update_hub_device) {
  687. ret = hcd->driver->update_hub_device(hcd, hdev,
  688. &hub->tt, GFP_NOIO);
  689. if (ret < 0) {
  690. dev_err(hub->intfdev, "Host not "
  691. "accepting hub info "
  692. "update.\n");
  693. dev_err(hub->intfdev, "LS/FS devices "
  694. "and hubs may not work "
  695. "under this hub\n.");
  696. }
  697. }
  698. hub_power_on(hub, true);
  699. } else {
  700. hub_power_on(hub, true);
  701. }
  702. }
  703. init2:
  704. /* Check each port and set hub->change_bits to let khubd know
  705. * which ports need attention.
  706. */
  707. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  708. struct usb_device *udev = hdev->children[port1-1];
  709. u16 portstatus, portchange;
  710. portstatus = portchange = 0;
  711. status = hub_port_status(hub, port1, &portstatus, &portchange);
  712. if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
  713. dev_dbg(hub->intfdev,
  714. "port %d: status %04x change %04x\n",
  715. port1, portstatus, portchange);
  716. /* After anything other than HUB_RESUME (i.e., initialization
  717. * or any sort of reset), every port should be disabled.
  718. * Unconnected ports should likewise be disabled (paranoia),
  719. * and so should ports for which we have no usb_device.
  720. */
  721. if ((portstatus & USB_PORT_STAT_ENABLE) && (
  722. type != HUB_RESUME ||
  723. !(portstatus & USB_PORT_STAT_CONNECTION) ||
  724. !udev ||
  725. udev->state == USB_STATE_NOTATTACHED)) {
  726. /*
  727. * USB3 protocol ports will automatically transition
  728. * to Enabled state when detect an USB3.0 device attach.
  729. * Do not disable USB3 protocol ports.
  730. */
  731. if (!hub_is_superspeed(hdev)) {
  732. clear_port_feature(hdev, port1,
  733. USB_PORT_FEAT_ENABLE);
  734. portstatus &= ~USB_PORT_STAT_ENABLE;
  735. } else {
  736. /* Pretend that power was lost for USB3 devs */
  737. portstatus &= ~USB_PORT_STAT_ENABLE;
  738. }
  739. }
  740. /* Clear status-change flags; we'll debounce later */
  741. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  742. need_debounce_delay = true;
  743. clear_port_feature(hub->hdev, port1,
  744. USB_PORT_FEAT_C_CONNECTION);
  745. }
  746. if (portchange & USB_PORT_STAT_C_ENABLE) {
  747. need_debounce_delay = true;
  748. clear_port_feature(hub->hdev, port1,
  749. USB_PORT_FEAT_C_ENABLE);
  750. }
  751. if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
  752. hub_is_superspeed(hub->hdev)) {
  753. need_debounce_delay = true;
  754. clear_port_feature(hub->hdev, port1,
  755. USB_PORT_FEAT_C_BH_PORT_RESET);
  756. }
  757. /* We can forget about a "removed" device when there's a
  758. * physical disconnect or the connect status changes.
  759. */
  760. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  761. (portchange & USB_PORT_STAT_C_CONNECTION))
  762. clear_bit(port1, hub->removed_bits);
  763. if (!udev || udev->state == USB_STATE_NOTATTACHED) {
  764. /* Tell khubd to disconnect the device or
  765. * check for a new connection
  766. */
  767. if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
  768. set_bit(port1, hub->change_bits);
  769. } else if (portstatus & USB_PORT_STAT_ENABLE) {
  770. bool port_resumed = (portstatus &
  771. USB_PORT_STAT_LINK_STATE) ==
  772. USB_SS_PORT_LS_U0;
  773. /* The power session apparently survived the resume.
  774. * If there was an overcurrent or suspend change
  775. * (i.e., remote wakeup request), have khubd
  776. * take care of it. Look at the port link state
  777. * for USB 3.0 hubs, since they don't have a suspend
  778. * change bit, and they don't set the port link change
  779. * bit on device-initiated resume.
  780. */
  781. if (portchange || (hub_is_superspeed(hub->hdev) &&
  782. port_resumed))
  783. set_bit(port1, hub->change_bits);
  784. } else if (udev->persist_enabled) {
  785. #ifdef CONFIG_PM
  786. udev->reset_resume = 1;
  787. #endif
  788. set_bit(port1, hub->change_bits);
  789. } else {
  790. /* The power session is gone; tell khubd */
  791. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  792. set_bit(port1, hub->change_bits);
  793. }
  794. }
  795. /* If no port-status-change flags were set, we don't need any
  796. * debouncing. If flags were set we can try to debounce the
  797. * ports all at once right now, instead of letting khubd do them
  798. * one at a time later on.
  799. *
  800. * If any port-status changes do occur during this delay, khubd
  801. * will see them later and handle them normally.
  802. */
  803. if (need_debounce_delay) {
  804. delay = HUB_DEBOUNCE_STABLE;
  805. /* Don't do a long sleep inside a workqueue routine */
  806. if (type == HUB_INIT2) {
  807. PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
  808. schedule_delayed_work(&hub->init_work,
  809. msecs_to_jiffies(delay));
  810. return; /* Continues at init3: below */
  811. } else {
  812. msleep(delay);
  813. }
  814. }
  815. init3:
  816. hub->quiescing = 0;
  817. status = usb_submit_urb(hub->urb, GFP_NOIO);
  818. if (status < 0)
  819. dev_err(hub->intfdev, "activate --> %d\n", status);
  820. if (hub->has_indicators && blinkenlights)
  821. schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
  822. /* Scan all ports that need attention */
  823. kick_khubd(hub);
  824. /* Allow autosuspend if it was suppressed */
  825. if (type <= HUB_INIT3)
  826. usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
  827. }
  828. /* Implement the continuations for the delays above */
  829. static void hub_init_func2(struct work_struct *ws)
  830. {
  831. struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
  832. hub_activate(hub, HUB_INIT2);
  833. }
  834. static void hub_init_func3(struct work_struct *ws)
  835. {
  836. struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
  837. hub_activate(hub, HUB_INIT3);
  838. }
  839. enum hub_quiescing_type {
  840. HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
  841. };
  842. static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
  843. {
  844. struct usb_device *hdev = hub->hdev;
  845. int i;
  846. cancel_delayed_work_sync(&hub->init_work);
  847. /* khubd and related activity won't re-trigger */
  848. hub->quiescing = 1;
  849. if (type != HUB_SUSPEND) {
  850. /* Disconnect all the children */
  851. for (i = 0; i < hdev->maxchild; ++i) {
  852. if (hdev->children[i])
  853. usb_disconnect(&hdev->children[i]);
  854. }
  855. }
  856. /* Stop khubd and related activity */
  857. usb_kill_urb(hub->urb);
  858. if (hub->has_indicators)
  859. cancel_delayed_work_sync(&hub->leds);
  860. if (hub->tt.hub)
  861. cancel_work_sync(&hub->tt.clear_work);
  862. }
  863. /* caller has locked the hub device */
  864. static int hub_pre_reset(struct usb_interface *intf)
  865. {
  866. struct usb_hub *hub = usb_get_intfdata(intf);
  867. hub_quiesce(hub, HUB_PRE_RESET);
  868. return 0;
  869. }
  870. /* caller has locked the hub device */
  871. static int hub_post_reset(struct usb_interface *intf)
  872. {
  873. struct usb_hub *hub = usb_get_intfdata(intf);
  874. hub_activate(hub, HUB_POST_RESET);
  875. return 0;
  876. }
  877. static int hub_configure(struct usb_hub *hub,
  878. struct usb_endpoint_descriptor *endpoint)
  879. {
  880. struct usb_hcd *hcd;
  881. struct usb_device *hdev = hub->hdev;
  882. struct device *hub_dev = hub->intfdev;
  883. u16 hubstatus, hubchange;
  884. u16 wHubCharacteristics;
  885. unsigned int pipe;
  886. int maxp, ret;
  887. char *message = "out of memory";
  888. hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
  889. if (!hub->buffer) {
  890. ret = -ENOMEM;
  891. goto fail;
  892. }
  893. hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
  894. if (!hub->status) {
  895. ret = -ENOMEM;
  896. goto fail;
  897. }
  898. mutex_init(&hub->status_mutex);
  899. hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
  900. if (!hub->descriptor) {
  901. ret = -ENOMEM;
  902. goto fail;
  903. }
  904. /* Request the entire hub descriptor.
  905. * hub->descriptor can handle USB_MAXCHILDREN ports,
  906. * but the hub can/will return fewer bytes here.
  907. */
  908. ret = get_hub_descriptor(hdev, hub->descriptor);
  909. if (ret < 0) {
  910. message = "can't read hub descriptor";
  911. goto fail;
  912. } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
  913. message = "hub has too many ports!";
  914. ret = -ENODEV;
  915. goto fail;
  916. }
  917. hdev->maxchild = hub->descriptor->bNbrPorts;
  918. dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
  919. (hdev->maxchild == 1) ? "" : "s");
  920. hdev->children = kzalloc(hdev->maxchild *
  921. sizeof(struct usb_device *), GFP_KERNEL);
  922. hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
  923. if (!hdev->children || !hub->port_owners) {
  924. ret = -ENOMEM;
  925. goto fail;
  926. }
  927. wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
  928. /* FIXME for USB 3.0, skip for now */
  929. if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
  930. !(hub_is_superspeed(hdev))) {
  931. int i;
  932. char portstr [USB_MAXCHILDREN + 1];
  933. for (i = 0; i < hdev->maxchild; i++)
  934. portstr[i] = hub->descriptor->u.hs.DeviceRemovable
  935. [((i + 1) / 8)] & (1 << ((i + 1) % 8))
  936. ? 'F' : 'R';
  937. portstr[hdev->maxchild] = 0;
  938. dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
  939. } else
  940. dev_dbg(hub_dev, "standalone hub\n");
  941. switch (wHubCharacteristics & HUB_CHAR_LPSM) {
  942. case HUB_CHAR_COMMON_LPSM:
  943. dev_dbg(hub_dev, "ganged power switching\n");
  944. break;
  945. case HUB_CHAR_INDV_PORT_LPSM:
  946. dev_dbg(hub_dev, "individual port power switching\n");
  947. break;
  948. case HUB_CHAR_NO_LPSM:
  949. case HUB_CHAR_LPSM:
  950. dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
  951. break;
  952. }
  953. switch (wHubCharacteristics & HUB_CHAR_OCPM) {
  954. case HUB_CHAR_COMMON_OCPM:
  955. dev_dbg(hub_dev, "global over-current protection\n");
  956. break;
  957. case HUB_CHAR_INDV_PORT_OCPM:
  958. dev_dbg(hub_dev, "individual port over-current protection\n");
  959. break;
  960. case HUB_CHAR_NO_OCPM:
  961. case HUB_CHAR_OCPM:
  962. dev_dbg(hub_dev, "no over-current protection\n");
  963. break;
  964. }
  965. spin_lock_init (&hub->tt.lock);
  966. INIT_LIST_HEAD (&hub->tt.clear_list);
  967. INIT_WORK(&hub->tt.clear_work, hub_tt_work);
  968. switch (hdev->descriptor.bDeviceProtocol) {
  969. case USB_HUB_PR_FS:
  970. break;
  971. case USB_HUB_PR_HS_SINGLE_TT:
  972. dev_dbg(hub_dev, "Single TT\n");
  973. hub->tt.hub = hdev;
  974. break;
  975. case USB_HUB_PR_HS_MULTI_TT:
  976. ret = usb_set_interface(hdev, 0, 1);
  977. if (ret == 0) {
  978. dev_dbg(hub_dev, "TT per port\n");
  979. hub->tt.multi = 1;
  980. } else
  981. dev_err(hub_dev, "Using single TT (err %d)\n",
  982. ret);
  983. hub->tt.hub = hdev;
  984. break;
  985. case USB_HUB_PR_SS:
  986. /* USB 3.0 hubs don't have a TT */
  987. break;
  988. default:
  989. dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
  990. hdev->descriptor.bDeviceProtocol);
  991. break;
  992. }
  993. /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
  994. switch (wHubCharacteristics & HUB_CHAR_TTTT) {
  995. case HUB_TTTT_8_BITS:
  996. if (hdev->descriptor.bDeviceProtocol != 0) {
  997. hub->tt.think_time = 666;
  998. dev_dbg(hub_dev, "TT requires at most %d "
  999. "FS bit times (%d ns)\n",
  1000. 8, hub->tt.think_time);
  1001. }
  1002. break;
  1003. case HUB_TTTT_16_BITS:
  1004. hub->tt.think_time = 666 * 2;
  1005. dev_dbg(hub_dev, "TT requires at most %d "
  1006. "FS bit times (%d ns)\n",
  1007. 16, hub->tt.think_time);
  1008. break;
  1009. case HUB_TTTT_24_BITS:
  1010. hub->tt.think_time = 666 * 3;
  1011. dev_dbg(hub_dev, "TT requires at most %d "
  1012. "FS bit times (%d ns)\n",
  1013. 24, hub->tt.think_time);
  1014. break;
  1015. case HUB_TTTT_32_BITS:
  1016. hub->tt.think_time = 666 * 4;
  1017. dev_dbg(hub_dev, "TT requires at most %d "
  1018. "FS bit times (%d ns)\n",
  1019. 32, hub->tt.think_time);
  1020. break;
  1021. }
  1022. /* probe() zeroes hub->indicator[] */
  1023. if (wHubCharacteristics & HUB_CHAR_PORTIND) {
  1024. hub->has_indicators = 1;
  1025. dev_dbg(hub_dev, "Port indicators are supported\n");
  1026. }
  1027. dev_dbg(hub_dev, "power on to power good time: %dms\n",
  1028. hub->descriptor->bPwrOn2PwrGood * 2);
  1029. /* power budgeting mostly matters with bus-powered hubs,
  1030. * and battery-powered root hubs (may provide just 8 mA).
  1031. */
  1032. ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
  1033. if (ret < 2) {
  1034. message = "can't get hub status";
  1035. goto fail;
  1036. }
  1037. le16_to_cpus(&hubstatus);
  1038. if (hdev == hdev->bus->root_hub) {
  1039. if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
  1040. hub->mA_per_port = 500;
  1041. else {
  1042. hub->mA_per_port = hdev->bus_mA;
  1043. hub->limited_power = 1;
  1044. }
  1045. } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  1046. dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
  1047. hub->descriptor->bHubContrCurrent);
  1048. hub->limited_power = 1;
  1049. if (hdev->maxchild > 0) {
  1050. int remaining = hdev->bus_mA -
  1051. hub->descriptor->bHubContrCurrent;
  1052. if (remaining < hdev->maxchild * 100)
  1053. dev_warn(hub_dev,
  1054. "insufficient power available "
  1055. "to use all downstream ports\n");
  1056. hub->mA_per_port = 100; /* 7.2.1.1 */
  1057. }
  1058. } else { /* Self-powered external hub */
  1059. /* FIXME: What about battery-powered external hubs that
  1060. * provide less current per port? */
  1061. hub->mA_per_port = 500;
  1062. }
  1063. if (hub->mA_per_port < 500)
  1064. dev_dbg(hub_dev, "%umA bus power budget for each child\n",
  1065. hub->mA_per_port);
  1066. /* Update the HCD's internal representation of this hub before khubd
  1067. * starts getting port status changes for devices under the hub.
  1068. */
  1069. hcd = bus_to_hcd(hdev->bus);
  1070. if (hcd->driver->update_hub_device) {
  1071. ret = hcd->driver->update_hub_device(hcd, hdev,
  1072. &hub->tt, GFP_KERNEL);
  1073. if (ret < 0) {
  1074. message = "can't update HCD hub info";
  1075. goto fail;
  1076. }
  1077. }
  1078. ret = hub_hub_status(hub, &hubstatus, &hubchange);
  1079. if (ret < 0) {
  1080. message = "can't get hub status";
  1081. goto fail;
  1082. }
  1083. /* local power status reports aren't always correct */
  1084. if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
  1085. dev_dbg(hub_dev, "local power source is %s\n",
  1086. (hubstatus & HUB_STATUS_LOCAL_POWER)
  1087. ? "lost (inactive)" : "good");
  1088. if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
  1089. dev_dbg(hub_dev, "%sover-current condition exists\n",
  1090. (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
  1091. /* set up the interrupt endpoint
  1092. * We use the EP's maxpacket size instead of (PORTS+1+7)/8
  1093. * bytes as USB2.0[11.12.3] says because some hubs are known
  1094. * to send more data (and thus cause overflow). For root hubs,
  1095. * maxpktsize is defined in hcd.c's fake endpoint descriptors
  1096. * to be big enough for at least USB_MAXCHILDREN ports. */
  1097. pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
  1098. maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
  1099. if (maxp > sizeof(*hub->buffer))
  1100. maxp = sizeof(*hub->buffer);
  1101. hub->urb = usb_alloc_urb(0, GFP_KERNEL);
  1102. if (!hub->urb) {
  1103. ret = -ENOMEM;
  1104. goto fail;
  1105. }
  1106. usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
  1107. hub, endpoint->bInterval);
  1108. /* maybe cycle the hub leds */
  1109. if (hub->has_indicators && blinkenlights)
  1110. hub->indicator [0] = INDICATOR_CYCLE;
  1111. hub_activate(hub, HUB_INIT);
  1112. return 0;
  1113. fail:
  1114. dev_err (hub_dev, "config failed, %s (err %d)\n",
  1115. message, ret);
  1116. /* hub_disconnect() frees urb and descriptor */
  1117. return ret;
  1118. }
  1119. static void hub_release(struct kref *kref)
  1120. {
  1121. struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
  1122. usb_put_intf(to_usb_interface(hub->intfdev));
  1123. kfree(hub);
  1124. }
  1125. static unsigned highspeed_hubs;
  1126. static void hub_disconnect(struct usb_interface *intf)
  1127. {
  1128. struct usb_hub *hub = usb_get_intfdata(intf);
  1129. struct usb_device *hdev = interface_to_usbdev(intf);
  1130. /* Take the hub off the event list and don't let it be added again */
  1131. spin_lock_irq(&hub_event_lock);
  1132. if (!list_empty(&hub->event_list)) {
  1133. list_del_init(&hub->event_list);
  1134. usb_autopm_put_interface_no_suspend(intf);
  1135. }
  1136. hub->disconnected = 1;
  1137. spin_unlock_irq(&hub_event_lock);
  1138. /* Disconnect all children and quiesce the hub */
  1139. hub->error = 0;
  1140. hub_quiesce(hub, HUB_DISCONNECT);
  1141. usb_set_intfdata (intf, NULL);
  1142. hub->hdev->maxchild = 0;
  1143. if (hub->hdev->speed == USB_SPEED_HIGH)
  1144. highspeed_hubs--;
  1145. usb_free_urb(hub->urb);
  1146. kfree(hdev->children);
  1147. kfree(hub->port_owners);
  1148. kfree(hub->descriptor);
  1149. kfree(hub->status);
  1150. kfree(hub->buffer);
  1151. kref_put(&hub->kref, hub_release);
  1152. }
  1153. static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1154. {
  1155. struct usb_host_interface *desc;
  1156. struct usb_endpoint_descriptor *endpoint;
  1157. struct usb_device *hdev;
  1158. struct usb_hub *hub;
  1159. desc = intf->cur_altsetting;
  1160. hdev = interface_to_usbdev(intf);
  1161. /* Hubs have proper suspend/resume support. */
  1162. usb_enable_autosuspend(hdev);
  1163. if (hdev->level == MAX_TOPO_LEVEL) {
  1164. dev_err(&intf->dev,
  1165. "Unsupported bus topology: hub nested too deep\n");
  1166. return -E2BIG;
  1167. }
  1168. #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
  1169. if (hdev->parent) {
  1170. dev_warn(&intf->dev, "ignoring external hub\n");
  1171. return -ENODEV;
  1172. }
  1173. #endif
  1174. /* Some hubs have a subclass of 1, which AFAICT according to the */
  1175. /* specs is not defined, but it works */
  1176. if ((desc->desc.bInterfaceSubClass != 0) &&
  1177. (desc->desc.bInterfaceSubClass != 1)) {
  1178. descriptor_error:
  1179. dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
  1180. return -EIO;
  1181. }
  1182. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  1183. if (desc->desc.bNumEndpoints != 1)
  1184. goto descriptor_error;
  1185. endpoint = &desc->endpoint[0].desc;
  1186. /* If it's not an interrupt in endpoint, we'd better punt! */
  1187. if (!usb_endpoint_is_int_in(endpoint))
  1188. goto descriptor_error;
  1189. /* We found a hub */
  1190. dev_info (&intf->dev, "USB hub found\n");
  1191. hub = kzalloc(sizeof(*hub), GFP_KERNEL);
  1192. if (!hub) {
  1193. dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
  1194. return -ENOMEM;
  1195. }
  1196. kref_init(&hub->kref);
  1197. INIT_LIST_HEAD(&hub->event_list);
  1198. hub->intfdev = &intf->dev;
  1199. hub->hdev = hdev;
  1200. INIT_DELAYED_WORK(&hub->leds, led_work);
  1201. INIT_DELAYED_WORK(&hub->init_work, NULL);
  1202. usb_get_intf(intf);
  1203. usb_set_intfdata (intf, hub);
  1204. intf->needs_remote_wakeup = 1;
  1205. if (hdev->speed == USB_SPEED_HIGH)
  1206. highspeed_hubs++;
  1207. if (hub_configure(hub, endpoint) >= 0)
  1208. return 0;
  1209. hub_disconnect (intf);
  1210. return -ENODEV;
  1211. }
  1212. static int
  1213. hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
  1214. {
  1215. struct usb_device *hdev = interface_to_usbdev (intf);
  1216. /* assert ifno == 0 (part of hub spec) */
  1217. switch (code) {
  1218. case USBDEVFS_HUB_PORTINFO: {
  1219. struct usbdevfs_hub_portinfo *info = user_data;
  1220. int i;
  1221. spin_lock_irq(&device_state_lock);
  1222. if (hdev->devnum <= 0)
  1223. info->nports = 0;
  1224. else {
  1225. info->nports = hdev->maxchild;
  1226. for (i = 0; i < info->nports; i++) {
  1227. if (hdev->children[i] == NULL)
  1228. info->port[i] = 0;
  1229. else
  1230. info->port[i] =
  1231. hdev->children[i]->devnum;
  1232. }
  1233. }
  1234. spin_unlock_irq(&device_state_lock);
  1235. return info->nports + 1;
  1236. }
  1237. default:
  1238. return -ENOSYS;
  1239. }
  1240. }
  1241. /*
  1242. * Allow user programs to claim ports on a hub. When a device is attached
  1243. * to one of these "claimed" ports, the program will "own" the device.
  1244. */
  1245. static int find_port_owner(struct usb_device *hdev, unsigned port1,
  1246. void ***ppowner)
  1247. {
  1248. if (hdev->state == USB_STATE_NOTATTACHED)
  1249. return -ENODEV;
  1250. if (port1 == 0 || port1 > hdev->maxchild)
  1251. return -EINVAL;
  1252. /* This assumes that devices not managed by the hub driver
  1253. * will always have maxchild equal to 0.
  1254. */
  1255. *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
  1256. return 0;
  1257. }
  1258. /* In the following three functions, the caller must hold hdev's lock */
  1259. int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
  1260. {
  1261. int rc;
  1262. void **powner;
  1263. rc = find_port_owner(hdev, port1, &powner);
  1264. if (rc)
  1265. return rc;
  1266. if (*powner)
  1267. return -EBUSY;
  1268. *powner = owner;
  1269. return rc;
  1270. }
  1271. int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
  1272. {
  1273. int rc;
  1274. void **powner;
  1275. rc = find_port_owner(hdev, port1, &powner);
  1276. if (rc)
  1277. return rc;
  1278. if (*powner != owner)
  1279. return -ENOENT;
  1280. *powner = NULL;
  1281. return rc;
  1282. }
  1283. void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
  1284. {
  1285. int n;
  1286. void **powner;
  1287. n = find_port_owner(hdev, 1, &powner);
  1288. if (n == 0) {
  1289. for (; n < hdev->maxchild; (++n, ++powner)) {
  1290. if (*powner == owner)
  1291. *powner = NULL;
  1292. }
  1293. }
  1294. }
  1295. /* The caller must hold udev's lock */
  1296. bool usb_device_is_owned(struct usb_device *udev)
  1297. {
  1298. struct usb_hub *hub;
  1299. if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
  1300. return false;
  1301. hub = hdev_to_hub(udev->parent);
  1302. return !!hub->port_owners[udev->portnum - 1];
  1303. }
  1304. static void recursively_mark_NOTATTACHED(struct usb_device *udev)
  1305. {
  1306. int i;
  1307. for (i = 0; i < udev->maxchild; ++i) {
  1308. if (udev->children[i])
  1309. recursively_mark_NOTATTACHED(udev->children[i]);
  1310. }
  1311. if (udev->state == USB_STATE_SUSPENDED)
  1312. udev->active_duration -= jiffies;
  1313. udev->state = USB_STATE_NOTATTACHED;
  1314. }
  1315. /**
  1316. * usb_set_device_state - change a device's current state (usbcore, hcds)
  1317. * @udev: pointer to device whose state should be changed
  1318. * @new_state: new state value to be stored
  1319. *
  1320. * udev->state is _not_ fully protected by the device lock. Although
  1321. * most transitions are made only while holding the lock, the state can
  1322. * can change to USB_STATE_NOTATTACHED at almost any time. This
  1323. * is so that devices can be marked as disconnected as soon as possible,
  1324. * without having to wait for any semaphores to be released. As a result,
  1325. * all changes to any device's state must be protected by the
  1326. * device_state_lock spinlock.
  1327. *
  1328. * Once a device has been added to the device tree, all changes to its state
  1329. * should be made using this routine. The state should _not_ be set directly.
  1330. *
  1331. * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
  1332. * Otherwise udev->state is set to new_state, and if new_state is
  1333. * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
  1334. * to USB_STATE_NOTATTACHED.
  1335. */
  1336. void usb_set_device_state(struct usb_device *udev,
  1337. enum usb_device_state new_state)
  1338. {
  1339. unsigned long flags;
  1340. int wakeup = -1;
  1341. spin_lock_irqsave(&device_state_lock, flags);
  1342. if (udev->state == USB_STATE_NOTATTACHED)
  1343. ; /* do nothing */
  1344. else if (new_state != USB_STATE_NOTATTACHED) {
  1345. /* root hub wakeup capabilities are managed out-of-band
  1346. * and may involve silicon errata ... ignore them here.
  1347. */
  1348. if (udev->parent) {
  1349. if (udev->state == USB_STATE_SUSPENDED
  1350. || new_state == USB_STATE_SUSPENDED)
  1351. ; /* No change to wakeup settings */
  1352. else if (new_state == USB_STATE_CONFIGURED)
  1353. wakeup = udev->actconfig->desc.bmAttributes
  1354. & USB_CONFIG_ATT_WAKEUP;
  1355. else
  1356. wakeup = 0;
  1357. }
  1358. if (udev->state == USB_STATE_SUSPENDED &&
  1359. new_state != USB_STATE_SUSPENDED)
  1360. udev->active_duration -= jiffies;
  1361. else if (new_state == USB_STATE_SUSPENDED &&
  1362. udev->state != USB_STATE_SUSPENDED)
  1363. udev->active_duration += jiffies;
  1364. udev->state = new_state;
  1365. } else
  1366. recursively_mark_NOTATTACHED(udev);
  1367. spin_unlock_irqrestore(&device_state_lock, flags);
  1368. if (wakeup >= 0)
  1369. device_set_wakeup_capable(&udev->dev, wakeup);
  1370. }
  1371. EXPORT_SYMBOL_GPL(usb_set_device_state);
  1372. /*
  1373. * Choose a device number.
  1374. *
  1375. * Device numbers are used as filenames in usbfs. On USB-1.1 and
  1376. * USB-2.0 buses they are also used as device addresses, however on
  1377. * USB-3.0 buses the address is assigned by the controller hardware
  1378. * and it usually is not the same as the device number.
  1379. *
  1380. * WUSB devices are simple: they have no hubs behind, so the mapping
  1381. * device <-> virtual port number becomes 1:1. Why? to simplify the
  1382. * life of the device connection logic in
  1383. * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
  1384. * handshake we need to assign a temporary address in the unauthorized
  1385. * space. For simplicity we use the first virtual port number found to
  1386. * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
  1387. * and that becomes it's address [X < 128] or its unauthorized address
  1388. * [X | 0x80].
  1389. *
  1390. * We add 1 as an offset to the one-based USB-stack port number
  1391. * (zero-based wusb virtual port index) for two reasons: (a) dev addr
  1392. * 0 is reserved by USB for default address; (b) Linux's USB stack
  1393. * uses always #1 for the root hub of the controller. So USB stack's
  1394. * port #1, which is wusb virtual-port #0 has address #2.
  1395. *
  1396. * Devices connected under xHCI are not as simple. The host controller
  1397. * supports virtualization, so the hardware assigns device addresses and
  1398. * the HCD must setup data structures before issuing a set address
  1399. * command to the hardware.
  1400. */
  1401. static void choose_devnum(struct usb_device *udev)
  1402. {
  1403. int devnum;
  1404. struct usb_bus *bus = udev->bus;
  1405. /* If khubd ever becomes multithreaded, this will need a lock */
  1406. if (udev->wusb) {
  1407. devnum = udev->portnum + 1;
  1408. BUG_ON(test_bit(devnum, bus->devmap.devicemap));
  1409. } else {
  1410. /* Try to allocate the next devnum beginning at
  1411. * bus->devnum_next. */
  1412. devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
  1413. bus->devnum_next);
  1414. if (devnum >= 128)
  1415. devnum = find_next_zero_bit(bus->devmap.devicemap,
  1416. 128, 1);
  1417. bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
  1418. }
  1419. if (devnum < 128) {
  1420. set_bit(devnum, bus->devmap.devicemap);
  1421. udev->devnum = devnum;
  1422. }
  1423. }
  1424. static void release_devnum(struct usb_device *udev)
  1425. {
  1426. if (udev->devnum > 0) {
  1427. clear_bit(udev->devnum, udev->bus->devmap.devicemap);
  1428. udev->devnum = -1;
  1429. }
  1430. }
  1431. static void update_devnum(struct usb_device *udev, int devnum)
  1432. {
  1433. /* The address for a WUSB device is managed by wusbcore. */
  1434. if (!udev->wusb)
  1435. udev->devnum = devnum;
  1436. }
  1437. static void hub_free_dev(struct usb_device *udev)
  1438. {
  1439. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1440. /* Root hubs aren't real devices, so don't free HCD resources */
  1441. if (hcd->driver->free_dev && udev->parent)
  1442. hcd->driver->free_dev(hcd, udev);
  1443. }
  1444. /**
  1445. * usb_disconnect - disconnect a device (usbcore-internal)
  1446. * @pdev: pointer to device being disconnected
  1447. * Context: !in_interrupt ()
  1448. *
  1449. * Something got disconnected. Get rid of it and all of its children.
  1450. *
  1451. * If *pdev is a normal device then the parent hub must already be locked.
  1452. * If *pdev is a root hub then this routine will acquire the
  1453. * usb_bus_list_lock on behalf of the caller.
  1454. *
  1455. * Only hub drivers (including virtual root hub drivers for host
  1456. * controllers) should ever call this.
  1457. *
  1458. * This call is synchronous, and may not be used in an interrupt context.
  1459. */
  1460. void usb_disconnect(struct usb_device **pdev)
  1461. {
  1462. struct usb_device *udev = *pdev;
  1463. int i;
  1464. /* mark the device as inactive, so any further urb submissions for
  1465. * this device (and any of its children) will fail immediately.
  1466. * this quiesces everything except pending urbs.
  1467. */
  1468. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  1469. dev_info(&udev->dev, "USB disconnect, device number %d\n",
  1470. udev->devnum);
  1471. usb_lock_device(udev);
  1472. /* Free up all the children before we remove this device */
  1473. for (i = 0; i < udev->maxchild; i++) {
  1474. if (udev->children[i])
  1475. usb_disconnect(&udev->children[i]);
  1476. }
  1477. /* deallocate hcd/hardware state ... nuking all pending urbs and
  1478. * cleaning up all state associated with the current configuration
  1479. * so that the hardware is now fully quiesced.
  1480. */
  1481. dev_dbg (&udev->dev, "unregistering device\n");
  1482. usb_disable_device(udev, 0);
  1483. usb_hcd_synchronize_unlinks(udev);
  1484. usb_remove_ep_devs(&udev->ep0);
  1485. usb_unlock_device(udev);
  1486. /* Unregister the device. The device driver is responsible
  1487. * for de-configuring the device and invoking the remove-device
  1488. * notifier chain (used by usbfs and possibly others).
  1489. */
  1490. device_del(&udev->dev);
  1491. /* Free the device number and delete the parent's children[]
  1492. * (or root_hub) pointer.
  1493. */
  1494. release_devnum(udev);
  1495. /* Avoid races with recursively_mark_NOTATTACHED() */
  1496. spin_lock_irq(&device_state_lock);
  1497. *pdev = NULL;
  1498. spin_unlock_irq(&device_state_lock);
  1499. hub_free_dev(udev);
  1500. put_device(&udev->dev);
  1501. }
  1502. #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
  1503. static void show_string(struct usb_device *udev, char *id, char *string)
  1504. {
  1505. if (!string)
  1506. return;
  1507. dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
  1508. }
  1509. static void announce_device(struct usb_device *udev)
  1510. {
  1511. dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
  1512. le16_to_cpu(udev->descriptor.idVendor),
  1513. le16_to_cpu(udev->descriptor.idProduct));
  1514. dev_info(&udev->dev,
  1515. "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  1516. udev->descriptor.iManufacturer,
  1517. udev->descriptor.iProduct,
  1518. udev->descriptor.iSerialNumber);
  1519. show_string(udev, "Product", udev->product);
  1520. show_string(udev, "Manufacturer", udev->manufacturer);
  1521. show_string(udev, "SerialNumber", udev->serial);
  1522. }
  1523. #else
  1524. static inline void announce_device(struct usb_device *udev) { }
  1525. #endif
  1526. #ifdef CONFIG_USB_OTG
  1527. #include "otg_whitelist.h"
  1528. #endif
  1529. /**
  1530. * usb_enumerate_device_otg - FIXME (usbcore-internal)
  1531. * @udev: newly addressed device (in ADDRESS state)
  1532. *
  1533. * Finish enumeration for On-The-Go devices
  1534. */
  1535. static int usb_enumerate_device_otg(struct usb_device *udev)
  1536. {
  1537. int err = 0;
  1538. #ifdef CONFIG_USB_OTG
  1539. /*
  1540. * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
  1541. * to wake us after we've powered off VBUS; and HNP, switching roles
  1542. * "host" to "peripheral". The OTG descriptor helps figure this out.
  1543. */
  1544. if (!udev->bus->is_b_host
  1545. && udev->config
  1546. && udev->parent == udev->bus->root_hub) {
  1547. struct usb_otg_descriptor *desc = NULL;
  1548. struct usb_bus *bus = udev->bus;
  1549. /* descriptor may appear anywhere in config */
  1550. if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
  1551. le16_to_cpu(udev->config[0].desc.wTotalLength),
  1552. USB_DT_OTG, (void **) &desc) == 0) {
  1553. if (desc->bmAttributes & USB_OTG_HNP) {
  1554. unsigned port1 = udev->portnum;
  1555. dev_info(&udev->dev,
  1556. "Dual-Role OTG device on %sHNP port\n",
  1557. (port1 == bus->otg_port)
  1558. ? "" : "non-");
  1559. /* enable HNP before suspend, it's simpler */
  1560. if (port1 == bus->otg_port)
  1561. bus->b_hnp_enable = 1;
  1562. err = usb_control_msg(udev,
  1563. usb_sndctrlpipe(udev, 0),
  1564. USB_REQ_SET_FEATURE, 0,
  1565. bus->b_hnp_enable
  1566. ? USB_DEVICE_B_HNP_ENABLE
  1567. : USB_DEVICE_A_ALT_HNP_SUPPORT,
  1568. 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  1569. if (err < 0) {
  1570. /* OTG MESSAGE: report errors here,
  1571. * customize to match your product.
  1572. */
  1573. dev_info(&udev->dev,
  1574. "can't set HNP mode: %d\n",
  1575. err);
  1576. bus->b_hnp_enable = 0;
  1577. }
  1578. }
  1579. }
  1580. }
  1581. if (!is_targeted(udev)) {
  1582. /* Maybe it can talk to us, though we can't talk to it.
  1583. * (Includes HNP test device.)
  1584. */
  1585. if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
  1586. err = usb_port_suspend(udev, PMSG_SUSPEND);
  1587. if (err < 0)
  1588. dev_dbg(&udev->dev, "HNP fail, %d\n", err);
  1589. }
  1590. err = -ENOTSUPP;
  1591. goto fail;
  1592. }
  1593. fail:
  1594. #endif
  1595. return err;
  1596. }
  1597. /**
  1598. * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
  1599. * @udev: newly addressed device (in ADDRESS state)
  1600. *
  1601. * This is only called by usb_new_device() and usb_authorize_device()
  1602. * and FIXME -- all comments that apply to them apply here wrt to
  1603. * environment.
  1604. *
  1605. * If the device is WUSB and not authorized, we don't attempt to read
  1606. * the string descriptors, as they will be errored out by the device
  1607. * until it has been authorized.
  1608. */
  1609. static int usb_enumerate_device(struct usb_device *udev)
  1610. {
  1611. int err;
  1612. if (udev->config == NULL) {
  1613. err = usb_get_configuration(udev);
  1614. if (err < 0) {
  1615. dev_err(&udev->dev, "can't read configurations, error %d\n",
  1616. err);
  1617. goto fail;
  1618. }
  1619. }
  1620. if (udev->wusb == 1 && udev->authorized == 0) {
  1621. udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1622. udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1623. udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1624. }
  1625. else {
  1626. /* read the standard strings and cache them if present */
  1627. udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
  1628. udev->manufacturer = usb_cache_string(udev,
  1629. udev->descriptor.iManufacturer);
  1630. udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
  1631. }
  1632. err = usb_enumerate_device_otg(udev);
  1633. fail:
  1634. return err;
  1635. }
  1636. static void set_usb_port_removable(struct usb_device *udev)
  1637. {
  1638. struct usb_device *hdev = udev->parent;
  1639. struct usb_hub *hub;
  1640. u8 port = udev->portnum;
  1641. u16 wHubCharacteristics;
  1642. bool removable = true;
  1643. if (!hdev)
  1644. return;
  1645. hub = hdev_to_hub(udev->parent);
  1646. wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
  1647. if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
  1648. return;
  1649. if (hub_is_superspeed(hdev)) {
  1650. if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
  1651. removable = false;
  1652. } else {
  1653. if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
  1654. removable = false;
  1655. }
  1656. if (removable)
  1657. udev->removable = USB_DEVICE_REMOVABLE;
  1658. else
  1659. udev->removable = USB_DEVICE_FIXED;
  1660. }
  1661. /**
  1662. * usb_new_device - perform initial device setup (usbcore-internal)
  1663. * @udev: newly addressed device (in ADDRESS state)
  1664. *
  1665. * This is called with devices which have been detected but not fully
  1666. * enumerated. The device descriptor is available, but not descriptors
  1667. * for any device configuration. The caller must have locked either
  1668. * the parent hub (if udev is a normal device) or else the
  1669. * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
  1670. * udev has already been installed, but udev is not yet visible through
  1671. * sysfs or other filesystem code.
  1672. *
  1673. * It will return if the device is configured properly or not. Zero if
  1674. * the interface was registered with the driver core; else a negative
  1675. * errno value.
  1676. *
  1677. * This call is synchronous, and may not be used in an interrupt context.
  1678. *
  1679. * Only the hub driver or root-hub registrar should ever call this.
  1680. */
  1681. int usb_new_device(struct usb_device *udev)
  1682. {
  1683. int err;
  1684. if (udev->parent) {
  1685. /* Initialize non-root-hub device wakeup to disabled;
  1686. * device (un)configuration controls wakeup capable
  1687. * sysfs power/wakeup controls wakeup enabled/disabled
  1688. */
  1689. device_init_wakeup(&udev->dev, 0);
  1690. }
  1691. /* Tell the runtime-PM framework the device is active */
  1692. pm_runtime_set_active(&udev->dev);
  1693. pm_runtime_get_noresume(&udev->dev);
  1694. pm_runtime_use_autosuspend(&udev->dev);
  1695. pm_runtime_enable(&udev->dev);
  1696. /* By default, forbid autosuspend for all devices. It will be
  1697. * allowed for hubs during binding.
  1698. */
  1699. usb_disable_autosuspend(udev);
  1700. err = usb_enumerate_device(udev); /* Read descriptors */
  1701. if (err < 0)
  1702. goto fail;
  1703. dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
  1704. udev->devnum, udev->bus->busnum,
  1705. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  1706. /* export the usbdev device-node for libusb */
  1707. udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
  1708. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  1709. /* Tell the world! */
  1710. announce_device(udev);
  1711. device_enable_async_suspend(&udev->dev);
  1712. /*
  1713. * check whether the hub marks this port as non-removable. Do it
  1714. * now so that platform-specific data can override it in
  1715. * device_add()
  1716. */
  1717. if (udev->parent)
  1718. set_usb_port_removable(udev);
  1719. /* Register the device. The device driver is responsible
  1720. * for configuring the device and invoking the add-device
  1721. * notifier chain (used by usbfs and possibly others).
  1722. */
  1723. err = device_add(&udev->dev);
  1724. if (err) {
  1725. dev_err(&udev->dev, "can't device_add, error %d\n", err);
  1726. goto fail;
  1727. }
  1728. (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
  1729. usb_mark_last_busy(udev);
  1730. pm_runtime_put_sync_autosuspend(&udev->dev);
  1731. return err;
  1732. fail:
  1733. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  1734. pm_runtime_disable(&udev->dev);
  1735. pm_runtime_set_suspended(&udev->dev);
  1736. return err;
  1737. }
  1738. /**
  1739. * usb_deauthorize_device - deauthorize a device (usbcore-internal)
  1740. * @usb_dev: USB device
  1741. *
  1742. * Move the USB device to a very basic state where interfaces are disabled
  1743. * and the device is in fact unconfigured and unusable.
  1744. *
  1745. * We share a lock (that we have) with device_del(), so we need to
  1746. * defer its call.
  1747. */
  1748. int usb_deauthorize_device(struct usb_device *usb_dev)
  1749. {
  1750. usb_lock_device(usb_dev);
  1751. if (usb_dev->authorized == 0)
  1752. goto out_unauthorized;
  1753. usb_dev->authorized = 0;
  1754. usb_set_configuration(usb_dev, -1);
  1755. kfree(usb_dev->product);
  1756. usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1757. kfree(usb_dev->manufacturer);
  1758. usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1759. kfree(usb_dev->serial);
  1760. usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1761. usb_destroy_configuration(usb_dev);
  1762. usb_dev->descriptor.bNumConfigurations = 0;
  1763. out_unauthorized:
  1764. usb_unlock_device(usb_dev);
  1765. return 0;
  1766. }
  1767. int usb_authorize_device(struct usb_device *usb_dev)
  1768. {
  1769. int result = 0, c;
  1770. usb_lock_device(usb_dev);
  1771. if (usb_dev->authorized == 1)
  1772. goto out_authorized;
  1773. result = usb_autoresume_device(usb_dev);
  1774. if (result < 0) {
  1775. dev_err(&usb_dev->dev,
  1776. "can't autoresume for authorization: %d\n", result);
  1777. goto error_autoresume;
  1778. }
  1779. result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
  1780. if (result < 0) {
  1781. dev_err(&usb_dev->dev, "can't re-read device descriptor for "
  1782. "authorization: %d\n", result);
  1783. goto error_device_descriptor;
  1784. }
  1785. kfree(usb_dev->product);
  1786. usb_dev->product = NULL;
  1787. kfree(usb_dev->manufacturer);
  1788. usb_dev->manufacturer = NULL;
  1789. kfree(usb_dev->serial);
  1790. usb_dev->serial = NULL;
  1791. usb_dev->authorized = 1;
  1792. result = usb_enumerate_device(usb_dev);
  1793. if (result < 0)
  1794. goto error_enumerate;
  1795. /* Choose and set the configuration. This registers the interfaces
  1796. * with the driver core and lets interface drivers bind to them.
  1797. */
  1798. c = usb_choose_configuration(usb_dev);
  1799. if (c >= 0) {
  1800. result = usb_set_configuration(usb_dev, c);
  1801. if (result) {
  1802. dev_err(&usb_dev->dev,
  1803. "can't set config #%d, error %d\n", c, result);
  1804. /* This need not be fatal. The user can try to
  1805. * set other configurations. */
  1806. }
  1807. }
  1808. dev_info(&usb_dev->dev, "authorized to connect\n");
  1809. error_enumerate:
  1810. error_device_descriptor:
  1811. usb_autosuspend_device(usb_dev);
  1812. error_autoresume:
  1813. out_authorized:
  1814. usb_unlock_device(usb_dev); // complements locktree
  1815. return result;
  1816. }
  1817. /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
  1818. static unsigned hub_is_wusb(struct usb_hub *hub)
  1819. {
  1820. struct usb_hcd *hcd;
  1821. if (hub->hdev->parent != NULL) /* not a root hub? */
  1822. return 0;
  1823. hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
  1824. return hcd->wireless;
  1825. }
  1826. #define PORT_RESET_TRIES 5
  1827. #define SET_ADDRESS_TRIES 2
  1828. #define GET_DESCRIPTOR_TRIES 2
  1829. #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
  1830. #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
  1831. #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
  1832. #define HUB_SHORT_RESET_TIME 10
  1833. #define HUB_BH_RESET_TIME 50
  1834. #define HUB_LONG_RESET_TIME 200
  1835. #define HUB_RESET_TIMEOUT 500
  1836. static int hub_port_reset(struct usb_hub *hub, int port1,
  1837. struct usb_device *udev, unsigned int delay, bool warm);
  1838. /* Is a USB 3.0 port in the Inactive state? */
  1839. static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus)
  1840. {
  1841. return hub_is_superspeed(hub->hdev) &&
  1842. (portstatus & USB_PORT_STAT_LINK_STATE) ==
  1843. USB_SS_PORT_LS_SS_INACTIVE;
  1844. }
  1845. static int hub_port_wait_reset(struct usb_hub *hub, int port1,
  1846. struct usb_device *udev, unsigned int delay, bool warm)
  1847. {
  1848. int delay_time, ret;
  1849. u16 portstatus;
  1850. u16 portchange;
  1851. for (delay_time = 0;
  1852. delay_time < HUB_RESET_TIMEOUT;
  1853. delay_time += delay) {
  1854. /* wait to give the device a chance to reset */
  1855. msleep(delay);
  1856. /* read and decode port status */
  1857. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  1858. if (ret < 0)
  1859. return ret;
  1860. /*
  1861. * Some buggy devices require a warm reset to be issued even
  1862. * when the port appears not to be connected.
  1863. */
  1864. if (!warm) {
  1865. /*
  1866. * Some buggy devices can cause an NEC host controller
  1867. * to transition to the "Error" state after a hot port
  1868. * reset. This will show up as the port state in
  1869. * "Inactive", and the port may also report a
  1870. * disconnect. Forcing a warm port reset seems to make
  1871. * the device work.
  1872. *
  1873. * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
  1874. */
  1875. if (hub_port_inactive(hub, portstatus)) {
  1876. int ret;
  1877. if ((portchange & USB_PORT_STAT_C_CONNECTION))
  1878. clear_port_feature(hub->hdev, port1,
  1879. USB_PORT_FEAT_C_CONNECTION);
  1880. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  1881. clear_port_feature(hub->hdev, port1,
  1882. USB_PORT_FEAT_C_PORT_LINK_STATE);
  1883. if (portchange & USB_PORT_STAT_C_RESET)
  1884. clear_port_feature(hub->hdev, port1,
  1885. USB_PORT_FEAT_C_RESET);
  1886. dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
  1887. port1);
  1888. ret = hub_port_reset(hub, port1,
  1889. udev, HUB_BH_RESET_TIME,
  1890. true);
  1891. if ((portchange & USB_PORT_STAT_C_CONNECTION))
  1892. clear_port_feature(hub->hdev, port1,
  1893. USB_PORT_FEAT_C_CONNECTION);
  1894. return ret;
  1895. }
  1896. /* Device went away? */
  1897. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  1898. return -ENOTCONN;
  1899. /* bomb out completely if the connection bounced */
  1900. if ((portchange & USB_PORT_STAT_C_CONNECTION))
  1901. return -ENOTCONN;
  1902. /* if we`ve finished resetting, then break out of
  1903. * the loop
  1904. */
  1905. if (!(portstatus & USB_PORT_STAT_RESET) &&
  1906. (portstatus & USB_PORT_STAT_ENABLE)) {
  1907. if (hub_is_wusb(hub))
  1908. udev->speed = USB_SPEED_WIRELESS;
  1909. else if (hub_is_superspeed(hub->hdev))
  1910. udev->speed = USB_SPEED_SUPER;
  1911. else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  1912. udev->speed = USB_SPEED_HIGH;
  1913. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  1914. udev->speed = USB_SPEED_LOW;
  1915. else
  1916. udev->speed = USB_SPEED_FULL;
  1917. return 0;
  1918. }
  1919. } else {
  1920. if (portchange & USB_PORT_STAT_C_BH_RESET)
  1921. return 0;
  1922. }
  1923. /* switch to the long delay after two short delay failures */
  1924. if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
  1925. delay = HUB_LONG_RESET_TIME;
  1926. dev_dbg (hub->intfdev,
  1927. "port %d not %sreset yet, waiting %dms\n",
  1928. port1, warm ? "warm " : "", delay);
  1929. }
  1930. return -EBUSY;
  1931. }
  1932. static void hub_port_finish_reset(struct usb_hub *hub, int port1,
  1933. struct usb_device *udev, int *status, bool warm)
  1934. {
  1935. switch (*status) {
  1936. case 0:
  1937. if (!warm) {
  1938. struct usb_hcd *hcd;
  1939. /* TRSTRCY = 10 ms; plus some extra */
  1940. msleep(10 + 40);
  1941. update_devnum(udev, 0);
  1942. hcd = bus_to_hcd(udev->bus);
  1943. if (hcd->driver->reset_device) {
  1944. *status = hcd->driver->reset_device(hcd, udev);
  1945. if (*status < 0) {
  1946. dev_err(&udev->dev, "Cannot reset "
  1947. "HCD device state\n");
  1948. break;
  1949. }
  1950. }
  1951. }
  1952. /* FALL THROUGH */
  1953. case -ENOTCONN:
  1954. case -ENODEV:
  1955. clear_port_feature(hub->hdev,
  1956. port1, USB_PORT_FEAT_C_RESET);
  1957. /* FIXME need disconnect() for NOTATTACHED device */
  1958. if (warm) {
  1959. clear_port_feature(hub->hdev, port1,
  1960. USB_PORT_FEAT_C_BH_PORT_RESET);
  1961. clear_port_feature(hub->hdev, port1,
  1962. USB_PORT_FEAT_C_PORT_LINK_STATE);
  1963. } else {
  1964. usb_set_device_state(udev, *status
  1965. ? USB_STATE_NOTATTACHED
  1966. : USB_STATE_DEFAULT);
  1967. }
  1968. break;
  1969. }
  1970. }
  1971. /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
  1972. static int hub_port_reset(struct usb_hub *hub, int port1,
  1973. struct usb_device *udev, unsigned int delay, bool warm)
  1974. {
  1975. int i, status;
  1976. if (!warm) {
  1977. /* Block EHCI CF initialization during the port reset.
  1978. * Some companion controllers don't like it when they mix.
  1979. */
  1980. down_read(&ehci_cf_port_reset_rwsem);
  1981. } else {
  1982. if (!hub_is_superspeed(hub->hdev)) {
  1983. dev_err(hub->intfdev, "only USB3 hub support "
  1984. "warm reset\n");
  1985. return -EINVAL;
  1986. }
  1987. }
  1988. /* Reset the port */
  1989. for (i = 0; i < PORT_RESET_TRIES; i++) {
  1990. status = set_port_feature(hub->hdev, port1, (warm ?
  1991. USB_PORT_FEAT_BH_PORT_RESET :
  1992. USB_PORT_FEAT_RESET));
  1993. if (status) {
  1994. dev_err(hub->intfdev,
  1995. "cannot %sreset port %d (err = %d)\n",
  1996. warm ? "warm " : "", port1, status);
  1997. } else {
  1998. status = hub_port_wait_reset(hub, port1, udev, delay,
  1999. warm);
  2000. if (status && status != -ENOTCONN)
  2001. dev_dbg(hub->intfdev,
  2002. "port_wait_reset: err = %d\n",
  2003. status);
  2004. }
  2005. /* return on disconnect or reset */
  2006. if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
  2007. hub_port_finish_reset(hub, port1, udev, &status, warm);
  2008. goto done;
  2009. }
  2010. dev_dbg (hub->intfdev,
  2011. "port %d not enabled, trying %sreset again...\n",
  2012. port1, warm ? "warm " : "");
  2013. delay = HUB_LONG_RESET_TIME;
  2014. }
  2015. dev_err (hub->intfdev,
  2016. "Cannot enable port %i. Maybe the USB cable is bad?\n",
  2017. port1);
  2018. done:
  2019. if (!warm)
  2020. up_read(&ehci_cf_port_reset_rwsem);
  2021. return status;
  2022. }
  2023. /* Check if a port is power on */
  2024. static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
  2025. {
  2026. int ret = 0;
  2027. if (hub_is_superspeed(hub->hdev)) {
  2028. if (portstatus & USB_SS_PORT_STAT_POWER)
  2029. ret = 1;
  2030. } else {
  2031. if (portstatus & USB_PORT_STAT_POWER)
  2032. ret = 1;
  2033. }
  2034. return ret;
  2035. }
  2036. #ifdef CONFIG_PM
  2037. /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
  2038. static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
  2039. {
  2040. int ret = 0;
  2041. if (hub_is_superspeed(hub->hdev)) {
  2042. if ((portstatus & USB_PORT_STAT_LINK_STATE)
  2043. == USB_SS_PORT_LS_U3)
  2044. ret = 1;
  2045. } else {
  2046. if (portstatus & USB_PORT_STAT_SUSPEND)
  2047. ret = 1;
  2048. }
  2049. return ret;
  2050. }
  2051. /* Determine whether the device on a port is ready for a normal resume,
  2052. * is ready for a reset-resume, or should be disconnected.
  2053. */
  2054. static int check_port_resume_type(struct usb_device *udev,
  2055. struct usb_hub *hub, int port1,
  2056. int status, unsigned portchange, unsigned portstatus)
  2057. {
  2058. /* Is the device still present? */
  2059. if (status || port_is_suspended(hub, portstatus) ||
  2060. !port_is_power_on(hub, portstatus) ||
  2061. !(portstatus & USB_PORT_STAT_CONNECTION)) {
  2062. if (status >= 0)
  2063. status = -ENODEV;
  2064. }
  2065. /* Can't do a normal resume if the port isn't enabled,
  2066. * so try a reset-resume instead.
  2067. */
  2068. else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
  2069. if (udev->persist_enabled)
  2070. udev->reset_resume = 1;
  2071. else
  2072. status = -ENODEV;
  2073. }
  2074. if (status) {
  2075. dev_dbg(hub->intfdev,
  2076. "port %d status %04x.%04x after resume, %d\n",
  2077. port1, portchange, portstatus, status);
  2078. } else if (udev->reset_resume) {
  2079. /* Late port handoff can set status-change bits */
  2080. if (portchange & USB_PORT_STAT_C_CONNECTION)
  2081. clear_port_feature(hub->hdev, port1,
  2082. USB_PORT_FEAT_C_CONNECTION);
  2083. if (portchange & USB_PORT_STAT_C_ENABLE)
  2084. clear_port_feature(hub->hdev, port1,
  2085. USB_PORT_FEAT_C_ENABLE);
  2086. }
  2087. return status;
  2088. }
  2089. #ifdef CONFIG_USB_SUSPEND
  2090. /*
  2091. * usb_port_suspend - suspend a usb device's upstream port
  2092. * @udev: device that's no longer in active use, not a root hub
  2093. * Context: must be able to sleep; device not locked; pm locks held
  2094. *
  2095. * Suspends a USB device that isn't in active use, conserving power.
  2096. * Devices may wake out of a suspend, if anything important happens,
  2097. * using the remote wakeup mechanism. They may also be taken out of
  2098. * suspend by the host, using usb_port_resume(). It's also routine
  2099. * to disconnect devices while they are suspended.
  2100. *
  2101. * This only affects the USB hardware for a device; its interfaces
  2102. * (and, for hubs, child devices) must already have been suspended.
  2103. *
  2104. * Selective port suspend reduces power; most suspended devices draw
  2105. * less than 500 uA. It's also used in OTG, along with remote wakeup.
  2106. * All devices below the suspended port are also suspended.
  2107. *
  2108. * Devices leave suspend state when the host wakes them up. Some devices
  2109. * also support "remote wakeup", where the device can activate the USB
  2110. * tree above them to deliver data, such as a keypress or packet. In
  2111. * some cases, this wakes the USB host.
  2112. *
  2113. * Suspending OTG devices may trigger HNP, if that's been enabled
  2114. * between a pair of dual-role devices. That will change roles, such
  2115. * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
  2116. *
  2117. * Devices on USB hub ports have only one "suspend" state, corresponding
  2118. * to ACPI D2, "may cause the device to lose some context".
  2119. * State transitions include:
  2120. *
  2121. * - suspend, resume ... when the VBUS power link stays live
  2122. * - suspend, disconnect ... VBUS lost
  2123. *
  2124. * Once VBUS drop breaks the circuit, the port it's using has to go through
  2125. * normal re-enumeration procedures, starting with enabling VBUS power.
  2126. * Other than re-initializing the hub (plug/unplug, except for root hubs),
  2127. * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
  2128. * timer, no SRP, no requests through sysfs.
  2129. *
  2130. * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
  2131. * the root hub for their bus goes into global suspend ... so we don't
  2132. * (falsely) update the device power state to say it suspended.
  2133. *
  2134. * Returns 0 on success, else negative errno.
  2135. */
  2136. int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
  2137. {
  2138. struct usb_hub *hub = hdev_to_hub(udev->parent);
  2139. int port1 = udev->portnum;
  2140. int status;
  2141. /* enable remote wakeup when appropriate; this lets the device
  2142. * wake up the upstream hub (including maybe the root hub).
  2143. *
  2144. * NOTE: OTG devices may issue remote wakeup (or SRP) even when
  2145. * we don't explicitly enable it here.
  2146. */
  2147. if (udev->do_remote_wakeup) {
  2148. if (!hub_is_superspeed(hub->hdev)) {
  2149. status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2150. USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
  2151. USB_DEVICE_REMOTE_WAKEUP, 0,
  2152. NULL, 0,
  2153. USB_CTRL_SET_TIMEOUT);
  2154. } else {
  2155. /* Assume there's only one function on the USB 3.0
  2156. * device and enable remote wake for the first
  2157. * interface. FIXME if the interface association
  2158. * descriptor shows there's more than one function.
  2159. */
  2160. status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2161. USB_REQ_SET_FEATURE,
  2162. USB_RECIP_INTERFACE,
  2163. USB_INTRF_FUNC_SUSPEND,
  2164. USB_INTRF_FUNC_SUSPEND_RW |
  2165. USB_INTRF_FUNC_SUSPEND_LP,
  2166. NULL, 0,
  2167. USB_CTRL_SET_TIMEOUT);
  2168. }
  2169. if (status) {
  2170. dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
  2171. status);
  2172. /* bail if autosuspend is requested */
  2173. if (PMSG_IS_AUTO(msg))
  2174. return status;
  2175. }
  2176. }
  2177. /* disable USB2 hardware LPM */
  2178. if (udev->usb2_hw_lpm_enabled == 1)
  2179. usb_set_usb2_hardware_lpm(udev, 0);
  2180. /* see 7.1.7.6 */
  2181. if (hub_is_superspeed(hub->hdev))
  2182. status = set_port_feature(hub->hdev,
  2183. port1 | (USB_SS_PORT_LS_U3 << 3),
  2184. USB_PORT_FEAT_LINK_STATE);
  2185. else
  2186. status = set_port_feature(hub->hdev, port1,
  2187. USB_PORT_FEAT_SUSPEND);
  2188. if (status) {
  2189. dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
  2190. port1, status);
  2191. /* paranoia: "should not happen" */
  2192. if (udev->do_remote_wakeup)
  2193. (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2194. USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
  2195. USB_DEVICE_REMOTE_WAKEUP, 0,
  2196. NULL, 0,
  2197. USB_CTRL_SET_TIMEOUT);
  2198. /* System sleep transitions should never fail */
  2199. if (!PMSG_IS_AUTO(msg))
  2200. status = 0;
  2201. } else {
  2202. /* device has up to 10 msec to fully suspend */
  2203. dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
  2204. (PMSG_IS_AUTO(msg) ? "auto-" : ""),
  2205. udev->do_remote_wakeup);
  2206. usb_set_device_state(udev, USB_STATE_SUSPENDED);
  2207. msleep(10);
  2208. }
  2209. usb_mark_last_busy(hub->hdev);
  2210. return status;
  2211. }
  2212. /*
  2213. * If the USB "suspend" state is in use (rather than "global suspend"),
  2214. * many devices will be individually taken out of suspend state using
  2215. * special "resume" signaling. This routine kicks in shortly after
  2216. * hardware resume signaling is finished, either because of selective
  2217. * resume (by host) or remote wakeup (by device) ... now see what changed
  2218. * in the tree that's rooted at this device.
  2219. *
  2220. * If @udev->reset_resume is set then the device is reset before the
  2221. * status check is done.
  2222. */
  2223. static int finish_port_resume(struct usb_device *udev)
  2224. {
  2225. int status = 0;
  2226. u16 devstatus;
  2227. /* caller owns the udev device lock */
  2228. dev_dbg(&udev->dev, "%s\n",
  2229. udev->reset_resume ? "finish reset-resume" : "finish resume");
  2230. /* usb ch9 identifies four variants of SUSPENDED, based on what
  2231. * state the device resumes to. Linux currently won't see the
  2232. * first two on the host side; they'd be inside hub_port_init()
  2233. * during many timeouts, but khubd can't suspend until later.
  2234. */
  2235. usb_set_device_state(udev, udev->actconfig
  2236. ? USB_STATE_CONFIGURED
  2237. : USB_STATE_ADDRESS);
  2238. /* 10.5.4.5 says not to reset a suspended port if the attached
  2239. * device is enabled for remote wakeup. Hence the reset
  2240. * operation is carried out here, after the port has been
  2241. * resumed.
  2242. */
  2243. if (udev->reset_resume)
  2244. retry_reset_resume:
  2245. status = usb_reset_and_verify_device(udev);
  2246. /* 10.5.4.5 says be sure devices in the tree are still there.
  2247. * For now let's assume the device didn't go crazy on resume,
  2248. * and device drivers will know about any resume quirks.
  2249. */
  2250. if (status == 0) {
  2251. devstatus = 0;
  2252. status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
  2253. if (status >= 0)
  2254. status = (status > 0 ? 0 : -ENODEV);
  2255. /* If a normal resume failed, try doing a reset-resume */
  2256. if (status && !udev->reset_resume && udev->persist_enabled) {
  2257. dev_dbg(&udev->dev, "retry with reset-resume\n");
  2258. udev->reset_resume = 1;
  2259. goto retry_reset_resume;
  2260. }
  2261. }
  2262. if (status) {
  2263. dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
  2264. status);
  2265. } else if (udev->actconfig) {
  2266. le16_to_cpus(&devstatus);
  2267. if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
  2268. status = usb_control_msg(udev,
  2269. usb_sndctrlpipe(udev, 0),
  2270. USB_REQ_CLEAR_FEATURE,
  2271. USB_RECIP_DEVICE,
  2272. USB_DEVICE_REMOTE_WAKEUP, 0,
  2273. NULL, 0,
  2274. USB_CTRL_SET_TIMEOUT);
  2275. if (status)
  2276. dev_dbg(&udev->dev,
  2277. "disable remote wakeup, status %d\n",
  2278. status);
  2279. }
  2280. status = 0;
  2281. }
  2282. return status;
  2283. }
  2284. /*
  2285. * usb_port_resume - re-activate a suspended usb device's upstream port
  2286. * @udev: device to re-activate, not a root hub
  2287. * Context: must be able to sleep; device not locked; pm locks held
  2288. *
  2289. * This will re-activate the suspended device, increasing power usage
  2290. * while letting drivers communicate again with its endpoints.
  2291. * USB resume explicitly guarantees that the power session between
  2292. * the host and the device is the same as it was when the device
  2293. * suspended.
  2294. *
  2295. * If @udev->reset_resume is set then this routine won't check that the
  2296. * port is still enabled. Furthermore, finish_port_resume() above will
  2297. * reset @udev. The end result is that a broken power session can be
  2298. * recovered and @udev will appear to persist across a loss of VBUS power.
  2299. *
  2300. * For example, if a host controller doesn't maintain VBUS suspend current
  2301. * during a system sleep or is reset when the system wakes up, all the USB
  2302. * power sessions below it will be broken. This is especially troublesome
  2303. * for mass-storage devices containing mounted filesystems, since the
  2304. * device will appear to have disconnected and all the memory mappings
  2305. * to it will be lost. Using the USB_PERSIST facility, the device can be
  2306. * made to appear as if it had not disconnected.
  2307. *
  2308. * This facility can be dangerous. Although usb_reset_and_verify_device() makes
  2309. * every effort to insure that the same device is present after the
  2310. * reset as before, it cannot provide a 100% guarantee. Furthermore it's
  2311. * quite possible for a device to remain unaltered but its media to be
  2312. * changed. If the user replaces a flash memory card while the system is
  2313. * asleep, he will have only himself to blame when the filesystem on the
  2314. * new card is corrupted and the system crashes.
  2315. *
  2316. * Returns 0 on success, else negative errno.
  2317. */
  2318. int usb_port_resume(struct usb_device *udev, pm_message_t msg)
  2319. {
  2320. struct usb_hub *hub = hdev_to_hub(udev->parent);
  2321. int port1 = udev->portnum;
  2322. int status;
  2323. u16 portchange, portstatus;
  2324. /* Skip the initial Clear-Suspend step for a remote wakeup */
  2325. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2326. if (status == 0 && !port_is_suspended(hub, portstatus))
  2327. goto SuspendCleared;
  2328. // dev_dbg(hub->intfdev, "resume port %d\n", port1);
  2329. set_bit(port1, hub->busy_bits);
  2330. /* see 7.1.7.7; affects power usage, but not budgeting */
  2331. if (hub_is_superspeed(hub->hdev))
  2332. status = set_port_feature(hub->hdev,
  2333. port1 | (USB_SS_PORT_LS_U0 << 3),
  2334. USB_PORT_FEAT_LINK_STATE);
  2335. else
  2336. status = clear_port_feature(hub->hdev,
  2337. port1, USB_PORT_FEAT_SUSPEND);
  2338. if (status) {
  2339. dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
  2340. port1, status);
  2341. } else {
  2342. /* drive resume for at least 20 msec */
  2343. dev_dbg(&udev->dev, "usb %sresume\n",
  2344. (PMSG_IS_AUTO(msg) ? "auto-" : ""));
  2345. msleep(25);
  2346. /* Virtual root hubs can trigger on GET_PORT_STATUS to
  2347. * stop resume signaling. Then finish the resume
  2348. * sequence.
  2349. */
  2350. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2351. /* TRSMRCY = 10 msec */
  2352. msleep(10);
  2353. }
  2354. SuspendCleared:
  2355. if (status == 0) {
  2356. if (hub_is_superspeed(hub->hdev)) {
  2357. if (portchange & USB_PORT_STAT_C_LINK_STATE)
  2358. clear_port_feature(hub->hdev, port1,
  2359. USB_PORT_FEAT_C_PORT_LINK_STATE);
  2360. } else {
  2361. if (portchange & USB_PORT_STAT_C_SUSPEND)
  2362. clear_port_feature(hub->hdev, port1,
  2363. USB_PORT_FEAT_C_SUSPEND);
  2364. }
  2365. }
  2366. clear_bit(port1, hub->busy_bits);
  2367. status = check_port_resume_type(udev,
  2368. hub, port1, status, portchange, portstatus);
  2369. if (status == 0)
  2370. status = finish_port_resume(udev);
  2371. if (status < 0) {
  2372. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  2373. hub_port_logical_disconnect(hub, port1);
  2374. } else {
  2375. /* Try to enable USB2 hardware LPM */
  2376. if (udev->usb2_hw_lpm_capable == 1)
  2377. usb_set_usb2_hardware_lpm(udev, 1);
  2378. }
  2379. return status;
  2380. }
  2381. /* caller has locked udev */
  2382. int usb_remote_wakeup(struct usb_device *udev)
  2383. {
  2384. int status = 0;
  2385. if (udev->state == USB_STATE_SUSPENDED) {
  2386. dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
  2387. status = usb_autoresume_device(udev);
  2388. if (status == 0) {
  2389. /* Let the drivers do their thing, then... */
  2390. usb_autosuspend_device(udev);
  2391. }
  2392. }
  2393. return status;
  2394. }
  2395. #else /* CONFIG_USB_SUSPEND */
  2396. /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
  2397. int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
  2398. {
  2399. return 0;
  2400. }
  2401. /* However we may need to do a reset-resume */
  2402. int usb_port_resume(struct usb_device *udev, pm_message_t msg)
  2403. {
  2404. struct usb_hub *hub = hdev_to_hub(udev->parent);
  2405. int port1 = udev->portnum;
  2406. int status;
  2407. u16 portchange, portstatus;
  2408. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2409. status = check_port_resume_type(udev,
  2410. hub, port1, status, portchange, portstatus);
  2411. if (status) {
  2412. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  2413. hub_port_logical_disconnect(hub, port1);
  2414. } else if (udev->reset_resume) {
  2415. dev_dbg(&udev->dev, "reset-resume\n");
  2416. status = usb_reset_and_verify_device(udev);
  2417. }
  2418. return status;
  2419. }
  2420. #endif
  2421. static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
  2422. {
  2423. struct usb_hub *hub = usb_get_intfdata (intf);
  2424. struct usb_device *hdev = hub->hdev;
  2425. unsigned port1;
  2426. int status;
  2427. /* Warn if children aren't already suspended */
  2428. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  2429. struct usb_device *udev;
  2430. udev = hdev->children [port1-1];
  2431. if (udev && udev->can_submit) {
  2432. dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
  2433. if (PMSG_IS_AUTO(msg))
  2434. return -EBUSY;
  2435. }
  2436. }
  2437. if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
  2438. /* Enable hub to send remote wakeup for all ports. */
  2439. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  2440. status = set_port_feature(hdev,
  2441. port1 |
  2442. USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
  2443. USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
  2444. USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
  2445. USB_PORT_FEAT_REMOTE_WAKE_MASK);
  2446. }
  2447. }
  2448. dev_dbg(&intf->dev, "%s\n", __func__);
  2449. /* stop khubd and related activity */
  2450. hub_quiesce(hub, HUB_SUSPEND);
  2451. return 0;
  2452. }
  2453. static int hub_resume(struct usb_interface *intf)
  2454. {
  2455. struct usb_hub *hub = usb_get_intfdata(intf);
  2456. dev_dbg(&intf->dev, "%s\n", __func__);
  2457. hub_activate(hub, HUB_RESUME);
  2458. return 0;
  2459. }
  2460. static int hub_reset_resume(struct usb_interface *intf)
  2461. {
  2462. struct usb_hub *hub = usb_get_intfdata(intf);
  2463. dev_dbg(&intf->dev, "%s\n", __func__);
  2464. hub_activate(hub, HUB_RESET_RESUME);
  2465. return 0;
  2466. }
  2467. /**
  2468. * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
  2469. * @rhdev: struct usb_device for the root hub
  2470. *
  2471. * The USB host controller driver calls this function when its root hub
  2472. * is resumed and Vbus power has been interrupted or the controller
  2473. * has been reset. The routine marks @rhdev as having lost power.
  2474. * When the hub driver is resumed it will take notice and carry out
  2475. * power-session recovery for all the "USB-PERSIST"-enabled child devices;
  2476. * the others will be disconnected.
  2477. */
  2478. void usb_root_hub_lost_power(struct usb_device *rhdev)
  2479. {
  2480. dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
  2481. rhdev->reset_resume = 1;
  2482. }
  2483. EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
  2484. #else /* CONFIG_PM */
  2485. #define hub_suspend NULL
  2486. #define hub_resume NULL
  2487. #define hub_reset_resume NULL
  2488. #endif
  2489. /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
  2490. *
  2491. * Between connect detection and reset signaling there must be a delay
  2492. * of 100ms at least for debounce and power-settling. The corresponding
  2493. * timer shall restart whenever the downstream port detects a disconnect.
  2494. *
  2495. * Apparently there are some bluetooth and irda-dongles and a number of
  2496. * low-speed devices for which this debounce period may last over a second.
  2497. * Not covered by the spec - but easy to deal with.
  2498. *
  2499. * This implementation uses a 1500ms total debounce timeout; if the
  2500. * connection isn't stable by then it returns -ETIMEDOUT. It checks
  2501. * every 25ms for transient disconnects. When the port status has been
  2502. * unchanged for 100ms it returns the port status.
  2503. */
  2504. static int hub_port_debounce(struct usb_hub *hub, int port1)
  2505. {
  2506. int ret;
  2507. int total_time, stable_time = 0;
  2508. u16 portchange, portstatus;
  2509. unsigned connection = 0xffff;
  2510. for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
  2511. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  2512. if (ret < 0)
  2513. return ret;
  2514. if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
  2515. (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
  2516. stable_time += HUB_DEBOUNCE_STEP;
  2517. if (stable_time >= HUB_DEBOUNCE_STABLE)
  2518. break;
  2519. } else {
  2520. stable_time = 0;
  2521. connection = portstatus & USB_PORT_STAT_CONNECTION;
  2522. }
  2523. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  2524. clear_port_feature(hub->hdev, port1,
  2525. USB_PORT_FEAT_C_CONNECTION);
  2526. }
  2527. if (total_time >= HUB_DEBOUNCE_TIMEOUT)
  2528. break;
  2529. msleep(HUB_DEBOUNCE_STEP);
  2530. }
  2531. dev_dbg (hub->intfdev,
  2532. "debounce: port %d: total %dms stable %dms status 0x%x\n",
  2533. port1, total_time, stable_time, portstatus);
  2534. if (stable_time < HUB_DEBOUNCE_STABLE)
  2535. return -ETIMEDOUT;
  2536. return portstatus;
  2537. }
  2538. void usb_ep0_reinit(struct usb_device *udev)
  2539. {
  2540. usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
  2541. usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
  2542. usb_enable_endpoint(udev, &udev->ep0, true);
  2543. }
  2544. EXPORT_SYMBOL_GPL(usb_ep0_reinit);
  2545. #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
  2546. #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
  2547. static int hub_set_address(struct usb_device *udev, int devnum)
  2548. {
  2549. int retval;
  2550. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2551. /*
  2552. * The host controller will choose the device address,
  2553. * instead of the core having chosen it earlier
  2554. */
  2555. if (!hcd->driver->address_device && devnum <= 1)
  2556. return -EINVAL;
  2557. if (udev->state == USB_STATE_ADDRESS)
  2558. return 0;
  2559. if (udev->state != USB_STATE_DEFAULT)
  2560. return -EINVAL;
  2561. if (hcd->driver->address_device)
  2562. retval = hcd->driver->address_device(hcd, udev);
  2563. else
  2564. retval = usb_control_msg(udev, usb_sndaddr0pipe(),
  2565. USB_REQ_SET_ADDRESS, 0, devnum, 0,
  2566. NULL, 0, USB_CTRL_SET_TIMEOUT);
  2567. if (retval == 0) {
  2568. update_devnum(udev, devnum);
  2569. /* Device now using proper address. */
  2570. usb_set_device_state(udev, USB_STATE_ADDRESS);
  2571. usb_ep0_reinit(udev);
  2572. }
  2573. return retval;
  2574. }
  2575. /* Reset device, (re)assign address, get device descriptor.
  2576. * Device connection must be stable, no more debouncing needed.
  2577. * Returns device in USB_STATE_ADDRESS, except on error.
  2578. *
  2579. * If this is called for an already-existing device (as part of
  2580. * usb_reset_and_verify_device), the caller must own the device lock. For a
  2581. * newly detected device that is not accessible through any global
  2582. * pointers, it's not necessary to lock the device.
  2583. */
  2584. static int
  2585. hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
  2586. int retry_counter)
  2587. {
  2588. static DEFINE_MUTEX(usb_address0_mutex);
  2589. struct usb_device *hdev = hub->hdev;
  2590. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  2591. int i, j, retval;
  2592. unsigned delay = HUB_SHORT_RESET_TIME;
  2593. enum usb_device_speed oldspeed = udev->speed;
  2594. const char *speed;
  2595. int devnum = udev->devnum;
  2596. /* root hub ports have a slightly longer reset period
  2597. * (from USB 2.0 spec, section 7.1.7.5)
  2598. */
  2599. if (!hdev->parent) {
  2600. delay = HUB_ROOT_RESET_TIME;
  2601. if (port1 == hdev->bus->otg_port)
  2602. hdev->bus->b_hnp_enable = 0;
  2603. }
  2604. /* Some low speed devices have problems with the quick delay, so */
  2605. /* be a bit pessimistic with those devices. RHbug #23670 */
  2606. if (oldspeed == USB_SPEED_LOW)
  2607. delay = HUB_LONG_RESET_TIME;
  2608. mutex_lock(&usb_address0_mutex);
  2609. /* Reset the device; full speed may morph to high speed */
  2610. /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
  2611. retval = hub_port_reset(hub, port1, udev, delay, false);
  2612. if (retval < 0) /* error or disconnect */
  2613. goto fail;
  2614. /* success, speed is known */
  2615. retval = -ENODEV;
  2616. if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
  2617. dev_dbg(&udev->dev, "device reset changed speed!\n");
  2618. goto fail;
  2619. }
  2620. oldspeed = udev->speed;
  2621. /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
  2622. * it's fixed size except for full speed devices.
  2623. * For Wireless USB devices, ep0 max packet is always 512 (tho
  2624. * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
  2625. */
  2626. switch (udev->speed) {
  2627. case USB_SPEED_SUPER:
  2628. case USB_SPEED_WIRELESS: /* fixed at 512 */
  2629. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
  2630. break;
  2631. case USB_SPEED_HIGH: /* fixed at 64 */
  2632. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  2633. break;
  2634. case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
  2635. /* to determine the ep0 maxpacket size, try to read
  2636. * the device descriptor to get bMaxPacketSize0 and
  2637. * then correct our initial guess.
  2638. */
  2639. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  2640. break;
  2641. case USB_SPEED_LOW: /* fixed at 8 */
  2642. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
  2643. break;
  2644. default:
  2645. goto fail;
  2646. }
  2647. if (udev->speed == USB_SPEED_WIRELESS)
  2648. speed = "variable speed Wireless";
  2649. else
  2650. speed = usb_speed_string(udev->speed);
  2651. if (udev->speed != USB_SPEED_SUPER)
  2652. dev_info(&udev->dev,
  2653. "%s %s USB device number %d using %s\n",
  2654. (udev->config) ? "reset" : "new", speed,
  2655. devnum, udev->bus->controller->driver->name);
  2656. /* Set up TT records, if needed */
  2657. if (hdev->tt) {
  2658. udev->tt = hdev->tt;
  2659. udev->ttport = hdev->ttport;
  2660. } else if (udev->speed != USB_SPEED_HIGH
  2661. && hdev->speed == USB_SPEED_HIGH) {
  2662. if (!hub->tt.hub) {
  2663. dev_err(&udev->dev, "parent hub has no TT\n");
  2664. retval = -EINVAL;
  2665. goto fail;
  2666. }
  2667. udev->tt = &hub->tt;
  2668. udev->ttport = port1;
  2669. }
  2670. /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
  2671. * Because device hardware and firmware is sometimes buggy in
  2672. * this area, and this is how Linux has done it for ages.
  2673. * Change it cautiously.
  2674. *
  2675. * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
  2676. * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
  2677. * so it may help with some non-standards-compliant devices.
  2678. * Otherwise we start with SET_ADDRESS and then try to read the
  2679. * first 8 bytes of the device descriptor to get the ep0 maxpacket
  2680. * value.
  2681. */
  2682. for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
  2683. if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
  2684. struct usb_device_descriptor *buf;
  2685. int r = 0;
  2686. #define GET_DESCRIPTOR_BUFSIZE 64
  2687. buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
  2688. if (!buf) {
  2689. retval = -ENOMEM;
  2690. continue;
  2691. }
  2692. /* Retry on all errors; some devices are flakey.
  2693. * 255 is for WUSB devices, we actually need to use
  2694. * 512 (WUSB1.0[4.8.1]).
  2695. */
  2696. for (j = 0; j < 3; ++j) {
  2697. buf->bMaxPacketSize0 = 0;
  2698. r = usb_control_msg(udev, usb_rcvaddr0pipe(),
  2699. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  2700. USB_DT_DEVICE << 8, 0,
  2701. buf, GET_DESCRIPTOR_BUFSIZE,
  2702. initial_descriptor_timeout);
  2703. switch (buf->bMaxPacketSize0) {
  2704. case 8: case 16: case 32: case 64: case 255:
  2705. if (buf->bDescriptorType ==
  2706. USB_DT_DEVICE) {
  2707. r = 0;
  2708. break;
  2709. }
  2710. /* FALL THROUGH */
  2711. default:
  2712. if (r == 0)
  2713. r = -EPROTO;
  2714. break;
  2715. }
  2716. if (r == 0)
  2717. break;
  2718. }
  2719. udev->descriptor.bMaxPacketSize0 =
  2720. buf->bMaxPacketSize0;
  2721. kfree(buf);
  2722. retval = hub_port_reset(hub, port1, udev, delay, false);
  2723. if (retval < 0) /* error or disconnect */
  2724. goto fail;
  2725. if (oldspeed != udev->speed) {
  2726. dev_dbg(&udev->dev,
  2727. "device reset changed speed!\n");
  2728. retval = -ENODEV;
  2729. goto fail;
  2730. }
  2731. if (r) {
  2732. dev_err(&udev->dev,
  2733. "device descriptor read/64, error %d\n",
  2734. r);
  2735. retval = -EMSGSIZE;
  2736. continue;
  2737. }
  2738. #undef GET_DESCRIPTOR_BUFSIZE
  2739. }
  2740. /*
  2741. * If device is WUSB, we already assigned an
  2742. * unauthorized address in the Connect Ack sequence;
  2743. * authorization will assign the final address.
  2744. */
  2745. if (udev->wusb == 0) {
  2746. for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
  2747. retval = hub_set_address(udev, devnum);
  2748. if (retval >= 0)
  2749. break;
  2750. msleep(200);
  2751. }
  2752. if (retval < 0) {
  2753. dev_err(&udev->dev,
  2754. "device not accepting address %d, error %d\n",
  2755. devnum, retval);
  2756. goto fail;
  2757. }
  2758. if (udev->speed == USB_SPEED_SUPER) {
  2759. devnum = udev->devnum;
  2760. dev_info(&udev->dev,
  2761. "%s SuperSpeed USB device number %d using %s\n",
  2762. (udev->config) ? "reset" : "new",
  2763. devnum, udev->bus->controller->driver->name);
  2764. }
  2765. /* cope with hardware quirkiness:
  2766. * - let SET_ADDRESS settle, some device hardware wants it
  2767. * - read ep0 maxpacket even for high and low speed,
  2768. */
  2769. msleep(10);
  2770. if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
  2771. break;
  2772. }
  2773. retval = usb_get_device_descriptor(udev, 8);
  2774. if (retval < 8) {
  2775. dev_err(&udev->dev,
  2776. "device descriptor read/8, error %d\n",
  2777. retval);
  2778. if (retval >= 0)
  2779. retval = -EMSGSIZE;
  2780. } else {
  2781. retval = 0;
  2782. break;
  2783. }
  2784. }
  2785. if (retval)
  2786. goto fail;
  2787. /*
  2788. * Some superspeed devices have finished the link training process
  2789. * and attached to a superspeed hub port, but the device descriptor
  2790. * got from those devices show they aren't superspeed devices. Warm
  2791. * reset the port attached by the devices can fix them.
  2792. */
  2793. if ((udev->speed == USB_SPEED_SUPER) &&
  2794. (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
  2795. dev_err(&udev->dev, "got a wrong device descriptor, "
  2796. "warm reset device\n");
  2797. hub_port_reset(hub, port1, udev,
  2798. HUB_BH_RESET_TIME, true);
  2799. retval = -EINVAL;
  2800. goto fail;
  2801. }
  2802. if (udev->descriptor.bMaxPacketSize0 == 0xff ||
  2803. udev->speed == USB_SPEED_SUPER)
  2804. i = 512;
  2805. else
  2806. i = udev->descriptor.bMaxPacketSize0;
  2807. if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
  2808. if (udev->speed == USB_SPEED_LOW ||
  2809. !(i == 8 || i == 16 || i == 32 || i == 64)) {
  2810. dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
  2811. retval = -EMSGSIZE;
  2812. goto fail;
  2813. }
  2814. if (udev->speed == USB_SPEED_FULL)
  2815. dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
  2816. else
  2817. dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
  2818. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
  2819. usb_ep0_reinit(udev);
  2820. }
  2821. retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
  2822. if (retval < (signed)sizeof(udev->descriptor)) {
  2823. dev_err(&udev->dev, "device descriptor read/all, error %d\n",
  2824. retval);
  2825. if (retval >= 0)
  2826. retval = -ENOMSG;
  2827. goto fail;
  2828. }
  2829. if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
  2830. retval = usb_get_bos_descriptor(udev);
  2831. if (!retval) {
  2832. if (udev->bos->ext_cap && (USB_LPM_SUPPORT &
  2833. le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
  2834. udev->lpm_capable = 1;
  2835. }
  2836. }
  2837. retval = 0;
  2838. /* notify HCD that we have a device connected and addressed */
  2839. if (hcd->driver->update_device)
  2840. hcd->driver->update_device(hcd, udev);
  2841. fail:
  2842. if (retval) {
  2843. hub_port_disable(hub, port1, 0);
  2844. update_devnum(udev, devnum); /* for disconnect processing */
  2845. }
  2846. mutex_unlock(&usb_address0_mutex);
  2847. return retval;
  2848. }
  2849. static void
  2850. check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
  2851. {
  2852. struct usb_qualifier_descriptor *qual;
  2853. int status;
  2854. qual = kmalloc (sizeof *qual, GFP_KERNEL);
  2855. if (qual == NULL)
  2856. return;
  2857. status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
  2858. qual, sizeof *qual);
  2859. if (status == sizeof *qual) {
  2860. dev_info(&udev->dev, "not running at top speed; "
  2861. "connect to a high speed hub\n");
  2862. /* hub LEDs are probably harder to miss than syslog */
  2863. if (hub->has_indicators) {
  2864. hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
  2865. schedule_delayed_work (&hub->leds, 0);
  2866. }
  2867. }
  2868. kfree(qual);
  2869. }
  2870. static unsigned
  2871. hub_power_remaining (struct usb_hub *hub)
  2872. {
  2873. struct usb_device *hdev = hub->hdev;
  2874. int remaining;
  2875. int port1;
  2876. if (!hub->limited_power)
  2877. return 0;
  2878. remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
  2879. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  2880. struct usb_device *udev = hdev->children[port1 - 1];
  2881. int delta;
  2882. if (!udev)
  2883. continue;
  2884. /* Unconfigured devices may not use more than 100mA,
  2885. * or 8mA for OTG ports */
  2886. if (udev->actconfig)
  2887. delta = udev->actconfig->desc.bMaxPower * 2;
  2888. else if (port1 != udev->bus->otg_port || hdev->parent)
  2889. delta = 100;
  2890. else
  2891. delta = 8;
  2892. if (delta > hub->mA_per_port)
  2893. dev_warn(&udev->dev,
  2894. "%dmA is over %umA budget for port %d!\n",
  2895. delta, hub->mA_per_port, port1);
  2896. remaining -= delta;
  2897. }
  2898. if (remaining < 0) {
  2899. dev_warn(hub->intfdev, "%dmA over power budget!\n",
  2900. - remaining);
  2901. remaining = 0;
  2902. }
  2903. return remaining;
  2904. }
  2905. /* Handle physical or logical connection change events.
  2906. * This routine is called when:
  2907. * a port connection-change occurs;
  2908. * a port enable-change occurs (often caused by EMI);
  2909. * usb_reset_and_verify_device() encounters changed descriptors (as from
  2910. * a firmware download)
  2911. * caller already locked the hub
  2912. */
  2913. static void hub_port_connect_change(struct usb_hub *hub, int port1,
  2914. u16 portstatus, u16 portchange)
  2915. {
  2916. struct usb_device *hdev = hub->hdev;
  2917. struct device *hub_dev = hub->intfdev;
  2918. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  2919. unsigned wHubCharacteristics =
  2920. le16_to_cpu(hub->descriptor->wHubCharacteristics);
  2921. struct usb_device *udev;
  2922. int status, i;
  2923. dev_dbg (hub_dev,
  2924. "port %d, status %04x, change %04x, %s\n",
  2925. port1, portstatus, portchange, portspeed(hub, portstatus));
  2926. if (hub->has_indicators) {
  2927. set_port_led(hub, port1, HUB_LED_AUTO);
  2928. hub->indicator[port1-1] = INDICATOR_AUTO;
  2929. }
  2930. #ifdef CONFIG_USB_OTG
  2931. /* during HNP, don't repeat the debounce */
  2932. if (hdev->bus->is_b_host)
  2933. portchange &= ~(USB_PORT_STAT_C_CONNECTION |
  2934. USB_PORT_STAT_C_ENABLE);
  2935. #endif
  2936. /* Try to resuscitate an existing device */
  2937. udev = hdev->children[port1-1];
  2938. if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
  2939. udev->state != USB_STATE_NOTATTACHED) {
  2940. usb_lock_device(udev);
  2941. if (portstatus & USB_PORT_STAT_ENABLE) {
  2942. status = 0; /* Nothing to do */
  2943. #ifdef CONFIG_USB_SUSPEND
  2944. } else if (udev->state == USB_STATE_SUSPENDED &&
  2945. udev->persist_enabled) {
  2946. /* For a suspended device, treat this as a
  2947. * remote wakeup event.
  2948. */
  2949. status = usb_remote_wakeup(udev);
  2950. #endif
  2951. } else {
  2952. status = -ENODEV; /* Don't resuscitate */
  2953. }
  2954. usb_unlock_device(udev);
  2955. if (status == 0) {
  2956. clear_bit(port1, hub->change_bits);
  2957. return;
  2958. }
  2959. }
  2960. /* Disconnect any existing devices under this port */
  2961. if (udev)
  2962. usb_disconnect(&hdev->children[port1-1]);
  2963. clear_bit(port1, hub->change_bits);
  2964. /* We can forget about a "removed" device when there's a physical
  2965. * disconnect or the connect status changes.
  2966. */
  2967. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  2968. (portchange & USB_PORT_STAT_C_CONNECTION))
  2969. clear_bit(port1, hub->removed_bits);
  2970. if (portchange & (USB_PORT_STAT_C_CONNECTION |
  2971. USB_PORT_STAT_C_ENABLE)) {
  2972. status = hub_port_debounce(hub, port1);
  2973. if (status < 0) {
  2974. if (printk_ratelimit())
  2975. dev_err(hub_dev, "connect-debounce failed, "
  2976. "port %d disabled\n", port1);
  2977. portstatus &= ~USB_PORT_STAT_CONNECTION;
  2978. } else {
  2979. portstatus = status;
  2980. }
  2981. }
  2982. /* Return now if debouncing failed or nothing is connected or
  2983. * the device was "removed".
  2984. */
  2985. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  2986. test_bit(port1, hub->removed_bits)) {
  2987. /* maybe switch power back on (e.g. root hub was reset) */
  2988. if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
  2989. && !port_is_power_on(hub, portstatus))
  2990. set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  2991. if (portstatus & USB_PORT_STAT_ENABLE)
  2992. goto done;
  2993. return;
  2994. }
  2995. for (i = 0; i < SET_CONFIG_TRIES; i++) {
  2996. /* reallocate for each attempt, since references
  2997. * to the previous one can escape in various ways
  2998. */
  2999. udev = usb_alloc_dev(hdev, hdev->bus, port1);
  3000. if (!udev) {
  3001. dev_err (hub_dev,
  3002. "couldn't allocate port %d usb_device\n",
  3003. port1);
  3004. goto done;
  3005. }
  3006. usb_set_device_state(udev, USB_STATE_POWERED);
  3007. udev->bus_mA = hub->mA_per_port;
  3008. udev->level = hdev->level + 1;
  3009. udev->wusb = hub_is_wusb(hub);
  3010. /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
  3011. if (hub_is_superspeed(hub->hdev))
  3012. udev->speed = USB_SPEED_SUPER;
  3013. else
  3014. udev->speed = USB_SPEED_UNKNOWN;
  3015. choose_devnum(udev);
  3016. if (udev->devnum <= 0) {
  3017. status = -ENOTCONN; /* Don't retry */
  3018. goto loop;
  3019. }
  3020. /* reset (non-USB 3.0 devices) and get descriptor */
  3021. status = hub_port_init(hub, udev, port1, i);
  3022. if (status < 0)
  3023. goto loop;
  3024. usb_detect_quirks(udev);
  3025. if (udev->quirks & USB_QUIRK_DELAY_INIT)
  3026. msleep(1000);
  3027. /* consecutive bus-powered hubs aren't reliable; they can
  3028. * violate the voltage drop budget. if the new child has
  3029. * a "powered" LED, users should notice we didn't enable it
  3030. * (without reading syslog), even without per-port LEDs
  3031. * on the parent.
  3032. */
  3033. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
  3034. && udev->bus_mA <= 100) {
  3035. u16 devstat;
  3036. status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
  3037. &devstat);
  3038. if (status < 2) {
  3039. dev_dbg(&udev->dev, "get status %d ?\n", status);
  3040. goto loop_disable;
  3041. }
  3042. le16_to_cpus(&devstat);
  3043. if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  3044. dev_err(&udev->dev,
  3045. "can't connect bus-powered hub "
  3046. "to this port\n");
  3047. if (hub->has_indicators) {
  3048. hub->indicator[port1-1] =
  3049. INDICATOR_AMBER_BLINK;
  3050. schedule_delayed_work (&hub->leds, 0);
  3051. }
  3052. status = -ENOTCONN; /* Don't retry */
  3053. goto loop_disable;
  3054. }
  3055. }
  3056. /* check for devices running slower than they could */
  3057. if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
  3058. && udev->speed == USB_SPEED_FULL
  3059. && highspeed_hubs != 0)
  3060. check_highspeed (hub, udev, port1);
  3061. /* Store the parent's children[] pointer. At this point
  3062. * udev becomes globally accessible, although presumably
  3063. * no one will look at it until hdev is unlocked.
  3064. */
  3065. status = 0;
  3066. /* We mustn't add new devices if the parent hub has
  3067. * been disconnected; we would race with the
  3068. * recursively_mark_NOTATTACHED() routine.
  3069. */
  3070. spin_lock_irq(&device_state_lock);
  3071. if (hdev->state == USB_STATE_NOTATTACHED)
  3072. status = -ENOTCONN;
  3073. else
  3074. hdev->children[port1-1] = udev;
  3075. spin_unlock_irq(&device_state_lock);
  3076. /* Run it through the hoops (find a driver, etc) */
  3077. if (!status) {
  3078. status = usb_new_device(udev);
  3079. if (status) {
  3080. spin_lock_irq(&device_state_lock);
  3081. hdev->children[port1-1] = NULL;
  3082. spin_unlock_irq(&device_state_lock);
  3083. }
  3084. }
  3085. if (status)
  3086. goto loop_disable;
  3087. status = hub_power_remaining(hub);
  3088. if (status)
  3089. dev_dbg(hub_dev, "%dmA power budget left\n", status);
  3090. return;
  3091. loop_disable:
  3092. hub_port_disable(hub, port1, 1);
  3093. loop:
  3094. usb_ep0_reinit(udev);
  3095. release_devnum(udev);
  3096. hub_free_dev(udev);
  3097. usb_put_dev(udev);
  3098. if ((status == -ENOTCONN) || (status == -ENOTSUPP))
  3099. break;
  3100. }
  3101. if (hub->hdev->parent ||
  3102. !hcd->driver->port_handed_over ||
  3103. !(hcd->driver->port_handed_over)(hcd, port1))
  3104. dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
  3105. port1);
  3106. done:
  3107. hub_port_disable(hub, port1, 1);
  3108. if (hcd->driver->relinquish_port && !hub->hdev->parent)
  3109. hcd->driver->relinquish_port(hcd, port1);
  3110. }
  3111. /* Returns 1 if there was a remote wakeup and a connect status change. */
  3112. static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
  3113. u16 portstatus, u16 portchange)
  3114. {
  3115. struct usb_device *hdev;
  3116. struct usb_device *udev;
  3117. int connect_change = 0;
  3118. int ret;
  3119. hdev = hub->hdev;
  3120. udev = hdev->children[port-1];
  3121. if (!hub_is_superspeed(hdev)) {
  3122. if (!(portchange & USB_PORT_STAT_C_SUSPEND))
  3123. return 0;
  3124. clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
  3125. } else {
  3126. if (!udev || udev->state != USB_STATE_SUSPENDED ||
  3127. (portstatus & USB_PORT_STAT_LINK_STATE) !=
  3128. USB_SS_PORT_LS_U0)
  3129. return 0;
  3130. }
  3131. if (udev) {
  3132. /* TRSMRCY = 10 msec */
  3133. msleep(10);
  3134. usb_lock_device(udev);
  3135. ret = usb_remote_wakeup(udev);
  3136. usb_unlock_device(udev);
  3137. if (ret < 0)
  3138. connect_change = 1;
  3139. } else {
  3140. ret = -ENODEV;
  3141. hub_port_disable(hub, port, 1);
  3142. }
  3143. dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
  3144. port, ret);
  3145. return connect_change;
  3146. }
  3147. static void hub_events(void)
  3148. {
  3149. struct list_head *tmp;
  3150. struct usb_device *hdev;
  3151. struct usb_interface *intf;
  3152. struct usb_hub *hub;
  3153. struct device *hub_dev;
  3154. u16 hubstatus;
  3155. u16 hubchange;
  3156. u16 portstatus;
  3157. u16 portchange;
  3158. int i, ret;
  3159. int connect_change, wakeup_change;
  3160. /*
  3161. * We restart the list every time to avoid a deadlock with
  3162. * deleting hubs downstream from this one. This should be
  3163. * safe since we delete the hub from the event list.
  3164. * Not the most efficient, but avoids deadlocks.
  3165. */
  3166. while (1) {
  3167. /* Grab the first entry at the beginning of the list */
  3168. spin_lock_irq(&hub_event_lock);
  3169. if (list_empty(&hub_event_list)) {
  3170. spin_unlock_irq(&hub_event_lock);
  3171. break;
  3172. }
  3173. tmp = hub_event_list.next;
  3174. list_del_init(tmp);
  3175. hub = list_entry(tmp, struct usb_hub, event_list);
  3176. kref_get(&hub->kref);
  3177. spin_unlock_irq(&hub_event_lock);
  3178. hdev = hub->hdev;
  3179. hub_dev = hub->intfdev;
  3180. intf = to_usb_interface(hub_dev);
  3181. dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
  3182. hdev->state, hub->descriptor
  3183. ? hub->descriptor->bNbrPorts
  3184. : 0,
  3185. /* NOTE: expects max 15 ports... */
  3186. (u16) hub->change_bits[0],
  3187. (u16) hub->event_bits[0]);
  3188. /* Lock the device, then check to see if we were
  3189. * disconnected while waiting for the lock to succeed. */
  3190. usb_lock_device(hdev);
  3191. if (unlikely(hub->disconnected))
  3192. goto loop_disconnected;
  3193. /* If the hub has died, clean up after it */
  3194. if (hdev->state == USB_STATE_NOTATTACHED) {
  3195. hub->error = -ENODEV;
  3196. hub_quiesce(hub, HUB_DISCONNECT);
  3197. goto loop;
  3198. }
  3199. /* Autoresume */
  3200. ret = usb_autopm_get_interface(intf);
  3201. if (ret) {
  3202. dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
  3203. goto loop;
  3204. }
  3205. /* If this is an inactive hub, do nothing */
  3206. if (hub->quiescing)
  3207. goto loop_autopm;
  3208. if (hub->error) {
  3209. dev_dbg (hub_dev, "resetting for error %d\n",
  3210. hub->error);
  3211. ret = usb_reset_device(hdev);
  3212. if (ret) {
  3213. dev_dbg (hub_dev,
  3214. "error resetting hub: %d\n", ret);
  3215. goto loop_autopm;
  3216. }
  3217. hub->nerrors = 0;
  3218. hub->error = 0;
  3219. }
  3220. /* deal with port status changes */
  3221. for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
  3222. if (test_bit(i, hub->busy_bits))
  3223. continue;
  3224. connect_change = test_bit(i, hub->change_bits);
  3225. wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
  3226. if (!test_and_clear_bit(i, hub->event_bits) &&
  3227. !connect_change && !wakeup_change)
  3228. continue;
  3229. ret = hub_port_status(hub, i,
  3230. &portstatus, &portchange);
  3231. if (ret < 0)
  3232. continue;
  3233. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  3234. clear_port_feature(hdev, i,
  3235. USB_PORT_FEAT_C_CONNECTION);
  3236. connect_change = 1;
  3237. }
  3238. if (portchange & USB_PORT_STAT_C_ENABLE) {
  3239. if (!connect_change)
  3240. dev_dbg (hub_dev,
  3241. "port %d enable change, "
  3242. "status %08x\n",
  3243. i, portstatus);
  3244. clear_port_feature(hdev, i,
  3245. USB_PORT_FEAT_C_ENABLE);
  3246. /*
  3247. * EM interference sometimes causes badly
  3248. * shielded USB devices to be shutdown by
  3249. * the hub, this hack enables them again.
  3250. * Works at least with mouse driver.
  3251. */
  3252. if (!(portstatus & USB_PORT_STAT_ENABLE)
  3253. && !connect_change
  3254. && hdev->children[i-1]) {
  3255. dev_err (hub_dev,
  3256. "port %i "
  3257. "disabled by hub (EMI?), "
  3258. "re-enabling...\n",
  3259. i);
  3260. connect_change = 1;
  3261. }
  3262. }
  3263. if (hub_handle_remote_wakeup(hub, i,
  3264. portstatus, portchange))
  3265. connect_change = 1;
  3266. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  3267. u16 status = 0;
  3268. u16 unused;
  3269. dev_dbg(hub_dev, "over-current change on port "
  3270. "%d\n", i);
  3271. clear_port_feature(hdev, i,
  3272. USB_PORT_FEAT_C_OVER_CURRENT);
  3273. msleep(100); /* Cool down */
  3274. hub_power_on(hub, true);
  3275. hub_port_status(hub, i, &status, &unused);
  3276. if (status & USB_PORT_STAT_OVERCURRENT)
  3277. dev_err(hub_dev, "over-current "
  3278. "condition on port %d\n", i);
  3279. }
  3280. if (portchange & USB_PORT_STAT_C_RESET) {
  3281. dev_dbg (hub_dev,
  3282. "reset change on port %d\n",
  3283. i);
  3284. clear_port_feature(hdev, i,
  3285. USB_PORT_FEAT_C_RESET);
  3286. }
  3287. if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
  3288. hub_is_superspeed(hub->hdev)) {
  3289. dev_dbg(hub_dev,
  3290. "warm reset change on port %d\n",
  3291. i);
  3292. clear_port_feature(hdev, i,
  3293. USB_PORT_FEAT_C_BH_PORT_RESET);
  3294. }
  3295. if (portchange & USB_PORT_STAT_C_LINK_STATE) {
  3296. clear_port_feature(hub->hdev, i,
  3297. USB_PORT_FEAT_C_PORT_LINK_STATE);
  3298. }
  3299. if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
  3300. dev_warn(hub_dev,
  3301. "config error on port %d\n",
  3302. i);
  3303. clear_port_feature(hub->hdev, i,
  3304. USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
  3305. }
  3306. /* Warm reset a USB3 protocol port if it's in
  3307. * SS.Inactive state.
  3308. */
  3309. if (hub_is_superspeed(hub->hdev) &&
  3310. (portstatus & USB_PORT_STAT_LINK_STATE)
  3311. == USB_SS_PORT_LS_SS_INACTIVE) {
  3312. dev_dbg(hub_dev, "warm reset port %d\n", i);
  3313. hub_port_reset(hub, i, NULL,
  3314. HUB_BH_RESET_TIME, true);
  3315. }
  3316. if (connect_change)
  3317. hub_port_connect_change(hub, i,
  3318. portstatus, portchange);
  3319. } /* end for i */
  3320. /* deal with hub status changes */
  3321. if (test_and_clear_bit(0, hub->event_bits) == 0)
  3322. ; /* do nothing */
  3323. else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
  3324. dev_err (hub_dev, "get_hub_status failed\n");
  3325. else {
  3326. if (hubchange & HUB_CHANGE_LOCAL_POWER) {
  3327. dev_dbg (hub_dev, "power change\n");
  3328. clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
  3329. if (hubstatus & HUB_STATUS_LOCAL_POWER)
  3330. /* FIXME: Is this always true? */
  3331. hub->limited_power = 1;
  3332. else
  3333. hub->limited_power = 0;
  3334. }
  3335. if (hubchange & HUB_CHANGE_OVERCURRENT) {
  3336. u16 status = 0;
  3337. u16 unused;
  3338. dev_dbg(hub_dev, "over-current change\n");
  3339. clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
  3340. msleep(500); /* Cool down */
  3341. hub_power_on(hub, true);
  3342. hub_hub_status(hub, &status, &unused);
  3343. if (status & HUB_STATUS_OVERCURRENT)
  3344. dev_err(hub_dev, "over-current "
  3345. "condition\n");
  3346. }
  3347. }
  3348. loop_autopm:
  3349. /* Balance the usb_autopm_get_interface() above */
  3350. usb_autopm_put_interface_no_suspend(intf);
  3351. loop:
  3352. /* Balance the usb_autopm_get_interface_no_resume() in
  3353. * kick_khubd() and allow autosuspend.
  3354. */
  3355. usb_autopm_put_interface(intf);
  3356. loop_disconnected:
  3357. usb_unlock_device(hdev);
  3358. kref_put(&hub->kref, hub_release);
  3359. } /* end while (1) */
  3360. }
  3361. static int hub_thread(void *__unused)
  3362. {
  3363. /* khubd needs to be freezable to avoid intefering with USB-PERSIST
  3364. * port handover. Otherwise it might see that a full-speed device
  3365. * was gone before the EHCI controller had handed its port over to
  3366. * the companion full-speed controller.
  3367. */
  3368. set_freezable();
  3369. do {
  3370. hub_events();
  3371. wait_event_freezable(khubd_wait,
  3372. !list_empty(&hub_event_list) ||
  3373. kthread_should_stop());
  3374. } while (!kthread_should_stop() || !list_empty(&hub_event_list));
  3375. pr_debug("%s: khubd exiting\n", usbcore_name);
  3376. return 0;
  3377. }
  3378. static const struct usb_device_id hub_id_table[] = {
  3379. { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  3380. .bDeviceClass = USB_CLASS_HUB},
  3381. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  3382. .bInterfaceClass = USB_CLASS_HUB},
  3383. { } /* Terminating entry */
  3384. };
  3385. MODULE_DEVICE_TABLE (usb, hub_id_table);
  3386. static struct usb_driver hub_driver = {
  3387. .name = "hub",
  3388. .probe = hub_probe,
  3389. .disconnect = hub_disconnect,
  3390. .suspend = hub_suspend,
  3391. .resume = hub_resume,
  3392. .reset_resume = hub_reset_resume,
  3393. .pre_reset = hub_pre_reset,
  3394. .post_reset = hub_post_reset,
  3395. .unlocked_ioctl = hub_ioctl,
  3396. .id_table = hub_id_table,
  3397. .supports_autosuspend = 1,
  3398. };
  3399. int usb_hub_init(void)
  3400. {
  3401. if (usb_register(&hub_driver) < 0) {
  3402. printk(KERN_ERR "%s: can't register hub driver\n",
  3403. usbcore_name);
  3404. return -1;
  3405. }
  3406. khubd_task = kthread_run(hub_thread, NULL, "khubd");
  3407. if (!IS_ERR(khubd_task))
  3408. return 0;
  3409. /* Fall through if kernel_thread failed */
  3410. usb_deregister(&hub_driver);
  3411. printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
  3412. return -1;
  3413. }
  3414. void usb_hub_cleanup(void)
  3415. {
  3416. kthread_stop(khubd_task);
  3417. /*
  3418. * Hub resources are freed for us by usb_deregister. It calls
  3419. * usb_driver_purge on every device which in turn calls that
  3420. * devices disconnect function if it is using this driver.
  3421. * The hub_disconnect function takes care of releasing the
  3422. * individual hub resources. -greg
  3423. */
  3424. usb_deregister(&hub_driver);
  3425. } /* usb_hub_cleanup() */
  3426. static int descriptors_changed(struct usb_device *udev,
  3427. struct usb_device_descriptor *old_device_descriptor)
  3428. {
  3429. int changed = 0;
  3430. unsigned index;
  3431. unsigned serial_len = 0;
  3432. unsigned len;
  3433. unsigned old_length;
  3434. int length;
  3435. char *buf;
  3436. if (memcmp(&udev->descriptor, old_device_descriptor,
  3437. sizeof(*old_device_descriptor)) != 0)
  3438. return 1;
  3439. /* Since the idVendor, idProduct, and bcdDevice values in the
  3440. * device descriptor haven't changed, we will assume the
  3441. * Manufacturer and Product strings haven't changed either.
  3442. * But the SerialNumber string could be different (e.g., a
  3443. * different flash card of the same brand).
  3444. */
  3445. if (udev->serial)
  3446. serial_len = strlen(udev->serial) + 1;
  3447. len = serial_len;
  3448. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  3449. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  3450. len = max(len, old_length);
  3451. }
  3452. buf = kmalloc(len, GFP_NOIO);
  3453. if (buf == NULL) {
  3454. dev_err(&udev->dev, "no mem to re-read configs after reset\n");
  3455. /* assume the worst */
  3456. return 1;
  3457. }
  3458. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  3459. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  3460. length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
  3461. old_length);
  3462. if (length != old_length) {
  3463. dev_dbg(&udev->dev, "config index %d, error %d\n",
  3464. index, length);
  3465. changed = 1;
  3466. break;
  3467. }
  3468. if (memcmp (buf, udev->rawdescriptors[index], old_length)
  3469. != 0) {
  3470. dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
  3471. index,
  3472. ((struct usb_config_descriptor *) buf)->
  3473. bConfigurationValue);
  3474. changed = 1;
  3475. break;
  3476. }
  3477. }
  3478. if (!changed && serial_len) {
  3479. length = usb_string(udev, udev->descriptor.iSerialNumber,
  3480. buf, serial_len);
  3481. if (length + 1 != serial_len) {
  3482. dev_dbg(&udev->dev, "serial string error %d\n",
  3483. length);
  3484. changed = 1;
  3485. } else if (memcmp(buf, udev->serial, length) != 0) {
  3486. dev_dbg(&udev->dev, "serial string changed\n");
  3487. changed = 1;
  3488. }
  3489. }
  3490. kfree(buf);
  3491. return changed;
  3492. }
  3493. /**
  3494. * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
  3495. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  3496. *
  3497. * WARNING - don't use this routine to reset a composite device
  3498. * (one with multiple interfaces owned by separate drivers)!
  3499. * Use usb_reset_device() instead.
  3500. *
  3501. * Do a port reset, reassign the device's address, and establish its
  3502. * former operating configuration. If the reset fails, or the device's
  3503. * descriptors change from their values before the reset, or the original
  3504. * configuration and altsettings cannot be restored, a flag will be set
  3505. * telling khubd to pretend the device has been disconnected and then
  3506. * re-connected. All drivers will be unbound, and the device will be
  3507. * re-enumerated and probed all over again.
  3508. *
  3509. * Returns 0 if the reset succeeded, -ENODEV if the device has been
  3510. * flagged for logical disconnection, or some other negative error code
  3511. * if the reset wasn't even attempted.
  3512. *
  3513. * The caller must own the device lock. For example, it's safe to use
  3514. * this from a driver probe() routine after downloading new firmware.
  3515. * For calls that might not occur during probe(), drivers should lock
  3516. * the device using usb_lock_device_for_reset().
  3517. *
  3518. * Locking exception: This routine may also be called from within an
  3519. * autoresume handler. Such usage won't conflict with other tasks
  3520. * holding the device lock because these tasks should always call
  3521. * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
  3522. */
  3523. static int usb_reset_and_verify_device(struct usb_device *udev)
  3524. {
  3525. struct usb_device *parent_hdev = udev->parent;
  3526. struct usb_hub *parent_hub;
  3527. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3528. struct usb_device_descriptor descriptor = udev->descriptor;
  3529. int i, ret = 0;
  3530. int port1 = udev->portnum;
  3531. if (udev->state == USB_STATE_NOTATTACHED ||
  3532. udev->state == USB_STATE_SUSPENDED) {
  3533. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  3534. udev->state);
  3535. return -EINVAL;
  3536. }
  3537. if (!parent_hdev) {
  3538. /* this requires hcd-specific logic; see ohci_restart() */
  3539. dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
  3540. return -EISDIR;
  3541. }
  3542. parent_hub = hdev_to_hub(parent_hdev);
  3543. set_bit(port1, parent_hub->busy_bits);
  3544. for (i = 0; i < SET_CONFIG_TRIES; ++i) {
  3545. /* ep0 maxpacket size may change; let the HCD know about it.
  3546. * Other endpoints will be handled by re-enumeration. */
  3547. usb_ep0_reinit(udev);
  3548. ret = hub_port_init(parent_hub, udev, port1, i);
  3549. if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
  3550. break;
  3551. }
  3552. clear_bit(port1, parent_hub->busy_bits);
  3553. if (ret < 0)
  3554. goto re_enumerate;
  3555. /* Device might have changed firmware (DFU or similar) */
  3556. if (descriptors_changed(udev, &descriptor)) {
  3557. dev_info(&udev->dev, "device firmware changed\n");
  3558. udev->descriptor = descriptor; /* for disconnect() calls */
  3559. goto re_enumerate;
  3560. }
  3561. /* Restore the device's previous configuration */
  3562. if (!udev->actconfig)
  3563. goto done;
  3564. mutex_lock(hcd->bandwidth_mutex);
  3565. ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
  3566. if (ret < 0) {
  3567. dev_warn(&udev->dev,
  3568. "Busted HC? Not enough HCD resources for "
  3569. "old configuration.\n");
  3570. mutex_unlock(hcd->bandwidth_mutex);
  3571. goto re_enumerate;
  3572. }
  3573. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3574. USB_REQ_SET_CONFIGURATION, 0,
  3575. udev->actconfig->desc.bConfigurationValue, 0,
  3576. NULL, 0, USB_CTRL_SET_TIMEOUT);
  3577. if (ret < 0) {
  3578. dev_err(&udev->dev,
  3579. "can't restore configuration #%d (error=%d)\n",
  3580. udev->actconfig->desc.bConfigurationValue, ret);
  3581. mutex_unlock(hcd->bandwidth_mutex);
  3582. goto re_enumerate;
  3583. }
  3584. mutex_unlock(hcd->bandwidth_mutex);
  3585. usb_set_device_state(udev, USB_STATE_CONFIGURED);
  3586. /* Put interfaces back into the same altsettings as before.
  3587. * Don't bother to send the Set-Interface request for interfaces
  3588. * that were already in altsetting 0; besides being unnecessary,
  3589. * many devices can't handle it. Instead just reset the host-side
  3590. * endpoint state.
  3591. */
  3592. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  3593. struct usb_host_config *config = udev->actconfig;
  3594. struct usb_interface *intf = config->interface[i];
  3595. struct usb_interface_descriptor *desc;
  3596. desc = &intf->cur_altsetting->desc;
  3597. if (desc->bAlternateSetting == 0) {
  3598. usb_disable_interface(udev, intf, true);
  3599. usb_enable_interface(udev, intf, true);
  3600. ret = 0;
  3601. } else {
  3602. /* Let the bandwidth allocation function know that this
  3603. * device has been reset, and it will have to use
  3604. * alternate setting 0 as the current alternate setting.
  3605. */
  3606. intf->resetting_device = 1;
  3607. ret = usb_set_interface(udev, desc->bInterfaceNumber,
  3608. desc->bAlternateSetting);
  3609. intf->resetting_device = 0;
  3610. }
  3611. if (ret < 0) {
  3612. dev_err(&udev->dev, "failed to restore interface %d "
  3613. "altsetting %d (error=%d)\n",
  3614. desc->bInterfaceNumber,
  3615. desc->bAlternateSetting,
  3616. ret);
  3617. goto re_enumerate;
  3618. }
  3619. }
  3620. done:
  3621. return 0;
  3622. re_enumerate:
  3623. hub_port_logical_disconnect(parent_hub, port1);
  3624. return -ENODEV;
  3625. }
  3626. /**
  3627. * usb_reset_device - warn interface drivers and perform a USB port reset
  3628. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  3629. *
  3630. * Warns all drivers bound to registered interfaces (using their pre_reset
  3631. * method), performs the port reset, and then lets the drivers know that
  3632. * the reset is over (using their post_reset method).
  3633. *
  3634. * Return value is the same as for usb_reset_and_verify_device().
  3635. *
  3636. * The caller must own the device lock. For example, it's safe to use
  3637. * this from a driver probe() routine after downloading new firmware.
  3638. * For calls that might not occur during probe(), drivers should lock
  3639. * the device using usb_lock_device_for_reset().
  3640. *
  3641. * If an interface is currently being probed or disconnected, we assume
  3642. * its driver knows how to handle resets. For all other interfaces,
  3643. * if the driver doesn't have pre_reset and post_reset methods then
  3644. * we attempt to unbind it and rebind afterward.
  3645. */
  3646. int usb_reset_device(struct usb_device *udev)
  3647. {
  3648. int ret;
  3649. int i;
  3650. struct usb_host_config *config = udev->actconfig;
  3651. if (udev->state == USB_STATE_NOTATTACHED ||
  3652. udev->state == USB_STATE_SUSPENDED) {
  3653. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  3654. udev->state);
  3655. return -EINVAL;
  3656. }
  3657. /* Prevent autosuspend during the reset */
  3658. usb_autoresume_device(udev);
  3659. if (config) {
  3660. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  3661. struct usb_interface *cintf = config->interface[i];
  3662. struct usb_driver *drv;
  3663. int unbind = 0;
  3664. if (cintf->dev.driver) {
  3665. drv = to_usb_driver(cintf->dev.driver);
  3666. if (drv->pre_reset && drv->post_reset)
  3667. unbind = (drv->pre_reset)(cintf);
  3668. else if (cintf->condition ==
  3669. USB_INTERFACE_BOUND)
  3670. unbind = 1;
  3671. if (unbind)
  3672. usb_forced_unbind_intf(cintf);
  3673. }
  3674. }
  3675. }
  3676. ret = usb_reset_and_verify_device(udev);
  3677. if (config) {
  3678. for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
  3679. struct usb_interface *cintf = config->interface[i];
  3680. struct usb_driver *drv;
  3681. int rebind = cintf->needs_binding;
  3682. if (!rebind && cintf->dev.driver) {
  3683. drv = to_usb_driver(cintf->dev.driver);
  3684. if (drv->post_reset)
  3685. rebind = (drv->post_reset)(cintf);
  3686. else if (cintf->condition ==
  3687. USB_INTERFACE_BOUND)
  3688. rebind = 1;
  3689. }
  3690. if (ret == 0 && rebind)
  3691. usb_rebind_intf(cintf);
  3692. }
  3693. }
  3694. usb_autosuspend_device(udev);
  3695. return ret;
  3696. }
  3697. EXPORT_SYMBOL_GPL(usb_reset_device);
  3698. /**
  3699. * usb_queue_reset_device - Reset a USB device from an atomic context
  3700. * @iface: USB interface belonging to the device to reset
  3701. *
  3702. * This function can be used to reset a USB device from an atomic
  3703. * context, where usb_reset_device() won't work (as it blocks).
  3704. *
  3705. * Doing a reset via this method is functionally equivalent to calling
  3706. * usb_reset_device(), except for the fact that it is delayed to a
  3707. * workqueue. This means that any drivers bound to other interfaces
  3708. * might be unbound, as well as users from usbfs in user space.
  3709. *
  3710. * Corner cases:
  3711. *
  3712. * - Scheduling two resets at the same time from two different drivers
  3713. * attached to two different interfaces of the same device is
  3714. * possible; depending on how the driver attached to each interface
  3715. * handles ->pre_reset(), the second reset might happen or not.
  3716. *
  3717. * - If a driver is unbound and it had a pending reset, the reset will
  3718. * be cancelled.
  3719. *
  3720. * - This function can be called during .probe() or .disconnect()
  3721. * times. On return from .disconnect(), any pending resets will be
  3722. * cancelled.
  3723. *
  3724. * There is no no need to lock/unlock the @reset_ws as schedule_work()
  3725. * does its own.
  3726. *
  3727. * NOTE: We don't do any reference count tracking because it is not
  3728. * needed. The lifecycle of the work_struct is tied to the
  3729. * usb_interface. Before destroying the interface we cancel the
  3730. * work_struct, so the fact that work_struct is queued and or
  3731. * running means the interface (and thus, the device) exist and
  3732. * are referenced.
  3733. */
  3734. void usb_queue_reset_device(struct usb_interface *iface)
  3735. {
  3736. schedule_work(&iface->reset_ws);
  3737. }
  3738. EXPORT_SYMBOL_GPL(usb_queue_reset_device);