hub.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091
  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. static int hub_port_status(struct usb_hub *hub, int port1,
  1212. u16 *status, u16 *change)
  1213. {
  1214. int ret;
  1215. mutex_lock(&hub->status_mutex);
  1216. ret = get_port_status(hub->hdev, port1, &hub->status->port);
  1217. if (ret < 4) {
  1218. dev_err (hub->intfdev,
  1219. "%s failed (err = %d)\n", __FUNCTION__, ret);
  1220. if (ret >= 0)
  1221. ret = -EIO;
  1222. } else {
  1223. *status = le16_to_cpu(hub->status->port.wPortStatus);
  1224. *change = le16_to_cpu(hub->status->port.wPortChange);
  1225. ret = 0;
  1226. }
  1227. mutex_unlock(&hub->status_mutex);
  1228. return ret;
  1229. }
  1230. /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
  1231. static unsigned hub_is_wusb(struct usb_hub *hub)
  1232. {
  1233. struct usb_hcd *hcd;
  1234. if (hub->hdev->parent != NULL) /* not a root hub? */
  1235. return 0;
  1236. hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
  1237. return hcd->wireless;
  1238. }
  1239. #define PORT_RESET_TRIES 5
  1240. #define SET_ADDRESS_TRIES 2
  1241. #define GET_DESCRIPTOR_TRIES 2
  1242. #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
  1243. #define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first)
  1244. #define HUB_ROOT_RESET_TIME 50 /* times are in msec */
  1245. #define HUB_SHORT_RESET_TIME 10
  1246. #define HUB_LONG_RESET_TIME 200
  1247. #define HUB_RESET_TIMEOUT 500
  1248. static int hub_port_wait_reset(struct usb_hub *hub, int port1,
  1249. struct usb_device *udev, unsigned int delay)
  1250. {
  1251. int delay_time, ret;
  1252. u16 portstatus;
  1253. u16 portchange;
  1254. for (delay_time = 0;
  1255. delay_time < HUB_RESET_TIMEOUT;
  1256. delay_time += delay) {
  1257. /* wait to give the device a chance to reset */
  1258. msleep(delay);
  1259. /* read and decode port status */
  1260. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  1261. if (ret < 0)
  1262. return ret;
  1263. /* Device went away? */
  1264. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  1265. return -ENOTCONN;
  1266. /* bomb out completely if the connection bounced */
  1267. if ((portchange & USB_PORT_STAT_C_CONNECTION))
  1268. return -ENOTCONN;
  1269. /* if we`ve finished resetting, then break out of the loop */
  1270. if (!(portstatus & USB_PORT_STAT_RESET) &&
  1271. (portstatus & USB_PORT_STAT_ENABLE)) {
  1272. if (hub_is_wusb(hub))
  1273. udev->speed = USB_SPEED_VARIABLE;
  1274. else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
  1275. udev->speed = USB_SPEED_HIGH;
  1276. else if (portstatus & USB_PORT_STAT_LOW_SPEED)
  1277. udev->speed = USB_SPEED_LOW;
  1278. else
  1279. udev->speed = USB_SPEED_FULL;
  1280. return 0;
  1281. }
  1282. /* switch to the long delay after two short delay failures */
  1283. if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
  1284. delay = HUB_LONG_RESET_TIME;
  1285. dev_dbg (hub->intfdev,
  1286. "port %d not reset yet, waiting %dms\n",
  1287. port1, delay);
  1288. }
  1289. return -EBUSY;
  1290. }
  1291. static int hub_port_reset(struct usb_hub *hub, int port1,
  1292. struct usb_device *udev, unsigned int delay)
  1293. {
  1294. int i, status;
  1295. /* Reset the port */
  1296. for (i = 0; i < PORT_RESET_TRIES; i++) {
  1297. status = set_port_feature(hub->hdev,
  1298. port1, USB_PORT_FEAT_RESET);
  1299. if (status)
  1300. dev_err(hub->intfdev,
  1301. "cannot reset port %d (err = %d)\n",
  1302. port1, status);
  1303. else {
  1304. status = hub_port_wait_reset(hub, port1, udev, delay);
  1305. if (status && status != -ENOTCONN)
  1306. dev_dbg(hub->intfdev,
  1307. "port_wait_reset: err = %d\n",
  1308. status);
  1309. }
  1310. /* return on disconnect or reset */
  1311. switch (status) {
  1312. case 0:
  1313. /* TRSTRCY = 10 ms; plus some extra */
  1314. msleep(10 + 40);
  1315. udev->devnum = 0; /* Device now at address 0 */
  1316. /* FALL THROUGH */
  1317. case -ENOTCONN:
  1318. case -ENODEV:
  1319. clear_port_feature(hub->hdev,
  1320. port1, USB_PORT_FEAT_C_RESET);
  1321. /* FIXME need disconnect() for NOTATTACHED device */
  1322. usb_set_device_state(udev, status
  1323. ? USB_STATE_NOTATTACHED
  1324. : USB_STATE_DEFAULT);
  1325. return status;
  1326. }
  1327. dev_dbg (hub->intfdev,
  1328. "port %d not enabled, trying reset again...\n",
  1329. port1);
  1330. delay = HUB_LONG_RESET_TIME;
  1331. }
  1332. dev_err (hub->intfdev,
  1333. "Cannot enable port %i. Maybe the USB cable is bad?\n",
  1334. port1);
  1335. return status;
  1336. }
  1337. #ifdef CONFIG_PM
  1338. #ifdef CONFIG_USB_SUSPEND
  1339. /*
  1340. * usb_port_suspend - suspend a usb device's upstream port
  1341. * @udev: device that's no longer in active use, not a root hub
  1342. * Context: must be able to sleep; device not locked; pm locks held
  1343. *
  1344. * Suspends a USB device that isn't in active use, conserving power.
  1345. * Devices may wake out of a suspend, if anything important happens,
  1346. * using the remote wakeup mechanism. They may also be taken out of
  1347. * suspend by the host, using usb_port_resume(). It's also routine
  1348. * to disconnect devices while they are suspended.
  1349. *
  1350. * This only affects the USB hardware for a device; its interfaces
  1351. * (and, for hubs, child devices) must already have been suspended.
  1352. *
  1353. * Selective port suspend reduces power; most suspended devices draw
  1354. * less than 500 uA. It's also used in OTG, along with remote wakeup.
  1355. * All devices below the suspended port are also suspended.
  1356. *
  1357. * Devices leave suspend state when the host wakes them up. Some devices
  1358. * also support "remote wakeup", where the device can activate the USB
  1359. * tree above them to deliver data, such as a keypress or packet. In
  1360. * some cases, this wakes the USB host.
  1361. *
  1362. * Suspending OTG devices may trigger HNP, if that's been enabled
  1363. * between a pair of dual-role devices. That will change roles, such
  1364. * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
  1365. *
  1366. * Devices on USB hub ports have only one "suspend" state, corresponding
  1367. * to ACPI D2, "may cause the device to lose some context".
  1368. * State transitions include:
  1369. *
  1370. * - suspend, resume ... when the VBUS power link stays live
  1371. * - suspend, disconnect ... VBUS lost
  1372. *
  1373. * Once VBUS drop breaks the circuit, the port it's using has to go through
  1374. * normal re-enumeration procedures, starting with enabling VBUS power.
  1375. * Other than re-initializing the hub (plug/unplug, except for root hubs),
  1376. * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
  1377. * timer, no SRP, no requests through sysfs.
  1378. *
  1379. * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
  1380. * the root hub for their bus goes into global suspend ... so we don't
  1381. * (falsely) update the device power state to say it suspended.
  1382. *
  1383. * Returns 0 on success, else negative errno.
  1384. */
  1385. int usb_port_suspend(struct usb_device *udev)
  1386. {
  1387. struct usb_hub *hub = hdev_to_hub(udev->parent);
  1388. int port1 = udev->portnum;
  1389. int status;
  1390. // dev_dbg(hub->intfdev, "suspend port %d\n", port1);
  1391. /* enable remote wakeup when appropriate; this lets the device
  1392. * wake up the upstream hub (including maybe the root hub).
  1393. *
  1394. * NOTE: OTG devices may issue remote wakeup (or SRP) even when
  1395. * we don't explicitly enable it here.
  1396. */
  1397. if (udev->do_remote_wakeup) {
  1398. status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  1399. USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
  1400. USB_DEVICE_REMOTE_WAKEUP, 0,
  1401. NULL, 0,
  1402. USB_CTRL_SET_TIMEOUT);
  1403. if (status)
  1404. dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
  1405. status);
  1406. }
  1407. /* see 7.1.7.6 */
  1408. status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
  1409. if (status) {
  1410. dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
  1411. port1, status);
  1412. /* paranoia: "should not happen" */
  1413. (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  1414. USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
  1415. USB_DEVICE_REMOTE_WAKEUP, 0,
  1416. NULL, 0,
  1417. USB_CTRL_SET_TIMEOUT);
  1418. } else {
  1419. /* device has up to 10 msec to fully suspend */
  1420. dev_dbg(&udev->dev, "usb %ssuspend\n",
  1421. udev->auto_pm ? "auto-" : "");
  1422. usb_set_device_state(udev, USB_STATE_SUSPENDED);
  1423. msleep(10);
  1424. }
  1425. return status;
  1426. }
  1427. /*
  1428. * If the USB "suspend" state is in use (rather than "global suspend"),
  1429. * many devices will be individually taken out of suspend state using
  1430. * special "resume" signaling. This routine kicks in shortly after
  1431. * hardware resume signaling is finished, either because of selective
  1432. * resume (by host) or remote wakeup (by device) ... now see what changed
  1433. * in the tree that's rooted at this device.
  1434. *
  1435. * If @udev->reset_resume is set then the device is reset before the
  1436. * status check is done.
  1437. */
  1438. static int finish_port_resume(struct usb_device *udev)
  1439. {
  1440. int status = 0;
  1441. u16 devstatus;
  1442. /* caller owns the udev device lock */
  1443. dev_dbg(&udev->dev, "finish %sresume\n",
  1444. udev->reset_resume ? "reset-" : "");
  1445. /* usb ch9 identifies four variants of SUSPENDED, based on what
  1446. * state the device resumes to. Linux currently won't see the
  1447. * first two on the host side; they'd be inside hub_port_init()
  1448. * during many timeouts, but khubd can't suspend until later.
  1449. */
  1450. usb_set_device_state(udev, udev->actconfig
  1451. ? USB_STATE_CONFIGURED
  1452. : USB_STATE_ADDRESS);
  1453. /* 10.5.4.5 says not to reset a suspended port if the attached
  1454. * device is enabled for remote wakeup. Hence the reset
  1455. * operation is carried out here, after the port has been
  1456. * resumed.
  1457. */
  1458. if (udev->reset_resume)
  1459. status = usb_reset_device(udev);
  1460. /* 10.5.4.5 says be sure devices in the tree are still there.
  1461. * For now let's assume the device didn't go crazy on resume,
  1462. * and device drivers will know about any resume quirks.
  1463. */
  1464. if (status == 0) {
  1465. devstatus = 0;
  1466. status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
  1467. if (status >= 0)
  1468. status = (status > 0 ? 0 : -ENODEV);
  1469. }
  1470. if (status) {
  1471. dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
  1472. status);
  1473. } else if (udev->actconfig) {
  1474. le16_to_cpus(&devstatus);
  1475. if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
  1476. status = usb_control_msg(udev,
  1477. usb_sndctrlpipe(udev, 0),
  1478. USB_REQ_CLEAR_FEATURE,
  1479. USB_RECIP_DEVICE,
  1480. USB_DEVICE_REMOTE_WAKEUP, 0,
  1481. NULL, 0,
  1482. USB_CTRL_SET_TIMEOUT);
  1483. if (status)
  1484. dev_dbg(&udev->dev, "disable remote "
  1485. "wakeup, status %d\n", status);
  1486. }
  1487. status = 0;
  1488. }
  1489. return status;
  1490. }
  1491. /*
  1492. * usb_port_resume - re-activate a suspended usb device's upstream port
  1493. * @udev: device to re-activate, not a root hub
  1494. * Context: must be able to sleep; device not locked; pm locks held
  1495. *
  1496. * This will re-activate the suspended device, increasing power usage
  1497. * while letting drivers communicate again with its endpoints.
  1498. * USB resume explicitly guarantees that the power session between
  1499. * the host and the device is the same as it was when the device
  1500. * suspended.
  1501. *
  1502. * If CONFIG_USB_PERSIST and @udev->reset_resume are both set then this
  1503. * routine won't check that the port is still enabled. Furthermore,
  1504. * if @udev->reset_resume is set then finish_port_resume() above will
  1505. * reset @udev. The end result is that a broken power session can be
  1506. * recovered and @udev will appear to persist across a loss of VBUS power.
  1507. *
  1508. * For example, if a host controller doesn't maintain VBUS suspend current
  1509. * during a system sleep or is reset when the system wakes up, all the USB
  1510. * power sessions below it will be broken. This is especially troublesome
  1511. * for mass-storage devices containing mounted filesystems, since the
  1512. * device will appear to have disconnected and all the memory mappings
  1513. * to it will be lost. Using the USB_PERSIST facility, the device can be
  1514. * made to appear as if it had not disconnected.
  1515. *
  1516. * This facility is inherently dangerous. Although usb_reset_device()
  1517. * makes every effort to insure that the same device is present after the
  1518. * reset as before, it cannot provide a 100% guarantee. Furthermore it's
  1519. * quite possible for a device to remain unaltered but its media to be
  1520. * changed. If the user replaces a flash memory card while the system is
  1521. * asleep, he will have only himself to blame when the filesystem on the
  1522. * new card is corrupted and the system crashes.
  1523. *
  1524. * Returns 0 on success, else negative errno.
  1525. */
  1526. int usb_port_resume(struct usb_device *udev)
  1527. {
  1528. struct usb_hub *hub = hdev_to_hub(udev->parent);
  1529. int port1 = udev->portnum;
  1530. int status;
  1531. u16 portchange, portstatus;
  1532. unsigned mask_flags, want_flags;
  1533. /* Skip the initial Clear-Suspend step for a remote wakeup */
  1534. status = hub_port_status(hub, port1, &portstatus, &portchange);
  1535. if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
  1536. goto SuspendCleared;
  1537. // dev_dbg(hub->intfdev, "resume port %d\n", port1);
  1538. set_bit(port1, hub->busy_bits);
  1539. /* see 7.1.7.7; affects power usage, but not budgeting */
  1540. status = clear_port_feature(hub->hdev,
  1541. port1, USB_PORT_FEAT_SUSPEND);
  1542. if (status) {
  1543. dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
  1544. port1, status);
  1545. } else {
  1546. /* drive resume for at least 20 msec */
  1547. dev_dbg(&udev->dev, "usb %sresume\n",
  1548. udev->auto_pm ? "auto-" : "");
  1549. msleep(25);
  1550. /* Virtual root hubs can trigger on GET_PORT_STATUS to
  1551. * stop resume signaling. Then finish the resume
  1552. * sequence.
  1553. */
  1554. status = hub_port_status(hub, port1, &portstatus, &portchange);
  1555. SuspendCleared:
  1556. if (USB_PERSIST && udev->reset_resume)
  1557. want_flags = USB_PORT_STAT_POWER
  1558. | USB_PORT_STAT_CONNECTION;
  1559. else
  1560. want_flags = USB_PORT_STAT_POWER
  1561. | USB_PORT_STAT_CONNECTION
  1562. | USB_PORT_STAT_ENABLE;
  1563. mask_flags = want_flags | USB_PORT_STAT_SUSPEND;
  1564. if (status < 0 || (portstatus & mask_flags) != want_flags) {
  1565. dev_dbg(hub->intfdev,
  1566. "port %d status %04x.%04x after resume, %d\n",
  1567. port1, portchange, portstatus, status);
  1568. if (status >= 0)
  1569. status = -ENODEV;
  1570. } else {
  1571. if (portchange & USB_PORT_STAT_C_SUSPEND)
  1572. clear_port_feature(hub->hdev, port1,
  1573. USB_PORT_FEAT_C_SUSPEND);
  1574. /* TRSMRCY = 10 msec */
  1575. msleep(10);
  1576. }
  1577. }
  1578. clear_bit(port1, hub->busy_bits);
  1579. if (!hub->hdev->parent && !hub->busy_bits[0])
  1580. usb_enable_root_hub_irq(hub->hdev->bus);
  1581. if (status == 0)
  1582. status = finish_port_resume(udev);
  1583. if (status < 0) {
  1584. dev_dbg(&udev->dev, "can't resume, status %d\n", status);
  1585. hub_port_logical_disconnect(hub, port1);
  1586. }
  1587. return status;
  1588. }
  1589. static int remote_wakeup(struct usb_device *udev)
  1590. {
  1591. int status = 0;
  1592. usb_lock_device(udev);
  1593. if (udev->state == USB_STATE_SUSPENDED) {
  1594. dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
  1595. usb_mark_last_busy(udev);
  1596. status = usb_external_resume_device(udev);
  1597. }
  1598. usb_unlock_device(udev);
  1599. return status;
  1600. }
  1601. #else /* CONFIG_USB_SUSPEND */
  1602. /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
  1603. int usb_port_suspend(struct usb_device *udev)
  1604. {
  1605. return 0;
  1606. }
  1607. int usb_port_resume(struct usb_device *udev)
  1608. {
  1609. int status = 0;
  1610. /* However we may need to do a reset-resume */
  1611. if (udev->reset_resume) {
  1612. dev_dbg(&udev->dev, "reset-resume\n");
  1613. status = usb_reset_device(udev);
  1614. }
  1615. return status;
  1616. }
  1617. static inline int remote_wakeup(struct usb_device *udev)
  1618. {
  1619. return 0;
  1620. }
  1621. #endif
  1622. static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
  1623. {
  1624. struct usb_hub *hub = usb_get_intfdata (intf);
  1625. struct usb_device *hdev = hub->hdev;
  1626. unsigned port1;
  1627. /* fail if children aren't already suspended */
  1628. for (port1 = 1; port1 <= hdev->maxchild; port1++) {
  1629. struct usb_device *udev;
  1630. udev = hdev->children [port1-1];
  1631. if (udev && msg.event == PM_EVENT_SUSPEND &&
  1632. #ifdef CONFIG_USB_SUSPEND
  1633. udev->state != USB_STATE_SUSPENDED
  1634. #else
  1635. udev->dev.power.power_state.event
  1636. == PM_EVENT_ON
  1637. #endif
  1638. ) {
  1639. if (!hdev->auto_pm)
  1640. dev_dbg(&intf->dev, "port %d nyet suspended\n",
  1641. port1);
  1642. return -EBUSY;
  1643. }
  1644. }
  1645. dev_dbg(&intf->dev, "%s\n", __FUNCTION__);
  1646. /* stop khubd and related activity */
  1647. hub_quiesce(hub);
  1648. return 0;
  1649. }
  1650. static int hub_resume(struct usb_interface *intf)
  1651. {
  1652. struct usb_hub *hub = usb_get_intfdata (intf);
  1653. dev_dbg(&intf->dev, "%s\n", __FUNCTION__);
  1654. /* tell khubd to look for changes on this hub */
  1655. hub_activate(hub);
  1656. return 0;
  1657. }
  1658. static int hub_reset_resume(struct usb_interface *intf)
  1659. {
  1660. struct usb_hub *hub = usb_get_intfdata(intf);
  1661. struct usb_device *hdev = hub->hdev;
  1662. int port1;
  1663. hub_power_on(hub);
  1664. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  1665. struct usb_device *child = hdev->children[port1-1];
  1666. if (child) {
  1667. /* For "USB_PERSIST"-enabled children we must
  1668. * mark the child device for reset-resume and
  1669. * turn off the connect-change status to prevent
  1670. * khubd from disconnecting it later.
  1671. */
  1672. if (USB_PERSIST && child->persist_enabled) {
  1673. child->reset_resume = 1;
  1674. clear_port_feature(hdev, port1,
  1675. USB_PORT_FEAT_C_CONNECTION);
  1676. /* Otherwise we must disconnect the child,
  1677. * but as we may not lock the child device here
  1678. * we have to do a "logical" disconnect.
  1679. */
  1680. } else {
  1681. hub_port_logical_disconnect(hub, port1);
  1682. }
  1683. }
  1684. }
  1685. hub_activate(hub);
  1686. return 0;
  1687. }
  1688. /**
  1689. * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
  1690. * @rhdev: struct usb_device for the root hub
  1691. *
  1692. * The USB host controller driver calls this function when its root hub
  1693. * is resumed and Vbus power has been interrupted or the controller
  1694. * has been reset. The routine marks @rhdev as having lost power. When
  1695. * the hub driver is resumed it will take notice; if CONFIG_USB_PERSIST
  1696. * is enabled then it will carry out power-session recovery, otherwise
  1697. * it will disconnect all the child devices.
  1698. */
  1699. void usb_root_hub_lost_power(struct usb_device *rhdev)
  1700. {
  1701. dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
  1702. rhdev->reset_resume = 1;
  1703. }
  1704. EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
  1705. #else /* CONFIG_PM */
  1706. static inline int remote_wakeup(struct usb_device *udev)
  1707. {
  1708. return 0;
  1709. }
  1710. #define hub_suspend NULL
  1711. #define hub_resume NULL
  1712. #define hub_reset_resume NULL
  1713. #endif
  1714. /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
  1715. *
  1716. * Between connect detection and reset signaling there must be a delay
  1717. * of 100ms at least for debounce and power-settling. The corresponding
  1718. * timer shall restart whenever the downstream port detects a disconnect.
  1719. *
  1720. * Apparently there are some bluetooth and irda-dongles and a number of
  1721. * low-speed devices for which this debounce period may last over a second.
  1722. * Not covered by the spec - but easy to deal with.
  1723. *
  1724. * This implementation uses a 1500ms total debounce timeout; if the
  1725. * connection isn't stable by then it returns -ETIMEDOUT. It checks
  1726. * every 25ms for transient disconnects. When the port status has been
  1727. * unchanged for 100ms it returns the port status.
  1728. */
  1729. #define HUB_DEBOUNCE_TIMEOUT 1500
  1730. #define HUB_DEBOUNCE_STEP 25
  1731. #define HUB_DEBOUNCE_STABLE 100
  1732. static int hub_port_debounce(struct usb_hub *hub, int port1)
  1733. {
  1734. int ret;
  1735. int total_time, stable_time = 0;
  1736. u16 portchange, portstatus;
  1737. unsigned connection = 0xffff;
  1738. for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
  1739. ret = hub_port_status(hub, port1, &portstatus, &portchange);
  1740. if (ret < 0)
  1741. return ret;
  1742. if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
  1743. (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
  1744. stable_time += HUB_DEBOUNCE_STEP;
  1745. if (stable_time >= HUB_DEBOUNCE_STABLE)
  1746. break;
  1747. } else {
  1748. stable_time = 0;
  1749. connection = portstatus & USB_PORT_STAT_CONNECTION;
  1750. }
  1751. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  1752. clear_port_feature(hub->hdev, port1,
  1753. USB_PORT_FEAT_C_CONNECTION);
  1754. }
  1755. if (total_time >= HUB_DEBOUNCE_TIMEOUT)
  1756. break;
  1757. msleep(HUB_DEBOUNCE_STEP);
  1758. }
  1759. dev_dbg (hub->intfdev,
  1760. "debounce: port %d: total %dms stable %dms status 0x%x\n",
  1761. port1, total_time, stable_time, portstatus);
  1762. if (stable_time < HUB_DEBOUNCE_STABLE)
  1763. return -ETIMEDOUT;
  1764. return portstatus;
  1765. }
  1766. static void ep0_reinit(struct usb_device *udev)
  1767. {
  1768. usb_disable_endpoint(udev, 0 + USB_DIR_IN);
  1769. usb_disable_endpoint(udev, 0 + USB_DIR_OUT);
  1770. usb_enable_endpoint(udev, &udev->ep0);
  1771. }
  1772. #define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
  1773. #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
  1774. static int hub_set_address(struct usb_device *udev, int devnum)
  1775. {
  1776. int retval;
  1777. if (devnum <= 1)
  1778. return -EINVAL;
  1779. if (udev->state == USB_STATE_ADDRESS)
  1780. return 0;
  1781. if (udev->state != USB_STATE_DEFAULT)
  1782. return -EINVAL;
  1783. retval = usb_control_msg(udev, usb_sndaddr0pipe(),
  1784. USB_REQ_SET_ADDRESS, 0, devnum, 0,
  1785. NULL, 0, USB_CTRL_SET_TIMEOUT);
  1786. if (retval == 0) {
  1787. udev->devnum = devnum; /* Device now using proper address */
  1788. usb_set_device_state(udev, USB_STATE_ADDRESS);
  1789. ep0_reinit(udev);
  1790. }
  1791. return retval;
  1792. }
  1793. /* Reset device, (re)assign address, get device descriptor.
  1794. * Device connection must be stable, no more debouncing needed.
  1795. * Returns device in USB_STATE_ADDRESS, except on error.
  1796. *
  1797. * If this is called for an already-existing device (as part of
  1798. * usb_reset_device), the caller must own the device lock. For a
  1799. * newly detected device that is not accessible through any global
  1800. * pointers, it's not necessary to lock the device.
  1801. */
  1802. static int
  1803. hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
  1804. int retry_counter)
  1805. {
  1806. static DEFINE_MUTEX(usb_address0_mutex);
  1807. struct usb_device *hdev = hub->hdev;
  1808. int i, j, retval;
  1809. unsigned delay = HUB_SHORT_RESET_TIME;
  1810. enum usb_device_speed oldspeed = udev->speed;
  1811. char *speed, *type;
  1812. int devnum = udev->devnum;
  1813. /* root hub ports have a slightly longer reset period
  1814. * (from USB 2.0 spec, section 7.1.7.5)
  1815. */
  1816. if (!hdev->parent) {
  1817. delay = HUB_ROOT_RESET_TIME;
  1818. if (port1 == hdev->bus->otg_port)
  1819. hdev->bus->b_hnp_enable = 0;
  1820. }
  1821. /* Some low speed devices have problems with the quick delay, so */
  1822. /* be a bit pessimistic with those devices. RHbug #23670 */
  1823. if (oldspeed == USB_SPEED_LOW)
  1824. delay = HUB_LONG_RESET_TIME;
  1825. mutex_lock(&usb_address0_mutex);
  1826. /* Reset the device; full speed may morph to high speed */
  1827. retval = hub_port_reset(hub, port1, udev, delay);
  1828. if (retval < 0) /* error or disconnect */
  1829. goto fail;
  1830. /* success, speed is known */
  1831. retval = -ENODEV;
  1832. if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
  1833. dev_dbg(&udev->dev, "device reset changed speed!\n");
  1834. goto fail;
  1835. }
  1836. oldspeed = udev->speed;
  1837. /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
  1838. * it's fixed size except for full speed devices.
  1839. * For Wireless USB devices, ep0 max packet is always 512 (tho
  1840. * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
  1841. */
  1842. switch (udev->speed) {
  1843. case USB_SPEED_VARIABLE: /* fixed at 512 */
  1844. udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(512);
  1845. break;
  1846. case USB_SPEED_HIGH: /* fixed at 64 */
  1847. udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
  1848. break;
  1849. case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
  1850. /* to determine the ep0 maxpacket size, try to read
  1851. * the device descriptor to get bMaxPacketSize0 and
  1852. * then correct our initial guess.
  1853. */
  1854. udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
  1855. break;
  1856. case USB_SPEED_LOW: /* fixed at 8 */
  1857. udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(8);
  1858. break;
  1859. default:
  1860. goto fail;
  1861. }
  1862. type = "";
  1863. switch (udev->speed) {
  1864. case USB_SPEED_LOW: speed = "low"; break;
  1865. case USB_SPEED_FULL: speed = "full"; break;
  1866. case USB_SPEED_HIGH: speed = "high"; break;
  1867. case USB_SPEED_VARIABLE:
  1868. speed = "variable";
  1869. type = "Wireless ";
  1870. break;
  1871. default: speed = "?"; break;
  1872. }
  1873. dev_info (&udev->dev,
  1874. "%s %s speed %sUSB device using %s and address %d\n",
  1875. (udev->config) ? "reset" : "new", speed, type,
  1876. udev->bus->controller->driver->name, devnum);
  1877. /* Set up TT records, if needed */
  1878. if (hdev->tt) {
  1879. udev->tt = hdev->tt;
  1880. udev->ttport = hdev->ttport;
  1881. } else if (udev->speed != USB_SPEED_HIGH
  1882. && hdev->speed == USB_SPEED_HIGH) {
  1883. udev->tt = &hub->tt;
  1884. udev->ttport = port1;
  1885. }
  1886. /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
  1887. * Because device hardware and firmware is sometimes buggy in
  1888. * this area, and this is how Linux has done it for ages.
  1889. * Change it cautiously.
  1890. *
  1891. * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
  1892. * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
  1893. * so it may help with some non-standards-compliant devices.
  1894. * Otherwise we start with SET_ADDRESS and then try to read the
  1895. * first 8 bytes of the device descriptor to get the ep0 maxpacket
  1896. * value.
  1897. */
  1898. for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
  1899. if (USE_NEW_SCHEME(retry_counter)) {
  1900. struct usb_device_descriptor *buf;
  1901. int r = 0;
  1902. #define GET_DESCRIPTOR_BUFSIZE 64
  1903. buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
  1904. if (!buf) {
  1905. retval = -ENOMEM;
  1906. continue;
  1907. }
  1908. /* Retry on all errors; some devices are flakey.
  1909. * 255 is for WUSB devices, we actually need to use
  1910. * 512 (WUSB1.0[4.8.1]).
  1911. */
  1912. for (j = 0; j < 3; ++j) {
  1913. buf->bMaxPacketSize0 = 0;
  1914. r = usb_control_msg(udev, usb_rcvaddr0pipe(),
  1915. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
  1916. USB_DT_DEVICE << 8, 0,
  1917. buf, GET_DESCRIPTOR_BUFSIZE,
  1918. USB_CTRL_GET_TIMEOUT);
  1919. switch (buf->bMaxPacketSize0) {
  1920. case 8: case 16: case 32: case 64: case 255:
  1921. if (buf->bDescriptorType ==
  1922. USB_DT_DEVICE) {
  1923. r = 0;
  1924. break;
  1925. }
  1926. /* FALL THROUGH */
  1927. default:
  1928. if (r == 0)
  1929. r = -EPROTO;
  1930. break;
  1931. }
  1932. if (r == 0)
  1933. break;
  1934. }
  1935. udev->descriptor.bMaxPacketSize0 =
  1936. buf->bMaxPacketSize0;
  1937. kfree(buf);
  1938. retval = hub_port_reset(hub, port1, udev, delay);
  1939. if (retval < 0) /* error or disconnect */
  1940. goto fail;
  1941. if (oldspeed != udev->speed) {
  1942. dev_dbg(&udev->dev,
  1943. "device reset changed speed!\n");
  1944. retval = -ENODEV;
  1945. goto fail;
  1946. }
  1947. if (r) {
  1948. dev_err(&udev->dev, "device descriptor "
  1949. "read/%s, error %d\n",
  1950. "64", r);
  1951. retval = -EMSGSIZE;
  1952. continue;
  1953. }
  1954. #undef GET_DESCRIPTOR_BUFSIZE
  1955. }
  1956. for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
  1957. retval = hub_set_address(udev, devnum);
  1958. if (retval >= 0)
  1959. break;
  1960. msleep(200);
  1961. }
  1962. if (retval < 0) {
  1963. dev_err(&udev->dev,
  1964. "device not accepting address %d, error %d\n",
  1965. devnum, retval);
  1966. goto fail;
  1967. }
  1968. /* cope with hardware quirkiness:
  1969. * - let SET_ADDRESS settle, some device hardware wants it
  1970. * - read ep0 maxpacket even for high and low speed,
  1971. */
  1972. msleep(10);
  1973. if (USE_NEW_SCHEME(retry_counter))
  1974. break;
  1975. retval = usb_get_device_descriptor(udev, 8);
  1976. if (retval < 8) {
  1977. dev_err(&udev->dev, "device descriptor "
  1978. "read/%s, error %d\n",
  1979. "8", retval);
  1980. if (retval >= 0)
  1981. retval = -EMSGSIZE;
  1982. } else {
  1983. retval = 0;
  1984. break;
  1985. }
  1986. }
  1987. if (retval)
  1988. goto fail;
  1989. i = udev->descriptor.bMaxPacketSize0 == 0xff?
  1990. 512 : udev->descriptor.bMaxPacketSize0;
  1991. if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
  1992. if (udev->speed != USB_SPEED_FULL ||
  1993. !(i == 8 || i == 16 || i == 32 || i == 64)) {
  1994. dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
  1995. retval = -EMSGSIZE;
  1996. goto fail;
  1997. }
  1998. dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
  1999. udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
  2000. ep0_reinit(udev);
  2001. }
  2002. retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
  2003. if (retval < (signed)sizeof(udev->descriptor)) {
  2004. dev_err(&udev->dev, "device descriptor read/%s, error %d\n",
  2005. "all", retval);
  2006. if (retval >= 0)
  2007. retval = -ENOMSG;
  2008. goto fail;
  2009. }
  2010. retval = 0;
  2011. fail:
  2012. if (retval) {
  2013. hub_port_disable(hub, port1, 0);
  2014. udev->devnum = devnum; /* for disconnect processing */
  2015. }
  2016. mutex_unlock(&usb_address0_mutex);
  2017. return retval;
  2018. }
  2019. static void
  2020. check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
  2021. {
  2022. struct usb_qualifier_descriptor *qual;
  2023. int status;
  2024. qual = kmalloc (sizeof *qual, GFP_KERNEL);
  2025. if (qual == NULL)
  2026. return;
  2027. status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
  2028. qual, sizeof *qual);
  2029. if (status == sizeof *qual) {
  2030. dev_info(&udev->dev, "not running at top speed; "
  2031. "connect to a high speed hub\n");
  2032. /* hub LEDs are probably harder to miss than syslog */
  2033. if (hub->has_indicators) {
  2034. hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
  2035. schedule_delayed_work (&hub->leds, 0);
  2036. }
  2037. }
  2038. kfree(qual);
  2039. }
  2040. static unsigned
  2041. hub_power_remaining (struct usb_hub *hub)
  2042. {
  2043. struct usb_device *hdev = hub->hdev;
  2044. int remaining;
  2045. int port1;
  2046. if (!hub->limited_power)
  2047. return 0;
  2048. remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
  2049. for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
  2050. struct usb_device *udev = hdev->children[port1 - 1];
  2051. int delta;
  2052. if (!udev)
  2053. continue;
  2054. /* Unconfigured devices may not use more than 100mA,
  2055. * or 8mA for OTG ports */
  2056. if (udev->actconfig)
  2057. delta = udev->actconfig->desc.bMaxPower * 2;
  2058. else if (port1 != udev->bus->otg_port || hdev->parent)
  2059. delta = 100;
  2060. else
  2061. delta = 8;
  2062. if (delta > hub->mA_per_port)
  2063. dev_warn(&udev->dev, "%dmA is over %umA budget "
  2064. "for port %d!\n",
  2065. delta, hub->mA_per_port, port1);
  2066. remaining -= delta;
  2067. }
  2068. if (remaining < 0) {
  2069. dev_warn(hub->intfdev, "%dmA over power budget!\n",
  2070. - remaining);
  2071. remaining = 0;
  2072. }
  2073. return remaining;
  2074. }
  2075. /* Handle physical or logical connection change events.
  2076. * This routine is called when:
  2077. * a port connection-change occurs;
  2078. * a port enable-change occurs (often caused by EMI);
  2079. * usb_reset_device() encounters changed descriptors (as from
  2080. * a firmware download)
  2081. * caller already locked the hub
  2082. */
  2083. static void hub_port_connect_change(struct usb_hub *hub, int port1,
  2084. u16 portstatus, u16 portchange)
  2085. {
  2086. struct usb_device *hdev = hub->hdev;
  2087. struct device *hub_dev = hub->intfdev;
  2088. u16 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
  2089. int status, i;
  2090. dev_dbg (hub_dev,
  2091. "port %d, status %04x, change %04x, %s\n",
  2092. port1, portstatus, portchange, portspeed (portstatus));
  2093. if (hub->has_indicators) {
  2094. set_port_led(hub, port1, HUB_LED_AUTO);
  2095. hub->indicator[port1-1] = INDICATOR_AUTO;
  2096. }
  2097. /* Disconnect any existing devices under this port */
  2098. if (hdev->children[port1-1])
  2099. usb_disconnect(&hdev->children[port1-1]);
  2100. clear_bit(port1, hub->change_bits);
  2101. #ifdef CONFIG_USB_OTG
  2102. /* during HNP, don't repeat the debounce */
  2103. if (hdev->bus->is_b_host)
  2104. portchange &= ~USB_PORT_STAT_C_CONNECTION;
  2105. #endif
  2106. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  2107. status = hub_port_debounce(hub, port1);
  2108. if (status < 0) {
  2109. if (printk_ratelimit())
  2110. dev_err (hub_dev, "connect-debounce failed, "
  2111. "port %d disabled\n", port1);
  2112. goto done;
  2113. }
  2114. portstatus = status;
  2115. }
  2116. /* Return now if nothing is connected */
  2117. if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
  2118. /* maybe switch power back on (e.g. root hub was reset) */
  2119. if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
  2120. && !(portstatus & (1 << USB_PORT_FEAT_POWER)))
  2121. set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
  2122. if (portstatus & USB_PORT_STAT_ENABLE)
  2123. goto done;
  2124. return;
  2125. }
  2126. for (i = 0; i < SET_CONFIG_TRIES; i++) {
  2127. struct usb_device *udev;
  2128. /* reallocate for each attempt, since references
  2129. * to the previous one can escape in various ways
  2130. */
  2131. udev = usb_alloc_dev(hdev, hdev->bus, port1);
  2132. if (!udev) {
  2133. dev_err (hub_dev,
  2134. "couldn't allocate port %d usb_device\n",
  2135. port1);
  2136. goto done;
  2137. }
  2138. usb_set_device_state(udev, USB_STATE_POWERED);
  2139. udev->speed = USB_SPEED_UNKNOWN;
  2140. udev->bus_mA = hub->mA_per_port;
  2141. udev->level = hdev->level + 1;
  2142. /* set the address */
  2143. choose_address(udev);
  2144. if (udev->devnum <= 0) {
  2145. status = -ENOTCONN; /* Don't retry */
  2146. goto loop;
  2147. }
  2148. /* reset and get descriptor */
  2149. status = hub_port_init(hub, udev, port1, i);
  2150. if (status < 0)
  2151. goto loop;
  2152. /* consecutive bus-powered hubs aren't reliable; they can
  2153. * violate the voltage drop budget. if the new child has
  2154. * a "powered" LED, users should notice we didn't enable it
  2155. * (without reading syslog), even without per-port LEDs
  2156. * on the parent.
  2157. */
  2158. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
  2159. && udev->bus_mA <= 100) {
  2160. u16 devstat;
  2161. status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
  2162. &devstat);
  2163. if (status < 2) {
  2164. dev_dbg(&udev->dev, "get status %d ?\n", status);
  2165. goto loop_disable;
  2166. }
  2167. le16_to_cpus(&devstat);
  2168. if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
  2169. dev_err(&udev->dev,
  2170. "can't connect bus-powered hub "
  2171. "to this port\n");
  2172. if (hub->has_indicators) {
  2173. hub->indicator[port1-1] =
  2174. INDICATOR_AMBER_BLINK;
  2175. schedule_delayed_work (&hub->leds, 0);
  2176. }
  2177. status = -ENOTCONN; /* Don't retry */
  2178. goto loop_disable;
  2179. }
  2180. }
  2181. /* check for devices running slower than they could */
  2182. if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
  2183. && udev->speed == USB_SPEED_FULL
  2184. && highspeed_hubs != 0)
  2185. check_highspeed (hub, udev, port1);
  2186. /* Store the parent's children[] pointer. At this point
  2187. * udev becomes globally accessible, although presumably
  2188. * no one will look at it until hdev is unlocked.
  2189. */
  2190. status = 0;
  2191. /* We mustn't add new devices if the parent hub has
  2192. * been disconnected; we would race with the
  2193. * recursively_mark_NOTATTACHED() routine.
  2194. */
  2195. spin_lock_irq(&device_state_lock);
  2196. if (hdev->state == USB_STATE_NOTATTACHED)
  2197. status = -ENOTCONN;
  2198. else
  2199. hdev->children[port1-1] = udev;
  2200. spin_unlock_irq(&device_state_lock);
  2201. /* Run it through the hoops (find a driver, etc) */
  2202. if (!status) {
  2203. status = usb_new_device(udev);
  2204. if (status) {
  2205. spin_lock_irq(&device_state_lock);
  2206. hdev->children[port1-1] = NULL;
  2207. spin_unlock_irq(&device_state_lock);
  2208. }
  2209. }
  2210. if (status)
  2211. goto loop_disable;
  2212. status = hub_power_remaining(hub);
  2213. if (status)
  2214. dev_dbg(hub_dev, "%dmA power budget left\n", status);
  2215. return;
  2216. loop_disable:
  2217. hub_port_disable(hub, port1, 1);
  2218. loop:
  2219. ep0_reinit(udev);
  2220. release_address(udev);
  2221. usb_put_dev(udev);
  2222. if ((status == -ENOTCONN) || (status == -ENOTSUPP))
  2223. break;
  2224. }
  2225. done:
  2226. hub_port_disable(hub, port1, 1);
  2227. }
  2228. static void hub_events(void)
  2229. {
  2230. struct list_head *tmp;
  2231. struct usb_device *hdev;
  2232. struct usb_interface *intf;
  2233. struct usb_hub *hub;
  2234. struct device *hub_dev;
  2235. u16 hubstatus;
  2236. u16 hubchange;
  2237. u16 portstatus;
  2238. u16 portchange;
  2239. int i, ret;
  2240. int connect_change;
  2241. /*
  2242. * We restart the list every time to avoid a deadlock with
  2243. * deleting hubs downstream from this one. This should be
  2244. * safe since we delete the hub from the event list.
  2245. * Not the most efficient, but avoids deadlocks.
  2246. */
  2247. while (1) {
  2248. /* Grab the first entry at the beginning of the list */
  2249. spin_lock_irq(&hub_event_lock);
  2250. if (list_empty(&hub_event_list)) {
  2251. spin_unlock_irq(&hub_event_lock);
  2252. break;
  2253. }
  2254. tmp = hub_event_list.next;
  2255. list_del_init(tmp);
  2256. hub = list_entry(tmp, struct usb_hub, event_list);
  2257. kref_get(&hub->kref);
  2258. spin_unlock_irq(&hub_event_lock);
  2259. hdev = hub->hdev;
  2260. hub_dev = hub->intfdev;
  2261. intf = to_usb_interface(hub_dev);
  2262. dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
  2263. hdev->state, hub->descriptor
  2264. ? hub->descriptor->bNbrPorts
  2265. : 0,
  2266. /* NOTE: expects max 15 ports... */
  2267. (u16) hub->change_bits[0],
  2268. (u16) hub->event_bits[0]);
  2269. /* Lock the device, then check to see if we were
  2270. * disconnected while waiting for the lock to succeed. */
  2271. usb_lock_device(hdev);
  2272. if (unlikely(hub->disconnected))
  2273. goto loop;
  2274. /* If the hub has died, clean up after it */
  2275. if (hdev->state == USB_STATE_NOTATTACHED) {
  2276. hub->error = -ENODEV;
  2277. hub_pre_reset(intf);
  2278. goto loop;
  2279. }
  2280. /* Autoresume */
  2281. ret = usb_autopm_get_interface(intf);
  2282. if (ret) {
  2283. dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
  2284. goto loop;
  2285. }
  2286. /* If this is an inactive hub, do nothing */
  2287. if (hub->quiescing)
  2288. goto loop_autopm;
  2289. if (hub->error) {
  2290. dev_dbg (hub_dev, "resetting for error %d\n",
  2291. hub->error);
  2292. ret = usb_reset_composite_device(hdev, intf);
  2293. if (ret) {
  2294. dev_dbg (hub_dev,
  2295. "error resetting hub: %d\n", ret);
  2296. goto loop_autopm;
  2297. }
  2298. hub->nerrors = 0;
  2299. hub->error = 0;
  2300. }
  2301. /* deal with port status changes */
  2302. for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
  2303. if (test_bit(i, hub->busy_bits))
  2304. continue;
  2305. connect_change = test_bit(i, hub->change_bits);
  2306. if (!test_and_clear_bit(i, hub->event_bits) &&
  2307. !connect_change && !hub->activating)
  2308. continue;
  2309. ret = hub_port_status(hub, i,
  2310. &portstatus, &portchange);
  2311. if (ret < 0)
  2312. continue;
  2313. if (hub->activating && !hdev->children[i-1] &&
  2314. (portstatus &
  2315. USB_PORT_STAT_CONNECTION))
  2316. connect_change = 1;
  2317. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  2318. clear_port_feature(hdev, i,
  2319. USB_PORT_FEAT_C_CONNECTION);
  2320. connect_change = 1;
  2321. }
  2322. if (portchange & USB_PORT_STAT_C_ENABLE) {
  2323. if (!connect_change)
  2324. dev_dbg (hub_dev,
  2325. "port %d enable change, "
  2326. "status %08x\n",
  2327. i, portstatus);
  2328. clear_port_feature(hdev, i,
  2329. USB_PORT_FEAT_C_ENABLE);
  2330. /*
  2331. * EM interference sometimes causes badly
  2332. * shielded USB devices to be shutdown by
  2333. * the hub, this hack enables them again.
  2334. * Works at least with mouse driver.
  2335. */
  2336. if (!(portstatus & USB_PORT_STAT_ENABLE)
  2337. && !connect_change
  2338. && hdev->children[i-1]) {
  2339. dev_err (hub_dev,
  2340. "port %i "
  2341. "disabled by hub (EMI?), "
  2342. "re-enabling...\n",
  2343. i);
  2344. connect_change = 1;
  2345. }
  2346. }
  2347. if (portchange & USB_PORT_STAT_C_SUSPEND) {
  2348. clear_port_feature(hdev, i,
  2349. USB_PORT_FEAT_C_SUSPEND);
  2350. if (hdev->children[i-1]) {
  2351. ret = remote_wakeup(hdev->
  2352. children[i-1]);
  2353. if (ret < 0)
  2354. connect_change = 1;
  2355. } else {
  2356. ret = -ENODEV;
  2357. hub_port_disable(hub, i, 1);
  2358. }
  2359. dev_dbg (hub_dev,
  2360. "resume on port %d, status %d\n",
  2361. i, ret);
  2362. }
  2363. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  2364. dev_err (hub_dev,
  2365. "over-current change on port %d\n",
  2366. i);
  2367. clear_port_feature(hdev, i,
  2368. USB_PORT_FEAT_C_OVER_CURRENT);
  2369. hub_power_on(hub);
  2370. }
  2371. if (portchange & USB_PORT_STAT_C_RESET) {
  2372. dev_dbg (hub_dev,
  2373. "reset change on port %d\n",
  2374. i);
  2375. clear_port_feature(hdev, i,
  2376. USB_PORT_FEAT_C_RESET);
  2377. }
  2378. if (connect_change)
  2379. hub_port_connect_change(hub, i,
  2380. portstatus, portchange);
  2381. } /* end for i */
  2382. /* deal with hub status changes */
  2383. if (test_and_clear_bit(0, hub->event_bits) == 0)
  2384. ; /* do nothing */
  2385. else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
  2386. dev_err (hub_dev, "get_hub_status failed\n");
  2387. else {
  2388. if (hubchange & HUB_CHANGE_LOCAL_POWER) {
  2389. dev_dbg (hub_dev, "power change\n");
  2390. clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
  2391. if (hubstatus & HUB_STATUS_LOCAL_POWER)
  2392. /* FIXME: Is this always true? */
  2393. hub->limited_power = 0;
  2394. else
  2395. hub->limited_power = 1;
  2396. }
  2397. if (hubchange & HUB_CHANGE_OVERCURRENT) {
  2398. dev_dbg (hub_dev, "overcurrent change\n");
  2399. msleep(500); /* Cool down */
  2400. clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
  2401. hub_power_on(hub);
  2402. }
  2403. }
  2404. hub->activating = 0;
  2405. /* If this is a root hub, tell the HCD it's okay to
  2406. * re-enable port-change interrupts now. */
  2407. if (!hdev->parent && !hub->busy_bits[0])
  2408. usb_enable_root_hub_irq(hdev->bus);
  2409. loop_autopm:
  2410. /* Allow autosuspend if we're not going to run again */
  2411. if (list_empty(&hub->event_list))
  2412. usb_autopm_enable(intf);
  2413. loop:
  2414. usb_unlock_device(hdev);
  2415. kref_put(&hub->kref, hub_release);
  2416. } /* end while (1) */
  2417. }
  2418. static int hub_thread(void *__unused)
  2419. {
  2420. set_freezable();
  2421. do {
  2422. hub_events();
  2423. wait_event_interruptible(khubd_wait,
  2424. !list_empty(&hub_event_list) ||
  2425. kthread_should_stop());
  2426. try_to_freeze();
  2427. } while (!kthread_should_stop() || !list_empty(&hub_event_list));
  2428. pr_debug("%s: khubd exiting\n", usbcore_name);
  2429. return 0;
  2430. }
  2431. static struct usb_device_id hub_id_table [] = {
  2432. { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  2433. .bDeviceClass = USB_CLASS_HUB},
  2434. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  2435. .bInterfaceClass = USB_CLASS_HUB},
  2436. { } /* Terminating entry */
  2437. };
  2438. MODULE_DEVICE_TABLE (usb, hub_id_table);
  2439. static struct usb_driver hub_driver = {
  2440. .name = "hub",
  2441. .probe = hub_probe,
  2442. .disconnect = hub_disconnect,
  2443. .suspend = hub_suspend,
  2444. .resume = hub_resume,
  2445. .reset_resume = hub_reset_resume,
  2446. .pre_reset = hub_pre_reset,
  2447. .post_reset = hub_post_reset,
  2448. .ioctl = hub_ioctl,
  2449. .id_table = hub_id_table,
  2450. .supports_autosuspend = 1,
  2451. };
  2452. int usb_hub_init(void)
  2453. {
  2454. if (usb_register(&hub_driver) < 0) {
  2455. printk(KERN_ERR "%s: can't register hub driver\n",
  2456. usbcore_name);
  2457. return -1;
  2458. }
  2459. khubd_task = kthread_run(hub_thread, NULL, "khubd");
  2460. if (!IS_ERR(khubd_task))
  2461. return 0;
  2462. /* Fall through if kernel_thread failed */
  2463. usb_deregister(&hub_driver);
  2464. printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
  2465. return -1;
  2466. }
  2467. void usb_hub_cleanup(void)
  2468. {
  2469. kthread_stop(khubd_task);
  2470. /*
  2471. * Hub resources are freed for us by usb_deregister. It calls
  2472. * usb_driver_purge on every device which in turn calls that
  2473. * devices disconnect function if it is using this driver.
  2474. * The hub_disconnect function takes care of releasing the
  2475. * individual hub resources. -greg
  2476. */
  2477. usb_deregister(&hub_driver);
  2478. } /* usb_hub_cleanup() */
  2479. static int config_descriptors_changed(struct usb_device *udev)
  2480. {
  2481. unsigned index;
  2482. unsigned len = 0;
  2483. struct usb_config_descriptor *buf;
  2484. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  2485. if (len < le16_to_cpu(udev->config[index].desc.wTotalLength))
  2486. len = le16_to_cpu(udev->config[index].desc.wTotalLength);
  2487. }
  2488. buf = kmalloc (len, GFP_KERNEL);
  2489. if (buf == NULL) {
  2490. dev_err(&udev->dev, "no mem to re-read configs after reset\n");
  2491. /* assume the worst */
  2492. return 1;
  2493. }
  2494. for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
  2495. int length;
  2496. int old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
  2497. length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
  2498. old_length);
  2499. if (length < old_length) {
  2500. dev_dbg(&udev->dev, "config index %d, error %d\n",
  2501. index, length);
  2502. break;
  2503. }
  2504. if (memcmp (buf, udev->rawdescriptors[index], old_length)
  2505. != 0) {
  2506. dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
  2507. index, buf->bConfigurationValue);
  2508. break;
  2509. }
  2510. }
  2511. kfree(buf);
  2512. return index != udev->descriptor.bNumConfigurations;
  2513. }
  2514. /**
  2515. * usb_reset_device - perform a USB port reset to reinitialize a device
  2516. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  2517. *
  2518. * WARNING - don't use this routine to reset a composite device
  2519. * (one with multiple interfaces owned by separate drivers)!
  2520. * Use usb_reset_composite_device() instead.
  2521. *
  2522. * Do a port reset, reassign the device's address, and establish its
  2523. * former operating configuration. If the reset fails, or the device's
  2524. * descriptors change from their values before the reset, or the original
  2525. * configuration and altsettings cannot be restored, a flag will be set
  2526. * telling khubd to pretend the device has been disconnected and then
  2527. * re-connected. All drivers will be unbound, and the device will be
  2528. * re-enumerated and probed all over again.
  2529. *
  2530. * Returns 0 if the reset succeeded, -ENODEV if the device has been
  2531. * flagged for logical disconnection, or some other negative error code
  2532. * if the reset wasn't even attempted.
  2533. *
  2534. * The caller must own the device lock. For example, it's safe to use
  2535. * this from a driver probe() routine after downloading new firmware.
  2536. * For calls that might not occur during probe(), drivers should lock
  2537. * the device using usb_lock_device_for_reset().
  2538. *
  2539. * Locking exception: This routine may also be called from within an
  2540. * autoresume handler. Such usage won't conflict with other tasks
  2541. * holding the device lock because these tasks should always call
  2542. * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
  2543. */
  2544. int usb_reset_device(struct usb_device *udev)
  2545. {
  2546. struct usb_device *parent_hdev = udev->parent;
  2547. struct usb_hub *parent_hub;
  2548. struct usb_device_descriptor descriptor = udev->descriptor;
  2549. int i, ret = 0;
  2550. int port1 = udev->portnum;
  2551. if (udev->state == USB_STATE_NOTATTACHED ||
  2552. udev->state == USB_STATE_SUSPENDED) {
  2553. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  2554. udev->state);
  2555. return -EINVAL;
  2556. }
  2557. if (!parent_hdev) {
  2558. /* this requires hcd-specific logic; see OHCI hc_restart() */
  2559. dev_dbg(&udev->dev, "%s for root hub!\n", __FUNCTION__);
  2560. return -EISDIR;
  2561. }
  2562. parent_hub = hdev_to_hub(parent_hdev);
  2563. set_bit(port1, parent_hub->busy_bits);
  2564. for (i = 0; i < SET_CONFIG_TRIES; ++i) {
  2565. /* ep0 maxpacket size may change; let the HCD know about it.
  2566. * Other endpoints will be handled by re-enumeration. */
  2567. ep0_reinit(udev);
  2568. ret = hub_port_init(parent_hub, udev, port1, i);
  2569. if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
  2570. break;
  2571. }
  2572. clear_bit(port1, parent_hub->busy_bits);
  2573. if (!parent_hdev->parent && !parent_hub->busy_bits[0])
  2574. usb_enable_root_hub_irq(parent_hdev->bus);
  2575. if (ret < 0)
  2576. goto re_enumerate;
  2577. /* Device might have changed firmware (DFU or similar) */
  2578. if (memcmp(&udev->descriptor, &descriptor, sizeof descriptor)
  2579. || config_descriptors_changed (udev)) {
  2580. dev_info(&udev->dev, "device firmware changed\n");
  2581. udev->descriptor = descriptor; /* for disconnect() calls */
  2582. goto re_enumerate;
  2583. }
  2584. if (!udev->actconfig)
  2585. goto done;
  2586. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  2587. USB_REQ_SET_CONFIGURATION, 0,
  2588. udev->actconfig->desc.bConfigurationValue, 0,
  2589. NULL, 0, USB_CTRL_SET_TIMEOUT);
  2590. if (ret < 0) {
  2591. dev_err(&udev->dev,
  2592. "can't restore configuration #%d (error=%d)\n",
  2593. udev->actconfig->desc.bConfigurationValue, ret);
  2594. goto re_enumerate;
  2595. }
  2596. usb_set_device_state(udev, USB_STATE_CONFIGURED);
  2597. for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
  2598. struct usb_interface *intf = udev->actconfig->interface[i];
  2599. struct usb_interface_descriptor *desc;
  2600. /* set_interface resets host side toggle even
  2601. * for altsetting zero. the interface may have no driver.
  2602. */
  2603. desc = &intf->cur_altsetting->desc;
  2604. ret = usb_set_interface(udev, desc->bInterfaceNumber,
  2605. desc->bAlternateSetting);
  2606. if (ret < 0) {
  2607. dev_err(&udev->dev, "failed to restore interface %d "
  2608. "altsetting %d (error=%d)\n",
  2609. desc->bInterfaceNumber,
  2610. desc->bAlternateSetting,
  2611. ret);
  2612. goto re_enumerate;
  2613. }
  2614. }
  2615. done:
  2616. return 0;
  2617. re_enumerate:
  2618. hub_port_logical_disconnect(parent_hub, port1);
  2619. return -ENODEV;
  2620. }
  2621. EXPORT_SYMBOL(usb_reset_device);
  2622. /**
  2623. * usb_reset_composite_device - warn interface drivers and perform a USB port reset
  2624. * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
  2625. * @iface: interface bound to the driver making the request (optional)
  2626. *
  2627. * Warns all drivers bound to registered interfaces (using their pre_reset
  2628. * method), performs the port reset, and then lets the drivers know that
  2629. * the reset is over (using their post_reset method).
  2630. *
  2631. * Return value is the same as for usb_reset_device().
  2632. *
  2633. * The caller must own the device lock. For example, it's safe to use
  2634. * this from a driver probe() routine after downloading new firmware.
  2635. * For calls that might not occur during probe(), drivers should lock
  2636. * the device using usb_lock_device_for_reset().
  2637. *
  2638. * The interface locks are acquired during the pre_reset stage and released
  2639. * during the post_reset stage. However if iface is not NULL and is
  2640. * currently being probed, we assume that the caller already owns its
  2641. * lock.
  2642. */
  2643. int usb_reset_composite_device(struct usb_device *udev,
  2644. struct usb_interface *iface)
  2645. {
  2646. int ret;
  2647. struct usb_host_config *config = udev->actconfig;
  2648. if (udev->state == USB_STATE_NOTATTACHED ||
  2649. udev->state == USB_STATE_SUSPENDED) {
  2650. dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
  2651. udev->state);
  2652. return -EINVAL;
  2653. }
  2654. /* Prevent autosuspend during the reset */
  2655. usb_autoresume_device(udev);
  2656. if (iface && iface->condition != USB_INTERFACE_BINDING)
  2657. iface = NULL;
  2658. if (config) {
  2659. int i;
  2660. struct usb_interface *cintf;
  2661. struct usb_driver *drv;
  2662. for (i = 0; i < config->desc.bNumInterfaces; ++i) {
  2663. cintf = config->interface[i];
  2664. if (cintf != iface)
  2665. down(&cintf->dev.sem);
  2666. if (device_is_registered(&cintf->dev) &&
  2667. cintf->dev.driver) {
  2668. drv = to_usb_driver(cintf->dev.driver);
  2669. if (drv->pre_reset)
  2670. (drv->pre_reset)(cintf);
  2671. /* FIXME: Unbind if pre_reset returns an error or isn't defined */
  2672. }
  2673. }
  2674. }
  2675. ret = usb_reset_device(udev);
  2676. if (config) {
  2677. int i;
  2678. struct usb_interface *cintf;
  2679. struct usb_driver *drv;
  2680. for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
  2681. cintf = config->interface[i];
  2682. if (device_is_registered(&cintf->dev) &&
  2683. cintf->dev.driver) {
  2684. drv = to_usb_driver(cintf->dev.driver);
  2685. if (drv->post_reset)
  2686. (drv->post_reset)(cintf);
  2687. /* FIXME: Unbind if post_reset returns an error or isn't defined */
  2688. }
  2689. if (cintf != iface)
  2690. up(&cintf->dev.sem);
  2691. }
  2692. }
  2693. usb_autosuspend_device(udev);
  2694. return ret;
  2695. }
  2696. EXPORT_SYMBOL(usb_reset_composite_device);