hcd.c 77 KB

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