hub.c 88 KB

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