hcd.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860
  1. /*
  2. * (C) Copyright Linus Torvalds 1999
  3. * (C) Copyright Johannes Erdfelt 1999-2001
  4. * (C) Copyright Andreas Gal 1999
  5. * (C) Copyright Gregory P. Smith 1999
  6. * (C) Copyright Deti Fliegl 1999
  7. * (C) Copyright Randy Dunlap 2000
  8. * (C) Copyright David Brownell 2000-2002
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. * for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/bcd.h>
  25. #include <linux/module.h>
  26. #include <linux/version.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/completion.h>
  30. #include <linux/utsname.h>
  31. #include <linux/mm.h>
  32. #include <asm/io.h>
  33. #include <linux/device.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/mutex.h>
  36. #include <asm/irq.h>
  37. #include <asm/byteorder.h>
  38. #include <asm/unaligned.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/pm_runtime.h>
  42. #include <linux/types.h>
  43. #include <linux/usb.h>
  44. #include <linux/usb/hcd.h>
  45. #include "usb.h"
  46. /*-------------------------------------------------------------------------*/
  47. /*
  48. * USB Host Controller Driver framework
  49. *
  50. * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
  51. * HCD-specific behaviors/bugs.
  52. *
  53. * This does error checks, tracks devices and urbs, and delegates to a
  54. * "hc_driver" only for code (and data) that really needs to know about
  55. * hardware differences. That includes root hub registers, i/o queues,
  56. * and so on ... but as little else as possible.
  57. *
  58. * Shared code includes most of the "root hub" code (these are emulated,
  59. * though each HC's hardware works differently) and PCI glue, plus request
  60. * tracking overhead. The HCD code should only block on spinlocks or on
  61. * hardware handshaking; blocking on software events (such as other kernel
  62. * threads releasing resources, or completing actions) is all generic.
  63. *
  64. * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
  65. * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
  66. * only by the hub driver ... and that neither should be seen or used by
  67. * usb client device drivers.
  68. *
  69. * Contributors of ideas or unattributed patches include: David Brownell,
  70. * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
  71. *
  72. * HISTORY:
  73. * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
  74. * associated cleanup. "usb_hcd" still != "usb_bus".
  75. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  76. */
  77. /*-------------------------------------------------------------------------*/
  78. /* Keep track of which host controller drivers are loaded */
  79. unsigned long usb_hcds_loaded;
  80. EXPORT_SYMBOL_GPL(usb_hcds_loaded);
  81. /* host controllers we manage */
  82. LIST_HEAD (usb_bus_list);
  83. EXPORT_SYMBOL_GPL (usb_bus_list);
  84. /* used when allocating bus numbers */
  85. #define USB_MAXBUS 64
  86. static DECLARE_BITMAP(busmap, USB_MAXBUS);
  87. /* used when updating list of hcds */
  88. DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
  89. EXPORT_SYMBOL_GPL (usb_bus_list_lock);
  90. /* used for controlling access to virtual root hubs */
  91. static DEFINE_SPINLOCK(hcd_root_hub_lock);
  92. /* used when updating an endpoint's URB list */
  93. static DEFINE_SPINLOCK(hcd_urb_list_lock);
  94. /* used to protect against unlinking URBs after the device is gone */
  95. static DEFINE_SPINLOCK(hcd_urb_unlink_lock);
  96. /* wait queue for synchronous unlinks */
  97. DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
  98. static inline int is_root_hub(struct usb_device *udev)
  99. {
  100. return (udev->parent == NULL);
  101. }
  102. /*-------------------------------------------------------------------------*/
  103. /*
  104. * Sharable chunks of root hub code.
  105. */
  106. /*-------------------------------------------------------------------------*/
  107. #define KERNEL_REL bin2bcd(((LINUX_VERSION_CODE >> 16) & 0x0ff))
  108. #define KERNEL_VER bin2bcd(((LINUX_VERSION_CODE >> 8) & 0x0ff))
  109. /* usb 3.0 root hub device descriptor */
  110. static const u8 usb3_rh_dev_descriptor[18] = {
  111. 0x12, /* __u8 bLength; */
  112. 0x01, /* __u8 bDescriptorType; Device */
  113. 0x00, 0x03, /* __le16 bcdUSB; v3.0 */
  114. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  115. 0x00, /* __u8 bDeviceSubClass; */
  116. 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */
  117. 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */
  118. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  119. 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
  120. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  121. 0x03, /* __u8 iManufacturer; */
  122. 0x02, /* __u8 iProduct; */
  123. 0x01, /* __u8 iSerialNumber; */
  124. 0x01 /* __u8 bNumConfigurations; */
  125. };
  126. /* usb 2.5 (wireless USB 1.0) root hub device descriptor */
  127. static const u8 usb25_rh_dev_descriptor[18] = {
  128. 0x12, /* __u8 bLength; */
  129. 0x01, /* __u8 bDescriptorType; Device */
  130. 0x50, 0x02, /* __le16 bcdUSB; v2.5 */
  131. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  132. 0x00, /* __u8 bDeviceSubClass; */
  133. 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
  134. 0xFF, /* __u8 bMaxPacketSize0; always 0xFF (WUSB Spec 7.4.1). */
  135. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  136. 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
  137. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  138. 0x03, /* __u8 iManufacturer; */
  139. 0x02, /* __u8 iProduct; */
  140. 0x01, /* __u8 iSerialNumber; */
  141. 0x01 /* __u8 bNumConfigurations; */
  142. };
  143. /* usb 2.0 root hub device descriptor */
  144. static const u8 usb2_rh_dev_descriptor[18] = {
  145. 0x12, /* __u8 bLength; */
  146. 0x01, /* __u8 bDescriptorType; Device */
  147. 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
  148. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  149. 0x00, /* __u8 bDeviceSubClass; */
  150. 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
  151. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  152. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  153. 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
  154. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  155. 0x03, /* __u8 iManufacturer; */
  156. 0x02, /* __u8 iProduct; */
  157. 0x01, /* __u8 iSerialNumber; */
  158. 0x01 /* __u8 bNumConfigurations; */
  159. };
  160. /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
  161. /* usb 1.1 root hub device descriptor */
  162. static const u8 usb11_rh_dev_descriptor[18] = {
  163. 0x12, /* __u8 bLength; */
  164. 0x01, /* __u8 bDescriptorType; Device */
  165. 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
  166. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  167. 0x00, /* __u8 bDeviceSubClass; */
  168. 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
  169. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  170. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */
  171. 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
  172. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  173. 0x03, /* __u8 iManufacturer; */
  174. 0x02, /* __u8 iProduct; */
  175. 0x01, /* __u8 iSerialNumber; */
  176. 0x01 /* __u8 bNumConfigurations; */
  177. };
  178. /*-------------------------------------------------------------------------*/
  179. /* Configuration descriptors for our root hubs */
  180. static const u8 fs_rh_config_descriptor[] = {
  181. /* one configuration */
  182. 0x09, /* __u8 bLength; */
  183. 0x02, /* __u8 bDescriptorType; Configuration */
  184. 0x19, 0x00, /* __le16 wTotalLength; */
  185. 0x01, /* __u8 bNumInterfaces; (1) */
  186. 0x01, /* __u8 bConfigurationValue; */
  187. 0x00, /* __u8 iConfiguration; */
  188. 0xc0, /* __u8 bmAttributes;
  189. Bit 7: must be set,
  190. 6: Self-powered,
  191. 5: Remote wakeup,
  192. 4..0: resvd */
  193. 0x00, /* __u8 MaxPower; */
  194. /* USB 1.1:
  195. * USB 2.0, single TT organization (mandatory):
  196. * one interface, protocol 0
  197. *
  198. * USB 2.0, multiple TT organization (optional):
  199. * two interfaces, protocols 1 (like single TT)
  200. * and 2 (multiple TT mode) ... config is
  201. * sometimes settable
  202. * NOT IMPLEMENTED
  203. */
  204. /* one interface */
  205. 0x09, /* __u8 if_bLength; */
  206. 0x04, /* __u8 if_bDescriptorType; Interface */
  207. 0x00, /* __u8 if_bInterfaceNumber; */
  208. 0x00, /* __u8 if_bAlternateSetting; */
  209. 0x01, /* __u8 if_bNumEndpoints; */
  210. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  211. 0x00, /* __u8 if_bInterfaceSubClass; */
  212. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  213. 0x00, /* __u8 if_iInterface; */
  214. /* one endpoint (status change endpoint) */
  215. 0x07, /* __u8 ep_bLength; */
  216. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  217. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  218. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  219. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  220. 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  221. };
  222. static const u8 hs_rh_config_descriptor[] = {
  223. /* one configuration */
  224. 0x09, /* __u8 bLength; */
  225. 0x02, /* __u8 bDescriptorType; Configuration */
  226. 0x19, 0x00, /* __le16 wTotalLength; */
  227. 0x01, /* __u8 bNumInterfaces; (1) */
  228. 0x01, /* __u8 bConfigurationValue; */
  229. 0x00, /* __u8 iConfiguration; */
  230. 0xc0, /* __u8 bmAttributes;
  231. Bit 7: must be set,
  232. 6: Self-powered,
  233. 5: Remote wakeup,
  234. 4..0: resvd */
  235. 0x00, /* __u8 MaxPower; */
  236. /* USB 1.1:
  237. * USB 2.0, single TT organization (mandatory):
  238. * one interface, protocol 0
  239. *
  240. * USB 2.0, multiple TT organization (optional):
  241. * two interfaces, protocols 1 (like single TT)
  242. * and 2 (multiple TT mode) ... config is
  243. * sometimes settable
  244. * NOT IMPLEMENTED
  245. */
  246. /* one interface */
  247. 0x09, /* __u8 if_bLength; */
  248. 0x04, /* __u8 if_bDescriptorType; Interface */
  249. 0x00, /* __u8 if_bInterfaceNumber; */
  250. 0x00, /* __u8 if_bAlternateSetting; */
  251. 0x01, /* __u8 if_bNumEndpoints; */
  252. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  253. 0x00, /* __u8 if_bInterfaceSubClass; */
  254. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  255. 0x00, /* __u8 if_iInterface; */
  256. /* one endpoint (status change endpoint) */
  257. 0x07, /* __u8 ep_bLength; */
  258. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  259. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  260. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  261. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  262. * see hub.c:hub_configure() for details. */
  263. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  264. 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  265. };
  266. static const u8 ss_rh_config_descriptor[] = {
  267. /* one configuration */
  268. 0x09, /* __u8 bLength; */
  269. 0x02, /* __u8 bDescriptorType; Configuration */
  270. 0x1f, 0x00, /* __le16 wTotalLength; */
  271. 0x01, /* __u8 bNumInterfaces; (1) */
  272. 0x01, /* __u8 bConfigurationValue; */
  273. 0x00, /* __u8 iConfiguration; */
  274. 0xc0, /* __u8 bmAttributes;
  275. Bit 7: must be set,
  276. 6: Self-powered,
  277. 5: Remote wakeup,
  278. 4..0: resvd */
  279. 0x00, /* __u8 MaxPower; */
  280. /* one interface */
  281. 0x09, /* __u8 if_bLength; */
  282. 0x04, /* __u8 if_bDescriptorType; Interface */
  283. 0x00, /* __u8 if_bInterfaceNumber; */
  284. 0x00, /* __u8 if_bAlternateSetting; */
  285. 0x01, /* __u8 if_bNumEndpoints; */
  286. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  287. 0x00, /* __u8 if_bInterfaceSubClass; */
  288. 0x00, /* __u8 if_bInterfaceProtocol; */
  289. 0x00, /* __u8 if_iInterface; */
  290. /* one endpoint (status change endpoint) */
  291. 0x07, /* __u8 ep_bLength; */
  292. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  293. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  294. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  295. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  296. * see hub.c:hub_configure() for details. */
  297. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  298. 0x0c, /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  299. /* one SuperSpeed endpoint companion descriptor */
  300. 0x06, /* __u8 ss_bLength */
  301. 0x30, /* __u8 ss_bDescriptorType; SuperSpeed EP Companion */
  302. 0x00, /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */
  303. 0x00, /* __u8 ss_bmAttributes; 1 packet per service interval */
  304. 0x02, 0x00 /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */
  305. };
  306. /* authorized_default behaviour:
  307. * -1 is authorized for all devices except wireless (old behaviour)
  308. * 0 is unauthorized for all devices
  309. * 1 is authorized for all devices
  310. */
  311. static int authorized_default = -1;
  312. module_param(authorized_default, int, S_IRUGO|S_IWUSR);
  313. MODULE_PARM_DESC(authorized_default,
  314. "Default USB device authorization: 0 is not authorized, 1 is "
  315. "authorized, -1 is authorized except for wireless USB (default, "
  316. "old behaviour");
  317. /*-------------------------------------------------------------------------*/
  318. /**
  319. * ascii2desc() - Helper routine for producing UTF-16LE string descriptors
  320. * @s: Null-terminated ASCII (actually ISO-8859-1) string
  321. * @buf: Buffer for USB string descriptor (header + UTF-16LE)
  322. * @len: Length (in bytes; may be odd) of descriptor buffer.
  323. *
  324. * Return: The number of bytes filled in: 2 + 2*strlen(s) or @len,
  325. * whichever is less.
  326. *
  327. * Note:
  328. * USB String descriptors can contain at most 126 characters; input
  329. * strings longer than that are truncated.
  330. */
  331. static unsigned
  332. ascii2desc(char const *s, u8 *buf, unsigned len)
  333. {
  334. unsigned n, t = 2 + 2*strlen(s);
  335. if (t > 254)
  336. t = 254; /* Longest possible UTF string descriptor */
  337. if (len > t)
  338. len = t;
  339. t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */
  340. n = len;
  341. while (n--) {
  342. *buf++ = t;
  343. if (!n--)
  344. break;
  345. *buf++ = t >> 8;
  346. t = (unsigned char)*s++;
  347. }
  348. return len;
  349. }
  350. /**
  351. * rh_string() - provides string descriptors for root hub
  352. * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
  353. * @hcd: the host controller for this root hub
  354. * @data: buffer for output packet
  355. * @len: length of the provided buffer
  356. *
  357. * Produces either a manufacturer, product or serial number string for the
  358. * virtual root hub device.
  359. *
  360. * Return: The number of bytes filled in: the length of the descriptor or
  361. * of the provided buffer, whichever is less.
  362. */
  363. static unsigned
  364. rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
  365. {
  366. char buf[100];
  367. char const *s;
  368. static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
  369. /* language ids */
  370. switch (id) {
  371. case 0:
  372. /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
  373. /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
  374. if (len > 4)
  375. len = 4;
  376. memcpy(data, langids, len);
  377. return len;
  378. case 1:
  379. /* Serial number */
  380. s = hcd->self.bus_name;
  381. break;
  382. case 2:
  383. /* Product name */
  384. s = hcd->product_desc;
  385. break;
  386. case 3:
  387. /* Manufacturer */
  388. snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
  389. init_utsname()->release, hcd->driver->description);
  390. s = buf;
  391. break;
  392. default:
  393. /* Can't happen; caller guarantees it */
  394. return 0;
  395. }
  396. return ascii2desc(s, data, len);
  397. }
  398. /* Root hub control transfers execute synchronously */
  399. static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
  400. {
  401. struct usb_ctrlrequest *cmd;
  402. u16 typeReq, wValue, wIndex, wLength;
  403. u8 *ubuf = urb->transfer_buffer;
  404. unsigned len = 0;
  405. int status;
  406. u8 patch_wakeup = 0;
  407. u8 patch_protocol = 0;
  408. u16 tbuf_size;
  409. u8 *tbuf = NULL;
  410. const u8 *bufp;
  411. might_sleep();
  412. spin_lock_irq(&hcd_root_hub_lock);
  413. status = usb_hcd_link_urb_to_ep(hcd, urb);
  414. spin_unlock_irq(&hcd_root_hub_lock);
  415. if (status)
  416. return status;
  417. urb->hcpriv = hcd; /* Indicate it's queued */
  418. cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  419. typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
  420. wValue = le16_to_cpu (cmd->wValue);
  421. wIndex = le16_to_cpu (cmd->wIndex);
  422. wLength = le16_to_cpu (cmd->wLength);
  423. if (wLength > urb->transfer_buffer_length)
  424. goto error;
  425. /*
  426. * tbuf should be at least as big as the
  427. * USB hub descriptor.
  428. */
  429. tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength);
  430. tbuf = kzalloc(tbuf_size, GFP_KERNEL);
  431. if (!tbuf)
  432. return -ENOMEM;
  433. bufp = tbuf;
  434. urb->actual_length = 0;
  435. switch (typeReq) {
  436. /* DEVICE REQUESTS */
  437. /* The root hub's remote wakeup enable bit is implemented using
  438. * driver model wakeup flags. If this system supports wakeup
  439. * through USB, userspace may change the default "allow wakeup"
  440. * policy through sysfs or these calls.
  441. *
  442. * Most root hubs support wakeup from downstream devices, for
  443. * runtime power management (disabling USB clocks and reducing
  444. * VBUS power usage). However, not all of them do so; silicon,
  445. * board, and BIOS bugs here are not uncommon, so these can't
  446. * be treated quite like external hubs.
  447. *
  448. * Likewise, not all root hubs will pass wakeup events upstream,
  449. * to wake up the whole system. So don't assume root hub and
  450. * controller capabilities are identical.
  451. */
  452. case DeviceRequest | USB_REQ_GET_STATUS:
  453. tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev)
  454. << USB_DEVICE_REMOTE_WAKEUP)
  455. | (1 << USB_DEVICE_SELF_POWERED);
  456. tbuf[1] = 0;
  457. len = 2;
  458. break;
  459. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  460. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  461. device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
  462. else
  463. goto error;
  464. break;
  465. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  466. if (device_can_wakeup(&hcd->self.root_hub->dev)
  467. && wValue == USB_DEVICE_REMOTE_WAKEUP)
  468. device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
  469. else
  470. goto error;
  471. break;
  472. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  473. tbuf[0] = 1;
  474. len = 1;
  475. /* FALLTHROUGH */
  476. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  477. break;
  478. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  479. switch (wValue & 0xff00) {
  480. case USB_DT_DEVICE << 8:
  481. switch (hcd->speed) {
  482. case HCD_USB3:
  483. bufp = usb3_rh_dev_descriptor;
  484. break;
  485. case HCD_USB25:
  486. bufp = usb25_rh_dev_descriptor;
  487. break;
  488. case HCD_USB2:
  489. bufp = usb2_rh_dev_descriptor;
  490. break;
  491. case HCD_USB11:
  492. bufp = usb11_rh_dev_descriptor;
  493. break;
  494. default:
  495. goto error;
  496. }
  497. len = 18;
  498. if (hcd->has_tt)
  499. patch_protocol = 1;
  500. break;
  501. case USB_DT_CONFIG << 8:
  502. switch (hcd->speed) {
  503. case HCD_USB3:
  504. bufp = ss_rh_config_descriptor;
  505. len = sizeof ss_rh_config_descriptor;
  506. break;
  507. case HCD_USB25:
  508. case HCD_USB2:
  509. bufp = hs_rh_config_descriptor;
  510. len = sizeof hs_rh_config_descriptor;
  511. break;
  512. case HCD_USB11:
  513. bufp = fs_rh_config_descriptor;
  514. len = sizeof fs_rh_config_descriptor;
  515. break;
  516. default:
  517. goto error;
  518. }
  519. if (device_can_wakeup(&hcd->self.root_hub->dev))
  520. patch_wakeup = 1;
  521. break;
  522. case USB_DT_STRING << 8:
  523. if ((wValue & 0xff) < 4)
  524. urb->actual_length = rh_string(wValue & 0xff,
  525. hcd, ubuf, wLength);
  526. else /* unsupported IDs --> "protocol stall" */
  527. goto error;
  528. break;
  529. case USB_DT_BOS << 8:
  530. goto nongeneric;
  531. default:
  532. goto error;
  533. }
  534. break;
  535. case DeviceRequest | USB_REQ_GET_INTERFACE:
  536. tbuf[0] = 0;
  537. len = 1;
  538. /* FALLTHROUGH */
  539. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  540. break;
  541. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  542. /* wValue == urb->dev->devaddr */
  543. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  544. wValue);
  545. break;
  546. /* INTERFACE REQUESTS (no defined feature/status flags) */
  547. /* ENDPOINT REQUESTS */
  548. case EndpointRequest | USB_REQ_GET_STATUS:
  549. /* ENDPOINT_HALT flag */
  550. tbuf[0] = 0;
  551. tbuf[1] = 0;
  552. len = 2;
  553. /* FALLTHROUGH */
  554. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  555. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  556. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  557. break;
  558. /* CLASS REQUESTS (and errors) */
  559. default:
  560. nongeneric:
  561. /* non-generic request */
  562. switch (typeReq) {
  563. case GetHubStatus:
  564. case GetPortStatus:
  565. len = 4;
  566. break;
  567. case GetHubDescriptor:
  568. len = sizeof (struct usb_hub_descriptor);
  569. break;
  570. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  571. /* len is returned by hub_control */
  572. break;
  573. }
  574. status = hcd->driver->hub_control (hcd,
  575. typeReq, wValue, wIndex,
  576. tbuf, wLength);
  577. if (typeReq == GetHubDescriptor)
  578. usb_hub_adjust_deviceremovable(hcd->self.root_hub,
  579. (struct usb_hub_descriptor *)tbuf);
  580. break;
  581. error:
  582. /* "protocol stall" on error */
  583. status = -EPIPE;
  584. }
  585. if (status < 0) {
  586. len = 0;
  587. if (status != -EPIPE) {
  588. dev_dbg (hcd->self.controller,
  589. "CTRL: TypeReq=0x%x val=0x%x "
  590. "idx=0x%x len=%d ==> %d\n",
  591. typeReq, wValue, wIndex,
  592. wLength, status);
  593. }
  594. } else if (status > 0) {
  595. /* hub_control may return the length of data copied. */
  596. len = status;
  597. status = 0;
  598. }
  599. if (len) {
  600. if (urb->transfer_buffer_length < len)
  601. len = urb->transfer_buffer_length;
  602. urb->actual_length = len;
  603. /* always USB_DIR_IN, toward host */
  604. memcpy (ubuf, bufp, len);
  605. /* report whether RH hardware supports remote wakeup */
  606. if (patch_wakeup &&
  607. len > offsetof (struct usb_config_descriptor,
  608. bmAttributes))
  609. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  610. |= USB_CONFIG_ATT_WAKEUP;
  611. /* report whether RH hardware has an integrated TT */
  612. if (patch_protocol &&
  613. len > offsetof(struct usb_device_descriptor,
  614. bDeviceProtocol))
  615. ((struct usb_device_descriptor *) ubuf)->
  616. bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT;
  617. }
  618. kfree(tbuf);
  619. /* any errors get returned through the urb completion */
  620. spin_lock_irq(&hcd_root_hub_lock);
  621. usb_hcd_unlink_urb_from_ep(hcd, urb);
  622. usb_hcd_giveback_urb(hcd, urb, status);
  623. spin_unlock_irq(&hcd_root_hub_lock);
  624. return 0;
  625. }
  626. /*-------------------------------------------------------------------------*/
  627. /*
  628. * Root Hub interrupt transfers are polled using a timer if the
  629. * driver requests it; otherwise the driver is responsible for
  630. * calling usb_hcd_poll_rh_status() when an event occurs.
  631. *
  632. * Completions are called in_interrupt(), but they may or may not
  633. * be in_irq().
  634. */
  635. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  636. {
  637. struct urb *urb;
  638. int length;
  639. unsigned long flags;
  640. char buffer[6]; /* Any root hubs with > 31 ports? */
  641. if (unlikely(!hcd->rh_pollable))
  642. return;
  643. if (!hcd->uses_new_polling && !hcd->status_urb)
  644. return;
  645. length = hcd->driver->hub_status_data(hcd, buffer);
  646. if (length > 0) {
  647. /* try to complete the status urb */
  648. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  649. urb = hcd->status_urb;
  650. if (urb) {
  651. clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  652. hcd->status_urb = NULL;
  653. urb->actual_length = length;
  654. memcpy(urb->transfer_buffer, buffer, length);
  655. usb_hcd_unlink_urb_from_ep(hcd, urb);
  656. usb_hcd_giveback_urb(hcd, urb, 0);
  657. } else {
  658. length = 0;
  659. set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
  660. }
  661. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  662. }
  663. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  664. * exceed that limit if HZ is 100. The math is more clunky than
  665. * maybe expected, this is to make sure that all timers for USB devices
  666. * fire at the same time to give the CPU a break in between */
  667. if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
  668. (length == 0 && hcd->status_urb != NULL))
  669. mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  670. }
  671. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  672. /* timer callback */
  673. static void rh_timer_func (unsigned long _hcd)
  674. {
  675. usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  676. }
  677. /*-------------------------------------------------------------------------*/
  678. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  679. {
  680. int retval;
  681. unsigned long flags;
  682. unsigned len = 1 + (urb->dev->maxchild / 8);
  683. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  684. if (hcd->status_urb || urb->transfer_buffer_length < len) {
  685. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  686. retval = -EINVAL;
  687. goto done;
  688. }
  689. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  690. if (retval)
  691. goto done;
  692. hcd->status_urb = urb;
  693. urb->hcpriv = hcd; /* indicate it's queued */
  694. if (!hcd->uses_new_polling)
  695. mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  696. /* If a status change has already occurred, report it ASAP */
  697. else if (HCD_POLL_PENDING(hcd))
  698. mod_timer(&hcd->rh_timer, jiffies);
  699. retval = 0;
  700. done:
  701. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  702. return retval;
  703. }
  704. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  705. {
  706. if (usb_endpoint_xfer_int(&urb->ep->desc))
  707. return rh_queue_status (hcd, urb);
  708. if (usb_endpoint_xfer_control(&urb->ep->desc))
  709. return rh_call_control (hcd, urb);
  710. return -EINVAL;
  711. }
  712. /*-------------------------------------------------------------------------*/
  713. /* Unlinks of root-hub control URBs are legal, but they don't do anything
  714. * since these URBs always execute synchronously.
  715. */
  716. static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  717. {
  718. unsigned long flags;
  719. int rc;
  720. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  721. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  722. if (rc)
  723. goto done;
  724. if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
  725. ; /* Do nothing */
  726. } else { /* Status URB */
  727. if (!hcd->uses_new_polling)
  728. del_timer (&hcd->rh_timer);
  729. if (urb == hcd->status_urb) {
  730. hcd->status_urb = NULL;
  731. usb_hcd_unlink_urb_from_ep(hcd, urb);
  732. usb_hcd_giveback_urb(hcd, urb, status);
  733. }
  734. }
  735. done:
  736. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  737. return rc;
  738. }
  739. /*
  740. * Show & store the current value of authorized_default
  741. */
  742. static ssize_t authorized_default_show(struct device *dev,
  743. struct device_attribute *attr, char *buf)
  744. {
  745. struct usb_device *rh_usb_dev = to_usb_device(dev);
  746. struct usb_bus *usb_bus = rh_usb_dev->bus;
  747. struct usb_hcd *usb_hcd;
  748. if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
  749. return -ENODEV;
  750. usb_hcd = bus_to_hcd(usb_bus);
  751. return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default);
  752. }
  753. static ssize_t authorized_default_store(struct device *dev,
  754. struct device_attribute *attr,
  755. const char *buf, size_t size)
  756. {
  757. ssize_t result;
  758. unsigned val;
  759. struct usb_device *rh_usb_dev = to_usb_device(dev);
  760. struct usb_bus *usb_bus = rh_usb_dev->bus;
  761. struct usb_hcd *usb_hcd;
  762. if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
  763. return -ENODEV;
  764. usb_hcd = bus_to_hcd(usb_bus);
  765. result = sscanf(buf, "%u\n", &val);
  766. if (result == 1) {
  767. usb_hcd->authorized_default = val ? 1 : 0;
  768. result = size;
  769. } else {
  770. result = -EINVAL;
  771. }
  772. return result;
  773. }
  774. static DEVICE_ATTR_RW(authorized_default);
  775. /* Group all the USB bus attributes */
  776. static struct attribute *usb_bus_attrs[] = {
  777. &dev_attr_authorized_default.attr,
  778. NULL,
  779. };
  780. static struct attribute_group usb_bus_attr_group = {
  781. .name = NULL, /* we want them in the same directory */
  782. .attrs = usb_bus_attrs,
  783. };
  784. /*-------------------------------------------------------------------------*/
  785. /**
  786. * usb_bus_init - shared initialization code
  787. * @bus: the bus structure being initialized
  788. *
  789. * This code is used to initialize a usb_bus structure, memory for which is
  790. * separately managed.
  791. */
  792. static void usb_bus_init (struct usb_bus *bus)
  793. {
  794. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  795. bus->devnum_next = 1;
  796. bus->root_hub = NULL;
  797. bus->busnum = -1;
  798. bus->bandwidth_allocated = 0;
  799. bus->bandwidth_int_reqs = 0;
  800. bus->bandwidth_isoc_reqs = 0;
  801. INIT_LIST_HEAD (&bus->bus_list);
  802. }
  803. /*-------------------------------------------------------------------------*/
  804. /**
  805. * usb_register_bus - registers the USB host controller with the usb core
  806. * @bus: pointer to the bus to register
  807. * Context: !in_interrupt()
  808. *
  809. * Assigns a bus number, and links the controller into usbcore data
  810. * structures so that it can be seen by scanning the bus list.
  811. *
  812. * Return: 0 if successful. A negative error code otherwise.
  813. */
  814. static int usb_register_bus(struct usb_bus *bus)
  815. {
  816. int result = -E2BIG;
  817. int busnum;
  818. mutex_lock(&usb_bus_list_lock);
  819. busnum = find_next_zero_bit(busmap, USB_MAXBUS, 1);
  820. if (busnum >= USB_MAXBUS) {
  821. printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  822. goto error_find_busnum;
  823. }
  824. set_bit(busnum, busmap);
  825. bus->busnum = busnum;
  826. /* Add it to the local list of buses */
  827. list_add (&bus->bus_list, &usb_bus_list);
  828. mutex_unlock(&usb_bus_list_lock);
  829. usb_notify_add_bus(bus);
  830. dev_info (bus->controller, "new USB bus registered, assigned bus "
  831. "number %d\n", bus->busnum);
  832. return 0;
  833. error_find_busnum:
  834. mutex_unlock(&usb_bus_list_lock);
  835. return result;
  836. }
  837. /**
  838. * usb_deregister_bus - deregisters the USB host controller
  839. * @bus: pointer to the bus to deregister
  840. * Context: !in_interrupt()
  841. *
  842. * Recycles the bus number, and unlinks the controller from usbcore data
  843. * structures so that it won't be seen by scanning the bus list.
  844. */
  845. static void usb_deregister_bus (struct usb_bus *bus)
  846. {
  847. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  848. /*
  849. * NOTE: make sure that all the devices are removed by the
  850. * controller code, as well as having it call this when cleaning
  851. * itself up
  852. */
  853. mutex_lock(&usb_bus_list_lock);
  854. list_del (&bus->bus_list);
  855. mutex_unlock(&usb_bus_list_lock);
  856. usb_notify_remove_bus(bus);
  857. clear_bit(bus->busnum, busmap);
  858. }
  859. /**
  860. * register_root_hub - called by usb_add_hcd() to register a root hub
  861. * @hcd: host controller for this root hub
  862. *
  863. * This function registers the root hub with the USB subsystem. It sets up
  864. * the device properly in the device tree and then calls usb_new_device()
  865. * to register the usb device. It also assigns the root hub's USB address
  866. * (always 1).
  867. *
  868. * Return: 0 if successful. A negative error code otherwise.
  869. */
  870. static int register_root_hub(struct usb_hcd *hcd)
  871. {
  872. struct device *parent_dev = hcd->self.controller;
  873. struct usb_device *usb_dev = hcd->self.root_hub;
  874. const int devnum = 1;
  875. int retval;
  876. usb_dev->devnum = devnum;
  877. usb_dev->bus->devnum_next = devnum + 1;
  878. memset (&usb_dev->bus->devmap.devicemap, 0,
  879. sizeof usb_dev->bus->devmap.devicemap);
  880. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  881. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  882. mutex_lock(&usb_bus_list_lock);
  883. usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
  884. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  885. if (retval != sizeof usb_dev->descriptor) {
  886. mutex_unlock(&usb_bus_list_lock);
  887. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  888. dev_name(&usb_dev->dev), retval);
  889. return (retval < 0) ? retval : -EMSGSIZE;
  890. }
  891. if (usb_dev->speed == USB_SPEED_SUPER) {
  892. retval = usb_get_bos_descriptor(usb_dev);
  893. if (retval < 0) {
  894. mutex_unlock(&usb_bus_list_lock);
  895. dev_dbg(parent_dev, "can't read %s bos descriptor %d\n",
  896. dev_name(&usb_dev->dev), retval);
  897. return retval;
  898. }
  899. usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev);
  900. }
  901. retval = usb_new_device (usb_dev);
  902. if (retval) {
  903. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  904. dev_name(&usb_dev->dev), retval);
  905. } else {
  906. spin_lock_irq (&hcd_root_hub_lock);
  907. hcd->rh_registered = 1;
  908. spin_unlock_irq (&hcd_root_hub_lock);
  909. /* Did the HC die before the root hub was registered? */
  910. if (HCD_DEAD(hcd))
  911. usb_hc_died (hcd); /* This time clean up */
  912. }
  913. mutex_unlock(&usb_bus_list_lock);
  914. return retval;
  915. }
  916. /*
  917. * usb_hcd_start_port_resume - a root-hub port is sending a resume signal
  918. * @bus: the bus which the root hub belongs to
  919. * @portnum: the port which is being resumed
  920. *
  921. * HCDs should call this function when they know that a resume signal is
  922. * being sent to a root-hub port. The root hub will be prevented from
  923. * going into autosuspend until usb_hcd_end_port_resume() is called.
  924. *
  925. * The bus's private lock must be held by the caller.
  926. */
  927. void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum)
  928. {
  929. unsigned bit = 1 << portnum;
  930. if (!(bus->resuming_ports & bit)) {
  931. bus->resuming_ports |= bit;
  932. pm_runtime_get_noresume(&bus->root_hub->dev);
  933. }
  934. }
  935. EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume);
  936. /*
  937. * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal
  938. * @bus: the bus which the root hub belongs to
  939. * @portnum: the port which is being resumed
  940. *
  941. * HCDs should call this function when they know that a resume signal has
  942. * stopped being sent to a root-hub port. The root hub will be allowed to
  943. * autosuspend again.
  944. *
  945. * The bus's private lock must be held by the caller.
  946. */
  947. void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum)
  948. {
  949. unsigned bit = 1 << portnum;
  950. if (bus->resuming_ports & bit) {
  951. bus->resuming_ports &= ~bit;
  952. pm_runtime_put_noidle(&bus->root_hub->dev);
  953. }
  954. }
  955. EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume);
  956. /*-------------------------------------------------------------------------*/
  957. /**
  958. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  959. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  960. * @is_input: true iff the transaction sends data to the host
  961. * @isoc: true for isochronous transactions, false for interrupt ones
  962. * @bytecount: how many bytes in the transaction.
  963. *
  964. * Return: Approximate bus time in nanoseconds for a periodic transaction.
  965. *
  966. * Note:
  967. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  968. * scheduled in software, this function is only used for such scheduling.
  969. */
  970. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  971. {
  972. unsigned long tmp;
  973. switch (speed) {
  974. case USB_SPEED_LOW: /* INTR only */
  975. if (is_input) {
  976. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  977. return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  978. } else {
  979. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  980. return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  981. }
  982. case USB_SPEED_FULL: /* ISOC or INTR */
  983. if (isoc) {
  984. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  985. return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
  986. } else {
  987. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  988. return 9107L + BW_HOST_DELAY + tmp;
  989. }
  990. case USB_SPEED_HIGH: /* ISOC or INTR */
  991. /* FIXME adjust for input vs output */
  992. if (isoc)
  993. tmp = HS_NSECS_ISO (bytecount);
  994. else
  995. tmp = HS_NSECS (bytecount);
  996. return tmp;
  997. default:
  998. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  999. return -1;
  1000. }
  1001. }
  1002. EXPORT_SYMBOL_GPL(usb_calc_bus_time);
  1003. /*-------------------------------------------------------------------------*/
  1004. /*
  1005. * Generic HC operations.
  1006. */
  1007. /*-------------------------------------------------------------------------*/
  1008. /**
  1009. * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
  1010. * @hcd: host controller to which @urb was submitted
  1011. * @urb: URB being submitted
  1012. *
  1013. * Host controller drivers should call this routine in their enqueue()
  1014. * method. The HCD's private spinlock must be held and interrupts must
  1015. * be disabled. The actions carried out here are required for URB
  1016. * submission, as well as for endpoint shutdown and for usb_kill_urb.
  1017. *
  1018. * Return: 0 for no error, otherwise a negative error code (in which case
  1019. * the enqueue() method must fail). If no error occurs but enqueue() fails
  1020. * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
  1021. * the private spinlock and returning.
  1022. */
  1023. int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
  1024. {
  1025. int rc = 0;
  1026. spin_lock(&hcd_urb_list_lock);
  1027. /* Check that the URB isn't being killed */
  1028. if (unlikely(atomic_read(&urb->reject))) {
  1029. rc = -EPERM;
  1030. goto done;
  1031. }
  1032. if (unlikely(!urb->ep->enabled)) {
  1033. rc = -ENOENT;
  1034. goto done;
  1035. }
  1036. if (unlikely(!urb->dev->can_submit)) {
  1037. rc = -EHOSTUNREACH;
  1038. goto done;
  1039. }
  1040. /*
  1041. * Check the host controller's state and add the URB to the
  1042. * endpoint's queue.
  1043. */
  1044. if (HCD_RH_RUNNING(hcd)) {
  1045. urb->unlinked = 0;
  1046. list_add_tail(&urb->urb_list, &urb->ep->urb_list);
  1047. } else {
  1048. rc = -ESHUTDOWN;
  1049. goto done;
  1050. }
  1051. done:
  1052. spin_unlock(&hcd_urb_list_lock);
  1053. return rc;
  1054. }
  1055. EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
  1056. /**
  1057. * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
  1058. * @hcd: host controller to which @urb was submitted
  1059. * @urb: URB being checked for unlinkability
  1060. * @status: error code to store in @urb if the unlink succeeds
  1061. *
  1062. * Host controller drivers should call this routine in their dequeue()
  1063. * method. The HCD's private spinlock must be held and interrupts must
  1064. * be disabled. The actions carried out here are required for making
  1065. * sure than an unlink is valid.
  1066. *
  1067. * Return: 0 for no error, otherwise a negative error code (in which case
  1068. * the dequeue() method must fail). The possible error codes are:
  1069. *
  1070. * -EIDRM: @urb was not submitted or has already completed.
  1071. * The completion function may not have been called yet.
  1072. *
  1073. * -EBUSY: @urb has already been unlinked.
  1074. */
  1075. int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
  1076. int status)
  1077. {
  1078. struct list_head *tmp;
  1079. /* insist the urb is still queued */
  1080. list_for_each(tmp, &urb->ep->urb_list) {
  1081. if (tmp == &urb->urb_list)
  1082. break;
  1083. }
  1084. if (tmp != &urb->urb_list)
  1085. return -EIDRM;
  1086. /* Any status except -EINPROGRESS means something already started to
  1087. * unlink this URB from the hardware. So there's no more work to do.
  1088. */
  1089. if (urb->unlinked)
  1090. return -EBUSY;
  1091. urb->unlinked = status;
  1092. return 0;
  1093. }
  1094. EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
  1095. /**
  1096. * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  1097. * @hcd: host controller to which @urb was submitted
  1098. * @urb: URB being unlinked
  1099. *
  1100. * Host controller drivers should call this routine before calling
  1101. * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  1102. * interrupts must be disabled. The actions carried out here are required
  1103. * for URB completion.
  1104. */
  1105. void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
  1106. {
  1107. /* clear all state linking urb to this dev (and hcd) */
  1108. spin_lock(&hcd_urb_list_lock);
  1109. list_del_init(&urb->urb_list);
  1110. spin_unlock(&hcd_urb_list_lock);
  1111. }
  1112. EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
  1113. /*
  1114. * Some usb host controllers can only perform dma using a small SRAM area.
  1115. * The usb core itself is however optimized for host controllers that can dma
  1116. * using regular system memory - like pci devices doing bus mastering.
  1117. *
  1118. * To support host controllers with limited dma capabilites we provide dma
  1119. * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
  1120. * For this to work properly the host controller code must first use the
  1121. * function dma_declare_coherent_memory() to point out which memory area
  1122. * that should be used for dma allocations.
  1123. *
  1124. * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
  1125. * dma using dma_alloc_coherent() which in turn allocates from the memory
  1126. * area pointed out with dma_declare_coherent_memory().
  1127. *
  1128. * So, to summarize...
  1129. *
  1130. * - We need "local" memory, canonical example being
  1131. * a small SRAM on a discrete controller being the
  1132. * only memory that the controller can read ...
  1133. * (a) "normal" kernel memory is no good, and
  1134. * (b) there's not enough to share
  1135. *
  1136. * - The only *portable* hook for such stuff in the
  1137. * DMA framework is dma_declare_coherent_memory()
  1138. *
  1139. * - So we use that, even though the primary requirement
  1140. * is that the memory be "local" (hence addressible
  1141. * by that device), not "coherent".
  1142. *
  1143. */
  1144. static int hcd_alloc_coherent(struct usb_bus *bus,
  1145. gfp_t mem_flags, dma_addr_t *dma_handle,
  1146. void **vaddr_handle, size_t size,
  1147. enum dma_data_direction dir)
  1148. {
  1149. unsigned char *vaddr;
  1150. if (*vaddr_handle == NULL) {
  1151. WARN_ON_ONCE(1);
  1152. return -EFAULT;
  1153. }
  1154. vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
  1155. mem_flags, dma_handle);
  1156. if (!vaddr)
  1157. return -ENOMEM;
  1158. /*
  1159. * Store the virtual address of the buffer at the end
  1160. * of the allocated dma buffer. The size of the buffer
  1161. * may be uneven so use unaligned functions instead
  1162. * of just rounding up. It makes sense to optimize for
  1163. * memory footprint over access speed since the amount
  1164. * of memory available for dma may be limited.
  1165. */
  1166. put_unaligned((unsigned long)*vaddr_handle,
  1167. (unsigned long *)(vaddr + size));
  1168. if (dir == DMA_TO_DEVICE)
  1169. memcpy(vaddr, *vaddr_handle, size);
  1170. *vaddr_handle = vaddr;
  1171. return 0;
  1172. }
  1173. static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
  1174. void **vaddr_handle, size_t size,
  1175. enum dma_data_direction dir)
  1176. {
  1177. unsigned char *vaddr = *vaddr_handle;
  1178. vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
  1179. if (dir == DMA_FROM_DEVICE)
  1180. memcpy(vaddr, *vaddr_handle, size);
  1181. hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
  1182. *vaddr_handle = vaddr;
  1183. *dma_handle = 0;
  1184. }
  1185. void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1186. {
  1187. if (urb->transfer_flags & URB_SETUP_MAP_SINGLE)
  1188. dma_unmap_single(hcd->self.controller,
  1189. urb->setup_dma,
  1190. sizeof(struct usb_ctrlrequest),
  1191. DMA_TO_DEVICE);
  1192. else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL)
  1193. hcd_free_coherent(urb->dev->bus,
  1194. &urb->setup_dma,
  1195. (void **) &urb->setup_packet,
  1196. sizeof(struct usb_ctrlrequest),
  1197. DMA_TO_DEVICE);
  1198. /* Make it safe to call this routine more than once */
  1199. urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL);
  1200. }
  1201. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma);
  1202. static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1203. {
  1204. if (hcd->driver->unmap_urb_for_dma)
  1205. hcd->driver->unmap_urb_for_dma(hcd, urb);
  1206. else
  1207. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1208. }
  1209. void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1210. {
  1211. enum dma_data_direction dir;
  1212. usb_hcd_unmap_urb_setup_for_dma(hcd, urb);
  1213. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1214. if (urb->transfer_flags & URB_DMA_MAP_SG)
  1215. dma_unmap_sg(hcd->self.controller,
  1216. urb->sg,
  1217. urb->num_sgs,
  1218. dir);
  1219. else if (urb->transfer_flags & URB_DMA_MAP_PAGE)
  1220. dma_unmap_page(hcd->self.controller,
  1221. urb->transfer_dma,
  1222. urb->transfer_buffer_length,
  1223. dir);
  1224. else if (urb->transfer_flags & URB_DMA_MAP_SINGLE)
  1225. dma_unmap_single(hcd->self.controller,
  1226. urb->transfer_dma,
  1227. urb->transfer_buffer_length,
  1228. dir);
  1229. else if (urb->transfer_flags & URB_MAP_LOCAL)
  1230. hcd_free_coherent(urb->dev->bus,
  1231. &urb->transfer_dma,
  1232. &urb->transfer_buffer,
  1233. urb->transfer_buffer_length,
  1234. dir);
  1235. /* Make it safe to call this routine more than once */
  1236. urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
  1237. URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
  1238. }
  1239. EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma);
  1240. static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1241. gfp_t mem_flags)
  1242. {
  1243. if (hcd->driver->map_urb_for_dma)
  1244. return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags);
  1245. else
  1246. return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
  1247. }
  1248. int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1249. gfp_t mem_flags)
  1250. {
  1251. enum dma_data_direction dir;
  1252. int ret = 0;
  1253. /* Map the URB's buffers for DMA access.
  1254. * Lower level HCD code should use *_dma exclusively,
  1255. * unless it uses pio or talks to another transport,
  1256. * or uses the provided scatter gather list for bulk.
  1257. */
  1258. if (usb_endpoint_xfer_control(&urb->ep->desc)) {
  1259. if (hcd->self.uses_pio_for_control)
  1260. return ret;
  1261. if (hcd->self.uses_dma) {
  1262. urb->setup_dma = dma_map_single(
  1263. hcd->self.controller,
  1264. urb->setup_packet,
  1265. sizeof(struct usb_ctrlrequest),
  1266. DMA_TO_DEVICE);
  1267. if (dma_mapping_error(hcd->self.controller,
  1268. urb->setup_dma))
  1269. return -EAGAIN;
  1270. urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
  1271. } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1272. ret = hcd_alloc_coherent(
  1273. urb->dev->bus, mem_flags,
  1274. &urb->setup_dma,
  1275. (void **)&urb->setup_packet,
  1276. sizeof(struct usb_ctrlrequest),
  1277. DMA_TO_DEVICE);
  1278. if (ret)
  1279. return ret;
  1280. urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
  1281. }
  1282. }
  1283. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1284. if (urb->transfer_buffer_length != 0
  1285. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1286. if (hcd->self.uses_dma) {
  1287. if (urb->num_sgs) {
  1288. int n;
  1289. /* We don't support sg for isoc transfers ! */
  1290. if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
  1291. WARN_ON(1);
  1292. return -EINVAL;
  1293. }
  1294. n = dma_map_sg(
  1295. hcd->self.controller,
  1296. urb->sg,
  1297. urb->num_sgs,
  1298. dir);
  1299. if (n <= 0)
  1300. ret = -EAGAIN;
  1301. else
  1302. urb->transfer_flags |= URB_DMA_MAP_SG;
  1303. urb->num_mapped_sgs = n;
  1304. if (n != urb->num_sgs)
  1305. urb->transfer_flags |=
  1306. URB_DMA_SG_COMBINED;
  1307. } else if (urb->sg) {
  1308. struct scatterlist *sg = urb->sg;
  1309. urb->transfer_dma = dma_map_page(
  1310. hcd->self.controller,
  1311. sg_page(sg),
  1312. sg->offset,
  1313. urb->transfer_buffer_length,
  1314. dir);
  1315. if (dma_mapping_error(hcd->self.controller,
  1316. urb->transfer_dma))
  1317. ret = -EAGAIN;
  1318. else
  1319. urb->transfer_flags |= URB_DMA_MAP_PAGE;
  1320. } else {
  1321. urb->transfer_dma = dma_map_single(
  1322. hcd->self.controller,
  1323. urb->transfer_buffer,
  1324. urb->transfer_buffer_length,
  1325. dir);
  1326. if (dma_mapping_error(hcd->self.controller,
  1327. urb->transfer_dma))
  1328. ret = -EAGAIN;
  1329. else
  1330. urb->transfer_flags |= URB_DMA_MAP_SINGLE;
  1331. }
  1332. } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1333. ret = hcd_alloc_coherent(
  1334. urb->dev->bus, mem_flags,
  1335. &urb->transfer_dma,
  1336. &urb->transfer_buffer,
  1337. urb->transfer_buffer_length,
  1338. dir);
  1339. if (ret == 0)
  1340. urb->transfer_flags |= URB_MAP_LOCAL;
  1341. }
  1342. if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
  1343. URB_SETUP_MAP_LOCAL)))
  1344. usb_hcd_unmap_urb_for_dma(hcd, urb);
  1345. }
  1346. return ret;
  1347. }
  1348. EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
  1349. /*-------------------------------------------------------------------------*/
  1350. /* may be called in any context with a valid urb->dev usecount
  1351. * caller surrenders "ownership" of urb
  1352. * expects usb_submit_urb() to have sanity checked and conditioned all
  1353. * inputs in the urb
  1354. */
  1355. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  1356. {
  1357. int status;
  1358. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1359. /* increment urb's reference count as part of giving it to the HCD
  1360. * (which will control it). HCD guarantees that it either returns
  1361. * an error or calls giveback(), but not both.
  1362. */
  1363. usb_get_urb(urb);
  1364. atomic_inc(&urb->use_count);
  1365. atomic_inc(&urb->dev->urbnum);
  1366. usbmon_urb_submit(&hcd->self, urb);
  1367. /* NOTE requirements on root-hub callers (usbfs and the hub
  1368. * driver, for now): URBs' urb->transfer_buffer must be
  1369. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1370. * they could clobber root hub response data. Also, control
  1371. * URBs must be submitted in process context with interrupts
  1372. * enabled.
  1373. */
  1374. if (is_root_hub(urb->dev)) {
  1375. status = rh_urb_enqueue(hcd, urb);
  1376. } else {
  1377. status = map_urb_for_dma(hcd, urb, mem_flags);
  1378. if (likely(status == 0)) {
  1379. status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
  1380. if (unlikely(status))
  1381. unmap_urb_for_dma(hcd, urb);
  1382. }
  1383. }
  1384. if (unlikely(status)) {
  1385. usbmon_urb_submit_error(&hcd->self, urb, status);
  1386. urb->hcpriv = NULL;
  1387. INIT_LIST_HEAD(&urb->urb_list);
  1388. atomic_dec(&urb->use_count);
  1389. atomic_dec(&urb->dev->urbnum);
  1390. if (atomic_read(&urb->reject))
  1391. wake_up(&usb_kill_urb_queue);
  1392. usb_put_urb(urb);
  1393. }
  1394. return status;
  1395. }
  1396. /*-------------------------------------------------------------------------*/
  1397. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1398. * off hardware queues (which may take a while) and returning it as
  1399. * soon as practical. we've already set up the urb's return status,
  1400. * but we can't know if the callback completed already.
  1401. */
  1402. static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
  1403. {
  1404. int value;
  1405. if (is_root_hub(urb->dev))
  1406. value = usb_rh_urb_dequeue(hcd, urb, status);
  1407. else {
  1408. /* The only reason an HCD might fail this call is if
  1409. * it has not yet fully queued the urb to begin with.
  1410. * Such failures should be harmless. */
  1411. value = hcd->driver->urb_dequeue(hcd, urb, status);
  1412. }
  1413. return value;
  1414. }
  1415. /*
  1416. * called in any context
  1417. *
  1418. * caller guarantees urb won't be recycled till both unlink()
  1419. * and the urb's completion function return
  1420. */
  1421. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1422. {
  1423. struct usb_hcd *hcd;
  1424. int retval = -EIDRM;
  1425. unsigned long flags;
  1426. /* Prevent the device and bus from going away while
  1427. * the unlink is carried out. If they are already gone
  1428. * then urb->use_count must be 0, since disconnected
  1429. * devices can't have any active URBs.
  1430. */
  1431. spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
  1432. if (atomic_read(&urb->use_count) > 0) {
  1433. retval = 0;
  1434. usb_get_dev(urb->dev);
  1435. }
  1436. spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
  1437. if (retval == 0) {
  1438. hcd = bus_to_hcd(urb->dev->bus);
  1439. retval = unlink1(hcd, urb, status);
  1440. usb_put_dev(urb->dev);
  1441. }
  1442. if (retval == 0)
  1443. retval = -EINPROGRESS;
  1444. else if (retval != -EIDRM && retval != -EBUSY)
  1445. dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
  1446. urb, retval);
  1447. return retval;
  1448. }
  1449. /*-------------------------------------------------------------------------*/
  1450. static void __usb_hcd_giveback_urb(struct urb *urb)
  1451. {
  1452. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1453. struct usb_anchor *anchor = urb->anchor;
  1454. int status = urb->unlinked;
  1455. unsigned long flags;
  1456. urb->hcpriv = NULL;
  1457. if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1458. urb->actual_length < urb->transfer_buffer_length &&
  1459. !status))
  1460. status = -EREMOTEIO;
  1461. unmap_urb_for_dma(hcd, urb);
  1462. usbmon_urb_complete(&hcd->self, urb, status);
  1463. usb_anchor_suspend_wakeups(anchor);
  1464. usb_unanchor_urb(urb);
  1465. /* pass ownership to the completion handler */
  1466. urb->status = status;
  1467. /*
  1468. * We disable local IRQs here avoid possible deadlock because
  1469. * drivers may call spin_lock() to hold lock which might be
  1470. * acquired in one hard interrupt handler.
  1471. *
  1472. * The local_irq_save()/local_irq_restore() around complete()
  1473. * will be removed if current USB drivers have been cleaned up
  1474. * and no one may trigger the above deadlock situation when
  1475. * running complete() in tasklet.
  1476. */
  1477. local_irq_save(flags);
  1478. urb->complete(urb);
  1479. local_irq_restore(flags);
  1480. usb_anchor_resume_wakeups(anchor);
  1481. atomic_dec(&urb->use_count);
  1482. if (unlikely(atomic_read(&urb->reject)))
  1483. wake_up(&usb_kill_urb_queue);
  1484. usb_put_urb(urb);
  1485. }
  1486. static void usb_giveback_urb_bh(unsigned long param)
  1487. {
  1488. struct giveback_urb_bh *bh = (struct giveback_urb_bh *)param;
  1489. struct list_head local_list;
  1490. spin_lock_irq(&bh->lock);
  1491. bh->running = true;
  1492. restart:
  1493. list_replace_init(&bh->head, &local_list);
  1494. spin_unlock_irq(&bh->lock);
  1495. while (!list_empty(&local_list)) {
  1496. struct urb *urb;
  1497. urb = list_entry(local_list.next, struct urb, urb_list);
  1498. list_del_init(&urb->urb_list);
  1499. bh->completing_ep = urb->ep;
  1500. __usb_hcd_giveback_urb(urb);
  1501. bh->completing_ep = NULL;
  1502. }
  1503. /* check if there are new URBs to giveback */
  1504. spin_lock_irq(&bh->lock);
  1505. if (!list_empty(&bh->head))
  1506. goto restart;
  1507. bh->running = false;
  1508. spin_unlock_irq(&bh->lock);
  1509. }
  1510. /**
  1511. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1512. * @hcd: host controller returning the URB
  1513. * @urb: urb being returned to the USB device driver.
  1514. * @status: completion status code for the URB.
  1515. * Context: in_interrupt()
  1516. *
  1517. * This hands the URB from HCD to its USB device driver, using its
  1518. * completion function. The HCD has freed all per-urb resources
  1519. * (and is done using urb->hcpriv). It also released all HCD locks;
  1520. * the device driver won't cause problems if it frees, modifies,
  1521. * or resubmits this URB.
  1522. *
  1523. * If @urb was unlinked, the value of @status will be overridden by
  1524. * @urb->unlinked. Erroneous short transfers are detected in case
  1525. * the HCD hasn't checked for them.
  1526. */
  1527. void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
  1528. {
  1529. struct giveback_urb_bh *bh;
  1530. bool running, high_prio_bh;
  1531. /* pass status to tasklet via unlinked */
  1532. if (likely(!urb->unlinked))
  1533. urb->unlinked = status;
  1534. if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
  1535. __usb_hcd_giveback_urb(urb);
  1536. return;
  1537. }
  1538. if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) {
  1539. bh = &hcd->high_prio_bh;
  1540. high_prio_bh = true;
  1541. } else {
  1542. bh = &hcd->low_prio_bh;
  1543. high_prio_bh = false;
  1544. }
  1545. spin_lock(&bh->lock);
  1546. list_add_tail(&urb->urb_list, &bh->head);
  1547. running = bh->running;
  1548. spin_unlock(&bh->lock);
  1549. if (running)
  1550. ;
  1551. else if (high_prio_bh)
  1552. tasklet_hi_schedule(&bh->bh);
  1553. else
  1554. tasklet_schedule(&bh->bh);
  1555. }
  1556. EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
  1557. /*-------------------------------------------------------------------------*/
  1558. /* Cancel all URBs pending on this endpoint and wait for the endpoint's
  1559. * queue to drain completely. The caller must first insure that no more
  1560. * URBs can be submitted for this endpoint.
  1561. */
  1562. void usb_hcd_flush_endpoint(struct usb_device *udev,
  1563. struct usb_host_endpoint *ep)
  1564. {
  1565. struct usb_hcd *hcd;
  1566. struct urb *urb;
  1567. if (!ep)
  1568. return;
  1569. might_sleep();
  1570. hcd = bus_to_hcd(udev->bus);
  1571. /* No more submits can occur */
  1572. spin_lock_irq(&hcd_urb_list_lock);
  1573. rescan:
  1574. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1575. int is_in;
  1576. if (urb->unlinked)
  1577. continue;
  1578. usb_get_urb (urb);
  1579. is_in = usb_urb_dir_in(urb);
  1580. spin_unlock(&hcd_urb_list_lock);
  1581. /* kick hcd */
  1582. unlink1(hcd, urb, -ESHUTDOWN);
  1583. dev_dbg (hcd->self.controller,
  1584. "shutdown urb %p ep%d%s%s\n",
  1585. urb, usb_endpoint_num(&ep->desc),
  1586. is_in ? "in" : "out",
  1587. ({ char *s;
  1588. switch (usb_endpoint_type(&ep->desc)) {
  1589. case USB_ENDPOINT_XFER_CONTROL:
  1590. s = ""; break;
  1591. case USB_ENDPOINT_XFER_BULK:
  1592. s = "-bulk"; break;
  1593. case USB_ENDPOINT_XFER_INT:
  1594. s = "-intr"; break;
  1595. default:
  1596. s = "-iso"; break;
  1597. };
  1598. s;
  1599. }));
  1600. usb_put_urb (urb);
  1601. /* list contents may have changed */
  1602. spin_lock(&hcd_urb_list_lock);
  1603. goto rescan;
  1604. }
  1605. spin_unlock_irq(&hcd_urb_list_lock);
  1606. /* Wait until the endpoint queue is completely empty */
  1607. while (!list_empty (&ep->urb_list)) {
  1608. spin_lock_irq(&hcd_urb_list_lock);
  1609. /* The list may have changed while we acquired the spinlock */
  1610. urb = NULL;
  1611. if (!list_empty (&ep->urb_list)) {
  1612. urb = list_entry (ep->urb_list.prev, struct urb,
  1613. urb_list);
  1614. usb_get_urb (urb);
  1615. }
  1616. spin_unlock_irq(&hcd_urb_list_lock);
  1617. if (urb) {
  1618. usb_kill_urb (urb);
  1619. usb_put_urb (urb);
  1620. }
  1621. }
  1622. }
  1623. /**
  1624. * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
  1625. * the bus bandwidth
  1626. * @udev: target &usb_device
  1627. * @new_config: new configuration to install
  1628. * @cur_alt: the current alternate interface setting
  1629. * @new_alt: alternate interface setting that is being installed
  1630. *
  1631. * To change configurations, pass in the new configuration in new_config,
  1632. * and pass NULL for cur_alt and new_alt.
  1633. *
  1634. * To reset a device's configuration (put the device in the ADDRESSED state),
  1635. * pass in NULL for new_config, cur_alt, and new_alt.
  1636. *
  1637. * To change alternate interface settings, pass in NULL for new_config,
  1638. * pass in the current alternate interface setting in cur_alt,
  1639. * and pass in the new alternate interface setting in new_alt.
  1640. *
  1641. * Return: An error if the requested bandwidth change exceeds the
  1642. * bus bandwidth or host controller internal resources.
  1643. */
  1644. int usb_hcd_alloc_bandwidth(struct usb_device *udev,
  1645. struct usb_host_config *new_config,
  1646. struct usb_host_interface *cur_alt,
  1647. struct usb_host_interface *new_alt)
  1648. {
  1649. int num_intfs, i, j;
  1650. struct usb_host_interface *alt = NULL;
  1651. int ret = 0;
  1652. struct usb_hcd *hcd;
  1653. struct usb_host_endpoint *ep;
  1654. hcd = bus_to_hcd(udev->bus);
  1655. if (!hcd->driver->check_bandwidth)
  1656. return 0;
  1657. /* Configuration is being removed - set configuration 0 */
  1658. if (!new_config && !cur_alt) {
  1659. for (i = 1; i < 16; ++i) {
  1660. ep = udev->ep_out[i];
  1661. if (ep)
  1662. hcd->driver->drop_endpoint(hcd, udev, ep);
  1663. ep = udev->ep_in[i];
  1664. if (ep)
  1665. hcd->driver->drop_endpoint(hcd, udev, ep);
  1666. }
  1667. hcd->driver->check_bandwidth(hcd, udev);
  1668. return 0;
  1669. }
  1670. /* Check if the HCD says there's enough bandwidth. Enable all endpoints
  1671. * each interface's alt setting 0 and ask the HCD to check the bandwidth
  1672. * of the bus. There will always be bandwidth for endpoint 0, so it's
  1673. * ok to exclude it.
  1674. */
  1675. if (new_config) {
  1676. num_intfs = new_config->desc.bNumInterfaces;
  1677. /* Remove endpoints (except endpoint 0, which is always on the
  1678. * schedule) from the old config from the schedule
  1679. */
  1680. for (i = 1; i < 16; ++i) {
  1681. ep = udev->ep_out[i];
  1682. if (ep) {
  1683. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1684. if (ret < 0)
  1685. goto reset;
  1686. }
  1687. ep = udev->ep_in[i];
  1688. if (ep) {
  1689. ret = hcd->driver->drop_endpoint(hcd, udev, ep);
  1690. if (ret < 0)
  1691. goto reset;
  1692. }
  1693. }
  1694. for (i = 0; i < num_intfs; ++i) {
  1695. struct usb_host_interface *first_alt;
  1696. int iface_num;
  1697. first_alt = &new_config->intf_cache[i]->altsetting[0];
  1698. iface_num = first_alt->desc.bInterfaceNumber;
  1699. /* Set up endpoints for alternate interface setting 0 */
  1700. alt = usb_find_alt_setting(new_config, iface_num, 0);
  1701. if (!alt)
  1702. /* No alt setting 0? Pick the first setting. */
  1703. alt = first_alt;
  1704. for (j = 0; j < alt->desc.bNumEndpoints; j++) {
  1705. ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
  1706. if (ret < 0)
  1707. goto reset;
  1708. }
  1709. }
  1710. }
  1711. if (cur_alt && new_alt) {
  1712. struct usb_interface *iface = usb_ifnum_to_if(udev,
  1713. cur_alt->desc.bInterfaceNumber);
  1714. if (!iface)
  1715. return -EINVAL;
  1716. if (iface->resetting_device) {
  1717. /*
  1718. * The USB core just reset the device, so the xHCI host
  1719. * and the device will think alt setting 0 is installed.
  1720. * However, the USB core will pass in the alternate
  1721. * setting installed before the reset as cur_alt. Dig
  1722. * out the alternate setting 0 structure, or the first
  1723. * alternate setting if a broken device doesn't have alt
  1724. * setting 0.
  1725. */
  1726. cur_alt = usb_altnum_to_altsetting(iface, 0);
  1727. if (!cur_alt)
  1728. cur_alt = &iface->altsetting[0];
  1729. }
  1730. /* Drop all the endpoints in the current alt setting */
  1731. for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
  1732. ret = hcd->driver->drop_endpoint(hcd, udev,
  1733. &cur_alt->endpoint[i]);
  1734. if (ret < 0)
  1735. goto reset;
  1736. }
  1737. /* Add all the endpoints in the new alt setting */
  1738. for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
  1739. ret = hcd->driver->add_endpoint(hcd, udev,
  1740. &new_alt->endpoint[i]);
  1741. if (ret < 0)
  1742. goto reset;
  1743. }
  1744. }
  1745. ret = hcd->driver->check_bandwidth(hcd, udev);
  1746. reset:
  1747. if (ret < 0)
  1748. hcd->driver->reset_bandwidth(hcd, udev);
  1749. return ret;
  1750. }
  1751. /* Disables the endpoint: synchronizes with the hcd to make sure all
  1752. * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
  1753. * have been called previously. Use for set_configuration, set_interface,
  1754. * driver removal, physical disconnect.
  1755. *
  1756. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1757. * type, maxpacket size, toggle, halt status, and scheduling.
  1758. */
  1759. void usb_hcd_disable_endpoint(struct usb_device *udev,
  1760. struct usb_host_endpoint *ep)
  1761. {
  1762. struct usb_hcd *hcd;
  1763. might_sleep();
  1764. hcd = bus_to_hcd(udev->bus);
  1765. if (hcd->driver->endpoint_disable)
  1766. hcd->driver->endpoint_disable(hcd, ep);
  1767. }
  1768. /**
  1769. * usb_hcd_reset_endpoint - reset host endpoint state
  1770. * @udev: USB device.
  1771. * @ep: the endpoint to reset.
  1772. *
  1773. * Resets any host endpoint state such as the toggle bit, sequence
  1774. * number and current window.
  1775. */
  1776. void usb_hcd_reset_endpoint(struct usb_device *udev,
  1777. struct usb_host_endpoint *ep)
  1778. {
  1779. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1780. if (hcd->driver->endpoint_reset)
  1781. hcd->driver->endpoint_reset(hcd, ep);
  1782. else {
  1783. int epnum = usb_endpoint_num(&ep->desc);
  1784. int is_out = usb_endpoint_dir_out(&ep->desc);
  1785. int is_control = usb_endpoint_xfer_control(&ep->desc);
  1786. usb_settoggle(udev, epnum, is_out, 0);
  1787. if (is_control)
  1788. usb_settoggle(udev, epnum, !is_out, 0);
  1789. }
  1790. }
  1791. /**
  1792. * usb_alloc_streams - allocate bulk endpoint stream IDs.
  1793. * @interface: alternate setting that includes all endpoints.
  1794. * @eps: array of endpoints that need streams.
  1795. * @num_eps: number of endpoints in the array.
  1796. * @num_streams: number of streams to allocate.
  1797. * @mem_flags: flags hcd should use to allocate memory.
  1798. *
  1799. * Sets up a group of bulk endpoints to have @num_streams stream IDs available.
  1800. * Drivers may queue multiple transfers to different stream IDs, which may
  1801. * complete in a different order than they were queued.
  1802. *
  1803. * Return: On success, the number of allocated streams. On failure, a negative
  1804. * error code.
  1805. */
  1806. int usb_alloc_streams(struct usb_interface *interface,
  1807. struct usb_host_endpoint **eps, unsigned int num_eps,
  1808. unsigned int num_streams, gfp_t mem_flags)
  1809. {
  1810. struct usb_hcd *hcd;
  1811. struct usb_device *dev;
  1812. int i;
  1813. dev = interface_to_usbdev(interface);
  1814. hcd = bus_to_hcd(dev->bus);
  1815. if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
  1816. return -EINVAL;
  1817. if (dev->speed != USB_SPEED_SUPER)
  1818. return -EINVAL;
  1819. /* Streams only apply to bulk endpoints. */
  1820. for (i = 0; i < num_eps; i++)
  1821. if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
  1822. return -EINVAL;
  1823. return hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
  1824. num_streams, mem_flags);
  1825. }
  1826. EXPORT_SYMBOL_GPL(usb_alloc_streams);
  1827. /**
  1828. * usb_free_streams - free bulk endpoint stream IDs.
  1829. * @interface: alternate setting that includes all endpoints.
  1830. * @eps: array of endpoints to remove streams from.
  1831. * @num_eps: number of endpoints in the array.
  1832. * @mem_flags: flags hcd should use to allocate memory.
  1833. *
  1834. * Reverts a group of bulk endpoints back to not using stream IDs.
  1835. * Can fail if we are given bad arguments, or HCD is broken.
  1836. *
  1837. * Return: On success, the number of allocated streams. On failure, a negative
  1838. * error code.
  1839. */
  1840. int usb_free_streams(struct usb_interface *interface,
  1841. struct usb_host_endpoint **eps, unsigned int num_eps,
  1842. gfp_t mem_flags)
  1843. {
  1844. struct usb_hcd *hcd;
  1845. struct usb_device *dev;
  1846. int i;
  1847. dev = interface_to_usbdev(interface);
  1848. hcd = bus_to_hcd(dev->bus);
  1849. if (dev->speed != USB_SPEED_SUPER)
  1850. return -EINVAL;
  1851. /* Streams only apply to bulk endpoints. */
  1852. for (i = 0; i < num_eps; i++)
  1853. if (!eps[i] || !usb_endpoint_xfer_bulk(&eps[i]->desc))
  1854. return -EINVAL;
  1855. return hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
  1856. }
  1857. EXPORT_SYMBOL_GPL(usb_free_streams);
  1858. /* Protect against drivers that try to unlink URBs after the device
  1859. * is gone, by waiting until all unlinks for @udev are finished.
  1860. * Since we don't currently track URBs by device, simply wait until
  1861. * nothing is running in the locked region of usb_hcd_unlink_urb().
  1862. */
  1863. void usb_hcd_synchronize_unlinks(struct usb_device *udev)
  1864. {
  1865. spin_lock_irq(&hcd_urb_unlink_lock);
  1866. spin_unlock_irq(&hcd_urb_unlink_lock);
  1867. }
  1868. /*-------------------------------------------------------------------------*/
  1869. /* called in any context */
  1870. int usb_hcd_get_frame_number (struct usb_device *udev)
  1871. {
  1872. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1873. if (!HCD_RH_RUNNING(hcd))
  1874. return -ESHUTDOWN;
  1875. return hcd->driver->get_frame_number (hcd);
  1876. }
  1877. /*-------------------------------------------------------------------------*/
  1878. #ifdef CONFIG_PM
  1879. int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
  1880. {
  1881. struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
  1882. int status;
  1883. int old_state = hcd->state;
  1884. dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
  1885. (PMSG_IS_AUTO(msg) ? "auto-" : ""),
  1886. rhdev->do_remote_wakeup);
  1887. if (HCD_DEAD(hcd)) {
  1888. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
  1889. return 0;
  1890. }
  1891. if (!hcd->driver->bus_suspend) {
  1892. status = -ENOENT;
  1893. } else {
  1894. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1895. hcd->state = HC_STATE_QUIESCING;
  1896. status = hcd->driver->bus_suspend(hcd);
  1897. }
  1898. if (status == 0) {
  1899. usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
  1900. hcd->state = HC_STATE_SUSPENDED;
  1901. /* Did we race with a root-hub wakeup event? */
  1902. if (rhdev->do_remote_wakeup) {
  1903. char buffer[6];
  1904. status = hcd->driver->hub_status_data(hcd, buffer);
  1905. if (status != 0) {
  1906. dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n");
  1907. hcd_bus_resume(rhdev, PMSG_AUTO_RESUME);
  1908. status = -EBUSY;
  1909. }
  1910. }
  1911. } else {
  1912. spin_lock_irq(&hcd_root_hub_lock);
  1913. if (!HCD_DEAD(hcd)) {
  1914. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1915. hcd->state = old_state;
  1916. }
  1917. spin_unlock_irq(&hcd_root_hub_lock);
  1918. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1919. "suspend", status);
  1920. }
  1921. return status;
  1922. }
  1923. int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
  1924. {
  1925. struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
  1926. int status;
  1927. int old_state = hcd->state;
  1928. dev_dbg(&rhdev->dev, "usb %sresume\n",
  1929. (PMSG_IS_AUTO(msg) ? "auto-" : ""));
  1930. if (HCD_DEAD(hcd)) {
  1931. dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
  1932. return 0;
  1933. }
  1934. if (!hcd->driver->bus_resume)
  1935. return -ENOENT;
  1936. if (HCD_RH_RUNNING(hcd))
  1937. return 0;
  1938. hcd->state = HC_STATE_RESUMING;
  1939. status = hcd->driver->bus_resume(hcd);
  1940. clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  1941. if (status == 0) {
  1942. struct usb_device *udev;
  1943. int port1;
  1944. spin_lock_irq(&hcd_root_hub_lock);
  1945. if (!HCD_DEAD(hcd)) {
  1946. usb_set_device_state(rhdev, rhdev->actconfig
  1947. ? USB_STATE_CONFIGURED
  1948. : USB_STATE_ADDRESS);
  1949. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  1950. hcd->state = HC_STATE_RUNNING;
  1951. }
  1952. spin_unlock_irq(&hcd_root_hub_lock);
  1953. /*
  1954. * Check whether any of the enabled ports on the root hub are
  1955. * unsuspended. If they are then a TRSMRCY delay is needed
  1956. * (this is what the USB-2 spec calls a "global resume").
  1957. * Otherwise we can skip the delay.
  1958. */
  1959. usb_hub_for_each_child(rhdev, port1, udev) {
  1960. if (udev->state != USB_STATE_NOTATTACHED &&
  1961. !udev->port_is_suspended) {
  1962. usleep_range(10000, 11000); /* TRSMRCY */
  1963. break;
  1964. }
  1965. }
  1966. } else {
  1967. hcd->state = old_state;
  1968. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1969. "resume", status);
  1970. if (status != -ESHUTDOWN)
  1971. usb_hc_died(hcd);
  1972. }
  1973. return status;
  1974. }
  1975. #endif /* CONFIG_PM */
  1976. #ifdef CONFIG_PM_RUNTIME
  1977. /* Workqueue routine for root-hub remote wakeup */
  1978. static void hcd_resume_work(struct work_struct *work)
  1979. {
  1980. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  1981. struct usb_device *udev = hcd->self.root_hub;
  1982. usb_lock_device(udev);
  1983. usb_remote_wakeup(udev);
  1984. usb_unlock_device(udev);
  1985. }
  1986. /**
  1987. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1988. * @hcd: host controller for this root hub
  1989. *
  1990. * The USB host controller calls this function when its root hub is
  1991. * suspended (with the remote wakeup feature enabled) and a remote
  1992. * wakeup request is received. The routine submits a workqueue request
  1993. * to resume the root hub (that is, manage its downstream ports again).
  1994. */
  1995. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1996. {
  1997. unsigned long flags;
  1998. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1999. if (hcd->rh_registered) {
  2000. set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
  2001. queue_work(pm_wq, &hcd->wakeup_work);
  2002. }
  2003. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  2004. }
  2005. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  2006. #endif /* CONFIG_PM_RUNTIME */
  2007. /*-------------------------------------------------------------------------*/
  2008. #ifdef CONFIG_USB_OTG
  2009. /**
  2010. * usb_bus_start_enum - start immediate enumeration (for OTG)
  2011. * @bus: the bus (must use hcd framework)
  2012. * @port_num: 1-based number of port; usually bus->otg_port
  2013. * Context: in_interrupt()
  2014. *
  2015. * Starts enumeration, with an immediate reset followed later by
  2016. * khubd identifying and possibly configuring the device.
  2017. * This is needed by OTG controller drivers, where it helps meet
  2018. * HNP protocol timing requirements for starting a port reset.
  2019. *
  2020. * Return: 0 if successful.
  2021. */
  2022. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  2023. {
  2024. struct usb_hcd *hcd;
  2025. int status = -EOPNOTSUPP;
  2026. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  2027. * boards with root hubs hooked up to internal devices (instead of
  2028. * just the OTG port) may need more attention to resetting...
  2029. */
  2030. hcd = container_of (bus, struct usb_hcd, self);
  2031. if (port_num && hcd->driver->start_port_reset)
  2032. status = hcd->driver->start_port_reset(hcd, port_num);
  2033. /* run khubd shortly after (first) root port reset finishes;
  2034. * it may issue others, until at least 50 msecs have passed.
  2035. */
  2036. if (status == 0)
  2037. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  2038. return status;
  2039. }
  2040. EXPORT_SYMBOL_GPL(usb_bus_start_enum);
  2041. #endif
  2042. /*-------------------------------------------------------------------------*/
  2043. /**
  2044. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  2045. * @irq: the IRQ being raised
  2046. * @__hcd: pointer to the HCD whose IRQ is being signaled
  2047. *
  2048. * If the controller isn't HALTed, calls the driver's irq handler.
  2049. * Checks whether the controller is now dead.
  2050. *
  2051. * Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise.
  2052. */
  2053. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  2054. {
  2055. struct usb_hcd *hcd = __hcd;
  2056. irqreturn_t rc;
  2057. if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd)))
  2058. rc = IRQ_NONE;
  2059. else if (hcd->driver->irq(hcd) == IRQ_NONE)
  2060. rc = IRQ_NONE;
  2061. else
  2062. rc = IRQ_HANDLED;
  2063. return rc;
  2064. }
  2065. EXPORT_SYMBOL_GPL(usb_hcd_irq);
  2066. /*-------------------------------------------------------------------------*/
  2067. /**
  2068. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  2069. * @hcd: pointer to the HCD representing the controller
  2070. *
  2071. * This is called by bus glue to report a USB host controller that died
  2072. * while operations may still have been pending. It's called automatically
  2073. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  2074. *
  2075. * Only call this function with the primary HCD.
  2076. */
  2077. void usb_hc_died (struct usb_hcd *hcd)
  2078. {
  2079. unsigned long flags;
  2080. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  2081. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  2082. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2083. set_bit(HCD_FLAG_DEAD, &hcd->flags);
  2084. if (hcd->rh_registered) {
  2085. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2086. /* make khubd clean up old urbs and devices */
  2087. usb_set_device_state (hcd->self.root_hub,
  2088. USB_STATE_NOTATTACHED);
  2089. usb_kick_khubd (hcd->self.root_hub);
  2090. }
  2091. if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
  2092. hcd = hcd->shared_hcd;
  2093. if (hcd->rh_registered) {
  2094. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2095. /* make khubd clean up old urbs and devices */
  2096. usb_set_device_state(hcd->self.root_hub,
  2097. USB_STATE_NOTATTACHED);
  2098. usb_kick_khubd(hcd->self.root_hub);
  2099. }
  2100. }
  2101. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  2102. /* Make sure that the other roothub is also deallocated. */
  2103. }
  2104. EXPORT_SYMBOL_GPL (usb_hc_died);
  2105. /*-------------------------------------------------------------------------*/
  2106. static void init_giveback_urb_bh(struct giveback_urb_bh *bh)
  2107. {
  2108. spin_lock_init(&bh->lock);
  2109. INIT_LIST_HEAD(&bh->head);
  2110. tasklet_init(&bh->bh, usb_giveback_urb_bh, (unsigned long)bh);
  2111. }
  2112. /**
  2113. * usb_create_shared_hcd - create and initialize an HCD structure
  2114. * @driver: HC driver that will use this hcd
  2115. * @dev: device for this HC, stored in hcd->self.controller
  2116. * @bus_name: value to store in hcd->self.bus_name
  2117. * @primary_hcd: a pointer to the usb_hcd structure that is sharing the
  2118. * PCI device. Only allocate certain resources for the primary HCD
  2119. * Context: !in_interrupt()
  2120. *
  2121. * Allocate a struct usb_hcd, with extra space at the end for the
  2122. * HC driver's private data. Initialize the generic members of the
  2123. * hcd structure.
  2124. *
  2125. * Return: On success, a pointer to the created and initialized HCD structure.
  2126. * On failure (e.g. if memory is unavailable), %NULL.
  2127. */
  2128. struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
  2129. struct device *dev, const char *bus_name,
  2130. struct usb_hcd *primary_hcd)
  2131. {
  2132. struct usb_hcd *hcd;
  2133. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  2134. if (!hcd) {
  2135. dev_dbg (dev, "hcd alloc failed\n");
  2136. return NULL;
  2137. }
  2138. if (primary_hcd == NULL) {
  2139. hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
  2140. GFP_KERNEL);
  2141. if (!hcd->bandwidth_mutex) {
  2142. kfree(hcd);
  2143. dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
  2144. return NULL;
  2145. }
  2146. mutex_init(hcd->bandwidth_mutex);
  2147. dev_set_drvdata(dev, hcd);
  2148. } else {
  2149. hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
  2150. hcd->primary_hcd = primary_hcd;
  2151. primary_hcd->primary_hcd = primary_hcd;
  2152. hcd->shared_hcd = primary_hcd;
  2153. primary_hcd->shared_hcd = hcd;
  2154. }
  2155. kref_init(&hcd->kref);
  2156. usb_bus_init(&hcd->self);
  2157. hcd->self.controller = dev;
  2158. hcd->self.bus_name = bus_name;
  2159. hcd->self.uses_dma = (dev->dma_mask != NULL);
  2160. init_timer(&hcd->rh_timer);
  2161. hcd->rh_timer.function = rh_timer_func;
  2162. hcd->rh_timer.data = (unsigned long) hcd;
  2163. #ifdef CONFIG_PM_RUNTIME
  2164. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  2165. #endif
  2166. hcd->driver = driver;
  2167. hcd->speed = driver->flags & HCD_MASK;
  2168. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  2169. "USB Host Controller";
  2170. return hcd;
  2171. }
  2172. EXPORT_SYMBOL_GPL(usb_create_shared_hcd);
  2173. /**
  2174. * usb_create_hcd - create and initialize an HCD structure
  2175. * @driver: HC driver that will use this hcd
  2176. * @dev: device for this HC, stored in hcd->self.controller
  2177. * @bus_name: value to store in hcd->self.bus_name
  2178. * Context: !in_interrupt()
  2179. *
  2180. * Allocate a struct usb_hcd, with extra space at the end for the
  2181. * HC driver's private data. Initialize the generic members of the
  2182. * hcd structure.
  2183. *
  2184. * Return: On success, a pointer to the created and initialized HCD
  2185. * structure. On failure (e.g. if memory is unavailable), %NULL.
  2186. */
  2187. struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
  2188. struct device *dev, const char *bus_name)
  2189. {
  2190. return usb_create_shared_hcd(driver, dev, bus_name, NULL);
  2191. }
  2192. EXPORT_SYMBOL_GPL(usb_create_hcd);
  2193. /*
  2194. * Roothubs that share one PCI device must also share the bandwidth mutex.
  2195. * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is
  2196. * deallocated.
  2197. *
  2198. * Make sure to only deallocate the bandwidth_mutex when the primary HCD is
  2199. * freed. When hcd_release() is called for the non-primary HCD, set the
  2200. * primary_hcd's shared_hcd pointer to null (since the non-primary HCD will be
  2201. * freed shortly).
  2202. */
  2203. static void hcd_release (struct kref *kref)
  2204. {
  2205. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  2206. if (usb_hcd_is_primary_hcd(hcd))
  2207. kfree(hcd->bandwidth_mutex);
  2208. else
  2209. hcd->shared_hcd->shared_hcd = NULL;
  2210. kfree(hcd);
  2211. }
  2212. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  2213. {
  2214. if (hcd)
  2215. kref_get (&hcd->kref);
  2216. return hcd;
  2217. }
  2218. EXPORT_SYMBOL_GPL(usb_get_hcd);
  2219. void usb_put_hcd (struct usb_hcd *hcd)
  2220. {
  2221. if (hcd)
  2222. kref_put (&hcd->kref, hcd_release);
  2223. }
  2224. EXPORT_SYMBOL_GPL(usb_put_hcd);
  2225. int usb_hcd_is_primary_hcd(struct usb_hcd *hcd)
  2226. {
  2227. if (!hcd->primary_hcd)
  2228. return 1;
  2229. return hcd == hcd->primary_hcd;
  2230. }
  2231. EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd);
  2232. int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1)
  2233. {
  2234. if (!hcd->driver->find_raw_port_number)
  2235. return port1;
  2236. return hcd->driver->find_raw_port_number(hcd, port1);
  2237. }
  2238. static int usb_hcd_request_irqs(struct usb_hcd *hcd,
  2239. unsigned int irqnum, unsigned long irqflags)
  2240. {
  2241. int retval;
  2242. if (hcd->driver->irq) {
  2243. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  2244. hcd->driver->description, hcd->self.busnum);
  2245. retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  2246. hcd->irq_descr, hcd);
  2247. if (retval != 0) {
  2248. dev_err(hcd->self.controller,
  2249. "request interrupt %d failed\n",
  2250. irqnum);
  2251. return retval;
  2252. }
  2253. hcd->irq = irqnum;
  2254. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  2255. (hcd->driver->flags & HCD_MEMORY) ?
  2256. "io mem" : "io base",
  2257. (unsigned long long)hcd->rsrc_start);
  2258. } else {
  2259. hcd->irq = 0;
  2260. if (hcd->rsrc_start)
  2261. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  2262. (hcd->driver->flags & HCD_MEMORY) ?
  2263. "io mem" : "io base",
  2264. (unsigned long long)hcd->rsrc_start);
  2265. }
  2266. return 0;
  2267. }
  2268. /**
  2269. * usb_add_hcd - finish generic HCD structure initialization and register
  2270. * @hcd: the usb_hcd structure to initialize
  2271. * @irqnum: Interrupt line to allocate
  2272. * @irqflags: Interrupt type flags
  2273. *
  2274. * Finish the remaining parts of generic HCD initialization: allocate the
  2275. * buffers of consistent memory, register the bus, request the IRQ line,
  2276. * and call the driver's reset() and start() routines.
  2277. */
  2278. int usb_add_hcd(struct usb_hcd *hcd,
  2279. unsigned int irqnum, unsigned long irqflags)
  2280. {
  2281. int retval;
  2282. struct usb_device *rhdev;
  2283. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  2284. /* Keep old behaviour if authorized_default is not in [0, 1]. */
  2285. if (authorized_default < 0 || authorized_default > 1)
  2286. hcd->authorized_default = hcd->wireless ? 0 : 1;
  2287. else
  2288. hcd->authorized_default = authorized_default;
  2289. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2290. /* HC is in reset state, but accessible. Now do the one-time init,
  2291. * bottom up so that hcds can customize the root hubs before khubd
  2292. * starts talking to them. (Note, bus id is assigned early too.)
  2293. */
  2294. if ((retval = hcd_buffer_create(hcd)) != 0) {
  2295. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  2296. return retval;
  2297. }
  2298. if ((retval = usb_register_bus(&hcd->self)) < 0)
  2299. goto err_register_bus;
  2300. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  2301. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  2302. retval = -ENOMEM;
  2303. goto err_allocate_root_hub;
  2304. }
  2305. hcd->self.root_hub = rhdev;
  2306. switch (hcd->speed) {
  2307. case HCD_USB11:
  2308. rhdev->speed = USB_SPEED_FULL;
  2309. break;
  2310. case HCD_USB2:
  2311. rhdev->speed = USB_SPEED_HIGH;
  2312. break;
  2313. case HCD_USB25:
  2314. rhdev->speed = USB_SPEED_WIRELESS;
  2315. break;
  2316. case HCD_USB3:
  2317. rhdev->speed = USB_SPEED_SUPER;
  2318. break;
  2319. default:
  2320. retval = -EINVAL;
  2321. goto err_set_rh_speed;
  2322. }
  2323. /* wakeup flag init defaults to "everything works" for root hubs,
  2324. * but drivers can override it in reset() if needed, along with
  2325. * recording the overall controller's system wakeup capability.
  2326. */
  2327. device_set_wakeup_capable(&rhdev->dev, 1);
  2328. /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
  2329. * registered. But since the controller can die at any time,
  2330. * let's initialize the flag before touching the hardware.
  2331. */
  2332. set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2333. /* "reset" is misnamed; its role is now one-time init. the controller
  2334. * should already have been reset (and boot firmware kicked off etc).
  2335. */
  2336. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  2337. dev_err(hcd->self.controller, "can't setup: %d\n", retval);
  2338. goto err_hcd_driver_setup;
  2339. }
  2340. hcd->rh_pollable = 1;
  2341. /* NOTE: root hub and controller capabilities may not be the same */
  2342. if (device_can_wakeup(hcd->self.controller)
  2343. && device_can_wakeup(&hcd->self.root_hub->dev))
  2344. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  2345. /* initialize tasklets */
  2346. init_giveback_urb_bh(&hcd->high_prio_bh);
  2347. init_giveback_urb_bh(&hcd->low_prio_bh);
  2348. /* enable irqs just before we start the controller,
  2349. * if the BIOS provides legacy PCI irqs.
  2350. */
  2351. if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
  2352. retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
  2353. if (retval)
  2354. goto err_request_irq;
  2355. }
  2356. hcd->state = HC_STATE_RUNNING;
  2357. retval = hcd->driver->start(hcd);
  2358. if (retval < 0) {
  2359. dev_err(hcd->self.controller, "startup error %d\n", retval);
  2360. goto err_hcd_driver_start;
  2361. }
  2362. /* starting here, usbcore will pay attention to this root hub */
  2363. if ((retval = register_root_hub(hcd)) != 0)
  2364. goto err_register_root_hub;
  2365. retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  2366. if (retval < 0) {
  2367. printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
  2368. retval);
  2369. goto error_create_attr_group;
  2370. }
  2371. if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
  2372. usb_hcd_poll_rh_status(hcd);
  2373. /*
  2374. * Host controllers don't generate their own wakeup requests;
  2375. * they only forward requests from the root hub. Therefore
  2376. * controllers should always be enabled for remote wakeup.
  2377. */
  2378. device_wakeup_enable(hcd->self.controller);
  2379. return retval;
  2380. error_create_attr_group:
  2381. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2382. if (HC_IS_RUNNING(hcd->state))
  2383. hcd->state = HC_STATE_QUIESCING;
  2384. spin_lock_irq(&hcd_root_hub_lock);
  2385. hcd->rh_registered = 0;
  2386. spin_unlock_irq(&hcd_root_hub_lock);
  2387. #ifdef CONFIG_PM_RUNTIME
  2388. cancel_work_sync(&hcd->wakeup_work);
  2389. #endif
  2390. mutex_lock(&usb_bus_list_lock);
  2391. usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  2392. mutex_unlock(&usb_bus_list_lock);
  2393. err_register_root_hub:
  2394. hcd->rh_pollable = 0;
  2395. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2396. del_timer_sync(&hcd->rh_timer);
  2397. hcd->driver->stop(hcd);
  2398. hcd->state = HC_STATE_HALT;
  2399. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2400. del_timer_sync(&hcd->rh_timer);
  2401. err_hcd_driver_start:
  2402. if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
  2403. free_irq(irqnum, hcd);
  2404. err_request_irq:
  2405. err_hcd_driver_setup:
  2406. err_set_rh_speed:
  2407. usb_put_dev(hcd->self.root_hub);
  2408. err_allocate_root_hub:
  2409. usb_deregister_bus(&hcd->self);
  2410. err_register_bus:
  2411. hcd_buffer_destroy(hcd);
  2412. return retval;
  2413. }
  2414. EXPORT_SYMBOL_GPL(usb_add_hcd);
  2415. /**
  2416. * usb_remove_hcd - shutdown processing for generic HCDs
  2417. * @hcd: the usb_hcd structure to remove
  2418. * Context: !in_interrupt()
  2419. *
  2420. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  2421. * invoking the HCD's stop() method.
  2422. */
  2423. void usb_remove_hcd(struct usb_hcd *hcd)
  2424. {
  2425. struct usb_device *rhdev = hcd->self.root_hub;
  2426. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  2427. usb_get_dev(rhdev);
  2428. sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  2429. clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
  2430. if (HC_IS_RUNNING (hcd->state))
  2431. hcd->state = HC_STATE_QUIESCING;
  2432. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  2433. spin_lock_irq (&hcd_root_hub_lock);
  2434. hcd->rh_registered = 0;
  2435. spin_unlock_irq (&hcd_root_hub_lock);
  2436. #ifdef CONFIG_PM_RUNTIME
  2437. cancel_work_sync(&hcd->wakeup_work);
  2438. #endif
  2439. mutex_lock(&usb_bus_list_lock);
  2440. usb_disconnect(&rhdev); /* Sets rhdev to NULL */
  2441. mutex_unlock(&usb_bus_list_lock);
  2442. /*
  2443. * tasklet_kill() isn't needed here because:
  2444. * - driver's disconnect() called from usb_disconnect() should
  2445. * make sure its URBs are completed during the disconnect()
  2446. * callback
  2447. *
  2448. * - it is too late to run complete() here since driver may have
  2449. * been removed already now
  2450. */
  2451. /* Prevent any more root-hub status calls from the timer.
  2452. * The HCD might still restart the timer (if a port status change
  2453. * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke
  2454. * the hub_status_data() callback.
  2455. */
  2456. hcd->rh_pollable = 0;
  2457. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2458. del_timer_sync(&hcd->rh_timer);
  2459. hcd->driver->stop(hcd);
  2460. hcd->state = HC_STATE_HALT;
  2461. /* In case the HCD restarted the timer, stop it again. */
  2462. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  2463. del_timer_sync(&hcd->rh_timer);
  2464. if (usb_hcd_is_primary_hcd(hcd)) {
  2465. if (hcd->irq > 0)
  2466. free_irq(hcd->irq, hcd);
  2467. }
  2468. usb_put_dev(hcd->self.root_hub);
  2469. usb_deregister_bus(&hcd->self);
  2470. hcd_buffer_destroy(hcd);
  2471. }
  2472. EXPORT_SYMBOL_GPL(usb_remove_hcd);
  2473. void
  2474. usb_hcd_platform_shutdown(struct platform_device *dev)
  2475. {
  2476. struct usb_hcd *hcd = platform_get_drvdata(dev);
  2477. if (hcd->driver->shutdown)
  2478. hcd->driver->shutdown(hcd);
  2479. }
  2480. EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
  2481. /*-------------------------------------------------------------------------*/
  2482. #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
  2483. struct usb_mon_operations *mon_ops;
  2484. /*
  2485. * The registration is unlocked.
  2486. * We do it this way because we do not want to lock in hot paths.
  2487. *
  2488. * Notice that the code is minimally error-proof. Because usbmon needs
  2489. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  2490. */
  2491. int usb_mon_register (struct usb_mon_operations *ops)
  2492. {
  2493. if (mon_ops)
  2494. return -EBUSY;
  2495. mon_ops = ops;
  2496. mb();
  2497. return 0;
  2498. }
  2499. EXPORT_SYMBOL_GPL (usb_mon_register);
  2500. void usb_mon_deregister (void)
  2501. {
  2502. if (mon_ops == NULL) {
  2503. printk(KERN_ERR "USB: monitor was not registered\n");
  2504. return;
  2505. }
  2506. mon_ops = NULL;
  2507. mb();
  2508. }
  2509. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  2510. #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */