hub.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952
  1. /*
  2. * USB hub driver.
  3. *
  4. * (C) Copyright 1999 Linus Torvalds
  5. * (C) Copyright 1999 Johannes Erdfelt
  6. * (C) Copyright 1999 Gregory P. Smith
  7. * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
  8. *
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/completion.h>
  15. #include <linux/sched.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/ioctl.h>
  19. #include <linux/usb.h>
  20. #include <linux/usbdevice_fs.h>
  21. #include <linux/usb/hcd.h>
  22. #include <linux/usb/quirks.h>
  23. #include <linux/kthread.h>
  24. #include <linux/mutex.h>
  25. #include <linux/freezer.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/byteorder.h>
  28. #include "usb.h"
  29. /* if we are in debug mode, always announce new devices */
  30. #ifdef DEBUG
  31. #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
  32. #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
  33. #endif
  34. #endif
  35. struct usb_hub {
  36. struct device *intfdev; /* the "interface" device */
  37. struct usb_device *hdev;
  38. struct kref kref;
  39. struct urb *urb; /* for interrupt polling pipe */
  40. /* buffer for urb ... with extra space in case of babble */
  41. char (*buffer)[8];
  42. union {
  43. struct usb_hub_status hub;
  44. struct usb_port_status port;
  45. } *status; /* buffer for status reports */
  46. struct mutex status_mutex; /* for the status buffer */
  47. int error; /* last reported error */
  48. int nerrors; /* track consecutive errors */
  49. struct list_head event_list; /* hubs w/data or errs ready */
  50. unsigned long event_bits[1]; /* status change bitmask */
  51. unsigned long change_bits[1]; /* ports with logical connect
  52. status change */
  53. unsigned long busy_bits[1]; /* ports being reset or
  54. resumed */
  55. unsigned long removed_bits[1]; /* ports with a "removed"
  56. device present */
  57. #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
  58. #error event_bits[] is too short!
  59. #endif
  60. struct usb_hub_descriptor *descriptor; /* class descriptor */
  61. struct usb_tt tt; /* Transaction Translator */
  62. unsigned mA_per_port; /* current for each child */
  63. unsigned limited_power:1;
  64. unsigned quiescing:1;
  65. unsigned disconnected:1;
  66. unsigned has_indicators:1;
  67. u8 indicator[USB_MAXCHILDREN];
  68. struct delayed_work leds;
  69. struct delayed_work init_work;
  70. void **port_owners;
  71. };
  72. static inline int hub_is_superspeed(struct usb_device *hdev)
  73. {
  74. return (hdev->descriptor.bDeviceProtocol == 3);
  75. }
  76. /* Protect struct usb_device->state and ->children members
  77. * Note: Both are also protected by ->dev.sem, except that ->state can
  78. * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
  79. static DEFINE_SPINLOCK(device_state_lock);
  80. /* khubd's worklist and its lock */
  81. static DEFINE_SPINLOCK(hub_event_lock);
  82. static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
  83. /* Wakes up khubd */
  84. static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
  85. static struct task_struct *khubd_task;
  86. /* cycle leds on hubs that aren't blinking for attention */
  87. static int blinkenlights = 0;
  88. module_param (blinkenlights, bool, S_IRUGO);
  89. MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
  90. /*
  91. * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
  92. * 10 seconds to send reply for the initial 64-byte descriptor request.
  93. */
  94. /* define initial 64-byte descriptor request timeout in milliseconds */
  95. static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
  96. module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
  97. MODULE_PARM_DESC(initial_descriptor_timeout,
  98. "initial 64-byte descriptor request timeout in milliseconds "
  99. "(default 5000 - 5.0 seconds)");
  100. /*
  101. * As of 2.6.10 we introduce a new USB device initialization scheme which
  102. * closely resembles the way Windows works. Hopefully it will be compatible
  103. * with a wider range of devices than the old scheme. However some previously
  104. * working devices may start giving rise to "device not accepting address"
  105. * errors; if that happens the user can try the old scheme by adjusting the
  106. * following module parameters.
  107. *
  108. * For maximum flexibility there are two boolean parameters to control the
  109. * hub driver's behavior. On the first initialization attempt, if the
  110. * "old_scheme_first" parameter is set then the old scheme will be used,
  111. * otherwise the new scheme is used. If that fails and "use_both_schemes"
  112. * is set, then the driver will make another attempt, using the other scheme.
  113. */
  114. static int old_scheme_first = 0;
  115. module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
  116. MODULE_PARM_DESC(old_scheme_first,
  117. "start with the old device initialization scheme");
  118. static int use_both_schemes = 1;
  119. module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
  120. MODULE_PARM_DESC(use_both_schemes,
  121. "try the other device initialization scheme if the "
  122. "first one fails");
  123. /* Mutual exclusion for EHCI CF initialization. This interferes with
  124. * port reset on some companion controllers.
  125. */
  126. DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
  127. EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
  128. #define HUB_DEBOUNCE_TIMEOUT 1500
  129. #define HUB_DEBOUNCE_STEP 25
  130. #define HUB_DEBOUNCE_STABLE 100
  131. static int usb_reset_and_verify_device(struct usb_device *udev);
  132. static inline char *portspeed(struct usb_hub *hub, int portstatus)
  133. {
  134. if (hub_is_superspeed(hub->hdev))
  135. return "5.0 Gb/s";
  136. if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  137. return "480 Mb/s";
  138. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  139. return "1.5 Mb/s";
  140. else
  141. return "12 Mb/s";
  142. }
  143. /* Note that hdev or one of its children must be locked! */
  144. static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
  145. {
  146. if (!hdev || !hdev->actconfig)
  147. return NULL;
  148. return usb_get_intfdata(hdev->actconfig->interface[0]);
  149. }
  150. /* USB 2.0 spec Section 11.24.4.5 */
  151. static int get_hub_descriptor(struct usb_device *hdev, void *data)
  152. {
  153. int i, ret, size;
  154. unsigned dtype;
  155. if (hub_is_superspeed(hdev)) {
  156. dtype = USB_DT_SS_HUB;
  157. size = USB_DT_SS_HUB_SIZE;
  158. } else {
  159. dtype = USB_DT_HUB;
  160. size = sizeof(struct usb_hub_descriptor);
  161. }
  162. for (i = 0; i < 3; i++) {
  163. ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  164. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  165. dtype << 8, 0, data, size,
  166. USB_CTRL_GET_TIMEOUT);
  167. if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
  168. return ret;
  169. }
  170. return -EINVAL;
  171. }
  172. /*
  173. * USB 2.0 spec Section 11.24.2.1
  174. */
  175. static int clear_hub_feature(struct usb_device *hdev, int feature)
  176. {
  177. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  178. USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
  179. }
  180. /*
  181. * USB 2.0 spec Section 11.24.2.2
  182. */
  183. static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
  184. {
  185. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  186. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
  187. NULL, 0, 1000);
  188. }
  189. /*
  190. * USB 2.0 spec Section 11.24.2.13
  191. */
  192. static int set_port_feature(struct usb_device *hdev, int port1, int feature)
  193. {
  194. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  195. USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
  196. NULL, 0, 1000);
  197. }
  198. /*
  199. * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
  200. * for info about using port indicators
  201. */
  202. static void set_port_led(
  203. struct usb_hub *hub,
  204. int port1,
  205. int selector
  206. )
  207. {
  208. int status = set_port_feature(hub->hdev, (selector << 8) | port1,
  209. USB_PORT_FEAT_INDICATOR);
  210. if (status < 0)
  211. dev_dbg (hub->intfdev,
  212. "port %d indicator %s status %d\n",
  213. port1,
  214. ({ char *s; switch (selector) {
  215. case HUB_LED_AMBER: s = "amber"; break;
  216. case HUB_LED_GREEN: s = "green"; break;
  217. case HUB_LED_OFF: s = "off"; break;
  218. case HUB_LED_AUTO: s = "auto"; break;
  219. default: s = "??"; break;
  220. }; s; }),
  221. status);
  222. }
  223. #define LED_CYCLE_PERIOD ((2*HZ)/3)
  224. static void led_work (struct work_struct *work)
  225. {
  226. struct usb_hub *hub =
  227. container_of(work, struct usb_hub, leds.work);
  228. struct usb_device *hdev = hub->hdev;
  229. unsigned i;
  230. unsigned changed = 0;
  231. int cursor = -1;
  232. if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
  233. return;
  234. for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
  235. unsigned selector, mode;
  236. /* 30%-50% duty cycle */
  237. switch (hub->indicator[i]) {
  238. /* cycle marker */
  239. case INDICATOR_CYCLE:
  240. cursor = i;
  241. selector = HUB_LED_AUTO;
  242. mode = INDICATOR_AUTO;
  243. break;
  244. /* blinking green = sw attention */
  245. case INDICATOR_GREEN_BLINK:
  246. selector = HUB_LED_GREEN;
  247. mode = INDICATOR_GREEN_BLINK_OFF;
  248. break;
  249. case INDICATOR_GREEN_BLINK_OFF:
  250. selector = HUB_LED_OFF;
  251. mode = INDICATOR_GREEN_BLINK;
  252. break;
  253. /* blinking amber = hw attention */
  254. case INDICATOR_AMBER_BLINK:
  255. selector = HUB_LED_AMBER;
  256. mode = INDICATOR_AMBER_BLINK_OFF;
  257. break;
  258. case INDICATOR_AMBER_BLINK_OFF:
  259. selector = HUB_LED_OFF;
  260. mode = INDICATOR_AMBER_BLINK;
  261. break;
  262. /* blink green/amber = reserved */
  263. case INDICATOR_ALT_BLINK:
  264. selector = HUB_LED_GREEN;
  265. mode = INDICATOR_ALT_BLINK_OFF;
  266. break;
  267. case INDICATOR_ALT_BLINK_OFF:
  268. selector = HUB_LED_AMBER;
  269. mode = INDICATOR_ALT_BLINK;
  270. break;
  271. default:
  272. continue;
  273. }
  274. if (selector != HUB_LED_AUTO)
  275. changed = 1;
  276. set_port_led(hub, i + 1, selector);
  277. hub->indicator[i] = mode;
  278. }
  279. if (!changed && blinkenlights) {
  280. cursor++;
  281. cursor %= hub->descriptor->bNbrPorts;
  282. set_port_led(hub, cursor + 1, HUB_LED_GREEN);
  283. hub->indicator[cursor] = INDICATOR_CYCLE;
  284. changed++;
  285. }
  286. if (changed)
  287. schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
  288. }
  289. /* use a short timeout for hub/port status fetches */
  290. #define USB_STS_TIMEOUT 1000
  291. #define USB_STS_RETRIES 5
  292. /*
  293. * USB 2.0 spec Section 11.24.2.6
  294. */
  295. static int get_hub_status(struct usb_device *hdev,
  296. struct usb_hub_status *data)
  297. {
  298. int i, status = -ETIMEDOUT;
  299. for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
  300. status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  301. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  302. data, sizeof(*data), USB_STS_TIMEOUT);
  303. }
  304. return status;
  305. }
  306. /*
  307. * USB 2.0 spec Section 11.24.2.7
  308. */
  309. static int get_port_status(struct usb_device *hdev, int port1,
  310. struct usb_port_status *data)
  311. {
  312. int i, status = -ETIMEDOUT;
  313. for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
  314. status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
  315. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
  316. data, sizeof(*data), USB_STS_TIMEOUT);
  317. }
  318. return status;
  319. }
  320. static int hub_port_status(struct usb_hub *hub, int port1,
  321. u16 *status, u16 *change)
  322. {
  323. int ret;
  324. mutex_lock(&hub->status_mutex);
  325. ret = get_port_status(hub->hdev, port1, &hub->status->port);
  326. if (ret < 4) {
  327. dev_err(hub->intfdev,
  328. "%s failed (err = %d)\n", __func__, ret);
  329. if (ret >= 0)
  330. ret = -EIO;
  331. } else {
  332. *status = le16_to_cpu(hub->status->port.wPortStatus);
  333. *change = le16_to_cpu(hub->status->port.wPortChange);
  334. if ((hub->hdev->parent != NULL) &&
  335. hub_is_superspeed(hub->hdev)) {
  336. /* Translate the USB 3 port status */
  337. u16 tmp = *status & USB_SS_PORT_STAT_MASK;
  338. if (*status & USB_SS_PORT_STAT_POWER)
  339. tmp |= USB_PORT_STAT_POWER;
  340. *status = tmp;
  341. }
  342. ret = 0;
  343. }
  344. mutex_unlock(&hub->status_mutex);
  345. return ret;
  346. }
  347. static void kick_khubd(struct usb_hub *hub)
  348. {
  349. unsigned long flags;
  350. spin_lock_irqsave(&hub_event_lock, flags);
  351. if (!hub->disconnected && list_empty(&hub->event_list)) {
  352. list_add_tail(&hub->event_list, &hub_event_list);
  353. /* Suppress autosuspend until khubd runs */
  354. usb_autopm_get_interface_no_resume(
  355. to_usb_interface(hub->intfdev));
  356. wake_up(&khubd_wait);
  357. }
  358. spin_unlock_irqrestore(&hub_event_lock, flags);
  359. }
  360. void usb_kick_khubd(struct usb_device *hdev)
  361. {
  362. struct usb_hub *hub = hdev_to_hub(hdev);
  363. if (hub)
  364. kick_khubd(hub);
  365. }
  366. /* completion function, fires on port status changes and various faults */
  367. static void hub_irq(struct urb *urb)
  368. {
  369. struct usb_hub *hub = urb->context;
  370. int status = urb->status;
  371. unsigned i;
  372. unsigned long bits;
  373. switch (status) {
  374. case -ENOENT: /* synchronous unlink */
  375. case -ECONNRESET: /* async unlink */
  376. case -ESHUTDOWN: /* hardware going away */
  377. return;
  378. default: /* presumably an error */
  379. /* Cause a hub reset after 10 consecutive errors */
  380. dev_dbg (hub->intfdev, "transfer --> %d\n", status);
  381. if ((++hub->nerrors < 10) || hub->error)
  382. goto resubmit;
  383. hub->error = status;
  384. /* FALL THROUGH */
  385. /* let khubd handle things */
  386. case 0: /* we got data: port status changed */
  387. bits = 0;
  388. for (i = 0; i < urb->actual_length; ++i)
  389. bits |= ((unsigned long) ((*hub->buffer)[i]))
  390. << (i*8);
  391. hub->event_bits[0] = bits;
  392. break;
  393. }
  394. hub->nerrors = 0;
  395. /* Something happened, let khubd figure it out */
  396. kick_khubd(hub);
  397. resubmit:
  398. if (hub->quiescing)
  399. return;
  400. if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
  401. && status != -ENODEV && status != -EPERM)
  402. dev_err (hub->intfdev, "resubmit --> %d\n", status);
  403. }
  404. /* USB 2.0 spec Section 11.24.2.3 */
  405. static inline int
  406. hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
  407. {
  408. return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  409. HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
  410. tt, NULL, 0, 1000);
  411. }
  412. /*
  413. * enumeration blocks khubd for a long time. we use keventd instead, since
  414. * long blocking there is the exception, not the rule. accordingly, HCDs
  415. * talking to TTs must queue control transfers (not just bulk and iso), so
  416. * both can talk to the same hub concurrently.
  417. */
  418. static void hub_tt_work(struct work_struct *work)
  419. {
  420. struct usb_hub *hub =
  421. container_of(work, struct usb_hub, tt.clear_work);
  422. unsigned long flags;
  423. int limit = 100;
  424. spin_lock_irqsave (&hub->tt.lock, flags);
  425. while (--limit && !list_empty (&hub->tt.clear_list)) {
  426. struct list_head *next;
  427. struct usb_tt_clear *clear;
  428. struct usb_device *hdev = hub->hdev;
  429. const struct hc_driver *drv;
  430. int status;
  431. next = hub->tt.clear_list.next;
  432. clear = list_entry (next, struct usb_tt_clear, clear_list);
  433. list_del (&clear->clear_list);
  434. /* drop lock so HCD can concurrently report other TT errors */
  435. spin_unlock_irqrestore (&hub->tt.lock, flags);
  436. status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
  437. if (status)
  438. dev_err (&hdev->dev,
  439. "clear tt %d (%04x) error %d\n",
  440. clear->tt, clear->devinfo, status);
  441. /* Tell the HCD, even if the operation failed */
  442. drv = clear->hcd->driver;
  443. if (drv->clear_tt_buffer_complete)
  444. (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
  445. kfree(clear);
  446. spin_lock_irqsave(&hub->tt.lock, flags);
  447. }
  448. spin_unlock_irqrestore (&hub->tt.lock, flags);
  449. }
  450. /**
  451. * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
  452. * @urb: an URB associated with the failed or incomplete split transaction
  453. *
  454. * High speed HCDs use this to tell the hub driver that some split control or
  455. * bulk transaction failed in a way that requires clearing internal state of
  456. * a transaction translator. This is normally detected (and reported) from
  457. * interrupt context.
  458. *
  459. * It may not be possible for that hub to handle additional full (or low)
  460. * speed transactions until that state is fully cleared out.
  461. */
  462. int usb_hub_clear_tt_buffer(struct urb *urb)
  463. {
  464. struct usb_device *udev = urb->dev;
  465. int pipe = urb->pipe;
  466. struct usb_tt *tt = udev->tt;
  467. unsigned long flags;
  468. struct usb_tt_clear *clear;
  469. /* we've got to cope with an arbitrary number of pending TT clears,
  470. * since each TT has "at least two" buffers that can need it (and
  471. * there can be many TTs per hub). even if they're uncommon.
  472. */
  473. if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
  474. dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
  475. /* FIXME recover somehow ... RESET_TT? */
  476. return -ENOMEM;
  477. }
  478. /* info that CLEAR_TT_BUFFER needs */
  479. clear->tt = tt->multi ? udev->ttport : 1;
  480. clear->devinfo = usb_pipeendpoint (pipe);
  481. clear->devinfo |= udev->devnum << 4;
  482. clear->devinfo |= usb_pipecontrol (pipe)
  483. ? (USB_ENDPOINT_XFER_CONTROL << 11)
  484. : (USB_ENDPOINT_XFER_BULK << 11);
  485. if (usb_pipein (pipe))
  486. clear->devinfo |= 1 << 15;
  487. /* info for completion callback */
  488. clear->hcd = bus_to_hcd(udev->bus);
  489. clear->ep = urb->ep;
  490. /* tell keventd to clear state for this TT */
  491. spin_lock_irqsave (&tt->lock, flags);
  492. list_add_tail (&clear->clear_list, &tt->clear_list);
  493. schedule_work(&tt->clear_work);
  494. spin_unlock_irqrestore (&tt->lock, flags);
  495. return 0;
  496. }
  497. EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
  498. /* If do_delay is false, return the number of milliseconds the caller
  499. * needs to delay.
  500. */
  501. static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
  502. {
  503. int port1;
  504. unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
  505. unsigned delay;
  506. u16 wHubCharacteristics =
  507. le16_to_cpu(hub->descriptor->wHubCharacteristics);
  508. /* Enable power on each port. Some hubs have reserved values
  509. * of LPSM (> 2) in their descriptors, even though they are
  510. * USB 2.0 hubs. Some hubs do not implement port-power switching
  511. * but only emulate it. In all cases, the ports won't work
  512. * unless we send these messages to the hub.
  513. */
  514. if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
  515. dev_dbg(hub->intfdev, "enabling power on all ports\n");
  516. else
  517. dev_dbg(hub->intfdev, "trying to enable port power on "
  518. "non-switchable hub\n");
  519. for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
  520. set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
  521. /* Wait at least 100 msec for power to become stable */
  522. delay = max(pgood_delay, (unsigned) 100);
  523. if (do_delay)
  524. msleep(delay);
  525. return delay;
  526. }
  527. static int hub_hub_status(struct usb_hub *hub,
  528. u16 *status, u16 *change)
  529. {
  530. int ret;
  531. mutex_lock(&hub->status_mutex);
  532. ret = get_hub_status(hub->hdev, &hub->status->hub);
  533. if (ret < 0)
  534. dev_err (hub->intfdev,
  535. "%s failed (err = %d)\n", __func__, ret);
  536. else {
  537. *status = le16_to_cpu(hub->status->hub.wHubStatus);
  538. *change = le16_to_cpu(hub->status->hub.wHubChange);
  539. ret = 0;
  540. }
  541. mutex_unlock(&hub->status_mutex);
  542. return ret;
  543. }
  544. static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
  545. {
  546. struct usb_device *hdev = hub->hdev;
  547. int ret = 0;
  548. if (hdev->children[port1-1] && set_state)
  549. usb_set_device_state(hdev->children[port1-1],
  550. USB_STATE_NOTATTACHED);
  551. if (!hub->error && !hub_is_superspeed(hub->hdev))
  552. ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
  553. if (ret)
  554. dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
  555. port1, ret);
  556. return ret;
  557. }
  558. /*
  559. * Disable a port and mark a logical connect-change event, so that some
  560. * time later khubd will disconnect() any existing usb_device on the port
  561. * and will re-enumerate if there actually is a device attached.
  562. */
  563. static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
  564. {
  565. dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
  566. hub_port_disable(hub, port1, 1);
  567. /* FIXME let caller ask to power down the port:
  568. * - some devices won't enumerate without a VBUS power cycle
  569. * - SRP saves power that way
  570. * - ... new call, TBD ...
  571. * That's easy if this hub can switch power per-port, and
  572. * khubd reactivates the port later (timer, SRP, etc).
  573. * Powerdown must be optional, because of reset/DFU.
  574. */
  575. set_bit(port1, hub->change_bits);
  576. kick_khubd(hub);
  577. }
  578. /**
  579. * usb_remove_device - disable a device's port on its parent hub
  580. * @udev: device to be disabled and removed
  581. * Context: @udev locked, must be able to sleep.
  582. *
  583. * After @udev's port has been disabled, khubd is notified and it will
  584. * see that the device has been disconnected. When the device is
  585. * physically unplugged and something is plugged in, the events will
  586. * be received and processed normally.
  587. */
  588. int usb_remove_device(struct usb_device *udev)
  589. {
  590. struct usb_hub *hub;
  591. struct usb_interface *intf;
  592. if (!udev->parent) /* Can't remove a root hub */
  593. return -EINVAL;
  594. hub = hdev_to_hub(udev->parent);
  595. intf = to_usb_interface(hub->intfdev);
  596. usb_autopm_get_interface(intf);
  597. set_bit(udev->portnum, hub->removed_bits);
  598. hub_port_logical_disconnect(hub, udev->portnum);
  599. usb_autopm_put_interface(intf);
  600. return 0;
  601. }
  602. enum hub_activation_type {
  603. HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
  604. HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
  605. };
  606. static void hub_init_func2(struct work_struct *ws);
  607. static void hub_init_func3(struct work_struct *ws);
  608. static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
  609. {
  610. struct usb_device *hdev = hub->hdev;
  611. struct usb_hcd *hcd;
  612. int ret;
  613. int port1;
  614. int status;
  615. bool need_debounce_delay = false;
  616. unsigned delay;
  617. /* Continue a partial initialization */
  618. if (type == HUB_INIT2)
  619. goto init2;
  620. if (type == HUB_INIT3)
  621. goto init3;
  622. /* After a resume, port power should still be on.
  623. * For any other type of activation, turn it on.
  624. */
  625. if (type != HUB_RESUME) {
  626. /* Speed up system boot by using a delayed_work for the
  627. * hub's initial power-up delays. This is pretty awkward
  628. * and the implementation looks like a home-brewed sort of
  629. * setjmp/longjmp, but it saves at least 100 ms for each
  630. * root hub (assuming usbcore is compiled into the kernel
  631. * rather than as a module). It adds up.
  632. *
  633. * This can't be done for HUB_RESUME or HUB_RESET_RESUME
  634. * because for those activation types the ports have to be
  635. * operational when we return. In theory this could be done
  636. * for HUB_POST_RESET, but it's easier not to.
  637. */
  638. if (type == HUB_INIT) {
  639. delay = hub_power_on(hub, false);
  640. PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
  641. schedule_delayed_work(&hub->init_work,
  642. msecs_to_jiffies(delay));
  643. /* Suppress autosuspend until init is done */
  644. usb_autopm_get_interface_no_resume(
  645. to_usb_interface(hub->intfdev));
  646. return; /* Continues at init2: below */
  647. } else if (type == HUB_RESET_RESUME) {
  648. /* The internal host controller state for the hub device
  649. * may be gone after a host power loss on system resume.
  650. * Update the device's info so the HW knows it's a hub.
  651. */
  652. hcd = bus_to_hcd(hdev->bus);
  653. if (hcd->driver->update_hub_device) {
  654. ret = hcd->driver->update_hub_device(hcd, hdev,
  655. &hub->tt, GFP_NOIO);
  656. if (ret < 0) {
  657. dev_err(hub->intfdev, "Host not "
  658. "accepting hub info "
  659. "update.\n");
  660. dev_err(hub->intfdev, "LS/FS devices "
  661. "and hubs may not work "
  662. "under this hub\n.");
  663. }
  664. }
  665. hub_power_on(hub, true);
  666. } else {
  667. hub_power_on(hub, true);
  668. }
  669. }
  670. init2:
  671. /* Check each port and set hub->change_bits to let khubd know
  672. * which ports need attention.
  673. */
  674. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  675. struct usb_device *udev = hdev->children[port1-1];
  676. u16 portstatus, portchange;
  677. portstatus = portchange = 0;
  678. status = hub_port_status(hub, port1, &portstatus, &portchange);
  679. if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
  680. dev_dbg(hub->intfdev,
  681. "port %d: status %04x change %04x\n",
  682. port1, portstatus, portchange);
  683. /* After anything other than HUB_RESUME (i.e., initialization
  684. * or any sort of reset), every port should be disabled.
  685. * Unconnected ports should likewise be disabled (paranoia),
  686. * and so should ports for which we have no usb_device.
  687. */
  688. if ((portstatus & USB_PORT_STAT_ENABLE) && (
  689. type != HUB_RESUME ||
  690. !(portstatus & USB_PORT_STAT_CONNECTION) ||
  691. !udev ||
  692. udev->state == USB_STATE_NOTATTACHED)) {
  693. /*
  694. * USB3 protocol ports will automatically transition
  695. * to Enabled state when detect an USB3.0 device attach.
  696. * Do not disable USB3 protocol ports.
  697. */
  698. if (!hub_is_superspeed(hdev)) {
  699. clear_port_feature(hdev, port1,
  700. USB_PORT_FEAT_ENABLE);
  701. portstatus &= ~USB_PORT_STAT_ENABLE;
  702. } else {
  703. /* Pretend that power was lost for USB3 devs */
  704. portstatus &= ~USB_PORT_STAT_ENABLE;
  705. }
  706. }
  707. /* Clear status-change flags; we'll debounce later */
  708. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  709. need_debounce_delay = true;
  710. clear_port_feature(hub->hdev, port1,
  711. USB_PORT_FEAT_C_CONNECTION);
  712. }
  713. if (portchange & USB_PORT_STAT_C_ENABLE) {
  714. need_debounce_delay = true;
  715. clear_port_feature(hub->hdev, port1,
  716. USB_PORT_FEAT_C_ENABLE);
  717. }
  718. if (portchange & USB_PORT_STAT_C_LINK_STATE) {
  719. need_debounce_delay = true;
  720. clear_port_feature(hub->hdev, port1,
  721. USB_PORT_FEAT_C_PORT_LINK_STATE);
  722. }
  723. /* We can forget about a "removed" device when there's a
  724. * physical disconnect or the connect status changes.
  725. */
  726. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  727. (portchange & USB_PORT_STAT_C_CONNECTION))
  728. clear_bit(port1, hub->removed_bits);
  729. if (!udev || udev->state == USB_STATE_NOTATTACHED) {
  730. /* Tell khubd to disconnect the device or
  731. * check for a new connection
  732. */
  733. if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
  734. set_bit(port1, hub->change_bits);
  735. } else if (portstatus & USB_PORT_STAT_ENABLE) {
  736. /* The power session apparently survived the resume.
  737. * If there was an overcurrent or suspend change
  738. * (i.e., remote wakeup request), have khubd
  739. * take care of it.
  740. */
  741. if (portchange)
  742. set_bit(port1, hub->change_bits);
  743. } else if (udev->persist_enabled) {
  744. #ifdef CONFIG_PM
  745. udev->reset_resume = 1;
  746. #endif
  747. set_bit(port1, hub->change_bits);
  748. } else {
  749. /* The power session is gone; tell khubd */
  750. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  751. set_bit(port1, hub->change_bits);
  752. }
  753. }
  754. /* If no port-status-change flags were set, we don't need any
  755. * debouncing. If flags were set we can try to debounce the
  756. * ports all at once right now, instead of letting khubd do them
  757. * one at a time later on.
  758. *
  759. * If any port-status changes do occur during this delay, khubd
  760. * will see them later and handle them normally.
  761. */
  762. if (need_debounce_delay) {
  763. delay = HUB_DEBOUNCE_STABLE;
  764. /* Don't do a long sleep inside a workqueue routine */
  765. if (type == HUB_INIT2) {
  766. PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
  767. schedule_delayed_work(&hub->init_work,
  768. msecs_to_jiffies(delay));
  769. return; /* Continues at init3: below */
  770. } else {
  771. msleep(delay);
  772. }
  773. }
  774. init3:
  775. hub->quiescing = 0;
  776. status = usb_submit_urb(hub->urb, GFP_NOIO);
  777. if (status < 0)
  778. dev_err(hub->intfdev, "activate --> %d\n", status);
  779. if (hub->has_indicators && blinkenlights)
  780. schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
  781. /* Scan all ports that need attention */
  782. kick_khubd(hub);
  783. /* Allow autosuspend if it was suppressed */
  784. if (type <= HUB_INIT3)
  785. usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
  786. }
  787. /* Implement the continuations for the delays above */
  788. static void hub_init_func2(struct work_struct *ws)
  789. {
  790. struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
  791. hub_activate(hub, HUB_INIT2);
  792. }
  793. static void hub_init_func3(struct work_struct *ws)
  794. {
  795. struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
  796. hub_activate(hub, HUB_INIT3);
  797. }
  798. enum hub_quiescing_type {
  799. HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
  800. };
  801. static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
  802. {
  803. struct usb_device *hdev = hub->hdev;
  804. int i;
  805. cancel_delayed_work_sync(&hub->init_work);
  806. /* khubd and related activity won't re-trigger */
  807. hub->quiescing = 1;
  808. if (type != HUB_SUSPEND) {
  809. /* Disconnect all the children */
  810. for (i = 0; i < hdev->maxchild; ++i) {
  811. if (hdev->children[i])
  812. usb_disconnect(&hdev->children[i]);
  813. }
  814. }
  815. /* Stop khubd and related activity */
  816. usb_kill_urb(hub->urb);
  817. if (hub->has_indicators)
  818. cancel_delayed_work_sync(&hub->leds);
  819. if (hub->tt.hub)
  820. cancel_work_sync(&hub->tt.clear_work);
  821. }
  822. /* caller has locked the hub device */
  823. static int hub_pre_reset(struct usb_interface *intf)
  824. {
  825. struct usb_hub *hub = usb_get_intfdata(intf);
  826. hub_quiesce(hub, HUB_PRE_RESET);
  827. return 0;
  828. }
  829. /* caller has locked the hub device */
  830. static int hub_post_reset(struct usb_interface *intf)
  831. {
  832. struct usb_hub *hub = usb_get_intfdata(intf);
  833. hub_activate(hub, HUB_POST_RESET);
  834. return 0;
  835. }
  836. static int hub_configure(struct usb_hub *hub,
  837. struct usb_endpoint_descriptor *endpoint)
  838. {
  839. struct usb_hcd *hcd;
  840. struct usb_device *hdev = hub->hdev;
  841. struct device *hub_dev = hub->intfdev;
  842. u16 hubstatus, hubchange;
  843. u16 wHubCharacteristics;
  844. unsigned int pipe;
  845. int maxp, ret;
  846. char *message = "out of memory";
  847. hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
  848. if (!hub->buffer) {
  849. ret = -ENOMEM;
  850. goto fail;
  851. }
  852. hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
  853. if (!hub->status) {
  854. ret = -ENOMEM;
  855. goto fail;
  856. }
  857. mutex_init(&hub->status_mutex);
  858. hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
  859. if (!hub->descriptor) {
  860. ret = -ENOMEM;
  861. goto fail;
  862. }
  863. if (hub_is_superspeed(hdev) && (hdev->parent != NULL)) {
  864. ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  865. HUB_SET_DEPTH, USB_RT_HUB,
  866. hdev->level - 1, 0, NULL, 0,
  867. USB_CTRL_SET_TIMEOUT);
  868. if (ret < 0) {
  869. message = "can't set hub depth";
  870. goto fail;
  871. }
  872. }
  873. /* Request the entire hub descriptor.
  874. * hub->descriptor can handle USB_MAXCHILDREN ports,
  875. * but the hub can/will return fewer bytes here.
  876. */
  877. ret = get_hub_descriptor(hdev, hub->descriptor);
  878. if (ret < 0) {
  879. message = "can't read hub descriptor";
  880. goto fail;
  881. } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
  882. message = "hub has too many ports!";
  883. ret = -ENODEV;
  884. goto fail;
  885. }
  886. hdev->maxchild = hub->descriptor->bNbrPorts;
  887. dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
  888. (hdev->maxchild == 1) ? "" : "s");
  889. hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
  890. if (!hub->port_owners) {
  891. ret = -ENOMEM;
  892. goto fail;
  893. }
  894. wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
  895. /* FIXME for USB 3.0, skip for now */
  896. if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
  897. !(hub_is_superspeed(hdev))) {
  898. int i;
  899. char portstr [USB_MAXCHILDREN + 1];
  900. for (i = 0; i < hdev->maxchild; i++)
  901. portstr[i] = hub->descriptor->u.hs.DeviceRemovable
  902. [((i + 1) / 8)] & (1 << ((i + 1) % 8))
  903. ? 'F' : 'R';
  904. portstr[hdev->maxchild] = 0;
  905. dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
  906. } else
  907. dev_dbg(hub_dev, "standalone hub\n");
  908. switch (wHubCharacteristics & HUB_CHAR_LPSM) {
  909. case 0x00:
  910. dev_dbg(hub_dev, "ganged power switching\n");
  911. break;
  912. case 0x01:
  913. dev_dbg(hub_dev, "individual port power switching\n");
  914. break;
  915. case 0x02:
  916. case 0x03:
  917. dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
  918. break;
  919. }
  920. switch (wHubCharacteristics & HUB_CHAR_OCPM) {
  921. case 0x00:
  922. dev_dbg(hub_dev, "global over-current protection\n");
  923. break;
  924. case 0x08:
  925. dev_dbg(hub_dev, "individual port over-current protection\n");
  926. break;
  927. case 0x10:
  928. case 0x18:
  929. dev_dbg(hub_dev, "no over-current protection\n");
  930. break;
  931. }
  932. spin_lock_init (&hub->tt.lock);
  933. INIT_LIST_HEAD (&hub->tt.clear_list);
  934. INIT_WORK(&hub->tt.clear_work, hub_tt_work);
  935. switch (hdev->descriptor.bDeviceProtocol) {
  936. case 0:
  937. break;
  938. case 1:
  939. dev_dbg(hub_dev, "Single TT\n");
  940. hub->tt.hub = hdev;
  941. break;
  942. case 2:
  943. ret = usb_set_interface(hdev, 0, 1);
  944. if (ret == 0) {
  945. dev_dbg(hub_dev, "TT per port\n");
  946. hub->tt.multi = 1;
  947. } else
  948. dev_err(hub_dev, "Using single TT (err %d)\n",
  949. ret);
  950. hub->tt.hub = hdev;
  951. break;
  952. case 3:
  953. /* USB 3.0 hubs don't have a TT */
  954. break;
  955. default:
  956. dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
  957. hdev->descriptor.bDeviceProtocol);
  958. break;
  959. }
  960. /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
  961. switch (wHubCharacteristics & HUB_CHAR_TTTT) {
  962. case HUB_TTTT_8_BITS:
  963. if (hdev->descriptor.bDeviceProtocol != 0) {
  964. hub->tt.think_time = 666;
  965. dev_dbg(hub_dev, "TT requires at most %d "
  966. "FS bit times (%d ns)\n",
  967. 8, hub->tt.think_time);
  968. }
  969. break;
  970. case HUB_TTTT_16_BITS:
  971. hub->tt.think_time = 666 * 2;
  972. dev_dbg(hub_dev, "TT requires at most %d "
  973. "FS bit times (%d ns)\n",
  974. 16, hub->tt.think_time);
  975. break;
  976. case HUB_TTTT_24_BITS:
  977. hub->tt.think_time = 666 * 3;
  978. dev_dbg(hub_dev, "TT requires at most %d "
  979. "FS bit times (%d ns)\n",
  980. 24, hub->tt.think_time);
  981. break;
  982. case HUB_TTTT_32_BITS:
  983. hub->tt.think_time = 666 * 4;
  984. dev_dbg(hub_dev, "TT requires at most %d "
  985. "FS bit times (%d ns)\n",
  986. 32, hub->tt.think_time);
  987. break;
  988. }
  989. /* probe() zeroes hub->indicator[] */
  990. if (wHubCharacteristics & HUB_CHAR_PORTIND) {
  991. hub->has_indicators = 1;
  992. dev_dbg(hub_dev, "Port indicators are supported\n");
  993. }
  994. dev_dbg(hub_dev, "power on to power good time: %dms\n",
  995. hub->descriptor->bPwrOn2PwrGood * 2);
  996. /* power budgeting mostly matters with bus-powered hubs,
  997. * and battery-powered root hubs (may provide just 8 mA).
  998. */
  999. ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
  1000. if (ret < 2) {
  1001. message = "can't get hub status";
  1002. goto fail;
  1003. }
  1004. le16_to_cpus(&hubstatus);
  1005. if (hdev == hdev->bus->root_hub) {
  1006. if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
  1007. hub->mA_per_port = 500;
  1008. else {
  1009. hub->mA_per_port = hdev->bus_mA;
  1010. hub->limited_power = 1;
  1011. }
  1012. } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  1013. dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
  1014. hub->descriptor->bHubContrCurrent);
  1015. hub->limited_power = 1;
  1016. if (hdev->maxchild > 0) {
  1017. int remaining = hdev->bus_mA -
  1018. hub->descriptor->bHubContrCurrent;
  1019. if (remaining < hdev->maxchild * 100)
  1020. dev_warn(hub_dev,
  1021. "insufficient power available "
  1022. "to use all downstream ports\n");
  1023. hub->mA_per_port = 100; /* 7.2.1.1 */
  1024. }
  1025. } else { /* Self-powered external hub */
  1026. /* FIXME: What about battery-powered external hubs that
  1027. * provide less current per port? */
  1028. hub->mA_per_port = 500;
  1029. }
  1030. if (hub->mA_per_port < 500)
  1031. dev_dbg(hub_dev, "%umA bus power budget for each child\n",
  1032. hub->mA_per_port);
  1033. /* Update the HCD's internal representation of this hub before khubd
  1034. * starts getting port status changes for devices under the hub.
  1035. */
  1036. hcd = bus_to_hcd(hdev->bus);
  1037. if (hcd->driver->update_hub_device) {
  1038. ret = hcd->driver->update_hub_device(hcd, hdev,
  1039. &hub->tt, GFP_KERNEL);
  1040. if (ret < 0) {
  1041. message = "can't update HCD hub info";
  1042. goto fail;
  1043. }
  1044. }
  1045. ret = hub_hub_status(hub, &hubstatus, &hubchange);
  1046. if (ret < 0) {
  1047. message = "can't get hub status";
  1048. goto fail;
  1049. }
  1050. /* local power status reports aren't always correct */
  1051. if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
  1052. dev_dbg(hub_dev, "local power source is %s\n",
  1053. (hubstatus & HUB_STATUS_LOCAL_POWER)
  1054. ? "lost (inactive)" : "good");
  1055. if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
  1056. dev_dbg(hub_dev, "%sover-current condition exists\n",
  1057. (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
  1058. /* set up the interrupt endpoint
  1059. * We use the EP's maxpacket size instead of (PORTS+1+7)/8
  1060. * bytes as USB2.0[11.12.3] says because some hubs are known
  1061. * to send more data (and thus cause overflow). For root hubs,
  1062. * maxpktsize is defined in hcd.c's fake endpoint descriptors
  1063. * to be big enough for at least USB_MAXCHILDREN ports. */
  1064. pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
  1065. maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
  1066. if (maxp > sizeof(*hub->buffer))
  1067. maxp = sizeof(*hub->buffer);
  1068. hub->urb = usb_alloc_urb(0, GFP_KERNEL);
  1069. if (!hub->urb) {
  1070. ret = -ENOMEM;
  1071. goto fail;
  1072. }
  1073. usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
  1074. hub, endpoint->bInterval);
  1075. /* maybe cycle the hub leds */
  1076. if (hub->has_indicators && blinkenlights)
  1077. hub->indicator [0] = INDICATOR_CYCLE;
  1078. hub_activate(hub, HUB_INIT);
  1079. return 0;
  1080. fail:
  1081. dev_err (hub_dev, "config failed, %s (err %d)\n",
  1082. message, ret);
  1083. /* hub_disconnect() frees urb and descriptor */
  1084. return ret;
  1085. }
  1086. static void hub_release(struct kref *kref)
  1087. {
  1088. struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
  1089. usb_put_intf(to_usb_interface(hub->intfdev));
  1090. kfree(hub);
  1091. }
  1092. static unsigned highspeed_hubs;
  1093. static void hub_disconnect(struct usb_interface *intf)
  1094. {
  1095. struct usb_hub *hub = usb_get_intfdata (intf);
  1096. /* Take the hub off the event list and don't let it be added again */
  1097. spin_lock_irq(&hub_event_lock);
  1098. if (!list_empty(&hub->event_list)) {
  1099. list_del_init(&hub->event_list);
  1100. usb_autopm_put_interface_no_suspend(intf);
  1101. }
  1102. hub->disconnected = 1;
  1103. spin_unlock_irq(&hub_event_lock);
  1104. /* Disconnect all children and quiesce the hub */
  1105. hub->error = 0;
  1106. hub_quiesce(hub, HUB_DISCONNECT);
  1107. usb_set_intfdata (intf, NULL);
  1108. hub->hdev->maxchild = 0;
  1109. if (hub->hdev->speed == USB_SPEED_HIGH)
  1110. highspeed_hubs--;
  1111. usb_free_urb(hub->urb);
  1112. kfree(hub->port_owners);
  1113. kfree(hub->descriptor);
  1114. kfree(hub->status);
  1115. kfree(hub->buffer);
  1116. kref_put(&hub->kref, hub_release);
  1117. }
  1118. static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1119. {
  1120. struct usb_host_interface *desc;
  1121. struct usb_endpoint_descriptor *endpoint;
  1122. struct usb_device *hdev;
  1123. struct usb_hub *hub;
  1124. desc = intf->cur_altsetting;
  1125. hdev = interface_to_usbdev(intf);
  1126. /* Hubs have proper suspend/resume support. USB 3.0 device suspend is
  1127. * different from USB 2.0/1.1 device suspend, and unfortunately we
  1128. * don't support it yet. So leave autosuspend disabled for USB 3.0
  1129. * external hubs for now. Enable autosuspend for USB 3.0 roothubs,
  1130. * since that isn't a "real" hub.
  1131. */
  1132. if (!hub_is_superspeed(hdev) || !hdev->parent)
  1133. usb_enable_autosuspend(hdev);
  1134. if (hdev->level == MAX_TOPO_LEVEL) {
  1135. dev_err(&intf->dev,
  1136. "Unsupported bus topology: hub nested too deep\n");
  1137. return -E2BIG;
  1138. }
  1139. #ifdef CONFIG_USB_OTG_BLACKLIST_HUB
  1140. if (hdev->parent) {
  1141. dev_warn(&intf->dev, "ignoring external hub\n");
  1142. return -ENODEV;
  1143. }
  1144. #endif
  1145. /* Some hubs have a subclass of 1, which AFAICT according to the */
  1146. /* specs is not defined, but it works */
  1147. if ((desc->desc.bInterfaceSubClass != 0) &&
  1148. (desc->desc.bInterfaceSubClass != 1)) {
  1149. descriptor_error:
  1150. dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
  1151. return -EIO;
  1152. }
  1153. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  1154. if (desc->desc.bNumEndpoints != 1)
  1155. goto descriptor_error;
  1156. endpoint = &desc->endpoint[0].desc;
  1157. /* If it's not an interrupt in endpoint, we'd better punt! */
  1158. if (!usb_endpoint_is_int_in(endpoint))
  1159. goto descriptor_error;
  1160. /* We found a hub */
  1161. dev_info (&intf->dev, "USB hub found\n");
  1162. hub = kzalloc(sizeof(*hub), GFP_KERNEL);
  1163. if (!hub) {
  1164. dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
  1165. return -ENOMEM;
  1166. }
  1167. kref_init(&hub->kref);
  1168. INIT_LIST_HEAD(&hub->event_list);
  1169. hub->intfdev = &intf->dev;
  1170. hub->hdev = hdev;
  1171. INIT_DELAYED_WORK(&hub->leds, led_work);
  1172. INIT_DELAYED_WORK(&hub->init_work, NULL);
  1173. usb_get_intf(intf);
  1174. usb_set_intfdata (intf, hub);
  1175. intf->needs_remote_wakeup = 1;
  1176. if (hdev->speed == USB_SPEED_HIGH)
  1177. highspeed_hubs++;
  1178. if (hub_configure(hub, endpoint) >= 0)
  1179. return 0;
  1180. hub_disconnect (intf);
  1181. return -ENODEV;
  1182. }
  1183. /* No BKL needed */
  1184. static int
  1185. hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
  1186. {
  1187. struct usb_device *hdev = interface_to_usbdev (intf);
  1188. /* assert ifno == 0 (part of hub spec) */
  1189. switch (code) {
  1190. case USBDEVFS_HUB_PORTINFO: {
  1191. struct usbdevfs_hub_portinfo *info = user_data;
  1192. int i;
  1193. spin_lock_irq(&device_state_lock);
  1194. if (hdev->devnum <= 0)
  1195. info->nports = 0;
  1196. else {
  1197. info->nports = hdev->maxchild;
  1198. for (i = 0; i < info->nports; i++) {
  1199. if (hdev->children[i] == NULL)
  1200. info->port[i] = 0;
  1201. else
  1202. info->port[i] =
  1203. hdev->children[i]->devnum;
  1204. }
  1205. }
  1206. spin_unlock_irq(&device_state_lock);
  1207. return info->nports + 1;
  1208. }
  1209. default:
  1210. return -ENOSYS;
  1211. }
  1212. }
  1213. /*
  1214. * Allow user programs to claim ports on a hub. When a device is attached
  1215. * to one of these "claimed" ports, the program will "own" the device.
  1216. */
  1217. static int find_port_owner(struct usb_device *hdev, unsigned port1,
  1218. void ***ppowner)
  1219. {
  1220. if (hdev->state == USB_STATE_NOTATTACHED)
  1221. return -ENODEV;
  1222. if (port1 == 0 || port1 > hdev->maxchild)
  1223. return -EINVAL;
  1224. /* This assumes that devices not managed by the hub driver
  1225. * will always have maxchild equal to 0.
  1226. */
  1227. *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
  1228. return 0;
  1229. }
  1230. /* In the following three functions, the caller must hold hdev's lock */
  1231. int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
  1232. {
  1233. int rc;
  1234. void **powner;
  1235. rc = find_port_owner(hdev, port1, &powner);
  1236. if (rc)
  1237. return rc;
  1238. if (*powner)
  1239. return -EBUSY;
  1240. *powner = owner;
  1241. return rc;
  1242. }
  1243. int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
  1244. {
  1245. int rc;
  1246. void **powner;
  1247. rc = find_port_owner(hdev, port1, &powner);
  1248. if (rc)
  1249. return rc;
  1250. if (*powner != owner)
  1251. return -ENOENT;
  1252. *powner = NULL;
  1253. return rc;
  1254. }
  1255. void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
  1256. {
  1257. int n;
  1258. void **powner;
  1259. n = find_port_owner(hdev, 1, &powner);
  1260. if (n == 0) {
  1261. for (; n < hdev->maxchild; (++n, ++powner)) {
  1262. if (*powner == owner)
  1263. *powner = NULL;
  1264. }
  1265. }
  1266. }
  1267. /* The caller must hold udev's lock */
  1268. bool usb_device_is_owned(struct usb_device *udev)
  1269. {
  1270. struct usb_hub *hub;
  1271. if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
  1272. return false;
  1273. hub = hdev_to_hub(udev->parent);
  1274. return !!hub->port_owners[udev->portnum - 1];
  1275. }
  1276. static void recursively_mark_NOTATTACHED(struct usb_device *udev)
  1277. {
  1278. int i;
  1279. for (i = 0; i < udev->maxchild; ++i) {
  1280. if (udev->children[i])
  1281. recursively_mark_NOTATTACHED(udev->children[i]);
  1282. }
  1283. if (udev->state == USB_STATE_SUSPENDED)
  1284. udev->active_duration -= jiffies;
  1285. udev->state = USB_STATE_NOTATTACHED;
  1286. }
  1287. /**
  1288. * usb_set_device_state - change a device's current state (usbcore, hcds)
  1289. * @udev: pointer to device whose state should be changed
  1290. * @new_state: new state value to be stored
  1291. *
  1292. * udev->state is _not_ fully protected by the device lock. Although
  1293. * most transitions are made only while holding the lock, the state can
  1294. * can change to USB_STATE_NOTATTACHED at almost any time. This
  1295. * is so that devices can be marked as disconnected as soon as possible,
  1296. * without having to wait for any semaphores to be released. As a result,
  1297. * all changes to any device's state must be protected by the
  1298. * device_state_lock spinlock.
  1299. *
  1300. * Once a device has been added to the device tree, all changes to its state
  1301. * should be made using this routine. The state should _not_ be set directly.
  1302. *
  1303. * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
  1304. * Otherwise udev->state is set to new_state, and if new_state is
  1305. * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
  1306. * to USB_STATE_NOTATTACHED.
  1307. */
  1308. void usb_set_device_state(struct usb_device *udev,
  1309. enum usb_device_state new_state)
  1310. {
  1311. unsigned long flags;
  1312. int wakeup = -1;
  1313. spin_lock_irqsave(&device_state_lock, flags);
  1314. if (udev->state == USB_STATE_NOTATTACHED)
  1315. ; /* do nothing */
  1316. else if (new_state != USB_STATE_NOTATTACHED) {
  1317. /* root hub wakeup capabilities are managed out-of-band
  1318. * and may involve silicon errata ... ignore them here.
  1319. */
  1320. if (udev->parent) {
  1321. if (udev->state == USB_STATE_SUSPENDED
  1322. || new_state == USB_STATE_SUSPENDED)
  1323. ; /* No change to wakeup settings */
  1324. else if (new_state == USB_STATE_CONFIGURED)
  1325. wakeup = udev->actconfig->desc.bmAttributes
  1326. & USB_CONFIG_ATT_WAKEUP;
  1327. else
  1328. wakeup = 0;
  1329. }
  1330. if (udev->state == USB_STATE_SUSPENDED &&
  1331. new_state != USB_STATE_SUSPENDED)
  1332. udev->active_duration -= jiffies;
  1333. else if (new_state == USB_STATE_SUSPENDED &&
  1334. udev->state != USB_STATE_SUSPENDED)
  1335. udev->active_duration += jiffies;
  1336. udev->state = new_state;
  1337. } else
  1338. recursively_mark_NOTATTACHED(udev);
  1339. spin_unlock_irqrestore(&device_state_lock, flags);
  1340. if (wakeup >= 0)
  1341. device_set_wakeup_capable(&udev->dev, wakeup);
  1342. }
  1343. EXPORT_SYMBOL_GPL(usb_set_device_state);
  1344. /*
  1345. * Choose a device number.
  1346. *
  1347. * Device numbers are used as filenames in usbfs. On USB-1.1 and
  1348. * USB-2.0 buses they are also used as device addresses, however on
  1349. * USB-3.0 buses the address is assigned by the controller hardware
  1350. * and it usually is not the same as the device number.
  1351. *
  1352. * WUSB devices are simple: they have no hubs behind, so the mapping
  1353. * device <-> virtual port number becomes 1:1. Why? to simplify the
  1354. * life of the device connection logic in
  1355. * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
  1356. * handshake we need to assign a temporary address in the unauthorized
  1357. * space. For simplicity we use the first virtual port number found to
  1358. * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
  1359. * and that becomes it's address [X < 128] or its unauthorized address
  1360. * [X | 0x80].
  1361. *
  1362. * We add 1 as an offset to the one-based USB-stack port number
  1363. * (zero-based wusb virtual port index) for two reasons: (a) dev addr
  1364. * 0 is reserved by USB for default address; (b) Linux's USB stack
  1365. * uses always #1 for the root hub of the controller. So USB stack's
  1366. * port #1, which is wusb virtual-port #0 has address #2.
  1367. *
  1368. * Devices connected under xHCI are not as simple. The host controller
  1369. * supports virtualization, so the hardware assigns device addresses and
  1370. * the HCD must setup data structures before issuing a set address
  1371. * command to the hardware.
  1372. */
  1373. static void choose_devnum(struct usb_device *udev)
  1374. {
  1375. int devnum;
  1376. struct usb_bus *bus = udev->bus;
  1377. /* If khubd ever becomes multithreaded, this will need a lock */
  1378. if (udev->wusb) {
  1379. devnum = udev->portnum + 1;
  1380. BUG_ON(test_bit(devnum, bus->devmap.devicemap));
  1381. } else {
  1382. /* Try to allocate the next devnum beginning at
  1383. * bus->devnum_next. */
  1384. devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
  1385. bus->devnum_next);
  1386. if (devnum >= 128)
  1387. devnum = find_next_zero_bit(bus->devmap.devicemap,
  1388. 128, 1);
  1389. bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
  1390. }
  1391. if (devnum < 128) {
  1392. set_bit(devnum, bus->devmap.devicemap);
  1393. udev->devnum = devnum;
  1394. }
  1395. }
  1396. static void release_devnum(struct usb_device *udev)
  1397. {
  1398. if (udev->devnum > 0) {
  1399. clear_bit(udev->devnum, udev->bus->devmap.devicemap);
  1400. udev->devnum = -1;
  1401. }
  1402. }
  1403. static void update_devnum(struct usb_device *udev, int devnum)
  1404. {
  1405. /* The address for a WUSB device is managed by wusbcore. */
  1406. if (!udev->wusb)
  1407. udev->devnum = devnum;
  1408. }
  1409. static void hub_free_dev(struct usb_device *udev)
  1410. {
  1411. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1412. /* Root hubs aren't real devices, so don't free HCD resources */
  1413. if (hcd->driver->free_dev && udev->parent)
  1414. hcd->driver->free_dev(hcd, udev);
  1415. }
  1416. /**
  1417. * usb_disconnect - disconnect a device (usbcore-internal)
  1418. * @pdev: pointer to device being disconnected
  1419. * Context: !in_interrupt ()
  1420. *
  1421. * Something got disconnected. Get rid of it and all of its children.
  1422. *
  1423. * If *pdev is a normal device then the parent hub must already be locked.
  1424. * If *pdev is a root hub then this routine will acquire the
  1425. * usb_bus_list_lock on behalf of the caller.
  1426. *
  1427. * Only hub drivers (including virtual root hub drivers for host
  1428. * controllers) should ever call this.
  1429. *
  1430. * This call is synchronous, and may not be used in an interrupt context.
  1431. */
  1432. void usb_disconnect(struct usb_device **pdev)
  1433. {
  1434. struct usb_device *udev = *pdev;
  1435. int i;
  1436. if (!udev) {
  1437. pr_debug ("%s nodev\n", __func__);
  1438. return;
  1439. }
  1440. /* mark the device as inactive, so any further urb submissions for
  1441. * this device (and any of its children) will fail immediately.
  1442. * this quiesces everyting except pending urbs.
  1443. */
  1444. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  1445. dev_info(&udev->dev, "USB disconnect, device number %d\n",
  1446. udev->devnum);
  1447. usb_lock_device(udev);
  1448. /* Free up all the children before we remove this device */
  1449. for (i = 0; i < USB_MAXCHILDREN; i++) {
  1450. if (udev->children[i])
  1451. usb_disconnect(&udev->children[i]);
  1452. }
  1453. /* deallocate hcd/hardware state ... nuking all pending urbs and
  1454. * cleaning up all state associated with the current configuration
  1455. * so that the hardware is now fully quiesced.
  1456. */
  1457. dev_dbg (&udev->dev, "unregistering device\n");
  1458. usb_disable_device(udev, 0);
  1459. usb_hcd_synchronize_unlinks(udev);
  1460. usb_remove_ep_devs(&udev->ep0);
  1461. usb_unlock_device(udev);
  1462. /* Unregister the device. The device driver is responsible
  1463. * for de-configuring the device and invoking the remove-device
  1464. * notifier chain (used by usbfs and possibly others).
  1465. */
  1466. device_del(&udev->dev);
  1467. /* Free the device number and delete the parent's children[]
  1468. * (or root_hub) pointer.
  1469. */
  1470. release_devnum(udev);
  1471. /* Avoid races with recursively_mark_NOTATTACHED() */
  1472. spin_lock_irq(&device_state_lock);
  1473. *pdev = NULL;
  1474. spin_unlock_irq(&device_state_lock);
  1475. hub_free_dev(udev);
  1476. put_device(&udev->dev);
  1477. }
  1478. #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
  1479. static void show_string(struct usb_device *udev, char *id, char *string)
  1480. {
  1481. if (!string)
  1482. return;
  1483. dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
  1484. }
  1485. static void announce_device(struct usb_device *udev)
  1486. {
  1487. dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
  1488. le16_to_cpu(udev->descriptor.idVendor),
  1489. le16_to_cpu(udev->descriptor.idProduct));
  1490. dev_info(&udev->dev,
  1491. "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
  1492. udev->descriptor.iManufacturer,
  1493. udev->descriptor.iProduct,
  1494. udev->descriptor.iSerialNumber);
  1495. show_string(udev, "Product", udev->product);
  1496. show_string(udev, "Manufacturer", udev->manufacturer);
  1497. show_string(udev, "SerialNumber", udev->serial);
  1498. }
  1499. #else
  1500. static inline void announce_device(struct usb_device *udev) { }
  1501. #endif
  1502. #ifdef CONFIG_USB_OTG
  1503. #include "otg_whitelist.h"
  1504. #endif
  1505. /**
  1506. * usb_enumerate_device_otg - FIXME (usbcore-internal)
  1507. * @udev: newly addressed device (in ADDRESS state)
  1508. *
  1509. * Finish enumeration for On-The-Go devices
  1510. */
  1511. static int usb_enumerate_device_otg(struct usb_device *udev)
  1512. {
  1513. int err = 0;
  1514. #ifdef CONFIG_USB_OTG
  1515. /*
  1516. * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
  1517. * to wake us after we've powered off VBUS; and HNP, switching roles
  1518. * "host" to "peripheral". The OTG descriptor helps figure this out.
  1519. */
  1520. if (!udev->bus->is_b_host
  1521. && udev->config
  1522. && udev->parent == udev->bus->root_hub) {
  1523. struct usb_otg_descriptor *desc = NULL;
  1524. struct usb_bus *bus = udev->bus;
  1525. /* descriptor may appear anywhere in config */
  1526. if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
  1527. le16_to_cpu(udev->config[0].desc.wTotalLength),
  1528. USB_DT_OTG, (void **) &desc) == 0) {
  1529. if (desc->bmAttributes & USB_OTG_HNP) {
  1530. unsigned port1 = udev->portnum;
  1531. dev_info(&udev->dev,
  1532. "Dual-Role OTG device on %sHNP port\n",
  1533. (port1 == bus->otg_port)
  1534. ? "" : "non-");
  1535. /* enable HNP before suspend, it's simpler */
  1536. if (port1 == bus->otg_port)
  1537. bus->b_hnp_enable = 1;
  1538. err = usb_control_msg(udev,
  1539. usb_sndctrlpipe(udev, 0),
  1540. USB_REQ_SET_FEATURE, 0,
  1541. bus->b_hnp_enable
  1542. ? USB_DEVICE_B_HNP_ENABLE
  1543. : USB_DEVICE_A_ALT_HNP_SUPPORT,
  1544. 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  1545. if (err < 0) {
  1546. /* OTG MESSAGE: report errors here,
  1547. * customize to match your product.
  1548. */
  1549. dev_info(&udev->dev,
  1550. "can't set HNP mode: %d\n",
  1551. err);
  1552. bus->b_hnp_enable = 0;
  1553. }
  1554. }
  1555. }
  1556. }
  1557. if (!is_targeted(udev)) {
  1558. /* Maybe it can talk to us, though we can't talk to it.
  1559. * (Includes HNP test device.)
  1560. */
  1561. if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
  1562. err = usb_port_suspend(udev, PMSG_SUSPEND);
  1563. if (err < 0)
  1564. dev_dbg(&udev->dev, "HNP fail, %d\n", err);
  1565. }
  1566. err = -ENOTSUPP;
  1567. goto fail;
  1568. }
  1569. fail:
  1570. #endif
  1571. return err;
  1572. }
  1573. /**
  1574. * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
  1575. * @udev: newly addressed device (in ADDRESS state)
  1576. *
  1577. * This is only called by usb_new_device() and usb_authorize_device()
  1578. * and FIXME -- all comments that apply to them apply here wrt to
  1579. * environment.
  1580. *
  1581. * If the device is WUSB and not authorized, we don't attempt to read
  1582. * the string descriptors, as they will be errored out by the device
  1583. * until it has been authorized.
  1584. */
  1585. static int usb_enumerate_device(struct usb_device *udev)
  1586. {
  1587. int err;
  1588. if (udev->config == NULL) {
  1589. err = usb_get_configuration(udev);
  1590. if (err < 0) {
  1591. dev_err(&udev->dev, "can't read configurations, error %d\n",
  1592. err);
  1593. goto fail;
  1594. }
  1595. }
  1596. if (udev->wusb == 1 && udev->authorized == 0) {
  1597. udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1598. udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1599. udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1600. }
  1601. else {
  1602. /* read the standard strings and cache them if present */
  1603. udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
  1604. udev->manufacturer = usb_cache_string(udev,
  1605. udev->descriptor.iManufacturer);
  1606. udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
  1607. }
  1608. err = usb_enumerate_device_otg(udev);
  1609. fail:
  1610. return err;
  1611. }
  1612. /**
  1613. * usb_new_device - perform initial device setup (usbcore-internal)
  1614. * @udev: newly addressed device (in ADDRESS state)
  1615. *
  1616. * This is called with devices which have been detected but not fully
  1617. * enumerated. The device descriptor is available, but not descriptors
  1618. * for any device configuration. The caller must have locked either
  1619. * the parent hub (if udev is a normal device) or else the
  1620. * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
  1621. * udev has already been installed, but udev is not yet visible through
  1622. * sysfs or other filesystem code.
  1623. *
  1624. * It will return if the device is configured properly or not. Zero if
  1625. * the interface was registered with the driver core; else a negative
  1626. * errno value.
  1627. *
  1628. * This call is synchronous, and may not be used in an interrupt context.
  1629. *
  1630. * Only the hub driver or root-hub registrar should ever call this.
  1631. */
  1632. int usb_new_device(struct usb_device *udev)
  1633. {
  1634. int err;
  1635. if (udev->parent) {
  1636. /* Initialize non-root-hub device wakeup to disabled;
  1637. * device (un)configuration controls wakeup capable
  1638. * sysfs power/wakeup controls wakeup enabled/disabled
  1639. */
  1640. device_init_wakeup(&udev->dev, 0);
  1641. }
  1642. /* Tell the runtime-PM framework the device is active */
  1643. pm_runtime_set_active(&udev->dev);
  1644. pm_runtime_get_noresume(&udev->dev);
  1645. pm_runtime_use_autosuspend(&udev->dev);
  1646. pm_runtime_enable(&udev->dev);
  1647. /* By default, forbid autosuspend for all devices. It will be
  1648. * allowed for hubs during binding.
  1649. */
  1650. usb_disable_autosuspend(udev);
  1651. err = usb_enumerate_device(udev); /* Read descriptors */
  1652. if (err < 0)
  1653. goto fail;
  1654. dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
  1655. udev->devnum, udev->bus->busnum,
  1656. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  1657. /* export the usbdev device-node for libusb */
  1658. udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
  1659. (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
  1660. /* Tell the world! */
  1661. announce_device(udev);
  1662. device_enable_async_suspend(&udev->dev);
  1663. /* Register the device. The device driver is responsible
  1664. * for configuring the device and invoking the add-device
  1665. * notifier chain (used by usbfs and possibly others).
  1666. */
  1667. err = device_add(&udev->dev);
  1668. if (err) {
  1669. dev_err(&udev->dev, "can't device_add, error %d\n", err);
  1670. goto fail;
  1671. }
  1672. (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
  1673. usb_mark_last_busy(udev);
  1674. pm_runtime_put_sync_autosuspend(&udev->dev);
  1675. return err;
  1676. fail:
  1677. usb_set_device_state(udev, USB_STATE_NOTATTACHED);
  1678. pm_runtime_disable(&udev->dev);
  1679. pm_runtime_set_suspended(&udev->dev);
  1680. return err;
  1681. }
  1682. /**
  1683. * usb_deauthorize_device - deauthorize a device (usbcore-internal)
  1684. * @usb_dev: USB device
  1685. *
  1686. * Move the USB device to a very basic state where interfaces are disabled
  1687. * and the device is in fact unconfigured and unusable.
  1688. *
  1689. * We share a lock (that we have) with device_del(), so we need to
  1690. * defer its call.
  1691. */
  1692. int usb_deauthorize_device(struct usb_device *usb_dev)
  1693. {
  1694. usb_lock_device(usb_dev);
  1695. if (usb_dev->authorized == 0)
  1696. goto out_unauthorized;
  1697. usb_dev->authorized = 0;
  1698. usb_set_configuration(usb_dev, -1);
  1699. kfree(usb_dev->product);
  1700. usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1701. kfree(usb_dev->manufacturer);
  1702. usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1703. kfree(usb_dev->serial);
  1704. usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
  1705. usb_destroy_configuration(usb_dev);
  1706. usb_dev->descriptor.bNumConfigurations = 0;
  1707. out_unauthorized:
  1708. usb_unlock_device(usb_dev);
  1709. return 0;
  1710. }
  1711. int usb_authorize_device(struct usb_device *usb_dev)
  1712. {
  1713. int result = 0, c;
  1714. usb_lock_device(usb_dev);
  1715. if (usb_dev->authorized == 1)
  1716. goto out_authorized;
  1717. result = usb_autoresume_device(usb_dev);
  1718. if (result < 0) {
  1719. dev_err(&usb_dev->dev,
  1720. "can't autoresume for authorization: %d\n", result);
  1721. goto error_autoresume;
  1722. }
  1723. result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
  1724. if (result < 0) {
  1725. dev_err(&usb_dev->dev, "can't re-read device descriptor for "
  1726. "authorization: %d\n", result);
  1727. goto error_device_descriptor;
  1728. }
  1729. kfree(usb_dev->product);
  1730. usb_dev->product = NULL;
  1731. kfree(usb_dev->manufacturer);
  1732. usb_dev->manufacturer = NULL;
  1733. kfree(usb_dev->serial);
  1734. usb_dev->serial = NULL;
  1735. usb_dev->authorized = 1;
  1736. result = usb_enumerate_device(usb_dev);
  1737. if (result < 0)
  1738. goto error_enumerate;
  1739. /* Choose and set the configuration. This registers the interfaces
  1740. * with the driver core and lets interface drivers bind to them.
  1741. */
  1742. c = usb_choose_configuration(usb_dev);
  1743. if (c >= 0) {
  1744. result = usb_set_configuration(usb_dev, c);
  1745. if (result) {
  1746. dev_err(&usb_dev->dev,
  1747. "can't set config #%d, error %d\n", c, result);
  1748. /* This need not be fatal. The user can try to
  1749. * set other configurations. */
  1750. }
  1751. }
  1752. dev_info(&usb_dev->dev, "authorized to connect\n");
  1753. error_enumerate:
  1754. error_device_descriptor:
  1755. usb_autosuspend_device(usb_dev);
  1756. error_autoresume:
  1757. out_authorized:
  1758. usb_unlock_device(usb_dev); // complements locktree
  1759. return result;
  1760. }
  1761. /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
  1762. static unsigned hub_is_wusb(struct usb_hub *hub)
  1763. {
  1764. struct usb_hcd *hcd;
  1765. if (hub->hdev->parent != NULL) /* not a root hub? */
  1766. return 0;
  1767. hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
  1768. return hcd->wireless;
  1769. }
  1770. #define PORT_RESET_TRIES 5
  1771. #define SET_ADDRESS_TRIES 2
  1772. #define GET_DESCRIPTOR_TRIES 2
  1773. #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
  1774. #define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first)
  1775. #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
  1776. #define HUB_SHORT_RESET_TIME 10
  1777. #define HUB_LONG_RESET_TIME 200
  1778. #define HUB_RESET_TIMEOUT 500
  1779. static int hub_port_wait_reset(struct usb_hub *hub, int port1,
  1780. struct usb_device *udev, unsigned int delay)
  1781. {
  1782. int delay_time, ret;
  1783. u16 portstatus;
  1784. u16 portchange;
  1785. for (delay_time = 0;
  1786. delay_time < HUB_RESET_TIMEOUT;
  1787. delay_time += delay) {
  1788. /* wait to give the device a chance to reset */
  1789. msleep(delay);
  1790. /* read and decode port status */
  1791. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  1792. if (ret < 0)
  1793. return ret;
  1794. /* Device went away? */
  1795. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  1796. return -ENOTCONN;
  1797. /* bomb out completely if the connection bounced */
  1798. if ((portchange & USB_PORT_STAT_C_CONNECTION))
  1799. return -ENOTCONN;
  1800. /* if we`ve finished resetting, then break out of the loop */
  1801. if (!(portstatus & USB_PORT_STAT_RESET) &&
  1802. (portstatus & USB_PORT_STAT_ENABLE)) {
  1803. if (hub_is_wusb(hub))
  1804. udev->speed = USB_SPEED_WIRELESS;
  1805. else if (hub_is_superspeed(hub->hdev))
  1806. udev->speed = USB_SPEED_SUPER;
  1807. else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  1808. udev->speed = USB_SPEED_HIGH;
  1809. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  1810. udev->speed = USB_SPEED_LOW;
  1811. else
  1812. udev->speed = USB_SPEED_FULL;
  1813. return 0;
  1814. }
  1815. /* switch to the long delay after two short delay failures */
  1816. if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
  1817. delay = HUB_LONG_RESET_TIME;
  1818. dev_dbg (hub->intfdev,
  1819. "port %d not reset yet, waiting %dms\n",
  1820. port1, delay);
  1821. }
  1822. return -EBUSY;
  1823. }
  1824. static int hub_port_reset(struct usb_hub *hub, int port1,
  1825. struct usb_device *udev, unsigned int delay)
  1826. {
  1827. int i, status;
  1828. struct usb_hcd *hcd;
  1829. hcd = bus_to_hcd(udev->bus);
  1830. /* Block EHCI CF initialization during the port reset.
  1831. * Some companion controllers don't like it when they mix.
  1832. */
  1833. down_read(&ehci_cf_port_reset_rwsem);
  1834. /* Reset the port */
  1835. for (i = 0; i < PORT_RESET_TRIES; i++) {
  1836. status = set_port_feature(hub->hdev,
  1837. port1, USB_PORT_FEAT_RESET);
  1838. if (status)
  1839. dev_err(hub->intfdev,
  1840. "cannot reset port %d (err = %d)\n",
  1841. port1, status);
  1842. else {
  1843. status = hub_port_wait_reset(hub, port1, udev, delay);
  1844. if (status && status != -ENOTCONN)
  1845. dev_dbg(hub->intfdev,
  1846. "port_wait_reset: err = %d\n",
  1847. status);
  1848. }
  1849. /* return on disconnect or reset */
  1850. switch (status) {
  1851. case 0:
  1852. /* TRSTRCY = 10 ms; plus some extra */
  1853. msleep(10 + 40);
  1854. update_devnum(udev, 0);
  1855. if (hcd->driver->reset_device) {
  1856. status = hcd->driver->reset_device(hcd, udev);
  1857. if (status < 0) {
  1858. dev_err(&udev->dev, "Cannot reset "
  1859. "HCD device state\n");
  1860. break;
  1861. }
  1862. }
  1863. /* FALL THROUGH */
  1864. case -ENOTCONN:
  1865. case -ENODEV:
  1866. clear_port_feature(hub->hdev,
  1867. port1, USB_PORT_FEAT_C_RESET);
  1868. /* FIXME need disconnect() for NOTATTACHED device */
  1869. usb_set_device_state(udev, status
  1870. ? USB_STATE_NOTATTACHED
  1871. : USB_STATE_DEFAULT);
  1872. goto done;
  1873. }
  1874. dev_dbg (hub->intfdev,
  1875. "port %d not enabled, trying reset again...\n",
  1876. port1);
  1877. delay = HUB_LONG_RESET_TIME;
  1878. }
  1879. dev_err (hub->intfdev,
  1880. "Cannot enable port %i. Maybe the USB cable is bad?\n",
  1881. port1);
  1882. done:
  1883. up_read(&ehci_cf_port_reset_rwsem);
  1884. return status;
  1885. }
  1886. #ifdef CONFIG_PM
  1887. #define MASK_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
  1888. USB_PORT_STAT_SUSPEND)
  1889. #define WANT_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
  1890. /* Determine whether the device on a port is ready for a normal resume,
  1891. * is ready for a reset-resume, or should be disconnected.
  1892. */
  1893. static int check_port_resume_type(struct usb_device *udev,
  1894. struct usb_hub *hub, int port1,
  1895. int status, unsigned portchange, unsigned portstatus)
  1896. {
  1897. /* Is the device still present? */
  1898. if (status || (portstatus & MASK_BITS) != WANT_BITS) {
  1899. if (status >= 0)
  1900. status = -ENODEV;
  1901. }
  1902. /* Can't do a normal resume if the port isn't enabled,
  1903. * so try a reset-resume instead.
  1904. */
  1905. else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
  1906. if (udev->persist_enabled)
  1907. udev->reset_resume = 1;
  1908. else
  1909. status = -ENODEV;
  1910. }
  1911. if (status) {
  1912. dev_dbg(hub->intfdev,
  1913. "port %d status %04x.%04x after resume, %d\n",
  1914. port1, portchange, portstatus, status);
  1915. } else if (udev->reset_resume) {
  1916. /* Late port handoff can set status-change bits */
  1917. if (portchange & USB_PORT_STAT_C_CONNECTION)
  1918. clear_port_feature(hub->hdev, port1,
  1919. USB_PORT_FEAT_C_CONNECTION);
  1920. if (portchange & USB_PORT_STAT_C_ENABLE)
  1921. clear_port_feature(hub->hdev, port1,
  1922. USB_PORT_FEAT_C_ENABLE);
  1923. }
  1924. return status;
  1925. }
  1926. #ifdef CONFIG_USB_SUSPEND
  1927. /*
  1928. * usb_port_suspend - suspend a usb device's upstream port
  1929. * @udev: device that's no longer in active use, not a root hub
  1930. * Context: must be able to sleep; device not locked; pm locks held
  1931. *
  1932. * Suspends a USB device that isn't in active use, conserving power.
  1933. * Devices may wake out of a suspend, if anything important happens,
  1934. * using the remote wakeup mechanism. They may also be taken out of
  1935. * suspend by the host, using usb_port_resume(). It's also routine
  1936. * to disconnect devices while they are suspended.
  1937. *
  1938. * This only affects the USB hardware for a device; its interfaces
  1939. * (and, for hubs, child devices) must already have been suspended.
  1940. *
  1941. * Selective port suspend reduces power; most suspended devices draw
  1942. * less than 500 uA. It's also used in OTG, along with remote wakeup.
  1943. * All devices below the suspended port are also suspended.
  1944. *
  1945. * Devices leave suspend state when the host wakes them up. Some devices
  1946. * also support "remote wakeup", where the device can activate the USB
  1947. * tree above them to deliver data, such as a keypress or packet. In
  1948. * some cases, this wakes the USB host.
  1949. *
  1950. * Suspending OTG devices may trigger HNP, if that's been enabled
  1951. * between a pair of dual-role devices. That will change roles, such
  1952. * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
  1953. *
  1954. * Devices on USB hub ports have only one "suspend" state, corresponding
  1955. * to ACPI D2, "may cause the device to lose some context".
  1956. * State transitions include:
  1957. *
  1958. * - suspend, resume ... when the VBUS power link stays live
  1959. * - suspend, disconnect ... VBUS lost
  1960. *
  1961. * Once VBUS drop breaks the circuit, the port it's using has to go through
  1962. * normal re-enumeration procedures, starting with enabling VBUS power.
  1963. * Other than re-initializing the hub (plug/unplug, except for root hubs),
  1964. * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
  1965. * timer, no SRP, no requests through sysfs.
  1966. *
  1967. * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
  1968. * the root hub for their bus goes into global suspend ... so we don't
  1969. * (falsely) update the device power state to say it suspended.
  1970. *
  1971. * Returns 0 on success, else negative errno.
  1972. */
  1973. int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
  1974. {
  1975. struct usb_hub *hub = hdev_to_hub(udev->parent);
  1976. int port1 = udev->portnum;
  1977. int status;
  1978. // dev_dbg(hub->intfdev, "suspend port %d\n", port1);
  1979. /* enable remote wakeup when appropriate; this lets the device
  1980. * wake up the upstream hub (including maybe the root hub).
  1981. *
  1982. * NOTE: OTG devices may issue remote wakeup (or SRP) even when
  1983. * we don't explicitly enable it here.
  1984. */
  1985. if (udev->do_remote_wakeup) {
  1986. status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  1987. USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
  1988. USB_DEVICE_REMOTE_WAKEUP, 0,
  1989. NULL, 0,
  1990. USB_CTRL_SET_TIMEOUT);
  1991. if (status) {
  1992. dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
  1993. status);
  1994. /* bail if autosuspend is requested */
  1995. if (msg.event & PM_EVENT_AUTO)
  1996. return status;
  1997. }
  1998. }
  1999. /* see 7.1.7.6 */
  2000. status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
  2001. if (status) {
  2002. dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
  2003. port1, status);
  2004. /* paranoia: "should not happen" */
  2005. if (udev->do_remote_wakeup)
  2006. (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2007. USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
  2008. USB_DEVICE_REMOTE_WAKEUP, 0,
  2009. NULL, 0,
  2010. USB_CTRL_SET_TIMEOUT);
  2011. } else {
  2012. /* device has up to 10 msec to fully suspend */
  2013. dev_dbg(&udev->dev, "usb %ssuspend\n",
  2014. (msg.event & PM_EVENT_AUTO ? "auto-" : ""));
  2015. usb_set_device_state(udev, USB_STATE_SUSPENDED);
  2016. msleep(10);
  2017. }
  2018. usb_mark_last_busy(hub->hdev);
  2019. return status;
  2020. }
  2021. /*
  2022. * If the USB "suspend" state is in use (rather than "global suspend"),
  2023. * many devices will be individually taken out of suspend state using
  2024. * special "resume" signaling. This routine kicks in shortly after
  2025. * hardware resume signaling is finished, either because of selective
  2026. * resume (by host) or remote wakeup (by device) ... now see what changed
  2027. * in the tree that's rooted at this device.
  2028. *
  2029. * If @udev->reset_resume is set then the device is reset before the
  2030. * status check is done.
  2031. */
  2032. static int finish_port_resume(struct usb_device *udev)
  2033. {
  2034. int status = 0;
  2035. u16 devstatus;
  2036. /* caller owns the udev device lock */
  2037. dev_dbg(&udev->dev, "%s\n",
  2038. udev->reset_resume ? "finish reset-resume" : "finish resume");
  2039. /* usb ch9 identifies four variants of SUSPENDED, based on what
  2040. * state the device resumes to. Linux currently won't see the
  2041. * first two on the host side; they'd be inside hub_port_init()
  2042. * during many timeouts, but khubd can't suspend until later.
  2043. */
  2044. usb_set_device_state(udev, udev->actconfig
  2045. ? USB_STATE_CONFIGURED
  2046. : USB_STATE_ADDRESS);
  2047. /* 10.5.4.5 says not to reset a suspended port if the attached
  2048. * device is enabled for remote wakeup. Hence the reset
  2049. * operation is carried out here, after the port has been
  2050. * resumed.
  2051. */
  2052. if (udev->reset_resume)
  2053. retry_reset_resume:
  2054. status = usb_reset_and_verify_device(udev);
  2055. /* 10.5.4.5 says be sure devices in the tree are still there.
  2056. * For now let's assume the device didn't go crazy on resume,
  2057. * and device drivers will know about any resume quirks.
  2058. */
  2059. if (status == 0) {
  2060. devstatus = 0;
  2061. status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
  2062. if (status >= 0)
  2063. status = (status > 0 ? 0 : -ENODEV);
  2064. /* If a normal resume failed, try doing a reset-resume */
  2065. if (status && !udev->reset_resume && udev->persist_enabled) {
  2066. dev_dbg(&udev->dev, "retry with reset-resume\n");
  2067. udev->reset_resume = 1;
  2068. goto retry_reset_resume;
  2069. }
  2070. }
  2071. if (status) {
  2072. dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
  2073. status);
  2074. } else if (udev->actconfig) {
  2075. le16_to_cpus(&devstatus);
  2076. if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
  2077. status = usb_control_msg(udev,
  2078. usb_sndctrlpipe(udev, 0),
  2079. USB_REQ_CLEAR_FEATURE,
  2080. USB_RECIP_DEVICE,
  2081. USB_DEVICE_REMOTE_WAKEUP, 0,
  2082. NULL, 0,
  2083. USB_CTRL_SET_TIMEOUT);
  2084. if (status)
  2085. dev_dbg(&udev->dev,
  2086. "disable remote wakeup, status %d\n",
  2087. status);
  2088. }
  2089. status = 0;
  2090. }
  2091. return status;
  2092. }
  2093. /*
  2094. * usb_port_resume - re-activate a suspended usb device's upstream port
  2095. * @udev: device to re-activate, not a root hub
  2096. * Context: must be able to sleep; device not locked; pm locks held
  2097. *
  2098. * This will re-activate the suspended device, increasing power usage
  2099. * while letting drivers communicate again with its endpoints.
  2100. * USB resume explicitly guarantees that the power session between
  2101. * the host and the device is the same as it was when the device
  2102. * suspended.
  2103. *
  2104. * If @udev->reset_resume is set then this routine won't check that the
  2105. * port is still enabled. Furthermore, finish_port_resume() above will
  2106. * reset @udev. The end result is that a broken power session can be
  2107. * recovered and @udev will appear to persist across a loss of VBUS power.
  2108. *
  2109. * For example, if a host controller doesn't maintain VBUS suspend current
  2110. * during a system sleep or is reset when the system wakes up, all the USB
  2111. * power sessions below it will be broken. This is especially troublesome
  2112. * for mass-storage devices containing mounted filesystems, since the
  2113. * device will appear to have disconnected and all the memory mappings
  2114. * to it will be lost. Using the USB_PERSIST facility, the device can be
  2115. * made to appear as if it had not disconnected.
  2116. *
  2117. * This facility can be dangerous. Although usb_reset_and_verify_device() makes
  2118. * every effort to insure that the same device is present after the
  2119. * reset as before, it cannot provide a 100% guarantee. Furthermore it's
  2120. * quite possible for a device to remain unaltered but its media to be
  2121. * changed. If the user replaces a flash memory card while the system is
  2122. * asleep, he will have only himself to blame when the filesystem on the
  2123. * new card is corrupted and the system crashes.
  2124. *
  2125. * Returns 0 on success, else negative errno.
  2126. */
  2127. int usb_port_resume(struct usb_device *udev, pm_message_t msg)
  2128. {
  2129. struct usb_hub *hub = hdev_to_hub(udev->parent);
  2130. int port1 = udev->portnum;
  2131. int status;
  2132. u16 portchange, portstatus;
  2133. /* Skip the initial Clear-Suspend step for a remote wakeup */
  2134. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2135. if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
  2136. goto SuspendCleared;
  2137. // dev_dbg(hub->intfdev, "resume port %d\n", port1);
  2138. set_bit(port1, hub->busy_bits);
  2139. /* see 7.1.7.7; affects power usage, but not budgeting */
  2140. status = clear_port_feature(hub->hdev,
  2141. port1, USB_PORT_FEAT_SUSPEND);
  2142. if (status) {
  2143. dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
  2144. port1, status);
  2145. } else {
  2146. /* drive resume for at least 20 msec */
  2147. dev_dbg(&udev->dev, "usb %sresume\n",
  2148. (msg.event & PM_EVENT_AUTO ? "auto-" : ""));
  2149. msleep(25);
  2150. /* Virtual root hubs can trigger on GET_PORT_STATUS to
  2151. * stop resume signaling. Then finish the resume
  2152. * sequence.
  2153. */
  2154. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2155. /* TRSMRCY = 10 msec */
  2156. msleep(10);
  2157. }
  2158. SuspendCleared:
  2159. if (status == 0) {
  2160. if (portchange & USB_PORT_STAT_C_SUSPEND)
  2161. clear_port_feature(hub->hdev, port1,
  2162. USB_PORT_FEAT_C_SUSPEND);
  2163. }
  2164. clear_bit(port1, hub->busy_bits);
  2165. status = check_port_resume_type(udev,
  2166. hub, port1, status, portchange, portstatus);
  2167. if (status == 0)
  2168. status = finish_port_resume(udev);
  2169. if (status < 0) {
  2170. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  2171. hub_port_logical_disconnect(hub, port1);
  2172. }
  2173. return status;
  2174. }
  2175. /* caller has locked udev */
  2176. int usb_remote_wakeup(struct usb_device *udev)
  2177. {
  2178. int status = 0;
  2179. if (udev->state == USB_STATE_SUSPENDED) {
  2180. dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
  2181. status = usb_autoresume_device(udev);
  2182. if (status == 0) {
  2183. /* Let the drivers do their thing, then... */
  2184. usb_autosuspend_device(udev);
  2185. }
  2186. }
  2187. return status;
  2188. }
  2189. #else /* CONFIG_USB_SUSPEND */
  2190. /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
  2191. int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
  2192. {
  2193. return 0;
  2194. }
  2195. /* However we may need to do a reset-resume */
  2196. int usb_port_resume(struct usb_device *udev, pm_message_t msg)
  2197. {
  2198. struct usb_hub *hub = hdev_to_hub(udev->parent);
  2199. int port1 = udev->portnum;
  2200. int status;
  2201. u16 portchange, portstatus;
  2202. status = hub_port_status(hub, port1, &portstatus, &portchange);
  2203. status = check_port_resume_type(udev,
  2204. hub, port1, status, portchange, portstatus);
  2205. if (status) {
  2206. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  2207. hub_port_logical_disconnect(hub, port1);
  2208. } else if (udev->reset_resume) {
  2209. dev_dbg(&udev->dev, "reset-resume\n");
  2210. status = usb_reset_and_verify_device(udev);
  2211. }
  2212. return status;
  2213. }
  2214. #endif
  2215. static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
  2216. {
  2217. struct usb_hub *hub = usb_get_intfdata (intf);
  2218. struct usb_device *hdev = hub->hdev;
  2219. unsigned port1;
  2220. /* fail if children aren't already suspended */
  2221. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  2222. struct usb_device *udev;
  2223. udev = hdev->children [port1-1];
  2224. if (udev && udev->can_submit) {
  2225. if (!(msg.event & PM_EVENT_AUTO))
  2226. dev_dbg(&intf->dev, "port %d nyet suspended\n",
  2227. port1);
  2228. return -EBUSY;
  2229. }
  2230. }
  2231. dev_dbg(&intf->dev, "%s\n", __func__);
  2232. /* stop khubd and related activity */
  2233. hub_quiesce(hub, HUB_SUSPEND);
  2234. return 0;
  2235. }
  2236. static int hub_resume(struct usb_interface *intf)
  2237. {
  2238. struct usb_hub *hub = usb_get_intfdata(intf);
  2239. dev_dbg(&intf->dev, "%s\n", __func__);
  2240. hub_activate(hub, HUB_RESUME);
  2241. return 0;
  2242. }
  2243. static int hub_reset_resume(struct usb_interface *intf)
  2244. {
  2245. struct usb_hub *hub = usb_get_intfdata(intf);
  2246. dev_dbg(&intf->dev, "%s\n", __func__);
  2247. hub_activate(hub, HUB_RESET_RESUME);
  2248. return 0;
  2249. }
  2250. /**
  2251. * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
  2252. * @rhdev: struct usb_device for the root hub
  2253. *
  2254. * The USB host controller driver calls this function when its root hub
  2255. * is resumed and Vbus power has been interrupted or the controller
  2256. * has been reset. The routine marks @rhdev as having lost power.
  2257. * When the hub driver is resumed it will take notice and carry out
  2258. * power-session recovery for all the "USB-PERSIST"-enabled child devices;
  2259. * the others will be disconnected.
  2260. */
  2261. void usb_root_hub_lost_power(struct usb_device *rhdev)
  2262. {
  2263. dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
  2264. rhdev->reset_resume = 1;
  2265. }
  2266. EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
  2267. #else /* CONFIG_PM */
  2268. #define hub_suspend NULL
  2269. #define hub_resume NULL
  2270. #define hub_reset_resume NULL
  2271. #endif
  2272. /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
  2273. *
  2274. * Between connect detection and reset signaling there must be a delay
  2275. * of 100ms at least for debounce and power-settling. The corresponding
  2276. * timer shall restart whenever the downstream port detects a disconnect.
  2277. *
  2278. * Apparently there are some bluetooth and irda-dongles and a number of
  2279. * low-speed devices for which this debounce period may last over a second.
  2280. * Not covered by the spec - but easy to deal with.
  2281. *
  2282. * This implementation uses a 1500ms total debounce timeout; if the
  2283. * connection isn't stable by then it returns -ETIMEDOUT. It checks
  2284. * every 25ms for transient disconnects. When the port status has been
  2285. * unchanged for 100ms it returns the port status.
  2286. */
  2287. static int hub_port_debounce(struct usb_hub *hub, int port1)
  2288. {
  2289. int ret;
  2290. int total_time, stable_time = 0;
  2291. u16 portchange, portstatus;
  2292. unsigned connection = 0xffff;
  2293. for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
  2294. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  2295. if (ret < 0)
  2296. return ret;
  2297. if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
  2298. (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
  2299. stable_time += HUB_DEBOUNCE_STEP;
  2300. if (stable_time >= HUB_DEBOUNCE_STABLE)
  2301. break;
  2302. } else {
  2303. stable_time = 0;
  2304. connection = portstatus & USB_PORT_STAT_CONNECTION;
  2305. }
  2306. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  2307. clear_port_feature(hub->hdev, port1,
  2308. USB_PORT_FEAT_C_CONNECTION);
  2309. }
  2310. if (total_time >= HUB_DEBOUNCE_TIMEOUT)
  2311. break;
  2312. msleep(HUB_DEBOUNCE_STEP);
  2313. }
  2314. dev_dbg (hub->intfdev,
  2315. "debounce: port %d: total %dms stable %dms status 0x%x\n",
  2316. port1, total_time, stable_time, portstatus);
  2317. if (stable_time < HUB_DEBOUNCE_STABLE)
  2318. return -ETIMEDOUT;
  2319. return portstatus;
  2320. }
  2321. void usb_ep0_reinit(struct usb_device *udev)
  2322. {
  2323. usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
  2324. usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
  2325. usb_enable_endpoint(udev, &udev->ep0, true);
  2326. }
  2327. EXPORT_SYMBOL_GPL(usb_ep0_reinit);
  2328. #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
  2329. #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
  2330. static int hub_set_address(struct usb_device *udev, int devnum)
  2331. {
  2332. int retval;
  2333. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  2334. /*
  2335. * The host controller will choose the device address,
  2336. * instead of the core having chosen it earlier
  2337. */
  2338. if (!hcd->driver->address_device && devnum <= 1)
  2339. return -EINVAL;
  2340. if (udev->state == USB_STATE_ADDRESS)
  2341. return 0;
  2342. if (udev->state != USB_STATE_DEFAULT)
  2343. return -EINVAL;
  2344. if (hcd->driver->address_device)
  2345. retval = hcd->driver->address_device(hcd, udev);
  2346. else
  2347. retval = usb_control_msg(udev, usb_sndaddr0pipe(),
  2348. USB_REQ_SET_ADDRESS, 0, devnum, 0,
  2349. NULL, 0, USB_CTRL_SET_TIMEOUT);
  2350. if (retval == 0) {
  2351. update_devnum(udev, devnum);
  2352. /* Device now using proper address. */
  2353. usb_set_device_state(udev, USB_STATE_ADDRESS);
  2354. usb_ep0_reinit(udev);
  2355. }
  2356. return retval;
  2357. }
  2358. /* Reset device, (re)assign address, get device descriptor.
  2359. * Device connection must be stable, no more debouncing needed.
  2360. * Returns device in USB_STATE_ADDRESS, except on error.
  2361. *
  2362. * If this is called for an already-existing device (as part of
  2363. * usb_reset_and_verify_device), the caller must own the device lock. For a
  2364. * newly detected device that is not accessible through any global
  2365. * pointers, it's not necessary to lock the device.
  2366. */
  2367. static int
  2368. hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
  2369. int retry_counter)
  2370. {
  2371. static DEFINE_MUTEX(usb_address0_mutex);
  2372. struct usb_device *hdev = hub->hdev;
  2373. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  2374. int i, j, retval;
  2375. unsigned delay = HUB_SHORT_RESET_TIME;
  2376. enum usb_device_speed oldspeed = udev->speed;
  2377. char *speed, *type;
  2378. int devnum = udev->devnum;
  2379. /* root hub ports have a slightly longer reset period
  2380. * (from USB 2.0 spec, section 7.1.7.5)
  2381. */
  2382. if (!hdev->parent) {
  2383. delay = HUB_ROOT_RESET_TIME;
  2384. if (port1 == hdev->bus->otg_port)
  2385. hdev->bus->b_hnp_enable = 0;
  2386. }
  2387. /* Some low speed devices have problems with the quick delay, so */
  2388. /* be a bit pessimistic with those devices. RHbug #23670 */
  2389. if (oldspeed == USB_SPEED_LOW)
  2390. delay = HUB_LONG_RESET_TIME;
  2391. mutex_lock(&usb_address0_mutex);
  2392. /* Reset the device; full speed may morph to high speed */
  2393. /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
  2394. retval = hub_port_reset(hub, port1, udev, delay);
  2395. if (retval < 0) /* error or disconnect */
  2396. goto fail;
  2397. /* success, speed is known */
  2398. retval = -ENODEV;
  2399. if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
  2400. dev_dbg(&udev->dev, "device reset changed speed!\n");
  2401. goto fail;
  2402. }
  2403. oldspeed = udev->speed;
  2404. /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
  2405. * it's fixed size except for full speed devices.
  2406. * For Wireless USB devices, ep0 max packet is always 512 (tho
  2407. * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
  2408. */
  2409. switch (udev->speed) {
  2410. case USB_SPEED_SUPER:
  2411. case USB_SPEED_WIRELESS: /* fixed at 512 */
  2412. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
  2413. break;
  2414. case USB_SPEED_HIGH: /* fixed at 64 */
  2415. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  2416. break;
  2417. case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
  2418. /* to determine the ep0 maxpacket size, try to read
  2419. * the device descriptor to get bMaxPacketSize0 and
  2420. * then correct our initial guess.
  2421. */
  2422. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  2423. break;
  2424. case USB_SPEED_LOW: /* fixed at 8 */
  2425. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
  2426. break;
  2427. default:
  2428. goto fail;
  2429. }
  2430. type = "";
  2431. switch (udev->speed) {
  2432. case USB_SPEED_LOW: speed = "low"; break;
  2433. case USB_SPEED_FULL: speed = "full"; break;
  2434. case USB_SPEED_HIGH: speed = "high"; break;
  2435. case USB_SPEED_SUPER:
  2436. speed = "super";
  2437. break;
  2438. case USB_SPEED_WIRELESS:
  2439. speed = "variable";
  2440. type = "Wireless ";
  2441. break;
  2442. default: speed = "?"; break;
  2443. }
  2444. if (udev->speed != USB_SPEED_SUPER)
  2445. dev_info(&udev->dev,
  2446. "%s %s speed %sUSB device number %d using %s\n",
  2447. (udev->config) ? "reset" : "new", speed, type,
  2448. devnum, udev->bus->controller->driver->name);
  2449. /* Set up TT records, if needed */
  2450. if (hdev->tt) {
  2451. udev->tt = hdev->tt;
  2452. udev->ttport = hdev->ttport;
  2453. } else if (udev->speed != USB_SPEED_HIGH
  2454. && hdev->speed == USB_SPEED_HIGH) {
  2455. if (!hub->tt.hub) {
  2456. dev_err(&udev->dev, "parent hub has no TT\n");
  2457. retval = -EINVAL;
  2458. goto fail;
  2459. }
  2460. udev->tt = &hub->tt;
  2461. udev->ttport = port1;
  2462. }
  2463. /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
  2464. * Because device hardware and firmware is sometimes buggy in
  2465. * this area, and this is how Linux has done it for ages.
  2466. * Change it cautiously.
  2467. *
  2468. * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
  2469. * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
  2470. * so it may help with some non-standards-compliant devices.
  2471. * Otherwise we start with SET_ADDRESS and then try to read the
  2472. * first 8 bytes of the device descriptor to get the ep0 maxpacket
  2473. * value.
  2474. */
  2475. for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
  2476. if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
  2477. struct usb_device_descriptor *buf;
  2478. int r = 0;
  2479. #define GET_DESCRIPTOR_BUFSIZE 64
  2480. buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
  2481. if (!buf) {
  2482. retval = -ENOMEM;
  2483. continue;
  2484. }
  2485. /* Retry on all errors; some devices are flakey.
  2486. * 255 is for WUSB devices, we actually need to use
  2487. * 512 (WUSB1.0[4.8.1]).
  2488. */
  2489. for (j = 0; j < 3; ++j) {
  2490. buf->bMaxPacketSize0 = 0;
  2491. r = usb_control_msg(udev, usb_rcvaddr0pipe(),
  2492. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  2493. USB_DT_DEVICE << 8, 0,
  2494. buf, GET_DESCRIPTOR_BUFSIZE,
  2495. initial_descriptor_timeout);
  2496. switch (buf->bMaxPacketSize0) {
  2497. case 8: case 16: case 32: case 64: case 255:
  2498. if (buf->bDescriptorType ==
  2499. USB_DT_DEVICE) {
  2500. r = 0;
  2501. break;
  2502. }
  2503. /* FALL THROUGH */
  2504. default:
  2505. if (r == 0)
  2506. r = -EPROTO;
  2507. break;
  2508. }
  2509. if (r == 0)
  2510. break;
  2511. }
  2512. udev->descriptor.bMaxPacketSize0 =
  2513. buf->bMaxPacketSize0;
  2514. kfree(buf);
  2515. retval = hub_port_reset(hub, port1, udev, delay);
  2516. if (retval < 0) /* error or disconnect */
  2517. goto fail;
  2518. if (oldspeed != udev->speed) {
  2519. dev_dbg(&udev->dev,
  2520. "device reset changed speed!\n");
  2521. retval = -ENODEV;
  2522. goto fail;
  2523. }
  2524. if (r) {
  2525. dev_err(&udev->dev,
  2526. "device descriptor read/64, error %d\n",
  2527. r);
  2528. retval = -EMSGSIZE;
  2529. continue;
  2530. }
  2531. #undef GET_DESCRIPTOR_BUFSIZE
  2532. }
  2533. /*
  2534. * If device is WUSB, we already assigned an
  2535. * unauthorized address in the Connect Ack sequence;
  2536. * authorization will assign the final address.
  2537. */
  2538. if (udev->wusb == 0) {
  2539. for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
  2540. retval = hub_set_address(udev, devnum);
  2541. if (retval >= 0)
  2542. break;
  2543. msleep(200);
  2544. }
  2545. if (retval < 0) {
  2546. dev_err(&udev->dev,
  2547. "device not accepting address %d, error %d\n",
  2548. devnum, retval);
  2549. goto fail;
  2550. }
  2551. if (udev->speed == USB_SPEED_SUPER) {
  2552. devnum = udev->devnum;
  2553. dev_info(&udev->dev,
  2554. "%s SuperSpeed USB device number %d using %s\n",
  2555. (udev->config) ? "reset" : "new",
  2556. devnum, udev->bus->controller->driver->name);
  2557. }
  2558. /* cope with hardware quirkiness:
  2559. * - let SET_ADDRESS settle, some device hardware wants it
  2560. * - read ep0 maxpacket even for high and low speed,
  2561. */
  2562. msleep(10);
  2563. if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
  2564. break;
  2565. }
  2566. retval = usb_get_device_descriptor(udev, 8);
  2567. if (retval < 8) {
  2568. dev_err(&udev->dev,
  2569. "device descriptor read/8, error %d\n",
  2570. retval);
  2571. if (retval >= 0)
  2572. retval = -EMSGSIZE;
  2573. } else {
  2574. retval = 0;
  2575. break;
  2576. }
  2577. }
  2578. if (retval)
  2579. goto fail;
  2580. if (udev->descriptor.bMaxPacketSize0 == 0xff ||
  2581. udev->speed == USB_SPEED_SUPER)
  2582. i = 512;
  2583. else
  2584. i = udev->descriptor.bMaxPacketSize0;
  2585. if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
  2586. if (udev->speed == USB_SPEED_LOW ||
  2587. !(i == 8 || i == 16 || i == 32 || i == 64)) {
  2588. dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
  2589. retval = -EMSGSIZE;
  2590. goto fail;
  2591. }
  2592. if (udev->speed == USB_SPEED_FULL)
  2593. dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
  2594. else
  2595. dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
  2596. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
  2597. usb_ep0_reinit(udev);
  2598. }
  2599. retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
  2600. if (retval < (signed)sizeof(udev->descriptor)) {
  2601. dev_err(&udev->dev, "device descriptor read/all, error %d\n",
  2602. retval);
  2603. if (retval >= 0)
  2604. retval = -ENOMSG;
  2605. goto fail;
  2606. }
  2607. retval = 0;
  2608. /* notify HCD that we have a device connected and addressed */
  2609. if (hcd->driver->update_device)
  2610. hcd->driver->update_device(hcd, udev);
  2611. fail:
  2612. if (retval) {
  2613. hub_port_disable(hub, port1, 0);
  2614. update_devnum(udev, devnum); /* for disconnect processing */
  2615. }
  2616. mutex_unlock(&usb_address0_mutex);
  2617. return retval;
  2618. }
  2619. static void
  2620. check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
  2621. {
  2622. struct usb_qualifier_descriptor *qual;
  2623. int status;
  2624. qual = kmalloc (sizeof *qual, GFP_KERNEL);
  2625. if (qual == NULL)
  2626. return;
  2627. status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
  2628. qual, sizeof *qual);
  2629. if (status == sizeof *qual) {
  2630. dev_info(&udev->dev, "not running at top speed; "
  2631. "connect to a high speed hub\n");
  2632. /* hub LEDs are probably harder to miss than syslog */
  2633. if (hub->has_indicators) {
  2634. hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
  2635. schedule_delayed_work (&hub->leds, 0);
  2636. }
  2637. }
  2638. kfree(qual);
  2639. }
  2640. static unsigned
  2641. hub_power_remaining (struct usb_hub *hub)
  2642. {
  2643. struct usb_device *hdev = hub->hdev;
  2644. int remaining;
  2645. int port1;
  2646. if (!hub->limited_power)
  2647. return 0;
  2648. remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
  2649. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  2650. struct usb_device *udev = hdev->children[port1 - 1];
  2651. int delta;
  2652. if (!udev)
  2653. continue;
  2654. /* Unconfigured devices may not use more than 100mA,
  2655. * or 8mA for OTG ports */
  2656. if (udev->actconfig)
  2657. delta = udev->actconfig->desc.bMaxPower * 2;
  2658. else if (port1 != udev->bus->otg_port || hdev->parent)
  2659. delta = 100;
  2660. else
  2661. delta = 8;
  2662. if (delta > hub->mA_per_port)
  2663. dev_warn(&udev->dev,
  2664. "%dmA is over %umA budget for port %d!\n",
  2665. delta, hub->mA_per_port, port1);
  2666. remaining -= delta;
  2667. }
  2668. if (remaining < 0) {
  2669. dev_warn(hub->intfdev, "%dmA over power budget!\n",
  2670. - remaining);
  2671. remaining = 0;
  2672. }
  2673. return remaining;
  2674. }
  2675. /* Handle physical or logical connection change events.
  2676. * This routine is called when:
  2677. * a port connection-change occurs;
  2678. * a port enable-change occurs (often caused by EMI);
  2679. * usb_reset_and_verify_device() encounters changed descriptors (as from
  2680. * a firmware download)
  2681. * caller already locked the hub
  2682. */
  2683. static void hub_port_connect_change(struct usb_hub *hub, int port1,
  2684. u16 portstatus, u16 portchange)
  2685. {
  2686. struct usb_device *hdev = hub->hdev;
  2687. struct device *hub_dev = hub->intfdev;
  2688. struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
  2689. unsigned wHubCharacteristics =
  2690. le16_to_cpu(hub->descriptor->wHubCharacteristics);
  2691. struct usb_device *udev;
  2692. int status, i;
  2693. dev_dbg (hub_dev,
  2694. "port %d, status %04x, change %04x, %s\n",
  2695. port1, portstatus, portchange, portspeed(hub, portstatus));
  2696. if (hub->has_indicators) {
  2697. set_port_led(hub, port1, HUB_LED_AUTO);
  2698. hub->indicator[port1-1] = INDICATOR_AUTO;
  2699. }
  2700. #ifdef CONFIG_USB_OTG
  2701. /* during HNP, don't repeat the debounce */
  2702. if (hdev->bus->is_b_host)
  2703. portchange &= ~(USB_PORT_STAT_C_CONNECTION |
  2704. USB_PORT_STAT_C_ENABLE);
  2705. #endif
  2706. /* Try to resuscitate an existing device */
  2707. udev = hdev->children[port1-1];
  2708. if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
  2709. udev->state != USB_STATE_NOTATTACHED) {
  2710. usb_lock_device(udev);
  2711. if (portstatus & USB_PORT_STAT_ENABLE) {
  2712. status = 0; /* Nothing to do */
  2713. #ifdef CONFIG_USB_SUSPEND
  2714. } else if (udev->state == USB_STATE_SUSPENDED &&
  2715. udev->persist_enabled) {
  2716. /* For a suspended device, treat this as a
  2717. * remote wakeup event.
  2718. */
  2719. status = usb_remote_wakeup(udev);
  2720. #endif
  2721. } else {
  2722. status = -ENODEV; /* Don't resuscitate */
  2723. }
  2724. usb_unlock_device(udev);
  2725. if (status == 0) {
  2726. clear_bit(port1, hub->change_bits);
  2727. return;
  2728. }
  2729. }
  2730. /* Disconnect any existing devices under this port */
  2731. if (udev)
  2732. usb_disconnect(&hdev->children[port1-1]);
  2733. clear_bit(port1, hub->change_bits);
  2734. /* We can forget about a "removed" device when there's a physical
  2735. * disconnect or the connect status changes.
  2736. */
  2737. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  2738. (portchange & USB_PORT_STAT_C_CONNECTION))
  2739. clear_bit(port1, hub->removed_bits);
  2740. if (portchange & (USB_PORT_STAT_C_CONNECTION |
  2741. USB_PORT_STAT_C_ENABLE)) {
  2742. status = hub_port_debounce(hub, port1);
  2743. if (status < 0) {
  2744. if (printk_ratelimit())
  2745. dev_err(hub_dev, "connect-debounce failed, "
  2746. "port %d disabled\n", port1);
  2747. portstatus &= ~USB_PORT_STAT_CONNECTION;
  2748. } else {
  2749. portstatus = status;
  2750. }
  2751. }
  2752. /* Return now if debouncing failed or nothing is connected or
  2753. * the device was "removed".
  2754. */
  2755. if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
  2756. test_bit(port1, hub->removed_bits)) {
  2757. /* maybe switch power back on (e.g. root hub was reset) */
  2758. if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
  2759. && !(portstatus & USB_PORT_STAT_POWER))
  2760. set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  2761. if (portstatus & USB_PORT_STAT_ENABLE)
  2762. goto done;
  2763. return;
  2764. }
  2765. for (i = 0; i < SET_CONFIG_TRIES; i++) {
  2766. /* reallocate for each attempt, since references
  2767. * to the previous one can escape in various ways
  2768. */
  2769. udev = usb_alloc_dev(hdev, hdev->bus, port1);
  2770. if (!udev) {
  2771. dev_err (hub_dev,
  2772. "couldn't allocate port %d usb_device\n",
  2773. port1);
  2774. goto done;
  2775. }
  2776. usb_set_device_state(udev, USB_STATE_POWERED);
  2777. udev->bus_mA = hub->mA_per_port;
  2778. udev->level = hdev->level + 1;
  2779. udev->wusb = hub_is_wusb(hub);
  2780. /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
  2781. if (hub_is_superspeed(hub->hdev))
  2782. udev->speed = USB_SPEED_SUPER;
  2783. else
  2784. udev->speed = USB_SPEED_UNKNOWN;
  2785. choose_devnum(udev);
  2786. if (udev->devnum <= 0) {
  2787. status = -ENOTCONN; /* Don't retry */
  2788. goto loop;
  2789. }
  2790. /* reset (non-USB 3.0 devices) and get descriptor */
  2791. status = hub_port_init(hub, udev, port1, i);
  2792. if (status < 0)
  2793. goto loop;
  2794. usb_detect_quirks(udev);
  2795. if (udev->quirks & USB_QUIRK_DELAY_INIT)
  2796. msleep(1000);
  2797. /* consecutive bus-powered hubs aren't reliable; they can
  2798. * violate the voltage drop budget. if the new child has
  2799. * a "powered" LED, users should notice we didn't enable it
  2800. * (without reading syslog), even without per-port LEDs
  2801. * on the parent.
  2802. */
  2803. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
  2804. && udev->bus_mA <= 100) {
  2805. u16 devstat;
  2806. status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
  2807. &devstat);
  2808. if (status < 2) {
  2809. dev_dbg(&udev->dev, "get status %d ?\n", status);
  2810. goto loop_disable;
  2811. }
  2812. le16_to_cpus(&devstat);
  2813. if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  2814. dev_err(&udev->dev,
  2815. "can't connect bus-powered hub "
  2816. "to this port\n");
  2817. if (hub->has_indicators) {
  2818. hub->indicator[port1-1] =
  2819. INDICATOR_AMBER_BLINK;
  2820. schedule_delayed_work (&hub->leds, 0);
  2821. }
  2822. status = -ENOTCONN; /* Don't retry */
  2823. goto loop_disable;
  2824. }
  2825. }
  2826. /* check for devices running slower than they could */
  2827. if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
  2828. && udev->speed == USB_SPEED_FULL
  2829. && highspeed_hubs != 0)
  2830. check_highspeed (hub, udev, port1);
  2831. /* Store the parent's children[] pointer. At this point
  2832. * udev becomes globally accessible, although presumably
  2833. * no one will look at it until hdev is unlocked.
  2834. */
  2835. status = 0;
  2836. /* We mustn't add new devices if the parent hub has
  2837. * been disconnected; we would race with the
  2838. * recursively_mark_NOTATTACHED() routine.
  2839. */
  2840. spin_lock_irq(&device_state_lock);
  2841. if (hdev->state == USB_STATE_NOTATTACHED)
  2842. status = -ENOTCONN;
  2843. else
  2844. hdev->children[port1-1] = udev;
  2845. spin_unlock_irq(&device_state_lock);
  2846. /* Run it through the hoops (find a driver, etc) */
  2847. if (!status) {
  2848. status = usb_new_device(udev);
  2849. if (status) {
  2850. spin_lock_irq(&device_state_lock);
  2851. hdev->children[port1-1] = NULL;
  2852. spin_unlock_irq(&device_state_lock);
  2853. }
  2854. }
  2855. if (status)
  2856. goto loop_disable;
  2857. status = hub_power_remaining(hub);
  2858. if (status)
  2859. dev_dbg(hub_dev, "%dmA power budget left\n", status);
  2860. return;
  2861. loop_disable:
  2862. hub_port_disable(hub, port1, 1);
  2863. loop:
  2864. usb_ep0_reinit(udev);
  2865. release_devnum(udev);
  2866. hub_free_dev(udev);
  2867. usb_put_dev(udev);
  2868. if ((status == -ENOTCONN) || (status == -ENOTSUPP))
  2869. break;
  2870. }
  2871. if (hub->hdev->parent ||
  2872. !hcd->driver->port_handed_over ||
  2873. !(hcd->driver->port_handed_over)(hcd, port1))
  2874. dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
  2875. port1);
  2876. done:
  2877. hub_port_disable(hub, port1, 1);
  2878. if (hcd->driver->relinquish_port && !hub->hdev->parent)
  2879. hcd->driver->relinquish_port(hcd, port1);
  2880. }
  2881. static void hub_events(void)
  2882. {
  2883. struct list_head *tmp;
  2884. struct usb_device *hdev;
  2885. struct usb_interface *intf;
  2886. struct usb_hub *hub;
  2887. struct device *hub_dev;
  2888. u16 hubstatus;
  2889. u16 hubchange;
  2890. u16 portstatus;
  2891. u16 portchange;
  2892. int i, ret;
  2893. int connect_change;
  2894. /*
  2895. * We restart the list every time to avoid a deadlock with
  2896. * deleting hubs downstream from this one. This should be
  2897. * safe since we delete the hub from the event list.
  2898. * Not the most efficient, but avoids deadlocks.
  2899. */
  2900. while (1) {
  2901. /* Grab the first entry at the beginning of the list */
  2902. spin_lock_irq(&hub_event_lock);
  2903. if (list_empty(&hub_event_list)) {
  2904. spin_unlock_irq(&hub_event_lock);
  2905. break;
  2906. }
  2907. tmp = hub_event_list.next;
  2908. list_del_init(tmp);
  2909. hub = list_entry(tmp, struct usb_hub, event_list);
  2910. kref_get(&hub->kref);
  2911. spin_unlock_irq(&hub_event_lock);
  2912. hdev = hub->hdev;
  2913. hub_dev = hub->intfdev;
  2914. intf = to_usb_interface(hub_dev);
  2915. dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
  2916. hdev->state, hub->descriptor
  2917. ? hub->descriptor->bNbrPorts
  2918. : 0,
  2919. /* NOTE: expects max 15 ports... */
  2920. (u16) hub->change_bits[0],
  2921. (u16) hub->event_bits[0]);
  2922. /* Lock the device, then check to see if we were
  2923. * disconnected while waiting for the lock to succeed. */
  2924. usb_lock_device(hdev);
  2925. if (unlikely(hub->disconnected))
  2926. goto loop_disconnected;
  2927. /* If the hub has died, clean up after it */
  2928. if (hdev->state == USB_STATE_NOTATTACHED) {
  2929. hub->error = -ENODEV;
  2930. hub_quiesce(hub, HUB_DISCONNECT);
  2931. goto loop;
  2932. }
  2933. /* Autoresume */
  2934. ret = usb_autopm_get_interface(intf);
  2935. if (ret) {
  2936. dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
  2937. goto loop;
  2938. }
  2939. /* If this is an inactive hub, do nothing */
  2940. if (hub->quiescing)
  2941. goto loop_autopm;
  2942. if (hub->error) {
  2943. dev_dbg (hub_dev, "resetting for error %d\n",
  2944. hub->error);
  2945. ret = usb_reset_device(hdev);
  2946. if (ret) {
  2947. dev_dbg (hub_dev,
  2948. "error resetting hub: %d\n", ret);
  2949. goto loop_autopm;
  2950. }
  2951. hub->nerrors = 0;
  2952. hub->error = 0;
  2953. }
  2954. /* deal with port status changes */
  2955. for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
  2956. if (test_bit(i, hub->busy_bits))
  2957. continue;
  2958. connect_change = test_bit(i, hub->change_bits);
  2959. if (!test_and_clear_bit(i, hub->event_bits) &&
  2960. !connect_change)
  2961. continue;
  2962. ret = hub_port_status(hub, i,
  2963. &portstatus, &portchange);
  2964. if (ret < 0)
  2965. continue;
  2966. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  2967. clear_port_feature(hdev, i,
  2968. USB_PORT_FEAT_C_CONNECTION);
  2969. connect_change = 1;
  2970. }
  2971. if (portchange & USB_PORT_STAT_C_ENABLE) {
  2972. if (!connect_change)
  2973. dev_dbg (hub_dev,
  2974. "port %d enable change, "
  2975. "status %08x\n",
  2976. i, portstatus);
  2977. clear_port_feature(hdev, i,
  2978. USB_PORT_FEAT_C_ENABLE);
  2979. /*
  2980. * EM interference sometimes causes badly
  2981. * shielded USB devices to be shutdown by
  2982. * the hub, this hack enables them again.
  2983. * Works at least with mouse driver.
  2984. */
  2985. if (!(portstatus & USB_PORT_STAT_ENABLE)
  2986. && !connect_change
  2987. && hdev->children[i-1]) {
  2988. dev_err (hub_dev,
  2989. "port %i "
  2990. "disabled by hub (EMI?), "
  2991. "re-enabling...\n",
  2992. i);
  2993. connect_change = 1;
  2994. }
  2995. }
  2996. if (portchange & USB_PORT_STAT_C_SUSPEND) {
  2997. struct usb_device *udev;
  2998. clear_port_feature(hdev, i,
  2999. USB_PORT_FEAT_C_SUSPEND);
  3000. udev = hdev->children[i-1];
  3001. if (udev) {
  3002. /* TRSMRCY = 10 msec */
  3003. msleep(10);
  3004. usb_lock_device(udev);
  3005. ret = usb_remote_wakeup(hdev->
  3006. children[i-1]);
  3007. usb_unlock_device(udev);
  3008. if (ret < 0)
  3009. connect_change = 1;
  3010. } else {
  3011. ret = -ENODEV;
  3012. hub_port_disable(hub, i, 1);
  3013. }
  3014. dev_dbg (hub_dev,
  3015. "resume on port %d, status %d\n",
  3016. i, ret);
  3017. }
  3018. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  3019. u16 status = 0;
  3020. u16 unused;
  3021. dev_dbg(hub_dev, "over-current change on port "
  3022. "%d\n", i);
  3023. clear_port_feature(hdev, i,
  3024. USB_PORT_FEAT_C_OVER_CURRENT);
  3025. msleep(100); /* Cool down */
  3026. hub_power_on(hub, true);
  3027. hub_port_status(hub, i, &status, &unused);
  3028. if (status & USB_PORT_STAT_OVERCURRENT)
  3029. dev_err(hub_dev, "over-current "
  3030. "condition on port %d\n", i);
  3031. }
  3032. if (portchange & USB_PORT_STAT_C_RESET) {
  3033. dev_dbg (hub_dev,
  3034. "reset change on port %d\n",
  3035. i);
  3036. clear_port_feature(hdev, i,
  3037. USB_PORT_FEAT_C_RESET);
  3038. }
  3039. if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
  3040. hub_is_superspeed(hub->hdev)) {
  3041. dev_dbg(hub_dev,
  3042. "warm reset change on port %d\n",
  3043. i);
  3044. clear_port_feature(hdev, i,
  3045. USB_PORT_FEAT_C_BH_PORT_RESET);
  3046. }
  3047. if (portchange & USB_PORT_STAT_C_LINK_STATE) {
  3048. clear_port_feature(hub->hdev, i,
  3049. USB_PORT_FEAT_C_PORT_LINK_STATE);
  3050. }
  3051. if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
  3052. dev_warn(hub_dev,
  3053. "config error on port %d\n",
  3054. i);
  3055. clear_port_feature(hub->hdev, i,
  3056. USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
  3057. }
  3058. if (connect_change)
  3059. hub_port_connect_change(hub, i,
  3060. portstatus, portchange);
  3061. } /* end for i */
  3062. /* deal with hub status changes */
  3063. if (test_and_clear_bit(0, hub->event_bits) == 0)
  3064. ; /* do nothing */
  3065. else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
  3066. dev_err (hub_dev, "get_hub_status failed\n");
  3067. else {
  3068. if (hubchange & HUB_CHANGE_LOCAL_POWER) {
  3069. dev_dbg (hub_dev, "power change\n");
  3070. clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
  3071. if (hubstatus & HUB_STATUS_LOCAL_POWER)
  3072. /* FIXME: Is this always true? */
  3073. hub->limited_power = 1;
  3074. else
  3075. hub->limited_power = 0;
  3076. }
  3077. if (hubchange & HUB_CHANGE_OVERCURRENT) {
  3078. u16 status = 0;
  3079. u16 unused;
  3080. dev_dbg(hub_dev, "over-current change\n");
  3081. clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
  3082. msleep(500); /* Cool down */
  3083. hub_power_on(hub, true);
  3084. hub_hub_status(hub, &status, &unused);
  3085. if (status & HUB_STATUS_OVERCURRENT)
  3086. dev_err(hub_dev, "over-current "
  3087. "condition\n");
  3088. }
  3089. }
  3090. loop_autopm:
  3091. /* Balance the usb_autopm_get_interface() above */
  3092. usb_autopm_put_interface_no_suspend(intf);
  3093. loop:
  3094. /* Balance the usb_autopm_get_interface_no_resume() in
  3095. * kick_khubd() and allow autosuspend.
  3096. */
  3097. usb_autopm_put_interface(intf);
  3098. loop_disconnected:
  3099. usb_unlock_device(hdev);
  3100. kref_put(&hub->kref, hub_release);
  3101. } /* end while (1) */
  3102. }
  3103. static int hub_thread(void *__unused)
  3104. {
  3105. /* khubd needs to be freezable to avoid intefering with USB-PERSIST
  3106. * port handover. Otherwise it might see that a full-speed device
  3107. * was gone before the EHCI controller had handed its port over to
  3108. * the companion full-speed controller.
  3109. */
  3110. set_freezable();
  3111. do {
  3112. hub_events();
  3113. wait_event_freezable(khubd_wait,
  3114. !list_empty(&hub_event_list) ||
  3115. kthread_should_stop());
  3116. } while (!kthread_should_stop() || !list_empty(&hub_event_list));
  3117. pr_debug("%s: khubd exiting\n", usbcore_name);
  3118. return 0;
  3119. }
  3120. static const struct usb_device_id hub_id_table[] = {
  3121. { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  3122. .bDeviceClass = USB_CLASS_HUB},
  3123. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  3124. .bInterfaceClass = USB_CLASS_HUB},
  3125. { } /* Terminating entry */
  3126. };
  3127. MODULE_DEVICE_TABLE (usb, hub_id_table);
  3128. static struct usb_driver hub_driver = {
  3129. .name = "hub",
  3130. .probe = hub_probe,
  3131. .disconnect = hub_disconnect,
  3132. .suspend = hub_suspend,
  3133. .resume = hub_resume,
  3134. .reset_resume = hub_reset_resume,
  3135. .pre_reset = hub_pre_reset,
  3136. .post_reset = hub_post_reset,
  3137. .unlocked_ioctl = hub_ioctl,
  3138. .id_table = hub_id_table,
  3139. .supports_autosuspend = 1,
  3140. };
  3141. int usb_hub_init(void)
  3142. {
  3143. if (usb_register(&hub_driver) < 0) {
  3144. printk(KERN_ERR "%s: can't register hub driver\n",
  3145. usbcore_name);
  3146. return -1;
  3147. }
  3148. khubd_task = kthread_run(hub_thread, NULL, "khubd");
  3149. if (!IS_ERR(khubd_task))
  3150. return 0;
  3151. /* Fall through if kernel_thread failed */
  3152. usb_deregister(&hub_driver);
  3153. printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
  3154. return -1;
  3155. }
  3156. void usb_hub_cleanup(void)
  3157. {
  3158. kthread_stop(khubd_task);
  3159. /*
  3160. * Hub resources are freed for us by usb_deregister. It calls
  3161. * usb_driver_purge on every device which in turn calls that
  3162. * devices disconnect function if it is using this driver.
  3163. * The hub_disconnect function takes care of releasing the
  3164. * individual hub resources. -greg
  3165. */
  3166. usb_deregister(&hub_driver);
  3167. } /* usb_hub_cleanup() */
  3168. static int descriptors_changed(struct usb_device *udev,
  3169. struct usb_device_descriptor *old_device_descriptor)
  3170. {
  3171. int changed = 0;
  3172. unsigned index;
  3173. unsigned serial_len = 0;
  3174. unsigned len;
  3175. unsigned old_length;
  3176. int length;
  3177. char *buf;
  3178. if (memcmp(&udev->descriptor, old_device_descriptor,
  3179. sizeof(*old_device_descriptor)) != 0)
  3180. return 1;
  3181. /* Since the idVendor, idProduct, and bcdDevice values in the
  3182. * device descriptor haven't changed, we will assume the
  3183. * Manufacturer and Product strings haven't changed either.
  3184. * But the SerialNumber string could be different (e.g., a
  3185. * different flash card of the same brand).
  3186. */
  3187. if (udev->serial)
  3188. serial_len = strlen(udev->serial) + 1;
  3189. len = serial_len;
  3190. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  3191. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  3192. len = max(len, old_length);
  3193. }
  3194. buf = kmalloc(len, GFP_NOIO);
  3195. if (buf == NULL) {
  3196. dev_err(&udev->dev, "no mem to re-read configs after reset\n");
  3197. /* assume the worst */
  3198. return 1;
  3199. }
  3200. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  3201. old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  3202. length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
  3203. old_length);
  3204. if (length != old_length) {
  3205. dev_dbg(&udev->dev, "config index %d, error %d\n",
  3206. index, length);
  3207. changed = 1;
  3208. break;
  3209. }
  3210. if (memcmp (buf, udev->rawdescriptors[index], old_length)
  3211. != 0) {
  3212. dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
  3213. index,
  3214. ((struct usb_config_descriptor *) buf)->
  3215. bConfigurationValue);
  3216. changed = 1;
  3217. break;
  3218. }
  3219. }
  3220. if (!changed && serial_len) {
  3221. length = usb_string(udev, udev->descriptor.iSerialNumber,
  3222. buf, serial_len);
  3223. if (length + 1 != serial_len) {
  3224. dev_dbg(&udev->dev, "serial string error %d\n",
  3225. length);
  3226. changed = 1;
  3227. } else if (memcmp(buf, udev->serial, length) != 0) {
  3228. dev_dbg(&udev->dev, "serial string changed\n");
  3229. changed = 1;
  3230. }
  3231. }
  3232. kfree(buf);
  3233. return changed;
  3234. }
  3235. /**
  3236. * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
  3237. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  3238. *
  3239. * WARNING - don't use this routine to reset a composite device
  3240. * (one with multiple interfaces owned by separate drivers)!
  3241. * Use usb_reset_device() instead.
  3242. *
  3243. * Do a port reset, reassign the device's address, and establish its
  3244. * former operating configuration. If the reset fails, or the device's
  3245. * descriptors change from their values before the reset, or the original
  3246. * configuration and altsettings cannot be restored, a flag will be set
  3247. * telling khubd to pretend the device has been disconnected and then
  3248. * re-connected. All drivers will be unbound, and the device will be
  3249. * re-enumerated and probed all over again.
  3250. *
  3251. * Returns 0 if the reset succeeded, -ENODEV if the device has been
  3252. * flagged for logical disconnection, or some other negative error code
  3253. * if the reset wasn't even attempted.
  3254. *
  3255. * The caller must own the device lock. For example, it's safe to use
  3256. * this from a driver probe() routine after downloading new firmware.
  3257. * For calls that might not occur during probe(), drivers should lock
  3258. * the device using usb_lock_device_for_reset().
  3259. *
  3260. * Locking exception: This routine may also be called from within an
  3261. * autoresume handler. Such usage won't conflict with other tasks
  3262. * holding the device lock because these tasks should always call
  3263. * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
  3264. */
  3265. static int usb_reset_and_verify_device(struct usb_device *udev)
  3266. {
  3267. struct usb_device *parent_hdev = udev->parent;
  3268. struct usb_hub *parent_hub;
  3269. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  3270. struct usb_device_descriptor descriptor = udev->descriptor;
  3271. int i, ret = 0;
  3272. int port1 = udev->portnum;
  3273. if (udev->state == USB_STATE_NOTATTACHED ||
  3274. udev->state == USB_STATE_SUSPENDED) {
  3275. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  3276. udev->state);
  3277. return -EINVAL;
  3278. }
  3279. if (!parent_hdev) {
  3280. /* this requires hcd-specific logic; see ohci_restart() */
  3281. dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
  3282. return -EISDIR;
  3283. }
  3284. parent_hub = hdev_to_hub(parent_hdev);
  3285. set_bit(port1, parent_hub->busy_bits);
  3286. for (i = 0; i < SET_CONFIG_TRIES; ++i) {
  3287. /* ep0 maxpacket size may change; let the HCD know about it.
  3288. * Other endpoints will be handled by re-enumeration. */
  3289. usb_ep0_reinit(udev);
  3290. ret = hub_port_init(parent_hub, udev, port1, i);
  3291. if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
  3292. break;
  3293. }
  3294. clear_bit(port1, parent_hub->busy_bits);
  3295. if (ret < 0)
  3296. goto re_enumerate;
  3297. /* Device might have changed firmware (DFU or similar) */
  3298. if (descriptors_changed(udev, &descriptor)) {
  3299. dev_info(&udev->dev, "device firmware changed\n");
  3300. udev->descriptor = descriptor; /* for disconnect() calls */
  3301. goto re_enumerate;
  3302. }
  3303. /* Restore the device's previous configuration */
  3304. if (!udev->actconfig)
  3305. goto done;
  3306. mutex_lock(hcd->bandwidth_mutex);
  3307. ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
  3308. if (ret < 0) {
  3309. dev_warn(&udev->dev,
  3310. "Busted HC? Not enough HCD resources for "
  3311. "old configuration.\n");
  3312. mutex_unlock(hcd->bandwidth_mutex);
  3313. goto re_enumerate;
  3314. }
  3315. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  3316. USB_REQ_SET_CONFIGURATION, 0,
  3317. udev->actconfig->desc.bConfigurationValue, 0,
  3318. NULL, 0, USB_CTRL_SET_TIMEOUT);
  3319. if (ret < 0) {
  3320. dev_err(&udev->dev,
  3321. "can't restore configuration #%d (error=%d)\n",
  3322. udev->actconfig->desc.bConfigurationValue, ret);
  3323. mutex_unlock(hcd->bandwidth_mutex);
  3324. goto re_enumerate;
  3325. }
  3326. mutex_unlock(hcd->bandwidth_mutex);
  3327. usb_set_device_state(udev, USB_STATE_CONFIGURED);
  3328. /* Put interfaces back into the same altsettings as before.
  3329. * Don't bother to send the Set-Interface request for interfaces
  3330. * that were already in altsetting 0; besides being unnecessary,
  3331. * many devices can't handle it. Instead just reset the host-side
  3332. * endpoint state.
  3333. */
  3334. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  3335. struct usb_host_config *config = udev->actconfig;
  3336. struct usb_interface *intf = config->interface[i];
  3337. struct usb_interface_descriptor *desc;
  3338. desc = &intf->cur_altsetting->desc;
  3339. if (desc->bAlternateSetting == 0) {
  3340. usb_disable_interface(udev, intf, true);
  3341. usb_enable_interface(udev, intf, true);
  3342. ret = 0;
  3343. } else {
  3344. /* Let the bandwidth allocation function know that this
  3345. * device has been reset, and it will have to use
  3346. * alternate setting 0 as the current alternate setting.
  3347. */
  3348. intf->resetting_device = 1;
  3349. ret = usb_set_interface(udev, desc->bInterfaceNumber,
  3350. desc->bAlternateSetting);
  3351. intf->resetting_device = 0;
  3352. }
  3353. if (ret < 0) {
  3354. dev_err(&udev->dev, "failed to restore interface %d "
  3355. "altsetting %d (error=%d)\n",
  3356. desc->bInterfaceNumber,
  3357. desc->bAlternateSetting,
  3358. ret);
  3359. goto re_enumerate;
  3360. }
  3361. }
  3362. done:
  3363. return 0;
  3364. re_enumerate:
  3365. hub_port_logical_disconnect(parent_hub, port1);
  3366. return -ENODEV;
  3367. }
  3368. /**
  3369. * usb_reset_device - warn interface drivers and perform a USB port reset
  3370. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  3371. *
  3372. * Warns all drivers bound to registered interfaces (using their pre_reset
  3373. * method), performs the port reset, and then lets the drivers know that
  3374. * the reset is over (using their post_reset method).
  3375. *
  3376. * Return value is the same as for usb_reset_and_verify_device().
  3377. *
  3378. * The caller must own the device lock. For example, it's safe to use
  3379. * this from a driver probe() routine after downloading new firmware.
  3380. * For calls that might not occur during probe(), drivers should lock
  3381. * the device using usb_lock_device_for_reset().
  3382. *
  3383. * If an interface is currently being probed or disconnected, we assume
  3384. * its driver knows how to handle resets. For all other interfaces,
  3385. * if the driver doesn't have pre_reset and post_reset methods then
  3386. * we attempt to unbind it and rebind afterward.
  3387. */
  3388. int usb_reset_device(struct usb_device *udev)
  3389. {
  3390. int ret;
  3391. int i;
  3392. struct usb_host_config *config = udev->actconfig;
  3393. if (udev->state == USB_STATE_NOTATTACHED ||
  3394. udev->state == USB_STATE_SUSPENDED) {
  3395. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  3396. udev->state);
  3397. return -EINVAL;
  3398. }
  3399. /* Prevent autosuspend during the reset */
  3400. usb_autoresume_device(udev);
  3401. if (config) {
  3402. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  3403. struct usb_interface *cintf = config->interface[i];
  3404. struct usb_driver *drv;
  3405. int unbind = 0;
  3406. if (cintf->dev.driver) {
  3407. drv = to_usb_driver(cintf->dev.driver);
  3408. if (drv->pre_reset && drv->post_reset)
  3409. unbind = (drv->pre_reset)(cintf);
  3410. else if (cintf->condition ==
  3411. USB_INTERFACE_BOUND)
  3412. unbind = 1;
  3413. if (unbind)
  3414. usb_forced_unbind_intf(cintf);
  3415. }
  3416. }
  3417. }
  3418. ret = usb_reset_and_verify_device(udev);
  3419. if (config) {
  3420. for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
  3421. struct usb_interface *cintf = config->interface[i];
  3422. struct usb_driver *drv;
  3423. int rebind = cintf->needs_binding;
  3424. if (!rebind && cintf->dev.driver) {
  3425. drv = to_usb_driver(cintf->dev.driver);
  3426. if (drv->post_reset)
  3427. rebind = (drv->post_reset)(cintf);
  3428. else if (cintf->condition ==
  3429. USB_INTERFACE_BOUND)
  3430. rebind = 1;
  3431. }
  3432. if (ret == 0 && rebind)
  3433. usb_rebind_intf(cintf);
  3434. }
  3435. }
  3436. usb_autosuspend_device(udev);
  3437. return ret;
  3438. }
  3439. EXPORT_SYMBOL_GPL(usb_reset_device);
  3440. /**
  3441. * usb_queue_reset_device - Reset a USB device from an atomic context
  3442. * @iface: USB interface belonging to the device to reset
  3443. *
  3444. * This function can be used to reset a USB device from an atomic
  3445. * context, where usb_reset_device() won't work (as it blocks).
  3446. *
  3447. * Doing a reset via this method is functionally equivalent to calling
  3448. * usb_reset_device(), except for the fact that it is delayed to a
  3449. * workqueue. This means that any drivers bound to other interfaces
  3450. * might be unbound, as well as users from usbfs in user space.
  3451. *
  3452. * Corner cases:
  3453. *
  3454. * - Scheduling two resets at the same time from two different drivers
  3455. * attached to two different interfaces of the same device is
  3456. * possible; depending on how the driver attached to each interface
  3457. * handles ->pre_reset(), the second reset might happen or not.
  3458. *
  3459. * - If a driver is unbound and it had a pending reset, the reset will
  3460. * be cancelled.
  3461. *
  3462. * - This function can be called during .probe() or .disconnect()
  3463. * times. On return from .disconnect(), any pending resets will be
  3464. * cancelled.
  3465. *
  3466. * There is no no need to lock/unlock the @reset_ws as schedule_work()
  3467. * does its own.
  3468. *
  3469. * NOTE: We don't do any reference count tracking because it is not
  3470. * needed. The lifecycle of the work_struct is tied to the
  3471. * usb_interface. Before destroying the interface we cancel the
  3472. * work_struct, so the fact that work_struct is queued and or
  3473. * running means the interface (and thus, the device) exist and
  3474. * are referenced.
  3475. */
  3476. void usb_queue_reset_device(struct usb_interface *iface)
  3477. {
  3478. schedule_work(&iface->reset_ws);
  3479. }
  3480. EXPORT_SYMBOL_GPL(usb_queue_reset_device);