hcd.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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. void usb_enable_root_hub_irq (struct usb_bus *bus)
  793. {
  794. struct usb_hcd *hcd;
  795. hcd = container_of (bus, struct usb_hcd, self);
  796. if (hcd->driver->hub_irq_enable && hcd->state != HC_STATE_HALT)
  797. hcd->driver->hub_irq_enable (hcd);
  798. }
  799. /*-------------------------------------------------------------------------*/
  800. /**
  801. * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
  802. * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
  803. * @is_input: true iff the transaction sends data to the host
  804. * @isoc: true for isochronous transactions, false for interrupt ones
  805. * @bytecount: how many bytes in the transaction.
  806. *
  807. * Returns approximate bus time in nanoseconds for a periodic transaction.
  808. * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
  809. * scheduled in software, this function is only used for such scheduling.
  810. */
  811. long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  812. {
  813. unsigned long tmp;
  814. switch (speed) {
  815. case USB_SPEED_LOW: /* INTR only */
  816. if (is_input) {
  817. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  818. return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  819. } else {
  820. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  821. return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
  822. }
  823. case USB_SPEED_FULL: /* ISOC or INTR */
  824. if (isoc) {
  825. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  826. return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
  827. } else {
  828. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  829. return (9107L + BW_HOST_DELAY + tmp);
  830. }
  831. case USB_SPEED_HIGH: /* ISOC or INTR */
  832. // FIXME adjust for input vs output
  833. if (isoc)
  834. tmp = HS_NSECS_ISO (bytecount);
  835. else
  836. tmp = HS_NSECS (bytecount);
  837. return tmp;
  838. default:
  839. pr_debug ("%s: bogus device speed!\n", usbcore_name);
  840. return -1;
  841. }
  842. }
  843. EXPORT_SYMBOL_GPL(usb_calc_bus_time);
  844. /*-------------------------------------------------------------------------*/
  845. /*
  846. * Generic HC operations.
  847. */
  848. /*-------------------------------------------------------------------------*/
  849. /**
  850. * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
  851. * @hcd: host controller to which @urb was submitted
  852. * @urb: URB being submitted
  853. *
  854. * Host controller drivers should call this routine in their enqueue()
  855. * method. The HCD's private spinlock must be held and interrupts must
  856. * be disabled. The actions carried out here are required for URB
  857. * submission, as well as for endpoint shutdown and for usb_kill_urb.
  858. *
  859. * Returns 0 for no error, otherwise a negative error code (in which case
  860. * the enqueue() method must fail). If no error occurs but enqueue() fails
  861. * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
  862. * the private spinlock and returning.
  863. */
  864. int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
  865. {
  866. int rc = 0;
  867. spin_lock(&hcd_urb_list_lock);
  868. /* Check that the URB isn't being killed */
  869. if (unlikely(urb->reject)) {
  870. rc = -EPERM;
  871. goto done;
  872. }
  873. if (unlikely(!urb->ep->enabled)) {
  874. rc = -ENOENT;
  875. goto done;
  876. }
  877. if (unlikely(!urb->dev->can_submit)) {
  878. rc = -EHOSTUNREACH;
  879. goto done;
  880. }
  881. /*
  882. * Check the host controller's state and add the URB to the
  883. * endpoint's queue.
  884. */
  885. switch (hcd->state) {
  886. case HC_STATE_RUNNING:
  887. case HC_STATE_RESUMING:
  888. urb->unlinked = 0;
  889. list_add_tail(&urb->urb_list, &urb->ep->urb_list);
  890. break;
  891. default:
  892. rc = -ESHUTDOWN;
  893. goto done;
  894. }
  895. done:
  896. spin_unlock(&hcd_urb_list_lock);
  897. return rc;
  898. }
  899. EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
  900. /**
  901. * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
  902. * @hcd: host controller to which @urb was submitted
  903. * @urb: URB being checked for unlinkability
  904. * @status: error code to store in @urb if the unlink succeeds
  905. *
  906. * Host controller drivers should call this routine in their dequeue()
  907. * method. The HCD's private spinlock must be held and interrupts must
  908. * be disabled. The actions carried out here are required for making
  909. * sure than an unlink is valid.
  910. *
  911. * Returns 0 for no error, otherwise a negative error code (in which case
  912. * the dequeue() method must fail). The possible error codes are:
  913. *
  914. * -EIDRM: @urb was not submitted or has already completed.
  915. * The completion function may not have been called yet.
  916. *
  917. * -EBUSY: @urb has already been unlinked.
  918. */
  919. int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
  920. int status)
  921. {
  922. struct list_head *tmp;
  923. /* insist the urb is still queued */
  924. list_for_each(tmp, &urb->ep->urb_list) {
  925. if (tmp == &urb->urb_list)
  926. break;
  927. }
  928. if (tmp != &urb->urb_list)
  929. return -EIDRM;
  930. /* Any status except -EINPROGRESS means something already started to
  931. * unlink this URB from the hardware. So there's no more work to do.
  932. */
  933. if (urb->unlinked)
  934. return -EBUSY;
  935. urb->unlinked = status;
  936. /* IRQ setup can easily be broken so that USB controllers
  937. * never get completion IRQs ... maybe even the ones we need to
  938. * finish unlinking the initial failed usb_set_address()
  939. * or device descriptor fetch.
  940. */
  941. if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) &&
  942. !is_root_hub(urb->dev)) {
  943. dev_warn(hcd->self.controller, "Unlink after no-IRQ? "
  944. "Controller is probably using the wrong IRQ.\n");
  945. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  946. }
  947. return 0;
  948. }
  949. EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
  950. /**
  951. * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
  952. * @hcd: host controller to which @urb was submitted
  953. * @urb: URB being unlinked
  954. *
  955. * Host controller drivers should call this routine before calling
  956. * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
  957. * interrupts must be disabled. The actions carried out here are required
  958. * for URB completion.
  959. */
  960. void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
  961. {
  962. /* clear all state linking urb to this dev (and hcd) */
  963. spin_lock(&hcd_urb_list_lock);
  964. list_del_init(&urb->urb_list);
  965. spin_unlock(&hcd_urb_list_lock);
  966. }
  967. EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
  968. /*
  969. * Some usb host controllers can only perform dma using a small SRAM area.
  970. * The usb core itself is however optimized for host controllers that can dma
  971. * using regular system memory - like pci devices doing bus mastering.
  972. *
  973. * To support host controllers with limited dma capabilites we provide dma
  974. * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
  975. * For this to work properly the host controller code must first use the
  976. * function dma_declare_coherent_memory() to point out which memory area
  977. * that should be used for dma allocations.
  978. *
  979. * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
  980. * dma using dma_alloc_coherent() which in turn allocates from the memory
  981. * area pointed out with dma_declare_coherent_memory().
  982. *
  983. * So, to summarize...
  984. *
  985. * - We need "local" memory, canonical example being
  986. * a small SRAM on a discrete controller being the
  987. * only memory that the controller can read ...
  988. * (a) "normal" kernel memory is no good, and
  989. * (b) there's not enough to share
  990. *
  991. * - The only *portable* hook for such stuff in the
  992. * DMA framework is dma_declare_coherent_memory()
  993. *
  994. * - So we use that, even though the primary requirement
  995. * is that the memory be "local" (hence addressible
  996. * by that device), not "coherent".
  997. *
  998. */
  999. static int hcd_alloc_coherent(struct usb_bus *bus,
  1000. gfp_t mem_flags, dma_addr_t *dma_handle,
  1001. void **vaddr_handle, size_t size,
  1002. enum dma_data_direction dir)
  1003. {
  1004. unsigned char *vaddr;
  1005. vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
  1006. mem_flags, dma_handle);
  1007. if (!vaddr)
  1008. return -ENOMEM;
  1009. /*
  1010. * Store the virtual address of the buffer at the end
  1011. * of the allocated dma buffer. The size of the buffer
  1012. * may be uneven so use unaligned functions instead
  1013. * of just rounding up. It makes sense to optimize for
  1014. * memory footprint over access speed since the amount
  1015. * of memory available for dma may be limited.
  1016. */
  1017. put_unaligned((unsigned long)*vaddr_handle,
  1018. (unsigned long *)(vaddr + size));
  1019. if (dir == DMA_TO_DEVICE)
  1020. memcpy(vaddr, *vaddr_handle, size);
  1021. *vaddr_handle = vaddr;
  1022. return 0;
  1023. }
  1024. static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
  1025. void **vaddr_handle, size_t size,
  1026. enum dma_data_direction dir)
  1027. {
  1028. unsigned char *vaddr = *vaddr_handle;
  1029. vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
  1030. if (dir == DMA_FROM_DEVICE)
  1031. memcpy(vaddr, *vaddr_handle, size);
  1032. hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
  1033. *vaddr_handle = vaddr;
  1034. *dma_handle = 0;
  1035. }
  1036. static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
  1037. gfp_t mem_flags)
  1038. {
  1039. enum dma_data_direction dir;
  1040. int ret = 0;
  1041. /* Map the URB's buffers for DMA access.
  1042. * Lower level HCD code should use *_dma exclusively,
  1043. * unless it uses pio or talks to another transport.
  1044. */
  1045. if (is_root_hub(urb->dev))
  1046. return 0;
  1047. if (usb_endpoint_xfer_control(&urb->ep->desc)
  1048. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
  1049. if (hcd->self.uses_dma)
  1050. urb->setup_dma = dma_map_single(
  1051. hcd->self.controller,
  1052. urb->setup_packet,
  1053. sizeof(struct usb_ctrlrequest),
  1054. DMA_TO_DEVICE);
  1055. else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1056. ret = hcd_alloc_coherent(
  1057. urb->dev->bus, mem_flags,
  1058. &urb->setup_dma,
  1059. (void **)&urb->setup_packet,
  1060. sizeof(struct usb_ctrlrequest),
  1061. DMA_TO_DEVICE);
  1062. }
  1063. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1064. if (ret == 0 && urb->transfer_buffer_length != 0
  1065. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1066. if (hcd->self.uses_dma)
  1067. urb->transfer_dma = dma_map_single (
  1068. hcd->self.controller,
  1069. urb->transfer_buffer,
  1070. urb->transfer_buffer_length,
  1071. dir);
  1072. else if (hcd->driver->flags & HCD_LOCAL_MEM) {
  1073. ret = hcd_alloc_coherent(
  1074. urb->dev->bus, mem_flags,
  1075. &urb->transfer_dma,
  1076. &urb->transfer_buffer,
  1077. urb->transfer_buffer_length,
  1078. dir);
  1079. if (ret && usb_endpoint_xfer_control(&urb->ep->desc)
  1080. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
  1081. hcd_free_coherent(urb->dev->bus,
  1082. &urb->setup_dma,
  1083. (void **)&urb->setup_packet,
  1084. sizeof(struct usb_ctrlrequest),
  1085. DMA_TO_DEVICE);
  1086. }
  1087. }
  1088. return ret;
  1089. }
  1090. static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
  1091. {
  1092. enum dma_data_direction dir;
  1093. if (is_root_hub(urb->dev))
  1094. return;
  1095. if (usb_endpoint_xfer_control(&urb->ep->desc)
  1096. && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
  1097. if (hcd->self.uses_dma)
  1098. dma_unmap_single(hcd->self.controller, urb->setup_dma,
  1099. sizeof(struct usb_ctrlrequest),
  1100. DMA_TO_DEVICE);
  1101. else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1102. hcd_free_coherent(urb->dev->bus, &urb->setup_dma,
  1103. (void **)&urb->setup_packet,
  1104. sizeof(struct usb_ctrlrequest),
  1105. DMA_TO_DEVICE);
  1106. }
  1107. dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1108. if (urb->transfer_buffer_length != 0
  1109. && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
  1110. if (hcd->self.uses_dma)
  1111. dma_unmap_single(hcd->self.controller,
  1112. urb->transfer_dma,
  1113. urb->transfer_buffer_length,
  1114. dir);
  1115. else if (hcd->driver->flags & HCD_LOCAL_MEM)
  1116. hcd_free_coherent(urb->dev->bus, &urb->transfer_dma,
  1117. &urb->transfer_buffer,
  1118. urb->transfer_buffer_length,
  1119. dir);
  1120. }
  1121. }
  1122. /*-------------------------------------------------------------------------*/
  1123. /* may be called in any context with a valid urb->dev usecount
  1124. * caller surrenders "ownership" of urb
  1125. * expects usb_submit_urb() to have sanity checked and conditioned all
  1126. * inputs in the urb
  1127. */
  1128. int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
  1129. {
  1130. int status;
  1131. struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
  1132. /* increment urb's reference count as part of giving it to the HCD
  1133. * (which will control it). HCD guarantees that it either returns
  1134. * an error or calls giveback(), but not both.
  1135. */
  1136. usb_get_urb(urb);
  1137. atomic_inc(&urb->use_count);
  1138. atomic_inc(&urb->dev->urbnum);
  1139. usbmon_urb_submit(&hcd->self, urb);
  1140. /* NOTE requirements on root-hub callers (usbfs and the hub
  1141. * driver, for now): URBs' urb->transfer_buffer must be
  1142. * valid and usb_buffer_{sync,unmap}() not be needed, since
  1143. * they could clobber root hub response data. Also, control
  1144. * URBs must be submitted in process context with interrupts
  1145. * enabled.
  1146. */
  1147. status = map_urb_for_dma(hcd, urb, mem_flags);
  1148. if (unlikely(status)) {
  1149. usbmon_urb_submit_error(&hcd->self, urb, status);
  1150. goto error;
  1151. }
  1152. if (is_root_hub(urb->dev))
  1153. status = rh_urb_enqueue(hcd, urb);
  1154. else
  1155. status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
  1156. if (unlikely(status)) {
  1157. usbmon_urb_submit_error(&hcd->self, urb, status);
  1158. unmap_urb_for_dma(hcd, urb);
  1159. error:
  1160. urb->hcpriv = NULL;
  1161. INIT_LIST_HEAD(&urb->urb_list);
  1162. atomic_dec(&urb->use_count);
  1163. atomic_dec(&urb->dev->urbnum);
  1164. if (urb->reject)
  1165. wake_up(&usb_kill_urb_queue);
  1166. usb_put_urb(urb);
  1167. }
  1168. return status;
  1169. }
  1170. /*-------------------------------------------------------------------------*/
  1171. /* this makes the hcd giveback() the urb more quickly, by kicking it
  1172. * off hardware queues (which may take a while) and returning it as
  1173. * soon as practical. we've already set up the urb's return status,
  1174. * but we can't know if the callback completed already.
  1175. */
  1176. static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
  1177. {
  1178. int value;
  1179. if (is_root_hub(urb->dev))
  1180. value = usb_rh_urb_dequeue(hcd, urb, status);
  1181. else {
  1182. /* The only reason an HCD might fail this call is if
  1183. * it has not yet fully queued the urb to begin with.
  1184. * Such failures should be harmless. */
  1185. value = hcd->driver->urb_dequeue(hcd, urb, status);
  1186. }
  1187. return value;
  1188. }
  1189. /*
  1190. * called in any context
  1191. *
  1192. * caller guarantees urb won't be recycled till both unlink()
  1193. * and the urb's completion function return
  1194. */
  1195. int usb_hcd_unlink_urb (struct urb *urb, int status)
  1196. {
  1197. struct usb_hcd *hcd;
  1198. int retval;
  1199. hcd = bus_to_hcd(urb->dev->bus);
  1200. retval = unlink1(hcd, urb, status);
  1201. if (retval == 0)
  1202. retval = -EINPROGRESS;
  1203. else if (retval != -EIDRM && retval != -EBUSY)
  1204. dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
  1205. urb, retval);
  1206. return retval;
  1207. }
  1208. /*-------------------------------------------------------------------------*/
  1209. /**
  1210. * usb_hcd_giveback_urb - return URB from HCD to device driver
  1211. * @hcd: host controller returning the URB
  1212. * @urb: urb being returned to the USB device driver.
  1213. * @status: completion status code for the URB.
  1214. * Context: in_interrupt()
  1215. *
  1216. * This hands the URB from HCD to its USB device driver, using its
  1217. * completion function. The HCD has freed all per-urb resources
  1218. * (and is done using urb->hcpriv). It also released all HCD locks;
  1219. * the device driver won't cause problems if it frees, modifies,
  1220. * or resubmits this URB.
  1221. *
  1222. * If @urb was unlinked, the value of @status will be overridden by
  1223. * @urb->unlinked. Erroneous short transfers are detected in case
  1224. * the HCD hasn't checked for them.
  1225. */
  1226. void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
  1227. {
  1228. urb->hcpriv = NULL;
  1229. if (unlikely(urb->unlinked))
  1230. status = urb->unlinked;
  1231. else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  1232. urb->actual_length < urb->transfer_buffer_length &&
  1233. !status))
  1234. status = -EREMOTEIO;
  1235. unmap_urb_for_dma(hcd, urb);
  1236. usbmon_urb_complete(&hcd->self, urb, status);
  1237. usb_unanchor_urb(urb);
  1238. /* pass ownership to the completion handler */
  1239. urb->status = status;
  1240. urb->complete (urb);
  1241. atomic_dec (&urb->use_count);
  1242. if (unlikely (urb->reject))
  1243. wake_up (&usb_kill_urb_queue);
  1244. usb_put_urb (urb);
  1245. }
  1246. EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
  1247. /*-------------------------------------------------------------------------*/
  1248. /* Cancel all URBs pending on this endpoint and wait for the endpoint's
  1249. * queue to drain completely. The caller must first insure that no more
  1250. * URBs can be submitted for this endpoint.
  1251. */
  1252. void usb_hcd_flush_endpoint(struct usb_device *udev,
  1253. struct usb_host_endpoint *ep)
  1254. {
  1255. struct usb_hcd *hcd;
  1256. struct urb *urb;
  1257. if (!ep)
  1258. return;
  1259. might_sleep();
  1260. hcd = bus_to_hcd(udev->bus);
  1261. /* No more submits can occur */
  1262. spin_lock_irq(&hcd_urb_list_lock);
  1263. rescan:
  1264. list_for_each_entry (urb, &ep->urb_list, urb_list) {
  1265. int is_in;
  1266. if (urb->unlinked)
  1267. continue;
  1268. usb_get_urb (urb);
  1269. is_in = usb_urb_dir_in(urb);
  1270. spin_unlock(&hcd_urb_list_lock);
  1271. /* kick hcd */
  1272. unlink1(hcd, urb, -ESHUTDOWN);
  1273. dev_dbg (hcd->self.controller,
  1274. "shutdown urb %p ep%d%s%s\n",
  1275. urb, usb_endpoint_num(&ep->desc),
  1276. is_in ? "in" : "out",
  1277. ({ char *s;
  1278. switch (usb_endpoint_type(&ep->desc)) {
  1279. case USB_ENDPOINT_XFER_CONTROL:
  1280. s = ""; break;
  1281. case USB_ENDPOINT_XFER_BULK:
  1282. s = "-bulk"; break;
  1283. case USB_ENDPOINT_XFER_INT:
  1284. s = "-intr"; break;
  1285. default:
  1286. s = "-iso"; break;
  1287. };
  1288. s;
  1289. }));
  1290. usb_put_urb (urb);
  1291. /* list contents may have changed */
  1292. spin_lock(&hcd_urb_list_lock);
  1293. goto rescan;
  1294. }
  1295. spin_unlock_irq(&hcd_urb_list_lock);
  1296. /* Wait until the endpoint queue is completely empty */
  1297. while (!list_empty (&ep->urb_list)) {
  1298. spin_lock_irq(&hcd_urb_list_lock);
  1299. /* The list may have changed while we acquired the spinlock */
  1300. urb = NULL;
  1301. if (!list_empty (&ep->urb_list)) {
  1302. urb = list_entry (ep->urb_list.prev, struct urb,
  1303. urb_list);
  1304. usb_get_urb (urb);
  1305. }
  1306. spin_unlock_irq(&hcd_urb_list_lock);
  1307. if (urb) {
  1308. usb_kill_urb (urb);
  1309. usb_put_urb (urb);
  1310. }
  1311. }
  1312. }
  1313. /* Disables the endpoint: synchronizes with the hcd to make sure all
  1314. * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
  1315. * have been called previously. Use for set_configuration, set_interface,
  1316. * driver removal, physical disconnect.
  1317. *
  1318. * example: a qh stored in ep->hcpriv, holding state related to endpoint
  1319. * type, maxpacket size, toggle, halt status, and scheduling.
  1320. */
  1321. void usb_hcd_disable_endpoint(struct usb_device *udev,
  1322. struct usb_host_endpoint *ep)
  1323. {
  1324. struct usb_hcd *hcd;
  1325. might_sleep();
  1326. hcd = bus_to_hcd(udev->bus);
  1327. if (hcd->driver->endpoint_disable)
  1328. hcd->driver->endpoint_disable(hcd, ep);
  1329. }
  1330. /*-------------------------------------------------------------------------*/
  1331. /* called in any context */
  1332. int usb_hcd_get_frame_number (struct usb_device *udev)
  1333. {
  1334. struct usb_hcd *hcd = bus_to_hcd(udev->bus);
  1335. if (!HC_IS_RUNNING (hcd->state))
  1336. return -ESHUTDOWN;
  1337. return hcd->driver->get_frame_number (hcd);
  1338. }
  1339. /*-------------------------------------------------------------------------*/
  1340. #ifdef CONFIG_PM
  1341. int hcd_bus_suspend(struct usb_device *rhdev)
  1342. {
  1343. struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
  1344. int status;
  1345. int old_state = hcd->state;
  1346. dev_dbg(&rhdev->dev, "bus %s%s\n",
  1347. rhdev->auto_pm ? "auto-" : "", "suspend");
  1348. if (!hcd->driver->bus_suspend) {
  1349. status = -ENOENT;
  1350. } else {
  1351. hcd->state = HC_STATE_QUIESCING;
  1352. status = hcd->driver->bus_suspend(hcd);
  1353. }
  1354. if (status == 0) {
  1355. usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
  1356. hcd->state = HC_STATE_SUSPENDED;
  1357. } else {
  1358. hcd->state = old_state;
  1359. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1360. "suspend", status);
  1361. }
  1362. return status;
  1363. }
  1364. int hcd_bus_resume(struct usb_device *rhdev)
  1365. {
  1366. struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
  1367. int status;
  1368. int old_state = hcd->state;
  1369. dev_dbg(&rhdev->dev, "usb %s%s\n",
  1370. rhdev->auto_pm ? "auto-" : "", "resume");
  1371. if (!hcd->driver->bus_resume)
  1372. return -ENOENT;
  1373. if (hcd->state == HC_STATE_RUNNING)
  1374. return 0;
  1375. hcd->state = HC_STATE_RESUMING;
  1376. status = hcd->driver->bus_resume(hcd);
  1377. if (status == 0) {
  1378. /* TRSMRCY = 10 msec */
  1379. msleep(10);
  1380. usb_set_device_state(rhdev, rhdev->actconfig
  1381. ? USB_STATE_CONFIGURED
  1382. : USB_STATE_ADDRESS);
  1383. hcd->state = HC_STATE_RUNNING;
  1384. } else {
  1385. hcd->state = old_state;
  1386. dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
  1387. "resume", status);
  1388. if (status != -ESHUTDOWN)
  1389. usb_hc_died(hcd);
  1390. }
  1391. return status;
  1392. }
  1393. /* Workqueue routine for root-hub remote wakeup */
  1394. static void hcd_resume_work(struct work_struct *work)
  1395. {
  1396. struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
  1397. struct usb_device *udev = hcd->self.root_hub;
  1398. usb_lock_device(udev);
  1399. usb_mark_last_busy(udev);
  1400. usb_external_resume_device(udev);
  1401. usb_unlock_device(udev);
  1402. }
  1403. /**
  1404. * usb_hcd_resume_root_hub - called by HCD to resume its root hub
  1405. * @hcd: host controller for this root hub
  1406. *
  1407. * The USB host controller calls this function when its root hub is
  1408. * suspended (with the remote wakeup feature enabled) and a remote
  1409. * wakeup request is received. The routine submits a workqueue request
  1410. * to resume the root hub (that is, manage its downstream ports again).
  1411. */
  1412. void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
  1413. {
  1414. unsigned long flags;
  1415. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1416. if (hcd->rh_registered)
  1417. queue_work(ksuspend_usb_wq, &hcd->wakeup_work);
  1418. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1419. }
  1420. EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
  1421. #endif
  1422. /*-------------------------------------------------------------------------*/
  1423. #ifdef CONFIG_USB_OTG
  1424. /**
  1425. * usb_bus_start_enum - start immediate enumeration (for OTG)
  1426. * @bus: the bus (must use hcd framework)
  1427. * @port_num: 1-based number of port; usually bus->otg_port
  1428. * Context: in_interrupt()
  1429. *
  1430. * Starts enumeration, with an immediate reset followed later by
  1431. * khubd identifying and possibly configuring the device.
  1432. * This is needed by OTG controller drivers, where it helps meet
  1433. * HNP protocol timing requirements for starting a port reset.
  1434. */
  1435. int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
  1436. {
  1437. struct usb_hcd *hcd;
  1438. int status = -EOPNOTSUPP;
  1439. /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
  1440. * boards with root hubs hooked up to internal devices (instead of
  1441. * just the OTG port) may need more attention to resetting...
  1442. */
  1443. hcd = container_of (bus, struct usb_hcd, self);
  1444. if (port_num && hcd->driver->start_port_reset)
  1445. status = hcd->driver->start_port_reset(hcd, port_num);
  1446. /* run khubd shortly after (first) root port reset finishes;
  1447. * it may issue others, until at least 50 msecs have passed.
  1448. */
  1449. if (status == 0)
  1450. mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
  1451. return status;
  1452. }
  1453. EXPORT_SYMBOL_GPL(usb_bus_start_enum);
  1454. #endif
  1455. /*-------------------------------------------------------------------------*/
  1456. /**
  1457. * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
  1458. * @irq: the IRQ being raised
  1459. * @__hcd: pointer to the HCD whose IRQ is being signaled
  1460. *
  1461. * If the controller isn't HALTed, calls the driver's irq handler.
  1462. * Checks whether the controller is now dead.
  1463. */
  1464. irqreturn_t usb_hcd_irq (int irq, void *__hcd)
  1465. {
  1466. struct usb_hcd *hcd = __hcd;
  1467. unsigned long flags;
  1468. irqreturn_t rc;
  1469. /* IRQF_DISABLED doesn't work correctly with shared IRQs
  1470. * when the first handler doesn't use it. So let's just
  1471. * assume it's never used.
  1472. */
  1473. local_irq_save(flags);
  1474. if (unlikely(hcd->state == HC_STATE_HALT ||
  1475. !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
  1476. rc = IRQ_NONE;
  1477. } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
  1478. rc = IRQ_NONE;
  1479. } else {
  1480. set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  1481. if (unlikely(hcd->state == HC_STATE_HALT))
  1482. usb_hc_died(hcd);
  1483. rc = IRQ_HANDLED;
  1484. }
  1485. local_irq_restore(flags);
  1486. return rc;
  1487. }
  1488. /*-------------------------------------------------------------------------*/
  1489. /**
  1490. * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
  1491. * @hcd: pointer to the HCD representing the controller
  1492. *
  1493. * This is called by bus glue to report a USB host controller that died
  1494. * while operations may still have been pending. It's called automatically
  1495. * by the PCI glue, so only glue for non-PCI busses should need to call it.
  1496. */
  1497. void usb_hc_died (struct usb_hcd *hcd)
  1498. {
  1499. unsigned long flags;
  1500. dev_err (hcd->self.controller, "HC died; cleaning up\n");
  1501. spin_lock_irqsave (&hcd_root_hub_lock, flags);
  1502. if (hcd->rh_registered) {
  1503. hcd->poll_rh = 0;
  1504. /* make khubd clean up old urbs and devices */
  1505. usb_set_device_state (hcd->self.root_hub,
  1506. USB_STATE_NOTATTACHED);
  1507. usb_kick_khubd (hcd->self.root_hub);
  1508. }
  1509. spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
  1510. }
  1511. EXPORT_SYMBOL_GPL (usb_hc_died);
  1512. /*-------------------------------------------------------------------------*/
  1513. /**
  1514. * usb_create_hcd - create and initialize an HCD structure
  1515. * @driver: HC driver that will use this hcd
  1516. * @dev: device for this HC, stored in hcd->self.controller
  1517. * @bus_name: value to store in hcd->self.bus_name
  1518. * Context: !in_interrupt()
  1519. *
  1520. * Allocate a struct usb_hcd, with extra space at the end for the
  1521. * HC driver's private data. Initialize the generic members of the
  1522. * hcd structure.
  1523. *
  1524. * If memory is unavailable, returns NULL.
  1525. */
  1526. struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
  1527. struct device *dev, const char *bus_name)
  1528. {
  1529. struct usb_hcd *hcd;
  1530. hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
  1531. if (!hcd) {
  1532. dev_dbg (dev, "hcd alloc failed\n");
  1533. return NULL;
  1534. }
  1535. dev_set_drvdata(dev, hcd);
  1536. kref_init(&hcd->kref);
  1537. usb_bus_init(&hcd->self);
  1538. hcd->self.controller = dev;
  1539. hcd->self.bus_name = bus_name;
  1540. hcd->self.uses_dma = (dev->dma_mask != NULL);
  1541. init_timer(&hcd->rh_timer);
  1542. hcd->rh_timer.function = rh_timer_func;
  1543. hcd->rh_timer.data = (unsigned long) hcd;
  1544. #ifdef CONFIG_PM
  1545. INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
  1546. #endif
  1547. hcd->driver = driver;
  1548. hcd->product_desc = (driver->product_desc) ? driver->product_desc :
  1549. "USB Host Controller";
  1550. return hcd;
  1551. }
  1552. EXPORT_SYMBOL_GPL(usb_create_hcd);
  1553. static void hcd_release (struct kref *kref)
  1554. {
  1555. struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
  1556. kfree(hcd);
  1557. }
  1558. struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
  1559. {
  1560. if (hcd)
  1561. kref_get (&hcd->kref);
  1562. return hcd;
  1563. }
  1564. EXPORT_SYMBOL_GPL(usb_get_hcd);
  1565. void usb_put_hcd (struct usb_hcd *hcd)
  1566. {
  1567. if (hcd)
  1568. kref_put (&hcd->kref, hcd_release);
  1569. }
  1570. EXPORT_SYMBOL_GPL(usb_put_hcd);
  1571. /**
  1572. * usb_add_hcd - finish generic HCD structure initialization and register
  1573. * @hcd: the usb_hcd structure to initialize
  1574. * @irqnum: Interrupt line to allocate
  1575. * @irqflags: Interrupt type flags
  1576. *
  1577. * Finish the remaining parts of generic HCD initialization: allocate the
  1578. * buffers of consistent memory, register the bus, request the IRQ line,
  1579. * and call the driver's reset() and start() routines.
  1580. */
  1581. int usb_add_hcd(struct usb_hcd *hcd,
  1582. unsigned int irqnum, unsigned long irqflags)
  1583. {
  1584. int retval;
  1585. struct usb_device *rhdev;
  1586. dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
  1587. hcd->authorized_default = hcd->wireless? 0 : 1;
  1588. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1589. /* HC is in reset state, but accessible. Now do the one-time init,
  1590. * bottom up so that hcds can customize the root hubs before khubd
  1591. * starts talking to them. (Note, bus id is assigned early too.)
  1592. */
  1593. if ((retval = hcd_buffer_create(hcd)) != 0) {
  1594. dev_dbg(hcd->self.controller, "pool alloc failed\n");
  1595. return retval;
  1596. }
  1597. if ((retval = usb_register_bus(&hcd->self)) < 0)
  1598. goto err_register_bus;
  1599. if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
  1600. dev_err(hcd->self.controller, "unable to allocate root hub\n");
  1601. retval = -ENOMEM;
  1602. goto err_allocate_root_hub;
  1603. }
  1604. rhdev->speed = (hcd->driver->flags & HCD_USB2) ? USB_SPEED_HIGH :
  1605. USB_SPEED_FULL;
  1606. hcd->self.root_hub = rhdev;
  1607. /* wakeup flag init defaults to "everything works" for root hubs,
  1608. * but drivers can override it in reset() if needed, along with
  1609. * recording the overall controller's system wakeup capability.
  1610. */
  1611. device_init_wakeup(&rhdev->dev, 1);
  1612. /* "reset" is misnamed; its role is now one-time init. the controller
  1613. * should already have been reset (and boot firmware kicked off etc).
  1614. */
  1615. if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
  1616. dev_err(hcd->self.controller, "can't setup\n");
  1617. goto err_hcd_driver_setup;
  1618. }
  1619. /* NOTE: root hub and controller capabilities may not be the same */
  1620. if (device_can_wakeup(hcd->self.controller)
  1621. && device_can_wakeup(&hcd->self.root_hub->dev))
  1622. dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
  1623. /* enable irqs just before we start the controller */
  1624. if (hcd->driver->irq) {
  1625. /* IRQF_DISABLED doesn't work as advertised when used together
  1626. * with IRQF_SHARED. As usb_hcd_irq() will always disable
  1627. * interrupts we can remove it here.
  1628. */
  1629. irqflags &= ~IRQF_DISABLED;
  1630. snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
  1631. hcd->driver->description, hcd->self.busnum);
  1632. if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
  1633. hcd->irq_descr, hcd)) != 0) {
  1634. dev_err(hcd->self.controller,
  1635. "request interrupt %d failed\n", irqnum);
  1636. goto err_request_irq;
  1637. }
  1638. hcd->irq = irqnum;
  1639. dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
  1640. (hcd->driver->flags & HCD_MEMORY) ?
  1641. "io mem" : "io base",
  1642. (unsigned long long)hcd->rsrc_start);
  1643. } else {
  1644. hcd->irq = -1;
  1645. if (hcd->rsrc_start)
  1646. dev_info(hcd->self.controller, "%s 0x%08llx\n",
  1647. (hcd->driver->flags & HCD_MEMORY) ?
  1648. "io mem" : "io base",
  1649. (unsigned long long)hcd->rsrc_start);
  1650. }
  1651. if ((retval = hcd->driver->start(hcd)) < 0) {
  1652. dev_err(hcd->self.controller, "startup error %d\n", retval);
  1653. goto err_hcd_driver_start;
  1654. }
  1655. /* starting here, usbcore will pay attention to this root hub */
  1656. rhdev->bus_mA = min(500u, hcd->power_budget);
  1657. if ((retval = register_root_hub(hcd)) != 0)
  1658. goto err_register_root_hub;
  1659. retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
  1660. if (retval < 0) {
  1661. printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
  1662. retval);
  1663. goto error_create_attr_group;
  1664. }
  1665. if (hcd->uses_new_polling && hcd->poll_rh)
  1666. usb_hcd_poll_rh_status(hcd);
  1667. return retval;
  1668. error_create_attr_group:
  1669. mutex_lock(&usb_bus_list_lock);
  1670. usb_disconnect(&hcd->self.root_hub);
  1671. mutex_unlock(&usb_bus_list_lock);
  1672. err_register_root_hub:
  1673. hcd->driver->stop(hcd);
  1674. err_hcd_driver_start:
  1675. if (hcd->irq >= 0)
  1676. free_irq(irqnum, hcd);
  1677. err_request_irq:
  1678. err_hcd_driver_setup:
  1679. hcd->self.root_hub = NULL;
  1680. usb_put_dev(rhdev);
  1681. err_allocate_root_hub:
  1682. usb_deregister_bus(&hcd->self);
  1683. err_register_bus:
  1684. hcd_buffer_destroy(hcd);
  1685. return retval;
  1686. }
  1687. EXPORT_SYMBOL_GPL(usb_add_hcd);
  1688. /**
  1689. * usb_remove_hcd - shutdown processing for generic HCDs
  1690. * @hcd: the usb_hcd structure to remove
  1691. * Context: !in_interrupt()
  1692. *
  1693. * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
  1694. * invoking the HCD's stop() method.
  1695. */
  1696. void usb_remove_hcd(struct usb_hcd *hcd)
  1697. {
  1698. dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
  1699. if (HC_IS_RUNNING (hcd->state))
  1700. hcd->state = HC_STATE_QUIESCING;
  1701. dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
  1702. spin_lock_irq (&hcd_root_hub_lock);
  1703. hcd->rh_registered = 0;
  1704. spin_unlock_irq (&hcd_root_hub_lock);
  1705. #ifdef CONFIG_PM
  1706. cancel_work_sync(&hcd->wakeup_work);
  1707. #endif
  1708. sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
  1709. mutex_lock(&usb_bus_list_lock);
  1710. usb_disconnect(&hcd->self.root_hub);
  1711. mutex_unlock(&usb_bus_list_lock);
  1712. hcd->driver->stop(hcd);
  1713. hcd->state = HC_STATE_HALT;
  1714. hcd->poll_rh = 0;
  1715. del_timer_sync(&hcd->rh_timer);
  1716. if (hcd->irq >= 0)
  1717. free_irq(hcd->irq, hcd);
  1718. usb_deregister_bus(&hcd->self);
  1719. hcd_buffer_destroy(hcd);
  1720. }
  1721. EXPORT_SYMBOL_GPL(usb_remove_hcd);
  1722. void
  1723. usb_hcd_platform_shutdown(struct platform_device* dev)
  1724. {
  1725. struct usb_hcd *hcd = platform_get_drvdata(dev);
  1726. if (hcd->driver->shutdown)
  1727. hcd->driver->shutdown(hcd);
  1728. }
  1729. EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
  1730. /*-------------------------------------------------------------------------*/
  1731. #if defined(CONFIG_USB_MON)
  1732. struct usb_mon_operations *mon_ops;
  1733. /*
  1734. * The registration is unlocked.
  1735. * We do it this way because we do not want to lock in hot paths.
  1736. *
  1737. * Notice that the code is minimally error-proof. Because usbmon needs
  1738. * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
  1739. */
  1740. int usb_mon_register (struct usb_mon_operations *ops)
  1741. {
  1742. if (mon_ops)
  1743. return -EBUSY;
  1744. mon_ops = ops;
  1745. mb();
  1746. return 0;
  1747. }
  1748. EXPORT_SYMBOL_GPL (usb_mon_register);
  1749. void usb_mon_deregister (void)
  1750. {
  1751. if (mon_ops == NULL) {
  1752. printk(KERN_ERR "USB: monitor was not registered\n");
  1753. return;
  1754. }
  1755. mon_ops = NULL;
  1756. mb();
  1757. }
  1758. EXPORT_SYMBOL_GPL (usb_mon_deregister);
  1759. #endif /* CONFIG_USB_MON */