hub.c 119 KB

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