hub.c 120 KB

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