hub.c 82 KB

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