hub.c 82 KB

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