hub.c 96 KB

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