hcd.c 58 KB

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