hub.c 120 KB

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