hub.c 96 KB

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