hub.c 84 KB

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