hub.c 80 KB

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