hcd.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  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 "usb.h"
  42. #include "hcd.h"
  43. #include "hub.h"
  44. /*-------------------------------------------------------------------------*/
  45. /*
  46. * USB Host Controller Driver framework
  47. *
  48. * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
  49. * HCD-specific behaviors/bugs.
  50. *
  51. * This does error checks, tracks devices and urbs, and delegates to a
  52. * "hc_driver" only for code (and data) that really needs to know about
  53. * hardware differences. That includes root hub registers, i/o queues,
  54. * and so on ... but as little else as possible.
  55. *
  56. * Shared code includes most of the "root hub" code (these are emulated,
  57. * though each HC's hardware works differently) and PCI glue, plus request
  58. * tracking overhead. The HCD code should only block on spinlocks or on
  59. * hardware handshaking; blocking on software events (such as other kernel
  60. * threads releasing resources, or completing actions) is all generic.
  61. *
  62. * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
  63. * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
  64. * only by the hub driver ... and that neither should be seen or used by
  65. * usb client device drivers.
  66. *
  67. * Contributors of ideas or unattributed patches include: David Brownell,
  68. * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
  69. *
  70. * HISTORY:
  71. * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
  72. * associated cleanup. "usb_hcd" still != "usb_bus".
  73. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  74. */
  75. /*-------------------------------------------------------------------------*/
  76. /* Keep track of which host controller drivers are loaded */
  77. unsigned long usb_hcds_loaded;
  78. EXPORT_SYMBOL_GPL(usb_hcds_loaded);
  79. /* host controllers we manage */
  80. LIST_HEAD (usb_bus_list);
  81. EXPORT_SYMBOL_GPL (usb_bus_list);
  82. /* used when allocating bus numbers */
  83. #define USB_MAXBUS 64
  84. struct usb_busmap {
  85. unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
  86. };
  87. static struct usb_busmap busmap;
  88. /* used when updating list of hcds */
  89. DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
  90. EXPORT_SYMBOL_GPL (usb_bus_list_lock);
  91. /* used for controlling access to virtual root hubs */
  92. static DEFINE_SPINLOCK(hcd_root_hub_lock);
  93. /* used when updating an endpoint's URB list */
  94. static DEFINE_SPINLOCK(hcd_urb_list_lock);
  95. /* wait queue for synchronous unlinks */
  96. DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
  97. static inline int is_root_hub(struct usb_device *udev)
  98. {
  99. return (udev->parent == NULL);
  100. }
  101. /*-------------------------------------------------------------------------*/
  102. /*
  103. * Sharable chunks of root hub code.
  104. */
  105. /*-------------------------------------------------------------------------*/
  106. #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
  107. #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
  108. /* usb 2.0 root hub device descriptor */
  109. static const u8 usb2_rh_dev_descriptor [18] = {
  110. 0x12, /* __u8 bLength; */
  111. 0x01, /* __u8 bDescriptorType; Device */
  112. 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
  113. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  114. 0x00, /* __u8 bDeviceSubClass; */
  115. 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
  116. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  117. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
  118. 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
  119. KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
  120. 0x03, /* __u8 iManufacturer; */
  121. 0x02, /* __u8 iProduct; */
  122. 0x01, /* __u8 iSerialNumber; */
  123. 0x01 /* __u8 bNumConfigurations; */
  124. };
  125. /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
  126. /* usb 1.1 root hub device descriptor */
  127. static const u8 usb11_rh_dev_descriptor [18] = {
  128. 0x12, /* __u8 bLength; */
  129. 0x01, /* __u8 bDescriptorType; Device */
  130. 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
  131. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  132. 0x00, /* __u8 bDeviceSubClass; */
  133. 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
  134. 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
  135. 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
  136. 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
  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. /*-------------------------------------------------------------------------*/
  144. /* Configuration descriptors for our root hubs */
  145. static const u8 fs_rh_config_descriptor [] = {
  146. /* one configuration */
  147. 0x09, /* __u8 bLength; */
  148. 0x02, /* __u8 bDescriptorType; Configuration */
  149. 0x19, 0x00, /* __le16 wTotalLength; */
  150. 0x01, /* __u8 bNumInterfaces; (1) */
  151. 0x01, /* __u8 bConfigurationValue; */
  152. 0x00, /* __u8 iConfiguration; */
  153. 0xc0, /* __u8 bmAttributes;
  154. Bit 7: must be set,
  155. 6: Self-powered,
  156. 5: Remote wakeup,
  157. 4..0: resvd */
  158. 0x00, /* __u8 MaxPower; */
  159. /* USB 1.1:
  160. * USB 2.0, single TT organization (mandatory):
  161. * one interface, protocol 0
  162. *
  163. * USB 2.0, multiple TT organization (optional):
  164. * two interfaces, protocols 1 (like single TT)
  165. * and 2 (multiple TT mode) ... config is
  166. * sometimes settable
  167. * NOT IMPLEMENTED
  168. */
  169. /* one interface */
  170. 0x09, /* __u8 if_bLength; */
  171. 0x04, /* __u8 if_bDescriptorType; Interface */
  172. 0x00, /* __u8 if_bInterfaceNumber; */
  173. 0x00, /* __u8 if_bAlternateSetting; */
  174. 0x01, /* __u8 if_bNumEndpoints; */
  175. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  176. 0x00, /* __u8 if_bInterfaceSubClass; */
  177. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  178. 0x00, /* __u8 if_iInterface; */
  179. /* one endpoint (status change endpoint) */
  180. 0x07, /* __u8 ep_bLength; */
  181. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  182. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  183. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  184. 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  185. 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  186. };
  187. static const u8 hs_rh_config_descriptor [] = {
  188. /* one configuration */
  189. 0x09, /* __u8 bLength; */
  190. 0x02, /* __u8 bDescriptorType; Configuration */
  191. 0x19, 0x00, /* __le16 wTotalLength; */
  192. 0x01, /* __u8 bNumInterfaces; (1) */
  193. 0x01, /* __u8 bConfigurationValue; */
  194. 0x00, /* __u8 iConfiguration; */
  195. 0xc0, /* __u8 bmAttributes;
  196. Bit 7: must be set,
  197. 6: Self-powered,
  198. 5: Remote wakeup,
  199. 4..0: resvd */
  200. 0x00, /* __u8 MaxPower; */
  201. /* USB 1.1:
  202. * USB 2.0, single TT organization (mandatory):
  203. * one interface, protocol 0
  204. *
  205. * USB 2.0, multiple TT organization (optional):
  206. * two interfaces, protocols 1 (like single TT)
  207. * and 2 (multiple TT mode) ... config is
  208. * sometimes settable
  209. * NOT IMPLEMENTED
  210. */
  211. /* one interface */
  212. 0x09, /* __u8 if_bLength; */
  213. 0x04, /* __u8 if_bDescriptorType; Interface */
  214. 0x00, /* __u8 if_bInterfaceNumber; */
  215. 0x00, /* __u8 if_bAlternateSetting; */
  216. 0x01, /* __u8 if_bNumEndpoints; */
  217. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  218. 0x00, /* __u8 if_bInterfaceSubClass; */
  219. 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  220. 0x00, /* __u8 if_iInterface; */
  221. /* one endpoint (status change endpoint) */
  222. 0x07, /* __u8 ep_bLength; */
  223. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  224. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  225. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  226. /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
  227. * see hub.c:hub_configure() for details. */
  228. (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
  229. 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
  230. };
  231. /*-------------------------------------------------------------------------*/
  232. /*
  233. * helper routine for returning string descriptors in UTF-16LE
  234. * input can actually be ISO-8859-1; ASCII is its 7-bit subset
  235. */
  236. static int ascii2utf (char *s, u8 *utf, int utfmax)
  237. {
  238. int retval;
  239. for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
  240. *utf++ = *s++;
  241. *utf++ = 0;
  242. }
  243. if (utfmax > 0) {
  244. *utf = *s;
  245. ++retval;
  246. }
  247. return retval;
  248. }
  249. /*
  250. * rh_string - provides manufacturer, product and serial strings for root hub
  251. * @id: the string ID number (1: serial number, 2: product, 3: vendor)
  252. * @hcd: the host controller for this root hub
  253. * @data: return packet in UTF-16 LE
  254. * @len: length of the return packet
  255. *
  256. * Produces either a manufacturer, product or serial number string for the
  257. * virtual root hub device.
  258. */
  259. static int rh_string (
  260. int id,
  261. struct usb_hcd *hcd,
  262. u8 *data,
  263. int len
  264. ) {
  265. char buf [100];
  266. // language ids
  267. if (id == 0) {
  268. buf[0] = 4; buf[1] = 3; /* 4 bytes string data */
  269. buf[2] = 0x09; buf[3] = 0x04; /* MSFT-speak for "en-us" */
  270. len = min (len, 4);
  271. memcpy (data, buf, len);
  272. return len;
  273. // serial number
  274. } else if (id == 1) {
  275. strlcpy (buf, hcd->self.bus_name, sizeof buf);
  276. // product description
  277. } else if (id == 2) {
  278. strlcpy (buf, hcd->product_desc, sizeof buf);
  279. // id 3 == vendor description
  280. } else if (id == 3) {
  281. snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
  282. init_utsname()->release, hcd->driver->description);
  283. // unsupported IDs --> "protocol stall"
  284. } else
  285. return -EPIPE;
  286. switch (len) { /* All cases fall through */
  287. default:
  288. len = 2 + ascii2utf (buf, data + 2, len - 2);
  289. case 2:
  290. data [1] = 3; /* type == string */
  291. case 1:
  292. data [0] = 2 * (strlen (buf) + 1);
  293. case 0:
  294. ; /* Compiler wants a statement here */
  295. }
  296. return len;
  297. }
  298. /* Root hub control transfers execute synchronously */
  299. static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
  300. {
  301. struct usb_ctrlrequest *cmd;
  302. u16 typeReq, wValue, wIndex, wLength;
  303. u8 *ubuf = urb->transfer_buffer;
  304. u8 tbuf [sizeof (struct usb_hub_descriptor)]
  305. __attribute__((aligned(4)));
  306. const u8 *bufp = tbuf;
  307. int len = 0;
  308. int status;
  309. int n;
  310. u8 patch_wakeup = 0;
  311. u8 patch_protocol = 0;
  312. might_sleep();
  313. spin_lock_irq(&hcd_root_hub_lock);
  314. status = usb_hcd_link_urb_to_ep(hcd, urb);
  315. spin_unlock_irq(&hcd_root_hub_lock);
  316. if (status)
  317. return status;
  318. urb->hcpriv = hcd; /* Indicate it's queued */
  319. cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  320. typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
  321. wValue = le16_to_cpu (cmd->wValue);
  322. wIndex = le16_to_cpu (cmd->wIndex);
  323. wLength = le16_to_cpu (cmd->wLength);
  324. if (wLength > urb->transfer_buffer_length)
  325. goto error;
  326. urb->actual_length = 0;
  327. switch (typeReq) {
  328. /* DEVICE REQUESTS */
  329. /* The root hub's remote wakeup enable bit is implemented using
  330. * driver model wakeup flags. If this system supports wakeup
  331. * through USB, userspace may change the default "allow wakeup"
  332. * policy through sysfs or these calls.
  333. *
  334. * Most root hubs support wakeup from downstream devices, for
  335. * runtime power management (disabling USB clocks and reducing
  336. * VBUS power usage). However, not all of them do so; silicon,
  337. * board, and BIOS bugs here are not uncommon, so these can't
  338. * be treated quite like external hubs.
  339. *
  340. * Likewise, not all root hubs will pass wakeup events upstream,
  341. * to wake up the whole system. So don't assume root hub and
  342. * controller capabilities are identical.
  343. */
  344. case DeviceRequest | USB_REQ_GET_STATUS:
  345. tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
  346. << USB_DEVICE_REMOTE_WAKEUP)
  347. | (1 << USB_DEVICE_SELF_POWERED);
  348. tbuf [1] = 0;
  349. len = 2;
  350. break;
  351. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  352. if (wValue == USB_DEVICE_REMOTE_WAKEUP)
  353. device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
  354. else
  355. goto error;
  356. break;
  357. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  358. if (device_can_wakeup(&hcd->self.root_hub->dev)
  359. && wValue == USB_DEVICE_REMOTE_WAKEUP)
  360. device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
  361. else
  362. goto error;
  363. break;
  364. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  365. tbuf [0] = 1;
  366. len = 1;
  367. /* FALLTHROUGH */
  368. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  369. break;
  370. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  371. switch (wValue & 0xff00) {
  372. case USB_DT_DEVICE << 8:
  373. if (hcd->driver->flags & HCD_USB2)
  374. bufp = usb2_rh_dev_descriptor;
  375. else if (hcd->driver->flags & HCD_USB11)
  376. bufp = usb11_rh_dev_descriptor;
  377. else
  378. goto error;
  379. len = 18;
  380. if (hcd->has_tt)
  381. patch_protocol = 1;
  382. break;
  383. case USB_DT_CONFIG << 8:
  384. if (hcd->driver->flags & HCD_USB2) {
  385. bufp = hs_rh_config_descriptor;
  386. len = sizeof hs_rh_config_descriptor;
  387. } else {
  388. bufp = fs_rh_config_descriptor;
  389. len = sizeof fs_rh_config_descriptor;
  390. }
  391. if (device_can_wakeup(&hcd->self.root_hub->dev))
  392. patch_wakeup = 1;
  393. break;
  394. case USB_DT_STRING << 8:
  395. n = rh_string (wValue & 0xff, hcd, ubuf, wLength);
  396. if (n < 0)
  397. goto error;
  398. urb->actual_length = n;
  399. break;
  400. default:
  401. goto error;
  402. }
  403. break;
  404. case DeviceRequest | USB_REQ_GET_INTERFACE:
  405. tbuf [0] = 0;
  406. len = 1;
  407. /* FALLTHROUGH */
  408. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  409. break;
  410. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  411. // wValue == urb->dev->devaddr
  412. dev_dbg (hcd->self.controller, "root hub device address %d\n",
  413. wValue);
  414. break;
  415. /* INTERFACE REQUESTS (no defined feature/status flags) */
  416. /* ENDPOINT REQUESTS */
  417. case EndpointRequest | USB_REQ_GET_STATUS:
  418. // ENDPOINT_HALT flag
  419. tbuf [0] = 0;
  420. tbuf [1] = 0;
  421. len = 2;
  422. /* FALLTHROUGH */
  423. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  424. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  425. dev_dbg (hcd->self.controller, "no endpoint features yet\n");
  426. break;
  427. /* CLASS REQUESTS (and errors) */
  428. default:
  429. /* non-generic request */
  430. switch (typeReq) {
  431. case GetHubStatus:
  432. case GetPortStatus:
  433. len = 4;
  434. break;
  435. case GetHubDescriptor:
  436. len = sizeof (struct usb_hub_descriptor);
  437. break;
  438. }
  439. status = hcd->driver->hub_control (hcd,
  440. typeReq, wValue, wIndex,
  441. tbuf, wLength);
  442. break;
  443. error:
  444. /* "protocol stall" on error */
  445. status = -EPIPE;
  446. }
  447. if (status) {
  448. len = 0;
  449. if (status != -EPIPE) {
  450. dev_dbg (hcd->self.controller,
  451. "CTRL: TypeReq=0x%x val=0x%x "
  452. "idx=0x%x len=%d ==> %d\n",
  453. typeReq, wValue, wIndex,
  454. wLength, status);
  455. }
  456. }
  457. if (len) {
  458. if (urb->transfer_buffer_length < len)
  459. len = urb->transfer_buffer_length;
  460. urb->actual_length = len;
  461. // always USB_DIR_IN, toward host
  462. memcpy (ubuf, bufp, len);
  463. /* report whether RH hardware supports remote wakeup */
  464. if (patch_wakeup &&
  465. len > offsetof (struct usb_config_descriptor,
  466. bmAttributes))
  467. ((struct usb_config_descriptor *)ubuf)->bmAttributes
  468. |= USB_CONFIG_ATT_WAKEUP;
  469. /* report whether RH hardware has an integrated TT */
  470. if (patch_protocol &&
  471. len > offsetof(struct usb_device_descriptor,
  472. bDeviceProtocol))
  473. ((struct usb_device_descriptor *) ubuf)->
  474. bDeviceProtocol = 1;
  475. }
  476. /* any errors get returned through the urb completion */
  477. spin_lock_irq(&hcd_root_hub_lock);
  478. usb_hcd_unlink_urb_from_ep(hcd, urb);
  479. /* This peculiar use of spinlocks echoes what real HC drivers do.
  480. * Avoiding calls to local_irq_disable/enable makes the code
  481. * RT-friendly.
  482. */
  483. spin_unlock(&hcd_root_hub_lock);
  484. usb_hcd_giveback_urb(hcd, urb, status);
  485. spin_lock(&hcd_root_hub_lock);
  486. spin_unlock_irq(&hcd_root_hub_lock);
  487. return 0;
  488. }
  489. /*-------------------------------------------------------------------------*/
  490. /*
  491. * Root Hub interrupt transfers are polled using a timer if the
  492. * driver requests it; otherwise the driver is responsible for
  493. * calling usb_hcd_poll_rh_status() when an event occurs.
  494. *
  495. * Completions are called in_interrupt(), but they may or may not
  496. * be in_irq().
  497. */
  498. void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
  499. {
  500. struct urb *urb;
  501. int length;
  502. unsigned long flags;
  503. char buffer[4]; /* Any root hubs with > 31 ports? */
  504. if (unlikely(!hcd->rh_registered))
  505. return;
  506. if (!hcd->uses_new_polling && !hcd->status_urb)
  507. return;
  508. length = hcd->driver->hub_status_data(hcd, buffer);
  509. if (length > 0) {
  510. /* try to complete the status urb */
  511. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  512. urb = hcd->status_urb;
  513. if (urb) {
  514. hcd->poll_pending = 0;
  515. hcd->status_urb = NULL;
  516. urb->actual_length = length;
  517. memcpy(urb->transfer_buffer, buffer, length);
  518. usb_hcd_unlink_urb_from_ep(hcd, urb);
  519. spin_unlock(&hcd_root_hub_lock);
  520. usb_hcd_giveback_urb(hcd, urb, 0);
  521. spin_lock(&hcd_root_hub_lock);
  522. } else {
  523. length = 0;
  524. hcd->poll_pending = 1;
  525. }
  526. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  527. }
  528. /* The USB 2.0 spec says 256 ms. This is close enough and won't
  529. * exceed that limit if HZ is 100. The math is more clunky than
  530. * maybe expected, this is to make sure that all timers for USB devices
  531. * fire at the same time to give the CPU a break inbetween */
  532. if (hcd->uses_new_polling ? hcd->poll_rh :
  533. (length == 0 && hcd->status_urb != NULL))
  534. mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  535. }
  536. EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
  537. /* timer callback */
  538. static void rh_timer_func (unsigned long _hcd)
  539. {
  540. usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
  541. }
  542. /*-------------------------------------------------------------------------*/
  543. static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
  544. {
  545. int retval;
  546. unsigned long flags;
  547. int len = 1 + (urb->dev->maxchild / 8);
  548. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  549. if (hcd->status_urb || urb->transfer_buffer_length < len) {
  550. dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
  551. retval = -EINVAL;
  552. goto done;
  553. }
  554. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  555. if (retval)
  556. goto done;
  557. hcd->status_urb = urb;
  558. urb->hcpriv = hcd; /* indicate it's queued */
  559. if (!hcd->uses_new_polling)
  560. mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
  561. /* If a status change has already occurred, report it ASAP */
  562. else if (hcd->poll_pending)
  563. mod_timer(&hcd->rh_timer, jiffies);
  564. retval = 0;
  565. done:
  566. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  567. return retval;
  568. }
  569. static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
  570. {
  571. if (usb_endpoint_xfer_int(&urb->ep->desc))
  572. return rh_queue_status (hcd, urb);
  573. if (usb_endpoint_xfer_control(&urb->ep->desc))
  574. return rh_call_control (hcd, urb);
  575. return -EINVAL;
  576. }
  577. /*-------------------------------------------------------------------------*/
  578. /* Unlinks of root-hub control URBs are legal, but they don't do anything
  579. * since these URBs always execute synchronously.
  580. */
  581. static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  582. {
  583. unsigned long flags;
  584. int rc;
  585. spin_lock_irqsave(&hcd_root_hub_lock, flags);
  586. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  587. if (rc)
  588. goto done;
  589. if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
  590. ; /* Do nothing */
  591. } else { /* Status URB */
  592. if (!hcd->uses_new_polling)
  593. del_timer (&hcd->rh_timer);
  594. if (urb == hcd->status_urb) {
  595. hcd->status_urb = NULL;
  596. usb_hcd_unlink_urb_from_ep(hcd, urb);
  597. spin_unlock(&hcd_root_hub_lock);
  598. usb_hcd_giveback_urb(hcd, urb, status);
  599. spin_lock(&hcd_root_hub_lock);
  600. }
  601. }
  602. done:
  603. spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
  604. return rc;
  605. }
  606. /*
  607. * Show & store the current value of authorized_default
  608. */
  609. static ssize_t usb_host_authorized_default_show(struct device *dev,
  610. struct device_attribute *attr,
  611. char *buf)
  612. {
  613. struct usb_device *rh_usb_dev = to_usb_device(dev);
  614. struct usb_bus *usb_bus = rh_usb_dev->bus;
  615. struct usb_hcd *usb_hcd;
  616. if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
  617. return -ENODEV;
  618. usb_hcd = bus_to_hcd(usb_bus);
  619. return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default);
  620. }
  621. static ssize_t usb_host_authorized_default_store(struct device *dev,
  622. struct device_attribute *attr,
  623. const char *buf, size_t size)
  624. {
  625. ssize_t result;
  626. unsigned val;
  627. struct usb_device *rh_usb_dev = to_usb_device(dev);
  628. struct usb_bus *usb_bus = rh_usb_dev->bus;
  629. struct usb_hcd *usb_hcd;
  630. if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
  631. return -ENODEV;
  632. usb_hcd = bus_to_hcd(usb_bus);
  633. result = sscanf(buf, "%u\n", &val);
  634. if (result == 1) {
  635. usb_hcd->authorized_default = val? 1 : 0;
  636. result = size;
  637. }
  638. else
  639. result = -EINVAL;
  640. return result;
  641. }
  642. static DEVICE_ATTR(authorized_default, 0644,
  643. usb_host_authorized_default_show,
  644. usb_host_authorized_default_store);
  645. /* Group all the USB bus attributes */
  646. static struct attribute *usb_bus_attrs[] = {
  647. &dev_attr_authorized_default.attr,
  648. NULL,
  649. };
  650. static struct attribute_group usb_bus_attr_group = {
  651. .name = NULL, /* we want them in the same directory */
  652. .attrs = usb_bus_attrs,
  653. };
  654. /*-------------------------------------------------------------------------*/
  655. static struct class *usb_host_class;
  656. int usb_host_init(void)
  657. {
  658. int retval = 0;
  659. usb_host_class = class_create(THIS_MODULE, "usb_host");
  660. if (IS_ERR(usb_host_class))
  661. retval = PTR_ERR(usb_host_class);
  662. return retval;
  663. }
  664. void usb_host_cleanup(void)
  665. {
  666. class_destroy(usb_host_class);
  667. }
  668. /**
  669. * usb_bus_init - shared initialization code
  670. * @bus: the bus structure being initialized
  671. *
  672. * This code is used to initialize a usb_bus structure, memory for which is
  673. * separately managed.
  674. */
  675. static void usb_bus_init (struct usb_bus *bus)
  676. {
  677. memset (&bus->devmap, 0, sizeof(struct usb_devmap));
  678. bus->devnum_next = 1;
  679. bus->root_hub = NULL;
  680. bus->busnum = -1;
  681. bus->bandwidth_allocated = 0;
  682. bus->bandwidth_int_reqs = 0;
  683. bus->bandwidth_isoc_reqs = 0;
  684. INIT_LIST_HEAD (&bus->bus_list);
  685. }
  686. /*-------------------------------------------------------------------------*/
  687. /**
  688. * usb_register_bus - registers the USB host controller with the usb core
  689. * @bus: pointer to the bus to register
  690. * Context: !in_interrupt()
  691. *
  692. * Assigns a bus number, and links the controller into usbcore data
  693. * structures so that it can be seen by scanning the bus list.
  694. */
  695. static int usb_register_bus(struct usb_bus *bus)
  696. {
  697. int result = -E2BIG;
  698. int busnum;
  699. mutex_lock(&usb_bus_list_lock);
  700. busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
  701. if (busnum >= USB_MAXBUS) {
  702. printk (KERN_ERR "%s: too many buses\n", usbcore_name);
  703. goto error_find_busnum;
  704. }
  705. set_bit (busnum, busmap.busmap);
  706. bus->busnum = busnum;
  707. bus->dev = device_create(usb_host_class, bus->controller, MKDEV(0, 0),
  708. bus, "usb_host%d", busnum);
  709. result = PTR_ERR(bus->dev);
  710. if (IS_ERR(bus->dev))
  711. goto error_create_class_dev;
  712. /* Add it to the local list of buses */
  713. list_add (&bus->bus_list, &usb_bus_list);
  714. mutex_unlock(&usb_bus_list_lock);
  715. usb_notify_add_bus(bus);
  716. dev_info (bus->controller, "new USB bus registered, assigned bus "
  717. "number %d\n", bus->busnum);
  718. return 0;
  719. error_create_class_dev:
  720. clear_bit(busnum, busmap.busmap);
  721. error_find_busnum:
  722. mutex_unlock(&usb_bus_list_lock);
  723. return result;
  724. }
  725. /**
  726. * usb_deregister_bus - deregisters the USB host controller
  727. * @bus: pointer to the bus to deregister
  728. * Context: !in_interrupt()
  729. *
  730. * Recycles the bus number, and unlinks the controller from usbcore data
  731. * structures so that it won't be seen by scanning the bus list.
  732. */
  733. static void usb_deregister_bus (struct usb_bus *bus)
  734. {
  735. dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
  736. /*
  737. * NOTE: make sure that all the devices are removed by the
  738. * controller code, as well as having it call this when cleaning
  739. * itself up
  740. */
  741. mutex_lock(&usb_bus_list_lock);
  742. list_del (&bus->bus_list);
  743. mutex_unlock(&usb_bus_list_lock);
  744. usb_notify_remove_bus(bus);
  745. clear_bit (bus->busnum, busmap.busmap);
  746. device_unregister(bus->dev);
  747. }
  748. /**
  749. * register_root_hub - called by usb_add_hcd() to register a root hub
  750. * @hcd: host controller for this root hub
  751. *
  752. * This function registers the root hub with the USB subsystem. It sets up
  753. * the device properly in the device tree and then calls usb_new_device()
  754. * to register the usb device. It also assigns the root hub's USB address
  755. * (always 1).
  756. */
  757. static int register_root_hub(struct usb_hcd *hcd)
  758. {
  759. struct device *parent_dev = hcd->self.controller;
  760. struct usb_device *usb_dev = hcd->self.root_hub;
  761. const int devnum = 1;
  762. int retval;
  763. usb_dev->devnum = devnum;
  764. usb_dev->bus->devnum_next = devnum + 1;
  765. memset (&usb_dev->bus->devmap.devicemap, 0,
  766. sizeof usb_dev->bus->devmap.devicemap);
  767. set_bit (devnum, usb_dev->bus->devmap.devicemap);
  768. usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
  769. mutex_lock(&usb_bus_list_lock);
  770. usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
  771. retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
  772. if (retval != sizeof usb_dev->descriptor) {
  773. mutex_unlock(&usb_bus_list_lock);
  774. dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
  775. dev_name(&usb_dev->dev), retval);
  776. return (retval < 0) ? retval : -EMSGSIZE;
  777. }
  778. retval = usb_new_device (usb_dev);
  779. if (retval) {
  780. dev_err (parent_dev, "can't register root hub for %s, %d\n",
  781. dev_name(&usb_dev->dev), retval);
  782. }
  783. mutex_unlock(&usb_bus_list_lock);
  784. if (retval == 0) {
  785. spin_lock_irq (&hcd_root_hub_lock);
  786. hcd->rh_registered = 1;
  787. spin_unlock_irq (&hcd_root_hub_lock);
  788. /* Did the HC die before the root hub was registered? */
  789. if (hcd->state == HC_STATE_HALT)
  790. usb_hc_died (hcd); /* This time clean up */
  791. }
  792. return retval;
  793. }
  794. /*-------------------------------------------------------------------------*/
  795. /**
  796. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  797. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  798. * @is_input: true iff the transaction sends data to the host
  799. * @isoc: true for isochronous transactions, false for interrupt ones
  800. * @bytecount: how many bytes in the transaction.
  801. *
  802. * Returns approximate bus time in nanoseconds for a periodic transaction.
  803. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  804. * scheduled in software, this function is only used for such scheduling.
  805. */
  806. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  807. {
  808. unsigned long tmp;
  809. switch (speed) {
  810. case USB_SPEED_LOW: /* INTR only */
  811. if (is_input) {
  812. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  813. return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  814. } else {
  815. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  816. return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  817. }
  818. case USB_SPEED_FULL: /* ISOC or INTR */
  819. if (isoc) {
  820. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  821. return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
  822. } else {
  823. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  824. return (9107L + BW_HOST_DELAY + tmp);
  825. }
  826. case USB_SPEED_HIGH: /* ISOC or INTR */
  827. // FIXME adjust for input vs output
  828. if (isoc)
  829. tmp = HS_NSECS_ISO (bytecount);
  830. else
  831. tmp = HS_NSECS (bytecount);
  832. return tmp;
  833. default:
  834. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  835. return -1;
  836. }
  837. }
  838. EXPORT_SYMBOL_GPL(usb_calc_bus_time);
  839. /*-------------------------------------------------------------------------*/
  840. /*
  841. * Generic HC operations.
  842. */
  843. /*-------------------------------------------------------------------------*/
  844. /**
  845. * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
  846. * @hcd: host controller to which @urb was submitted
  847. * @urb: URB being submitted
  848. *
  849. * Host controller drivers should call this routine in their enqueue()
  850. * method. The HCD's private spinlock must be held and interrupts must
  851. * be disabled. The actions carried out here are required for URB
  852. * submission, as well as for endpoint shutdown and for usb_kill_urb.
  853. *
  854. * Returns 0 for no error, otherwise a negative error code (in which case
  855. * the enqueue() method must fail). If no error occurs but enqueue() fails
  856. * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
  857. * the private spinlock and returning.
  858. */
  859. int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
  860. {
  861. int rc = 0;
  862. spin_lock(&hcd_urb_list_lock);
  863. /* Check that the URB isn't being killed */
  864. if (unlikely(urb->reject)) {
  865. rc = -EPERM;
  866. goto done;
  867. }
  868. if (unlikely(!urb->ep->enabled)) {
  869. rc = -ENOENT;
  870. goto done;
  871. }
  872. if (unlikely(!urb->dev->can_submit)) {
  873. rc = -EHOSTUNREACH;
  874. goto done;
  875. }
  876. /*
  877. * Check the host controller's state and add the URB to the
  878. * endpoint's queue.
  879. */
  880. switch (hcd->state) {
  881. case HC_STATE_RUNNING:
  882. case HC_STATE_RESUMING:
  883. urb->unlinked = 0;
  884. list_add_tail(&urb->urb_list, &urb->ep->urb_list);
  885. break;
  886. default:
  887. rc = -ESHUTDOWN;
  888. goto done;
  889. }
  890. done:
  891. spin_unlock(&hcd_urb_list_lock);
  892. return rc;
  893. }
  894. EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
  895. /**
  896. * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
  897. * @hcd: host controller to which @urb was submitted
  898. * @urb: URB being checked for unlinkability
  899. * @status: error code to store in @urb if the unlink succeeds
  900. *
  901. * Host controller drivers should call this routine in their dequeue()
  902. * method. The HCD's private spinlock must be held and interrupts must
  903. * be disabled. The actions carried out here are required for making
  904. * sure than an unlink is valid.
  905. *
  906. * Returns 0 for no error, otherwise a negative error code (in which case
  907. * the dequeue() method must fail). The possible error codes are:
  908. *
  909. * -EIDRM: @urb was not submitted or has already completed.
  910. * The completion function may not have been called yet.
  911. *
  912. * -EBUSY: @urb has already been unlinked.
  913. */
  914. int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
  915. int status)
  916. {
  917. struct list_head *tmp;
  918. /* insist the urb is still queued */
  919. list_for_each(tmp, &urb->ep->urb_list) {
  920. if (tmp == &urb->urb_list)
  921. break;
  922. }
  923. if (tmp != &urb->urb_list)
  924. return -EIDRM;
  925. /* Any status except -EINPROGRESS means something already started to
  926. * unlink this URB from the hardware. So there's no more work to do.
  927. */
  928. if (urb->unlinked)
  929. return -EBUSY;
  930. urb->unlinked = status;
  931. /* IRQ setup can easily be broken so that USB controllers
  932. * never get completion IRQs ... maybe even the ones we need to
  933. * finish unlinking the initial failed usb_set_address()
  934. * or device descriptor fetch.
  935. */
  936. if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) &&
  937. !is_root_hub(urb->dev)) {
  938. dev_warn(hcd->self.controller, "Unlink after no-IRQ? "
  939. "Controller is probably using the wrong IRQ.\n");
  940. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  941. }
  942. return 0;
  943. }
  944. EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
  945. /**
  946. * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  947. * @hcd: host controller to which @urb was submitted
  948. * @urb: URB being unlinked
  949. *
  950. * Host controller drivers should call this routine before calling
  951. * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  952. * interrupts must be disabled. The actions carried out here are required
  953. * for URB completion.
  954. */
  955. void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
  956. {
  957. /* clear all state linking urb to this dev (and hcd) */
  958. spin_lock(&hcd_urb_list_lock);
  959. list_del_init(&urb->urb_list);
  960. spin_unlock(&hcd_urb_list_lock);
  961. }
  962. EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
  963. /*
  964. * Some usb host controllers can only perform dma using a small SRAM area.
  965. * The usb core itself is however optimized for host controllers that can dma
  966. * using regular system memory - like pci devices doing bus mastering.
  967. *
  968. * To support host controllers with limited dma capabilites we provide dma
  969. * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
  970. * For this to work properly the host controller code must first use the
  971. * function dma_declare_coherent_memory() to point out which memory area
  972. * that should be used for dma allocations.
  973. *
  974. * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
  975. * dma using dma_alloc_coherent() which in turn allocates from the memory
  976. * area pointed out with dma_declare_coherent_memory().
  977. *
  978. * So, to summarize...
  979. *
  980. * - We need "local" memory, canonical example being
  981. * a small SRAM on a discrete controller being the
  982. * only memory that the controller can read ...
  983. * (a) "normal" kernel memory is no good, and
  984. * (b) there's not enough to share
  985. *
  986. * - The only *portable* hook for such stuff in the
  987. * DMA framework is dma_declare_coherent_memory()
  988. *
  989. * - So we use that, even though the primary requirement
  990. * is that the memory be "local" (hence addressible
  991. * by that device), not "coherent".
  992. *
  993. */
  994. static int hcd_alloc_coherent(struct usb_bus *bus,
  995. gfp_t mem_flags, dma_addr_t *dma_handle,
  996. void **vaddr_handle, size_t size,
  997. enum dma_data_direction dir)
  998. {
  999. unsigned char *vaddr;
  1000. vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
  1001. mem_flags, dma_handle);
  1002. if (!vaddr)
  1003. return -ENOMEM;
  1004. /*
  1005. * Store the virtual address of the buffer at the end
  1006. * of the allocated dma buffer. The size of the buffer
  1007. * may be uneven so use unaligned functions instead
  1008. * of just rounding up. It makes sense to optimize for
  1009. * memory footprint over access speed since the amount
  1010. * of memory available for dma may be limited.
  1011. */
  1012. put_unaligned((unsigned long)*vaddr_handle,
  1013. (unsigned long *)(vaddr + size));
  1014. if (dir == DMA_TO_DEVICE)
  1015. memcpy(vaddr, *vaddr_handle, size);
  1016. *vaddr_handle = vaddr;
  1017. return 0;
  1018. }
  1019. static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
  1020. void **vaddr_handle, size_t size,
  1021. enum dma_data_direction dir)
  1022. {
  1023. unsigned char *vaddr = *vaddr_handle;
  1024. vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
  1025. if (dir == DMA_FROM_DEVICE)
  1026. memcpy(vaddr, *vaddr_handle, size);
  1027. hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
  1028. *vaddr_handle = vaddr;
  1029. *dma_handle = 0;
  1030. }
  1031. static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1032. gfp_t mem_flags)
  1033. {
  1034. enum dma_data_direction dir;
  1035. int ret = 0;
  1036. /* Map the URB's buffers for DMA access.
  1037. * Lower level HCD code should use *_dma exclusively,
  1038. * unless it uses pio or talks to another transport.
  1039. */
  1040. if (is_root_hub(urb->dev))
  1041. return 0;
  1042. if (usb_endpoint_xfer_control(&urb->ep->desc)
  1043. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
  1044. if (hcd->self.uses_dma)
  1045. urb->setup_dma = dma_map_single(
  1046. hcd->self.controller,
  1047. urb->setup_packet,
  1048. sizeof(struct usb_ctrlrequest),
  1049. DMA_TO_DEVICE);
  1050. else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1051. ret = hcd_alloc_coherent(
  1052. urb->dev->bus, mem_flags,
  1053. &urb->setup_dma,
  1054. (void **)&urb->setup_packet,
  1055. sizeof(struct usb_ctrlrequest),
  1056. DMA_TO_DEVICE);
  1057. }
  1058. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1059. if (ret == 0 && urb->transfer_buffer_length != 0
  1060. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1061. if (hcd->self.uses_dma)
  1062. urb->transfer_dma = dma_map_single (
  1063. hcd->self.controller,
  1064. urb->transfer_buffer,
  1065. urb->transfer_buffer_length,
  1066. dir);
  1067. else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1068. ret = hcd_alloc_coherent(
  1069. urb->dev->bus, mem_flags,
  1070. &urb->transfer_dma,
  1071. &urb->transfer_buffer,
  1072. urb->transfer_buffer_length,
  1073. dir);
  1074. if (ret && usb_endpoint_xfer_control(&urb->ep->desc)
  1075. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1076. hcd_free_coherent(urb->dev->bus,
  1077. &urb->setup_dma,
  1078. (void **)&urb->setup_packet,
  1079. sizeof(struct usb_ctrlrequest),
  1080. DMA_TO_DEVICE);
  1081. }
  1082. }
  1083. return ret;
  1084. }
  1085. static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1086. {
  1087. enum dma_data_direction dir;
  1088. if (is_root_hub(urb->dev))
  1089. return;
  1090. if (usb_endpoint_xfer_control(&urb->ep->desc)
  1091. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
  1092. if (hcd->self.uses_dma)
  1093. dma_unmap_single(hcd->self.controller, urb->setup_dma,
  1094. sizeof(struct usb_ctrlrequest),
  1095. DMA_TO_DEVICE);
  1096. else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1097. hcd_free_coherent(urb->dev->bus, &urb->setup_dma,
  1098. (void **)&urb->setup_packet,
  1099. sizeof(struct usb_ctrlrequest),
  1100. DMA_TO_DEVICE);
  1101. }
  1102. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1103. if (urb->transfer_buffer_length != 0
  1104. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1105. if (hcd->self.uses_dma)
  1106. dma_unmap_single(hcd->self.controller,
  1107. urb->transfer_dma,
  1108. urb->transfer_buffer_length,
  1109. dir);
  1110. else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1111. hcd_free_coherent(urb->dev->bus, &urb->transfer_dma,
  1112. &urb->transfer_buffer,
  1113. urb->transfer_buffer_length,
  1114. dir);
  1115. }
  1116. }
  1117. /*-------------------------------------------------------------------------*/
  1118. /* may be called in any context with a valid urb->dev usecount
  1119. * caller surrenders "ownership" of urb
  1120. * expects usb_submit_urb() to have sanity checked and conditioned all
  1121. * inputs in the urb
  1122. */
  1123. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  1124. {
  1125. int status;
  1126. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1127. /* increment urb's reference count as part of giving it to the HCD
  1128. * (which will control it). HCD guarantees that it either returns
  1129. * an error or calls giveback(), but not both.
  1130. */
  1131. usb_get_urb(urb);
  1132. atomic_inc(&urb->use_count);
  1133. atomic_inc(&urb->dev->urbnum);
  1134. usbmon_urb_submit(&hcd->self, urb);
  1135. /* NOTE requirements on root-hub callers (usbfs and the hub
  1136. * driver, for now): URBs' urb->transfer_buffer must be
  1137. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1138. * they could clobber root hub response data. Also, control
  1139. * URBs must be submitted in process context with interrupts
  1140. * enabled.
  1141. */
  1142. status = map_urb_for_dma(hcd, urb, mem_flags);
  1143. if (unlikely(status)) {
  1144. usbmon_urb_submit_error(&hcd->self, urb, status);
  1145. goto error;
  1146. }
  1147. if (is_root_hub(urb->dev))
  1148. status = rh_urb_enqueue(hcd, urb);
  1149. else
  1150. status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
  1151. if (unlikely(status)) {
  1152. usbmon_urb_submit_error(&hcd->self, urb, status);
  1153. unmap_urb_for_dma(hcd, urb);
  1154. error:
  1155. urb->hcpriv = NULL;
  1156. INIT_LIST_HEAD(&urb->urb_list);
  1157. atomic_dec(&urb->use_count);
  1158. atomic_dec(&urb->dev->urbnum);
  1159. if (urb->reject)
  1160. wake_up(&usb_kill_urb_queue);
  1161. usb_put_urb(urb);
  1162. }
  1163. return status;
  1164. }
  1165. /*-------------------------------------------------------------------------*/
  1166. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1167. * off hardware queues (which may take a while) and returning it as
  1168. * soon as practical. we've already set up the urb's return status,
  1169. * but we can't know if the callback completed already.
  1170. */
  1171. static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
  1172. {
  1173. int value;
  1174. if (is_root_hub(urb->dev))
  1175. value = usb_rh_urb_dequeue(hcd, urb, status);
  1176. else {
  1177. /* The only reason an HCD might fail this call is if
  1178. * it has not yet fully queued the urb to begin with.
  1179. * Such failures should be harmless. */
  1180. value = hcd->driver->urb_dequeue(hcd, urb, status);
  1181. }
  1182. return value;
  1183. }
  1184. /*
  1185. * called in any context
  1186. *
  1187. * caller guarantees urb won't be recycled till both unlink()
  1188. * and the urb's completion function return
  1189. */
  1190. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1191. {
  1192. struct usb_hcd *hcd;
  1193. int retval;
  1194. hcd = bus_to_hcd(urb->dev->bus);
  1195. retval = unlink1(hcd, urb, status);
  1196. if (retval == 0)
  1197. retval = -EINPROGRESS;
  1198. else if (retval != -EIDRM && retval != -EBUSY)
  1199. dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
  1200. urb, retval);
  1201. return retval;
  1202. }
  1203. /*-------------------------------------------------------------------------*/
  1204. /**
  1205. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1206. * @hcd: host controller returning the URB
  1207. * @urb: urb being returned to the USB device driver.
  1208. * @status: completion status code for the URB.
  1209. * Context: in_interrupt()
  1210. *
  1211. * This hands the URB from HCD to its USB device driver, using its
  1212. * completion function. The HCD has freed all per-urb resources
  1213. * (and is done using urb->hcpriv). It also released all HCD locks;
  1214. * the device driver won't cause problems if it frees, modifies,
  1215. * or resubmits this URB.
  1216. *
  1217. * If @urb was unlinked, the value of @status will be overridden by
  1218. * @urb->unlinked. Erroneous short transfers are detected in case
  1219. * the HCD hasn't checked for them.
  1220. */
  1221. void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
  1222. {
  1223. urb->hcpriv = NULL;
  1224. if (unlikely(urb->unlinked))
  1225. status = urb->unlinked;
  1226. else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1227. urb->actual_length < urb->transfer_buffer_length &&
  1228. !status))
  1229. status = -EREMOTEIO;
  1230. unmap_urb_for_dma(hcd, urb);
  1231. usbmon_urb_complete(&hcd->self, urb, status);
  1232. usb_unanchor_urb(urb);
  1233. /* pass ownership to the completion handler */
  1234. urb->status = status;
  1235. urb->complete (urb);
  1236. atomic_dec (&urb->use_count);
  1237. if (unlikely (urb->reject))
  1238. wake_up (&usb_kill_urb_queue);
  1239. usb_put_urb (urb);
  1240. }
  1241. EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
  1242. /*-------------------------------------------------------------------------*/
  1243. /* Cancel all URBs pending on this endpoint and wait for the endpoint's
  1244. * queue to drain completely. The caller must first insure that no more
  1245. * URBs can be submitted for this endpoint.
  1246. */
  1247. void usb_hcd_flush_endpoint(struct usb_device *udev,
  1248. struct usb_host_endpoint *ep)
  1249. {
  1250. struct usb_hcd *hcd;
  1251. struct urb *urb;
  1252. if (!ep)
  1253. return;
  1254. might_sleep();
  1255. hcd = bus_to_hcd(udev->bus);
  1256. /* No more submits can occur */
  1257. spin_lock_irq(&hcd_urb_list_lock);
  1258. rescan:
  1259. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1260. int is_in;
  1261. if (urb->unlinked)
  1262. continue;
  1263. usb_get_urb (urb);
  1264. is_in = usb_urb_dir_in(urb);
  1265. spin_unlock(&hcd_urb_list_lock);
  1266. /* kick hcd */
  1267. unlink1(hcd, urb, -ESHUTDOWN);
  1268. dev_dbg (hcd->self.controller,
  1269. "shutdown urb %p ep%d%s%s\n",
  1270. urb, usb_endpoint_num(&ep->desc),
  1271. is_in ? "in" : "out",
  1272. ({ char *s;
  1273. switch (usb_endpoint_type(&ep->desc)) {
  1274. case USB_ENDPOINT_XFER_CONTROL:
  1275. s = ""; break;
  1276. case USB_ENDPOINT_XFER_BULK:
  1277. s = "-bulk"; break;
  1278. case USB_ENDPOINT_XFER_INT:
  1279. s = "-intr"; break;
  1280. default:
  1281. s = "-iso"; break;
  1282. };
  1283. s;
  1284. }));
  1285. usb_put_urb (urb);
  1286. /* list contents may have changed */
  1287. spin_lock(&hcd_urb_list_lock);
  1288. goto rescan;
  1289. }
  1290. spin_unlock_irq(&hcd_urb_list_lock);
  1291. /* Wait until the endpoint queue is completely empty */
  1292. while (!list_empty (&ep->urb_list)) {
  1293. spin_lock_irq(&hcd_urb_list_lock);
  1294. /* The list may have changed while we acquired the spinlock */
  1295. urb = NULL;
  1296. if (!list_empty (&ep->urb_list)) {
  1297. urb = list_entry (ep->urb_list.prev, struct urb,
  1298. urb_list);
  1299. usb_get_urb (urb);
  1300. }
  1301. spin_unlock_irq(&hcd_urb_list_lock);
  1302. if (urb) {
  1303. usb_kill_urb (urb);
  1304. usb_put_urb (urb);
  1305. }
  1306. }
  1307. }
  1308. /* Disables the endpoint: synchronizes with the hcd to make sure all
  1309. * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
  1310. * have been called previously. Use for set_configuration, set_interface,
  1311. * driver removal, physical disconnect.
  1312. *
  1313. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1314. * type, maxpacket size, toggle, halt status, and scheduling.
  1315. */
  1316. void usb_hcd_disable_endpoint(struct usb_device *udev,
  1317. struct usb_host_endpoint *ep)
  1318. {
  1319. struct usb_hcd *hcd;
  1320. might_sleep();
  1321. hcd = bus_to_hcd(udev->bus);
  1322. if (hcd->driver->endpoint_disable)
  1323. hcd->driver->endpoint_disable(hcd, ep);
  1324. }
  1325. /*-------------------------------------------------------------------------*/
  1326. /* called in any context */
  1327. int usb_hcd_get_frame_number (struct usb_device *udev)
  1328. {
  1329. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1330. if (!HC_IS_RUNNING (hcd->state))
  1331. return -ESHUTDOWN;
  1332. return hcd->driver->get_frame_number (hcd);
  1333. }
  1334. /*-------------------------------------------------------------------------*/
  1335. #ifdef CONFIG_PM
  1336. int hcd_bus_suspend(struct usb_device *rhdev)
  1337. {
  1338. struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
  1339. int status;
  1340. int old_state = hcd->state;
  1341. dev_dbg(&rhdev->dev, "bus %s%s\n",
  1342. rhdev->auto_pm ? "auto-" : "", "suspend");
  1343. if (!hcd->driver->bus_suspend) {
  1344. status = -ENOENT;
  1345. } else {
  1346. hcd->state = HC_STATE_QUIESCING;
  1347. status = hcd->driver->bus_suspend(hcd);
  1348. }
  1349. if (status == 0) {
  1350. usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
  1351. hcd->state = HC_STATE_SUSPENDED;
  1352. } else {
  1353. hcd->state = old_state;
  1354. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1355. "suspend", status);
  1356. }
  1357. return status;
  1358. }
  1359. int hcd_bus_resume(struct usb_device *rhdev)
  1360. {
  1361. struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
  1362. int status;
  1363. int old_state = hcd->state;
  1364. dev_dbg(&rhdev->dev, "usb %s%s\n",
  1365. rhdev->auto_pm ? "auto-" : "", "resume");
  1366. if (!hcd->driver->bus_resume)
  1367. return -ENOENT;
  1368. if (hcd->state == HC_STATE_RUNNING)
  1369. return 0;
  1370. hcd->state = HC_STATE_RESUMING;
  1371. status = hcd->driver->bus_resume(hcd);
  1372. if (status == 0) {
  1373. /* TRSMRCY = 10 msec */
  1374. msleep(10);
  1375. usb_set_device_state(rhdev, rhdev->actconfig
  1376. ? USB_STATE_CONFIGURED
  1377. : USB_STATE_ADDRESS);
  1378. hcd->state = HC_STATE_RUNNING;
  1379. } else {
  1380. hcd->state = old_state;
  1381. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1382. "resume", status);
  1383. if (status != -ESHUTDOWN)
  1384. usb_hc_died(hcd);
  1385. }
  1386. return status;
  1387. }
  1388. /* Workqueue routine for root-hub remote wakeup */
  1389. static void hcd_resume_work(struct work_struct *work)
  1390. {
  1391. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  1392. struct usb_device *udev = hcd->self.root_hub;
  1393. usb_lock_device(udev);
  1394. usb_mark_last_busy(udev);
  1395. usb_external_resume_device(udev);
  1396. usb_unlock_device(udev);
  1397. }
  1398. /**
  1399. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1400. * @hcd: host controller for this root hub
  1401. *
  1402. * The USB host controller calls this function when its root hub is
  1403. * suspended (with the remote wakeup feature enabled) and a remote
  1404. * wakeup request is received. The routine submits a workqueue request
  1405. * to resume the root hub (that is, manage its downstream ports again).
  1406. */
  1407. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1408. {
  1409. unsigned long flags;
  1410. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1411. if (hcd->rh_registered)
  1412. queue_work(ksuspend_usb_wq, &hcd->wakeup_work);
  1413. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1414. }
  1415. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1416. #endif
  1417. /*-------------------------------------------------------------------------*/
  1418. #ifdef CONFIG_USB_OTG
  1419. /**
  1420. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1421. * @bus: the bus (must use hcd framework)
  1422. * @port_num: 1-based number of port; usually bus->otg_port
  1423. * Context: in_interrupt()
  1424. *
  1425. * Starts enumeration, with an immediate reset followed later by
  1426. * khubd identifying and possibly configuring the device.
  1427. * This is needed by OTG controller drivers, where it helps meet
  1428. * HNP protocol timing requirements for starting a port reset.
  1429. */
  1430. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1431. {
  1432. struct usb_hcd *hcd;
  1433. int status = -EOPNOTSUPP;
  1434. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1435. * boards with root hubs hooked up to internal devices (instead of
  1436. * just the OTG port) may need more attention to resetting...
  1437. */
  1438. hcd = container_of (bus, struct usb_hcd, self);
  1439. if (port_num && hcd->driver->start_port_reset)
  1440. status = hcd->driver->start_port_reset(hcd, port_num);
  1441. /* run khubd shortly after (first) root port reset finishes;
  1442. * it may issue others, until at least 50 msecs have passed.
  1443. */
  1444. if (status == 0)
  1445. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1446. return status;
  1447. }
  1448. EXPORT_SYMBOL_GPL(usb_bus_start_enum);
  1449. #endif
  1450. /*-------------------------------------------------------------------------*/
  1451. /**
  1452. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1453. * @irq: the IRQ being raised
  1454. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1455. *
  1456. * If the controller isn't HALTed, calls the driver's irq handler.
  1457. * Checks whether the controller is now dead.
  1458. */
  1459. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  1460. {
  1461. struct usb_hcd *hcd = __hcd;
  1462. unsigned long flags;
  1463. irqreturn_t rc;
  1464. /* IRQF_DISABLED doesn't work correctly with shared IRQs
  1465. * when the first handler doesn't use it. So let's just
  1466. * assume it's never used.
  1467. */
  1468. local_irq_save(flags);
  1469. if (unlikely(hcd->state == HC_STATE_HALT ||
  1470. !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
  1471. rc = IRQ_NONE;
  1472. } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
  1473. rc = IRQ_NONE;
  1474. } else {
  1475. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1476. if (unlikely(hcd->state == HC_STATE_HALT))
  1477. usb_hc_died(hcd);
  1478. rc = IRQ_HANDLED;
  1479. }
  1480. local_irq_restore(flags);
  1481. return rc;
  1482. }
  1483. /*-------------------------------------------------------------------------*/
  1484. /**
  1485. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1486. * @hcd: pointer to the HCD representing the controller
  1487. *
  1488. * This is called by bus glue to report a USB host controller that died
  1489. * while operations may still have been pending. It's called automatically
  1490. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1491. */
  1492. void usb_hc_died (struct usb_hcd *hcd)
  1493. {
  1494. unsigned long flags;
  1495. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1496. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1497. if (hcd->rh_registered) {
  1498. hcd->poll_rh = 0;
  1499. /* make khubd clean up old urbs and devices */
  1500. usb_set_device_state (hcd->self.root_hub,
  1501. USB_STATE_NOTATTACHED);
  1502. usb_kick_khubd (hcd->self.root_hub);
  1503. }
  1504. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1505. }
  1506. EXPORT_SYMBOL_GPL (usb_hc_died);
  1507. /*-------------------------------------------------------------------------*/
  1508. /**
  1509. * usb_create_hcd - create and initialize an HCD structure
  1510. * @driver: HC driver that will use this hcd
  1511. * @dev: device for this HC, stored in hcd->self.controller
  1512. * @bus_name: value to store in hcd->self.bus_name
  1513. * Context: !in_interrupt()
  1514. *
  1515. * Allocate a struct usb_hcd, with extra space at the end for the
  1516. * HC driver's private data. Initialize the generic members of the
  1517. * hcd structure.
  1518. *
  1519. * If memory is unavailable, returns NULL.
  1520. */
  1521. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1522. struct device *dev, const char *bus_name)
  1523. {
  1524. struct usb_hcd *hcd;
  1525. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1526. if (!hcd) {
  1527. dev_dbg (dev, "hcd alloc failed\n");
  1528. return NULL;
  1529. }
  1530. dev_set_drvdata(dev, hcd);
  1531. kref_init(&hcd->kref);
  1532. usb_bus_init(&hcd->self);
  1533. hcd->self.controller = dev;
  1534. hcd->self.bus_name = bus_name;
  1535. hcd->self.uses_dma = (dev->dma_mask != NULL);
  1536. init_timer(&hcd->rh_timer);
  1537. hcd->rh_timer.function = rh_timer_func;
  1538. hcd->rh_timer.data = (unsigned long) hcd;
  1539. #ifdef CONFIG_PM
  1540. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  1541. #endif
  1542. hcd->driver = driver;
  1543. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1544. "USB Host Controller";
  1545. return hcd;
  1546. }
  1547. EXPORT_SYMBOL_GPL(usb_create_hcd);
  1548. static void hcd_release (struct kref *kref)
  1549. {
  1550. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  1551. kfree(hcd);
  1552. }
  1553. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  1554. {
  1555. if (hcd)
  1556. kref_get (&hcd->kref);
  1557. return hcd;
  1558. }
  1559. EXPORT_SYMBOL_GPL(usb_get_hcd);
  1560. void usb_put_hcd (struct usb_hcd *hcd)
  1561. {
  1562. if (hcd)
  1563. kref_put (&hcd->kref, hcd_release);
  1564. }
  1565. EXPORT_SYMBOL_GPL(usb_put_hcd);
  1566. /**
  1567. * usb_add_hcd - finish generic HCD structure initialization and register
  1568. * @hcd: the usb_hcd structure to initialize
  1569. * @irqnum: Interrupt line to allocate
  1570. * @irqflags: Interrupt type flags
  1571. *
  1572. * Finish the remaining parts of generic HCD initialization: allocate the
  1573. * buffers of consistent memory, register the bus, request the IRQ line,
  1574. * and call the driver's reset() and start() routines.
  1575. */
  1576. int usb_add_hcd(struct usb_hcd *hcd,
  1577. unsigned int irqnum, unsigned long irqflags)
  1578. {
  1579. int retval;
  1580. struct usb_device *rhdev;
  1581. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1582. hcd->authorized_default = hcd->wireless? 0 : 1;
  1583. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1584. /* HC is in reset state, but accessible. Now do the one-time init,
  1585. * bottom up so that hcds can customize the root hubs before khubd
  1586. * starts talking to them. (Note, bus id is assigned early too.)
  1587. */
  1588. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1589. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1590. return retval;
  1591. }
  1592. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1593. goto err_register_bus;
  1594. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1595. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1596. retval = -ENOMEM;
  1597. goto err_allocate_root_hub;
  1598. }
  1599. rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
  1600. USB_SPEED_FULL;
  1601. hcd->self.root_hub = rhdev;
  1602. /* wakeup flag init defaults to "everything works" for root hubs,
  1603. * but drivers can override it in reset() if needed, along with
  1604. * recording the overall controller's system wakeup capability.
  1605. */
  1606. device_init_wakeup(&rhdev->dev, 1);
  1607. /* "reset" is misnamed; its role is now one-time init. the controller
  1608. * should already have been reset (and boot firmware kicked off etc).
  1609. */
  1610. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1611. dev_err(hcd->self.controller, "can't setup\n");
  1612. goto err_hcd_driver_setup;
  1613. }
  1614. /* NOTE: root hub and controller capabilities may not be the same */
  1615. if (device_can_wakeup(hcd->self.controller)
  1616. && device_can_wakeup(&hcd->self.root_hub->dev))
  1617. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1618. /* enable irqs just before we start the controller */
  1619. if (hcd->driver->irq) {
  1620. /* IRQF_DISABLED doesn't work as advertised when used together
  1621. * with IRQF_SHARED. As usb_hcd_irq() will always disable
  1622. * interrupts we can remove it here.
  1623. */
  1624. if (irqflags & IRQF_SHARED)
  1625. irqflags &= ~IRQF_DISABLED;
  1626. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1627. hcd->driver->description, hcd->self.busnum);
  1628. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1629. hcd->irq_descr, hcd)) != 0) {
  1630. dev_err(hcd->self.controller,
  1631. "request interrupt %d failed\n", irqnum);
  1632. goto err_request_irq;
  1633. }
  1634. hcd->irq = irqnum;
  1635. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  1636. (hcd->driver->flags & HCD_MEMORY) ?
  1637. "io mem" : "io base",
  1638. (unsigned long long)hcd->rsrc_start);
  1639. } else {
  1640. hcd->irq = -1;
  1641. if (hcd->rsrc_start)
  1642. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1643. (hcd->driver->flags & HCD_MEMORY) ?
  1644. "io mem" : "io base",
  1645. (unsigned long long)hcd->rsrc_start);
  1646. }
  1647. if ((retval = hcd->driver->start(hcd)) < 0) {
  1648. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1649. goto err_hcd_driver_start;
  1650. }
  1651. /* starting here, usbcore will pay attention to this root hub */
  1652. rhdev->bus_mA = min(500u, hcd->power_budget);
  1653. if ((retval = register_root_hub(hcd)) != 0)
  1654. goto err_register_root_hub;
  1655. retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  1656. if (retval < 0) {
  1657. printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
  1658. retval);
  1659. goto error_create_attr_group;
  1660. }
  1661. if (hcd->uses_new_polling && hcd->poll_rh)
  1662. usb_hcd_poll_rh_status(hcd);
  1663. return retval;
  1664. error_create_attr_group:
  1665. mutex_lock(&usb_bus_list_lock);
  1666. usb_disconnect(&hcd->self.root_hub);
  1667. mutex_unlock(&usb_bus_list_lock);
  1668. err_register_root_hub:
  1669. hcd->driver->stop(hcd);
  1670. err_hcd_driver_start:
  1671. if (hcd->irq >= 0)
  1672. free_irq(irqnum, hcd);
  1673. err_request_irq:
  1674. err_hcd_driver_setup:
  1675. hcd->self.root_hub = NULL;
  1676. usb_put_dev(rhdev);
  1677. err_allocate_root_hub:
  1678. usb_deregister_bus(&hcd->self);
  1679. err_register_bus:
  1680. hcd_buffer_destroy(hcd);
  1681. return retval;
  1682. }
  1683. EXPORT_SYMBOL_GPL(usb_add_hcd);
  1684. /**
  1685. * usb_remove_hcd - shutdown processing for generic HCDs
  1686. * @hcd: the usb_hcd structure to remove
  1687. * Context: !in_interrupt()
  1688. *
  1689. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1690. * invoking the HCD's stop() method.
  1691. */
  1692. void usb_remove_hcd(struct usb_hcd *hcd)
  1693. {
  1694. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1695. if (HC_IS_RUNNING (hcd->state))
  1696. hcd->state = HC_STATE_QUIESCING;
  1697. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1698. spin_lock_irq (&hcd_root_hub_lock);
  1699. hcd->rh_registered = 0;
  1700. spin_unlock_irq (&hcd_root_hub_lock);
  1701. #ifdef CONFIG_PM
  1702. cancel_work_sync(&hcd->wakeup_work);
  1703. #endif
  1704. sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
  1705. mutex_lock(&usb_bus_list_lock);
  1706. usb_disconnect(&hcd->self.root_hub);
  1707. mutex_unlock(&usb_bus_list_lock);
  1708. hcd->driver->stop(hcd);
  1709. hcd->state = HC_STATE_HALT;
  1710. hcd->poll_rh = 0;
  1711. del_timer_sync(&hcd->rh_timer);
  1712. if (hcd->irq >= 0)
  1713. free_irq(hcd->irq, hcd);
  1714. usb_deregister_bus(&hcd->self);
  1715. hcd_buffer_destroy(hcd);
  1716. }
  1717. EXPORT_SYMBOL_GPL(usb_remove_hcd);
  1718. void
  1719. usb_hcd_platform_shutdown(struct platform_device* dev)
  1720. {
  1721. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1722. if (hcd->driver->shutdown)
  1723. hcd->driver->shutdown(hcd);
  1724. }
  1725. EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
  1726. /*-------------------------------------------------------------------------*/
  1727. #if defined(CONFIG_USB_MON)
  1728. struct usb_mon_operations *mon_ops;
  1729. /*
  1730. * The registration is unlocked.
  1731. * We do it this way because we do not want to lock in hot paths.
  1732. *
  1733. * Notice that the code is minimally error-proof. Because usbmon needs
  1734. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1735. */
  1736. int usb_mon_register (struct usb_mon_operations *ops)
  1737. {
  1738. if (mon_ops)
  1739. return -EBUSY;
  1740. mon_ops = ops;
  1741. mb();
  1742. return 0;
  1743. }
  1744. EXPORT_SYMBOL_GPL (usb_mon_register);
  1745. void usb_mon_deregister (void)
  1746. {
  1747. if (mon_ops == NULL) {
  1748. printk(KERN_ERR "USB: monitor was not registered\n");
  1749. return;
  1750. }
  1751. mon_ops = NULL;
  1752. mb();
  1753. }
  1754. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  1755. #endif /* CONFIG_USB_MON */