hub.c 90 KB

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