hub.c 105 KB

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