hcd.c 79 KB

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