hub.c 84 KB

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