hcd.c 59 KB

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