hub.c 106 KB

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