hub.c 86 KB

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