hub.c 95 KB

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