hcd.c 84 KB

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